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 #include "caipinterface.h"
24 #include <asm/types.h>
26 #ifndef IPV6_ADD_MEMBERSHIP
27 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
30 #include <sys/types.h>
31 #include <sys/socket.h>
34 #include <sys/types.h>
36 #include <sys/select.h>
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
42 #include <linux/netlink.h>
43 #include <linux/rtnetlink.h>
47 #include "caadapterutils.h"
49 #include "caadapternetdtls.h"
52 #include "oic_malloc.h"
53 #include "oic_string.h"
57 * @brief Logging tag for module name
59 #define TAG "IP_SERVER"
61 #define SELECT_TIMEOUT 1 // select() seconds (and termination latency)
63 #define IPv4_MULTICAST "224.0.1.187"
64 static struct in_addr IPv4MulticastAddress = { 0 };
66 #define IPv6_DOMAINS 16
67 #define IPv6_MULTICAST_INT "ff01::fd"
68 static struct in6_addr IPv6MulticastAddressInt;
69 #define IPv6_MULTICAST_LNK "ff02::fd"
70 static struct in6_addr IPv6MulticastAddressLnk;
71 #define IPv6_MULTICAST_RLM "ff03::fd"
72 static struct in6_addr IPv6MulticastAddressRlm;
73 #define IPv6_MULTICAST_ADM "ff04::fd"
74 static struct in6_addr IPv6MulticastAddressAdm;
75 #define IPv6_MULTICAST_SIT "ff05::fd"
76 static struct in6_addr IPv6MulticastAddressSit;
77 #define IPv6_MULTICAST_ORG "ff08::fd"
78 static struct in6_addr IPv6MulticastAddressOrg;
79 #define IPv6_MULTICAST_GLB "ff0e::fd"
80 static struct in6_addr IPv6MulticastAddressGlb;
82 static char *ipv6mcnames[IPv6_DOMAINS] = {
101 static CAIPExceptionCallback g_exceptionCallback;
103 static CAIPPacketReceivedCallback g_packetReceivedCallback;
105 static void CAHandleNetlink();
106 static void CAApplyInterfaces();
107 static void CAFindReadyMessage();
108 static void CASelectReturned(fd_set *readFds, int ret);
109 static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags);
111 #define SET(TYPE, FDS) \
112 if (caglobals.ip.TYPE.fd != -1) \
114 FD_SET(caglobals.ip.TYPE.fd, FDS); \
117 #define ISSET(TYPE, FDS, FLAGS) \
118 if (caglobals.ip.TYPE.fd != -1 && FD_ISSET(caglobals.ip.TYPE.fd, FDS)) \
120 fd = caglobals.ip.TYPE.fd; \
124 static void CAReceiveHandler(void *data)
127 OIC_LOG(DEBUG, TAG, "IN");
129 while (!caglobals.ip.terminate)
131 CAFindReadyMessage();
134 OIC_LOG(DEBUG, TAG, "OUT");
137 static void CAFindReadyMessage()
140 struct timeval timeout;
142 timeout.tv_sec = caglobals.ip.selectTimeout;
144 struct timeval *tv = caglobals.ip.selectTimeout == -1 ? NULL : &timeout;
155 if (caglobals.ip.shutdownFds[0] != -1)
157 FD_SET(caglobals.ip.shutdownFds[0], &readFds);
159 if (caglobals.ip.netlinkFd != -1)
161 FD_SET(caglobals.ip.netlinkFd, &readFds);
164 int ret = select(caglobals.ip.maxfd + 1, &readFds, NULL, NULL, tv);
166 if (caglobals.ip.terminate)
168 OIC_LOG_V(DEBUG, TAG, "Packet receiver Stop request received.");
175 OIC_LOG_V(FATAL, TAG, "select error %s", strerror(errno));
180 CASelectReturned(&readFds, ret);
183 static void CASelectReturned(fd_set *readFds, int ret)
187 CATransportFlags_t flags = CA_DEFAULT_FLAGS;
189 while (!caglobals.ip.terminate)
191 ISSET(u6, readFds, CA_IPV6)
192 else ISSET(u6s, readFds, CA_IPV6 | CA_SECURE)
193 else ISSET(u4, readFds, CA_IPV4)
194 else ISSET(u4s, readFds, CA_IPV4 | CA_SECURE)
195 else ISSET(m6, readFds, CA_MULTICAST | CA_IPV6)
196 else ISSET(m6s, readFds, CA_MULTICAST | CA_IPV6 | CA_SECURE)
197 else ISSET(m4, readFds, CA_MULTICAST | CA_IPV4)
198 else ISSET(m4s, readFds, CA_MULTICAST | CA_IPV4 | CA_SECURE)
199 else if (FD_ISSET(caglobals.ip.netlinkFd, readFds))
209 (void)CAReceiveMessage(fd, flags);
214 static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags)
216 char recvBuffer[COAP_MAX_PDU_SIZE];
218 struct sockaddr_storage srcAddr;
219 socklen_t srcAddrLen = sizeof (srcAddr);
221 ssize_t recvLen = recvfrom(fd,
225 (struct sockaddr *)&srcAddr,
229 OIC_LOG_V(ERROR, TAG, "Recvfrom failed %s", strerror(errno));
230 return CA_STATUS_FAILED;
233 CAEndpoint_t ep = { .adapter = CA_ADAPTER_IP,
238 ep.interface = ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id;
239 ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id = 0;
241 CAConvertAddrToName(&srcAddr, ep.addr, &ep.port);
243 if (flags & CA_SECURE)
246 int ret = CAAdapterNetDtlsDecrypt(&ep, (uint8_t *)recvBuffer, recvLen);
247 OIC_LOG_V(DEBUG, TAG, "CAAdapterNetDtlsDecrypt returns [%d]", ret);
249 OIC_LOG(ERROR, TAG, "Encrypted message but no DTLS");
254 if (g_packetReceivedCallback)
256 g_packetReceivedCallback(&ep, recvBuffer, recvLen);
265 OIC_LOG(DEBUG, TAG, "IN");
266 OIC_LOG(DEBUG, TAG, "OUT");
269 static int CACreateSocket(int family, uint16_t *port)
271 int socktype = SOCK_DGRAM;
273 socktype |= SOCK_CLOEXEC;
275 int fd = socket(family, socktype, IPPROTO_UDP);
278 OIC_LOG_V(ERROR, TAG, "create socket failed: %s", strerror(errno));
283 int fl = fcntl(fd, F_GETFD);
284 if (-1 == fl || -1 == fcntl(fd, F_SETFD, fl|FD_CLOEXEC))
286 OIC_LOG_V(ERROR, TAG, "set FD_CLOEXEC failed: %s", strerror(errno));
292 struct sockaddr_storage sa = { .ss_family = family };
295 if (family == AF_INET6)
298 if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
300 OIC_LOG_V(ERROR, TAG, "IPV6_V6ONLY failed: %s", strerror(errno));
302 ((struct sockaddr_in6 *)&sa)->sin6_port = htons(*port);
303 socklen = sizeof (struct sockaddr_in6);
307 ((struct sockaddr_in *)&sa)->sin_port = htons(*port);
308 socklen = sizeof (struct sockaddr_in);
311 if (*port) // use the given port
314 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)))
316 OIC_LOG_V(ERROR, TAG, "SO_REUSEADDR failed: %s", strerror(errno));
322 if (-1 == bind(fd, (struct sockaddr *)&sa, socklen))
324 OIC_LOG_V(ERROR, TAG, "bind socket failed: %s", strerror(errno));
329 if (!*port) // return the assigned port
331 if (-1 == getsockname(fd, (struct sockaddr *)&sa, &socklen))
333 OIC_LOG_V(ERROR, TAG, "getsockname failed: %s", strerror(errno));
337 *port = ntohs(family == AF_INET6 ?
338 ((struct sockaddr_in6 *)&sa)->sin6_port :
339 ((struct sockaddr_in *)&sa)->sin_port);
345 #define CHECKFD(FD) \
346 if (FD > caglobals.ip.maxfd) \
347 caglobals.ip.maxfd = FD;
348 #define NEWSOCKET(FAMILY, NAME) \
349 caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port); \
350 CHECKFD(caglobals.ip.NAME.fd)
352 static void CAInitializeNetlink()
355 // create NETLINK fd for interface change notifications
356 struct sockaddr_nl sa = { AF_NETLINK, 0, 0, RTMGRP_LINK };
358 caglobals.ip.netlinkFd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE);
359 if (caglobals.ip.netlinkFd == -1)
361 OIC_LOG_V(ERROR, TAG, "netlink socket failed: %s", strerror(errno));
365 int r = bind(caglobals.ip.netlinkFd, (struct sockaddr *)&sa, sizeof (sa));
368 OIC_LOG_V(ERROR, TAG, "netlink bind failed: %s", strerror(errno));
369 close(caglobals.ip.netlinkFd);
370 caglobals.ip.netlinkFd = -1;
374 CHECKFD(caglobals.ip.netlinkFd);
380 static void CAInitializePipe()
382 caglobals.ip.selectTimeout = -1;
384 int ret = pipe2(caglobals.ip.shutdownFds, O_CLOEXEC);
386 int ret = pipe(caglobals.ip.shutdownFds);
389 ret = fcntl(caglobals.ip.shutdownFds[0], F_GETFD);
392 ret = fcntl(caglobals.ip.shutdownFds[0], F_SETFD, ret|FD_CLOEXEC);
396 ret = fcntl(caglobals.ip.shutdownFds[1], F_GETFD);
400 ret = fcntl(caglobals.ip.shutdownFds[1], F_SETFD, ret|FD_CLOEXEC);
404 close(caglobals.ip.shutdownFds[1]);
405 close(caglobals.ip.shutdownFds[0]);
406 caglobals.ip.shutdownFds[0] = -1;
407 caglobals.ip.shutdownFds[1] = -1;
413 OIC_LOG_V(ERROR, TAG, "pipe failed: %s", strerror(errno));
414 caglobals.ip.selectTimeout = SELECT_TIMEOUT; //poll needed for shutdown
418 CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
420 CAResult_t res = CA_STATUS_OK;
422 if (caglobals.ip.started)
427 if (!IPv4MulticastAddress.s_addr)
429 (void)inet_aton(IPv4_MULTICAST, &IPv4MulticastAddress);
430 (void)inet_pton(AF_INET6, IPv6_MULTICAST_INT, &IPv6MulticastAddressInt);
431 (void)inet_pton(AF_INET6, IPv6_MULTICAST_LNK, &IPv6MulticastAddressLnk);
432 (void)inet_pton(AF_INET6, IPv6_MULTICAST_RLM, &IPv6MulticastAddressRlm);
433 (void)inet_pton(AF_INET6, IPv6_MULTICAST_ADM, &IPv6MulticastAddressAdm);
434 (void)inet_pton(AF_INET6, IPv6_MULTICAST_SIT, &IPv6MulticastAddressSit);
435 (void)inet_pton(AF_INET6, IPv6_MULTICAST_ORG, &IPv6MulticastAddressOrg);
436 (void)inet_pton(AF_INET6, IPv6_MULTICAST_GLB, &IPv6MulticastAddressGlb);
439 if (!caglobals.ip.ipv6enabled && !caglobals.ip.ipv4enabled)
441 caglobals.ip.ipv4enabled = true; // only needed to run CA tests
444 if (caglobals.ip.ipv6enabled)
446 NEWSOCKET(AF_INET6, u6)
447 NEWSOCKET(AF_INET6, u6s)
448 NEWSOCKET(AF_INET6, m6)
449 NEWSOCKET(AF_INET6, m6s)
450 OIC_LOG_V(INFO, TAG, "IPv6 unicast port: %u", caglobals.ip.u6.port);
452 if (caglobals.ip.ipv4enabled)
454 NEWSOCKET(AF_INET, u4)
455 NEWSOCKET(AF_INET, u4s)
456 NEWSOCKET(AF_INET, m4)
457 NEWSOCKET(AF_INET, m4s)
458 OIC_LOG_V(INFO, TAG, "IPv4 unicast port: %u", caglobals.ip.u4.port);
461 OIC_LOG_V(DEBUG, TAG,
462 "socket summary: u6=%d, u6s=%d, u4=%d, u4s=%d, m6=%d, m6s=%d, m4=%d, m4s=%d",
463 caglobals.ip.u6.fd, caglobals.ip.u6s.fd,
464 caglobals.ip.u4.fd, caglobals.ip.u4s.fd,
465 caglobals.ip.m6.fd, caglobals.ip.m6s.fd,
466 caglobals.ip.m4.fd, caglobals.ip.m4s.fd);
468 // create pipe for fast shutdown
470 CHECKFD(caglobals.ip.shutdownFds[0]);
471 CHECKFD(caglobals.ip.shutdownFds[1]);
473 // create source of network interface change notifications
474 CAInitializeNetlink();
478 caglobals.ip.terminate = false;
479 res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL);
480 if (CA_STATUS_OK != res)
482 OIC_LOG(ERROR, TAG, "thread_pool_add_task failed");
485 OIC_LOG(DEBUG, TAG, "CAReceiveHandler thread started successfully.");
487 caglobals.ip.started = true;
491 void CAIPStopServer()
493 OIC_LOG(DEBUG, TAG, "IN");
495 caglobals.ip.terminate = true;
497 if (caglobals.ip.shutdownFds[1] != -1)
499 close(caglobals.ip.shutdownFds[1]);
500 // receive thread will stop immediately
504 // receive thread will stop in SELECT_TIMEOUT seconds.
507 OIC_LOG(DEBUG, TAG, "OUT");
510 static void applyMulticastToInterface4(struct in_addr inaddr)
512 if (!caglobals.ip.ipv4enabled)
517 struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress,
518 .imr_interface = inaddr};
519 if (setsockopt(caglobals.ip.m4.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
521 if (EADDRINUSE != errno)
523 OIC_LOG_V(ERROR, TAG, "IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
526 if (setsockopt(caglobals.ip.m4s.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
528 if (EADDRINUSE != errno)
530 OIC_LOG_V(ERROR, TAG, "secure IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
535 static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t interface)
537 struct ipv6_mreq mreq;
538 mreq.ipv6mr_multiaddr = *addr;
539 mreq.ipv6mr_interface = interface;
540 if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
542 if (EADDRINUSE != errno)
544 OIC_LOG_V(ERROR, TAG, "IPv6 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
549 static void applyMulticastToInterface6(uint32_t interface)
551 if (!caglobals.ip.ipv6enabled)
555 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressInt, interface);
556 applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressLnk, interface);
557 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressRlm, interface);
558 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressAdm, interface);
559 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressSit, interface);
560 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressOrg, interface);
561 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressGlb, interface);
562 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressInt, interface);
563 applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressLnk, interface);
564 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressRlm, interface);
565 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressAdm, interface);
566 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressSit, interface);
567 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressOrg, interface);
568 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressGlb, interface);
571 static void CAApplyInterfaces()
573 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
576 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
580 uint32_t len = u_arraylist_length(iflist);
581 OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
583 for (uint32_t i = 0; i < len; i++)
585 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
591 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
595 if (ifitem->family == AF_INET)
597 struct in_addr inaddr;
598 inaddr.s_addr = ifitem->ipv4addr;
599 applyMulticastToInterface4(inaddr);
600 OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s", ifitem->name);
602 if (ifitem->family == AF_INET6)
604 applyMulticastToInterface6(ifitem->index);
605 OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
609 u_arraylist_destroy(iflist);
612 static void CAHandleNetlink()
617 struct sockaddr_nl sa;
618 struct iovec iov = { buf, sizeof(buf) };
619 struct msghdr msg = { (void *)&sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
621 size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
623 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
625 if (nh->nlmsg_type == RTM_NEWLINK)
627 struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
628 if ((ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
633 int newIndex = ifi->ifi_index;
635 u_arraylist_t *iflist = CAIPGetInterfaceInformation(newIndex);
638 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
642 uint32_t listLength = u_arraylist_length(iflist);
643 for (uint32_t i = 0; i < listLength; i++)
645 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
651 if ((int)ifitem->index != newIndex)
656 applyMulticastToInterface6(newIndex);
657 struct in_addr inaddr;
658 inaddr.s_addr = ifitem->ipv4addr;
659 applyMulticastToInterface4(inaddr);
660 break; // we found the one we were looking for
662 u_arraylist_destroy(iflist);
668 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
670 OIC_LOG(DEBUG, TAG, "IN");
672 g_packetReceivedCallback = callback;
674 OIC_LOG(DEBUG, TAG, "OUT");
677 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
679 OIC_LOG(DEBUG, TAG, "IN");
681 g_exceptionCallback = callback;
683 OIC_LOG(DEBUG, TAG, "OUT");
686 static void sendData(int fd, const CAEndpoint_t *endpoint,
687 const void *data, uint32_t dlen,
688 const char *cast, const char *fam)
690 OIC_LOG(DEBUG, TAG, "IN");
692 char *secure = (endpoint->flags & CA_SECURE) ? "secure " : "";
693 (void)secure; // eliminates release warning
694 struct sockaddr_storage sock;
695 CAConvertNameToAddr(endpoint->addr, endpoint->port, &sock);
698 if (sock.ss_family == AF_INET6)
700 struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)&sock;
701 if (!sock6->sin6_scope_id)
703 sock6->sin6_scope_id = endpoint->interface;
705 socklen = sizeof(struct sockaddr_in6);
709 socklen = sizeof(struct sockaddr_in);
712 ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, socklen);
715 // If logging is not defined/enabled.
718 OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %s", secure, cast, fam, strerror(errno));
722 OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %d bytes", secure, cast, fam, len);
726 static void sendMulticastData6(const u_arraylist_t *iflist,
727 CAEndpoint_t *endpoint,
728 const void *data, uint32_t datalen)
730 int scope = endpoint->flags & CA_SCOPE_MASK;
731 char *ipv6mcname = ipv6mcnames[scope];
734 OIC_LOG_V(INFO, TAG, "IPv6 multicast scope invalid: %d", scope);
737 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), ipv6mcname);
738 int fd = caglobals.ip.u6.fd;
740 uint32_t len = u_arraylist_length(iflist);
741 for (uint32_t i = 0; i < len; i++)
743 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
748 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
752 if (ifitem->family != AF_INET6)
757 int index = ifitem->index;
758 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index, sizeof (index)))
760 OIC_LOG_V(ERROR, TAG, "setsockopt6 failed: %s", strerror(errno));
763 sendData(fd, endpoint, data, datalen, "multicast", "ipv6");
767 static void sendMulticastData4(const u_arraylist_t *iflist,
768 CAEndpoint_t *endpoint,
769 const void *data, uint32_t datalen)
771 struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress };
772 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
773 int fd = caglobals.ip.u4.fd;
775 uint32_t len = u_arraylist_length(iflist);
776 for (uint32_t i = 0; i < len; i++)
778 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
783 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
787 if (ifitem->family != AF_INET)
792 struct in_addr inaddr;
793 inaddr.s_addr = ifitem->ipv4addr;
794 mreq.imr_interface = inaddr;
795 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof (mreq)))
797 OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)",
800 sendData(fd, endpoint, data, datalen, "multicast", "ipv4");
804 void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
807 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
808 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
810 bool isSecure = (endpoint->flags & CA_SECURE) != 0;
814 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
816 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
819 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
823 if ((endpoint->flags & CA_IPV6) && caglobals.ip.ipv6enabled)
825 sendMulticastData6(iflist, endpoint, data, datalen);
827 if ((endpoint->flags & CA_IPV4) && caglobals.ip.ipv4enabled)
829 sendMulticastData4(iflist, endpoint, data, datalen);
832 u_arraylist_destroy(iflist);
836 if (!endpoint->port) // unicast discovery
838 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
842 if (caglobals.ip.ipv6enabled && (endpoint->flags & CA_IPV6))
844 fd = isSecure ? caglobals.ip.u6s.fd : caglobals.ip.u6.fd;
845 #ifndef __WITH_DTLS__
846 fd = caglobals.ip.u6.fd;
848 sendData(fd, endpoint, data, datalen, "unicast", "ipv6");
850 if (caglobals.ip.ipv4enabled && (endpoint->flags & CA_IPV4))
852 fd = isSecure ? caglobals.ip.u4s.fd : caglobals.ip.u4.fd;
853 #ifndef __WITH_DTLS__
854 fd = caglobals.ip.u4.fd;
856 sendData(fd, endpoint, data, datalen, "unicast", "ipv4");
861 CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
863 OIC_LOG(DEBUG, TAG, "IN");
865 VERIFY_NON_NULL(info, TAG, "info is NULL");
866 VERIFY_NON_NULL(size, TAG, "size is NULL");
868 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
871 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
872 return CA_STATUS_FAILED;
875 uint32_t len = u_arraylist_length(iflist);
877 CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(len, sizeof (CAEndpoint_t));
880 OIC_LOG(ERROR, TAG, "Malloc Failed");
881 u_arraylist_destroy(iflist);
882 return CA_MEMORY_ALLOC_FAILED;
885 for (uint32_t i = 0, j = 0; i < len; i++)
887 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
893 OICStrcpy(eps[j].addr, CA_INTERFACE_NAME_SIZE, ifitem->name);
894 eps[j].flags = ifitem->family == AF_INET6 ? CA_IPV6 : CA_IPV4;
895 eps[j].adapter = CA_ADAPTER_IP;
896 eps[j].interface = 0;
904 u_arraylist_destroy(iflist);
906 OIC_LOG(DEBUG, TAG, "OUT");