1 /*****************************************************************j
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #ifndef __APPLE_USE_RFC_3542
22 #define __APPLE_USE_RFC_3542 // for PKTINFO
25 #define _GNU_SOURCE // for in6_pktinfo
28 #include <sys/types.h>
29 #include <sys/socket.h>
32 #include <sys/types.h>
34 #include <sys/select.h>
35 #include <arpa/inet.h>
36 #include <netinet/in.h>
40 #include <linux/netlink.h>
41 #include <linux/rtnetlink.h>
45 #include "caipinterface.h"
46 #include "caadapterutils.h"
48 #include "caadapternetdtls.h"
51 #include "oic_malloc.h"
52 #include "oic_string.h"
55 * Logging tag for module name
57 #define TAG "OIC_CA_IP_SERVER"
59 #define SELECT_TIMEOUT 1 // select() seconds (and termination latency)
61 #define IPv4_MULTICAST "224.0.1.187"
62 static struct in_addr IPv4MulticastAddress = { 0 };
64 #define IPv6_DOMAINS 16
65 #define IPv6_MULTICAST_INT "ff01::fd"
66 static struct in6_addr IPv6MulticastAddressInt;
67 #define IPv6_MULTICAST_LNK "ff02::fd"
68 static struct in6_addr IPv6MulticastAddressLnk;
69 #define IPv6_MULTICAST_RLM "ff03::fd"
70 static struct in6_addr IPv6MulticastAddressRlm;
71 #define IPv6_MULTICAST_ADM "ff04::fd"
72 static struct in6_addr IPv6MulticastAddressAdm;
73 #define IPv6_MULTICAST_SIT "ff05::fd"
74 static struct in6_addr IPv6MulticastAddressSit;
75 #define IPv6_MULTICAST_ORG "ff08::fd"
76 static struct in6_addr IPv6MulticastAddressOrg;
77 #define IPv6_MULTICAST_GLB "ff0e::fd"
78 static struct in6_addr IPv6MulticastAddressGlb;
80 static char *ipv6mcnames[IPv6_DOMAINS] = {
99 static CAIPExceptionCallback g_exceptionCallback;
101 static CAIPPacketReceivedCallback g_packetReceivedCallback;
103 static void CAHandleNetlink();
104 static void CAFindReadyMessage();
105 static void CASelectReturned(fd_set *readFds, int ret);
106 static void CAProcessNewInterface(CAInterface_t *ifchanged);
107 static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags);
109 #define SET(TYPE, FDS) \
110 if (caglobals.ip.TYPE.fd != -1) \
112 FD_SET(caglobals.ip.TYPE.fd, FDS); \
115 #define ISSET(TYPE, FDS, FLAGS) \
116 if (caglobals.ip.TYPE.fd != -1 && FD_ISSET(caglobals.ip.TYPE.fd, FDS)) \
118 fd = caglobals.ip.TYPE.fd; \
122 static void CAReceiveHandler(void *data)
126 while (!caglobals.ip.terminate)
128 CAFindReadyMessage();
132 static void CAFindReadyMessage()
135 struct timeval timeout;
137 timeout.tv_sec = caglobals.ip.selectTimeout;
139 struct timeval *tv = caglobals.ip.selectTimeout == -1 ? NULL : &timeout;
150 if (caglobals.ip.shutdownFds[0] != -1)
152 FD_SET(caglobals.ip.shutdownFds[0], &readFds);
154 if (caglobals.ip.netlinkFd != -1)
156 FD_SET(caglobals.ip.netlinkFd, &readFds);
159 int ret = select(caglobals.ip.maxfd + 1, &readFds, NULL, NULL, tv);
161 if (caglobals.ip.terminate)
163 OIC_LOG_V(DEBUG, TAG, "Packet receiver Stop request received.");
170 OIC_LOG_V(FATAL, TAG, "select error %s", strerror(errno));
175 CASelectReturned(&readFds, ret);
178 static void CASelectReturned(fd_set *readFds, int ret)
182 CATransportFlags_t flags = CA_DEFAULT_FLAGS;
184 while (!caglobals.ip.terminate)
186 ISSET(u6, readFds, CA_IPV6)
187 else ISSET(u6s, readFds, CA_IPV6 | CA_SECURE)
188 else ISSET(u4, readFds, CA_IPV4)
189 else ISSET(u4s, readFds, CA_IPV4 | CA_SECURE)
190 else ISSET(m6, readFds, CA_MULTICAST | CA_IPV6)
191 else ISSET(m6s, readFds, CA_MULTICAST | CA_IPV6 | CA_SECURE)
192 else ISSET(m4, readFds, CA_MULTICAST | CA_IPV4)
193 else ISSET(m4s, readFds, CA_MULTICAST | CA_IPV4 | CA_SECURE)
194 else if (FD_ISSET(caglobals.ip.netlinkFd, readFds))
199 else if (FD_ISSET(caglobals.ip.shutdownFds[0], readFds))
202 ssize_t len = read(caglobals.ip.shutdownFds[0], buf, sizeof (buf));
208 CAInterface_t *ifchanged = CAFindInterfaceChange();
211 CAProcessNewInterface(ifchanged);
221 (void)CAReceiveMessage(fd, flags);
226 static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags)
228 char recvBuffer[COAP_MAX_PDU_SIZE];
231 int level, type, namelen;
232 struct sockaddr_storage srcAddr;
233 unsigned char *pktinfo = NULL;
234 struct cmsghdr *cmp = NULL;
235 struct iovec iov = { recvBuffer, sizeof (recvBuffer) };
239 unsigned char data[CMSG_SPACE(sizeof (struct in6_pktinfo))];
244 namelen = sizeof (struct sockaddr_in6);
245 level = IPPROTO_IPV6;
247 len = sizeof (struct in6_pktinfo);
251 namelen = sizeof (struct sockaddr_in);
254 len = sizeof (struct in6_pktinfo);
257 struct msghdr msg = { .msg_name = &srcAddr,
258 .msg_namelen = namelen,
261 .msg_control = &cmsg,
262 .msg_controllen = CMSG_SPACE(len) };
264 ssize_t recvLen = recvmsg(fd, &msg, flags);
267 OIC_LOG_V(ERROR, TAG, "Recvfrom failed %s", strerror(errno));
268 return CA_STATUS_FAILED;
271 if (flags & CA_MULTICAST)
273 for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL; cmp = CMSG_NXTHDR(&msg, cmp))
275 if (cmp->cmsg_level == level && cmp->cmsg_type == type)
277 pktinfo = CMSG_DATA(cmp);
282 CASecureEndpoint_t sep = {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = flags}};
286 sep.endpoint.interface = ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id;
287 ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id = 0;
289 if ((flags & CA_MULTICAST) && pktinfo)
291 struct in6_addr *addr = &(((struct in6_pktinfo *)pktinfo)->ipi6_addr);
292 unsigned char topbits = ((unsigned char *)addr)[0];
295 sep.endpoint.flags &= ~CA_MULTICAST;
301 if ((flags & CA_MULTICAST) && pktinfo)
303 struct in_addr *addr = &((struct in_pktinfo *)pktinfo)->ipi_addr;
304 uint32_t host = ntohl(addr->s_addr);
305 unsigned char topbits = ((unsigned char *)&host)[3];
306 if (topbits < 224 || topbits > 239)
308 sep.endpoint.flags &= ~CA_MULTICAST;
313 CAConvertAddrToName(&srcAddr, msg.msg_namelen, sep.endpoint.addr, &sep.endpoint.port);
315 if (flags & CA_SECURE)
318 int ret = CAAdapterNetDtlsDecrypt(&sep, (uint8_t *)recvBuffer, recvLen);
319 OIC_LOG_V(DEBUG, TAG, "CAAdapterNetDtlsDecrypt returns [%d]", ret);
321 OIC_LOG(ERROR, TAG, "Encrypted message but no DTLS");
326 if (g_packetReceivedCallback)
328 g_packetReceivedCallback(&sep, recvBuffer, recvLen);
337 OIC_LOG(DEBUG, TAG, "IN");
338 OIC_LOG(DEBUG, TAG, "OUT");
341 static int CACreateSocket(int family, uint16_t *port)
343 int socktype = SOCK_DGRAM;
345 socktype |= SOCK_CLOEXEC;
347 int fd = socket(family, socktype, IPPROTO_UDP);
350 OIC_LOG_V(ERROR, TAG, "create socket failed: %s", strerror(errno));
355 int fl = fcntl(fd, F_GETFD);
356 if (-1 == fl || -1 == fcntl(fd, F_SETFD, fl|FD_CLOEXEC))
358 OIC_LOG_V(ERROR, TAG, "set FD_CLOEXEC failed: %s", strerror(errno));
364 struct sockaddr_storage sa = { .ss_family = family };
367 if (family == AF_INET6)
371 if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
373 OIC_LOG_V(ERROR, TAG, "IPV6_V6ONLY failed: %s", strerror(errno));
376 if (*port) // only do this for multicast ports
378 if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof (on)))
380 OIC_LOG_V(ERROR, TAG, "IPV6_RECVPKTINFO failed: %s", strerror(errno));
384 ((struct sockaddr_in6 *)&sa)->sin6_port = htons(*port);
385 socklen = sizeof (struct sockaddr_in6);
389 if (*port) // only do this for multicast ports
392 if (-1 == setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &on, sizeof (on)))
394 OIC_LOG_V(ERROR, TAG, "IP_PKTINFO failed: %s", strerror(errno));
398 ((struct sockaddr_in *)&sa)->sin_port = htons(*port);
399 socklen = sizeof (struct sockaddr_in);
402 if (*port) // use the given port
405 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)))
407 OIC_LOG_V(ERROR, TAG, "SO_REUSEADDR failed: %s", strerror(errno));
413 if (-1 == bind(fd, (struct sockaddr *)&sa, socklen))
415 OIC_LOG_V(ERROR, TAG, "bind socket failed: %s", strerror(errno));
420 if (!*port) // return the assigned port
422 if (-1 == getsockname(fd, (struct sockaddr *)&sa, &socklen))
424 OIC_LOG_V(ERROR, TAG, "getsockname failed: %s", strerror(errno));
428 *port = ntohs(family == AF_INET6 ?
429 ((struct sockaddr_in6 *)&sa)->sin6_port :
430 ((struct sockaddr_in *)&sa)->sin_port);
436 #define CHECKFD(FD) \
437 if (FD > caglobals.ip.maxfd) \
438 caglobals.ip.maxfd = FD;
439 #define NEWSOCKET(FAMILY, NAME) \
440 caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port); \
441 CHECKFD(caglobals.ip.NAME.fd)
443 static void CAInitializeNetlink()
446 // create NETLINK fd for interface change notifications
447 struct sockaddr_nl sa = { AF_NETLINK, 0, 0, RTMGRP_LINK };
449 caglobals.ip.netlinkFd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE);
450 if (caglobals.ip.netlinkFd == -1)
452 OIC_LOG_V(ERROR, TAG, "netlink socket failed: %s", strerror(errno));
456 int r = bind(caglobals.ip.netlinkFd, (struct sockaddr *)&sa, sizeof (sa));
459 OIC_LOG_V(ERROR, TAG, "netlink bind failed: %s", strerror(errno));
460 close(caglobals.ip.netlinkFd);
461 caglobals.ip.netlinkFd = -1;
465 CHECKFD(caglobals.ip.netlinkFd);
471 static void CAInitializePipe()
473 caglobals.ip.selectTimeout = -1;
475 int ret = pipe2(caglobals.ip.shutdownFds, O_CLOEXEC);
477 int ret = pipe(caglobals.ip.shutdownFds);
480 ret = fcntl(caglobals.ip.shutdownFds[0], F_GETFD);
483 ret = fcntl(caglobals.ip.shutdownFds[0], F_SETFD, ret|FD_CLOEXEC);
487 ret = fcntl(caglobals.ip.shutdownFds[1], F_GETFD);
491 ret = fcntl(caglobals.ip.shutdownFds[1], F_SETFD, ret|FD_CLOEXEC);
495 close(caglobals.ip.shutdownFds[1]);
496 close(caglobals.ip.shutdownFds[0]);
497 caglobals.ip.shutdownFds[0] = -1;
498 caglobals.ip.shutdownFds[1] = -1;
504 OIC_LOG_V(ERROR, TAG, "pipe failed: %s", strerror(errno));
505 caglobals.ip.selectTimeout = SELECT_TIMEOUT; //poll needed for shutdown
509 CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
511 CAResult_t res = CA_STATUS_OK;
513 if (caglobals.ip.started)
518 if (!IPv4MulticastAddress.s_addr)
520 (void)inet_pton(AF_INET, IPv4_MULTICAST, &IPv4MulticastAddress);
521 (void)inet_pton(AF_INET6, IPv6_MULTICAST_INT, &IPv6MulticastAddressInt);
522 (void)inet_pton(AF_INET6, IPv6_MULTICAST_LNK, &IPv6MulticastAddressLnk);
523 (void)inet_pton(AF_INET6, IPv6_MULTICAST_RLM, &IPv6MulticastAddressRlm);
524 (void)inet_pton(AF_INET6, IPv6_MULTICAST_ADM, &IPv6MulticastAddressAdm);
525 (void)inet_pton(AF_INET6, IPv6_MULTICAST_SIT, &IPv6MulticastAddressSit);
526 (void)inet_pton(AF_INET6, IPv6_MULTICAST_ORG, &IPv6MulticastAddressOrg);
527 (void)inet_pton(AF_INET6, IPv6_MULTICAST_GLB, &IPv6MulticastAddressGlb);
530 if (!caglobals.ip.ipv6enabled && !caglobals.ip.ipv4enabled)
532 caglobals.ip.ipv4enabled = true; // only needed to run CA tests
535 if (caglobals.ip.ipv6enabled)
537 NEWSOCKET(AF_INET6, u6)
538 NEWSOCKET(AF_INET6, u6s)
539 NEWSOCKET(AF_INET6, m6)
540 NEWSOCKET(AF_INET6, m6s)
541 OIC_LOG_V(INFO, TAG, "IPv6 unicast port: %u", caglobals.ip.u6.port);
543 if (caglobals.ip.ipv4enabled)
545 NEWSOCKET(AF_INET, u4)
546 NEWSOCKET(AF_INET, u4s)
547 NEWSOCKET(AF_INET, m4)
548 NEWSOCKET(AF_INET, m4s)
549 OIC_LOG_V(INFO, TAG, "IPv4 unicast port: %u", caglobals.ip.u4.port);
552 OIC_LOG_V(DEBUG, TAG,
553 "socket summary: u6=%d, u6s=%d, u4=%d, u4s=%d, m6=%d, m6s=%d, m4=%d, m4s=%d",
554 caglobals.ip.u6.fd, caglobals.ip.u6s.fd, caglobals.ip.u4.fd, caglobals.ip.u4s.fd,
555 caglobals.ip.m6.fd, caglobals.ip.m6s.fd, caglobals.ip.m4.fd, caglobals.ip.m4s.fd);
557 OIC_LOG_V(DEBUG, TAG,
558 "port summary: u6 port=%d, u6s port=%d, u4 port=%d, u4s port=%d, m6 port=%d,"
559 "m6s port=%d, m4 port=%d, m4s port=%d",
560 caglobals.ip.u6.port, caglobals.ip.u6s.port, caglobals.ip.u4.port,
561 caglobals.ip.u4s.port, caglobals.ip.m6.port, caglobals.ip.m6s.port,
562 caglobals.ip.m4.port, caglobals.ip.m4s.port);
563 // create pipe for fast shutdown
565 CHECKFD(caglobals.ip.shutdownFds[0]);
566 CHECKFD(caglobals.ip.shutdownFds[1]);
568 // create source of network interface change notifications
569 CAInitializeNetlink();
571 caglobals.ip.selectTimeout = CAGetPollingInterval(caglobals.ip.selectTimeout);
573 res = CAIPStartListenServer();
574 if (CA_STATUS_OK != res)
576 OIC_LOG_V(ERROR, TAG, "Failed to start listening server![%d]", res);
580 caglobals.ip.terminate = false;
581 res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL);
582 if (CA_STATUS_OK != res)
584 OIC_LOG(ERROR, TAG, "thread_pool_add_task failed");
587 OIC_LOG(DEBUG, TAG, "CAReceiveHandler thread started successfully.");
589 caglobals.ip.started = true;
593 void CAIPStopServer()
595 caglobals.ip.started = false;
596 caglobals.ip.terminate = true;
598 if (caglobals.ip.shutdownFds[1] != -1)
600 close(caglobals.ip.shutdownFds[1]);
601 // receive thread will stop immediately
605 // receive thread will stop in SELECT_TIMEOUT seconds.
609 void CAWakeUpForChange()
611 if (caglobals.ip.shutdownFds[1] != -1)
616 len = write(caglobals.ip.shutdownFds[1], "w", 1);
617 } while ((len == -1) && (errno == EINTR));
618 if ((len == -1) && (errno != EINTR) && (errno != EPIPE))
620 OIC_LOG_V(DEBUG, TAG, "write failed: %s", strerror(errno));
625 static void applyMulticastToInterface4(struct in_addr inaddr)
627 if (!caglobals.ip.ipv4enabled)
632 struct ip_mreqn mreq = { .imr_multiaddr = IPv4MulticastAddress,
633 .imr_address = inaddr,
635 if (setsockopt(caglobals.ip.m4.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
637 if (EADDRINUSE != errno)
639 OIC_LOG_V(ERROR, TAG, "IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
642 if (setsockopt(caglobals.ip.m4s.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
644 if (EADDRINUSE != errno)
646 OIC_LOG_V(ERROR, TAG, "secure IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
651 static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t interface)
653 struct ipv6_mreq mreq = {.ipv6mr_multiaddr = *addr, .ipv6mr_interface = interface};
655 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof (mreq)))
657 if (EADDRINUSE != errno)
659 OIC_LOG_V(ERROR, TAG, "IPv6 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
664 static void applyMulticastToInterface6(uint32_t interface)
666 if (!caglobals.ip.ipv6enabled)
670 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressInt, interface);
671 applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressLnk, interface);
672 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressRlm, interface);
673 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressAdm, interface);
674 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressSit, interface);
675 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressOrg, interface);
676 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressGlb, interface);
678 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressInt, interface);
679 applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressLnk, interface);
680 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressRlm, interface);
681 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressAdm, interface);
682 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressSit, interface);
683 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressOrg, interface);
684 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressGlb, interface);
687 CAResult_t CAIPStartListenServer()
689 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
692 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
693 return CA_STATUS_FAILED;
696 uint32_t len = u_arraylist_length(iflist);
697 OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
699 for (uint32_t i = 0; i < len; i++)
701 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
707 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
711 if (ifitem->family == AF_INET)
713 struct in_addr inaddr;
714 inaddr.s_addr = ifitem->ipv4addr;
715 applyMulticastToInterface4(inaddr);
716 OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s", ifitem->name);
718 if (ifitem->family == AF_INET6)
720 applyMulticastToInterface6(ifitem->index);
721 OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
725 u_arraylist_destroy(iflist);
729 CAResult_t CAIPStopListenServer()
731 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
734 OIC_LOG_V(ERROR, TAG, "Get interface info failed: %s", strerror(errno));
735 return CA_STATUS_FAILED;
738 uint32_t len = u_arraylist_length(iflist);
739 OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
741 for (uint32_t i = 0; i < len; i++)
743 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
750 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
754 if (ifitem->family == AF_INET)
756 close(caglobals.ip.m4.fd);
757 close(caglobals.ip.m4s.fd);
758 caglobals.ip.m4.fd = -1;
759 caglobals.ip.m4s.fd = -1;
760 OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s cloed", ifitem->name);
762 if (ifitem->family == AF_INET6)
764 close(caglobals.ip.m6.fd);
765 close(caglobals.ip.m6s.fd);
766 caglobals.ip.m6.fd = -1;
767 caglobals.ip.m6s.fd = -1;
768 OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
771 u_arraylist_destroy(iflist);
775 static void CAProcessNewInterface(CAInterface_t *ifitem)
779 OIC_LOG(DEBUG, TAG, "ifitem is null");
783 applyMulticastToInterface6(ifitem->index);
784 struct in_addr inaddr = { .s_addr = ifitem->ipv4addr };
785 applyMulticastToInterface4(inaddr);
787 static void CAHandleNetlink()
792 struct sockaddr_nl sa;
793 struct iovec iov = { buf, sizeof (buf) };
794 struct msghdr msg = { (void *)&sa, sizeof (sa), &iov, 1, NULL, 0, 0 };
796 size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
798 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
800 if (nh != NULL && nh->nlmsg_type != RTM_NEWLINK)
805 struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
806 if (!ifi || (ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
811 int newIndex = ifi->ifi_index;
813 u_arraylist_t *iflist = CAIPGetInterfaceInformation(newIndex);
816 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
820 uint32_t listLength = u_arraylist_length(iflist);
821 for (uint32_t i = 0; i < listLength; i++)
823 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
829 if ((int)ifitem->index != newIndex)
834 CAProcessNewInterface(ifitem);
835 break; // we found the one we were looking for
837 u_arraylist_destroy(iflist);
842 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
844 g_packetReceivedCallback = callback;
847 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
849 g_exceptionCallback = callback;
852 static void sendData(int fd, const CAEndpoint_t *endpoint,
853 const void *data, uint32_t dlen,
854 const char *cast, const char *fam)
856 OIC_LOG(DEBUG, TAG, "IN");
860 OIC_LOG(DEBUG, TAG, "endpoint is null");
864 char *secure = (endpoint->flags & CA_SECURE) ? "secure " : "";
865 (void)secure; // eliminates release warning
866 struct sockaddr_storage sock;
867 CAConvertNameToAddr(endpoint->addr, endpoint->port, &sock);
870 if (sock.ss_family == AF_INET6)
872 struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)&sock;
873 if (!sock6->sin6_scope_id)
875 sock6->sin6_scope_id = endpoint->interface;
877 socklen = sizeof(struct sockaddr_in6);
881 socklen = sizeof(struct sockaddr_in);
884 ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, socklen);
887 // If logging is not defined/enabled.
890 OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %s", secure, cast, fam, strerror(errno));
894 OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %zd bytes", secure, cast, fam, len);
898 static void sendMulticastData6(const u_arraylist_t *iflist,
899 CAEndpoint_t *endpoint,
900 const void *data, uint32_t datalen)
904 OIC_LOG(DEBUG, TAG, "endpoint is null");
908 int scope = endpoint->flags & CA_SCOPE_MASK;
909 char *ipv6mcname = ipv6mcnames[scope];
912 OIC_LOG_V(INFO, TAG, "IPv6 multicast scope invalid: %d", scope);
915 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), ipv6mcname);
916 int fd = caglobals.ip.u6.fd;
918 uint32_t len = u_arraylist_length(iflist);
919 for (uint32_t i = 0; i < len; i++)
921 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
926 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
930 if (ifitem->family != AF_INET6)
935 int index = ifitem->index;
936 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index, sizeof (index)))
938 OIC_LOG_V(ERROR, TAG, "setsockopt6 failed: %s", strerror(errno));
941 sendData(fd, endpoint, data, datalen, "multicast", "ipv6");
945 static void sendMulticastData4(const u_arraylist_t *iflist,
946 CAEndpoint_t *endpoint,
947 const void *data, uint32_t datalen)
949 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
951 struct ip_mreqn mreq = { .imr_multiaddr = IPv4MulticastAddress,
953 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
954 int fd = caglobals.ip.u4.fd;
956 uint32_t len = u_arraylist_length(iflist);
957 for (uint32_t i = 0; i < len; i++)
959 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
964 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
968 if (ifitem->family != AF_INET)
973 struct in_addr inaddr;
974 inaddr.s_addr = ifitem->ipv4addr;
975 mreq.imr_address = inaddr;
976 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof (mreq)))
978 OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)",
981 sendData(fd, endpoint, data, datalen, "multicast", "ipv4");
985 void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
988 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
989 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
991 bool isSecure = (endpoint->flags & CA_SECURE) != 0;
995 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
997 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
1000 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
1004 if ((endpoint->flags & CA_IPV6) && caglobals.ip.ipv6enabled)
1006 sendMulticastData6(iflist, endpoint, data, datalen);
1008 if ((endpoint->flags & CA_IPV4) && caglobals.ip.ipv4enabled)
1010 sendMulticastData4(iflist, endpoint, data, datalen);
1013 u_arraylist_destroy(iflist);
1017 if (!endpoint->port) // unicast discovery
1019 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
1023 if (caglobals.ip.ipv6enabled && (endpoint->flags & CA_IPV6))
1025 fd = isSecure ? caglobals.ip.u6s.fd : caglobals.ip.u6.fd;
1026 #ifndef __WITH_DTLS__
1027 fd = caglobals.ip.u6.fd;
1029 sendData(fd, endpoint, data, datalen, "unicast", "ipv6");
1031 if (caglobals.ip.ipv4enabled && (endpoint->flags & CA_IPV4))
1033 fd = isSecure ? caglobals.ip.u4s.fd : caglobals.ip.u4.fd;
1034 #ifndef __WITH_DTLS__
1035 fd = caglobals.ip.u4.fd;
1037 sendData(fd, endpoint, data, datalen, "unicast", "ipv4");
1042 CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1044 VERIFY_NON_NULL(info, TAG, "info is NULL");
1045 VERIFY_NON_NULL(size, TAG, "size is NULL");
1047 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
1050 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
1051 return CA_STATUS_FAILED;
1054 uint32_t len = u_arraylist_length(iflist);
1055 uint32_t length = len;
1057 #ifdef __WITH_DTLS__
1058 //If DTLS is supported, each interface can support secure port as well
1062 CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(length, sizeof (CAEndpoint_t));
1065 OIC_LOG(ERROR, TAG, "Malloc Failed");
1066 u_arraylist_destroy(iflist);
1067 return CA_MEMORY_ALLOC_FAILED;
1070 for (uint32_t i = 0, j = 0; i < len; i++)
1072 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
1078 eps[j].adapter = CA_ADAPTER_IP;
1079 eps[j].interface = 0;
1081 if (ifitem->family == AF_INET6)
1083 eps[j].flags = CA_IPV6;
1084 eps[j].port = caglobals.ip.u6.port;
1088 eps[j].flags = CA_IPV4;
1089 eps[j].port = caglobals.ip.u4.port;
1091 inet_ntop(AF_INET, &(ifitem->ipv4addr), eps[j].addr, MAX_ADDR_STR_SIZE_CA);
1094 #ifdef __WITH_DTLS__
1097 eps[j].adapter = CA_ADAPTER_IP;
1098 eps[j].interface = 0;
1100 if (ifitem->family == AF_INET6)
1102 eps[j].flags = CA_IPV6 | CA_SECURE;
1103 eps[j].port = caglobals.ip.u6s.port;
1107 eps[j].flags = CA_IPV4 | CA_SECURE;
1108 eps[j].port = caglobals.ip.u4s.port;
1109 inet_ntop(AF_INET, &(ifitem->ipv4addr), eps[j].addr, MAX_ADDR_STR_SIZE_CA);
1118 u_arraylist_destroy(iflist);
1120 return CA_STATUS_OK;