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 ******************************************************************/
20 #include "caethernetinterface.h"
22 #include <sys/types.h>
23 #include <sys/socket.h>
26 #include <sys/select.h>
27 #include <arpa/inet.h>
28 #include <netinet/in.h>
32 #include "caadapterutils.h"
34 #include "caadapternetdtls.h"
37 #include "oic_malloc.h"
40 * @def ETHERNET_SERVER_TAG
41 * @brief Logging tag for module name
43 #define ETHERNET_SERVER_TAG "ETHERNET_SERVER"
46 * @def CA_UDP_BIND_RETRY_COUNT
47 * @brief Retry count in case of socket bind failure.
49 #define CA_UDP_BIND_RETRY_COUNT 10
53 * @brief max length for ip
58 * @var g_unicastServerSocketFD
59 * @brief Unicast server socket descriptor
61 static int32_t g_unicastServerSocketFD = -1;
64 * @var g_mutexUnicastServer
65 * @brief Mutex to synchronize unicast server
67 static u_mutex g_mutexUnicastServer = NULL;
71 * @brief Flag to control the Receive Unicast Data Thread
73 static bool g_stopUnicast = false;
76 * @var g_multicastServerSocketFD
77 * @brief socket descriptor for multicast server
79 static int32_t g_multicastServerSocketFD = -1;
82 * @var g_mutexMulticastServer
83 * @brief Mutex to synchronize secure multicast server
85 static u_mutex g_mutexMulticastServer = NULL;
88 * @var g_stopMulticast
89 * @brief Flag to control the Receive Multicast Data Thread
91 static bool g_stopMulticast = false;
95 * @var g_secureUnicastServerSocketFD
96 * @brief Secure unicast server socket descriptor
98 static int32_t g_secureUnicastServerSocketFD = -1;
101 * @var g_mutexSecureUnicastServer
102 * @brief Mutex to synchronize secure unicast server
104 static u_mutex g_mutexSecureUnicastServer = NULL;
107 * @var g_stopSecureUnicast
108 * @brief Flag to control the unicast secure data receive thread
110 static bool g_stopSecureUnicast = false;
115 * @brief ThreadPool for storing u_thread_pool_t handle passed from adapter
117 static u_thread_pool_t g_threadPool = NULL;
120 * @var g_multicastServerInterface
121 * @brief Local interface on which multicast server is running
123 static char g_multicastServerInterface[IPNAMESIZE];
126 * @var g_multicastMemberReq
127 * @brief ip_mreq structure passed to join a multicast group
129 static struct ip_mreq g_multicastMemberReq;
132 * @var g_packetReceivedCallback
133 * @brief Callback for notifying the upper layer on receival data from remote OIC device
135 static CAEthernetPacketReceivedCallback g_packetReceivedCallback = NULL;
138 * @var g_exceptionCallback
139 * @brief Callback for notifying the upper layer when unicast/multicast server encounters exception
141 static CAEthernetExceptionCallback g_exceptionCallback = NULL;
144 @brief Thread context information for unicast, multicast and secured unicast server
150 CAAdapterServerType_t type;
151 } CAAdapterReceiveThreadContext_t;
153 static void CAReceiveHandler(void *data)
155 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
158 VERIFY_NON_NULL_VOID(data, ETHERNET_SERVER_TAG, "Invalid thread context");
160 CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *)data;
162 struct timeval timeout;
163 char recvBuffer[COAP_MAX_PDU_SIZE] = {0};
165 while (true != *(ctx->stopFlag))
171 FD_SET(ctx->socket_fd, &reads);
173 int32_t ret = select(ctx->socket_fd + 1, &reads, NULL, NULL, &timeout);
174 if (*(ctx->stopFlag) == true)
176 OIC_LOG_V(DEBUG, ETHERNET_SERVER_TAG,
177 "Stop request received for [%d] server", ctx->type);
183 OIC_LOG_V(FATAL, ETHERNET_SERVER_TAG, "select returned error %s", strerror(errno));
187 if (!FD_ISSET(ctx->socket_fd, &reads))
192 memset(recvBuffer, 0, sizeof(recvBuffer));
194 // Read data from socket
195 struct sockaddr_in srcSockAddress;
197 socklen_t srcAddressLen = sizeof(srcSockAddress);
199 recvLen = recvfrom(ctx->socket_fd, recvBuffer,
200 sizeof(recvBuffer), 0, (struct sockaddr *) &srcSockAddress,
205 OIC_LOG_V(DEBUG, ETHERNET_SERVER_TAG, "%s", strerror(errno));
208 else if (0 == recvLen)
210 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Server socket shutdown [%d]!", ctx->type);
212 // Notify upper layer this exception
213 if (g_exceptionCallback)
215 g_exceptionCallback(ctx->type);
222 char srcIPAddress[CA_IPADDR_SIZE] = {0};
223 inet_ntop(AF_INET, &srcSockAddress.sin_addr.s_addr, srcIPAddress, sizeof(srcIPAddress));
224 uint16_t srcPort = 0;
225 srcPort = ntohs(srcSockAddress.sin_port);
227 OIC_LOG_V(DEBUG, ETHERNET_SERVER_TAG, "Received packet from %s:%d\n",
228 srcIPAddress, srcPort);
229 OIC_LOG_V(DEBUG, ETHERNET_SERVER_TAG, "Data: %s\t, DataLength: %d\n",
230 recvBuffer, recvLen);
232 char *netMask = NULL;
233 if (CA_STATUS_OK != CAEthernetGetInterfaceSubnetMask(&netMask))
235 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to get ethernet subnet");
239 if (!CAAdapterIsSameSubnet(g_multicastServerInterface, srcIPAddress, netMask))
241 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "Packet received from different subnet, Ignore!");
253 case CA_UNICAST_SERVER:
254 case CA_MULTICAST_SERVER:
255 // Notify data to upper layer
256 if (g_packetReceivedCallback)
258 g_packetReceivedCallback(srcIPAddress, srcPort, recvBuffer, recvLen, false);
262 case CA_SECURED_UNICAST_SERVER:
264 CAResult_t ret = CAAdapterNetDtlsDecrypt(srcIPAddress,
266 (uint8_t *)recvBuffer,
267 recvLen, DTLS_ETHERNET);
268 OIC_LOG_V(DEBUG, ETHERNET_SERVER_TAG,
269 "CAAdapterNetDtlsDecrypt returns [%d]", ret);
272 #endif //__WITH_DTLS__
274 // Should never occur
275 OIC_LOG_V(DEBUG, ETHERNET_SERVER_TAG, "Invalid server type");
285 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
288 static CAResult_t CACreateSocket(int32_t *socketFD, const char *localIp, uint16_t *port,
292 // Create a UDP socket
293 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
296 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to create Socket, Error code: %s",
298 return CA_STATUS_FAILED;
301 // Make the socket non-blocking
302 if (-1 == fcntl(sock, F_SETFL, O_NONBLOCK))
304 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to set non-block mode, Error code: %s",
308 return CA_STATUS_FAILED;
311 if (true == forceStart)
313 int32_t setOptionOn = 1;
314 if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
315 (char *) &setOptionOn,
316 sizeof(setOptionOn)))
318 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to set SO_REUSEADDR! Error code: %s",
322 return CA_STATUS_FAILED;
326 struct sockaddr_in sockAddr;
327 bool isBound = false;
328 uint16_t serverPort = *port;
330 memset(&sockAddr, 0, sizeof(sockAddr));
331 sockAddr.sin_family = AF_INET;
332 sockAddr.sin_port = htons(serverPort);
335 sockAddr.sin_addr.s_addr = inet_addr(localIp);
339 sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
343 for (i = 0; i < CA_UDP_BIND_RETRY_COUNT; i++)
345 if (-1 == bind(sock, (struct sockaddr *) &sockAddr,
348 if (false == forceStart)
350 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to bind socket[%s]. Trying again..",
353 //Set the port to next one
355 sockAddr.sin_port = htons(serverPort);
360 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to bind socket[%s]!",
370 if (false == isBound)
373 return CA_STATUS_FAILED;
381 static CAResult_t CACloseSocket(int32_t *socketFD)
383 if (socketFD == NULL)
385 return CA_STATUS_INVALID_PARAM;
390 OIC_LOG(INFO, ETHERNET_SERVER_TAG, "Server not running");
391 return CA_SERVER_NOT_STARTED;
395 if (-1 == close(*socketFD))
397 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to close the socket, Error code: %s\n",
399 return CA_STATUS_FAILED;
406 static CAResult_t CAStartUnicastServer(const char *localAddress, uint16_t *port,
407 const bool forceStart, bool isSecured, int32_t *serverFD)
409 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
411 if (serverFD == NULL)
413 return CA_STATUS_INVALID_PARAM;
416 CAResult_t ret = CACreateSocket(serverFD, localAddress, port, forceStart);
417 if (CA_STATUS_OK != ret)
419 OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Failed to create unicast socket");
424 * The task to listen for data from unicast socket is added to the thread pool.
425 * This is a blocking call is made where we try to receive some data..
426 * We will keep waiting until some data is received.
427 * This task will be terminated when thread pool is freed on stopping the adapters.
428 * Thread context will be freed by thread on exit.
430 CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *)
431 OICMalloc(sizeof(CAAdapterReceiveThreadContext_t));
434 OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Out of memory!");
436 return CA_MEMORY_ALLOC_FAILED;
439 ctx->stopFlag = &g_stopUnicast;
440 ctx->socket_fd = *serverFD;
441 ctx->type = isSecured ? CA_SECURED_UNICAST_SERVER : CA_UNICAST_SERVER;
442 if (CA_STATUS_OK != u_thread_pool_add_task(g_threadPool, CAReceiveHandler, (void *)ctx))
444 OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Failed to create read thread!");
448 return CA_STATUS_FAILED;
451 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
455 static void CAEthernetServerDestroyMutex(void)
457 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
459 if (g_mutexUnicastServer)
461 u_mutex_free(g_mutexUnicastServer);
462 g_mutexUnicastServer = NULL;
466 if (g_mutexSecureUnicastServer)
468 u_mutex_free(g_mutexSecureUnicastServer);
469 g_mutexSecureUnicastServer = NULL;
473 if (g_mutexMulticastServer)
475 u_mutex_free(g_mutexMulticastServer);
476 g_mutexMulticastServer = NULL;
479 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
482 static CAResult_t CAEthernetServerCreateMutex(void)
484 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
486 if(g_mutexUnicastServer) {
487 OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "mutex is already created!");
489 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
490 return CA_STATUS_FAILED;
493 g_mutexUnicastServer = u_mutex_new();
494 if (!g_mutexUnicastServer)
496 OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Failed to created mutex!");
497 return CA_STATUS_FAILED;
501 g_mutexSecureUnicastServer = u_mutex_new();
502 if (!g_mutexSecureUnicastServer)
504 OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Failed to created mutex!");
506 CAEthernetServerDestroyMutex();
507 return CA_STATUS_FAILED;
511 g_mutexMulticastServer = u_mutex_new();
512 if (!g_mutexMulticastServer)
514 OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Failed to created mutex!");
516 CAEthernetServerDestroyMutex();
517 return CA_STATUS_FAILED;
520 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
524 CAResult_t CAEthernetInitializeServer(const u_thread_pool_t threadPool)
526 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
529 VERIFY_NON_NULL(threadPool, ETHERNET_SERVER_TAG, "Thread pool handle is NULL");
532 if (CA_STATUS_OK != CAEthernetServerCreateMutex())
534 OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Failed to create mutex!");
535 return CA_STATUS_FAILED;
538 g_threadPool = threadPool;
540 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
544 void CAEthernetTerminateServer(void)
546 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
551 CAEthernetServerDestroyMutex();
553 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
556 CAResult_t CAEthernetStartUnicastServer(const char *localAddress, uint16_t *port,
557 bool forceStart, bool isSecured, int32_t *serverFD)
559 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
562 VERIFY_NON_NULL(localAddress, ETHERNET_SERVER_TAG, "localAddress");
563 VERIFY_NON_NULL(port, ETHERNET_SERVER_TAG, "port");
564 VERIFY_NON_NULL(serverFD, ETHERNET_SERVER_TAG, "server socket FD");
568 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Invalid input: port is invalid!");
569 return CA_STATUS_INVALID_PARAM;
573 if (false == isSecured)
575 u_mutex_lock(g_mutexUnicastServer);
576 if (-1 != g_unicastServerSocketFD)
578 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Unicast Server is Started Already!",
579 CA_SERVER_STARTED_ALREADY);
581 *serverFD = g_unicastServerSocketFD;
582 u_mutex_unlock(g_mutexUnicastServer);
583 return CA_SERVER_STARTED_ALREADY;
586 g_stopUnicast = false;
587 if (CA_STATUS_OK != CAStartUnicastServer(localAddress, port, forceStart, isSecured,
588 &g_unicastServerSocketFD))
590 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to start unicast server!");
591 g_unicastServerSocketFD = -1;
592 u_mutex_unlock(g_mutexUnicastServer);
593 return CA_STATUS_FAILED;
596 *serverFD = g_unicastServerSocketFD;
597 u_mutex_unlock(g_mutexUnicastServer);
600 else // Start unicast server for secured communication
602 u_mutex_lock(g_mutexSecureUnicastServer);
603 if (-1 != g_secureUnicastServerSocketFD)
605 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Unicast Server is Started Already!",
606 CA_SERVER_STARTED_ALREADY);
608 *serverFD = g_secureUnicastServerSocketFD;
609 u_mutex_unlock(g_mutexSecureUnicastServer);
610 return CA_SERVER_STARTED_ALREADY;
613 g_stopSecureUnicast = false;
614 if (CA_STATUS_OK != CAStartUnicastServer(localAddress, port, forceStart, isSecured,
615 &g_secureUnicastServerSocketFD))
617 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to start unicast server!");
618 g_secureUnicastServerSocketFD = -1;
619 u_mutex_unlock(g_mutexSecureUnicastServer);
620 return CA_STATUS_FAILED;
623 *serverFD = g_secureUnicastServerSocketFD;
624 u_mutex_unlock(g_mutexSecureUnicastServer);
627 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
631 CAResult_t CAEthernetStartMulticastServer(const char *localAddress,
632 const char *multicastAddress, const uint16_t multicastPort, int32_t *serverFD)
634 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
637 VERIFY_NON_NULL(localAddress, ETHERNET_SERVER_TAG, "localAddress");
638 VERIFY_NON_NULL(multicastAddress, ETHERNET_SERVER_TAG, "port");
639 VERIFY_NON_NULL(serverFD, ETHERNET_SERVER_TAG, "server socket FD");
641 uint16_t port = multicastPort;
644 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Invalid input: Multicast port is invalid!");
645 return CA_STATUS_INVALID_PARAM;
648 u_mutex_lock(g_mutexMulticastServer);
650 if (g_multicastServerSocketFD != -1)
652 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Multicast Server is already running!");
653 u_mutex_unlock(g_mutexMulticastServer);
654 return CA_SERVER_STARTED_ALREADY;
657 CAResult_t ret = CACreateSocket(&g_multicastServerSocketFD, multicastAddress, &port, true);
658 if (ret != CA_STATUS_OK)
660 OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Failed to create multicast socket");
661 u_mutex_unlock(g_mutexMulticastServer);
665 // Add membership to receiving socket (join group)
666 memset(&g_multicastMemberReq, 0, sizeof(struct ip_mreq));
667 g_multicastMemberReq.imr_interface.s_addr = inet_addr(localAddress);
668 inet_aton(multicastAddress, &g_multicastMemberReq.imr_multiaddr);
670 if (-1 == setsockopt(g_multicastServerSocketFD, IPPROTO_IP, IP_ADD_MEMBERSHIP,
671 (char *) &g_multicastMemberReq,
672 sizeof(struct ip_mreq)))
674 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to add to multicast group, Error code: %s\n",
676 close(g_multicastServerSocketFD);
677 g_multicastServerSocketFD = -1;
678 u_mutex_unlock(g_mutexMulticastServer);
679 return CA_STATUS_FAILED;
683 * The task to listen to data from multicastcast socket is added to the thread pool.
684 * This is a blocking call is made where we try to receive some data.
685 * We will keep waiting until some data is received.
686 * This task will be terminated when thread pool is freed on stopping the adapters.
687 * Thread context will be freed by thread on exit.
689 CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *)
690 OICMalloc(sizeof(CAAdapterReceiveThreadContext_t));
693 OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Out of memory!");
694 close(g_multicastServerSocketFD);
695 g_multicastServerSocketFD = -1;
696 return CA_MEMORY_ALLOC_FAILED;
699 ctx->stopFlag = &g_stopMulticast;
700 ctx->socket_fd = g_multicastServerSocketFD;
701 ctx->type = CA_MULTICAST_SERVER;
703 g_stopMulticast = false;
704 if (CA_STATUS_OK != u_thread_pool_add_task(g_threadPool, CAReceiveHandler, (void *)ctx))
706 OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "thread_pool_add_task failed!");
708 close(g_multicastServerSocketFD);
709 g_multicastServerSocketFD = -1;
710 g_stopMulticast = true;
711 u_mutex_unlock(g_mutexMulticastServer);
712 return CA_STATUS_FAILED;
715 *serverFD = g_multicastServerSocketFD;
716 strncpy(g_multicastServerInterface, localAddress, IPNAMESIZE);
717 u_mutex_unlock(g_mutexMulticastServer);
719 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
723 CAResult_t CAEthernetStopUnicastServer()
725 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
727 u_mutex_lock(g_mutexUnicastServer);
728 g_stopUnicast = true;
729 CAResult_t ret = CACloseSocket(&g_unicastServerSocketFD);
730 u_mutex_unlock(g_mutexUnicastServer);
732 OIC_LOG_V(INFO, ETHERNET_SERVER_TAG, "Unicast server stopped [%d]", ret);
737 CAResult_t CAEthernetStopSecureUnicastServer()
739 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
741 u_mutex_lock(g_mutexSecureUnicastServer);
742 g_stopSecureUnicast = true;
743 CAResult_t ret = CACloseSocket(&g_secureUnicastServerSocketFD);
744 u_mutex_unlock(g_mutexSecureUnicastServer);
746 OIC_LOG_V(INFO, ETHERNET_SERVER_TAG, "Secured unicast server stopped [%d]", ret);
751 CAResult_t CAEthernetStopMulticastServer(void)
753 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
755 u_mutex_lock(g_mutexMulticastServer);
757 if (g_multicastServerSocketFD == -1)
759 OIC_LOG(INFO, ETHERNET_SERVER_TAG, "Multicast server is not yet started");
760 u_mutex_unlock(g_mutexMulticastServer);
761 return CA_SERVER_NOT_STARTED;
764 g_stopMulticast = true;
766 // leave the group after you are done
767 if (-1 == setsockopt(g_multicastServerSocketFD, IPPROTO_IP, IP_DROP_MEMBERSHIP,
768 (char *)&g_multicastMemberReq,
769 sizeof(struct ip_mreq)))
771 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to leave multicast group, Error code: %s\n",
775 CAResult_t ret = CACloseSocket(&g_multicastServerSocketFD);
776 u_mutex_unlock(g_mutexMulticastServer);
778 OIC_LOG_V(INFO, ETHERNET_SERVER_TAG, "Multicast server stopped [%d]", ret);
782 CAResult_t CAEthernetGetUnicastServerInfo(bool isSecured,
783 char **ipAddress, uint16_t *port, int32_t *serverFD)
785 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
788 VERIFY_NON_NULL(ipAddress, ETHERNET_SERVER_TAG, "IP address");
789 VERIFY_NON_NULL(port, ETHERNET_SERVER_TAG, "Port");
790 VERIFY_NON_NULL(serverFD, ETHERNET_SERVER_TAG, "Server ID");
792 struct sockaddr_in sockAddr;
793 socklen_t len = sizeof(struct sockaddr_in);
794 if (-1 == getsockname(g_unicastServerSocketFD, (struct sockaddr *)&sockAddr, &len))
796 OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed in getsockname [%s]!", strerror(errno));
797 return CA_STATUS_FAILED;
801 const char *serverAddress = inet_ntoa(sockAddr.sin_addr);
802 *ipAddress = (serverAddress) ? strndup(serverAddress, strlen(serverAddress)) : NULL;
803 *port = ntohs(sockAddr.sin_port);
805 *serverFD = (true == isSecured) ? g_secureUnicastServerSocketFD : g_unicastServerSocketFD;
807 *serverFD = g_unicastServerSocketFD;
810 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
814 void CAEthernetSetPacketReceiveCallback(CAEthernetPacketReceivedCallback callback)
816 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
817 g_packetReceivedCallback = callback;
818 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
821 void CAEthernetSetExceptionCallback(CAEthernetExceptionCallback callback)
823 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
824 g_exceptionCallback = callback;
825 OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");