error msg
[oweals/gnunet.git] / src / transport / plugin_transport_http_common.c
index 5c6d197919f70fae5cb2b2d8f2f0ea85c3b489c4..bf983302e2da40bc5d8c15870173a3ab390ea440 100644 (file)
@@ -37,6 +37,18 @@ struct SplittedHTTPAddress
        int port;
 };
 
+static void
+http_clean_splitted (struct SplittedHTTPAddress *spa)
+{
+       if (NULL != spa)
+       {
+                       GNUNET_free_non_null (spa->protocol);
+                       GNUNET_free_non_null (spa->host);
+                       GNUNET_free_non_null (spa->path);
+                       GNUNET_free_non_null (spa);
+       }
+}
+
 struct SplittedHTTPAddress *
 http_split_address (const char * addr)
 {
@@ -47,7 +59,6 @@ http_split_address (const char * addr)
        char *v6_end = NULL;
        char *port_start = NULL;
        char *path_start = NULL;
-
        protocol_start = src;
        sp = GNUNET_malloc (sizeof (struct SplittedHTTPAddress));
 
@@ -103,6 +114,14 @@ http_split_address (const char * addr)
                                                        port_start[0] = '\0';
                                                        port_start ++;
                                                        sp->port = atoi (port_start);
+                                                       if ((0 == sp->port) || (65535 < sp->port))
+                                                       {
+                                                               GNUNET_free (src);
+                                                               GNUNET_free (sp->protocol);
+                                                               GNUNET_free (sp->path);
+                                                               GNUNET_free (sp);
+                                                               return NULL;
+                                                       }
                                        }
                                        else
                                        {
@@ -119,6 +138,14 @@ http_split_address (const char * addr)
                                        port_start[0] = '\0';
                                        port_start ++;
                                        sp->port = atoi (port_start);
+                                       if ((0 == sp->port) || (65535 < sp->port))
+                                       {
+                                               GNUNET_free (src);
+                                               GNUNET_free (sp->protocol);
+                                               GNUNET_free (sp->path);
+                                               GNUNET_free (sp);
+                                               return NULL;
+                                       }
                        }
        }
        else
@@ -149,7 +176,7 @@ http_split_address (const char * addr)
                        GNUNET_free (sp);
                        return NULL;
        }
-       //fprintf (stderr, "addr: `%s' protocol: `%s', host `%s' port `%u' path `%s'\n", addr, sp->protocol, sp->host, sp->port, sp->path);
+       GNUNET_free (src);
        return sp;
 }
 
@@ -274,11 +301,10 @@ http_common_address_from_socket (const char *protocol, const struct sockaddr *ad
 struct sockaddr *
 http_common_socket_from_address (const void *addr, size_t addrlen, int *res)
 {
+       struct SplittedHTTPAddress * spa;
   struct sockaddr_storage *s;
-  char *addrs;
-  char *addrs_org;
-  char *addrs_end;
   (*res) = GNUNET_SYSERR;
+  char * to_conv;
 
   if (NULL == addr)
     {
@@ -296,50 +322,35 @@ http_common_socket_from_address (const void *addr, size_t addrlen, int *res)
       return NULL;
     }
 
-  addrs_org = strdup ((char *) addr);
-  addrs = strstr (addrs_org , "://");
-  if (NULL == addrs)
-  {
-    GNUNET_break (0);
-    GNUNET_free (addrs_org);
-    return NULL;
-  }
-
-  if (strlen (addrs) < 3)
+  spa = http_split_address (addr);
+  if (NULL == spa)
   {
-    GNUNET_break (0);
-    GNUNET_free (addrs_org);
-    return NULL;
+      (*res) = GNUNET_SYSERR;
+      return NULL;
   }
 
-  addrs += 3;
-
-  addrs_end = strchr (addrs, '/');
-  if (NULL != addrs_end)
-    addrs[strlen (addrs) - strlen(addrs_end)] = '\0';
-
   s = GNUNET_malloc (sizeof (struct sockaddr_storage));
-  if (GNUNET_SYSERR == GNUNET_STRINGS_to_address_ip (addrs, strlen(addrs), s))
+  GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port);
+  if (GNUNET_SYSERR == GNUNET_STRINGS_to_address_ip (to_conv, strlen(to_conv), s))
   {
     /* could be a hostname */
-    GNUNET_free (s);
-    GNUNET_free (addrs_org);
+       GNUNET_free (s);
     (*res) = GNUNET_NO;
-    return NULL;
+    s = NULL;
+  }
+  else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
+  {
+
+               GNUNET_free (s);
+               (*res) = GNUNET_SYSERR;
+               s = NULL;
   }
   else
   {
-    if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
-    {
-      GNUNET_break (0);
-      GNUNET_free (s);
-      GNUNET_free (addrs_org);
-      (*res) = GNUNET_SYSERR;
-      return NULL;
-    }
+               (*res) = GNUNET_YES;
   }
-  (*res) = GNUNET_YES;
-  GNUNET_free (addrs_org);
+       http_clean_splitted (spa);
+  GNUNET_free (to_conv);
   return (struct sockaddr *) s;
 }