2 This file is part of GNUnet
3 Copyright (C) 2002-2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file transport/plugin_transport_http_common.c
21 * @brief functionality shared between http(s)client plugins
22 * @author Matthias Wachs
25 #include "gnunet_util_lib.h"
26 #include "gnunet_transport_plugin.h"
27 #include "plugin_transport_http_common.h"
28 #include "gnunet_resolver_service.h"
31 http_clean_splitted (struct SplittedHTTPAddress *spa)
35 GNUNET_free_non_null(spa->protocol);
36 GNUNET_free_non_null(spa->host);
37 GNUNET_free_non_null(spa->path);
38 GNUNET_free_non_null(spa);
43 struct SplittedHTTPAddress *
44 http_split_address (const char * addr)
46 struct SplittedHTTPAddress *sp;
47 char *src = GNUNET_strdup (addr);
48 char *protocol_start = NULL;
49 char *host_start = NULL;
51 char *port_start = NULL;
52 char *path_start = NULL;
55 sp = GNUNET_new (struct SplittedHTTPAddress);
56 /* Address string consists of protocol://host[:port]path*/
58 host_start = strstr (src, "://");
59 if (NULL == host_start)
66 sp->protocol = GNUNET_strdup (protocol_start);
68 host_start += strlen ("://");
69 if (strlen (host_start) == 0)
72 GNUNET_free(sp->protocol);
78 path_start = strchr (host_start, '/');
79 if (NULL != path_start)
81 sp->path = GNUNET_strdup (path_start);
85 sp->path = GNUNET_strdup ("");
87 if (strlen (host_start) < 1)
90 GNUNET_free(sp->protocol);
91 GNUNET_free(sp->path);
96 if (NULL != (port_start = strrchr (host_start, ':')))
98 /* *We COULD have a port, but also an IPv6 address! */
99 if (NULL != (v6_end = strchr (host_start, ']')))
101 if (v6_end < port_start)
103 /* IPv6 address + port */
104 port_start[0] = '\0';
106 sp->port = atoi (port_start);
107 if ((0 == sp->port) || (65535 < sp->port))
110 GNUNET_free(sp->protocol);
111 GNUNET_free(sp->path);
118 /* IPv6 address + no port */
119 if (0 == strcmp (sp->protocol, "https"))
120 sp->port = HTTPS_DEFAULT_PORT;
121 else if (0 == strcmp (sp->protocol, "http"))
122 sp->port = HTTP_DEFAULT_PORT;
127 /* No IPv6 address */
128 port_start[0] = '\0';
130 sp->port = atoi (port_start);
131 if ((0 == sp->port) || (65535 < sp->port))
134 GNUNET_free(sp->protocol);
135 GNUNET_free(sp->path);
143 /* No ':' as port separator, default port for protocol */
144 if (0 == strcmp (sp->protocol, "https"))
145 sp->port = HTTPS_DEFAULT_PORT;
146 else if (0 == strcmp (sp->protocol, "http"))
147 sp->port = HTTP_DEFAULT_PORT;
152 GNUNET_free(sp->protocol);
153 GNUNET_free(sp->path);
158 if (strlen (host_start) > 0)
159 sp->host = GNUNET_strdup (host_start);
164 GNUNET_free(sp->protocol);
165 GNUNET_free(sp->path);
174 * Closure for #append_port().
176 struct PrettyPrinterContext
181 struct PrettyPrinterContext *next;
186 struct PrettyPrinterContext *prev;
191 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
194 * Function to call with the result.
196 GNUNET_TRANSPORT_AddressStringCallback asc;
199 * Clsoure for @e asc.
206 struct GNUNET_SCHEDULER_Task * timeout_task;
211 struct SplittedHTTPAddress *saddr;
219 * Was conversion successful
232 static struct PrettyPrinterContext *dll_ppc_head;
237 static struct PrettyPrinterContext *dll_ppc_tail;
240 * Function called for a quick conversion of the binary address to
241 * a numeric address. Note that the caller must not free the
242 * address and that the next call to this function is allowed
243 * to override the address again.
245 * @param plugin the name of the plugin
246 * @param saddr the splitted http address
247 * @param options address options
248 * @param dnsresult dns name to include in address
249 * @return string representing the same address or NULL on error
252 http_common_plugin_dnsresult_to_address (const char *plugin,
253 const struct SplittedHTTPAddress *saddr,
255 const char *dnsresult)
257 static char rbuf[1024];
260 GNUNET_asprintf (&res, "%s.%u.%s://%s:%u%s", plugin, options, saddr->protocol,
261 dnsresult, saddr->port, saddr->path);
262 if (strlen (res) + 1 < 500)
264 GNUNET_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);
301 http_common_dns_reverse_lookup (const struct sockaddr *sockaddr,
302 socklen_t sockaddr_len,
304 struct SplittedHTTPAddress *saddr,
306 struct GNUNET_TIME_Relative timeout,
307 GNUNET_TRANSPORT_AddressStringCallback asc,
310 struct PrettyPrinterContext *ppc;
312 ppc = GNUNET_new (struct PrettyPrinterContext);
315 ppc->asc_cls = asc_cls;
316 ppc->plugin = GNUNET_strdup (type);
317 ppc->options = options;
318 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sockaddr,
322 &http_common_dns_reverse_lookup_cb,
324 if (NULL == ppc->resolver_handle)
326 GNUNET_free(ppc->plugin);
328 return GNUNET_SYSERR;
330 GNUNET_CONTAINER_DLL_insert (dll_ppc_head,
338 http_common_dns_ip_lookup_cb (void *cls,
339 const struct sockaddr *addr,
342 struct PrettyPrinterContext *ppc = cls;
346 ppc->asc (ppc->asc_cls,
347 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options,
348 GNUNET_a2s (addr, addrlen)), GNUNET_OK);
349 ppc->sucess = GNUNET_YES;
350 ppc->asc (ppc->asc_cls, GNUNET_a2s (addr, addrlen), GNUNET_OK);
354 ppc->asc (ppc->asc_cls, NULL,
355 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
357 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
358 GNUNET_free(ppc->plugin);
359 http_clean_splitted (ppc->saddr);
366 http_common_dns_ip_lookup (const char *name,
368 struct SplittedHTTPAddress *saddr,
370 struct GNUNET_TIME_Relative timeout,
371 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
373 struct PrettyPrinterContext *ppc;
375 ppc = GNUNET_new (struct PrettyPrinterContext);
376 ppc->sucess = GNUNET_NO;
379 ppc->asc_cls = asc_cls;
380 ppc->plugin = GNUNET_strdup (type);
381 ppc->options = options;
382 ppc->resolver_handle = GNUNET_RESOLVER_ip_get (name,
385 &http_common_dns_ip_lookup_cb,
387 if (NULL == ppc->resolver_handle)
389 GNUNET_free(ppc->plugin);
391 return GNUNET_SYSERR;
393 GNUNET_CONTAINER_DLL_insert (dll_ppc_head,
401 * Convert the transports address to a nice, human-readable
405 * @param type name of the transport that generated the address
406 * @param addr one of the addresses of the host, NULL for the last address
407 * the specific address format depends on the transport
408 * @param addrlen length of the @a addr
409 * @param numeric should (IP) addresses be displayed in numeric form?
410 * @param timeout after how long should we give up?
411 * @param asc function to call on each string
412 * @param asc_cls closure for @a asc
415 http_common_plugin_address_pretty_printer (void *cls, const char *type,
419 struct GNUNET_TIME_Relative timeout,
420 GNUNET_TRANSPORT_AddressStringCallback asc,
423 const struct HttpAddress *address = addr;
424 struct SplittedHTTPAddress *saddr;
425 struct sockaddr *sock_addr;
433 if ( (addrlen < sizeof(struct HttpAddress)) ||
434 (addrlen != http_common_address_get_size (address)) )
440 addr_str = (char *) &address[1];
441 if (addr_str[ntohl (address->urlen) - 1] != '\0')
447 saddr = http_split_address (addr_str);
454 sock_addr = http_common_socket_from_address (addr, addrlen, &res);
455 if (GNUNET_SYSERR == res)
457 /* Malformed address */
461 else if (GNUNET_NO == res)
463 /* Could not convert to IP */
466 else if (GNUNET_YES == res)
468 /* Converted to IP */
469 have_ip = GNUNET_YES;
473 /* Must not happen */
478 if ( (GNUNET_YES == numeric) &&
479 (GNUNET_YES == have_ip) )
481 /* No lookup required */
482 ret = http_common_plugin_address_to_string (type, address, addrlen);
483 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
484 asc (asc_cls, NULL, GNUNET_OK);
485 http_clean_splitted (saddr);
486 GNUNET_free_non_null (sock_addr);
489 if ( (GNUNET_YES == numeric) &&
490 (GNUNET_NO == have_ip) )
494 http_common_dns_ip_lookup (saddr->host, type, saddr,
495 address->options, timeout,
501 /* Wait for resolver callback */
502 GNUNET_free_non_null (sock_addr);
505 if ( (GNUNET_NO == numeric) &&
506 (GNUNET_YES == have_ip) )
510 http_common_dns_reverse_lookup (sock_addr,
511 (AF_INET == sock_addr->sa_family)
512 ? sizeof(struct sockaddr_in)
513 : sizeof(struct sockaddr_in6),
516 address->options, timeout,
522 /* Wait for resolver callback */
523 GNUNET_free_non_null (sock_addr);
526 if ( (GNUNET_NO == numeric) &&
527 (GNUNET_NO == have_ip) )
529 /* No lookup required */
530 ret = http_common_plugin_address_to_string (type, address, addrlen);
531 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
532 asc (asc_cls, NULL, GNUNET_OK);
533 GNUNET_free_non_null (sock_addr);
534 http_clean_splitted (saddr);
537 /* Error (argument supplied not GNUNET_YES or GNUNET_NO) */
543 asc (asc_cls, NULL, GNUNET_SYSERR);
544 asc (asc_cls, NULL, GNUNET_OK);
545 GNUNET_free_non_null (sock_addr);
547 http_clean_splitted (saddr);
555 http_common_plugin_address_to_url (void *cls,
559 static char rbuf[1024];
560 const struct HttpAddress *address = addr;
561 const char * addr_str;
573 if (addrlen != http_common_address_get_size (address))
578 addr_str = (char *) &address[1];
579 if (addr_str[ntohl (address->urlen) - 1] != '\0')
584 ntohl (address->urlen));
590 * Function called for a quick conversion of the binary address to
591 * a numeric address. Note that the caller must not free the
592 * address and that the next call to this function is allowed
593 * to override the address again.
595 * @param plugin the name of the plugin
596 * @param addr binary address
597 * @param addrlen length of the address
598 * @return string representing the same address
601 http_common_plugin_address_to_string (const char *plugin,
605 static char rbuf[1024];
606 const struct HttpAddress *address = addr;
607 const char * addr_str;
610 GNUNET_assert(NULL != plugin);
615 if (addrlen != http_common_address_get_size (address))
617 addr_str = (char *) &address[1];
618 if (addr_str[ntohl (address->urlen) - 1] != '\0')
620 GNUNET_asprintf (&res, "%s.%u.%s", plugin, ntohl (address->options),
622 if (strlen (res) + 1 < 500)
624 GNUNET_memcpy (rbuf, res, strlen (res) + 1);
634 * Function called to convert a string address to
637 * @param cls closure ('struct Plugin*')
638 * @param addr string address
639 * @param addrlen length of the @a addr
640 * @param buf location to store the buffer
641 * If the function returns #GNUNET_SYSERR, its contents are undefined.
642 * @param added length of created address
643 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
646 http_common_plugin_string_to_address (void *cls,
652 struct HttpAddress *a;
659 /* Format protocol.options.address:port */
663 if ((NULL == addr) || (addrlen == 0))
666 return GNUNET_SYSERR;
668 if ('\0' != addr[addrlen - 1])
671 return GNUNET_SYSERR;
673 if (strlen (addr) != addrlen - 1)
676 return GNUNET_SYSERR;
678 plugin = GNUNET_strdup (addr);
679 optionstr = strchr (plugin, '.');
680 if (NULL == optionstr)
684 return GNUNET_SYSERR;
688 options = atol (optionstr); /* 0 on conversion error, that's ok */
689 address = strchr (optionstr, '.');
694 return GNUNET_SYSERR;
698 urlen = strlen (address) + 1;
700 a = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
701 a->options = htonl (options);
702 a->urlen = htonl (urlen);
703 GNUNET_memcpy (&a[1], address, urlen);
706 (*added) = sizeof(struct HttpAddress) + urlen;
713 * Create a HTTP address from a socketaddr
715 * @param protocol protocol
716 * @param addr sockaddr * address
717 * @param addrlen length of the address
718 * @return the HttpAddress
721 http_common_address_from_socket (const char *protocol,
722 const struct sockaddr *addr,
725 struct HttpAddress *address = NULL;
729 GNUNET_asprintf (&res,
734 len = strlen (res) + 1;
735 address = GNUNET_malloc (sizeof (struct HttpAddress) + len);
736 address->options = htonl (HTTP_OPTIONS_NONE);
737 address->urlen = htonl (len);
738 GNUNET_memcpy (&address[1], res, len);
745 * Create a socketaddr from a HTTP address
747 * @param addr a `sockaddr *` address
748 * @param addrlen length of the @a addr
749 * @param res the result:
750 * #GNUNET_SYSERR, invalid input,
751 * #GNUNET_YES: could convert to ip,
752 * #GNUNET_NO: valid input but could not convert to ip (hostname?)
756 http_common_socket_from_address (const void *addr,
760 const struct HttpAddress *ha;
761 struct SplittedHTTPAddress * spa;
762 struct sockaddr_storage *s;
766 (*res) = GNUNET_SYSERR;
767 ha = (const struct HttpAddress *) addr;
778 if (addrlen < sizeof(struct HttpAddress))
783 urlen = ntohl (ha->urlen);
784 if (sizeof(struct HttpAddress) + urlen != addrlen)
786 /* This is a legacy addresses */
789 if (addrlen < sizeof(struct HttpAddress) + urlen)
791 /* This is a legacy addresses */
794 if (((char *) addr)[addrlen - 1] != '\0')
799 spa = http_split_address ((const char *) &ha[1]);
802 (*res) = GNUNET_SYSERR;
806 s = GNUNET_new (struct sockaddr_storage);
807 GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port);
809 == GNUNET_STRINGS_to_address_ip (to_conv, strlen (to_conv), s))
811 /* could be a hostname */
816 else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
819 (*res) = GNUNET_SYSERR;
826 http_clean_splitted (spa);
827 GNUNET_free (to_conv);
828 return (struct sockaddr *) s;
833 * Get the length of an address
835 * @param addr address
839 http_common_address_get_size (const struct HttpAddress * addr)
841 return sizeof(struct HttpAddress) + ntohl (addr->urlen);
846 * Compare addr1 to addr2
848 * @param addr1 address1
849 * @param addrlen1 address 1 length
850 * @param addr2 address2
851 * @param addrlen2 address 2 length
852 * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error
855 http_common_cmp_addresses (const void *addr1,
860 const char *a1 = addr1;
861 const char *a2 = addr2;
862 const struct HttpAddress *ha1;
863 const struct HttpAddress *ha2;
864 ha1 = (const struct HttpAddress *) a1;
865 ha2 = (const struct HttpAddress *) a2;
868 return GNUNET_SYSERR;
870 return GNUNET_SYSERR;
871 if (a1[addrlen1 - 1] != '\0')
872 return GNUNET_SYSERR;
875 return GNUNET_SYSERR;
877 return GNUNET_SYSERR;
878 if (a2[addrlen2 - 1] != '\0')
879 return GNUNET_SYSERR;
881 if (addrlen1 != addrlen2)
883 if (ha1->urlen != ha2->urlen)
886 if (0 == strcmp ((const char *) &ha1[1], (const char *) &ha2[1]))
893 * Function obtain the network type for an address.
895 * @param env the environment
896 * @param address the address
897 * @return the network type
899 enum GNUNET_ATS_Network_Type
900 http_common_get_network_for_address (struct GNUNET_TRANSPORT_PluginEnvironment *env,
901 const struct GNUNET_HELLO_Address *address)
905 enum GNUNET_ATS_Network_Type net_type;
909 net_type = GNUNET_ATS_NET_UNSPECIFIED;
910 sa = http_common_socket_from_address (address->address,
911 address->address_length,
913 if (GNUNET_SYSERR == res)
915 if (GNUNET_YES == res)
917 GNUNET_assert (NULL != sa);
918 if (AF_INET == sa->sa_family)
920 salen = sizeof (struct sockaddr_in);
922 else if (AF_INET6 == sa->sa_family)
924 salen = sizeof (struct sockaddr_in6);
926 net_type = env->get_address_type (env->cls,
936 /* end of plugin_transport_http_common.c */