]> granicus.if.org Git - esp-idf/commitdiff
coap: pass null-terminated string to gethostbyname
authorchenwu <chenwu@espressif.com>
Thu, 28 Jun 2018 09:27:10 +0000 (17:27 +0800)
committerchenwu <chenwu@espressif.com>
Mon, 2 Jul 2018 02:05:03 +0000 (10:05 +0800)
coap client parse a string by `coap_split_uri`,fetch host should by `host.length`
otherwise, would cause `gethostbyname` failed because of inappropriate  parameter.

examples/protocols/coap_client/main/coap_client_example_main.c

index 98f003e1aded7da29a3ff22a30138f91167b630a..e2d1b968451733a49655d497c94ce7920da6c81d 100644 (file)
@@ -79,7 +79,7 @@ static void coap_example_task(void *p)
     coap_pdu_t*       request = NULL;
     const char*       server_uri = COAP_DEFAULT_DEMO_URI;
     uint8_t     get_method = 1;
-
+    char* phostname = NULL;
     while (1) {
         /* Wait for the callback to set the CONNECTED_BIT in the
            event group.
@@ -93,7 +93,16 @@ static void coap_example_task(void *p)
             break;
         }
 
-        hp = gethostbyname((const char *)uri.host.s);
+        phostname = (char *)calloc(1, uri.host.length + 1);
+
+        if (phostname == NULL) {
+            ESP_LOGE(TAG, "calloc failed");
+            continue;
+        }
+
+        memcpy(phostname, uri.host.s, uri.host.length);
+        hp = gethostbyname(phostname);
+        free(phostname);
 
         if (hp == NULL) {
             ESP_LOGE(TAG, "DNS lookup failed");