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 };
293 if (family == AF_INET6)
296 if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
298 OIC_LOG_V(ERROR, TAG, "IPV6_V6ONLY failed: %s", strerror(errno));
300 ((struct sockaddr_in6 *)&sa)->sin6_port = htons(*port);
304 ((struct sockaddr_in *)&sa)->sin_port = htons(*port);
307 if (*port) // use the given port
310 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)))
312 OIC_LOG_V(ERROR, TAG, "SO_REUSEADDR failed: %s", strerror(errno));
318 if (-1 == bind(fd, (struct sockaddr *)&sa, sizeof(sa)))
320 OIC_LOG_V(ERROR, TAG, "bind socket failed: %s", strerror(errno));
325 if (!*port) // return the assigned port
327 struct sockaddr_storage sa;
328 socklen_t socklen = sizeof (sa);
329 if (-1 == getsockname(fd, (struct sockaddr *)&sa, &socklen))
331 OIC_LOG_V(ERROR, TAG, "getsockname failed: %s", strerror(errno));
335 *port = ntohs(family == AF_INET6 ?
336 ((struct sockaddr_in6 *)&sa)->sin6_port :
337 ((struct sockaddr_in *)&sa)->sin_port);
343 #define CHECKFD(FD) \
344 if (FD > caglobals.ip.maxfd) \
345 caglobals.ip.maxfd = FD;
346 #define NEWSOCKET(FAMILY, NAME) \
347 caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port); \
348 CHECKFD(caglobals.ip.NAME.fd)
350 static void CAInitializeNetlink()
353 // create NETLINK fd for interface change notifications
354 struct sockaddr_nl sa = { AF_NETLINK, 0, 0, RTMGRP_LINK };
356 caglobals.ip.netlinkFd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE);
357 if (caglobals.ip.netlinkFd == -1)
359 OIC_LOG_V(ERROR, TAG, "netlink socket failed: %s", strerror(errno));
363 int r = bind(caglobals.ip.netlinkFd, (struct sockaddr *)&sa, sizeof (sa));
366 OIC_LOG_V(ERROR, TAG, "netlink bind failed: %s", strerror(errno));
367 close(caglobals.ip.netlinkFd);
368 caglobals.ip.netlinkFd = -1;
372 CHECKFD(caglobals.ip.netlinkFd);
378 static void CAInitializePipe()
380 caglobals.ip.selectTimeout = -1;
382 int ret = pipe2(caglobals.ip.shutdownFds, O_CLOEXEC);
384 int ret = pipe(caglobals.ip.shutdownFds);
387 ret = fcntl(caglobals.ip.shutdownFds[0], F_GETFD);
390 ret = fcntl(caglobals.ip.shutdownFds[0], F_SETFD, ret|FD_CLOEXEC);
394 ret = fcntl(caglobals.ip.shutdownFds[1], F_GETFD);
398 ret = fcntl(caglobals.ip.shutdownFds[1], F_SETFD, ret|FD_CLOEXEC);
402 close(caglobals.ip.shutdownFds[1]);
403 close(caglobals.ip.shutdownFds[0]);
404 caglobals.ip.shutdownFds[0] = -1;
405 caglobals.ip.shutdownFds[1] = -1;
411 OIC_LOG_V(ERROR, TAG, "pipe failed: %s", strerror(errno));
412 caglobals.ip.selectTimeout = SELECT_TIMEOUT; //poll needed for shutdown
416 CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
418 CAResult_t res = CA_STATUS_OK;
420 if (caglobals.ip.started)
425 if (!IPv4MulticastAddress.s_addr)
427 (void)inet_aton(IPv4_MULTICAST, &IPv4MulticastAddress);
428 (void)inet_pton(AF_INET6, IPv6_MULTICAST_INT, &IPv6MulticastAddressInt);
429 (void)inet_pton(AF_INET6, IPv6_MULTICAST_LNK, &IPv6MulticastAddressLnk);
430 (void)inet_pton(AF_INET6, IPv6_MULTICAST_RLM, &IPv6MulticastAddressRlm);
431 (void)inet_pton(AF_INET6, IPv6_MULTICAST_ADM, &IPv6MulticastAddressAdm);
432 (void)inet_pton(AF_INET6, IPv6_MULTICAST_SIT, &IPv6MulticastAddressSit);
433 (void)inet_pton(AF_INET6, IPv6_MULTICAST_ORG, &IPv6MulticastAddressOrg);
434 (void)inet_pton(AF_INET6, IPv6_MULTICAST_GLB, &IPv6MulticastAddressGlb);
437 if (!caglobals.ip.ipv6enabled && !caglobals.ip.ipv4enabled)
439 caglobals.ip.ipv4enabled = true; // only needed to run CA tests
442 if (caglobals.ip.ipv6enabled)
444 NEWSOCKET(AF_INET6, u6)
445 NEWSOCKET(AF_INET6, u6s)
446 NEWSOCKET(AF_INET6, m6)
447 NEWSOCKET(AF_INET6, m6s)
449 if (caglobals.ip.ipv4enabled)
451 NEWSOCKET(AF_INET, u4)
452 NEWSOCKET(AF_INET, u4s)
453 NEWSOCKET(AF_INET, m4)
454 NEWSOCKET(AF_INET, m4s)
457 OIC_LOG_V(DEBUG, TAG,
458 "socket summary: u6=%d, u6s=%d, u4=%d, u4s=%d, m6=%d, m6s=%d, m4=%d, m4s=%d",
459 caglobals.ip.u6.fd, caglobals.ip.u6s.fd,
460 caglobals.ip.u4.fd, caglobals.ip.u4s.fd,
461 caglobals.ip.m6.fd, caglobals.ip.m6s.fd,
462 caglobals.ip.m4.fd, caglobals.ip.m4s.fd);
464 // create pipe for fast shutdown
466 CHECKFD(caglobals.ip.shutdownFds[0]);
467 CHECKFD(caglobals.ip.shutdownFds[1]);
469 // create source of network interface change notifications
470 CAInitializeNetlink();
474 caglobals.ip.terminate = false;
475 res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL);
476 if (CA_STATUS_OK != res)
478 OIC_LOG(ERROR, TAG, "thread_pool_add_task failed");
481 OIC_LOG(DEBUG, TAG, "CAReceiveHandler thread started successfully.");
483 caglobals.ip.started = true;
487 void CAIPStopServer()
489 OIC_LOG(DEBUG, TAG, "IN");
491 caglobals.ip.terminate = true;
493 if (caglobals.ip.shutdownFds[1] != -1)
495 close(caglobals.ip.shutdownFds[1]);
496 // receive thread will stop immediately
500 // receive thread will stop in SELECT_TIMEOUT seconds.
503 OIC_LOG(DEBUG, TAG, "OUT");
506 static void applyMulticastToInterface4(struct in_addr inaddr)
508 if (!caglobals.ip.ipv4enabled)
513 struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress,
514 .imr_interface = inaddr};
515 if (setsockopt(caglobals.ip.m4.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
517 if (EADDRINUSE != errno)
519 OIC_LOG_V(ERROR, TAG, "IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
522 if (setsockopt(caglobals.ip.m4s.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
524 if (EADDRINUSE != errno)
526 OIC_LOG_V(ERROR, TAG, "secure IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
531 static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t interface)
533 struct ipv6_mreq mreq;
534 mreq.ipv6mr_multiaddr = *addr;
535 mreq.ipv6mr_interface = interface;
536 if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
538 if (EADDRINUSE != errno)
540 OIC_LOG_V(ERROR, TAG, "IPv6 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
545 static void applyMulticastToInterface6(uint32_t interface)
547 if (!caglobals.ip.ipv6enabled)
551 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressInt, interface);
552 applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressLnk, interface);
553 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressRlm, interface);
554 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressAdm, interface);
555 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressSit, interface);
556 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressOrg, interface);
557 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressGlb, interface);
558 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressInt, interface);
559 applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressLnk, interface);
560 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressRlm, interface);
561 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressAdm, interface);
562 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressSit, interface);
563 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressOrg, interface);
564 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressGlb, interface);
567 static void CAApplyInterfaces()
569 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
572 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
576 uint32_t len = u_arraylist_length(iflist);
577 OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
579 for (uint32_t i = 0; i < len; i++)
581 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
587 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
591 if (ifitem->family == AF_INET)
593 struct in_addr inaddr;
594 inaddr.s_addr = ifitem->ipv4addr;
595 applyMulticastToInterface4(inaddr);
596 OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s", ifitem->name);
598 if (ifitem->family == AF_INET6)
600 applyMulticastToInterface6(ifitem->index);
601 OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
605 u_arraylist_destroy(iflist);
608 static void CAHandleNetlink()
613 struct sockaddr_nl sa;
614 struct iovec iov = { buf, sizeof(buf) };
615 struct msghdr msg = { (void *)&sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
617 int len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
618 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
620 if (nh->nlmsg_type == RTM_NEWLINK)
622 struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
623 if ((ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
628 int newIndex = ifi->ifi_index;
630 u_arraylist_t *iflist = CAIPGetInterfaceInformation(newIndex);
633 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
637 uint32_t len = u_arraylist_length(iflist);
638 for (uint32_t i = 0; i < len; i++)
640 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
646 if ((int)ifitem->index != newIndex)
651 applyMulticastToInterface6(newIndex);
652 struct in_addr inaddr;
653 inaddr.s_addr = ifitem->ipv4addr;
654 applyMulticastToInterface4(inaddr);
655 break; // we found the one we were looking for
657 u_arraylist_destroy(iflist);
663 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
665 OIC_LOG(DEBUG, TAG, "IN");
667 g_packetReceivedCallback = callback;
669 OIC_LOG(DEBUG, TAG, "OUT");
672 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
674 OIC_LOG(DEBUG, TAG, "IN");
676 g_exceptionCallback = callback;
678 OIC_LOG(DEBUG, TAG, "OUT");
681 static void sendData(int fd, const CAEndpoint_t *endpoint,
682 const void *data, uint32_t dlen,
683 const char *cast, const char *fam)
685 OIC_LOG(DEBUG, TAG, "IN");
687 char *secure = (endpoint->flags & CA_SECURE) ? "secure " : "";
688 (void)secure; // eliminates release warning
689 struct sockaddr_storage sock;
690 CAConvertNameToAddr(endpoint->addr, endpoint->port, &sock);
692 if (sock.ss_family == AF_INET6)
694 struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)&sock;
695 if (!sock6->sin6_scope_id)
697 sock6->sin6_scope_id = endpoint->interface;
701 ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, sizeof (sock));
704 // If logging is not defined/enabled.
707 OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %s", secure, cast, fam, strerror(errno));
711 OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %d bytes", secure, cast, fam, len);
715 static void sendMulticastData6(const u_arraylist_t *iflist,
716 CAEndpoint_t *endpoint,
717 const void *data, uint32_t datalen)
719 int scope = endpoint->flags & CA_SCOPE_MASK;
720 char *ipv6mcname = ipv6mcnames[scope];
723 OIC_LOG_V(INFO, TAG, "IPv6 multicast scope invalid: %d", scope);
726 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), ipv6mcname);
727 int fd = caglobals.ip.u6.fd;
729 uint32_t len = u_arraylist_length(iflist);
730 for (uint32_t i = 0; i < len; i++)
732 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
737 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
741 if (ifitem->family != AF_INET6)
746 int index = ifitem->index;
747 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index, sizeof (index)))
749 OIC_LOG_V(ERROR, TAG, "setsockopt6 failed: %s", strerror(errno));
752 sendData(fd, endpoint, data, datalen, "multicast", "ipv6");
756 static void sendMulticastData4(const u_arraylist_t *iflist,
757 CAEndpoint_t *endpoint,
758 const void *data, uint32_t datalen)
760 struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress };
761 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
762 int fd = caglobals.ip.u4.fd;
764 uint32_t len = u_arraylist_length(iflist);
765 for (uint32_t i = 0; i < len; i++)
767 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
772 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
776 if (ifitem->family != AF_INET)
781 struct in_addr inaddr;
782 inaddr.s_addr = ifitem->ipv4addr;
783 mreq.imr_interface = inaddr;
784 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof (mreq)))
786 OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)",
789 sendData(fd, endpoint, data, datalen, "multicast", "ipv4");
793 void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
796 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
797 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
799 bool isSecure = (endpoint->flags & CA_SECURE) != 0;
803 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
805 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
808 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
812 if ((endpoint->flags & CA_IPV6) && caglobals.ip.ipv6enabled)
814 sendMulticastData6(iflist, endpoint, data, datalen);
816 if ((endpoint->flags & CA_IPV4) && caglobals.ip.ipv4enabled)
818 sendMulticastData4(iflist, endpoint, data, datalen);
821 u_arraylist_destroy(iflist);
826 if (endpoint->flags & CA_IPV6)
828 fd = isSecure ? caglobals.ip.u6s.fd : caglobals.ip.u6.fd;
829 #ifndef __WITH_DTLS__
830 fd = caglobals.ip.u6.fd;
832 sendData(fd, endpoint, data, datalen, "unicast", "ipv6");
834 if (endpoint->flags & CA_IPV4)
836 fd = isSecure ? caglobals.ip.u4s.fd : caglobals.ip.u4.fd;
837 #ifndef __WITH_DTLS__
838 fd = caglobals.ip.u4.fd;
840 sendData(fd, endpoint, data, datalen, "unicast", "ipv4");
845 CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
847 OIC_LOG(DEBUG, TAG, "IN");
849 VERIFY_NON_NULL(info, TAG, "info is NULL");
850 VERIFY_NON_NULL(size, TAG, "size is NULL");
852 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
855 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
856 return CA_STATUS_FAILED;
859 uint32_t len = u_arraylist_length(iflist);
861 CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(len, sizeof (CAEndpoint_t));
864 OIC_LOG(ERROR, TAG, "Malloc Failed");
865 u_arraylist_destroy(iflist);
866 return CA_MEMORY_ALLOC_FAILED;
869 for (uint32_t i = 0, j = 0; i < len; i++)
871 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
877 OICStrcpy(eps[j].addr, CA_INTERFACE_NAME_SIZE, ifitem->name);
878 eps[j].flags = ifitem->family == AF_INET6 ? CA_IPV6 : CA_IPV4;
879 eps[j].adapter = CA_ADAPTER_IP;
880 eps[j].interface = 0;
888 u_arraylist_destroy(iflist);
890 OIC_LOG(DEBUG, TAG, "OUT");