From ac60c98eb4d77b73d5cfe614133607eec0bdd3ae Mon Sep 17 00:00:00 2001 From: Dave Thaler Date: Wed, 12 Oct 2016 10:32:13 -0700 Subject: [PATCH] IOT-1447 Fix scope_id usage 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 Reviewed-on: https://gerrit.iotivity.org/gerrit/13181 Reviewed-by: Jaewook Jung Tested-by: jenkins-iotivity Reviewed-by: Dan Mihai Reviewed-by: David Antler Reviewed-by: Ashok Babu Channa --- resource/c_common/SConscript | 1 + .../connectivity/src/ip_adapter/caipserver.c | 74 +++++++++---------- .../src/tcp_adapter/catcpserver.c | 5 -- 3 files changed, 34 insertions(+), 46 deletions(-) diff --git a/resource/c_common/SConscript b/resource/c_common/SConscript index b167e7466..d2c1e9548 100644 --- a/resource/c_common/SConscript +++ b/resource/c_common/SConscript @@ -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', diff --git a/resource/csdk/connectivity/src/ip_adapter/caipserver.c b/resource/csdk/connectivity/src/ip_adapter/caipserver.c index 10f988bfc..4ed067913 100644 --- a/resource/csdk/connectivity/src/ip_adapter/caipserver.c +++ b/resource/csdk/connectivity/src/ip_adapter/caipserver.c @@ -27,28 +27,31 @@ #include "iotivity_config.h" #include -#if !defined(_WIN32) +#ifdef HAVE_SYS_SOCKET_H #include #endif - -#if defined(_WIN32) #include +#ifdef HAVE_WINSOCK2_H #include -#include -#include +#endif +#ifdef HAVE_WS2TCPIP_H #include #endif - #include -#if !defined(_MSC_VER) +#ifdef HAVE_UNISTD_H #include -#endif //!defined(_MSC_VER) -#include +#endif #include -#if !defined(_WIN32) +#ifdef HAVE_SYS_SELECT_H #include +#endif +#ifdef HAVE_ARPA_INET_H #include +#endif +#ifdef HAVE_NETINET_IN_H #include +#endif +#ifdef HAVE_NET_IF_H #include #endif #include @@ -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 diff --git a/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c b/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c index 4cdcf9f47..f18a6c950 100644 --- a/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c +++ b/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c @@ -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 -- 2.34.1