1 /******************************************************************
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/select.h>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
33 #include "caadapterutils.h"
35 #include "caadapternetdtls.h"
38 #include "oic_malloc.h"
42 * @brief Logging tag for module name
44 #define IP_SERVER_TAG "IP_SERVER"
47 * @def CA_UDP_BIND_RETRY_COUNT
48 * @brief Retry count in case of socket bind failure.
50 #define CA_UDP_BIND_RETRY_COUNT 10
54 * @brief Max length for ip address.
60 * @brief Socket option.
62 #define SOCKETOPTION 1
65 * @var g_packetHandlerStopFlag
66 * @brief Flag for stopping packet handler thread.
68 static bool g_packetHandlerStopFlag = false;
71 * @var CAAdapterIPServerContext_t
72 * @brief Thread context information for callbacks and threadpool.
76 ca_thread_pool_t threadPool;
77 CAIPPacketReceivedCallback packetReceivedCallback;
78 CAIPExceptionCallback exceptionCallback;
79 } CAAdapterIPServerContext_t;
82 * @var g_serverInfoList
83 * @brief Mutex to synchronize ethenet adapter context.
85 static u_arraylist_t *g_serverInfoList = NULL;
88 * @var g_mutexServerInfoList
89 * @brief Mutex to synchronize Server Information.
91 static ca_mutex g_mutexServerInfoList = NULL;
94 * @var g_adapterEthServerContext
95 * @brief Mutex to synchronize ethenet adapter context.
97 static CAAdapterIPServerContext_t *g_adapterEthServerContext = NULL;
100 * @var g_mutexAdapterServerContext
101 * @brief Mutex to synchronize unicast server
103 static ca_mutex g_mutexAdapterServerContext = NULL;
105 static void CAReceiveHandler(void *data)
107 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
111 struct timeval timeout;
112 char recvBuffer[COAP_MAX_PDU_SIZE] = { 0 };
114 while (true != g_packetHandlerStopFlag)
120 ca_mutex_lock(g_mutexServerInfoList);
121 uint32_t listIndex = 0;
122 uint32_t listLength = u_arraylist_length(g_serverInfoList);
124 u_arraylist_t *tempServerInfoList = u_arraylist_create();
125 if (!tempServerInfoList)
127 OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_create failed");
128 ca_mutex_unlock(g_mutexServerInfoList);
132 for (listIndex = 0; listIndex < listLength; listIndex++)
134 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(g_serverInfoList, listIndex);
141 int sd = info->socketFd;
142 //if valid socket descriptor then add to read list
145 FD_SET(sd, &readFds);
148 //highest file descriptor number, need it for the select function
154 CAServerInfo_t *newInfo = (CAServerInfo_t *) OICMalloc(sizeof(CAServerInfo_t));
157 OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
158 CAClearServerInfoList(tempServerInfoList);
159 ca_mutex_unlock(g_mutexServerInfoList);
163 memcpy(newInfo, info, sizeof(CAServerInfo_t));
165 CAResult_t result = u_arraylist_add(tempServerInfoList, (void *) newInfo);
166 if (CA_STATUS_OK != result)
168 OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_add failed!Thread exit");
169 CAClearServerInfoList(tempServerInfoList);
170 ca_mutex_unlock(g_mutexServerInfoList);
175 ca_mutex_unlock(g_mutexServerInfoList);
177 int ret = select(maxSd + 1, &readFds, NULL, NULL, &timeout);
178 if (g_packetHandlerStopFlag)
180 OIC_LOG_V(DEBUG, IP_SERVER_TAG,
181 "Packet receiver handler Stop request received. Thread exited");
182 CAClearServerInfoList(tempServerInfoList);
187 OIC_LOG_V(FATAL, IP_SERVER_TAG, "select returned error %s", strerror(errno));
188 CAClearServerInfoList(tempServerInfoList);
192 listLength = u_arraylist_length(tempServerInfoList);
193 for (listIndex = 0; listIndex < listLength; listIndex++)
195 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(tempServerInfoList,
202 int sd = info->socketFd;
203 if (FD_ISSET(sd , &readFds))
205 OIC_LOG_V(ERROR, IP_SERVER_TAG,
206 "data Received server information ip %s, port %d socket %d",
207 info->ipAddress, info->port, sd);
208 memset(recvBuffer, 0, sizeof(recvBuffer));
210 struct sockaddr_in srcSockAddress = { 0 };
211 socklen_t srcAddressLen = sizeof(srcSockAddress);
213 //Reading from socket
214 ssize_t recvLen = recvfrom(sd, recvBuffer, sizeof(recvBuffer), 0,
215 (struct sockaddr *) &srcSockAddress, &srcAddressLen);
218 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Recvfrom failed %s", strerror(errno));
221 else if (0 == recvLen)
223 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Server socket shutdown sock fd[%d]", sd);
224 ca_mutex_lock(g_mutexAdapterServerContext);
225 // Notify upper layer this exception
226 if (g_adapterEthServerContext->exceptionCallback)
228 // need to make proper exception callback.
229 //g_adapterEthServerContext->exceptionCallback(ctx->type);
231 ca_mutex_unlock(g_mutexAdapterServerContext);
234 char srcIPAddress[CA_IPADDR_SIZE] = { 0 };
235 if (!inet_ntop(AF_INET, &srcSockAddress.sin_addr.s_addr, srcIPAddress,
236 sizeof(srcIPAddress)))
239 OIC_LOG(ERROR, IP_SERVER_TAG, "inet_ntop is failed!");
243 uint16_t srcPort = ntohs(srcSockAddress.sin_port);
245 OIC_LOG_V(DEBUG, IP_SERVER_TAG, "Received packet from %s:%d len %d",
246 srcIPAddress, srcPort, recvLen);
248 char *netMask = NULL;
249 if (CA_STATUS_OK != CAIPGetInterfaceSubnetMask(info->ifAddr, &netMask))
251 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to get IP subnet");
255 if (!CAAdapterIsSameSubnet(info->ifAddr, srcIPAddress, netMask))
257 OIC_LOG(DEBUG, IP_SERVER_TAG,
258 "Packet received from different subnet, Ignore!");
267 CAResult_t ret = CAAdapterNetDtlsDecrypt(srcIPAddress, srcPort,
268 (uint8_t *)recvBuffer, recvLen,
270 OIC_LOG_V(DEBUG, IP_SERVER_TAG,
271 "CAAdapterNetDtlsDecrypt returns [%d]", ret);
274 else //both multicast and unicast
276 ca_mutex_lock(g_mutexAdapterServerContext);
278 if (g_adapterEthServerContext->packetReceivedCallback)
280 g_adapterEthServerContext->packetReceivedCallback(srcIPAddress, srcPort,
285 ca_mutex_unlock(g_mutexAdapterServerContext);
289 CAClearServerInfoList(tempServerInfoList);
291 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
294 static CAResult_t CACreateSocket(int *socketFD, const char *localIp, uint16_t *port,
297 VERIFY_NON_NULL(socketFD, IP_SERVER_TAG, "socketFD is NULL");
298 VERIFY_NON_NULL(localIp, IP_SERVER_TAG, "localIp is NULL");
299 VERIFY_NON_NULL(port, IP_SERVER_TAG, "port is NULL");
300 // Create a UDP socket
301 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
304 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to create Socket, Error code: %s",
306 return CA_STATUS_FAILED;
309 // Make the socket non-blocking
310 if (-1 == fcntl(sock, F_SETFL, O_NONBLOCK))
312 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to set non-block mode, Error code: %s",
316 return CA_STATUS_FAILED;
319 if (true == forceBindStart)
321 int setOptionOn = SOCKETOPTION;
322 if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &setOptionOn,
323 sizeof(setOptionOn)))
325 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to set SO_REUSEADDR! Error code: %s",
328 return CA_STATUS_FAILED;
332 struct sockaddr_in sockAddr = { 0 };
333 uint16_t serverPort = *port;
334 sockAddr.sin_family = AF_INET;
335 sockAddr.sin_port = htons(serverPort);
338 sockAddr.sin_addr.s_addr = inet_addr(localIp);
342 sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
346 bool isBound = false;
347 for (i = 0; i < CA_UDP_BIND_RETRY_COUNT; i++)
349 if (-1 == bind(sock, (struct sockaddr *) &sockAddr, sizeof(sockAddr)))
351 if (false == forceBindStart)
353 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to bind socket[%s]. Trying again..",
356 //Set the port to next one
358 sockAddr.sin_port = htons(serverPort);
363 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to bind socket[%s]!",
373 if (false == isBound)
376 return CA_STATUS_FAILED;
384 static void CACloseSocket(int socketFD)
388 OIC_LOG(ERROR, IP_SERVER_TAG, "Invalid Socket Fd");
393 if (-1 == close(socketFD))
395 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to close the socket, Error code: %s\n",
400 static CAResult_t CAStartUnicastServer(const char *localAddress, uint16_t *port,
401 bool forceBindStart, bool isSecured, int *serverFD)
403 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
405 VERIFY_NON_NULL(serverFD, IP_SERVER_TAG, "serverFD");
406 VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
407 VERIFY_NON_NULL(port, IP_SERVER_TAG, "port");
409 CAResult_t ret = CACreateSocket(serverFD, localAddress, port, forceBindStart);
410 if (CA_STATUS_OK != ret)
412 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create unicast socket");
415 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
419 static CAResult_t CAIPStartPacketReceiverHandler()
421 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
423 ca_mutex_lock(g_mutexServerInfoList);
425 uint32_t listLength = u_arraylist_length(g_serverInfoList);
427 ca_mutex_unlock(g_mutexServerInfoList);
429 ca_mutex_lock(g_mutexAdapterServerContext);
431 if (!g_adapterEthServerContext)
433 OIC_LOG(ERROR, IP_SERVER_TAG, "g_adapterEthServerContext NULL");
434 ca_mutex_unlock(g_mutexAdapterServerContext);
435 return CA_STATUS_FAILED;
438 if (1 == listLength) //Its first time.
440 if (CA_STATUS_OK != ca_thread_pool_add_task(g_adapterEthServerContext->threadPool,
441 CAReceiveHandler, NULL ))
443 OIC_LOG(ERROR, IP_SERVER_TAG, "thread_pool_add_task failed!");
444 ca_mutex_unlock(g_mutexAdapterServerContext);
445 return CA_STATUS_FAILED;
447 OIC_LOG(DEBUG, IP_SERVER_TAG, "CAReceiveHandler thread started successfully.");
451 OIC_LOG(DEBUG, IP_SERVER_TAG, "CAReceiveHandler thread already is running");
453 ca_mutex_unlock(g_mutexAdapterServerContext);
455 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
460 static void CAIPServerDestroyMutex(void)
462 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
464 if (g_mutexServerInfoList)
466 ca_mutex_free(g_mutexServerInfoList);
467 g_mutexServerInfoList = NULL;
470 if (g_mutexAdapterServerContext)
472 ca_mutex_free(g_mutexAdapterServerContext);
473 g_mutexAdapterServerContext = NULL;
476 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
479 static CAResult_t CAIPServerCreateMutex(void)
481 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
483 g_mutexServerInfoList = ca_mutex_new();
484 if (!g_mutexServerInfoList)
486 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
487 return CA_MEMORY_ALLOC_FAILED;
490 g_mutexAdapterServerContext = ca_mutex_new();
491 if (!g_mutexAdapterServerContext)
493 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to created mutex!");
494 ca_mutex_free(g_mutexServerInfoList);
495 g_mutexServerInfoList = NULL;
496 return CA_MEMORY_ALLOC_FAILED;
499 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
503 CAResult_t CAIPInitializeServer(const ca_thread_pool_t threadPool)
505 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
508 VERIFY_NON_NULL(threadPool, IP_SERVER_TAG, "Thread pool handle is NULL");
511 if (CA_STATUS_OK != CAIPServerCreateMutex())
513 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create mutex!");
514 return CA_STATUS_FAILED;
517 ca_mutex_lock(g_mutexAdapterServerContext);
518 g_adapterEthServerContext = (CAAdapterIPServerContext_t *) OICCalloc(1,
519 sizeof(CAAdapterIPServerContext_t));
521 if (!g_adapterEthServerContext)
523 OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
524 ca_mutex_unlock(g_mutexAdapterServerContext);
525 return CA_MEMORY_ALLOC_FAILED;
528 g_adapterEthServerContext->threadPool = threadPool;
530 ca_mutex_unlock(g_mutexAdapterServerContext);
532 ca_mutex_lock(g_mutexServerInfoList);
534 g_serverInfoList = u_arraylist_create();
535 if (!g_serverInfoList)
537 OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_create failed");
538 ca_mutex_unlock(g_mutexServerInfoList);
539 return CA_MEMORY_ALLOC_FAILED;
541 ca_mutex_unlock(g_mutexServerInfoList);
542 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
546 void CAIPTerminateServer()
548 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
549 ca_mutex_lock(g_mutexAdapterServerContext);
550 if (!g_adapterEthServerContext)
552 OIC_LOG(ERROR, IP_SERVER_TAG, "g_adapterEthServerContext NULL");
553 ca_mutex_unlock(g_adapterEthServerContext);
557 OICFree(g_adapterEthServerContext);
558 g_adapterEthServerContext = NULL;
560 ca_mutex_unlock(g_mutexAdapterServerContext);
562 ca_mutex_lock(g_mutexAdapterServerContext);
564 CAClearServerInfoList(g_serverInfoList);
565 g_serverInfoList = NULL;
567 ca_mutex_unlock(g_mutexAdapterServerContext);
569 CAIPServerDestroyMutex();
571 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
575 CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port,
576 bool forceBindStart, bool isSecured)
578 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
581 VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
582 VERIFY_NON_NULL(port, IP_SERVER_TAG, "port");
586 OIC_LOG(ERROR, IP_SERVER_TAG, "Invalid input: port is invalid!");
587 return CA_STATUS_INVALID_PARAM;
590 ca_mutex_lock(g_mutexServerInfoList);
591 bool isUnicastServerStarted = CAIsUnicastServerStarted(g_serverInfoList, localAddress, *port);
592 if (!isUnicastServerStarted)
594 int unicastServerFd = -1;
595 if (CA_STATUS_OK != CAStartUnicastServer(localAddress, port, forceBindStart, isSecured,
598 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to start unicast server!");
599 ca_mutex_unlock(g_mutexServerInfoList);
600 return CA_STATUS_FAILED;
603 CAServerInfo_t *info = (CAServerInfo_t *) OICCalloc(1, sizeof(CAServerInfo_t));
606 OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
607 close(unicastServerFd);
608 ca_mutex_unlock(g_mutexServerInfoList);
609 return CA_MEMORY_ALLOC_FAILED;
612 char *netMask = NULL;
613 if (CA_STATUS_OK != CAIPGetInterfaceSubnetMask(localAddress, &netMask))
615 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to get IP subnet");
619 strncpy(info->subNetMask, netMask, strlen(netMask));
622 strncpy(info->ipAddress, localAddress, strlen(localAddress));
624 info->socketFd = unicastServerFd;
625 info->isSecured = isSecured;
626 info->isServerStarted = true;
627 info->isMulticastServer = false;
628 strncpy(info->ifAddr, localAddress, strlen(localAddress));
630 CAResult_t res = CAAddServerInfo(g_serverInfoList, info);
631 if (CA_STATUS_OK != res)
633 OIC_LOG(ERROR, IP_SERVER_TAG, "CAAddServerInfo failed!");
634 close(unicastServerFd);
635 ca_mutex_unlock(g_mutexServerInfoList);
638 ca_mutex_unlock(g_mutexServerInfoList);
640 res = CAIPStartPacketReceiverHandler();
641 if (CA_STATUS_OK != res)
643 OIC_LOG(ERROR, IP_SERVER_TAG, "CAIPStartPacketReceiverHandler failed!");
644 close(unicastServerFd);
650 OIC_LOG_V(DEBUG, IP_SERVER_TAG, "Already Unicast Server Started ip [%s] port [%d]",
651 localAddress, *port);
652 ca_mutex_unlock(g_mutexServerInfoList);
655 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
659 CAResult_t CAIPStartMulticastServer(const char *localAddress, const char *multicastAddress,
660 uint16_t multicastPort)
662 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
665 VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
666 VERIFY_NON_NULL(multicastAddress, IP_SERVER_TAG, "port");
668 uint16_t port = multicastPort;
671 OIC_LOG(ERROR, IP_SERVER_TAG, "Invalid input: Multicast port is invalid!");
672 return CA_STATUS_INVALID_PARAM;
675 ca_mutex_lock(g_mutexServerInfoList);
676 bool isMulticastServerStarted = CAIsMulticastServerStarted(g_serverInfoList, localAddress,
677 multicastAddress, port);
678 if (!isMulticastServerStarted)
680 int mulicastServerFd = -1;
681 CAResult_t ret = CACreateSocket(&mulicastServerFd, multicastAddress, &port, true);
682 if (ret != CA_STATUS_OK)
684 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create multicast socket");
685 ca_mutex_unlock(g_mutexServerInfoList);
689 struct ip_mreq multicastMemberReq = {.imr_interface.s_addr = inet_addr(localAddress)};
690 inet_aton(multicastAddress, &multicastMemberReq.imr_multiaddr);
692 if (-1 == setsockopt(mulicastServerFd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
693 (char *) &multicastMemberReq, sizeof(struct ip_mreq)))
695 OIC_LOG_V(ERROR, IP_SERVER_TAG,
696 "Failed to add to multicast group, Error code: %s\n", strerror(errno));
697 close(mulicastServerFd);
698 ca_mutex_unlock(g_mutexServerInfoList);
699 return CA_STATUS_FAILED;
702 CAServerInfo_t *info = (CAServerInfo_t *) OICCalloc(1, sizeof(CAServerInfo_t));
705 OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
706 close(mulicastServerFd);
707 ca_mutex_unlock(g_mutexServerInfoList);
708 return CA_MEMORY_ALLOC_FAILED;
711 char *netMask = NULL;
712 if (CA_STATUS_OK != CAIPGetInterfaceSubnetMask(localAddress, &netMask))
714 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to get IP subnet");
718 strncpy(info->subNetMask, netMask, strlen(netMask));
722 strncpy(info->ipAddress, multicastAddress, strlen(multicastAddress));
723 info->port = multicastPort;
724 info->socketFd = mulicastServerFd;
725 info->isSecured = false;
726 info->isServerStarted = true;
727 info->isMulticastServer = true;
728 strncpy(info->ifAddr, localAddress, strlen(localAddress));
730 ret = CAAddServerInfo(g_serverInfoList, info);
732 if (CA_STATUS_OK != ret)
734 OIC_LOG(ERROR, IP_SERVER_TAG, "CAAddServerInfo failed!");
735 close(mulicastServerFd);
736 ca_mutex_unlock(g_mutexServerInfoList);
739 ca_mutex_unlock(g_mutexServerInfoList);
741 ret = CAIPStartPacketReceiverHandler();
742 if (CA_STATUS_OK != ret)
744 OIC_LOG(ERROR, IP_SERVER_TAG, "CAIPStartPacketReceiverHandler failed!");
745 close(mulicastServerFd);
751 OIC_LOG_V(DEBUG, IP_SERVER_TAG,
752 "Multicast Server is already started on interface addr[%s]", localAddress);
753 ca_mutex_unlock(g_mutexServerInfoList);
756 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
760 CAResult_t CAIPStopServer(const char *interfaceAddress)
762 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
764 VERIFY_NON_NULL(interfaceAddress, IP_SERVER_TAG, "interfaceAddress is NULL");
766 ca_mutex_lock(g_mutexServerInfoList);
767 uint32_t listIndex = 0;
768 uint32_t listLength = u_arraylist_length(g_serverInfoList);
770 for (listIndex = 0; listIndex < listLength;)
772 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(g_serverInfoList, listIndex);
779 if (info->isMulticastServer && strncmp(interfaceAddress, info->ifAddr, strlen(info->ifAddr))
782 if (u_arraylist_remove(g_serverInfoList, listIndex))
784 struct ip_mreq multicastMemberReq = { { 0 }, { 0 } };
786 multicastMemberReq.imr_interface.s_addr = inet_addr(info->ifAddr);
787 inet_aton(info->ipAddress, &multicastMemberReq.imr_multiaddr);
788 if (-1 == setsockopt(info->socketFd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
789 (char *) &multicastMemberReq, sizeof(struct ip_mreq)))
791 OIC_LOG_V(ERROR, IP_SERVER_TAG,
792 "Failed to leave multicast group, Error code: %s", strerror(errno));
794 CACloseSocket(info->socketFd);
796 OIC_LOG_V(DEBUG, IP_SERVER_TAG,
797 "Multicast server is stopped successfully on IF [%s]", info->ifAddr);
798 // Reduce list length by 1 as we removed one element.
803 OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_remove failed.");
804 ca_mutex_unlock(g_mutexServerInfoList);
805 return CA_STATUS_FAILED;
808 else if (strncmp(interfaceAddress, info->ipAddress, strlen(info->ipAddress)) == 0)
810 if (u_arraylist_remove(g_serverInfoList, listIndex))
812 CACloseSocket(info->socketFd);
814 OIC_LOG_V(DEBUG, IP_SERVER_TAG,
815 "Unicast server is stopped successfully on IF [%s]", info->ifAddr);
816 // Reduce list length by 1 as we removed one element.
821 OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_remove failed.");
822 ca_mutex_unlock(g_mutexServerInfoList);
823 return CA_STATUS_FAILED;
832 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
833 ca_mutex_unlock(g_mutexServerInfoList);
837 CAResult_t CAIPStopAllServers()
839 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
841 g_packetHandlerStopFlag = true;
843 ca_mutex_lock(g_mutexServerInfoList);
845 uint32_t listIndex = 0;
846 uint32_t listLength = u_arraylist_length(g_serverInfoList);
847 for (listIndex = 0; listIndex < listLength;)
849 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(g_serverInfoList, listIndex);
855 if (u_arraylist_remove(g_serverInfoList, listIndex))
857 if (info->isMulticastServer)
859 struct ip_mreq multicastMemberReq = { { 0 }, { 0 } };
861 multicastMemberReq.imr_interface.s_addr = inet_addr(info->ifAddr);
862 inet_aton(info->ipAddress, &multicastMemberReq.imr_multiaddr);
863 if (-1 == setsockopt(info->socketFd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
864 (char *) &multicastMemberReq, sizeof(struct ip_mreq)))
866 OIC_LOG_V(ERROR, IP_SERVER_TAG,
867 "Failed to leave multicast group, Error code: %s", strerror(errno));
870 CACloseSocket(info->socketFd);
871 //Freeing server info.
873 // Reduce list length by 1 as we removed one element.
878 OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_remove failed.");
879 ca_mutex_unlock(g_mutexServerInfoList);
880 return CA_STATUS_FAILED;
884 ca_mutex_unlock(g_mutexServerInfoList);
886 OIC_LOG(DEBUG, IP_SERVER_TAG, "All Server stopped successfully. OUT");
890 uint16_t CAGetServerPortNum(const char *ipAddress, bool isSecured)
892 ca_mutex_lock(g_mutexServerInfoList);
894 uint16_t port = CAGetServerPort(g_serverInfoList, ipAddress, isSecured);
896 ca_mutex_unlock(g_mutexServerInfoList);
901 CAResult_t CAGetIPServerInfoList(u_arraylist_t **serverInfoList)
903 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
904 ca_mutex_lock(g_mutexServerInfoList);
906 uint32_t list_index = 0;
907 uint32_t list_length = u_arraylist_length(g_serverInfoList);
908 for (list_index = 0; list_index < list_length; list_index++)
910 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(g_serverInfoList, list_index);
916 CAServerInfo_t *newNetinfo = (CAServerInfo_t *) OICMalloc(sizeof(CAServerInfo_t));
919 OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed!");
920 ca_mutex_unlock(g_mutexServerInfoList);
921 return CA_MEMORY_ALLOC_FAILED;
924 memcpy(newNetinfo, info, sizeof(*info));
926 CAResult_t result = u_arraylist_add(*serverInfoList, (void *) newNetinfo);
927 if (CA_STATUS_OK != result)
929 OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_add failed!");
930 ca_mutex_unlock(g_mutexServerInfoList);
931 return CA_STATUS_FAILED;
934 ca_mutex_unlock(g_mutexServerInfoList);
935 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
939 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
941 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
943 ca_mutex_lock(g_mutexAdapterServerContext);
945 if (!g_adapterEthServerContext)
947 OIC_LOG(ERROR, IP_SERVER_TAG, "g_adapterEthServerContext NULL");
948 ca_mutex_unlock(g_mutexAdapterServerContext);
951 g_adapterEthServerContext->packetReceivedCallback = callback;
953 ca_mutex_unlock(g_mutexAdapterServerContext);
955 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
958 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
960 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
961 ca_mutex_lock(g_mutexAdapterServerContext);
963 if (!g_adapterEthServerContext)
965 OIC_LOG(ERROR, IP_SERVER_TAG, "g_adapterEthServerContext NULL");
966 ca_mutex_unlock(g_mutexAdapterServerContext);
969 g_adapterEthServerContext->exceptionCallback = callback;
971 ca_mutex_unlock(g_mutexAdapterServerContext);
973 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");