new operation queue for limiting overlay connects
[oweals/gnunet.git] / src / transport / plugin_transport_http_common.c
index b1bf7b0deb7e005949c0f953ebae3be9ebf0511a..fc4c4a211aa50d7292c39350bd2750457556ebe2 100644 (file)
@@ -50,8 +50,24 @@ http_common_plugin_address_pretty_printer (void *cls, const char *type,
                                         GNUNET_TRANSPORT_AddressStringCallback
                                         asc, void *asc_cls)
 {
-  GNUNET_break (0);
-  asc (asc_cls, NULL);
+  const char *saddr = (const char *) addr;
+  if (NULL == saddr)
+  {
+      asc (asc_cls, NULL);
+      return;
+  }
+  if (0 >= addrlen)
+  if (NULL == saddr)
+  {
+      asc (asc_cls, NULL);
+      return;
+  }
+  if (saddr[addrlen-1] != '\0')
+  {
+      asc (asc_cls, NULL);
+      return;
+  }
+  asc (asc_cls, saddr);
 }
 
 
@@ -69,8 +85,14 @@ http_common_plugin_address_pretty_printer (void *cls, const char *type,
 const char *
 http_common_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
 {
-  GNUNET_break (0);
-  return NULL;
+  const char *saddr = (const char *) addr;
+  if (NULL == saddr)
+      return NULL;
+  if (0 >= addrlen)
+    return NULL;
+  if (saddr[addrlen-1] != '\0')
+    return NULL;
+  return saddr;
 }
 
 /**
@@ -92,8 +114,166 @@ http_common_plugin_string_to_address (void *cls,
                         void **buf,
                         size_t *added)
 {
-  GNUNET_break (0);
-  return GNUNET_SYSERR;
+  if (NULL == addr)
+      return GNUNET_SYSERR;
+  if (0 >= addrlen)
+    return GNUNET_SYSERR;
+  if (addr[addrlen-1] != '\0')
+    return GNUNET_SYSERR;
+
+  (*buf) = strdup (addr);
+  (*added) = strlen (addr) + 1;
+  return GNUNET_OK;
+}
+
+/**
+ * Create a HTTP address from a socketaddr
+ *
+ * @param protocol protocol
+ * @param addr sockaddr * address
+ * @param addrlen length of the address
+ * @return the string
+ */
+char *
+http_common_address_from_socket (const char *protocol, const struct sockaddr *addr, socklen_t addrlen)
+{
+  char *res;
+  GNUNET_asprintf(&res, "%s://%s", protocol, GNUNET_a2s (addr, addrlen));
+  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
+ *
+ * @param addr address
+ * @return the size
+ */
+size_t
+http_common_address_get_size (const void *addr)
+{
+ return strlen (addr) + 1;
+}
+
+/**
+ * Compare addr1 to addr2
+ *
+ * @param addr1 address1
+ * @param addrlen1 address 1 length
+ * @param addr2 address2
+ * @param addrlen2 address 2 length
+ * @return GNUNET_YES if equal, GNUNET_NO if not, GNUNET_SYSERR on error
+ */
+int
+http_common_cmp_addresses (const void *addr1, size_t addrlen1, const void *addr2, size_t addrlen2)
+{
+  const char *a1 = (const char *) addr1;
+  const char *a2 = (const char *) addr2;
+
+  if (NULL == a1)
+      return GNUNET_SYSERR;
+  if (0 >= addrlen1)
+    return GNUNET_SYSERR;
+  if (a1[addrlen1-1] != '\0')
+    return GNUNET_SYSERR;
+
+  if (NULL == a2)
+      return GNUNET_SYSERR;
+  if (0 >= addrlen2)
+    return GNUNET_SYSERR;
+  if (a2[addrlen2-1] != '\0')
+    return GNUNET_SYSERR;
+
+  if (addrlen1 != addrlen2)
+    return GNUNET_NO;
+
+  if (0 == strcmp (addr1, addr2))
+    return GNUNET_YES;
+  return GNUNET_NO;
 }
 
+
+
 /* end of plugin_transport_http_common.c */