From 0d3fc76af81e705237ab7ccebe6d5c3c71d1757f Mon Sep 17 00:00:00 2001 From: Matthias Wachs Date: Tue, 28 Aug 2012 14:51:35 +0000 Subject: changes --- src/transport/plugin_transport_http_common.c | 84 ++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'src/transport/plugin_transport_http_common.c') diff --git a/src/transport/plugin_transport_http_common.c b/src/transport/plugin_transport_http_common.c index f13884507..e71b16e93 100644 --- a/src/transport/plugin_transport_http_common.c +++ b/src/transport/plugin_transport_http_common.c @@ -143,6 +143,90 @@ http_common_address_from_socket (const char *protocol, const struct sockaddr *ad return res; } +/** + * Create a socketaddr from a HTTP address + * + * @param protocol protocol + * @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 * -- cgit v1.2.3