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 addr binary address
248 * @param addrlen length of the address
249 * @return string representing the same address
252 http_common_plugin_dnsresult_to_address (const char *plugin,
253 const struct SplittedHTTPAddress *saddr, uint32_t options,
254 const char *dnsresult)
256 static char rbuf[1024];
259 GNUNET_asprintf (&res, "%s.%u.%s://%s:%u%s", plugin, options, saddr->protocol,
260 dnsresult, saddr->port, saddr->path);
261 if (strlen (res) + 1 < 500)
263 memcpy (rbuf, res, strlen (res) + 1);
273 http_common_dns_reverse_lookup_cb (void *cls, const char *hostname)
275 struct PrettyPrinterContext *ppc = cls;
277 if (NULL != hostname)
279 ppc->asc (ppc->asc_cls,
280 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options,
281 hostname), GNUNET_OK);
282 ppc->sucess = GNUNET_YES;
287 ppc->asc (ppc->asc_cls, NULL,
288 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
290 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
291 http_clean_splitted (ppc->saddr);
292 GNUNET_free(ppc->plugin);
298 http_common_dns_reverse_lookup (const struct sockaddr *sockaddr,
299 socklen_t sockaddr_len, const char *type, struct SplittedHTTPAddress *saddr,
301 struct GNUNET_TIME_Relative timeout,
302 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
304 struct PrettyPrinterContext *ppc;
305 ppc = GNUNET_new (struct PrettyPrinterContext);
307 ppc->sucess = GNUNET_NO;
310 ppc->asc_cls = asc_cls;
311 ppc->plugin = GNUNET_strdup (type);
312 ppc->options = options;
314 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sockaddr, sockaddr_len,
315 GNUNET_YES, timeout, &http_common_dns_reverse_lookup_cb, ppc);
316 if (NULL == ppc->resolver_handle)
318 GNUNET_free(ppc->plugin);
320 return GNUNET_SYSERR;
323 GNUNET_CONTAINER_DLL_insert(dll_ppc_head, dll_ppc_tail, ppc);
328 http_common_dns_ip_lookup_cb (void *cls, const struct sockaddr *addr,
331 struct PrettyPrinterContext *ppc = cls;
335 ppc->asc (ppc->asc_cls,
336 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options,
337 GNUNET_a2s (addr, addrlen)), GNUNET_OK);
338 ppc->sucess = GNUNET_YES;
339 ppc->asc (ppc->asc_cls, GNUNET_a2s (addr, addrlen), GNUNET_OK);
343 ppc->asc (ppc->asc_cls, NULL,
344 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
346 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
347 GNUNET_free(ppc->plugin);
348 http_clean_splitted (ppc->saddr);
354 http_common_dns_ip_lookup (const char *name, const char *type,
355 struct SplittedHTTPAddress *saddr,
357 struct GNUNET_TIME_Relative timeout,
358 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
360 struct PrettyPrinterContext *ppc;
361 ppc = GNUNET_new (struct PrettyPrinterContext);
363 ppc->sucess = GNUNET_NO;
366 ppc->asc_cls = asc_cls;
367 ppc->plugin = GNUNET_strdup (type);
368 ppc->options = options;
370 ppc->resolver_handle = GNUNET_RESOLVER_ip_get (name, AF_UNSPEC, timeout,
371 &http_common_dns_ip_lookup_cb, ppc);
372 if (NULL == ppc->resolver_handle)
374 GNUNET_free(ppc->plugin);
376 return GNUNET_SYSERR;
379 GNUNET_CONTAINER_DLL_insert(dll_ppc_head, dll_ppc_tail, ppc);
384 * Convert the transports address to a nice, human-readable
388 * @param type name of the transport that generated the address
389 * @param addr one of the addresses of the host, NULL for the last address
390 * the specific address format depends on the transport
391 * @param addrlen length of the @a addr
392 * @param numeric should (IP) addresses be displayed in numeric form?
393 * @param timeout after how long should we give up?
394 * @param asc function to call on each string
395 * @param asc_cls closure for @a asc
398 http_common_plugin_address_pretty_printer (void *cls, const char *type,
399 const void *addr, size_t addrlen, int numeric,
400 struct GNUNET_TIME_Relative timeout,
401 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
403 const struct HttpAddress *address = addr;
404 struct SplittedHTTPAddress *saddr;
405 struct sockaddr *sock_addr;
413 if ((addrlen < sizeof(struct HttpAddress))
414 || (addrlen != http_common_address_get_size (address)))
420 addr_str = (char *) &address[1];
421 if (addr_str[ntohl (address->urlen) - 1] != '\0')
427 saddr = http_split_address (addr_str);
434 sock_addr = http_common_socket_from_address (addr, addrlen, &res);
435 if (GNUNET_SYSERR == res)
437 /* Malformed address */
441 else if (GNUNET_NO == res)
443 /* Could not convert to IP */
446 else if (GNUNET_YES == res)
448 /* Converted to IP */
449 have_ip = GNUNET_YES;
453 /* Must not happen */
458 if ((GNUNET_YES == numeric) && (GNUNET_YES == have_ip))
460 /* No lookup required */
461 ret = http_common_plugin_address_to_string (type, address, addrlen);
462 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
463 asc (asc_cls, NULL, GNUNET_OK);
464 http_clean_splitted (saddr);
465 GNUNET_free_non_null(sock_addr);
468 else if ((GNUNET_YES == numeric) && (GNUNET_NO == have_ip))
472 == http_common_dns_ip_lookup (saddr->host, type, saddr, address->options, timeout, asc,
478 /* Wait for resolver callback */
481 else if ((GNUNET_NO == numeric) && (GNUNET_YES == have_ip))
485 == http_common_dns_reverse_lookup (sock_addr,
486 (AF_INET == sock_addr->sa_family) ?
487 sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), type,
488 saddr, address->options, timeout, asc, asc_cls))
493 /* Wait for resolver callback */
496 else if ((GNUNET_NO == numeric) && (GNUNET_NO == have_ip))
498 /* No lookup required */
499 ret = http_common_plugin_address_to_string (type, address, addrlen);
500 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
501 asc (asc_cls, NULL, GNUNET_OK);
502 GNUNET_free_non_null(sock_addr);
503 http_clean_splitted (saddr);
515 asc (asc_cls, NULL, GNUNET_SYSERR);
516 asc (asc_cls, NULL, GNUNET_OK);
517 GNUNET_free_non_null(sock_addr);
519 http_clean_splitted (saddr);
528 http_common_plugin_address_to_url (void *cls, const void *addr, size_t addrlen)
530 static char rbuf[1024];
531 const struct HttpAddress *address = addr;
532 const char * addr_str;
544 if (addrlen != http_common_address_get_size (address))
549 addr_str = (char *) &address[1];
551 if (addr_str[ntohl (address->urlen) - 1] != '\0')
554 memcpy (rbuf, &address[1], ntohl (address->urlen));
559 * Function called for a quick conversion of the binary address to
560 * a numeric address. Note that the caller must not free the
561 * address and that the next call to this function is allowed
562 * to override the address again.
564 * @param plugin the name of the plugin
565 * @param addr binary address
566 * @param addrlen length of the address
567 * @return string representing the same address
570 http_common_plugin_address_to_string (const char *plugin, const void *addr,
573 static char rbuf[1024];
574 const struct HttpAddress *address = addr;
575 const char * addr_str;
578 GNUNET_assert(NULL != plugin);
583 if (addrlen != http_common_address_get_size (address))
585 addr_str = (char *) &address[1];
586 if (addr_str[ntohl (address->urlen) - 1] != '\0')
588 GNUNET_asprintf (&res, "%s.%u.%s", plugin, ntohl (address->options),
590 if (strlen (res) + 1 < 500)
592 memcpy (rbuf, res, strlen (res) + 1);
602 * Function called to convert a string address to
605 * @param cls closure ('struct Plugin*')
606 * @param addr string address
607 * @param addrlen length of the @a addr
608 * @param buf location to store the buffer
609 * If the function returns #GNUNET_SYSERR, its contents are undefined.
610 * @param added length of created address
611 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
614 http_common_plugin_string_to_address (void *cls, const char *addr,
615 uint16_t addrlen, void **buf, size_t *added)
617 struct HttpAddress *a;
624 /* Format protocol.options.address:port */
628 if ((NULL == addr) || (addrlen == 0))
631 return GNUNET_SYSERR;
633 if ('\0' != addr[addrlen - 1])
636 return GNUNET_SYSERR;
638 if (strlen (addr) != addrlen - 1)
641 return GNUNET_SYSERR;
643 plugin = GNUNET_strdup (addr);
644 optionstr = strchr (plugin, '.');
645 if (NULL == optionstr)
649 return GNUNET_SYSERR;
653 options = atol (optionstr); /* 0 on conversion error, that's ok */
654 address = strchr (optionstr, '.');
659 return GNUNET_SYSERR;
663 urlen = strlen (address) + 1;
665 a = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
666 a->options = htonl (options);
667 a->urlen = htonl (urlen);
668 memcpy (&a[1], address, urlen);
671 (*added) = sizeof(struct HttpAddress) + urlen;
677 * Create a HTTP address from a socketaddr
679 * @param protocol protocol
680 * @param addr sockaddr * address
681 * @param addrlen length of the address
682 * @return the HttpAddress
685 http_common_address_from_socket (const char *protocol,
686 const struct sockaddr *addr, socklen_t addrlen)
688 struct HttpAddress *address = NULL;
692 GNUNET_asprintf (&res, "%s://%s", protocol, GNUNET_a2s (addr, addrlen));
693 len = strlen (res) + 1;
694 address = GNUNET_malloc (sizeof (struct HttpAddress) + len);
695 address->options = htonl (HTTP_OPTIONS_NONE);
696 address->urlen = htonl (len);
697 memcpy (&address[1], res, len);
703 * Create a socketaddr from a HTTP address
705 * @param addr a `sockaddr *` address
706 * @param addrlen length of the @a addr
707 * @param res the result:
708 * #GNUNET_SYSERR, invalid input,
709 * #GNUNET_YES: could convert to ip,
710 * #GNUNET_NO: valid input but could not convert to ip (hostname?)
714 http_common_socket_from_address (const void *addr, size_t addrlen, int *res)
716 const struct HttpAddress *ha;
717 struct SplittedHTTPAddress * spa;
718 struct sockaddr_storage *s;
722 (*res) = GNUNET_SYSERR;
723 ha = (const struct HttpAddress *) addr;
734 if (addrlen < sizeof(struct HttpAddress))
739 urlen = ntohl (ha->urlen);
740 if (sizeof(struct HttpAddress) + urlen != addrlen)
742 /* This is a legacy addresses */
745 if (addrlen < sizeof(struct HttpAddress) + urlen)
747 /* This is a legacy addresses */
750 if (((char *) addr)[addrlen - 1] != '\0')
755 spa = http_split_address ((const char *) &ha[1]);
758 (*res) = GNUNET_SYSERR;
762 s = GNUNET_new (struct sockaddr_storage);
763 GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port);
765 == GNUNET_STRINGS_to_address_ip (to_conv, strlen (to_conv), s))
767 /* could be a hostname */
772 else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
776 (*res) = GNUNET_SYSERR;
783 http_clean_splitted (spa);
784 GNUNET_free(to_conv);
785 return (struct sockaddr *) s;
789 * Get the length of an address
791 * @param addr address
795 http_common_address_get_size (const struct HttpAddress * addr)
797 return sizeof(struct HttpAddress) + ntohl (addr->urlen);
801 * Compare addr1 to addr2
803 * @param addr1 address1
804 * @param addrlen1 address 1 length
805 * @param addr2 address2
806 * @param addrlen2 address 2 length
807 * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error
810 http_common_cmp_addresses (const void *addr1, size_t addrlen1,
811 const void *addr2, size_t addrlen2)
813 const char *a1 = addr1;
814 const char *a2 = addr2;
815 const struct HttpAddress *ha1;
816 const struct HttpAddress *ha2;
817 ha1 = (const struct HttpAddress *) a1;
818 ha2 = (const struct HttpAddress *) a2;
821 return GNUNET_SYSERR;
823 return GNUNET_SYSERR;
824 if (a1[addrlen1 - 1] != '\0')
825 return GNUNET_SYSERR;
828 return GNUNET_SYSERR;
830 return GNUNET_SYSERR;
831 if (a2[addrlen2 - 1] != '\0')
832 return GNUNET_SYSERR;
834 if (addrlen1 != addrlen2)
836 if (ha1->urlen != ha2->urlen)
839 if (0 == strcmp ((const char *) &ha1[1], (const char *) &ha2[1]))
844 /* end of plugin_transport_http_common.c */