Merge branch '1.1-rel'
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / caipserver.c
index f1b133c..c64fcb5 100644 (file)
  *
  ******************************************************************/
 
+#ifndef __APPLE_USE_RFC_3542
 #define __APPLE_USE_RFC_3542 // for PKTINFO
+#endif
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE // for in6_pktinfo
+#endif
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -115,6 +119,63 @@ static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags);
         flags = FLAGS; \
     }
 
+void CADeInitializeIPGlobals()
+{
+    if (caglobals.ip.u6.fd != -1)
+    {
+        close(caglobals.ip.u6.fd);
+        caglobals.ip.u6.fd = -1;
+    }
+
+    if (caglobals.ip.u6s.fd != -1)
+    {
+        close(caglobals.ip.u6s.fd);
+        caglobals.ip.u6s.fd = -1;
+    }
+
+    if (caglobals.ip.u4.fd != -1)
+    {
+        close(caglobals.ip.u4.fd);
+        caglobals.ip.u4.fd = -1;
+    }
+
+    if (caglobals.ip.u4s.fd != -1)
+    {
+        close(caglobals.ip.u4s.fd);
+        caglobals.ip.u4s.fd = -1;
+    }
+
+    if (caglobals.ip.m6.fd != -1)
+    {
+        close(caglobals.ip.m6.fd);
+        caglobals.ip.m6.fd = -1;
+    }
+
+    if (caglobals.ip.m6s.fd != -1)
+    {
+        close(caglobals.ip.m6s.fd);
+        caglobals.ip.m6s.fd = -1;
+    }
+
+    if (caglobals.ip.m4.fd != -1)
+    {
+        close(caglobals.ip.m4.fd);
+        caglobals.ip.m4.fd = -1;
+    }
+
+    if (caglobals.ip.m4s.fd != -1)
+    {
+        close(caglobals.ip.m4s.fd);
+        caglobals.ip.m4s.fd = -1;
+    }
+
+    if (caglobals.ip.netlinkFd != -1)
+    {
+        close(caglobals.ip.netlinkFd);
+        caglobals.ip.netlinkFd = -1;
+    }
+}
+
 static void CAReceiveHandler(void *data)
 {
     (void)data;
@@ -189,7 +250,12 @@ static void CASelectReturned(fd_set *readFds, int ret)
         else ISSET(m4s, readFds, CA_MULTICAST | CA_IPV4 | CA_SECURE)
         else if (FD_ISSET(caglobals.ip.netlinkFd, readFds))
         {
-            CAHandleNetlink();
+            CAInterface_t *ifchanged = CAFindInterfaceChange();
+            if (ifchanged)
+            {
+                CAProcessNewInterface(ifchanged);
+                OICFree(ifchanged);
+            }
             break;
         }
         else if (FD_ISSET(caglobals.ip.shutdownFds[0], readFds))
@@ -200,13 +266,6 @@ static void CASelectReturned(fd_set *readFds, int ret)
             {
                 continue;
             }
-
-            CAInterface_t *ifchanged = CAFindInterfaceChange();
-            if (ifchanged)
-            {
-                CAProcessNewInterface(ifchanged);
-                OICFree(ifchanged);
-            }
             break;
         }
         else
@@ -775,64 +834,15 @@ static void CAProcessNewInterface(CAInterface_t *ifitem)
         OIC_LOG(DEBUG, TAG, "ifitem is null");
         return;
     }
-
-    applyMulticastToInterface6(ifitem->index);
-    struct in_addr inaddr = { .s_addr = ifitem->ipv4addr };
-    applyMulticastToInterface4(inaddr);
-}
-static void CAHandleNetlink()
-{
-#ifdef __linux__
-    char buf[4096];
-    struct nlmsghdr *nh;
-    struct sockaddr_nl sa;
-    struct iovec iov = { buf, sizeof (buf) };
-    struct msghdr msg = { (void *)&sa, sizeof (sa), &iov, 1, NULL, 0, 0 };
-
-    size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
-
-    for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
+    if (ifitem->family == AF_INET6)
     {
-        if (nh->nlmsg_type != RTM_NEWLINK)
-        {
-            continue;
-        }
-
-        struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
-        if (!ifi || (ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
-        {
-            continue;
-        }
-
-        int newIndex = ifi->ifi_index;
-
-        u_arraylist_t *iflist = CAIPGetInterfaceInformation(newIndex);
-        if (!iflist)
-        {
-            OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
-            return;
-        }
-
-        uint32_t listLength = u_arraylist_length(iflist);
-        for (uint32_t i = 0; i < listLength; i++)
-        {
-            CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
-            if (!ifitem)
-            {
-                continue;
-            }
-
-            if ((int)ifitem->index != newIndex)
-            {
-                continue;
-            }
-
-            CAProcessNewInterface(ifitem);
-            break; // we found the one we were looking for
-        }
-        u_arraylist_destroy(iflist);
+        applyMulticastToInterface6(ifitem->index);
+    }
+    if (ifitem->family == AF_INET)
+    {
+        struct in_addr inaddr = { .s_addr = ifitem->ipv4addr };
+        applyMulticastToInterface4(inaddr);
     }
-#endif // __linux__
 }
 
 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
@@ -845,6 +855,11 @@ void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
     g_exceptionCallback = callback;
 }
 
+void CAIPSetConnectionStateChangeCallback(CAIPConnectionStateChangeCallback callback)
+{
+    CAIPSetNetworkMonitorCallback(callback);
+}
+
 static void sendData(int fd, const CAEndpoint_t *endpoint,
                      const void *data, uint32_t dlen,
                      const char *cast, const char *fam)