diff options
Diffstat (limited to 'libtransport/src/core/name.cc')
-rw-r--r-- | libtransport/src/core/name.cc | 41 |
1 files changed, 33 insertions, 8 deletions
diff --git a/libtransport/src/core/name.cc b/libtransport/src/core/name.cc index 3455460fc..811e93b87 100644 --- a/libtransport/src/core/name.cc +++ b/libtransport/src/core/name.cc @@ -13,13 +13,14 @@ * limitations under the License. */ -#include <core/manifest_format.h> #include <hicn/transport/core/name.h> #include <hicn/transport/errors/errors.h> #include <hicn/transport/errors/tokenizer_exception.h> #include <hicn/transport/utils/hash.h> #include <hicn/transport/utils/string_tokenizer.h> +#include <core/manifest_format.h> + namespace transport { namespace core { @@ -28,19 +29,28 @@ Name::Name() { name_ = {}; } Name::Name(int family, const uint8_t *ip_address, std::uint32_t suffix) : name_({}) { + name_.type = HNT_UNSPEC; + std::size_t length; + uint8_t *dst = NULL; + if (family == AF_INET) { - name_.prefix.ip4.as_u32 = *(u32 *)(ip_address); + dst = name_.ip4.prefix_as_u8; + length = IPV4_ADDR_LEN; + name_.type = HNT_CONTIGUOUS_V4; } else if (family == AF_INET6) { - std::memcpy(&name_.prefix.ip6.as_u64[0], ip_address, IPV6_ADDR_LEN); + dst = name_.ip6.prefix_as_u8; + length = IPV6_ADDR_LEN; + name_.type = HNT_CONTIGUOUS_V6; } else { throw errors::RuntimeException("Specified name family does not exist."); } - name_.suffix = suffix; + std::memcpy(dst, ip_address, length); + *reinterpret_cast<std::uint32_t *>(dst + length) = suffix; } Name::Name(const char *name, uint32_t segment) { - name_ = {}; + name_.type = HNT_UNSPEC; if (hicn_name_create(name, segment, &name_) < 0) { throw errors::InvalidIpAddressException(); } @@ -50,7 +60,7 @@ Name::Name(const std::string &uri, uint32_t segment) : Name(uri.c_str(), segment) {} Name::Name(const std::string &uri) { - name_ = {}; + name_.type = HNT_UNSPEC; utils::StringTokenizer tokenizer(uri, "|"); std::string ip_address; std::string seq_number; @@ -115,7 +125,9 @@ uint32_t Name::getHash32(bool consider_suffix) const { return hash; } -void Name::clear() { name_ = {}; }; +void Name::clear() { name_.type = HNT_UNSPEC; }; + +Name::Type Name::getType() const { return name_.type; } uint32_t Name::getSuffix() const { uint32_t ret = 0; @@ -136,7 +148,20 @@ Name &Name::setSuffix(uint32_t seq_number) { } std::shared_ptr<Sockaddr> Name::getAddress() const { - Sockaddr *ret = (Sockaddr *)(new sockaddr_storage()); + Sockaddr *ret = nullptr; + + switch (name_.type) { + case HNT_CONTIGUOUS_V4: + case HNT_IOV_V4: + ret = (Sockaddr *)new Sockaddr4; + break; + case HNT_CONTIGUOUS_V6: + case HNT_IOV_V6: + ret = (Sockaddr *)new Sockaddr6; + break; + default: + throw errors::MalformedNameException(); + } if (hicn_name_to_sockaddr_address((hicn_name_t *)&name_, ret) < 0) { throw errors::MalformedNameException(); |