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"
39 #include "oic_string.h"
43 * @brief Logging tag for module name
45 #define IP_SERVER_TAG "IP_SERVER"
48 * @def CA_UDP_BIND_RETRY_COUNT
49 * @brief Retry count in case of socket bind failure.
51 #define CA_UDP_BIND_RETRY_COUNT 10
55 * @brief Max length for ip address.
61 * @brief Socket option.
63 #define SOCKETOPTION 1
66 * @var g_packetHandlerStopFlag
67 * @brief Flag for stopping packet handler thread.
69 static bool g_packetHandlerStopFlag = false;
72 * @var CAAdapterIPServerContext_t
73 * @brief Thread context information for callbacks and threadpool.
77 ca_thread_pool_t threadPool;
78 CAIPPacketReceivedCallback packetReceivedCallback;
79 CAIPExceptionCallback exceptionCallback;
80 } CAAdapterIPServerContext_t;
83 * @var g_serverInfoList
84 * @brief Mutex to synchronize ethenet adapter context.
86 static u_arraylist_t *g_serverInfoList = NULL;
89 * @var g_mutexServerInfoList
90 * @brief Mutex to synchronize Server Information.
92 static ca_mutex g_mutexServerInfoList = NULL;
95 * @var g_adapterEthServerContext
96 * @brief Mutex to synchronize ethenet adapter context.
98 static CAAdapterIPServerContext_t *g_adapterEthServerContext = NULL;
101 * @var g_mutexAdapterServerContext
102 * @brief Mutex to synchronize unicast server
104 static ca_mutex g_mutexAdapterServerContext = NULL;
106 static void CAReceiveHandler(void *data)
108 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
112 struct timeval timeout;
113 char recvBuffer[COAP_MAX_PDU_SIZE] = { 0 };
115 while (true != g_packetHandlerStopFlag)
121 ca_mutex_lock(g_mutexServerInfoList);
122 uint32_t listIndex = 0;
123 uint32_t listLength = u_arraylist_length(g_serverInfoList);
125 u_arraylist_t *tempServerInfoList = u_arraylist_create();
126 if (!tempServerInfoList)
128 OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_create failed");
129 ca_mutex_unlock(g_mutexServerInfoList);
133 for (listIndex = 0; listIndex < listLength; listIndex++)
135 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(g_serverInfoList, listIndex);
142 int sd = info->socketFd;
143 //if valid socket descriptor then add to read list
146 FD_SET(sd, &readFds);
149 //highest file descriptor number, need it for the select function
155 CAServerInfo_t *newInfo = (CAServerInfo_t *) OICMalloc(sizeof(CAServerInfo_t));
158 OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
159 CAClearServerInfoList(tempServerInfoList);
160 ca_mutex_unlock(g_mutexServerInfoList);
166 CAResult_t result = u_arraylist_add(tempServerInfoList, (void *) newInfo);
167 if (CA_STATUS_OK != result)
169 OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_add failed!Thread exit");
170 CAClearServerInfoList(tempServerInfoList);
171 ca_mutex_unlock(g_mutexServerInfoList);
176 ca_mutex_unlock(g_mutexServerInfoList);
178 int ret = select(maxSd + 1, &readFds, NULL, NULL, &timeout);
179 if (g_packetHandlerStopFlag)
181 OIC_LOG_V(DEBUG, IP_SERVER_TAG,
182 "Packet receiver handler Stop request received. Thread exited");
183 CAClearServerInfoList(tempServerInfoList);
188 OIC_LOG_V(FATAL, IP_SERVER_TAG, "select returned error %s", strerror(errno));
189 CAClearServerInfoList(tempServerInfoList);
193 listLength = u_arraylist_length(tempServerInfoList);
194 for (listIndex = 0; listIndex < listLength; listIndex++)
196 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(tempServerInfoList,
203 int sd = info->socketFd;
204 if (FD_ISSET(sd , &readFds))
206 OIC_LOG_V(ERROR, IP_SERVER_TAG,
207 "data Received server information ip %s, port %d socket %d",
208 info->endpoint.addr, info->endpoint.port, sd);
209 memset(recvBuffer, 0, sizeof(recvBuffer));
211 struct sockaddr_in srcSockAddress = { 0 };
212 socklen_t srcAddressLen = sizeof(srcSockAddress);
214 //Reading from socket
215 ssize_t recvLen = recvfrom(sd, recvBuffer, sizeof(recvBuffer), 0,
216 (struct sockaddr *) &srcSockAddress, &srcAddressLen);
219 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Recvfrom failed %s", strerror(errno));
222 else if (0 == recvLen)
224 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Server socket shutdown sock fd[%d]", sd);
225 ca_mutex_lock(g_mutexAdapterServerContext);
226 // Notify upper layer this exception
227 if (g_adapterEthServerContext->exceptionCallback)
229 // need to make proper exception callback.
230 //g_adapterEthServerContext->exceptionCallback(ctx->type);
232 ca_mutex_unlock(g_mutexAdapterServerContext);
235 char srcIPAddress[CA_IPADDR_SIZE] = { 0 };
236 if (!inet_ntop(AF_INET, &srcSockAddress.sin_addr.s_addr, srcIPAddress,
237 sizeof(srcIPAddress)))
240 OIC_LOG(ERROR, IP_SERVER_TAG, "inet_ntop is failed!");
244 uint16_t srcPort = ntohs(srcSockAddress.sin_port);
246 OIC_LOG_V(DEBUG, IP_SERVER_TAG, "Received packet from %s:%d len %d",
247 srcIPAddress, srcPort, recvLen);
249 char *netMask = NULL;
250 if (CA_STATUS_OK != CAIPGetInterfaceSubnetMask(info->ifAddr, &netMask))
252 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to get IP subnet");
256 if (!CAAdapterIsSameSubnet(info->ifAddr, srcIPAddress, netMask))
258 OIC_LOG(DEBUG, IP_SERVER_TAG,
259 "Packet received from different subnet, Ignore!");
266 strncpy(ep.addr, srcIPAddress, MAX_ADDR_STR_SIZE_CA);
269 ep.adapter = CA_ADAPTER_IP;
271 if (info->endpoint.flags & CA_SECURE)
274 ep.flags |= CA_SECURE;
275 (void)CAAdapterNetDtlsDecrypt(&ep, (uint8_t *)recvBuffer, recvLen);
276 OIC_LOG_V(DEBUG, IP_SERVER_TAG,
277 "CAAdapterNetDtlsDecrypt returns [%d]", ret);
280 else //both multicast and unicast
282 ca_mutex_lock(g_mutexAdapterServerContext);
284 if (g_adapterEthServerContext->packetReceivedCallback)
286 g_adapterEthServerContext->packetReceivedCallback(&ep,
287 recvBuffer, recvLen);
290 ca_mutex_unlock(g_mutexAdapterServerContext);
294 CAClearServerInfoList(tempServerInfoList);
296 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
299 static CAResult_t CACreateSocket(int *socketFD, const char *localIp, uint16_t *port,
302 VERIFY_NON_NULL(socketFD, IP_SERVER_TAG, "socketFD is NULL");
303 VERIFY_NON_NULL(localIp, IP_SERVER_TAG, "localIp is NULL");
304 VERIFY_NON_NULL(port, IP_SERVER_TAG, "port is NULL");
305 // Create a UDP socket
309 sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
314 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
319 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to create Socket, Error code: %s",
321 return CA_STATUS_FAILED;
324 // Make the socket non-blocking
325 if (-1 == fcntl(sock, F_SETFL, O_NONBLOCK))
327 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to set non-block mode, Error code: %s",
331 return CA_STATUS_FAILED;
334 if (true == forceBindStart)
336 int setOptionOn = SOCKETOPTION;
337 if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &setOptionOn,
338 sizeof(setOptionOn)))
340 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to set SO_REUSEADDR! Error code: %s",
343 return CA_STATUS_FAILED;
347 struct sockaddr_in sockAddr = { 0 };
348 uint16_t serverPort = *port;
349 sockAddr.sin_family = AF_INET;
350 sockAddr.sin_port = htons(serverPort);
353 sockAddr.sin_addr.s_addr = inet_addr(localIp);
357 bool isBound = false;
358 for (i = 0; i < CA_UDP_BIND_RETRY_COUNT; i++)
360 if (-1 == bind(sock, (struct sockaddr *) &sockAddr, sizeof(sockAddr)))
362 if (false == forceBindStart)
364 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to bind socket[%s]. Trying again..",
367 //Set the port to next one
369 sockAddr.sin_port = htons(serverPort);
374 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to bind socket[%s]!",
384 if (false == isBound)
387 return CA_STATUS_FAILED;
395 static void CACloseSocket(int socketFD)
399 OIC_LOG(ERROR, IP_SERVER_TAG, "Invalid Socket Fd");
404 if (-1 == close(socketFD))
406 OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to close the socket, Error code: %s\n",
411 static CAResult_t CAStartUnicastServer(const char *localAddress, uint16_t *port,
412 bool forceBindStart, bool isSecured, int *serverFD)
414 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
416 VERIFY_NON_NULL(serverFD, IP_SERVER_TAG, "serverFD");
417 VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
418 VERIFY_NON_NULL(port, IP_SERVER_TAG, "port");
420 CAResult_t ret = CACreateSocket(serverFD, localAddress, port, forceBindStart);
421 if (CA_STATUS_OK != ret)
423 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create unicast socket");
426 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
430 static CAResult_t CAIPStartPacketReceiverHandler()
432 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
434 ca_mutex_lock(g_mutexServerInfoList);
436 uint32_t listLength = u_arraylist_length(g_serverInfoList);
438 ca_mutex_unlock(g_mutexServerInfoList);
440 ca_mutex_lock(g_mutexAdapterServerContext);
442 if (!g_adapterEthServerContext)
444 OIC_LOG(ERROR, IP_SERVER_TAG, "g_adapterEthServerContext NULL");
445 ca_mutex_unlock(g_mutexAdapterServerContext);
446 return CA_STATUS_FAILED;
449 if (1 == listLength) //Its first time.
451 g_packetHandlerStopFlag = false;
452 if (CA_STATUS_OK != ca_thread_pool_add_task(g_adapterEthServerContext->threadPool,
453 CAReceiveHandler, NULL ))
455 OIC_LOG(ERROR, IP_SERVER_TAG, "thread_pool_add_task failed!");
456 ca_mutex_unlock(g_mutexAdapterServerContext);
457 return CA_STATUS_FAILED;
459 OIC_LOG(DEBUG, IP_SERVER_TAG, "CAReceiveHandler thread started successfully.");
463 OIC_LOG(DEBUG, IP_SERVER_TAG, "CAReceiveHandler thread already is running");
465 ca_mutex_unlock(g_mutexAdapterServerContext);
467 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
472 static void CAIPServerDestroyMutex(void)
474 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
476 if (g_mutexServerInfoList)
478 ca_mutex_free(g_mutexServerInfoList);
479 g_mutexServerInfoList = NULL;
482 if (g_mutexAdapterServerContext)
484 ca_mutex_free(g_mutexAdapterServerContext);
485 g_mutexAdapterServerContext = NULL;
488 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
491 static CAResult_t CAIPServerCreateMutex(void)
493 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
495 g_mutexServerInfoList = ca_mutex_new();
496 if (!g_mutexServerInfoList)
498 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
499 return CA_MEMORY_ALLOC_FAILED;
502 g_mutexAdapterServerContext = ca_mutex_new();
503 if (!g_mutexAdapterServerContext)
505 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to created mutex!");
506 ca_mutex_free(g_mutexServerInfoList);
507 g_mutexServerInfoList = NULL;
508 return CA_MEMORY_ALLOC_FAILED;
511 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
515 CAResult_t CAIPInitializeServer(const ca_thread_pool_t threadPool)
517 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
520 VERIFY_NON_NULL(threadPool, IP_SERVER_TAG, "Thread pool handle is NULL");
523 if (CA_STATUS_OK != CAIPServerCreateMutex())
525 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create mutex!");
526 return CA_STATUS_FAILED;
529 ca_mutex_lock(g_mutexAdapterServerContext);
530 g_adapterEthServerContext = (CAAdapterIPServerContext_t *) OICCalloc(1,
531 sizeof(CAAdapterIPServerContext_t));
533 if (!g_adapterEthServerContext)
535 OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
536 ca_mutex_unlock(g_mutexAdapterServerContext);
537 return CA_MEMORY_ALLOC_FAILED;
540 g_adapterEthServerContext->threadPool = threadPool;
542 ca_mutex_unlock(g_mutexAdapterServerContext);
544 ca_mutex_lock(g_mutexServerInfoList);
546 g_serverInfoList = u_arraylist_create();
547 if (!g_serverInfoList)
549 OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_create failed");
550 ca_mutex_unlock(g_mutexServerInfoList);
551 return CA_MEMORY_ALLOC_FAILED;
553 ca_mutex_unlock(g_mutexServerInfoList);
554 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
558 void CAIPTerminateServer()
560 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
561 ca_mutex_lock(g_mutexAdapterServerContext);
562 if (!g_adapterEthServerContext)
564 OIC_LOG(ERROR, IP_SERVER_TAG, "g_adapterEthServerContext NULL");
565 ca_mutex_unlock(g_mutexAdapterServerContext);
569 OICFree(g_adapterEthServerContext);
570 g_adapterEthServerContext = NULL;
572 ca_mutex_unlock(g_mutexAdapterServerContext);
574 ca_mutex_lock(g_mutexServerInfoList);
576 CAClearServerInfoList(g_serverInfoList);
577 g_serverInfoList = NULL;
579 ca_mutex_unlock(g_mutexServerInfoList);
581 CAIPServerDestroyMutex();
583 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
587 CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port,
588 bool forceBindStart, bool isSecured)
590 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
593 VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
594 VERIFY_NON_NULL(port, IP_SERVER_TAG, "port");
598 OIC_LOG(ERROR, IP_SERVER_TAG, "Invalid input: port is invalid!");
599 return CA_STATUS_INVALID_PARAM;
602 ca_mutex_lock(g_mutexServerInfoList);
603 bool isUnicastServerStarted = CAIsUnicastServerStarted(g_serverInfoList, localAddress, *port);
604 if (!isUnicastServerStarted)
606 int unicastServerFd = -1;
607 if (CA_STATUS_OK != CAStartUnicastServer(localAddress, port, forceBindStart, isSecured,
610 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to start unicast server!");
611 ca_mutex_unlock(g_mutexServerInfoList);
612 return CA_STATUS_FAILED;
615 CAServerInfo_t *info = (CAServerInfo_t *) OICCalloc(1, sizeof(CAServerInfo_t));
618 OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
619 close(unicastServerFd);
620 ca_mutex_unlock(g_mutexServerInfoList);
621 return CA_MEMORY_ALLOC_FAILED;
624 char *netMask = NULL;
625 if (CA_STATUS_OK != CAIPGetInterfaceSubnetMask(localAddress, &netMask))
627 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to get IP subnet");
631 OICStrcpy(info->subNetMask, sizeof(info->subNetMask), netMask);
634 OICStrcpy(info->endpoint.addr, sizeof(info->endpoint.addr), localAddress);
635 info->endpoint.port = *port;
636 info->endpoint.flags = isSecured ? CA_SECURE : 0;
637 info->endpoint.adapter = CA_ADAPTER_IP;
638 info->socketFd = unicastServerFd;
639 info->isServerStarted = true;
640 info->isMulticastServer = false;
641 OICStrcpy(info->ifAddr, sizeof(info->ifAddr), localAddress);
643 CAResult_t res = CAAddServerInfo(g_serverInfoList, info);
644 if (CA_STATUS_OK != res)
646 OIC_LOG(ERROR, IP_SERVER_TAG, "CAAddServerInfo failed!");
647 close(unicastServerFd);
648 ca_mutex_unlock(g_mutexServerInfoList);
651 ca_mutex_unlock(g_mutexServerInfoList);
653 res = CAIPStartPacketReceiverHandler();
654 if (CA_STATUS_OK != res)
656 OIC_LOG(ERROR, IP_SERVER_TAG, "CAIPStartPacketReceiverHandler failed!");
657 close(unicastServerFd);
663 OIC_LOG_V(DEBUG, IP_SERVER_TAG, "Already Unicast Server Started ip [%s] port [%d]",
664 localAddress, *port);
665 ca_mutex_unlock(g_mutexServerInfoList);
668 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
672 CAResult_t CAIPStartMulticastServer(const char *localAddress, const char *multicastAddress,
673 uint16_t multicastPort)
675 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
678 VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
679 VERIFY_NON_NULL(multicastAddress, IP_SERVER_TAG, "port");
681 uint16_t port = multicastPort;
684 OIC_LOG(ERROR, IP_SERVER_TAG, "Invalid input: Multicast port is invalid!");
685 return CA_STATUS_INVALID_PARAM;
688 ca_mutex_lock(g_mutexServerInfoList);
689 bool isMulticastServerStarted = CAIsMulticastServerStarted(g_serverInfoList, localAddress,
690 multicastAddress, port);
691 if (!isMulticastServerStarted)
693 int mulicastServerFd = -1;
694 CAResult_t ret = CACreateSocket(&mulicastServerFd, multicastAddress, &port, true);
695 if (ret != CA_STATUS_OK)
697 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create multicast socket");
698 ca_mutex_unlock(g_mutexServerInfoList);
702 struct ip_mreq multicastMemberReq = {.imr_interface.s_addr = inet_addr(localAddress)};
703 inet_aton(multicastAddress, &multicastMemberReq.imr_multiaddr);
705 if (-1 == setsockopt(mulicastServerFd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
706 (char *) &multicastMemberReq, sizeof(struct ip_mreq)))
708 OIC_LOG_V(ERROR, IP_SERVER_TAG,
709 "Failed to add to multicast group, Error code: %s\n", strerror(errno));
710 close(mulicastServerFd);
711 ca_mutex_unlock(g_mutexServerInfoList);
712 return CA_STATUS_FAILED;
715 CAServerInfo_t *info = (CAServerInfo_t *) OICCalloc(1, sizeof(CAServerInfo_t));
718 OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
719 close(mulicastServerFd);
720 ca_mutex_unlock(g_mutexServerInfoList);
721 return CA_MEMORY_ALLOC_FAILED;
724 char *netMask = NULL;
725 if (CA_STATUS_OK != CAIPGetInterfaceSubnetMask(localAddress, &netMask))
727 OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to get IP subnet");
731 OICStrcpy(info->subNetMask, sizeof(info->subNetMask), netMask);
735 OICStrcpy(info->endpoint.addr, sizeof(info->endpoint.addr), multicastAddress);
736 info->endpoint.port = multicastPort;
737 info->endpoint.flags = 0;
738 info->socketFd = mulicastServerFd;
739 info->isServerStarted = true;
740 info->isMulticastServer = true;
741 OICStrcpy(info->ifAddr, sizeof(info->ifAddr), localAddress);
743 ret = CAAddServerInfo(g_serverInfoList, info);
745 if (CA_STATUS_OK != ret)
747 OIC_LOG(ERROR, IP_SERVER_TAG, "CAAddServerInfo failed!");
748 close(mulicastServerFd);
749 ca_mutex_unlock(g_mutexServerInfoList);
752 ca_mutex_unlock(g_mutexServerInfoList);
754 ret = CAIPStartPacketReceiverHandler();
755 if (CA_STATUS_OK != ret)
757 OIC_LOG(ERROR, IP_SERVER_TAG, "CAIPStartPacketReceiverHandler failed!");
758 close(mulicastServerFd);
764 OIC_LOG_V(DEBUG, IP_SERVER_TAG,
765 "Multicast Server is already started on interface addr[%s]", localAddress);
766 ca_mutex_unlock(g_mutexServerInfoList);
769 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
773 CAResult_t CAIPStopServer(const char *interfaceAddress)
775 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
777 VERIFY_NON_NULL(interfaceAddress, IP_SERVER_TAG, "interfaceAddress is NULL");
779 ca_mutex_lock(g_mutexServerInfoList);
780 uint32_t listIndex = 0;
781 uint32_t listLength = u_arraylist_length(g_serverInfoList);
783 for (listIndex = 0; listIndex < listLength;)
785 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(g_serverInfoList, listIndex);
792 if (info->isMulticastServer && strncmp(interfaceAddress, info->ifAddr, strlen(info->ifAddr))
795 if (u_arraylist_remove(g_serverInfoList, listIndex))
797 struct ip_mreq multicastMemberReq = { { 0 }, { 0 } };
799 multicastMemberReq.imr_interface.s_addr = inet_addr(info->ifAddr);
800 inet_aton(info->endpoint.addr, &multicastMemberReq.imr_multiaddr);
801 if (-1 == setsockopt(info->socketFd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
802 (char *) &multicastMemberReq, sizeof(struct ip_mreq)))
804 OIC_LOG_V(ERROR, IP_SERVER_TAG,
805 "Failed to leave multicast group, Error code: %s", strerror(errno));
807 CACloseSocket(info->socketFd);
809 OIC_LOG(DEBUG, IP_SERVER_TAG, "Multicast server is stopped successfully.");
810 // Reduce list length by 1 as we removed one element.
815 OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_remove failed.");
816 ca_mutex_unlock(g_mutexServerInfoList);
817 return CA_STATUS_FAILED;
820 else if (strncmp(interfaceAddress, info->endpoint.addr, strlen(info->endpoint.addr)) == 0)
822 if (u_arraylist_remove(g_serverInfoList, listIndex))
824 CACloseSocket(info->socketFd);
826 OIC_LOG(DEBUG, IP_SERVER_TAG, "Unicast server is stopped successfully.");
827 // Reduce list length by 1 as we removed one element.
832 OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_remove failed.");
833 ca_mutex_unlock(g_mutexServerInfoList);
834 return CA_STATUS_FAILED;
843 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
844 ca_mutex_unlock(g_mutexServerInfoList);
848 CAResult_t CAIPStopAllServers()
850 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
852 g_packetHandlerStopFlag = true;
854 ca_mutex_lock(g_mutexServerInfoList);
856 uint32_t listIndex = 0;
857 uint32_t listLength = u_arraylist_length(g_serverInfoList);
858 for (listIndex = 0; listIndex < listLength;)
860 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(g_serverInfoList, listIndex);
866 if (u_arraylist_remove(g_serverInfoList, listIndex))
868 if (info->isMulticastServer)
870 struct ip_mreq multicastMemberReq = { { 0 }, { 0 } };
872 multicastMemberReq.imr_interface.s_addr = inet_addr(info->ifAddr);
873 inet_aton(info->endpoint.addr, &multicastMemberReq.imr_multiaddr);
874 if (-1 == setsockopt(info->socketFd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
875 (char *) &multicastMemberReq, sizeof(struct ip_mreq)))
877 OIC_LOG_V(ERROR, IP_SERVER_TAG,
878 "Failed to leave multicast group, Error code: %s", strerror(errno));
881 CACloseSocket(info->socketFd);
882 //Freeing server info.
884 // Reduce list length by 1 as we removed one element.
889 OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_remove failed.");
890 ca_mutex_unlock(g_mutexServerInfoList);
891 return CA_STATUS_FAILED;
895 ca_mutex_unlock(g_mutexServerInfoList);
897 OIC_LOG(DEBUG, IP_SERVER_TAG, "All Server stopped successfully. OUT");
901 uint16_t CAGetServerPortNum(const char *ipAddress, bool isSecured)
903 ca_mutex_lock(g_mutexServerInfoList);
905 uint16_t port = CAGetServerPort(g_serverInfoList, ipAddress, isSecured);
907 ca_mutex_unlock(g_mutexServerInfoList);
912 CAResult_t CAGetIPServerInfoList(u_arraylist_t **serverInfoList)
914 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
915 ca_mutex_lock(g_mutexServerInfoList);
917 uint32_t list_index = 0;
918 uint32_t list_length = u_arraylist_length(g_serverInfoList);
919 for (list_index = 0; list_index < list_length; list_index++)
921 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(g_serverInfoList, list_index);
927 CAServerInfo_t *newNetinfo = (CAServerInfo_t *) OICMalloc(sizeof(CAServerInfo_t));
930 OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed!");
931 ca_mutex_unlock(g_mutexServerInfoList);
932 return CA_MEMORY_ALLOC_FAILED;
937 CAResult_t result = u_arraylist_add(*serverInfoList, (void *) newNetinfo);
938 if (CA_STATUS_OK != result)
940 OIC_LOG(ERROR, IP_SERVER_TAG, "u_arraylist_add failed!");
941 ca_mutex_unlock(g_mutexServerInfoList);
942 return CA_STATUS_FAILED;
945 ca_mutex_unlock(g_mutexServerInfoList);
946 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
950 void CAIPSetPacketReceiveCallback(CAIPPacketReceivedCallback callback)
952 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
954 ca_mutex_lock(g_mutexAdapterServerContext);
956 if (!g_adapterEthServerContext)
958 OIC_LOG(ERROR, IP_SERVER_TAG, "g_adapterEthServerContext NULL");
959 ca_mutex_unlock(g_mutexAdapterServerContext);
962 g_adapterEthServerContext->packetReceivedCallback = callback;
964 ca_mutex_unlock(g_mutexAdapterServerContext);
966 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
969 void CAIPSetExceptionCallback(CAIPExceptionCallback callback)
971 OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
972 ca_mutex_lock(g_mutexAdapterServerContext);
974 if (!g_adapterEthServerContext)
976 OIC_LOG(ERROR, IP_SERVER_TAG, "g_adapterEthServerContext NULL");
977 ca_mutex_unlock(g_mutexAdapterServerContext);
980 g_adapterEthServerContext->exceptionCallback = callback;
982 ca_mutex_unlock(g_mutexAdapterServerContext);
984 OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");