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 by http client and server transport service plugin
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_plugin.h"
29 #include "plugin_transport_http_common.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);
177 * Convert the transports address to a nice, human-readable
181 * @param type name of the transport that generated the address
182 * @param addr one of the addresses of the host, NULL for the last address
183 * the specific address format depends on the transport
184 * @param addrlen length of the @a addr
185 * @param numeric should (IP) addresses be displayed in numeric form?
186 * @param timeout after how long should we give up?
187 * @param asc function to call on each string
188 * @param asc_cls closure for @a asc
191 http_common_plugin_address_pretty_printer (void *cls,
196 struct GNUNET_TIME_Relative timeout,
197 GNUNET_TRANSPORT_AddressStringCallback asc,
200 const struct HttpAddress *address = addr;
203 ret = http_common_plugin_address_to_string (type,
208 (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
219 http_common_plugin_address_to_url (void *cls,
223 static char rbuf[1024];
224 const struct HttpAddress *address = addr;
225 const char * addr_str;
237 if (addrlen != http_common_address_get_size (address))
242 addr_str = (char *) &address[1];
244 if (addr_str[ntohl(address->urlen) -1] != '\0')
247 memcpy (rbuf, &address[1], ntohl(address->urlen));
253 * Function called for a quick conversion of the binary address to
254 * a numeric address. Note that the caller must not free the
255 * address and that the next call to this function is allowed
256 * to override the address again.
258 * @param plugin the name of the plugin
259 * @param addr binary address
260 * @param addrlen length of the address
261 * @return string representing the same address
264 http_common_plugin_address_to_string (const char *plugin,
268 static char rbuf[1024];
269 const struct HttpAddress *address = addr;
270 const char * addr_str;
273 GNUNET_assert (NULL != plugin);
277 return TRANSPORT_SESSION_INBOUND_STRING;
278 if (addrlen != http_common_address_get_size (address))
280 addr_str = (char *) &address[1];
281 if (addr_str[ntohl(address->urlen) -1] != '\0')
283 GNUNET_asprintf (&res,
286 ntohl (address->options),
288 if (strlen(res) + 1 < 500)
290 memcpy (rbuf, res, strlen(res) + 1);
301 * Function called to convert a string address to
304 * @param cls closure ('struct Plugin*')
305 * @param addr string address
306 * @param addrlen length of the @a addr
307 * @param buf location to store the buffer
308 * If the function returns #GNUNET_SYSERR, its contents are undefined.
309 * @param added length of created address
310 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
313 http_common_plugin_string_to_address (void *cls,
319 struct HttpAddress *a;
326 /* Format protocol.options.address:port */
330 if ((NULL == addr) || (addrlen == 0))
333 return GNUNET_SYSERR;
335 if ('\0' != addr[addrlen - 1])
338 return GNUNET_SYSERR;
340 if (strlen (addr) != addrlen - 1)
343 return GNUNET_SYSERR;
345 plugin = GNUNET_strdup (addr);
346 optionstr = strchr (plugin, '.');
347 if (NULL == optionstr)
350 GNUNET_free (plugin);
351 return GNUNET_SYSERR;
355 options = atol (optionstr); /* 0 on conversion error, that's ok */
356 address = strchr (optionstr, '.');
360 GNUNET_free (plugin);
361 return GNUNET_SYSERR;
365 urlen = strlen (address) + 1;
367 a = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
368 a->options = htonl(options);
369 a->urlen = htonl(urlen);
370 memcpy (&a[1], address, urlen);
373 (*added) = sizeof (struct HttpAddress) + urlen;
374 GNUNET_free (plugin);
380 * Create a HTTP address from a socketaddr
382 * @param protocol protocol
383 * @param addr sockaddr * address
384 * @param addrlen length of the address
385 * @return the HttpAddress
388 http_common_address_from_socket (const char *protocol,
389 const struct sockaddr *addr,
392 struct HttpAddress *address = NULL;
396 GNUNET_asprintf(&res,
399 GNUNET_a2s (addr, addrlen));
400 len = strlen (res)+1;
401 address = GNUNET_malloc (sizeof (struct HttpAddress) + len);
402 address->options = htonl (HTTP_OPTIONS_NONE);
403 address->urlen = htonl (len);
404 memcpy (&address[1], res, len);
411 * Create a socketaddr from a HTTP address
413 * @param addr a `sockaddr *` address
414 * @param addrlen length of the @a addr
415 * @param res the result:
416 * #GNUNET_SYSERR, invalid input,
417 * #GNUNET_YES: could convert to ip,
418 * #GNUNET_NO: valid input but could not convert to ip (hostname?)
422 http_common_socket_from_address (const void *addr,
426 const struct HttpAddress *ha;
427 struct SplittedHTTPAddress * spa;
428 struct sockaddr_storage *s;
432 (*res) = GNUNET_SYSERR;
433 ha = (const struct HttpAddress *) addr;
444 if (addrlen < sizeof(struct HttpAddress))
449 urlen = ntohl (ha->urlen);
450 if (sizeof(struct HttpAddress) + urlen != addrlen)
452 /* This is a legacy addresses */
455 if (addrlen < sizeof(struct HttpAddress) + urlen)
457 /* This is a legacy addresses */
460 if (((char *) addr)[addrlen - 1] != '\0')
465 spa = http_split_address ((const char *) &ha[1]);
468 (*res) = GNUNET_SYSERR;
472 s = GNUNET_new (struct sockaddr_storage);
473 GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port);
474 if (GNUNET_SYSERR == GNUNET_STRINGS_to_address_ip (to_conv, strlen(to_conv), s))
476 /* could be a hostname */
481 else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
485 (*res) = GNUNET_SYSERR;
492 http_clean_splitted (spa);
493 GNUNET_free (to_conv);
494 return (struct sockaddr *) s;
499 * Get the length of an address
501 * @param addr address
505 http_common_address_get_size (const struct HttpAddress * addr)
507 return sizeof (struct HttpAddress) + ntohl(addr->urlen);
512 * Compare addr1 to addr2
514 * @param addr1 address1
515 * @param addrlen1 address 1 length
516 * @param addr2 address2
517 * @param addrlen2 address 2 length
518 * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error
521 http_common_cmp_addresses (const void *addr1,
526 const char *a1 = addr1;
527 const char *a2 = addr2;
528 const struct HttpAddress *ha1;
529 const struct HttpAddress *ha2;
530 ha1 = (const struct HttpAddress *) a1;
531 ha2 = (const struct HttpAddress *) a2;
534 return GNUNET_SYSERR;
536 return GNUNET_SYSERR;
537 if (a1[addrlen1-1] != '\0')
538 return GNUNET_SYSERR;
541 return GNUNET_SYSERR;
543 return GNUNET_SYSERR;
544 if (a2[addrlen2-1] != '\0')
545 return GNUNET_SYSERR;
547 if (addrlen1 != addrlen2)
549 if (ha1->urlen != ha2->urlen)
552 if (0 == strcmp ((const char *) &ha1[1],(const char *) &ha2[1]))
559 /* end of plugin_transport_http_common.c */