diff options
Diffstat (limited to 'hicn-light')
38 files changed, 500 insertions, 605 deletions
diff --git a/hicn-light/CMakeLists.txt b/hicn-light/CMakeLists.txt index 39fce8c1f..1e5805d68 100644 --- a/hicn-light/CMakeLists.txt +++ b/hicn-light/CMakeLists.txt @@ -39,8 +39,9 @@ if(NOT WIN32) else () set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4996") endif () + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g") -if(ANDROID_API) +if(${CMAKE_SYSTEM_NAME} STREQUAL "Android") message("############ Detected cross compile for $ENV{CMAKE_SYSTEM_NAME}") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${ANDROID_C_FLAGS}") endif() @@ -58,7 +59,7 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) set(HICN_LIGHT_CONTROL ${HICN_LIGHT}-control) set(HICN_LIGHT_DAEMON ${HICN_LIGHT}-daemon) else() - if (ANDROID_API) + if (${CMAKE_SYSTEM_NAME} STREQUAL "Android") set(HICN_LIBRARIES ${LIBHICN_STATIC}) list(APPEND DEPENDENCIES ${LIBHICN_STATIC} diff --git a/hicn-light/src/hicn/CMakeLists.txt b/hicn-light/src/hicn/CMakeLists.txt index 9129cba17..64b0ff2a0 100644 --- a/hicn-light/src/hicn/CMakeLists.txt +++ b/hicn-light/src/hicn/CMakeLists.txt @@ -14,8 +14,7 @@ list(APPEND COMPILER_DEFINITIONS "-DWITH_POLICY" ) - -if(NOT ANDROID_API AND NOT COMPILE_FOR_IOS) +if(NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Android" AND NOT COMPILE_FOR_IOS) add_subdirectory(command_line) endif () @@ -43,7 +42,7 @@ list(APPEND TO_INSTALL_HEADER_FILES ${CMAKE_CURRENT_BINARY_DIR}/hicn-light/config.h ) -if (ANDROID_API) +if (${CMAKE_SYSTEM_NAME} STREQUAL "Android") build_library(${LIBHICN_LIGHT} STATIC SOURCES ${SOURCE_FILES} diff --git a/hicn-light/src/hicn/config/configuration.c b/hicn-light/src/hicn/config/configuration.c index 182c2fdc1..b14ea551a 100644 --- a/hicn-light/src/hicn/config/configuration.c +++ b/hicn-light/src/hicn/config/configuration.c @@ -363,30 +363,32 @@ struct iovec *configuration_ProcessCreateTunnel(Configuration *config, Address *source = NULL; Address *destination = NULL; - if (!symbolicNameTable_Exists(config->symbolicNameTable, symbolicName)) { - if (control->ipType == ADDR_INET) { - source = - addressFromInaddr4Port(&control->localIp.ipv4, &control->localPort); - destination = - addressFromInaddr4Port(&control->remoteIp.ipv4, &control->remotePort); - } else if (control->ipType == ADDR_INET6) { - source = - addressFromInaddr6Port(&control->localIp.ipv6, &control->localPort); - destination = - addressFromInaddr6Port(&control->remoteIp.ipv6, &control->remotePort); - } else { - printf("Invalid IP type.\n"); // will generate a Nack - } - - AddressPair *pair = addressPair_Create(source, destination); - conn = (Connection *)connectionTable_FindByAddressPair( - forwarder_GetConnectionTable(config->forwarder), pair); + if (symbolicNameTable_Exists(config->symbolicNameTable, symbolicName)) { + logger_Log(config->logger, LoggerFacility_Config, PARCLogLevel_Error, + __func__, "Listener symbolic name already exists"); + goto ERR; + } - addressPair_Release(&pair); + if (control->ipType == ADDR_INET) { + source = + addressFromInaddr4Port(&control->localIp.ipv4, &control->localPort); + destination = + addressFromInaddr4Port(&control->remoteIp.ipv4, &control->remotePort); + } else if (control->ipType == ADDR_INET6) { + source = + addressFromInaddr6Port(&control->localIp.ipv6, &control->localPort); + destination = + addressFromInaddr6Port(&control->remoteIp.ipv6, &control->remotePort); } else { - conn = NULL; + printf("Invalid IP type.\n"); // will generate a Nack } + AddressPair *pair = addressPair_Create(source, destination); + conn = (Connection *)connectionTable_FindByAddressPair( + forwarder_GetConnectionTable(config->forwarder), pair); + + addressPair_Release(&pair); + if (!conn) { IoOperations *ops = NULL; switch (control->connectionType) { @@ -460,11 +462,14 @@ struct iovec *configuration_ProcessCreateTunnel(Configuration *config, if (destination) addressDestroy(&destination); - if (success) { // ACK - return utils_CreateAck(header, control, sizeof(add_connection_command)); - } else { // NACK + if (!success) + goto ERR; + + // ACK + return utils_CreateAck(header, control, sizeof(add_connection_command)); + +ERR: return utils_CreateNack(header, control, sizeof(add_connection_command)); - } } /** @@ -577,6 +582,13 @@ struct iovec *configuration_ProcessRemoveTunnel(Configuration *config, return response; } +void _strlwr(char *string) { + char *p = string; + while ((*p = tolower(*p))) { + p++; + } +} + struct iovec *configuration_ProcessConnectionList(Configuration *config, struct iovec *request) { ConnectionTable *table = forwarder_GetConnectionTable(config->forwarder); @@ -603,8 +615,17 @@ struct iovec *configuration_ProcessConnectionList(Configuration *config, // set structure fields listConnectionsCommand->connid = connection_GetConnectionId(original); + + const char *connectionName = symbolicNameTable_GetNameByIndex(config->symbolicNameTable, connection_GetConnectionId(original)); + snprintf(listConnectionsCommand->connectionName, 16, "%s", connectionName); + _strlwr(listConnectionsCommand->connectionName); + + snprintf(listConnectionsCommand->interfaceName, 16, "%s", ioOperations_GetInterfaceName(connection_GetIoOperations(original))); + listConnectionsCommand->state = connection_IsUp(original) ? IFACE_UP : IFACE_DOWN; + listConnectionsCommand->admin_state = + (connection_GetAdminState(original) == CONNECTION_STATE_UP) ? IFACE_UP : IFACE_DOWN; listConnectionsCommand->connectionData.connectionType = ioOperations_GetConnectionType(connection_GetIoOperations(original)); @@ -707,6 +728,14 @@ struct iovec *configuration_ProcessListenersList(Configuration *config, listListenersCommand->address.ipv6 = tmpAddr6.sin6_addr; listListenersCommand->port = tmpAddr6.sin6_port; } + + const char * listenerName = listenerEntry->getListenerName(listenerEntry); + snprintf(listListenersCommand->listenerName, 16, "%s", listenerName); + if (listenerEntry->getEncapType(listenerEntry) == ENCAP_TCP || + listenerEntry->getEncapType(listenerEntry) == ENCAP_UDP) { + const char * interfaceName = listenerEntry->getInterfaceName(listenerEntry); + snprintf(listListenersCommand->interfaceName, 16, "%s", interfaceName); + } } // send response diff --git a/hicn-light/src/hicn/config/configurationListeners.c b/hicn-light/src/hicn/config/configurationListeners.c index 97e7dbb87..86d8a215a 100644 --- a/hicn-light/src/hicn/config/configurationListeners.c +++ b/hicn-light/src/hicn/config/configurationListeners.c @@ -211,32 +211,21 @@ static bool _addEther(Configuration *config, add_listener_command *control, return false; } -#ifdef __linux__ /* * Create a new IPV4/TCP listener. * * @param [in,out] forwarder The hicn-light forwarder instance + * @param [in] listenerName The name of the listener * @param [in] addr4 The ipv4 address in network byte order * @param [in] port The port number in network byte order * @param [in] interfaceName The name of the interface to bind the socket * * return true if success, false otherwise */ -static bool _setupTcpListenerOnInet(Forwarder *forwarder, ipv4_addr_t *addr4, - uint16_t *port, const char *interfaceName) { -#else -/* - * Create a new IPV4/TCP listener. - * - * @param [in,out] forwarder The hicn-light forwarder instance - * @param [in] addr4 The ipv4 address in network byte order - * @param [in] port The port number in network byte order - * - * return true if success, false otherwise - */ -static bool _setupTcpListenerOnInet(Forwarder *forwarder, ipv4_addr_t *addr4, - uint16_t *port) { -#endif +static bool _setupTcpListenerOnInet(Forwarder *forwarder, char *listenerName, ipv4_addr_t *addr4, + uint16_t *port, char *interfaceName) { + parcAssertNotNull(listenerName, "Parameter listenerName must be non-null"); + bool success = false; struct sockaddr_in addr; @@ -245,42 +234,31 @@ static bool _setupTcpListenerOnInet(Forwarder *forwarder, ipv4_addr_t *addr4, addr.sin_port = *port; addr.sin_addr.s_addr = *addr4; - ListenerOps *ops = tcpListener_CreateInet(forwarder, addr); + ListenerOps *ops = tcpListener_CreateInet(forwarder, listenerName, addr, interfaceName); if (ops) { success = listenerSet_Add(forwarder_GetListenerSet(forwarder), ops); +#if 0 parcAssertTrue(success, "Failed to add TCP listener on %s to ListenerSet", addressToString(ops->getListenAddress(ops))); +#endif } return success; } -#ifdef __linux__ /* * Create a new IPV4/UDP listener. * * @param [in,out] forwarder The hicn-light forwarder instance + * @param [in] listenerName The name of the listener * @param [in] addr4 The ipv4 address in network byte order * @param [in] port The port number in network byte order * @param [in] interfaceName The name of the interface to bind the socket * * return true if success, false otherwise */ -static bool _setupUdpListenerOnInet(Forwarder *forwarder, ipv4_addr_t *addr4, +static bool _setupUdpListenerOnInet(Forwarder *forwarder, char *listenerName, ipv4_addr_t *addr4, uint16_t *port, char *interfaceName) { -#else -/* - * Create a new IPV4/UDP listener. - * - * @param [in,out] forwarder The hicn-light forwarder instance - * @param [in] addr4 The ipv4 address in network byte order - * @param [in] port The port number in network byte order - * - * return true if success, false otherwise - */ -static bool _setupUdpListenerOnInet(Forwarder *forwarder, ipv4_addr_t *addr4, - uint16_t *port) { -#endif bool success = false; struct sockaddr_in addr; @@ -289,21 +267,18 @@ static bool _setupUdpListenerOnInet(Forwarder *forwarder, ipv4_addr_t *addr4, addr.sin_port = *port; addr.sin_addr.s_addr = *addr4; -#ifdef __linux__ - ListenerOps *ops = udpListener_CreateInet(forwarder, addr, interfaceName); -#else - ListenerOps *ops = udpListener_CreateInet(forwarder, addr); -#endif + ListenerOps *ops = udpListener_CreateInet(forwarder, listenerName, addr, interfaceName); if (ops) { success = listenerSet_Add(forwarder_GetListenerSet(forwarder), ops); +#if 0 parcAssertTrue(success, "Failed to add UDP listener on %s to ListenerSet", addressToString(ops->getListenAddress(ops))); +#endif } return success; } -#ifdef __linux__ /* * Create a new IPV6/TCP listener. * @@ -314,23 +289,9 @@ static bool _setupUdpListenerOnInet(Forwarder *forwarder, ipv4_addr_t *addr4, * * return true if success, false otherwise */ -static bool _setupTcpListenerOnInet6Light(Forwarder *forwarder, +static bool _setupTcpListenerOnInet6Light(Forwarder *forwarder, char *listenerName, ipv6_addr_t *addr6, uint16_t *port, char *interfaceName, uint32_t scopeId) { -#else -/* - * Create a new IPV6/TCP listener. - * - * @param [in,out] forwarder The hicn-light forwarder instance - * @param [in] addr6 The ipv6 address in network byte order - * @param [in] port The port number in network byte order - * - * return true if success, false otherwise - */ -static bool _setupTcpListenerOnInet6Light(Forwarder *forwarder, - ipv6_addr_t *addr6, uint16_t *port, - uint32_t scopeId) { -#endif bool success = false; struct sockaddr_in6 addr; @@ -340,17 +301,18 @@ static bool _setupTcpListenerOnInet6Light(Forwarder *forwarder, addr.sin6_addr = *addr6; addr.sin6_scope_id = scopeId; - ListenerOps *ops = tcpListener_CreateInet6(forwarder, addr); + ListenerOps *ops = tcpListener_CreateInet6(forwarder, listenerName, addr, interfaceName); if (ops) { success = listenerSet_Add(forwarder_GetListenerSet(forwarder), ops); +#if 0 parcAssertTrue(success, "Failed to add TCP6 listener on %s to ListenerSet", addressToString(ops->getListenAddress(ops))); +#endif } return success; } -#ifdef __linux__ /* * Create a new IPV6/UDP listener. * @@ -361,21 +323,8 @@ static bool _setupTcpListenerOnInet6Light(Forwarder *forwarder, * * return true if success, false otherwise */ -static bool _setupUdpListenerOnInet6Light(Forwarder *forwarder, +static bool _setupUdpListenerOnInet6Light(Forwarder *forwarder, char *listenerName, ipv6_addr_t *addr6, uint16_t *port, char *interfaceName) { -#else -/* - * Create a new IPV6/UDP listener. - * - * @param [in,out] forwarder The hicn-light forwarder instance - * @param [in] addr6 The ipv6 address in network byte order - * @param [in] port The port number in network byte order - * - * return true if success, false otherwise - */ -static bool _setupUdpListenerOnInet6Light(Forwarder *forwarder, - ipv6_addr_t *addr6, uint16_t *port) { -#endif bool success = false; struct sockaddr_in6 addr; @@ -385,15 +334,13 @@ static bool _setupUdpListenerOnInet6Light(Forwarder *forwarder, addr.sin6_addr = *addr6; addr.sin6_scope_id = 0; -#ifdef __linux__ - ListenerOps *ops = udpListener_CreateInet6(forwarder, addr, interfaceName); -#else - ListenerOps *ops = udpListener_CreateInet6(forwarder, addr); -#endif + ListenerOps *ops = udpListener_CreateInet6(forwarder, listenerName, addr, interfaceName); if (ops) { success = listenerSet_Add(forwarder_GetListenerSet(forwarder), ops); +#if 0 parcAssertTrue(success, "Failed to add UDP6 listener on %s to ListenerSet", addressToString(ops->getListenAddress(ops))); +#endif } return success; } @@ -460,56 +407,33 @@ bool _addHicn(Configuration *config, add_listener_command *control, bool _addIP(Configuration *config, add_listener_command *control, unsigned ingressId) { bool success = false; + char *symbolic = control->symbolic; switch (control->addressType) { case ADDR_INET: { -#ifdef __linux__ if (control->connectionType == UDP_CONN) { success = - _setupUdpListenerOnInet(configuration_GetForwarder(config), + _setupUdpListenerOnInet(configuration_GetForwarder(config), symbolic, &control->address.ipv4, &control->port, control->interfaceName); } else if (control->connectionType == TCP_CONN) { success = - _setupTcpListenerOnInet(configuration_GetForwarder(config), + _setupTcpListenerOnInet(configuration_GetForwarder(config), symbolic, &control->address.ipv4, &control->port, control->interfaceName); } -#else - if (control->connectionType == UDP_CONN) { - success = - _setupUdpListenerOnInet(configuration_GetForwarder(config), - &control->address.ipv4, &control->port); - } else if (control->connectionType == TCP_CONN) { - success = - _setupTcpListenerOnInet(configuration_GetForwarder(config), - &control->address.ipv4, &control->port); - } -#endif break; } case ADDR_INET6: { -#ifdef __linux__ if (control->connectionType == UDP_CONN) { success = _setupUdpListenerOnInet6Light( - configuration_GetForwarder(config), &control->address.ipv6, + configuration_GetForwarder(config), symbolic, &control->address.ipv6, &control->port, control->interfaceName); } else if (control->connectionType == TCP_CONN) { success = _setupTcpListenerOnInet6Light( - configuration_GetForwarder(config), &control->address.ipv6, + configuration_GetForwarder(config), symbolic, &control->address.ipv6, &control->port, control->interfaceName, 0); } -#else - if (control->connectionType == UDP_CONN) { - success = _setupUdpListenerOnInet6Light( - configuration_GetForwarder(config), &control->address.ipv6, - &control->port); - } else if (control->connectionType == TCP_CONN) { - success = _setupTcpListenerOnInet6Light( - configuration_GetForwarder(config), &control->address.ipv6, - &control->port, 0); - } -#endif break; } @@ -551,20 +475,25 @@ struct iovec *configurationListeners_Add(Configuration *config, bool success = false; - if (control->listenerMode == ETHER_MODE) { - parcTrapNotImplemented("Add Ethernet Listener is not supported"); - success = _addEther(config, control, ingressId); - // it is a failure - } else if (control->listenerMode == IP_MODE) { - success = _addIP(config, control, ingressId); - } else if (control->listenerMode == HICN_MODE) { - success = _addHicn(config, control, ingressId); - } else { - Logger *logger = configuration_GetLogger(config); - if (logger_IsLoggable(logger, LoggerFacility_Config, - PARCLogLevel_Warning)) { - logger_Log(logger, LoggerFacility_Config, PARCLogLevel_Warning, __func__, - "Unsupported encapsulation mode (ingress id %u)", ingressId); + ListenerSet *listenerSet = forwarder_GetListenerSet(configuration_GetForwarder(config)); + int listenerId = listenerSet_FindIdByListenerName(listenerSet, control->symbolic); + + if (listenerId < 0) { + if (control->listenerMode == ETHER_MODE) { + parcTrapNotImplemented("Add Ethernet Listener is not supported"); + success = _addEther(config, control, ingressId); + // it is a failure + } else if (control->listenerMode == IP_MODE) { + success = _addIP(config, control, ingressId); + } else if (control->listenerMode == HICN_MODE) { + success = _addHicn(config, control, ingressId); + } else { + Logger *logger = configuration_GetLogger(config); + if (logger_IsLoggable(logger, LoggerFacility_Config, + PARCLogLevel_Warning)) { + logger_Log(logger, LoggerFacility_Config, PARCLogLevel_Warning, __func__, + "Unsupported encapsulation mode (ingress id %u)", ingressId); + } } } @@ -619,7 +548,7 @@ struct iovec *configurationListeners_AddPunting(Configuration *config, //=========================== INITIAL LISTENERS ==================== -static void _setupListenersOnAddress(Forwarder *forwarder, +static void _setupListenersOnAddress(Forwarder *forwarder, char *listenerName, const Address *address, uint16_t port, char *interfaceName) { address_type type = addressGetType(address); @@ -627,24 +556,15 @@ static void _setupListenersOnAddress(Forwarder *forwarder, case ADDR_INET: { struct sockaddr_in tmp; addressGetInet(address, &tmp); -#ifdef __linux__ - _setupTcpListenerOnInet(forwarder, &tmp.sin_addr.s_addr, &port, interfaceName); -#else - _setupTcpListenerOnInet(forwarder, &tmp.sin_addr.s_addr, &port); -#endif + _setupTcpListenerOnInet(forwarder, listenerName, &tmp.sin_addr.s_addr, &port, interfaceName); break; } case ADDR_INET6: { struct sockaddr_in6 tmp; addressGetInet6(address, &tmp); -#ifdef __linux__ - _setupTcpListenerOnInet6Light(forwarder, &tmp.sin6_addr, &port, interfaceName, - tmp.sin6_scope_id); -#else - _setupTcpListenerOnInet6Light(forwarder, &tmp.sin6_addr, &port, + _setupTcpListenerOnInet6Light(forwarder, listenerName, &tmp.sin6_addr, &port, interfaceName, tmp.sin6_scope_id); -#endif break; } @@ -674,8 +594,14 @@ void configurationListeners_SetupAll(const Configuration *config, uint16_t port, const Address *address = addressListGetItem(addresses, j); // Do not start on link address + char listenerName[16]; +#ifdef __ANDROID__ + snprintf(listenerName, 16, "local_%zu", i); +#else + snprintf(listenerName, 16, "local_%ld", i); +#endif if (addressGetType(address) != ADDR_LINK) { - _setupListenersOnAddress(forwarder, address, port, + _setupListenersOnAddress(forwarder, listenerName, address, port, (char *)interfaceGetName(iface)); } } @@ -689,16 +615,11 @@ void configurationListeners_SetutpLocalIPv4(const Configuration *config, Forwarder *forwarder = configuration_GetForwarder(config); in_addr_t addr = inet_addr("127.0.0.1"); uint16_t network_byte_order_port = htons(port); -#ifdef __linux__ + char listenerNameUdp[16] = "lo_udp"; + char listenerNameTcp[16] = "lo_tcp"; char *loopback_interface = "lo"; - _setupUdpListenerOnInet(forwarder, (ipv4_addr_t *)&(addr), + _setupUdpListenerOnInet(forwarder, listenerNameUdp,(ipv4_addr_t *)&(addr), &network_byte_order_port, loopback_interface); - _setupTcpListenerOnInet(forwarder, (ipv4_addr_t *)&(addr), + _setupTcpListenerOnInet(forwarder, listenerNameTcp, (ipv4_addr_t *)&(addr), &network_byte_order_port, loopback_interface); -#else - _setupUdpListenerOnInet(forwarder, (ipv4_addr_t *)&(addr), - &network_byte_order_port); - _setupTcpListenerOnInet(forwarder, (ipv4_addr_t *)&(addr), - &network_byte_order_port); -#endif } diff --git a/hicn-light/src/hicn/config/controlListConnections.c b/hicn-light/src/hicn/config/controlListConnections.c index 0eb6392ea..dbd9707ca 100644 --- a/hicn-light/src/hicn/config/controlListConnections.c +++ b/hicn-light/src/hicn/config/controlListConnections.c @@ -83,7 +83,7 @@ static CommandReturn _controlListConnections_Execute(CommandParser *parser, return CommandReturn_Failure; } #ifdef WITH_POLICY - char flags_str[POLICY_TAG_N]; + char flags_str[POLICY_TAG_N+1]; char *s; #endif /* WITH_POLICY */ @@ -113,6 +113,12 @@ static CommandReturn _controlListConnections_Execute(CommandParser *parser, } } +#ifdef WITH_POLICY + printf("%5s %10s %12s %6s %40s %40s %5s %s\n", "id", "name", "admin_state", "state", "source", "destination", "type", "flags"); +#else + printf("%5s %10s %12s %6s %40s %40s %5s\n", "id", "name", "admin_state", "state", "source", "destination", "type"); +#endif /* WITH_POLICY */ + // Process/Print payload for (int i = 0; i < receivedHeader->length; i++) { list_connections_command *listConnectionsCommand = @@ -140,7 +146,8 @@ foreach_policy_tag *s = '\0'; parcBufferComposer_Format( - composer, "%5d %4s %s %s %s [%s]", listConnectionsCommand->connid, + composer, "%5d %10s %12s %6s %40s %40s %5s [%s]", listConnectionsCommand->connid, listConnectionsCommand->connectionName, + stateString[listConnectionsCommand->admin_state], stateString[listConnectionsCommand->state], sourceString, destinationString, connTypeString[listConnectionsCommand->connectionData.connectionType], @@ -148,7 +155,8 @@ foreach_policy_tag #else parcBufferComposer_Format( - composer, "%5d %4s %s %s %s", listConnectionsCommand->connid, + composer, "%5d %10s %12s %6s %40s %40s %5s", listConnectionsCommand->connid, listConnectionsCommand->connectionName, + stateString[listConnectionsCommand->admin_state], stateString[listConnectionsCommand->state], sourceString, destinationString, connTypeString[listConnectionsCommand->connectionData.connectionType]); diff --git a/hicn-light/src/hicn/config/controlListListeners.c b/hicn-light/src/hicn/config/controlListListeners.c index 1f4ad7f2c..5be7b0a9b 100644 --- a/hicn-light/src/hicn/config/controlListListeners.c +++ b/hicn-light/src/hicn/config/controlListListeners.c @@ -95,7 +95,8 @@ static CommandReturn _controlListListeners_Execute(CommandParser *parser, char *addrString = NULL; if (receivedHeader->length > 0) { - printf("%6.6s %50.70s %s\n", "iface", "address", "type"); + printf("%6.6s %16s %50.70s %6s %10s\n", "iface", "name", "address", "type", "interface"); + } else { printf(" --- No entry in the list \n"); } @@ -111,16 +112,26 @@ static CommandReturn _controlListListeners_Execute(CommandParser *parser, PARCBufferComposer *composer = parcBufferComposer_Create(); - parcBufferComposer_Format(composer, "%6u %50.70s %3s", - listListenersCommand->connid, addrString, - listenerType[listListenersCommand->encapType]); + if (strcmp(listenerType[listListenersCommand->encapType], "UDP") == 0 || + strcmp(listenerType[listListenersCommand->encapType], "TCP") == 0) { + parcBufferComposer_Format(composer, "%6u %16s %50.70s %6s %10s", + listListenersCommand->connid, + listListenersCommand->listenerName,addrString, + listenerType[listListenersCommand->encapType], + listListenersCommand->interfaceName); + } else { + parcBufferComposer_Format(composer, "%6u %16s %50.70s %6s", + listListenersCommand->connid, + listListenersCommand->listenerName,addrString, + listenerType[listListenersCommand->encapType]); + } PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer); char *result = parcBuffer_ToString(tempBuffer); parcBuffer_Release(&tempBuffer); if (!controlState_IsInteractive(state)) { - strcpy(commandOutputMain[i], result); + strncpy(commandOutputMain[i], result, 128); } puts(result); diff --git a/hicn-light/src/hicn/config/controlUpdateConnection.c b/hicn-light/src/hicn/config/controlUpdateConnection.c index 3e573d41d..ff834522e 100644 --- a/hicn-light/src/hicn/config/controlUpdateConnection.c +++ b/hicn-light/src/hicn/config/controlUpdateConnection.c @@ -31,9 +31,9 @@ #include <hicn/config/controlUpdateConnection.h> +#include <hicn/policy.h> #include <hicn/utils/commands.h> #include <hicn/utils/utils.h> -#include <hicn/utils/policy.h> static CommandReturn _controlUpdateConnection_Execute(CommandParser *parser, CommandOps *ops, diff --git a/hicn-light/src/hicn/core/connection.c b/hicn-light/src/hicn/core/connection.c index 2f50dbf7f..821da884d 100644 --- a/hicn-light/src/hicn/core/connection.c +++ b/hicn-light/src/hicn/core/connection.c @@ -28,7 +28,7 @@ #include <parc/algol/parc_Memory.h> #include <parc/assert/parc_Assert.h> #ifdef WITH_POLICY -#include <hicn/utils/policy.h> +#include <hicn/policy.h> #endif /* WITH_POLICY */ struct connection { @@ -329,6 +329,14 @@ void connection_SetAdminState(Connection *conn, connection_state_t admin_state) ioOperations_SetAdminState(conn->ops, admin_state); } +const char * connection_GetInterfaceName(const Connection * conn) +{ + parcAssertNotNull(conn, "Parameter conn must be non-null"); + if (!conn->ops) + return NULL; + return ioOperations_GetInterfaceName(conn->ops); +} + #ifdef WITH_POLICY void connection_AddTag(Connection *conn, policy_tag_t tag) diff --git a/hicn-light/src/hicn/core/connection.h b/hicn-light/src/hicn/core/connection.h index c007a1a26..b7b5e3c91 100644 --- a/hicn-light/src/hicn/core/connection.h +++ b/hicn-light/src/hicn/core/connection.h @@ -41,7 +41,7 @@ typedef enum { #endif /* WITH_MAPME */ #ifdef WITH_POLICY -#include <hicn/utils/policy.h> +#include <hicn/policy.h> #endif /* WITH_POLICY */ // packet types for probing @@ -183,6 +183,8 @@ connection_state_t connection_GetAdminState(const Connection *conn); void connection_SetAdminState(Connection *conn, connection_state_t admin_state); +const char * connection_GetInterfaceName(const Connection * conn); + #ifdef WITH_POLICY void connection_AddTag(Connection *conn, policy_tag_t tag); void connection_RemoveTag(Connection *conn, policy_tag_t tag); diff --git a/hicn-light/src/hicn/core/mapMe.c b/hicn-light/src/hicn/core/mapMe.c index 0f86dfd7e..2b621387b 100644 --- a/hicn-light/src/hicn/core/mapMe.c +++ b/hicn-light/src/hicn/core/mapMe.c @@ -207,11 +207,11 @@ static PARCIterator *mapMeTFIB_CreateKeyIterator(const MapMeTFIB *tfib) { int hicn_prefix_from_name(const Name *name, hicn_prefix_t *prefix) { NameBitvector *bv = name_GetContentName(name); - ip_address_t ip_address; - nameBitvector_ToIPAddress(bv, &ip_address); + ip_prefix_t ip_prefix; + nameBitvector_ToIPAddress(bv, &ip_prefix); /* The name length will be equal to ip address' prefix length */ - return hicn_prefix_create_from_ip_address(&ip_address, prefix); + return hicn_prefix_create_from_ip_prefix(&ip_prefix, prefix); } static Message *mapMe_createMessage(const MapMe *mapme, const Name *name, diff --git a/hicn-light/src/hicn/core/nameBitvector.c b/hicn-light/src/hicn/core/nameBitvector.c index ad6884d02..9a734359c 100644 --- a/hicn-light/src/hicn/core/nameBitvector.c +++ b/hicn-light/src/hicn/core/nameBitvector.c @@ -264,11 +264,11 @@ void nameBitvector_clear(NameBitvector *a, uint8_t start_from){ } int nameBitvector_ToIPAddress(const NameBitvector *name, - ip_address_t *ip_address) { + ip_prefix_t *prefix) { if (name->IPversion == IPv4_TYPE) { - struct in_addr *addr = (struct in_addr *)(&ip_address->buffer); - ip_address->family = AF_INET; - ip_address->prefix_len = IPV4_ADDR_LEN_BITS; + struct in_addr *addr = (struct in_addr *)(&prefix->address.buffer); + prefix->family = AF_INET; + prefix->len = IPV4_ADDR_LEN_BITS; uint32_t tmp_addr = name->bits[0] >> 32ULL; uint8_t addr_1 = (tmp_addr & 0xff000000) >> 24; @@ -283,9 +283,9 @@ int nameBitvector_ToIPAddress(const NameBitvector *name, addr->s_addr = (addr->s_addr | addr_1); } else { - struct in6_addr *addr = (struct in6_addr *)(&ip_address->buffer); - ip_address->family = AF_INET6; - ip_address->prefix_len = name->len; // IPV6_ADDR_LEN_BITS; + struct in6_addr *addr = (struct in6_addr *)(&prefix->address.buffer); + prefix->family = AF_INET6; + prefix->len = name->len; // IPV6_ADDR_LEN_BITS; for (int i = 0; i < 8; i++) { addr->s6_addr[i] = (uint8_t)((name->bits[0] >> 8 * (7 - i)) & 0xFF); diff --git a/hicn-light/src/hicn/core/nameBitvector.h b/hicn-light/src/hicn/core/nameBitvector.h index 44cc45662..19944778c 100644 --- a/hicn-light/src/hicn/core/nameBitvector.h +++ b/hicn-light/src/hicn/core/nameBitvector.h @@ -48,8 +48,7 @@ uint32_t nameBitvector_lpm(const NameBitvector *a, const NameBitvector *b); void nameBitvector_clear(NameBitvector *a, uint8_t start_from); -int nameBitvector_ToIPAddress(const NameBitvector *name, - ip_address_t *ip_address); +int nameBitvector_ToIPAddress(const NameBitvector *name, ip_prefix_t *prefix); void nameBitvector_setLen(NameBitvector *name, uint8_t len); Address *nameBitvector_ToAddress(const NameBitvector *name); diff --git a/hicn-light/src/hicn/io/hicnConnection.c b/hicn-light/src/hicn/io/hicnConnection.c index 124885b33..d56231c38 100644 --- a/hicn-light/src/hicn/io/hicnConnection.c +++ b/hicn-light/src/hicn/io/hicnConnection.c @@ -40,6 +40,7 @@ typedef struct hicn_state { Forwarder *forwarder; + char * interfaceName; Logger *logger; // the hicn listener socket we receive packets on @@ -96,6 +97,7 @@ static connection_state_t _getState(const IoOperations *ops); static void _setState(IoOperations *ops, connection_state_t state); static connection_state_t _getAdminState(const IoOperations *ops); static void _setAdminState(IoOperations *ops, connection_state_t admin_state); +static const char * _getInterfaceName(const IoOperations *ops); /* * This assigns a unique pointer to the void * which we use @@ -127,6 +129,7 @@ static IoOperations _template = { .setState = &_setState, .getAdminState = &_getAdminState, .setAdminState = &_setAdminState, + .getInterfaceName = &_getInterfaceName, }; // ================================================================= @@ -136,7 +139,7 @@ static bool _saveSockaddr(_HicnState *hicnConnState, const AddressPair *pair); static void _refreshProbeDestAddress(_HicnState *hicnConnState, const uint8_t *message); -IoOperations *hicnConnection_Create(Forwarder *forwarder, int fd, +IoOperations *hicnConnection_Create(Forwarder *forwarder, const char * interfaceName, int fd, const AddressPair *pair, bool isLocal) { IoOperations *io_ops = NULL; @@ -146,6 +149,7 @@ IoOperations *hicnConnection_Create(Forwarder *forwarder, int fd, sizeof(_HicnState)); hicnConnState->forwarder = forwarder; + hicnConnState->interfaceName = strdup(interfaceName); hicnConnState->logger = logger_Acquire(forwarder_GetLogger(forwarder)); bool saved = _saveSockaddr(hicnConnState, pair); @@ -183,6 +187,7 @@ IoOperations *hicnConnection_Create(Forwarder *forwarder, int fd, // _saveSockaddr will already log an error, no need for extra log message // here logger_Release(&hicnConnState->logger); + free(hicnConnState->interfaceName); parcMemory_Deallocate((void **)&hicnConnState); } @@ -223,6 +228,7 @@ static void _destroy(IoOperations **opsPtr) { // should I say something to libhicn? logger_Release(&hicnConnState->logger); + free(hicnConnState->interfaceName); parcMemory_Deallocate((void **)&hicnConnState); parcMemory_Deallocate((void **)&ops); @@ -585,3 +591,11 @@ static void _setAdminState(IoOperations *ops, connection_state_t admin_state) { (_HicnState *)ioOperations_GetClosure(ops); hicnConnState->admin_state = admin_state; } + +static const char * _getInterfaceName(const IoOperations *ops) +{ + parcAssertNotNull(ops, "Parameter must be non-null"); + _HicnState *hicnConnState = + (_HicnState *)ioOperations_GetClosure(ops); + return hicnConnState->interfaceName; +} diff --git a/hicn-light/src/hicn/io/hicnConnection.h b/hicn-light/src/hicn/io/hicnConnection.h index a14024a4e..fec18e1bd 100644 --- a/hicn-light/src/hicn/io/hicnConnection.h +++ b/hicn-light/src/hicn/io/hicnConnection.h @@ -48,6 +48,6 @@ * <#example#> * @endcode */ -IoOperations *hicnConnection_Create(Forwarder *forwarder, int fd, +IoOperations *hicnConnection_Create(Forwarder *forwarder, const char * interfaceName, int fd, const AddressPair *pair, bool isLocal); #endif // hicnConnection_h diff --git a/hicn-light/src/hicn/io/hicnListener.c b/hicn-light/src/hicn/io/hicnListener.c index d13dc5b4d..995347d6a 100644 --- a/hicn-light/src/hicn/io/hicnListener.c +++ b/hicn-light/src/hicn/io/hicnListener.c @@ -42,6 +42,9 @@ #define MAX_HICN_RETRY 5 struct hicn_listener { + + char *listenerName; + Forwarder *forwarder; Logger *logger; @@ -71,6 +74,8 @@ struct hicn_listener { }; static void _destroy(ListenerOps **listenerOpsPtr); +static const char *_getListenerName(const ListenerOps *ops); +static const char *_getInterfaceName(const ListenerOps *ops); static unsigned _getInterfaceIndex(const ListenerOps *ops); static const Address *_getListenAddress(const ListenerOps *ops); static EncapType _getEncapType(const ListenerOps *ops); @@ -85,6 +90,8 @@ static ListenerOps _hicnTemplate = { .getListenAddress = &_getListenAddress, .getEncapType = &_getEncapType, .getSocket = &_getSocket, + .getInterfaceName = &_getInterfaceName, + .getListenerName = &_getListenerName, .createConnection = &_createNewConnection, .lookupConnection = &_lookupConnection, }; @@ -125,6 +132,9 @@ ListenerOps *hicnListener_CreateInet(Forwarder *forwarder, char *symbolic, sizeof(HicnListener)); hicn->forwarder = forwarder; + hicn->listenerName = parcMemory_StringDuplicate(symbolic, strlen(symbolic)); + hicn->logger = logger_Acquire(forwarder_GetLogger(forwarder)); + hicn->logger = logger_Acquire(forwarder_GetLogger(forwarder)); hicn->conn_id = forwarder_GetNextConnectionId(forwarder); @@ -164,6 +174,7 @@ ListenerOps *hicnListener_CreateInet(Forwarder *forwarder, char *symbolic, } logger_Release(&hicn->logger); addressDestroy(&hicn->localAddress); + parcMemory_Deallocate((void **)&hicn->listenerName); parcMemory_Deallocate((void **)&hicn); return NULL; } @@ -206,6 +217,7 @@ ListenerOps *hicnListener_CreateInet6(Forwarder *forwarder, char *symbolic, sizeof(HicnListener)); hicn->forwarder = forwarder; + hicn->listenerName = parcMemory_StringDuplicate(symbolic, strlen(symbolic)); hicn->logger = logger_Acquire(forwarder_GetLogger(forwarder)); hicn->conn_id = forwarder_GetNextConnectionId(forwarder); @@ -255,6 +267,7 @@ ListenerOps *hicnListener_CreateInet6(Forwarder *forwarder, char *symbolic, } logger_Release(&hicn->logger); addressDestroy(&hicn->localAddress); + parcMemory_Deallocate((void **)&hicn->listenerName); parcMemory_Deallocate((void **)&hicn); return NULL; } @@ -421,6 +434,15 @@ static void _destroy(ListenerOps **listenerOpsPtr) { *listenerOpsPtr = NULL; } +static const char *_getListenerName(const ListenerOps *ops) { + HicnListener *hicn = (HicnListener *)ops->context; + return hicn->listenerName; +} +static const char *_getInterfaceName(const ListenerOps *ops) { + const char *interfaceName = ""; + return interfaceName; +} + static unsigned _getInterfaceIndex(const ListenerOps *ops) { HicnListener *hicn = (HicnListener *)ops->context; return hicn->conn_id; @@ -468,7 +490,7 @@ static unsigned _createNewConnection(ListenerOps * listener, int fd, bool isLocal = false; // udpConnection_Create takes ownership of the pair - IoOperations *ops = hicnConnection_Create(hicn->forwarder, fd, pair, isLocal); + IoOperations *ops = hicnConnection_Create(hicn->forwarder, listener->getInterfaceName(listener), fd, pair, isLocal); Connection *conn = connection_Create(ops); connectionTable_Add(forwarder_GetConnectionTable(hicn->forwarder), conn); diff --git a/hicn-light/src/hicn/io/hicnTunnel.c b/hicn-light/src/hicn/io/hicnTunnel.c index baf10c637..fd5acc680 100644 --- a/hicn-light/src/hicn/io/hicnTunnel.c +++ b/hicn-light/src/hicn/io/hicnTunnel.c @@ -56,7 +56,7 @@ IoOperations *hicnTunnel_CreateOnListener(Forwarder *forwarder, AddressPair *pair = addressPair_Create(localAddress, remoteAddress); bool isLocal = false; int fd = localListener->getSocket(localListener); - ops = hicnConnection_Create(forwarder, fd, pair, isLocal); + ops = hicnConnection_Create(forwarder, localListener->getInterfaceName(localListener), fd, pair, isLocal); addressPair_Release(&pair); } else { diff --git a/hicn-light/src/hicn/io/ioOperations.c b/hicn-light/src/hicn/io/ioOperations.c index bbcea41f8..31e37a461 100644 --- a/hicn-light/src/hicn/io/ioOperations.c +++ b/hicn-light/src/hicn/io/ioOperations.c @@ -83,3 +83,7 @@ connection_state_t ioOperations_GetAdminState(const IoOperations *ops) { void ioOperations_SetAdminState(IoOperations *ops, connection_state_t admin_state) { ops->setAdminState(ops, admin_state); } + +const char * ioOperations_GetInterfaceName(const IoOperations *ops) { + return ops->getInterfaceName(ops); +} diff --git a/hicn-light/src/hicn/io/ioOperations.h b/hicn-light/src/hicn/io/ioOperations.h index 7a48b7e3e..c8a107199 100644 --- a/hicn-light/src/hicn/io/ioOperations.h +++ b/hicn-light/src/hicn/io/ioOperations.h @@ -67,6 +67,8 @@ typedef struct io_ops IoOperations; * state). * @constant setAdminState Allows to set the administrative state of a * connection. + * @constant getInterfaceName Returns the interface name associated to a + * connection. * @discussion <#Discussion#> */ struct io_ops { @@ -87,6 +89,7 @@ struct io_ops { void (*setState)(IoOperations *ops, connection_state_t state); connection_state_t (*getAdminState)(const IoOperations *ops); void (*setAdminState)(IoOperations *ops, connection_state_t admin_state); + const char * (*getInterfaceName)(const IoOperations *ops); }; /** @@ -413,4 +416,12 @@ connection_state_t ioOperations_GetAdminState(const IoOperations *ops); */ void ioOperations_SetAdminState(IoOperations *ops, connection_state_t admin_state); +/** + * Sets the interface name associated to the connection. + * + * @param [in] ops The connection implementation. + * @return the name associated to the connection (const char *) + */ +const char * ioOperations_GetInterfaceName(const IoOperations *ops); + #endif // io_h diff --git a/hicn-light/src/hicn/io/listener.h b/hicn-light/src/hicn/io/listener.h index ef1955b12..1b473be59 100644 --- a/hicn-light/src/hicn/io/listener.h +++ b/hicn-light/src/hicn/io/listener.h @@ -55,6 +55,15 @@ struct listener_ops { void (*destroy)(ListenerOps **listenerOpsPtr); /** + * Returns the listener name of the listener. + * + * @param [in] ops Pointer to this structure + * + * @return the listener name of the listener + */ + const char *(*getListenerName)(const ListenerOps *ops); + + /** * Returns the interface index of the listener. * * @param [in] ops Pointer to this structure @@ -82,6 +91,15 @@ struct listener_ops { EncapType (*getEncapType)(const ListenerOps *ops); /** + * Returns the interface name of the listener. + * + * @param [in] ops Pointer to this structure + * + * @return the interface name of the listener + */ + const char *(*getInterfaceName)(const ListenerOps *ops); + + /** * Returns the underlying socket associated with the listener * * Not all listeners are capable of returning a useful socket. In those diff --git a/hicn-light/src/hicn/io/listenerSet.c b/hicn-light/src/hicn/io/listenerSet.c index 982373b63..3e44973d7 100644 --- a/hicn-light/src/hicn/io/listenerSet.c +++ b/hicn-light/src/hicn/io/listenerSet.c @@ -130,3 +130,39 @@ ListenerOps *listenerSet_Find(const ListenerSet *set, EncapType encapType, return match; } + +ListenerOps *listenerSet_FindById(const ListenerSet *set, unsigned id) { + parcAssertNotNull(set, "Parameter set must be non-null"); + + ListenerOps *match = NULL; + + for (size_t i = 0; i < parcArrayList_Size(set->listOfListeners) && !match; + i++) { + ListenerOps *ops = parcArrayList_Get(set->listOfListeners, i); + parcAssertNotNull(ops, "Got null listener ops at index %zu", i); + if (ops->getInterfaceIndex(ops) == id) { + match = ops; + } + } + + return match; +} + +int listenerSet_FindIdByListenerName(const ListenerSet *set, const char *listenerName ) { + parcAssertNotNull(set, "Parameter set must be non-null"); + parcAssertNotNull(listenerName, "Parameter listenerName must be non-null"); + + ListenerOps *match = NULL; + int index = -1; + for (size_t i = 0; i < parcArrayList_Size(set->listOfListeners) && !match; + i++) { + ListenerOps *ops = parcArrayList_Get(set->listOfListeners, i); + parcAssertNotNull(ops, "Got null listener ops at index %zu", i); + if (ops->getListenerName(ops) && strcmp(ops->getListenerName(ops), listenerName) == 0) { + index = ops->getInterfaceIndex(ops); + break; + } + } + + return index; +} diff --git a/hicn-light/src/hicn/io/listenerSet.h b/hicn-light/src/hicn/io/listenerSet.h index 8d15a2736..c8937fa02 100644 --- a/hicn-light/src/hicn/io/listenerSet.h +++ b/hicn-light/src/hicn/io/listenerSet.h @@ -134,4 +134,41 @@ ListenerOps *listenerSet_Get(const ListenerSet *set, size_t index); */ ListenerOps *listenerSet_Find(const ListenerSet *set, EncapType encapType, const Address *localAddress); + + +/** + * Looks up a listener by its id + * + * <#Paragraphs Of Explanation#> + * + * @param [in] set An allocated listener set + * @param [in] id of the listener + * + * @retval non-null The listener matching the query + * @retval null Does not exist + * + * Example: + * @code + * + * @endcode + */ +ListenerOps *listenerSet_FindById(const ListenerSet *set, unsigned id); +/** + * Looks up a listener by its id + * + * <#Paragraphs Of Explanation#> + * + * @param [in] set An allocated listener set + * @param [in] name of the listener + * + * @retval greater or equal to 0 The listener matching the query + * @retval -1 Does not exist + * + * Example: + * @code + * + * @endcode + */ +int listenerSet_FindIdByListenerName(const ListenerSet *set, const char *listenerName); + #endif diff --git a/hicn-light/src/hicn/io/streamConnection.c b/hicn-light/src/hicn/io/streamConnection.c index ce2341cb8..224f129f7 100644 --- a/hicn-light/src/hicn/io/streamConnection.c +++ b/hicn-light/src/hicn/io/streamConnection.c @@ -47,6 +47,7 @@ static void _conn_eventcb(PARCEventQueue *bufferEventVector, typedef struct stream_state { Forwarder *forwarder; + char * interfaceName; Logger *logger; int fd; @@ -90,6 +91,7 @@ static connection_state_t _streamConnection_getState(const IoOperations *ops); static void _streamConnection_setState(IoOperations *ops, connection_state_t state); static connection_state_t _streamConnection_getAdminState(const IoOperations *ops); static void _streamConnection_setAdminState(IoOperations *ops, connection_state_t admin_state); +static const char * _streamConnection_getInterfaceName(const IoOperations *ops); /* * This assigns a unique pointer to the void * which we use @@ -121,6 +123,7 @@ static IoOperations _template = { .setState = &_streamConnection_setState, .getAdminState = &_streamConnection_getAdminState, .setAdminState = &_streamConnection_setAdminState, + .getInterfaceName = &_streamConnection_getInterfaceName, }; IoOperations *streamConnection_AcceptConnection(Forwarder *forwarder, int fd, @@ -137,6 +140,7 @@ IoOperations *streamConnection_AcceptConnection(Forwarder *forwarder, int fd, PARCEventQueueOption_CloseOnFree | PARCEventQueueOption_DeferCallbacks); stream->forwarder = forwarder; + stream->interfaceName = NULL; stream->logger = logger_Acquire(forwarder_GetLogger(forwarder)); stream->fd = fd; stream->id = forwarder_GetNextConnectionId(forwarder); @@ -191,6 +195,7 @@ IoOperations *streamConnection_OpenConnection(Forwarder *forwarder, sizeof(_StreamState)); stream->forwarder = forwarder; + stream->interfaceName = NULL; stream->logger = logger_Acquire(forwarder_GetLogger(forwarder)); stream->fd = parcEventQueue_GetFileDescriptor(bufferEventVector); stream->bufferEventVector = bufferEventVector; @@ -606,7 +611,7 @@ static void _conn_readcb(PARCEventQueue *event, PARCEventType type, } else { parcAssertTrue(false, - "(Local stream connection) malformend packet received"); + "(Local stream connection) malformed packet received"); } } @@ -728,3 +733,11 @@ static void _streamConnection_setAdminState(IoOperations *ops, connection_state_ (_StreamState *)ioOperations_GetClosure(ops); stream->admin_state = admin_state; } + +static const char * _streamConnection_getInterfaceName(const IoOperations *ops) +{ + parcAssertNotNull(ops, "Parameter must be non-null"); + _StreamState *stream = + (_StreamState *)ioOperations_GetClosure(ops); + return stream->interfaceName; +} diff --git a/hicn-light/src/hicn/io/tcpListener.c b/hicn-light/src/hicn/io/tcpListener.c index cc6fdbba8..4464edf28 100644 --- a/hicn-light/src/hicn/io/tcpListener.c +++ b/hicn-light/src/hicn/io/tcpListener.c @@ -30,6 +30,8 @@ #include <parc/assert/parc_Assert.h> typedef struct tcp_listener { + char *listenerName; + Forwarder *forwarder; Logger *logger; @@ -38,6 +40,7 @@ typedef struct tcp_listener { Address *localAddress; unsigned id; + char *interfaceName; // is the localAddress as 127.0.0.0 address? bool isLocalAddressLocal; @@ -46,30 +49,36 @@ typedef struct tcp_listener { static void _tcpListener_Destroy(_TcpListener **listenerPtr); static void _tcpListener_OpsDestroy(ListenerOps **listenerOpsPtr); +static const char *_tcpListener_ListenerName(const ListenerOps *ops); static unsigned _tcpListener_OpsGetInterfaceIndex(const ListenerOps *ops); static const Address *_tcpListener_OpsGetListenAddress(const ListenerOps *ops); +static const char *_tcpListener_InterfaceName(const ListenerOps *ops); static EncapType _tcpListener_OpsGetEncapType(const ListenerOps *ops); static ListenerOps _tcpTemplate = { .context = NULL, .destroy = &_tcpListener_OpsDestroy, + .getListenerName = &_tcpListener_ListenerName, .getInterfaceIndex = &_tcpListener_OpsGetInterfaceIndex, .getListenAddress = &_tcpListener_OpsGetListenAddress, .getEncapType = &_tcpListener_OpsGetEncapType, + .getInterfaceName = &_tcpListener_InterfaceName, .getSocket = NULL}; // STREAM daemon listener callback static void _tcpListener_Listen(int, struct sockaddr *, int socklen, void *tcpVoid); -ListenerOps *tcpListener_CreateInet6(Forwarder *forwarder, - struct sockaddr_in6 sin6) { +ListenerOps *tcpListener_CreateInet6(Forwarder *forwarder, char *listenerName, + struct sockaddr_in6 sin6, char *interfaceName) { _TcpListener *tcp = parcMemory_AllocateAndClear(sizeof(_TcpListener)); parcAssertNotNull(tcp, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(_TcpListener)); tcp->forwarder = forwarder; + tcp->listenerName = parcMemory_StringDuplicate(listenerName, strlen(listenerName)); tcp->logger = logger_Acquire(forwarder_GetLogger(forwarder)); + tcp->interfaceName = parcMemory_StringDuplicate(interfaceName, strlen(interfaceName)); tcp->listener = dispatcher_CreateListener( forwarder_GetDispatcher(forwarder), _tcpListener_Listen, (void *)tcp, -1, @@ -107,8 +116,8 @@ ListenerOps *tcpListener_CreateInet6(Forwarder *forwarder, return ops; } -ListenerOps *tcpListener_CreateInet(Forwarder *forwarder, - struct sockaddr_in sin) { +ListenerOps *tcpListener_CreateInet(Forwarder *forwarder, char *listenerName, + struct sockaddr_in sin, char *interfaceName) { _TcpListener *tcp = parcMemory_AllocateAndClear(sizeof(_TcpListener)); parcAssertNotNull(tcp, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(_TcpListener)); @@ -164,6 +173,9 @@ static void _tcpListener_Destroy(_TcpListener **listenerPtr) { parcMemory_Deallocate((void **)&str); } + parcMemory_Deallocate((void **)&tcp->listenerName); + parcMemory_Deallocate((void **)&tcp->interfaceName); + logger_Release(&tcp->logger); dispatcher_DestroyListener(forwarder_GetDispatcher(tcp->forwarder), &tcp->listener); @@ -174,6 +186,16 @@ static void _tcpListener_Destroy(_TcpListener **listenerPtr) { // ================================================== +static const char *_tcpListener_ListenerName(const ListenerOps *ops) { + _TcpListener *tcp = (_TcpListener *)ops->context; + return tcp->listenerName; +} + +static const char *_tcpListener_InterfaceName(const ListenerOps *ops) { + _TcpListener *tcp = (_TcpListener *)ops->context; + return tcp->interfaceName; +} + static void _tcpListener_Listen(int fd, struct sockaddr *sa, int socklen, void *tcpVoid) { _TcpListener *tcp = (_TcpListener *)tcpVoid; diff --git a/hicn-light/src/hicn/io/tcpListener.h b/hicn-light/src/hicn/io/tcpListener.h index df0bbea1e..a841738e5 100644 --- a/hicn-light/src/hicn/io/tcpListener.h +++ b/hicn-light/src/hicn/io/tcpListener.h @@ -33,8 +33,8 @@ #include <hicn/io/listener.h> #include <stdlib.h> -ListenerOps *tcpListener_CreateInet6(Forwarder *forwarder, - struct sockaddr_in6 sin6); -ListenerOps *tcpListener_CreateInet(Forwarder *forwarder, - struct sockaddr_in sin); +ListenerOps *tcpListener_CreateInet6(Forwarder *forwarder, char *listenerName, + struct sockaddr_in6 sin6, char *interfaceName); +ListenerOps *tcpListener_CreateInet(Forwarder *forwarder, char *listenerName, + struct sockaddr_in sin, char *interfaceName); #endif // tcpListener_h diff --git a/hicn-light/src/hicn/io/udpConnection.c b/hicn-light/src/hicn/io/udpConnection.c index 78157f01f..9ad70403f 100644 --- a/hicn-light/src/hicn/io/udpConnection.c +++ b/hicn-light/src/hicn/io/udpConnection.c @@ -40,6 +40,7 @@ typedef struct udp_state { Forwarder *forwarder; + char * interfaceName; Logger *logger; // the udp listener socket we receive packets on @@ -81,6 +82,7 @@ static connection_state_t _getState(const IoOperations *ops); static void _setState(IoOperations *ops, connection_state_t state); static connection_state_t _getAdminState(const IoOperations *ops); static void _setAdminState(IoOperations *ops, connection_state_t admin_state); +static const char * _getInterfaceName(const IoOperations *ops); /* * This assigns a unique pointer to the void * which we use @@ -112,6 +114,7 @@ static IoOperations _template = { .setState = &_setState, .getAdminState = &_getAdminState, .setAdminState = &_setAdminState, + .getInterfaceName = &_getInterfaceName, }; // ================================================================= @@ -119,7 +122,7 @@ static IoOperations _template = { static void _setConnectionState(_UdpState *Udp, bool isUp); static bool _saveSockaddr(_UdpState *udpConnState, const AddressPair *pair); -IoOperations *udpConnection_Create(Forwarder *forwarder, int fd, +IoOperations *udpConnection_Create(Forwarder *forwarder, const char * interfaceName, int fd, const AddressPair *pair, bool isLocal) { IoOperations *io_ops = NULL; @@ -129,6 +132,7 @@ IoOperations *udpConnection_Create(Forwarder *forwarder, int fd, sizeof(_UdpState)); udpConnState->forwarder = forwarder; + udpConnState->interfaceName = strdup(interfaceName); udpConnState->logger = logger_Acquire(forwarder_GetLogger(forwarder)); bool saved = _saveSockaddr(udpConnState, pair); @@ -166,6 +170,8 @@ IoOperations *udpConnection_Create(Forwarder *forwarder, int fd, // _saveSockaddr will already log an error, no need for extra log message // here logger_Release(&udpConnState->logger); + + free(udpConnState->interfaceName); parcMemory_Deallocate((void **)&udpConnState); } @@ -202,6 +208,7 @@ static void _destroy(IoOperations **opsPtr) { // that when its done logger_Release(&udpConnState->logger); + free(udpConnState->interfaceName); parcMemory_Deallocate((void **)&udpConnState); parcMemory_Deallocate((void **)&ops); @@ -435,3 +442,11 @@ static void _setAdminState(IoOperations *ops, connection_state_t admin_state) { (_UdpState *)ioOperations_GetClosure(ops); udpConnState->admin_state = admin_state; } + +static const char * _getInterfaceName(const IoOperations *ops) +{ + parcAssertNotNull(ops, "Parameter must be non-null"); + _UdpState *udpConnState = + (_UdpState *)ioOperations_GetClosure(ops); + return udpConnState->interfaceName; +} diff --git a/hicn-light/src/hicn/io/udpConnection.h b/hicn-light/src/hicn/io/udpConnection.h index 07c4da9bd..9fbc5348b 100644 --- a/hicn-light/src/hicn/io/udpConnection.h +++ b/hicn-light/src/hicn/io/udpConnection.h @@ -48,6 +48,6 @@ * <#example#> * @endcode */ -IoOperations *udpConnection_Create(Forwarder *forwarder, int fd, +IoOperations *udpConnection_Create(Forwarder *forwarder, const char * interfaceName, int fd, const AddressPair *pair, bool isLocal); #endif // udpConnection_h diff --git a/hicn-light/src/hicn/io/udpListener.c b/hicn-light/src/hicn/io/udpListener.c index f6b434fed..050ca104c 100644 --- a/hicn-light/src/hicn/io/udpListener.c +++ b/hicn-light/src/hicn/io/udpListener.c @@ -41,6 +41,7 @@ struct udp_listener { Forwarder *forwarder; + char *listenerName; Logger *logger; PARCEvent *udp_event; @@ -48,13 +49,16 @@ struct udp_listener { uint16_t port; unsigned id; + char *interfaceName; Address *localAddress; }; static void _destroy(ListenerOps **listenerOpsPtr); +static const char *_getListenerName(const ListenerOps *ops); static unsigned _getInterfaceIndex(const ListenerOps *ops); static const Address *_getListenAddress(const ListenerOps *ops); static EncapType _getEncapType(const ListenerOps *ops); +static const char *_getInterfaceName(const ListenerOps *ops); static int _getSocket(const ListenerOps *ops); static unsigned _createNewConnection(ListenerOps *listener, int fd, const AddressPair *pair); static const Connection * _lookupConnection(ListenerOps * listener, const AddressPair *pair); @@ -66,8 +70,10 @@ static ListenerOps udpTemplate = { .getListenAddress = &_getListenAddress, .getEncapType = &_getEncapType, .getSocket = &_getSocket, + .getListenerName = &_getListenerName, .createConnection = &_createNewConnection, .lookupConnection = &_lookupConnection, + .getInterfaceName = &_getInterfaceName, }; static void _readcb(int fd, PARCEventType what, void * listener_void); @@ -76,19 +82,16 @@ static void _readcb(int fd, PARCEventType what, void * listener_void); extern int bindSocket(int sock, const char* ifname); #endif -#ifdef __linux__ -ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, +ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, char *listenerName, struct sockaddr_in6 sin6, const char *interfaceName) { -#else -ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, - struct sockaddr_in6 sin6) { -#endif ListenerOps *ops = NULL; UdpListener *udp = parcMemory_AllocateAndClear(sizeof(UdpListener)); parcAssertNotNull(udp, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(UdpListener)); udp->forwarder = forwarder; + udp->listenerName = parcMemory_StringDuplicate(listenerName, strlen(listenerName)); + udp->interfaceName = parcMemory_StringDuplicate(interfaceName, strlen(interfaceName)); udp->logger = logger_Acquire(forwarder_GetLogger(forwarder)); udp->localAddress = addressCreateFromInet6(&sin6); udp->id = forwarder_GetNextConnectionId(forwarder); @@ -172,6 +175,8 @@ ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, myerrno, strerror(myerrno)); parcMemory_Deallocate((void **)&str); } + parcMemory_Deallocate((void **)&udp->listenerName); + parcMemory_Deallocate((void **)&udp->interfaceName); #ifndef _WIN32 close(udp->udp_socket); #else @@ -185,19 +190,16 @@ ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, return ops; } -#ifdef __linux__ -ListenerOps *udpListener_CreateInet(Forwarder *forwarder, +ListenerOps *udpListener_CreateInet(Forwarder *forwarder, char *listenerName, struct sockaddr_in sin, const char *interfaceName) { -#else -ListenerOps *udpListener_CreateInet(Forwarder *forwarder, - struct sockaddr_in sin) { -#endif ListenerOps *ops = NULL; UdpListener *udp = parcMemory_AllocateAndClear(sizeof(UdpListener)); parcAssertNotNull(udp, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(UdpListener)); udp->forwarder = forwarder; + udp->listenerName = parcMemory_StringDuplicate(listenerName, strlen(listenerName)); + udp->interfaceName = parcMemory_StringDuplicate(interfaceName, strlen(interfaceName)); udp->logger = logger_Acquire(forwarder_GetLogger(forwarder)); udp->localAddress = addressCreateFromInet(&sin); udp->id = forwarder_GetNextConnectionId(forwarder); @@ -315,6 +317,16 @@ static void udpListener_Destroy(UdpListener **listenerPtr) { *listenerPtr = NULL; } +static const char *_getListenerName(const ListenerOps *ops) { + UdpListener *udp = (UdpListener *)ops->context; + return udp->listenerName; +} + +static const char *_getInterfaceName(const ListenerOps *ops) { + UdpListener *udp = (UdpListener *)ops->context; + return udp->interfaceName; +} + static void _destroy(ListenerOps **listenerOpsPtr) { ListenerOps *ops = *listenerOpsPtr; UdpListener *udp = (UdpListener *)ops->context; @@ -420,7 +432,7 @@ static unsigned _createNewConnection(ListenerOps * listener, int fd, } // metisUdpConnection_Create takes ownership of the pair - IoOperations *ops = udpConnection_Create(udp->forwarder, fd, pair, isLocal); + IoOperations *ops = udpConnection_Create(udp->forwarder, udp->interfaceName, fd, pair, isLocal); Connection *conn = connection_Create(ops); // connection_AllowWldrAutoStart(conn); diff --git a/hicn-light/src/hicn/io/udpListener.h b/hicn-light/src/hicn/io/udpListener.h index 81d191eab..62c09e4db 100644 --- a/hicn-light/src/hicn/io/udpListener.h +++ b/hicn-light/src/hicn/io/udpListener.h @@ -27,16 +27,9 @@ struct udp_listener; typedef struct udp_listener UdpListener; -#ifdef __linux__ -ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, +ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, char *listenerName, struct sockaddr_in6 sin6, const char *if_bind); -ListenerOps *udpListener_CreateInet(Forwarder *forwarder, +ListenerOps *udpListener_CreateInet(Forwarder *forwarder, char *listenerName, struct sockaddr_in sin, const char *if_bind); -#else -ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, - struct sockaddr_in6 sin6); -ListenerOps *udpListener_CreateInet(Forwarder *forwarder, - struct sockaddr_in sin); -#endif // void udpListener_SetPacketType(ListenerOps *ops, MessagePacketType type); #endif // udpListener_h diff --git a/hicn-light/src/hicn/io/udpTunnel.c b/hicn-light/src/hicn/io/udpTunnel.c index 2f2f84105..9f5249e3c 100644 --- a/hicn-light/src/hicn/io/udpTunnel.c +++ b/hicn-light/src/hicn/io/udpTunnel.c @@ -60,7 +60,7 @@ IoOperations *udpTunnel_CreateOnListener(Forwarder *forwarder, int fd = localListener->getSocket(localListener); // udpListener_SetPacketType(localListener, // MessagePacketType_ContentObject); - ops = udpConnection_Create(forwarder, fd, pair, isLocal); + ops = udpConnection_Create(forwarder, localListener->getInterfaceName(localListener), fd, pair, isLocal); addressPair_Release(&pair); } else { diff --git a/hicn-light/src/hicn/processor/fibEntry.c b/hicn-light/src/hicn/processor/fibEntry.c index 9d82b086e..04e453ab9 100644 --- a/hicn-light/src/hicn/processor/fibEntry.c +++ b/hicn-light/src/hicn/processor/fibEntry.c @@ -39,7 +39,7 @@ #ifdef WITH_POLICY #include <hicn/core/forwarder.h> -#include <hicn/utils/policy.h> +#include <hicn/policy.h> #ifdef WITH_MAPME #include <hicn/core/mapMe.h> @@ -218,6 +218,8 @@ fibEntry_GetAvailableNextHops(const FibEntry *fibEntry, unsigned in_connection) ConnectionList * list = connectionTable_GetEntries(table); for (size_t i = 0; i < connectionList_Length(list); i++) { Connection *conn = connectionList_Get(list, i); + if (connection_GetAdminState(conn) == CONNECTION_STATE_DOWN) + continue; if (connection_GetState(conn) == CONNECTION_STATE_DOWN) continue; if (connection_IsLocal(conn)) @@ -236,6 +238,8 @@ fibEntry_GetAvailableNextHops(const FibEntry *fibEntry, unsigned in_connection) const Connection * conn = connectionTable_FindById(table, conn_id); if (!conn) continue; + if (connection_GetAdminState(conn) == CONNECTION_STATE_DOWN) + continue; if (connection_GetState(conn) == CONNECTION_STATE_DOWN) continue; if (!connection_IsLocal(conn)) @@ -259,6 +263,8 @@ fibEntry_GetAvailableNextHops(const FibEntry *fibEntry, unsigned in_connection) conn = connectionTable_FindById(table, conn_id); if (!conn) continue; + if (connection_GetAdminState(conn) == CONNECTION_STATE_DOWN) + continue; if (connection_GetState(conn) == CONNECTION_STATE_DOWN) continue; diff --git a/hicn-light/src/hicn/socket/api.c b/hicn-light/src/hicn/socket/api.c index 213163675..eca02ec38 100644 --- a/hicn-light/src/hicn/socket/api.c +++ b/hicn-light/src/hicn/socket/api.c @@ -39,7 +39,7 @@ static hicn_conf_t hicn_default_conf = { struct ip_rule_state_ { char tun_name[IF_NAMESIZE]; - ip_address_t ip_address; + ip_prefix_t prefix; uint32_t table_id; uint8_t priority; uint8_t address_family; @@ -160,7 +160,7 @@ void hicn_destroy() { } } else { rc = ops.del_prio_rule( - &rules_to_remove[i].ip_address, rules_to_remove[i].address_family, + &rules_to_remove[i].prefix, rules_to_remove[i].address_family, rules_to_remove[i].priority, rules_to_remove[i].table_id); if (rc < 0) { goto ERR; @@ -204,7 +204,7 @@ int hicn_socket_cmp(hicn_socket_t *a, hicn_socket_t *b) { return b->fd - a->fd; } -ip_address_t *hicn_socket_get_src_ip(hicn_socket_t *socket) { +ip_prefix_t *hicn_socket_get_src_ip(hicn_socket_t *socket) { if (socket->type != HS_CONNECTION) { return NULL; } @@ -252,7 +252,7 @@ int hicn_set_local_endpoint(hicn_socket_t *socket, const char *local_ip_address, */ /* Copy the local IP address inside the connection */ - rc = hicn_ip_pton(local_ip_address, &socket->connection.tun_ip_address); + rc = ip_prefix_pton(local_ip_address, &socket->connection.tun_ip_address); if (rc < 0) { rc = HICN_SOCKET_ERROR_SOCKET_LOCAL_REPR; goto end; @@ -262,13 +262,13 @@ end: return rc; } -int hicn_get_local_address(const ip_address_t *remote_address, - ip_address_t *local_address) { +int hicn_get_local_address(const ip_prefix_t *remote_address, + ip_prefix_t *local_address) { int rc = 0; uint32_t interface_id; - char remote_address_str[INET_MAX_ADDRSTRLEN]; + char remote_address_str[INET_MAX_ADDRSTRLEN + 4 ]; - rc = hicn_ip_ntop(remote_address, remote_address_str, + rc = ip_prefix_ntop(remote_address, remote_address_str, sizeof(remote_address_str)); if (rc < 0) { rc = HICN_SOCKET_ERROR_BIND_REMOTE_REPR; @@ -300,7 +300,7 @@ ERR: int hicn_set_remote_endpoint(hicn_socket_t *socket, const char *remote_ip_address) { int af, rc = HICN_SOCKET_ERROR_NONE; - ip_address_t addr; + ip_prefix_t addr; af = get_addr_family(remote_ip_address); if ((af != AF_INET6) && (af != AF_INET)) { @@ -308,8 +308,8 @@ int hicn_set_remote_endpoint(hicn_socket_t *socket, } /* Bind local endpoint if not done yet */ - if (ip_address_empty(&socket->connection.tun_ip_address)) { - char local_ip_address[INET_MAX_ADDRSTRLEN]; + if (ip_prefix_empty(&socket->connection.tun_ip_address)) { + char local_ip_address[INET_MAX_ADDRSTRLEN + 4]; /* Local interface id */ // INFO("Getting interface_id from gateway IP address %s", @@ -338,7 +338,7 @@ int hicn_set_remote_endpoint(hicn_socket_t *socket, ///// /* Convert to representation format */ - rc = hicn_ip_ntop(&addr, local_ip_address, sizeof(local_ip_address)); + rc = ip_prefix_ntop(&addr, local_ip_address, sizeof(local_ip_address)); if (rc < 0) { rc = HICN_SOCKET_ERROR_BIND_REMOTE_REPR; goto ERR; @@ -455,8 +455,8 @@ int hicn_listen(hicn_socket_helper_t *hicn, int fd, const char *prefix) { return rc; } - ip_address_t ip_address; - rc = hicn_ip_pton(prefix, &ip_address); + ip_prefix_t ip_prefix; + rc = ip_prefix_pton(prefix, &ip_prefix); if (rc < 0) { return rc; } @@ -467,7 +467,7 @@ int hicn_listen(hicn_socket_helper_t *hicn, int fd, const char *prefix) { if (punting_table_id == -1) punting_table_id = socket->connection.table_id; - rc = ops.add_prio_rule(&ip_address, ip_address.family, 0, + rc = ops.add_prio_rule(&ip_prefix, ip_prefix.family, 0, socket->connection.table_id); if (rc < 0) { return rc; @@ -475,8 +475,8 @@ int hicn_listen(hicn_socket_helper_t *hicn, int fd, const char *prefix) { strcpy(rules_to_remove[rules_counter].tun_name, "NONE"); - rules_to_remove[rules_counter].ip_address = ip_address; - rules_to_remove[rules_counter].address_family = ip_address.family; + rules_to_remove[rules_counter].prefix = ip_prefix; + rules_to_remove[rules_counter].address_family = ip_prefix.family; rules_to_remove[rules_counter].table_id = socket->connection.table_id; rules_to_remove[rules_counter].priority = 0; ++rules_counter; diff --git a/hicn-light/src/hicn/socket/api.h b/hicn-light/src/hicn/socket/api.h index 283065fc7..1a7f5c700 100644 --- a/hicn-light/src/hicn/socket/api.h +++ b/hicn-light/src/hicn/socket/api.h @@ -84,7 +84,7 @@ typedef struct hicn_socket_s { union { struct { - ip_address_t tun_ip_address; + ip_prefix_t tun_ip_address; uint32_t interface_id; /* ID of the corresponding table : avoid default values of 0, 32766 and @@ -163,8 +163,8 @@ void hicn_free(hicn_socket_helper_t *hicn); * * @return 0 in case of success, -1 otherwise. */ -int hicn_get_local_address(const ip_address_t *remote_address, - ip_address_t *local_address); +int hicn_get_local_address(const ip_prefix_t *remote_address, + ip_prefix_t *local_address); /* hICN socket */ diff --git a/hicn-light/src/hicn/socket/ops.h b/hicn-light/src/hicn/socket/ops.h index 249caf87a..1bee7c6f6 100644 --- a/hicn-light/src/hicn/socket/ops.h +++ b/hicn-light/src/hicn/socket/ops.h @@ -17,10 +17,10 @@ typedef struct { int (*get_output_ifid)(const char *ip_address, uint8_t address_family, uint32_t *interface_id); int (*get_ip_addr)(uint32_t interface_id, uint8_t address_family, - ip_address_t *ip_address); - int (*set_ip_addr)(uint32_t interface_id, ip_address_t *ip_address); + ip_prefix_t *ip_address); + int (*set_ip_addr)(uint32_t interface_id, ip_prefix_t *ip_address); int (*up_if)(uint32_t interface_id); - int (*add_in_route_table)(const ip_address_t *prefix, + int (*add_in_route_table)(const ip_prefix_t *prefix, const uint32_t interface_id, const uint8_t table_id); int (*add_in_route_table_s)(const char *prefix, const uint32_t interface_id, @@ -30,23 +30,23 @@ typedef struct { const uint8_t table_id, int default_route); int (*del_out_route)(const char *gateway, const uint8_t address_family, const uint8_t table_id); - int (*del_lo_route)(const ip_address_t *ip_address); + int (*del_lo_route)(const ip_prefix_t *ip_address); int (*add_rule)(const char *interface_name, const uint8_t address_family, const uint8_t table_id); int (*del_rule)(const char *interface_name, const uint8_t address_family, const uint8_t table_id); - int (*add_neigh_proxy)(const ip_address_t *ip_address, + int (*add_neigh_proxy)(const ip_prefix_t *ip_address, const uint32_t interface_id); - int (*add_prio_rule)(const ip_address_t *ip_address, + int (*add_prio_rule)(const ip_prefix_t *ip_address, const uint8_t address_family, const uint32_t priority, const uint8_t table_id); - int (*add_lo_prio_rule)(const ip_address_t *ip_address, + int (*add_lo_prio_rule)(const ip_prefix_t *ip_address, const uint8_t address_family, const uint32_t priority); - int (*del_prio_rule)(const ip_address_t *ip_address, + int (*del_prio_rule)(const ip_prefix_t *ip_address, const uint8_t address_family, const uint32_t priority, const uint8_t table_id); - int (*del_lo_prio_rule)(const ip_address_t *ip_address, + int (*del_lo_prio_rule)(const ip_prefix_t *ip_address, const uint8_t address_family, const uint32_t priority); } hicn_socket_ops_t; diff --git a/hicn-light/src/hicn/socket/ops_linux.c b/hicn-light/src/hicn/socket/ops_linux.c index 0b1c6b817..f81ceff37 100644 --- a/hicn-light/src/hicn/socket/ops_linux.c +++ b/hicn-light/src/hicn/socket/ops_linux.c @@ -60,13 +60,13 @@ int _nl_get_output_ifid(const char *ip_address, uint8_t address_family, * @see getifaddrs */ int _nl_get_ip_addr(uint32_t interface_id, uint8_t address_family, - ip_address_t *ip_address); + ip_prefix_t *ip_address); -int _nl_set_ip_addr(uint32_t interface_id, ip_address_t *ip_address); +int _nl_set_ip_addr(uint32_t interface_id, ip_prefix_t *ip_address); int _nl_up_if(uint32_t interface_id); -int _nl_add_in_route_table(const ip_address_t *prefix, +int _nl_add_in_route_table(const ip_prefix_t *prefix, const uint32_t interface_id, const uint8_t table_id); int _nl_add_in_route_table_s(const char *prefix, const uint32_t interface_id, const uint8_t table_id); @@ -77,25 +77,25 @@ int _nl_add_out_route(const char *gateway, const uint8_t address_family, int _nl_del_out_route(const char *gateway, const uint8_t address_family, const uint8_t table_id); -int _nl_del_lo_route(const ip_address_t *ip_address); +int _nl_del_lo_route(const ip_prefix_t *ip_address); int _nl_add_rule(const char *interface_name, const uint8_t address_family, const uint8_t table_id); int _nl_del_rule(const char *interface_name, const uint8_t address_family, const uint8_t table_id); -int _nl_add_neigh_proxy(const ip_address_t *ip_address, +int _nl_add_neigh_proxy(const ip_prefix_t *ip_address, const uint32_t interface_id); -int _nl_add_prio_rule(const ip_address_t *ip_address, +int _nl_add_prio_rule(const ip_prefix_t *ip_address, const uint8_t address_family, const uint32_t priority, const uint8_t table_id); -int _nl_add_lo_prio_rule(const ip_address_t *ip_address, +int _nl_add_lo_prio_rule(const ip_prefix_t *ip_address, const uint8_t address_family, const uint32_t priority); -int _nl_del_prio_rule(const ip_address_t *ip_address, +int _nl_del_prio_rule(const ip_prefix_t *ip_address, const uint8_t address_family, const uint32_t priority, const uint8_t table_id); -int _nl_del_lo_prio_rule(const ip_address_t *ip_address, +int _nl_del_lo_prio_rule(const ip_prefix_t *ip_address, const uint8_t address_family, const uint32_t priority); #endif /* HICN_NETLINK_H */ @@ -530,7 +530,7 @@ ERR: } int _nl_get_ip_addr(uint32_t interface_id, uint8_t address_family, - ip_address_t *ip_address) { + ip_prefix_t *prefix) { char buffer[BUFSIZE]; struct nlmsghdr *hdr = (struct nlmsghdr *)buffer; size_t n; @@ -575,9 +575,9 @@ int _nl_get_ip_addr(uint32_t interface_id, uint8_t address_family, if ((payload->ifa_index == interface_id) && (payload->ifa_prefixlen < IPV6_ADDR_LEN * 8)) { printf("got ip address\n"); - memcpy(ip_address->buffer, RTA_DATA(payload + 1), IPV6_ADDR_LEN); - ip_address->family = AF_INET6; - ip_address->prefix_len = IPV6_ADDR_LEN_BITS; + memcpy(prefix->address.buffer, RTA_DATA(payload + 1), IPV6_ADDR_LEN); + prefix->family = AF_INET6; + prefix->len = IPV6_ADDR_LEN_BITS; printf("returning %d\n", HICN_SOCKET_ERROR_NONE); return HICN_SOCKET_ERROR_NONE; } @@ -585,9 +585,9 @@ int _nl_get_ip_addr(uint32_t interface_id, uint8_t address_family, if ((payload->ifa_index == interface_id) && (payload->ifa_prefixlen < IPV4_ADDR_LEN * 8)) { printf("got ip address\n"); - memcpy(ip_address->buffer, RTA_DATA(payload + 1), IPV4_ADDR_LEN); - ip_address->family = AF_INET; - ip_address->prefix_len = IPV4_ADDR_LEN_BITS; + memcpy(prefix->address.buffer, RTA_DATA(payload + 1), IPV4_ADDR_LEN); + prefix->family = AF_INET; + prefix->len = IPV4_ADDR_LEN_BITS; printf("returning %d\n", HICN_SOCKET_ERROR_NONE); return HICN_SOCKET_ERROR_NONE; } @@ -604,7 +604,7 @@ ERR_SOCKET: return HICN_SOCKET_ERROR_UNSPEC; } -int _nl_set_ip_addr(uint32_t interface_id, ip_address_t *ip_address) { +int _nl_set_ip_addr(uint32_t interface_id, ip_prefix_t *prefix) { char buffer[BUFSIZE]; struct nlmsghdr *hdr = (struct nlmsghdr *)buffer; size_t n; @@ -617,19 +617,19 @@ int _nl_set_ip_addr(uint32_t interface_id, ip_address_t *ip_address) { .hdr.nlmsg_type = RTM_NEWADDR, .hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_MATCH | NLM_F_ATOMIC, .hdr.nlmsg_seq = seq++, - .payload.ifa_family = ip_address->family, - .payload.ifa_prefixlen = ip_address->prefix_len, + .payload.ifa_family = prefix->family, + .payload.ifa_prefixlen = prefix->len, .payload.ifa_flags = 0, .payload.ifa_scope = RT_SCOPE_UNIVERSE, .payload.ifa_index = interface_id}; /* Set attributes = length/type/value */ - struct rtattr ifa_address = {RTA_LENGTH(ip_address_len(ip_address)), + struct rtattr ifa_address = {RTA_LENGTH(ip_prefix_len(prefix)), IFA_ADDRESS}; struct iovec iov[] = { {&msg, sizeof(msg)}, {&ifa_address, sizeof(ifa_address)}, - {(void *)&ip_address->buffer, sizeof(ip_address->buffer)}, + {(void *)&prefix->address.buffer, sizeof(prefix->address.buffer)}, }; msg.hdr.nlmsg_len = iov_length(iov, ARRAY_SIZE(iov)); @@ -966,7 +966,7 @@ ERR_SOCKET: * ip route del 1:2::2 dev lo table local * */ -int _nl_del_lo_route(const ip_address_t *ip_address) { +int _nl_del_lo_route(const ip_prefix_t *prefix) { char buffer[BUFSIZE]; struct nlmsghdr *hdr = (struct nlmsghdr *)buffer; size_t n; @@ -979,8 +979,8 @@ int _nl_del_lo_route(const ip_address_t *ip_address) { .hdr.nlmsg_type = RTM_DELROUTE, .hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, .hdr.nlmsg_seq = seq++, - .payload.rtm_family = ip_address->family, - .payload.rtm_dst_len = ip_address->prefix_len, + .payload.rtm_family = prefix->family, + .payload.rtm_dst_len = prefix->len, .payload.rtm_src_len = 0, .payload.rtm_tos = 0, .payload.rtm_table = RT_TABLE_LOCAL, @@ -992,13 +992,13 @@ int _nl_del_lo_route(const ip_address_t *ip_address) { /* Set attribute = length/type/value */ uint32_t one = 1; - struct rtattr a_dst = {RTA_LENGTH(ip_address_len(ip_address)), RTA_DST}; + struct rtattr a_dst = {RTA_LENGTH(ip_prefix_len(prefix)), RTA_DST}; struct rtattr a_ifid_lo = {RTA_LENGTH(sizeof(uint32_t)), RTA_OIF}; struct iovec iov[] = { {&msg, sizeof(msg)}, /* Ip address */ {&a_dst, sizeof(a_dst)}, - {(void *)&ip_address->buffer, ip_address_len(ip_address)}, + {(void *)&prefix->address.buffer, ip_prefix_len(prefix)}, /* Interface id */ {&a_ifid_lo, sizeof(a_ifid_lo)}, {&one, sizeof(one)}}; @@ -1129,7 +1129,7 @@ ERR_SOCKET: * ip -6 neigh add proxy 1:2::2 dev hicnc-cons-eth0 2>&1 | grep nei * */ -int _nl_add_neigh_proxy(const ip_address_t *ip_address, +int _nl_add_neigh_proxy(const ip_prefix_t *prefix, const uint32_t interface_id) { /* Buffer for holding the response, with appropriate casting on the header */ char buffer[BUFSIZE]; @@ -1147,21 +1147,21 @@ int _nl_add_neigh_proxy(const ip_address_t *ip_address, .hdr.nlmsg_type = RTM_NEWNEIGH, .hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK | NLM_F_EXCL, .hdr.nlmsg_seq = seq++, - .payload.ndm_family = ip_address->family, + .payload.ndm_family = prefix->family, .payload.ndm_ifindex = interface_id, .payload.ndm_state = NUD_PERMANENT, .payload.ndm_flags = NTF_PROXY, }; /* Message attributes = length/type/value */ - struct rtattr a_dst = {RTA_LENGTH(ip_address_len(ip_address)), NDA_DST}; + struct rtattr a_dst = {RTA_LENGTH(ip_prefix_len(prefix)), NDA_DST}; /* Iovec describing the packets */ struct iovec iov[] = { {&msg, sizeof(msg)}, /* Ip address */ {&a_dst, sizeof(a_dst)}, - {(void *)&ip_address->buffer, sizeof(ip_address->buffer)}, + {(void *)&prefix->address.buffer, sizeof(prefix->address.buffer)}, }; msg.hdr.nlmsg_len = iov_length(iov, ARRAY_SIZE(iov)); @@ -1199,7 +1199,7 @@ ERR: /* ip -6 route add 0:1::/64 dev hicn-if0 table 100 */ /* ip -6 route add 0:2::/64 dev hicn-if1 table 100 */ -int _nl_add_in_route_table(const ip_address_t *prefix, +int _nl_add_in_route_table(const ip_prefix_t *prefix, const uint32_t interface_id, const uint8_t table_id) { /* Buffer for holding the response, with appropriate casting on the header */ @@ -1219,7 +1219,7 @@ int _nl_add_in_route_table(const ip_address_t *prefix, .hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK | NLM_F_EXCL, .hdr.nlmsg_seq = seq++, .payload.rtm_family = prefix->family, - .payload.rtm_dst_len = prefix->prefix_len, + .payload.rtm_dst_len = prefix->len, .payload.rtm_src_len = 0, .payload.rtm_tos = 0, .payload.rtm_table = table_id, /* RT_TABLE_MAIN, etc. */ @@ -1231,7 +1231,7 @@ int _nl_add_in_route_table(const ip_address_t *prefix, }; /* Message attributes = length/type/value */ - struct rtattr a_dst = {RTA_LENGTH(ip_address_len(prefix)), RTA_DST}; + struct rtattr a_dst = {RTA_LENGTH(ip_prefix_len(prefix)), RTA_DST}; struct rtattr a_oif = {RTA_LENGTH(sizeof(uint32_t)), RTA_OIF}; /* Iovec describing the packets */ @@ -1239,7 +1239,7 @@ int _nl_add_in_route_table(const ip_address_t *prefix, {&msg, sizeof(msg)}, /* Destination prefix / ip address */ {&a_dst, sizeof(a_dst)}, - {(void *)&prefix->buffer, ip_address_len(prefix)}, + {(void *)&prefix->address.buffer, ip_prefix_len(prefix)}, /* Output interface */ {&a_oif, sizeof(a_oif)}, {(void *)&interface_id, sizeof(uint32_t)}, @@ -1283,9 +1283,9 @@ ERR: int _nl_add_in_route_table_s(const char *prefix, const uint32_t interface_id, const uint8_t table_id) { int rc; - ip_address_t ip_address; + ip_prefix_t ip_address; - rc = hicn_ip_pton(prefix, &ip_address); + rc = ip_prefix_pton(prefix, &ip_address); if (rc < 0) { return rc; } @@ -1298,7 +1298,7 @@ int _nl_add_in_route_s(const char *prefix, const uint32_t interface_id) { } /* ip -6 rule add from b001::/16 prio 0 table 100 */ -int _nl_add_prio_rule(const ip_address_t *ip_address, uint8_t address_family, +int _nl_add_prio_rule(const ip_prefix_t *prefix, uint8_t address_family, const uint32_t priority, const uint8_t table_id) { /* Buffer for holding the response, with appropriate casting on the header */ char buffer[BUFSIZE]; @@ -1318,7 +1318,7 @@ int _nl_add_prio_rule(const ip_address_t *ip_address, uint8_t address_family, .hdr.nlmsg_seq = seq++, .payload.family = address_family, //.payload.dst_len = , - .payload.src_len = ip_address ? ip_address->prefix_len : 0, + .payload.src_len = prefix ? prefix->len : 0, .payload.tos = 0, .payload.table = table_id, .payload.action = FR_ACT_TO_TBL, @@ -1331,17 +1331,17 @@ int _nl_add_prio_rule(const ip_address_t *ip_address, uint8_t address_family, goto ERR; } - if (ip_address) { + if (prefix) { /* Message attributes = length/type/value */ - struct rtattr a_src = {RTA_LENGTH(ip_address_len(ip_address)), FRA_SRC}; + struct rtattr a_src = {RTA_LENGTH(ip_prefix_len(prefix)), FRA_SRC}; struct rtattr a_prio = {RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY}; /* Iovec describing the packets */ struct iovec iov[] = { {&msg, sizeof(msg)}, - /* Source prefix / ip_address */ + /* Source prefix / prefix */ {&a_src, sizeof(a_src)}, - {(void *)&ip_address->buffer, ip_address_len(ip_address)}, + {(void *)&prefix->address.buffer, ip_prefix_len(prefix)}, /* Priority */ {&a_prio, sizeof(a_prio)}, {(void *)&priority, sizeof(uint32_t)}, @@ -1392,14 +1392,14 @@ ERR: return HICN_SOCKET_ERROR_UNSPEC; } -int _nl_add_lo_prio_rule(const ip_address_t *ip_address, uint8_t address_family, +int _nl_add_lo_prio_rule(const ip_prefix_t *prefix, uint8_t address_family, const uint32_t priority) { - return _nl_add_prio_rule(ip_address, address_family, priority, + return _nl_add_prio_rule(prefix, address_family, priority, RT_TABLE_LOCAL); } /* ip -6 rule del from all prio 0 table local */ -int _nl_del_prio_rule(const ip_address_t *ip_address, uint8_t address_family, +int _nl_del_prio_rule(const ip_prefix_t *prefix, uint8_t address_family, const uint32_t priority, const uint8_t table_id) { /* Buffer for holding the response, with appropriate casting on the header */ char buffer[BUFSIZE]; @@ -1419,7 +1419,7 @@ int _nl_del_prio_rule(const ip_address_t *ip_address, uint8_t address_family, .hdr.nlmsg_seq = seq++, .payload.family = address_family, //.payload.dst_len = , - .payload.src_len = ip_address ? ip_address->prefix_len : 0, + .payload.src_len = prefix ? prefix->len : 0, .payload.tos = 0, .payload.table = table_id, .payload.action = FR_ACT_TO_TBL, @@ -1433,16 +1433,16 @@ int _nl_del_prio_rule(const ip_address_t *ip_address, uint8_t address_family, } /* Message attributes = length/type/value */ - if (ip_address) { - struct rtattr a_src = {RTA_LENGTH(ip_address_len(ip_address)), FRA_SRC}; + if (prefix) { + struct rtattr a_src = {RTA_LENGTH(ip_prefix_len(prefix)), FRA_SRC}; struct rtattr a_prio = {RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY}; /* Iovec describing the packets */ struct iovec iov[] = { {&msg, sizeof(msg)}, - /* Source prefix / ip_address */ + /* Source prefix / prefix */ {&a_src, sizeof(a_src)}, - {(void *)&ip_address->buffer, ip_address_len(ip_address)}, + {(void *)&prefix->address.buffer, ip_prefix_len(prefix)}, /* Priority */ {&a_prio, sizeof(a_prio)}, {(void *)&priority, sizeof(uint32_t)}, @@ -1495,7 +1495,7 @@ ERR: return HICN_SOCKET_ERROR_UNSPEC; } -int _nl_del_lo_prio_rule(const ip_address_t *ip_address, uint8_t address_family, +int _nl_del_lo_prio_rule(const ip_prefix_t *ip_address, uint8_t address_family, const uint32_t priority) { return _nl_del_prio_rule(ip_address, address_family, priority, RT_TABLE_LOCAL); diff --git a/hicn-light/src/hicn/utils/CMakeLists.txt b/hicn-light/src/hicn/utils/CMakeLists.txt index dd9e12c15..1ab38deba 100644 --- a/hicn-light/src/hicn/utils/CMakeLists.txt +++ b/hicn-light/src/hicn/utils/CMakeLists.txt @@ -19,7 +19,6 @@ list(APPEND HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/commands.h ${CMAKE_CURRENT_SOURCE_DIR}/interface.h ${CMAKE_CURRENT_SOURCE_DIR}/interfaceSet.h - ${CMAKE_CURRENT_SOURCE_DIR}/policy.h ${CMAKE_CURRENT_SOURCE_DIR}/punting.h ${CMAKE_CURRENT_SOURCE_DIR}/token.h ${CMAKE_CURRENT_SOURCE_DIR}/utils.h @@ -30,7 +29,6 @@ list(APPEND SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/addressList.c ${CMAKE_CURRENT_SOURCE_DIR}/interface.c ${CMAKE_CURRENT_SOURCE_DIR}/interfaceSet.c - ${CMAKE_CURRENT_SOURCE_DIR}/policy.c ${CMAKE_CURRENT_SOURCE_DIR}/punting.c ${CMAKE_CURRENT_SOURCE_DIR}/utils.c ) diff --git a/hicn-light/src/hicn/utils/commands.h b/hicn-light/src/hicn/utils/commands.h index 223bbffaa..834da6259 100644 --- a/hicn-light/src/hicn/utils/commands.h +++ b/hicn-light/src/hicn/utils/commands.h @@ -31,7 +31,9 @@ #include <stdint.h> #include <stdlib.h> -#include <hicn/utils/policy.h> +#ifdef WITH_POLICY +#include <hicn/policy.h> +#endif /* WITH_POLICY */ typedef struct in6_addr ipv6_addr_t; typedef uint32_t ipv4_addr_t; @@ -113,9 +115,7 @@ typedef enum { ETHER_MODE, IP_MODE, HICN_MODE } listener_mode; typedef struct { char symbolic[16]; -#ifdef __linux__ char interfaceName[16]; -#endif union commandAddr address; uint16_t port; // uint16_t etherType; @@ -130,6 +130,7 @@ typedef struct { typedef struct { char symbolic[16]; + //char interfaceName[16]; union commandAddr remoteIp; union commandAddr localIp; uint16_t remotePort; @@ -165,6 +166,9 @@ typedef struct { add_connection_command connectionData; uint32_t connid; uint8_t state; + uint8_t admin_state; + char connectionName[16]; + char interfaceName[16]; } list_connections_command; // SIZE=64 @@ -273,6 +277,8 @@ typedef struct { typedef struct { union commandAddr address; + char listenerName[16]; + char interfaceName[16]; uint32_t connid; uint16_t port; uint8_t addressType; @@ -300,6 +306,7 @@ typedef struct { typedef struct { char symbolicOrConnid[16]; uint8_t admin_state; + uint16_t pad16; } connection_set_admin_state_command; #ifdef WITH_POLICY diff --git a/hicn-light/src/hicn/utils/policy.c b/hicn-light/src/hicn/utils/policy.c deleted file mode 100644 index 6c8651ee3..000000000 --- a/hicn-light/src/hicn/utils/policy.c +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2017-2019 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * \file policy.h - * \brief Implementation of policy description - */ - -#include <stdio.h> -#include "policy.h" - -const char * policy_tag_str[] = { - #define _(x, y) [POLICY_TAG_ ## x] = STRINGIZE(x), - foreach_policy_tag - #undef _ -}; - -const char policy_tag_short_str[] = { - #define _(x, y) [POLICY_TAG_ ## x] = y, - foreach_policy_tag - #undef _ -}; - -const char * policy_state_str[] = { - #define _(x) [POLICY_STATE_ ## x] = STRINGIZE(x), - foreach_policy_state - #undef _ -}; - -int -policy_tag_state_snprintf(char * s, size_t size, const policy_tag_state_t * tag_state) -{ - char *cur = s; - int rc; - - if (tag_state->disabled > 1) - return -1; - - rc = snprintf(cur, s + size - cur, "%s%s", (tag_state->disabled == 1) ? "!" : "", policy_state_str[tag_state->state]); - if (rc < 0) - return rc; - cur += rc; - if (size != 0 && cur >= s + size) - return cur - s; - - return cur - s; -} diff --git a/hicn-light/src/hicn/utils/policy.h b/hicn-light/src/hicn/utils/policy.h deleted file mode 100644 index 993fdca44..000000000 --- a/hicn-light/src/hicn/utils/policy.h +++ /dev/null @@ -1,232 +0,0 @@ -/* - * Copyright (c) 2017-2019 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * \file policy.h - * \brief Policy description - */ -#ifndef HICN_POLICY_H -#define HICN_POLICY_H - -#include <netinet/in.h> // INET*_ADDRSTRLEN -#include <string.h> // strcasecmp -#include "token.h" - -/* POLICY TAG */ - -#define foreach_policy_tag \ - /* Interface type */ \ - _(WIRED, 'E') \ - _(WIFI, 'W') \ - _(CELLULAR, 'C') \ - /* QoS */ \ - _(BEST_EFFORT, 'b') \ - _(REALTIME, 'r') \ - _(MULTIPATH, 'M') \ - /* Security */ \ - _(TRUSTED, 'T') - -typedef enum { -#define _(x, y) POLICY_TAG_ ## x, -foreach_policy_tag -#undef _ - POLICY_TAG_N -} policy_tag_t; - -#define MAXSZ_POLICY_TAG_ 11 -#define MAXSZ_POLICY_TAG MAXSZ_POLICY_TAG_ + 1 - -extern const char * policy_tag_str[]; -extern const char policy_tag_short_str[]; - -static inline -policy_tag_t -policy_tag_from_str(const char * str) -{ -#define _(x, y) if (strcasecmp(str, policy_tag_str[POLICY_TAG_ ## x] ) == 0) { return POLICY_TAG_ ## x; } else -foreach_policy_tag -#undef _ - return POLICY_TAG_N; -} - -/* POLICY_TAGS */ - -typedef int policy_tags_t; - -static inline -void policy_tags_add(policy_tags_t * tags, policy_tag_t tag) -{ - *tags |= (1 << tag); -} - -static inline -void policy_tags_remove(policy_tags_t * tags, policy_tag_t tag) -{ - *tags &= ~(1 << tag); -} - -static inline -int policy_tags_has(policy_tags_t tags, policy_tag_t tag) -{ - return tags & (1 << tag); -} - -#define POLICY_TAGS_EMPTY 0 - -static inline -int -policy_tags_snprintf(char * s, size_t size, policy_tags_t tags) -{ -#define _(x, y) s[POLICY_TAG_ ## x] = policy_tags_has(tags, POLICY_TAG_ ## x) ? y : '.'; -foreach_policy_tag -#undef _ - s[POLICY_TAG_N] = '\0'; - return POLICY_TAG_N + 1; -} - -#define MAXSZ_POLICY_TAGS_ POLICY_TAG_N -#define MAXSZ_POLICY_TAGS MAXSZ_POLICY_TAGS_ + 1 - -/* POLICY STATE */ - -#define foreach_policy_state \ - _(NEUTRAL) \ - _(REQUIRE) \ - _(PREFER) \ - _(AVOID) \ - _(PROHIBIT) \ - _(N) - -typedef enum { -#define _(x) POLICY_STATE_ ## x, -foreach_policy_state -#undef _ -} policy_state_t; - -#define MAXSZ_POLICY_STATE_ 8 -#define MAXSZ_POLICY_STATE MAXSZ_POLICY_STATE_ + 1 - -extern const char * policy_state_str[]; - - -/* POLICY TAG STATE */ - -typedef struct { - policy_state_t state; - uint8_t disabled; -} policy_tag_state_t; - -#define MAXSZ_POLICY_TAG_STATE_ 8 -#define MAXSZ_POLICY_TAG_STATE MAXSZ_POLICY_TAG_STATE_ + 1 - -int policy_tag_state_snprintf(char * s, size_t size, const policy_tag_state_t * tag_state); - - -/* INTERFACE STATS */ - -typedef struct { - float throughput; - float latency; - float loss_rate; -} interface_stats_t; - -#define INTERFACE_STATS_NONE { \ - .throughput = 0, \ - .latency = 0, \ - .loss_rate = 0, \ -} - - -/* POLICY STATS */ - -typedef struct { - interface_stats_t wired; - interface_stats_t wifi; - interface_stats_t cellular; - interface_stats_t all; -} policy_stats_t; - -#define POLICY_STATS_NONE { \ - .wired = INTERFACE_STATS_NONE, \ - .wifi = INTERFACE_STATS_NONE, \ - .cellular = INTERFACE_STATS_NONE, \ - .all = INTERFACE_STATS_NONE, \ -} - -typedef struct { - uint32_t num_packets; - uint32_t num_bytes; - uint32_t num_losses; - uint32_t latency_idle; -} interface_counters_t; - -#define INTERFACE_COUNTERS_NONE { \ - .num_packets = 0, \ - .num_bytes = 0, \ - .num_losses = 0, \ - .latency_idle = 0, \ -} - -typedef struct { - interface_counters_t wired; - interface_counters_t wifi; - interface_counters_t cellular; - interface_counters_t all; - uint64_t last_update; -} policy_counters_t; - -#define POLICY_COUNTERS_NONE (policy_counters_t) { \ - .wired = INTERFACE_COUNTERS_NONE, \ - .wifi = INTERFACE_COUNTERS_NONE, \ - .cellular = INTERFACE_COUNTERS_NONE, \ - .all = INTERFACE_COUNTERS_NONE, \ - .last_update = 0, \ -} - -/* POLICY */ - -#define APP_NAME_LEN 128 - -typedef struct { - char app_name[APP_NAME_LEN]; - policy_tag_state_t tags[POLICY_TAG_N]; - policy_stats_t stats; -} policy_t; - -static const policy_t POLICY_NONE = { - .app_name = { 0 }, - .tags = { -#define _(x, y) [POLICY_TAG_ ## x] = { POLICY_STATE_NEUTRAL, 0 }, -foreach_policy_tag -#undef _ - }, - .stats = POLICY_STATS_NONE, -}; - - -/* POLICY DESCRIPTION */ - -#define PFX_STRLEN 4 /* eg. /128 */ - -typedef struct { - int family; - union { - char ipv4_prefix[INET_ADDRSTRLEN + PFX_STRLEN]; - char ipv6_prefix[INET6_ADDRSTRLEN + PFX_STRLEN]; - }; - policy_t policy; -} policy_description_t; - -#endif /* HICN_POLICY_H */ |