diff options
author | Jordan Augé <jordan.auge+fdio@cisco.com> | 2019-07-26 23:20:30 +0200 |
---|---|---|
committer | Mauro Sardara <msardara@cisco.com> | 2019-07-29 17:13:35 +0200 |
commit | 0a1c6b5565e20167d1f1f33a5a8b597f420b18b0 (patch) | |
tree | 98c5da8f231fbd3dc2ce6502040e29c8333d9ffc /hicn-light/src/hicn/io | |
parent | 05ca0aa8f612ee48fb66d4dbebe596b7f1e03181 (diff) |
[HICN-252] Add per-application policy framework to hicn-light forwarder
Change-Id: I0531cd7a7de179581295ae34766c81cd9cf3e172
Signed-off-by: Jordan Augé <jordan.auge+fdio@cisco.com>
Signed-off-by: Mauro Sardara <msardara@cisco.com>
Co-authored-by: Mauro Sardara <msardara@cisco.com>
Diffstat (limited to 'hicn-light/src/hicn/io')
-rw-r--r-- | hicn-light/src/hicn/io/hicnConnection.c | 2 | ||||
-rw-r--r-- | hicn-light/src/hicn/io/hicnListener.c | 281 | ||||
-rw-r--r-- | hicn-light/src/hicn/io/listener.h | 5 | ||||
-rw-r--r-- | hicn-light/src/hicn/io/udpListener.c | 146 |
4 files changed, 237 insertions, 197 deletions
diff --git a/hicn-light/src/hicn/io/hicnConnection.c b/hicn-light/src/hicn/io/hicnConnection.c index 6193e8696..124885b33 100644 --- a/hicn-light/src/hicn/io/hicnConnection.c +++ b/hicn-light/src/hicn/io/hicnConnection.c @@ -218,7 +218,6 @@ static void _destroy(IoOperations **opsPtr) { __func__, "HicnConnection %p destroyed", (void *)hicnConnState); } - // XXX // do not close hicListenerSocket, the listener will close // that when its done // should I say something to libhicn? @@ -279,7 +278,6 @@ static bool _send(IoOperations *ops, const Address *dummy, Message *message) { _HicnState *hicnConnState = (_HicnState *)ioOperations_GetClosure(ops); // NAT for HICN - // XXX if (message_GetType(message) == MessagePacketType_ContentObject) { // this is a data packet. We need to put the remote address in the // destination field diff --git a/hicn-light/src/hicn/io/hicnListener.c b/hicn-light/src/hicn/io/hicnListener.c index 40cdadcd6..e8cab9aca 100644 --- a/hicn-light/src/hicn/io/hicnListener.c +++ b/hicn-light/src/hicn/io/hicnListener.c @@ -29,12 +29,6 @@ #include <hicn/core/connection.h> #include <hicn/core/connectionTable.h> #include <hicn/core/forwarder.h> -#ifdef WITH_MAPME -#include <hicn/config/symbolicNameTable.h> -#include <hicn/core/mapMe.h> -#include <hicn/core/message.h> -#include <hicn/io/hicnTunnel.h> -#endif /* WITH_MAPME */ #include <parc/algol/parc_Memory.h> #include <parc/algol/parc_Network.h> #include <parc/assert/parc_Assert.h> @@ -82,13 +76,19 @@ static unsigned _getInterfaceIndex(const ListenerOps *ops); static const Address *_getListenAddress(const ListenerOps *ops); static EncapType _getEncapType(const ListenerOps *ops); static int _getSocket(const ListenerOps *ops); - -static ListenerOps _hicnTemplate = {.context = NULL, - .destroy = &_destroy, - .getInterfaceIndex = &_getInterfaceIndex, - .getListenAddress = &_getListenAddress, - .getEncapType = &_getEncapType, - .getSocket = &_getSocket}; +static unsigned _createNewConnection(ListenerOps *listener, int fd, const AddressPair *pair); +static const Connection * _lookupConnection(ListenerOps * listener, const AddressPair *pair); + +static ListenerOps _hicnTemplate = { + .context = NULL, + .destroy = &_destroy, + .getInterfaceIndex = &_getInterfaceIndex, + .getListenAddress = &_getListenAddress, + .getEncapType = &_getEncapType, + .getSocket = &_getSocket, + .createConnection = &_createNewConnection, + .lookupConnection = &_lookupConnection, +}; static void _hicnListener_readcb(int fd, PARCEventType what, void *hicnVoid); @@ -177,12 +177,6 @@ ListenerOps *hicnListener_CreateInet(Forwarder *forwarder, char *symbolic, parcAssertFalse(failure, "fcntl failed to set file descriptor flags (%d)", errno); - hicn->hicn_event = dispatcher_CreateNetworkEvent( - forwarder_GetDispatcher(forwarder), true, _hicnListener_readcb, - (void *)hicn, hicn->hicn_fd); - dispatcher_StartNetworkEvent(forwarder_GetDispatcher(forwarder), - hicn->hicn_event); - ListenerOps *ops = parcMemory_AllocateAndClear(sizeof(ListenerOps)); parcAssertNotNull(ops, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(ListenerOps)); @@ -190,6 +184,13 @@ ListenerOps *hicnListener_CreateInet(Forwarder *forwarder, char *symbolic, memcpy(ops, &_hicnTemplate, sizeof(ListenerOps)); ops->context = hicn; + hicn->hicn_event = dispatcher_CreateNetworkEvent( + forwarder_GetDispatcher(forwarder), true, _hicnListener_readcb, + (void *)ops, hicn->hicn_fd); + dispatcher_StartNetworkEvent(forwarder_GetDispatcher(forwarder), + hicn->hicn_event); + + if (logger_IsLoggable(hicn->logger, LoggerFacility_IO, PARCLogLevel_Debug)) { logger_Log(hicn->logger, LoggerFacility_IO, PARCLogLevel_Debug, __func__, "HicnListener %s created", symbolic); @@ -267,12 +268,6 @@ ListenerOps *hicnListener_CreateInet6(Forwarder *forwarder, char *symbolic, parcAssertFalse(failure, "fcntl failed to set file descriptor flags (%d)", errno); - hicn->hicn_event = dispatcher_CreateNetworkEvent( - forwarder_GetDispatcher(forwarder), true, _hicnListener_readcb, - (void *)hicn, hicn->hicn_fd); - dispatcher_StartNetworkEvent(forwarder_GetDispatcher(forwarder), - hicn->hicn_event); - ListenerOps *ops = parcMemory_AllocateAndClear(sizeof(ListenerOps)); parcAssertNotNull(ops, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(ListenerOps)); @@ -280,6 +275,12 @@ ListenerOps *hicnListener_CreateInet6(Forwarder *forwarder, char *symbolic, memcpy(ops, &_hicnTemplate, sizeof(ListenerOps)); ops->context = hicn; + hicn->hicn_event = dispatcher_CreateNetworkEvent( + forwarder_GetDispatcher(forwarder), true, _hicnListener_readcb, + (void *)ops, hicn->hicn_fd); + dispatcher_StartNetworkEvent(forwarder_GetDispatcher(forwarder), + hicn->hicn_event); + if (logger_IsLoggable(hicn->logger, LoggerFacility_IO, PARCLogLevel_Debug)) { logger_Log(hicn->logger, LoggerFacility_IO, PARCLogLevel_Debug, __func__, "HicnListener %s created", symbolic); @@ -405,7 +406,6 @@ static void _hicnListener_Destroy(HicnListener **listenerPtr) { HicnListener *hicn = *listenerPtr; - // close(hicn->hicn_fd); //XXX close the fd in the hicnlib (detroy listener?) dispatcher_DestroyNetworkEvent(forwarder_GetDispatcher(hicn->forwarder), &hicn->hicn_event); logger_Release(&hicn->logger); @@ -454,8 +454,6 @@ static void _readFrameToDiscard(HicnListener *hicn, int fd) { "Discarded frame from fd %d", fd); } } else if (nread < 0) { - printf("Error trying to discard frame from fd %d: (%d) %s", fd, errno, - strerror(errno)); if (logger_IsLoggable(hicn->logger, LoggerFacility_IO, PARCLogLevel_Error)) { logger_Log(hicn->logger, LoggerFacility_IO, PARCLogLevel_Error, __func__, @@ -465,8 +463,9 @@ static void _readFrameToDiscard(HicnListener *hicn, int fd) { } } -static unsigned _createNewConnection(HicnListener *hicn, int fd, +static unsigned _createNewConnection(ListenerOps * listener, int fd, const AddressPair *pair) { + HicnListener * hicn = (HicnListener *)listener->context; bool isLocal = false; // udpConnection_Create takes ownership of the pair @@ -479,8 +478,11 @@ static unsigned _createNewConnection(HicnListener *hicn, int fd, return connid; } -const Connection *_findConnectionFromPacket(HicnListener *hicn, - Address *packetSourceAddress) { +static const Connection * _lookupConnection(ListenerOps * listener, + const AddressPair *pair) { + HicnListener * hicn = (HicnListener*)listener->context; + const Address * packetSourceAddress = addressPair_GetLocal(pair); + const Connection *conn = NULL; if (hicn->connection_id != -1) { conn = connectionTable_FindById( @@ -500,7 +502,6 @@ const Connection *_findConnectionFromPacket(HicnListener *hicn, return conn; } -#if 0 static Address *_createAddressFromPacket(uint8_t *msgBuffer) { Address *packetAddr = NULL; if (messageHandler_GetIPPacketType(msgBuffer) == IPv6_TYPE) { @@ -522,25 +523,33 @@ static Address *_createAddressFromPacket(uint8_t *msgBuffer) { } return packetAddr; } -#endif -static void _handleProbeMessage(HicnListener *hicn, uint8_t *msgBuffer) { +static void _handleProbeMessage(ListenerOps * listener, uint8_t *msgBuffer) { + HicnListener * hicn = (HicnListener *)listener->context; + Address *packetAddr = _createAddressFromPacket(msgBuffer); + AddressPair * pair = addressPair_Create(packetAddr, /* dummy */ hicn->localAddress); - if (packetAddr != NULL) { - const Connection *conn = _findConnectionFromPacket(hicn, packetAddr); - if (conn != NULL) { - // we drop all the probes for a connection that does not exists - connection_HandleProbe((Connection *)conn, msgBuffer, - forwarder_GetTicks(hicn->forwarder)); - } - } + if (!packetAddr) + goto DROP; + + // we drop all the probes for a connection that does not exists + const Connection *conn = _lookupConnection(listener, pair); + if (!conn) + goto DROP; + + connection_HandleProbe((Connection *)conn, msgBuffer, + forwarder_GetTicks(hicn->forwarder)); +DROP: + addressPair_Release(&pair); addressDestroy(&packetAddr); parcMemory_Deallocate((void **)&msgBuffer); } -static void _handleWldrNotification(HicnListener *hicn, uint8_t *msgBuffer) { +static void _handleWldrNotification(ListenerOps *listener, uint8_t *msgBuffer) { + HicnListener * hicn = (HicnListener *)listener->context; + Address *packetAddr = _createAddressFromPacket(msgBuffer); if (packetAddr == NULL) { @@ -548,14 +557,18 @@ static void _handleWldrNotification(HicnListener *hicn, uint8_t *msgBuffer) { return; } - const Connection *conn = _findConnectionFromPacket(hicn, packetAddr); + AddressPair * pair = addressPair_Create(packetAddr, /* dummy */ hicn->localAddress); + + const Connection *conn = _lookupConnection(listener, pair); + + addressPair_Release(&pair); + addressDestroy(&packetAddr); + if (conn == NULL) { - addressDestroy(&packetAddr); + parcMemory_Deallocate((void **)&msgBuffer); return; } - addressDestroy(&packetAddr); - Message *message = message_CreateFromByteArray( connection_GetConnectionId(conn), msgBuffer, MessagePacketType_WldrNotification, forwarder_GetTicks(hicn->forwarder), @@ -566,75 +579,87 @@ static void _handleWldrNotification(HicnListener *hicn, uint8_t *msgBuffer) { message_Release(&message); } -#ifdef WITH_MAPME -static void _handleMapMe(HicnListener *hicn, int fd, uint8_t *msgBuffer) { - Address *packetAddr = _createAddressFromPacket(msgBuffer); - - if (packetAddr == NULL) { - parcMemory_Deallocate((void **)&msgBuffer); - return; - } - - const Connection *conn = _findConnectionFromPacket(hicn, packetAddr); - unsigned conn_id; - if (conn == NULL) { - /* Unlike the interest path, we don't create virtual connections bound - * on the listener, whose only interest is to send data, but full - * tunnels to be able to route interests - * - * packetAddr is the remote address, we need to ask the lib for our - * local address - * hicn->localAddress is None as the interest is received by the main - * listener. - */ - printf("MapMe, connection did not exist, creating\n"); - - /* Populate remote_address through packetAddr */ - struct sockaddr_in6 sockaddr; // XXX IPv6 only - addressGetInet6(packetAddr, &sockaddr); - ip_address_t remote_address = {.family = AF_INET6, - .prefix_len = IPV6_ADDR_LEN_BITS}; - memcpy(&remote_address.buffer, &sockaddr.sin6_addr, - ip_address_len(&remote_address)); - - /* Get local address through libhicn */ - ip_address_t local_address; - int rc = hicn_get_local_address(&remote_address, &local_address); - if (rc < 0) { - printf("Error getting local address. Discarded mapme packet.\n"); - return; - } +static const +AddressPair * +_createRecvAddressPairFromPacket(const uint8_t *msgBuffer) { + Address *packetSrcAddr = NULL; /* This one is in the packet */ + Address *localAddr = NULL; /* This one is to be determined */ + if (messageHandler_GetIPPacketType(msgBuffer) == IPv6_TYPE) { struct sockaddr_in6 addr_in6; addr_in6.sin6_family = AF_INET6; addr_in6.sin6_port = htons(1234); addr_in6.sin6_flowinfo = 0; addr_in6.sin6_scope_id = 0; - memcpy(&addr_in6.sin6_addr, (struct in6_addr *)&(local_address.buffer), 16); + memcpy(&addr_in6.sin6_addr, + (struct in6_addr *)messageHandler_GetSource(msgBuffer), 16); + packetSrcAddr = addressCreateFromInet6(&addr_in6); + + /* We now determine the local address used to reach the packet src address */ + int sock = socket (AF_INET6, SOCK_DGRAM, 0); + if (sock < 0) + goto ERR; + + struct sockaddr_in6 remote, local; + memset(&remote, 0, sizeof(remote)); + remote.sin6_family = AF_INET6; + remote.sin6_addr = addr_in6.sin6_addr; + remote.sin6_port = htons(1234); + + socklen_t locallen = sizeof(local); + if (connect(sock, (const struct sockaddr*)&remote, sizeof(remote)) == -1) + goto ERR; + if (getsockname(sock, (struct sockaddr*) &local, &locallen) == -1) + goto ERR; - Address *localAddr = addressCreateFromInet6(&addr_in6); - IoOperations *ops = - hicnTunnel_Create(hicn->forwarder, localAddr, packetAddr); + local.sin6_port = htons(1234); + localAddr = addressCreateFromInet6(&local); - if (!ops) { - printf("Error creating tunnel. Discarded mapme packet.\n"); - return; + close(sock); + + } else if (messageHandler_GetIPPacketType(msgBuffer) == IPv4_TYPE) { + struct sockaddr_in addr_in; + addr_in.sin_family = AF_INET; + addr_in.sin_port = htons(1234); + memcpy(&addr_in.sin_addr, + (struct in_addr *)messageHandler_GetSource(msgBuffer), 4); + packetSrcAddr = addressCreateFromInet(&addr_in); + + /* We now determine the local address used to reach the packet src address */ + + int sock = socket (AF_INET, SOCK_DGRAM, 0); + if (sock < 0) { + perror("Socket error"); + goto ERR; } - conn = connection_Create(ops); + struct sockaddr_in remote, local; + memset(&remote, 0, sizeof(remote)); + remote.sin_family = AF_INET; + remote.sin_addr = addr_in.sin_addr; + remote.sin_port = htons(1234); - connectionTable_Add(forwarder_GetConnectionTable(hicn->forwarder), - (Connection *)conn); - } - conn_id = connection_GetConnectionId(conn); + socklen_t locallen = sizeof(local); + if (connect(sock, (const struct sockaddr*)&remote, sizeof(remote)) == -1) + goto ERR; + if (getsockname(sock, (struct sockaddr*) &local, &locallen) == -1) + goto ERR; - addressDestroy(&packetAddr); + local.sin_port = htons(1234); + localAddr = addressCreateFromInet(&local); + + close(sock); + } + /* As this is a receive pair, we swap src and dst */ + return addressPair_Create(localAddr, packetSrcAddr); - forwarder_ProcessMapMe(hicn->forwarder, msgBuffer, conn_id); +ERR: + perror("Socket error"); + return NULL; } -#endif /* WITH_MAPME */ -static Message *_readMessage(HicnListener *hicn, int fd, uint8_t *msgBuffer) { +static Message *_readMessage(ListenerOps * listener, int fd, uint8_t *msgBuffer) { + HicnListener * hicn = (HicnListener*)listener->context; Message *message = NULL; ssize_t readLength = read(fd, msgBuffer, MTU_SIZE); @@ -667,11 +692,13 @@ static Message *_readMessage(HicnListener *hicn, int fd, uint8_t *msgBuffer) { // run time) uses as a local address 0::0, so the main tun pktType = MessagePacketType_Interest; Address *packetAddr = _createAddressFromPacket(msgBuffer); - const Connection *conn = _findConnectionFromPacket(hicn, packetAddr); + AddressPair *pair_find = addressPair_Create(packetAddr, /* dummy */ hicn->localAddress); + const Connection *conn = _lookupConnection(listener, pair_find); + addressPair_Release(&pair_find); if (conn == NULL) { AddressPair *pair = addressPair_Create(hicn->localAddress, packetAddr); - connid = _createNewConnection(hicn, fd, pair); + connid = _createNewConnection(listener, fd, pair); addressPair_Release(&pair); } else { connid = connection_GetConnectionId(conn); @@ -690,42 +717,52 @@ static Message *_readMessage(HicnListener *hicn, int fd, uint8_t *msgBuffer) { parcMemory_Deallocate((void **)&msgBuffer); } } else if (messageHandler_IsWldrNotification(msgBuffer)) { - _handleWldrNotification(hicn, msgBuffer); + _handleWldrNotification(listener, msgBuffer); } else if (messageHandler_IsLoadBalancerProbe(msgBuffer)) { - _handleProbeMessage(hicn, msgBuffer); -#ifdef WITH_MAPME - } else if (mapMe_isMapMe(msgBuffer)) { - /* This function triggers the handling of the MAP-Me message, and we - * will return NULL so as to terminate the processing of this - * msgBuffer. */ - _handleMapMe(hicn, fd, msgBuffer); -#endif /* WITH_MAPME */ - } + _handleProbeMessage(listener, msgBuffer); + } else { + const AddressPair * pair = _createRecvAddressPairFromPacket(msgBuffer); + if (!pair) + goto ERR; + + /* Find connection and eventually create it */ + const Connection * conn = connectionTable_FindByAddressPair( + forwarder_GetConnectionTable(hicn->forwarder), pair); + unsigned conn_id; + if (conn == NULL) { + conn_id = listener->createConnection(listener, fd, pair); + } else { + conn_id = connection_GetConnectionId(conn); + } - if (messageHandler_handleHooks(hicn->forwarder, hicn->connection_id, - hicn->localAddress, msgBuffer)) - goto END; + messageHandler_handleHooks(hicn->forwarder, msgBuffer, conn_id); + +ERR: + parcMemory_Deallocate((void **)&msgBuffer); + + } -END: return message; } -static void _receivePacket(HicnListener *hicn, int fd) { +static void _receivePacket(ListenerOps * listener, int fd) { + HicnListener * hicn = (HicnListener*)listener->context; Message *msg = NULL; uint8_t *msgBuffer = parcMemory_AllocateAndClear(MTU_SIZE); - msg = _readMessage(hicn, fd, msgBuffer); + msg = _readMessage(listener, fd, msgBuffer); if (msg) { forwarder_Receive(hicn->forwarder, msg); } } -static void _hicnListener_readcb(int fd, PARCEventType what, void *hicnVoid) { - HicnListener *hicn = (HicnListener *)hicnVoid; +static void _hicnListener_readcb(int fd, PARCEventType what, void *listener_void) { + ListenerOps * listener = (ListenerOps *)listener_void; + HicnListener *hicn = (HicnListener *)listener->context; if (hicn->inetFamily == IPv4 || hicn->inetFamily == IPv6) { if (what & PARCEventType_Read) { - _receivePacket(hicn, fd); + _receivePacket(listener, fd); } } else { _readFrameToDiscard(hicn, fd); diff --git a/hicn-light/src/hicn/io/listener.h b/hicn-light/src/hicn/io/listener.h index 3d195ee88..ef1955b12 100644 --- a/hicn-light/src/hicn/io/listener.h +++ b/hicn-light/src/hicn/io/listener.h @@ -27,6 +27,8 @@ #define listener_h #include <hicn/utils/address.h> +#include <hicn/io/addressPair.h> +#include <hicn/core/connection.h> struct listener_ops; typedef struct listener_ops ListenerOps; @@ -101,5 +103,8 @@ struct listener_ops { * @endcode */ int (*getSocket)(const ListenerOps *ops); + + unsigned (*createConnection)(ListenerOps *listener, int fd, const AddressPair *pair); + const Connection * (*lookupConnection)(ListenerOps * listener, const AddressPair *pair); }; #endif // listener_h diff --git a/hicn-light/src/hicn/io/udpListener.c b/hicn-light/src/hicn/io/udpListener.c index 3dd4b466c..32f633984 100644 --- a/hicn-light/src/hicn/io/udpListener.c +++ b/hicn-light/src/hicn/io/udpListener.c @@ -36,10 +36,6 @@ #include <hicn/core/forwarder.h> #include <hicn/core/messagePacketType.h> -#ifdef WITH_MAPME -#include <hicn/core/mapMe.h> -#endif /* WITH_MAPME */ - #define IPv4 4 #define IPv6 6 @@ -60,15 +56,21 @@ static unsigned _getInterfaceIndex(const ListenerOps *ops); static const Address *_getListenAddress(const ListenerOps *ops); static EncapType _getEncapType(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); + +static ListenerOps udpTemplate = { + .context = NULL, + .destroy = &_destroy, + .getInterfaceIndex = &_getInterfaceIndex, + .getListenAddress = &_getListenAddress, + .getEncapType = &_getEncapType, + .getSocket = &_getSocket, + .createConnection = &_createNewConnection, + .lookupConnection = &_lookupConnection, +}; -static ListenerOps udpTemplate = {.context = NULL, - .destroy = &_destroy, - .getInterfaceIndex = &_getInterfaceIndex, - .getListenAddress = &_getListenAddress, - .getEncapType = &_getEncapType, - .getSocket = &_getSocket}; - -static void _readcb(int fd, PARCEventType what, void *udpVoid); +static void _readcb(int fd, PARCEventType what, void * listener_void); #ifdef __linux__ ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, @@ -123,11 +125,6 @@ ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, setsockopt(udp->udp_socket, SOL_SOCKET, SO_BINDTODEVICE, interfaceName, strlen(interfaceName) + 1); #endif - udp->udp_event = - dispatcher_CreateNetworkEvent(forwarder_GetDispatcher(forwarder), true, - _readcb, (void *)udp, udp->udp_socket); - dispatcher_StartNetworkEvent(forwarder_GetDispatcher(forwarder), - udp->udp_event); ops = parcMemory_AllocateAndClear(sizeof(ListenerOps)); parcAssertNotNull(ops, "parcMemory_AllocateAndClear(%zu) returned NULL", @@ -135,6 +132,12 @@ ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, memcpy(ops, &udpTemplate, sizeof(ListenerOps)); ops->context = udp; + udp->udp_event = + dispatcher_CreateNetworkEvent(forwarder_GetDispatcher(forwarder), true, + _readcb, (void*)ops, udp->udp_socket); + dispatcher_StartNetworkEvent(forwarder_GetDispatcher(forwarder), + udp->udp_event); + if (logger_IsLoggable(udp->logger, LoggerFacility_IO, PARCLogLevel_Debug)) { char *str = addressToString(udp->localAddress); logger_Log(udp->logger, LoggerFacility_IO, PARCLogLevel_Debug, __func__, @@ -214,18 +217,19 @@ ListenerOps *udpListener_CreateInet(Forwarder *forwarder, setsockopt(udp->udp_socket, SOL_SOCKET, SO_BINDTODEVICE, interfaceName, strlen(interfaceName) + 1); #endif - udp->udp_event = - dispatcher_CreateNetworkEvent(forwarder_GetDispatcher(forwarder), true, - _readcb, (void *)udp, udp->udp_socket); - dispatcher_StartNetworkEvent(forwarder_GetDispatcher(forwarder), - udp->udp_event); - ops = parcMemory_AllocateAndClear(sizeof(ListenerOps)); parcAssertNotNull(ops, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(ListenerOps)); memcpy(ops, &udpTemplate, sizeof(ListenerOps)); ops->context = udp; + udp->udp_event = + dispatcher_CreateNetworkEvent(forwarder_GetDispatcher(forwarder), true, + _readcb, (void *)ops, udp->udp_socket); + dispatcher_StartNetworkEvent(forwarder_GetDispatcher(forwarder), + udp->udp_event); + + if (logger_IsLoggable(udp->logger, LoggerFacility_IO, PARCLogLevel_Debug)) { char *str = addressToString(udp->localAddress); logger_Log(udp->logger, LoggerFacility_IO, PARCLogLevel_Debug, __func__, @@ -341,28 +345,12 @@ static AddressPair *_constructAddressPair(UdpListener *udp, return pair; } -/** - * @function _lookupConnectionId - * @abstract Lookup a connection in the connection table - * @discussion - * Looks up the connection in the connection table and returns the connection - * id if it exists. - * - * @param outputConnectionIdPtr is the output parameter - * @return true if connection found and outputConnectionIdPtr set - */ -static bool _lookupConnectionId(UdpListener *udp, AddressPair *pair, - unsigned *outputConnectionIdPtr) { +static const Connection * _lookupConnection(ListenerOps * listener, + const AddressPair *pair) { + UdpListener * udp = (UdpListener *)listener->context; ConnectionTable *connTable = forwarder_GetConnectionTable(udp->forwarder); + return connectionTable_FindByAddressPair(connTable, pair); - const Connection *conn = connectionTable_FindByAddressPair(connTable, pair); - if (conn) { - *outputConnectionIdPtr = connection_GetConnectionId(conn); - return true; - } else { - *outputConnectionIdPtr = 0; - return false; - } } /** @@ -378,8 +366,10 @@ static bool _lookupConnectionId(UdpListener *udp, AddressPair *pair, * @return The connection id for the new connection */ -static unsigned _createNewConnection(UdpListener *udp, int fd, +static unsigned _createNewConnection(ListenerOps * listener, int fd, const AddressPair *pair) { + UdpListener * udp = (UdpListener *)listener->context; + //check it the connection is local bool isLocal = false; const Address *localAddress = addressPair_GetLocal(pair); @@ -428,13 +418,23 @@ static void _handleWldrNotification(UdpListener *udp, unsigned connId, message_Release(&message); } -static Message *_readMessage(UdpListener *udp, int fd, +static Message *_readMessage(ListenerOps * ops, int fd, AddressPair *pair, uint8_t * packet, bool * processed) { + UdpListener * udp = (UdpListener *)ops->context; Message *message = NULL; - unsigned connid = 0; - bool foundConnection = _lookupConnectionId(udp, pair, &connid); + unsigned connid; + bool foundConnection; + + const Connection *conn = _lookupConnection(ops, pair); + if (conn) { + connid = connection_GetConnectionId(conn); + foundConnection = true; + } else { + connid = 0; + foundConnection = false; + } if (messageHandler_IsTCP(packet)) { *processed = true; @@ -449,7 +449,7 @@ static Message *_readMessage(UdpListener *udp, int fd, } else if (messageHandler_IsInterest(packet)) { pktType = MessagePacketType_Interest; if (!foundConnection) { - connid = _createNewConnection(udp, fd, pair); + connid = _createNewConnection(ops, fd, pair); } } else { printf("Got a packet that is not a data nor an interest, drop it!\n"); @@ -470,25 +470,20 @@ static Message *_readMessage(UdpListener *udp, int fd, } else if (messageHandler_IsLoadBalancerProbe(packet)) { *processed = true; _handleProbeMessage(udp, packet); -#ifdef WITH_MAPME - } else if (mapMe_isMapMe(packet)) { - *processed = true; - forwarder_ProcessMapMe(udp->forwarder, packet, connid); -#endif /* WITH_MAPME */ - } + } else { + /* Generic hook handler */ + if (!foundConnection) + connid = _createNewConnection(ops, fd, pair); - /* Generic hook handler */ - if (messageHandler_handleHooks(udp->forwarder, CONNECTION_ID_UNDEFINED, - udp->localAddress, packet)) - goto END; -END: + *processed = messageHandler_handleHooks(udp->forwarder, packet, connid); + } return message; } -static void _readCommand(UdpListener *udp, int fd, - AddressPair *pair, - uint8_t * command) { +static void _readCommand(ListenerOps * listener, int fd, + AddressPair *pair, uint8_t * command) { + UdpListener * udp = (UdpListener *)listener->context; if (*command != REQUEST_LIGHT){ printf("the message received is not a command, drop\n"); @@ -502,10 +497,13 @@ static void _readCommand(UdpListener *udp, int fd, return; } - unsigned connid = 0; - bool foundConnection = _lookupConnectionId(udp, pair, &connid); - if(!foundConnection){ - connid = _createNewConnection(udp, fd, pair); + unsigned connid; + + const Connection *conn = _lookupConnection(listener, pair); + if (conn) { + connid = connection_GetConnectionId(conn); + } else { + connid = _createNewConnection(listener, fd, pair); } struct iovec *request; @@ -525,11 +523,12 @@ static void _readCommand(UdpListener *udp, int fd, } -static bool _receivePacket(UdpListener *udp, int fd, +static bool _receivePacket(ListenerOps * listener, int fd, AddressPair *pair, uint8_t * packet) { + UdpListener * udp = (UdpListener *)listener->context; bool processed = false; - Message *message = _readMessage(udp, fd, pair, + Message *message = _readMessage(listener, fd, pair, packet, &processed); if (message) { forwarder_Receive(udp->forwarder, message); @@ -537,8 +536,9 @@ static bool _receivePacket(UdpListener *udp, int fd, return processed; } -static void _readcb(int fd, PARCEventType what, void *udpVoid) { - UdpListener *udp = (UdpListener *)udpVoid; +static void _readcb(int fd, PARCEventType what, void * listener_void) { + ListenerOps * listener = (ListenerOps *)listener_void; + UdpListener * udp = (UdpListener *)listener->context; if (logger_IsLoggable(udp->logger, LoggerFacility_IO, PARCLogLevel_Debug)) { logger_Log(udp->logger, LoggerFacility_IO, PARCLogLevel_Debug, __func__, @@ -546,7 +546,7 @@ static void _readcb(int fd, PARCEventType what, void *udpVoid) { (what & PARCEventType_Timeout) ? " timeout" : "", (what & PARCEventType_Read) ? " read" : "", (what & PARCEventType_Write) ? " write" : "", - (what & PARCEventType_Signal) ? " signal" : "", udpVoid); + (what & PARCEventType_Signal) ? " signal" : "", udp); } if (what & PARCEventType_Read) { @@ -570,9 +570,9 @@ static void _readcb(int fd, PARCEventType what, void *udpVoid) { AddressPair *pair = _constructAddressPair( udp, (struct sockaddr *)&peerIpAddress, peerIpAddressLength); - bool done = _receivePacket(udp, fd, pair, packet); + bool done = _receivePacket(listener, fd, pair, packet); if(!done){ - _readCommand(udp, fd, pair, packet); + _readCommand(listener, fd, pair, packet); } addressPair_Release(&pair); |