Code portability cleanup.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ethernet_adapter / linux / caethernetserver.c
index 400d746..0b248f2 100644 (file)
 * limitations under the License.
 *
 ******************************************************************/
+
+// Defining _POSIX_C_SOURCE macro with 200809L (or greater) as value
+// causes header files to expose definitions
+// corresponding to the POSIX.1-2008 base
+// specification (excluding the XSI extension).
+// For POSIX.1-2008 base specification,
+// Refer http://pubs.opengroup.org/stage7tc1/
+//
+// For this specific file, see use of strndup,
+// Refer http://man7.org/linux/man-pages/man3/strdup.3.html
+#ifndef _POSIX_C_SOURCE
+#define _POSIX_C_SOURCE 200809L
+#endif
+
+// Defining _BSD_SOURCE or _DEFAULT_SOURCE causes header files to expose
+// definitions that may otherwise be skipped. Skipping can cause implicit
+// declaration warnings and/or bugs and subtle problems in code execution.
+// For glibc information on feature test macros,
+// Refer http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
+//
+// This file requires #define use due to struct ip_mreq
+#define _DEFAULT_SOURCE
+#define _BSD_SOURCE
+#include <netinet/in.h>
+
 #include "caethernetinterface.h"
 
 #include <sys/types.h>
@@ -25,7 +50,8 @@
 #include <fcntl.h>
 #include <sys/select.h>
 #include <arpa/inet.h>
-#include <netinet/in.h>
+#include <string.h>
+
 #include <errno.h>
 
 #include "pdu.h"
@@ -34,6 +60,7 @@
 #include "caadapternetdtls.h"
 #endif
 #include "umutex.h"
+#include "oic_malloc.h"
 
 /**
  * @def ETHERNET_SERVER_TAG
 #define IPNAMESIZE 16
 
 /**
- * @var gUnicastServerSocketFD
+ * @var g_unicastServerSocketFD
  * @brief Unicast server socket descriptor
  */
-static int32_t gUnicastServerSocketFD = -1;
+static int32_t g_unicastServerSocketFD = -1;
 
 /**
- * @var gMutexUnicastServer
+ * @var g_mutexUnicastServer
  * @brief Mutex to synchronize unicast server
  */
-static u_mutex gMutexUnicastServer = NULL;
+static u_mutex g_mutexUnicastServer = NULL;
 
 /**
- * @var gStopUnicast
+ * @var g_stopUnicast
  * @brief Flag to control the Receive Unicast Data Thread
  */
-static bool gStopUnicast = false;
+static bool g_stopUnicast = false;
 
 /**
- * @var gMulticastServerSocketFD
+ * @var g_multicastServerSocketFD
  * @brief socket descriptor for multicast server
  */
-static int32_t gMulticastServerSocketFD = -1;
+static int32_t g_multicastServerSocketFD = -1;
 
 /**
- * @var gMutexMulticastServer
+ * @var g_mutexMulticastServer
  * @brief Mutex to synchronize secure multicast server
  */
-static u_mutex gMutexMulticastServer = NULL;
+static u_mutex g_mutexMulticastServer = NULL;
 
 /**
- * @var gStopMulticast
+ * @var g_stopMulticast
  * @brief Flag to control the Receive Multicast Data Thread
  */
-static bool gStopMulticast = false;
+static bool g_stopMulticast = false;
 
 #ifdef __WITH_DTLS__
 /**
- * @var gSecureUnicastServerSocketFD
+ * @var g_secureUnicastServerSocketFD
  * @brief Secure unicast server socket descriptor
  */
-static int32_t gSecureUnicastServerSocketFD = -1;
+static int32_t g_secureUnicastServerSocketFD = -1;
 
 /**
- * @var gMutexSecureUnicastServer
+ * @var g_mutexSecureUnicastServer
  * @brief Mutex to synchronize secure unicast server
  */
