2 This file is part of GNUnet
3 (C) 2002-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_http_common.c
23 * @brief functionality shared between http(s)client plugins
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_plugin.h"
29 #include "plugin_transport_http_common.h"
30 #include "gnunet_resolver_service.h"
33 http_clean_splitted (struct SplittedHTTPAddress *spa)
37 GNUNET_free_non_null(spa->protocol);
38 GNUNET_free_non_null(spa->host);
39 GNUNET_free_non_null(spa->path);
40 GNUNET_free_non_null(spa);
44 struct SplittedHTTPAddress *
45 http_split_address (const char * addr)
47 struct SplittedHTTPAddress *sp;
48 char *src = GNUNET_strdup (addr);
49 char *protocol_start = NULL;
50 char *host_start = NULL;
52 char *port_start = NULL;
53 char *path_start = NULL;
56 sp = GNUNET_new (struct SplittedHTTPAddress);
57 /* Address string consists of protocol://host[:port]path*/
59 host_start = strstr (src, "://");
60 if (NULL == host_start)
67 sp->protocol = GNUNET_strdup (protocol_start);
69 host_start += strlen ("://");
70 if (strlen (host_start) == 0)
73 GNUNET_free(sp->protocol);
79 path_start = strchr (host_start, '/');
80 if (NULL != path_start)
82 sp->path = GNUNET_strdup (path_start);
86 sp->path = GNUNET_strdup ("");
88 if (strlen (host_start) < 1)
91 GNUNET_free(sp->protocol);
92 GNUNET_free(sp->path);
97 if (NULL != (port_start = strrchr (host_start, ':')))
99 /* *We COULD have a port, but also an IPv6 address! */
100 if (NULL != (v6_end = strchr (host_start, ']')))
102 if (v6_end < port_start)
104 /* IPv6 address + port */
105 port_start[0] = '\0';
107 sp->port = atoi (port_start);
108 if ((0 == sp->port) || (65535 < sp->port))
111 GNUNET_free(sp->protocol);
112 GNUNET_free(sp->path);
119 /* IPv6 address + no port */
120 if (0 == strcmp (sp->protocol, "https"))
121 sp->port = HTTPS_DEFAULT_PORT;
122 else if (0 == strcmp (sp->protocol, "http"))
123 sp->port = HTTP_DEFAULT_PORT;
128 /* No IPv6 address */
129 port_start[0] = '\0';
131 sp->port = atoi (port_start);
132 if ((0 == sp->port) || (65535 < sp->port))
135 GNUNET_free(sp->protocol);
136 GNUNET_free(sp->path);
144 /* No ':' as port separator, default port for protocol */
145 if (0 == strcmp (sp->protocol, "https"))
146 sp->port = HTTPS_DEFAULT_PORT;
147 else if (0 == strcmp (sp->protocol, "http"))
148 sp->port = HTTP_DEFAULT_PORT;
153 GNUNET_free(sp->protocol);
154 GNUNET_free(sp->path);
159 if (strlen (host_start) > 0)
160 sp->host = GNUNET_strdup (host_start);
165 GNUNET_free(sp->protocol);
166 GNUNET_free(sp->path);
175 * Closure for #append_port().
177 struct PrettyPrinterContext
182 struct PrettyPrinterContext *next;
187 struct PrettyPrinterContext *prev;
192 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
195 * Function to call with the result.
197 GNUNET_TRANSPORT_AddressStringCallback asc;
200 * Clsoure for @e asc.
207 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
212 struct SplittedHTTPAddress *saddr;
220 * Was conversion successful
233 static struct PrettyPrinterContext *dll_ppc_head;
238 static struct PrettyPrinterContext *dll_ppc_tail;
241 * Function called for a quick conversion of the binary address to
242 * a numeric address. Note that the caller must not free the
243 * address and that the next call to this function is allowed
244 * to override the address again.
246 * @param plugin the name of the plugin
247 * @param saddr the splitted http address
248 * @param options address options
249 * @param dnsresult dns name to include in address
250 * @return string representing the same address or NULL on error
253 http_common_plugin_dnsresult_to_address (const char *plugin,
254 const struct SplittedHTTPAddress *saddr,
256 const char *dnsresult)
258 static char rbuf[1024];
261 GNUNET_asprintf (&res, "%s.%u.%s://%s:%u%s", plugin, options, saddr->protocol,
262 dnsresult, saddr->port, saddr->path);
263 if (strlen (res) + 1 < 500)
265 memcpy (rbuf, res, strlen (res) + 1);
276 http_common_dns_reverse_lookup_cb (void *cls, const char *hostname)
278 struct PrettyPrinterContext *ppc = cls;
280 if (NULL != hostname)
282 ppc->asc (ppc->asc_cls,
283 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options,
284 hostname), GNUNET_OK);
285 ppc->sucess = GNUNET_YES;
290 ppc->asc (ppc->asc_cls, NULL,
291 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
293 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
294 http_clean_splitted (ppc->saddr);
295 GNUNET_free(ppc->plugin);
302 http_common_dns_reverse_lookup (const struct sockaddr *sockaddr,
303 socklen_t sockaddr_len,
305 struct SplittedHTTPAddress *saddr,
307 struct GNUNET_TIME_Relative timeout,
308 GNUNET_TRANSPORT_AddressStringCallback asc,
311 struct PrettyPrinterContext *ppc;
313 ppc = GNUNET_new (struct PrettyPrinterContext);
316 ppc->asc_cls = asc_cls;
317 ppc->plugin = GNUNET_strdup (type);
318 ppc->options = options;
319 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sockaddr,
323 &http_common_dns_reverse_lookup_cb,
325 if (NULL == ppc->resolver_handle)
327 GNUNET_free(ppc->plugin);
329 return GNUNET_SYSERR;
331 GNUNET_CONTAINER_DLL_insert (dll_ppc_head,
339 http_common_dns_ip_lookup_cb (void *cls,
340 const struct sockaddr *addr,
343 struct PrettyPrinterContext *ppc = cls;
347 ppc->asc (ppc->asc_cls,
348 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options,
349 GNUNET_a2s (addr, addrlen)), GNUNET_OK);
350 ppc->sucess = GNUNET_YES;
351 ppc->asc (ppc->asc_cls, GNUNET_a2s (addr, addrlen), GNUNET_OK);
355 ppc->asc (ppc->asc_cls, NULL,
356 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
358 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
359 GNUNET_free(ppc->plugin);
360 http_clean_splitted (ppc->saddr);
366 http_common_dns_ip_lookup (const char *name, const char *type,
367 struct SplittedHTTPAddress *saddr,
369 struct GNUNET_TIME_Relative timeout,
370 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
372 struct PrettyPrinterContext *ppc;
373 ppc = GNUNET_new (struct PrettyPrinterContext);
375 ppc->sucess = GNUNET_NO;
378 ppc->asc_cls = asc_cls;
379 ppc->plugin = GNUNET_strdup (type);
380 ppc->options = options;
382 ppc->resolver_handle = GNUNET_RESOLVER_ip_get (name, AF_UNSPEC, timeout,
383 &http_common_dns_ip_lookup_cb, ppc);
384 if (NULL == ppc->resolver_handle)
386 GNUNET_free(ppc->plugin);
388 return GNUNET_SYSERR;
391 GNUNET_CONTAINER_DLL_insert(dll_ppc_head, dll_ppc_tail, ppc);
396 * Convert the transports address to a nice, human-readable
400 * @param type name of the transport that generated the address
401 * @param addr one of the addresses of the host, NULL for the last address
402 * the specific address format depends on the transport
403 * @param addrlen length of the @a addr
404 * @param numeric should (IP) addresses be displayed in numeric form?
405 * @param timeout after how long should we give up?
406 * @param asc function to call on each string
407 * @param asc_cls closure for @a asc
410 http_common_plugin_address_pretty_printer (void *cls, const char *type,
414 struct GNUNET_TIME_Relative timeout,
415 GNUNET_TRANSPORT_AddressStringCallback asc,
418 const struct HttpAddress *address = addr;
419 struct SplittedHTTPAddress *saddr;
420 struct sockaddr *sock_addr;
428 if ((addrlen < sizeof(struct HttpAddress))
429 || (addrlen != http_common_address_get_size (address)))
435 addr_str = (char *) &address[1];
436 if (addr_str[ntohl (address->urlen) - 1] != '\0')
442 saddr = http_split_address (addr_str);
449 sock_addr = http_common_socket_from_address (addr, addrlen, &res);
450 if (GNUNET_SYSERR == res)
452 /* Malformed address */
456 else if (GNUNET_NO == res)
458 /* Could not convert to IP */
461 else if (GNUNET_YES == res)
463 /* Converted to IP */
464 have_ip = GNUNET_YES;
468 /* Must not happen */
473 if ((GNUNET_YES == numeric) && (GNUNET_YES == have_ip))
475 /* No lookup required */
476 ret = http_common_plugin_address_to_string (type, address, addrlen);
477 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
478 asc (asc_cls, NULL, GNUNET_OK);
479 http_clean_splitted (saddr);
480 GNUNET_free_non_null (sock_addr);
483 else if ((GNUNET_YES == numeric) && (GNUNET_NO == have_ip))
487 http_common_dns_ip_lookup (saddr->host, type, saddr,
488 address->options, timeout,
494 /* Wait for resolver callback */
495 GNUNET_free_non_null (sock_addr);
498 else if ((GNUNET_NO == numeric) && (GNUNET_YES == have_ip))
502 http_common_dns_reverse_lookup (sock_addr,
503 (AF_INET == sock_addr->sa_family)
504 ? sizeof(struct sockaddr_in)
505 : sizeof(struct sockaddr_in6),
508 address->options, timeout,
514 /* Wait for resolver callback */
515 GNUNET_free_non_null (sock_addr);
518 else if ((GNUNET_NO == numeric) && (GNUNET_NO == have_ip))
520 /* No lookup required */
521 ret = http_common_plugin_address_to_string (type, address, addrlen);
522 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
523 asc (asc_cls, NULL, GNUNET_OK);
524 GNUNET_free_non_null (sock_addr);
525 http_clean_splitted (saddr);
533 GNUNET_free_non_null (sock_addr);
538 asc (asc_cls, NULL, GNUNET_SYSERR);
539 asc (asc_cls, NULL, GNUNET_OK);
540 GNUNET_free_non_null (sock_addr);
542 http_clean_splitted (saddr);
551 http_common_plugin_address_to_url (void *cls, const void *addr, size_t addrlen)
553 static char rbuf[1024];
554 const struct HttpAddress *address = addr;
555 const char * addr_str;
567 if (addrlen != http_common_address_get_size (address))
572 addr_str = (char *) &address[1];
574 if (addr_str[ntohl (address->urlen) - 1] != '\0')
577 memcpy (rbuf, &address[1], ntohl (address->urlen));
582 * Function called for a quick conversion of the binary address to
583 * a numeric address. Note that the caller must not free the
584 * address and that the next call to this function is allowed
585 * to override the address again.
587 * @param plugin the name of the plugin
588 * @param addr binary address
589 * @param addrlen length of the address
590 * @return string representing the same address
593 http_common_plugin_address_to_string (const char *plugin, const void *addr,
596 static char rbuf[1024];
597 const struct HttpAddress *address = addr;
598 const char * addr_str;
601 GNUNET_assert(NULL != plugin);
606 if (addrlen != http_common_address_get_size (address))
608 addr_str = (char *) &address[1];
609 if (addr_str[ntohl (address->urlen) - 1] != '\0')
611 GNUNET_asprintf (&res, "%s.%u.%s", plugin, ntohl (address->options),
613 if (strlen (res) + 1 < 500)
615 memcpy (rbuf, res, strlen (res) + 1);
625 * Function called to convert a string address to
628 * @param cls closure ('struct Plugin*')
629 * @param addr string address
630 * @param addrlen length of the @a addr
631 * @param buf location to store the buffer
632 * If the function returns #GNUNET_SYSERR, its contents are undefined.
633 * @param added length of created address
634 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
637 http_common_plugin_string_to_address (void *cls, const char *addr,
638 uint16_t addrlen, void **buf, size_t *added)
640 struct HttpAddress *a;
647 /* Format protocol.options.address:port */
651 if ((NULL == addr) || (addrlen == 0))
654 return GNUNET_SYSERR;
656 if ('\0' != addr[addrlen - 1])
659 return GNUNET_SYSERR;
661 if (strlen (addr) != addrlen - 1)
664 return GNUNET_SYSERR;
666 plugin = GNUNET_strdup (addr);
667 optionstr = strchr (plugin, '.');
668 if (NULL == optionstr)
672 return GNUNET_SYSERR;
676 options = atol (optionstr); /* 0 on conversion error, that's ok */
677 address = strchr (optionstr, '.');
682 return GNUNET_SYSERR;
686 urlen = strlen (address) + 1;
688 a = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
689 a->options = htonl (options);
690 a->urlen = htonl (urlen);
691 memcpy (&a[1], address, urlen);
694 (*added) = sizeof(struct HttpAddress) + urlen;
700 * Create a HTTP address from a socketaddr
702 * @param protocol protocol
703 * @param addr sockaddr * address
704 * @param addrlen length of the address
705 * @return the HttpAddress
708 http_common_address_from_socket (const char *protocol,
709 const struct sockaddr *addr, socklen_t addrlen)
711 struct HttpAddress *address = NULL;
715 GNUNET_asprintf (&res, "%s://%s", protocol, GNUNET_a2s (addr, addrlen));
716 len = strlen (res) + 1;
717 address = GNUNET_malloc (sizeof (struct HttpAddress) + len);
718 address->options = htonl (HTTP_OPTIONS_NONE);
719 address->urlen = htonl (len);
720 memcpy (&address[1], res, len);
726 * Create a socketaddr from a HTTP address
728 * @param addr a `sockaddr *` address
729 * @param addrlen length of the @a addr
730 * @param res the result:
731 * #GNUNET_SYSERR, invalid input,
732 * #GNUNET_YES: could convert to ip,
733 * #GNUNET_NO: valid input but could not convert to ip (hostname?)
737 http_common_socket_from_address (const void *addr,
741 const struct HttpAddress *ha;
742 struct SplittedHTTPAddress * spa;
743 struct sockaddr_storage *s;
747 (*res) = GNUNET_SYSERR;
748 ha = (const struct HttpAddress *) addr;
759 if (addrlen < sizeof(struct HttpAddress))
764 urlen = ntohl (ha->urlen);
765 if (sizeof(struct HttpAddress) + urlen != addrlen)
767 /* This is a legacy addresses */
770 if (addrlen < sizeof(struct HttpAddress) + urlen)
772 /* This is a legacy addresses */
775 if (((char *) addr)[addrlen - 1] != '\0')
780 spa = http_split_address ((const char *) &ha[1]);
783 (*res) = GNUNET_SYSERR;
787 s = GNUNET_new (struct sockaddr_storage);
788 GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port);
790 == GNUNET_STRINGS_to_address_ip (to_conv, strlen (to_conv), s))
792 /* could be a hostname */
797 else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
800 (*res) = GNUNET_SYSERR;
807 http_clean_splitted (spa);
808 GNUNET_free (to_conv);
809 return (struct sockaddr *) s;
813 * Get the length of an address
815 * @param addr address
819 http_common_address_get_size (const struct HttpAddress * addr)
821 return sizeof(struct HttpAddress) + ntohl (addr->urlen);
825 * Compare addr1 to addr2
827 * @param addr1 address1
828 * @param addrlen1 address 1 length
829 * @param addr2 address2
830 * @param addrlen2 address 2 length
831 * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error
834 http_common_cmp_addresses (const void *addr1, size_t addrlen1,
835 const void *addr2, size_t addrlen2)
837 const char *a1 = addr1;
838 const char *a2 = addr2;
839 const struct HttpAddress *ha1;
840 const struct HttpAddress *ha2;
841 ha1 = (const struct HttpAddress *) a1;
842 ha2 = (const struct HttpAddress *) a2;
845 return GNUNET_SYSERR;
847 return GNUNET_SYSERR;
848 if (a1[addrlen1 - 1] != '\0')
849 return GNUNET_SYSERR;
852 return GNUNET_SYSERR;
854 return GNUNET_SYSERR;
855 if (a2[addrlen2 - 1] != '\0')
856 return GNUNET_SYSERR;
858 if (addrlen1 != addrlen2)
860 if (ha1->urlen != ha2->urlen)
863 if (0 == strcmp ((const char *) &ha1[1], (const char *) &ha2[1]))
868 /* end of plugin_transport_http_common.c */