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);
178 * Closure for #append_port().
180 struct PrettyPrinterContext
185 struct PrettyPrinterContext *next;
190 struct PrettyPrinterContext *prev;
195 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
198 * Function to call with the result.
200 GNUNET_TRANSPORT_AddressStringCallback asc;
203 * Clsoure for @e asc.
210 struct GNUNET_SCHEDULER_Task *timeout_task;
215 struct SplittedHTTPAddress *saddr;
223 * Was conversion successful
236 static struct PrettyPrinterContext *dll_ppc_head;
241 static struct PrettyPrinterContext *dll_ppc_tail;
244 * Function called for a quick conversion of the binary address to
245 * a numeric address. Note that the caller must not free the
246 * address and that the next call to this function is allowed
247 * to override the address again.
249 * @param plugin the name of the plugin
250 * @param saddr the splitted http address
251 * @param options address options
252 * @param dnsresult dns name to include in address
253 * @return string representing the same address or NULL on error
256 http_common_plugin_dnsresult_to_address (const char *plugin,
258 SplittedHTTPAddress *saddr,
260 const char *dnsresult)
262 static char rbuf[1024];
265 GNUNET_asprintf (&res, "%s.%u.%s://%s:%u%s", plugin, options, saddr->protocol,
266 dnsresult, saddr->port, saddr->path);
267 if (strlen (res) + 1 < 500)
269 GNUNET_memcpy (rbuf, res, strlen (res) + 1);
280 http_common_dns_reverse_lookup_cb (void *cls, const char *hostname)
282 struct PrettyPrinterContext *ppc = cls;
284 if (NULL != hostname)
286 ppc->asc (ppc->asc_cls,
287 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr,
289 hostname), GNUNET_OK);
290 ppc->sucess = GNUNET_YES;
294 ppc->asc (ppc->asc_cls, NULL,
295 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
297 GNUNET_CONTAINER_DLL_remove (dll_ppc_head, dll_ppc_tail, ppc);
298 http_clean_splitted (ppc->saddr);
299 GNUNET_free (ppc->plugin);
306 http_common_dns_reverse_lookup (const struct sockaddr *sockaddr,
307 socklen_t sockaddr_len,
309 struct SplittedHTTPAddress *saddr,
311 struct GNUNET_TIME_Relative timeout,
312 GNUNET_TRANSPORT_AddressStringCallback asc,
315 struct PrettyPrinterContext *ppc;
317 ppc = GNUNET_new (struct PrettyPrinterContext);
320 ppc->asc_cls = asc_cls;
321 ppc->plugin = GNUNET_strdup (type);
322 ppc->options = options;
323 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sockaddr,
328 http_common_dns_reverse_lookup_cb,
330 if (NULL == ppc->resolver_handle)
332 GNUNET_free (ppc->plugin);
334 return GNUNET_SYSERR;
336 GNUNET_CONTAINER_DLL_insert (dll_ppc_head,
344 http_common_dns_ip_lookup_cb (void *cls,
345 const struct sockaddr *addr,
348 struct PrettyPrinterContext *ppc = cls;
352 ppc->asc (ppc->asc_cls,
353 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr,
358 ppc->sucess = GNUNET_YES;
359 ppc->asc (ppc->asc_cls, GNUNET_a2s (addr, addrlen), GNUNET_OK);
363 ppc->asc (ppc->asc_cls, NULL,
364 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
366 GNUNET_CONTAINER_DLL_remove (dll_ppc_head, dll_ppc_tail, ppc);
367 GNUNET_free (ppc->plugin);
368 http_clean_splitted (ppc->saddr);
375 http_common_dns_ip_lookup (const char *name,
377 struct SplittedHTTPAddress *saddr,
379 struct GNUNET_TIME_Relative timeout,
380 GNUNET_TRANSPORT_AddressStringCallback asc,
383 struct PrettyPrinterContext *ppc;
385 ppc = GNUNET_new (struct PrettyPrinterContext);
386 ppc->sucess = GNUNET_NO;
389 ppc->asc_cls = asc_cls;
390 ppc->plugin = GNUNET_strdup (type);
391 ppc->options = options;
392 ppc->resolver_handle = GNUNET_RESOLVER_ip_get (name,
395 &http_common_dns_ip_lookup_cb,
397 if (NULL == ppc->resolver_handle)
399 GNUNET_free (ppc->plugin);
401 return GNUNET_SYSERR;
403 GNUNET_CONTAINER_DLL_insert (dll_ppc_head,
411 * Convert the transports address to a nice, human-readable
415 * @param type name of the transport that generated the address
416 * @param addr one of the addresses of the host, NULL for the last address
417 * the specific address format depends on the transport
418 * @param addrlen length of the @a addr
419 * @param numeric should (IP) addresses be displayed in numeric form?
420 * @param timeout after how long should we give up?
421 * @param asc function to call on each string
422 * @param asc_cls closure for @a asc
425 http_common_plugin_address_pretty_printer (void *cls, const char *type,
429 struct GNUNET_TIME_Relative timeout,
430 GNUNET_TRANSPORT_AddressStringCallback
434 const struct HttpAddress *address = addr;
435 struct SplittedHTTPAddress *saddr;
436 struct sockaddr *sock_addr;
444 if ((addrlen < sizeof(struct HttpAddress)) ||
445 (addrlen != http_common_address_get_size (address)))
451 addr_str = (char *) &address[1];
452 if (addr_str[ntohl (address->urlen) - 1] != '\0')
458 saddr = http_split_address (addr_str);
465 sock_addr = http_common_socket_from_address (addr, addrlen, &res);
466 if (GNUNET_SYSERR == res)
468 /* Malformed address */
472 else if (GNUNET_NO == res)
474 /* Could not convert to IP */
477 else if (GNUNET_YES == res)
479 /* Converted to IP */
480 have_ip = GNUNET_YES;
484 /* Must not happen */
489 if ((GNUNET_YES == numeric) &&
490 (GNUNET_YES == have_ip))
492 /* No lookup required */
493 ret = http_common_plugin_address_to_string (type, address, addrlen);
494 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
495 asc (asc_cls, NULL, GNUNET_OK);
496 http_clean_splitted (saddr);
497 GNUNET_free_non_null (sock_addr);
500 if ((GNUNET_YES == numeric) &&
501 (GNUNET_NO == have_ip))
505 http_common_dns_ip_lookup (saddr->host, type, saddr,
506 address->options, timeout,
512 /* Wait for resolver callback */
513 GNUNET_free_non_null (sock_addr);
516 if ((GNUNET_NO == numeric) &&
517 (GNUNET_YES == have_ip))
521 http_common_dns_reverse_lookup (sock_addr,
522 (AF_INET == sock_addr->sa_family)
523 ? sizeof(struct sockaddr_in)
524 : sizeof(struct sockaddr_in6),
527 address->options, timeout,
533 /* Wait for resolver callback */
534 GNUNET_free_non_null (sock_addr);
537 if ((GNUNET_NO == numeric) &&
538 (GNUNET_NO == have_ip))
540 /* No lookup required */
541 ret = http_common_plugin_address_to_string (type, address, addrlen);
542 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
543 asc (asc_cls, NULL, GNUNET_OK);
544 GNUNET_free_non_null (sock_addr);
545 http_clean_splitted (saddr);
548 /* Error (argument supplied not GNUNET_YES or GNUNET_NO) */
554 asc (asc_cls, NULL, GNUNET_SYSERR);
555 asc (asc_cls, NULL, GNUNET_OK);
556 GNUNET_free_non_null (sock_addr);
558 http_clean_splitted (saddr);
566 http_common_plugin_address_to_url (void *cls,
570 static char rbuf[1024];
571 const struct HttpAddress *address = addr;
572 const char *addr_str;
584 if (addrlen != http_common_address_get_size (address))
589 addr_str = (char *) &address[1];
590 if (addr_str[ntohl (address->urlen) - 1] != '\0')
595 ntohl (address->urlen));
601 * Function called for a quick conversion of the binary address to
602 * a numeric address. Note that the caller must not free the
603 * address and that the next call to this function is allowed
604 * to override the address again.
606 * @param plugin the name of the plugin
607 * @param addr binary address
608 * @param addrlen length of the address
609 * @return string representing the same address
612 http_common_plugin_address_to_string (const char *plugin,
616 static char rbuf[1024];
617 const struct HttpAddress *address = addr;
618 const char *addr_str;
621 GNUNET_assert (NULL != plugin);
626 if (addrlen != http_common_address_get_size (address))
628 addr_str = (char *) &address[1];
629 if (addr_str[ntohl (address->urlen) - 1] != '\0')
631 GNUNET_asprintf (&res, "%s.%u.%s", plugin, ntohl (address->options),
633 if (strlen (res) + 1 < 500)
635 GNUNET_memcpy (rbuf, res, strlen (res) + 1);
646 * Function called to convert a string address to
649 * @param cls closure ('struct Plugin*')
650 * @param addr string address
651 * @param addrlen length of the @a addr
652 * @param buf location to store the buffer
653 * If the function returns #GNUNET_SYSERR, its contents are undefined.
654 * @param added length of created address
655 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
658 http_common_plugin_string_to_address (void *cls,
664 struct HttpAddress *a;
671 /* Format protocol.options.address:port */
675 if ((NULL == addr) || (addrlen == 0))
678 return GNUNET_SYSERR;
680 if ('\0' != addr[addrlen - 1])
683 return GNUNET_SYSERR;
685 if (strlen (addr) != addrlen - 1)
688 return GNUNET_SYSERR;
690 plugin = GNUNET_strdup (addr);
691 optionstr = strchr (plugin, '.');
692 if (NULL == optionstr)
695 GNUNET_free (plugin);
696 return GNUNET_SYSERR;
700 options = atol (optionstr); /* 0 on conversion error, that's ok */
701 address = strchr (optionstr, '.');
705 GNUNET_free (plugin);
706 return GNUNET_SYSERR;
710 urlen = strlen (address) + 1;
712 a = GNUNET_malloc (sizeof(struct HttpAddress) + urlen);
713 a->options = htonl (options);
714 a->urlen = htonl (urlen);
715 GNUNET_memcpy (&a[1], address, urlen);
718 (*added) = sizeof(struct HttpAddress) + urlen;
719 GNUNET_free (plugin);
725 * Create a HTTP address from a socketaddr
727 * @param protocol protocol
728 * @param addr sockaddr * address
729 * @param addrlen length of the address
730 * @return the HttpAddress
733 http_common_address_from_socket (const char *protocol,
734 const struct sockaddr *addr,
737 struct HttpAddress *address = NULL;
741 GNUNET_asprintf (&res,
746 len = strlen (res) + 1;
747 address = GNUNET_malloc (sizeof(struct HttpAddress) + len);
748 address->options = htonl (HTTP_OPTIONS_NONE);
749 address->urlen = htonl (len);
750 GNUNET_memcpy (&address[1], res, len);
757 * Create a socketaddr from a HTTP address
759 * @param addr a `sockaddr *` address
760 * @param addrlen length of the @a addr
761 * @param res the result:
762 * #GNUNET_SYSERR, invalid input,
763 * #GNUNET_YES: could convert to ip,
764 * #GNUNET_NO: valid input but could not convert to ip (hostname?)
768 http_common_socket_from_address (const void *addr,
772 const struct HttpAddress *ha;
773 struct SplittedHTTPAddress *spa;
774 struct sockaddr_storage *s;
778 (*res) = GNUNET_SYSERR;
779 ha = (const struct HttpAddress *) addr;
790 if (addrlen < sizeof(struct HttpAddress))
795 urlen = ntohl (ha->urlen);
796 if (sizeof(struct HttpAddress) + urlen != addrlen)
798 /* This is a legacy addresses */
801 if (addrlen < sizeof(struct HttpAddress) + urlen)
803 /* This is a legacy addresses */
806 if (((char *) addr)[addrlen - 1] != '\0')
811 spa = http_split_address ((const char *) &ha[1]);
814 (*res) = GNUNET_SYSERR;
818 s = GNUNET_new (struct sockaddr_storage);
819 GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port);
821 == GNUNET_STRINGS_to_address_ip (to_conv, strlen (to_conv), s))
823 /* could be a hostname */
828 else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
831 (*res) = GNUNET_SYSERR;
838 http_clean_splitted (spa);
839 GNUNET_free (to_conv);
840 return (struct sockaddr *) s;
845 * Get the length of an address
847 * @param addr address
851 http_common_address_get_size (const struct HttpAddress *addr)
853 return sizeof(struct HttpAddress) + ntohl (addr->urlen);
858 * Compare addr1 to addr2
860 * @param addr1 address1
861 * @param addrlen1 address 1 length
862 * @param addr2 address2
863 * @param addrlen2 address 2 length
864 * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error
867 http_common_cmp_addresses (const void *addr1,
872 const char *a1 = addr1;
873 const char *a2 = addr2;
874 const struct HttpAddress *ha1;
875 const struct HttpAddress *ha2;
877 ha1 = (const struct HttpAddress *) a1;
878 ha2 = (const struct HttpAddress *) a2;
881 return GNUNET_SYSERR;
883 return GNUNET_SYSERR;
884 if (a1[addrlen1 - 1] != '\0')
885 return GNUNET_SYSERR;
888 return GNUNET_SYSERR;
890 return GNUNET_SYSERR;
891 if (a2[addrlen2 - 1] != '\0')
892 return GNUNET_SYSERR;
894 if (addrlen1 != addrlen2)
896 if (ha1->urlen != ha2->urlen)
899 if (0 == strcmp ((const char *) &ha1[1], (const char *) &ha2[1]))
906 * Function obtain the network type for an address.
908 * @param env the environment
909 * @param address the address
910 * @return the network type
912 enum GNUNET_NetworkType
913 http_common_get_network_for_address (struct
914 GNUNET_TRANSPORT_PluginEnvironment *env,
915 const struct GNUNET_HELLO_Address *address)
918 enum GNUNET_NetworkType net_type;
922 net_type = GNUNET_NT_UNSPECIFIED;
923 sa = http_common_socket_from_address (address->address,
924 address->address_length,
926 if (GNUNET_SYSERR == res)
928 if (GNUNET_YES == res)
930 GNUNET_assert (NULL != sa);
931 if (AF_INET == sa->sa_family)
933 salen = sizeof(struct sockaddr_in);
935 else if (AF_INET6 == sa->sa_family)
937 salen = sizeof(struct sockaddr_in6);
939 net_type = env->get_address_type (env->cls,
948 /* end of plugin_transport_http_common.c */