- fix for 601 assertion
[oweals/gnunet.git] / src / transport / plugin_transport_http.c
1 /*
2      This file is part of GNUnet
3      (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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.c
23  * @brief http transport service plugin
24  * @author Matthias Wachs
25  */
26
27 #include "plugin_transport_http.h"
28
29 /**
30  * After how long do we expire an address that we
31  * learned from another peer if it is not reconfirmed
32  * by anyone?
33  */
34 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
35
36 /**
37  * Wrapper to manage IPv4 addresses
38  */
39 struct IPv4HttpAddressWrapper
40 {
41   /**
42    * Linked list next
43    */
44   struct IPv4HttpAddressWrapper *next;
45
46   /**
47    * Linked list previous
48    */
49   struct IPv4HttpAddressWrapper *prev;
50
51   struct IPv4HttpAddress addr;
52 };
53
54 /**
55  * Wrapper for IPv4 addresses.
56  */
57 struct IPv6HttpAddressWrapper
58 {
59   /**
60    * Linked list next
61    */
62   struct IPv6HttpAddressWrapper *next;
63
64   /**
65    * Linked list previous
66    */
67   struct IPv6HttpAddressWrapper *prev;
68
69   struct IPv6HttpAddress addr6;
70 };
71
72
73 /**
74  * Context for address to string conversion.
75  */
76 struct PrettyPrinterContext
77 {
78   /**
79    * Function to call with the result.
80    */
81   GNUNET_TRANSPORT_AddressStringCallback asc;
82
83   /**
84    * Plugin
85    */
86   struct Plugin *plugin;
87
88   /**
89    * Clsoure for 'asc'.
90    */
91   void *asc_cls;
92
93   /**
94    * Port to add after the IP address.
95    */
96   uint16_t port;
97
98   uint32_t addrlen;
99
100   int numeric;
101 };
102
103
104 /**
105  * Encapsulation of all of the state of the plugin.
106  */
107 struct Plugin;
108
109
110
111 /**
112  * Append our port and forward the result.
113  *
114  * @param cls the 'struct PrettyPrinterContext*'
115  * @param hostname hostname part of the address
116  */
117 static void
118 append_port (void *cls, const char *hostname)
119 {
120   struct PrettyPrinterContext *ppc = cls;
121   static char rbuf[INET6_ADDRSTRLEN + 13];
122
123   if (hostname == NULL)
124   {
125     ppc->asc (ppc->asc_cls, NULL);
126     GNUNET_free (ppc);
127     return;
128   }
129
130 #if !BUILD_HTTPS
131   const char *protocol = "http";
132 #else
133   const char *protocol = "https";
134 #endif
135   GNUNET_assert ((strlen (hostname) + 7) < (INET6_ADDRSTRLEN + 13));
136   if (ppc->addrlen == sizeof (struct IPv6HttpAddress))
137   {
138     if (ppc->numeric == GNUNET_YES)
139       GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol, hostname, ppc->port);
140     else
141     {
142       if (strchr(hostname, ':') != NULL)
143         GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol, hostname, ppc->port);
144       else
145         GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, hostname, ppc->port);
146     }
147   }
148   else if (ppc->addrlen == sizeof (struct IPv4HttpAddress))
149     GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, hostname, ppc->port);
150   ppc->asc (ppc->asc_cls, rbuf);
151 }
152
153
154
155
156
157 /**
158  * Convert the transports address to a nice, human-readable
159  * format.
160  *
161  * @param cls closure
162  * @param type name of the transport that generated the address
163  * @param addr one of the addresses of the host, NULL for the last address
164  *        the specific address format depends on the transport
165  * @param addrlen length of the address
166  * @param numeric should (IP) addresses be displayed in numeric form?
167  * @param timeout after how long should we give up?
168  * @param asc function to call on each string
169  * @param asc_cls closure for asc
170  */
171 static void
172 http_plugin_address_pretty_printer (void *cls, const char *type,
173                                     const void *addr, size_t addrlen,
174                                     int numeric,
175                                     struct GNUNET_TIME_Relative timeout,
176                                     GNUNET_TRANSPORT_AddressStringCallback asc,
177                                     void *asc_cls)
178 {
179   GNUNET_assert (cls != NULL);
180   struct PrettyPrinterContext *ppc;
181   const void *sb;
182   struct sockaddr_in s4;
183   struct sockaddr_in6 s6;
184   size_t sbs;
185   uint16_t port = 0;
186
187   if ((addrlen == sizeof (struct IPv6HttpAddress))  && (addr != NULL))
188   {
189     struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
190     s6.sin6_family = AF_INET6;
191     s6.sin6_addr = a6->ipv6_addr;
192     s6.sin6_port = a6->u6_port;
193 #if HAVE_SOCKADDR_IN_SIN_LEN
194     s6.sin6_len = sizeof (struct sockaddr_in6);
195 #endif
196     sb = &s6;
197     sbs = sizeof (struct sockaddr_in6);
198     port = ntohs (a6->u6_port);
199
200   }
201   else if ((addrlen == sizeof (struct IPv4HttpAddress))  && (addr != NULL))
202   {
203     struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
204
205     s4.sin_family = AF_INET;
206     s4.sin_addr.s_addr = a4->ipv4_addr;
207     s4.sin_port = a4->u4_port;
208 #if HAVE_SOCKADDR_IN_SIN_LEN
209     s4.sin_len = sizeof (struct sockaddr_in);
210 #endif
211     sb = &s4;
212     sbs = sizeof (struct sockaddr_in);
213     port = ntohs (a4->u4_port);
214   }
215   else if (0 == addrlen)
216   {
217     asc (asc_cls, "<inbound connection>");
218     asc (asc_cls, NULL);
219     return;
220   }
221   else
222   {
223     /* invalid address */
224     GNUNET_break_op (0);
225     asc (asc_cls, NULL);
226     return;
227   }
228   ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
229   ppc->asc = asc;
230   ppc->asc_cls = asc_cls;
231   ppc->port = port;
232   ppc->plugin = cls;
233   ppc->addrlen = addrlen;
234   ppc->numeric = numeric;
235   GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
236 }
237
238
239
240 /**
241  * Another peer has suggested an address for this
242  * peer and transport plugin.  Check that this could be a valid
243  * address.  If so, consider adding it to the list
244  * of addresses.
245  *
246  * @param cls closure
247  * @param addr pointer to the address
248  * @param addrlen length of addr
249  * @return GNUNET_OK if this is a plausible address for this peer
250  *         and transport
251  */
252 static int
253 http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
254 {
255
256   struct Plugin *plugin = cls;
257   struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
258   struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
259
260
261
262   GNUNET_assert (cls != NULL);
263   if ((addrlen != sizeof (struct sockaddr_in)) ||
264       (addrlen != sizeof (struct sockaddr_in6)))
265     return GNUNET_SYSERR;
266
267   if (addrlen == sizeof (struct IPv4HttpAddress))
268   {
269     struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
270
271     while (w_tv4 != NULL)
272     {
273       if ((0 ==
274            memcmp (&w_tv4->addr.ipv4_addr, &a4->ipv4_addr,
275                    sizeof (struct in_addr))) &&
276           (w_tv4->addr.u4_port == a4->u4_port))
277         break;
278       w_tv4 = w_tv4->next;
279     }
280     if (w_tv4 != NULL)
281       return GNUNET_OK;
282     else
283       return GNUNET_SYSERR;
284   }
285   if (addrlen == sizeof (struct sockaddr_in6))
286   {
287     struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
288
289     while (w_tv6 != NULL)
290     {
291       if ((0 ==
292            memcmp (&w_tv6->addr6.ipv6_addr, &a6->ipv6_addr,
293                    sizeof (struct in6_addr))) &&
294           (w_tv6->addr6.u6_port == a6->u6_port))
295         break;
296       w_tv6 = w_tv6->next;
297     }
298     if (w_tv6 != NULL)
299       return GNUNET_OK;
300     else
301       return GNUNET_SYSERR;
302   }
303   return GNUNET_OK;
304 }
305
306 struct GNUNET_TIME_Relative
307 http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
308                      const struct GNUNET_MessageHeader *message,
309                      struct Session *session, const char *sender_address,
310                      uint16_t sender_address_len)
311 {
312   struct Session *s = cls;
313   struct Plugin *plugin = s->plugin;
314   struct GNUNET_TIME_Relative delay;
315   struct GNUNET_ATS_Information atsi[2];
316
317   atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
318   atsi[0].value = htonl (1);
319   atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
320   atsi[1].value = session->ats_address_network_type;
321   GNUNET_break (session->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
322
323   delay =
324       plugin->env->receive (plugin->env->cls, &s->target, message,
325                             (const struct GNUNET_ATS_Information *) &atsi,
326                             2, s, s->addr, s->addrlen);
327   return delay;
328 }
329
330
331 /**
332  * Function called to convert a string address to
333  * a binary address.
334  *
335  * @param cls closure ('struct Plugin*')
336  * @param addr string address
337  * @param addrlen length of the address
338  * @param buf location to store the buffer
339  *        If the function returns GNUNET_SYSERR, its contents are undefined.
340  * @param added length of created address
341  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
342  */
343 int http_string_to_address (void *cls,
344                            const char *addr,
345                            uint16_t addrlen,
346                            void **buf,
347                            size_t *added)
348 {
349 #if !BUILD_HTTPS
350   char *protocol = "http";
351 #else
352   char *protocol = "https";
353 #endif
354   char *addr_str = NULL;
355   struct sockaddr_in addr_4;
356   struct sockaddr_in6 addr_6;
357   struct IPv4HttpAddress * http_4addr;
358   struct IPv6HttpAddress * http_6addr;
359
360   if ((NULL == addr) || (addrlen == 0))
361   {
362     GNUNET_break (0);
363     return GNUNET_SYSERR;
364   }
365
366   if ('\0' != addr[addrlen - 1])
367   {
368     GNUNET_break (0);
369     return GNUNET_SYSERR;
370   }
371
372   if (strlen (addr) != addrlen - 1)
373   {
374     GNUNET_break (0);
375     return GNUNET_SYSERR;
376   }
377
378   /* protocoll + "://" + ":" */
379   if (addrlen <= (strlen (protocol) + 4))
380   {
381     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
382                      "Invalid address string `%s' to convert to address\n",
383                      addr);
384     GNUNET_break (0);
385     return GNUNET_SYSERR;
386   }
387
388   if (NULL == (addr_str = strstr(addr, "://")))
389   {
390     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
391                "Invalid address string `%s' to convert to address\n",
392                addr);
393     GNUNET_break (0);
394     return GNUNET_SYSERR;
395   }
396   addr_str = &addr_str[3];
397
398   if (addr_str[strlen(addr_str)-1] == '/')
399     addr_str[strlen(addr_str)-1] = '\0';
400
401   if (GNUNET_OK == GNUNET_STRINGS_to_address_ipv4(addr_str, strlen(addr_str), &addr_4))
402   {
403     http_4addr = GNUNET_malloc (sizeof (struct IPv4HttpAddress));
404     http_4addr->u4_port = addr_4.sin_port;
405     http_4addr->ipv4_addr = (uint32_t) addr_4.sin_addr.s_addr;
406     (*buf) = http_4addr;
407     (*added) = sizeof (struct IPv4HttpAddress);
408     return GNUNET_OK;
409   }
410   else if (GNUNET_OK == GNUNET_STRINGS_to_address_ipv6(addr_str, strlen(addr_str), &addr_6))
411   {
412     http_6addr = GNUNET_malloc (sizeof (struct IPv6HttpAddress));
413     http_6addr->u6_port = addr_6.sin6_port;
414     http_6addr->ipv6_addr = addr_6.sin6_addr;
415     (*buf) = http_6addr;
416     (*added) = sizeof (struct IPv6HttpAddress);
417     return GNUNET_OK;
418   }
419   else
420   {
421     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
422                      "Invalid address string `%s' to convert to address\n",
423                      addr_str);
424     GNUNET_break (0);
425     return GNUNET_SYSERR;
426   }
427 }
428
429
430
431 /**
432  * Function called for a quick conversion of the binary address to
433  * a numeric address.  Note that the caller must not free the
434  * address and that the next call to this function is allowed
435  * to override the address again.
436  *
437  * @param cls closure
438  * @param addr binary address
439  * @param addrlen length of the address
440  * @return string representing the same address
441  */
442 const char *
443 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
444 {
445
446   struct IPv4HttpAddress *a4;
447   struct IPv6HttpAddress *a6;
448   char *address;
449   static char rbuf[INET6_ADDRSTRLEN + 13];
450   uint16_t port;
451   int res = 0;
452
453   if (addrlen == sizeof (struct IPv6HttpAddress))
454   {
455     a6 = (struct IPv6HttpAddress *) addr;
456     address = GNUNET_malloc (INET6_ADDRSTRLEN);
457     GNUNET_assert (NULL !=
458                    inet_ntop (AF_INET6, &a6->ipv6_addr, address,
459                               INET6_ADDRSTRLEN));
460     port = ntohs (a6->u6_port);
461   }
462   else if (addrlen == sizeof (struct IPv4HttpAddress))
463   {
464     a4 = (struct IPv4HttpAddress *) addr;
465     address = GNUNET_malloc (INET_ADDRSTRLEN);
466     GNUNET_assert (NULL !=
467                    inet_ntop (AF_INET, &(a4->ipv4_addr), address,
468                               INET_ADDRSTRLEN));
469     port = ntohs (a4->u4_port);
470   }
471   else
472   {
473     /* invalid address */
474     GNUNET_break (0);
475     return NULL;
476   }
477 #if !BUILD_HTTPS
478   char *protocol = "http";
479 #else
480   char *protocol = "https";
481 #endif
482
483   GNUNET_assert (strlen (address) + 7 < (INET6_ADDRSTRLEN + 13));
484   if (addrlen == sizeof (struct IPv6HttpAddress))
485     res =
486         GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol,
487                          address, port);
488   else if (addrlen == sizeof (struct IPv4HttpAddress))
489     res =
490         GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, address,
491                          port);
492
493   GNUNET_free (address);
494   GNUNET_assert (res != 0);
495   return rbuf;
496 }
497
498 struct Session *
499 lookup_session_old (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
500                 struct Session *session, const void *addr, size_t addrlen,
501                 int force_address)
502 {
503   struct Session *t;
504   int e_peer;
505   int e_addr;
506
507   for (t = plugin->head; NULL != t; t = t->next)
508   {
509 #if 0
510     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
511                      "Comparing peer `%s' address `%s' len %i session %X to \n",
512                      GNUNET_i2s (target), GNUNET_a2s (addr, addrlen), addrlen,
513                      session);
514     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
515                      "peer `%s' address `%s' len %i session %X \n\n",
516                      GNUNET_i2s (&t->target), GNUNET_a2s (t->addr, t->addrlen),
517                      t->addrlen, t);
518     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, "memcmp %i \n",
519                      memcmp (addr, t->addr, addrlen));
520 #endif
521     e_peer = GNUNET_NO;
522     e_addr = GNUNET_NO;
523     if (0 == memcmp (target, &t->target, sizeof (struct GNUNET_PeerIdentity)))
524     {
525       e_peer = GNUNET_YES;
526       if ( (addrlen == t->addrlen) &&
527            (0 == memcmp (addr, t->addr, addrlen)) )
528         e_addr = GNUNET_YES;    
529       if ( (t == session) &&
530            (t->addrlen == session->addrlen) &&
531            (0 == memcmp (session->addr, t->addr, t->addrlen)) )
532         e_addr = GNUNET_YES;
533     }
534
535     if ( ((e_peer == GNUNET_YES) && (force_address == GNUNET_NO)) ||
536          ((e_peer == GNUNET_YES) && (force_address == GNUNET_YES) && (e_addr == GNUNET_YES)) ||
537          ((e_peer == GNUNET_YES) && (force_address == GNUNET_SYSERR)) )
538       return t;
539   }
540   return NULL;
541 }
542
543 struct Session *
544 lookup_session (struct Plugin *plugin,
545                 const struct GNUNET_HELLO_Address *address)
546 {
547   struct Session *pos;
548
549   for (pos = plugin->head; NULL != pos; pos = pos->next)
550     if ( (0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
551          (address->address_length == pos->addrlen) &&
552          (0 == memcmp (address->address, pos->addr, pos->addrlen)) )
553       return pos;
554   return NULL;
555 }
556
557 int
558 exist_session (struct Plugin *plugin, struct Session *s)
559 {
560   struct Session * head;
561
562   GNUNET_assert (NULL != plugin);
563   GNUNET_assert (NULL != s);
564
565   for (head = plugin->head; head != NULL; head = head->next)
566   {
567     if (head == s)
568       return GNUNET_YES;
569   }
570   return GNUNET_NO;
571 }
572
573
574
575 void
576 delete_session (struct Session *s)
577 {
578   if (s->msg_tk != NULL)
579   {
580     GNUNET_SERVER_mst_destroy (s->msg_tk);
581     s->msg_tk = NULL;
582   }
583   GNUNET_free (s->addr);
584   GNUNET_free_non_null (s->server_recv);
585   GNUNET_free_non_null (s->server_send);
586   GNUNET_free (s);
587 }
588
589 struct Session *
590 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
591                 const void *addr, size_t addrlen,
592                 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
593 {
594   struct Session *s = NULL;
595
596   GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) ||
597                  (addrlen == sizeof (struct IPv4HttpAddress)));
598   s = GNUNET_malloc (sizeof (struct Session));
599   memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
600   s->plugin = plugin;
601   s->addr = GNUNET_malloc (addrlen);
602   memcpy (s->addr, addr, addrlen);
603   s->addrlen = addrlen;
604   s->next = NULL;
605   s->next_receive = GNUNET_TIME_absolute_get_zero ();
606   s->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
607   return s;
608 }
609
610 void
611 notify_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
612                     struct Session *s)
613 {
614   struct Plugin *plugin = cls;
615
616   plugin->env->session_end (plugin->env->cls, peer, s);
617   GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
618   delete_session (s);
619 }
620
621 /**
622  * Creates a new outbound session the transport service will use to send data to the
623  * peer
624  *
625  * @param cls the plugin
626  * @param address the address
627  * @return the session or NULL of max connections exceeded
628  */
629
630 static struct Session *
631 http_get_session (void *cls,
632                   const struct GNUNET_HELLO_Address *address)
633 {
634   struct Plugin *plugin = cls;
635   struct Session * s = NULL;
636   struct GNUNET_ATS_Information ats;
637   size_t addrlen;
638
639   GNUNET_assert (plugin != NULL);
640   GNUNET_assert (address != NULL);
641   GNUNET_assert (address->address != NULL);
642
643   ats.type = htonl (GNUNET_ATS_ARRAY_TERMINATOR);
644   ats.value = htonl (GNUNET_ATS_ARRAY_TERMINATOR);
645
646   /* find existing session */
647   s = lookup_session (plugin, address);
648   if (s != NULL)
649     return s;
650
651   if (plugin->max_connections <= plugin->cur_connections)
652   {
653     GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
654                      "Maximum number of connections reached, "
655                      "cannot connect to peer `%s'\n", GNUNET_i2s (&address->peer));
656     return NULL;
657   }
658
659   /* create new session */
660   addrlen = address->address_length;
661
662   GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) ||
663                  (addrlen == sizeof (struct IPv4HttpAddress)));
664
665   s = GNUNET_malloc (sizeof (struct Session));
666   memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
667   s->plugin = plugin;
668   s->addr = GNUNET_malloc (address->address_length);
669   memcpy (s->addr, address->address, address->address_length);
670   s->addrlen = addrlen;
671   s->next = NULL;
672   s->next_receive = GNUNET_TIME_absolute_get_zero ();
673   s->inbound = GNUNET_NO;
674   s->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
675
676   /* Get ATS type */
677   if (addrlen == sizeof (struct IPv4HttpAddress))
678   {
679     struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) address->address;
680     struct sockaddr_in s4;
681
682     s4.sin_family = AF_INET;
683     s4.sin_addr.s_addr = a4->ipv4_addr;
684     s4.sin_port = a4->u4_port;
685 #if HAVE_SOCKADDR_IN_SIN_LEN
686     s4.sin_len = sizeof (struct sockaddr_in);
687 #endif
688     ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s4, sizeof (struct sockaddr_in));
689   }
690   if (addrlen == sizeof (struct IPv6HttpAddress))
691   {
692     struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) address->address;
693     struct sockaddr_in6 s6;
694
695     s6.sin6_family = AF_INET6;
696     s6.sin6_addr = a6->ipv6_addr;
697     s6.sin6_port = a6->u6_port;
698 #if HAVE_SOCKADDR_IN_SIN_LEN
699     s6.sin6_len = sizeof (struct sockaddr_in6);
700 #endif
701     ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s6, sizeof (struct sockaddr_in6));
702   }
703   s->ats_address_network_type = ats.value;
704
705   /* add new session */
706   GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
707   /* initiate new connection */
708   if (GNUNET_SYSERR == client_connect (s))
709   {
710     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
711                      "Cannot connect to peer `%s' address `%s''\n",
712                      http_plugin_address_to_string(NULL, s->addr, s->addrlen),
713                      GNUNET_i2s (&s->target));
714     GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
715     delete_session (s);
716     return NULL;
717   }
718
719   return s;
720 }
721
722 /**
723  * Function that can be used by the transport service to transmit
724  * a message using the plugin.   Note that in the case of a
725  * peer disconnecting, the continuation MUST be called
726  * prior to the disconnect notification itself.  This function
727  * will be called with this peer's HELLO message to initiate
728  * a fresh connection to another peer.
729  *
730  * @param cls closure
731  * @param session which session must be used
732  * @param msgbuf the message to transmit
733  * @param msgbuf_size number of bytes in 'msgbuf'
734  * @param priority how important is the message (most plugins will
735  *                 ignore message priority and just FIFO)
736  * @param to how long to wait at most for the transmission (does not
737  *                require plugins to discard the message after the timeout,
738  *                just advisory for the desired delay; most plugins will ignore
739  *                this as well)
740  * @param cont continuation to call once the message has
741  *        been transmitted (or if the transport is ready
742  *        for the next transmission call; or if the
743  *        peer disconnected...); can be NULL
744  * @param cont_cls closure for cont
745  * @return number of bytes used (on the physical network, with overheads);
746  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
747  *         and does NOT mean that the message was not transmitted (DV)
748  */
749 static ssize_t
750 http_plugin_send (void *cls,
751                   struct Session *session,
752                   const char *msgbuf, size_t msgbuf_size,
753                   unsigned int priority,
754                   struct GNUNET_TIME_Relative to,
755                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
756 {
757   struct Plugin *plugin = cls;
758   struct HTTP_Message *msg;
759   struct Session *tmp;
760   size_t res = -1;
761
762   GNUNET_assert (plugin != NULL);
763   GNUNET_assert (session != NULL);
764
765   /* lookup if session is really existing */
766   tmp = plugin->head;
767   while (tmp != NULL)
768   {
769     if ((tmp == session) &&
770        (0 == memcmp (&session->target, &tmp->target, sizeof (struct GNUNET_PeerIdentity))) &&
771        (session->addrlen == tmp->addrlen) &&
772        (0 == memcmp (session->addr, tmp->addr, tmp->addrlen)))
773       break;
774     tmp = tmp->next;
775   }
776   if (tmp == NULL)
777   {
778     GNUNET_break_op (0);
779     return res;
780   }
781
782   /* create new message and schedule */
783
784   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
785   msg->next = NULL;
786   msg->size = msgbuf_size;
787   msg->pos = 0;
788   msg->buf = (char *) &msg[1];
789   msg->transmit_cont = cont;
790   msg->transmit_cont_cls = cont_cls;
791   memcpy (msg->buf, msgbuf, msgbuf_size);
792
793   if (session->inbound == GNUNET_NO)
794   {
795     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
796                      "Using outbound client session %p to send to `%s'\n", session,
797                      GNUNET_i2s (&session->target));
798     client_send (session, msg);
799     res = msgbuf_size;
800   }
801   if (session->inbound == GNUNET_YES)
802   {
803     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
804                      "Using inbound server %p session to send to `%s'\n", session,
805                      GNUNET_i2s (&session->target));
806     server_send (session, msg);
807     res = msgbuf_size;
808   }
809   return res;
810
811 }
812
813
814 /**
815  * Function that can be used to force the plugin to disconnect
816  * from the given peer and cancel all previous transmissions
817  * (and their continuationc).
818  *
819  * @param cls closure
820  * @param target peer from which to disconnect
821  */
822 static void
823 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
824 {
825   struct Plugin *plugin = cls;
826   struct Session *next = NULL;
827   struct Session *s = plugin->head;
828
829   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
830                    "Transport tells me to disconnect `%s'\n",
831                    GNUNET_i2s (target));
832   while (s != NULL)
833   {
834     next = s->next;
835     if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
836     {
837       if (s->inbound == GNUNET_NO)
838         GNUNET_assert (GNUNET_OK == client_disconnect (s));
839       else
840         GNUNET_assert (GNUNET_OK == server_disconnect (s));
841       GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
842
843       struct HTTP_Message *msg = s->msg_head;
844       struct HTTP_Message *tmp = NULL;
845
846       while (msg != NULL)
847       {
848         tmp = msg->next;
849
850         GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
851         if (msg->transmit_cont != NULL)
852         {
853           msg->transmit_cont (msg->transmit_cont_cls, target, GNUNET_SYSERR);
854         }
855         GNUNET_free (msg);
856         msg = tmp;
857       }
858
859       delete_session (s);
860     }
861     s = next;
862   }
863 }
864
865 static void *
866 find_address (struct Plugin *plugin, const struct sockaddr *addr, socklen_t addrlen)
867 {
868   int af;
869   struct IPv4HttpAddressWrapper *w_t4 = NULL;
870   struct IPv6HttpAddressWrapper *w_t6 = NULL;
871
872   af = addr->sa_family;
873   switch (af)
874   {
875   case AF_INET:
876     w_t4 = plugin->ipv4_addr_head;
877     struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
878
879     while (w_t4 != NULL)
880     {
881       int res = memcmp (&w_t4->addr.ipv4_addr,
882                         &a4->sin_addr,
883                         sizeof (struct in_addr));
884
885       if (res == 0)
886       {
887         if (a4->sin_port != w_t4->addr.u4_port)
888           res = -1;
889       }
890
891       if (0 == res)
892         break;
893       w_t4 = w_t4->next;
894     }
895     return w_t4;
896     break;
897   case AF_INET6:
898     w_t6 = plugin->ipv6_addr_head;
899     struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
900
901     while (w_t6)
902     {
903       int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
904                         sizeof (struct in6_addr));
905
906       if (res == 0)
907       {
908         if (a6->sin6_port != w_t6->addr6.u6_port)
909           res = -1;
910       }
911       if (0 == res)
912         break;
913       w_t6 = w_t6->next;
914     }
915     return w_t6;
916     break;
917   default:
918     return NULL;
919   }
920   return NULL;
921 }
922
923 static void
924 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
925                  socklen_t addrlen)
926 {
927   struct Plugin *plugin = cls;
928   struct IPv4HttpAddressWrapper *w_t4 = NULL;
929   struct IPv6HttpAddressWrapper *w_t6 = NULL;
930   int af;
931
932   af = addr->sa_family;
933   switch (af)
934   {
935   case AF_INET:
936     w_t4 = find_address (plugin, addr, addrlen);
937     if (w_t4 == NULL)
938     {
939       struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
940       w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
941       memcpy (&w_t4->addr.ipv4_addr, &a4->sin_addr, sizeof (struct in_addr));
942       w_t4->addr.u4_port = a4->sin_port;
943
944       GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
945                                    plugin->ipv4_addr_tail, w_t4);
946
947     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
948                      "Notifying transport to add IPv4 address `%s'\n",
949                      http_plugin_address_to_string (NULL, &w_t4->addr,
950                                                     sizeof (struct
951                                                             IPv4HttpAddress)));
952     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
953                                  sizeof (struct IPv4HttpAddress));
954     }
955     break;
956   case AF_INET6:
957     w_t6 = find_address (plugin, addr, addrlen);
958     if (w_t6 == NULL)
959     {
960       w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
961       struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
962       memcpy (&w_t6->addr6.ipv6_addr, &a6->sin6_addr, sizeof (struct in6_addr));
963       w_t6->addr6.u6_port = a6->sin6_port;
964
965       GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
966                                    plugin->ipv6_addr_tail, w_t6);
967
968       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
969                      "Notifying transport to add IPv6 address `%s'\n",
970                      http_plugin_address_to_string (NULL, &w_t6->addr6,
971                                                     sizeof (struct
972                                                             IPv6HttpAddress)));
973       plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
974                                  sizeof (struct IPv6HttpAddress));
975     }
976     break;
977   default:
978     return;
979   }
980
981 }
982
983 static void
984 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
985                     socklen_t addrlen)
986 {
987   struct Plugin *plugin = cls;
988   struct IPv4HttpAddressWrapper *w_t4 = NULL;
989   struct IPv6HttpAddressWrapper *w_t6 = NULL;
990   int af;
991
992   af = addr->sa_family;
993   switch (af)
994   {
995   case AF_INET:
996     w_t4 = find_address (plugin, addr, addrlen);
997     if (w_t4 == NULL)
998       return;
999
1000
1001     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1002                      "Notifying transport to remove IPv4 address `%s'\n",
1003                      http_plugin_address_to_string (NULL, &w_t4->addr,
1004                                                     sizeof (struct
1005                                                             IPv4HttpAddress)));
1006     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
1007                                  sizeof (struct IPv4HttpAddress));
1008
1009     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
1010                                  w_t4);
1011     GNUNET_free (w_t4);
1012     break;
1013   case AF_INET6:
1014     w_t6 = find_address (plugin, addr, addrlen);
1015     if (w_t6 == NULL)
1016       return;
1017
1018     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1019                      "Notifying transport to remove IPv6 address `%s'\n",
1020                      http_plugin_address_to_string (NULL, &w_t6->addr6,
1021                                                     sizeof (struct
1022                                                             IPv6HttpAddress)));
1023
1024     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
1025                                  sizeof (struct IPv6HttpAddress));
1026
1027     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1028                                  w_t6);
1029     GNUNET_free (w_t6);
1030     break;
1031   default:
1032     return;
1033   }
1034
1035 }
1036
1037 /**
1038  * Our external IP address/port mapping has changed.
1039  *
1040  * @param cls closure, the 'struct LocalAddrList'
1041  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1042  *     the previous (now invalid) one
1043  * @param addr either the previous or the new public IP address
1044  * @param addrlen actual lenght of the address
1045  */
1046 static void
1047 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
1048                        socklen_t addrlen)
1049 {
1050   GNUNET_assert (cls != NULL);
1051   struct Plugin *plugin = cls;
1052
1053   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1054                    "NPMC called %s to address `%s'\n",
1055                    (add_remove == GNUNET_NO) ? "remove" : "add",
1056                    GNUNET_a2s (addr, addrlen));
1057
1058   switch (add_remove)
1059   {
1060   case GNUNET_YES:
1061     nat_add_address (cls, add_remove, addr, addrlen);
1062     break;
1063   case GNUNET_NO:
1064     nat_remove_address (cls, add_remove, addr, addrlen);
1065     break;
1066   }
1067 }
1068
1069 void
1070 http_check_ipv6 (struct Plugin *plugin)
1071 {
1072   struct GNUNET_NETWORK_Handle *desc = NULL;
1073
1074   if (plugin->ipv6 == GNUNET_YES)
1075   {
1076     /* probe IPv6 support */
1077     desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
1078     if (NULL == desc)
1079     {
1080       if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
1081           (errno == EACCES))
1082       {
1083         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1084       }
1085       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1086                        _
1087                        ("Disabling IPv6 since it is not supported on this system!\n"));
1088       plugin->ipv6 = GNUNET_NO;
1089     }
1090     else
1091     {
1092       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
1093       desc = NULL;
1094     }
1095
1096     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1097                      "Testing IPv6 on this system: %s\n",
1098                      (plugin->ipv6 == GNUNET_YES) ? "successful" : "failed");
1099   }
1100 }
1101
1102 int
1103 http_get_addresses (struct Plugin *plugin, const char *serviceName,
1104                     const struct GNUNET_CONFIGURATION_Handle *cfg,
1105                     struct sockaddr ***addrs, socklen_t ** addr_lens)
1106 {
1107   int disablev6;
1108   unsigned long long port;
1109   struct addrinfo hints;
1110   struct addrinfo *res;
1111   struct addrinfo *pos;
1112   struct addrinfo *next;
1113   unsigned int i;
1114   int resi;
1115   int ret;
1116   struct sockaddr **saddrs;
1117   socklen_t *saddrlens;
1118   char *hostname;
1119
1120   *addrs = NULL;
1121   *addr_lens = NULL;
1122
1123   disablev6 = !plugin->ipv6;
1124
1125   port = 0;
1126   if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
1127   {
1128     GNUNET_break (GNUNET_OK ==
1129                   GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
1130                                                          "PORT", &port));
1131     if (port > 65535)
1132     {
1133       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1134                   _
1135                   ("Require valid port number for service in configuration!\n"));
1136       return GNUNET_SYSERR;
1137     }
1138   }
1139
1140   if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
1141   {
1142     GNUNET_break (GNUNET_OK ==
1143                   GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
1144                                                          "BINDTO", &hostname));
1145   }
1146   else
1147     hostname = NULL;
1148
1149   if (hostname != NULL)
1150   {
1151     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1152                      "Resolving `%s' since that is where `%s' will bind to.\n",
1153                      hostname, serviceName);
1154     memset (&hints, 0, sizeof (struct addrinfo));
1155     if (disablev6)
1156       hints.ai_family = AF_INET;
1157     if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1158         (res == NULL))
1159     {
1160       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
1161                   hostname, gai_strerror (ret));
1162       GNUNET_free (hostname);
1163       return GNUNET_SYSERR;
1164     }
1165     next = res;
1166     i = 0;
1167     while (NULL != (pos = next))
1168     {
1169       next = pos->ai_next;
1170       if ((disablev6) && (pos->ai_family == AF_INET6))
1171         continue;
1172       i++;
1173     }
1174     if (0 == i)
1175     {
1176       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1177                   _("Failed to find %saddress for `%s'.\n"),
1178                   disablev6 ? "IPv4 " : "", hostname);
1179       freeaddrinfo (res);
1180       GNUNET_free (hostname);
1181       return GNUNET_SYSERR;
1182     }
1183     resi = i;
1184     saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1185     saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1186     i = 0;
1187     next = res;
1188     while (NULL != (pos = next))
1189     {
1190       next = pos->ai_next;
1191       if ((disablev6) && (pos->ai_family == AF_INET6))
1192         continue;
1193       if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
1194         continue;               /* not TCP */
1195       if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
1196         continue;               /* huh? */
1197       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1198                        "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
1199                                                                   pos->ai_addrlen));
1200       if (pos->ai_family == AF_INET)
1201       {
1202         GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
1203         saddrlens[i] = pos->ai_addrlen;
1204         saddrs[i] = GNUNET_malloc (saddrlens[i]);
1205         memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1206         ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1207       }
1208       else
1209       {
1210         GNUNET_assert (pos->ai_family == AF_INET6);
1211         GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
1212         saddrlens[i] = pos->ai_addrlen;
1213         saddrs[i] = GNUNET_malloc (saddrlens[i]);
1214         memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1215         ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1216       }
1217       i++;
1218     }
1219     GNUNET_free (hostname);
1220     freeaddrinfo (res);
1221     resi = i;
1222   }
1223   else
1224   {
1225     /* will bind against everything, just set port */
1226     if (disablev6)
1227     {
1228       /* V4-only */
1229       resi = 1;
1230       i = 0;
1231       saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1232       saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1233
1234       saddrlens[i] = sizeof (struct sockaddr_in);
1235       saddrs[i] = GNUNET_malloc (saddrlens[i]);
1236 #if HAVE_SOCKADDR_IN_SIN_LEN
1237       ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1238 #endif
1239       ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1240       ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1241     }
1242     else
1243     {
1244       /* dual stack */
1245       resi = 2;
1246       saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1247       saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1248       i = 0;
1249       saddrlens[i] = sizeof (struct sockaddr_in6);
1250       saddrs[i] = GNUNET_malloc (saddrlens[i]);
1251 #if HAVE_SOCKADDR_IN_SIN_LEN
1252       ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1253 #endif
1254       ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1255       ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1256       i++;
1257       saddrlens[i] = sizeof (struct sockaddr_in);
1258       saddrs[i] = GNUNET_malloc (saddrlens[i]);
1259 #if HAVE_SOCKADDR_IN_SIN_LEN
1260       ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1261 #endif
1262       ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1263       ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1264     }
1265   }
1266   *addrs = saddrs;
1267   *addr_lens = saddrlens;
1268   return resi;
1269 }
1270
1271 static void
1272 start_report_addresses (struct Plugin *plugin)
1273 {
1274   int res = GNUNET_OK;
1275   struct sockaddr **addrs;
1276   socklen_t *addrlens;
1277
1278   res =
1279       http_get_addresses (plugin, plugin->name, plugin->env->cfg, &addrs,
1280                           &addrlens);
1281   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1282                    _("Found %u addresses to report to NAT service\n"), res);
1283
1284   if (res != GNUNET_SYSERR)
1285   {
1286     plugin->nat =
1287         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
1288                              (unsigned int) res,
1289                              (const struct sockaddr **) addrs, addrlens,
1290                              &nat_port_map_callback, NULL, plugin);
1291     while (res > 0)
1292     {
1293       res--;
1294 #if 0
1295       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, _("FREEING %s\n"),
1296                        GNUNET_a2s (addrs[res], addrlens[res]));
1297 #endif
1298       GNUNET_assert (addrs[res] != NULL);
1299       GNUNET_free (addrs[res]);
1300     }
1301     GNUNET_free_non_null (addrs);
1302     GNUNET_free_non_null (addrlens);
1303   }
1304   else
1305   {
1306     plugin->nat =
1307         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
1308                              NULL, NULL, plugin);
1309   }
1310 }
1311
1312 static void
1313 stop_report_addresses (struct Plugin *plugin)
1314 {
1315   /* Stop NAT handle */
1316   GNUNET_NAT_unregister (plugin->nat);
1317
1318   /* Clean up addresses */
1319   struct IPv4HttpAddressWrapper *w_t4;
1320   struct IPv6HttpAddressWrapper *w_t6;
1321
1322   while (plugin->ipv4_addr_head != NULL)
1323   {
1324     w_t4 = plugin->ipv4_addr_head;
1325     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
1326                                  w_t4);
1327     GNUNET_free (w_t4);
1328   }
1329
1330   while (plugin->ipv6_addr_head != NULL)
1331   {
1332     w_t6 = plugin->ipv6_addr_head;
1333     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1334                                  w_t6);
1335     GNUNET_free (w_t6);
1336   }
1337 }
1338
1339 static int
1340 configure_plugin (struct Plugin *plugin)
1341 {
1342   int res = GNUNET_OK;
1343
1344   /* Use IPv4? */
1345   if (GNUNET_CONFIGURATION_have_value
1346       (plugin->env->cfg, plugin->name, "USE_IPv4"))
1347   {
1348     plugin->ipv4 =
1349         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1350                                               "USE_IPv4");
1351   }
1352   else
1353     plugin->ipv4 = GNUNET_YES;
1354
1355   /* Use IPv6? */
1356   if (GNUNET_CONFIGURATION_have_value
1357       (plugin->env->cfg, plugin->name, "USE_IPv6"))
1358   {
1359     plugin->ipv6 =
1360         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1361                                               "USE_IPv6");
1362   }
1363   else
1364     plugin->ipv6 = GNUNET_YES;
1365
1366   if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
1367   {
1368     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1369                      _
1370                      ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
1371                      plugin->name);
1372     res = GNUNET_SYSERR;
1373   }
1374
1375   /* Reading port number from config file */
1376   unsigned long long port;
1377
1378   if ((GNUNET_OK !=
1379        GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1380                                               "PORT", &port)) || (port > 65535))
1381   {
1382     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1383                      _("Port is required! Fix in configuration\n"),
1384                      plugin->name);
1385     res = GNUNET_SYSERR;
1386     goto fail;
1387   }
1388   plugin->port = port;
1389
1390   plugin->client_only = GNUNET_NO;
1391   if (plugin->port == 0)
1392   {
1393     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1394                      _("Port 0, client only mode\n"));
1395     plugin->client_only = GNUNET_YES;
1396   }
1397
1398   char *bind4_address = NULL;
1399
1400   if ((plugin->ipv4 == GNUNET_YES) &&
1401       (GNUNET_YES ==
1402        GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1403                                               "BINDTO", &bind4_address)))
1404   {
1405     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1406                      "Binding %s plugin to specific IPv4 address: `%s'\n",
1407                      plugin->protocol, bind4_address);
1408     plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1409     if (1 !=
1410         inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
1411     {
1412       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1413                        _
1414                        ("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1415                        bind4_address, plugin->protocol);
1416       GNUNET_free (plugin->server_addr_v4);
1417       plugin->server_addr_v4 = NULL;
1418     }
1419     else
1420     {
1421       plugin->server_addr_v4->sin_family = AF_INET;
1422       plugin->server_addr_v4->sin_port = htons (plugin->port);
1423     }
1424     GNUNET_free (bind4_address);
1425   }
1426
1427
1428   char *bind6_address = NULL;
1429
1430   if ((plugin->ipv6 == GNUNET_YES) &&
1431       (GNUNET_YES ==
1432        GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1433                                               "BINDTO6", &bind6_address)))
1434   {
1435     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1436                      "Binding %s plugin to specific IPv6 address: `%s'\n",
1437                      plugin->protocol, bind6_address);
1438     plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1439     if (1 !=
1440         inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
1441     {
1442       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1443                        _
1444                        ("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1445                        bind6_address, plugin->protocol);
1446       GNUNET_free (plugin->server_addr_v6);
1447       plugin->server_addr_v6 = NULL;
1448     }
1449     else
1450     {
1451       plugin->server_addr_v6->sin6_family = AF_INET6;
1452       plugin->server_addr_v6->sin6_port = htons (plugin->port);
1453     }
1454     GNUNET_free (bind6_address);
1455   }
1456
1457
1458   /* Optional parameters */
1459   unsigned long long maxneigh;
1460
1461   if (GNUNET_OK !=
1462       GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1463                                              "MAX_CONNECTIONS", &maxneigh))
1464     maxneigh = 128;
1465   plugin->max_connections = maxneigh;
1466
1467 fail:
1468   return res;
1469 }
1470
1471 /**
1472  * Entry point for the plugin.
1473  */
1474 void *
1475 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1476 {
1477   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1478   struct GNUNET_TRANSPORT_PluginFunctions *api;
1479   struct Plugin *plugin;
1480   int res;
1481
1482   if (NULL == env->receive)
1483   {
1484     /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1485        initialze the plugin or the API */
1486     api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1487     api->cls = NULL;
1488     api->address_pretty_printer = &http_plugin_address_pretty_printer;
1489     api->address_to_string = &http_plugin_address_to_string;
1490     api->string_to_address = &http_string_to_address;
1491     return api;
1492   }
1493
1494   plugin = GNUNET_malloc (sizeof (struct Plugin));
1495   plugin->env = env;
1496   plugin->outbound_sessions = 0;
1497   plugin->inbound_sessions = 0;
1498   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1499   api->cls = plugin;
1500   api->disconnect = &http_plugin_disconnect;
1501   api->address_pretty_printer = &http_plugin_address_pretty_printer;
1502   api->check_address = &http_plugin_address_suggested;
1503   api->address_to_string = &http_plugin_address_to_string;
1504   api->string_to_address = &http_string_to_address;
1505   api->get_session = &http_get_session;
1506   api->send = &http_plugin_send;
1507
1508 #if BUILD_HTTPS
1509   plugin->name = "transport-https";
1510   plugin->protocol = "https";
1511 #else
1512   plugin->name = "transport-http";
1513   plugin->protocol = "http";
1514 #endif
1515   /* Configure plugin from configuration */
1516   res = configure_plugin (plugin);
1517   if (res == GNUNET_SYSERR)
1518   {
1519     GNUNET_free_non_null (plugin->server_addr_v4);
1520     GNUNET_free_non_null (plugin->server_addr_v6);
1521     GNUNET_free (plugin);
1522     GNUNET_free (api);
1523     return NULL;
1524   }
1525
1526   /* checking IPv6 support */
1527   http_check_ipv6 (plugin);
1528
1529   /* Start client */
1530   res = client_start (plugin);
1531   if (res == GNUNET_SYSERR)
1532   {
1533     GNUNET_free_non_null (plugin->server_addr_v4);
1534     GNUNET_free_non_null (plugin->server_addr_v6);
1535     GNUNET_free (plugin);
1536     GNUNET_free (api);
1537     return NULL;
1538   }
1539
1540   /* Start server */
1541   if (plugin->client_only == GNUNET_NO)
1542   {
1543     res = server_start (plugin);
1544     if (res == GNUNET_SYSERR)
1545     {
1546       server_stop (plugin);
1547       client_stop (plugin);
1548
1549       GNUNET_free_non_null (plugin->server_addr_v4);
1550       GNUNET_free_non_null (plugin->server_addr_v6);
1551       GNUNET_free (plugin);
1552       GNUNET_free (api);
1553       return NULL;
1554     }
1555   }
1556   /* Report addresses to transport service */
1557   start_report_addresses (plugin);
1558
1559   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1560                    "Plugin `%s' loaded\n", plugin->name);
1561   return api;
1562 }
1563
1564
1565 /**
1566  * Exit point from the plugin.
1567  */
1568 void *
1569 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1570 {
1571   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1572   struct Plugin *plugin = api->cls;
1573   struct Session *s;
1574
1575   if (NULL == plugin)
1576   {
1577     GNUNET_free (api);
1578     return NULL;
1579   }
1580
1581   /* Stop reporting addresses to transport service */
1582   stop_report_addresses (plugin);
1583
1584   /* cleaning up sessions */
1585   s = plugin->head;
1586   while (s != NULL)
1587   {
1588     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1589                      "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1590     if (s->inbound == GNUNET_NO)
1591       GNUNET_assert (GNUNET_OK == client_disconnect (s));
1592     else
1593       GNUNET_assert (GNUNET_OK == server_disconnect (s));
1594     s = s->next;
1595   }
1596
1597   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping server\n");
1598   /* Stop server */
1599   server_stop (plugin);
1600
1601   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping client\n");
1602   /* Stop client */
1603   client_stop (plugin);
1604
1605   /* deleting up sessions */
1606   s = plugin->head;
1607   while (s != NULL)
1608   {
1609     struct Session *t = s->next;
1610
1611     GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
1612
1613     struct HTTP_Message *msg = s->msg_head;
1614     struct HTTP_Message *tmp = NULL;
1615
1616     while (msg != NULL)
1617     {
1618       tmp = msg->next;
1619
1620       GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1621       if (msg->transmit_cont != NULL)
1622       {
1623         msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
1624       }
1625       GNUNET_free (msg);
1626       msg = tmp;
1627     }
1628
1629     delete_session (s);
1630     s = t;
1631   }
1632
1633   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1634                    "Plugin `%s' unloaded\n", plugin->name);
1635   GNUNET_free_non_null (plugin->server_addr_v4);
1636   GNUNET_free_non_null (plugin->server_addr_v6);
1637   GNUNET_free (plugin);
1638   GNUNET_free (api);
1639   return NULL;
1640 }
1641
1642 /* end of plugin_transport_http.c */