Modified OCInitUDP to support security.
authorSashi Penta <sashi.kumar.penta@intel.com>
Mon, 10 Nov 2014 21:21:25 +0000 (13:21 -0800)
committerSashi Penta <sashi.kumar.penta@intel.com>
Fri, 14 Nov 2014 04:22:13 +0000 (20:22 -0800)
Added a new flag to OCInitUDP to indicated whether to reuse the address or not.
Modified create_new_context API. It now takes the ipAddr[4] as input. This needs
to be passed further down to dtls_init.

signed-off-by: Sashi Penta <sashi.kumar.penta@intel.com>
Change-Id: I536f215bf2a1ff4c90685e7a19e32fcb76f1e912

resource/csdk/libcoap-4.1.1/net.c
resource/csdk/libcoap-4.1.1/net.h
resource/csdk/libcoap-4.1.1/sec/netdtls.c
resource/csdk/libcoap-4.1.1/sec/netdtls.h
resource/csdk/occoap/src/occoap.c
resource/csdk/ocsocket/include/ocsocket.h
resource/csdk/ocsocket/src/ocsocket.c
resource/csdk/ocsocket/src/ocsocket_arduino.cpp
resource/csdk/ocsocket/src/ocsocket_arduino_wifi.cpp

index b0bc23c..eb1f1eb 100644 (file)
@@ -293,7 +293,11 @@ is_wkc(coap_key_t k) {
 #endif
 
 coap_context_t *
-coap_new_context(const coap_address_t *listen_addr) {
+coap_new_context(uint8_t ipAddr[], uint16_t port) {
+
+    OCDevAddr devAddr;
+    coap_address_t* listen_addr;
+
 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
     coap_context_t *c = (coap_context_t*)coap_malloc( sizeof( coap_context_t ) );
     //int reuse = 1;
@@ -308,6 +312,11 @@ coap_new_context(const coap_address_t *listen_addr) {
     return NULL;
 #endif /* WITH_CONTIKI */
 
+    OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3], port,
+            &devAddr);
+
+    listen_addr = (coap_address_t*) &devAddr;
+
     if (!listen_addr) {
         coap_free(c);
         coap_log(LOG_EMERG, "no listen address specified\n");
@@ -359,13 +368,14 @@ coap_new_context(const coap_address_t *listen_addr) {
     coap_register_option(c, COAP_OPTION_BLOCK1);
 
 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
-    if (OCInitUDP((OCDevAddr *)listen_addr, (int32_t *)&(c->sockfd)) != ERR_SUCCESS) {
+    if (OCInitUDP((OCDevAddr *)listen_addr,
+                    (int32_t *)&(c->sockfd), OC_SOCKET_REUSEADDR) != ERR_SUCCESS) {
         coap_free( c);
         return NULL;
     }
 
 #if defined(WITH_DTLS)
-    if (coap_dtls_init(c) != 0) {
+    if (coap_dtls_init(c, ipAddr) != 0) {
         coap_free( c);
         return NULL;
     }
index f66a4ff..81f8031 100644 (file)
@@ -238,7 +238,7 @@ coap_queue_t *coap_peek_next( coap_context_t *context );
 coap_queue_t *coap_pop_next( coap_context_t *context );
 
 /** Creates a new coap_context_t object that will hold the CoAP stack status.  */
-coap_context_t *coap_new_context(const coap_address_t *listen_addr);
+coap_context_t *coap_new_context(uint8_t ipAddr[], uint16_t port);
 
 /** Joins the CoAP stack to well-known multicast address.  */
 int coap_join_wellknown_group(coap_context_t *context,
index b5e4ee0..b15ed99 100644 (file)
@@ -364,10 +364,12 @@ static int get_psk_credentials(dtls_context_t *ctx,
  *
  * @param ctx - handle to global coap_context_t.
  *
+ * @param ipAddr - ip address.
+ *
  * @return A value less than zero on error, greater or
  *           equal otherwise.
  */
-int coap_dtls_init(coap_context_t *ctx) {
+int coap_dtls_init(coap_context_t *ctx, uint8_t ipAddr[]) {
 
     int ret = -1;
     coap_dtls_context_t *coap_dtls_ctx = NULL;
@@ -384,11 +386,12 @@ int coap_dtls_init(coap_context_t *ctx) {
     memset(coap_dtls_ctx, 0, sizeof(coap_dtls_ctx));
     ctx->sockfd_dtls = -1;
 
-    //TODO : Initialize secure socket descriptor
-    OCBuildIPv4Address(0, 0, 0, 0, COAP_DTLS_DEFAULT_PORT, &dev_addr);
-    if (OCInitUDP((OCDevAddr *)&dev_addr, (int32_t *)&(ctx->sockfd_dtls)) != ERR_SUCCESS) {
-        OCBuildIPv4Address(0, 0, 0, 0, 5685, &dev_addr);
-        if (OCInitUDP((OCDevAddr *)&dev_addr, (int32_t *)&(ctx->sockfd_dtls)) != ERR_SUCCESS) {
+    OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3],
+                                        COAP_DTLS_DEFAULT_PORT, &dev_addr);
+    if (OCInitUDP((OCDevAddr *)&dev_addr, (int32_t *)&(ctx->sockfd_dtls), 0) != ERR_SUCCESS) {
+        OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3],
+                                        COAP_DTLS_RANDOM_PORT, &dev_addr);
+        if (OCInitUDP((OCDevAddr *)&dev_addr, (int32_t *)&(ctx->sockfd_dtls), 0) != ERR_SUCCESS) {
             goto exit;
         }
     }
index 97ecdc0..1348bba 100644 (file)
@@ -27,6 +27,7 @@
 #include "dtls.h"
 
 #define COAP_DTLS_DEFAULT_PORT  5684
+#define COAP_DTLS_RANDOM_PORT   0
 
 /**
  * Data structure for holding the tinyDTLS interface
@@ -65,10 +66,12 @@ typedef enum
  *
  * @param ctx - handle to global coap_context_t.
  *
+ * @param ipAddr - ip address.
+ *
  * @return A value less than zero on error, greater or
  *           equal otherwise.
  */
-int coap_dtls_init(coap_context_t *ctx);
+int coap_dtls_init(coap_context_t *ctx, uint8_t ipAddr[]);
 
 /**
  * Closes secure port and de-inits tinyDTLS library.
index 5c8b26f..709f08d 100644 (file)
@@ -602,7 +602,6 @@ OCStackResult OCInitCoAP(const char *address, uint16_t port, OCMode mode) {
 
     TODO ("Below should go away and be replaced by OC_LOG");
     coap_log_t log_level = (coap_log_t)(LOG_DEBUG + 1);
-    OCDevAddr devAddr;
     OCDevAddr mcastAddr;
     uint8_t ipAddr[4] = { 0 };
     uint16_t parsedPort = 0;
@@ -623,10 +622,7 @@ OCStackResult OCInitCoAP(const char *address, uint16_t port, OCMode mode) {
                                ipAddr[0],ipAddr[1],ipAddr[2],ipAddr[3]);
     }
 
-    OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3], port,
-            &devAddr);
-
-    gCoAPCtx = coap_new_context((coap_address_t*) &devAddr);
+    gCoAPCtx = coap_new_context(ipAddr, port);
     VERIFY_NON_NULL(gCoAPCtx);
 
     // To allow presence notification work we need to init socket gCoAPCtx->sockfd_wellknown
index ed9b412..ec6c0b7 100644 (file)
@@ -87,6 +87,11 @@ typedef struct OCDevAddr {
 //------------------------------------------------------------------------
 int32_t OCInitNetworkStack();
 
+typedef enum
+{
+    OC_SOCKET_NOOPTION = 0,
+    OC_SOCKET_REUSEADDR
+} OC_SOCKET_OPTION;
 
 //-- OCInitUDP -----------------------------------------------------------
 /** @ingroup ocsocket
@@ -98,11 +103,13 @@ int32_t OCInitNetworkStack();
  *              device address with which the new socket will be bind.
  * @param[out] sockfd
  *              reference to the new socket.
+ * @param[in] sockoption
+ *              specifies which socket option to be used.
  *
  * @retval 0 for Success, otherwise some error value
  */
 //------------------------------------------------------------------------
-int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd);
+int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd, OC_SOCKET_OPTION sockoption);
 
 
 
index f2911d3..205eba1 100644 (file)
@@ -172,7 +172,7 @@ exit:
 #endif //__ANDROID__
 
 /// Creates a BSD socket and binds it specified port for UDP
-int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t *sockfd)
+int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t *sockfd, OC_SOCKET_OPTION sockoption)
 {
     int32_t ret = ERR_UNKNOWN;
     int32_t sfd = 0xFFFFFFFF;
@@ -189,11 +189,14 @@ int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t *sockfd)
         goto exit;
     }
 
-    if ((ret = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*) &set_option_on,
-                sizeof(set_option_on))) < 0) {
-        OC_LOG_V(FATAL, MOD_NAME, "setsockopt API failed with errno %s",
-                strerror(errno));
-        goto exit;
+    if(OC_SOCKET_REUSEADDR == sockoption)
+    {
+        if ((ret = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*) &set_option_on,
+                    sizeof(set_option_on))) < 0) {
+            OC_LOG_V(FATAL, MOD_NAME, "setsockopt API failed with errno %s",
+                    strerror(errno));
+            goto exit;
+        }
     }
 
     if ((ret = bind(sfd, (struct sockaddr*)ipAddr->addr, ipAddr->size)) < 0) {
index 4bc94b5..a579bef 100644 (file)
@@ -95,7 +95,7 @@ int32_t OCGetInterfaceAddress(uint8_t* ifName, uint32_t ifNameLen, uint16_t addr
 }
 
 /// Retrieves a empty socket and bind it for UDP with the input port
-int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd)
+int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd, OC_SOCKET_OPTION sockoption)
 {
     uint8_t state;
     ArduinoAddr* ardAddr = (ArduinoAddr*)ipAddr;
index 42a5348..6f42420 100644 (file)
@@ -104,7 +104,7 @@ int32_t OCGetInterfaceAddress(uint8_t* ifName, uint32_t ifNameLen, uint16_t addr
 }
 
 /// Retrieves a empty socket and bind it for UDP with the input port
-int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd)
+int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd, OC_SOCKET_OPTION sockoption)
 {
     ArduinoAddr* ardAddr = (ArduinoAddr*)ipAddr;
     uint8_t sock;
@@ -138,7 +138,7 @@ int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd)
 /// Currently WiFi shield does NOT support multicast.
 int32_t OCInitUDPMulticast(OCDevAddr* ipMcastMacAddr, int32_t* sockfd)
 {
-    return OCInitUDP(ipMcastMacAddr, sockfd);
+    return OCInitUDP(ipMcastMacAddr, sockfd, OC_SOCKET_REUSEADDR);
 }