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;
58 sp = GNUNET_new (struct SplittedHTTPAddress);
59 /* Address string consists of protocol://host[:port]path*/
61 host_start = strstr (src, "://");
62 if (NULL == host_start)
69 sp->protocol = GNUNET_strdup (protocol_start);
71 host_start += strlen ("://");
72 if (strlen (host_start) == 0)
75 GNUNET_free (sp->protocol);
81 path_start = strchr (host_start, '/');
82 if (NULL != path_start)
84 sp->path = GNUNET_strdup (path_start);
88 sp->path = GNUNET_strdup ("");
90 if (strlen (host_start) < 1)
93 GNUNET_free (sp->protocol);
94 GNUNET_free (sp->path);
99 if (NULL != (port_start = strrchr (host_start, ':')))
101 /* *We COULD have a port, but also an IPv6 address! */
102 if (NULL != (v6_end = strchr (host_start, ']')))
104 if (v6_end < port_start)
106 /* IPv6 address + port */
107 port_start[0] = '\0';
109 sp->port = atoi (port_start);
110 if ((0 == sp->port) || (65535 < sp->port))
113 GNUNET_free (sp->protocol);
114 GNUNET_free (sp->path);
121 /* IPv6 address + no port */
122 if (0 == strcmp (sp->protocol, "https"))
123 sp->port = HTTPS_DEFAULT_PORT;
124 else if (0 == strcmp (sp->protocol, "http"))
125 sp->port = HTTP_DEFAULT_PORT;
130 /* No IPv6 address */
131 port_start[0] = '\0';
133 sp->port = atoi (port_start);
134 if ((0 == sp->port) || (65535 < sp->port))
137 GNUNET_free (sp->protocol);
138 GNUNET_free (sp->path);
146 /* No ':' as port separator, default port for protocol */
147 if (0 == strcmp (sp->protocol, "https"))
148 sp->port = HTTPS_DEFAULT_PORT;
149 else if (0 == strcmp (sp->protocol, "http"))
150 sp->port = HTTP_DEFAULT_PORT;
155 GNUNET_free (sp->protocol);
156 GNUNET_free (sp->path);
161 if (strlen (host_start) > 0)
162 sp->host = GNUNET_strdup (host_start);
167 GNUNET_free (sp->protocol);
168 GNUNET_free (sp->path);
177 * Closure for #append_port().
179 struct PrettyPrinterContext
184 struct PrettyPrinterContext *next;
189 struct PrettyPrinterContext *prev;
194 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
197 * Function to call with the result.
199 GNUNET_TRANSPORT_AddressStringCallback asc;
202 * Clsoure for @e asc.
209 struct GNUNET_SCHEDULER_Task *timeout_task;
214 struct SplittedHTTPAddress *saddr;
222 * Was conversion successful
235 static struct PrettyPrinterContext *dll_ppc_head;
240 static struct PrettyPrinterContext *dll_ppc_tail;
243 * Function called for a quick conversion of the binary address to
244 * a numeric address. Note that the caller must not free the
245 * address and that the next call to this function is allowed
246 * to override the address again.
248 * @param plugin the name of the plugin
249 * @param saddr the splitted http address
250 * @param options address options
251 * @param dnsresult dns name to include in address
252 * @return string representing the same address or NULL on error
255 http_common_plugin_dnsresult_to_address (const char *plugin,
257 SplittedHTTPAddress *saddr,
259 const char *dnsresult)
261 static char rbuf[1024];
264 GNUNET_asprintf (&res, "%s.%u.%s://%s:%u%s", plugin, options, saddr->protocol,
265 dnsresult, saddr->port, saddr->path);
266 if (strlen (res) + 1 < 500)
268 GNUNET_memcpy (rbuf, res, strlen (res) + 1);
279 http_common_dns_reverse_lookup_cb (void *cls, const char *hostname)
281 struct PrettyPrinterContext *ppc = cls;
283 if (NULL != hostname)
285 ppc->asc (ppc->asc_cls,
286 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr,
288 hostname), GNUNET_OK);
289 ppc->sucess = GNUNET_YES;
293 ppc->asc (ppc->asc_cls, NULL,
294 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
296 GNUNET_CONTAINER_DLL_remove (dll_ppc_head, dll_ppc_tail, ppc);
297 http_clean_splitted (ppc->saddr);
298 GNUNET_free (ppc->plugin);
305 http_common_dns_reverse_lookup (const struct sockaddr *sockaddr,
306 socklen_t sockaddr_len,
308 struct SplittedHTTPAddress *saddr,
310 struct GNUNET_TIME_Relative timeout,
311 GNUNET_TRANSPORT_AddressStringCallback asc,
314 struct PrettyPrinterContext *ppc;
316 ppc = GNUNET_new (struct PrettyPrinterContext);
319 ppc->asc_cls = asc_cls;
320 ppc->plugin = GNUNET_strdup (type);
321 ppc->options = options;
322 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sockaddr,
327 http_common_dns_reverse_lookup_cb,
329 if (NULL == ppc->resolver_handle)
331 GNUNET_free (ppc->plugin);
333 return GNUNET_SYSERR;
335 GNUNET_CONTAINER_DLL_insert (dll_ppc_head,
343 http_common_dns_ip_lookup_cb (void *cls,
344 const struct sockaddr *addr,
347 struct PrettyPrinterContext *ppc = cls;
351 ppc->asc (ppc->asc_cls,
352 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr,
357 ppc->sucess = GNUNET_YES;
358 ppc->asc (ppc->asc_cls, GNUNET_a2s (addr, addrlen), GNUNET_OK);
362 ppc->asc (ppc->asc_cls, NULL,
363 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
365 GNUNET_CONTAINER_DLL_remove (dll_ppc_head, dll_ppc_tail, ppc);
366 GNUNET_free (ppc->plugin);
367 http_clean_splitted (ppc->saddr);
374 http_common_dns_ip_lookup (const char *name,
376 struct SplittedHTTPAddress *saddr,
378 struct GNUNET_TIME_Relative timeout,
379 GNUNET_TRANSPORT_AddressStringCallback asc,
382 struct PrettyPrinterContext *ppc;
384 ppc = GNUNET_new (struct PrettyPrinterContext);
385 ppc->sucess = GNUNET_NO;
388 ppc->asc_cls = asc_cls;
389 ppc->plugin = GNUNET_strdup (type);
390 ppc->options = options;
391 ppc->resolver_handle = GNUNET_RESOLVER_ip_get (name,
394 &http_common_dns_ip_lookup_cb,
396 if (NULL == ppc->resolver_handle)
398 GNUNET_free (ppc->plugin);
400 return GNUNET_SYSERR;
402 GNUNET_CONTAINER_DLL_insert (dll_ppc_head,
410 * Convert the transports address to a nice, human-readable
414 * @param type name of the transport that generated the address
415 * @param addr one of the addresses of the host, NULL for the last address
416 * the specific address format depends on the transport
417 * @param addrlen length of the @a addr
418 * @param numeric should (IP) addresses be displayed in numeric form?
419 * @param timeout after how long should we give up?
420 * @param asc function to call on each string
421 * @param asc_cls closure for @a asc
424 http_common_plugin_address_pretty_printer (void *cls, const char *type,
428 struct GNUNET_TIME_Relative timeout,
429 GNUNET_TRANSPORT_AddressStringCallback
433 const struct HttpAddress *address = addr;
434 struct SplittedHTTPAddress *saddr;
435 struct sockaddr *sock_addr;
443 if ((addrlen < sizeof(struct HttpAddress)) ||
444 (addrlen != http_common_address_get_size (address)))
450 addr_str = (char *) &address[1];
451 if (addr_str[ntohl (address->urlen) - 1] != '\0')
457 saddr = http_split_address (addr_str);
464 sock_addr = http_common_socket_from_address (addr, addrlen, &res);
465 if (GNUNET_SYSERR == res)
467 /* Malformed address */
471 else if (GNUNET_NO == res)
473 /* Could not convert to IP */
476 else if (GNUNET_YES == res)
478 /* Converted to IP */
479 have_ip = GNUNET_YES;
483 /* Must not happen */
488 if ((GNUNET_YES == numeric) &&
489 (GNUNET_YES == have_ip))
491 /* No lookup required */
492 ret = http_common_plugin_address_to_string (type, address, addrlen);
493 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
494 asc (asc_cls, NULL, GNUNET_OK);
495 http_clean_splitted (saddr);
496 GNUNET_free_non_null (sock_addr);
499 if ((GNUNET_YES == numeric) &&
500 (GNUNET_NO == have_ip))
504 http_common_dns_ip_lookup (saddr->host, type, saddr,
505 address->options, timeout,
511 /* Wait for resolver callback */
512 GNUNET_free_non_null (sock_addr);
515 if ((GNUNET_NO == numeric) &&
516 (GNUNET_YES == have_ip))
520 http_common_dns_reverse_lookup (sock_addr,
521 (AF_INET == sock_addr->sa_family)
522 ? sizeof(struct sockaddr_in)
523 : sizeof(struct sockaddr_in6),
526 address->options, timeout,
532 /* Wait for resolver callback */
533 GNUNET_free_non_null (sock_addr);
536 if ((GNUNET_NO == numeric) &&
537 (GNUNET_NO == have_ip))
539 /* No lookup required */
540 ret = http_common_plugin_address_to_string (type, address, addrlen);
541 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
542 asc (asc_cls, NULL, GNUNET_OK);
543 GNUNET_free_non_null (sock_addr);
544 http_clean_splitted (saddr);
547 /* Error (argument supplied not GNUNET_YES or GNUNET_NO) */
553 asc (asc_cls, NULL, GNUNET_SYSERR);
554 asc (asc_cls, NULL, GNUNET_OK);
555 GNUNET_free_non_null (sock_addr);
557 http_clean_splitted (saddr);
565 http_common_plugin_address_to_url (void *cls,
569 static char rbuf[1024];
570 const struct HttpAddress *address = addr;
571 const char *addr_str;
583 if (addrlen != http_common_address_get_size (address))
588 addr_str = (char *) &address[1];
589 if (addr_str[ntohl (address->urlen) - 1] != '\0')
594 ntohl (address->urlen));
600 * Function called for a quick conversion of the binary address to
601 * a numeric address. Note that the caller must not free the
602 * address and that the next call to this function is allowed
603 * to override the address again.
605 * @param plugin the name of the plugin
606 * @param addr binary address
607 * @param addrlen length of the address
608 * @return string representing the same address
611 http_common_plugin_address_to_string (const char *plugin,
615 static char rbuf[1024];
616 const struct HttpAddress *address = addr;
617 const char *addr_str;
620 GNUNET_assert (NULL != plugin);
625 if (addrlen != http_common_address_get_size (address))
627 addr_str = (char *) &address[1];
628 if (addr_str[ntohl (address->urlen) - 1] != '\0')
630 GNUNET_asprintf (&res, "%s.%u.%s", plugin, ntohl (address->options),
632 if (strlen (res) + 1 < 500)
634 GNUNET_memcpy (rbuf, res, strlen (res) + 1);
644 * Function called to convert a string address to
647 * @param cls closure ('struct Plugin*')
648 * @param addr string address
649 * @param addrlen length of the @a addr
650 * @param buf location to store the buffer
651 * If the function returns #GNUNET_SYSERR, its contents are undefined.
652 * @param added length of created address
653 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
656 http_common_plugin_string_to_address (void *cls,
662 struct HttpAddress *a;
669 /* Format protocol.options.address:port */
673 if ((NULL == addr) || (addrlen == 0))
676 return GNUNET_SYSERR;
678 if ('\0' != addr[addrlen - 1])
681 return GNUNET_SYSERR;
683 if (strlen (addr) != addrlen - 1)
686 return GNUNET_SYSERR;
688 plugin = GNUNET_strdup (addr);
689 optionstr = strchr (plugin, '.');
690 if (NULL == optionstr)
693 GNUNET_free (plugin);
694 return GNUNET_SYSERR;
698 options = atol (optionstr); /* 0 on conversion error, that's ok */
699 address = strchr (optionstr, '.');
703 GNUNET_free (plugin);
704 return GNUNET_SYSERR;
708 urlen = strlen (address) + 1;
710 a = GNUNET_malloc (sizeof(struct HttpAddress) + urlen);
711 a->options = htonl (options);
712 a->urlen = htonl (urlen);
713 GNUNET_memcpy (&a[1], address, urlen);
716 (*added) = sizeof(struct HttpAddress) + urlen;
717 GNUNET_free (plugin);
723 * Create a HTTP address from a socketaddr
725 * @param protocol protocol
726 * @param addr sockaddr * address
727 * @param addrlen length of the address
728 * @return the HttpAddress
731 http_common_address_from_socket (const char *protocol,
732 const struct sockaddr *addr,
735 struct HttpAddress *address = NULL;
739 GNUNET_asprintf (&res,
744 len = strlen (res) + 1;
745 address = GNUNET_malloc (sizeof(struct HttpAddress) + len);
746 address->options = htonl (HTTP_OPTIONS_NONE);
747 address->urlen = htonl (len);
748 GNUNET_memcpy (&address[1], res, len);
755 * Create a socketaddr from a HTTP address
757 * @param addr a `sockaddr *` address
758 * @param addrlen length of the @a addr
759 * @param res the result:
760 * #GNUNET_SYSERR, invalid input,
761 * #GNUNET_YES: could convert to ip,
762 * #GNUNET_NO: valid input but could not convert to ip (hostname?)
766 http_common_socket_from_address (const void *addr,
770 const struct HttpAddress *ha;
771 struct SplittedHTTPAddress *spa;
772 struct sockaddr_storage *s;
776 (*res) = GNUNET_SYSERR;
777 ha = (const struct HttpAddress *) addr;
788 if (addrlen < sizeof(struct HttpAddress))
793 urlen = ntohl (ha->urlen);
794 if (sizeof(struct HttpAddress) + urlen != addrlen)
796 /* This is a legacy addresses */
799 if (addrlen < sizeof(struct HttpAddress) + urlen)
801 /* This is a legacy addresses */
804 if (((char *) addr)[addrlen - 1] != '\0')
809 spa = http_split_address ((const char *) &ha[1]);
812 (*res) = GNUNET_SYSERR;
816 s = GNUNET_new (struct sockaddr_storage);
817 GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port);
819 == GNUNET_STRINGS_to_address_ip (to_conv, strlen (to_conv), s))
821 /* could be a hostname */
826 else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
829 (*res) = GNUNET_SYSERR;
836 http_clean_splitted (spa);
837 GNUNET_free (to_conv);
838 return (struct sockaddr *) s;
843 * Get the length of an address
845 * @param addr address
849 http_common_address_get_size (const struct HttpAddress *addr)
851 return sizeof(struct HttpAddress) + ntohl (addr->urlen);
856 * Compare addr1 to addr2
858 * @param addr1 address1
859 * @param addrlen1 address 1 length
860 * @param addr2 address2
861 * @param addrlen2 address 2 length
862 * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error
865 http_common_cmp_addresses (const void *addr1,
870 const char *a1 = addr1;
871 const char *a2 = addr2;
872 const struct HttpAddress *ha1;
873 const struct HttpAddress *ha2;
875 ha1 = (const struct HttpAddress *) a1;
876 ha2 = (const struct HttpAddress *) a2;
879 return GNUNET_SYSERR;
881 return GNUNET_SYSERR;
882 if (a1[addrlen1 - 1] != '\0')
883 return GNUNET_SYSERR;
886 return GNUNET_SYSERR;
888 return GNUNET_SYSERR;
889 if (a2[addrlen2 - 1] != '\0')
890 return GNUNET_SYSERR;
892 if (addrlen1 != addrlen2)
894 if (ha1->urlen != ha2->urlen)
897 if (0 == strcmp ((const char *) &ha1[1], (const char *) &ha2[1]))
904 * Function obtain the network type for an address.
906 * @param env the environment
907 * @param address the address
908 * @return the network type
910 enum GNUNET_NetworkType
911 http_common_get_network_for_address (struct
912 GNUNET_TRANSPORT_PluginEnvironment *env,
913 const struct GNUNET_HELLO_Address *address)
916 enum GNUNET_NetworkType net_type;
920 net_type = GNUNET_NT_UNSPECIFIED;
921 sa = http_common_socket_from_address (address->address,
922 address->address_length,
924 if (GNUNET_SYSERR == res)
926 if (GNUNET_YES == res)
928 GNUNET_assert (NULL != sa);
929 if (AF_INET == sa->sa_family)
931 salen = sizeof(struct sockaddr_in);
933 else if (AF_INET6 == sa->sa_family)
935 salen = sizeof(struct sockaddr_in6);
937 net_type = env->get_address_type (env->cls,
947 /* end of plugin_transport_http_common.c */