2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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"
31 struct SplittedHTTPAddress
41 http_clean_splitted (struct SplittedHTTPAddress *spa)
45 GNUNET_free_non_null (spa->protocol);
46 GNUNET_free_non_null (spa->host);
47 GNUNET_free_non_null (spa->path);
48 GNUNET_free_non_null (spa);
52 struct SplittedHTTPAddress *
53 http_split_address (const char * addr)
55 struct SplittedHTTPAddress *sp;
56 char *src = GNUNET_strdup (addr);
57 char *protocol_start = NULL;
58 char *host_start = NULL;
60 char *port_start = NULL;
61 char *path_start = NULL;
63 sp = GNUNET_malloc (sizeof (struct SplittedHTTPAddress));
65 /* Address string consists of protocol://host[:port]path*/
67 host_start = strstr (src, "://");
68 if (NULL == host_start)
76 sp->protocol = GNUNET_strdup (protocol_start);
78 host_start += strlen ("://");
79 if (strlen (host_start) == 0)
82 GNUNET_free (sp->protocol);
88 path_start = strchr (host_start, '/');
89 if (NULL != path_start)
91 sp->path = GNUNET_strdup (path_start);
95 sp->path = GNUNET_strdup ("");
97 if (strlen(host_start) < 1)
100 GNUNET_free (sp->protocol);
101 GNUNET_free (sp->path);
106 if (NULL != (port_start = strrchr (host_start, ':')))
108 /* *We COULD have a port, but also an IPv6 address! */
109 if (NULL != (v6_end = strchr(host_start, ']')))
111 if (v6_end < port_start)
113 /* IPv6 address + port */
114 port_start[0] = '\0';
116 sp->port = atoi (port_start);
117 if ((0 == sp->port) || (65535 < sp->port))
120 GNUNET_free (sp->protocol);
121 GNUNET_free (sp->path);
128 /* IPv6 address + no port */
129 if (0 == strcmp(sp->protocol, "https"))
130 sp->port = HTTPS_DEFAULT_PORT;
131 else if (0 == strcmp(sp->protocol, "http"))
132 sp->port = HTTP_DEFAULT_PORT;
137 /* No IPv6 address */
138 port_start[0] = '\0';
140 sp->port = atoi (port_start);
141 if ((0 == sp->port) || (65535 < sp->port))
144 GNUNET_free (sp->protocol);
145 GNUNET_free (sp->path);
153 /* No ':' as port separator, default port for protocol */
154 if (0 == strcmp(sp->protocol, "https"))
155 sp->port = HTTPS_DEFAULT_PORT;
156 else if (0 == strcmp(sp->protocol, "http"))
157 sp->port = HTTP_DEFAULT_PORT;
162 GNUNET_free (sp->protocol);
163 GNUNET_free (sp->path);
168 if (strlen (host_start) > 0)
169 sp->host = GNUNET_strdup (host_start);
174 GNUNET_free (sp->protocol);
175 GNUNET_free (sp->path);
184 * Convert the transports address to a nice, human-readable
188 * @param type name of the transport that generated the address
189 * @param addr one of the addresses of the host, NULL for the last address
190 * the specific address format depends on the transport
191 * @param addrlen length of the address
192 * @param numeric should (IP) addresses be displayed in numeric form?
193 * @param timeout after how long should we give up?
194 * @param asc function to call on each string
195 * @param asc_cls closure for asc
198 http_common_plugin_address_pretty_printer (void *cls, const char *type,
199 const void *addr, size_t addrlen,
201 struct GNUNET_TIME_Relative timeout,
202 GNUNET_TRANSPORT_AddressStringCallback
205 const struct HttpAddress *address = addr;
207 if (NULL == http_common_plugin_address_to_string (NULL, (char *) type, address, addrlen))
212 asc (asc_cls, http_common_plugin_address_to_string (NULL, (char *) type, address, addrlen));
217 http_common_plugin_address_to_url (void *cls, const void *addr, size_t addrlen)
219 static char rbuf[1024];
220 const struct HttpAddress *address = addr;
221 const char * addr_str;
235 if (addrlen != http_common_address_get_size (address))
240 addr_str = (char *) &address[1];
242 if (addr_str[ntohl(address->urlen) -1] != '\0')
245 memcpy (rbuf, &address[1], ntohl(address->urlen));
250 * Function called for a quick conversion of the binary address to
251 * a numeric address. Note that the caller must not free the
252 * address and that the next call to this function is allowed
253 * to override the address again.
256 * @param plugin the plugin
257 * @param addr binary address
258 * @param addrlen length of the address
259 * @return string representing the same address
262 http_common_plugin_address_to_string (void *cls, char *plugin, const void *addr, size_t addrlen)
264 static char rbuf[1024];
265 const struct HttpAddress *address = addr;
266 const char * addr_str;
269 GNUNET_assert (NULL != plugin);
274 return TRANSPORT_SESSION_INBOUND_STRING;
275 if (addrlen != http_common_address_get_size (address))
277 addr_str = (char *) &address[1];
279 if (addr_str[ntohl(address->urlen) -1] != '\0')
281 GNUNET_asprintf (&res, "%s.%u.%s", plugin, ntohl(address->options), &address[1]);
282 if (strlen(res) + 1 < 500)
284 memcpy (rbuf, res, strlen(res) + 1);
294 * Function called to convert a string address to
297 * @param cls closure ('struct Plugin*')
298 * @param addr string address
299 * @param addrlen length of the address
300 * @param buf location to store the buffer
301 * If the function returns GNUNET_SYSERR, its contents are undefined.
302 * @param added length of created address
303 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
306 http_common_plugin_string_to_address (void *cls,
312 struct HttpAddress *a;
319 /* Format protocol.options.address:port */
323 if ((NULL == addr) || (addrlen == 0))
326 return GNUNET_SYSERR;
328 if ('\0' != addr[addrlen - 1])
331 return GNUNET_SYSERR;
333 if (strlen (addr) != addrlen - 1)
336 return GNUNET_SYSERR;
338 plugin = GNUNET_strdup (addr);
339 optionstr = strchr (plugin, '.');
340 if (NULL == optionstr)
343 GNUNET_free (plugin);
344 return GNUNET_SYSERR;
348 options = atol (optionstr); /* 0 on conversion error, that's ok */
349 address = strchr (optionstr, '.');
353 GNUNET_free (plugin);
354 return GNUNET_SYSERR;
358 urlen = strlen (address) + 1;
360 a = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
361 a->options = htonl(options);
362 a->urlen = htonl(urlen);
363 memcpy (&a[1], address, urlen);
366 (*added) = sizeof (struct HttpAddress) + urlen;
367 GNUNET_free (plugin);
372 * Create a HTTP address from a socketaddr
374 * @param protocol protocol
375 * @param addr sockaddr * address
376 * @param addrlen length of the address
377 * @return the HttpAddress
380 http_common_address_from_socket (const char *protocol, const struct sockaddr *addr, socklen_t addrlen)
382 struct HttpAddress *address = NULL;
386 GNUNET_asprintf(&res, "%s://%s", protocol, GNUNET_a2s (addr, addrlen));
387 len = strlen (res)+1;
389 address = GNUNET_malloc (sizeof (struct HttpAddress) + len);
390 address->options = htonl (HTTP_OPTIONS_NONE);
391 address->urlen = htonl (len);
392 memcpy (&address[1], res, len);
399 * Create a socketaddr from a HTTP address
401 * @param addr sockaddr * address
402 * @param addrlen length of the address
403 * @param res the result:
404 * GNUNET_SYSERR, invalid input,
405 * GNUNET_YES: could convert to ip,
406 * GNUNET_NO: valid input but could not convert to ip (hostname?)
410 http_common_socket_from_address (const void *addr, size_t addrlen, int *res)
412 const struct HttpAddress *ha;
413 struct SplittedHTTPAddress * spa;
414 struct sockaddr_storage *s;
415 (*res) = GNUNET_SYSERR;
419 ha = (const struct HttpAddress *) addr;
430 if (addrlen < sizeof(struct HttpAddress))
435 urlen = ntohl (ha->urlen);
436 if (sizeof(struct HttpAddress) + urlen != addrlen)
438 /* This is a legacy addresses */
441 if (addrlen < sizeof(struct HttpAddress) + urlen)
443 /* This is a legacy addresses */
446 if (((char *) addr)[addrlen - 1] != '\0')
451 spa = http_split_address ((const char *) &ha[1]);
454 (*res) = GNUNET_SYSERR;
458 s = GNUNET_malloc (sizeof (struct sockaddr_storage));
459 GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port);
460 if (GNUNET_SYSERR == GNUNET_STRINGS_to_address_ip (to_conv, strlen(to_conv), s))
462 /* could be a hostname */
467 else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
471 (*res) = GNUNET_SYSERR;
478 http_clean_splitted (spa);
479 GNUNET_free (to_conv);
480 return (struct sockaddr *) s;
484 * Get the length of an address
486 * @param addr address
490 http_common_address_get_size (const struct HttpAddress * addr)
492 return sizeof (struct HttpAddress) + ntohl(addr->urlen);
496 * Compare addr1 to addr2
498 * @param addr1 address1
499 * @param addrlen1 address 1 length
500 * @param addr2 address2
501 * @param addrlen2 address 2 length
502 * @return GNUNET_YES if equal, GNUNET_NO if not, GNUNET_SYSERR on error
505 http_common_cmp_addresses (const void *addr1, size_t addrlen1, const void *addr2, size_t addrlen2)
507 const struct HttpAddress *ha1;
508 const struct HttpAddress *ha2;
509 const char *a1 = (const char *) addr1;
510 const char *a2 = (const char *) addr2;
511 ha1 = (const struct HttpAddress *) a1;
512 ha2 = (const struct HttpAddress *) a2;
515 return GNUNET_SYSERR;
517 return GNUNET_SYSERR;
518 if (a1[addrlen1-1] != '\0')
519 return GNUNET_SYSERR;
522 return GNUNET_SYSERR;
524 return GNUNET_SYSERR;
525 if (a2[addrlen2-1] != '\0')
526 return GNUNET_SYSERR;
528 if (addrlen1 != addrlen2)
530 if (ha1->urlen != ha2->urlen)
533 if (0 == strcmp ((const char *) &ha1[1],(const char *) &ha2[1]))
540 /* end of plugin_transport_http_common.c */