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 #define __APPLE_USE_RFC_3542 // for PKTINFO
22 #define _GNU_SOURCE // for in6_pktinfo
24 #include <sys/types.h>
25 #include <sys/socket.h>
28 #include <sys/types.h>
30 #include <sys/select.h>
31 #include <arpa/inet.h>
32 #include <netinet/in.h>
36 #include <linux/netlink.h>
37 #include <linux/rtnetlink.h>
41 #include "caipinterface.h"
42 #include "caadapterutils.h"
44 #include "caadapternetdtls.h"
47 #include "oic_malloc.h"
48 #include "oic_string.h"
51 * Logging tag for module name
53 #define TAG "OIC_CA_IP_SERVER"
55 #define SELECT_TIMEOUT 1 // select() seconds (and termination latency)
57 #define IPv4_MULTICAST "224.0.1.187"
58 static struct in_addr IPv4MulticastAddress = { 0 };
60 #define IPv6_DOMAINS 16
61 #define IPv6_MULTICAST_INT "ff01::fd"
62 static struct in6_addr IPv6MulticastAddressInt;
63 #define IPv6_MULTICAST_LNK "ff02::fd"
64 static struct in6_addr IPv6MulticastAddressLnk;
65 #define IPv6_MULTICAST_RLM "ff03::fd"
66 static struct in6_addr IPv6MulticastAddressRlm;
67 #define IPv6_MULTICAST_ADM "ff04::fd"
68 static struct in6_addr IPv6MulticastAddressAdm;
69 #define IPv6_MULTICAST_SIT "ff05::fd"
70 static struct in6_addr IPv6MulticastAddressSit;
71 #define IPv6_MULTICAST_ORG "ff08::fd"
72 static struct in6_addr IPv6MulticastAddressOrg;
73 #define IPv6_MULTICAST_GLB "ff0e::fd"
74 static struct in6_addr IPv6MulticastAddressGlb;
76 static char *ipv6mcnames[IPv6_DOMAINS] = {
95 static CAIPExceptionCallback g_exceptionCallback;
97 static CAIPPacketReceivedCallback g_packetReceivedCallback;
99 static void CAHandleNetlink();
100 static void CAFindReadyMessage();
101 static void CASelectReturned(fd_set *readFds, int ret);
102 static void CAProcessNewInterface(CAInterface_t *ifchanged);
103 static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags);
105 #define SET(TYPE, FDS) \
106 if (caglobals.ip.TYPE.fd != -1) \
108 FD_SET(caglobals.ip.TYPE.fd, FDS); \
111 #define ISSET(TYPE, FDS, FLAGS) \
112 if (caglobals.ip.TYPE.fd != -1 && FD_ISSET(caglobals.ip.TYPE.fd, FDS)) \
114 fd = caglobals.ip.TYPE.fd; \
118 static void CAReceiveHandler(void *data)
122 while (!caglobals.ip.terminate)
124 CAFindReadyMessage();
128 static void CAFindReadyMessage()
131 struct timeval timeout;
133 timeout.tv_sec = caglobals.ip.selectTimeout;
135 struct timeval *tv = caglobals.ip.selectTimeout == -1 ? NULL : &timeout;
146 if (caglobals.ip.shutdownFds[0] != -1)
148 FD_SET(caglobals.ip.shutdownFds[0], &readFds);
150 if (caglobals.ip.netlinkFd != -1)
152 FD_SET(caglobals.ip.netlinkFd, &readFds);
155 int ret = select(caglobals.ip.maxfd + 1, &readFds, NULL, NULL, tv);
157 if (caglobals.ip.terminate)
159 OIC_LOG_V(DEBUG, TAG, "Packet receiver Stop request received.");
166 OIC_LOG_V(FATAL, TAG, "select error %s", strerror(errno));
171 CASelectReturned(&readFds, ret);
174 static void CASelectReturned(fd_set *readFds, int ret)
178 CATransportFlags_t flags = CA_DEFAULT_FLAGS;
180 while (!caglobals.ip.terminate)
182 ISSET(u6, readFds, CA_IPV6)
183 else ISSET(u6s, readFds, CA_IPV6 | CA_SECURE)
184 else ISSET(u4, readFds, CA_IPV4)
185 else ISSET(u4s, readFds, CA_IPV4 | CA_SECURE)
186 else ISSET(m6, readFds, CA_MULTICAST | CA_IPV6)
187 else ISSET(m6s, readFds, CA_MULTICAST | CA_IPV6 | CA_SECURE)
188 else ISSET(m4, readFds, CA_MULTICAST | CA_IPV4)
189 else ISSET(m4s, readFds, CA_MULTICAST | CA_IPV4 | CA_SECURE)
190 else if (FD_ISSET(caglobals.ip.netlinkFd, readFds))
195 else if (FD_ISSET(caglobals.ip.shutdownFds[0], readFds))
198 ssize_t len = read(caglobals.ip.shutdownFds[0], buf, sizeof (buf));
204 CAInterface_t *ifchanged = CAFindInterfaceChange();
207 CAProcessNewInterface(ifchanged);
217 (void)CAReceiveMessage(fd, flags);
222 static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags)
224 char recvBuffer[COAP_MAX_PDU_SIZE];
227 int level, type, namelen;
228 struct sockaddr_storage srcAddr;
229 unsigned char *pktinfo = NULL;
230 struct cmsghdr *cmp = NULL;
231 struct iovec iov = { recvBuffer, sizeof (recvBuffer) };
235 unsigned char data[CMSG_SPACE(sizeof (struct in6_pktinfo))];
240 namelen = sizeof (struct sockaddr_in6);
241 level = IPPROTO_IPV6;
243 len = sizeof (struct in6_pktinfo);
247 namelen = sizeof (struct sockaddr_in);
250 len = sizeof (struct in6_pktinfo);
253 struct msghdr msg = { .msg_name = &srcAddr,
254 .msg_namelen = namelen,
257 .msg_control = &cmsg,
258 .msg_controllen = CMSG_SPACE(len) };
260 ssize_t recvLen = recvmsg(fd, &msg, flags);
263 OIC_LOG_V(ERROR, TAG, "Recvfrom failed %s", strerror(errno));
264 return CA_STATUS_FAILED;
267 if (flags & CA_MULTICAST)
269 for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL; cmp = CMSG_NXTHDR(&msg, cmp))
271 if (cmp->cmsg_level == level && cmp->cmsg_type == type)
273 pktinfo = CMSG_DATA(cmp);
278 CASecureEndpoint_t sep = {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = flags}};
282 sep.endpoint.interface = ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id;
283 ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id = 0;
285 if ((flags & CA_MULTICAST) && pktinfo)
287 struct in6_addr *addr = &(((struct in6_pktinfo *)pktinfo)->ipi6_addr);
288 unsigned char topbits = ((unsigned char *)addr)[0];
291 sep.endpoint.flags &= ~CA_MULTICAST;
297 if ((flags & CA_MULTICAST) && pktinfo)
299 struct in_addr *addr = &((struct in_pktinfo *)pktinfo)->ipi_addr;
300 uint32_t host = ntohl(addr->s_addr);
301 unsigned char topbits = ((unsigned char *)&host)[3];
302 if (topbits < 224 || topbits > 239)
304 sep.endpoint.flags &= ~CA_MULTICAST;
309 CAConvertAddrToName(&srcAddr, msg.msg_namelen, sep.endpoint.addr, &sep.endpoint.port);
311 if (flags & CA_SECURE)
314 int ret = CAAdapterNetDtlsDecrypt(&sep, (uint8_t *)recvBuffer, recvLen);
315 OIC_LOG_V(DEBUG, TAG, "CAAdapterNetDtlsDecrypt returns [%d]", ret);
317 OIC_LOG(ERROR, TAG, "Encrypted message but no DTLS");
322 if (g_packetReceivedCallback)
324 g_packetReceivedCallback(&sep, recvBuffer, recvLen);
333 OIC_LOG(DEBUG, TAG, "IN");
334 OIC_LOG(DEBUG, TAG, "OUT");
337 static int CACreateSocket(int family, uint16_t *port)
339 int socktype = SOCK_DGRAM;
341 socktype |= SOCK_CLOEXEC;
343 int fd = socket(family, socktype, IPPROTO_UDP);
346 OIC_LOG_V(ERROR, TAG, "create socket failed: %s", strerror(errno));
351 int fl = fcntl(fd, F_GETFD);
352 if (-1 == fl || -1 == fcntl(fd, F_SETFD, fl|FD_CLOEXEC))
354 OIC_LOG_V(ERROR, TAG, "set FD_CLOEXEC failed: %s", strerror(errno));
360 struct sockaddr_storage sa = { .ss_family = family };
363 if (family == AF_INET6)
367 if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
369 OIC_LOG_V(ERROR, TAG, "IPV6_V6ONLY failed: %s", strerror(errno));
372 if (*port) // only do this for multicast ports
374 if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof (on)))
376 OIC_LOG_V(ERROR, TAG, "IPV6_RECVPKTINFO failed: %s", strerror(errno));
380 ((struct sockaddr_in6 *)&sa)->sin6_port = htons(*port);
381 socklen = sizeof (struct sockaddr_in6);
385 if (*port) // only do this for multicast ports
388 if (-1 == setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &on, sizeof (on)))
390 OIC_LOG_V(ERROR, TAG, "IP_PKTINFO failed: %s", strerror(errno));
394 ((struct sockaddr_in *)&sa)->sin_port = htons(*port);
395 socklen = sizeof (struct sockaddr_in);
398 if (*port) // use the given port
401 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)))
403 OIC_LOG_V(ERROR, TAG, "SO_REUSEADDR failed: %s", strerror(errno));
409 if (-1 == bind(fd, (struct sockaddr *)&sa, socklen))
411 OIC_LOG_V(ERROR, TAG, "bind socket failed: %s", strerror(errno));
416 if (!*port) // return the assigned port
418 if (-1 == getsockname(fd, (struct sockaddr *)&sa, &socklen))
420 OIC_LOG_V(ERROR, TAG, "getsockname failed: %s", strerror(errno));
424 *port = ntohs(family == AF_INET6 ?
425 ((struct sockaddr_in6 *)&sa)->sin6_port :
426 ((struct sockaddr_in *)&sa)->sin_port);
432 #define CHECKFD(FD) \
433 if (FD > caglobals.ip.maxfd) \
434 caglobals.ip.maxfd = FD;
435 #define NEWSOCKET(FAMILY, NAME) \
436 caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port); \
437 CHECKFD(caglobals.ip.NAME.fd)
439 static void CAInitializeNetlink()
442 // create NETLINK fd for interface change notifications
443 struct sockaddr_nl sa = { AF_NETLINK, 0, 0, RTMGRP_LINK };
445 caglobals.ip.netlinkFd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE);
446 if (caglobals.ip.netlinkFd == -1)
448 OIC_LOG_V(ERROR, TAG, "netlink socket failed: %s", strerror(errno));
452 int r = bind(caglobals.ip.netlinkFd, (struct sockaddr *)&sa, sizeof (sa));
455 OIC_LOG_V(ERROR, TAG, "netlink bind failed: %s", strerror(errno));
456 close(caglobals.ip.netlinkFd);
457 caglobals.ip.netlinkFd = -1;
461 CHECKFD(caglobals.ip.netlinkFd);
467 static void CAInitializePipe()
469 caglobals.ip.selectTimeout = -1;
471 int ret = pipe2(caglobals.ip.shutdownFds, O_CLOEXEC);
473 int ret = pipe(caglobals.ip.shutdownFds);
476 ret = fcntl(caglobals.ip.shutdownFds[0], F_GETFD);
479 ret = fcntl(caglobals.ip.shutdownFds[0], F_SETFD, ret|FD_CLOEXEC);
483 ret = fcntl(caglobals.ip.shutdownFds[1], F_GETFD);
487 ret = fcntl(caglobals.ip.shutdownFds[1], F_SETFD, ret|FD_CLOEXEC);
491 close(caglobals.ip.shutdownFds[1]);
492 close(caglobals.ip.shutdownFds[0]);
493 caglobals.ip.shutdownFds[0] = -1;
494 caglobals.ip.shutdownFds[1] = -1;
500 OIC_LOG_V(ERROR, TAG, "pipe failed: %s", strerror(errno));
501 caglobals.ip.selectTimeout = SELECT_TIMEOUT; //poll needed for shutdown
505 CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
507 CAResult_t res = CA_STATUS_OK;
509 if (caglobals.ip.started)
514 if (!IPv4MulticastAddress.s_addr)
516 (void)inet_pton(AF_INET, IPv4_MULTICAST, &IPv4MulticastAddress);
517 (void)inet_pton(AF_INET6, IPv6_MULTICAST_INT, &IPv6MulticastAddressInt);
518 (void)inet_pton(AF_INET6, IPv6_MULTICAST_LNK, &IPv6MulticastAddressLnk);
519 (void)inet_pton(AF_INET6, IPv6_MULTICAST_RLM, &IPv6MulticastAddressRlm);
520 (void)inet_pton(AF_INET6, IPv6_MULTICAST_ADM, &IPv6MulticastAddressAdm);
521 (void)inet_pton(AF_INET6, IPv6_MULTICAST_SIT, &IPv6MulticastAddressSit);
522 (void)inet_pton(AF_INET6, IPv6_MULTICAST_ORG, &IPv6MulticastAddressOrg);
523 (void)inet_pton(AF_INET6, IPv6_MULTICAST_GLB, &IPv6MulticastAddressGlb);
526 if (!caglobals.ip.ipv6enabled && !caglobals.ip.ipv4enabled)
528 caglobals.ip.ipv4enabled = true; // only needed to run CA tests
531 if (caglobals.ip.ipv6enabled)
533 NEWSOCKET(AF_INET6, u6)
534 NEWSOCKET(AF_INET6, u6s)
535 NEWSOCKET(AF_INET6, m6)
536 NEWSOCKET(AF_INET6, m6s)
537 OIC_LOG_V(INFO, TAG, "IPv6 unicast port: %u", caglobals.ip.u6.port);
539 if (caglobals.ip.ipv4enabled)
541 NEWSOCKET(AF_INET, u4)
542 NEWSOCKET(AF_INET, u4s)
543 NEWSOCKET(AF_INET, m4)
544 NEWSOCKET(AF_INET, m4s)
545 OIC_LOG_V(INFO, TAG, "IPv4 unicast port: %u", caglobals.ip.u4.port);
548 OIC_LOG_V(DEBUG, TAG,
549 "socket summary: u6=%d, u6s=%d, u4=%d, u4s=%d, m6=%d, m6s=%d, m4=%d, m4s=%d",
550 caglobals.ip.u6.fd, caglobals.ip.u6s.fd, caglobals.ip.u4.fd, caglobals.ip.u4s.fd,
551 caglobals.ip.m6.fd, caglobals.ip.m6s.fd, caglobals.ip.m4.fd, caglobals.ip.m4s.fd);
553 OIC_LOG_V(DEBUG, TAG,
554 "port summary: u6 port=%d, u6s port=%d, u4 port=%d, u4s port=%d, m6 port=%d,"
555 "m6s port=%d, m4 port=%d, m4s port=%d",
556 caglobals.ip.u6.port, caglobals.ip.u6s.port, caglobals.ip.u4.port,
557 caglobals.ip.u4s.port, caglobals.ip.m6.port, caglobals.ip.m6s.port,
558 caglobals.ip.m4.port, caglobals.ip.m4s.port);
559 // create pipe for fast shutdown
561 CHECKFD(caglobals.ip.shutdownFds[0]);
562 CHECKFD(caglobals.ip.shutdownFds[1]);
564 // create source of network interface change notifications
565 CAInitializeNetlink();
567 caglobals.ip.selectTimeout = CAGetPollingInterval(caglobals.ip.selectTimeout);
569 res = CAIPStartListenServer();
570 if (CA_STATUS_OK != res)
572 OIC_LOG_V(ERROR, TAG, "Failed to start listening server![%d]", res);
576 caglobals.ip.terminate = false;
577 res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL);
578 if (CA_STATUS_OK != res)
580 OIC_LOG(ERROR, TAG, "thread_pool_add_task failed");
583 OIC_LOG(DEBUG, TAG, "CAReceiveHandler thread started successfully.");
585 caglobals.ip.started = true;
589 void CAIPStopServer()
591 caglobals.ip.started = false;
592 caglobals.ip.terminate = true;
594 if (caglobals.ip.shutdownFds[1] != -1)
596 close(caglobals.ip.shutdownFds[1]);
597 // receive thread will stop immediately
601 // receive thread will stop in SELECT_TIMEOUT seconds.
605 void CAWakeUpForChange()
607 if (caglobals.ip.shutdownFds[1] != -1)
612 len = write(caglobals.ip.shutdownFds[1], "w", 1);
613 } while ((len == -1) && (errno == EINTR));
614 if ((len == -1) && (errno != EINTR) && (errno != EPIPE))
616 OIC_LOG_V(DEBUG, TAG, "write failed: %s", strerror(errno));
621 static void applyMulticastToInterface4(struct in_addr inaddr)
623 if (!caglobals.ip.ipv4enabled)
628 struct ip_mreqn mreq = { .imr_multiaddr = IPv4MulticastAddress,
629 .imr_address = inaddr,
631 if (setsockopt(caglobals.ip.m4.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
633 if (EADDRINUSE != errno)
635 OIC_LOG_V(ERROR, TAG, "IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
638 if (setsockopt(caglobals.ip.m4s.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
640 if (EADDRINUSE != errno)
642 OIC_LOG_V(ERROR, TAG, "secure IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
647 static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t interface)
649 struct ipv6_mreq mreq = {.ipv6mr_multiaddr = *addr, .ipv6mr_interface = interface};
651 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof (mreq)))
653 if (EADDRINUSE != errno)
655 OIC_LOG_V(ERROR, TAG, "IPv6 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
660 static void applyMulticastToInterface6(uint32_t interface)
662 if (!caglobals.ip.ipv6enabled)
666 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressInt, interface);
667 applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressLnk, interface);
668 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressRlm, interface);
669 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressAdm, interface);
670 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressSit, interface);
671 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressOrg, interface);
672 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressGlb, interface);
674 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressInt, interface);
675 applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressLnk, interface);
676 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressRlm, interface);
677 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressAdm, interface);
678 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressSit, interface);
679 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressOrg, interface);
680 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressGlb, interface);
683 CAResult_t CAIPStartListenServer()
685 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
688 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
689 return CA_STATUS_FAILED;
692 uint32_t len = u_arraylist_length(iflist);
693 OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
695 for (uint32_t i = 0; i < len; i++)
697 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
703 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
707 if (ifitem->family == AF_INET)
709 struct in_addr inaddr;
710 inaddr.s_addr = ifitem->ipv4addr;
711 applyMulticastToInterface4(inaddr);
712 OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s", ifitem->name);
714 if (ifitem->family == AF_INET6)
716 applyMulticastToInterface6(ifitem->index);
717 OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
721 u_arraylist_destroy(iflist);
725 CAResult_t CAIPStopListenServer()
727 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
730 OIC_LOG_V(ERROR, TAG, "Get interface info failed: %s", strerror(errno));
731 return CA_STATUS_FAILED;
734 uint32_t len = u_arraylist_length(iflist);
735 OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
737 for (uint32_t i = 0; i < len; i++)
739 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
746 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
750 if (ifitem->family == AF_INET)
752 close(caglobals.ip.m4.fd);
753 close(caglobals.ip.m4s.fd);
754 caglobals.ip.m4.fd = -1;
755 caglobals.ip.m4s.fd = -1;
756 OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s cloed", ifitem->name);
758 if (ifitem->family == AF_INET6)
760 close(caglobals.ip.m6.fd);
761 close(caglobals.ip.m6s.fd);
762 caglobals.ip.m6.fd = -1;
763 caglobals.ip.m6s.fd = -1;
764 OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
767 u_arraylist_destroy(iflist);
771 static void CAProcessNewInterface(CAInterface_t *ifitem)
775 OIC_LOG(DEBUG, TAG, "ifitem is null");
779 applyMulticastToInterface6(ifitem->index);
780 struct in_addr inaddr = { .s_addr = ifitem->ipv4addr };
781 applyMulticastToInterface4(inaddr);
783 static void CAHandleNetlink()
788 struct sockaddr_nl sa;
789 struct iovec iov = { buf, sizeof (buf) };
790 struct msghdr msg = { (void *)&sa, sizeof (sa), &iov, 1, NULL, 0, 0 };
792 size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
794 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
796 if (nh->nlmsg_type != RTM_NEWLINK)
801 struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
802 if (!ifi || (ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
807 int newIndex = ifi->ifi_index;
809 u_arraylist_t *iflist = CAIPGetInterfaceInformation(newIndex);
812 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
816 uint32_t listLength = u_arraylist_length(iflist);
817 for (uint32_t i = 0; i < listLength; i++)
819 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
825 if ((int)ifitem->index != newIndex)
830 CAProcessNewInterface(ifitem);
831 break; // we found the one we were looking for
833 u_arraylist_destroy(iflist);
838 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
840 g_packetReceivedCallback = callback;
843 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
845 g_exceptionCallback = callback;
848 static void sendData(int fd, const CAEndpoint_t *endpoint,
849 const void *data, uint32_t dlen,
850 const char *cast, const char *fam)
852 OIC_LOG(DEBUG, TAG, "IN");
856 OIC_LOG(DEBUG, TAG, "endpoint is null");
860 char *secure = (endpoint->flags & CA_SECURE) ? "secure " : "";
861 (void)secure; // eliminates release warning
862 struct sockaddr_storage sock;
863 CAConvertNameToAddr(endpoint->addr, endpoint->port, &sock);
866 if (sock.ss_family == AF_INET6)
868 struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)&sock;
869 if (!sock6->sin6_scope_id)
871 sock6->sin6_scope_id = endpoint->interface;
873 socklen = sizeof(struct sockaddr_in6);
877 socklen = sizeof(struct sockaddr_in);
880 ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, socklen);
883 // If logging is not defined/enabled.
886 OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %s", secure, cast, fam, strerror(errno));
890 OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %zd bytes", secure, cast, fam, len);
894 static void sendMulticastData6(const u_arraylist_t *iflist,
895 CAEndpoint_t *endpoint,
896 const void *data, uint32_t datalen)
900 OIC_LOG(DEBUG, TAG, "endpoint is null");
904 int scope = endpoint->flags & CA_SCOPE_MASK;
905 char *ipv6mcname = ipv6mcnames[scope];
908 OIC_LOG_V(INFO, TAG, "IPv6 multicast scope invalid: %d", scope);
911 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), ipv6mcname);
912 int fd = caglobals.ip.u6.fd;
914 uint32_t len = u_arraylist_length(iflist);
915 for (uint32_t i = 0; i < len; i++)
917 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
922 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
926 if (ifitem->family != AF_INET6)
931 int index = ifitem->index;
932 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index, sizeof (index)))
934 OIC_LOG_V(ERROR, TAG, "setsockopt6 failed: %s", strerror(errno));
937 sendData(fd, endpoint, data, datalen, "multicast", "ipv6");
941 static void sendMulticastData4(const u_arraylist_t *iflist,
942 CAEndpoint_t *endpoint,
943 const void *data, uint32_t datalen)
945 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
947 struct ip_mreqn mreq = { .imr_multiaddr = IPv4MulticastAddress,
949 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
950 int fd = caglobals.ip.u4.fd;
952 uint32_t len = u_arraylist_length(iflist);
953 for (uint32_t i = 0; i < len; i++)
955 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
960 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
964 if (ifitem->family != AF_INET)
969 struct in_addr inaddr;
970 inaddr.s_addr = ifitem->ipv4addr;
971 mreq.imr_address = inaddr;
972 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof (mreq)))
974 OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)",
977 sendData(fd, endpoint, data, datalen, "multicast", "ipv4");
981 void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
984 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
985 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
987 bool isSecure = (endpoint->flags & CA_SECURE) != 0;
991 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
993 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
996 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
1000 if ((endpoint->flags & CA_IPV6) && caglobals.ip.ipv6enabled)
1002 sendMulticastData6(iflist, endpoint, data, datalen);
1004 if ((endpoint->flags & CA_IPV4) && caglobals.ip.ipv4enabled)
1006 sendMulticastData4(iflist, endpoint, data, datalen);
1009 u_arraylist_destroy(iflist);
1013 if (!endpoint->port) // unicast discovery
1015 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
1019 if (caglobals.ip.ipv6enabled && (endpoint->flags & CA_IPV6))
1021 fd = isSecure ? caglobals.ip.u6s.fd : caglobals.ip.u6.fd;
1022 #ifndef __WITH_DTLS__
1023 fd = caglobals.ip.u6.fd;
1025 sendData(fd, endpoint, data, datalen, "unicast", "ipv6");
1027 if (caglobals.ip.ipv4enabled && (endpoint->flags & CA_IPV4))
1029 fd = isSecure ? caglobals.ip.u4s.fd : caglobals.ip.u4.fd;
1030 #ifndef __WITH_DTLS__
1031 fd = caglobals.ip.u4.fd;
1033 sendData(fd, endpoint, data, datalen, "unicast", "ipv4");
1038 CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1040 VERIFY_NON_NULL(info, TAG, "info is NULL");
1041 VERIFY_NON_NULL(size, TAG, "size is NULL");
1043 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
1046 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
1047 return CA_STATUS_FAILED;
1050 uint32_t len = u_arraylist_length(iflist);
1051 uint32_t length = len;
1053 #ifdef __WITH_DTLS__
1054 //If DTLS is supported, each interface can support secure port as well
1058 CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(length, sizeof (CAEndpoint_t));
1061 OIC_LOG(ERROR, TAG, "Malloc Failed");
1062 u_arraylist_destroy(iflist);
1063 return CA_MEMORY_ALLOC_FAILED;
1066 for (uint32_t i = 0, j = 0; i < len; i++)
1068 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
1074 eps[j].adapter = CA_ADAPTER_IP;
1075 eps[j].interface = 0;
1077 if (ifitem->family == AF_INET6)
1079 eps[j].flags = CA_IPV6;
1080 eps[j].port = caglobals.ip.u6.port;
1084 eps[j].flags = CA_IPV4;
1085 eps[j].port = caglobals.ip.u4.port;
1087 inet_ntop(AF_INET, &(ifitem->ipv4addr), eps[j].addr, MAX_ADDR_STR_SIZE_CA);
1090 #ifdef __WITH_DTLS__
1093 eps[j].adapter = CA_ADAPTER_IP;
1094 eps[j].interface = 0;
1096 if (ifitem->family == AF_INET6)
1098 eps[j].flags = CA_IPV6 | CA_SECURE;
1099 eps[j].port = caglobals.ip.u6s.port;
1103 eps[j].flags = CA_IPV4 | CA_SECURE;
1104 eps[j].port = caglobals.ip.u4s.port;
1105 inet_ntop(AF_INET, &(ifitem->ipv4addr), eps[j].addr, MAX_ADDR_STR_SIZE_CA);
1114 u_arraylist_destroy(iflist);
1116 return CA_STATUS_OK;