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 int len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
622 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
624 if (nh->nlmsg_type == RTM_NEWLINK)
626 struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
627 if ((ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
632 int newIndex = ifi->ifi_index;
634 u_arraylist_t *iflist = CAIPGetInterfaceInformation(newIndex);
637 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
641 uint32_t len = u_arraylist_length(iflist);
642 for (uint32_t i = 0; i < len; i++)
644 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
650 if ((int)ifitem->index != newIndex)
655 applyMulticastToInterface6(newIndex);
656 struct in_addr inaddr;
657 inaddr.s_addr = ifitem->ipv4addr;
658 applyMulticastToInterface4(inaddr);
659 break; // we found the one we were looking for
661 u_arraylist_destroy(iflist);
667 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
669 OIC_LOG(DEBUG, TAG, "IN");
671 g_packetReceivedCallback = callback;
673 OIC_LOG(DEBUG, TAG, "OUT");
676 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
678 OIC_LOG(DEBUG, TAG, "IN");
680 g_exceptionCallback = callback;
682 OIC_LOG(DEBUG, TAG, "OUT");
685 static void sendData(int fd, const CAEndpoint_t *endpoint,
686 const void *data, uint32_t dlen,
687 const char *cast, const char *fam)
689 OIC_LOG(DEBUG, TAG, "IN");
691 char *secure = (endpoint->flags & CA_SECURE) ? "secure " : "";
692 (void)secure; // eliminates release warning
693 struct sockaddr_storage sock;
694 CAConvertNameToAddr(endpoint->addr, endpoint->port, &sock);
696 if (sock.ss_family == AF_INET6)
698 struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)&sock;
699 if (!sock6->sin6_scope_id)
701 sock6->sin6_scope_id = endpoint->interface;
705 ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, sizeof (sock));
708 // If logging is not defined/enabled.
711 OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %s", secure, cast, fam, strerror(errno));
715 OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %d bytes", secure, cast, fam, len);
719 static void sendMulticastData6(const u_arraylist_t *iflist,
720 CAEndpoint_t *endpoint,
721 const void *data, uint32_t datalen)
723 int scope = endpoint->flags & CA_SCOPE_MASK;
724 char *ipv6mcname = ipv6mcnames[scope];
727 OIC_LOG_V(INFO, TAG, "IPv6 multicast scope invalid: %d", scope);
730 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), ipv6mcname);
731 int fd = caglobals.ip.u6.fd;
733 uint32_t len = u_arraylist_length(iflist);
734 for (uint32_t i = 0; i < len; i++)
736 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
741 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
745 if (ifitem->family != AF_INET6)
750 int index = ifitem->index;
751 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index, sizeof (index)))
753 OIC_LOG_V(ERROR, TAG, "setsockopt6 failed: %s", strerror(errno));
756 sendData(fd, endpoint, data, datalen, "multicast", "ipv6");
760 static void sendMulticastData4(const u_arraylist_t *iflist,
761 CAEndpoint_t *endpoint,
762 const void *data, uint32_t datalen)
764 struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress };
765 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
766 int fd = caglobals.ip.u4.fd;
768 uint32_t len = u_arraylist_length(iflist);
769 for (uint32_t i = 0; i < len; i++)
771 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
776 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
780 if (ifitem->family != AF_INET)
785 struct in_addr inaddr;
786 inaddr.s_addr = ifitem->ipv4addr;
787 mreq.imr_interface = inaddr;
788 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof (mreq)))
790 OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)",
793 sendData(fd, endpoint, data, datalen, "multicast", "ipv4");
797 void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
800 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
801 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
803 bool isSecure = (endpoint->flags & CA_SECURE) != 0;
807 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
809 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
812 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
816 if ((endpoint->flags & CA_IPV6) && caglobals.ip.ipv6enabled)
818 sendMulticastData6(iflist, endpoint, data, datalen);
820 if ((endpoint->flags & CA_IPV4) && caglobals.ip.ipv4enabled)
822 sendMulticastData4(iflist, endpoint, data, datalen);
825 u_arraylist_destroy(iflist);
829 if (!endpoint->port) // unicast discovery
831 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
835 if (caglobals.ip.ipv6enabled && (endpoint->flags & CA_IPV6))
837 fd = isSecure ? caglobals.ip.u6s.fd : caglobals.ip.u6.fd;
838 #ifndef __WITH_DTLS__
839 fd = caglobals.ip.u6.fd;
841 sendData(fd, endpoint, data, datalen, "unicast", "ipv6");
843 if (caglobals.ip.ipv4enabled && (endpoint->flags & CA_IPV4))
845 fd = isSecure ? caglobals.ip.u4s.fd : caglobals.ip.u4.fd;
846 #ifndef __WITH_DTLS__
847 fd = caglobals.ip.u4.fd;
849 sendData(fd, endpoint, data, datalen, "unicast", "ipv4");
854 CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
856 OIC_LOG(DEBUG, TAG, "IN");
858 VERIFY_NON_NULL(info, TAG, "info is NULL");
859 VERIFY_NON_NULL(size, TAG, "size is NULL");
861 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
864 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
865 return CA_STATUS_FAILED;
868 uint32_t len = u_arraylist_length(iflist);
870 CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(len, sizeof (CAEndpoint_t));
873 OIC_LOG(ERROR, TAG, "Malloc Failed");
874 u_arraylist_destroy(iflist);
875 return CA_MEMORY_ALLOC_FAILED;
878 for (uint32_t i = 0, j = 0; i < len; i++)
880 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
886 OICStrcpy(eps[j].addr, CA_INTERFACE_NAME_SIZE, ifitem->name);
887 eps[j].flags = ifitem->family == AF_INET6 ? CA_IPV6 : CA_IPV4;
888 eps[j].adapter = CA_ADAPTER_IP;
889 eps[j].interface = 0;
897 u_arraylist_destroy(iflist);
899 OIC_LOG(DEBUG, TAG, "OUT");