diff options
Diffstat (limited to 'src/transport/plugin_transport_http_common.c')
-rw-r--r-- | src/transport/plugin_transport_http_common.c | 948 |
1 files changed, 0 insertions, 948 deletions
diff --git a/src/transport/plugin_transport_http_common.c b/src/transport/plugin_transport_http_common.c deleted file mode 100644 index 75bf8b74a..000000000 --- a/src/transport/plugin_transport_http_common.c +++ /dev/null | |||
@@ -1,948 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2002-2013 GNUnet e.V. | ||
4 | |||
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 | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
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 | Affero General Public License for more details. | ||
14 | |||
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/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file transport/plugin_transport_http_common.c | ||
23 | * @brief functionality shared between http(s)client plugins | ||
24 | * @author Matthias Wachs | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_transport_plugin.h" | ||
29 | #include "plugin_transport_http_common.h" | ||
30 | #include "gnunet_resolver_service.h" | ||
31 | |||
32 | static void | ||
33 | http_clean_splitted (struct SplittedHTTPAddress *spa) | ||
34 | { | ||
35 | if (NULL != spa) | ||
36 | { | ||
37 | GNUNET_free (spa->protocol); | ||
38 | GNUNET_free (spa->host); | ||
39 | GNUNET_free (spa->path); | ||
40 | GNUNET_free (spa); | ||
41 | } | ||
42 | } | ||
43 | |||
44 | |||
45 | struct SplittedHTTPAddress * | ||
46 | http_split_address (const char *addr) | ||
47 | { | ||
48 | struct SplittedHTTPAddress *sp; | ||
49 | char *src = GNUNET_strdup (addr); | ||
50 | char *protocol_start = NULL; | ||
51 | char *host_start = NULL; | ||
52 | char *v6_end = NULL; | ||
53 | char *port_start = NULL; | ||
54 | char *path_start = NULL; | ||
55 | |||
56 | protocol_start = src; | ||
57 | |||
58 | sp = GNUNET_new (struct SplittedHTTPAddress); | ||
59 | /* Address string consists of protocol://host[:port]path*/ | ||
60 | |||
61 | host_start = strstr (src, "://"); | ||
62 | if (NULL == host_start) | ||
63 | { | ||
64 | GNUNET_free (src); | ||
65 | GNUNET_free (sp); | ||
66 | return NULL; | ||
67 | } | ||
68 | host_start[0] = '\0'; | ||
69 | sp->protocol = GNUNET_strdup (protocol_start); | ||
70 | |||
71 | host_start += strlen ("://"); | ||
72 | if (strlen (host_start) == 0) | ||
73 | { | ||
74 | GNUNET_free (src); | ||
75 | GNUNET_free (sp->protocol); | ||
76 | GNUNET_free (sp); | ||
77 | return NULL; | ||
78 | } | ||
79 | |||
80 | /* Find path start */ | ||
81 | path_start = strchr (host_start, '/'); | ||
82 | if (NULL != path_start) | ||
83 | { | ||
84 | sp->path = GNUNET_strdup (path_start); | ||
85 | path_start[0] = '\0'; | ||
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 | } | ||
144 | else | ||
145 | { | ||
146 | /* No ':' as port separator, default port for protocol */ | ||
147 | if (0 == strcmp (sp->protocol, "https")) | ||
148 | sp->port = HTTPS_DEFAULT_PORT; | ||
149 | else if (0 == strcmp (sp->protocol, "http")) | ||
150 | sp->port = HTTP_DEFAULT_PORT; | ||
151 | else | ||
152 | { | ||
153 | GNUNET_break (0); | ||
154 | GNUNET_free (src); | ||
155 | GNUNET_free (sp->protocol); | ||
156 | GNUNET_free (sp->path); | ||
157 | GNUNET_free (sp); | ||
158 | return NULL; | ||
159 | } | ||
160 | } | ||
161 | if (strlen (host_start) > 0) | ||
162 | sp->host = GNUNET_strdup (host_start); | ||
163 | else | ||
164 | { | ||
165 | GNUNET_break (0); | ||
166 | GNUNET_free (src); | ||
167 | GNUNET_free (sp->protocol); | ||
168 | GNUNET_free (sp->path); | ||
169 | GNUNET_free (sp); | ||
170 | return NULL; | ||
171 | } | ||
172 | GNUNET_free (src); | ||
173 | return sp; | ||
174 | } | ||
175 | |||
176 | |||
177 | /** | ||
178 | * Closure for #append_port(). | ||
179 | */ | ||
180 | struct PrettyPrinterContext | ||
181 | { | ||
182 | /** | ||
183 | * DLL | ||
184 | */ | ||
185 | struct PrettyPrinterContext *next; | ||
186 | |||
187 | /** | ||
188 | * DLL | ||
189 | */ | ||
190 | struct PrettyPrinterContext *prev; | ||
191 | |||
192 | /** | ||
193 | * Resolver handle | ||
194 | */ | ||
195 | struct GNUNET_RESOLVER_RequestHandle *resolver_handle; | ||
196 | |||
197 | /** | ||
198 | * Function to call with the result. | ||
199 | */ | ||
200 | GNUNET_TRANSPORT_AddressStringCallback asc; | ||
201 | |||
202 | /** | ||
203 | * Clsoure for @e asc. | ||
204 | */ | ||
205 | void *asc_cls; | ||
206 | |||
207 | /** | ||
208 | * Timeout task | ||
209 | */ | ||
210 | struct GNUNET_SCHEDULER_Task *timeout_task; | ||
211 | |||
212 | /** | ||
213 | * Split Address | ||
214 | */ | ||
215 | struct SplittedHTTPAddress *saddr; | ||
216 | |||
217 | /** | ||
218 | * Plugin String | ||
219 | */ | ||
220 | char *plugin; | ||
221 | |||
222 | /** | ||
223 | * Was conversion successful | ||
224 | */ | ||
225 | int success; | ||
226 | |||
227 | /** | ||
228 | * Address options | ||
229 | */ | ||
230 | uint32_t options; | ||
231 | }; | ||
232 | |||
233 | /** | ||
234 | * Head of PPC list | ||
235 | */ | ||
236 | static struct PrettyPrinterContext *dll_ppc_head; | ||
237 | |||
238 | /** | ||
239 | * Tail of PPC list | ||
240 | */ | ||
241 | static struct PrettyPrinterContext *dll_ppc_tail; | ||
242 | |||
243 | /** | ||
244 | * Function called for a quick conversion of the binary address to | ||
245 | * a numeric address. Note that the caller must not free the | ||
246 | * address and that the next call to this function is allowed | ||
247 | * to override the address again. | ||
248 | * | ||
249 | * @param plugin the name of the plugin | ||
250 | * @param saddr the split http address | ||
251 | * @param options address options | ||
252 | * @param dnsresult dns name to include in address | ||
253 | * @return string representing the same address or NULL on error | ||
254 | */ | ||
255 | static const char * | ||
256 | http_common_plugin_dnsresult_to_address (const char *plugin, | ||
257 | const struct | ||
258 | SplittedHTTPAddress *saddr, | ||
259 | uint32_t options, | ||
260 | const char *dnsresult) | ||
261 | { | ||
262 | static char rbuf[1024]; | ||
263 | char *res; | ||
264 | |||
265 | GNUNET_asprintf (&res, "%s.%u.%s://%s:%u%s", plugin, options, saddr->protocol, | ||
266 | dnsresult, saddr->port, saddr->path); | ||
267 | if (strlen (res) + 1 < 500) | ||
268 | { | ||
269 | GNUNET_memcpy (rbuf, res, strlen (res) + 1); | ||
270 | GNUNET_free (res); | ||
271 | return rbuf; | ||
272 | } | ||
273 | GNUNET_break (0); | ||
274 | GNUNET_free (res); | ||
275 | return NULL; | ||
276 | } | ||
277 | |||
278 | |||
279 | static void | ||
280 | http_common_dns_reverse_lookup_cb (void *cls, const char *hostname) | ||
281 | { | ||
282 | struct PrettyPrinterContext *ppc = cls; | ||
283 | |||
284 | if (NULL != hostname) | ||
285 | { | ||
286 | ppc->asc (ppc->asc_cls, | ||
287 | http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, | ||
288 | ppc->options, | ||
289 | hostname), GNUNET_OK); | ||
290 | ppc->success = GNUNET_YES; | ||
291 | } | ||
292 | else | ||
293 | { | ||
294 | ppc->asc (ppc->asc_cls, NULL, | ||
295 | (GNUNET_NO == ppc->success) ? GNUNET_SYSERR : GNUNET_OK); | ||
296 | |||
297 | GNUNET_CONTAINER_DLL_remove (dll_ppc_head, dll_ppc_tail, ppc); | ||
298 | http_clean_splitted (ppc->saddr); | ||
299 | GNUNET_free (ppc->plugin); | ||
300 | GNUNET_free (ppc); | ||
301 | } | ||
302 | } | ||
303 | |||
304 | |||
305 | static int | ||
306 | http_common_dns_reverse_lookup (const struct sockaddr *sockaddr, | ||
307 | socklen_t sockaddr_len, | ||
308 | const char *type, | ||
309 | struct SplittedHTTPAddress *saddr, | ||
310 | uint32_t options, | ||
311 | struct GNUNET_TIME_Relative timeout, | ||
312 | GNUNET_TRANSPORT_AddressStringCallback asc, | ||
313 | void *asc_cls) | ||
314 | { | ||
315 | struct PrettyPrinterContext *ppc; | ||
316 | |||
317 | ppc = GNUNET_new (struct PrettyPrinterContext); | ||
318 | ppc->saddr = saddr; | ||
319 | ppc->asc = asc; | ||
320 | ppc->asc_cls = asc_cls; | ||
321 | ppc->plugin = GNUNET_strdup (type); | ||
322 | ppc->options = options; | ||
323 | ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sockaddr, | ||
324 | sockaddr_len, | ||
325 | GNUNET_YES, | ||
326 | timeout, | ||
327 | & | ||
328 | http_common_dns_reverse_lookup_cb, | ||
329 | ppc); | ||
330 | if (NULL == ppc->resolver_handle) | ||
331 | { | ||
332 | GNUNET_free (ppc->plugin); | ||
333 | GNUNET_free (ppc); | ||
334 | return GNUNET_SYSERR; | ||
335 | } | ||
336 | GNUNET_CONTAINER_DLL_insert (dll_ppc_head, | ||
337 | dll_ppc_tail, | ||
338 | ppc); | ||
339 | return GNUNET_OK; | ||
340 | } | ||
341 | |||
342 | |||
343 | static void | ||
344 | http_common_dns_ip_lookup_cb (void *cls, | ||
345 | const struct sockaddr *addr, | ||
346 | socklen_t addrlen) | ||
347 | { | ||
348 | struct PrettyPrinterContext *ppc = cls; | ||
349 | |||
350 | if (NULL != addr) | ||
351 | { | ||
352 | ppc->asc (ppc->asc_cls, | ||
353 | http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, | ||
354 | ppc->options, | ||
355 | GNUNET_a2s (addr, | ||
356 | addrlen)), | ||
357 | GNUNET_OK); | ||
358 | ppc->success = GNUNET_YES; | ||
359 | ppc->asc (ppc->asc_cls, GNUNET_a2s (addr, addrlen), GNUNET_OK); | ||
360 | } | ||
361 | else | ||
362 | { | ||
363 | ppc->asc (ppc->asc_cls, NULL, | ||
364 | (GNUNET_NO == ppc->success) ? GNUNET_SYSERR : GNUNET_OK); | ||
365 | |||
366 | GNUNET_CONTAINER_DLL_remove (dll_ppc_head, dll_ppc_tail, ppc); | ||
367 | GNUNET_free (ppc->plugin); | ||
368 | http_clean_splitted (ppc->saddr); | ||
369 | GNUNET_free (ppc); | ||
370 | } | ||
371 | } | ||
372 | |||
373 | |||
374 | static int | ||
375 | http_common_dns_ip_lookup (const char *name, | ||
376 | const char *type, | ||
377 | struct SplittedHTTPAddress *saddr, | ||
378 | uint32_t options, | ||
379 | struct GNUNET_TIME_Relative timeout, | ||
380 | GNUNET_TRANSPORT_AddressStringCallback asc, | ||
381 | void *asc_cls) | ||
382 | { | ||
383 | struct PrettyPrinterContext *ppc; | ||
384 | |||
385 | ppc = GNUNET_new (struct PrettyPrinterContext); | ||
386 | ppc->success = GNUNET_NO; | ||
387 | ppc->saddr = saddr; | ||
388 | ppc->asc = asc; | ||
389 | ppc->asc_cls = asc_cls; | ||
390 | ppc->plugin = GNUNET_strdup (type); | ||
391 | ppc->options = options; | ||
392 | ppc->resolver_handle = GNUNET_RESOLVER_ip_get (name, | ||
393 | AF_UNSPEC, | ||
394 | timeout, | ||
395 | &http_common_dns_ip_lookup_cb, | ||
396 | ppc); | ||
397 | if (NULL == ppc->resolver_handle) | ||
398 | { | ||
399 | GNUNET_free (ppc->plugin); | ||
400 | GNUNET_free (ppc); | ||
401 | return GNUNET_SYSERR; | ||
402 | } | ||
403 | GNUNET_CONTAINER_DLL_insert (dll_ppc_head, | ||
404 | dll_ppc_tail, | ||
405 | ppc); | ||
406 | return GNUNET_OK; | ||
407 | } | ||
408 | |||
409 | |||
410 | /** | ||
411 | * Convert the transports address to a nice, human-readable | ||
412 | * format. | ||
413 | * | ||
414 | * @param cls closure | ||
415 | * @param type name of the transport that generated the address | ||
416 | * @param addr one of the addresses of the host, NULL for the last address | ||
417 | * the specific address format depends on the transport | ||
418 | * @param addrlen length of the @a addr | ||
419 | * @param numeric should (IP) addresses be displayed in numeric form? | ||
420 | * @param timeout after how long should we give up? | ||
421 | * @param asc function to call on each string | ||
422 | * @param asc_cls closure for @a asc | ||
423 | */ | ||
424 | void | ||
425 | http_common_plugin_address_pretty_printer (void *cls, const char *type, | ||
426 | const void *addr, | ||
427 | size_t addrlen, | ||
428 | int numeric, | ||
429 | struct GNUNET_TIME_Relative timeout, | ||
430 | GNUNET_TRANSPORT_AddressStringCallback | ||
431 | asc, | ||
432 | void *asc_cls) | ||
433 | { | ||
434 | const struct HttpAddress *address = addr; | ||
435 | struct SplittedHTTPAddress *saddr; | ||
436 | struct sockaddr *sock_addr; | ||
437 | const char *ret; | ||
438 | char *addr_str; | ||
439 | int res; | ||
440 | int have_ip; | ||
441 | |||
442 | saddr = NULL; | ||
443 | sock_addr = NULL; | ||
444 | if ((addrlen < sizeof(struct HttpAddress)) || | ||
445 | (addrlen != http_common_address_get_size (address))) | ||
446 | { | ||
447 | GNUNET_break (0); | ||
448 | goto handle_error; | ||
449 | } | ||
450 | |||
451 | addr_str = (char *) &address[1]; | ||
452 | if (addr_str[ntohl (address->urlen) - 1] != '\0') | ||
453 | { | ||
454 | GNUNET_break (0); | ||
455 | goto handle_error; | ||
456 | } | ||
457 | |||
458 | saddr = http_split_address (addr_str); | ||
459 | if (NULL == saddr) | ||
460 | { | ||
461 | GNUNET_break (0); | ||
462 | goto handle_error; | ||
463 | } | ||
464 | |||
465 | sock_addr = http_common_socket_from_address (addr, addrlen, &res); | ||
466 | if (GNUNET_SYSERR == res) | ||
467 | { | ||
468 | /* Malformed address */ | ||
469 | GNUNET_break (0); | ||
470 | goto handle_error; | ||
471 | } | ||
472 | else if (GNUNET_NO == res) | ||
473 | { | ||
474 | /* Could not convert to IP */ | ||
475 | have_ip = GNUNET_NO; | ||
476 | } | ||
477 | else if (GNUNET_YES == res) | ||
478 | { | ||
479 | /* Converted to IP */ | ||
480 | have_ip = GNUNET_YES; | ||
481 | } | ||
482 | else | ||
483 | { | ||
484 | /* Must not happen */ | ||
485 | GNUNET_break (0); | ||
486 | goto handle_error; | ||
487 | } | ||
488 | |||
489 | if ((GNUNET_YES == numeric) && | ||
490 | (GNUNET_YES == have_ip)) | ||
491 | { | ||
492 | /* No lookup required */ | ||
493 | ret = http_common_plugin_address_to_string (type, address, addrlen); | ||
494 | asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK); | ||
495 | asc (asc_cls, NULL, GNUNET_OK); | ||
496 | http_clean_splitted (saddr); | ||
497 | GNUNET_free (sock_addr); | ||
498 | return; | ||
499 | } | ||
500 | if ((GNUNET_YES == numeric) && | ||
501 | (GNUNET_NO == have_ip)) | ||
502 | { | ||
503 | /* Forward lookup */ | ||
504 | if (GNUNET_SYSERR == | ||
505 | http_common_dns_ip_lookup (saddr->host, type, saddr, | ||
506 | address->options, timeout, | ||
507 | asc, asc_cls)) | ||
508 | { | ||
509 | GNUNET_break (0); | ||
510 | goto handle_error; | ||
511 | } | ||
512 | /* Wait for resolver callback */ | ||
513 | GNUNET_free (sock_addr); | ||
514 | return; | ||
515 | } | ||
516 | if ((GNUNET_NO == numeric) && | ||
517 | (GNUNET_YES == have_ip)) | ||
518 | { | ||
519 | /* Reverse lookup */ | ||
520 | if (GNUNET_SYSERR == | ||
521 | http_common_dns_reverse_lookup (sock_addr, | ||
522 | (AF_INET == sock_addr->sa_family) | ||
523 | ? sizeof(struct sockaddr_in) | ||
524 | : sizeof(struct sockaddr_in6), | ||
525 | type, | ||
526 | saddr, | ||
527 | address->options, timeout, | ||
528 | asc, asc_cls)) | ||
529 | { | ||
530 | GNUNET_break (0); | ||
531 | goto handle_error; | ||
532 | } | ||
533 | /* Wait for resolver callback */ | ||
534 | GNUNET_free (sock_addr); | ||
535 | return; | ||
536 | } | ||
537 | if ((GNUNET_NO == numeric) && | ||
538 | (GNUNET_NO == have_ip)) | ||
539 | { | ||
540 | /* No lookup required */ | ||
541 | ret = http_common_plugin_address_to_string (type, address, addrlen); | ||
542 | asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK); | ||
543 | asc (asc_cls, NULL, GNUNET_OK); | ||
544 | GNUNET_free (sock_addr); | ||
545 | http_clean_splitted (saddr); | ||
546 | return; | ||
547 | } | ||
548 | /* Error (argument supplied not GNUNET_YES or GNUNET_NO) */ | ||
549 | GNUNET_break (0); | ||
550 | goto handle_error; | ||
551 | |||
552 | handle_error: | ||
553 | /* Report error */ | ||
554 | asc (asc_cls, NULL, GNUNET_SYSERR); | ||
555 | asc (asc_cls, NULL, GNUNET_OK); | ||
556 | GNUNET_free (sock_addr); | ||
557 | if (NULL != saddr) | ||
558 | http_clean_splitted (saddr); | ||
559 | } | ||
560 | |||
561 | |||
562 | /** | ||
563 | * FIXME. | ||
564 | */ | ||
565 | const char * | ||
566 | http_common_plugin_address_to_url (void *cls, | ||
567 | const void *addr, | ||
568 | size_t addrlen) | ||
569 | { | ||
570 | static char rbuf[1024]; | ||
571 | const struct HttpAddress *address = addr; | ||
572 | const char *addr_str; | ||
573 | |||
574 | if (NULL == addr) | ||
575 | { | ||
576 | GNUNET_break (0); | ||
577 | return NULL; | ||
578 | } | ||
579 | if (0 == addrlen) | ||
580 | { | ||
581 | GNUNET_break (0); | ||
582 | return NULL; | ||
583 | } | ||
584 | if (addrlen != http_common_address_get_size (address)) | ||
585 | { | ||
586 | GNUNET_break (0); | ||
587 | return NULL; | ||
588 | } | ||
589 | addr_str = (char *) &address[1]; | ||
590 | if (addr_str[ntohl (address->urlen) - 1] != '\0') | ||
591 | return NULL; | ||
592 | |||
593 | GNUNET_memcpy (rbuf, | ||
594 | &address[1], | ||
595 | ntohl (address->urlen)); | ||
596 | return rbuf; | ||
597 | } | ||
598 | |||
599 | |||
600 | /** | ||
601 | * Function called for a quick conversion of the binary address to | ||
602 | * a numeric address. Note that the caller must not free the | ||
603 | * address and that the next call to this function is allowed | ||
604 | * to override the address again. | ||
605 | * | ||
606 | * @param plugin the name of the plugin | ||
607 | * @param addr binary address | ||
608 | * @param addrlen length of the address | ||
609 | * @return string representing the same address | ||
610 | */ | ||
611 | const char * | ||
612 | http_common_plugin_address_to_string (const char *plugin, | ||
613 | const void *addr, | ||
614 | size_t addrlen) | ||
615 | { | ||
616 | static char rbuf[1024]; | ||
617 | const struct HttpAddress *address = addr; | ||
618 | const char *addr_str; | ||
619 | char *res; | ||
620 | |||
621 | GNUNET_assert (NULL != plugin); | ||
622 | if (NULL == addr) | ||
623 | return NULL; | ||
624 | if (0 == addrlen) | ||
625 | return NULL; | ||
626 | if (addrlen != http_common_address_get_size (address)) | ||
627 | return NULL; | ||
628 | addr_str = (char *) &address[1]; | ||
629 | if (addr_str[ntohl (address->urlen) - 1] != '\0') | ||
630 | return NULL; | ||
631 | GNUNET_asprintf (&res, "%s.%u.%s", plugin, ntohl (address->options), | ||
632 | (char*)&address[1]); | ||
633 | if (strlen (res) + 1 < 500) | ||
634 | { | ||
635 | GNUNET_memcpy (rbuf, res, strlen (res) + 1); | ||
636 | GNUNET_free (res); | ||
637 | return rbuf; | ||
638 | } | ||
639 | GNUNET_break (0); | ||
640 | GNUNET_free (res); | ||
641 | return NULL; | ||
642 | } | ||
643 | |||
644 | |||
645 | /** | ||
646 | * Function called to convert a string address to | ||
647 | * a binary address. | ||
648 | * | ||
649 | * @param cls closure ('struct Plugin*') | ||
650 | * @param addr string address | ||
651 | * @param addrlen length of the @a addr | ||
652 | * @param buf location to store the buffer | ||
653 | * If the function returns #GNUNET_SYSERR, its contents are undefined. | ||
654 | * @param added length of created address | ||
655 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure | ||
656 | */ | ||
657 | int | ||
658 | http_common_plugin_string_to_address (void *cls, | ||
659 | const char *addr, | ||
660 | uint16_t addrlen, | ||
661 | void **buf, | ||
662 | size_t *added) | ||
663 | { | ||
664 | struct HttpAddress *a; | ||
665 | char *address; | ||
666 | char *plugin; | ||
667 | char *optionstr; | ||
668 | size_t urlen; | ||
669 | uint32_t options; | ||
670 | |||
671 | /* Format protocol.options.address:port */ | ||
672 | address = NULL; | ||
673 | plugin = NULL; | ||
674 | optionstr = NULL; | ||
675 | if ((NULL == addr) || (addrlen == 0)) | ||
676 | { | ||
677 | GNUNET_break (0); | ||
678 | return GNUNET_SYSERR; | ||
679 | } | ||
680 | if ('\0' != addr[addrlen - 1]) | ||
681 | { | ||
682 | GNUNET_break (0); | ||
683 | return GNUNET_SYSERR; | ||
684 | } | ||
685 | if (strlen (addr) != addrlen - 1) | ||
686 | { | ||
687 | GNUNET_break (0); | ||
688 | return GNUNET_SYSERR; | ||
689 | } | ||
690 | plugin = GNUNET_strdup (addr); | ||
691 | optionstr = strchr (plugin, '.'); | ||
692 | if (NULL == optionstr) | ||
693 | { | ||
694 | GNUNET_break (0); | ||
695 | GNUNET_free (plugin); | ||
696 | return GNUNET_SYSERR; | ||
697 | } | ||
698 | optionstr[0] = '\0'; | ||
699 | optionstr++; | ||
700 | options = atol (optionstr); /* 0 on conversion error, that's ok */ | ||
701 | address = strchr (optionstr, '.'); | ||
702 | if (NULL == address) | ||
703 | { | ||
704 | GNUNET_break (0); | ||
705 | GNUNET_free (plugin); | ||
706 | return GNUNET_SYSERR; | ||
707 | } | ||
708 | address[0] = '\0'; | ||
709 | address++; | ||
710 | urlen = strlen (address) + 1; | ||
711 | |||
712 | a = GNUNET_malloc (sizeof(struct HttpAddress) + urlen); | ||
713 | a->options = htonl (options); | ||
714 | a->urlen = htonl (urlen); | ||
715 | GNUNET_memcpy (&a[1], address, urlen); | ||
716 | |||
717 | (*buf) = a; | ||
718 | (*added) = sizeof(struct HttpAddress) + urlen; | ||
719 | GNUNET_free (plugin); | ||
720 | return GNUNET_OK; | ||
721 | } | ||
722 | |||
723 | |||
724 | /** | ||
725 | * Create a HTTP address from a socketaddr | ||
726 | * | ||
727 | * @param protocol protocol | ||
728 | * @param addr sockaddr * address | ||
729 | * @param addrlen length of the address | ||
730 | * @return the HttpAddress | ||
731 | */ | ||
732 | struct HttpAddress * | ||
733 | http_common_address_from_socket (const char *protocol, | ||
734 | const struct sockaddr *addr, | ||
735 | socklen_t addrlen) | ||
736 | { | ||
737 | struct HttpAddress *address = NULL; | ||
738 | char *res; | ||
739 | size_t len; | ||
740 | |||
741 | GNUNET_asprintf (&res, | ||
742 | "%s://%s", | ||
743 | protocol, | ||
744 | GNUNET_a2s (addr, | ||
745 | addrlen)); | ||
746 | len = strlen (res) + 1; | ||
747 | address = GNUNET_malloc (sizeof(struct HttpAddress) + len); | ||
748 | address->options = htonl (HTTP_OPTIONS_NONE); | ||
749 | address->urlen = htonl (len); | ||
750 | GNUNET_memcpy (&address[1], res, len); | ||
751 | GNUNET_free (res); | ||
752 | return address; | ||
753 | } | ||
754 | |||
755 | |||
756 | /** | ||
757 | * Create a socketaddr from a HTTP address | ||
758 | * | ||
759 | * @param addr a `sockaddr *` address | ||
760 | * @param addrlen length of the @a addr | ||
761 | * @param res the result: | ||
762 | * #GNUNET_SYSERR, invalid input, | ||
763 | * #GNUNET_YES: could convert to ip, | ||
764 | * #GNUNET_NO: valid input but could not convert to ip (hostname?) | ||
765 | * @return the string | ||
766 | */ | ||
767 | struct sockaddr * | ||
768 | http_common_socket_from_address (const void *addr, | ||
769 | size_t addrlen, | ||
770 | int *res) | ||
771 | { | ||
772 | const struct HttpAddress *ha; | ||
773 | struct SplittedHTTPAddress *spa; | ||
774 | struct sockaddr_storage *s; | ||
775 | char *to_conv; | ||
776 | size_t urlen; | ||
777 | |||
778 | (*res) = GNUNET_SYSERR; | ||
779 | ha = (const struct HttpAddress *) addr; | ||
780 | if (NULL == addr) | ||
781 | { | ||
782 | GNUNET_break (0); | ||
783 | return NULL; | ||
784 | } | ||
785 | if (0 == addrlen) | ||
786 | { | ||
787 | GNUNET_break (0); | ||
788 | return NULL; | ||
789 | } | ||
790 | if (addrlen < sizeof(struct HttpAddress)) | ||
791 | { | ||
792 | GNUNET_break (0); | ||
793 | return NULL; | ||
794 | } | ||
795 | urlen = ntohl (ha->urlen); | ||
796 | if (sizeof(struct HttpAddress) + urlen != addrlen) | ||
797 | { | ||
798 | /* This is a legacy addresses */ | ||
799 | return NULL; | ||
800 | } | ||
801 | if (addrlen < sizeof(struct HttpAddress) + urlen) | ||
802 | { | ||
803 | /* This is a legacy addresses */ | ||
804 | return NULL; | ||
805 | } | ||
806 | if (((char *) addr)[addrlen - 1] != '\0') | ||
807 | { | ||
808 | GNUNET_break (0); | ||
809 | return NULL; | ||
810 | } | ||
811 | spa = http_split_address ((const char *) &ha[1]); | ||
812 | if (NULL == spa) | ||
813 | { | ||
814 | (*res) = GNUNET_SYSERR; | ||
815 | return NULL; | ||
816 | } | ||
817 | |||
818 | s = GNUNET_new (struct sockaddr_storage); | ||
819 | GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port); | ||
820 | if (GNUNET_SYSERR | ||
821 | == GNUNET_STRINGS_to_address_ip (to_conv, strlen (to_conv), s)) | ||
822 | { | ||
823 | /* could be a hostname */ | ||
824 | GNUNET_free (s); | ||
825 | (*res) = GNUNET_NO; | ||
826 | s = NULL; | ||
827 | } | ||
828 | else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family)) | ||
829 | { | ||
830 | GNUNET_free (s); | ||
831 | (*res) = GNUNET_SYSERR; | ||
832 | s = NULL; | ||
833 | } | ||
834 | else | ||
835 | { | ||
836 | (*res) = GNUNET_YES; | ||
837 | } | ||
838 | http_clean_splitted (spa); | ||
839 | GNUNET_free (to_conv); | ||
840 | return (struct sockaddr *) s; | ||
841 | } | ||
842 | |||
843 | |||
844 | /** | ||
845 | * Get the length of an address | ||
846 | * | ||
847 | * @param addr address | ||
848 | * @return the size | ||
849 | */ | ||
850 | size_t | ||
851 | http_common_address_get_size (const struct HttpAddress *addr) | ||
852 | { | ||
853 | return sizeof(struct HttpAddress) + ntohl (addr->urlen); | ||
854 | } | ||
855 | |||
856 | |||
857 | /** | ||
858 | * Compare addr1 to addr2 | ||
859 | * | ||
860 | * @param addr1 address1 | ||
861 | * @param addrlen1 address 1 length | ||
862 | * @param addr2 address2 | ||
863 | * @param addrlen2 address 2 length | ||
864 | * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error | ||
865 | */ | ||
866 | size_t | ||
867 | http_common_cmp_addresses (const void *addr1, | ||
868 | size_t addrlen1, | ||
869 | const void *addr2, | ||
870 | size_t addrlen2) | ||
871 | { | ||
872 | const char *a1 = addr1; | ||
873 | const char *a2 = addr2; | ||
874 | const struct HttpAddress *ha1; | ||
875 | const struct HttpAddress *ha2; | ||
876 | |||
877 | ha1 = (const struct HttpAddress *) a1; | ||
878 | ha2 = (const struct HttpAddress *) a2; | ||
879 | |||
880 | if (NULL == a1) | ||
881 | return GNUNET_SYSERR; | ||
882 | if (0 == addrlen1) | ||
883 | return GNUNET_SYSERR; | ||
884 | if (a1[addrlen1 - 1] != '\0') | ||
885 | return GNUNET_SYSERR; | ||
886 | |||
887 | if (NULL == a2) | ||
888 | return GNUNET_SYSERR; | ||
889 | if (0 == addrlen2) | ||
890 | return GNUNET_SYSERR; | ||
891 | if (a2[addrlen2 - 1] != '\0') | ||
892 | return GNUNET_SYSERR; | ||
893 | |||
894 | if (addrlen1 != addrlen2) | ||
895 | return GNUNET_NO; | ||
896 | if (ha1->urlen != ha2->urlen) | ||
897 | return GNUNET_NO; | ||
898 | |||
899 | if (0 == strcmp ((const char *) &ha1[1], (const char *) &ha2[1])) | ||
900 | return GNUNET_YES; | ||
901 | return GNUNET_NO; | ||
902 | } | ||
903 | |||
904 | |||
905 | /** | ||
906 | * Function obtain the network type for an address. | ||
907 | * | ||
908 | * @param env the environment | ||
909 | * @param address the address | ||
910 | * @return the network type | ||
911 | */ | ||
912 | enum GNUNET_NetworkType | ||
913 | http_common_get_network_for_address (struct | ||
914 | GNUNET_TRANSPORT_PluginEnvironment *env, | ||
915 | const struct GNUNET_HELLO_Address *address) | ||
916 | { | ||
917 | struct sockaddr *sa; | ||
918 | enum GNUNET_NetworkType net_type; | ||
919 | size_t salen = 0; | ||
920 | int res; | ||
921 | |||
922 | net_type = GNUNET_NT_UNSPECIFIED; | ||
923 | sa = http_common_socket_from_address (address->address, | ||
924 | address->address_length, | ||
925 | &res); | ||
926 | if (GNUNET_SYSERR == res) | ||
927 | return net_type; | ||
928 | if (GNUNET_YES == res) | ||
929 | { | ||
930 | GNUNET_assert (NULL != sa); | ||
931 | if (AF_INET == sa->sa_family) | ||
932 | { | ||
933 | salen = sizeof(struct sockaddr_in); | ||
934 | } | ||
935 | else if (AF_INET6 == sa->sa_family) | ||
936 | { | ||
937 | salen = sizeof(struct sockaddr_in6); | ||
938 | } | ||
939 | net_type = env->get_address_type (env->cls, | ||
940 | sa, | ||
941 | salen); | ||
942 | GNUNET_free (sa); | ||
943 | } | ||
944 | return net_type; | ||
945 | } | ||
946 | |||
947 | |||
948 | /* end of plugin_transport_http_common.c */ | ||