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);
275 http_common_dns_reverse_lookup_cb (void *cls, const char *hostname)
277 struct PrettyPrinterContext *ppc = cls;
279 if (NULL != hostname)
281 ppc->asc (ppc->asc_cls,
282 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options,
283 hostname), GNUNET_OK);
284 ppc->sucess = GNUNET_YES;
289 ppc->asc (ppc->asc_cls, NULL,
290 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
292 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
293 http_clean_splitted (ppc->saddr);
294 GNUNET_free(ppc->plugin);
300 http_common_dns_reverse_lookup (const struct sockaddr *sockaddr,
301 socklen_t sockaddr_len, const char *type, struct SplittedHTTPAddress *saddr,
303 struct GNUNET_TIME_Relative timeout,
304 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
306 struct PrettyPrinterContext *ppc;
307 ppc = GNUNET_new (struct PrettyPrinterContext);
309 ppc->sucess = GNUNET_NO;
312 ppc->asc_cls = asc_cls;
313 ppc->plugin = GNUNET_strdup (type);
314 ppc->options = options;
316 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sockaddr, sockaddr_len,
317 GNUNET_YES, timeout, &http_common_dns_reverse_lookup_cb, ppc);
318 if (NULL == ppc->resolver_handle)
320 GNUNET_free(ppc->plugin);
322 return GNUNET_SYSERR;
325 GNUNET_CONTAINER_DLL_insert(dll_ppc_head, dll_ppc_tail, ppc);
330 http_common_dns_ip_lookup_cb (void *cls, const struct sockaddr *addr,
333 struct PrettyPrinterContext *ppc = cls;
337 ppc->asc (ppc->asc_cls,
338 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options,
339 GNUNET_a2s (addr, addrlen)), GNUNET_OK);
340 ppc->sucess = GNUNET_YES;
341 ppc->asc (ppc->asc_cls, GNUNET_a2s (addr, addrlen), GNUNET_OK);
345 ppc->asc (ppc->asc_cls, NULL,
346 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
348 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
349 GNUNET_free(ppc->plugin);
350 http_clean_splitted (ppc->saddr);
356 http_common_dns_ip_lookup (const char *name, const char *type,
357 struct SplittedHTTPAddress *saddr,
359 struct GNUNET_TIME_Relative timeout,
360 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
362 struct PrettyPrinterContext *ppc;
363 ppc = GNUNET_new (struct PrettyPrinterContext);
365 ppc->sucess = GNUNET_NO;
368 ppc->asc_cls = asc_cls;
369 ppc->plugin = GNUNET_strdup (type);
370 ppc->options = options;
372 ppc->resolver_handle = GNUNET_RESOLVER_ip_get (name, AF_UNSPEC, timeout,
373 &http_common_dns_ip_lookup_cb, ppc);
374 if (NULL == ppc->resolver_handle)
376 GNUNET_free(ppc->plugin);
378 return GNUNET_SYSERR;
381 GNUNET_CONTAINER_DLL_insert(dll_ppc_head, dll_ppc_tail, ppc);
386 * Convert the transports address to a nice, human-readable
390 * @param type name of the transport that generated the address
391 * @param addr one of the addresses of the host, NULL for the last address
392 * the specific address format depends on the transport
393 * @param addrlen length of the @a addr
394 * @param numeric should (IP) addresses be displayed in numeric form?
395 * @param timeout after how long should we give up?
396 * @param asc function to call on each string
397 * @param asc_cls closure for @a asc
400 http_common_plugin_address_pretty_printer (void *cls, const char *type,
401 const void *addr, size_t addrlen, int numeric,
402 struct GNUNET_TIME_Relative timeout,
403 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
405 const struct HttpAddress *address = addr;
406 struct SplittedHTTPAddress *saddr;
407 struct sockaddr *sock_addr;
415 if ((addrlen < sizeof(struct HttpAddress))
416 || (addrlen != http_common_address_get_size (address)))
422 addr_str = (char *) &address[1];
423 if (addr_str[ntohl (address->urlen) - 1] != '\0')
429 saddr = http_split_address (addr_str);
436 sock_addr = http_common_socket_from_address (addr, addrlen, &res);
437 if (GNUNET_SYSERR == res)
439 /* Malformed address */
443 else if (GNUNET_NO == res)
445 /* Could not convert to IP */
448 else if (GNUNET_YES == res)
450 /* Converted to IP */
451 have_ip = GNUNET_YES;
455 /* Must not happen */
460 if ((GNUNET_YES == numeric) && (GNUNET_YES == have_ip))
462 /* No lookup required */
463 ret = http_common_plugin_address_to_string (type, address, addrlen);
464 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
465 asc (asc_cls, NULL, GNUNET_OK);
466 http_clean_splitted (saddr);
467 GNUNET_free_non_null(sock_addr);
470 else if ((GNUNET_YES == numeric) && (GNUNET_NO == have_ip))
474 == http_common_dns_ip_lookup (saddr->host, type, saddr, address->options, timeout, asc,
480 /* Wait for resolver callback */
483 else if ((GNUNET_NO == numeric) && (GNUNET_YES == have_ip))
487 == http_common_dns_reverse_lookup (sock_addr,
488 (AF_INET == sock_addr->sa_family) ?
489 sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), type,
490 saddr, address->options, timeout, asc, asc_cls))
495 /* Wait for resolver callback */
498 else if ((GNUNET_NO == numeric) && (GNUNET_NO == have_ip))
500 /* No lookup required */
501 ret = http_common_plugin_address_to_string (type, address, addrlen);
502 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
503 asc (asc_cls, NULL, GNUNET_OK);
504 GNUNET_free_non_null(sock_addr);
505 http_clean_splitted (saddr);
517 asc (asc_cls, NULL, GNUNET_SYSERR);
518 asc (asc_cls, NULL, GNUNET_OK);
519 GNUNET_free_non_null(sock_addr);
521 http_clean_splitted (saddr);
530 http_common_plugin_address_to_url (void *cls, const void *addr, size_t addrlen)
532 static char rbuf[1024];
533 const struct HttpAddress *address = addr;
534 const char * addr_str;
546 if (addrlen != http_common_address_get_size (address))
551 addr_str = (char *) &address[1];
553 if (addr_str[ntohl (address->urlen) - 1] != '\0')
556 memcpy (rbuf, &address[1], ntohl (address->urlen));
561 * Function called for a quick conversion of the binary address to
562 * a numeric address. Note that the caller must not free the
563 * address and that the next call to this function is allowed
564 * to override the address again.
566 * @param plugin the name of the plugin
567 * @param addr binary address
568 * @param addrlen length of the address
569 * @return string representing the same address
572 http_common_plugin_address_to_string (const char *plugin, const void *addr,
575 static char rbuf[1024];
576 const struct HttpAddress *address = addr;
577 const char * addr_str;
580 GNUNET_assert(NULL != plugin);
585 if (addrlen != http_common_address_get_size (address))
587 addr_str = (char *) &address[1];
588 if (addr_str[ntohl (address->urlen) - 1] != '\0')
590 GNUNET_asprintf (&res, "%s.%u.%s", plugin, ntohl (address->options),
592 if (strlen (res) + 1 < 500)
594 memcpy (rbuf, res, strlen (res) + 1);
604 * Function called to convert a string address to
607 * @param cls closure ('struct Plugin*')
608 * @param addr string address
609 * @param addrlen length of the @a addr
610 * @param buf location to store the buffer
611 * If the function returns #GNUNET_SYSERR, its contents are undefined.
612 * @param added length of created address
613 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
616 http_common_plugin_string_to_address (void *cls, const char *addr,
617 uint16_t addrlen, void **buf, size_t *added)
619 struct HttpAddress *a;
626 /* Format protocol.options.address:port */
630 if ((NULL == addr) || (addrlen == 0))
633 return GNUNET_SYSERR;
635 if ('\0' != addr[addrlen - 1])
638 return GNUNET_SYSERR;
640 if (strlen (addr) != addrlen - 1)
643 return GNUNET_SYSERR;
645 plugin = GNUNET_strdup (addr);
646 optionstr = strchr (plugin, '.');
647 if (NULL == optionstr)
651 return GNUNET_SYSERR;
655 options = atol (optionstr); /* 0 on conversion error, that's ok */
656 address = strchr (optionstr, '.');
661 return GNUNET_SYSERR;
665 urlen = strlen (address) + 1;
667 a = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
668 a->options = htonl (options);
669 a->urlen = htonl (urlen);
670 memcpy (&a[1], address, urlen);
673 (*added) = sizeof(struct HttpAddress) + urlen;
679 * Create a HTTP address from a socketaddr
681 * @param protocol protocol
682 * @param addr sockaddr * address
683 * @param addrlen length of the address
684 * @return the HttpAddress
687 http_common_address_from_socket (const char *protocol,
688 const struct sockaddr *addr, socklen_t addrlen)
690 struct HttpAddress *address = NULL;
694 GNUNET_asprintf (&res, "%s://%s", protocol, GNUNET_a2s (addr, addrlen));
695 len = strlen (res) + 1;
696 address = GNUNET_malloc (sizeof (struct HttpAddress) + len);
697 address->options = htonl (HTTP_OPTIONS_NONE);
698 address->urlen = htonl (len);
699 memcpy (&address[1], res, len);
705 * Create a socketaddr from a HTTP address
707 * @param addr a `sockaddr *` address
708 * @param addrlen length of the @a addr
709 * @param res the result:
710 * #GNUNET_SYSERR, invalid input,
711 * #GNUNET_YES: could convert to ip,
712 * #GNUNET_NO: valid input but could not convert to ip (hostname?)
716 http_common_socket_from_address (const void *addr, size_t addrlen, int *res)
718 const struct HttpAddress *ha;
719 struct SplittedHTTPAddress * spa;
720 struct sockaddr_storage *s;
724 (*res) = GNUNET_SYSERR;
725 ha = (const struct HttpAddress *) addr;
736 if (addrlen < sizeof(struct HttpAddress))
741 urlen = ntohl (ha->urlen);
742 if (sizeof(struct HttpAddress) + urlen != addrlen)
744 /* This is a legacy addresses */
747 if (addrlen < sizeof(struct HttpAddress) + urlen)
749 /* This is a legacy addresses */
752 if (((char *) addr)[addrlen - 1] != '\0')
757 spa = http_split_address ((const char *) &ha[1]);
760 (*res) = GNUNET_SYSERR;
764 s = GNUNET_new (struct sockaddr_storage);
765 GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port);
767 == GNUNET_STRINGS_to_address_ip (to_conv, strlen (to_conv), s))
769 /* could be a hostname */
774 else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
778 (*res) = GNUNET_SYSERR;
785 http_clean_splitted (spa);
786 GNUNET_free(to_conv);
787 return (struct sockaddr *) s;
791 * Get the length of an address
793 * @param addr address
797 http_common_address_get_size (const struct HttpAddress * addr)
799 return sizeof(struct HttpAddress) + ntohl (addr->urlen);
803 * Compare addr1 to addr2
805 * @param addr1 address1
806 * @param addrlen1 address 1 length
807 * @param addr2 address2
808 * @param addrlen2 address 2 length
809 * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error
812 http_common_cmp_addresses (const void *addr1, size_t addrlen1,
813 const void *addr2, size_t addrlen2)
815 const char *a1 = addr1;
816 const char *a2 = addr2;
817 const struct HttpAddress *ha1;
818 const struct HttpAddress *ha2;
819 ha1 = (const struct HttpAddress *) a1;
820 ha2 = (const struct HttpAddress *) a2;
823 return GNUNET_SYSERR;
825 return GNUNET_SYSERR;
826 if (a1[addrlen1 - 1] != '\0')
827 return GNUNET_SYSERR;
830 return GNUNET_SYSERR;
832 return GNUNET_SYSERR;
833 if (a2[addrlen2 - 1] != '\0')
834 return GNUNET_SYSERR;
836 if (addrlen1 != addrlen2)
838 if (ha1->urlen != ha2->urlen)
841 if (0 == strcmp ((const char *) &ha1[1], (const char *) &ha2[1]))
846 /* end of plugin_transport_http_common.c */