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