new operation queue for limiting overlay connects
[oweals/gnunet.git] / src / transport / plugin_transport_http_common.c
index f138845074f841e5ac5b99a37740d7d37984ca08..fc4c4a211aa50d7292c39350bd2750457556ebe2 100644 (file)
@@ -63,7 +63,6 @@ http_common_plugin_address_pretty_printer (void *cls, const char *type,
       return;
   }
   if (saddr[addrlen-1] != '\0')
-  if (NULL == saddr)
   {
       asc (asc_cls, NULL);
       return;
@@ -143,6 +142,89 @@ http_common_address_from_socket (const char *protocol, const struct sockaddr *ad
   return res;
 }
 
+/**
+ * Create a socketaddr from a HTTP address
+ *
+ * @param addr sockaddr * address
+ * @param addrlen length of the address
+ * @param res the result:
+ * GNUNET_SYSERR, invalid input,
+ * GNUNET_YES: could convert to ip,
+ * GNUNET_NO: valid input but could not convert to ip (hostname?)
+ * @return the string
+ */
+struct sockaddr *
+http_common_socket_from_address (const void *addr, size_t addrlen, int *res)
+{
+  struct sockaddr_storage *s;
+  char *addrs;
+  char *addrs_org;
+  char *addrs_end;
+  (*res) = GNUNET_SYSERR;
+
+  if (NULL == addr)
+    {
+      GNUNET_break (0);
+      return NULL;
+    }
+  if (0 >= addrlen)
+    {
+      GNUNET_break (0);
+      return NULL;
+    }
+  if (((char *) addr)[addrlen-1] != '\0')
+    {
+      GNUNET_break (0);
+      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)
+  {
+    GNUNET_break (0);
+    GNUNET_free (addrs_org);
+    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))
+  {
+    /* could be a hostname */
+    GNUNET_free (s);
+    GNUNET_free (addrs_org);
+    (*res) = GNUNET_NO;
+    return 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;
+  GNUNET_free (addrs_org);
+  return (struct sockaddr *) s;
+}
+
 /**
  * Get the length of an address
  *