From f5a0b8a5e24cede05e15ab696f0e15257a503525 Mon Sep 17 00:00:00 2001 From: Angelo Mantellini Date: Thu, 31 Jan 2019 18:20:48 +0100 Subject: [HICN24] Windows compatibility for hicn-light Change-Id: I8e19e52c9b4ec0fcbd7344c28765f5da1937569c Signed-off-by: Angelo Mantellini --- hicn-light/src/io/hicnListener.c | 26 +++++++++++++----------- hicn-light/src/io/tcpListener.h | 3 +++ hicn-light/src/io/udpListener.c | 44 +++++++++++++++++++++++++++++++++++----- hicn-light/src/io/udpListener.h | 3 +++ 4 files changed, 59 insertions(+), 17 deletions(-) (limited to 'hicn-light/src/io') diff --git a/hicn-light/src/io/hicnListener.c b/hicn-light/src/io/hicnListener.c index 789face90..bcf4de6a3 100644 --- a/hicn-light/src/io/hicnListener.c +++ b/hicn-light/src/io/hicnListener.c @@ -55,22 +55,24 @@ struct hicn_listener { PARCEvent *hicn_event; int hicn_fd; // this is the file descriptor got from hicn library - Address *localAddress; // this is the local address or 0::0 in case of the - // main listener this is the address used inside - // forwarder to identify the listener. Notice that this - // address is the same as the fisical interfaces on - // which we create the TUN. it is NOT the TUN address - // which is given by libhicn after the bind operation - // However the user alway uses this address since is - // the only one available at configuration time + Address + *localAddress; // this is the local address or 0::0 in case of the + // main listener this is the address used inside + // forwarder to identify the listener. Notice that this + // address is the same as the fisical interfaces on + // which we create the TUN. it is NOT the TUN address + // which is given by libhicn after the bind operation + // However the user alway uses this address since is + // the only one available at configuration time unsigned inetFamily; int connection_id; // this is used only if the listener is used to receive // data packets we assume that 1 connection is associated - // to one listener in this case so we set the connection_id - // we the connection is create. if this id is not set and a - // data packet is received, the packet is dropped + // to one listener in this case so we set the + // connection_id we the connection is create. if this id + // is not set and a data packet is received, the packet is + // dropped unsigned conn_id; }; @@ -216,7 +218,7 @@ ListenerOps *hicnListener_CreateInet6(Forwarder *forwarder, char *symbolic, // the call to libhicn is the same both for the main and the normal listeners // in both cases we need to set only the identifier. In the case of normal // listener (listener for data packet) we let the library select the right ip - //address we just need to set the right type of packet + // address we just need to set the right type of packet hicn_socket_helper_t *hicnSocketHelper = forwarder_GetHicnSocketHelper(forwarder); diff --git a/hicn-light/src/io/tcpListener.h b/hicn-light/src/io/tcpListener.h index c5d1e33af..9664b32b0 100644 --- a/hicn-light/src/io/tcpListener.h +++ b/hicn-light/src/io/tcpListener.h @@ -25,7 +25,10 @@ #ifndef tcpListener_h #define tcpListener_h +#ifndef _WIN32 #include +#endif + #include #include #include diff --git a/hicn-light/src/io/udpListener.c b/hicn-light/src/io/udpListener.c index 31c0e673b..1a16d6161 100644 --- a/hicn-light/src/io/udpListener.c +++ b/hicn-light/src/io/udpListener.c @@ -13,14 +13,16 @@ * limitations under the License. */ +#ifndef _WIN32 #include +#include +#endif #include #include #include #include #include #include -#include #include @@ -83,13 +85,24 @@ ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, parcAssertFalse(udp->udp_socket < 0, "Error opening UDP socket: (%d) %s", errno, strerror(errno)); + int failure = 0; +#ifndef _WIN32 // Set non-blocking flag int flags = fcntl(udp->udp_socket, F_GETFL, NULL); parcAssertTrue(flags != -1, "fcntl failed to obtain file descriptor flags (%d)", errno); - int failure = fcntl(udp->udp_socket, F_SETFL, flags | O_NONBLOCK); + failure = fcntl(udp->udp_socket, F_SETFL, flags | O_NONBLOCK); parcAssertFalse(failure, "fcntl failed to set file descriptor flags (%d)", errno); +#else + // Set non-blocking flag + u_long mode = 1; + int result = ioctlsocket(udp->udp_socket, FIONBIO, &mode); + if (result != NO_ERROR) { + parcAssertTrue(result != NO_ERROR, + "ioctlsocket failed to set file descriptor"); + } +#endif int one = 1; // don't hang onto address after listener has closed @@ -126,8 +139,11 @@ ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, myerrno, strerror(myerrno)); parcMemory_Deallocate((void **)&str); } - +#ifndef _WIN32 close(udp->udp_socket); +#else + closesocket(udp->udp_socket); +#endif addressDestroy(&udp->localAddress); logger_Release(&udp->logger); parcMemory_Deallocate((void **)&udp); @@ -152,13 +168,23 @@ ListenerOps *udpListener_CreateInet(Forwarder *forwarder, parcAssertFalse(udp->udp_socket < 0, "Error opening UDP socket: (%d) %s", errno, strerror(errno)); + int failure = 0; +#ifndef _WIN32 // Set non-blocking flag int flags = fcntl(udp->udp_socket, F_GETFL, NULL); parcAssertTrue(flags != -1, "fcntl failed to obtain file descriptor flags (%d)", errno); - int failure = fcntl(udp->udp_socket, F_SETFL, flags | O_NONBLOCK); + failure = fcntl(udp->udp_socket, F_SETFL, flags | O_NONBLOCK); parcAssertFalse(failure, "fcntl failed to set file descriptor flags (%d)", errno); +#else + u_long mode = 1; + int result = ioctlsocket(udp->udp_socket, FIONBIO, &mode); + if (result != NO_ERROR) { + parcAssertTrue(result != NO_ERROR, + "ioctlsocket failed to set file descriptor"); + } +#endif int one = 1; // don't hang onto address after listener has closed @@ -217,7 +243,12 @@ static void udpListener_Destroy(UdpListener **listenerPtr) { "UdpListener %p destroyed", (void *)udp); } +#ifndef _WIN32 close(udp->udp_socket); +#else + closesocket(udp->udp_socket); +#endif + addressDestroy(&udp->localAddress); dispatcher_DestroyNetworkEvent(forwarder_GetDispatcher(udp->forwarder), &udp->udp_event); @@ -276,7 +307,8 @@ static size_t _peekMessageLength(UdpListener *udp, int fd, size_t packetLength = 0; - uint8_t fixedHeader[messageHandler_GetIPHeaderLength(IPv6)]; + uint8_t *fixedHeader = (uint8_t *)malloc( + sizeof(uint8_t) * messageHandler_GetIPHeaderLength(IPv6)); // peek at the UDP packet and read in the fixed header. // Also returns the socket information for the remote peer @@ -294,6 +326,8 @@ static size_t _peekMessageLength(UdpListener *udp, int fd, } } + free(fixedHeader); + return packetLength; } diff --git a/hicn-light/src/io/udpListener.h b/hicn-light/src/io/udpListener.h index 1cf3bd887..14c03fd52 100644 --- a/hicn-light/src/io/udpListener.h +++ b/hicn-light/src/io/udpListener.h @@ -16,7 +16,10 @@ #ifndef udpListener_h #define udpListener_h +#ifndef _WIN32 #include +#endif + #include #include #include -- cgit 1.2.3-korg