*
******************************************************************/
-#include "caipinterface.h"
+#define __APPLE_USE_RFC_3542 // for PKTINFO
+#define _GNU_SOURCE // for in6_pktinfo
#include <sys/types.h>
#include <sys/socket.h>
#endif
#include "pdu.h"
+#include "caipinterface.h"
#include "caadapterutils.h"
#ifdef __WITH_DTLS__
#include "caadapternetdtls.h"
static CAIPPacketReceivedCallback g_packetReceivedCallback;
static void CAHandleNetlink();
-static void CAApplyInterfaces();
static void CAFindReadyMessage();
static void CASelectReturned(fd_set *readFds, int ret);
static void CAProcessNewInterface(CAInterface_t *ifchanged);
static void CAReceiveHandler(void *data)
{
+ (void)data;
OIC_LOG(DEBUG, TAG, "IN");
-
while (!caglobals.ip.terminate)
{
CAFindReadyMessage();
static void CASelectReturned(fd_set *readFds, int ret)
{
+ (void)ret;
int fd = -1;
CATransportFlags_t flags = CA_DEFAULT_FLAGS;
CAHandleNetlink();
break;
}
- else
+ else if (FD_ISSET(caglobals.ip.shutdownFds[0], readFds))
{
+ char buf[10] = {0};
+ (void)read(caglobals.ip.shutdownFds[0], buf, sizeof (buf));
CAInterface_t *ifchanged = CAFindInterfaceChange();
if (ifchanged)
{
CAProcessNewInterface(ifchanged);
+ OICFree(ifchanged);
}
break;
}
+ else
+ {
+ break;
+ }
(void)CAReceiveMessage(fd, flags);
FD_CLR(fd, readFds);
{
char recvBuffer[COAP_MAX_PDU_SIZE];
+ size_t len;
+ int level, type;
struct sockaddr_storage srcAddr;
- socklen_t srcAddrLen = sizeof (srcAddr);
-
- ssize_t recvLen = recvfrom(fd,
- recvBuffer,
- sizeof (recvBuffer),
- 0,
- (struct sockaddr *)&srcAddr,
- &srcAddrLen);
+ unsigned char *pktinfo = NULL;
+ struct msghdr msg = { 0 };
+ struct cmsghdr *cmp;
+ struct iovec iov = { recvBuffer, sizeof (recvBuffer) };
+ union control
+ {
+ struct cmsghdr cmsg;
+ unsigned char data[CMSG_SPACE(sizeof (struct in6_pktinfo))];
+ } cmsg;
+
+ if (flags & CA_IPV6)
+ {
+ msg.msg_namelen = sizeof (struct sockaddr_in6);
+ level = IPPROTO_IPV6;
+ type = IPV6_PKTINFO;
+ len = sizeof (struct in6_pktinfo);
+ }
+ else
+ {
+ msg.msg_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);
+
+ ssize_t recvLen = recvmsg(fd, &msg, flags);
if (-1 == recvLen)
{
OIC_LOG_V(ERROR, TAG, "Recvfrom failed %s", strerror(errno));
return CA_STATUS_FAILED;
}
- CAEndpoint_t ep = { CA_ADAPTER_IP, flags };
+ if (flags & CA_MULTICAST)
+ {
+ for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL; cmp = CMSG_NXTHDR(&msg, cmp))
+ {
+ if (cmp->cmsg_level == level && cmp->cmsg_type == type)
+ {
+ pktinfo = CMSG_DATA(cmp);
+ }
+ }
+ }
+
+ CASecureEndpoint_t sep = {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = flags}};
if (flags & CA_IPV6)
{
- ep.interface = ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id;
+ sep.endpoint.interface = ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id;
((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id = 0;
+
+ if ((flags & CA_MULTICAST) && pktinfo)
+ {
+ struct in6_addr *addr = &(((struct in6_pktinfo *)pktinfo)->ipi6_addr);
+ unsigned char topbits = ((unsigned char *)addr)[0];
+ if (topbits != 0xff)
+ {
+ sep.endpoint.flags &= ~CA_MULTICAST;
+ }
+ }
+ }
+ else
+ {
+ if ((flags & CA_MULTICAST) && pktinfo)
+ {
+ struct in_addr *addr = &((struct in_pktinfo *)pktinfo)->ipi_addr;
+ uint32_t host = ntohl(addr->s_addr);
+ unsigned char topbits = ((unsigned char *)&host)[3];
+ if (topbits < 224 || topbits > 239)
+ {
+ sep.endpoint.flags &= ~CA_MULTICAST;
+ }
+ }
}
- CAConvertAddrToName(&srcAddr, ep.addr, &ep.port);
+
+ CAConvertAddrToName(&srcAddr, sep.endpoint.addr, &sep.endpoint.port);
if (flags & CA_SECURE)
{
#ifdef __WITH_DTLS__
- int ret = CAAdapterNetDtlsDecrypt(&ep, (uint8_t *)recvBuffer, recvLen);
+ int ret = CAAdapterNetDtlsDecrypt(&sep, (uint8_t *)recvBuffer, recvLen);
OIC_LOG_V(DEBUG, TAG, "CAAdapterNetDtlsDecrypt returns [%d]", ret);
#else
OIC_LOG(ERROR, TAG, "Encrypted message but no DTLS");
{
if (g_packetReceivedCallback)
{
- g_packetReceivedCallback(&ep, recvBuffer, recvLen);
+ g_packetReceivedCallback(&sep, recvBuffer, recvLen);
}
}
}
#endif
- struct sockaddr_storage sa = { family };
+ struct sockaddr_storage sa = { .ss_family = family };
+ socklen_t socklen;
+
if (family == AF_INET6)
{
int on = 1;
+
if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
{
OIC_LOG_V(ERROR, TAG, "IPV6_V6ONLY failed: %s", strerror(errno));
}
+
+ if (*port) // only do this for multicast ports
+ {
+ if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof (on)))
+ {
+ OIC_LOG_V(ERROR, TAG, "IPV6_RECVPKTINFO failed: %s", strerror(errno));
+ }
+ }
+
((struct sockaddr_in6 *)&sa)->sin6_port = htons(*port);
+ socklen = sizeof (struct sockaddr_in6);
}
else
{
+ if (*port) // only do this for multicast ports
+ {
+ int on = 1;
+ if (-1 == setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &on, sizeof (on)))
+ {
+ OIC_LOG_V(ERROR, TAG, "IP_PKTINFO failed: %s", strerror(errno));
+ }
+ }
+
((struct sockaddr_in *)&sa)->sin_port = htons(*port);
+ socklen = sizeof (struct sockaddr_in);
}
if (*port) // use the given port
}
}
- if (-1 == bind(fd, (struct sockaddr *)&sa, sizeof(sa)))
+ if (-1 == bind(fd, (struct sockaddr *)&sa, socklen))
{
OIC_LOG_V(ERROR, TAG, "bind socket failed: %s", strerror(errno));
close(fd);
if (!*port) // return the assigned port
{
- struct sockaddr_storage sa;
- socklen_t socklen = sizeof (sa);
if (-1 == getsockname(fd, (struct sockaddr *)&sa, &socklen))
{
OIC_LOG_V(ERROR, TAG, "getsockname failed: %s", strerror(errno));
OIC_LOG_V(DEBUG, TAG,
"socket summary: u6=%d, u6s=%d, u4=%d, u4s=%d, m6=%d, m6s=%d, m4=%d, m4s=%d",
- caglobals.ip.u6.fd, caglobals.ip.u6s.fd,
- caglobals.ip.u4.fd, caglobals.ip.u4s.fd,
- caglobals.ip.m6.fd, caglobals.ip.m6s.fd,
- caglobals.ip.m4.fd, caglobals.ip.m4s.fd);
+ caglobals.ip.u6.fd, caglobals.ip.u6s.fd, caglobals.ip.u4.fd, caglobals.ip.u4s.fd,
+ caglobals.ip.m6.fd, caglobals.ip.m6s.fd, caglobals.ip.m4.fd, caglobals.ip.m4s.fd);
+ OIC_LOG_V(DEBUG, TAG,
+ "port summary: u6 port=%d, u6s port=%d, u4 port=%d, u4s port=%d, m6 port=%d,"
+ "m6s port=%d, m4 port=%d, m4s port=%d",
+ 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);
// create pipe for fast shutdown
CAInitializePipe();
CHECKFD(caglobals.ip.shutdownFds[0]);
caglobals.ip.selectTimeout = CAGetPollingInterval(caglobals.ip.selectTimeout);
- CAApplyInterfaces();
+ res = CAIPStartListenServer();
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to start listening server![%d]", res);
+ return res;
+ }
caglobals.ip.terminate = false;
res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL);
{
OIC_LOG(DEBUG, TAG, "IN");
+ caglobals.ip.started = false;
caglobals.ip.terminate = true;
if (caglobals.ip.shutdownFds[1] != -1)
{
if (caglobals.ip.shutdownFds[1] != -1)
{
- write(caglobals.ip.shutdownFds[1], "w", 1);
+ ssize_t len = 0;
+ do
+ {
+ len = write(caglobals.ip.shutdownFds[1], "w", 1);
+ } while ((len == -1) && (errno == EINTR));
+ if ((len == -1) && (errno != EINTR) && (errno != EPIPE))
+ {
+ OIC_LOG_V(DEBUG, TAG, "write failed: %s", strerror(errno));
+ }
}
}
return;
}
- struct ip_mreq mreq = { IPv4MulticastAddress };
- mreq.imr_interface = inaddr;
+ struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress,
+ .imr_interface = inaddr};
if (setsockopt(caglobals.ip.m4.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
{
if (EADDRINUSE != errno)
//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);
//applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressGlb, interface);
}
-static void CAApplyInterfaces()
+CAResult_t CAIPStartListenServer()
{
u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
if (!iflist)
{
OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
- return;
+ return CA_STATUS_FAILED;
}
uint32_t len = u_arraylist_length(iflist);
{
CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
+ if (!ifitem)
+ {
+ continue;
+ }
if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
{
continue;
}
u_arraylist_destroy(iflist);
+ return CA_STATUS_OK;
+}
+
+CAResult_t CAIPStopListenServer()
+{
+ u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
+ if (!iflist)
+ {
+ OIC_LOG_V(ERROR, TAG, "Get interface info failed: %s", strerror(errno));
+ return CA_STATUS_FAILED;
+ }
+
+ uint32_t len = u_arraylist_length(iflist);
+ OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
+
+ for (uint32_t i = 0; i < len; i++)
+ {
+ CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
+
+ if (!ifitem)
+ {
+ continue;
+ }
+
+ if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
+ {
+ continue;
+ }
+ 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;
+ 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;
+ OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
+ }
+ }
+ u_arraylist_destroy(iflist);
+ return CA_STATUS_OK;
}
static void CAProcessNewInterface(CAInterface_t *ifitem)
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 };
-
-int len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
-for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
-{
- if (nh->nlmsg_type == RTM_NEWLINK)
+ 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 (nh->nlmsg_type != RTM_NEWLINK)
+ {
+ continue;
+ }
+
struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
- if ((ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
+ if (!ifi || (ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
{
continue;
}
return;
}
- uint32_t len = u_arraylist_length(iflist);
- for (uint32_t i = 0; i < len; i++)
+ 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->index != newIndex)
+ if (!ifitem)
+ {
+ continue;
+ }
+
+ if ((int)ifitem->index != newIndex)
{
continue;
}
CAProcessNewInterface(ifitem);
- break; // we found the one we were looking for
+ break; // we found the one we were looking for
}
u_arraylist_destroy(iflist);
}
-}
#endif // __linux__
}
void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
{
-OIC_LOG(DEBUG, TAG, "IN");
+ OIC_LOG(DEBUG, TAG, "IN");
-g_packetReceivedCallback = callback;
+ g_packetReceivedCallback = callback;
-OIC_LOG(DEBUG, TAG, "OUT");
+ OIC_LOG(DEBUG, TAG, "OUT");
}
void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
{
-OIC_LOG(DEBUG, TAG, "IN");
+ OIC_LOG(DEBUG, TAG, "IN");
g_exceptionCallback = callback;
struct sockaddr_storage sock;
CAConvertNameToAddr(endpoint->addr, endpoint->port, &sock);
+ socklen_t socklen;
if (sock.ss_family == AF_INET6)
{
struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)&sock;
{
sock6->sin6_scope_id = endpoint->interface;
}
+ socklen = sizeof(struct sockaddr_in6);
+ }
+ else
+ {
+ socklen = sizeof(struct sockaddr_in);
}
- ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, sizeof (sock));
+ ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, socklen);
if (-1 == len)
{
+ // If logging is not defined/enabled.
+ (void)cast;
+ (void)fam;
OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %s", secure, cast, fam, strerror(errno));
}
else
{
- OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %d bytes", secure, cast, fam, len);
+ OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %ld bytes", secure, cast, fam, len);
}
}
OIC_LOG_V(INFO, TAG, "IPv6 multicast scope invalid: %d", scope);
return;
}
- strncpy(endpoint->addr, ipv6mcname, MAX_ADDR_STR_SIZE_CA);
+ OICStrcpy(endpoint->addr, sizeof(endpoint->addr), ipv6mcname);
int fd = caglobals.ip.u6.fd;
uint32_t len = u_arraylist_length(iflist);
for (uint32_t i = 0; i < len; i++)
{
CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
+ if (!ifitem)
+ {
+ continue;
+ }
if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
{
continue;
CAEndpoint_t *endpoint,
const void *data, uint32_t datalen)
{
- struct ip_mreq mreq = { IPv4MulticastAddress };
- strncpy(endpoint->addr, IPv4_MULTICAST, MAX_ADDR_STR_SIZE_CA);
+ struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress };
+ OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
int fd = caglobals.ip.u4.fd;
uint32_t len = u_arraylist_length(iflist);
for (uint32_t i = 0; i < len; i++)
{
CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
+ if (!ifitem)
+ {
+ continue;
+ }
if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
{
continue;
mreq.imr_interface = 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)", strerror(errno));
+ OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)",
+ strerror(errno));
}
sendData(fd, endpoint, data, datalen, "multicast", "ipv4");
}
}
uint32_t len = u_arraylist_length(iflist);
+ uint32_t length = len;
- CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(len, sizeof (CAEndpoint_t));
+#ifdef __WITH_DTLS__
+ //If DTLS is supported, each interface can support secure port as well
+ length = len * 2;
+#endif
+
+ CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(length, sizeof (CAEndpoint_t));
if (!eps)
{
OIC_LOG(ERROR, TAG, "Malloc Failed");
for (uint32_t i = 0, j = 0; i < len; i++)
{
CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
+ if(!ifitem)
+ {
+ continue;
+ }
+
+ eps[j].adapter = CA_ADAPTER_IP;
+ eps[j].interface = 0;
+
+ if (ifitem->family == AF_INET6)
+ {
+ eps[j].flags = CA_IPV6;
+ eps[j].port = caglobals.ip.u6.port;
+ }
+ else
+ {
+ 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]);
+ }
+
+#ifdef __WITH_DTLS__
+ j++;
- OICStrcpy(eps[j].addr, CA_INTERFACE_NAME_SIZE, ifitem->name);
- eps[j].flags = ifitem->family == AF_INET6 ? CA_IPV6 : CA_IPV4;
eps[j].adapter = CA_ADAPTER_IP;
eps[j].interface = 0;
- eps[j].port = 0;
+
+ if (ifitem->family == AF_INET6)
+ {
+ eps[j].flags = CA_IPV6 | CA_SECURE;
+ eps[j].port = caglobals.ip.u6s.port;
+ }
+ else
+ {
+ 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]);
+ }
+#endif
j++;
}
OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
}
-