Merge branch 'windows-port'
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / caipserver.c
index cacf7b0..7e9d2cc 100644 (file)
 #endif
 
 #include <sys/types.h>
-#if !defined(__msys_nt__)
+#if !defined(_WIN32)
 #include <sys/socket.h>
 #endif
 
-#if defined(__msys_nt__)
+#if defined(_WIN32)
+#include <assert.h>
 #include <winsock2.h>
 #include <ws2def.h>
 #include <mswsock.h>
 #endif
 
 #include <stdio.h>
+#if !defined(_MSC_VER)
 #include <unistd.h>
+#endif //!defined(_MSC_VER)
 #include <sys/types.h>
 #include <fcntl.h>
-#if !defined(__msys_nt__)
+#if !defined(_WIN32)
 #include <sys/select.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
@@ -62,6 +65,7 @@
 #include "camutex.h"
 #include "oic_malloc.h"
 #include "oic_string.h"
+#include "platform_features.h"
 
 #define USE_IP_MREQN
 #if defined(_WIN32)
@@ -129,63 +133,51 @@ static char *ipv6mcnames[IPv6_DOMAINS] = {
 #define CAIPS_GET_ERROR \
     strerror(errno)
 #endif
-static CAIPExceptionCallback g_exceptionCallback;
+static CAIPErrorHandleCallback g_ipErrorHandler = NULL;
 
-static CAIPPacketReceivedCallback g_packetReceivedCallback;
+static CAIPPacketReceivedCallback g_packetReceivedCallback = NULL;
 
-static void CAHandleNetlink();
 static void CAFindReadyMessage();
+#if !defined(WSA_WAIT_EVENT_0)
 static void CASelectReturned(fd_set *readFds, int ret);
+#else
+static void CAEventReturned(CASocketFd_t socket);
+#endif
 static void CAProcessNewInterface(CAInterface_t *ifchanged);
-static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags);
+static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags);
 
