IOT-1447 Fix scope_id usage
authorDave Thaler <dthaler@microsoft.com>
Wed, 12 Oct 2016 17:32:13 +0000 (10:32 -0700)
committerAshok Babu Channa <ashok.channa@samsung.com>
Fri, 14 Oct 2016 05:01:58 +0000 (05:01 +0000)
Never assume that a scope id and an interface index are the same.
Removed bad code from the tcp adapter; the scope id should always
be present in the addr string of a scoped IPv6 address, and is correctly
handled by CAConvertNameToAddr and CAConvertAddrToName.

Change-Id: I19f8739d7da2e7944e3904770ca0d8e914608ba2
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/13181
Reviewed-by: Jaewook Jung <jw0213.jung@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Dan Mihai <Daniel.Mihai@microsoft.com>
Reviewed-by: David Antler <david.a.antler@intel.com>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
resource/c_common/SConscript
resource/csdk/connectivity/src/ip_adapter/caipserver.c
resource/csdk/connectivity/src/tcp_adapter/catcpserver.c

index b167e74..d2c1e95 100644 (file)
@@ -64,6 +64,7 @@ cxx_headers = ['arpa/inet.h',
                'in6addr.h',
                'linux/limits.h',
                'memory.h',
+               'net/if.h',
                'netdb.h',
                'netinet/in.h',
                'pthread.h',
index 10f988b..4ed0679 100644 (file)
 
 #include "iotivity_config.h"
 #include <sys/types.h>
-#if !defined(_WIN32)
+#ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
-
-#if defined(_WIN32)
 #include <assert.h>
+#ifdef HAVE_WINSOCK2_H
 #include <winsock2.h>
-#include <ws2def.h>
-#include <mswsock.h>
+#endif
+#ifdef HAVE_WS2TCPIP_H
 #include <ws2tcpip.h>
 #endif
-
 #include <stdio.h>
-#if !defined(_MSC_VER)
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif //!defined(_MSC_VER)
-#include <sys/types.h>
+#endif
 #include <fcntl.h>
-#if !defined(_WIN32)
+#ifdef HAVE_SYS_SELECT_H
 #include <sys/select.h>
+#endif
+#ifdef HAVE_ARPA_INET_H
 #include <arpa/inet.h>
+#endif
+#ifdef HAVE_NETINET_IN_H
 #include <netinet/in.h>
+#endif
+#ifdef HAVE_NET_IF_H
 #include <net/if.h>
 #endif
 #include <errno.h>
@@ -556,14 +559,11 @@ static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags)
         return CA_STATUS_FAILED;
     }
 
-    if (flags & CA_MULTICAST)
+    for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL; cmp = CMSG_NXTHDR(&msg, cmp))
     {
-        for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL; cmp = CMSG_NXTHDR(&msg, cmp))
+        if (cmp->cmsg_level == level && cmp->cmsg_type == type)
         {
-            if (cmp->cmsg_level == level && cmp->cmsg_type == type)
-            {
-                pktinfo = CMSG_DATA(cmp);
-            }
+            pktinfo = CMSG_DATA(cmp);
         }
     }
 #else // if defined(WSA_CMSG_DATA)
@@ -603,15 +603,12 @@ static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags)
         OIC_LOG_V(ERROR, TAG, "WSARecvMsg failed %i", WSAGetLastError());
     }
 
-    if (flags & CA_MULTICAST)
+    for (WSACMSGHDR *cmp = WSA_CMSG_FIRSTHDR(&msg); cmp != NULL;
+         cmp = WSA_CMSG_NXTHDR(&msg, cmp))
     {
-        for (WSACMSGHDR *cmp = WSA_CMSG_FIRSTHDR(&msg); cmp != NULL;
-             cmp = WSA_CMSG_NXTHDR(&msg, cmp))
+        if (cmp->cmsg_level == level && cmp->cmsg_type == type)
         {
-            if (cmp->cmsg_level == level && cmp->cmsg_type == type)
-            {
-                pktinfo = WSA_CMSG_DATA(cmp);
-            }
+            pktinfo = WSA_CMSG_DATA(cmp);
         }
     }
 #endif // !defined(WSA_CMSG_DATA)
