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)
448 OIC_LOG_V(INFO, TAG, "IPv6 unicast port: %u", caglobals.ip.u6.port);
450 if (caglobals.ip.ipv4enabled)
452 NEWSOCKET(AF_INET, u4)
453 NEWSOCKET(AF_INET, u4s)
454 NEWSOCKET(AF_INET, m4)
455 NEWSOCKET(AF_INET, m4s)
456 OIC_LOG_V(INFO, TAG, "IPv4 unicast port: %u", caglobals.ip.u4.port);
459 OIC_LOG_V(DEBUG, TAG,
460 "socket summary: u6=%d, u6s=%d, u4=%d, u4s=%d, m6=%d, m6s=%d, m4=%d, m4s=%d",
461 caglobals.ip.u6.fd, caglobals.ip.u6s.fd,
462 caglobals.ip.u4.fd, caglobals.ip.u4s.fd,
463 caglobals.ip.m6.fd, caglobals.ip.m6s.fd,
464 caglobals.ip.m4.fd, caglobals.ip.m4s.fd);
466 // create pipe for fast shutdown
468 CHECKFD(caglobals.ip.shutdownFds[0]);
469 CHECKFD(caglobals.ip.shutdownFds[1]);
471 // create source of network interface change notifications
472 CAInitializeNetlink();
476 caglobals.ip.terminate = false;
477 res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL);
478 if (CA_STATUS_OK != res)
480 OIC_LOG(ERROR, TAG, "thread_pool_add_task failed");
483 OIC_LOG(DEBUG, TAG, "CAReceiveHandler thread started successfully.");
485 caglobals.ip.started = true;
489 void CAIPStopServer()
491 OIC_LOG(DEBUG, TAG, "IN");
493 caglobals.ip.terminate = true;
495 if (caglobals.ip.shutdownFds[1] != -1)
497 close(caglobals.ip.shutdownFds[1]);
498 // receive thread will stop immediately
502 // receive thread will stop in SELECT_TIMEOUT seconds.
505 OIC_LOG(DEBUG, TAG, "OUT");
508 static void applyMulticastToInterface4(struct in_addr inaddr)
510 if (!caglobals.ip.ipv4enabled)
515 struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress,
516 .imr_interface = inaddr};
517 if (setsockopt(caglobals.ip.m4.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
519 if (EADDRINUSE != errno)
521 OIC_LOG_V(ERROR, TAG, "IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
524 if (setsockopt(caglobals.ip.m4s.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
526 if (EADDRINUSE != errno)
528 OIC_LOG_V(ERROR, TAG, "secure IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
533 static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t interface)
535 struct ipv6_mreq mreq;
536 mreq.ipv6mr_multiaddr = *addr;
537 mreq.ipv6mr_interface = interface;
538 if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
540 if (EADDRINUSE != errno)
542 OIC_LOG_V(ERROR, TAG, "IPv6 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
547 static void applyMulticastToInterface6(uint32_t interface)
549 if (!caglobals.ip.ipv6enabled)
553 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressInt, interface);
554 applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressLnk, interface);
555 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressRlm, interface);
556 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressAdm, interface);
557 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressSit, interface);
558 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressOrg, interface);
559 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressGlb, interface);
560 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressInt, interface);
561 applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressLnk, interface);
562 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressRlm, interface);
563 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressAdm, interface);
564 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressSit, interface);
565 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressOrg, interface);
566 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressGlb, interface);
569 static void CAApplyInterfaces()
571 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
574 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
578 uint32_t len = u_arraylist_length(iflist);
579 OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
581 for (uint32_t i = 0; i < len; i++)
583 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
589 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
593 if (ifitem->family == AF_INET)
595 struct in_addr inaddr;
596 inaddr.s_addr = ifitem->ipv4addr;
597 applyMulticastToInterface4(inaddr);
598 OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s", ifitem->name);
600 if (ifitem->family == AF_INET6)
602 applyMulticastToInterface6(ifitem->index);
603 OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
607 u_arraylist_destroy(iflist);
610 static void CAHandleNetlink()
615 struct sockaddr_nl sa;
616 struct iovec iov = { buf, sizeof(buf) };
617 struct msghdr msg = { (void *)&sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
619 int len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
620 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
622 if (nh->nlmsg_type == RTM_NEWLINK)
624 struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
625 if ((ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
630 int newIndex = ifi->ifi_index;
632 u_arraylist_t *iflist = CAIPGetInterfaceInformation(newIndex);
635 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
639 uint32_t len = u_arraylist_length(iflist);
640 for (uint32_t i = 0; i < len; i++)
642 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
648 if ((int)ifitem->index != newIndex)
653 applyMulticastToInterface6(newIndex);
654 struct in_addr inaddr;
655 inaddr.s_addr = ifitem->ipv4addr;
656 applyMulticastToInterface4(inaddr);
657 break; // we found the one we were looking for
659 u_arraylist_destroy(iflist);
665 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
667 OIC_LOG(DEBUG, TAG, "IN");
669 g_packetReceivedCallback = callback;
671 OIC_LOG(DEBUG, TAG, "OUT");
674 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
676 OIC_LOG(DEBUG, TAG, "IN");
678 g_exceptionCallback = callback;
680 OIC_LOG(DEBUG, TAG, "OUT");
683 static void sendData(int fd, const CAEndpoint_t *endpoint,
684 const void *data, uint32_t dlen,
685 const char *cast, const char *fam)
687 OIC_LOG(DEBUG, TAG, "IN");
689 char *secure = (endpoint->flags & CA_SECURE) ? "secure " : "";
690 (void)secure; // eliminates release warning
691 struct sockaddr_storage sock;
692 CAConvertNameToAddr(endpoint->addr, endpoint->port, &sock);
694 if (sock.ss_family == AF_INET6)
696 struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)&sock;
697 if (!sock6->sin6_scope_id)
699 sock6->sin6_scope_id = endpoint->interface;
703 ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, sizeof (sock));
706 // If logging is not defined/enabled.
709 OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %s", secure, cast, fam, strerror(errno));
713 OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %d bytes", secure, cast, fam, len);
717 static void sendMulticastData6(const u_arraylist_t *iflist,
718 CAEndpoint_t *endpoint,
719 const void *data, uint32_t datalen)
721 int scope = endpoint->flags & CA_SCOPE_MASK;
722 char *ipv6mcname = ipv6mcnames[scope];
725 OIC_LOG_V(INFO, TAG, "IPv6 multicast scope invalid: %d", scope);
728 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), ipv6mcname);
729 int fd = caglobals.ip.u6.fd;
731 uint32_t len = u_arraylist_length(iflist);
732 for (uint32_t i = 0; i < len; i++)
734 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
739 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
743 if (ifitem->family != AF_INET6)
748 int index = ifitem->index;
749 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index, sizeof (index)))
751 OIC_LOG_V(ERROR, TAG, "setsockopt6 failed: %s", strerror(errno));
754 sendData(fd, endpoint, data, datalen, "multicast", "ipv6");
758 static void sendMulticastData4(const u_arraylist_t *iflist,
759 CAEndpoint_t *endpoint,
760 const void *data, uint32_t datalen)
762 struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress };
763 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
764 int fd = caglobals.ip.u4.fd;
766 uint32_t len = u_arraylist_length(iflist);
767 for (uint32_t i = 0; i < len; i++)
769 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
774 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
778 if (ifitem->family != AF_INET)
783 struct in_addr inaddr;
784 inaddr.s_addr = ifitem->ipv4addr;
785 mreq.imr_interface = inaddr;
786 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof (mreq)))
788 OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)",
791 sendData(fd, endpoint, data, datalen, "multicast", "ipv4");
795 void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
798 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
799 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
801 bool isSecure = (endpoint->flags & CA_SECURE) != 0;
805 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
807 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
810 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
814 if ((endpoint->flags & CA_IPV6) && caglobals.ip.ipv6enabled)
816 sendMulticastData6(iflist, endpoint, data, datalen);
818 if ((endpoint->flags & CA_IPV4) && caglobals.ip.ipv4enabled)
820 sendMulticastData4(iflist, endpoint, data, datalen);
823 u_arraylist_destroy(iflist);
827 if (!endpoint->port) // unicast discovery
829 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
833 if (caglobals.ip.ipv6enabled && (endpoint->flags & CA_IPV6))
835 fd = isSecure ? caglobals.ip.u6s.fd : caglobals.ip.u6.fd;
836 #ifndef __WITH_DTLS__
837 fd = caglobals.ip.u6.fd;
839 sendData(fd, endpoint, data, datalen, "unicast", "ipv6");
841 if (caglobals.ip.ipv4enabled && (endpoint->flags & CA_IPV4))
843 fd = isSecure ? caglobals.ip.u4s.fd : caglobals.ip.u4.fd;
844 #ifndef __WITH_DTLS__
845 fd = caglobals.ip.u4.fd;
847 sendData(fd, endpoint, data, datalen, "unicast", "ipv4");
852 CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
854 OIC_LOG(DEBUG, TAG, "IN");
856 VERIFY_NON_NULL(info, TAG, "info is NULL");
857 VERIFY_NON_NULL(size, TAG, "size is NULL");
859 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
862 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
863 return CA_STATUS_FAILED;
866 uint32_t len = u_arraylist_length(iflist);
868 CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(len, sizeof (CAEndpoint_t));
871 OIC_LOG(ERROR, TAG, "Malloc Failed");
872 u_arraylist_destroy(iflist);
873 return CA_MEMORY_ALLOC_FAILED;
876 for (uint32_t i = 0, j = 0; i < len; i++)
878 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
884 OICStrcpy(eps[j].addr, CA_INTERFACE_NAME_SIZE, ifitem->name);
885 eps[j].flags = ifitem->family == AF_INET6 ? CA_IPV6 : CA_IPV4;
886 eps[j].adapter = CA_ADAPTER_IP;
887 eps[j].interface = 0;
895 u_arraylist_destroy(iflist);
897 OIC_LOG(DEBUG, TAG, "OUT");