-#define SET(TYPE, FDS) \
-    if (caglobals.ip.TYPE.fd != -1) \
-    { \
-        FD_SET(caglobals.ip.TYPE.fd, FDS); \
-    }
+static void CAReceiveHandler(void *data)
+{
+    (void)data;
 
-#define ISSET(TYPE, FDS, FLAGS) \
-    if (caglobals.ip.TYPE.fd != -1 && FD_ISSET(caglobals.ip.TYPE.fd, FDS)) \
-    { \
-        fd = caglobals.ip.TYPE.fd; \
-        flags = FLAGS; \
+    while (!caglobals.ip.terminate)
+    {
+        CAFindReadyMessage();
     }
+}
+
+#if !defined(WSA_WAIT_EVENT_0)
 
 #define CLOSE_SOCKET(TYPE) \
-    if (caglobals.ip.TYPE.fd != -1) \
+    if (caglobals.ip.TYPE.fd != OC_INVALID_SOCKET) \
     { \
         close(caglobals.ip.TYPE.fd); \
-        caglobals.ip.TYPE.fd = -1; \
+        caglobals.ip.TYPE.fd = OC_INVALID_SOCKET; \
     }
 
-void CADeInitializeIPGlobals()
-{
-    CLOSE_SOCKET(u6);
-    CLOSE_SOCKET(u6s);
-    CLOSE_SOCKET(u4);
-    CLOSE_SOCKET(u4s);
-    CLOSE_SOCKET(m6);
-    CLOSE_SOCKET(m6s);
-    CLOSE_SOCKET(m4);
-    CLOSE_SOCKET(m4s);
-
-    if (caglobals.ip.netlinkFd != -1)
-    {
-        close(caglobals.ip.netlinkFd);
-        caglobals.ip.netlinkFd = -1;
+#define SET(TYPE, FDS) \
+    if (caglobals.ip.TYPE.fd != OC_INVALID_SOCKET) \
+    { \
+        FD_SET(caglobals.ip.TYPE.fd, FDS); \
     }
-}
 
-static void CAReceiveHandler(void *data)
-{
-    (void)data;
-
-    while (!caglobals.ip.terminate)
-    {
-        CAFindReadyMessage();
+#define ISSET(TYPE, FDS, FLAGS) \
+    if (caglobals.ip.TYPE.fd != OC_INVALID_SOCKET && FD_ISSET(caglobals.ip.TYPE.fd, FDS)) \
+    { \
+        fd = caglobals.ip.TYPE.fd; \
+        flags = FLAGS; \
     }
-}
+
 
 static void CAFindReadyMessage()
 {
@@ -206,16 +198,15 @@ static void CAFindReadyMessage()
     SET(m4,  &readFds)
     SET(m4s, &readFds)
 
-#if !defined(_WIN32)
     if (caglobals.ip.shutdownFds[0] != -1)
     {
         FD_SET(caglobals.ip.shutdownFds[0], &readFds);
     }
-    if (caglobals.ip.netlinkFd != -1)
+    if (caglobals.ip.netlinkFd != OC_INVALID_SOCKET)
     {
         FD_SET(caglobals.ip.netlinkFd, &readFds);
     }
-#endif
+
     int ret = select(caglobals.ip.maxfd + 1, &readFds, NULL, NULL, tv);
 
     if (caglobals.ip.terminate)
@@ -239,7 +230,7 @@ static void CAFindReadyMessage()
 static void CASelectReturned(fd_set *readFds, int ret)
 {
     (void)ret;
-    int fd = -1;
+    CASocketFd_t fd = OC_INVALID_SOCKET;
     CATransportFlags_t flags = CA_DEFAULT_FLAGS;
 
     while (!caglobals.ip.terminate)
@@ -252,8 +243,7 @@ static void CASelectReturned(fd_set *readFds, int ret)
         else ISSET(m6s, readFds, CA_MULTICAST | CA_IPV6 | CA_SECURE)
         else ISSET(m4,  readFds, CA_MULTICAST | CA_IPV4)
         else ISSET(m4s, readFds, CA_MULTICAST | CA_IPV4 | CA_SECURE)
-#if !defined(_WIN32)
-        else if (FD_ISSET(caglobals.ip.netlinkFd, readFds))
+        else if ((caglobals.ip.netlinkFd != OC_INVALID_SOCKET) && FD_ISSET(caglobals.ip.netlinkFd, readFds))
         {
             CAInterface_t *ifchanged = CAFindInterfaceChange();
             if (ifchanged)
@@ -277,20 +267,223 @@ static void CASelectReturned(fd_set *readFds, int ret)
         {
             break;
         }
-#else
+        (void)CAReceiveMessage(fd, flags);
+        FD_CLR(fd, readFds);
+    }
+}
+
+#else // if defined(WSA_WAIT_EVENT_0)
+
+#define CLOSE_SOCKET(TYPE) \
+    if (caglobals.ip.TYPE.fd != OC_INVALID_SOCKET) \
+    { \
+        closesocket(caglobals.ip.TYPE.fd); \
+        caglobals.ip.TYPE.fd = OC_INVALID_SOCKET; \
+    }
+
+#define PUSH_HANDLE(HANDLE, ARRAY, INDEX) \
+{ \
+    ARRAY[INDEX] = HANDLE; \
+    INDEX++; \
+}
+
+// Turn handle into WSAEvent and push to ARRAY
+#define PUSH_SOCKET(SOCKET, ARRAY, INDEX) \
+    if (SOCKET != OC_INVALID_SOCKET) \
+    { \
+        WSAEVENT NewEvent; \
+        NewEvent = WSACreateEvent(); \
+        if (WSA_INVALID_EVENT != NewEvent) \
+        { \
+            if (0 != WSAEventSelect(SOCKET, NewEvent, FD_READ)) \
+            { \
+                OIC_LOG_V(ERROR, TAG, "WSAEventSelect failed 0x%08x ", WSAGetLastError()); \
+                if (!WSACloseEvent(NewEvent)) \
+                { \
+                    OIC_LOG_V(ERROR, TAG, "WSACloseEvent(NewEvent) failed 0x%08x", WSAGetLastError()); \
+                } \
+            } \
+            else \
+            { \
+                PUSH_HANDLE(NewEvent, ARRAY, INDEX); \
+            } \
+        } \
+        else \
+        { \
+            OIC_LOG_V(ERROR, TAG, "WSACreateEvent(NewEvent) failed 0x%08x", WSAGetLastError()); \
+        }\
+    }
+
+#define INSERT_SOCKET(FD, ARRAY, INDEX) \
+    { \
+        if (OC_INVALID_SOCKET != FD) \
+        { \
+            ARRAY[INDEX] = FD; \
+        } \
+    }
+
+
+// Inserts the socket into the SOCKET_ARRAY and pushes the socket event into EVENT_ARRAY
+#define PUSH_IP_SOCKET(TYPE, EVENT_ARRAY, SOCKET_ARRAY, INDEX) \
+    { \
+        if (OC_INVALID_SOCKET != caglobals.ip.TYPE.fd) \
+        { \
+            INSERT_SOCKET(caglobals.ip.TYPE.fd, SOCKET_ARRAY, INDEX); \
+            PUSH_SOCKET(caglobals.ip.TYPE.fd, EVENT_ARRAY, INDEX); \
+        } \
+    }
+
+#define IS_MATCHING_IP_SOCKET(TYPE, SOCKET, FLAGS) \
+    if ((caglobals.ip.TYPE.fd != OC_INVALID_SOCKET) && (caglobals.ip.TYPE.fd == SOCKET)) \
+    { \
+        fd = caglobals.ip.TYPE.fd; \
+        flags = FLAGS; \
+    }
+
+#define EVENT_ARRAY_SIZE  10
+
+static void CAFindReadyMessage()
+{
+    CASocketFd_t socketArray[EVENT_ARRAY_SIZE];
+    HANDLE eventArray[EVENT_ARRAY_SIZE];
+    int arraySize = 0;
+    int eventIndex;
+
+    // socketArray and eventArray should have same number of elements
+    OC_STATIC_ASSERT(_countof(socketArray) == _countof(eventArray), "Arrays should have same number of elements");
+
+    PUSH_IP_SOCKET(u6,  eventArray, socketArray, arraySize);
+    PUSH_IP_SOCKET(u6s, eventArray, socketArray, arraySize);
+    PUSH_IP_SOCKET(u4,  eventArray, socketArray, arraySize);
+    PUSH_IP_SOCKET(u4s, eventArray, socketArray, arraySize);
+    PUSH_IP_SOCKET(m6,  eventArray, socketArray, arraySize);
+    PUSH_IP_SOCKET(m6s, eventArray, socketArray, arraySize);
+    PUSH_IP_SOCKET(m4,  eventArray, socketArray, arraySize);
+    PUSH_IP_SOCKET(m4s, eventArray, socketArray, arraySize);
+
+    if (-1 != caglobals.ip.shutdownEvent)
+    {
+        INSERT_SOCKET(OC_INVALID_SOCKET, socketArray, arraySize);
+        PUSH_HANDLE(caglobals.ip.shutdownEvent, eventArray, arraySize);
+    }
+
+    /** @todo Support netlink events */
+
+    // Should not have overflowed buffer
+    assert(arraySize <= (_countof(socketArray)));
+
+    // Timeout is unnecessary on Windows
+    assert(-1 == caglobals.ip.selectTimeout);
+
+    while (!caglobals.ip.terminate)
+    {
+        int ret = WSAWaitForMultipleEvents(arraySize, eventArray, FALSE, WSA_INFINITE, FALSE);
+
+        switch (ret)
+        {
+            case WSA_WAIT_FAILED:
+                OIC_LOG_V(ERROR, TAG, "WSAWaitForMultipleEvents returned WSA_WAIT_FAILED 0x%08x", WSAGetLastError());
+                break;
+            case WSA_WAIT_IO_COMPLETION:
+                OIC_LOG_V(ERROR, TAG, "WSAWaitForMultipleEvents returned WSA_WAIT_IO_COMPLETION 0x%08x", WSAGetLastError());
+                break;
+            case WSA_WAIT_TIMEOUT:
+                OIC_LOG_V(ERROR, TAG, "WSAWaitForMultipleEvents returned WSA_WAIT_TIMEOUT 0x%08x", WSAGetLastError());
+                break;
+            default:
+                eventIndex = ret - WSA_WAIT_EVENT_0;
+                if ((eventIndex >= 0) && (eventIndex < arraySize))
+                {
+                    if (false == WSAResetEvent(eventArray[eventIndex]))
+                    {
+                        OIC_LOG_V(ERROR, TAG, "WSAResetEvent failed 0x%08x", WSAGetLastError());
+                    }
+
+                    // Break out if shutdownEvent is triggered
+                    if ((caglobals.ip.shutdownEvent != -1) &&
+                        (caglobals.ip.shutdownEvent == eventArray[eventIndex]))
+                    {
+                        break;
+                    }
+                    CAEventReturned(socketArray[eventIndex]);
+                }
+                else
+                {
+                    OIC_LOG_V(ERROR, TAG, "WSAWaitForMultipleEvents failed 0x%08x", WSAGetLastError());
+                }
+                break;
+        }
+
+    }
+
+    while (arraySize > 0)
+    {
+        arraySize--;
+        if (!WSACloseEvent(eventArray[arraySize]))
+        {
+            OIC_LOG_V(ERROR, TAG, "WSACloseEvent (Index %i) failed 0x%08x", arraySize, WSAGetLastError());
+        }
+    }
+
+    if (caglobals.ip.terminate)
+    {
+        caglobals.ip.shutdownEvent = -1;
+        WSACleanup();
+    }
+}
+
+static void CAEventReturned(CASocketFd_t socket)
+{
+    CASocketFd_t fd = OC_INVALID_SOCKET;
+    CATransportFlags_t flags = CA_DEFAULT_FLAGS;
+
+    while (!caglobals.ip.terminate)
+    {
+        IS_MATCHING_IP_SOCKET(u6,  socket, CA_IPV6)
+        else IS_MATCHING_IP_SOCKET(u6s, socket, CA_IPV6 | CA_SECURE)
+        else IS_MATCHING_IP_SOCKET(u4,  socket, CA_IPV4)
+        else IS_MATCHING_IP_SOCKET(u4s, socket, CA_IPV4 | CA_SECURE)
+        else IS_MATCHING_IP_SOCKET(m6,  socket, CA_MULTICAST | CA_IPV6)
+        else IS_MATCHING_IP_SOCKET(m6s, socket, CA_MULTICAST | CA_IPV6 | CA_SECURE)
+        else IS_MATCHING_IP_SOCKET(m4,  socket, CA_MULTICAST | CA_IPV4)
+        else IS_MATCHING_IP_SOCKET(m4s, socket, CA_MULTICAST | CA_IPV4 | CA_SECURE)
         else
         {
             break;
         }
+        (void)CAReceiveMessage(socket, flags);
+        // We will never get more than one match per socket, so always break.
+        break;
+    }
+}
+
 #endif
-        (void)CAReceiveMessage(fd, flags);
-        FD_CLR(fd, readFds);
+
+void CADeInitializeIPGlobals()
+{
+    CLOSE_SOCKET(u6);
+    CLOSE_SOCKET(u6s);
+    CLOSE_SOCKET(u4);
+    CLOSE_SOCKET(u4s);
+    CLOSE_SOCKET(m6);
+    CLOSE_SOCKET(m6s);
+    CLOSE_SOCKET(m4);
+    CLOSE_SOCKET(m4s);
+
+    if (caglobals.ip.netlinkFd != OC_INVALID_SOCKET)
+    {
+#ifdef _WIN32
+        closesocket(caglobals.ip.netlinkFd);
+#else
+        close(caglobals.ip.netlinkFd);
+#endif
+        caglobals.ip.netlinkFd = OC_INVALID_SOCKET;
     }
 }
 
-static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags)
+static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags)
 {
-    char recvBuffer[COAP_MAX_PDU_SIZE];
+    char recvBuffer[COAP_MAX_PDU_SIZE] = {0};
 
     size_t len;
     int level, type, namelen;
@@ -328,7 +521,7 @@ static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags)
                           .msg_controllen = CMSG_SPACE(len) };
 
     ssize_t recvLen = recvmsg(fd, &msg, flags);
-    if (-1 == recvLen)
+    if (OC_SOCKET_ERROR == recvLen)
     {
         OIC_LOG_V(ERROR, TAG, "Recvfrom failed %s", strerror(errno));
         return CA_STATUS_FAILED;
@@ -376,7 +569,7 @@ static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags)
     uint32_t recvLen = 0;
     uint32_t ret = caglobals.ip.wsaRecvMsg(fd, &msg, &recvLen, 0,0);
     OIC_LOG_V(DEBUG, TAG, "WSARecvMsg recvd %u bytes", recvLen);
-    if (SOCKET_ERROR == ret)
+    if (OC_SOCKET_ERROR == ret)
     {
         OIC_LOG_V(ERROR, TAG, "WSARecvMsg failed %i", WSAGetLastError());
     }
@@ -451,17 +644,17 @@ void CAIPPullData()
     OIC_LOG(DEBUG, TAG, "OUT");
 }
 
