aboutsummaryrefslogtreecommitdiff
path: root/src/transport/plugin_transport_http_common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/plugin_transport_http_common.c')
-rw-r--r--src/transport/plugin_transport_http_common.c961
1 files changed, 480 insertions, 481 deletions
diff --git a/src/transport/plugin_transport_http_common.c b/src/transport/plugin_transport_http_common.c
index 0a0545dd6..902fc5c36 100644
--- a/src/transport/plugin_transport_http_common.c
+++ b/src/transport/plugin_transport_http_common.c
@@ -1,19 +1,19 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2002-2013 GNUnet e.V. 3 Copyright (C) 2002-2013 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your 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 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
@@ -30,144 +30,145 @@
30#include "gnunet_resolver_service.h" 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 44
45struct SplittedHTTPAddress * 45struct SplittedHTTPAddress *
46http_split_address (const char * addr) 46http_split_address(const char * addr)
47{ 47{
48 struct SplittedHTTPAddress *sp; 48 struct SplittedHTTPAddress *sp;
49 char *src = GNUNET_strdup (addr); 49 char *src = GNUNET_strdup(addr);
50 char *protocol_start = NULL; 50 char *protocol_start = NULL;
51 char *host_start = NULL; 51 char *host_start = NULL;
52 char *v6_end = NULL; 52 char *v6_end = NULL;
53 char *port_start = NULL; 53 char *port_start = NULL;
54 char *path_start = NULL; 54 char *path_start = NULL;
55
55 protocol_start = src; 56 protocol_start = src;
56 57
57 sp = GNUNET_new (struct SplittedHTTPAddress); 58 sp = GNUNET_new(struct SplittedHTTPAddress);
58 /* Address string consists of protocol://host[:port]path*/ 59 /* Address string consists of protocol://host[:port]path*/
59 60
60 host_start = strstr (src, "://"); 61 host_start = strstr(src, "://");
61 if (NULL == host_start) 62 if (NULL == host_start)
62 { 63 {
63 GNUNET_free(src); 64 GNUNET_free(src);
64 GNUNET_free(sp); 65 GNUNET_free(sp);
65 return NULL ; 66 return NULL;
66 } 67 }
67 host_start[0] = '\0'; 68 host_start[0] = '\0';
68 sp->protocol = GNUNET_strdup (protocol_start); 69 sp->protocol = GNUNET_strdup(protocol_start);
69 70
70 host_start += strlen ("://"); 71 host_start += strlen("://");
71 if (strlen (host_start) == 0) 72 if (strlen(host_start) == 0)
72 { 73 {
73 GNUNET_free(src); 74 GNUNET_free(src);
74 GNUNET_free(sp->protocol); 75 GNUNET_free(sp->protocol);
75 GNUNET_free(sp); 76 GNUNET_free(sp);
76 return NULL ; 77 return NULL;
77 } 78 }
78 79
79 /* Find path start */ 80 /* Find path start */
80 path_start = strchr (host_start, '/'); 81 path_start = strchr(host_start, '/');
81 if (NULL != path_start) 82 if (NULL != path_start)
82 { 83 {
83 sp->path = GNUNET_strdup (path_start); 84 sp->path = GNUNET_strdup(path_start);
84 path_start[0] = '\0'; 85 path_start[0] = '\0';
85 } 86 }
87 else
88 sp->path = GNUNET_strdup("");
89
90 if (strlen(host_start) < 1)
91 {
92 GNUNET_free(src);
93 GNUNET_free(sp->protocol);
94 GNUNET_free(sp->path);
95 GNUNET_free(sp);
96 return NULL;
97 }
98
99 if (NULL != (port_start = strrchr(host_start, ':')))
100 {
101 /* *We COULD have a port, but also an IPv6 address! */
102 if (NULL != (v6_end = strchr(host_start, ']')))
103 {
104 if (v6_end < port_start)
105 {
106 /* IPv6 address + port */
107 port_start[0] = '\0';
108 port_start++;
109 sp->port = atoi(port_start);
110 if ((0 == sp->port) || (65535 < sp->port))
111 {
112 GNUNET_free(src);
113 GNUNET_free(sp->protocol);
114 GNUNET_free(sp->path);
115 GNUNET_free(sp);
116 return NULL;
117 }
118 }
119 else
120 {
121 /* IPv6 address + no port */
122 if (0 == strcmp(sp->protocol, "https"))
123 sp->port = HTTPS_DEFAULT_PORT;
124 else if (0 == strcmp(sp->protocol, "http"))
125 sp->port = HTTP_DEFAULT_PORT;
126 }
127 }
128 else
129 {
130 /* No IPv6 address */
131 port_start[0] = '\0';
132 port_start++;
133 sp->port = atoi(port_start);
134 if ((0 == sp->port) || (65535 < sp->port))
135 {
136 GNUNET_free(src);
137 GNUNET_free(sp->protocol);
138 GNUNET_free(sp->path);
139 GNUNET_free(sp);
140 return NULL;
141 }
142 }
143 }
86 else 144 else
87 sp->path = GNUNET_strdup (""); 145 {
88 146 /* No ':' as port separator, default port for protocol */
89 if (strlen (host_start) < 1) 147 if (0 == strcmp(sp->protocol, "https"))
90 { 148 sp->port = HTTPS_DEFAULT_PORT;
91 GNUNET_free(src); 149 else if (0 == strcmp(sp->protocol, "http"))
92 GNUNET_free(sp->protocol); 150 sp->port = HTTP_DEFAULT_PORT;
93 GNUNET_free(sp->path); 151 else
94 GNUNET_free(sp);
95 return NULL ;
96 }
97
98 if (NULL != (port_start = strrchr (host_start, ':')))
99 {
100 /* *We COULD have a port, but also an IPv6 address! */
101 if (NULL != (v6_end = strchr (host_start, ']')))
102 {
103 if (v6_end < port_start)
104 {
105 /* IPv6 address + port */
106 port_start[0] = '\0';
107 port_start++;
108 sp->port = atoi (port_start);
109 if ((0 == sp->port) || (65535 < sp->port))
110 { 152 {
153 GNUNET_break(0);
111 GNUNET_free(src); 154 GNUNET_free(src);
112 GNUNET_free(sp->protocol); 155 GNUNET_free(sp->protocol);
113 GNUNET_free(sp->path); 156 GNUNET_free(sp->path);
114 GNUNET_free(sp); 157 GNUNET_free(sp);
115 return NULL ; 158 return NULL;
116 } 159 }
117 } 160 }
118 else 161 if (strlen(host_start) > 0)
119 { 162 sp->host = GNUNET_strdup(host_start);
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;
125 }
126 }
127 else
128 {
129 /* No IPv6 address */
130 port_start[0] = '\0';
131 port_start++;
132 sp->port = atoi (port_start);
133 if ((0 == sp->port) || (65535 < sp->port))
134 {
135 GNUNET_free(src);
136 GNUNET_free(sp->protocol);
137 GNUNET_free(sp->path);
138 GNUNET_free(sp);
139 return NULL ;
140 }
141 }
142 }
143 else 163 else
144 {
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;
150 else
151 { 164 {
152 GNUNET_break(0); 165 GNUNET_break(0);
153 GNUNET_free(src); 166 GNUNET_free(src);
154 GNUNET_free(sp->protocol); 167 GNUNET_free(sp->protocol);
155 GNUNET_free(sp->path); 168 GNUNET_free(sp->path);
156 GNUNET_free(sp); 169 GNUNET_free(sp);
157 return NULL ; 170 return NULL;
158 } 171 }
159 }
160 if (strlen (host_start) > 0)
161 sp->host = GNUNET_strdup (host_start);
162 else
163 {
164 GNUNET_break(0);
165 GNUNET_free(src);
166 GNUNET_free(sp->protocol);
167 GNUNET_free(sp->path);
168 GNUNET_free(sp);
169 return NULL ;
170 }
171 GNUNET_free(src); 172 GNUNET_free(src);
172 return sp; 173 return sp;
173} 174}
@@ -175,8 +176,7 @@ http_split_address (const char * addr)
175/** 176/**
176 * Closure for #append_port(). 177 * Closure for #append_port().
177 */ 178 */
178struct PrettyPrinterContext 179struct PrettyPrinterContext {
179{
180 /** 180 /**
181 * DLL 181 * DLL
182 */ 182 */
@@ -251,150 +251,149 @@ static struct PrettyPrinterContext *dll_ppc_tail;
251 * @return string representing the same address or NULL on error 251 * @return string representing the same address or NULL on error
252 */ 252 */
253static const char * 253static const char *
254http_common_plugin_dnsresult_to_address (const char *plugin, 254http_common_plugin_dnsresult_to_address(const char *plugin,
255 const struct SplittedHTTPAddress *saddr, 255 const struct SplittedHTTPAddress *saddr,
256 uint32_t options, 256 uint32_t options,
257 const char *dnsresult) 257 const char *dnsresult)
258{ 258{
259 static char rbuf[1024]; 259 static char rbuf[1024];
260 char *res; 260 char *res;
261 261
262 GNUNET_asprintf (&res, "%s.%u.%s://%s:%u%s", plugin, options, saddr->protocol, 262 GNUNET_asprintf(&res, "%s.%u.%s://%s:%u%s", plugin, options, saddr->protocol,
263 dnsresult, saddr->port, saddr->path); 263 dnsresult, saddr->port, saddr->path);
264 if (strlen (res) + 1 < 500) 264 if (strlen(res) + 1 < 500)
265 { 265 {
266 GNUNET_memcpy (rbuf, res, strlen (res) + 1); 266 GNUNET_memcpy(rbuf, res, strlen(res) + 1);
267 GNUNET_free(res); 267 GNUNET_free(res);
268 return rbuf; 268 return rbuf;
269 } 269 }
270 GNUNET_break(0); 270 GNUNET_break(0);
271 GNUNET_free(res); 271 GNUNET_free(res);
272 return NULL ; 272 return NULL;
273} 273}
274 274
275 275
276static void 276static void
277http_common_dns_reverse_lookup_cb (void *cls, const char *hostname) 277http_common_dns_reverse_lookup_cb(void *cls, const char *hostname)
278{ 278{
279 struct PrettyPrinterContext *ppc = cls; 279 struct PrettyPrinterContext *ppc = cls;
280 280
281 if (NULL != hostname) 281 if (NULL != hostname)
282 { 282 {
283 ppc->asc (ppc->asc_cls, 283 ppc->asc(ppc->asc_cls,
284 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options, 284 http_common_plugin_dnsresult_to_address(ppc->plugin, ppc->saddr, ppc->options,
285 hostname), GNUNET_OK); 285 hostname), GNUNET_OK);
286 ppc->sucess = GNUNET_YES; 286 ppc->sucess = GNUNET_YES;
287 287 }
288 }
289 else 288 else
290 { 289 {
291 ppc->asc (ppc->asc_cls, NULL, 290 ppc->asc(ppc->asc_cls, NULL,
292 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK); 291 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
293 292
294 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc); 293 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
295 http_clean_splitted (ppc->saddr); 294 http_clean_splitted(ppc->saddr);
296 GNUNET_free(ppc->plugin); 295 GNUNET_free(ppc->plugin);
297 GNUNET_free(ppc); 296 GNUNET_free(ppc);
298 } 297 }
299} 298}
300 299
301 300
302static int 301static int
303http_common_dns_reverse_lookup (const struct sockaddr *sockaddr, 302http_common_dns_reverse_lookup(const struct sockaddr *sockaddr,
304 socklen_t sockaddr_len, 303 socklen_t sockaddr_len,
305 const char *type, 304 const char *type,
306 struct SplittedHTTPAddress *saddr, 305 struct SplittedHTTPAddress *saddr,
307 uint32_t options, 306 uint32_t options,
308 struct GNUNET_TIME_Relative timeout, 307 struct GNUNET_TIME_Relative timeout,
309 GNUNET_TRANSPORT_AddressStringCallback asc, 308 GNUNET_TRANSPORT_AddressStringCallback asc,
310 void *asc_cls) 309 void *asc_cls)
311{ 310{
312 struct PrettyPrinterContext *ppc; 311 struct PrettyPrinterContext *ppc;
313 312
314 ppc = GNUNET_new (struct PrettyPrinterContext); 313 ppc = GNUNET_new(struct PrettyPrinterContext);
315 ppc->saddr = saddr; 314 ppc->saddr = saddr;
316 ppc->asc = asc; 315 ppc->asc = asc;
317 ppc->asc_cls = asc_cls; 316 ppc->asc_cls = asc_cls;
318 ppc->plugin = GNUNET_strdup (type); 317 ppc->plugin = GNUNET_strdup(type);
319 ppc->options = options; 318 ppc->options = options;
320 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sockaddr, 319 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get(sockaddr,
321 sockaddr_len, 320 sockaddr_len,
322 GNUNET_YES, 321 GNUNET_YES,
323 timeout, 322 timeout,
324 &http_common_dns_reverse_lookup_cb, 323 &http_common_dns_reverse_lookup_cb,
325 ppc); 324 ppc);
326 if (NULL == ppc->resolver_handle) 325 if (NULL == ppc->resolver_handle)
327 { 326 {
328 GNUNET_free(ppc->plugin); 327 GNUNET_free(ppc->plugin);
329 GNUNET_free(ppc); 328 GNUNET_free(ppc);
330 return GNUNET_SYSERR; 329 return GNUNET_SYSERR;
331 } 330 }
332 GNUNET_CONTAINER_DLL_insert (dll_ppc_head, 331 GNUNET_CONTAINER_DLL_insert(dll_ppc_head,
333 dll_ppc_tail, 332 dll_ppc_tail,
334 ppc); 333 ppc);
335 return GNUNET_OK; 334 return GNUNET_OK;
336} 335}
337 336
338 337
339static void 338static void
340http_common_dns_ip_lookup_cb (void *cls, 339http_common_dns_ip_lookup_cb(void *cls,
341 const struct sockaddr *addr, 340 const struct sockaddr *addr,
342 socklen_t addrlen) 341 socklen_t addrlen)
343{ 342{
344 struct PrettyPrinterContext *ppc = cls; 343 struct PrettyPrinterContext *ppc = cls;
345 344
346 if (NULL != addr) 345 if (NULL != addr)
347 { 346 {
348 ppc->asc (ppc->asc_cls, 347 ppc->asc(ppc->asc_cls,
349 http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options, 348 http_common_plugin_dnsresult_to_address(ppc->plugin, ppc->saddr, ppc->options,
350 GNUNET_a2s (addr, addrlen)), GNUNET_OK); 349 GNUNET_a2s(addr, addrlen)), GNUNET_OK);
351 ppc->sucess = GNUNET_YES; 350 ppc->sucess = GNUNET_YES;
352 ppc->asc (ppc->asc_cls, GNUNET_a2s (addr, addrlen), GNUNET_OK); 351 ppc->asc(ppc->asc_cls, GNUNET_a2s(addr, addrlen), GNUNET_OK);
353 } 352 }
354 else 353 else
355 { 354 {
356 ppc->asc (ppc->asc_cls, NULL, 355 ppc->asc(ppc->asc_cls, NULL,
357 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK); 356 (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
358 357
359 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc); 358 GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
360 GNUNET_free(ppc->plugin); 359 GNUNET_free(ppc->plugin);
361 http_clean_splitted (ppc->saddr); 360 http_clean_splitted(ppc->saddr);
362 GNUNET_free(ppc); 361 GNUNET_free(ppc);
363 } 362 }
364} 363}
365 364
366 365
367static int 366static int
368http_common_dns_ip_lookup (const char *name, 367http_common_dns_ip_lookup(const char *name,
369 const char *type, 368 const char *type,
370 struct SplittedHTTPAddress *saddr, 369 struct SplittedHTTPAddress *saddr,
371 uint32_t options, 370 uint32_t options,
372 struct GNUNET_TIME_Relative timeout, 371 struct GNUNET_TIME_Relative timeout,
373 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls) 372 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
374{ 373{
375 struct PrettyPrinterContext *ppc; 374 struct PrettyPrinterContext *ppc;
376 375
377 ppc = GNUNET_new (struct PrettyPrinterContext); 376 ppc = GNUNET_new(struct PrettyPrinterContext);
378 ppc->sucess = GNUNET_NO; 377 ppc->sucess = GNUNET_NO;
379 ppc->saddr = saddr; 378 ppc->saddr = saddr;
380 ppc->asc = asc; 379 ppc->asc = asc;
381 ppc->asc_cls = asc_cls; 380 ppc->asc_cls = asc_cls;
382 ppc->plugin = GNUNET_strdup (type); 381 ppc->plugin = GNUNET_strdup(type);
383 ppc->options = options; 382 ppc->options = options;
384 ppc->resolver_handle = GNUNET_RESOLVER_ip_get (name, 383 ppc->resolver_handle = GNUNET_RESOLVER_ip_get(name,
385 AF_UNSPEC, 384 AF_UNSPEC,
386 timeout, 385 timeout,
387 &http_common_dns_ip_lookup_cb, 386 &http_common_dns_ip_lookup_cb,
388 ppc); 387 ppc);
389 if (NULL == ppc->resolver_handle) 388 if (NULL == ppc->resolver_handle)
390 { 389 {
391 GNUNET_free(ppc->plugin); 390 GNUNET_free(ppc->plugin);
392 GNUNET_free(ppc); 391 GNUNET_free(ppc);
393 return GNUNET_SYSERR; 392 return GNUNET_SYSERR;
394 } 393 }
395 GNUNET_CONTAINER_DLL_insert (dll_ppc_head, 394 GNUNET_CONTAINER_DLL_insert(dll_ppc_head,
396 dll_ppc_tail, 395 dll_ppc_tail,
397 ppc); 396 ppc);
398 return GNUNET_OK; 397 return GNUNET_OK;
399} 398}
400 399
@@ -414,13 +413,13 @@ http_common_dns_ip_lookup (const char *name,
414 * @param asc_cls closure for @a asc 413 * @param asc_cls closure for @a asc
415 */ 414 */
416void 415void
417http_common_plugin_address_pretty_printer (void *cls, const char *type, 416http_common_plugin_address_pretty_printer(void *cls, const char *type,
418 const void *addr, 417 const void *addr,
419 size_t addrlen, 418 size_t addrlen,
420 int numeric, 419 int numeric,
421 struct GNUNET_TIME_Relative timeout, 420 struct GNUNET_TIME_Relative timeout,
422 GNUNET_TRANSPORT_AddressStringCallback asc, 421 GNUNET_TRANSPORT_AddressStringCallback asc,
423 void *asc_cls) 422 void *asc_cls)
424{ 423{
425 const struct HttpAddress *address = addr; 424 const struct HttpAddress *address = addr;
426 struct SplittedHTTPAddress *saddr; 425 struct SplittedHTTPAddress *saddr;
@@ -432,121 +431,121 @@ http_common_plugin_address_pretty_printer (void *cls, const char *type,
432 431
433 saddr = NULL; 432 saddr = NULL;
434 sock_addr = NULL; 433 sock_addr = NULL;
435 if ( (addrlen < sizeof(struct HttpAddress)) || 434 if ((addrlen < sizeof(struct HttpAddress)) ||
436 (addrlen != http_common_address_get_size (address)) ) 435 (addrlen != http_common_address_get_size(address)))
437 { 436 {
438 GNUNET_break(0); 437 GNUNET_break(0);
439 goto handle_error; 438 goto handle_error;
440 } 439 }
441 440
442 addr_str = (char *) &address[1]; 441 addr_str = (char *)&address[1];
443 if (addr_str[ntohl (address->urlen) - 1] != '\0') 442 if (addr_str[ntohl(address->urlen) - 1] != '\0')
444 { 443 {
445 GNUNET_break(0); 444 GNUNET_break(0);
446 goto handle_error; 445 goto handle_error;
447 } 446 }
448 447
449 saddr = http_split_address (addr_str); 448 saddr = http_split_address(addr_str);
450 if (NULL == saddr) 449 if (NULL == saddr)
451 { 450 {
452 GNUNET_break(0); 451 GNUNET_break(0);
453 goto handle_error; 452 goto handle_error;
454 } 453 }
455 454
456 sock_addr = http_common_socket_from_address (addr, addrlen, &res); 455 sock_addr = http_common_socket_from_address(addr, addrlen, &res);
457 if (GNUNET_SYSERR == res) 456 if (GNUNET_SYSERR == res)
458 { 457 {
459 /* Malformed address */ 458 /* Malformed address */
460 GNUNET_break (0); 459 GNUNET_break(0);
461 goto handle_error; 460 goto handle_error;
462 } 461 }
463 else if (GNUNET_NO == res) 462 else if (GNUNET_NO == res)
464 { 463 {
465 /* Could not convert to IP */ 464 /* Could not convert to IP */
466 have_ip = GNUNET_NO; 465 have_ip = GNUNET_NO;
467 } 466 }
468 else if (GNUNET_YES == res) 467 else if (GNUNET_YES == res)
469 { 468 {
470 /* Converted to IP */ 469 /* Converted to IP */
471 have_ip = GNUNET_YES; 470 have_ip = GNUNET_YES;
472 } 471 }
473 else 472 else
474 {
475 /* Must not happen */
476 GNUNET_break (0);
477 goto handle_error;
478 }
479
480 if ( (GNUNET_YES == numeric) &&
481 (GNUNET_YES == have_ip) )
482 {
483 /* No lookup required */
484 ret = http_common_plugin_address_to_string (type, address, addrlen);
485 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
486 asc (asc_cls, NULL, GNUNET_OK);
487 http_clean_splitted (saddr);
488 GNUNET_free_non_null (sock_addr);
489 return;
490 }
491 if ( (GNUNET_YES == numeric) &&
492 (GNUNET_NO == have_ip) )
493 {
494 /* Forward lookup */
495 if (GNUNET_SYSERR ==
496 http_common_dns_ip_lookup (saddr->host, type, saddr,
497 address->options, timeout,
498 asc, asc_cls))
499 { 473 {
474 /* Must not happen */
500 GNUNET_break(0); 475 GNUNET_break(0);
501 goto handle_error; 476 goto handle_error;
502 } 477 }
503 /* Wait for resolver callback */ 478
504 GNUNET_free_non_null (sock_addr); 479 if ((GNUNET_YES == numeric) &&
505 return; 480 (GNUNET_YES == have_ip))
506 }
507 if ( (GNUNET_NO == numeric) &&
508 (GNUNET_YES == have_ip) )
509 {
510 /* Reverse lookup */
511 if (GNUNET_SYSERR ==
512 http_common_dns_reverse_lookup (sock_addr,
513 (AF_INET == sock_addr->sa_family)
514 ? sizeof(struct sockaddr_in)
515 : sizeof(struct sockaddr_in6),
516 type,
517 saddr,
518 address->options, timeout,
519 asc, asc_cls))
520 { 481 {
521 GNUNET_break(0); 482 /* No lookup required */
522 goto handle_error; 483 ret = http_common_plugin_address_to_string(type, address, addrlen);
484 asc(asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
485 asc(asc_cls, NULL, GNUNET_OK);
486 http_clean_splitted(saddr);
487 GNUNET_free_non_null(sock_addr);
488 return;
489 }
490 if ((GNUNET_YES == numeric) &&
491 (GNUNET_NO == have_ip))
492 {
493 /* Forward lookup */
494 if (GNUNET_SYSERR ==
495 http_common_dns_ip_lookup(saddr->host, type, saddr,
496 address->options, timeout,
497 asc, asc_cls))
498 {
499 GNUNET_break(0);
500 goto handle_error;
501 }
502 /* Wait for resolver callback */
503 GNUNET_free_non_null(sock_addr);
504 return;
505 }
506 if ((GNUNET_NO == numeric) &&
507 (GNUNET_YES == have_ip))
508 {
509 /* Reverse lookup */
510 if (GNUNET_SYSERR ==
511 http_common_dns_reverse_lookup(sock_addr,
512 (AF_INET == sock_addr->sa_family)
513 ? sizeof(struct sockaddr_in)
514 : sizeof(struct sockaddr_in6),
515 type,
516 saddr,
517 address->options, timeout,
518 asc, asc_cls))
519 {
520 GNUNET_break(0);
521 goto handle_error;
522 }
523 /* Wait for resolver callback */
524 GNUNET_free_non_null(sock_addr);
525 return;
526 }
527 if ((GNUNET_NO == numeric) &&
528 (GNUNET_NO == have_ip))
529 {
530 /* No lookup required */
531 ret = http_common_plugin_address_to_string(type, address, addrlen);
532 asc(asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
533 asc(asc_cls, NULL, GNUNET_OK);
534 GNUNET_free_non_null(sock_addr);
535 http_clean_splitted(saddr);
536 return;
523 } 537 }
524 /* Wait for resolver callback */
525 GNUNET_free_non_null (sock_addr);
526 return;
527 }
528 if ( (GNUNET_NO == numeric) &&
529 (GNUNET_NO == have_ip) )
530 {
531 /* No lookup required */
532 ret = http_common_plugin_address_to_string (type, address, addrlen);
533 asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
534 asc (asc_cls, NULL, GNUNET_OK);
535 GNUNET_free_non_null (sock_addr);
536 http_clean_splitted (saddr);
537 return;
538 }
539 /* Error (argument supplied not GNUNET_YES or GNUNET_NO) */ 538 /* Error (argument supplied not GNUNET_YES or GNUNET_NO) */
540 GNUNET_break (0); 539 GNUNET_break(0);
541 goto handle_error; 540 goto handle_error;
542 541
543 handle_error: 542handle_error:
544 /* Report error */ 543 /* Report error */
545 asc (asc_cls, NULL, GNUNET_SYSERR); 544 asc(asc_cls, NULL, GNUNET_SYSERR);
546 asc (asc_cls, NULL, GNUNET_OK); 545 asc(asc_cls, NULL, GNUNET_OK);
547 GNUNET_free_non_null (sock_addr); 546 GNUNET_free_non_null(sock_addr);
548 if (NULL != saddr) 547 if (NULL != saddr)
549 http_clean_splitted (saddr); 548 http_clean_splitted(saddr);
550} 549}
551 550
552 551
@@ -554,36 +553,36 @@ http_common_plugin_address_pretty_printer (void *cls, const char *type,
554 * FIXME. 553 * FIXME.
555 */ 554 */
556const char * 555const char *
557http_common_plugin_address_to_url (void *cls, 556http_common_plugin_address_to_url(void *cls,
558 const void *addr, 557 const void *addr,
559 size_t addrlen) 558 size_t addrlen)
560{ 559{
561 static char rbuf[1024]; 560 static char rbuf[1024];
562 const struct HttpAddress *address = addr; 561 const struct HttpAddress *address = addr;
563 const char * addr_str; 562 const char * addr_str;
564 563
565 if (NULL == addr) 564 if (NULL == addr)
566 { 565 {
567 GNUNET_break(0); 566 GNUNET_break(0);
568 return NULL; 567 return NULL;
569 } 568 }
570 if (0 == addrlen) 569 if (0 == addrlen)
571 { 570 {
572 GNUNET_break(0); 571 GNUNET_break(0);
573 return NULL; 572 return NULL;
574 } 573 }
575 if (addrlen != http_common_address_get_size (address)) 574 if (addrlen != http_common_address_get_size(address))
576 { 575 {
577 GNUNET_break(0); 576 GNUNET_break(0);
578 return NULL; 577 return NULL;
579 } 578 }
580 addr_str = (char *) &address[1]; 579 addr_str = (char *)&address[1];
581 if (addr_str[ntohl (address->urlen) - 1] != '\0') 580 if (addr_str[ntohl(address->urlen) - 1] != '\0')
582 return NULL; 581 return NULL;
583 582
584 GNUNET_memcpy (rbuf, 583 GNUNET_memcpy(rbuf,
585 &address[1], 584 &address[1],
586 ntohl (address->urlen)); 585 ntohl(address->urlen));
587 return rbuf; 586 return rbuf;
588} 587}
589 588
@@ -600,9 +599,9 @@ http_common_plugin_address_to_url (void *cls,
600 * @return string representing the same address 599 * @return string representing the same address
601 */ 600 */
602const char * 601const char *
603http_common_plugin_address_to_string (const char *plugin, 602http_common_plugin_address_to_string(const char *plugin,
604 const void *addr, 603 const void *addr,
605 size_t addrlen) 604 size_t addrlen)
606{ 605{
607 static char rbuf[1024]; 606 static char rbuf[1024];
608 const struct HttpAddress *address = addr; 607 const struct HttpAddress *address = addr;
@@ -614,19 +613,19 @@ http_common_plugin_address_to_string (const char *plugin,
614 return NULL; 613 return NULL;
615 if (0 == addrlen) 614 if (0 == addrlen)
616 return NULL; 615 return NULL;
617 if (addrlen != http_common_address_get_size (address)) 616 if (addrlen != http_common_address_get_size(address))
618 return NULL; 617 return NULL;
619 addr_str = (char *) &address[1]; 618 addr_str = (char *)&address[1];
620 if (addr_str[ntohl (address->urlen) - 1] != '\0') 619 if (addr_str[ntohl(address->urlen) - 1] != '\0')
621 return NULL; 620 return NULL;
622 GNUNET_asprintf (&res, "%s.%u.%s", plugin, ntohl (address->options), 621 GNUNET_asprintf(&res, "%s.%u.%s", plugin, ntohl(address->options),
623 &address[1]); 622 &address[1]);
624 if (strlen (res) + 1 < 500) 623 if (strlen(res) + 1 < 500)
625 { 624 {
626 GNUNET_memcpy (rbuf, res, strlen (res) + 1); 625 GNUNET_memcpy(rbuf, res, strlen(res) + 1);
627 GNUNET_free(res); 626 GNUNET_free(res);
628 return rbuf; 627 return rbuf;
629 } 628 }
630 GNUNET_break(0); 629 GNUNET_break(0);
631 GNUNET_free(res); 630 GNUNET_free(res);
632 return NULL; 631 return NULL;
@@ -645,11 +644,11 @@ http_common_plugin_address_to_string (const char *plugin,
645 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure 644 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
646 */ 645 */
647int 646int
648http_common_plugin_string_to_address (void *cls, 647http_common_plugin_string_to_address(void *cls,
649 const char *addr, 648 const char *addr,
650 uint16_t addrlen, 649 uint16_t addrlen,
651 void **buf, 650 void **buf,
652 size_t *added) 651 size_t *added)
653{ 652{
654 struct HttpAddress *a; 653 struct HttpAddress *a;
655 char *address; 654 char *address;
@@ -663,46 +662,46 @@ http_common_plugin_string_to_address (void *cls,
663 plugin = NULL; 662 plugin = NULL;
664 optionstr = NULL; 663 optionstr = NULL;
665 if ((NULL == addr) || (addrlen == 0)) 664 if ((NULL == addr) || (addrlen == 0))
666 { 665 {
667 GNUNET_break(0); 666 GNUNET_break(0);
668 return GNUNET_SYSERR; 667 return GNUNET_SYSERR;
669 } 668 }
670 if ('\0' != addr[addrlen - 1]) 669 if ('\0' != addr[addrlen - 1])
671 { 670 {
672 GNUNET_break(0); 671 GNUNET_break(0);
673 return GNUNET_SYSERR; 672 return GNUNET_SYSERR;
674 } 673 }
675 if (strlen (addr) != addrlen - 1) 674 if (strlen(addr) != addrlen - 1)
676 { 675 {
677 GNUNET_break(0); 676 GNUNET_break(0);
678 return GNUNET_SYSERR; 677 return GNUNET_SYSERR;
679 } 678 }
680 plugin = GNUNET_strdup (addr); 679 plugin = GNUNET_strdup(addr);
681 optionstr = strchr (plugin, '.'); 680 optionstr = strchr(plugin, '.');
682 if (NULL == optionstr) 681 if (NULL == optionstr)
683 { 682 {
684 GNUNET_break(0); 683 GNUNET_break(0);
685 GNUNET_free(plugin); 684 GNUNET_free(plugin);
686 return GNUNET_SYSERR; 685 return GNUNET_SYSERR;
687 } 686 }
688 optionstr[0] = '\0'; 687 optionstr[0] = '\0';
689 optionstr++; 688 optionstr++;
690 options = atol (optionstr); /* 0 on conversion error, that's ok */ 689 options = atol(optionstr); /* 0 on conversion error, that's ok */
691 address = strchr (optionstr, '.'); 690 address = strchr(optionstr, '.');
692 if (NULL == address) 691 if (NULL == address)
693 { 692 {
694 GNUNET_break(0); 693 GNUNET_break(0);
695 GNUNET_free(plugin); 694 GNUNET_free(plugin);
696 return GNUNET_SYSERR; 695 return GNUNET_SYSERR;
697 } 696 }
698 address[0] = '\0'; 697 address[0] = '\0';
699 address++; 698 address++;
700 urlen = strlen (address) + 1; 699 urlen = strlen(address) + 1;
701 700
702 a = GNUNET_malloc (sizeof (struct HttpAddress) + urlen); 701 a = GNUNET_malloc(sizeof(struct HttpAddress) + urlen);
703 a->options = htonl (options); 702 a->options = htonl(options);
704 a->urlen = htonl (urlen); 703 a->urlen = htonl(urlen);
705 GNUNET_memcpy (&a[1], address, urlen); 704 GNUNET_memcpy(&a[1], address, urlen);
706 705
707 (*buf) = a; 706 (*buf) = a;
708 (*added) = sizeof(struct HttpAddress) + urlen; 707 (*added) = sizeof(struct HttpAddress) + urlen;
@@ -720,24 +719,24 @@ http_common_plugin_string_to_address (void *cls,
720 * @return the HttpAddress 719 * @return the HttpAddress
721 */ 720 */
722struct HttpAddress * 721struct HttpAddress *
723http_common_address_from_socket (const char *protocol, 722http_common_address_from_socket(const char *protocol,
724 const struct sockaddr *addr, 723 const struct sockaddr *addr,
725 socklen_t addrlen) 724 socklen_t addrlen)
726{ 725{
727 struct HttpAddress *address = NULL; 726 struct HttpAddress *address = NULL;
728 char *res; 727 char *res;
729 size_t len; 728 size_t len;
730 729
731 GNUNET_asprintf (&res, 730 GNUNET_asprintf(&res,
732 "%s://%s", 731 "%s://%s",
733 protocol, 732 protocol,
734 GNUNET_a2s (addr, 733 GNUNET_a2s(addr,
735 addrlen)); 734 addrlen));
736 len = strlen (res) + 1; 735 len = strlen(res) + 1;
737 address = GNUNET_malloc (sizeof (struct HttpAddress) + len); 736 address = GNUNET_malloc(sizeof(struct HttpAddress) + len);
738 address->options = htonl (HTTP_OPTIONS_NONE); 737 address->options = htonl(HTTP_OPTIONS_NONE);
739 address->urlen = htonl (len); 738 address->urlen = htonl(len);
740 GNUNET_memcpy (&address[1], res, len); 739 GNUNET_memcpy(&address[1], res, len);
741 GNUNET_free(res); 740 GNUNET_free(res);
742 return address; 741 return address;
743} 742}
@@ -755,9 +754,9 @@ http_common_address_from_socket (const char *protocol,
755 * @return the string 754 * @return the string
756 */ 755 */
757struct sockaddr * 756struct sockaddr *
758http_common_socket_from_address (const void *addr, 757http_common_socket_from_address(const void *addr,
759 size_t addrlen, 758 size_t addrlen,
760 int *res) 759 int *res)
761{ 760{
762 const struct HttpAddress *ha; 761 const struct HttpAddress *ha;
763 struct SplittedHTTPAddress * spa; 762 struct SplittedHTTPAddress * spa;
@@ -766,68 +765,68 @@ http_common_socket_from_address (const void *addr,
766 size_t urlen; 765 size_t urlen;
767 766
768 (*res) = GNUNET_SYSERR; 767 (*res) = GNUNET_SYSERR;
769 ha = (const struct HttpAddress *) addr; 768 ha = (const struct HttpAddress *)addr;
770 if (NULL == addr) 769 if (NULL == addr)
771 { 770 {
772 GNUNET_break (0); 771 GNUNET_break(0);
773 return NULL; 772 return NULL;
774 } 773 }
775 if (0 == addrlen) 774 if (0 == addrlen)
776 { 775 {
777 GNUNET_break (0); 776 GNUNET_break(0);
778 return NULL; 777 return NULL;
779 } 778 }
780 if (addrlen < sizeof(struct HttpAddress)) 779 if (addrlen < sizeof(struct HttpAddress))
781 { 780 {
782 GNUNET_break (0); 781 GNUNET_break(0);
783 return NULL; 782 return NULL;
784 } 783 }
785 urlen = ntohl (ha->urlen); 784 urlen = ntohl(ha->urlen);
786 if (sizeof(struct HttpAddress) + urlen != addrlen) 785 if (sizeof(struct HttpAddress) + urlen != addrlen)
787 { 786 {
788 /* This is a legacy addresses */ 787 /* This is a legacy addresses */
789 return NULL; 788 return NULL;
790 } 789 }
791 if (addrlen < sizeof(struct HttpAddress) + urlen) 790 if (addrlen < sizeof(struct HttpAddress) + urlen)
792 { 791 {
793 /* This is a legacy addresses */ 792 /* This is a legacy addresses */
794 return NULL; 793 return NULL;
795 } 794 }
796 if (((char *) addr)[addrlen - 1] != '\0') 795 if (((char *)addr)[addrlen - 1] != '\0')
797 { 796 {
798 GNUNET_break (0); 797 GNUNET_break(0);
799 return NULL; 798 return NULL;
800 } 799 }
801 spa = http_split_address ((const char *) &ha[1]); 800 spa = http_split_address((const char *)&ha[1]);
802 if (NULL == spa) 801 if (NULL == spa)
803 { 802 {
804 (*res) = GNUNET_SYSERR; 803 (*res) = GNUNET_SYSERR;
805 return NULL; 804 return NULL;
806 } 805 }
807 806
808 s = GNUNET_new (struct sockaddr_storage); 807 s = GNUNET_new(struct sockaddr_storage);
809 GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port); 808 GNUNET_asprintf(&to_conv, "%s:%u", spa->host, spa->port);
810 if (GNUNET_SYSERR 809 if (GNUNET_SYSERR
811 == GNUNET_STRINGS_to_address_ip (to_conv, strlen (to_conv), s)) 810 == GNUNET_STRINGS_to_address_ip(to_conv, strlen(to_conv), s))
812 { 811 {
813 /* could be a hostname */ 812 /* could be a hostname */
814 GNUNET_free(s); 813 GNUNET_free(s);
815 (*res) = GNUNET_NO; 814 (*res) = GNUNET_NO;
816 s = NULL; 815 s = NULL;
817 } 816 }
818 else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family)) 817 else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
819 { 818 {
820 GNUNET_free (s); 819 GNUNET_free(s);
821 (*res) = GNUNET_SYSERR; 820 (*res) = GNUNET_SYSERR;
822 s = NULL; 821 s = NULL;
823 } 822 }
824 else 823 else
825 { 824 {
826 (*res) = GNUNET_YES; 825 (*res) = GNUNET_YES;
827 } 826 }
828 http_clean_splitted (spa); 827 http_clean_splitted(spa);
829 GNUNET_free (to_conv); 828 GNUNET_free(to_conv);
830 return (struct sockaddr *) s; 829 return (struct sockaddr *)s;
831} 830}
832 831
833 832
@@ -838,9 +837,9 @@ http_common_socket_from_address (const void *addr,
838 * @return the size 837 * @return the size
839 */ 838 */
840size_t 839size_t
841http_common_address_get_size (const struct HttpAddress * addr) 840http_common_address_get_size(const struct HttpAddress * addr)
842{ 841{
843 return sizeof(struct HttpAddress) + ntohl (addr->urlen); 842 return sizeof(struct HttpAddress) + ntohl(addr->urlen);
844} 843}
845 844
846 845
@@ -854,17 +853,18 @@ http_common_address_get_size (const struct HttpAddress * addr)
854 * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error 853 * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error
855 */ 854 */
856size_t 855size_t
857http_common_cmp_addresses (const void *addr1, 856http_common_cmp_addresses(const void *addr1,
858 size_t addrlen1, 857 size_t addrlen1,
859 const void *addr2, 858 const void *addr2,
860 size_t addrlen2) 859 size_t addrlen2)
861{ 860{
862 const char *a1 = addr1; 861 const char *a1 = addr1;
863 const char *a2 = addr2; 862 const char *a2 = addr2;
864 const struct HttpAddress *ha1; 863 const struct HttpAddress *ha1;
865 const struct HttpAddress *ha2; 864 const struct HttpAddress *ha2;
866 ha1 = (const struct HttpAddress *) a1; 865
867 ha2 = (const struct HttpAddress *) a2; 866 ha1 = (const struct HttpAddress *)a1;
867 ha2 = (const struct HttpAddress *)a2;
868 868
869 if (NULL == a1) 869 if (NULL == a1)
870 return GNUNET_SYSERR; 870 return GNUNET_SYSERR;
@@ -885,7 +885,7 @@ http_common_cmp_addresses (const void *addr1,
885 if (ha1->urlen != ha2->urlen) 885 if (ha1->urlen != ha2->urlen)
886 return GNUNET_NO; 886 return GNUNET_NO;
887 887
888 if (0 == strcmp ((const char *) &ha1[1], (const char *) &ha2[1])) 888 if (0 == strcmp((const char *)&ha1[1], (const char *)&ha2[1]))
889 return GNUNET_YES; 889 return GNUNET_YES;
890 return GNUNET_NO; 890 return GNUNET_NO;
891} 891}
@@ -899,37 +899,36 @@ http_common_cmp_addresses (const void *addr1,
899 * @return the network type 899 * @return the network type
900 */ 900 */
901enum GNUNET_NetworkType 901enum GNUNET_NetworkType
902http_common_get_network_for_address (struct GNUNET_TRANSPORT_PluginEnvironment *env, 902http_common_get_network_for_address(struct GNUNET_TRANSPORT_PluginEnvironment *env,
903 const struct GNUNET_HELLO_Address *address) 903 const struct GNUNET_HELLO_Address *address)
904{ 904{
905
906 struct sockaddr *sa; 905 struct sockaddr *sa;
907 enum GNUNET_NetworkType net_type; 906 enum GNUNET_NetworkType net_type;
908 size_t salen = 0; 907 size_t salen = 0;
909 int res; 908 int res;
910 909
911 net_type = GNUNET_NT_UNSPECIFIED; 910 net_type = GNUNET_NT_UNSPECIFIED;
912 sa = http_common_socket_from_address (address->address, 911 sa = http_common_socket_from_address(address->address,
913 address->address_length, 912 address->address_length,
914 &res); 913 &res);
915 if (GNUNET_SYSERR == res) 914 if (GNUNET_SYSERR == res)
916 return net_type; 915 return net_type;
917 if (GNUNET_YES == res) 916 if (GNUNET_YES == res)
918 {
919 GNUNET_assert (NULL != sa);
920 if (AF_INET == sa->sa_family)
921 {
922 salen = sizeof (struct sockaddr_in);
923 }
924 else if (AF_INET6 == sa->sa_family)
925 { 917 {
926 salen = sizeof (struct sockaddr_in6); 918 GNUNET_assert(NULL != sa);
919 if (AF_INET == sa->sa_family)
920 {
921 salen = sizeof(struct sockaddr_in);
922 }
923 else if (AF_INET6 == sa->sa_family)
924 {
925 salen = sizeof(struct sockaddr_in6);
926 }
927 net_type = env->get_address_type(env->cls,
928 sa,
929 salen);
930 GNUNET_free(sa);
927 } 931 }
928 net_type = env->get_address_type (env->cls,
929 sa,
930 salen);
931 GNUNET_free (sa);
932 }
933 return net_type; 932 return net_type;
934} 933}
935 934