profiler done
[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 saddr the splitted http address
248  * @param options address options
249  * @param dnsresult dns name to include in address
250  * @return string representing the same address or NULL on error
251  */
252 static const char *
253 http_common_plugin_dnsresult_to_address (const char *plugin,
254     const struct SplittedHTTPAddress *saddr,
255     uint32_t options,
256     const char *dnsresult)
257 {
258   static char rbuf[1024];
259   char *res;
260
261   GNUNET_asprintf (&res, "%s.%u.%s://%s:%u%s", plugin, options, saddr->protocol,
262       dnsresult, saddr->port, saddr->path);
263   if (strlen (res) + 1 < 500)
264   {
265     memcpy (rbuf, res, strlen (res) + 1);
266     GNUNET_free(res);
267     return rbuf;
268   }
269   GNUNET_break(0);
270   GNUNET_free(res);
271   return NULL ;
272 }
273
274 static void
275 http_common_dns_reverse_lookup_cb (void *cls, const char *hostname)
276 {
277   struct PrettyPrinterContext *ppc = cls;
278
279   if (NULL != hostname)
280   {
281     ppc->asc (ppc->asc_cls,
282         http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options,
283             hostname), GNUNET_OK);
284     ppc->sucess = GNUNET_YES;
285
286   }
287   else
288   {
289     ppc->asc (ppc->asc_cls, NULL,
290         (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
291
292     GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
293     http_clean_splitted (ppc->saddr);
294     GNUNET_free(ppc->plugin);
295     GNUNET_free(ppc);
296   }
297 }
298
299 static int
300 http_common_dns_reverse_lookup (const struct sockaddr *sockaddr,
301     socklen_t sockaddr_len, const char *type, struct SplittedHTTPAddress *saddr,
302     uint32_t options,
303     struct GNUNET_TIME_Relative timeout,
304     GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
305 {
306   struct PrettyPrinterContext *ppc;
307   ppc = GNUNET_new (struct PrettyPrinterContext);
308
309   ppc->sucess = GNUNET_NO;
310   ppc->saddr = saddr;
311   ppc->asc = asc;
312   ppc->asc_cls = asc_cls;
313   ppc->plugin = GNUNET_strdup (type);
314   ppc->options = options;
315
316   ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sockaddr, sockaddr_len,
317       GNUNET_YES, timeout, &http_common_dns_reverse_lookup_cb, ppc);
318   if (NULL == ppc->resolver_handle)
319   {
320     GNUNET_free(ppc->plugin);
321     GNUNET_free(ppc);
322     return GNUNET_SYSERR;
323   }
324
325   GNUNET_CONTAINER_DLL_insert(dll_ppc_head, dll_ppc_tail, ppc);
326   return GNUNET_OK;
327 }
328
329 static void
330 http_common_dns_ip_lookup_cb (void *cls, const struct sockaddr *addr,
331     socklen_t addrlen)
332 {
333   struct PrettyPrinterContext *ppc = cls;
334
335   if (NULL != addr)
336   {
337     ppc->asc (ppc->asc_cls,
338         http_common_plugin_dnsresult_to_address (ppc->plugin, ppc->saddr, ppc->options,
339             GNUNET_a2s (addr, addrlen)), GNUNET_OK);
340     ppc->sucess = GNUNET_YES;
341     ppc->asc (ppc->asc_cls, GNUNET_a2s (addr, addrlen), GNUNET_OK);
342   }
343   else
344   {
345     ppc->asc (ppc->asc_cls, NULL,
346         (GNUNET_NO == ppc->sucess) ? GNUNET_SYSERR : GNUNET_OK);
347
348     GNUNET_CONTAINER_DLL_remove(dll_ppc_head, dll_ppc_tail, ppc);
349     GNUNET_free(ppc->plugin);
350     http_clean_splitted (ppc->saddr);
351     GNUNET_free(ppc);
352   }
353 }
354
355 static int
356 http_common_dns_ip_lookup (const char *name, const char *type,
357     struct SplittedHTTPAddress *saddr,
358     uint32_t options,
359     struct GNUNET_TIME_Relative timeout,
360     GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
361 {
362   struct PrettyPrinterContext *ppc;
363   ppc = GNUNET_new (struct PrettyPrinterContext);
364
365   ppc->sucess = GNUNET_NO;
366   ppc->saddr = saddr;
367   ppc->asc = asc;
368   ppc->asc_cls = asc_cls;
369   ppc->plugin = GNUNET_strdup (type);
370   ppc->options = options;
371
372   ppc->resolver_handle = GNUNET_RESOLVER_ip_get (name, AF_UNSPEC, timeout,
373       &http_common_dns_ip_lookup_cb, ppc);
374   if (NULL == ppc->resolver_handle)
375   {
376     GNUNET_free(ppc->plugin);
377     GNUNET_free(ppc);
378     return GNUNET_SYSERR;
379   }
380
381   GNUNET_CONTAINER_DLL_insert(dll_ppc_head, dll_ppc_tail, ppc);
382   return GNUNET_OK;
383 }
384
385 /**
386  * Convert the transports address to a nice, human-readable
387  * format.
388  *
389  * @param cls closure
390  * @param type name of the transport that generated the address
391  * @param addr one of the addresses of the host, NULL for the last address
392  *        the specific address format depends on the transport
393  * @param addrlen length of the @a addr
394  * @param numeric should (IP) addresses be displayed in numeric form?
395  * @param timeout after how long should we give up?
396  * @param asc function to call on each string
397  * @param asc_cls closure for @a asc
398  */
399 void
400 http_common_plugin_address_pretty_printer (void *cls, const char *type,
401     const void *addr, size_t addrlen, int numeric,
402     struct GNUNET_TIME_Relative timeout,
403     GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
404 {
405   const struct HttpAddress *address = addr;
406   struct SplittedHTTPAddress *saddr;
407   struct sockaddr *sock_addr;
408   const char *ret;
409   char *addr_str;
410   int res;
411   int have_ip;
412
413   saddr = NULL;
414   sock_addr = NULL;
415   if ((addrlen < sizeof(struct HttpAddress))
416       || (addrlen != http_common_address_get_size (address)))
417   {
418     GNUNET_break(0);
419     goto handle_error;
420   }
421
422   addr_str = (char *) &address[1];
423   if (addr_str[ntohl (address->urlen) - 1] != '\0')
424   {
425     GNUNET_break(0);
426     goto handle_error;
427   }
428
429   saddr = http_split_address (addr_str);
430   if (NULL == saddr)
431   {
432     GNUNET_break(0);
433     goto handle_error;
434   }
435
436   sock_addr = http_common_socket_from_address (addr, addrlen, &res);
437   if (GNUNET_SYSERR == res)
438   {
439     /* Malformed address */
440     GNUNET_break(0);
441     goto handle_error;
442   }
443   else if (GNUNET_NO == res)
444   {
445     /* Could not convert to IP */
446     have_ip = GNUNET_NO;
447   }
448   else if (GNUNET_YES == res)
449   {
450     /* Converted to IP */
451     have_ip = GNUNET_YES;
452   }
453   else
454   {
455     /* Must not happen */
456     GNUNET_break(0);
457     goto handle_error;
458   }
459
460   if ((GNUNET_YES == numeric) && (GNUNET_YES == have_ip))
461   {
462     /* No lookup required */
463     ret = http_common_plugin_address_to_string (type, address, addrlen);
464     asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
465     asc (asc_cls, NULL, GNUNET_OK);
466     http_clean_splitted (saddr);
467     GNUNET_free_non_null(sock_addr);
468     return;
469   }
470   else if ((GNUNET_YES == numeric) && (GNUNET_NO == have_ip))
471   {
472     /* Forward lookup */
473     if (GNUNET_SYSERR
474         == http_common_dns_ip_lookup (saddr->host, type, saddr, address->options, timeout, asc,
475             asc_cls))
476     {
477       GNUNET_break(0);
478       goto handle_error;
479     }
480     /* Wait for resolver callback */
481     return;
482   }
483   else if ((GNUNET_NO == numeric) && (GNUNET_YES == have_ip))
484   {
485     /* Reverse lookup */
486     if (GNUNET_SYSERR
487         == http_common_dns_reverse_lookup (sock_addr,
488             (AF_INET == sock_addr->sa_family) ?
489                 sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), type,
490             saddr, address->options, timeout, asc, asc_cls))
491     {
492       GNUNET_break(0);
493       goto handle_error;
494     }
495     /* Wait for resolver callback */
496     return;
497   }
498   else if ((GNUNET_NO == numeric) && (GNUNET_NO == have_ip))
499   {
500     /* No lookup required */
501     ret = http_common_plugin_address_to_string (type, address, addrlen);
502     asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
503     asc (asc_cls, NULL, GNUNET_OK);
504     GNUNET_free_non_null(sock_addr);
505     http_clean_splitted (saddr);
506     return;
507   }
508   else
509   {
510     /* Error */
511     goto handle_error;
512   }
513   return;
514
515   handle_error:
516   /* Error */
517   asc (asc_cls, NULL, GNUNET_SYSERR);
518   asc (asc_cls, NULL, GNUNET_OK);
519   GNUNET_free_non_null(sock_addr);
520   if (NULL != saddr)
521     http_clean_splitted (saddr);
522   return;
523
524 }
525
526 /**
527  * FIXME.
528  */
529 const char *
530 http_common_plugin_address_to_url (void *cls, const void *addr, size_t addrlen)
531 {
532   static char rbuf[1024];
533   const struct HttpAddress *address = addr;
534   const char * addr_str;
535
536   if (NULL == addr)
537   {
538     GNUNET_break(0);
539     return NULL ;
540   }
541   if (0 >= addrlen)
542   {
543     GNUNET_break(0);
544     return NULL ;
545   }
546   if (addrlen != http_common_address_get_size (address))
547   {
548     GNUNET_break(0);
549     return NULL ;
550   }
551   addr_str = (char *) &address[1];
552
553   if (addr_str[ntohl (address->urlen) - 1] != '\0')
554     return NULL ;
555
556   memcpy (rbuf, &address[1], ntohl (address->urlen));
557   return rbuf;
558 }
559
560 /**
561  * Function called for a quick conversion of the binary address to
562  * a numeric address.  Note that the caller must not free the
563  * address and that the next call to this function is allowed
564  * to override the address again.
565  *
566  * @param plugin the name of the plugin
567  * @param addr binary address
568  * @param addrlen length of the address
569  * @return string representing the same address
570  */
571 const char *
572 http_common_plugin_address_to_string (const char *plugin, const void *addr,
573     size_t addrlen)
574 {
575   static char rbuf[1024];
576   const struct HttpAddress *address = addr;
577   const char * addr_str;
578   char *res;
579
580   GNUNET_assert(NULL != plugin);
581   if (NULL == addr)
582     return NULL ;
583   if (0 == addrlen)
584     return NULL ;
585   if (addrlen != http_common_address_get_size (address))
586     return NULL ;
587   addr_str = (char *) &address[1];
588   if (addr_str[ntohl (address->urlen) - 1] != '\0')
589     return NULL ;
590   GNUNET_asprintf (&res, "%s.%u.%s", plugin, ntohl (address->options),
591       &address[1]);
592   if (strlen (res) + 1 < 500)
593   {
594     memcpy (rbuf, res, strlen (res) + 1);
595     GNUNET_free(res);
596     return rbuf;
597   }
598   GNUNET_break(0);
599   GNUNET_free(res);
600   return NULL ;
601 }
602
603 /**
604  * Function called to convert a string address to
605  * a binary address.
606  *
607  * @param cls closure ('struct Plugin*')
608  * @param addr string address
609  * @param addrlen length of the @a addr
610  * @param buf location to store the buffer
611  *        If the function returns #GNUNET_SYSERR, its contents are undefined.
612  * @param added length of created address
613  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
614  */
615 int
616 http_common_plugin_string_to_address (void *cls, const char *addr,
617     uint16_t addrlen, void **buf, size_t *added)
618 {
619   struct HttpAddress *a;
620   char *address;
621   char *plugin;
622   char *optionstr;
623   size_t urlen;
624   uint32_t options;
625
626   /* Format protocol.options.address:port */
627   address = NULL;
628   plugin = NULL;
629   optionstr = NULL;
630   if ((NULL == addr) || (addrlen == 0))
631   {
632     GNUNET_break(0);
633     return GNUNET_SYSERR;
634   }
635   if ('\0' != addr[addrlen - 1])
636   {
637     GNUNET_break(0);
638     return GNUNET_SYSERR;
639   }
640   if (strlen (addr) != addrlen - 1)
641   {
642     GNUNET_break(0);
643     return GNUNET_SYSERR;
644   }
645   plugin = GNUNET_strdup (addr);
646   optionstr = strchr (plugin, '.');
647   if (NULL == optionstr)
648   {
649     GNUNET_break(0);
650     GNUNET_free(plugin);
651     return GNUNET_SYSERR;
652   }
653   optionstr[0] = '\0';
654   optionstr++;
655   options = atol (optionstr); /* 0 on conversion error, that's ok */
656   address = strchr (optionstr, '.');
657   if (NULL == address)
658   {
659     GNUNET_break(0);
660     GNUNET_free(plugin);
661     return GNUNET_SYSERR;
662   }
663   address[0] = '\0';
664   address++;
665   urlen = strlen (address) + 1;
666
667   a = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
668   a->options = htonl (options);
669   a->urlen = htonl (urlen);
670   memcpy (&a[1], address, urlen);
671
672   (*buf) = a;
673   (*added) = sizeof(struct HttpAddress) + urlen;
674   GNUNET_free(plugin);
675   return GNUNET_OK;
676 }
677
678 /**
679  * Create a HTTP address from a socketaddr
680  *
681  * @param protocol protocol
682  * @param addr sockaddr * address
683  * @param addrlen length of the address
684  * @return the HttpAddress
685  */
686 struct HttpAddress *
687 http_common_address_from_socket (const char *protocol,
688     const struct sockaddr *addr, socklen_t addrlen)
689 {
690   struct HttpAddress *address = NULL;
691   char *res;
692   size_t len;
693
694   GNUNET_asprintf (&res, "%s://%s", protocol, GNUNET_a2s (addr, addrlen));
695   len = strlen (res) + 1;
696   address = GNUNET_malloc (sizeof (struct HttpAddress) + len);
697   address->options = htonl (HTTP_OPTIONS_NONE);
698   address->urlen = htonl (len);
699   memcpy (&address[1], res, len);
700   GNUNET_free(res);
701   return address;
702 }
703
704 /**
705  * Create a socketaddr from a HTTP address
706  *
707  * @param addr a `sockaddr *` address
708  * @param addrlen length of the @a addr
709  * @param res the result:
710  *   #GNUNET_SYSERR, invalid input,
711  *   #GNUNET_YES: could convert to ip,
712  *   #GNUNET_NO: valid input but could not convert to ip (hostname?)
713  * @return the string
714  */
715 struct sockaddr *
716 http_common_socket_from_address (const void *addr, size_t addrlen, int *res)
717 {
718   const struct HttpAddress *ha;
719   struct SplittedHTTPAddress * spa;
720   struct sockaddr_storage *s;
721   char * to_conv;
722   size_t urlen;
723
724   (*res) = GNUNET_SYSERR;
725   ha = (const struct HttpAddress *) addr;
726   if (NULL == addr)
727   {
728     GNUNET_break(0);
729     return NULL ;
730   }
731   if (0 >= addrlen)
732   {
733     GNUNET_break(0);
734     return NULL ;
735   }
736   if (addrlen < sizeof(struct HttpAddress))
737   {
738     GNUNET_break(0);
739     return NULL ;
740   }
741   urlen = ntohl (ha->urlen);
742   if (sizeof(struct HttpAddress) + urlen != addrlen)
743   {
744     /* This is a legacy addresses */
745     return NULL ;
746   }
747   if (addrlen < sizeof(struct HttpAddress) + urlen)
748   {
749     /* This is a legacy addresses */
750     return NULL ;
751   }
752   if (((char *) addr)[addrlen - 1] != '\0')
753   {
754     GNUNET_break(0);
755     return NULL ;
756   }
757   spa = http_split_address ((const char *) &ha[1]);
758   if (NULL == spa)
759   {
760     (*res) = GNUNET_SYSERR;
761     return NULL ;
762   }
763
764   s = GNUNET_new (struct sockaddr_storage);
765   GNUNET_asprintf (&to_conv, "%s:%u", spa->host, spa->port);
766   if (GNUNET_SYSERR
767       == GNUNET_STRINGS_to_address_ip (to_conv, strlen (to_conv), s))
768   {
769     /* could be a hostname */
770     GNUNET_free(s);
771     (*res) = GNUNET_NO;
772     s = NULL;
773   }
774   else if ((AF_INET != s->ss_family) && (AF_INET6 != s->ss_family))
775   {
776
777     GNUNET_free(s);
778     (*res) = GNUNET_SYSERR;
779     s = NULL;
780   }
781   else
782   {
783     (*res) = GNUNET_YES;
784   }
785   http_clean_splitted (spa);
786   GNUNET_free(to_conv);
787   return (struct sockaddr *) s;
788 }
789
790 /**
791  * Get the length of an address
792  *
793  * @param addr address
794  * @return the size
795  */
796 size_t
797 http_common_address_get_size (const struct HttpAddress * addr)
798 {
799   return sizeof(struct HttpAddress) + ntohl (addr->urlen);
800 }
801
802 /**
803  * Compare addr1 to addr2
804  *
805  * @param addr1 address1
806  * @param addrlen1 address 1 length
807  * @param addr2 address2
808  * @param addrlen2 address 2 length
809  * @return #GNUNET_YES if equal, #GNUNET_NO if not, #GNUNET_SYSERR on error
810  */
811 size_t
812 http_common_cmp_addresses (const void *addr1, size_t addrlen1,
813     const void *addr2, size_t addrlen2)
814 {
815   const char *a1 = addr1;
816   const char *a2 = addr2;
817   const struct HttpAddress *ha1;
818   const struct HttpAddress *ha2;
819   ha1 = (const struct HttpAddress *) a1;
820   ha2 = (const struct HttpAddress *) a2;
821
822   if (NULL == a1)
823     return GNUNET_SYSERR;
824   if (0 >= addrlen1)
825     return GNUNET_SYSERR;
826   if (a1[addrlen1 - 1] != '\0')
827     return GNUNET_SYSERR;
828
829   if (NULL == a2)
830     return GNUNET_SYSERR;
831   if (0 >= addrlen2)
832     return GNUNET_SYSERR;
833   if (a2[addrlen2 - 1] != '\0')
834     return GNUNET_SYSERR;
835
836   if (addrlen1 != addrlen2)
837     return GNUNET_NO;
838   if (ha1->urlen != ha2->urlen)
839     return GNUNET_NO;
840
841   if (0 == strcmp ((const char *) &ha1[1], (const char *) &ha2[1]))
842     return GNUNET_YES;
843   return GNUNET_NO;
844 }
845
846 /* end of plugin_transport_http_common.c */