-static int CACreateSocket(int family, uint16_t *port)
+static CASocketFd_t CACreateSocket(int family, uint16_t *port, bool isMulticast)
 {
     int socktype = SOCK_DGRAM;
 #ifdef SOCK_CLOEXEC
     socktype |= SOCK_CLOEXEC;
 #endif
-    int fd = socket(family, socktype, IPPROTO_UDP);
-    if (-1 == fd)
+    CASocketFd_t fd = socket(family, socktype, IPPROTO_UDP);
+    if (OC_INVALID_SOCKET == fd)
     {
         OIC_LOG_V(ERROR, TAG, "create socket failed: %s", CAIPS_GET_ERROR);
-        return -1;
+        return OC_INVALID_SOCKET;
     }
 
 #if !defined(SOCK_CLOEXEC) && defined(FD_CLOEXEC)
@@ -470,7 +663,7 @@ static int CACreateSocket(int family, uint16_t *port)
     {
         OIC_LOG_V(ERROR, TAG, "set FD_CLOEXEC failed: %s", strerror(errno));
         close(fd);
-        return -1;
+        return OC_INVALID_SOCKET;
     }
 #endif
     struct sockaddr_storage sa = { .ss_family = family };
@@ -480,17 +673,17 @@ static int CACreateSocket(int family, uint16_t *port)
     {
         int on = 1;
 
-        if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
+        if (OC_SOCKET_ERROR == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&on), sizeof (on)))
         {
             OIC_LOG_V(ERROR, TAG, "IPV6_V6ONLY failed: %s", CAIPS_GET_ERROR);
         }
 
