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
28 #include "gnunet_common.h"
29 #include "gnunet_transport_plugin.h"
30 #include "plugin_transport_http_common.h"
32 struct SplittedHTTPAddress
40 struct SplittedHTTPAddress *
41 http_split_address (const char * addr)
43 struct SplittedHTTPAddress *sp;
44 char *src = GNUNET_strdup (addr);
45 char *protocol_start = NULL;
46 char *host_start = NULL;
48 char *port_start = NULL;
49 char *path_start = NULL;
52 sp = GNUNET_malloc (sizeof (struct SplittedHTTPAddress));
54 /* Address string consists of protocol://host[:port]path*/
56 host_start = strstr (src, "://");
57 if (NULL == host_start)
65 sp->protocol = GNUNET_strdup (protocol_start);
67 host_start += strlen ("://");
68 if (strlen (host_start) == 0)
71 GNUNET_free (sp->protocol);
77 path_start = strchr (host_start, '/');
78 if (NULL != path_start)
80 sp->path = GNUNET_strdup (path_start);
84 sp->path = GNUNET_strdup ("");
86 if (strlen(host_start) < 1)
89 GNUNET_free (sp->protocol);
90 GNUNET_free (sp->path);
95 if (NULL != (port_start = strrchr (host_start, ':')))
97 /* *We COULD have a port, but also an IPv6 address! */
98 if (NULL != (v6_end = strchr(host_start, ']')))
100 if (v6_end < port_start)
102 /* IPv6 address + port */
103 port_start[0] = '\0';
105 sp->port = atoi (port_start);
109 /* IPv6 address + no port */
110 if (0 == strcmp(sp->protocol, "https"))
111 sp->port = HTTPS_DEFAULT_PORT;
112 else if (0 == strcmp(sp->protocol, "http"))
113 sp->port = HTTP_DEFAULT_PORT;
118 /* No IPv6 address */
119 port_start[0] = '\0';
121 sp->port = atoi (port_start);
126 /* No ':' as port separator, default port for protocol */
127 if (0 == strcmp(sp->protocol, "https"))
128 sp->port = HTTPS_DEFAULT_PORT;
129 else if (0 == strcmp(sp->protocol, "http"))
130 sp->port = HTTP_DEFAULT_PORT;
135 GNUNET_free (sp->protocol);
136 GNUNET_free (sp->path);
141 if (strlen (host_start) > 0)
142 sp->host = GNUNET_strdup (host_start);
147 GNUNET_free (sp->protocol);
148 GNUNET_free (sp->path);
153 //fprintf (stderr, "addr: `%s' protocol: `%s', host `%s' port `%u' path `%s'\n", addr, sp->protocol, sp->host, sp->port, sp->path);
158 * Convert the transports address to a nice, human-readable
162 * @param type name of the transport that generated the address
163 * @param addr one of the addresses of the host, NULL for the last address
164 * the specific address format depends on the transport
165 * @param addrlen length of the address
166 * @param numeric should (IP) addresses be displayed in numeric form?
167 * @param timeout after how long should we give up?
168 * @param asc function to call on each string
169 * @param asc_cls closure for asc
172 http_common_plugin_address_pretty_printer (void *cls, const char *type,
173 const void *addr, size_t addrlen,
175 struct GNUNET_TIME_Relative timeout,
176 GNUNET_TRANSPORT_AddressStringCallback
179 const char *saddr = (const char *) addr;
181 if ( (NULL == saddr) ||
183 ('\0' != saddr[addrlen-1]) )
188 asc (asc_cls, saddr);
194 * Function called for a quick conversion of the binary address to
195 * a numeric address. Note that the caller must not free the
196 * address and that the next call to this function is allowed
197 * to override the address again.
200 * @param addr binary address
201 * @param addrlen length of the address
202 * @return string representing the same address
205 http_common_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
207 const char *saddr = (const char *) addr;
212 if (saddr[addrlen-1] != '\0')
218 * Function called to convert a string address to
221 * @param cls closure ('struct Plugin*')
222 * @param addr string address
223 * @param addrlen length of the address
224 * @param buf location to store the buffer
225 * If the function returns GNUNET_SYSERR, its contents are undefined.
226 * @param added length of created address
227 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
230 http_common_plugin_string_to_address (void *cls,
237 return GNUNET_SYSERR;
239 return GNUNET_SYSERR;
240 if (addr[addrlen-1] != '\0')
241 return GNUNET_SYSERR;
243 (*buf) = strdup (addr);
244 (*added) = strlen (addr) + 1;
249 * Create a HTTP address from a socketaddr
251 * @param protocol protocol
252 * @param addr sockaddr * address
253 * @param addrlen length of the address
257 http_common_address_from_socket (const char *protocol, const struct sockaddr *addr, socklen_t addrlen)
260 GNUNET_asprintf(&res, "%s://%s", protocol, GNUNET_a2s (addr, addrlen));
265 * Create a socketaddr from a HTTP address
267 * @param addr sockaddr * address
268 * @param addrlen length of the address
269 * @param res the result:
270 * GNUNET_SYSERR, invalid input,
271 * GNUNET_YES: could convert to ip,
272 * GNUNET_NO: valid input but could not convert to ip (hostname?)
276 http_common_socket_from_address (const void *addr, size_t addrlen, int *res)
278 struct sockaddr_storage *s;
282 (*res) = GNUNET_SYSERR;
294 if (((char *) addr)[addrlen-1] != '\0')
300 addrs_org = strdup ((char *) addr);
301 addrs = strstr (addrs_org , "://");
305 GNUNET_free (addrs_org);
309 if (strlen (addrs) < 3)
312 GNUNET_free (addrs_org);
318 addrs_end = strchr (addrs, '/');
319 if (NULL != addrs_end)
320 addrs[strlen (addrs) - strlen(addrs_end)] = '\0';
322 s = GNUNET_malloc (sizeof (struct sockaddr_storage));
323 if (GNUNET_SYSERR == GNUNET_STRINGS_to_address_ip (addrs, strlen(addrs), s))
325 /* could be a hostname */
327 GNUNET_free (addrs_org);
333 if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
337 GNUNET_free (addrs_org);
338 (*res) = GNUNET_SYSERR;
343 GNUNET_free (addrs_org);
344 return (struct sockaddr *) s;
348 * Get the length of an address
350 * @param addr address
354 http_common_address_get_size (const void *addr)
356 return strlen (addr) + 1;
360 * Compare addr1 to addr2
362 * @param addr1 address1
363 * @param addrlen1 address 1 length
364 * @param addr2 address2
365 * @param addrlen2 address 2 length
366 * @return GNUNET_YES if equal, GNUNET_NO if not, GNUNET_SYSERR on error
369 http_common_cmp_addresses (const void *addr1, size_t addrlen1, const void *addr2, size_t addrlen2)
371 const char *a1 = (const char *) addr1;
372 const char *a2 = (const char *) addr2;
375 return GNUNET_SYSERR;
377 return GNUNET_SYSERR;
378 if (a1[addrlen1-1] != '\0')
379 return GNUNET_SYSERR;
382 return GNUNET_SYSERR;
384 return GNUNET_SYSERR;
385 if (a2[addrlen2-1] != '\0')
386 return GNUNET_SYSERR;
388 if (addrlen1 != addrlen2)
391 if (0 == strcmp (addr1, addr2))
398 /* end of plugin_transport_http_common.c */