aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/transport/plugin_transport_http_common.c643
1 files changed, 464 insertions, 179 deletions
diff --git a/src/transport/plugin_transport_http_common.c b/src/transport/plugin_transport_http_common.c
index a2e4fd129..a420b8cdc 100644
--- a/src/transport/plugin_transport_http_common.c
+++ b/src/transport/plugin_transport_http_common.c
@@ -1,47 +1,46 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 (C) 2002-2013 Christian Grothoff (and other contributing authors) 3 (C) 2002-2013 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 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 7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version. 8 option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details. 13 General Public License for more details.
14 14
15 You should have received a copy of the GNU General Public License 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 16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19 */
20 20
21/** 21/**
22 * @file transport/plugin_transport_http_common.c 22 * @file transport/plugin_transport_http_common.c
23 * @brief functionality shared by http client and server transport service plugin 23 * @brief functionality shared between http(s)client plugins
24 * @author Matthias Wachs 24 * @author Matthias Wachs
25 */ 25 */
26#include "platform.h" 26#include "platform.h"
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28#include "gnunet_transport_plugin.h" 28#include "gnunet_transport_plugin.h"
29#include "plugin_transport_http_common.h" 29#include "plugin_transport_http_common.h"
30 30#include "gnunet_resolver_service.h"
31 31
32static void 32static void
33http_clean_splitted (struct SplittedHTTPAddress *spa) 33http_clean_splitted (struct SplittedHTTPAddress *spa)
34{ 34{
35 if (NULL != spa) 35 if (NULL != spa)
36 { 36 {
37 GNUNET_free_non_null (spa->protocol); 37 GNUNET_free_non_null(spa->protocol);
38 GNUNET_free_non_null (spa->host); 38 GNUNET_free_non_null(spa->host);
39 GNUNET_free_non_null (spa->path); 39 GNUNET_free_non_null(spa->path);
40 GNUNET_free_non_null (spa); 40 GNUNET_free_non_null(spa);
41 } 41 }
42} 42}
43 43
44
45struct SplittedHTTPAddress * 44struct SplittedHTTPAddress *
46http_split_address (const char * addr) 45http_split_address (const char * addr)
47{ 46{
@@ -60,9 +59,9 @@ http_split_address (const char * addr)
60 host_start = strstr (src, "://"); 59 host_start = strstr (src, "://");
61 if (NULL == host_start) 60 if (NULL == host_start)
62 { 61 {
63 GNUNET_free (src); 62 GNUNET_free(src);
64 GNUNET_free (sp); 63 GNUNET_free(sp);
65 return NULL; 64 return NULL ;
66 } 65 }
67 host_start[0] = '\0'; 66 host_start[0] = '\0';
68 sp->protocol = GNUNET_strdup (protocol_start); 67 sp->protocol = GNUNET_strdup (protocol_start);
@@ -70,10 +69,10 @@ http_split_address (const char * addr)
70 host_start += strlen ("://"); 69 host_start += strlen ("://");
71 if (strlen (host_start) == 0) 70 if (strlen (host_start) == 0)
72 { 71 {
73 GNUNET_free (src); 72 GNUNET_free(src);
74 GNUNET_free (sp->protocol); 73 GNUNET_free(sp->protocol);
75 GNUNET_free (sp); 74 GNUNET_free(sp);
76 return NULL; 75 return NULL ;
77 } 76 }
78 77
79 /* Find path start */ 78 /* Find path start */
@@ -86,41 +85,41 @@ http_split_address (const char * addr)
86 else 85 else
87 sp->path = GNUNET_strdup (""); 86 sp->path = GNUNET_strdup ("");
88 87
89 if (strlen(host_start) < 1) 88 if (strlen (host_start) < 1)
90 { 89 {
91 GNUNET_free (src); 90 GNUNET_free(src);
92 GNUNET_free (sp->protocol); 91 GNUNET_free(sp->protocol);
93 GNUNET_free (sp->path); 92 GNUNET_free(sp->path);
94 GNUNET_free (sp); 93 GNUNET_free(sp);
95 return NULL; 94 return NULL ;
96 } 95 }
97 96
98 if (NULL != (port_start = strrchr (host_start, ':'))) 97 if (NULL != (port_start = strrchr (host_start, ':')))
99 { 98 {
100 /* *We COULD have a port, but also an IPv6 address! */ 99 /* *We COULD have a port, but also an IPv6 address! */
101 if (NULL != (v6_end = strchr(host_start, ']'))) 100 if (NULL != (v6_end = strchr (host_start, ']')))
102 { 101 {
103 if (v6_end < port_start) 102 if (v6_end < port_start)
104 { 103 {
105 /* IPv6 address + port */ 104 /* IPv6 address + port */
106 port_start[0] = '\0'; 105 port_start[0] = '\0';
107 port_start ++; 106 port_start++;
108 sp->port = atoi (port_start); 107 sp->port = atoi (port_start);
109 if ((0 == sp->port) || (65535 < sp->port)) 108 if ((0 == sp->port) || (65535 < sp->port))
110 { 109 {
111 GNUNET_free (src); 110 GNUNET_free(src);
112 GNUNET_free (sp->protocol); 111 GNUNET_free(sp->protocol);
113 GNUNET_free (sp->path); 112 GNUNET_free(sp->path);
114 GNUNET_free (sp); 113 GNUNET_free(sp);
115 return NULL; 114 return NULL ;
116 } 115 }
117 } 116 }
118 else 117 else
119 { 118 {
120 /* IPv6 address + no port */ 119 /* IPv6 address + no port */
121 if (0 == strcmp(sp->protocol, "https")) 120 if (0 == strcmp (sp->protocol, "https"))
122 sp->port = HTTPS_DEFAULT_PORT; 121 sp->port = HTTPS_DEFAULT_PORT;
123 else if (0 == strcmp(sp->protocol, "http")) 122 else if (0 == strcmp (sp->protocol, "http"))
124 sp->port = HTTP_DEFAULT_PORT; 123 sp->port = HTTP_DEFAULT_PORT;
125 } 124 }
126 } 125 }
@@ -128,50 +127,258 @@ http_split_address (const char * addr)
128 { 127 {
129 /* No IPv6 address */ 128 /* No IPv6 address */
130 port_start[0] = '\0'; 129 port_start[0] = '\0';
131 port_start ++; 130 port_start++;
132 sp->port = atoi (port_start); 131 sp->port = atoi (port_start);
133 if ((0 == sp->port) || (65535 < sp->port)) 132 if ((0 == sp->port) || (65535 < sp->port))
134 { 133 {
135 GNUNET_free (src); 134 GNUNET_free(src);
136 GNUNET_free (sp->protocol); 135 GNUNET_free(sp->protocol);
137 GNUNET_free (sp->path); 136 GNUNET_free(sp->path);
138 GNUNET_free (sp); 137 GNUNET_free(sp);
139 return NULL; 138 return NULL ;
140 } 139 }
141 } 140 }
142 } 141 }
143 else 142 else
144 { 143 {
145 /* No ':' as port separator, default port for protocol */ 144 /* No ':' as port separator, default port for protocol */
146 if (0 == strcmp(sp->protocol, "https")) 145 if (0 == strcmp (sp->protocol, "https"))
147 sp->port = HTTPS_DEFAULT_PORT; 146 sp->port = HTTPS_DEFAULT_PORT;
148 else if (0 == strcmp(sp->protocol, "http")) 147 else if (0 == strcmp (sp->protocol, "http"))
149 sp->port = HTTP_DEFAULT_PORT; 148 sp->port = HTTP_DEFAULT_PORT;
150 else 149 else
151 { 150 {
152 GNUNET_break (0); 151 GNUNET_break(0);
153 GNUNET_free (src); 152 GNUNET_free(src);
154 GNUNET_free (sp->protocol); 153 GNUNET_free(sp->protocol);
155 GNUNET_free (sp->path); 154 GNUNET_free(sp->path);
156 GNUNET_free (sp); 155 GNUNET_free(sp);
157 return NULL; 156 return NULL ;
158 } 157 }
159 } 158 }
160 if (strlen (host_start) > 0) 159 if (strlen (host_start) > 0)
161 sp->host = GNUNET_strdup (host_start); 160 sp->host = GNUNET_strdup (host_start);
162 else 161 else
163 { 162 {
164 GNUNET_break (0); 163 GNUNET_break(0);
165 GNUNET_free (src); 164 GNUNET_free(src);
166 GNUNET_free (sp->protocol); 165 GNUNET_free(sp->protocol);
167 GNUNET_free (sp->path); 166 GNUNET_free(sp->path);
168 GNUNET_free (sp); 167 GNUNET_free(sp);
169 return NULL; 168 return NULL ;
170 } 169 }
171 GNUNET_free (src); 170 GNUNET_free(src);
172 return sp; 171 return sp;
173} 172}
174 173
174/**
175 * Closure for #append_port().
176 */
177struct PrettyPrinterContext
178{
179 /**
180 * DLL
181 */
182 struct PrettyPrinterContext *next;
183
184 /**
185 * DLL
186 */
187 struct PrettyPrinterContext *prev;
188
189 /**
190 * Resolver handle
191 */
192 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
193
194 /**
195 * Function to call with the result.
196 */
197 GNUNET_TRANSPORT_AddressStringCallback asc;
198
199 /**
200 * Clsoure for @e asc.
201 */
202 void *asc_cls;
203
204 /**
205 * Timeout task
206 */
207 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
208
209 /**
210 * Splitted Address
211 */
212 struct SplittedHTTPAddress *saddr;
213
214 /**
215 * Plugin String
216 */
217 char *plugin;
218
219 /**
220 * Was conversion successful
221 */
222 int sucess;
223
224 /**
225 * Address options
226 */
227 uint32_t options;
228};
229
230/**
231 * Head of PPC list
232 */
233static struct PrettyPrinterContext *dll_ppc_head;
234
235/**
236 * Tail of PPC list
237 */
238static struct PrettyPrinterContext *dll_ppc_tail;
239
240/**
241 * Function called for a quick conversion of the binary address to
242 * a numeric address. Note that the caller must not free the
243 * address and that the next call to this function is allowed
244 * to override the address again.
245 *
246 * @param plugin the name of the plugin
247 * @param addr binary address
248 * @param addrlen length of the address
249 * @return string representing the same address
250 */
251static const char *
252http_common_plugin_dnsresult_to_address (const char *plugin,
253 const struct SplittedHTTPAddress *saddr, uint32_t options,
254 const char *dnsresult)
255{
256 static char rbuf[1024];
257 char *res;
258
259 GNUNET_asprintf (&res, "%s.%u.%s://%s:%u%s", plugin, options, saddr->protocol,
260 dnsresult, saddr->port, saddr->path);
261 if (strlen (res) + 1 < 500)
262 {
263 memcpy (rbuf, res, strlen (res) + 1);
264 GNUNET_free(res);
265 return rbuf;
266 }
267 GNUNET_break(0);
268 GNUNET_free(res);
269 return NULL ;
270}
271
272static void
273http_common_dns_reverse_lookup_cb (void *cls, const char *hostname)
274{
275 struct PrettyPrinterContext *ppc = cls;
276
277 if (NULL != hostname)
278 {
279 ppc->asc (ppc->asc_cls,
280 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options,
281 hostname), GNUNET_OK);
282 ppc->sucess = GNUNET_YES;
283
284 }
285 else
286 {
287 ppc->asc (ppc->asc_cls, NULL,
288 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
289
290 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
291 http_clean_splitted (ppc->saddr);
292 GNUNET_free(ppc->plugin);
293 GNUNET_free(ppc);
294 }
295}
296
297static int
298http_common_dns_reverse_lookup (const struct sockaddr *sockaddr,
299 socklen_t sockaddr_len, const char *type, struct SplittedHTTPAddress *saddr,
300 uint32_t options,
301 struct GNUNET_TIME_Relative timeout,
302 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
303{
304 struct PrettyPrinterContext *ppc;
305 ppc = GNUNET_new (struct PrettyPrinterContext);
306
307 ppc->sucess = GNUNET_NO;
308 ppc->saddr = saddr;
309 ppc->asc = asc;
310 ppc->asc_cls = asc_cls;
311 ppc->plugin = GNUNET_strdup (type);
312 ppc->options = options;
313
314 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sockaddr, sockaddr_len,
315 GNUNET_YES, timeout, &http_common_dns_reverse_lookup_cb, ppc);
316 if (NULL == ppc->resolver_handle)
317 {
318 GNUNET_free(ppc->plugin);
319 GNUNET_free(ppc);
320 return GNUNET_SYSERR;
321 }
322
323 GNUNET_CONTAINER_DLL_insert(dll_ppc_head, dll_ppc_tail, ppc);
324 return GNUNET_OK;
325}
326
327static void
328http_common_dns_ip_lookup_cb (void *cls, const struct sockaddr *addr,
329 socklen_t addrlen)
330{
331 struct PrettyPrinterContext *ppc = cls;
332
333 if (NULL != addr)
334 {
335 ppc->asc (ppc->asc_cls,
336 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options,
337 GNUNET_a2s (addr, addrlen)), GNUNET_OK);
338 ppc->sucess = GNUNET_YES;
339 ppc->asc (ppc->asc_cls, GNUNET_a2s (addr, addrlen), GNUNET_OK);
340 }
341 else
342 {
343 ppc->asc (ppc->asc_cls, NULL,
344 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
345
346 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
347 GNUNET_free(ppc->plugin);
348 http_clean_splitted (ppc->saddr);
349 GNUNET_free(ppc);
350 }
351}
352
353static int
354http_common_dns_ip_lookup (const char *name, const char *type,
355 struct SplittedHTTPAddress *saddr,
356 uint32_t options,
357 struct GNUNET_TIME_Relative timeout,
358 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
359{
360 struct PrettyPrinterContext *ppc;
361 ppc = GNUNET_new (struct PrettyPrinterContext);
362
363 ppc->sucess = GNUNET_NO;
364 ppc->saddr = saddr;
365 ppc->asc = asc;
366 ppc->asc_cls = asc_cls;
367 ppc->plugin = GNUNET_strdup (type);
368 ppc->options = options;
369
370 ppc->resolver_handle = GNUNET_RESOLVER_ip_get (name, AF_UNSPEC, timeout,
371 &http_common_dns_ip_lookup_cb, ppc);
372 if (NULL == ppc->resolver_handle)
373 {
374 GNUNET_free(ppc->plugin);
375 GNUNET_free(ppc);
376 return GNUNET_SYSERR;
377 }
378
379 GNUNET_CONTAINER_DLL_insert(dll_ppc_head, dll_ppc_tail, ppc);
380 return GNUNET_OK;
381}
175 382
176/** 383/**
177 * Convert the transports address to a nice, human-readable 384 * Convert the transports address to a nice, human-readable
@@ -188,37 +395,137 @@ http_split_address (const char * addr)
188 * @param asc_cls closure for @a asc 395 * @param asc_cls closure for @a asc
189 */ 396 */
190void 397void
191http_common_plugin_address_pretty_printer (void *cls, 398http_common_plugin_address_pretty_printer (void *cls, const char *type,
192 const char *type, 399 const void *addr, size_t addrlen, int numeric,
193 const void *addr, 400 struct GNUNET_TIME_Relative timeout,
194 size_t addrlen, 401 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
195 int numeric,
196 struct GNUNET_TIME_Relative timeout,
197 GNUNET_TRANSPORT_AddressStringCallback asc,
198 void *asc_cls)
199{ 402{
200 const struct HttpAddress *address = addr; 403 const struct HttpAddress *address = addr;
404 struct SplittedHTTPAddress *saddr;
405 struct sockaddr *sock_addr;
201 const char *ret; 406 const char *ret;
407 char *addr_str;
408 int res;
409 int have_ip;
410
411 saddr = NULL;
412 sock_addr = NULL;
413 if ((addrlen < sizeof(struct HttpAddress))
414 || (addrlen != http_common_address_get_size (address)))
415 {
416 GNUNET_break(0);
417 goto handle_error;
418 }
202 419
203 ret = http_common_plugin_address_to_string (type, 420 addr_str = (char *) &address[1];
204 address, 421 if (addr_str[ntohl (address->urlen) - 1] != '\0')
205 addrlen); 422 {
206 asc (asc_cls, 423 GNUNET_break(0);
207 ret, 424 goto handle_error;
208 (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK); 425 }
209 asc (asc_cls,
210 NULL,
211 GNUNET_OK);
212}
213 426
427 saddr = http_split_address (addr_str);
428 if (NULL == saddr)
429 {
430 GNUNET_break(0);
431 goto handle_error;
432 }
433
434 sock_addr = http_common_socket_from_address (addr, addrlen, &res);
435 if (GNUNET_SYSERR == res)
436 {
437 /* Malformed address */
438 GNUNET_break(0);
439 goto handle_error;
440 }
441 else if (GNUNET_NO == res)
442 {
443 /* Could not convert to IP */
444 have_ip = GNUNET_NO;
445 }
446 else if (GNUNET_YES == res)
447 {
448 /* Converted to IP */
449 have_ip = GNUNET_YES;
450 }
451 else
452 {
453 /* Must not happen */
454 GNUNET_break(0);
455 goto handle_error;
456 }
457
458 if ((GNUNET_YES == numeric) && (GNUNET_YES == have_ip))
459 {
460 /* No lookup required */
461 ret = http_common_plugin_address_to_string (type, address, addrlen);
462 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
463 asc (asc_cls, NULL, GNUNET_OK);
464 http_clean_splitted (saddr);
465 GNUNET_free_non_null(sock_addr);
466 return;
467 }
468 else if ((GNUNET_YES == numeric) && (GNUNET_NO == have_ip))
469 {
470 /* Forward lookup */
471 if (GNUNET_SYSERR
472 == http_common_dns_ip_lookup (saddr->host, type, saddr, address->options, timeout, asc,
473 asc_cls))
474 {
475 GNUNET_break(0);
476 goto handle_error;
477 }
478 /* Wait for resolver callback */
479 return;
480 }
481 else if ((GNUNET_NO == numeric) && (GNUNET_YES == have_ip))
482 {
483 /* Reverse lookup */
484 if (GNUNET_SYSERR
485 == http_common_dns_reverse_lookup (sock_addr,
486 (AF_INET == sock_addr->sa_family) ?
487 sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), type,
488 saddr, address->options, timeout, asc, asc_cls))
489 {
490 GNUNET_break(0);
491 goto handle_error;
492 }
493 /* Wait for resolver callback */
494 return;
495 }
496 else if ((GNUNET_NO == numeric) && (GNUNET_NO == have_ip))
497 {
498 /* No lookup required */
499 ret = http_common_plugin_address_to_string (type, address, addrlen);
500 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
501 asc (asc_cls, NULL, GNUNET_OK);
502 GNUNET_free_non_null(sock_addr);
503 http_clean_splitted (saddr);
504 return;
505 }
506 else
507 {
508 /* Error */
509 goto handle_error;
510 }
511 return;
512
513 handle_error:
514 /* Error */
515 asc (asc_cls, NULL, GNUNET_SYSERR);
516 asc (asc_cls, NULL, GNUNET_OK);
517 GNUNET_free_non_null(sock_addr);
518 if (NULL != saddr)
519 http_clean_splitted (saddr);
520 return;
521
522}
214 523
215/** 524/**
216 * FIXME. 525 * FIXME.
217 */ 526 */
218const char * 527const char *
219http_common_plugin_address_to_url (void *cls, 528http_common_plugin_address_to_url (void *cls, const void *addr, size_t addrlen)
220 const void *addr,
221 size_t addrlen)
222{ 529{
223 static char rbuf[1024]; 530 static char rbuf[1024];
224 const struct HttpAddress *address = addr; 531 const struct HttpAddress *address = addr;
@@ -226,29 +533,28 @@ http_common_plugin_address_to_url (void *cls,
226 533
227 if (NULL == addr) 534 if (NULL == addr)
228 { 535 {
229 GNUNET_break (0); 536 GNUNET_break(0);
230 return NULL; 537 return NULL ;
231 } 538 }
232 if (0 >= addrlen) 539 if (0 >= addrlen)
233 { 540 {
234 GNUNET_break (0); 541 GNUNET_break(0);
235 return NULL; 542 return NULL ;
236 } 543 }
237 if (addrlen != http_common_address_get_size (address)) 544 if (addrlen != http_common_address_get_size (address))
238 { 545 {
239 GNUNET_break (0); 546 GNUNET_break(0);
240 return NULL; 547 return NULL ;
241 } 548 }
242 addr_str = (char *) &address[1]; 549 addr_str = (char *) &address[1];
243 550
244 if (addr_str[ntohl(address->urlen) -1] != '\0') 551 if (addr_str[ntohl (address->urlen) - 1] != '\0')
245 return NULL; 552 return NULL ;
246 553
247 memcpy (rbuf, &address[1], ntohl(address->urlen)); 554 memcpy (rbuf, &address[1], ntohl (address->urlen));
248 return rbuf; 555 return rbuf;
249} 556}
250 557
251
252/** 558/**
253 * Function called for a quick conversion of the binary address to 559 * Function called for a quick conversion of the binary address to
254 * a numeric address. Note that the caller must not free the 560 * a numeric address. Note that the caller must not free the
@@ -261,42 +567,37 @@ http_common_plugin_address_to_url (void *cls,
261 * @return string representing the same address 567 * @return string representing the same address
262 */ 568 */
263const char * 569const char *
264http_common_plugin_address_to_string (const char *plugin, 570http_common_plugin_address_to_string (const char *plugin, const void *addr,
265 const void *addr, 571 size_t addrlen)
266 size_t addrlen)
267{ 572{
268 static char rbuf[1024]; 573 static char rbuf[1024];
269 const struct HttpAddress *address = addr; 574 const struct HttpAddress *address = addr;
270 const char * addr_str; 575 const char * addr_str;
271 char *res; 576 char *res;
272 577
273 GNUNET_assert (NULL != plugin); 578 GNUNET_assert(NULL != plugin);
274 if (NULL == addr) 579 if (NULL == addr)
275 return NULL; 580 return NULL ;
276 if (0 == addrlen) 581 if (0 == addrlen)
277 return TRANSPORT_SESSION_INBOUND_STRING; 582 return NULL ;
278 if (addrlen != http_common_address_get_size (address)) 583 if (addrlen != http_common_address_get_size (address))
279 return NULL; 584 return NULL ;
280 addr_str = (char *) &address[1]; 585 addr_str = (char *) &address[1];
281 if (addr_str[ntohl(address->urlen) -1] != '\0') 586 if (addr_str[ntohl (address->urlen) - 1] != '\0')
282 return NULL; 587 return NULL ;
283 GNUNET_asprintf (&res, 588 GNUNET_asprintf (&res, "%s.%u.%s", plugin, ntohl (address->options),
284 "%s.%u.%s", 589 &address[1]);
285 plugin, 590 if (strlen (res) + 1 < 500)
286 ntohl (address->options), 591 {
287 &address[1]); 592 memcpy (rbuf, res, strlen (res) + 1);
288 if (strlen(res) + 1 < 500) 593 GNUNET_free(res);
289 {
290 memcpy (rbuf, res, strlen(res) + 1);
291 GNUNET_free (res);
292 return rbuf; 594 return rbuf;
293 } 595 }
294 GNUNET_break (0); 596 GNUNET_break(0);
295 GNUNET_free (res); 597 GNUNET_free(res);
296 return NULL; 598 return NULL ;
297} 599}
298 600
299
300/** 601/**
301 * Function called to convert a string address to 602 * Function called to convert a string address to
302 * a binary address. 603 * a binary address.
@@ -310,11 +611,8 @@ http_common_plugin_address_to_string (const char *plugin,
310 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 611 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
311 */ 612 */
312int 613int
313http_common_plugin_string_to_address (void *cls, 614http_common_plugin_string_to_address (void *cls, const char *addr,
314 const char *addr, 615 uint16_t addrlen, void **buf, size_t *added)
315 uint16_t addrlen,
316 void **buf,
317 size_t *added)
318{ 616{
319 struct HttpAddress *a; 617 struct HttpAddress *a;
320 char *address; 618 char *address;
@@ -329,53 +627,52 @@ http_common_plugin_string_to_address (void *cls,
329 optionstr = NULL; 627 optionstr = NULL;
330 if ((NULL == addr) || (addrlen == 0)) 628 if ((NULL == addr) || (addrlen == 0))
331 { 629 {
332 GNUNET_break (0); 630 GNUNET_break(0);
333 return GNUNET_SYSERR; 631 return GNUNET_SYSERR;
334 } 632 }
335 if ('\0' != addr[addrlen - 1]) 633 if ('\0' != addr[addrlen - 1])
336 { 634 {
337 GNUNET_break (0); 635 GNUNET_break(0);
338 return GNUNET_SYSERR; 636 return GNUNET_SYSERR;
339 } 637 }
340 if (strlen (addr) != addrlen - 1) 638 if (strlen (addr) != addrlen - 1)
341 { 639 {
342 GNUNET_break (0); 640 GNUNET_break(0);
343 return GNUNET_SYSERR; 641 return GNUNET_SYSERR;
344 } 642 }
345 plugin = GNUNET_strdup (addr); 643 plugin = GNUNET_strdup (addr);
346 optionstr = strchr (plugin, '.'); 644 optionstr = strchr (plugin, '.');
347 if (NULL == optionstr) 645 if (NULL == optionstr)
348 { 646 {
349 GNUNET_break (0); 647 GNUNET_break(0);
350 GNUNET_free (plugin); 648 GNUNET_free(plugin);
351 return GNUNET_SYSERR; 649 return GNUNET_SYSERR;
352 } 650 }
353 optionstr[0] = '\0'; 651 optionstr[0] = '\0';
354 optionstr ++; 652 optionstr++;
355 options = atol (optionstr); /* 0 on conversion error, that's ok */ 653 options = atol (optionstr); /* 0 on conversion error, that's ok */
356 address = strchr (optionstr, '.'); 654 address = strchr (optionstr, '.');
357 if (NULL == address) 655 if (NULL == address)
358 { 656 {
359 GNUNET_break (0); 657 GNUNET_break(0);
360 GNUNET_free (plugin); 658 GNUNET_free(plugin);
361 return GNUNET_SYSERR; 659 return GNUNET_SYSERR;
362 } 660 }
363 address[0] = '\0'; 661 address[0] = '\0';
364 address ++; 662 address++;
365 urlen = strlen (address) + 1; 663 urlen = strlen (address) + 1;
366 664
367 a = GNUNET_malloc (sizeof (struct HttpAddress) + urlen); 665 a = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
368 a->options = htonl(options); 666 a->options = htonl (options);
369 a->urlen = htonl(urlen); 667 a->urlen = htonl (urlen);
370 memcpy (&a[1], address, urlen); 668 memcpy (&a[1], address, urlen);
371 669
372 (*buf) = a; 670 (*buf) = a;
373 (*added) = sizeof (struct HttpAddress) + urlen; 671 (*added) = sizeof(struct HttpAddress) + urlen;
374 GNUNET_free (plugin); 672 GNUNET_free(plugin);
375 return GNUNET_OK; 673 return GNUNET_OK;
376} 674}
377 675
378
379/** 676/**
380 * Create a HTTP address from a socketaddr 677 * Create a HTTP address from a socketaddr
381 * 678 *
@@ -386,27 +683,22 @@ http_common_plugin_string_to_address (void *cls,
386 */ 683 */
387struct HttpAddress * 684struct HttpAddress *
388http_common_address_from_socket (const char *protocol, 685http_common_address_from_socket (const char *protocol,
389 const struct sockaddr *addr, 686 const struct sockaddr *addr, socklen_t addrlen)
390 socklen_t addrlen)
391{ 687{
392 struct HttpAddress *address = NULL; 688 struct HttpAddress *address = NULL;
393 char *res; 689 char *res;
394 size_t len; 690 size_t len;
395 691
396 GNUNET_asprintf(&res, 692 GNUNET_asprintf (&res, "%s://%s", protocol, GNUNET_a2s (addr, addrlen));
397 "%s://%s", 693 len = strlen (res) + 1;
398 protocol,
399 GNUNET_a2s (addr, addrlen));
400 len = strlen (res)+1;
401 address = GNUNET_malloc (sizeof (struct HttpAddress) + len); 694 address = GNUNET_malloc (sizeof (struct HttpAddress) + len);
402 address->options = htonl (HTTP_OPTIONS_NONE); 695 address->options = htonl (HTTP_OPTIONS_NONE);
403 address->urlen = htonl (len); 696 address->urlen = htonl (len);
404 memcpy (&address[1], res, len); 697 memcpy (&address[1], res, len);
405 GNUNET_free (res); 698 GNUNET_free(res);
406 return address; 699 return address;
407} 700}
408 701
409
410/** 702/**
411 * Create a socketaddr from a HTTP address 703 * Create a socketaddr from a HTTP address
412 * 704 *
@@ -419,9 +711,7 @@ http_common_address_from_socket (const char *protocol,
419 * @return the string 711 * @return the string
420 */ 712 */
421struct sockaddr * 713struct sockaddr *
422http_common_socket_from_address (const void *addr, 714http_common_socket_from_address (const void *addr, size_t addrlen, int *res)
423 size_t addrlen,
424 int *res)
425{ 715{
426 const struct HttpAddress *ha; 716 const struct HttpAddress *ha;
427 struct SplittedHTTPAddress * spa; 717 struct SplittedHTTPAddress * spa;
@@ -466,35 +756,35 @@ http_common_socket_from_address (const void *addr,
466 if (NULL == spa) 756 if (NULL == spa)
467 { 757 {
468 (*res) = GNUNET_SYSERR; 758 (*res) = GNUNET_SYSERR;
469 return NULL; 759 return NULL ;
470 } 760 }
471 761
472 s = GNUNET_new (struct sockaddr_storage); 762 s = GNUNET_new (struct sockaddr_storage);
473 GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port); 763 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)) 764 if (GNUNET_SYSERR
765 == GNUNET_STRINGS_to_address_ip (to_conv, strlen (to_conv), s))
475 { 766 {
476 /* could be a hostname */ 767 /* could be a hostname */
477 GNUNET_free (s); 768 GNUNET_free(s);
478 (*res) = GNUNET_NO; 769 (*res) = GNUNET_NO;
479 s = NULL; 770 s = NULL;
480 } 771 }
481 else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family)) 772 else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
482 { 773 {
483 774
484 GNUNET_free (s); 775 GNUNET_free(s);
485 (*res) = GNUNET_SYSERR; 776 (*res) = GNUNET_SYSERR;
486 s = NULL; 777 s = NULL;
487 } 778 }
488 else 779 else
489 { 780 {
490 (*res) = GNUNET_YES; 781 (*res) = GNUNET_YES;
491 } 782 }
492 http_clean_splitted (spa); 783 http_clean_splitted (spa);
493 GNUNET_free (to_conv); 784 GNUNET_free(to_conv);
494 return (struct sockaddr *) s; 785 return (struct sockaddr *) s;
495} 786}
496 787
497
498/** 788/**
499 * Get the length of an address 789 * Get the length of an address
500 * 790 *
@@ -504,10 +794,9 @@ http_common_socket_from_address (const void *addr,
504size_t 794size_t
505http_common_address_get_size (const struct HttpAddress * addr) 795http_common_address_get_size (const struct HttpAddress * addr)
506{ 796{
507 return sizeof (struct HttpAddress) + ntohl(addr->urlen); 797 return sizeof(struct HttpAddress) + ntohl (addr->urlen);
508} 798}
509 799
510
511/** 800/**
512 * Compare addr1 to addr2 801 * Compare addr1 to addr2
513 * 802 *
@@ -518,10 +807,8 @@ http_common_address_get_size (const struct HttpAddress * addr)
518 * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error 807 * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error
519 */ 808 */
520size_t 809size_t
521http_common_cmp_addresses (const void *addr1, 810http_common_cmp_addresses (const void *addr1, size_t addrlen1,
522 size_t addrlen1, 811 const void *addr2, size_t addrlen2)
523 const void *addr2,
524 size_t addrlen2)
525{ 812{
526 const char *a1 = addr1; 813 const char *a1 = addr1;
527 const char *a2 = addr2; 814 const char *a2 = addr2;
@@ -531,17 +818,17 @@ http_common_cmp_addresses (const void *addr1,
531 ha2 = (const struct HttpAddress *) a2; 818 ha2 = (const struct HttpAddress *) a2;
532 819
533 if (NULL == a1) 820 if (NULL == a1)
534 return GNUNET_SYSERR; 821 return GNUNET_SYSERR;
535 if (0 >= addrlen1) 822 if (0 >= addrlen1)
536 return GNUNET_SYSERR; 823 return GNUNET_SYSERR;
537 if (a1[addrlen1-1] != '\0') 824 if (a1[addrlen1 - 1] != '\0')
538 return GNUNET_SYSERR; 825 return GNUNET_SYSERR;
539 826
540 if (NULL == a2) 827 if (NULL == a2)
541 return GNUNET_SYSERR; 828 return GNUNET_SYSERR;
542 if (0 >= addrlen2) 829 if (0 >= addrlen2)
543 return GNUNET_SYSERR; 830 return GNUNET_SYSERR;
544 if (a2[addrlen2-1] != '\0') 831 if (a2[addrlen2 - 1] != '\0')
545 return GNUNET_SYSERR; 832 return GNUNET_SYSERR;
546 833
547 if (addrlen1 != addrlen2) 834 if (addrlen1 != addrlen2)
@@ -549,11 +836,9 @@ http_common_cmp_addresses (const void *addr1,
549 if (ha1->urlen != ha2->urlen) 836 if (ha1->urlen != ha2->urlen)
550 return GNUNET_NO; 837 return GNUNET_NO;
551 838
552 if (0 == strcmp ((const char *) &ha1[1],(const char *) &ha2[1])) 839 if (0 == strcmp ((const char *) &ha1[1], (const char *) &ha2[1]))
553 return GNUNET_YES; 840 return GNUNET_YES;
554 return GNUNET_NO; 841 return GNUNET_NO;
555} 842}
556 843
557
558
559/* end of plugin_transport_http_common.c */ 844/* end of plugin_transport_http_common.c */