-        if (*port) // only do this for multicast ports
+        if (isMulticast && *port) // only do this for multicast ports
         {
 #if defined(IPV6_RECVPKTINFO)
-            if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof (on)))
+            if (OC_SOCKET_ERROR == setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof (on)))
 #else
-            if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &on, sizeof (on)))
+            if (OC_SOCKET_ERROR == setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, OPTVAL_T(&on), sizeof (on)))
 #endif
             {
                 OIC_LOG_V(ERROR, TAG, "IPV6_RECVPKTINFO failed: %s",CAIPS_GET_ERROR);
@@ -502,10 +695,10 @@ static int CACreateSocket(int family, uint16_t *port)
     }
     else
     {
-        if (*port) // only do this for multicast ports
+        if (isMulticast && *port) // only do this for multicast ports
         {
             int on = 1;
-            if (-1 == setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &on, sizeof (on)))
+            if (OC_SOCKET_ERROR == setsockopt(fd, IPPROTO_IP, IP_PKTINFO, OPTVAL_T(&on), sizeof (on)))
             {
                 OIC_LOG_V(ERROR, TAG, "IP_PKTINFO failed: %s", CAIPS_GET_ERROR);
             }
@@ -515,31 +708,43 @@ static int CACreateSocket(int family, uint16_t *port)
         socklen = sizeof (struct sockaddr_in);
     }
 
