updated the code related ipadapter more clearly
authorjihwan.seo <jihwan.seo@samsung.com>
Tue, 22 Dec 2015 07:10:59 +0000 (16:10 +0900)
committerJon A. Cruz <jonc@osg.samsung.com>
Wed, 13 Jan 2016 07:33:33 +0000 (07:33 +0000)
Change-Id: Iff3bf220017bc447ef0e73c8f25f92bb50aa9539
Signed-off-by: jihwan.seo <jihwan.seo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4699
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
resource/csdk/connectivity/src/ip_adapter/caipadapter.c
resource/csdk/connectivity/src/ip_adapter/caipserver.c

index 4bb258f..ccefc9a 100644 (file)
@@ -52,7 +52,7 @@ typedef struct
     void *data;
     uint32_t dataLen;
     bool isMulticast;
-} CAIPData;
+} CAIPData_t;
 
 /**
  * Queue handle for Send Data.
@@ -90,10 +90,11 @@ static void CAIPDeinitializeQueueHandles();
 
 static void CAIPSendDataThread(void *threadData);
 
-static CAIPData *CACreateIPData(const CAEndpoint_t *remoteEndpoint,
-                                const void *data, uint32_t dataLength,
-                                bool isMulticast);
-void CAFreeIPData(CAIPData *ipData);
+static CAIPData_t *CACreateIPData(const CAEndpoint_t *remoteEndpoint,
+                                  const void *data, uint32_t dataLength,
+                                  bool isMulticast);
+
+void CAFreeIPData(CAIPData_t *ipData);
 
 static void CADataDestroyer(void *data, uint32_t size);
 
@@ -356,12 +357,11 @@ CAResult_t CAStopIPListeningServer()
 
 CAResult_t CAStartIPDiscoveryServer()
 {
-    OIC_LOG(DEBUG, TAG, "IN");
     return CAStartIPListeningServer();
 }
 
 static int32_t CAQueueIPData(bool isMulticast, const CAEndpoint_t *endpoint,
-                            const void *data, uint32_t dataLength)
+                             const void *data, uint32_t dataLength)
 {
     OIC_LOG(DEBUG, TAG, "IN");
 
@@ -383,14 +383,14 @@ static int32_t CAQueueIPData(bool isMulticast, const CAEndpoint_t *endpoint,
 
     VERIFY_NON_NULL_RET(g_sendQueueHandle, TAG, "sendQueueHandle", -1);
     // Create IPData to add to queue
-    CAIPData *ipData = CACreateIPData(endpoint, data, dataLength, isMulticast);
+    CAIPData_t *ipData = CACreateIPData(endpoint, data, dataLength, isMulticast);
     if (!ipData)
     {
         OIC_LOG(ERROR, TAG, "Failed to create ipData!");
         return -1;
     }
     // Add message to send queue
-    CAQueueingThreadAddData(g_sendQueueHandle, ipData, sizeof(CAIPData));
+    CAQueueingThreadAddData(g_sendQueueHandle, ipData, sizeof(CAIPData_t));
 
 #endif // SINGLE_THREAD
 
@@ -468,7 +468,7 @@ void CAIPSendDataThread(void *threadData)
 {
     OIC_LOG(DEBUG, TAG, "IN");
 
-    CAIPData *ipData = (CAIPData *) threadData;
+    CAIPData_t *ipData = (CAIPData_t *) threadData;
     if (!ipData)
     {
         OIC_LOG(DEBUG, TAG, "Invalid ip data!");
@@ -485,7 +485,7 @@ void CAIPSendDataThread(void *threadData)
     {
         //Processing for sending unicast
 #ifdef __WITH_DTLS__
-        if (ipData->remoteEndpoint->flags & CA_SECURE)
+        if (ipData->remoteEndpoint && ipData->remoteEndpoint->flags & CA_SECURE)
         {
             OIC_LOG(DEBUG, TAG, "CAAdapterNetDtlsEncrypt called!");
             CAResult_t result = CAAdapterNetDtlsEncrypt(ipData->remoteEndpoint,
@@ -514,12 +514,12 @@ void CAIPSendDataThread(void *threadData)
 
 #ifndef SINGLE_THREAD
 
-CAIPData *CACreateIPData(const CAEndpoint_t *remoteEndpoint, const void *data,
-                                     uint32_t dataLength, bool isMulticast)
+CAIPData_t *CACreateIPData(const CAEndpoint_t *remoteEndpoint, const void *data,
+                           uint32_t dataLength, bool isMulticast)
 {
     VERIFY_NON_NULL_RET(data, TAG, "IPData is NULL", NULL);
 
-    CAIPData *ipData = (CAIPData *) OICMalloc(sizeof(CAIPData));
+    CAIPData_t *ipData = (CAIPData_t *) OICMalloc(sizeof(*ipData));
     if (!ipData)
     {
         OIC_LOG(ERROR, TAG, "Memory allocation failed!");
@@ -543,7 +543,7 @@ CAIPData *CACreateIPData(const CAEndpoint_t *remoteEndpoint, const void *data,
     return ipData;
 }
 
-void CAFreeIPData(CAIPData *ipData)
+void CAFreeIPData(CAIPData_t *ipData)
 {
     VERIFY_NON_NULL_VOID(ipData, TAG, "ipData is NULL");
 
@@ -554,11 +554,11 @@ void CAFreeIPData(CAIPData *ipData)
 
 void CADataDestroyer(void *data, uint32_t size)
 {
-    if (size < sizeof(CAIPData))
+    if (size < sizeof(CAIPData_t))
     {
         OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %d", data, size);
     }
-    CAIPData *etdata = (CAIPData *) data;
+    CAIPData_t *etdata = (CAIPData_t *) data;
 
     CAFreeIPData(etdata);
 }
index 49bfdbf..9f5fe0c 100644 (file)
@@ -47,9 +47,8 @@
 #include "oic_malloc.h"
 #include "oic_string.h"
 
-/**
- * @def TAG
- * @brief Logging tag for module name
+/*
+ * Logging tag for module name
  */
 #define TAG "IP_SERVER"
 