-static u_mutex gMutexSecureUnicastServer = NULL;
+static u_mutex g_mutexSecureUnicastServer = NULL;
 
 /**
- * @var gStopSecureUnicast
+ * @var g_stopSecureUnicast
  * @brief Flag to control the unicast secure data receive thread
  */
-static bool gStopSecureUnicast = false;
+static bool g_stopSecureUnicast = false;
 #endif
 
 /**
- * @var gThreadPool
+ * @var g_threadPool
  * @brief ThreadPool for storing u_thread_pool_t handle passed from adapter
  */
-static u_thread_pool_t gThreadPool = NULL;
+static u_thread_pool_t g_threadPool = NULL;
 
 /**
- * @var gMulticastServerInterface
+ * @var g_multicastServerInterface
  * @brief Local interface on which multicast server is running
  */
-static char gMulticastServerInterface[IPNAMESIZE];
+static char g_multicastServerInterface[IPNAMESIZE];
 
 /**
- * @var gMulticastMemberReq
+ * @var g_multicastMemberReq
  * @brief ip_mreq structure passed to join a multicast group
  */
-static struct ip_mreq gMulticastMemberReq;
+static struct ip_mreq g_multicastMemberReq;
 
 /**
- * @var gPacketReceivedCallback
+ * @var g_packetReceivedCallback
  * @brief Callback for notifying the upper layer on receival data from remote OIC device
  */
-static CAEthernetPacketReceivedCallback gPacketReceivedCallback = NULL;
+static CAEthernetPacketReceivedCallback g_packetReceivedCallback = NULL;
 
 /**
- * @var gExceptionCallback
+ * @var g_exceptionCallback
  * @brief Callback for notifying the upper layer when unicast/multicast server encounters exception
  */