-    if (*port) // use the given port
+    if (isMulticast && *port) // use the given port
     {
         int on = 1;
-        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)))
+        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on), sizeof (on)))
         {
             OIC_LOG_V(ERROR, TAG, "SO_REUSEADDR failed: %s", CAIPS_GET_ERROR);
+#ifdef _WIN32
+            closesocket(fd);
+#else
             close(fd);
-            return -1;
+#endif
+            return OC_INVALID_SOCKET;
         }
     }
 
-    if (-1 == bind(fd, (struct sockaddr *)&sa, socklen))
+    if (OC_SOCKET_ERROR == bind(fd, (struct sockaddr *)&sa, socklen))
     {
         OIC_LOG_V(ERROR, TAG, "bind socket failed: %s", CAIPS_GET_ERROR);
+#ifdef _WIN32
+        closesocket(fd);
+#else
         close(fd);
-        return -1;
+#endif
+        return OC_INVALID_SOCKET;
     }
 
     if (!*port) // return the assigned port
     {
-        if (-1 == getsockname(fd, (struct sockaddr *)&sa, &socklen))
+        if (OC_SOCKET_ERROR == getsockname(fd, (struct sockaddr *)&sa, &socklen))
         {
             OIC_LOG_V(ERROR, TAG, "getsockname failed: %s", CAIPS_GET_ERROR);
+#ifdef _WIN32
+            closesocket(fd);
+#else
             close(fd);
-            return -1;
+#endif
+            return OC_INVALID_SOCKET;
         }
         *port = ntohs(family == AF_INET6 ?
                       ((struct sockaddr_in6 *)&sa)->sin6_port :
@@ -552,18 +757,24 @@ static int CACreateSocket(int family, uint16_t *port)
 #define CHECKFD(FD) \
     if (FD > caglobals.ip.maxfd) \
         caglobals.ip.maxfd = FD;
-#define NEWSOCKET(FAMILY, NAME) \
-    caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port); \
+#define NEWSOCKET(FAMILY, NAME, MULTICAST) \
+    caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port, MULTICAST); \
+    if (caglobals.ip.NAME.fd == OC_INVALID_SOCKET) \
+    {   \
+        caglobals.ip.NAME.port = 0; \
+        caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port, MULTICAST); \
+    }   \
     CHECKFD(caglobals.ip.NAME.fd)
 
 static void CAInitializeNetlink()
 {
+    caglobals.ip.netlinkFd = OC_INVALID_SOCKET;
 #ifdef __linux__
     // create NETLINK fd for interface change notifications
     struct sockaddr_nl sa = { AF_NETLINK, 0, 0, RTMGRP_LINK };
 
     caglobals.ip.netlinkFd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE);
-    if (caglobals.ip.netlinkFd == -1)
+    if (caglobals.ip.netlinkFd == OC_INVALID_SOCKET)
     {
         OIC_LOG_V(ERROR, TAG, "netlink socket failed: %s", strerror(errno));
     }
