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 #define CLOSE_SOCKET(TYPE) \
123 if (caglobals.ip.TYPE.fd != -1) \
125 close(caglobals.ip.TYPE.fd); \
126 caglobals.ip.TYPE.fd = -1; \
129 void CADeInitializeIPGlobals()
140 if (caglobals.ip.netlinkFd != -1)
142 close(caglobals.ip.netlinkFd);
143 caglobals.ip.netlinkFd = -1;
147 static void CAReceiveHandler(void *data)
151 while (!caglobals.ip.terminate)
153 CAFindReadyMessage();
157 static void CAFindReadyMessage()
160 struct timeval timeout;
162 timeout.tv_sec = caglobals.ip.selectTimeout;
164 struct timeval *tv = caglobals.ip.selectTimeout == -1 ? NULL : &timeout;
175 if (caglobals.ip.shutdownFds[0] != -1)
177 FD_SET(caglobals.ip.shutdownFds[0], &readFds);
179 if (caglobals.ip.netlinkFd != -1)
181 FD_SET(caglobals.ip.netlinkFd, &readFds);
184 int ret = select(caglobals.ip.maxfd + 1, &readFds, NULL, NULL, tv);
186 if (caglobals.ip.terminate)
188 OIC_LOG_V(DEBUG, TAG, "Packet receiver Stop request received.");
195 OIC_LOG_V(FATAL, TAG, "select error %s", strerror(errno));
200 CASelectReturned(&readFds, ret);
203 static void CASelectReturned(fd_set *readFds, int ret)
207 CATransportFlags_t flags = CA_DEFAULT_FLAGS;
209 while (!caglobals.ip.terminate)
211 ISSET(u6, readFds, CA_IPV6)
212 else ISSET(u6s, readFds, CA_IPV6 | CA_SECURE)
213 else ISSET(u4, readFds, CA_IPV4)
214 else ISSET(u4s, readFds, CA_IPV4 | CA_SECURE)
215 else ISSET(m6, readFds, CA_MULTICAST | CA_IPV6)
216 else ISSET(m6s, readFds, CA_MULTICAST | CA_IPV6 | CA_SECURE)
217 else ISSET(m4, readFds, CA_MULTICAST | CA_IPV4)
218 else ISSET(m4s, readFds, CA_MULTICAST | CA_IPV4 | CA_SECURE)
219 else if (FD_ISSET(caglobals.ip.netlinkFd, readFds))
221 CAInterface_t *ifchanged = CAFindInterfaceChange();
224 CAProcessNewInterface(ifchanged);
229 else if (FD_ISSET(caglobals.ip.shutdownFds[0], readFds))
232 ssize_t len = read(caglobals.ip.shutdownFds[0], buf, sizeof (buf));
244 (void)CAReceiveMessage(fd, flags);
249 static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags)
251 char recvBuffer[COAP_MAX_PDU_SIZE];
254 int level, type, namelen;
255 struct sockaddr_storage srcAddr;
256 unsigned char *pktinfo = NULL;
257 struct cmsghdr *cmp = NULL;
258 struct iovec iov = { recvBuffer, sizeof (recvBuffer) };
262 unsigned char data[CMSG_SPACE(sizeof (struct in6_pktinfo))];
267 namelen = sizeof (struct sockaddr_in6);
268 level = IPPROTO_IPV6;
270 len = sizeof (struct in6_pktinfo);
274 namelen = sizeof (struct sockaddr_in);
277 len = sizeof (struct in6_pktinfo);
280 struct msghdr msg = { .msg_name = &srcAddr,
281 .msg_namelen = namelen,
284 .msg_control = &cmsg,
285 .msg_controllen = CMSG_SPACE(len) };
287 ssize_t recvLen = recvmsg(fd, &msg, flags);
290 OIC_LOG_V(ERROR, TAG, "Recvfrom failed %s", strerror(errno));
291 return CA_STATUS_FAILED;
294 if (flags & CA_MULTICAST)
296 for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL; cmp = CMSG_NXTHDR(&msg, cmp))
298 if (cmp->cmsg_level == level && cmp->cmsg_type == type)
300 pktinfo = CMSG_DATA(cmp);
305 CASecureEndpoint_t sep = {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = flags}};
309 sep.endpoint.interface = ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id;
310 ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id = 0;
312 if ((flags & CA_MULTICAST) && pktinfo)
314 struct in6_addr *addr = &(((struct in6_pktinfo *)pktinfo)->ipi6_addr);
315 unsigned char topbits = ((unsigned char *)addr)[0];
318 sep.endpoint.flags &= ~CA_MULTICAST;
324 if ((flags & CA_MULTICAST) && pktinfo)
326 struct in_addr *addr = &((struct in_pktinfo *)pktinfo)->ipi_addr;
327 uint32_t host = ntohl(addr->s_addr);
328 unsigned char topbits = ((unsigned char *)&host)[3];
329 if (topbits < 224 || topbits > 239)
331 sep.endpoint.flags &= ~CA_MULTICAST;
336 CAConvertAddrToName(&srcAddr, msg.msg_namelen, sep.endpoint.addr, &sep.endpoint.port);
338 if (flags & CA_SECURE)
341 int ret = CAAdapterNetDtlsDecrypt(&sep, (uint8_t *)recvBuffer, recvLen);
342 OIC_LOG_V(DEBUG, TAG, "CAAdapterNetDtlsDecrypt returns [%d]", ret);
344 OIC_LOG(ERROR, TAG, "Encrypted message but no DTLS");
349 if (g_packetReceivedCallback)
351 g_packetReceivedCallback(&sep, recvBuffer, recvLen);
360 OIC_LOG(DEBUG, TAG, "IN");
361 OIC_LOG(DEBUG, TAG, "OUT");
364 static int CACreateSocket(int family, uint16_t *port)
366 int socktype = SOCK_DGRAM;
368 socktype |= SOCK_CLOEXEC;
370 int fd = socket(family, socktype, IPPROTO_UDP);
373 OIC_LOG_V(ERROR, TAG, "create socket failed: %s", strerror(errno));
378 int fl = fcntl(fd, F_GETFD);
379 if (-1 == fl || -1 == fcntl(fd, F_SETFD, fl|FD_CLOEXEC))
381 OIC_LOG_V(ERROR, TAG, "set FD_CLOEXEC failed: %s", strerror(errno));
387 struct sockaddr_storage sa = { .ss_family = family };
390 if (family == AF_INET6)
394 if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
396 OIC_LOG_V(ERROR, TAG, "IPV6_V6ONLY failed: %s", strerror(errno));
399 if (*port) // only do this for multicast ports
401 if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof (on)))
403 OIC_LOG_V(ERROR, TAG, "IPV6_RECVPKTINFO failed: %s", strerror(errno));
407 ((struct sockaddr_in6 *)&sa)->sin6_port = htons(*port);
408 socklen = sizeof (struct sockaddr_in6);
412 if (*port) // only do this for multicast ports
415 if (-1 == setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &on, sizeof (on)))
417 OIC_LOG_V(ERROR, TAG, "IP_PKTINFO failed: %s", strerror(errno));
421 ((struct sockaddr_in *)&sa)->sin_port = htons(*port);
422 socklen = sizeof (struct sockaddr_in);
425 if (*port) // use the given port
428 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)))
430 OIC_LOG_V(ERROR, TAG, "SO_REUSEADDR failed: %s", strerror(errno));
436 if (-1 == bind(fd, (struct sockaddr *)&sa, socklen))
438 OIC_LOG_V(ERROR, TAG, "bind socket failed: %s", strerror(errno));
443 if (!*port) // return the assigned port
445 if (-1 == getsockname(fd, (struct sockaddr *)&sa, &socklen))
447 OIC_LOG_V(ERROR, TAG, "getsockname failed: %s", strerror(errno));
451 *port = ntohs(family == AF_INET6 ?
452 ((struct sockaddr_in6 *)&sa)->sin6_port :
453 ((struct sockaddr_in *)&sa)->sin_port);
459 #define CHECKFD(FD) \
460 if (FD > caglobals.ip.maxfd) \
461 caglobals.ip.maxfd = FD;
462 #define NEWSOCKET(FAMILY, NAME) \
463 caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port); \
464 CHECKFD(caglobals.ip.NAME.fd)
466 static void CAInitializeNetlink()
469 // create NETLINK fd for interface change notifications
470 struct sockaddr_nl sa = { AF_NETLINK, 0, 0, RTMGRP_LINK };
472 caglobals.ip.netlinkFd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE);
473 if (caglobals.ip.netlinkFd == -1)
475 OIC_LOG_V(ERROR, TAG, "netlink socket failed: %s", strerror(errno));
479 int r = bind(caglobals.ip.netlinkFd, (struct sockaddr *)&sa, sizeof (sa));
482 OIC_LOG_V(ERROR, TAG, "netlink bind failed: %s", strerror(errno));
483 close(caglobals.ip.netlinkFd);
484 caglobals.ip.netlinkFd = -1;
488 CHECKFD(caglobals.ip.netlinkFd);
494 static void CAInitializePipe()
496 caglobals.ip.selectTimeout = -1;
498 int ret = pipe2(caglobals.ip.shutdownFds, O_CLOEXEC);
500 int ret = pipe(caglobals.ip.shutdownFds);
503 ret = fcntl(caglobals.ip.shutdownFds[0], F_GETFD);
506 ret = fcntl(caglobals.ip.shutdownFds[0], F_SETFD, ret|FD_CLOEXEC);
510 ret = fcntl(caglobals.ip.shutdownFds[1], F_GETFD);
514 ret = fcntl(caglobals.ip.shutdownFds[1], F_SETFD, ret|FD_CLOEXEC);
518 close(caglobals.ip.shutdownFds[1]);
519 close(caglobals.ip.shutdownFds[0]);
520 caglobals.ip.shutdownFds[0] = -1;
521 caglobals.ip.shutdownFds[1] = -1;
527 OIC_LOG_V(ERROR, TAG, "pipe failed: %s", strerror(errno));
528 caglobals.ip.selectTimeout = SELECT_TIMEOUT; //poll needed for shutdown
532 CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
534 CAResult_t res = CA_STATUS_OK;
536 if (caglobals.ip.started)
541 if (!IPv4MulticastAddress.s_addr)
543 (void)inet_pton(AF_INET, IPv4_MULTICAST, &IPv4MulticastAddress);
544 (void)inet_pton(AF_INET6, IPv6_MULTICAST_INT, &IPv6MulticastAddressInt);
545 (void)inet_pton(AF_INET6, IPv6_MULTICAST_LNK, &IPv6MulticastAddressLnk);
546 (void)inet_pton(AF_INET6, IPv6_MULTICAST_RLM, &IPv6MulticastAddressRlm);
547 (void)inet_pton(AF_INET6, IPv6_MULTICAST_ADM, &IPv6MulticastAddressAdm);
548 (void)inet_pton(AF_INET6, IPv6_MULTICAST_SIT, &IPv6MulticastAddressSit);
549 (void)inet_pton(AF_INET6, IPv6_MULTICAST_ORG, &IPv6MulticastAddressOrg);
550 (void)inet_pton(AF_INET6, IPv6_MULTICAST_GLB, &IPv6MulticastAddressGlb);
553 if (!caglobals.ip.ipv6enabled && !caglobals.ip.ipv4enabled)
555 caglobals.ip.ipv4enabled = true; // only needed to run CA tests
558 if (caglobals.ip.ipv6enabled)
560 NEWSOCKET(AF_INET6, u6)
561 NEWSOCKET(AF_INET6, u6s)
562 NEWSOCKET(AF_INET6, m6)
563 NEWSOCKET(AF_INET6, m6s)
564 OIC_LOG_V(INFO, TAG, "IPv6 unicast port: %u", caglobals.ip.u6.port);
566 if (caglobals.ip.ipv4enabled)
568 NEWSOCKET(AF_INET, u4)
569 NEWSOCKET(AF_INET, u4s)
570 NEWSOCKET(AF_INET, m4)
571 NEWSOCKET(AF_INET, m4s)
572 OIC_LOG_V(INFO, TAG, "IPv4 unicast port: %u", caglobals.ip.u4.port);
575 OIC_LOG_V(DEBUG, TAG,
576 "socket summary: u6=%d, u6s=%d, u4=%d, u4s=%d, m6=%d, m6s=%d, m4=%d, m4s=%d",
577 caglobals.ip.u6.fd, caglobals.ip.u6s.fd, caglobals.ip.u4.fd, caglobals.ip.u4s.fd,
578 caglobals.ip.m6.fd, caglobals.ip.m6s.fd, caglobals.ip.m4.fd, caglobals.ip.m4s.fd);
580 OIC_LOG_V(DEBUG, TAG,
581 "port summary: u6 port=%d, u6s port=%d, u4 port=%d, u4s port=%d, m6 port=%d,"
582 "m6s port=%d, m4 port=%d, m4s port=%d",
583 caglobals.ip.u6.port, caglobals.ip.u6s.port, caglobals.ip.u4.port,
584 caglobals.ip.u4s.port, caglobals.ip.m6.port, caglobals.ip.m6s.port,
585 caglobals.ip.m4.port, caglobals.ip.m4s.port);
586 // create pipe for fast shutdown
588 CHECKFD(caglobals.ip.shutdownFds[0]);
589 CHECKFD(caglobals.ip.shutdownFds[1]);
591 // create source of network interface change notifications
592 CAInitializeNetlink();
594 caglobals.ip.selectTimeout = CAGetPollingInterval(caglobals.ip.selectTimeout);
596 res = CAIPStartListenServer();
597 if (CA_STATUS_OK != res)
599 OIC_LOG_V(ERROR, TAG, "Failed to start listening server![%d]", res);
603 caglobals.ip.terminate = false;
604 res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL);
605 if (CA_STATUS_OK != res)
607 OIC_LOG(ERROR, TAG, "thread_pool_add_task failed");
610 OIC_LOG(DEBUG, TAG, "CAReceiveHandler thread started successfully.");
612 caglobals.ip.started = true;
616 void CAIPStopServer()
618 caglobals.ip.started = false;
619 caglobals.ip.terminate = true;
621 if (caglobals.ip.shutdownFds[1] != -1)
623 close(caglobals.ip.shutdownFds[1]);
624 // receive thread will stop immediately
628 // receive thread will stop in SELECT_TIMEOUT seconds.
632 void CAWakeUpForChange()
634 if (caglobals.ip.shutdownFds[1] != -1)
639 len = write(caglobals.ip.shutdownFds[1], "w", 1);
640 } while ((len == -1) && (errno == EINTR));
641 if ((len == -1) && (errno != EINTR) && (errno != EPIPE))
643 OIC_LOG_V(DEBUG, TAG, "write failed: %s", strerror(errno));
648 static void applyMulticastToInterface4(struct in_addr inaddr)
650 if (!caglobals.ip.ipv4enabled)
655 struct ip_mreqn mreq = { .imr_multiaddr = IPv4MulticastAddress,
656 .imr_address = inaddr,
658 if (setsockopt(caglobals.ip.m4.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
660 if (EADDRINUSE != errno)
662 OIC_LOG_V(ERROR, TAG, "IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
665 if (setsockopt(caglobals.ip.m4s.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
667 if (EADDRINUSE != errno)
669 OIC_LOG_V(ERROR, TAG, "secure IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
674 static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t interface)
676 struct ipv6_mreq mreq = {.ipv6mr_multiaddr = *addr, .ipv6mr_interface = interface};
678 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof (mreq)))
680 if (EADDRINUSE != errno)
682 OIC_LOG_V(ERROR, TAG, "IPv6 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
687 static void applyMulticastToInterface6(uint32_t interface)
689 if (!caglobals.ip.ipv6enabled)
693 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressInt, interface);
694 applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressLnk, interface);
695 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressRlm, interface);
696 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressAdm, interface);
697 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressSit, interface);
698 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressOrg, interface);
699 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressGlb, interface);
701 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressInt, interface);
702 applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressLnk, interface);
703 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressRlm, interface);
704 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressAdm, interface);
705 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressSit, interface);
706 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressOrg, interface);
707 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressGlb, interface);
710 CAResult_t CAIPStartListenServer()
712 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
715 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
716 return CA_STATUS_FAILED;
719 uint32_t len = u_arraylist_length(iflist);
720 OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
722 for (uint32_t i = 0; i < len; i++)
724 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
730 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
734 if (ifitem->family == AF_INET)
736 struct in_addr inaddr;
737 inaddr.s_addr = ifitem->ipv4addr;
738 applyMulticastToInterface4(inaddr);
739 OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s", ifitem->name);
741 if (ifitem->family == AF_INET6)
743 applyMulticastToInterface6(ifitem->index);
744 OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
748 u_arraylist_destroy(iflist);
752 CAResult_t CAIPStopListenServer()
754 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
757 OIC_LOG_V(ERROR, TAG, "Get interface info failed: %s", strerror(errno));
758 return CA_STATUS_FAILED;
761 uint32_t len = u_arraylist_length(iflist);
762 OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
764 for (uint32_t i = 0; i < len; i++)
766 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
773 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
777 if (ifitem->family == AF_INET)
779 close(caglobals.ip.m4.fd);
780 close(caglobals.ip.m4s.fd);
781 caglobals.ip.m4.fd = -1;
782 caglobals.ip.m4s.fd = -1;
783 OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s cloed", ifitem->name);
785 if (ifitem->family == AF_INET6)
787 close(caglobals.ip.m6.fd);
788 close(caglobals.ip.m6s.fd);
789 caglobals.ip.m6.fd = -1;
790 caglobals.ip.m6s.fd = -1;
791 OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
794 u_arraylist_destroy(iflist);
798 static void CAProcessNewInterface(CAInterface_t *ifitem)
802 OIC_LOG(DEBUG, TAG, "ifitem is null");
805 if (ifitem->family == AF_INET6)
807 applyMulticastToInterface6(ifitem->index);
809 if (ifitem->family == AF_INET)
811 struct in_addr inaddr = { .s_addr = ifitem->ipv4addr };
812 applyMulticastToInterface4(inaddr);
815 static void CAHandleNetlink()
819 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
821 g_packetReceivedCallback = callback;
824 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
826 g_exceptionCallback = callback;
829 void CAIPSetConnectionStateChangeCallback(CAIPConnectionStateChangeCallback callback)
831 CAIPSetNetworkMonitorCallback(callback);
834 static void sendData(int fd, const CAEndpoint_t *endpoint,
835 const void *data, uint32_t dlen,
836 const char *cast, const char *fam)
838 OIC_LOG(DEBUG, TAG, "IN");
842 OIC_LOG(DEBUG, TAG, "endpoint is null");
846 char *secure = (endpoint->flags & CA_SECURE) ? "secure " : "";
847 (void)secure; // eliminates release warning
848 struct sockaddr_storage sock;
849 CAConvertNameToAddr(endpoint->addr, endpoint->port, &sock);
852 if (sock.ss_family == AF_INET6)
854 struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)&sock;
855 if (!sock6->sin6_scope_id)
857 sock6->sin6_scope_id = endpoint->interface;
859 socklen = sizeof(struct sockaddr_in6);
863 socklen = sizeof(struct sockaddr_in);
866 ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, socklen);
869 // If logging is not defined/enabled.
872 OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %s", secure, cast, fam, strerror(errno));
876 OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %zd bytes", secure, cast, fam, len);
880 static void sendMulticastData6(const u_arraylist_t *iflist,
881 CAEndpoint_t *endpoint,
882 const void *data, uint32_t datalen)
886 OIC_LOG(DEBUG, TAG, "endpoint is null");
890 int scope = endpoint->flags & CA_SCOPE_MASK;
891 char *ipv6mcname = ipv6mcnames[scope];
894 OIC_LOG_V(INFO, TAG, "IPv6 multicast scope invalid: %d", scope);
897 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), ipv6mcname);
898 int fd = caglobals.ip.u6.fd;
900 uint32_t len = u_arraylist_length(iflist);
901 for (uint32_t i = 0; i < len; i++)
903 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
908 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
912 if (ifitem->family != AF_INET6)
917 int index = ifitem->index;
918 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index, sizeof (index)))
920 OIC_LOG_V(ERROR, TAG, "setsockopt6 failed: %s", strerror(errno));
923 sendData(fd, endpoint, data, datalen, "multicast", "ipv6");
927 static void sendMulticastData4(const u_arraylist_t *iflist,
928 CAEndpoint_t *endpoint,
929 const void *data, uint32_t datalen)
931 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
933 struct ip_mreqn mreq = { .imr_multiaddr = IPv4MulticastAddress,
935 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
936 int fd = caglobals.ip.u4.fd;
938 uint32_t len = u_arraylist_length(iflist);
939 for (uint32_t i = 0; i < len; i++)
941 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
946 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
950 if (ifitem->family != AF_INET)
955 struct in_addr inaddr;
956 inaddr.s_addr = ifitem->ipv4addr;
957 mreq.imr_address = inaddr;
958 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof (mreq)))
960 OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)",
963 sendData(fd, endpoint, data, datalen, "multicast", "ipv4");
967 void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
970 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
971 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
973 bool isSecure = (endpoint->flags & CA_SECURE) != 0;
977 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
979 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
982 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
986 if ((endpoint->flags & CA_IPV6) && caglobals.ip.ipv6enabled)
988 sendMulticastData6(iflist, endpoint, data, datalen);
990 if ((endpoint->flags & CA_IPV4) && caglobals.ip.ipv4enabled)
992 sendMulticastData4(iflist, endpoint, data, datalen);
995 u_arraylist_destroy(iflist);
999 if (!endpoint->port) // unicast discovery
1001 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
1005 if (caglobals.ip.ipv6enabled && (endpoint->flags & CA_IPV6))
1007 fd = isSecure ? caglobals.ip.u6s.fd : caglobals.ip.u6.fd;
1008 #ifndef __WITH_DTLS__
1009 fd = caglobals.ip.u6.fd;
1011 sendData(fd, endpoint, data, datalen, "unicast", "ipv6");
1013 if (caglobals.ip.ipv4enabled && (endpoint->flags & CA_IPV4))
1015 fd = isSecure ? caglobals.ip.u4s.fd : caglobals.ip.u4.fd;
1016 #ifndef __WITH_DTLS__
1017 fd = caglobals.ip.u4.fd;
1019 sendData(fd, endpoint, data, datalen, "unicast", "ipv4");
1024 CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1026 VERIFY_NON_NULL(info, TAG, "info is NULL");
1027 VERIFY_NON_NULL(size, TAG, "size is NULL");
1029 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
1032 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
1033 return CA_STATUS_FAILED;
1036 uint32_t len = u_arraylist_length(iflist);
1037 uint32_t length = len;
1039 #ifdef __WITH_DTLS__
1040 //If DTLS is supported, each interface can support secure port as well
1044 CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(length, sizeof (CAEndpoint_t));
1047 OIC_LOG(ERROR, TAG, "Malloc Failed");
1048 u_arraylist_destroy(iflist);
1049 return CA_MEMORY_ALLOC_FAILED;
1052 for (uint32_t i = 0, j = 0; i < len; i++)
1054 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
1060 eps[j].adapter = CA_ADAPTER_IP;
1061 eps[j].interface = 0;
1063 if (ifitem->family == AF_INET6)
1065 eps[j].flags = CA_IPV6;
1066 eps[j].port = caglobals.ip.u6.port;
1070 eps[j].flags = CA_IPV4;
1071 eps[j].port = caglobals.ip.u4.port;
1073 inet_ntop(AF_INET, &(ifitem->ipv4addr), eps[j].addr, MAX_ADDR_STR_SIZE_CA);
1076 #ifdef __WITH_DTLS__
1079 eps[j].adapter = CA_ADAPTER_IP;
1080 eps[j].interface = 0;
1082 if (ifitem->family == AF_INET6)
1084 eps[j].flags = CA_IPV6 | CA_SECURE;
1085 eps[j].port = caglobals.ip.u6s.port;
1089 eps[j].flags = CA_IPV4 | CA_SECURE;
1090 eps[j].port = caglobals.ip.u4s.port;
1091 inet_ntop(AF_INET, &(ifitem->ipv4addr), eps[j].addr, MAX_ADDR_STR_SIZE_CA);
1100 u_arraylist_destroy(iflist);
1102 return CA_STATUS_OK;