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"
32 struct SplittedHTTPAddress
42 http_clean_splitted (struct SplittedHTTPAddress *spa)
46 GNUNET_free_non_null (spa->protocol);
47 GNUNET_free_non_null (spa->host);
48 GNUNET_free_non_null (spa->path);
49 GNUNET_free_non_null (spa);
54 struct SplittedHTTPAddress *
55 http_split_address (const char * addr)
57 struct SplittedHTTPAddress *sp;
58 char *src = GNUNET_strdup (addr);
59 char *protocol_start = NULL;
60 char *host_start = NULL;
62 char *port_start = NULL;
63 char *path_start = NULL;
66 sp = GNUNET_new (struct SplittedHTTPAddress);
67 /* Address string consists of protocol://host[:port]path*/
69 host_start = strstr (src, "://");
70 if (NULL == host_start)
77 sp->protocol = GNUNET_strdup (protocol_start);
79 host_start += strlen ("://");
80 if (strlen (host_start) == 0)
83 GNUNET_free (sp->protocol);
89 path_start = strchr (host_start, '/');
90 if (NULL != path_start)
92 sp->path = GNUNET_strdup (path_start);
96 sp->path = GNUNET_strdup ("");
98 if (strlen(host_start) < 1)
101 GNUNET_free (sp->protocol);
102 GNUNET_free (sp->path);
107 if (NULL != (port_start = strrchr (host_start, ':')))
109 /* *We COULD have a port, but also an IPv6 address! */
110 if (NULL != (v6_end = strchr(host_start, ']')))
112 if (v6_end < port_start)
114 /* IPv6 address + port */
115 port_start[0] = '\0';
117 sp->port = atoi (port_start);
118 if ((0 == sp->port) || (65535 < sp->port))
121 GNUNET_free (sp->protocol);
122 GNUNET_free (sp->path);
129 /* IPv6 address + no port */
130 if (0 == strcmp(sp->protocol, "https"))
131 sp->port = HTTPS_DEFAULT_PORT;
132 else if (0 == strcmp(sp->protocol, "http"))
133 sp->port = HTTP_DEFAULT_PORT;
138 /* No IPv6 address */
139 port_start[0] = '\0';
141 sp->port = atoi (port_start);
142 if ((0 == sp->port) || (65535 < sp->port))
145 GNUNET_free (sp->protocol);
146 GNUNET_free (sp->path);
154 /* No ':' as port separator, default port for protocol */
155 if (0 == strcmp(sp->protocol, "https"))
156 sp->port = HTTPS_DEFAULT_PORT;
157 else if (0 == strcmp(sp->protocol, "http"))
158 sp->port = HTTP_DEFAULT_PORT;
163 GNUNET_free (sp->protocol);
164 GNUNET_free (sp->path);
169 if (strlen (host_start) > 0)
170 sp->host = GNUNET_strdup (host_start);
175 GNUNET_free (sp->protocol);
176 GNUNET_free (sp->path);
186 * Convert the transports address to a nice, human-readable
190 * @param type name of the transport that generated the address
191 * @param addr one of the addresses of the host, NULL for the last address
192 * the specific address format depends on the transport
193 * @param addrlen length of the @a addr
194 * @param numeric should (IP) addresses be displayed in numeric form?
195 * @param timeout after how long should we give up?
196 * @param asc function to call on each string
197 * @param asc_cls closure for @a asc
200 http_common_plugin_address_pretty_printer (void *cls,
205 struct GNUNET_TIME_Relative timeout,
206 GNUNET_TRANSPORT_AddressStringCallback asc,
209 const struct HttpAddress *address = addr;
212 == http_common_plugin_address_to_string (NULL, type, address, addrlen))
213 asc (asc_cls, NULL, GNUNET_SYSERR);
216 http_common_plugin_address_to_string (NULL, type, address, addrlen),
218 asc (asc_cls, NULL, GNUNET_OK);
223 http_common_plugin_address_to_url (void *cls,
227 static char rbuf[1024];
228 const struct HttpAddress *address = addr;
229 const char * addr_str;
241 if (addrlen != http_common_address_get_size (address))
246 addr_str = (char *) &address[1];
248 if (addr_str[ntohl(address->urlen) -1] != '\0')
251 memcpy (rbuf, &address[1], ntohl(address->urlen));
257 * Function called for a quick conversion of the binary address to
258 * a numeric address. Note that the caller must not free the
259 * address and that the next call to this function is allowed
260 * to override the address again.
263 * @param plugin the plugin
264 * @param addr binary address
265 * @param addrlen length of the address
266 * @return string representing the same address
269 http_common_plugin_address_to_string (void *cls,
274 static char rbuf[1024];
275 const struct HttpAddress *address = addr;
276 const char * addr_str;
279 GNUNET_assert (NULL != plugin);
284 return TRANSPORT_SESSION_INBOUND_STRING;
285 if (addrlen != http_common_address_get_size (address))
287 addr_str = (char *) &address[1];
289 if (addr_str[ntohl(address->urlen) -1] != '\0')
291 GNUNET_asprintf (&res, "%s.%u.%s", plugin, ntohl(address->options), &address[1]);
292 if (strlen(res) + 1 < 500)
294 memcpy (rbuf, res, strlen(res) + 1);
305 * Function called to convert a string address to
308 * @param cls closure ('struct Plugin*')
309 * @param addr string address
310 * @param addrlen length of the @a addr
311 * @param buf location to store the buffer
312 * If the function returns #GNUNET_SYSERR, its contents are undefined.
313 * @param added length of created address
314 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
317 http_common_plugin_string_to_address (void *cls,
323 struct HttpAddress *a;
330 /* Format protocol.options.address:port */
334 if ((NULL == addr) || (addrlen == 0))
337 return GNUNET_SYSERR;
339 if ('\0' != addr[addrlen - 1])
342 return GNUNET_SYSERR;
344 if (strlen (addr) != addrlen - 1)
347 return GNUNET_SYSERR;
349 plugin = GNUNET_strdup (addr);
350 optionstr = strchr (plugin, '.');
351 if (NULL == optionstr)
354 GNUNET_free (plugin);
355 return GNUNET_SYSERR;
359 options = atol (optionstr); /* 0 on conversion error, that's ok */
360 address = strchr (optionstr, '.');
364 GNUNET_free (plugin);
365 return GNUNET_SYSERR;
369 urlen = strlen (address) + 1;
371 a = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
372 a->options = htonl(options);
373 a->urlen = htonl(urlen);
374 memcpy (&a[1], address, urlen);
377 (*added) = sizeof (struct HttpAddress) + urlen;
378 GNUNET_free (plugin);
384 * Create a HTTP address from a socketaddr
386 * @param protocol protocol
387 * @param addr sockaddr * address
388 * @param addrlen length of the address
389 * @return the HttpAddress
392 http_common_address_from_socket (const char *protocol,
393 const struct sockaddr *addr,
396 struct HttpAddress *address = NULL;
400 GNUNET_asprintf(&res,
403 GNUNET_a2s (addr, addrlen));
404 len = strlen (res)+1;
405 address = GNUNET_malloc (sizeof (struct HttpAddress) + len);
406 address->options = htonl (HTTP_OPTIONS_NONE);
407 address->urlen = htonl (len);
408 memcpy (&address[1], res, len);
415 * Create a socketaddr from a HTTP address
417 * @param addr a `sockaddr *` address
418 * @param addrlen length of the @a addr
419 * @param res the result:
420 * #GNUNET_SYSERR, invalid input,
421 * #GNUNET_YES: could convert to ip,
422 * #GNUNET_NO: valid input but could not convert to ip (hostname?)
426 http_common_socket_from_address (const void *addr,
430 const struct HttpAddress *ha;
431 struct SplittedHTTPAddress * spa;
432 struct sockaddr_storage *s;
436 (*res) = GNUNET_SYSERR;
437 ha = (const struct HttpAddress *) addr;
448 if (addrlen < sizeof(struct HttpAddress))
453 urlen = ntohl (ha->urlen);
454 if (sizeof(struct HttpAddress) + urlen != addrlen)
456 /* This is a legacy addresses */
459 if (addrlen < sizeof(struct HttpAddress) + urlen)
461 /* This is a legacy addresses */
464 if (((char *) addr)[addrlen - 1] != '\0')
469 spa = http_split_address ((const char *) &ha[1]);
472 (*res) = GNUNET_SYSERR;
476 s = GNUNET_new (struct sockaddr_storage);
477 GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port);
478 if (GNUNET_SYSERR == GNUNET_STRINGS_to_address_ip (to_conv, strlen(to_conv), s))
480 /* could be a hostname */
485 else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
489 (*res) = GNUNET_SYSERR;
496 http_clean_splitted (spa);
497 GNUNET_free (to_conv);
498 return (struct sockaddr *) s;
503 * Get the length of an address
505 * @param addr address
509 http_common_address_get_size (const struct HttpAddress * addr)
511 return sizeof (struct HttpAddress) + ntohl(addr->urlen);
516 * Compare addr1 to addr2
518 * @param addr1 address1
519 * @param addrlen1 address 1 length
520 * @param addr2 address2
521 * @param addrlen2 address 2 length
522 * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error
525 http_common_cmp_addresses (const void *addr1,
530 const char *a1 = addr1;
531 const char *a2 = addr2;
532 const struct HttpAddress *ha1;
533 const struct HttpAddress *ha2;
534 ha1 = (const struct HttpAddress *) a1;
535 ha2 = (const struct HttpAddress *) a2;
538 return GNUNET_SYSERR;
540 return GNUNET_SYSERR;
541 if (a1[addrlen1-1] != '\0')
542 return GNUNET_SYSERR;
545 return GNUNET_SYSERR;
547 return GNUNET_SYSERR;
548 if (a2[addrlen2-1] != '\0')
549 return GNUNET_SYSERR;
551 if (addrlen1 != addrlen2)
553 if (ha1->urlen != ha2->urlen)
556 if (0 == strcmp ((const char *) &ha1[1],(const char *) &ha2[1]))
563 /* end of plugin_transport_http_common.c */