[CA] Assigned unicast port number 0 instead of fixed number
authorkoushik.girijala <g.koushik@samsung.com>
Thu, 18 Jun 2015 08:22:37 +0000 (13:52 +0530)
committerErich Keane <erich.keane@intel.com>
Tue, 23 Jun 2015 16:14:52 +0000 (16:14 +0000)
Fix for jira issue IOT 379

Change-Id: Iaefe63189473fb106c5a43eb8648fc903ed3954e
Signed-off-by: koushik.girijala <g.koushik@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1338
Reviewed-by: Abhishek Sharma <ce.abhishek@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: John Light <john.j.light@intel.com>
Reviewed-by: Erich Keane <erich.keane@intel.com>
resource/csdk/connectivity/inc/caipinterface.h
resource/csdk/connectivity/inc/caprotocolmessage.h
resource/csdk/connectivity/src/caprotocolmessage.c
resource/csdk/connectivity/src/caretransmission_singlethread.c
resource/csdk/connectivity/src/ip_adapter/caipadapter.c
resource/csdk/connectivity/src/ip_adapter/caipadapter_singlethread.c
resource/csdk/connectivity/src/ip_adapter/caipserver.c

index 8d61e90..4a403c2 100644 (file)
@@ -111,8 +111,6 @@ CAResult_t CAIPStartMulticastServer(const char *localAddress, const char *multic
  * @param  localAddress [IN]      Local adapter address to which server to be binded.
  * @param  port         [IN,OUT]  Port number on which server will be running. If binding
  *                                the port failed, server starts in the next available port.
- * @param  forceStart   [IN]      Indicate whether to start server forcesfully on specified port
- *                                or not.
  * @param  secured      [IN]      True if the secure server to be started, otherwise false.
  *
  * @return  #CA_STATUS_OK or Appropriate error code
@@ -121,8 +119,7 @@ CAResult_t CAIPStartMulticastServer(const char *localAddress, const char *multic
  * @retval  #CA_SERVER_STARTED_ALREADY Unicast server is already started and running.
  * @retval  #CA_STATUS_FAILED Operation failed
  */
-CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port, bool forceStart,
-                                  bool secured);
+CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port, bool secured);
 
 /**
  * @brief  Stop servers that are running in particular interface address.
index c190ddf..b71c9b9 100644 (file)
@@ -40,6 +40,15 @@ typedef uint32_t code_t;
 #define CA_RESPONSE_CLASS(C) (((C) >> 5)*100)
 #define CA_RESPONSE_CODE(C) (CA_RESPONSE_CLASS(C) + (C - COAP_RESPONSE_CODE(CA_RESPONSE_CLASS(C))))
 
+
+// Include files from the arduino platform do not provide these conversions:
+#ifdef ARDUINO
+#define htons(x) ( ((x)<< 8 & 0xFF00) | ((x)>> 8 & 0x00FF) )
+#define ntohs(x) htons(x)
+#else
+#define HAVE_TIME_H 1
+#endif
+
 /**
  * @brief   generates pdu structure from the given information.
  * @param   code             [IN]    code of the pdu packet
index 4510963..7c7fefc 100644 (file)
 #define _DEFAULT_SOURCE
 #define _BSD_SOURCE
 
-// Include files from the arduino platform do not provide these conversions:
-#ifdef ARDUINO
-#define htons(x) ( ((x)<< 8 & 0xFF00) | ((x)>> 8 & 0x00FF) )
-#define ntohs(x) htons(x)
-#else
-#define HAVE_TIME_H 1
-#endif
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
index 2665b2c..7a05bef 100644 (file)
@@ -230,7 +230,7 @@ CAResult_t CARetransmissionSentData(CARetransmission_t *context, const CAEndpoin
     if (!(context->config.supportType & endpoint->adapter))
     {
         OIC_LOG(ERROR, TAG, "error");
-        OIC_LOG_V(ERROR, TAG, "not supported conntype=%d", endpoint->transportType);
+        OIC_LOG_V(ERROR, TAG, "not supported conntype=%d", context->config.supportType);
         return CA_NOT_SUPPORTED;
     }
 
index cbb1a41..37b8f6b 100644 (file)
@@ -46,7 +46,7 @@
  * @def CA_PORT
  * @brief Port to listen for incoming data
  */
