Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / caipserver.c
index 08c46b0..c49b064 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>
 #include "oic_malloc.h"
 #include "oic_string.h"
 
-/**
- * @def TAG
- * @brief Logging tag for module name
+/*
+ * Logging tag for module name
  */
-#define TAG "IP_SERVER"
+#define TAG "OIC_CA_IP_SERVER"
 
 #define SELECT_TIMEOUT 1     // select() seconds (and termination latency)
 
@@ -116,16 +119,65 @@ 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;
+    }
+}
+
 static void CAReceiveHandler(void *data)
 {
     (void)data;
-    OIC_LOG(DEBUG, TAG, "IN");
+
     while (!caglobals.ip.terminate)
     {
         CAFindReadyMessage();
     }
-
-    OIC_LOG(DEBUG, TAG, "OUT");
 }
 
 static void CAFindReadyMessage()
@@ -197,8 +249,13 @@ static void CASelectReturned(fd_set *readFds, int ret)
         }
         else if (FD_ISSET(caglobals.ip.shutdownFds[0], readFds))
         {
-            char buf[10];
-            (void)read(caglobals.ip.shutdownFds[0], buf, sizeof (buf));
+            char buf[10] = {0};
+            ssize_t len = read(caglobals.ip.shutdownFds[0], buf, sizeof (buf));
+            if (-1 == len)
+            {
+                continue;
+            }
+
             CAInterface_t *ifchanged = CAFindInterfaceChange();
             if (ifchanged)
             {
@@ -222,11 +279,10 @@ static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags)
     char recvBuffer[COAP_MAX_PDU_SIZE];
 
     size_t len;
-    int level, type;
+    int level, type, namelen;
     struct sockaddr_storage srcAddr;
     unsigned char *pktinfo = NULL;
-    struct msghdr msg = { 0 };
-    struct cmsghdr *cmp;
+    struct cmsghdr *cmp = NULL;
     struct iovec iov = { recvBuffer, sizeof (recvBuffer) };
     union control
     {
@@ -236,24 +292,25 @@ static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags)
 
     if (flags & CA_IPV6)
     {
-        msg.msg_namelen = sizeof (struct sockaddr_in6);
+        namelen = sizeof (struct sockaddr_in6);
         level = IPPROTO_IPV6;
         type = IPV6_PKTINFO;
         len = sizeof (struct in6_pktinfo);
     }
     else
     {
-        msg.msg_namelen = sizeof (struct sockaddr_in);
+        namelen = sizeof (struct sockaddr_in);
         level = IPPROTO_IP;
         type = IP_PKTINFO;
         len = sizeof (struct in6_pktinfo);
     }
 
-    msg.msg_name = &srcAddr;
-    msg.msg_iov = &iov;
-    msg.msg_iovlen = 1;
-    msg.msg_control = &cmsg;
-    msg.msg_controllen = CMSG_SPACE(len);
+    struct msghdr msg = { .msg_name = &srcAddr,
+                          .msg_namelen = namelen,
+                          .msg_iov = &iov,
+                          .msg_iovlen = 1,
+                          .msg_control = &cmsg,
+                          .msg_controllen = CMSG_SPACE(len) };
 
     ssize_t recvLen = recvmsg(fd, &msg, flags);
     if (-1 == recvLen)
@@ -335,9 +392,9 @@ void CAIPPullData()
 static int CACreateSocket(int family, uint16_t *port)
 {
     int socktype = SOCK_DGRAM;
-    #ifdef SOCK_CLOEXEC
+#ifdef SOCK_CLOEXEC
     socktype |= SOCK_CLOEXEC;
-    #endif
+#endif
     int fd = socket(family, socktype, IPPROTO_UDP);
     if (-1 == fd)
     {
@@ -345,7 +402,7 @@ static int CACreateSocket(int family, uint16_t *port)
         return -1;
     }
 
-    #ifndef SOCK_CLOEXEC
+#ifndef SOCK_CLOEXEC
     int fl = fcntl(fd, F_GETFD);
     if (-1 == fl || -1 == fcntl(fd, F_SETFD, fl|FD_CLOEXEC))
     {
@@ -353,7 +410,7 @@ static int CACreateSocket(int family, uint16_t *port)
         close(fd);
         return -1;
     }
-    #endif
+#endif
 
     struct sockaddr_storage sa = { .ss_family = family };
     socklen_t socklen;
@@ -511,7 +568,7 @@ CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
 
     if (!IPv4MulticastAddress.s_addr)
     {
-        (void)inet_aton(IPv4_MULTICAST, &IPv4MulticastAddress);
+        (void)inet_pton(AF_INET, IPv4_MULTICAST, &IPv4MulticastAddress);
         (void)inet_pton(AF_INET6, IPv6_MULTICAST_INT, &IPv6MulticastAddressInt);
         (void)inet_pton(AF_INET6, IPv6_MULTICAST_LNK, &IPv6MulticastAddressLnk);
         (void)inet_pton(AF_INET6, IPv6_MULTICAST_RLM, &IPv6MulticastAddressRlm);
@@ -586,8 +643,6 @@ CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
 
 void CAIPStopServer()
 {
-    OIC_LOG(DEBUG, TAG, "IN");
-
     caglobals.ip.started = false;
     caglobals.ip.terminate = true;
 
@@ -600,8 +655,6 @@ void CAIPStopServer()
     {
         // receive thread will stop in SELECT_TIMEOUT seconds.
     }
-
-    OIC_LOG(DEBUG, TAG, "OUT");
 }
 
 void CAWakeUpForChange()
@@ -627,8 +680,9 @@ static void applyMulticastToInterface4(struct in_addr inaddr)
         return;
     }
 
-    struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress,
-                            .imr_interface = inaddr};
+    struct ip_mreqn mreq = { .imr_multiaddr = IPv4MulticastAddress,
+                             .imr_address = inaddr,
+                             .imr_ifindex = 0 };
     if (setsockopt(caglobals.ip.m4.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
     {
         if (EADDRINUSE != errno)
@@ -647,9 +701,8 @@ static void applyMulticastToInterface4(struct in_addr inaddr)
 
 static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t interface)
 {
-    struct ipv6_mreq mreq;
-    mreq.ipv6mr_multiaddr = *addr;
-    mreq.ipv6mr_interface = interface;
+    struct ipv6_mreq mreq = {.ipv6mr_multiaddr = *addr, .ipv6mr_interface = interface};
+
     if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof (mreq)))
     {
         if (EADDRINUSE != errno)
@@ -672,6 +725,7 @@ static void applyMulticastToInterface6(uint32_t interface)
     //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressSit, interface);
     //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressOrg, interface);
     //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressGlb, interface);
+
     //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressInt, interface);
     applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressLnk, interface);
     //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressRlm, interface);
@@ -771,9 +825,14 @@ CAResult_t CAIPStopListenServer()
 
 static void CAProcessNewInterface(CAInterface_t *ifitem)
 {
+    if (!ifitem)
+    {
+        OIC_LOG(DEBUG, TAG, "ifitem is null");
+        return;
+    }
+
     applyMulticastToInterface6(ifitem->index);
-    struct in_addr inaddr;
-    inaddr.s_addr = ifitem->ipv4addr;
+    struct in_addr inaddr = { .s_addr = ifitem->ipv4addr };
     applyMulticastToInterface4(inaddr);
 }
 static void CAHandleNetlink()
@@ -789,7 +848,7 @@ static void CAHandleNetlink()
 
     for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
     {
-        if (nh->nlmsg_type != RTM_NEWLINK)
+        if (nh != NULL && nh->nlmsg_type != RTM_NEWLINK)
         {
             continue;
         }
@@ -833,20 +892,12 @@ static void CAHandleNetlink()
 
 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
 {
-    OIC_LOG(DEBUG, TAG, "IN");
-
     g_packetReceivedCallback = callback;
-
-    OIC_LOG(DEBUG, TAG, "OUT");
 }
 
 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
 {
-    OIC_LOG(DEBUG, TAG, "IN");
-
     g_exceptionCallback = callback;
-
-    OIC_LOG(DEBUG, TAG, "OUT");
 }
 
 static void sendData(int fd, const CAEndpoint_t *endpoint,
@@ -855,6 +906,12 @@ static void sendData(int fd, const CAEndpoint_t *endpoint,
 {
     OIC_LOG(DEBUG, TAG, "IN");
 
+    if (!endpoint)
+    {
+        OIC_LOG(DEBUG, TAG, "endpoint is null");
+        return;
+    }
+
     char *secure = (endpoint->flags & CA_SECURE) ? "secure " : "";
     (void)secure;   // eliminates release warning
     struct sockaddr_storage sock;
@@ -885,7 +942,7 @@ static void sendData(int fd, const CAEndpoint_t *endpoint,
     }
     else
     {
-        OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %ld bytes", secure, cast, fam, len);
+        OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %zd bytes", secure, cast, fam, len);
     }
 }
 
@@ -893,6 +950,12 @@ static void sendMulticastData6(const u_arraylist_t *iflist,
                                CAEndpoint_t *endpoint,
                                const void *data, uint32_t datalen)
 {
+    if (!endpoint)
+    {
+        OIC_LOG(DEBUG, TAG, "endpoint is null");
+        return;
+    }
+
     int scope = endpoint->flags & CA_SCOPE_MASK;
     char *ipv6mcname = ipv6mcnames[scope];
     if (!ipv6mcname)
@@ -934,7 +997,10 @@ static void sendMulticastData4(const u_arraylist_t *iflist,
                                CAEndpoint_t *endpoint,
                                const void *data, uint32_t datalen)
 {
-    struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress };
+    VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
+
+    struct ip_mreqn mreq = { .imr_multiaddr = IPv4MulticastAddress,
+                             .imr_ifindex = 0 };
     OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
     int fd = caglobals.ip.u4.fd;
 
@@ -957,7 +1023,7 @@ static void sendMulticastData4(const u_arraylist_t *iflist,
 
         struct in_addr inaddr;
         inaddr.s_addr = ifitem->ipv4addr;
-        mreq.imr_interface = inaddr;
+        mreq.imr_address = inaddr;
         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof (mreq)))
         {
             OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)",
@@ -968,7 +1034,7 @@ static void sendMulticastData4(const u_arraylist_t *iflist,
 }
 
 void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
-                                                            bool isMulticast)
+                  bool isMulticast)
 {
     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
     VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
@@ -1008,17 +1074,17 @@ void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
         if (caglobals.ip.ipv6enabled && (endpoint->flags & CA_IPV6))
         {
             fd = isSecure ? caglobals.ip.u6s.fd : caglobals.ip.u6.fd;
-            #ifndef __WITH_DTLS__
+#ifndef __WITH_DTLS__
             fd = caglobals.ip.u6.fd;
-            #endif
+#endif
             sendData(fd, endpoint, data, datalen, "unicast", "ipv6");
         }
         if (caglobals.ip.ipv4enabled && (endpoint->flags & CA_IPV4))
         {
             fd = isSecure ? caglobals.ip.u4s.fd : caglobals.ip.u4.fd;
-            #ifndef __WITH_DTLS__
+#ifndef __WITH_DTLS__
             fd = caglobals.ip.u4.fd;
-            #endif
+#endif
             sendData(fd, endpoint, data, datalen, "unicast", "ipv4");
         }
     }
@@ -1026,8 +1092,6 @@ void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
 
 CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
 {
-    OIC_LOG(DEBUG, TAG, "IN");
-
     VERIFY_NON_NULL(info, TAG, "info is NULL");
     VERIFY_NON_NULL(size, TAG, "size is NULL");
 
@@ -1075,9 +1139,7 @@ CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
             eps[j].flags = CA_IPV4;
             eps[j].port = caglobals.ip.u4.port;
 
-            unsigned char *addr=  (unsigned char *) &(ifitem->ipv4addr);
-            snprintf(eps[j].addr, MAX_ADDR_STR_SIZE_CA, "%d.%d.%d.%d",
-                     addr[0], addr[1], addr[2], addr[3]);
+            inet_ntop(AF_INET, &(ifitem->ipv4addr), eps[j].addr, MAX_ADDR_STR_SIZE_CA);
         }
 
 #ifdef __WITH_DTLS__
@@ -1095,10 +1157,7 @@ CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
         {
             eps[j].flags = CA_IPV4 | CA_SECURE;
             eps[j].port = caglobals.ip.u4s.port;
-
-            unsigned char *addr=  (unsigned char *) &(ifitem->ipv4addr);
-            snprintf(eps[j].addr, MAX_ADDR_STR_SIZE_CA, "%d.%d.%d.%d",
-                     addr[0], addr[1], addr[2], addr[3]);
+            inet_ntop(AF_INET, &(ifitem->ipv4addr), eps[j].addr, MAX_ADDR_STR_SIZE_CA);
         }
 #endif
         j++;
@@ -1109,6 +1168,5 @@ CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
 
     u_arraylist_destroy(iflist);
 
-    OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }