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 void CADeInitializeIPGlobals()
124 if (caglobals.ip.u6.fd != -1)
126 close(caglobals.ip.u6.fd);
127 caglobals.ip.u6.fd = -1;
130 if (caglobals.ip.u6s.fd != -1)
132 close(caglobals.ip.u6s.fd);
133 caglobals.ip.u6s.fd = -1;
136 if (caglobals.ip.u4.fd != -1)
138 close(caglobals.ip.u4.fd);
139 caglobals.ip.u4.fd = -1;
142 if (caglobals.ip.u4s.fd != -1)
144 close(caglobals.ip.u4s.fd);
145 caglobals.ip.u4s.fd = -1;
148 if (caglobals.ip.m6.fd != -1)
150 close(caglobals.ip.m6.fd);
151 caglobals.ip.m6.fd = -1;
154 if (caglobals.ip.m6s.fd != -1)
156 close(caglobals.ip.m6s.fd);
157 caglobals.ip.m6s.fd = -1;
160 if (caglobals.ip.m4.fd != -1)
162 close(caglobals.ip.m4.fd);
163 caglobals.ip.m4.fd = -1;
166 if (caglobals.ip.m4s.fd != -1)
168 close(caglobals.ip.m4s.fd);
169 caglobals.ip.m4s.fd = -1;
173 static void CAReceiveHandler(void *data)
177 while (!caglobals.ip.terminate)
179 CAFindReadyMessage();
183 static void CAFindReadyMessage()
186 struct timeval timeout;
188 timeout.tv_sec = caglobals.ip.selectTimeout;
190 struct timeval *tv = caglobals.ip.selectTimeout == -1 ? NULL : &timeout;
201 if (caglobals.ip.shutdownFds[0] != -1)
203 FD_SET(caglobals.ip.shutdownFds[0], &readFds);
205 if (caglobals.ip.netlinkFd != -1)
207 FD_SET(caglobals.ip.netlinkFd, &readFds);
210 int ret = select(caglobals.ip.maxfd + 1, &readFds, NULL, NULL, tv);
212 if (caglobals.ip.terminate)
214 OIC_LOG_V(DEBUG, TAG, "Packet receiver Stop request received.");
221 OIC_LOG_V(FATAL, TAG, "select error %s", strerror(errno));
226 CASelectReturned(&readFds, ret);
229 static void CASelectReturned(fd_set *readFds, int ret)
233 CATransportFlags_t flags = CA_DEFAULT_FLAGS;
235 while (!caglobals.ip.terminate)
237 ISSET(u6, readFds, CA_IPV6)
238 else ISSET(u6s, readFds, CA_IPV6 | CA_SECURE)
239 else ISSET(u4, readFds, CA_IPV4)
240 else ISSET(u4s, readFds, CA_IPV4 | CA_SECURE)
241 else ISSET(m6, readFds, CA_MULTICAST | CA_IPV6)
242 else ISSET(m6s, readFds, CA_MULTICAST | CA_IPV6 | CA_SECURE)
243 else ISSET(m4, readFds, CA_MULTICAST | CA_IPV4)
244 else ISSET(m4s, readFds, CA_MULTICAST | CA_IPV4 | CA_SECURE)
245 else if (FD_ISSET(caglobals.ip.netlinkFd, readFds))
250 else if (FD_ISSET(caglobals.ip.shutdownFds[0], readFds))
253 ssize_t len = read(caglobals.ip.shutdownFds[0], buf, sizeof (buf));
259 CAInterface_t *ifchanged = CAFindInterfaceChange();
262 CAProcessNewInterface(ifchanged);
272 (void)CAReceiveMessage(fd, flags);
277 static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags)
279 char recvBuffer[COAP_MAX_PDU_SIZE];
282 int level, type, namelen;
283 struct sockaddr_storage srcAddr;
284 unsigned char *pktinfo = NULL;
285 struct cmsghdr *cmp = NULL;
286 struct iovec iov = { recvBuffer, sizeof (recvBuffer) };
290 unsigned char data[CMSG_SPACE(sizeof (struct in6_pktinfo))];
295 namelen = sizeof (struct sockaddr_in6);
296 level = IPPROTO_IPV6;
298 len = sizeof (struct in6_pktinfo);
302 namelen = sizeof (struct sockaddr_in);
305 len = sizeof (struct in6_pktinfo);
308 struct msghdr msg = { .msg_name = &srcAddr,
309 .msg_namelen = namelen,
312 .msg_control = &cmsg,
313 .msg_controllen = CMSG_SPACE(len) };
315 ssize_t recvLen = recvmsg(fd, &msg, flags);
318 OIC_LOG_V(ERROR, TAG, "Recvfrom failed %s", strerror(errno));
319 return CA_STATUS_FAILED;
322 if (flags & CA_MULTICAST)
324 for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL; cmp = CMSG_NXTHDR(&msg, cmp))
326 if (cmp->cmsg_level == level && cmp->cmsg_type == type)
328 pktinfo = CMSG_DATA(cmp);
333 CASecureEndpoint_t sep = {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = flags}};
337 sep.endpoint.interface = ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id;
338 ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id = 0;
340 if ((flags & CA_MULTICAST) && pktinfo)
342 struct in6_addr *addr = &(((struct in6_pktinfo *)pktinfo)->ipi6_addr);
343 unsigned char topbits = ((unsigned char *)addr)[0];
346 sep.endpoint.flags &= ~CA_MULTICAST;
352 if ((flags & CA_MULTICAST) && pktinfo)
354 struct in_addr *addr = &((struct in_pktinfo *)pktinfo)->ipi_addr;
355 uint32_t host = ntohl(addr->s_addr);
356 unsigned char topbits = ((unsigned char *)&host)[3];
357 if (topbits < 224 || topbits > 239)
359 sep.endpoint.flags &= ~CA_MULTICAST;
364 CAConvertAddrToName(&srcAddr, msg.msg_namelen, sep.endpoint.addr, &sep.endpoint.port);
366 if (flags & CA_SECURE)
369 int ret = CAAdapterNetDtlsDecrypt(&sep, (uint8_t *)recvBuffer, recvLen);
370 OIC_LOG_V(DEBUG, TAG, "CAAdapterNetDtlsDecrypt returns [%d]", ret);
372 OIC_LOG(ERROR, TAG, "Encrypted message but no DTLS");
377 if (g_packetReceivedCallback)
379 g_packetReceivedCallback(&sep, recvBuffer, recvLen);
388 OIC_LOG(DEBUG, TAG, "IN");
389 OIC_LOG(DEBUG, TAG, "OUT");
392 static int CACreateSocket(int family, uint16_t *port)
394 int socktype = SOCK_DGRAM;
396 socktype |= SOCK_CLOEXEC;
398 int fd = socket(family, socktype, IPPROTO_UDP);
401 OIC_LOG_V(ERROR, TAG, "create socket failed: %s", strerror(errno));
406 int fl = fcntl(fd, F_GETFD);
407 if (-1 == fl || -1 == fcntl(fd, F_SETFD, fl|FD_CLOEXEC))
409 OIC_LOG_V(ERROR, TAG, "set FD_CLOEXEC failed: %s", strerror(errno));
415 struct sockaddr_storage sa = { .ss_family = family };
418 if (family == AF_INET6)
422 if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
424 OIC_LOG_V(ERROR, TAG, "IPV6_V6ONLY failed: %s", strerror(errno));
427 if (*port) // only do this for multicast ports
429 if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof (on)))
431 OIC_LOG_V(ERROR, TAG, "IPV6_RECVPKTINFO failed: %s", strerror(errno));
435 ((struct sockaddr_in6 *)&sa)->sin6_port = htons(*port);
436 socklen = sizeof (struct sockaddr_in6);
440 if (*port) // only do this for multicast ports
443 if (-1 == setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &on, sizeof (on)))
445 OIC_LOG_V(ERROR, TAG, "IP_PKTINFO failed: %s", strerror(errno));
449 ((struct sockaddr_in *)&sa)->sin_port = htons(*port);
450 socklen = sizeof (struct sockaddr_in);
453 if (*port) // use the given port
456 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)))
458 OIC_LOG_V(ERROR, TAG, "SO_REUSEADDR failed: %s", strerror(errno));
464 if (-1 == bind(fd, (struct sockaddr *)&sa, socklen))
466 OIC_LOG_V(ERROR, TAG, "bind socket failed: %s", strerror(errno));
471 if (!*port) // return the assigned port
473 if (-1 == getsockname(fd, (struct sockaddr *)&sa, &socklen))
475 OIC_LOG_V(ERROR, TAG, "getsockname failed: %s", strerror(errno));
479 *port = ntohs(family == AF_INET6 ?
480 ((struct sockaddr_in6 *)&sa)->sin6_port :
481 ((struct sockaddr_in *)&sa)->sin_port);
487 #define CHECKFD(FD) \
488 if (FD > caglobals.ip.maxfd) \
489 caglobals.ip.maxfd = FD;
490 #define NEWSOCKET(FAMILY, NAME) \
491 caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port); \
492 CHECKFD(caglobals.ip.NAME.fd)
494 static void CAInitializeNetlink()
497 // create NETLINK fd for interface change notifications
498 struct sockaddr_nl sa = { AF_NETLINK, 0, 0, RTMGRP_LINK };
500 caglobals.ip.netlinkFd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE);
501 if (caglobals.ip.netlinkFd == -1)
503 OIC_LOG_V(ERROR, TAG, "netlink socket failed: %s", strerror(errno));
507 int r = bind(caglobals.ip.netlinkFd, (struct sockaddr *)&sa, sizeof (sa));
510 OIC_LOG_V(ERROR, TAG, "netlink bind failed: %s", strerror(errno));
511 close(caglobals.ip.netlinkFd);
512 caglobals.ip.netlinkFd = -1;
516 CHECKFD(caglobals.ip.netlinkFd);
522 static void CAInitializePipe()
524 caglobals.ip.selectTimeout = -1;
526 int ret = pipe2(caglobals.ip.shutdownFds, O_CLOEXEC);
528 int ret = pipe(caglobals.ip.shutdownFds);
531 ret = fcntl(caglobals.ip.shutdownFds[0], F_GETFD);
534 ret = fcntl(caglobals.ip.shutdownFds[0], F_SETFD, ret|FD_CLOEXEC);
538 ret = fcntl(caglobals.ip.shutdownFds[1], F_GETFD);
542 ret = fcntl(caglobals.ip.shutdownFds[1], F_SETFD, ret|FD_CLOEXEC);
546 close(caglobals.ip.shutdownFds[1]);
547 close(caglobals.ip.shutdownFds[0]);
548 caglobals.ip.shutdownFds[0] = -1;
549 caglobals.ip.shutdownFds[1] = -1;
555 OIC_LOG_V(ERROR, TAG, "pipe failed: %s", strerror(errno));
556 caglobals.ip.selectTimeout = SELECT_TIMEOUT; //poll needed for shutdown
560 CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
562 CAResult_t res = CA_STATUS_OK;
564 if (caglobals.ip.started)
569 if (!IPv4MulticastAddress.s_addr)
571 (void)inet_pton(AF_INET, IPv4_MULTICAST, &IPv4MulticastAddress);
572 (void)inet_pton(AF_INET6, IPv6_MULTICAST_INT, &IPv6MulticastAddressInt);
573 (void)inet_pton(AF_INET6, IPv6_MULTICAST_LNK, &IPv6MulticastAddressLnk);
574 (void)inet_pton(AF_INET6, IPv6_MULTICAST_RLM, &IPv6MulticastAddressRlm);
575 (void)inet_pton(AF_INET6, IPv6_MULTICAST_ADM, &IPv6MulticastAddressAdm);
576 (void)inet_pton(AF_INET6, IPv6_MULTICAST_SIT, &IPv6MulticastAddressSit);
577 (void)inet_pton(AF_INET6, IPv6_MULTICAST_ORG, &IPv6MulticastAddressOrg);
578 (void)inet_pton(AF_INET6, IPv6_MULTICAST_GLB, &IPv6MulticastAddressGlb);
581 if (!caglobals.ip.ipv6enabled && !caglobals.ip.ipv4enabled)
583 caglobals.ip.ipv4enabled = true; // only needed to run CA tests
586 if (caglobals.ip.ipv6enabled)
588 NEWSOCKET(AF_INET6, u6)
589 NEWSOCKET(AF_INET6, u6s)
590 NEWSOCKET(AF_INET6, m6)
591 NEWSOCKET(AF_INET6, m6s)
592 OIC_LOG_V(INFO, TAG, "IPv6 unicast port: %u", caglobals.ip.u6.port);
594 if (caglobals.ip.ipv4enabled)
596 NEWSOCKET(AF_INET, u4)
597 NEWSOCKET(AF_INET, u4s)
598 NEWSOCKET(AF_INET, m4)
599 NEWSOCKET(AF_INET, m4s)
600 OIC_LOG_V(INFO, TAG, "IPv4 unicast port: %u", caglobals.ip.u4.port);
603 OIC_LOG_V(DEBUG, TAG,
604 "socket summary: u6=%d, u6s=%d, u4=%d, u4s=%d, m6=%d, m6s=%d, m4=%d, m4s=%d",
605 caglobals.ip.u6.fd, caglobals.ip.u6s.fd, caglobals.ip.u4.fd, caglobals.ip.u4s.fd,
606 caglobals.ip.m6.fd, caglobals.ip.m6s.fd, caglobals.ip.m4.fd, caglobals.ip.m4s.fd);
608 OIC_LOG_V(DEBUG, TAG,
609 "port summary: u6 port=%d, u6s port=%d, u4 port=%d, u4s port=%d, m6 port=%d,"
610 "m6s port=%d, m4 port=%d, m4s port=%d",
611 caglobals.ip.u6.port, caglobals.ip.u6s.port, caglobals.ip.u4.port,
612 caglobals.ip.u4s.port, caglobals.ip.m6.port, caglobals.ip.m6s.port,
613 caglobals.ip.m4.port, caglobals.ip.m4s.port);
614 // create pipe for fast shutdown
616 CHECKFD(caglobals.ip.shutdownFds[0]);
617 CHECKFD(caglobals.ip.shutdownFds[1]);
619 // create source of network interface change notifications
620 CAInitializeNetlink();
622 caglobals.ip.selectTimeout = CAGetPollingInterval(caglobals.ip.selectTimeout);
624 res = CAIPStartListenServer();
625 if (CA_STATUS_OK != res)
627 OIC_LOG_V(ERROR, TAG, "Failed to start listening server![%d]", res);
631 caglobals.ip.terminate = false;
632 res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL);
633 if (CA_STATUS_OK != res)
635 OIC_LOG(ERROR, TAG, "thread_pool_add_task failed");
638 OIC_LOG(DEBUG, TAG, "CAReceiveHandler thread started successfully.");
640 caglobals.ip.started = true;
644 void CAIPStopServer()
646 caglobals.ip.started = false;
647 caglobals.ip.terminate = true;
649 if (caglobals.ip.shutdownFds[1] != -1)
651 close(caglobals.ip.shutdownFds[1]);
652 // receive thread will stop immediately
656 // receive thread will stop in SELECT_TIMEOUT seconds.
660 void CAWakeUpForChange()
662 if (caglobals.ip.shutdownFds[1] != -1)
667 len = write(caglobals.ip.shutdownFds[1], "w", 1);
668 } while ((len == -1) && (errno == EINTR));
669 if ((len == -1) && (errno != EINTR) && (errno != EPIPE))
671 OIC_LOG_V(DEBUG, TAG, "write failed: %s", strerror(errno));
676 static void applyMulticastToInterface4(struct in_addr inaddr)
678 if (!caglobals.ip.ipv4enabled)
683 struct ip_mreqn mreq = { .imr_multiaddr = IPv4MulticastAddress,
684 .imr_address = inaddr,
686 if (setsockopt(caglobals.ip.m4.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
688 if (EADDRINUSE != errno)
690 OIC_LOG_V(ERROR, TAG, "IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
693 if (setsockopt(caglobals.ip.m4s.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
695 if (EADDRINUSE != errno)
697 OIC_LOG_V(ERROR, TAG, "secure IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
702 static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t interface)
704 struct ipv6_mreq mreq = {.ipv6mr_multiaddr = *addr, .ipv6mr_interface = interface};
706 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof (mreq)))
708 if (EADDRINUSE != errno)
710 OIC_LOG_V(ERROR, TAG, "IPv6 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
715 static void applyMulticastToInterface6(uint32_t interface)
717 if (!caglobals.ip.ipv6enabled)
721 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressInt, interface);
722 applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressLnk, interface);
723 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressRlm, interface);
724 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressAdm, interface);
725 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressSit, interface);
726 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressOrg, interface);
727 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressGlb, interface);
729 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressInt, interface);
730 applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressLnk, interface);
731 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressRlm, interface);
732 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressAdm, interface);
733 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressSit, interface);
734 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressOrg, interface);
735 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressGlb, interface);
738 CAResult_t CAIPStartListenServer()
740 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
743 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
744 return CA_STATUS_FAILED;
747 uint32_t len = u_arraylist_length(iflist);
748 OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
750 for (uint32_t i = 0; i < len; i++)
752 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
758 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
762 if (ifitem->family == AF_INET)
764 struct in_addr inaddr;
765 inaddr.s_addr = ifitem->ipv4addr;
766 applyMulticastToInterface4(inaddr);
767 OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s", ifitem->name);
769 if (ifitem->family == AF_INET6)
771 applyMulticastToInterface6(ifitem->index);
772 OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
776 u_arraylist_destroy(iflist);
780 CAResult_t CAIPStopListenServer()
782 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
785 OIC_LOG_V(ERROR, TAG, "Get interface info failed: %s", strerror(errno));
786 return CA_STATUS_FAILED;
789 uint32_t len = u_arraylist_length(iflist);
790 OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
792 for (uint32_t i = 0; i < len; i++)
794 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
801 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
805 if (ifitem->family == AF_INET)
807 close(caglobals.ip.m4.fd);
808 close(caglobals.ip.m4s.fd);
809 caglobals.ip.m4.fd = -1;
810 caglobals.ip.m4s.fd = -1;
811 OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s cloed", ifitem->name);
813 if (ifitem->family == AF_INET6)
815 close(caglobals.ip.m6.fd);
816 close(caglobals.ip.m6s.fd);
817 caglobals.ip.m6.fd = -1;
818 caglobals.ip.m6s.fd = -1;
819 OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
822 u_arraylist_destroy(iflist);
826 static void CAProcessNewInterface(CAInterface_t *ifitem)
830 OIC_LOG(DEBUG, TAG, "ifitem is null");
834 applyMulticastToInterface6(ifitem->index);
835 struct in_addr inaddr = { .s_addr = ifitem->ipv4addr };
836 applyMulticastToInterface4(inaddr);
838 static void CAHandleNetlink()
843 struct sockaddr_nl sa;
844 struct iovec iov = { buf, sizeof (buf) };
845 struct msghdr msg = { (void *)&sa, sizeof (sa), &iov, 1, NULL, 0, 0 };
847 size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
849 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
851 if (nh != NULL && nh->nlmsg_type != RTM_NEWLINK)
856 struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
857 if (!ifi || (ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
862 int newIndex = ifi->ifi_index;
864 u_arraylist_t *iflist = CAIPGetInterfaceInformation(newIndex);
867 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
871 uint32_t listLength = u_arraylist_length(iflist);
872 for (uint32_t i = 0; i < listLength; i++)
874 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
880 if ((int)ifitem->index != newIndex)
885 CAProcessNewInterface(ifitem);
886 break; // we found the one we were looking for
888 u_arraylist_destroy(iflist);
893 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
895 g_packetReceivedCallback = callback;
898 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
900 g_exceptionCallback = callback;
903 static void sendData(int fd, const CAEndpoint_t *endpoint,
904 const void *data, uint32_t dlen,
905 const char *cast, const char *fam)
907 OIC_LOG(DEBUG, TAG, "IN");
911 OIC_LOG(DEBUG, TAG, "endpoint is null");
915 char *secure = (endpoint->flags & CA_SECURE) ? "secure " : "";
916 (void)secure; // eliminates release warning
917 struct sockaddr_storage sock;
918 CAConvertNameToAddr(endpoint->addr, endpoint->port, &sock);
921 if (sock.ss_family == AF_INET6)
923 struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)&sock;
924 if (!sock6->sin6_scope_id)
926 sock6->sin6_scope_id = endpoint->interface;
928 socklen = sizeof(struct sockaddr_in6);
932 socklen = sizeof(struct sockaddr_in);
935 ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, socklen);
938 // If logging is not defined/enabled.
941 OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %s", secure, cast, fam, strerror(errno));
945 OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %zd bytes", secure, cast, fam, len);
949 static void sendMulticastData6(const u_arraylist_t *iflist,
950 CAEndpoint_t *endpoint,
951 const void *data, uint32_t datalen)
955 OIC_LOG(DEBUG, TAG, "endpoint is null");
959 int scope = endpoint->flags & CA_SCOPE_MASK;
960 char *ipv6mcname = ipv6mcnames[scope];
963 OIC_LOG_V(INFO, TAG, "IPv6 multicast scope invalid: %d", scope);
966 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), ipv6mcname);
967 int fd = caglobals.ip.u6.fd;
969 uint32_t len = u_arraylist_length(iflist);
970 for (uint32_t i = 0; i < len; i++)
972 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
977 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
981 if (ifitem->family != AF_INET6)
986 int index = ifitem->index;
987 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index, sizeof (index)))
989 OIC_LOG_V(ERROR, TAG, "setsockopt6 failed: %s", strerror(errno));
992 sendData(fd, endpoint, data, datalen, "multicast", "ipv6");
996 static void sendMulticastData4(const u_arraylist_t *iflist,
997 CAEndpoint_t *endpoint,
998 const void *data, uint32_t datalen)
1000 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
1002 struct ip_mreqn mreq = { .imr_multiaddr = IPv4MulticastAddress,
1004 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
1005 int fd = caglobals.ip.u4.fd;
1007 uint32_t len = u_arraylist_length(iflist);
1008 for (uint32_t i = 0; i < len; i++)
1010 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
1015 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
1019 if (ifitem->family != AF_INET)
1024 struct in_addr inaddr;
1025 inaddr.s_addr = ifitem->ipv4addr;
1026 mreq.imr_address = inaddr;
1027 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof (mreq)))
1029 OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)",
1032 sendData(fd, endpoint, data, datalen, "multicast", "ipv4");
1036 void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
1039 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
1040 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
1042 bool isSecure = (endpoint->flags & CA_SECURE) != 0;
1046 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
1048 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
1051 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
1055 if ((endpoint->flags & CA_IPV6) && caglobals.ip.ipv6enabled)
1057 sendMulticastData6(iflist, endpoint, data, datalen);
1059 if ((endpoint->flags & CA_IPV4) && caglobals.ip.ipv4enabled)
1061 sendMulticastData4(iflist, endpoint, data, datalen);
1064 u_arraylist_destroy(iflist);
1068 if (!endpoint->port) // unicast discovery
1070 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
1074 if (caglobals.ip.ipv6enabled && (endpoint->flags & CA_IPV6))
1076 fd = isSecure ? caglobals.ip.u6s.fd : caglobals.ip.u6.fd;
1077 #ifndef __WITH_DTLS__
1078 fd = caglobals.ip.u6.fd;
1080 sendData(fd, endpoint, data, datalen, "unicast", "ipv6");
1082 if (caglobals.ip.ipv4enabled && (endpoint->flags & CA_IPV4))
1084 fd = isSecure ? caglobals.ip.u4s.fd : caglobals.ip.u4.fd;
1085 #ifndef __WITH_DTLS__
1086 fd = caglobals.ip.u4.fd;
1088 sendData(fd, endpoint, data, datalen, "unicast", "ipv4");
1093 CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1095 VERIFY_NON_NULL(info, TAG, "info is NULL");
1096 VERIFY_NON_NULL(size, TAG, "size is NULL");
1098 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
1101 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
1102 return CA_STATUS_FAILED;
1105 uint32_t len = u_arraylist_length(iflist);
1106 uint32_t length = len;
1108 #ifdef __WITH_DTLS__
1109 //If DTLS is supported, each interface can support secure port as well
1113 CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(length, sizeof (CAEndpoint_t));
1116 OIC_LOG(ERROR, TAG, "Malloc Failed");
1117 u_arraylist_destroy(iflist);
1118 return CA_MEMORY_ALLOC_FAILED;
1121 for (uint32_t i = 0, j = 0; i < len; i++)
1123 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
1129 eps[j].adapter = CA_ADAPTER_IP;
1130 eps[j].interface = 0;
1132 if (ifitem->family == AF_INET6)
1134 eps[j].flags = CA_IPV6;
1135 eps[j].port = caglobals.ip.u6.port;
1139 eps[j].flags = CA_IPV4;
1140 eps[j].port = caglobals.ip.u4.port;
1142 inet_ntop(AF_INET, &(ifitem->ipv4addr), eps[j].addr, MAX_ADDR_STR_SIZE_CA);
1145 #ifdef __WITH_DTLS__
1148 eps[j].adapter = CA_ADAPTER_IP;
1149 eps[j].interface = 0;
1151 if (ifitem->family == AF_INET6)
1153 eps[j].flags = CA_IPV6 | CA_SECURE;
1154 eps[j].port = caglobals.ip.u6s.port;
1158 eps[j].flags = CA_IPV4 | CA_SECURE;
1159 eps[j].port = caglobals.ip.u4s.port;
1160 inet_ntop(AF_INET, &(ifitem->ipv4addr), eps[j].addr, MAX_ADDR_STR_SIZE_CA);
1169 u_arraylist_destroy(iflist);
1171 return CA_STATUS_OK;