@@ -335,9 +334,9 @@ void CAIPPullData()
 static int CACreateSocket(int family, uint16_t *port)
 {
     int socktype = SOCK_DGRAM;
-    #ifdef SOCK_CLOEXEC
+#ifdef SOCK_CLOEXEC
     socktype |= SOCK_CLOEXEC;
-    #endif
+#endif
     int fd = socket(family, socktype, IPPROTO_UDP);
     if (-1 == fd)
     {
@@ -345,7 +344,7 @@ static int CACreateSocket(int family, uint16_t *port)
         return -1;
     }
 
-    #ifndef SOCK_CLOEXEC
+#ifndef SOCK_CLOEXEC
     int fl = fcntl(fd, F_GETFD);
     if (-1 == fl || -1 == fcntl(fd, F_SETFD, fl|FD_CLOEXEC))
     {
@@ -353,7 +352,7 @@ static int CACreateSocket(int family, uint16_t *port)
         close(fd);
         return -1;
     }
-    #endif
+#endif
 
     struct sockaddr_storage sa = { .ss_family = family };
     socklen_t socklen;
@@ -648,9 +647,8 @@ static void applyMulticastToInterface4(struct in_addr inaddr)
 
 static void applyMulticast6(int fd, struct in6_addr *addr, uint32_t interface)
 {
-    struct ipv6_mreq mreq;
-    mreq.ipv6mr_multiaddr = *addr;
-    mreq.ipv6mr_interface = interface;
+    struct ipv6_mreq mreq = {.ipv6mr_multiaddr = *addr, .ipv6mr_interface = interface};
+
     if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof (mreq)))
     {
         if (EADDRINUSE != errno)
@@ -773,9 +771,14 @@ CAResult_t CAIPStopListenServer()
 
 static void CAProcessNewInterface(CAInterface_t *ifitem)
 {
+    if (!ifitem)
+    {
+        OIC_LOG(DEBUG, TAG, "ifitem is null");
+        return;
+    }
+
     applyMulticastToInterface6(ifitem->index);
-    struct in_addr inaddr;
-    inaddr.s_addr = ifitem->ipv4addr;
+    struct in_addr inaddr = { .s_addr = ifitem->ipv4addr };
     applyMulticastToInterface4(inaddr);
 }
 static void CAHandleNetlink()
@@ -857,6 +860,12 @@ static void sendData(int fd, const CAEndpoint_t *endpoint,
 {
     OIC_LOG(DEBUG, TAG, "IN");
 
+    if (!endpoint)
+    {
+        OIC_LOG(DEBUG, TAG, "endpoint is null");
+        return;
+    }
+
     char *secure = (endpoint->flags & CA_SECURE) ? "secure " : "";
     (void)secure;   // eliminates release warning
     struct sockaddr_storage sock;
@@ -895,6 +904,12 @@ static void sendMulticastData6(const u_arraylist_t *iflist,
                                CAEndpoint_t *endpoint,
                                const void *data, uint32_t datalen)
 {
+    if (!endpoint)
+    {
+        OIC_LOG(DEBUG, TAG, "endpoint is null");
+        return;
+    }
+
     int scope = endpoint->flags & CA_SCOPE_MASK;
     char *ipv6mcname = ipv6mcnames[scope];
     if (!ipv6mcname)
@@ -936,6 +951,8 @@ static void sendMulticastData4(const u_arraylist_t *iflist,
                                CAEndpoint_t *endpoint,
                                const void *data, uint32_t datalen)
 {
+    VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
+
     struct ip_mreqn mreq = { .imr_multiaddr = IPv4MulticastAddress,
                              .imr_ifindex = 0 };
     OICStrcpy(endpoint->addr, sizeof(endpoint->addr), IPv4_MULTICAST);
@@ -971,7 +988,7 @@ static void sendMulticastData4(const u_arraylist_t *iflist,
 }
 
 void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
-                                                            bool isMulticast)
+                  bool isMulticast)
 {
     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
     VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
@@ -1011,17 +1028,17 @@ void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t datalen,
         if (caglobals.ip.ipv6enabled && (endpoint->flags & CA_IPV6))
         {
             fd = isSecure ? caglobals.ip.u6s.fd : caglobals.ip.u6.fd;
-            #ifndef __WITH_DTLS__
+#ifndef __WITH_DTLS__
             fd = caglobals.ip.u6.fd;
-            #endif
+#endif
             sendData(fd, endpoint, data, datalen, "unicast", "ipv6");
         }
         if (caglobals.ip.ipv4enabled && (endpoint->flags & CA_IPV4))
         {
             fd = isSecure ? caglobals.ip.u4s.fd : caglobals.ip.u4.fd;
-            #ifndef __WITH_DTLS__
+#ifndef __WITH_DTLS__
             fd = caglobals.ip.u4.fd;
-            #endif
+#endif
             sendData(fd, endpoint, data, datalen, "unicast", "ipv4");
         }
     }