@@ -574,7 +785,7 @@ static void CAInitializeNetlink()
         {
             OIC_LOG_V(ERROR, TAG, "netlink bind failed: %s", strerror(errno));
             close(caglobals.ip.netlinkFd);
-            caglobals.ip.netlinkFd = -1;
+            caglobals.ip.netlinkFd = OC_INVALID_SOCKET;
         }
         else
         {
@@ -584,14 +795,28 @@ static void CAInitializeNetlink()
 #endif
 }
 
-static void CAInitializePipe()
+static void CAInitializeFastShutdownMechanism()
 {
-#if !defined(_WIN32)
-    caglobals.ip.selectTimeout = -1;
-#ifdef HAVE_PIPE2
-    int ret = pipe2(caglobals.ip.shutdownFds, O_CLOEXEC);
+    caglobals.ip.selectTimeout = -1; // don't poll for shutdown
+    int ret = -1;
+#if defined(WSA_WAIT_EVENT_0)
+    caglobals.ip.shutdownEvent = -1;
+    caglobals.ip.shutdownEvent = WSACreateEvent();
+
+    if (caglobals.ip.shutdownEvent == WSA_INVALID_EVENT)
+    {
+        caglobals.ip.shutdownEvent = -1;
+    }
+    else
+    {
+        ret = 0;
+    }
+#elif defined(HAVE_PIPE2)
+    ret = pipe2(caglobals.ip.shutdownFds, O_CLOEXEC);
+    CHECKFD(caglobals.ip.shutdownFds[0]);
+    CHECKFD(caglobals.ip.shutdownFds[1]);
 #else
-    int ret = pipe(caglobals.ip.shutdownFds);
+    ret = pipe(caglobals.ip.shutdownFds);
     if (-1 != ret)
     {
         ret = fcntl(caglobals.ip.shutdownFds[0], F_GETFD);
@@ -615,15 +840,14 @@ static void CAInitializePipe()
             caglobals.ip.shutdownFds[1] = -1;
         }
     }
+    CHECKFD(caglobals.ip.shutdownFds[0]);
+    CHECKFD(caglobals.ip.shutdownFds[1]);
 #endif
     if (-1 == ret)
     {
-        OIC_LOG_V(ERROR, TAG, "pipe failed: %s", strerror(errno));
+        OIC_LOG_V(ERROR, TAG, "fast shutdown mechanism init failed: %s", CAIPS_GET_ERROR);
         caglobals.ip.selectTimeout = SELECT_TIMEOUT; //poll needed for shutdown
     }
-#else
-    /** @todo Refactor to support Windows-specific inter-thread communication code. */
-#endif
 }
 
 CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
@@ -664,18 +888,18 @@ CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
 
     if (caglobals.ip.ipv6enabled)
     {
-        NEWSOCKET(AF_INET6, u6)
-        NEWSOCKET(AF_INET6, u6s)
-        NEWSOCKET(AF_INET6, m6)
-        NEWSOCKET(AF_INET6, m6s)
+        NEWSOCKET(AF_INET6, u6, false)
+        NEWSOCKET(AF_INET6, u6s, false)
+        NEWSOCKET(AF_INET6, m6, true)
+        NEWSOCKET(AF_INET6, m6s, true)
         OIC_LOG_V(INFO, TAG, "IPv6 unicast port: %u", caglobals.ip.u6.port);
     }
     if (caglobals.ip.ipv4enabled)
     {
-        NEWSOCKET(AF_INET, u4)
-        NEWSOCKET(AF_INET, u4s)
-        NEWSOCKET(AF_INET, m4)
-        NEWSOCKET(AF_INET, m4s)
+        NEWSOCKET(AF_INET, u4, false)
+        NEWSOCKET(AF_INET, u4s, false)
+        NEWSOCKET(AF_INET, m4, true)
+        NEWSOCKET(AF_INET, m4s, true)
         OIC_LOG_V(INFO, TAG, "IPv4 unicast port: %u", caglobals.ip.u4.port);
     }
 
@@ -690,7 +914,7 @@ CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
               caglobals.ip.u6.port, caglobals.ip.u6s.port, caglobals.ip.u4.port,
               caglobals.ip.u4s.port, caglobals.ip.m6.port, caglobals.ip.m6s.port,
               caglobals.ip.m4.port, caglobals.ip.m4s.port);
-#if defined (_WIN32)
+#if defined (SIO_GET_EXTENSION_FUNCTION_POINTER)
     caglobals.ip.wsaRecvMsg = NULL;
     GUID GuidWSARecvMsg = WSAID_WSARECVMSG;
     DWORD copied = 0;
