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"
23 #include <sys/types.h>
24 #include <sys/socket.h>
27 #include <sys/types.h>
29 #include <sys/select.h>
30 #include <arpa/inet.h>
31 #include <netinet/in.h>
35 #include <linux/netlink.h>
36 #include <linux/rtnetlink.h>
40 #include "caadapterutils.h"
42 #include "caadapternetdtls.h"
45 #include "oic_malloc.h"
46 #include "oic_string.h"
50 * @brief Logging tag for module name
52 #define TAG "IP_SERVER"
54 #define SELECT_TIMEOUT 1 // select() seconds (and termination latency)
56 #define IPv4_MULTICAST "224.0.1.187"
57 static struct in_addr IPv4MulticastAddress = { 0 };
59 #define IPv6_DOMAINS 16
60 #define IPv6_MULTICAST_INT "ff01::fd"
61 static struct in6_addr IPv6MulticastAddressInt;
62 #define IPv6_MULTICAST_LNK "ff02::fd"
63 static struct in6_addr IPv6MulticastAddressLnk;
64 #define IPv6_MULTICAST_RLM "ff03::fd"
65 static struct in6_addr IPv6MulticastAddressRlm;
66 #define IPv6_MULTICAST_ADM "ff04::fd"
67 static struct in6_addr IPv6MulticastAddressAdm;
68 #define IPv6_MULTICAST_SIT "ff05::fd"
69 static struct in6_addr IPv6MulticastAddressSit;
70 #define IPv6_MULTICAST_ORG "ff08::fd"
71 static struct in6_addr IPv6MulticastAddressOrg;
72 #define IPv6_MULTICAST_GLB "ff0e::fd"
73 static struct in6_addr IPv6MulticastAddressGlb;
75 static char *ipv6mcnames[IPv6_DOMAINS] = {
94 static CAIPExceptionCallback g_exceptionCallback;
96 static CAIPPacketReceivedCallback g_packetReceivedCallback;
98 static void CAHandleNetlink();
99 static void CAApplyInterfaces();
100 static void CAFindReadyMessage();
101 static void CASelectReturned(fd_set *readFds, int ret);
102 static void CAProcessNewInterface(CAInterface_t *ifchanged);
103 static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags);
105 #define SET(TYPE, FDS) \
106 if (caglobals.ip.TYPE.fd != -1) \
108 FD_SET(caglobals.ip.TYPE.fd, FDS); \
111 #define ISSET(TYPE, FDS, FLAGS) \
112 if (caglobals.ip.TYPE.fd != -1 && FD_ISSET(caglobals.ip.TYPE.fd, FDS)) \
114 fd = caglobals.ip.TYPE.fd; \
118 static void CAReceiveHandler(void *data)
121 OIC_LOG(DEBUG, TAG, "IN");
123 while (!caglobals.ip.terminate)
125 CAFindReadyMessage();
128 OIC_LOG(DEBUG, TAG, "OUT");
131 static void CAFindReadyMessage()
134 struct timeval timeout;
136 timeout.tv_sec = caglobals.ip.selectTimeout;
138 struct timeval *tv = caglobals.ip.selectTimeout == -1 ? NULL : &timeout;
149 if (caglobals.ip.shutdownFds[0] != -1)
151 FD_SET(caglobals.ip.shutdownFds[0], &readFds);
153 if (caglobals.ip.netlinkFd != -1)
155 FD_SET(caglobals.ip.netlinkFd, &readFds);
158 int ret = select(caglobals.ip.maxfd + 1, &readFds, NULL, NULL, tv);
160 if (caglobals.ip.terminate)
162 OIC_LOG_V(DEBUG, TAG, "Packet receiver Stop request received.");
169 OIC_LOG_V(FATAL, TAG, "select error %s", strerror(errno));
174 CASelectReturned(&readFds, ret);
177 static void CASelectReturned(fd_set *readFds, int ret)
181 CATransportFlags_t flags = CA_DEFAULT_FLAGS;
183 while (!caglobals.ip.terminate)
185 ISSET(u6, readFds, CA_IPV6)
186 else ISSET(u6s, readFds, CA_IPV6 | CA_SECURE)
187 else ISSET(u4, readFds, CA_IPV4)
188 else ISSET(u4s, readFds, CA_IPV4 | CA_SECURE)
189 else ISSET(m6, readFds, CA_MULTICAST | CA_IPV6)
190 else ISSET(m6s, readFds, CA_MULTICAST | CA_IPV6 | CA_SECURE)
191 else ISSET(m4, readFds, CA_MULTICAST | CA_IPV4)
192 else ISSET(m4s, readFds, CA_MULTICAST | CA_IPV4 | CA_SECURE)
193 else if (FD_ISSET(caglobals.ip.netlinkFd, readFds))
200 CAInterface_t *ifchanged = CAFindInterfaceChange();
203 CAProcessNewInterface(ifchanged);
208 (void)CAReceiveMessage(fd, flags);
213 static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags)
215 char recvBuffer[COAP_MAX_PDU_SIZE];
217 struct sockaddr_storage srcAddr;
218 socklen_t srcAddrLen = sizeof (srcAddr);
220 ssize_t recvLen = recvfrom(fd,
224 (struct sockaddr *)&srcAddr,
228 OIC_LOG_V(ERROR, TAG, "Recvfrom failed %s", strerror(errno));
229 return CA_STATUS_FAILED;
232 CASecureEndpoint_t sep =
233 {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = flags}};
237 sep.endpoint.interface = ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id;
238 ((struct sockaddr_in6 *)&srcAddr)->sin6_scope_id = 0;
240 CAConvertAddrToName(&srcAddr, sep.endpoint.addr, &sep.endpoint.port);
242 if (flags & CA_SECURE)
245 int ret = CAAdapterNetDtlsDecrypt(&sep, (uint8_t *)recvBuffer, recvLen);
246 OIC_LOG_V(DEBUG, TAG, "CAAdapterNetDtlsDecrypt returns [%d]", ret);
248 OIC_LOG(ERROR, TAG, "Encrypted message but no DTLS");
253 if (g_packetReceivedCallback)
255 g_packetReceivedCallback(&sep, recvBuffer, recvLen);
264 OIC_LOG(DEBUG, TAG, "IN");
265 OIC_LOG(DEBUG, TAG, "OUT");
268 static int CACreateSocket(int family, uint16_t *port)
270 int socktype = SOCK_DGRAM;
272 socktype |= SOCK_CLOEXEC;
274 int fd = socket(family, socktype, IPPROTO_UDP);
277 OIC_LOG_V(ERROR, TAG, "create socket failed: %s", strerror(errno));
282 int fl = fcntl(fd, F_GETFD);
283 if (-1 == fl || -1 == fcntl(fd, F_SETFD, fl|FD_CLOEXEC))
285 OIC_LOG_V(ERROR, TAG, "set FD_CLOEXEC failed: %s", strerror(errno));
291 struct sockaddr_storage sa = { .ss_family = family };
294 if (family == AF_INET6)
297 if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
299 OIC_LOG_V(ERROR, TAG, "IPV6_V6ONLY failed: %s", strerror(errno));
301 ((struct sockaddr_in6 *)&sa)->sin6_port = htons(*port);
302 socklen = sizeof (struct sockaddr_in6);
306 ((struct sockaddr_in *)&sa)->sin_port = htons(*port);
307 socklen = sizeof (struct sockaddr_in);
310 if (*port) // use the given port
313 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)))
315 OIC_LOG_V(ERROR, TAG, "SO_REUSEADDR failed: %s", strerror(errno));
321 if (-1 == bind(fd, (struct sockaddr *)&sa, socklen))
323 OIC_LOG_V(ERROR, TAG, "bind socket failed: %s", strerror(errno));
328 if (!*port) // return the assigned port
330 if (-1 == getsockname(fd, (struct sockaddr *)&sa, &socklen))
332 OIC_LOG_V(ERROR, TAG, "getsockname failed: %s", strerror(errno));
336 *port = ntohs(family == AF_INET6 ?
337 ((struct sockaddr_in6 *)&sa)->sin6_port :
338 ((struct sockaddr_in *)&sa)->sin_port);
344 #define CHECKFD(FD) \
345 if (FD > caglobals.ip.maxfd) \
346 caglobals.ip.maxfd = FD;
347 #define NEWSOCKET(FAMILY, NAME) \
348 caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port); \
349 CHECKFD(caglobals.ip.NAME.fd)
351 static void CAInitializeNetlink()
354 // create NETLINK fd for interface change notifications
355 struct sockaddr_nl sa = { AF_NETLINK, 0, 0, RTMGRP_LINK };
357 caglobals.ip.netlinkFd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE);
358 if (caglobals.ip.netlinkFd == -1)
360 OIC_LOG_V(ERROR, TAG, "netlink socket failed: %s", strerror(errno));
364 int r = bind(caglobals.ip.netlinkFd, (struct sockaddr *)&sa, sizeof (sa));
367 OIC_LOG_V(ERROR, TAG, "netlink bind failed: %s", strerror(errno));
368 close(caglobals.ip.netlinkFd);
369 caglobals.ip.netlinkFd = -1;
373 CHECKFD(caglobals.ip.netlinkFd);
379 static void CAInitializePipe()
381 caglobals.ip.selectTimeout = -1;
383 int ret = pipe2(caglobals.ip.shutdownFds, O_CLOEXEC);
385 int ret = pipe(caglobals.ip.shutdownFds);
388 ret = fcntl(caglobals.ip.shutdownFds[0], F_GETFD);
391 ret = fcntl(caglobals.ip.shutdownFds[0], F_SETFD, ret|FD_CLOEXEC);
395 ret = fcntl(caglobals.ip.shutdownFds[1], F_GETFD);
399 ret = fcntl(caglobals.ip.shutdownFds[1], F_SETFD, ret|FD_CLOEXEC);
403 close(caglobals.ip.shutdownFds[1]);
404 close(caglobals.ip.shutdownFds[0]);
405 caglobals.ip.shutdownFds[0] = -1;
406 caglobals.ip.shutdownFds[1] = -1;
412 OIC_LOG_V(ERROR, TAG, "pipe failed: %s", strerror(errno));
413 caglobals.ip.selectTimeout = SELECT_TIMEOUT; //poll needed for shutdown
417 CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
419 CAResult_t res = CA_STATUS_OK;
421 if (caglobals.ip.started)
426 if (!IPv4MulticastAddress.s_addr)
428 (void)inet_aton(IPv4_MULTICAST, &IPv4MulticastAddress);
429 (void)inet_pton(AF_INET6, IPv6_MULTICAST_INT, &IPv6MulticastAddressInt);
430 (void)inet_pton(AF_INET6, IPv6_MULTICAST_LNK, &IPv6MulticastAddressLnk);
431 (void)inet_pton(AF_INET6, IPv6_MULTICAST_RLM, &IPv6MulticastAddressRlm);
432 (void)inet_pton(AF_INET6, IPv6_MULTICAST_ADM, &IPv6MulticastAddressAdm);
433 (void)inet_pton(AF_INET6, IPv6_MULTICAST_SIT, &IPv6MulticastAddressSit);
434 (void)inet_pton(AF_INET6, IPv6_MULTICAST_ORG, &IPv6MulticastAddressOrg);
435 (void)inet_pton(AF_INET6, IPv6_MULTICAST_GLB, &IPv6MulticastAddressGlb);
438 if (!caglobals.ip.ipv6enabled && !caglobals.ip.ipv4enabled)
440 caglobals.ip.ipv4enabled = true; // only needed to run CA tests
443 if (caglobals.ip.ipv6enabled)
445 NEWSOCKET(AF_INET6, u6)
446 NEWSOCKET(AF_INET6, u6s)
447 NEWSOCKET(AF_INET6, m6)
448 NEWSOCKET(AF_INET6, m6s)
449 OIC_LOG_V(INFO, TAG, "IPv6 unicast port: %u", caglobals.ip.u6.port);
451 if (caglobals.ip.ipv4enabled)
453 NEWSOCKET(AF_INET, u4)
454 NEWSOCKET(AF_INET, u4s)
455 NEWSOCKET(AF_INET, m4)
456 NEWSOCKET(AF_INET, m4s)
457 OIC_LOG_V(INFO, TAG, "IPv4 unicast port: %u", caglobals.ip.u4.port);
460 OIC_LOG_V(DEBUG, TAG,
461 "socket summary: u6=%d, u6s=%d, u4=%d, u4s=%d, m6=%d, m6s=%d, m4=%d, m4s=%d",
462 caglobals.ip.u6.fd, caglobals.ip.u6s.fd,
463 caglobals.ip.u4.fd, caglobals.ip.u4s.fd,
464 caglobals.ip.m6.fd, caglobals.ip.m6s.fd,
465 caglobals.ip.m4.fd, caglobals.ip.m4s.fd);
467 // create pipe for fast shutdown
469 CHECKFD(caglobals.ip.shutdownFds[0]);
470 CHECKFD(caglobals.ip.shutdownFds[1]);
472 // create source of network interface change notifications
473 CAInitializeNetlink();
475 caglobals.ip.selectTimeout = CAGetPollingInterval(caglobals.ip.selectTimeout);
479 caglobals.ip.terminate = false;
480 res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL);
481 if (CA_STATUS_OK != res)
483 OIC_LOG(ERROR, TAG, "thread_pool_add_task failed");
486 OIC_LOG(DEBUG, TAG, "CAReceiveHandler thread started successfully.");
488 caglobals.ip.started = true;
492 void CAIPStopServer()
494 OIC_LOG(DEBUG, TAG, "IN");
496 caglobals.ip.terminate = true;
498 if (caglobals.ip.shutdownFds[1] != -1)
500 close(caglobals.ip.shutdownFds[1]);
501 // receive thread will stop immediately
505 // receive thread will stop in SELECT_TIMEOUT seconds.
508 OIC_LOG(DEBUG, TAG, "OUT");
511 void CAWakeUpForChange()
513 if (caglobals.ip.shutdownFds[1] != -1)
518 len = write(caglobals.ip.shutdownFds[1], "w", 1);
519 } while ((len == -1) && (errno == EINTR));
520 if ((len == -1) && (errno != EINTR) && (errno != EPIPE))
522 OIC_LOG_V(DEBUG, TAG, "write failed: %s", strerror(errno));
527 static void applyMulticastToInterface4(struct in_addr inaddr)
529 if (!caglobals.ip.ipv4enabled)
534 struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress,
535 .imr_interface = inaddr};
536 if (setsockopt(caglobals.ip.m4.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
538 if (EADDRINUSE != errno)
540 OIC_LOG_V(ERROR, TAG, "IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
543 if (setsockopt(caglobals.ip.m4s.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)))
545 if (EADDRINUSE != errno)
547 OIC_LOG_V(ERROR, TAG, "secure IPv4 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
552 static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t interface)
554 struct ipv6_mreq mreq;
555 mreq.ipv6mr_multiaddr = *addr;
556 mreq.ipv6mr_interface = interface;
557 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof (mreq)))
559 if (EADDRINUSE != errno)
561 OIC_LOG_V(ERROR, TAG, "IPv6 IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
566 static void applyMulticastToInterface6(uint32_t interface)
568 if (!caglobals.ip.ipv6enabled)
572 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressInt, interface);
573 applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressLnk, interface);
574 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressRlm, interface);
575 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressAdm, interface);
576 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressSit, interface);
577 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressOrg, interface);
578 //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressGlb, interface);
579 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressInt, interface);
580 applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressLnk, interface);
581 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressRlm, interface);
582 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressAdm, interface);
583 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressSit, interface);
584 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressOrg, interface);
585 //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressGlb, interface);
588 static void CAApplyInterfaces()
590 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
593 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
597 uint32_t len = u_arraylist_length(iflist);
598 OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
600 for (uint32_t i = 0; i < len; i++)
602 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
608 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
612 if (ifitem->family == AF_INET)
614 struct in_addr inaddr;
615 inaddr.s_addr = ifitem->ipv4addr;
616 applyMulticastToInterface4(inaddr);
617 OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s", ifitem->name);
619 if (ifitem->family == AF_INET6)
621 applyMulticastToInterface6(ifitem->index);
622 OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
626 u_arraylist_destroy(iflist);
629 static void CAProcessNewInterface(CAInterface_t *ifitem)
631 applyMulticastToInterface6(ifitem->index);
632 struct in_addr inaddr;
633 inaddr.s_addr = ifitem->ipv4addr;
634 applyMulticastToInterface4(inaddr);
636 static void CAHandleNetlink()
641 struct sockaddr_nl sa;
642 struct iovec iov = { buf, sizeof (buf) };
643 struct msghdr msg = { (void *)&sa, sizeof (sa), &iov, 1, NULL, 0, 0 };
645 size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
647 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
649 if (nh->nlmsg_type != RTM_NEWLINK)
654 struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
655 if (!ifi || (ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
660 int newIndex = ifi->ifi_index;
662 u_arraylist_t *iflist = CAIPGetInterfaceInformation(newIndex);
665 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
669 uint32_t listLength = u_arraylist_length(iflist);
670 for (uint32_t i = 0; i < listLength; i++)
672 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
678 if ((int)ifitem->index != newIndex)
683 CAProcessNewInterface(ifitem);
684 break; // we found the one we were looking for
686 u_arraylist_destroy(iflist);
691 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
693 OIC_LOG(DEBUG, TAG, "IN");
695 g_packetReceivedCallback = callback;
697 OIC_LOG(DEBUG, TAG, "OUT");
700 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
702 OIC_LOG(DEBUG, TAG, "IN");
704 g_exceptionCallback = callback;
706 OIC_LOG(DEBUG, TAG, "OUT");
709 static void sendData(int fd, const CAEndpoint_t *endpoint,
710 const void *data, uint32_t dlen,
711 const char *cast, const char *fam)
713 OIC_LOG(DEBUG, TAG, "IN");
715 char *secure = (endpoint->flags & CA_SECURE) ? "secure " : "";
716 (void)secure; // eliminates release warning
717 struct sockaddr_storage sock;
718 CAConvertNameToAddr(endpoint->addr, endpoint->port, &sock);
721 if (sock.ss_family == AF_INET6)
723 struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)&sock;
724 if (!sock6->sin6_scope_id)
726 sock6->sin6_scope_id = endpoint->interface;
728 socklen = sizeof(struct sockaddr_in6);
732 socklen = sizeof(struct sockaddr_in);
735 ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, socklen);
738 // If logging is not defined/enabled.
741 OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %s", secure, cast, fam, strerror(errno));
745 OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %d bytes", secure, cast, fam, len);
749 static void sendMulticastData6(const u_arraylist_t *iflist,
750 CAEndpoint_t *endpoint,
751 const void *data, uint32_t datalen)
753 int scope = endpoint->flags & CA_SCOPE_MASK;
754 char *ipv6mcname = ipv6mcnames[scope];
757 OIC_LOG_V(INFO, TAG, "IPv6 multicast scope invalid: %d", scope);
760 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), ipv6mcname);
761 int fd = caglobals.ip.u6.fd;
763 uint32_t len = u_arraylist_length(iflist);
764 for (uint32_t i = 0; i < len; i++)
766 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
771 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
775 if (ifitem->family != AF_INET6)
780 int index = ifitem->index;
781 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index, sizeof (index)))
783 OIC_LOG_V(ERROR, TAG, "setsockopt6 failed: %s", strerror(errno));
786 sendData(fd, endpoint, data, datalen, "multicast", "ipv6");
790 static void sendMulticastData4(const u_arraylist_t *iflist,
791 CAEndpoint_t *endpoint,
792 const void *data, uint32_t datalen)
794 struct ip_mreq mreq = { .imr_multiaddr = IPv4MulticastAddress };
795 OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
796 int fd = caglobals.ip.u4.fd;
798 uint32_t len = u_arraylist_length(iflist);
799 for (uint32_t i = 0; i < len; i++)
801 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
806 if ((ifitem->flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
810 if (ifitem->family != AF_INET)
815 struct in_addr inaddr;
816 inaddr.s_addr = ifitem->ipv4addr;
817 mreq.imr_interface = inaddr;
818 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof (mreq)))
820 OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)",
823 sendData(fd, endpoint, data, datalen, "multicast", "ipv4");
827 void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
830 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
831 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
833 bool isSecure = (endpoint->flags & CA_SECURE) != 0;
837 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
839 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
842 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
846 if ((endpoint->flags & CA_IPV6) && caglobals.ip.ipv6enabled)
848 sendMulticastData6(iflist, endpoint, data, datalen);
850 if ((endpoint->flags & CA_IPV4) && caglobals.ip.ipv4enabled)
852 sendMulticastData4(iflist, endpoint, data, datalen);
855 u_arraylist_destroy(iflist);
859 if (!endpoint->port) // unicast discovery
861 endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
865 if (caglobals.ip.ipv6enabled && (endpoint->flags & CA_IPV6))
867 fd = isSecure ? caglobals.ip.u6s.fd : caglobals.ip.u6.fd;
868 #ifndef __WITH_DTLS__
869 fd = caglobals.ip.u6.fd;
871 sendData(fd, endpoint, data, datalen, "unicast", "ipv6");
873 if (caglobals.ip.ipv4enabled && (endpoint->flags & CA_IPV4))
875 fd = isSecure ? caglobals.ip.u4s.fd : caglobals.ip.u4.fd;
876 #ifndef __WITH_DTLS__
877 fd = caglobals.ip.u4.fd;
879 sendData(fd, endpoint, data, datalen, "unicast", "ipv4");
884 CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
886 OIC_LOG(DEBUG, TAG, "IN");
888 VERIFY_NON_NULL(info, TAG, "info is NULL");
889 VERIFY_NON_NULL(size, TAG, "size is NULL");
891 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
894 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
895 return CA_STATUS_FAILED;
898 uint32_t len = u_arraylist_length(iflist);
900 CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(len, sizeof (CAEndpoint_t));
903 OIC_LOG(ERROR, TAG, "Malloc Failed");
904 u_arraylist_destroy(iflist);
905 return CA_MEMORY_ALLOC_FAILED;
908 for (uint32_t i = 0, j = 0; i < len; i++)
910 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
916 OICStrcpy(eps[j].addr, CA_INTERFACE_NAME_SIZE, ifitem->name);
917 eps[j].flags = ifitem->family == AF_INET6 ? CA_IPV6 : CA_IPV4;
918 eps[j].adapter = CA_ADAPTER_IP;
919 eps[j].interface = 0;
927 u_arraylist_destroy(iflist);
929 OIC_LOG(DEBUG, TAG, "OUT");