-#define CA_PORT   6298
+#define CA_PORT   0
 
 /**
  * @def CA_SECURE_PORT
@@ -208,7 +208,7 @@ void CAIPConnectionStateCB(const char *ipAddress, CANetworkStatus_t status)
     if (CA_INTERFACE_UP == status)
     {
         uint16_t port = CA_PORT;
-        CAResult_t ret = CAIPStartUnicastServer(ipAddress, &port, false, false);
+        CAResult_t ret = CAIPStartUnicastServer(ipAddress, &port, false);
         if (CA_STATUS_OK == ret)
         {
             OIC_LOG_V(DEBUG, IP_ADAPTER_TAG, "Unicast server started on %d port", port);
@@ -220,7 +220,7 @@ void CAIPConnectionStateCB(const char *ipAddress, CANetworkStatus_t status)
 
 #ifdef __WITH_DTLS__
         port = CA_SECURE_PORT;
-        ret = CAIPStartUnicastServer(ipAddress, &port, false, true);
+        ret = CAIPStartUnicastServer(ipAddress, &port, true);
         if (CA_STATUS_OK == ret)
         {
             OIC_LOG_V(DEBUG, IP_ADAPTER_TAG, "Secure Unicast server started on %d", port);
@@ -413,7 +413,7 @@ CAResult_t CAStartIP()
             continue;
         }
         uint16_t unicastPort = CA_PORT;
-        ret = CAIPStartUnicastServer(netInfo->ipAddress, &unicastPort, false, false);
+        ret = CAIPStartUnicastServer(netInfo->ipAddress, &unicastPort, false);
         if (CA_STATUS_OK == ret)
         {
             OIC_LOG_V(DEBUG, IP_ADAPTER_TAG, "Unicast server started on %d port",
@@ -422,7 +422,7 @@ CAResult_t CAStartIP()
 
 #ifdef __WITH_DTLS__
         unicastPort = CA_SECURE_PORT;
-        ret = CAIPStartUnicastServer(netInfo->ipAddress, &unicastPort, false, true);
+        ret = CAIPStartUnicastServer(netInfo->ipAddress, &unicastPort, true);
 
         if (CA_STATUS_OK == ret)
         {
index 0a8b6b7..e087234 100644 (file)
@@ -40,7 +40,9 @@
  * @brief Unicast port number (to listen for incoming data on unicast server).
  * Note :- Actual port number may differ based on result of bind() operation.
  */
