a420b8cdc5a73a20852546b027b95341f4af58c2
[oweals/gnunet.git] / src / transport / plugin_transport_http_common.c
1 /*
2  This file is part of GNUnet
3  (C) 2002-2013 Christian Grothoff (and other contributing authors)
4
5  GNUnet is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published
7  by the Free Software Foundation; either version 3, or (at your
8  option) any later version.
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  General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with GNUnet; see the file COPYING.  If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  Boston, MA 02111-1307, USA.
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_non_null(spa->protocol);
38     GNUNET_free_non_null(spa->host);
39     GNUNET_free_non_null(spa->path);
40     GNUNET_free_non_null(spa);
41   }
42 }
43
44 struct SplittedHTTPAddress *
45 http_split_address (const char * addr)
46 {
47   struct SplittedHTTPAddress *sp;
48   char *src = GNUNET_strdup (addr);
49   char *protocol_start = NULL;
50   char *host_start = NULL;
51   char *v6_end = NULL;
52   char *port_start = NULL;
53   char *path_start = NULL;
54   protocol_start = src;
55
56   sp = GNUNET_new (struct SplittedHTTPAddress);
57   /* Address string consists of protocol://host[:port]path*/
58
59   host_start = strstr (src, "://");
60   if (NULL == host_start)
61   {
62     GNUNET_free(src);
63     GNUNET_free(sp);
64     return NULL ;
65   }
66   host_start[0] = '\0';
67   sp->protocol = GNUNET_strdup (protocol_start);
68
69   host_start += strlen ("://");
70   if (strlen (host_start) == 0)
71   {
72     GNUNET_free(src);
73     GNUNET_free(sp->protocol);
74     GNUNET_free(sp);
75     return NULL ;
76   }
77
78   /* Find path start */
79   path_start = strchr (host_start, '/');
80   if (NULL != path_start)
81   {
82     sp->path = GNUNET_strdup (path_start);
83     path_start[0] = '\0';
84   }
85   else
86     sp->path = GNUNET_strdup ("");
87
88   if (strlen (host_start) < 1)
89   {
90     GNUNET_free(src);
91     GNUNET_free(sp->protocol);
92     GNUNET_free(sp->path);
93     GNUNET_free(sp);
94     return NULL ;
95   }
96
97   if (NULL != (port_start = strrchr (host_start, ':')))
98   {
99     /* *We COULD have a port, but also an IPv6 address! */
100     if (NULL != (v6_end = strchr (host_start, ']')))
101     {
102       if (v6_end < port_start)
103       {
104         /* IPv6 address + port */
105         port_start[0] = '\0';
106         port_start++;
107         sp->port = atoi (port_start);
108         if ((0 == sp->port) || (65535 < sp->port))
109         {
110           GNUNET_free(src);
111           GNUNET_free(sp->protocol);
112           GNUNET_free(sp->path);
113           GNUNET_free(sp);
114           return NULL ;
115         }
116       }
117       else
118       {
119         /* IPv6 address + no port */
120         if (0 == strcmp (sp->protocol, "https"))
121           sp->port = HTTPS_DEFAULT_PORT;
122         else if (0 == strcmp (sp->protocol, "http"))
123           sp->port = HTTP_DEFAULT_PORT;
124       }
125     }
126     else
127     {
128       /* No IPv6 address */
129       port_start[0] = '\0';
130       port_start++;
131       sp->port = atoi (port_start);
132       if ((0 == sp->port) || (65535 < sp->port))
133       {
134         GNUNET_free(src);
135         GNUNET_free(sp->protocol);
136         GNUNET_free(sp->path);
137         GNUNET_free(sp);
138         return NULL ;
139       }
140     }
141   }
142   else
143   {
144     /* No ':' as port separator, default port for protocol */
145     if (0 == strcmp (sp->protocol, "https"))
146       sp->port = HTTPS_DEFAULT_PORT;
147     else if (0 == strcmp (sp->protocol, "http"))
148       sp->port = HTTP_DEFAULT_PORT;
149     else
150     {
151       GNUNET_break(0);
152       GNUNET_free(src);
153       GNUNET_free(sp->protocol);
154       GNUNET_free(sp->path);
155       GNUNET_free(sp);
156       return NULL ;
157     }
158   }
159   if (strlen (host_start) > 0)
160     sp->host = GNUNET_strdup (host_start);
161   else
162   {
163     GNUNET_break(0);
164     GNUNET_free(src);
165     GNUNET_free(sp->protocol);
166     GNUNET_free(sp->path);
167     GNUNET_free(sp);
168     return NULL ;
169   }
170   GNUNET_free(src);
171   return sp;
172 }
173
174 /**
175  * Closure for #append_port().
176  */
177 struct 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  */
233 static struct PrettyPrinterContext *dll_ppc_head;
234
235 /**
236  * Tail of PPC list
237  */
238 static 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  */
251 static const char *
252 http_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
272 static void
273 http_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
297 static int
298 http_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
327 static void
328 http_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
353 static int
354 http_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 }
382
383 /**
384  * Convert the transports address to a nice, human-readable
385  * format.
386  *
387  * @param cls closure
388  * @param type name of the transport that generated the address
389  * @param addr one of the addresses of the host, NULL for the last address
390  *        the specific address format depends on the transport
391  * @param addrlen length of the @a addr
392  * @param numeric should (IP) addresses be displayed in numeric form?
393  * @param timeout after how long should we give up?
394  * @param asc function to call on each string
395  * @param asc_cls closure for @a asc
396  */
397 void
398 http_common_plugin_address_pretty_printer (void *cls, const char *type,
399     const void *addr, size_t addrlen, int numeric,
400     struct GNUNET_TIME_Relative timeout,
401     GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
402 {
403   const struct HttpAddress *address = addr;
404   struct SplittedHTTPAddress *saddr;
405   struct sockaddr *sock_addr;
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   }
419
420   addr_str = (char *) &address[1];
421   if (addr_str[ntohl (address->urlen) - 1] != '\0')
422   {
423     GNUNET_break(0);
424     goto handle_error;
425   }
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 }
523
524 /**
525  * FIXME.
526  */
527 const char *
528 http_common_plugin_address_to_url (void *cls, const void *addr, size_t addrlen)
529 {
530   static char rbuf[1024];
531   const struct HttpAddress *address = addr;
532   const char * addr_str;
533
534   if (NULL == addr)
535   {
536     GNUNET_break(0);
537     return NULL ;
538   }
539   if (0 >= addrlen)
540   {
541     GNUNET_break(0);
542     return NULL ;
543   }
544   if (addrlen != http_common_address_get_size (address))
545   {
546     GNUNET_break(0);
547     return NULL ;
548   }
549   addr_str = (char *) &address[1];
550
551   if (addr_str[ntohl (address->urlen) - 1] != '\0')
552     return NULL ;
553
554   memcpy (rbuf, &address[1], ntohl (address->urlen));
555   return rbuf;
556 }
557
558 /**
559  * Function called for a quick conversion of the binary address to
560  * a numeric address.  Note that the caller must not free the
561  * address and that the next call to this function is allowed
562  * to override the address again.
563  *
564  * @param plugin the name of the plugin
565  * @param addr binary address
566  * @param addrlen length of the address
567  * @return string representing the same address
568  */
569 const char *
570 http_common_plugin_address_to_string (const char *plugin, const void *addr,
571     size_t addrlen)
572 {
573   static char rbuf[1024];
574   const struct HttpAddress *address = addr;
575   const char * addr_str;
576   char *res;
577
578   GNUNET_assert(NULL != plugin);
579   if (NULL == addr)
580     return NULL ;
581   if (0 == addrlen)
582     return NULL ;
583   if (addrlen != http_common_address_get_size (address))
584     return NULL ;
585   addr_str = (char *) &address[1];
586   if (addr_str[ntohl (address->urlen) - 1] != '\0')
587     return NULL ;
588   GNUNET_asprintf (&res, "%s.%u.%s", plugin, ntohl (address->options),
589       &address[1]);
590   if (strlen (res) + 1 < 500)
591   {
592     memcpy (rbuf, res, strlen (res) + 1);
593     GNUNET_free(res);
594     return rbuf;
595   }
596   GNUNET_break(0);
597   GNUNET_free(res);
598   return NULL ;
599 }
600
601 /**
602  * Function called to convert a string address to
603  * a binary address.
604  *
605  * @param cls closure ('struct Plugin*')
606  * @param addr string address
607  * @param addrlen length of the @a addr
608  * @param buf location to store the buffer
609  *        If the function returns #GNUNET_SYSERR, its contents are undefined.
610  * @param added length of created address
611  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
612  */
613 int
614 http_common_plugin_string_to_address (void *cls, const char *addr,
615     uint16_t addrlen, void **buf, size_t *added)
616 {
617   struct HttpAddress *a;
618   char *address;
619   char *plugin;
620   char *optionstr;
621   size_t urlen;
622   uint32_t options;
623
624   /* Format protocol.options.address:port */
625   address = NULL;
626   plugin = NULL;
627   optionstr = NULL;
628   if ((NULL == addr) || (addrlen == 0))
629   {
630     GNUNET_break(0);
631     return GNUNET_SYSERR;
632   }
633   if ('\0' != addr[addrlen - 1])
634   {
635     GNUNET_break(0);
636     return GNUNET_SYSERR;
637   }
638   if (strlen (addr) != addrlen - 1)
639   {
640     GNUNET_break(0);
641     return GNUNET_SYSERR;
642   }
643   plugin = GNUNET_strdup (addr);
644   optionstr = strchr (plugin, '.');
645   if (NULL == optionstr)
646   {
647     GNUNET_break(0);
648     GNUNET_free(plugin);
649     return GNUNET_SYSERR;
650   }
651   optionstr[0] = '\0';
652   optionstr++;
653   options = atol (optionstr); /* 0 on conversion error, that's ok */
654   address = strchr (optionstr, '.');
655   if (NULL == address)
656   {
657     GNUNET_break(0);
658     GNUNET_free(plugin);
659     return GNUNET_SYSERR;
660   }
661   address[0] = '\0';
662   address++;
663   urlen = strlen (address) + 1;
664
665   a = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
666   a->options = htonl (options);
667   a->urlen = htonl (urlen);
668   memcpy (&a[1], address, urlen);
669
670   (*buf) = a;
671   (*added) = sizeof(struct HttpAddress) + urlen;
672   GNUNET_free(plugin);
673   return GNUNET_OK;
674 }
675
676 /**
677  * Create a HTTP address from a socketaddr
678  *
679  * @param protocol protocol
680  * @param addr sockaddr * address
681  * @param addrlen length of the address
682  * @return the HttpAddress
683  */
684 struct HttpAddress *
685 http_common_address_from_socket (const char *protocol,
686     const struct sockaddr *addr, socklen_t addrlen)
687 {
688   struct HttpAddress *address = NULL;
689   char *res;
690   size_t len;
691
692   GNUNET_asprintf (&res, "%s://%s", protocol, GNUNET_a2s (addr, addrlen));
693   len = strlen (res) + 1;
694   address = GNUNET_malloc (sizeof (struct HttpAddress) + len);
695   address->options = htonl (HTTP_OPTIONS_NONE);
696   address->urlen = htonl (len);
697   memcpy (&address[1], res, len);
698   GNUNET_free(res);
699   return address;
700 }
701
702 /**
703  * Create a socketaddr from a HTTP address
704  *
705  * @param addr a `sockaddr *` address
706  * @param addrlen length of the @a addr
707  * @param res the result:
708  *   #GNUNET_SYSERR, invalid input,
709  *   #GNUNET_YES: could convert to ip,
710  *   #GNUNET_NO: valid input but could not convert to ip (hostname?)
711  * @return the string
712  */
713 struct sockaddr *
714 http_common_socket_from_address (const void *addr, size_t addrlen, int *res)
715 {
716   const struct HttpAddress *ha;
717   struct SplittedHTTPAddress * spa;
718   struct sockaddr_storage *s;
719   char * to_conv;
720   size_t urlen;
721
722   (*res) = GNUNET_SYSERR;
723   ha = (const struct HttpAddress *) addr;
724   if (NULL == addr)
725   {
726     GNUNET_break(0);
727     return NULL ;
728   }
729   if (0 >= addrlen)
730   {
731     GNUNET_break(0);
732     return NULL ;
733   }
734   if (addrlen < sizeof(struct HttpAddress))
735   {
736     GNUNET_break(0);
737     return NULL ;
738   }
739   urlen = ntohl (ha->urlen);
740   if (sizeof(struct HttpAddress) + urlen != addrlen)
741   {
742     /* This is a legacy addresses */
743     return NULL ;
744   }
745   if (addrlen < sizeof(struct HttpAddress) + urlen)
746   {
747     /* This is a legacy addresses */
748     return NULL ;
749   }
750   if (((char *) addr)[addrlen - 1] != '\0')
751   {
752     GNUNET_break(0);
753     return NULL ;
754   }
755   spa = http_split_address ((const char *) &ha[1]);
756   if (NULL == spa)
757   {
758     (*res) = GNUNET_SYSERR;
759     return NULL ;
760   }
761
762   s = GNUNET_new (struct sockaddr_storage);
763   GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port);
764   if (GNUNET_SYSERR
765       == GNUNET_STRINGS_to_address_ip (to_conv, strlen (to_conv), s))
766   {
767     /* could be a hostname */
768     GNUNET_free(s);
769     (*res) = GNUNET_NO;
770     s = NULL;
771   }
772   else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
773   {
774
775     GNUNET_free(s);
776     (*res) = GNUNET_SYSERR;
777     s = NULL;
778   }
779   else
780   {
781     (*res) = GNUNET_YES;
782   }
783   http_clean_splitted (spa);
784   GNUNET_free(to_conv);
785   return (struct sockaddr *) s;
786 }
787
788 /**
789  * Get the length of an address
790  *
791  * @param addr address
792  * @return the size
793  */
794 size_t
795 http_common_address_get_size (const struct HttpAddress * addr)
796 {
797   return sizeof(struct HttpAddress) + ntohl (addr->urlen);
798 }
799
800 /**
801  * Compare addr1 to addr2
802  *
803  * @param addr1 address1
804  * @param addrlen1 address 1 length
805  * @param addr2 address2
806  * @param addrlen2 address 2 length
807  * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error
808  */
809 size_t
810 http_common_cmp_addresses (const void *addr1, size_t addrlen1,
811     const void *addr2, size_t addrlen2)
812 {
813   const char *a1 = addr1;
814   const char *a2 = addr2;
815   const struct HttpAddress *ha1;
816   const struct HttpAddress *ha2;
817   ha1 = (const struct HttpAddress *) a1;
818   ha2 = (const struct HttpAddress *) a2;
819
820   if (NULL == a1)
821     return GNUNET_SYSERR;
822   if (0 >= addrlen1)
823     return GNUNET_SYSERR;
824   if (a1[addrlen1 - 1] != '\0')
825     return GNUNET_SYSERR;
826
827   if (NULL == a2)
828     return GNUNET_SYSERR;
829   if (0 >= addrlen2)
830     return GNUNET_SYSERR;
831   if (a2[addrlen2 - 1] != '\0')
832     return GNUNET_SYSERR;
833
834   if (addrlen1 != addrlen2)
835     return GNUNET_NO;
836   if (ha1->urlen != ha2->urlen)
837     return GNUNET_NO;
838
839   if (0 == strcmp ((const char *) &ha1[1], (const char *) &ha2[1]))
840     return GNUNET_YES;
841   return GNUNET_NO;
842 }
843
844 /* end of plugin_transport_http_common.c */