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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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);
45 struct SplittedHTTPAddress *
46 http_split_address (const char * addr)
48 struct SplittedHTTPAddress *sp;
49 char *src = GNUNET_strdup (addr);
50 char *protocol_start = NULL;
51 char *host_start = NULL;
53 char *port_start = NULL;
54 char *path_start = NULL;
57 sp = GNUNET_new (struct SplittedHTTPAddress);
58 /* Address string consists of protocol://host[:port]path*/
60 host_start = strstr (src, "://");
61 if (NULL == host_start)
68 sp->protocol = GNUNET_strdup (protocol_start);
70 host_start += strlen ("://");
71 if (strlen (host_start) == 0)
74 GNUNET_free(sp->protocol);
80 path_start = strchr (host_start, '/');
81 if (NULL != path_start)
83 sp->path = GNUNET_strdup (path_start);
87 sp->path = GNUNET_strdup ("");
89 if (strlen (host_start) < 1)
92 GNUNET_free(sp->protocol);
93 GNUNET_free(sp->path);
98 if (NULL != (port_start = strrchr (host_start, ':')))
100 /* *We COULD have a port, but also an IPv6 address! */
101 if (NULL != (v6_end = strchr (host_start, ']')))
103 if (v6_end < port_start)
105 /* IPv6 address + port */
106 port_start[0] = '\0';
108 sp->port = atoi (port_start);
109 if ((0 == sp->port) || (65535 < sp->port))
112 GNUNET_free(sp->protocol);
113 GNUNET_free(sp->path);
120 /* IPv6 address + no port */
121 if (0 == strcmp (sp->protocol, "https"))
122 sp->port = HTTPS_DEFAULT_PORT;
123 else if (0 == strcmp (sp->protocol, "http"))
124 sp->port = HTTP_DEFAULT_PORT;
129 /* No IPv6 address */
130 port_start[0] = '\0';
132 sp->port = atoi (port_start);
133 if ((0 == sp->port) || (65535 < sp->port))
136 GNUNET_free(sp->protocol);
137 GNUNET_free(sp->path);
145 /* No ':' as port separator, default port for protocol */
146 if (0 == strcmp (sp->protocol, "https"))
147 sp->port = HTTPS_DEFAULT_PORT;
148 else if (0 == strcmp (sp->protocol, "http"))
149 sp->port = HTTP_DEFAULT_PORT;
154 GNUNET_free(sp->protocol);
155 GNUNET_free(sp->path);
160 if (strlen (host_start) > 0)
161 sp->host = GNUNET_strdup (host_start);
166 GNUNET_free(sp->protocol);
167 GNUNET_free(sp->path);
176 * Closure for #append_port().
178 struct PrettyPrinterContext
183 struct PrettyPrinterContext *next;
188 struct PrettyPrinterContext *prev;
193 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
196 * Function to call with the result.
198 GNUNET_TRANSPORT_AddressStringCallback asc;
201 * Clsoure for @e asc.
208 struct GNUNET_SCHEDULER_Task * timeout_task;
213 struct SplittedHTTPAddress *saddr;
221 * Was conversion successful
234 static struct PrettyPrinterContext *dll_ppc_head;
239 static struct PrettyPrinterContext *dll_ppc_tail;
242 * Function called for a quick conversion of the binary address to
243 * a numeric address. Note that the caller must not free the
244 * address and that the next call to this function is allowed
245 * to override the address again.
247 * @param plugin the name of the plugin
248 * @param saddr the splitted http address
249 * @param options address options
250 * @param dnsresult dns name to include in address
251 * @return string representing the same address or NULL on error
254 http_common_plugin_dnsresult_to_address (const char *plugin,
255 const struct SplittedHTTPAddress *saddr,
257 const char *dnsresult)
259 static char rbuf[1024];
262 GNUNET_asprintf (&res, "%s.%u.%s://%s:%u%s", plugin, options, saddr->protocol,
263 dnsresult, saddr->port, saddr->path);
264 if (strlen (res) + 1 < 500)
266 GNUNET_memcpy (rbuf, res, strlen (res) + 1);
277 http_common_dns_reverse_lookup_cb (void *cls, const char *hostname)
279 struct PrettyPrinterContext *ppc = cls;
281 if (NULL != hostname)
283 ppc->asc (ppc->asc_cls,
284 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options,
285 hostname), GNUNET_OK);
286 ppc->sucess = GNUNET_YES;
291 ppc->asc (ppc->asc_cls, NULL,
292 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
294 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
295 http_clean_splitted (ppc->saddr);
296 GNUNET_free(ppc->plugin);
303 http_common_dns_reverse_lookup (const struct sockaddr *sockaddr,
304 socklen_t sockaddr_len,
306 struct SplittedHTTPAddress *saddr,
308 struct GNUNET_TIME_Relative timeout,
309 GNUNET_TRANSPORT_AddressStringCallback asc,
312 struct PrettyPrinterContext *ppc;
314 ppc = GNUNET_new (struct PrettyPrinterContext);
317 ppc->asc_cls = asc_cls;
318 ppc->plugin = GNUNET_strdup (type);
319 ppc->options = options;
320 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sockaddr,
324 &http_common_dns_reverse_lookup_cb,
326 if (NULL == ppc->resolver_handle)
328 GNUNET_free(ppc->plugin);
330 return GNUNET_SYSERR;
332 GNUNET_CONTAINER_DLL_insert (dll_ppc_head,
340 http_common_dns_ip_lookup_cb (void *cls,
341 const struct sockaddr *addr,
344 struct PrettyPrinterContext *ppc = cls;
348 ppc->asc (ppc->asc_cls,
349 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options,
350 GNUNET_a2s (addr, addrlen)), GNUNET_OK);
351 ppc->sucess = GNUNET_YES;
352 ppc->asc (ppc->asc_cls, GNUNET_a2s (addr, addrlen), GNUNET_OK);
356 ppc->asc (ppc->asc_cls, NULL,
357 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
359 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
360 GNUNET_free(ppc->plugin);
361 http_clean_splitted (ppc->saddr);
368 http_common_dns_ip_lookup (const char *name,
370 struct SplittedHTTPAddress *saddr,
372 struct GNUNET_TIME_Relative timeout,
373 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
375 struct PrettyPrinterContext *ppc;
377 ppc = GNUNET_new (struct PrettyPrinterContext);
378 ppc->sucess = GNUNET_NO;
381 ppc->asc_cls = asc_cls;
382 ppc->plugin = GNUNET_strdup (type);
383 ppc->options = options;
384 ppc->resolver_handle = GNUNET_RESOLVER_ip_get (name,
387 &http_common_dns_ip_lookup_cb,
389 if (NULL == ppc->resolver_handle)
391 GNUNET_free(ppc->plugin);
393 return GNUNET_SYSERR;
395 GNUNET_CONTAINER_DLL_insert (dll_ppc_head,
403 * Convert the transports address to a nice, human-readable
407 * @param type name of the transport that generated the address
408 * @param addr one of the addresses of the host, NULL for the last address
409 * the specific address format depends on the transport
410 * @param addrlen length of the @a addr
411 * @param numeric should (IP) addresses be displayed in numeric form?
412 * @param timeout after how long should we give up?
413 * @param asc function to call on each string
414 * @param asc_cls closure for @a asc
417 http_common_plugin_address_pretty_printer (void *cls, const char *type,
421 struct GNUNET_TIME_Relative timeout,
422 GNUNET_TRANSPORT_AddressStringCallback asc,
425 const struct HttpAddress *address = addr;
426 struct SplittedHTTPAddress *saddr;
427 struct sockaddr *sock_addr;
435 if ( (addrlen < sizeof(struct HttpAddress)) ||
436 (addrlen != http_common_address_get_size (address)) )
442 addr_str = (char *) &address[1];
443 if (addr_str[ntohl (address->urlen) - 1] != '\0')
449 saddr = http_split_address (addr_str);
456 sock_addr = http_common_socket_from_address (addr, addrlen, &res);
457 if (GNUNET_SYSERR == res)
459 /* Malformed address */
463 else if (GNUNET_NO == res)
465 /* Could not convert to IP */
468 else if (GNUNET_YES == res)
470 /* Converted to IP */
471 have_ip = GNUNET_YES;
475 /* Must not happen */
480 if ( (GNUNET_YES == numeric) &&
481 (GNUNET_YES == have_ip) )
483 /* No lookup required */
484 ret = http_common_plugin_address_to_string (type, address, addrlen);
485 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
486 asc (asc_cls, NULL, GNUNET_OK);
487 http_clean_splitted (saddr);
488 GNUNET_free_non_null (sock_addr);
491 if ( (GNUNET_YES == numeric) &&
492 (GNUNET_NO == have_ip) )
496 http_common_dns_ip_lookup (saddr->host, type, saddr,
497 address->options, timeout,
503 /* Wait for resolver callback */
504 GNUNET_free_non_null (sock_addr);
507 if ( (GNUNET_NO == numeric) &&
508 (GNUNET_YES == have_ip) )
512 http_common_dns_reverse_lookup (sock_addr,
513 (AF_INET == sock_addr->sa_family)
514 ? sizeof(struct sockaddr_in)
515 : sizeof(struct sockaddr_in6),
518 address->options, timeout,
524 /* Wait for resolver callback */
525 GNUNET_free_non_null (sock_addr);
528 if ( (GNUNET_NO == numeric) &&
529 (GNUNET_NO == have_ip) )
531 /* No lookup required */
532 ret = http_common_plugin_address_to_string (type, address, addrlen);
533 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
534 asc (asc_cls, NULL, GNUNET_OK);
535 GNUNET_free_non_null (sock_addr);
536 http_clean_splitted (saddr);
539 /* Error (argument supplied not GNUNET_YES or GNUNET_NO) */
545 asc (asc_cls, NULL, GNUNET_SYSERR);
546 asc (asc_cls, NULL, GNUNET_OK);
547 GNUNET_free_non_null (sock_addr);
549 http_clean_splitted (saddr);
557 http_common_plugin_address_to_url (void *cls,
561 static char rbuf[1024];
562 const struct HttpAddress *address = addr;
563 const char * addr_str;
575 if (addrlen != http_common_address_get_size (address))
580 addr_str = (char *) &address[1];
581 if (addr_str[ntohl (address->urlen) - 1] != '\0')
586 ntohl (address->urlen));
592 * Function called for a quick conversion of the binary address to
593 * a numeric address. Note that the caller must not free the
594 * address and that the next call to this function is allowed
595 * to override the address again.
597 * @param plugin the name of the plugin
598 * @param addr binary address
599 * @param addrlen length of the address
600 * @return string representing the same address
603 http_common_plugin_address_to_string (const char *plugin,
607 static char rbuf[1024];
608 const struct HttpAddress *address = addr;
609 const char * addr_str;
612 GNUNET_assert(NULL != plugin);
617 if (addrlen != http_common_address_get_size (address))
619 addr_str = (char *) &address[1];
620 if (addr_str[ntohl (address->urlen) - 1] != '\0')
622 GNUNET_asprintf (&res, "%s.%u.%s", plugin, ntohl (address->options),
624 if (strlen (res) + 1 < 500)
626 GNUNET_memcpy (rbuf, res, strlen (res) + 1);
636 * Function called to convert a string address to
639 * @param cls closure ('struct Plugin*')
640 * @param addr string address
641 * @param addrlen length of the @a addr
642 * @param buf location to store the buffer
643 * If the function returns #GNUNET_SYSERR, its contents are undefined.
644 * @param added length of created address
645 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
648 http_common_plugin_string_to_address (void *cls,
654 struct HttpAddress *a;
661 /* Format protocol.options.address:port */
665 if ((NULL == addr) || (addrlen == 0))
668 return GNUNET_SYSERR;
670 if ('\0' != addr[addrlen - 1])
673 return GNUNET_SYSERR;
675 if (strlen (addr) != addrlen - 1)
678 return GNUNET_SYSERR;
680 plugin = GNUNET_strdup (addr);
681 optionstr = strchr (plugin, '.');
682 if (NULL == optionstr)
686 return GNUNET_SYSERR;
690 options = atol (optionstr); /* 0 on conversion error, that's ok */
691 address = strchr (optionstr, '.');
696 return GNUNET_SYSERR;
700 urlen = strlen (address) + 1;
702 a = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
703 a->options = htonl (options);
704 a->urlen = htonl (urlen);
705 GNUNET_memcpy (&a[1], address, urlen);
708 (*added) = sizeof(struct HttpAddress) + urlen;
715 * Create a HTTP address from a socketaddr
717 * @param protocol protocol
718 * @param addr sockaddr * address
719 * @param addrlen length of the address
720 * @return the HttpAddress
723 http_common_address_from_socket (const char *protocol,
724 const struct sockaddr *addr,
727 struct HttpAddress *address = NULL;
731 GNUNET_asprintf (&res,
736 len = strlen (res) + 1;
737 address = GNUNET_malloc (sizeof (struct HttpAddress) + len);
738 address->options = htonl (HTTP_OPTIONS_NONE);
739 address->urlen = htonl (len);
740 GNUNET_memcpy (&address[1], res, len);
747 * Create a socketaddr from a HTTP address
749 * @param addr a `sockaddr *` address
750 * @param addrlen length of the @a addr
751 * @param res the result:
752 * #GNUNET_SYSERR, invalid input,
753 * #GNUNET_YES: could convert to ip,
754 * #GNUNET_NO: valid input but could not convert to ip (hostname?)
758 http_common_socket_from_address (const void *addr,
762 const struct HttpAddress *ha;
763 struct SplittedHTTPAddress * spa;
764 struct sockaddr_storage *s;
768 (*res) = GNUNET_SYSERR;
769 ha = (const struct HttpAddress *) addr;
780 if (addrlen < sizeof(struct HttpAddress))
785 urlen = ntohl (ha->urlen);
786 if (sizeof(struct HttpAddress) + urlen != addrlen)
788 /* This is a legacy addresses */
791 if (addrlen < sizeof(struct HttpAddress) + urlen)
793 /* This is a legacy addresses */
796 if (((char *) addr)[addrlen - 1] != '\0')
801 spa = http_split_address ((const char *) &ha[1]);
804 (*res) = GNUNET_SYSERR;
808 s = GNUNET_new (struct sockaddr_storage);
809 GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port);
811 == GNUNET_STRINGS_to_address_ip (to_conv, strlen (to_conv), s))
813 /* could be a hostname */
818 else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
821 (*res) = GNUNET_SYSERR;
828 http_clean_splitted (spa);
829 GNUNET_free (to_conv);
830 return (struct sockaddr *) s;
835 * Get the length of an address
837 * @param addr address
841 http_common_address_get_size (const struct HttpAddress * addr)
843 return sizeof(struct HttpAddress) + ntohl (addr->urlen);
848 * Compare addr1 to addr2
850 * @param addr1 address1
851 * @param addrlen1 address 1 length
852 * @param addr2 address2
853 * @param addrlen2 address 2 length
854 * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error
857 http_common_cmp_addresses (const void *addr1,
862 const char *a1 = addr1;
863 const char *a2 = addr2;
864 const struct HttpAddress *ha1;
865 const struct HttpAddress *ha2;
866 ha1 = (const struct HttpAddress *) a1;
867 ha2 = (const struct HttpAddress *) a2;
870 return GNUNET_SYSERR;
872 return GNUNET_SYSERR;
873 if (a1[addrlen1 - 1] != '\0')
874 return GNUNET_SYSERR;
877 return GNUNET_SYSERR;
879 return GNUNET_SYSERR;
880 if (a2[addrlen2 - 1] != '\0')
881 return GNUNET_SYSERR;
883 if (addrlen1 != addrlen2)
885 if (ha1->urlen != ha2->urlen)
888 if (0 == strcmp ((const char *) &ha1[1], (const char *) &ha2[1]))
895 * Function obtain the network type for an address.
897 * @param env the environment
898 * @param address the address
899 * @return the network type
901 enum GNUNET_NetworkType
902 http_common_get_network_for_address (struct GNUNET_TRANSPORT_PluginEnvironment *env,
903 const struct GNUNET_HELLO_Address *address)
907 enum GNUNET_NetworkType net_type;
911 net_type = GNUNET_NT_UNSPECIFIED;
912 sa = http_common_socket_from_address (address->address,
913 address->address_length,
915 if (GNUNET_SYSERR == res)
917 if (GNUNET_YES == res)
919 GNUNET_assert (NULL != sa);
920 if (AF_INET == sa->sa_family)
922 salen = sizeof (struct sockaddr_in);
924 else if (AF_INET6 == sa->sa_family)
926 salen = sizeof (struct sockaddr_in6);
928 net_type = env->get_address_type (env->cls,
938 /* end of plugin_transport_http_common.c */