diff options
Diffstat (limited to 'hicn-light/src/hicn/core/name.c')
-rw-r--r-- | hicn-light/src/hicn/core/name.c | 236 |
1 files changed, 84 insertions, 152 deletions
diff --git a/hicn-light/src/hicn/core/name.c b/hicn-light/src/hicn/core/name.c index b4a5e8d1b..8886cc929 100644 --- a/hicn-light/src/hicn/core/name.c +++ b/hicn-light/src/hicn/core/name.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -13,201 +13,133 @@ * limitations under the License. */ +#include <assert.h> #include <limits.h> #include <hicn/hicn-light/config.h> #include <stdbool.h> #include <stdio.h> #include <string.h> -#include <parc/algol/parc_BufferComposer.h> -#include <parc/algol/parc_Hash.h> -#include <parc/algol/parc_Memory.h> - +#include <hicn/common.h> // cumulative_hash32 #include <hicn/core/messageHandler.h> #include <hicn/core/name.h> - -#include <parc/algol/parc_Hash.h> - -#include <parc/assert/parc_Assert.h> +#include <hicn/util/log.h> +#include <hicn/base/hash.h> #define IPv6_TYPE 6 #define IPv4_TYPE 4 -// assumption: the IPv6 address is the name, the TCP segment number is the ICN -// segment - -struct name { - NameBitvector *content_name; - uint32_t segment; - uint32_t name_hash; - // the refcount is shared between all copies - unsigned *refCountPtr; -}; - -// ===================================================== - -static unsigned _getRefCount(const Name *name) { return *name->refCountPtr; } - -static void _incrementRefCount(Name *name) { - parcAssertTrue(*name->refCountPtr > 0, - "Illegal State: Trying to increment a 0 refcount!"); - (*name->refCountPtr)++; -} - -static void _decrementRefCount(Name *name) { - parcAssertTrue(*name->refCountPtr > 0, - "Illegal State: Trying to decrement a 0 refcount!"); - (*name->refCountPtr)--; -} - static uint32_t _computeHash(Name *name) { - parcAssertNotNull(name, "Parameter must be non-null pointer"); + assert(name); - uint32_t hash1 = nameBitvector_GetHash32(name->content_name); - return parcHash32_Data_Cumulative((const uint8_t *)&name->segment, 4, hash1); + uint32_t hash1 = nameBitvector_GetHash32(&(name->content_name)); + return hashlittle(&name->segment, sizeof(name->segment), hash1); } // ============================================================================ -Name *name_CreateFromPacket(const uint8_t *packet, MessagePacketType type) { - Name *name = parcMemory_AllocateAndClear(sizeof(Name)); - parcAssertNotNull(name, "parcMemory_AllocateAndClear(%zu) returned NULL", - sizeof(Name)); +void name_create_from_interest(const uint8_t *packet, Name *name) { + assert(packet); + assert(name); if (messageHandler_GetIPPacketType(packet) == IPv6_TYPE) { - if (type == MessagePacketType_Interest) { - name->content_name = nameBitvector_CreateFromIn6Addr( - (struct in6_addr *)messageHandler_GetDestination(packet), 128); - } else if (type == MessagePacketType_ContentObject) { - name->content_name = nameBitvector_CreateFromIn6Addr( - (struct in6_addr *)messageHandler_GetSource(packet), 128); - } else { - parcMemory_Deallocate((void **)&name); - return NULL; - } + nameBitvector_CreateFromIn6Addr( + &(name->content_name), + (struct in6_addr *)messageHandler_GetDestination(packet), 128); } else if (messageHandler_GetIPPacketType(packet) == IPv4_TYPE) { - if (type == MessagePacketType_Interest) { - name->content_name = nameBitvector_CreateFromInAddr( - *((uint32_t *)messageHandler_GetDestination(packet)), 32); - } else if (type == MessagePacketType_ContentObject) { - name->content_name = nameBitvector_CreateFromInAddr( - *((uint32_t *)messageHandler_GetSource(packet)), 32); - } else { - parcMemory_Deallocate((void **)&name); - return NULL; - } + nameBitvector_CreateFromInAddr( + &(name->content_name), + *((uint32_t *)messageHandler_GetDestination(packet)), 32); } else { - printf("Error: unknown message type\n"); - parcMemory_Deallocate((void **)&name); - return NULL; + ERROR("Error: unknown message type\n"); + return; } name->segment = messageHandler_GetSegment(packet); name->name_hash = _computeHash(name); - - name->refCountPtr = parcMemory_Allocate(sizeof(unsigned)); - parcAssertNotNull(name->refCountPtr, "parcMemory_Allocate(%zu) returned NULL", - sizeof(unsigned)); - *name->refCountPtr = 1; - return name; } -Name *name_CreateFromAddress(address_type addressType, ip_address_t addr, - uint8_t len) { - Name *name = parcMemory_AllocateAndClear(sizeof(Name)); - parcAssertNotNull(name, "parcMemory_AllocateAndClear(%zu) returned NULL", - sizeof(Name)); - if (addressType == ADDR_INET) { - name->content_name = nameBitvector_CreateFromInAddr(addr.v4.as_u32, len); - } else if (addressType == ADDR_INET6) { - name->content_name = nameBitvector_CreateFromIn6Addr(&addr.v6.as_in6addr, len); +void name_create_from_data(const uint8_t *packet, Name *name) { + assert(packet); + assert(name); + + if (messageHandler_GetIPPacketType(packet) == IPv6_TYPE) { + nameBitvector_CreateFromIn6Addr( + &(name->content_name), + (struct in6_addr *)messageHandler_GetSource(packet), 128); + } else if (messageHandler_GetIPPacketType(packet) == IPv4_TYPE) { + nameBitvector_CreateFromInAddr( + &(name->content_name), *((uint32_t *)messageHandler_GetSource(packet)), + 32); } else { - parcTrapNotImplemented("Unkown packet type"); + printf("Error: unknown message type\n"); + return; } - name->segment = 0; + name->segment = messageHandler_GetSegment(packet); name->name_hash = _computeHash(name); - - name->refCountPtr = parcMemory_Allocate(sizeof(unsigned)); - parcAssertNotNull(name->refCountPtr, "parcMemory_Allocate(%zu) returned NULL", - sizeof(unsigned)); - *name->refCountPtr = 1; - - return name; } -void name_Release(Name **namePtr) { - parcAssertNotNull(namePtr, "Parameter must be non-null double pointer"); - parcAssertNotNull(*namePtr, "Parameter must dereference to non-null pointer"); - - Name *name = *namePtr; - _decrementRefCount(name); - if (_getRefCount(name) == 0) { - parcMemory_Deallocate((void **)&(name->refCountPtr)); - nameBitvector_Destroy(&(name->content_name)); +void name_CreateFromAddress(Name *name, int family, ip_address_t addr, + uint8_t len) { + assert(name); + + switch (family) { + case AF_INET: + nameBitvector_CreateFromInAddr(&(name->content_name), addr.v4.as_u32, + len); + break; + case AF_INET6: + nameBitvector_CreateFromIn6Addr(&(name->content_name), + &addr.v6.as_in6addr, len); + break; + default: + return; } - parcMemory_Deallocate((void **)&name); - *namePtr = NULL; -} - -Name *name_Acquire(const Name *original) { - parcAssertNotNull(original, "Parameter must be non-null"); - Name *copy = parcMemory_AllocateAndClear(sizeof(Name)); - parcAssertNotNull(copy, "parcMemory_AllocateAndClear(%zu) returned NULL", - sizeof(Name)); - memcpy(copy, original, sizeof(Name)); - _incrementRefCount(copy); - - return copy; + name->segment = 0; + name->name_hash = _computeHash(name); } -Name *name_Copy(const Name *original) { - parcAssertNotNull(original, "Parameter must be non-null"); - Name *copy = parcMemory_AllocateAndClear(sizeof(Name)); - parcAssertNotNull(copy, "parcMemory_AllocateAndClear(%zu) returned NULL", - sizeof(Name)); +void name_Copy(const Name *original, Name *copy) { + assert(original); + assert(copy); - copy->content_name = nameBitvector_Copy(original->content_name); + nameBitvector_Copy(&(original->content_name), &(copy->content_name)); copy->segment = original->segment; copy->name_hash = original->name_hash; - - copy->refCountPtr = parcMemory_Allocate(sizeof(unsigned)); - parcAssertNotNull(copy->refCountPtr, "parcMemory_Allocate(%zu) returned NULL", - sizeof(unsigned)); - *copy->refCountPtr = 1; - - return copy; } uint32_t name_HashCode(const Name *name) { - parcAssertNotNull(name, "Parameter must be non-null"); + assert(name); return name->name_hash; } NameBitvector *name_GetContentName(const Name *name) { - parcAssertNotNull(name, "Parameter must be non-null"); - return name->content_name; + assert(name); + return (NameBitvector *)&(name->content_name); } -bool name_Equals(const Name *a, const Name *b) { - parcAssertNotNull(a, "Parameter a must be non-null"); - parcAssertNotNull(b, "Parameter b must be non-null"); +uint32_t name_GetSegment(const Name *name) { + assert(name); + return name->segment; +} - /* BEGIN: Workaround for HICN-400 */ - if ((!a->content_name) || (!b->content_name)) - return false; - /* END: Workaround for HICN-400 */ +void name_SetSegment(Name *name, uint32_t segment) { name->segment = segment; } - if ((nameBitvector_Equals(a->content_name, b->content_name) && +bool name_Equals(const Name *a, const Name *b) { + assert(a); + assert(b); + + if ((nameBitvector_Equals(&(a->content_name), &(b->content_name)) && a->segment == b->segment)) return true; return false; } int name_Compare(const Name *a, const Name *b) { - parcAssertNotNull(a, "Parameter a must be non-null"); - parcAssertNotNull(b, "Parameter b must be non-null"); + assert(a); + assert(b); if (a == NULL && b == NULL) { return 0; @@ -219,7 +151,7 @@ int name_Compare(const Name *a, const Name *b) { return +1; } - int res = nameBitvector_Compare(a->content_name, b->content_name); + int res = nameBitvector_Compare(&(a->content_name), &(b->content_name)); if (res != 0) { return res; @@ -235,30 +167,30 @@ int name_Compare(const Name *a, const Name *b) { } char *name_ToString(const Name *name) { - char *output = malloc(128); + char *output = malloc(NI_MAXHOST * 2); + address_t address; + nameBitvector_ToAddress(name_GetContentName(name), &address); - Address *packetAddr = nameBitvector_ToAddress(name_GetContentName(name)); + char addr_str[NI_MAXHOST]; + int err = address_to_string(&address, addr_str, NULL); + _ASSERT(!err); - char * address_str = addressToString(packetAddr); - sprintf(output, "name: %s seq: %u", address_str, name->segment); - parcMemory_Deallocate((void **)&address_str); - - addressDestroy(&packetAddr); + int chars_written = + snprintf(output, NI_MAXHOST * 2, "name=%s|%u", addr_str, name->segment); + _ASSERT(chars_written > 0); return output; } void name_setLen(Name *name, uint8_t len) { - nameBitvector_setLen(name->content_name, len); + nameBitvector_setLen(&(name->content_name), len); name->name_hash = _computeHash(name); } #ifdef WITH_POLICY -uint32_t name_GetSuffix(const Name * name) { - return name->segment; -} +uint32_t name_GetSuffix(const Name *name) { return name->segment; } -uint8_t name_GetLen(const Name * name) { - return nameBitvector_GetLength(name->content_name); +uint8_t name_GetLen(const Name *name) { + return nameBitvector_GetLength(&(name->content_name)); } #endif /* WITH_POLICY */ |