[Win32] Add win32-specific networking APIs
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / caipserver.c
1 /* ****************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #ifndef __APPLE_USE_RFC_3542
22 #define __APPLE_USE_RFC_3542 // for PKTINFO
23 #endif
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE // for in6_pktinfo
26 #endif
27
28 #include <sys/types.h>
29 #if !defined(__msys_nt__)
30 #include <sys/socket.h>
31 #endif
32
33 #if defined(__msys_nt__)
34 #include <winsock2.h>
35 #include <ws2def.h>
36 #include <mswsock.h>
37 #include <ws2tcpip.h>
38 #endif
39
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <sys/types.h>
43 #include <fcntl.h>
44 #if !defined(__msys_nt__)
45 #include <sys/select.h>
46 #include <arpa/inet.h>
47 #include <netinet/in.h>
48 #include <net/if.h>
49 #endif
50 #include <errno.h>
51 #ifdef __linux__
52 #include <linux/netlink.h>
53 #include <linux/rtnetlink.h>
54 #endif
55
56 #include "pdu.h"
57 #include "caipinterface.h"
58 #include "caadapterutils.h"
59 #ifdef __WITH_DTLS__
60 #include "caadapternetdtls.h"
61 #endif
62 #include "camutex.h"
63 #include "oic_malloc.h"
64 #include "oic_string.h"
65
66 #define USE_IP_MREQN
67 #if defined(_WIN32)
68 #undef USE_IP_MREQN
69 #endif
70
71 /*
72  * Logging tag for module name
73  */
74 #define TAG "OIC_CA_IP_SERVER"
75
76 #define SELECT_TIMEOUT 1     // select() seconds (and termination latency)
77
78 #define IPv4_MULTICAST     "224.0.1.187"
79 static struct in_addr IPv4MulticastAddress = { 0 };
80
81 #define IPv6_DOMAINS       16
82 #define IPv6_MULTICAST_INT "ff01::fd"
83 static struct in6_addr IPv6MulticastAddressInt;
84 #define IPv6_MULTICAST_LNK "ff02::fd"
85 static struct in6_addr IPv6MulticastAddressLnk;
86 #define IPv6_MULTICAST_RLM "ff03::fd"
87 static struct in6_addr IPv6MulticastAddressRlm;
88 #define IPv6_MULTICAST_ADM "ff04::fd"
89 static struct in6_addr IPv6MulticastAddressAdm;
90 #define IPv6_MULTICAST_SIT "ff05::fd"
91 static struct in6_addr IPv6MulticastAddressSit;
92 #define IPv6_MULTICAST_ORG "ff08::fd"
93 static struct in6_addr IPv6MulticastAddressOrg;
94 #define IPv6_MULTICAST_GLB "ff0e::fd"
95 static struct in6_addr IPv6MulticastAddressGlb;
96
97 static char *ipv6mcnames[IPv6_DOMAINS] = {
98     NULL,
99     IPv6_MULTICAST_INT,
100     IPv6_MULTICAST_LNK,
101     IPv6_MULTICAST_RLM,
102     IPv6_MULTICAST_ADM,
103     IPv6_MULTICAST_SIT,
104     NULL,
105     NULL,
106     IPv6_MULTICAST_ORG,
107     NULL,
108     NULL,
109     NULL,
110     NULL,
111     NULL,
112     IPv6_MULTICAST_GLB,
113     NULL
114 };
115
116 #if defined (_WIN32)
117 #define IFF_UP_RUNNING_FLAGS  (IFF_UP)
118
119     char* caips_get_error(){
120         static char buffer[32];
121         snprintf(buffer, 32, "%i", WSAGetLastError());
122         return buffer;
123     }
124 #define CAIPS_GET_ERROR \
125     caips_get_error()
126 #else
127 #define IFF_UP_RUNNING_FLAGS  (IFF_UP|IFF_RUNNING)
128
129 #define CAIPS_GET_ERROR \
130     strerror(errno)
131 #endif
132 static CAIPExceptionCallback g_exceptionCallback;
133
134 static CAIPPacketReceivedCallback g_packetReceivedCallback;
135
136 static void CAHandleNetlink();
137 static void CAFindReadyMessage();
138 static void CASelectReturned(fd_set *readFds, int ret);
139 static void CAProcessNewInterface(CAInterface_t *ifchanged);
140 static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags);
141
142 #define SET(TYPE, FDS) \
143     if (caglobals.ip.TYPE.fd != -1) \
144     { \
145         FD_SET(caglobals.ip.TYPE.fd, FDS); \
146     }
147
148 #define ISSET(TYPE, FDS, FLAGS) \
149     if (caglobals.ip.TYPE.fd != -1 && FD_ISSET(caglobals.ip.TYPE.fd, FDS)) \
150     { \
151         fd = caglobals.ip.TYPE.fd; \
152         flags = FLAGS; \
153     }
154
155 #define CLOSE_SOCKET(TYPE) \
156     if (caglobals.ip.TYPE.fd != -1) \
157     { \
158         close(caglobals.ip.TYPE.fd); \
159         caglobals.ip.TYPE.fd = -1; \
160     }
161
162 void CADeInitializeIPGlobals()
163 {
164     CLOSE_SOCKET(u6);
165     CLOSE_SOCKET(u6s);
166     CLOSE_SOCKET(u4);
167     CLOSE_SOCKET(u4s);
168     CLOSE_SOCKET(m6);
169     CLOSE_SOCKET(m6s);
170     CLOSE_SOCKET(m4);
171     CLOSE_SOCKET(m4s);
172
173     if (caglobals.ip.netlinkFd != -1)
174     {
175         close(caglobals.ip.netlinkFd);
176         caglobals.ip.netlinkFd = -1;
177     }
178 }
179
180 static void CAReceiveHandler(void *data)
181 {
182     (void)data;
183
184     while (!caglobals.ip.terminate)
185     {
186         CAFindReadyMessage();
187     }
188 }
189
190 static void CAFindReadyMessage()
191 {
192     fd_set readFds;
193     struct timeval timeout;
194
195     timeout.tv_sec = caglobals.ip.selectTimeout;
196     timeout.tv_usec = 0;
197     struct timeval *tv = caglobals.ip.selectTimeout == -1 ? NULL : &timeout;
198
199     FD_ZERO(&readFds);
200     SET(u6,  &readFds)
201     SET(u6s, &readFds)
202     SET(u4,  &readFds)
203     SET(u4s, &readFds)
204     SET(m6,  &readFds)
205     SET(m6s, &readFds)
206     SET(m4,  &readFds)
207     SET(m4s, &readFds)
208
209 #if !defined(_WIN32)
210     if (caglobals.ip.shutdownFds[0] != -1)
211     {
212         FD_SET(caglobals.ip.shutdownFds[0], &readFds);
213     }
214     if (caglobals.ip.netlinkFd != -1)
215     {
216         FD_SET(caglobals.ip.netlinkFd, &readFds);
217     }
218 #endif
219     int ret = select(caglobals.ip.maxfd + 1, &readFds, NULL, NULL, tv);
220
221     if (caglobals.ip.terminate)
222     {
223         OIC_LOG_V(DEBUG, TAG, "Packet receiver Stop request received.");
224         return;
225     }
226
227     if (ret <= 0)
228     {
229         if (ret < 0)
230         {
231             OIC_LOG_V(FATAL, TAG, "select error %s", CAIPS_GET_ERROR);
232         }
233         return;
234     }
235
236     CASelectReturned(&readFds, ret);
237 }
238
239 static void CASelectReturned(fd_set *readFds, int ret)
240 {
241     (void)ret;
242     int fd = -1;
243     CATransportFlags_t flags = CA_DEFAULT_FLAGS;
244
245     while (!caglobals.ip.terminate)
246     {
247         ISSET(u6,  readFds, CA_IPV6)
248         else ISSET(u6s, readFds, CA_IPV6 | CA_SECURE)
249         else ISSET(u4,  readFds, CA_IPV4)
250         else ISSET(u4s, readFds, CA_IPV4 | CA_SECURE)
251         else ISSET(m6,  readFds, CA_MULTICAST | CA_IPV6)
252         else ISSET(m6s, readFds, CA_MULTICAST | CA_IPV6 | CA_SECURE)
253         else ISSET(m4,  readFds, CA_MULTICAST | CA_IPV4)
254         else ISSET(m4s, readFds, CA_MULTICAST | CA_IPV4 | CA_SECURE)
255 #if !defined(_WIN32)
256         else if (FD_ISSET(caglobals.ip.netlinkFd, readFds))
257         {
258             CAInterface_t *ifchanged = CAFindInterfaceChange();
259             if (ifchanged)
260             {
261                 CAProcessNewInterface(ifchanged);
262                 OICFree(ifchanged);
263             }
264             break;
265         }
266         else if (FD_ISSET(caglobals.ip.shutdownFds[0], readFds))
267         {
268             char buf[10] = {0};
269             ssize_t len = read(caglobals.ip.shutdownFds[0], buf, sizeof (buf));
270             if (-1 == len)
271             {
272                 continue;
273             }
274             break;
275         }
276         else
277         {
278             break;
279         }
280 #else
281         else
282         {
283             break;
284         }
285 #endif
286         (void)CAReceiveMessage(fd, flags);
287         FD_CLR(fd, readFds);
288     }
289 }
290
291 static CAResult_t CAReceiveMessage(int fd, CATransportFlags_t flags)
292 {
293     char recvBuffer[COAP_MAX_PDU_SIZE];
294
295     size_t len;
296     int level, type, namelen;
297     struct sockaddr_storage srcAddr;
298     unsigned char *pktinfo = NULL;
299 #if !defined(WSA_CMSG_DATA)
300     struct cmsghdr *cmp = NULL;
301     struct iovec iov = { .iov_base = recvBuffer, .iov_len = sizeof (recvBuffer) };
302     union control
303     {
304         struct cmsghdr cmsg;
305         unsigned char data[CMSG_SPACE(sizeof (struct in6_pktinfo))];
306     } cmsg;
307
308     if (flags & CA_IPV6)
309     {
310         namelen = sizeof (struct sockaddr_in6);
311         level = IPPROTO_IPV6;
312         type = IPV6_PKTINFO;
313         len = sizeof (struct in6_pktinfo);
314     }
315     else
316     {
317         namelen = sizeof (struct sockaddr_in);
318         level = IPPROTO_IP;
319         type = IP_PKTINFO;
320         len = sizeof (struct in6_pktinfo);
321     }
322
323     struct msghdr msg = { .msg_name = &srcAddr,
324                           .msg_namelen = namelen,
325                           .msg_iov = &iov,
326                           .msg_iovlen = 1,
327                           .msg_control = &cmsg,
328                           .msg_controllen = CMSG_SPACE(len) };
329
330     ssize_t recvLen = recvmsg(fd, &msg, flags);
331     if (-1 == recvLen)
332     {
333         OIC_LOG_V(ERROR, TAG, "Recvfrom failed %s", strerror(errno));
334         return CA_STATUS_FAILED;
335     }
336
337     if (flags & CA_MULTICAST)
338     {
339         for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL; cmp = CMSG_NXTHDR(&msg, cmp))
340         {
341             if (cmp->cmsg_level == level && cmp->cmsg_type == type)
342             {
343                 pktinfo = CMSG_DATA(cmp);
344             }
345         }
346     }
347 #else // if defined(WSA_CMSG_DATA)
348     union control
349     {
350         WSACMSGHDR cmsg;
351         uint8_t data[WSA_CMSG_SPACE(sizeof (IN6_PKTINFO))];
352     } cmsg;
353     memset(&cmsg, 0, sizeof(cmsg));
354
355     if (flags & CA_IPV6)
356     {
357         namelen  = sizeof (struct sockaddr_in6);
358         level = IPPROTO_IPV6;
359         type = IPV6_PKTINFO;
360     }
361     else
362     {
363         namelen = sizeof (struct sockaddr_in);
364         level = IPPROTO_IP;
365         type = IP_PKTINFO;
366     }
367
368     WSABUF iov = {.len = sizeof (recvBuffer), .buf = recvBuffer};
369     WSAMSG msg = {.name = &srcAddr,
370                   .namelen = namelen,
371                   .lpBuffers = &iov,
372                   .dwBufferCount = 1,
373                   .Control = {.buf = cmsg.data, .len = sizeof (cmsg)}
374                  };
375
376     uint32_t recvLen = 0;
377     uint32_t ret = caglobals.ip.wsaRecvMsg(fd, &msg, &recvLen, 0,0);
378     OIC_LOG_V(DEBUG, TAG, "WSARecvMsg recvd %u bytes", recvLen);
379     if (SOCKET_ERROR == ret)
380     {
381         OIC_LOG_V(ERROR, TAG, "WSARecvMsg failed %i", WSAGetLastError());
382     }
383
384     if (flags & CA_MULTICAST)
385     {
386         for (WSACMSGHDR *cmp = WSA_CMSG_FIRSTHDR(&msg); cmp != NULL;
387              cmp = WSA_CMSG_NXTHDR(&msg, cmp))
388         {
389             if (cmp->cmsg_level == level && cmp->cmsg_type == type)
390             {
391                 pktinfo = WSA_CMSG_DATA(cmp);
392             }
393         }
394     }
395 #endif // !defined(WSA_CMSG_DATA)
396     CASecureEndpoint_t sep = {.endpoint = {.adapter = CA_ADAPTER_IP, .flags = flags}};
397
398     if (flags & CA_IPV6)
399     {
400         /** @todo figure out correct usage for ifindex, and sin6_scope_id.*/
401         if ((flags & CA_MULTICAST) && pktinfo)
402         {
403             struct in6_addr *addr = &(((struct in6_pktinfo *)pktinfo)->ipi6_addr);
404             unsigned char topbits = ((unsigned char *)addr)[0];
405             if (topbits != 0xff)
406             {
407                 sep.endpoint.flags &= ~CA_MULTICAST;
408             }
409         }
410     }
411     else
412     {
413         if ((flags & CA_MULTICAST) && pktinfo)
414         {
415             struct in_addr *addr = &((struct in_pktinfo *)pktinfo)->ipi_addr;
416             uint32_t host = ntohl(addr->s_addr);
417             unsigned char topbits = ((unsigned char *)&host)[3];
418             if (topbits < 224 || topbits > 239)
419             {
420                 sep.endpoint.flags &= ~CA_MULTICAST;
421             }
422         }
423     }
424
425     CAConvertAddrToName(&srcAddr, namelen, sep.endpoint.addr, &sep.endpoint.port);
426
427     if (flags & CA_SECURE)
428     {
429 #ifdef __WITH_DTLS__
430         int ret = CAAdapterNetDtlsDecrypt(&sep, (uint8_t *)recvBuffer, recvLen);
431         OIC_LOG_V(DEBUG, TAG, "CAAdapterNetDtlsDecrypt returns [%d]", ret);
432 #else
433         OIC_LOG(ERROR, TAG, "Encrypted message but no DTLS");
434 #endif
435     }
436     else
437     {
438         if (g_packetReceivedCallback)
439         {
440             g_packetReceivedCallback(&sep, recvBuffer, recvLen);
441         }
442     }
443
444     return CA_STATUS_OK;
445
446 }
447
448 void CAIPPullData()
449 {
450     OIC_LOG(DEBUG, TAG, "IN");
451     OIC_LOG(DEBUG, TAG, "OUT");
452 }
453
454 static int CACreateSocket(int family, uint16_t *port)
455 {
456     int socktype = SOCK_DGRAM;
457 #ifdef SOCK_CLOEXEC
458     socktype |= SOCK_CLOEXEC;
459 #endif
460     int fd = socket(family, socktype, IPPROTO_UDP);
461     if (-1 == fd)
462     {
463         OIC_LOG_V(ERROR, TAG, "create socket failed: %s", CAIPS_GET_ERROR);
464         return -1;
465     }
466
467 #if !defined(SOCK_CLOEXEC) && defined(FD_CLOEXEC)
468     int fl = fcntl(fd, F_GETFD);
469     if (-1 == fl || -1 == fcntl(fd, F_SETFD, fl|FD_CLOEXEC))
470     {
471         OIC_LOG_V(ERROR, TAG, "set FD_CLOEXEC failed: %s", strerror(errno));
472         close(fd);
473         return -1;
474     }
475 #endif
476     struct sockaddr_storage sa = { .ss_family = family };
477     socklen_t socklen;
478
479     if (family == AF_INET6)
480     {
481         int on = 1;
482
483         if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
484         {
485             OIC_LOG_V(ERROR, TAG, "IPV6_V6ONLY failed: %s", CAIPS_GET_ERROR);
486         }
487
488         if (*port) // only do this for multicast ports
489         {
490 #if defined(IPV6_RECVPKTINFO)
491             if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof (on)))
492 #else
493             if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &on, sizeof (on)))
494 #endif
495             {
496                 OIC_LOG_V(ERROR, TAG, "IPV6_RECVPKTINFO failed: %s",CAIPS_GET_ERROR);
497             }
498         }
499
500         ((struct sockaddr_in6 *)&sa)->sin6_port = htons(*port);
501         socklen = sizeof (struct sockaddr_in6);
502     }
503     else
504     {
505         if (*port) // only do this for multicast ports
506         {
507             int on = 1;
508             if (-1 == setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &on, sizeof (on)))
509             {
510                 OIC_LOG_V(ERROR, TAG, "IP_PKTINFO failed: %s", CAIPS_GET_ERROR);
511             }
512         }
513
514         ((struct sockaddr_in *)&sa)->sin_port = htons(*port);
515         socklen = sizeof (struct sockaddr_in);
516     }
517
518     if (*port) // use the given port
519     {
520         int on = 1;
521         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)))
522         {
523             OIC_LOG_V(ERROR, TAG, "SO_REUSEADDR failed: %s", CAIPS_GET_ERROR);
524             close(fd);
525             return -1;
526         }
527     }
528
529     if (-1 == bind(fd, (struct sockaddr *)&sa, socklen))
530     {
531         OIC_LOG_V(ERROR, TAG, "bind socket failed: %s", CAIPS_GET_ERROR);
532         close(fd);
533         return -1;
534     }
535
536     if (!*port) // return the assigned port
537     {
538         if (-1 == getsockname(fd, (struct sockaddr *)&sa, &socklen))
539         {
540             OIC_LOG_V(ERROR, TAG, "getsockname failed: %s", CAIPS_GET_ERROR);
541             close(fd);
542             return -1;
543         }
544         *port = ntohs(family == AF_INET6 ?
545                       ((struct sockaddr_in6 *)&sa)->sin6_port :
546                       ((struct sockaddr_in *)&sa)->sin_port);
547     }
548
549     return fd;
550 }
551
552 #define CHECKFD(FD) \
553     if (FD > caglobals.ip.maxfd) \
554         caglobals.ip.maxfd = FD;
555 #define NEWSOCKET(FAMILY, NAME) \
556     caglobals.ip.NAME.fd = CACreateSocket(FAMILY, &caglobals.ip.NAME.port); \
557     CHECKFD(caglobals.ip.NAME.fd)
558
559 static void CAInitializeNetlink()
560 {
561 #ifdef __linux__
562     // create NETLINK fd for interface change notifications
563     struct sockaddr_nl sa = { AF_NETLINK, 0, 0, RTMGRP_LINK };
564
565     caglobals.ip.netlinkFd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE);
566     if (caglobals.ip.netlinkFd == -1)
567     {
568         OIC_LOG_V(ERROR, TAG, "netlink socket failed: %s", strerror(errno));
569     }
570     else
571     {
572         int r = bind(caglobals.ip.netlinkFd, (struct sockaddr *)&sa, sizeof (sa));
573         if (r)
574         {
575             OIC_LOG_V(ERROR, TAG, "netlink bind failed: %s", strerror(errno));
576             close(caglobals.ip.netlinkFd);
577             caglobals.ip.netlinkFd = -1;
578         }
579         else
580         {
581             CHECKFD(caglobals.ip.netlinkFd);
582         }
583     }
584 #endif
585 }
586
587 static void CAInitializePipe()
588 {
589 #if !defined(_WIN32)
590     caglobals.ip.selectTimeout = -1;
591 #ifdef HAVE_PIPE2
592     int ret = pipe2(caglobals.ip.shutdownFds, O_CLOEXEC);
593 #else
594     int ret = pipe(caglobals.ip.shutdownFds);
595     if (-1 != ret)
596     {
597         ret = fcntl(caglobals.ip.shutdownFds[0], F_GETFD);
598         if (-1 != ret)
599         {
600             ret = fcntl(caglobals.ip.shutdownFds[0], F_SETFD, ret|FD_CLOEXEC);
601         }
602         if (-1 != ret)
603         {
604             ret = fcntl(caglobals.ip.shutdownFds[1], F_GETFD);
605         }
606         if (-1 != ret)
607         {
608             ret = fcntl(caglobals.ip.shutdownFds[1], F_SETFD, ret|FD_CLOEXEC);
609         }
610         if (-1 == ret)
611         {
612             close(caglobals.ip.shutdownFds[1]);
613             close(caglobals.ip.shutdownFds[0]);
614             caglobals.ip.shutdownFds[0] = -1;
615             caglobals.ip.shutdownFds[1] = -1;
616         }
617     }
618 #endif
619     if (-1 == ret)
620     {
621         OIC_LOG_V(ERROR, TAG, "pipe failed: %s", strerror(errno));
622         caglobals.ip.selectTimeout = SELECT_TIMEOUT; //poll needed for shutdown
623     }
624 #else
625     /** @todo Refactor to support Windows-specific inter-thread communication code. */
626 #endif
627 }
628
629 CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
630 {
631     CAResult_t res = CA_STATUS_OK;
632
633     if (caglobals.ip.started)
634     {
635         return res;
636     }
637 #if defined (_WIN32)
638     WORD wVersionRequested = MAKEWORD(2, 2);
639     WSADATA wsaData ={.wVersion = 0};
640     int err = WSAStartup(wVersionRequested, &wsaData);
641     if (err != 0)
642     {
643         OIC_LOG_V(ERROR, TAG, "WSAStartup failed: %i", err);
644         return CA_STATUS_FAILED;
645     }
646     OIC_LOG(DEBUG, TAG, "WSAStartup Succeeded");
647 #endif
648     if (!IPv4MulticastAddress.s_addr)
649     {
650         (void)inet_pton(AF_INET, IPv4_MULTICAST, &IPv4MulticastAddress);
651         (void)inet_pton(AF_INET6, IPv6_MULTICAST_INT, &IPv6MulticastAddressInt);
652         (void)inet_pton(AF_INET6, IPv6_MULTICAST_LNK, &IPv6MulticastAddressLnk);
653         (void)inet_pton(AF_INET6, IPv6_MULTICAST_RLM, &IPv6MulticastAddressRlm);
654         (void)inet_pton(AF_INET6, IPv6_MULTICAST_ADM, &IPv6MulticastAddressAdm);
655         (void)inet_pton(AF_INET6, IPv6_MULTICAST_SIT, &IPv6MulticastAddressSit);
656         (void)inet_pton(AF_INET6, IPv6_MULTICAST_ORG, &IPv6MulticastAddressOrg);
657         (void)inet_pton(AF_INET6, IPv6_MULTICAST_GLB, &IPv6MulticastAddressGlb);
658     }
659
660     if (!caglobals.ip.ipv6enabled && !caglobals.ip.ipv4enabled)
661     {
662         caglobals.ip.ipv4enabled = true;  // only needed to run CA tests
663     }
664
665     if (caglobals.ip.ipv6enabled)
666     {
667         NEWSOCKET(AF_INET6, u6)
668         NEWSOCKET(AF_INET6, u6s)
669         NEWSOCKET(AF_INET6, m6)
670         NEWSOCKET(AF_INET6, m6s)
671         OIC_LOG_V(INFO, TAG, "IPv6 unicast port: %u", caglobals.ip.u6.port);
672     }
673     if (caglobals.ip.ipv4enabled)
674     {
675         NEWSOCKET(AF_INET, u4)
676         NEWSOCKET(AF_INET, u4s)
677         NEWSOCKET(AF_INET, m4)
678         NEWSOCKET(AF_INET, m4s)
679         OIC_LOG_V(INFO, TAG, "IPv4 unicast port: %u", caglobals.ip.u4.port);
680     }
681
682     OIC_LOG_V(DEBUG, TAG,
683               "socket summary: u6=%d, u6s=%d, u4=%d, u4s=%d, m6=%d, m6s=%d, m4=%d, m4s=%d",
684               caglobals.ip.u6.fd, caglobals.ip.u6s.fd, caglobals.ip.u4.fd, caglobals.ip.u4s.fd,
685               caglobals.ip.m6.fd, caglobals.ip.m6s.fd, caglobals.ip.m4.fd, caglobals.ip.m4s.fd);
686
687     OIC_LOG_V(DEBUG, TAG,
688               "port summary: u6 port=%d, u6s port=%d, u4 port=%d, u4s port=%d, m6 port=%d,"
689               "m6s port=%d, m4 port=%d, m4s port=%d",
690               caglobals.ip.u6.port, caglobals.ip.u6s.port, caglobals.ip.u4.port,
691               caglobals.ip.u4s.port, caglobals.ip.m6.port, caglobals.ip.m6s.port,
692               caglobals.ip.m4.port, caglobals.ip.m4s.port);
693 #if defined (_WIN32)
694     caglobals.ip.wsaRecvMsg = NULL;
695     GUID GuidWSARecvMsg = WSAID_WSARECVMSG;
696     DWORD copied = 0;
697     err = WSAIoctl(caglobals.ip.u4.fd, SIO_GET_EXTENSION_FUNCTION_POINTER, &GuidWSARecvMsg, sizeof(GuidWSARecvMsg), &(caglobals.ip.wsaRecvMsg), sizeof(caglobals.ip.wsaRecvMsg), &copied, 0, 0);
698     if (0 != err)
699     {
700         OIC_LOG_V(ERROR, TAG, "WSAIoctl failed %i", WSAGetLastError());
701         return CA_STATUS_FAILED;
702     }
703 #endif
704     // create pipe for fast shutdown
705     CAInitializePipe();
706     CHECKFD(caglobals.ip.shutdownFds[0]);
707     CHECKFD(caglobals.ip.shutdownFds[1]);
708
709     // create source of network interface change notifications
710     CAInitializeNetlink();
711
712     caglobals.ip.selectTimeout = CAGetPollingInterval(caglobals.ip.selectTimeout);
713
714     res = CAIPStartListenServer();
715     if (CA_STATUS_OK != res)
716     {
717         OIC_LOG_V(ERROR, TAG, "Failed to start listening server![%d]", res);
718         return res;
719     }
720
721     caglobals.ip.terminate = false;
722     res = ca_thread_pool_add_task(threadPool, CAReceiveHandler, NULL);
723     if (CA_STATUS_OK != res)
724     {
725         OIC_LOG(ERROR, TAG, "thread_pool_add_task failed");
726         return res;
727     }
728     OIC_LOG(DEBUG, TAG, "CAReceiveHandler thread started successfully.");
729
730     caglobals.ip.started = true;
731     return CA_STATUS_OK;
732 }
733
734 void CAIPStopServer()
735 {
736     caglobals.ip.started = false;
737     caglobals.ip.terminate = true;
738 #if !defined(_WIN32)
739
740     if (caglobals.ip.shutdownFds[1] != -1)
741     {
742         close(caglobals.ip.shutdownFds[1]);
743         // receive thread will stop immediately
744     }
745     else
746     {
747         // receive thread will stop in SELECT_TIMEOUT seconds.
748     }
749 #else
750     /** @todo Refactor to support Windows-specific inter-thread communication code. */
751 #endif
752 }
753
754 void CAWakeUpForChange()
755 {
756 #if !defined(_WIN32)
757     if (caglobals.ip.shutdownFds[1] != -1)
758     {
759         ssize_t len = 0;
760         do
761         {
762             len = write(caglobals.ip.shutdownFds[1], "w", 1);
763         } while ((len == -1) && (errno == EINTR));
764         if ((len == -1) && (errno != EINTR) && (errno != EPIPE))
765         {
766             OIC_LOG_V(DEBUG, TAG, "write failed: %s", strerror(errno));
767         }
768     }
769 #else
770     /** @todo Refactor to support Windows-specific inter-thread communication code. */
771 #endif
772 }
773
774 static void applyMulticastToInterface4(uint32_t ifindex)
775 {
776     if (!caglobals.ip.ipv4enabled)
777     {
778         return;
779     }
780
781 #if defined(USE_IP_MREQN)
782     struct ip_mreqn mreq = { .imr_multiaddr = IPv4MulticastAddress,
783                              .imr_address.s_addr = htonl(INADDR_ANY),
784                              .imr_ifindex = ifindex };
785 #else
786     struct ip_mreq mreq  = { .imr_multiaddr = IPv4MulticastAddress,
787                              .imr_interface.s_addr = htonl(ifindex) };
788 #endif
789
790     int ret = setsockopt(caglobals.ip.m4.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq));
791     if (-1 == ret)
792     {
793 #if !defined(WSAEINVAL)
794         if (EADDRINUSE != errno)
795 #else
796         if (WSAEINVAL != WSAGetLastError()) // Joining multicast group more than once (IPv4 Flavor)
797 #endif
798         {
799             OIC_LOG_V(ERROR, TAG, "       IPv4 IP_ADD_MEMBERSHIP failed: %s", CAIPS_GET_ERROR);
800         }
801     }
802     ret = setsockopt(caglobals.ip.m4s.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq));
803     if (-1 == ret)
804     {
805 #if !defined(WSAEINVAL)
806         if (EADDRINUSE != errno)
807 #else
808         if (WSAEINVAL != WSAGetLastError()) // Joining multicast group more than once (IPv4 Flavor)
809 #endif
810         {
811             OIC_LOG_V(ERROR, TAG, "SECURE IPv4 IP_ADD_MEMBERSHIP failed: %s", CAIPS_GET_ERROR);
812         }
813     }
814 }
815
816 static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t ifindex)
817 {
818     struct ipv6_mreq mreq = {.ipv6mr_multiaddr = *addr,
819                              .ipv6mr_interface = ifindex };
820     int ret = setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof (mreq));
821     if (-1 == ret)
822     {
823 #if !defined(_WIN32)
824                 if (EADDRINUSE != errno)
825 #else
826                 if (WSAEINVAL != WSAGetLastError()) // Joining multicast group more than once (IPv6 Flavor)
827 #endif
828         {
829             OIC_LOG_V(ERROR, TAG, "IPv6 IPV6_JOIN_GROUP failed: %s", CAIPS_GET_ERROR);
830         }
831     }
832 }
833
834 static void applyMulticastToInterface6(uint32_t ifindex)
835 {
836     if (!caglobals.ip.ipv6enabled)
837     {
838         return;
839     }
840     //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressInt, ifindex);
841     applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressLnk, ifindex);
842     //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressRlm, ifindex);
843     //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressAdm, ifindex);
844     //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressSit, ifindex);
845     //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressOrg, ifindex);
846     //applyMulticast6(caglobals.ip.m6.fd, &IPv6MulticastAddressGlb, ifindex);
847
848     //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressInt, ifindex);
849     applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressLnk, ifindex);
850     //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressRlm, ifindex);
851     //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressAdm, ifindex);
852     //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressSit, ifindex);
853     //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressOrg, ifindex);
854     //applyMulticast6(caglobals.ip.m6s.fd, &IPv6MulticastAddressGlb, ifindex);
855 }
856
857 CAResult_t CAIPStartListenServer()
858 {
859     u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
860     if (!iflist)
861     {
862         OIC_LOG_V(ERROR, TAG, "CAIPGetInterfaceInformation() failed: %s", strerror(errno));
863         return CA_STATUS_FAILED;
864     }
865
866     uint32_t len = u_arraylist_length(iflist);
867     OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
868
869     for (uint32_t i = 0; i < len; i++)
870     {
871         CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
872
873         if (!ifitem)
874         {
875             continue;
876         }
877         if ((ifitem->flags & IFF_UP_RUNNING_FLAGS) != IFF_UP_RUNNING_FLAGS)
878         {
879             continue;
880         }
881         if (ifitem->family == AF_INET)
882         {
883             OIC_LOG_V(DEBUG, TAG, "Adding IPv4 interface %i to multicast group", ifitem->index);
884             applyMulticastToInterface4(ifitem->index);
885         }
886         if (ifitem->family == AF_INET6)
887         {
888             OIC_LOG_V(DEBUG, TAG, "Adding IPv6 interface %i to multicast group", ifitem->index);
889             applyMulticastToInterface6(ifitem->index);
890         }
891     }
892
893     u_arraylist_destroy(iflist);
894     return CA_STATUS_OK;
895 }
896
897 CAResult_t CAIPStopListenServer()
898 {
899     u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
900     if (!iflist)
901     {
902         OIC_LOG_V(ERROR, TAG, "Get interface info failed: %s", strerror(errno));
903         return CA_STATUS_FAILED;
904     }
905
906     uint32_t len = u_arraylist_length(iflist);
907     OIC_LOG_V(DEBUG, TAG, "IP network interfaces found: %d", len);
908
909     for (uint32_t i = 0; i < len; i++)
910     {
911         CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
912
913         if (!ifitem)
914         {
915             continue;
916         }
917         if ((ifitem->flags & IFF_UP_RUNNING_FLAGS) != IFF_UP_RUNNING_FLAGS)
918         {
919             continue;
920         }
921         if (ifitem->family == AF_INET)
922         {
923             close(caglobals.ip.m4.fd);
924             close(caglobals.ip.m4s.fd);
925             caglobals.ip.m4.fd = -1;
926             caglobals.ip.m4s.fd = -1;
927             OIC_LOG_V(DEBUG, TAG, "IPv4 network interface: %s cloed", ifitem->name);
928         }
929         if (ifitem->family == AF_INET6)
930         {
931             close(caglobals.ip.m6.fd);
932             close(caglobals.ip.m6s.fd);
933             caglobals.ip.m6.fd = -1;
934             caglobals.ip.m6s.fd = -1;
935             OIC_LOG_V(DEBUG, TAG, "IPv6 network interface: %s", ifitem->name);
936         }
937     }
938     u_arraylist_destroy(iflist);
939     return CA_STATUS_OK;
940 }
941
942 static void CAProcessNewInterface(CAInterface_t *ifitem)
943 {
944     if (!ifitem)
945     {
946         OIC_LOG(DEBUG, TAG, "ifitem is null");
947         return;
948     }
949
950     if (ifitem->family == AF_INET6)
951     {
952         applyMulticastToInterface6(ifitem->index);
953     }
954     if (ifitem->family == AF_INET)
955     {
956         applyMulticastToInterface4(ifitem->index);
957     }
958 }
959
960 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
961 {
962     g_packetReceivedCallback = callback;
963 }
964
965 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
966 {
967     g_exceptionCallback = callback;
968 }
969
970 void CAIPSetConnectionStateChangeCallback(CAIPConnectionStateChangeCallback callback)
971 {
972     CAIPSetNetworkMonitorCallback(callback);
973 }
974
975 static void sendData(int fd, const CAEndpoint_t *endpoint,
976                      const void *data, uint32_t dlen,
977                      const char *cast, const char *fam)
978 {
979     OIC_LOG(DEBUG, TAG, "IN");
980
981     if (!endpoint)
982     {
983         OIC_LOG(DEBUG, TAG, "endpoint is null");
984         return;
985     }
986
987     char *secure = (endpoint->flags & CA_SECURE) ? "secure " : "";
988
989     (void)secure;   // eliminates release warning
990     (void)cast;
991     (void)fam;
992
993     struct sockaddr_storage sock;
994     CAConvertNameToAddr(endpoint->addr, endpoint->port, &sock);
995
996     socklen_t socklen;
997     if (sock.ss_family == AF_INET6)
998     {
999         /** @todo figure out correct usage for ifindex, and sin6_scope_id */
1000         socklen = sizeof(struct sockaddr_in6);
1001     }
1002     else
1003     {
1004         socklen = sizeof(struct sockaddr_in);
1005     }
1006 #if !defined(_WIN32)
1007     ssize_t len = sendto(fd, data, dlen, 0, (struct sockaddr *)&sock, socklen);
1008     if (-1 == len)
1009     {
1010         OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %s", secure, cast, fam, strerror(errno));
1011     }
1012     else
1013     {
1014         OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %zd bytes", secure, cast, fam, len);
1015     }
1016 #else
1017     int err = 0;
1018     int len = 0;
1019     int sent = 0;
1020     do {
1021         len = sendto(fd, ((char*)data) + sent, dlen - sent, 0, (struct sockaddr *)&sock, socklen);
1022         if (SOCKET_ERROR == len)
1023         {
1024             err = WSAGetLastError();
1025             if ((WSAEWOULDBLOCK != err) && (WSAENOBUFS != err))
1026             {
1027                 OIC_LOG_V(ERROR, TAG, "%s%s %s sendTo failed: %i", secure, cast, fam, err);
1028             }
1029         }
1030         else
1031         {
1032             sent += len;
1033             if (sent != len)
1034             {
1035                 OIC_LOG_V(DEBUG, TAG, "%s%s %s sendTo (Partial Send) is successful: "
1036                                       "currently sent: %ld bytes, "
1037                                       "total sent: %ld bytes, "
1038                                       "remaining: %ld bytes", 
1039                                       secure, cast, fam, len, sent, dlen-sent);
1040             }
1041             else
1042             {
1043                 OIC_LOG_V(INFO, TAG, "%s%s %s sendTo is successful: %ld bytes", 
1044                                      secure, cast, fam, len);
1045             }
1046         }
1047     } while ((SOCKET_ERROR == len) && ((WSAEWOULDBLOCK == err) || (WSAENOBUFS == err)) || (sent < dlen));
1048 #endif
1049 }
1050
1051 static void sendMulticastData6(const u_arraylist_t *iflist,
1052                                CAEndpoint_t *endpoint,
1053                                const void *data, uint32_t datalen)
1054 {
1055     if (!endpoint)
1056     {
1057         OIC_LOG(DEBUG, TAG, "endpoint is null");
1058         return;
1059     }
1060
1061     int scope = endpoint->flags & CA_SCOPE_MASK;
1062     char *ipv6mcname = ipv6mcnames[scope];
1063     if (!ipv6mcname)
1064     {
1065         OIC_LOG_V(INFO, TAG, "IPv6 multicast scope invalid: %d", scope);
1066         return;
1067     }
1068     OICStrcpy(endpoint->addr, sizeof(endpoint->addr), ipv6mcname);
1069     int fd = caglobals.ip.u6.fd;
1070
1071     uint32_t len = u_arraylist_length(iflist);
1072     for (uint32_t i = 0; i < len; i++)
1073     {
1074         CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
1075         if (!ifitem)
1076         {
1077             continue;
1078         }
1079         if ((ifitem->flags & IFF_UP_RUNNING_FLAGS) != IFF_UP_RUNNING_FLAGS)
1080         {
1081             continue;
1082         }
1083         if (ifitem->family != AF_INET6)
1084         {
1085             continue;
1086         }
1087
1088         int index = ifitem->index;
1089         if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &index, sizeof (index)))
1090         {
1091             OIC_LOG_V(ERROR, TAG, "setsockopt6 failed: %s", CAIPS_GET_ERROR);
1092             return;
1093         }
1094         sendData(fd, endpoint, data, datalen, "multicast", "ipv6");
1095     }
1096 }
1097
1098 static void sendMulticastData4(const u_arraylist_t *iflist,
1099                                CAEndpoint_t *endpoint,
1100                                const void *data, uint32_t datalen)
1101 {
1102     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
1103
1104 #if defined(USE_IP_MREQN)
1105     struct ip_mreqn mreq = { .imr_multiaddr = IPv4MulticastAddress,
1106                              .imr_address.s_addr = htonl(INADDR_ANY),
1107                              .imr_ifindex = 0};
1108 #else
1109     struct ip_mreq mreq  = { .imr_multiaddr = IPv4MulticastAddress,
1110                              .imr_interface = {0}};
1111 #endif
1112
1113     OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
1114     int fd = caglobals.ip.u4.fd;
1115
1116     uint32_t len = u_arraylist_length(iflist);
1117     for (uint32_t i = 0; i < len; i++)
1118     {
1119         CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
1120         if (!ifitem)
1121         {
1122             continue;
1123         }
1124         if ((ifitem->flags & IFF_UP_RUNNING_FLAGS) != IFF_UP_RUNNING_FLAGS)
1125         {
1126             continue;
1127         }
1128         if (ifitem->family != AF_INET)
1129         {
1130             continue;
1131         }
1132 #if defined(USE_IP_MREQN)
1133         mreq.imr_ifindex = ifitem->index;
1134 #else
1135         mreq.imr_interface.s_addr = htonl(ifitem->index);
1136 #endif
1137         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq, sizeof (mreq)))
1138         {
1139             OIC_LOG_V(ERROR, TAG, "send IP_MULTICAST_IF failed: %s (using defualt)",
1140                     CAIPS_GET_ERROR);
1141         }
1142         sendData(fd, endpoint, data, datalen, "multicast", "ipv4");
1143     }
1144 }
1145
1146 void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
1147                   bool isMulticast)
1148 {
1149     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
1150     VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
1151
1152     bool isSecure = (endpoint->flags & CA_SECURE) != 0;
1153
1154     if (isMulticast)
1155     {
1156         endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
1157
1158         u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
1159         if (!iflist)
1160         {
1161             OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
1162             return;
1163         }
1164
1165         if ((endpoint->flags & CA_IPV6) && caglobals.ip.ipv6enabled)
1166         {
1167             sendMulticastData6(iflist, endpoint, data, datalen);
1168         }
1169         if ((endpoint->flags & CA_IPV4) && caglobals.ip.ipv4enabled)
1170         {
1171             sendMulticastData4(iflist, endpoint, data, datalen);
1172         }
1173
1174         u_arraylist_destroy(iflist);
1175     }
1176     else
1177     {
1178         if (!endpoint->port)    // unicast discovery
1179         {
1180             endpoint->port = isSecure ? CA_SECURE_COAP : CA_COAP;
1181         }
1182
1183         int fd;
1184         if (caglobals.ip.ipv6enabled && (endpoint->flags & CA_IPV6))
1185         {
1186             fd = isSecure ? caglobals.ip.u6s.fd : caglobals.ip.u6.fd;
1187 #ifndef __WITH_DTLS__
1188             fd = caglobals.ip.u6.fd;
1189 #endif
1190             sendData(fd, endpoint, data, datalen, "unicast", "ipv6");
1191         }
1192         if (caglobals.ip.ipv4enabled && (endpoint->flags & CA_IPV4))
1193         {
1194             fd = isSecure ? caglobals.ip.u4s.fd : caglobals.ip.u4.fd;
1195 #ifndef __WITH_DTLS__
1196             fd = caglobals.ip.u4.fd;
1197 #endif
1198             sendData(fd, endpoint, data, datalen, "unicast", "ipv4");
1199         }
1200     }
1201 }
1202
1203 CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
1204 {
1205     VERIFY_NON_NULL(info, TAG, "info is NULL");
1206     VERIFY_NON_NULL(size, TAG, "size is NULL");
1207
1208     u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
1209     if (!iflist)
1210     {
1211         OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
1212         return CA_STATUS_FAILED;
1213     }
1214
1215     uint32_t len = u_arraylist_length(iflist);
1216     uint32_t length = len;
1217
1218 #ifdef __WITH_DTLS__
1219     //If DTLS is supported, each interface can support secure port as well
1220     length = len * 2;
1221 #endif
1222
1223     CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(length, sizeof (CAEndpoint_t));
1224     if (!eps)
1225     {
1226         OIC_LOG(ERROR, TAG, "Malloc Failed");
1227         u_arraylist_destroy(iflist);
1228         return CA_MEMORY_ALLOC_FAILED;
1229     }
1230
1231     for (uint32_t i = 0, j = 0; i < len; i++)
1232     {
1233         CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
1234         if(!ifitem)
1235         {
1236             continue;
1237         }
1238
1239         eps[j].adapter = CA_ADAPTER_IP;
1240         eps[j].ifindex = 0;
1241
1242         if (ifitem->family == AF_INET6)
1243         {
1244             eps[j].flags = CA_IPV6;
1245             eps[j].port = caglobals.ip.u6.port;
1246         }
1247         else
1248         {
1249             eps[j].flags = CA_IPV4;
1250             eps[j].port = caglobals.ip.u4.port;
1251             /** @todo eps[j].addr not populated with IPv4 address string.
1252              * it was using ifitem->ipv4addr to accomplish this.
1253              * Need to understand what ipv4addr means to whom*/
1254         }
1255
1256 #ifdef __WITH_DTLS__
1257         j++;
1258
1259         eps[j].adapter = CA_ADAPTER_IP;
1260         eps[j].ifindex = 0;
1261
1262         if (ifitem->family == AF_INET6)
1263         {
1264             eps[j].flags = CA_IPV6 | CA_SECURE;
1265             eps[j].port = caglobals.ip.u6s.port;
1266         }
1267         else
1268         {
1269             eps[j].flags = CA_IPV4 | CA_SECURE;
1270             eps[j].port = caglobals.ip.u4s.port;
1271             inet_ntop(AF_INET, &(ifitem->ipv4addr), eps[j].addr, MAX_ADDR_STR_SIZE_CA);
1272         }
1273 #endif
1274         j++;
1275     }
1276
1277     *info = eps;
1278     *size = len;
1279
1280     u_arraylist_destroy(iflist);
1281
1282     return CA_STATUS_OK;
1283 }