-#define CA_PORT   6298
+ // Assigning fixed number as port number as Arduino eth and wifi libraries dont have proper api to
+ // return assigned port number
+#define CA_PORT   55555
 
 /**
  * @def CA_SECURE_PORT
index 75364fa..6d28dfa 100644 (file)
@@ -296,8 +296,7 @@ static void CAReceiveHandler(void *data)
     OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
 }
 
-static CAResult_t CACreateSocket(int *socketFD, const char *localIp, uint16_t *port,
-                                 bool forceBindStart)
+static CAResult_t CACreateSocket(int *socketFD, const char *localIp, uint16_t *port)
 {
     VERIFY_NON_NULL(socketFD, IP_SERVER_TAG, "socketFD is NULL");
     VERIFY_NON_NULL(localIp, IP_SERVER_TAG, "localIp is NULL");
@@ -331,7 +330,7 @@ static CAResult_t CACreateSocket(int *socketFD, const char *localIp, uint16_t *p
         return CA_STATUS_FAILED;
     }
 
-    if (true == forceBindStart)
+    if (0 != *port)
     {
         int setOptionOn = SOCKETOPTION;
         if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &setOptionOn,
@@ -353,36 +352,27 @@ static CAResult_t CACreateSocket(int *socketFD, const char *localIp, uint16_t *p
         sockAddr.sin_addr.s_addr = inet_addr(localIp);
     }
 
-    int16_t i = 0;
-    bool isBound = false;
-    for (i = 0; i < CA_UDP_BIND_RETRY_COUNT; i++)
+    if (-1 != bind(sock, (struct sockaddr *) &sockAddr, sizeof(sockAddr)))
     {
-        if (-1 == bind(sock, (struct sockaddr *) &sockAddr, sizeof(sockAddr)))
-        {
-            if (false == forceBindStart)
-            {
-                OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to bind socket[%s]. Trying again..",
-                          strerror(errno));
+        struct sockaddr_in sin;
+        socklen_t len = sizeof(sin);
 
-                //Set the port to next one
-                serverPort += 1;
-                sockAddr.sin_port = htons(serverPort);
-                continue;
-            }
-            else
-            {
-                OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to bind socket[%s]!",
-                          strerror(errno));
-                break;
-            }
+        if (getsockname(sock, (struct sockaddr *)&sin, &len) == -1)
+        {
+            OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to get socket[%s]!",
+                      strerror(errno));
+            close(sock);
+            return CA_STATUS_FAILED;
+        }
+        else
+        {
+            serverPort = (uint16_t) ntohs(sin.sin_port);
         }
-
-        isBound = true;
-        break;
     }
-
-    if (false == isBound)
+    else
     {
+        OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to bind socket[%s]!",
+                  strerror(errno));
         close(sock);
         return CA_STATUS_FAILED;
     }
@@ -409,7 +399,7 @@ static void CACloseSocket(int socketFD)
 }
 
 static CAResult_t CAStartUnicastServer(const char *localAddress, uint16_t *port,
-                                       bool forceBindStart, bool isSecured, int *serverFD)
+                                       bool isSecured, int *serverFD)
 {
     OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
 
@@ -417,7 +407,7 @@ static CAResult_t CAStartUnicastServer(const char *localAddress, uint16_t *port,
     VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
     VERIFY_NON_NULL(port, IP_SERVER_TAG, "port");
 
-    CAResult_t ret = CACreateSocket(serverFD, localAddress, port, forceBindStart);
+    CAResult_t ret = CACreateSocket(serverFD, localAddress, port);
     if (CA_STATUS_OK != ret)
     {
         OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create unicast socket");
@@ -584,8 +574,7 @@ void CAIPTerminateServer()
 
 }
 
-CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port,
-                                        bool forceBindStart, bool isSecured)
+CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port, bool isSecured)
 {
     OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
 
@@ -593,7 +582,7 @@ CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port,
     VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
     VERIFY_NON_NULL(port, IP_SERVER_TAG, "port");
 
-    if (0 >= *port)
+    if (0 > *port)
     {
         OIC_LOG(ERROR, IP_SERVER_TAG, "Invalid input: port is invalid!");
         return CA_STATUS_INVALID_PARAM;
@@ -604,7 +593,7 @@ CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port,
     if (!isUnicastServerStarted)
     {
         int unicastServerFd = -1;
-        if (CA_STATUS_OK != CAStartUnicastServer(localAddress, port, forceBindStart, isSecured,
+        if (CA_STATUS_OK != CAStartUnicastServer(localAddress, port, isSecured,
                                                  &unicastServerFd))
         {
             OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to start unicast server!");
@@ -691,7 +680,7 @@ CAResult_t CAIPStartMulticastServer(const char *localAddress, const char *multic
     if (!isMulticastServerStarted)
     {
         int mulicastServerFd = -1;
-        CAResult_t ret = CACreateSocket(&mulicastServerFd, multicastAddress, &port, true);
+        CAResult_t ret = CACreateSocket(&mulicastServerFd, multicastAddress, &port);
         if (ret != CA_STATUS_OK)
         {
             OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create multicast socket");