-static CAEthernetExceptionCallback gExceptionCallback = NULL;
+static CAEthernetExceptionCallback g_exceptionCallback = NULL;
 
 /**
  @brief Thread context information for unicast, multicast and secured unicast server
@@ -172,7 +199,8 @@ static void CAReceiveHandler(void *data)
         int32_t ret = select(ctx->socket_fd + 1, &reads, NULL, NULL, &timeout);
         if (*(ctx->stopFlag) == true)
         {
-            OIC_LOG_V(DEBUG, ETHERNET_SERVER_TAG, "Stop request received for [%d] server", ctx->type);
+            OIC_LOG_V(DEBUG, ETHERNET_SERVER_TAG,
+                "Stop request received for [%d] server", ctx->type);
             break;
         }
 
@@ -193,9 +221,12 @@ static void CAReceiveHandler(void *data)
         struct sockaddr_in srcSockAddress;
         int32_t recvLen;
         socklen_t srcAddressLen = sizeof(srcSockAddress);
-        if (-1 == (recvLen = recvfrom(ctx->socket_fd, recvBuffer,
+
+        recvLen = recvfrom(ctx->socket_fd, recvBuffer,
                                       sizeof(recvBuffer), 0, (struct sockaddr *) &srcSockAddress,
-                                      &srcAddressLen)))
+                                      &srcAddressLen);
+
+        if (-1 == recvLen)
         {
             OIC_LOG_V(DEBUG, ETHERNET_SERVER_TAG, "%s", strerror(errno));
             continue;
@@ -205,19 +236,18 @@ static void CAReceiveHandler(void *data)
             OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Server socket shutdown [%d]!", ctx->type);
 
             // Notify upper layer this exception
-            if (gExceptionCallback)
+            if (g_exceptionCallback)
             {
-                gExceptionCallback(ctx->type);
+                g_exceptionCallback(ctx->type);
             }
             OICFree(ctx);
             ctx = NULL;
             return;
         }
 
-        const char *srcIPAddress = NULL;
-        int32_t srcPort = -1;
-
-        srcIPAddress = inet_ntoa(srcSockAddress.sin_addr);
+        char srcIPAddress[CA_IPADDR_SIZE] = {0};
+        inet_ntop(AF_INET, &srcSockAddress.sin_addr.s_addr, srcIPAddress, sizeof(srcIPAddress));
+        uint16_t srcPort = 0;
         srcPort = ntohs(srcSockAddress.sin_port);
 
         OIC_LOG_V(DEBUG, ETHERNET_SERVER_TAG, "Received packet from %s:%d\n",
@@ -232,7 +262,7 @@ static void CAReceiveHandler(void *data)
             continue;
         }
 
-        if (!CAAdapterIsSameSubnet(gMulticastServerInterface, srcIPAddress, netMask))
+        if (!CAAdapterIsSameSubnet(g_multicastServerInterface, srcIPAddress, netMask))
         {
             OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "Packet received from different subnet, Ignore!");
             if (NULL != netMask)
@@ -249,9 +279,9 @@ static void CAReceiveHandler(void *data)
             case CA_UNICAST_SERVER:
             case CA_MULTICAST_SERVER:
                 // Notify data to upper layer
-                if (gPacketReceivedCallback)
+                if (g_packetReceivedCallback)
                 {
-                    gPacketReceivedCallback(srcIPAddress, srcPort, recvBuffer, recvLen, false);
+                    g_packetReceivedCallback(srcIPAddress, srcPort, recvBuffer, recvLen, false);
                 }
                 break;
 #ifdef __WITH_DTLS__
@@ -261,7 +291,8 @@ static void CAReceiveHandler(void *data)
                                      srcPort,
                                      (uint8_t *)recvBuffer,
                                      recvLen, DTLS_ETHERNET);
-                    OIC_LOG_V(DEBUG, ETHERNET_SERVER_TAG, "CAAdapterNetDtlsDecrypt returns [%d]", ret);
+                    OIC_LOG_V(DEBUG, ETHERNET_SERVER_TAG,
+                        "CAAdapterNetDtlsDecrypt returns [%d]", ret);
                 }
                 break;
 #endif //__WITH_DTLS__
@@ -280,12 +311,13 @@ static void CAReceiveHandler(void *data)
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
 }
 
-static CAResult_t CACreateSocket(int32_t *socketFD, const char *localIp, int16_t *port,
-                                 const bool forceStart)
+static CAResult_t CACreateSocket(int32_t *socketFD, const char *localIp, uint16_t *port,
+                                 bool forceStart)
 {
     int32_t sock = -1;
     // Create a UDP socket
-    if (-1 == (sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)))
+    sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+    if (-1 == sock)
     {
         OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to create Socket, Error code: %s",
                   strerror(errno));
@@ -319,9 +351,9 @@ static CAResult_t CACreateSocket(int32_t *socketFD, const char *localIp, int16_t
 
     struct sockaddr_in sockAddr;
     bool isBound = false;
-    int16_t serverPort = *port;
+    uint16_t serverPort = *port;
 
-    memset((char *) &sockAddr, 0, sizeof(sockAddr));
+    memset(&sockAddr, 0, sizeof(sockAddr));
     sockAddr.sin_family = AF_INET;
     sockAddr.sin_port = htons(serverPort);
     if (localIp)
@@ -374,6 +406,11 @@ static CAResult_t CACreateSocket(int32_t *socketFD, const char *localIp, int16_t
 
 static CAResult_t CACloseSocket(int32_t *socketFD)
 {
+    if (socketFD == NULL)
+    {
+        return CA_STATUS_INVALID_PARAM;
+    }
+
     if (-1 == *socketFD)
     {
         OIC_LOG(INFO, ETHERNET_SERVER_TAG, "Server not running");
@@ -392,11 +429,16 @@ static CAResult_t CACloseSocket(int32_t *socketFD)
     return CA_STATUS_OK;
 }
 
-static CAResult_t CAStartUnicastServer(const char *localAddress, int16_t *port,
+static CAResult_t CAStartUnicastServer(const char *localAddress, uint16_t *port,
                                        const bool forceStart, bool isSecured, int32_t *serverFD)
 {
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
 
+    if (serverFD == NULL)
+    {
+        return CA_STATUS_INVALID_PARAM;
+    }
+
     CAResult_t ret = CACreateSocket(serverFD, localAddress, port, forceStart);
     if (CA_STATUS_OK != ret)
     {
@@ -420,14 +462,15 @@ static CAResult_t CAStartUnicastServer(const char *localAddress, int16_t *port,
         return CA_MEMORY_ALLOC_FAILED;
     }
 
-    ctx->stopFlag = &gStopUnicast;
+    ctx->stopFlag = &g_stopUnicast;
     ctx->socket_fd = *serverFD;
     ctx->type = isSecured ? CA_SECURED_UNICAST_SERVER : CA_UNICAST_SERVER;
-    if (CA_STATUS_OK != u_thread_pool_add_task(gThreadPool, CAReceiveHandler, (void *)ctx))
+    if (CA_STATUS_OK != u_thread_pool_add_task(g_threadPool, CAReceiveHandler, (void *)ctx))
     {
         OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Failed to create read thread!");
-        OICFree((void *)ctx);
+        OICFree(ctx);
         close(*serverFD);
+        *serverFD = -1;
         return CA_STATUS_FAILED;
     }
 
@@ -439,24 +482,24 @@ static void CAEthernetServerDestroyMutex(void)
 {
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
 
-    if (gMutexUnicastServer)
+    if (g_mutexUnicastServer)
     {
-        u_mutex_free(gMutexUnicastServer);
-        gMutexUnicastServer = NULL;
+        u_mutex_free(g_mutexUnicastServer);
+        g_mutexUnicastServer = NULL;
     }
 
 #ifdef __WITH_DTLS__
-    if (gMutexSecureUnicastServer)
+    if (g_mutexSecureUnicastServer)
     {
-        u_mutex_free(gMutexSecureUnicastServer);
-        gMutexSecureUnicastServer = NULL;
+        u_mutex_free(g_mutexSecureUnicastServer);
+        g_mutexSecureUnicastServer = NULL;
     }
 #endif
 
-    if (gMutexMulticastServer)
+    if (g_mutexMulticastServer)
     {
-        u_mutex_free(gMutexMulticastServer);
-        gMutexMulticastServer = NULL;
+        u_mutex_free(g_mutexMulticastServer);
+        g_mutexMulticastServer = NULL;
     }
 
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
@@ -466,16 +509,23 @@ static CAResult_t CAEthernetServerCreateMutex(void)
 {
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
 
-    gMutexUnicastServer = u_mutex_new();
-    if (!gMutexUnicastServer)
+    if(g_mutexUnicastServer) {
+        OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "mutex is already created!");
+
+        OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
+        return CA_STATUS_FAILED;
+    }
+
+    g_mutexUnicastServer = u_mutex_new();
+    if (!g_mutexUnicastServer)
     {
         OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Failed to created mutex!");
         return CA_STATUS_FAILED;
     }
 
 #ifdef __WITH_DTLS__
-    gMutexSecureUnicastServer = u_mutex_new();
-    if (!gMutexSecureUnicastServer)
+    g_mutexSecureUnicastServer = u_mutex_new();
+    if (!g_mutexSecureUnicastServer)
     {
         OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Failed to created mutex!");
 
@@ -484,8 +534,8 @@ static CAResult_t CAEthernetServerCreateMutex(void)
     }
 #endif
 
-    gMutexMulticastServer = u_mutex_new();
-    if (!gMutexMulticastServer)
+    g_mutexMulticastServer = u_mutex_new();
+    if (!g_mutexMulticastServer)
     {
         OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Failed to created mutex!");
 
@@ -511,7 +561,7 @@ CAResult_t CAEthernetInitializeServer(const u_thread_pool_t threadPool)
         return CA_STATUS_FAILED;
     }
 
-    gThreadPool = threadPool;
+    g_threadPool = threadPool;
 
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
     return CA_STATUS_OK;
@@ -521,7 +571,7 @@ void CAEthernetTerminateServer(void)
 {
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
 
-    gThreadPool = NULL;
+    g_threadPool = NULL;
 
     // Destroy mutex
     CAEthernetServerDestroyMutex();
@@ -529,9 +579,8 @@ void CAEthernetTerminateServer(void)
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
 }
 
-CAResult_t CAEthernetStartUnicastServer(const char *localAddress, int16_t *port,
-                                        const bool forceStart, const bool isSecured,
-                                        int32_t *serverFD)
+CAResult_t CAEthernetStartUnicastServer(const char *localAddress, uint16_t *port,
+                                        bool forceStart, bool isSecured, int32_t *serverFD)
 {
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
 
@@ -547,66 +596,66 @@ CAResult_t CAEthernetStartUnicastServer(const char *localAddress, int16_t *port,
     }
 
     *serverFD = -1;
-    if (CA_FALSE == isSecured)
+    if (false == isSecured)
     {
-        u_mutex_lock(gMutexUnicastServer);
-        if (-1 != gUnicastServerSocketFD)
+        u_mutex_lock(g_mutexUnicastServer);
+        if (-1 != g_unicastServerSocketFD)
         {
             OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Unicast Server is Started Already!",
                       CA_SERVER_STARTED_ALREADY);
 
-            *serverFD = gUnicastServerSocketFD;
-            u_mutex_unlock(gMutexUnicastServer);
+            *serverFD = g_unicastServerSocketFD;
+            u_mutex_unlock(g_mutexUnicastServer);
             return CA_SERVER_STARTED_ALREADY;
         }
 
-        gStopUnicast = false;
+        g_stopUnicast = false;
         if (CA_STATUS_OK != CAStartUnicastServer(localAddress, port, forceStart, isSecured,
-                &gUnicastServerSocketFD))
+                &g_unicastServerSocketFD))
         {
             OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to start unicast server!");
-            gUnicastServerSocketFD = -1;
-            u_mutex_unlock(gMutexUnicastServer);
+            g_unicastServerSocketFD = -1;
+            u_mutex_unlock(g_mutexUnicastServer);
             return CA_STATUS_FAILED;
         }
 
-        *serverFD = gUnicastServerSocketFD;
-        u_mutex_unlock(gMutexUnicastServer);
+        *serverFD = g_unicastServerSocketFD;
+        u_mutex_unlock(g_mutexUnicastServer);
     }
 #ifdef __WITH_DTLS__
     else // Start unicast server for secured communication
     {
-        u_mutex_lock(gMutexSecureUnicastServer);
-        if (-1 != gSecureUnicastServerSocketFD)
+        u_mutex_lock(g_mutexSecureUnicastServer);
+        if (-1 != g_secureUnicastServerSocketFD)
         {
             OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Unicast Server is Started Already!",
                       CA_SERVER_STARTED_ALREADY);
 
-            *serverFD = gSecureUnicastServerSocketFD;
-            u_mutex_unlock(gMutexSecureUnicastServer);
+            *serverFD = g_secureUnicastServerSocketFD;
+            u_mutex_unlock(g_mutexSecureUnicastServer);
             return CA_SERVER_STARTED_ALREADY;
         }
 
-        gStopSecureUnicast = false;
+        g_stopSecureUnicast = false;
         if (CA_STATUS_OK != CAStartUnicastServer(localAddress, port, forceStart, isSecured,
-                &gSecureUnicastServerSocketFD))
+                &g_secureUnicastServerSocketFD))
         {
             OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to start unicast server!");
-            gSecureUnicastServerSocketFD = -1;
-            u_mutex_unlock(gMutexSecureUnicastServer);
+            g_secureUnicastServerSocketFD = -1;
+            u_mutex_unlock(g_mutexSecureUnicastServer);
             return CA_STATUS_FAILED;
         }
 
-        *serverFD = gSecureUnicastServerSocketFD;
-        u_mutex_unlock(gMutexSecureUnicastServer);
+        *serverFD = g_secureUnicastServerSocketFD;
+        u_mutex_unlock(g_mutexSecureUnicastServer);
     }
 #endif
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
     return CA_STATUS_OK;
 }
 
-CAResult_t CAEthernetStartMulticastServer(const char *localAddress, const char *multicastAddress,
-        const int16_t multicastPort, int32_t *serverFD)
+CAResult_t CAEthernetStartMulticastServer(const char *localAddress,
+    const char *multicastAddress, const uint16_t multicastPort, int32_t *serverFD)
 {
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
 
@@ -615,44 +664,44 @@ CAResult_t CAEthernetStartMulticastServer(const char *localAddress, const char *
     VERIFY_NON_NULL(multicastAddress, ETHERNET_SERVER_TAG, "port");
     VERIFY_NON_NULL(serverFD, ETHERNET_SERVER_TAG, "server socket FD");
 
-    int16_t port = multicastPort;
+    uint16_t port = multicastPort;
     if (0 >= port)
     {
         OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Invalid input: Multicast port is invalid!");
         return CA_STATUS_INVALID_PARAM;
     }
 
-    u_mutex_lock(gMutexMulticastServer);
+    u_mutex_lock(g_mutexMulticastServer);
 
-    if (gMulticastServerSocketFD != -1)
+    if (g_multicastServerSocketFD != -1)
     {
         OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Multicast Server is already running!");
-        u_mutex_unlock(gMutexMulticastServer);
+        u_mutex_unlock(g_mutexMulticastServer);
         return CA_SERVER_STARTED_ALREADY;
     }
 
-    CAResult_t ret = CACreateSocket(&gMulticastServerSocketFD, multicastAddress, &port, true);
+    CAResult_t ret = CACreateSocket(&g_multicastServerSocketFD, multicastAddress, &port, true);
     if (ret != CA_STATUS_OK)
     {
         OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Failed to create multicast socket");
-        u_mutex_unlock(gMutexMulticastServer);
+        u_mutex_unlock(g_mutexMulticastServer);
         return ret;
     }
 
     // Add membership to receiving socket (join group)
-    memset(&gMulticastMemberReq, 0, sizeof(struct ip_mreq));
-    gMulticastMemberReq.imr_interface.s_addr = inet_addr(localAddress);
-    inet_aton(multicastAddress, &gMulticastMemberReq.imr_multiaddr);
+    memset(&g_multicastMemberReq, 0, sizeof(struct ip_mreq));
+    g_multicastMemberReq.imr_interface.s_addr = inet_addr(localAddress);
+    inet_aton(multicastAddress, &g_multicastMemberReq.imr_multiaddr);
 
-    if (-1 == setsockopt(gMulticastServerSocketFD, IPPROTO_IP, IP_ADD_MEMBERSHIP,
-                         (char *) &gMulticastMemberReq,
+    if (-1 == setsockopt(g_multicastServerSocketFD, IPPROTO_IP, IP_ADD_MEMBERSHIP,
+                         (char *) &g_multicastMemberReq,
                          sizeof(struct ip_mreq)))
     {
         OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to add to multicast group, Error code: %s\n",
                   strerror(errno));
-        close(gMulticastServerSocketFD);
-        gMulticastServerSocketFD = -1;
-        u_mutex_unlock(gMutexMulticastServer);
+        close(g_multicastServerSocketFD);
+        g_multicastServerSocketFD = -1;
+        u_mutex_unlock(g_mutexMulticastServer);
         return CA_STATUS_FAILED;
     }
 
@@ -668,30 +717,30 @@ CAResult_t CAEthernetStartMulticastServer(const char *localAddress, const char *
     if (!ctx)
     {
         OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "Out of memory!");
-        close(gMulticastServerSocketFD);
-        gMulticastServerSocketFD = -1;
+        close(g_multicastServerSocketFD);
+        g_multicastServerSocketFD = -1;
         return CA_MEMORY_ALLOC_FAILED;
     }
 
-    ctx->stopFlag = &gStopMulticast;
-    ctx->socket_fd = gMulticastServerSocketFD;
+    ctx->stopFlag = &g_stopMulticast;
+    ctx->socket_fd = g_multicastServerSocketFD;
     ctx->type = CA_MULTICAST_SERVER;
 
-    gStopMulticast = false;
-    if (CA_STATUS_OK != u_thread_pool_add_task(gThreadPool, CAReceiveHandler, (void *)ctx))
+    g_stopMulticast = false;
+    if (CA_STATUS_OK != u_thread_pool_add_task(g_threadPool, CAReceiveHandler, (void *)ctx))
     {
         OIC_LOG(ERROR, ETHERNET_SERVER_TAG, "thread_pool_add_task failed!");
 
-        close(gMulticastServerSocketFD);
-        gMulticastServerSocketFD = -1;
-        gStopMulticast = true;
-        u_mutex_unlock(gMutexMulticastServer);
+        close(g_multicastServerSocketFD);
+        g_multicastServerSocketFD = -1;
+        g_stopMulticast = true;
+        u_mutex_unlock(g_mutexMulticastServer);
         return CA_STATUS_FAILED;
     }
 
-    *serverFD = gMulticastServerSocketFD;
-    strncpy(gMulticastServerInterface, localAddress, IPNAMESIZE);
-    u_mutex_unlock(gMutexMulticastServer);
+    *serverFD = g_multicastServerSocketFD;
+    strncpy(g_multicastServerInterface, localAddress, IPNAMESIZE);
+    u_mutex_unlock(g_mutexMulticastServer);
 
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
     return CA_STATUS_OK;
@@ -701,10 +750,10 @@ CAResult_t CAEthernetStopUnicastServer()
 {
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
 
-    u_mutex_lock(gMutexUnicastServer);
-    gStopUnicast = true;
-    CAResult_t ret = CACloseSocket(&gUnicastServerSocketFD);
-    u_mutex_unlock(gMutexUnicastServer);
+    u_mutex_lock(g_mutexUnicastServer);
+    g_stopUnicast = true;
+    CAResult_t ret = CACloseSocket(&g_unicastServerSocketFD);
+    u_mutex_unlock(g_mutexUnicastServer);
 
     OIC_LOG_V(INFO, ETHERNET_SERVER_TAG, "Unicast server stopped [%d]", ret);
     return ret;
@@ -715,10 +764,10 @@ CAResult_t CAEthernetStopSecureUnicastServer()
 {
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
 
-    u_mutex_lock(gMutexSecureUnicastServer);
-    gStopSecureUnicast = true;
-    CAResult_t ret = CACloseSocket(&gSecureUnicastServerSocketFD);
-    u_mutex_unlock(gMutexSecureUnicastServer);
+    u_mutex_lock(g_mutexSecureUnicastServer);
+    g_stopSecureUnicast = true;
+    CAResult_t ret = CACloseSocket(&g_secureUnicastServerSocketFD);
+    u_mutex_unlock(g_mutexSecureUnicastServer);
 
     OIC_LOG_V(INFO, ETHERNET_SERVER_TAG, "Secured unicast server stopped [%d]", ret);
     return ret;
@@ -729,35 +778,35 @@ CAResult_t CAEthernetStopMulticastServer(void)
 {
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
 
-    u_mutex_lock(gMutexMulticastServer);
+    u_mutex_lock(g_mutexMulticastServer);
 
-    if (gMulticastServerSocketFD == -1)
+    if (g_multicastServerSocketFD == -1)
     {
         OIC_LOG(INFO, ETHERNET_SERVER_TAG, "Multicast server is not yet started");
-        u_mutex_unlock(gMutexMulticastServer);
+        u_mutex_unlock(g_mutexMulticastServer);
         return CA_SERVER_NOT_STARTED;
     }
 
-    gStopMulticast = true;
+    g_stopMulticast = true;
 
     // leave the group after you are done
-    if (-1 == setsockopt(gMulticastServerSocketFD, IPPROTO_IP, IP_DROP_MEMBERSHIP,
-                         (char *)&gMulticastMemberReq,
+    if (-1 == setsockopt(g_multicastServerSocketFD, IPPROTO_IP, IP_DROP_MEMBERSHIP,
+                         (char *)&g_multicastMemberReq,
                          sizeof(struct ip_mreq)))
     {
         OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed to leave multicast group, Error code: %s\n",
                   strerror(errno));
     }
 
-    CAResult_t ret = CACloseSocket(&gMulticastServerSocketFD);
-    u_mutex_unlock(gMutexMulticastServer);
+    CAResult_t ret = CACloseSocket(&g_multicastServerSocketFD);
+    u_mutex_unlock(g_mutexMulticastServer);
 
     OIC_LOG_V(INFO, ETHERNET_SERVER_TAG, "Multicast server stopped [%d]", ret);
     return ret;
 }
 
-CAResult_t CAEthernetGetUnicastServerInfo(const bool isSecured, char **ipAddress, int16_t *port,
-        int32_t *serverFD)
+CAResult_t CAEthernetGetUnicastServerInfo(bool isSecured,
+    char **ipAddress, uint16_t *port, int32_t *serverFD)
 {
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
 
@@ -768,7 +817,7 @@ CAResult_t CAEthernetGetUnicastServerInfo(const bool isSecured, char **ipAddress
 
     struct sockaddr_in sockAddr;
     socklen_t len = sizeof(struct sockaddr_in);
-    if (-1 == getsockname(gUnicastServerSocketFD, (struct sockaddr *)&sockAddr, &len))
+    if (-1 == getsockname(g_unicastServerSocketFD, (struct sockaddr *)&sockAddr, &len))
     {
         OIC_LOG_V(ERROR, ETHERNET_SERVER_TAG, "Failed in getsockname [%s]!", strerror(errno));
         return CA_STATUS_FAILED;
@@ -779,9 +828,9 @@ CAResult_t CAEthernetGetUnicastServerInfo(const bool isSecured, char **ipAddress
     *ipAddress = (serverAddress) ? strndup(serverAddress, strlen(serverAddress)) : NULL;
     *port = ntohs(sockAddr.sin_port);
 #ifdef __WITH_DTLS__
-    *serverFD = (CA_TRUE == isSecured) ? gSecureUnicastServerSocketFD : gUnicastServerSocketFD;
+    *serverFD = (true == isSecured) ? g_secureUnicastServerSocketFD : g_unicastServerSocketFD;
 #else
-    *serverFD = gUnicastServerSocketFD;
+    *serverFD = g_unicastServerSocketFD;
 #endif
 
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
@@ -791,11 +840,14 @@ CAResult_t CAEthernetGetUnicastServerInfo(const bool isSecured, char **ipAddress
 void CAEthernetSetPacketReceiveCallback(CAEthernetPacketReceivedCallback callback)
 {
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
-    gPacketReceivedCallback = callback;
+    g_packetReceivedCallback = callback;
+    OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
 }
 
 void CAEthernetSetExceptionCallback(CAEthernetExceptionCallback callback)
 {
     OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "IN");
-    gExceptionCallback = callback;
+    g_exceptionCallback = callback;
+    OIC_LOG(DEBUG, ETHERNET_SERVER_TAG, "OUT");
 }
+