@@ -701,10 +925,8 @@ CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
         return CA_STATUS_FAILED;
     }
 #endif
-    // create pipe for fast shutdown
-    CAInitializePipe();
-    CHECKFD(caglobals.ip.shutdownFds[0]);
-    CHECKFD(caglobals.ip.shutdownFds[1]);
+    // set up appropriate FD mechanism for fast shutdown
+    CAInitializeFastShutdownMechanism();
 
     // create source of network interface change notifications
     CAInitializeNetlink();
@@ -735,8 +957,8 @@ void CAIPStopServer()
 {
     caglobals.ip.started = false;
     caglobals.ip.terminate = true;
-#if !defined(_WIN32)
 
+#if !defined(WSA_WAIT_EVENT_0)
     if (caglobals.ip.shutdownFds[1] != -1)
     {
         close(caglobals.ip.shutdownFds[1]);
@@ -747,13 +969,17 @@ void CAIPStopServer()
         // receive thread will stop in SELECT_TIMEOUT seconds.
     }
 #else
-    /** @todo Refactor to support Windows-specific inter-thread communication code. */
+    // receive thread will stop immediately.
+    if (!WSASetEvent(caglobals.ip.shutdownEvent))
+    {
+        OIC_LOG_V(DEBUG, TAG, "set shutdown event failed: %#08X", GetLastError());
+    }
 #endif
 }
 
 void CAWakeUpForChange()
 {
-#if !defined(_WIN32)
+#if !defined(WSA_WAIT_EVENT_0)
     if (caglobals.ip.shutdownFds[1] != -1)
     {
         ssize_t len = 0;
@@ -767,7 +993,10 @@ void CAWakeUpForChange()
         }
     }
 #else
-    /** @todo Refactor to support Windows-specific inter-thread communication code. */
+    if (!WSASetEvent(caglobals.ip.shutdownEvent))
+    {
+        OIC_LOG_V(DEBUG, TAG, "set shutdown event failed: %#08X", GetLastError());
+    }
 #endif
 }
 
@@ -787,8 +1016,8 @@ static void applyMulticastToInterface4(uint32_t ifindex)
                              .imr_interface.s_addr = htonl(ifindex) };
 #endif
 
-    int ret = setsockopt(caglobals.ip.m4.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq));
-    if (-1 == ret)
+    int ret = setsockopt(caglobals.ip.m4.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, OPTVAL_T(&mreq), sizeof (mreq));
+    if (OC_SOCKET_ERROR == ret)
     {
 #if !defined(WSAEINVAL)
         if (EADDRINUSE != errno)
@@ -799,8 +1028,8 @@ static void applyMulticastToInterface4(uint32_t ifindex)
             OIC_LOG_V(ERROR, TAG, "       IPv4 IP_ADD_MEMBERSHIP failed: %s", CAIPS_GET_ERROR);
         }
     }
-    ret = setsockopt(caglobals.ip.m4s.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq));
-    if (-1 == ret)
+    ret = setsockopt(caglobals.ip.m4s.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, OPTVAL_T(&mreq), sizeof (mreq));
+    if (OC_SOCKET_ERROR == ret)
     {
 #if !defined(WSAEINVAL)
         if (EADDRINUSE != errno)
@@ -817,8 +1046,8 @@ static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t ifindex)
 {
     struct ipv6_mreq mreq = {.ipv6mr_multiaddr = *addr,
                              .ipv6mr_interface = ifindex };
-    int ret = setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof (mreq));
-    if (-1 == ret)
+    int ret = setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, OPTVAL_T(&mreq), sizeof (mreq));
+    if (OC_SOCKET_ERROR == ret)
     {
 #if !defined(_WIN32)
                 if (EADDRINUSE != errno)
@@ -920,18 +1149,14 @@ CAResult_t CAIPStopListenServer()
         }
         if (ifitem->family == AF_INET)
         {
-            close(caglobals.ip.m4.fd);
-            close(caglobals.ip.m4s.fd);
-            caglobals.ip.m4.fd = -1;
-            caglobals.ip.m4s.fd = -1;
+            CLOSE_SOCKET(m4);
+            CLOSE_SOCKET(m4s);
             OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s cloed", ifitem->name);
         }
         if (ifitem->family == AF_INET6)
         {
-            close(caglobals.ip.m6.fd);
-            close(caglobals.ip.m6s.fd);
-            caglobals.ip.m6.fd = -1;
-            caglobals.ip.m6s.fd = -1;
+            CLOSE_SOCKET(m6);
+            CLOSE_SOCKET(m6s);
             OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
         }
     }