@@ -619,8 +616,9 @@ static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags)
 
     if (flags & CA_IPV6)
     {
-        /** @todo figure out correct usage for ifindex, and sin6_scope_id.*/
-        if ((flags & CA_MULTICAST) && pktinfo)
+        sep.endpoint.ifindex = ((struct in6_pktinfo *)pktinfo)->ipi6_ifindex;
+
+        if (flags & CA_MULTICAST)
         {
             struct in6_addr *addr = &(((struct in6_pktinfo *)pktinfo)->ipi6_addr);
             unsigned char topbits = ((unsigned char *)addr)[0];
@@ -632,7 +630,9 @@ static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags)
     }
     else
     {
-        if ((flags & CA_MULTICAST) && pktinfo)
+        sep.endpoint.ifindex = ((struct in_pktinfo *)pktinfo)->ipi_ifindex;
+
+        if (flags & CA_MULTICAST)
         {
             struct in_addr *addr = &((struct in_pktinfo *)pktinfo)->ipi_addr;
             uint32_t host = ntohl(addr->s_addr);
@@ -664,7 +664,6 @@ static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags)
     }
 
     return CA_STATUS_OK;
-
 }
 
 void CAIPPullData()
@@ -707,16 +706,13 @@ static CASocketFd_t CACreateSocket(int family, uint16_t *port, bool isMulticast)
             OIC_LOG_V(ERROR, TAG, "IPV6_V6ONLY failed: %s", CAIPS_GET_ERROR);
         }
 
-        if (isMulticast && *port) // only do this for multicast ports
-        {
 #if defined(IPV6_RECVPKTINFO)
-            if (OC_SOCKET_ERROR == setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof (on)))
+        if (OC_SOCKET_ERROR == setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof (on)))
 #else
-            if (OC_SOCKET_ERROR == setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, OPTVAL_T(&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);
-            }
+        {
+            OIC_LOG_V(ERROR, TAG, "IPV6_RECVPKTINFO failed: %s",CAIPS_GET_ERROR);
         }
 
         ((struct sockaddr_in6 *)&sa)->sin6_port = htons(*port);
@@ -724,13 +720,10 @@ static CASocketFd_t CACreateSocket(int family, uint16_t *port, bool isMulticast)
     }
     else
     {
-        if (isMulticast && *port) // only do this for multicast ports
+        int on = 1;
+        if (OC_SOCKET_ERROR == setsockopt(fd, IPPROTO_IP, IP_PKTINFO, OPTVAL_T(&on), sizeof (on)))
         {
-            int on = 1;
-            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);
-            }
+            OIC_LOG_V(ERROR, TAG, "IP_PKTINFO failed: %s", CAIPS_GET_ERROR);
         }
 
         ((struct sockaddr_in *)&sa)->sin_port = htons(*port);
@@ -1267,7 +1260,6 @@ static void sendData(int fd, const CAEndpoint_t *endpoint,
     socklen_t socklen = 0;
     if (sock.ss_family == AF_INET6)
     {
-        /** @todo figure out correct usage for ifindex, and sin6_scope_id */
         socklen = sizeof(struct sockaddr_in6);
     }
     else
index 4cdcf9f..f18a6c9 100644 (file)
@@ -686,11 +686,6 @@ static int CATCPCreateSocket(int family, CATCPSessionInfo_t *svritem)
     socklen_t socklen = 0;
     if (sa.ss_family == AF_INET6)
     {
-        struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)&sa;
-        if (!sock6->sin6_scope_id)
-        {
-            sock6->sin6_scope_id = svritem->sep.endpoint.ifindex;
-        }
         socklen = sizeof(struct sockaddr_in6);
     }
     else