move GNUNET_TRANSPORT_ATS_ to GNUNET_ATS_
[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
99
100 /**
101  * Encapsulation of all of the state of the plugin.
102  */
103 struct Plugin;
104
105
106
107 /**
108  * Append our port and forward the result.
109  *
110  * @param cls the 'struct PrettyPrinterContext*'
111  * @param hostname hostname part of the address
112  */
113 static void
114 append_port (void *cls, const char *hostname)
115 {
116   struct PrettyPrinterContext *ppc = cls;
117   char *ret;
118
119   if (hostname == NULL)
120   {
121     ppc->asc (ppc->asc_cls, NULL);
122     GNUNET_free (ppc);
123     return;
124   }
125   GNUNET_asprintf (&ret, "%s://%s:%d", ppc->plugin->protocol, hostname,
126                    ppc->plugin->port);
127   ppc->asc (ppc->asc_cls, ret);
128   GNUNET_free (ret);
129 }
130
131
132 /**
133  * Convert the transports address to a nice, human-readable
134  * format.
135  *
136  * @param cls closure
137  * @param type name of the transport that generated the address
138  * @param addr one of the addresses of the host, NULL for the last address
139  *        the specific address format depends on the transport
140  * @param addrlen length of the address
141  * @param numeric should (IP) addresses be displayed in numeric form?
142  * @param timeout after how long should we give up?
143  * @param asc function to call on each string
144  * @param asc_cls closure for asc
145  */
146 static void
147 http_plugin_address_pretty_printer (void *cls, const char *type,
148                                     const void *addr, size_t addrlen,
149                                     int numeric,
150                                     struct GNUNET_TIME_Relative timeout,
151                                     GNUNET_TRANSPORT_AddressStringCallback asc,
152                                     void *asc_cls)
153 {
154   GNUNET_assert (cls != NULL);
155   struct PrettyPrinterContext *ppc;
156   const void *sb;
157   size_t sbs;
158   uint16_t port = 0 ;
159
160   if (addrlen == sizeof (struct IPv6HttpAddress))
161   {
162     struct IPv6HttpAddress * a6 = (struct IPv6HttpAddress *) addr;
163     sb = &a6->ipv6_addr;
164     sbs = sizeof (struct in6_addr);
165     port = ntohs (a6->u6_port);
166   }
167   else if (addrlen == sizeof (struct IPv4HttpAddress))
168   {
169     struct IPv4HttpAddress * a4 = (struct IPv4HttpAddress *) addr;
170     sb = &a4->ipv4_addr;
171     sbs = sizeof (struct in_addr);
172     port = ntohs (a4->u4_port);
173   }
174   else
175   {
176     /* invalid address */
177     GNUNET_break_op (0);
178     asc (asc_cls, NULL);
179     return;
180   }
181   ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
182   ppc->asc = asc;
183   ppc->asc_cls = asc_cls;
184   ppc->port = port;
185   ppc->plugin = cls;
186   GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
187 }
188
189
190
191 /**
192  * Another peer has suggested an address for this
193  * peer and transport plugin.  Check that this could be a valid
194  * address.  If so, consider adding it to the list
195  * of addresses.
196  *
197  * @param cls closure
198  * @param addr pointer to the address
199  * @param addrlen length of addr
200  * @return GNUNET_OK if this is a plausible address for this peer
201  *         and transport
202  */
203 static int
204 http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
205 {
206
207   struct Plugin *plugin = cls;
208   struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
209   struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
210
211
212
213   GNUNET_assert (cls != NULL);
214   if ((addrlen != sizeof (struct sockaddr_in)) ||
215       (addrlen != sizeof (struct sockaddr_in6)))
216     return GNUNET_SYSERR;
217
218   if (addrlen == sizeof (struct IPv4HttpAddress))
219   {
220     struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
221     while (w_tv4 != NULL)
222     {
223       if ((0 == memcmp (&w_tv4->addr.ipv4_addr, &a4->ipv4_addr, sizeof (struct in_addr))) &&
224           (w_tv4->addr.u4_port == a4->u4_port))
225         break;
226       w_tv4 = w_tv4->next;
227     }
228     if (w_tv4 != NULL)
229       return GNUNET_OK;
230     else
231       return GNUNET_SYSERR;
232   }
233   if (addrlen == sizeof (struct sockaddr_in6))
234   {
235     struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
236     while (w_tv6 != NULL)
237     {
238       if ((0 == memcmp (&w_tv6->addr6.ipv6_addr, &a6->ipv6_addr, sizeof (struct in6_addr))) &&
239           (w_tv6->addr6.u6_port == a6->u6_port))
240         break;
241       w_tv6 = w_tv6->next;
242     }
243     if (w_tv6 != NULL)
244       return GNUNET_OK;
245     else
246       return GNUNET_SYSERR;
247   }
248   return GNUNET_OK;
249 }
250
251 struct GNUNET_TIME_Relative
252 http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity * peer,
253     const struct  GNUNET_MessageHeader * message,
254     struct Session * session,
255     const char *sender_address,
256     uint16_t sender_address_len)
257 {
258   struct Session *s = cls;
259   struct Plugin *plugin = s->plugin;
260   struct GNUNET_ATS_Information distance;
261   struct GNUNET_TIME_Relative delay;
262
263   distance.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
264   distance.value = htonl (1);
265
266   delay = plugin->env->receive (plugin->env->cls, &s->target, message, (const struct GNUNET_ATS_Information*) &distance, 1, s, s->addr, s->addrlen);
267   return delay;
268 }
269
270 /**
271  * Function called for a quick conversion of the binary address to
272  * a numeric address.  Note that the caller must not free the
273  * address and that the next call to this function is allowed
274  * to override the address again.
275  *
276  * @param cls closure
277  * @param addr binary address
278  * @param addrlen length of the address
279  * @return string representing the same address
280  */
281 const char *
282 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
283 {
284
285   struct IPv4HttpAddress *a4;
286   struct IPv6HttpAddress *a6;
287   char *address;
288   static char rbuf[INET6_ADDRSTRLEN + 13];
289   uint16_t port;
290   int res = 0;
291
292   if (addrlen == sizeof (struct IPv6HttpAddress))
293   {
294     a6 = (struct IPv6HttpAddress *) addr;
295     address = GNUNET_malloc (INET6_ADDRSTRLEN);
296     GNUNET_assert(NULL != inet_ntop (AF_INET6, &a6->ipv6_addr, address, INET6_ADDRSTRLEN));
297     port = ntohs (a6->u6_port);
298   }
299   else if (addrlen == sizeof (struct IPv4HttpAddress))
300   {
301     a4 = (struct IPv4HttpAddress *) addr;
302     address = GNUNET_malloc (INET_ADDRSTRLEN);
303     GNUNET_assert(NULL != inet_ntop (AF_INET, &(a4->ipv4_addr), address, INET_ADDRSTRLEN));
304     port = ntohs (a4->u4_port);
305   }
306   else
307   {
308     /* invalid address */
309     GNUNET_break (0);
310     return NULL;
311   }
312 #if !BUILD_HTTPS  
313   char * protocol = "http";
314 #else
315   char * protocol = "https";
316 #endif
317
318   GNUNET_assert (strlen (address) + 7 < (INET6_ADDRSTRLEN + 13));
319   if (addrlen == sizeof (struct IPv6HttpAddress))
320     res = GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol, address, port);
321   else if (addrlen == sizeof (struct IPv4HttpAddress))
322     res = GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, address, port);
323
324   GNUNET_free (address);
325   GNUNET_assert (res != 0);
326   return rbuf;
327 }
328
329 struct Session *
330 lookup_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
331                 struct Session * session,
332                 const void *addr, size_t addrlen, int force_address)
333 {
334   struct Session *s = NULL;
335   struct Session *t = NULL;
336   int e_peer;
337   int e_addr;
338
339   t = plugin->head;
340   if (t == NULL)
341     return NULL;
342   while (t != NULL)
343   {
344 #if 0
345     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
346                      "Comparing peer `%s' address `%s' len %i session %X to \n", GNUNET_i2s(target), GNUNET_a2s(addr,addrlen), addrlen, session);
347     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,"peer `%s' address `%s' len %i session %X \n\n", GNUNET_i2s(&t->target), GNUNET_a2s(t->addr,t->addrlen), t->addrlen, t);
348     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,"memcmp %i \n", memcmp (addr, t->addr, addrlen));
349 #endif
350     e_peer = GNUNET_NO;
351     e_addr = GNUNET_NO;
352
353     if (0 == memcmp (target, &t->target, sizeof (struct GNUNET_PeerIdentity)))
354     {
355       e_peer = GNUNET_YES;
356       if (addrlen == t->addrlen)
357       {
358         if (0 == memcmp (addr, t->addr, addrlen))
359         {
360           e_addr = GNUNET_YES;
361         }
362       }
363       if ((t == session))
364       {
365        if(t->addrlen == session->addrlen)
366        {
367         if (0 == memcmp (session->addr, t->addr, t->addrlen))
368         {
369           e_addr = GNUNET_YES;
370         }
371        }
372       }
373     }
374
375     if ((e_peer == GNUNET_YES) && (force_address == GNUNET_NO))
376     {
377       s = t;
378       break;
379     }
380     if ((e_peer == GNUNET_YES) && (force_address == GNUNET_YES) && (e_addr == GNUNET_YES))
381     {
382       s = t;
383       break;
384     }
385     if ((e_peer == GNUNET_YES) && (force_address == GNUNET_SYSERR))
386     {
387       s = t;
388       break;
389     }
390     if (s != NULL)
391       break;
392     t = t->next;
393   }
394
395
396   return s;
397 }
398
399 void
400 delete_session (struct Session *s)
401 {
402   if (s->msg_tk != NULL)
403   {
404     GNUNET_SERVER_mst_destroy (s->msg_tk);
405     s->msg_tk = NULL;
406   }
407   GNUNET_free (s->addr);
408   GNUNET_free_non_null(s->server_recv);
409   GNUNET_free_non_null(s->server_send);
410   GNUNET_free (s);
411 }
412
413 struct Session *
414 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
415                 const void *addr, size_t addrlen,
416                 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
417 {
418   struct Session *s = NULL;
419   GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) || (addrlen == sizeof (struct IPv4HttpAddress)));
420   s = GNUNET_malloc (sizeof (struct Session));
421   memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
422   s->plugin = plugin;
423   s->addr = GNUNET_malloc (addrlen);
424   memcpy (s->addr, addr, addrlen);
425   s->addrlen = addrlen;
426   s->next = NULL;
427   s->next_receive = GNUNET_TIME_absolute_get_zero();
428   return s;
429 }
430
431 void
432 notify_session_end (void *cls,
433                     const struct GNUNET_PeerIdentity *
434                     peer, struct Session * s)
435 {
436   struct Plugin *plugin = cls;
437
438   plugin->env->session_end (NULL, peer, s);
439   GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
440   delete_session (s);
441 }
442
443
444 /**
445  * Function that can be used by the transport service to transmit
446  * a message using the plugin.   Note that in the case of a
447  * peer disconnecting, the continuation MUST be called
448  * prior to the disconnect notification itself.  This function
449  * will be called with this peer's HELLO message to initiate
450  * a fresh connection to another peer.
451  *
452  * @param cls closure
453  * @param target who should receive this message
454  * @param msgbuf the message to transmit
455  * @param msgbuf_size number of bytes in 'msgbuf'
456  * @param priority how important is the message (most plugins will
457  *                 ignore message priority and just FIFO)
458  * @param to how long to wait at most for the transmission (does not
459  *                require plugins to discard the message after the timeout,
460  *                just advisory for the desired delay; most plugins will ignore
461  *                this as well)
462  * @param session which session must be used (or NULL for "any")
463  * @param addr the address to use (can be NULL if the plugin
464  *                is "on its own" (i.e. re-use existing TCP connection))
465  * @param addrlen length of the address in bytes
466  * @param force_address GNUNET_YES if the plugin MUST use the given address,
467  *                GNUNET_NO means the plugin may use any other address and
468  *                GNUNET_SYSERR means that only reliable existing
469  *                bi-directional connections should be used (regardless
470  *                of address)
471  * @param cont continuation to call once the message has
472  *        been transmitted (or if the transport is ready
473  *        for the next transmission call; or if the
474  *        peer disconnected...); can be NULL
475  * @param cont_cls closure for cont
476  * @return number of bytes used (on the physical network, with overheads);
477  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
478  *         and does NOT mean that the message was not transmitted (DV)
479  */
480 static ssize_t
481 http_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
482                   const char *msgbuf, size_t msgbuf_size, unsigned int priority,
483                   struct GNUNET_TIME_Relative to, struct Session *session,
484                   const void *addr, size_t addrlen, int force_address,
485                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
486 {
487   struct Plugin *plugin = cls;
488   struct HTTP_Message *msg;
489   GNUNET_assert (plugin != NULL);
490
491   int res = GNUNET_SYSERR;
492
493 #if DEBUG_HTTP
494   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
495                    "Sending %u bytes to peer `%s' on address `%s' %X %i\n", msgbuf_size,
496                    GNUNET_i2s (target), ((addr != NULL) && (addrlen != 0)) ? http_plugin_address_to_string(plugin, addr, addrlen) : "<inbound>", session, force_address);
497 #endif
498
499   struct Session *s = NULL;
500
501
502   if (addrlen != 0)
503      GNUNET_assert ((addrlen == sizeof (struct IPv4HttpAddress)) ||
504                     (addrlen == sizeof (struct IPv6HttpAddress)));
505
506   /* look for existing connection */
507   s = lookup_session (plugin, target, session, addr, addrlen, 1);
508 #if DEBUG_HTTP
509   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
510                    "%s existing session: %s\n", (s!=NULL) ? "Found" : "NOT Found", ((s != NULL) && (s->inbound == GNUNET_YES)) ? "inbound" : "outbound");
511 #endif
512
513   /* create new outbound connection */
514   if (s == NULL)
515   {
516     if (plugin->max_connections <= plugin->cur_connections)
517     {
518       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
519                        "Maximum number of connections reached, "
520                        "cannot connect to peer `%s'\n",
521                        GNUNET_i2s (target));
522       return res;
523     }
524
525 #if DEBUG_HTTP
526     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
527                      "Initiiating new connection to peer `%s'\n",
528                      GNUNET_i2s (target));
529 #endif
530     s = create_session (plugin, target, addr, addrlen, cont, cont_cls);
531     GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
532     // initiate new connection
533     if (GNUNET_SYSERR == (res = client_connect (s)))
534     {
535       GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
536       delete_session (s);
537       return GNUNET_SYSERR;
538     }
539   }
540
541   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
542   msg->next = NULL;
543   msg->size = msgbuf_size;
544   msg->pos = 0;
545   msg->buf = (char *) &msg[1];
546   msg->transmit_cont = cont;
547   msg->transmit_cont_cls = cont_cls;
548   memcpy (msg->buf, msgbuf, msgbuf_size);
549
550   if (s->inbound == GNUNET_NO)
551   {
552 #if DEBUG_HTTP
553     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
554                      "Using outbound client session to send to `%s'\n",
555                      GNUNET_i2s (target));
556 #endif
557      client_send (s, msg);
558      res = msgbuf_size;
559   }
560   if (s->inbound == GNUNET_YES)
561   {
562     server_send (s, msg);
563     res = msgbuf_size;
564 #if DEBUG_HTTP
565     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
566                      "Using inbound server session to send to `%s'\n",
567                      GNUNET_i2s (target));
568 #endif
569
570   }
571   return res;
572 }
573
574
575 /**
576  * Function that can be used to force the plugin to disconnect
577  * from the given peer and cancel all previous transmissions
578  * (and their continuationc).
579  *
580  * @param cls closure
581  * @param target peer from which to disconnect
582  */
583 static void
584 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
585 {
586   struct Plugin *plugin = cls;
587   struct Session *next = NULL;
588   struct Session *s = plugin->head;
589
590   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
591                    "Transport tells me to disconnect `%s'\n",
592                    GNUNET_i2s (target));
593   while (s != NULL)
594   {
595     next = s->next;
596     if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
597     {
598       if (s->inbound == GNUNET_NO)
599         GNUNET_assert (GNUNET_OK == client_disconnect (s));
600       else
601         GNUNET_assert (GNUNET_OK == server_disconnect (s));
602       GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
603       delete_session (s);
604     }
605     s = next;
606   }
607 }
608
609 static void
610 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
611                  socklen_t addrlen)
612 {
613   struct Plugin *plugin = cls;
614   struct IPv4HttpAddressWrapper *w_t4 = NULL;
615   struct IPv6HttpAddressWrapper *w_t6 = NULL;
616   int af;
617
618   af = addr->sa_family;
619   switch (af)
620   {
621   case AF_INET:
622     w_t4 = plugin->ipv4_addr_head;
623     struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
624     while (w_t4 != NULL)
625     {
626       int res = memcmp (&w_t4->addr.ipv4_addr,
627                         &a4->sin_addr,
628                         sizeof (struct in_addr));
629       if (res == 0)
630       {
631         if (a4->sin_port!= w_t4->addr.u4_port)
632           res = -1;
633       }
634
635       if (0 == res)
636         break;
637       w_t4 = w_t4->next;
638     }
639     if (w_t4 == NULL)
640     {
641       w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
642       memcpy (&w_t4->addr.ipv4_addr, &a4->sin_addr,
643               sizeof (struct in_addr));
644       w_t4->addr.u4_port = a4->sin_port;
645
646       GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
647                                    plugin->ipv4_addr_tail, w_t4);
648     }
649 #if DEBUG_HTTP
650     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
651                      "Notifying transport to add IPv4 address `%s'\n",
652                      http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct IPv4HttpAddress)));
653 #endif
654     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr, sizeof (struct IPv4HttpAddress));
655
656     break;
657   case AF_INET6:
658     w_t6 = plugin->ipv6_addr_head;
659     struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
660     while (w_t6)
661     {
662       int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
663                         sizeof (struct in6_addr));
664       if (res == 0)
665       {
666         if (a6->sin6_port != w_t6->addr6.u6_port)
667           res = -1;
668       }
669       if (0 == res)
670         break;
671       w_t6 = w_t6->next;
672     }
673     if (w_t6 == NULL)
674     {
675       w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
676
677       memcpy (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
678               sizeof (struct in6_addr));
679       w_t6->addr6.u6_port = a6->sin6_port;
680
681       GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
682                                    plugin->ipv6_addr_tail, w_t6);
683     }
684 #if DEBUG_HTTP
685     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
686                      "Notifying transport to add IPv6 address `%s'\n",
687                      http_plugin_address_to_string(NULL, &w_t6->addr6, sizeof (struct IPv6HttpAddress)));
688 #endif
689     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6, sizeof (struct IPv6HttpAddress));
690     break;
691   default:
692     return;
693   }
694
695 }
696
697 static void
698 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
699                     socklen_t addrlen)
700 {
701   struct Plugin *plugin = cls;
702   struct IPv4HttpAddressWrapper *w_t4 = NULL;
703   struct IPv6HttpAddressWrapper *w_t6 = NULL;
704   int af;
705
706   af = addr->sa_family;
707   switch (af)
708   {
709   case AF_INET:
710     w_t4 = plugin->ipv4_addr_head;
711     struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
712     while (w_t4 != NULL)
713     {
714       int res = memcmp (&w_t4->addr.ipv4_addr,
715                         &a4->sin_addr,
716                         sizeof (struct in_addr));
717       if (res == 0)
718       {
719         if (a4->sin_port!= w_t4->addr.u4_port)
720           res = -1;
721       }
722
723       if (0 == res)
724         break;
725       w_t4 = w_t4->next;
726     }
727     if (w_t4 == NULL)
728       return;
729
730 #if DEBUG_HTTP
731     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
732                      "Notifying transport to remove IPv4 address `%s'\n",
733                      http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct IPv4HttpAddress)));
734 #endif
735     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
736                                  sizeof (struct IPv4HttpAddress));
737
738     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
739                                  w_t4);
740     GNUNET_free (w_t4);
741     break;
742   case AF_INET6:
743     w_t6 = plugin->ipv6_addr_head;
744     struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
745     while (w_t6)
746     {
747       int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
748                         sizeof (struct in6_addr));
749       if (res == 0)
750       {
751         if (a6->sin6_port != w_t6->addr6.u6_port)
752           res = -1;
753       }
754       if (0 == res)
755         break;
756       w_t6 = w_t6->next;
757     }
758     if (w_t6 == NULL)
759       return;
760 #if DEBUG_HTTP
761     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
762                      "Notifying transport to remove IPv6 address `%s'\n",
763                      http_plugin_address_to_string(NULL, &w_t6->addr6, sizeof (struct IPv6HttpAddress)));
764 #endif
765     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
766                                  sizeof (struct IPv6HttpAddress));
767
768     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
769                                  w_t6);
770     GNUNET_free (w_t6);
771     break;
772   default:
773     return;
774   }
775
776 }
777
778 /**
779  * Our external IP address/port mapping has changed.
780  *
781  * @param cls closure, the 'struct LocalAddrList'
782  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
783  *     the previous (now invalid) one
784  * @param addr either the previous or the new public IP address
785  * @param addrlen actual lenght of the address
786  */
787 static void
788 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
789                        socklen_t addrlen)
790 {
791   GNUNET_assert (cls != NULL);
792 #if DEBUG_HTTP
793   struct Plugin *plugin = cls;
794 #endif
795 #if DEBUG_HTTP
796   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
797                    "NPMC called %s to address `%s'\n",
798                    (add_remove == GNUNET_NO) ? "remove" : "add",
799                    GNUNET_a2s (addr, addrlen));
800 #endif
801   switch (add_remove)
802   {
803   case GNUNET_YES:
804       nat_add_address (cls, add_remove, addr, addrlen);
805     break;
806   case GNUNET_NO:
807     nat_remove_address (cls, add_remove, addr, addrlen);
808     break;
809   }
810 }
811
812 void
813 http_check_ipv6 (struct Plugin *plugin)
814 {
815   struct GNUNET_NETWORK_Handle *desc = NULL;
816   if (plugin->ipv6 == GNUNET_YES)
817   {
818     /* probe IPv6 support */
819     desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
820     if (NULL == desc)
821     {
822       if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
823           (errno == EACCES))
824       {
825         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
826       }
827       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
828                   _
829                   ("Disabling IPv6 since it is not supported on this system!\n"));
830       plugin->ipv6 = GNUNET_NO;
831     }
832     else
833     {
834       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
835       desc = NULL;
836     }
837
838   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
839               "Testing IPv6 on this system: %s\n", (plugin->ipv6 == GNUNET_YES) ? "successful" : "failed");
840   }
841 }
842
843 int
844 http_get_addresses (struct Plugin *plugin,
845                     const char *serviceName,
846                     const struct GNUNET_CONFIGURATION_Handle
847                     *cfg, struct sockaddr ***addrs,
848                     socklen_t ** addr_lens)
849 {
850   int disablev6;
851   unsigned long long port;
852   struct addrinfo hints;
853   struct addrinfo *res;
854   struct addrinfo *pos;
855   struct addrinfo *next;
856   unsigned int i;
857   int resi;
858   int ret;
859   struct sockaddr **saddrs;
860   socklen_t *saddrlens;
861   char *hostname;
862
863   *addrs = NULL;
864   *addr_lens = NULL;
865
866   disablev6 = !plugin->ipv6;
867
868   port = 0;
869   if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
870   {
871     GNUNET_break (GNUNET_OK ==
872                   GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
873                                                          "PORT", &port));
874     if (port > 65535)
875     {
876       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
877                   _
878                   ("Require valid port number for service in configuration!\n"));
879       return GNUNET_SYSERR;
880     }
881   }
882
883   if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
884   {
885     GNUNET_break (GNUNET_OK ==
886                   GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
887                                                          "BINDTO", &hostname));
888   }
889   else
890     hostname = NULL;
891
892   if (hostname != NULL)
893   {
894     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
895                 "Resolving `%s' since that is where `%s' will bind to.\n",
896                 hostname, serviceName);
897     memset (&hints, 0, sizeof (struct addrinfo));
898     if (disablev6)
899       hints.ai_family = AF_INET;
900     if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
901         (res == NULL))
902     {
903       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
904                   hostname, gai_strerror (ret));
905       GNUNET_free (hostname);
906       return GNUNET_SYSERR;
907     }
908     next = res;
909     i = 0;
910     while (NULL != (pos = next))
911     {
912       next = pos->ai_next;
913       if ((disablev6) && (pos->ai_family == AF_INET6))
914         continue;
915       i++;
916     }
917     if (0 == i)
918     {
919       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
920                   _("Failed to find %saddress for `%s'.\n"),
921                   disablev6 ? "IPv4 " : "", hostname);
922       freeaddrinfo (res);
923       GNUNET_free (hostname);
924       return GNUNET_SYSERR;
925     }
926     resi = i;
927     saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
928     saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
929     i = 0;
930     next = res;
931     while (NULL != (pos = next))
932     {
933       next = pos->ai_next;
934       if ((disablev6) && (pos->ai_family == AF_INET6))
935         continue;
936       if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
937         continue;               /* not TCP */
938       if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
939         continue;               /* huh? */
940       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
941                   "Service will bind to `%s'\n",
942                   GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
943       if (pos->ai_family == AF_INET)
944       {
945         GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
946         saddrlens[i] = pos->ai_addrlen;
947         saddrs[i] = GNUNET_malloc (saddrlens[i]);
948         memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
949         ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
950       }
951       else
952       {
953         GNUNET_assert (pos->ai_family == AF_INET6);
954         GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
955         saddrlens[i] = pos->ai_addrlen;
956         saddrs[i] = GNUNET_malloc (saddrlens[i]);
957         memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
958         ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
959       }
960       i++;
961     }
962     GNUNET_free (hostname);
963     freeaddrinfo (res);
964     resi = i;
965   }
966   else
967   {
968     /* will bind against everything, just set port */
969     if (disablev6)
970     {
971       /* V4-only */
972       resi = 1;
973       i = 0;
974       saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
975       saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
976
977       saddrlens[i] = sizeof (struct sockaddr_in);
978       saddrs[i] = GNUNET_malloc (saddrlens[i]);
979 #if HAVE_SOCKADDR_IN_SIN_LEN
980       ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
981 #endif
982       ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
983       ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
984     }
985     else
986     {
987       /* dual stack */
988       resi = 2;
989       saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
990       saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
991       i = 0;
992       saddrlens[i] = sizeof (struct sockaddr_in6);
993       saddrs[i] = GNUNET_malloc (saddrlens[i]);
994 #if HAVE_SOCKADDR_IN_SIN_LEN
995       ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
996 #endif
997       ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
998       ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
999       i++;
1000       saddrlens[i] = sizeof (struct sockaddr_in);
1001       saddrs[i] = GNUNET_malloc (saddrlens[i]);
1002 #if HAVE_SOCKADDR_IN_SIN_LEN
1003       ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1004 #endif
1005       ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1006       ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1007     }
1008   }
1009   *addrs = saddrs;
1010   *addr_lens = saddrlens;
1011   return resi;
1012 }
1013
1014 static void
1015 start_report_addresses (struct Plugin *plugin)
1016 {
1017   int res = GNUNET_OK;
1018   struct sockaddr **addrs;
1019   socklen_t *addrlens;
1020
1021   res =
1022       http_get_addresses (plugin, plugin->name, plugin->env->cfg,
1023                                            &addrs, &addrlens);
1024   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1025                    _("Found %u addresses to report to NAT service\n"),res);
1026
1027   if (res != GNUNET_SYSERR)
1028   {
1029     plugin->nat =
1030         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
1031                              (unsigned int) res,
1032                              (const struct sockaddr **) addrs, addrlens,
1033                              &nat_port_map_callback, NULL,
1034                              plugin);
1035     while (res > 0)
1036     {
1037       res--;
1038 #if 0
1039       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1040                        _("FREEING %s\n"),
1041                        GNUNET_a2s (addrs[res], addrlens[res]));
1042 #endif
1043       GNUNET_assert (addrs[res] != NULL);
1044       GNUNET_free (addrs[res]);
1045     }
1046     GNUNET_free_non_null (addrs);
1047     GNUNET_free_non_null (addrlens);
1048   }
1049   else
1050   {
1051     plugin->nat =
1052         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
1053                              NULL, NULL, plugin);
1054   }
1055 }
1056
1057 static void
1058 stop_report_addresses (struct Plugin *plugin)
1059 {
1060   /* Stop NAT handle */
1061   GNUNET_NAT_unregister (plugin->nat);
1062
1063   /* Clean up addresses */
1064   struct IPv4HttpAddressWrapper *w_t4;
1065   struct IPv6HttpAddressWrapper *w_t6;
1066
1067   while (plugin->ipv4_addr_head != NULL)
1068   {
1069     w_t4 = plugin->ipv4_addr_head;
1070     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
1071                                  w_t4);
1072     GNUNET_free (w_t4);
1073   }
1074
1075   while (plugin->ipv6_addr_head != NULL)
1076   {
1077     w_t6 = plugin->ipv6_addr_head;
1078     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1079                                  w_t6);
1080     GNUNET_free (w_t6);
1081   }
1082 }
1083
1084 static int
1085 configure_plugin (struct Plugin *plugin)
1086 {
1087   int res = GNUNET_OK;
1088
1089   /* Use IPv4? */
1090   if (GNUNET_CONFIGURATION_have_value
1091       (plugin->env->cfg, plugin->name, "USE_IPv4"))
1092   {
1093     plugin->ipv4 =
1094         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1095                                               "USE_IPv4");
1096   }
1097   else
1098     plugin->ipv4 = GNUNET_YES;
1099
1100   /* Use IPv6? */
1101   if (GNUNET_CONFIGURATION_have_value
1102       (plugin->env->cfg, plugin->name, "USE_IPv6"))
1103   {
1104     plugin->ipv6 =
1105         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1106                                               "USE_IPv6");
1107   }
1108   else
1109     plugin->ipv6 = GNUNET_YES;
1110
1111   if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
1112   {
1113     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1114                      _
1115                      ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
1116                      plugin->name);
1117     res = GNUNET_SYSERR;
1118   }
1119
1120   /* Reading port number from config file */
1121   unsigned long long port;
1122
1123   if ((GNUNET_OK !=
1124        GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1125                                               "PORT", &port)) || (port > 65535))
1126   {
1127     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1128                      _("Port is required! Fix in configuration\n"),
1129                      plugin->name);
1130     res = GNUNET_SYSERR;
1131     goto fail;
1132   }
1133   plugin->port = port;
1134
1135   plugin->client_only = GNUNET_NO;
1136   if (plugin->port == 0)
1137   {
1138     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1139                    _("Port 0, client only mode\n"));
1140     plugin->client_only = GNUNET_YES;
1141   }
1142
1143   char * bind4_address = NULL;
1144   if ((plugin->ipv4 == GNUNET_YES) && (GNUNET_YES ==
1145       GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1146                                              "BINDTO", &bind4_address)))
1147   {
1148     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1149                 "Binding %s plugin to specific IPv4 address: `%s'\n",
1150                 plugin->protocol, bind4_address);
1151     plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1152     if (1 != inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
1153     {
1154       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1155                   _("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1156                   bind4_address, plugin->protocol);
1157       GNUNET_free (plugin->server_addr_v4);
1158       plugin->server_addr_v4 = NULL;
1159     }
1160     else
1161     {
1162       plugin->server_addr_v4->sin_family = AF_INET;
1163       plugin->server_addr_v4->sin_port = htons (plugin->port);
1164     }
1165     GNUNET_free (bind4_address);
1166   }
1167
1168
1169   char * bind6_address = NULL;
1170   if ((plugin->ipv6 == GNUNET_YES) && (GNUNET_YES ==
1171       GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1172                                              "BINDTO6", &bind6_address)))
1173   {
1174     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1175                 "Binding %s plugin to specific IPv6 address: `%s'\n",
1176                 plugin->protocol, bind6_address);
1177     plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1178     if (1 != inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
1179     {
1180       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1181                   _("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1182                   bind6_address, plugin->protocol);
1183       GNUNET_free (plugin->server_addr_v6);
1184       plugin->server_addr_v6 = NULL;
1185     }
1186     else
1187     {
1188       plugin->server_addr_v6->sin6_family = AF_INET6;
1189       plugin->server_addr_v6->sin6_port = htons (plugin->port);
1190     }
1191     GNUNET_free (bind6_address);
1192   }
1193
1194
1195   /* Optional parameters */
1196   unsigned long long maxneigh;
1197   if (GNUNET_OK !=
1198       GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1199                                              "MAX_CONNECTIONS", &maxneigh))
1200     maxneigh = 128;
1201   plugin->max_connections = maxneigh;
1202
1203 fail:
1204   return res;
1205 }
1206
1207 /**
1208  * Entry point for the plugin.
1209  */
1210 void *
1211 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1212 {
1213   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1214   struct GNUNET_TRANSPORT_PluginFunctions *api;
1215   struct Plugin *plugin;
1216   int res;
1217
1218   plugin = GNUNET_malloc (sizeof (struct Plugin));
1219   plugin->env = env;
1220   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1221   api->cls = plugin;
1222   api->send = &http_plugin_send;
1223   api->disconnect = &http_plugin_disconnect;
1224   api->address_pretty_printer = &http_plugin_address_pretty_printer;
1225   api->check_address = &http_plugin_address_suggested;
1226   api->address_to_string = &http_plugin_address_to_string;
1227
1228 #if BUILD_HTTPS
1229   plugin->name = "transport-https";
1230   plugin->protocol = "https";
1231 #else
1232   plugin->name = "transport-http";
1233   plugin->protocol = "http";
1234 #endif
1235   /* Configure plugin from configuration */
1236   res = configure_plugin (plugin);
1237   if (res == GNUNET_SYSERR)
1238   {
1239     GNUNET_free_non_null (plugin->server_addr_v4);
1240     GNUNET_free_non_null (plugin->server_addr_v6);
1241     GNUNET_free (plugin);
1242     GNUNET_free (api);
1243     return NULL;
1244   }
1245
1246   /* checking IPv6 support */
1247   http_check_ipv6 (plugin);
1248
1249   /* Start client */
1250   res = client_start (plugin);
1251   if (res == GNUNET_SYSERR)
1252   {
1253     GNUNET_free_non_null (plugin->server_addr_v4);
1254     GNUNET_free_non_null (plugin->server_addr_v6);
1255     GNUNET_free (plugin);
1256     GNUNET_free (api);
1257     return NULL;
1258   }
1259
1260   /* Start server */
1261   if (plugin->client_only == GNUNET_NO)
1262   {
1263     res = server_start (plugin);
1264     if (res == GNUNET_SYSERR)
1265     {
1266       server_stop (plugin);
1267       client_stop (plugin);
1268
1269       GNUNET_free_non_null (plugin->server_addr_v4);
1270       GNUNET_free_non_null (plugin->server_addr_v6);
1271       GNUNET_free (plugin);
1272       GNUNET_free (api);
1273       return NULL;
1274     }
1275   }
1276   /* Report addresses to transport service */
1277   start_report_addresses (plugin);
1278
1279 #if DEBUG_HTTP
1280   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1281                    "Plugin `%s' loaded\n", plugin->name);
1282 #endif
1283
1284   return api;
1285 }
1286
1287
1288 /**
1289  * Exit point from the plugin.
1290  */
1291 void *
1292 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1293 {
1294   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1295   struct Plugin *plugin = api->cls;
1296   struct Session *s = NULL;
1297
1298   /* Stop reporting addresses to transport service */
1299   stop_report_addresses (plugin);
1300
1301   /* cleaning up sessions */
1302   s = plugin->head;
1303   while (s != NULL)
1304   {
1305 #if DEBUG_HTTP
1306   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1307                    "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1308 #endif
1309     if (s->inbound == GNUNET_NO)
1310       GNUNET_assert (GNUNET_OK == client_disconnect (s));
1311     else
1312       GNUNET_assert (GNUNET_OK == server_disconnect (s));
1313     s = s->next;
1314   }
1315
1316 #if DEBUG_HTTP
1317   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1318                    "Stopping server\n");
1319 #endif
1320   /* Stop server */
1321   server_stop (plugin);
1322
1323 #if DEBUG_HTTP
1324   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1325                    "Stopping client\n");
1326 #endif
1327   /* Stop client */
1328   client_stop (plugin);
1329
1330   /* deleting up sessions */
1331   s = plugin->head;
1332   while (s != NULL)
1333   {
1334     struct Session *t = s->next;
1335     GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
1336     delete_session (s);
1337     s = t;
1338   }
1339
1340
1341 #if DEBUG_HTTP
1342   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1343                    "Plugin `%s' unloaded\n", plugin->name);
1344 #endif
1345
1346   GNUNET_free_non_null (plugin->server_addr_v4);
1347   GNUNET_free_non_null (plugin->server_addr_v6);
1348   GNUNET_free (plugin);
1349   GNUNET_free (api);
1350
1351   return NULL;
1352 }
1353
1354 /* end of plugin_transport_http.c */