@@ -962,11 +1187,6 @@ void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
     g_packetReceivedCallback = callback;
 }
 
-void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
-{
-    g_exceptionCallback = callback;
-}
-
 void CAIPSetConnectionStateChangeCallback(CAIPConnectionStateChangeCallback callback)
 {
     CAIPSetNetworkMonitorCallback(callback);
@@ -981,13 +1201,16 @@ static void sendData(int fd, const CAEndpoint_t *endpoint,
     if (!endpoint)
     {
         OIC_LOG(DEBUG, TAG, "endpoint is null");
+        if (g_ipErrorHandler)
+        {
+            g_ipErrorHandler(endpoint, data, dlen, CA_STATUS_INVALID_PARAM);
+        }
         return;
     }
 
     char *secure = (endpoint->flags & CA_SECURE) ? "secure " : "";
 
-    (void)secure;   // eliminates release warning
-    (void)cast;
+    (void)cast;  // eliminates release warning
     (void)fam;
 
     struct sockaddr_storage sock;
@@ -1005,8 +1228,13 @@ static void sendData(int fd, const CAEndpoint_t *endpoint,
     }
 #if !defined(_WIN32)
     ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, socklen);
-    if (-1 == len)
+    if (OC_SOCKET_ERROR == len)
     {
+         // If logging is not defined/enabled.
+        if (g_ipErrorHandler)
+        {
+            g_ipErrorHandler(endpoint, data, dlen, CA_SEND_FAILED);
+        }
         OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %s", secure, cast, fam, strerror(errno));
     }
     else
@@ -1019,11 +1247,17 @@ static void sendData(int fd, const CAEndpoint_t *endpoint,
     int sent = 0;
     do {
         len = sendto(fd, ((char*)data) + sent, dlen - sent, 0, (struct sockaddr *)&sock, socklen);
-        if (SOCKET_ERROR == len)
+        if (OC_SOCKET_ERROR == len)
         {
             err = WSAGetLastError();
             if ((WSAEWOULDBLOCK != err) && (WSAENOBUFS != err))
             {
+                 // If logging is not defined/enabled.
+                if (g_ipErrorHandler)
+                {
+                    g_ipErrorHandler(endpoint, data, dlen, CA_SEND_FAILED);
+                }
+
                 OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %i", secure, cast, fam, err);
             }
         }
@@ -1035,16 +1269,16 @@ static void sendData(int fd, const CAEndpoint_t *endpoint,
                 OIC_LOG_V(DEBUG, TAG, "%s%s %s sendTo (Partial Send) is successful: "
                                       "currently sent: %ld bytes, "
                                       "total sent: %ld bytes, "
-                                      "remaining: %ld bytes", 
+                                      "remaining: %ld bytes",
                                       secure, cast, fam, len, sent, dlen-sent);
             }
             else
             {
-                OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %ld bytes", 
+                OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %ld bytes",
                                      secure, cast, fam, len);
             }
         }
-    } while ((SOCKET_ERROR == len) && ((WSAEWOULDBLOCK == err) || (WSAENOBUFS == err)) || (sent < dlen));
+    } while ((OC_SOCKET_ERROR == len) && ((WSAEWOULDBLOCK == err) || (WSAENOBUFS == err)) || (sent < dlen));
 #endif
 }
 
@@ -1086,7 +1320,7 @@ static void sendMulticastData6(const u_arraylist_t *iflist,
         }
 
         int index = ifitem->index;
-        if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index, sizeof (index)))
+        if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, OPTVAL_T(&index), sizeof (index)))
         {
             OIC_LOG_V(ERROR, TAG, "setsockopt6 failed: %s", CAIPS_GET_ERROR);
             return;
@@ -1134,7 +1368,7 @@ static void sendMulticastData4(const u_arraylist_t *iflist,
 #else
         mreq.imr_interface.s_addr = htonl(ifitem->index);
 #endif
-        if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof (mreq)))
+        if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, OPTVAL_T(&mreq), sizeof (mreq)))
         {
             OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)",
                     CAIPS_GET_ERROR);
@@ -1180,7 +1414,7 @@ void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
             endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
         }
 
-        int fd;
+        CASocketFd_t fd;
         if (caglobals.ip.ipv6enabled && (endpoint->flags & CA_IPV6))
         {
             fd = isSecure ? caglobals.ip.u6s.fd : caglobals.ip.u6.fd;
@@ -1281,3 +1515,8 @@ CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
 
     return CA_STATUS_OK;
 }
+
+void CAIPSetErrorHandler(CAIPErrorHandleCallback errorHandleCallback)
+{
+    g_ipErrorHandler = errorHandleCallback;
+}