clean up, asserts, FIXME
[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
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   struct Session *s = NULL;
532
533
534   if (addrlen != 0)
535     GNUNET_assert ((addrlen == sizeof (struct IPv4HttpAddress)) ||
536                    (addrlen == sizeof (struct IPv6HttpAddress)));
537
538   /* look for existing connection */
539   s = lookup_session (plugin, target, session, addr, addrlen, 1);
540 #if DEBUG_HTTP
541   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
542                    "%s existing session: %s\n",
543                    (s != NULL) ? "Found" : "NOT Found", ((s != NULL) &&
544                                                          (s->inbound ==
545                                                           GNUNET_YES)) ?
546                    "inbound" : "outbound");
547 #endif
548
549   /* create new outbound connection */
550   if (s == NULL)
551   {
552     if (plugin->max_connections <= plugin->cur_connections)
553     {
554       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
555                        "Maximum number of connections reached, "
556                        "cannot connect to peer `%s'\n", GNUNET_i2s (target));
557       return res;
558     }
559
560 #if DEBUG_HTTP
561     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
562                      "Initiiating new connection to peer `%s'\n",
563                      GNUNET_i2s (target));
564 #endif
565     s = create_session (plugin, target, addr, addrlen, cont, cont_cls);
566     GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
567     // initiate new connection
568     if (GNUNET_SYSERR == (res = client_connect (s)))
569     {
570       GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
571       delete_session (s);
572       return GNUNET_SYSERR;
573     }
574   }
575
576   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
577   msg->next = NULL;
578   msg->size = msgbuf_size;
579   msg->pos = 0;
580   msg->buf = (char *) &msg[1];
581   msg->transmit_cont = cont;
582   msg->transmit_cont_cls = cont_cls;
583   memcpy (msg->buf, msgbuf, msgbuf_size);
584
585   if (s->inbound == GNUNET_NO)
586   {
587 #if DEBUG_HTTP
588     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
589                      "Using outbound client session to send to `%s'\n",
590                      GNUNET_i2s (target));
591 #endif
592     client_send (s, msg);
593     res = msgbuf_size;
594   }
595   if (s->inbound == GNUNET_YES)
596   {
597     server_send (s, msg);
598     res = msgbuf_size;
599 #if DEBUG_HTTP
600     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
601                      "Using inbound server session to send to `%s'\n",
602                      GNUNET_i2s (target));
603 #endif
604
605   }
606   return res;
607 }
608
609
610 /**
611  * Function that can be used to force the plugin to disconnect
612  * from the given peer and cancel all previous transmissions
613  * (and their continuationc).
614  *
615  * @param cls closure
616  * @param target peer from which to disconnect
617  */
618 static void
619 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
620 {
621   struct Plugin *plugin = cls;
622   struct Session *next = NULL;
623   struct Session *s = plugin->head;
624
625   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
626                    "Transport tells me to disconnect `%s'\n",
627                    GNUNET_i2s (target));
628   while (s != NULL)
629   {
630     next = s->next;
631     if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
632     {
633       if (s->inbound == GNUNET_NO)
634         GNUNET_assert (GNUNET_OK == client_disconnect (s));
635       else
636         GNUNET_assert (GNUNET_OK == server_disconnect (s));
637       GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
638       delete_session (s);
639     }
640     s = next;
641   }
642 }
643
644 static void
645 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
646                  socklen_t addrlen)
647 {
648   struct Plugin *plugin = cls;
649   struct IPv4HttpAddressWrapper *w_t4 = NULL;
650   struct IPv6HttpAddressWrapper *w_t6 = NULL;
651   int af;
652
653   af = addr->sa_family;
654   switch (af)
655   {
656   case AF_INET:
657     w_t4 = plugin->ipv4_addr_head;
658     struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
659
660     while (w_t4 != NULL)
661     {
662       int res = memcmp (&w_t4->addr.ipv4_addr,
663                         &a4->sin_addr,
664                         sizeof (struct in_addr));
665
666       if (res == 0)
667       {
668         if (a4->sin_port != w_t4->addr.u4_port)
669           res = -1;
670       }
671
672       if (0 == res)
673         break;
674       w_t4 = w_t4->next;
675     }
676     if (w_t4 == NULL)
677     {
678       w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
679       memcpy (&w_t4->addr.ipv4_addr, &a4->sin_addr, sizeof (struct in_addr));
680       w_t4->addr.u4_port = a4->sin_port;
681
682       GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
683                                    plugin->ipv4_addr_tail, w_t4);
684     }
685 #if DEBUG_HTTP
686     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
687                      "Notifying transport to add IPv4 address `%s'\n",
688                      http_plugin_address_to_string (NULL, &w_t4->addr,
689                                                     sizeof (struct
690                                                             IPv4HttpAddress)));
691 #endif
692     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
693                                  sizeof (struct IPv4HttpAddress));
694
695     break;
696   case AF_INET6:
697     w_t6 = plugin->ipv6_addr_head;
698     struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
699
700     while (w_t6)
701     {
702       int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
703                         sizeof (struct in6_addr));
704
705       if (res == 0)
706       {
707         if (a6->sin6_port != w_t6->addr6.u6_port)
708           res = -1;
709       }
710       if (0 == res)
711         break;
712       w_t6 = w_t6->next;
713     }
714     if (w_t6 == NULL)
715     {
716       w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
717
718       memcpy (&w_t6->addr6.ipv6_addr, &a6->sin6_addr, sizeof (struct in6_addr));
719       w_t6->addr6.u6_port = a6->sin6_port;
720
721       GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
722                                    plugin->ipv6_addr_tail, w_t6);
723     }
724 #if DEBUG_HTTP
725     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
726                      "Notifying transport to add IPv6 address `%s'\n",
727                      http_plugin_address_to_string (NULL, &w_t6->addr6,
728                                                     sizeof (struct
729                                                             IPv6HttpAddress)));
730 #endif
731     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
732                                  sizeof (struct IPv6HttpAddress));
733     break;
734   default:
735     return;
736   }
737
738 }
739
740 static void
741 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
742                     socklen_t addrlen)
743 {
744   struct Plugin *plugin = cls;
745   struct IPv4HttpAddressWrapper *w_t4 = NULL;
746   struct IPv6HttpAddressWrapper *w_t6 = NULL;
747   int af;
748
749   af = addr->sa_family;
750   switch (af)
751   {
752   case AF_INET:
753     w_t4 = plugin->ipv4_addr_head;
754     struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
755
756     while (w_t4 != NULL)
757     {
758       int res = memcmp (&w_t4->addr.ipv4_addr,
759                         &a4->sin_addr,
760                         sizeof (struct in_addr));
761
762       if (res == 0)
763       {
764         if (a4->sin_port != w_t4->addr.u4_port)
765           res = -1;
766       }
767
768       if (0 == res)
769         break;
770       w_t4 = w_t4->next;
771     }
772     if (w_t4 == NULL)
773       return;
774
775 #if DEBUG_HTTP
776     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
777                      "Notifying transport to remove IPv4 address `%s'\n",
778                      http_plugin_address_to_string (NULL, &w_t4->addr,
779                                                     sizeof (struct
780                                                             IPv4HttpAddress)));
781 #endif
782     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
783                                  sizeof (struct IPv4HttpAddress));
784
785     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
786                                  w_t4);
787     GNUNET_free (w_t4);
788     break;
789   case AF_INET6:
790     w_t6 = plugin->ipv6_addr_head;
791     struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
792
793     while (w_t6)
794     {
795       int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
796                         sizeof (struct in6_addr));
797
798       if (res == 0)
799       {
800         if (a6->sin6_port != w_t6->addr6.u6_port)
801           res = -1;
802       }
803       if (0 == res)
804         break;
805       w_t6 = w_t6->next;
806     }
807     if (w_t6 == NULL)
808       return;
809 #if DEBUG_HTTP
810     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
811                      "Notifying transport to remove IPv6 address `%s'\n",
812                      http_plugin_address_to_string (NULL, &w_t6->addr6,
813                                                     sizeof (struct
814                                                             IPv6HttpAddress)));
815 #endif
816     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
817                                  sizeof (struct IPv6HttpAddress));
818
819     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
820                                  w_t6);
821     GNUNET_free (w_t6);
822     break;
823   default:
824     return;
825   }
826
827 }
828
829 /**
830  * Our external IP address/port mapping has changed.
831  *
832  * @param cls closure, the 'struct LocalAddrList'
833  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
834  *     the previous (now invalid) one
835  * @param addr either the previous or the new public IP address
836  * @param addrlen actual lenght of the address
837  */
838 static void
839 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
840                        socklen_t addrlen)
841 {
842   GNUNET_assert (cls != NULL);
843 #if DEBUG_HTTP
844   struct Plugin *plugin = cls;
845 #endif
846 #if DEBUG_HTTP
847   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
848                    "NPMC called %s to address `%s'\n",
849                    (add_remove == GNUNET_NO) ? "remove" : "add",
850                    GNUNET_a2s (addr, addrlen));
851 #endif
852   switch (add_remove)
853   {
854   case GNUNET_YES:
855     nat_add_address (cls, add_remove, addr, addrlen);
856     break;
857   case GNUNET_NO:
858     nat_remove_address (cls, add_remove, addr, addrlen);
859     break;
860   }
861 }
862
863 void
864 http_check_ipv6 (struct Plugin *plugin)
865 {
866   struct GNUNET_NETWORK_Handle *desc = NULL;
867
868   if (plugin->ipv6 == GNUNET_YES)
869   {
870     /* probe IPv6 support */
871     desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
872     if (NULL == desc)
873     {
874       if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
875           (errno == EACCES))
876       {
877         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
878       }
879       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
880                        _
881                        ("Disabling IPv6 since it is not supported on this system!\n"));
882       plugin->ipv6 = GNUNET_NO;
883     }
884     else
885     {
886       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
887       desc = NULL;
888     }
889
890     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
891                      "Testing IPv6 on this system: %s\n",
892                      (plugin->ipv6 == GNUNET_YES) ? "successful" : "failed");
893   }
894 }
895
896 int
897 http_get_addresses (struct Plugin *plugin, const char *serviceName,
898                     const struct GNUNET_CONFIGURATION_Handle *cfg,
899                     struct sockaddr ***addrs, socklen_t ** addr_lens)
900 {
901   int disablev6;
902   unsigned long long port;
903   struct addrinfo hints;
904   struct addrinfo *res;
905   struct addrinfo *pos;
906   struct addrinfo *next;
907   unsigned int i;
908   int resi;
909   int ret;
910   struct sockaddr **saddrs;
911   socklen_t *saddrlens;
912   char *hostname;
913
914   *addrs = NULL;
915   *addr_lens = NULL;
916
917   disablev6 = !plugin->ipv6;
918
919   port = 0;
920   if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
921   {
922     GNUNET_break (GNUNET_OK ==
923                   GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
924                                                          "PORT", &port));
925     if (port > 65535)
926     {
927       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
928                   _
929                   ("Require valid port number for service in configuration!\n"));
930       return GNUNET_SYSERR;
931     }
932   }
933
934   if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
935   {
936     GNUNET_break (GNUNET_OK ==
937                   GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
938                                                          "BINDTO", &hostname));
939   }
940   else
941     hostname = NULL;
942
943   if (hostname != NULL)
944   {
945     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
946                      "Resolving `%s' since that is where `%s' will bind to.\n",
947                      hostname, serviceName);
948     memset (&hints, 0, sizeof (struct addrinfo));
949     if (disablev6)
950       hints.ai_family = AF_INET;
951     if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
952         (res == NULL))
953     {
954       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
955                   hostname, gai_strerror (ret));
956       GNUNET_free (hostname);
957       return GNUNET_SYSERR;
958     }
959     next = res;
960     i = 0;
961     while (NULL != (pos = next))
962     {
963       next = pos->ai_next;
964       if ((disablev6) && (pos->ai_family == AF_INET6))
965         continue;
966       i++;
967     }
968     if (0 == i)
969     {
970       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
971                   _("Failed to find %saddress for `%s'.\n"),
972                   disablev6 ? "IPv4 " : "", hostname);
973       freeaddrinfo (res);
974       GNUNET_free (hostname);
975       return GNUNET_SYSERR;
976     }
977     resi = i;
978     saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
979     saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
980     i = 0;
981     next = res;
982     while (NULL != (pos = next))
983     {
984       next = pos->ai_next;
985       if ((disablev6) && (pos->ai_family == AF_INET6))
986         continue;
987       if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
988         continue;               /* not TCP */
989       if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
990         continue;               /* huh? */
991       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
992                        "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
993                                                                   pos->ai_addrlen));
994       if (pos->ai_family == AF_INET)
995       {
996         GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
997         saddrlens[i] = pos->ai_addrlen;
998         saddrs[i] = GNUNET_malloc (saddrlens[i]);
999         memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1000         ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1001       }
1002       else
1003       {
1004         GNUNET_assert (pos->ai_family == AF_INET6);
1005         GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
1006         saddrlens[i] = pos->ai_addrlen;
1007         saddrs[i] = GNUNET_malloc (saddrlens[i]);
1008         memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1009         ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1010       }
1011       i++;
1012     }
1013     GNUNET_free (hostname);
1014     freeaddrinfo (res);
1015     resi = i;
1016   }
1017   else
1018   {
1019     /* will bind against everything, just set port */
1020     if (disablev6)
1021     {
1022       /* V4-only */
1023       resi = 1;
1024       i = 0;
1025       saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1026       saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1027
1028       saddrlens[i] = sizeof (struct sockaddr_in);
1029       saddrs[i] = GNUNET_malloc (saddrlens[i]);
1030 #if HAVE_SOCKADDR_IN_SIN_LEN
1031       ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1032 #endif
1033       ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1034       ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1035     }
1036     else
1037     {
1038       /* dual stack */
1039       resi = 2;
1040       saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1041       saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1042       i = 0;
1043       saddrlens[i] = sizeof (struct sockaddr_in6);
1044       saddrs[i] = GNUNET_malloc (saddrlens[i]);
1045 #if HAVE_SOCKADDR_IN_SIN_LEN
1046       ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1047 #endif
1048       ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1049       ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1050       i++;
1051       saddrlens[i] = sizeof (struct sockaddr_in);
1052       saddrs[i] = GNUNET_malloc (saddrlens[i]);
1053 #if HAVE_SOCKADDR_IN_SIN_LEN
1054       ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1055 #endif
1056       ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1057       ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1058     }
1059   }
1060   *addrs = saddrs;
1061   *addr_lens = saddrlens;
1062   return resi;
1063 }
1064
1065 static void
1066 start_report_addresses (struct Plugin *plugin)
1067 {
1068   int res = GNUNET_OK;
1069   struct sockaddr **addrs;
1070   socklen_t *addrlens;
1071
1072   res =
1073       http_get_addresses (plugin, plugin->name, plugin->env->cfg, &addrs,
1074                           &addrlens);
1075   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1076                    _("Found %u addresses to report to NAT service\n"), res);
1077
1078   if (res != GNUNET_SYSERR)
1079   {
1080     plugin->nat =
1081         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
1082                              (unsigned int) res,
1083                              (const struct sockaddr **) addrs, addrlens,
1084                              &nat_port_map_callback, NULL, plugin);
1085     while (res > 0)
1086     {
1087       res--;
1088 #if 0
1089       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, _("FREEING %s\n"),
1090                        GNUNET_a2s (addrs[res], addrlens[res]));
1091 #endif
1092       GNUNET_assert (addrs[res] != NULL);
1093       GNUNET_free (addrs[res]);
1094     }
1095     GNUNET_free_non_null (addrs);
1096     GNUNET_free_non_null (addrlens);
1097   }
1098   else
1099   {
1100     plugin->nat =
1101         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
1102                              NULL, NULL, plugin);
1103   }
1104 }
1105
1106 static void
1107 stop_report_addresses (struct Plugin *plugin)
1108 {
1109   /* Stop NAT handle */
1110   GNUNET_NAT_unregister (plugin->nat);
1111
1112   /* Clean up addresses */
1113   struct IPv4HttpAddressWrapper *w_t4;
1114   struct IPv6HttpAddressWrapper *w_t6;
1115
1116   while (plugin->ipv4_addr_head != NULL)
1117   {
1118     w_t4 = plugin->ipv4_addr_head;
1119     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
1120                                  w_t4);
1121     GNUNET_free (w_t4);
1122   }
1123
1124   while (plugin->ipv6_addr_head != NULL)
1125   {
1126     w_t6 = plugin->ipv6_addr_head;
1127     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1128                                  w_t6);
1129     GNUNET_free (w_t6);
1130   }
1131 }
1132
1133 static int
1134 configure_plugin (struct Plugin *plugin)
1135 {
1136   int res = GNUNET_OK;
1137
1138   /* Use IPv4? */
1139   if (GNUNET_CONFIGURATION_have_value
1140       (plugin->env->cfg, plugin->name, "USE_IPv4"))
1141   {
1142     plugin->ipv4 =
1143         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1144                                               "USE_IPv4");
1145   }
1146   else
1147     plugin->ipv4 = GNUNET_YES;
1148
1149   /* Use IPv6? */
1150   if (GNUNET_CONFIGURATION_have_value
1151       (plugin->env->cfg, plugin->name, "USE_IPv6"))
1152   {
1153     plugin->ipv6 =
1154         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1155                                               "USE_IPv6");
1156   }
1157   else
1158     plugin->ipv6 = GNUNET_YES;
1159
1160   if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
1161   {
1162     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1163                      _
1164                      ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
1165                      plugin->name);
1166     res = GNUNET_SYSERR;
1167   }
1168
1169   /* Reading port number from config file */
1170   unsigned long long port;
1171
1172   if ((GNUNET_OK !=
1173        GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1174                                               "PORT", &port)) || (port > 65535))
1175   {
1176     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1177                      _("Port is required! Fix in configuration\n"),
1178                      plugin->name);
1179     res = GNUNET_SYSERR;
1180     goto fail;
1181   }
1182   plugin->port = port;
1183
1184   plugin->client_only = GNUNET_NO;
1185   if (plugin->port == 0)
1186   {
1187     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1188                      _("Port 0, client only mode\n"));
1189     plugin->client_only = GNUNET_YES;
1190   }
1191
1192   char *bind4_address = NULL;
1193
1194   if ((plugin->ipv4 == GNUNET_YES) &&
1195       (GNUNET_YES ==
1196        GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1197                                               "BINDTO", &bind4_address)))
1198   {
1199     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1200                      "Binding %s plugin to specific IPv4 address: `%s'\n",
1201                      plugin->protocol, bind4_address);
1202     plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1203     if (1 !=
1204         inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
1205     {
1206       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1207                        _
1208                        ("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1209                        bind4_address, plugin->protocol);
1210       GNUNET_free (plugin->server_addr_v4);
1211       plugin->server_addr_v4 = NULL;
1212     }
1213     else
1214     {
1215       plugin->server_addr_v4->sin_family = AF_INET;
1216       plugin->server_addr_v4->sin_port = htons (plugin->port);
1217     }
1218     GNUNET_free (bind4_address);
1219   }
1220
1221
1222   char *bind6_address = NULL;
1223
1224   if ((plugin->ipv6 == GNUNET_YES) &&
1225       (GNUNET_YES ==
1226        GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1227                                               "BINDTO6", &bind6_address)))
1228   {
1229     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1230                      "Binding %s plugin to specific IPv6 address: `%s'\n",
1231                      plugin->protocol, bind6_address);
1232     plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1233     if (1 !=
1234         inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
1235     {
1236       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1237                        _
1238                        ("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1239                        bind6_address, plugin->protocol);
1240       GNUNET_free (plugin->server_addr_v6);
1241       plugin->server_addr_v6 = NULL;
1242     }
1243     else
1244     {
1245       plugin->server_addr_v6->sin6_family = AF_INET6;
1246       plugin->server_addr_v6->sin6_port = htons (plugin->port);
1247     }
1248     GNUNET_free (bind6_address);
1249   }
1250
1251
1252   /* Optional parameters */
1253   unsigned long long maxneigh;
1254
1255   if (GNUNET_OK !=
1256       GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1257                                              "MAX_CONNECTIONS", &maxneigh))
1258     maxneigh = 128;
1259   plugin->max_connections = maxneigh;
1260
1261 fail:
1262   return res;
1263 }
1264
1265 /**
1266  * Entry point for the plugin.
1267  */
1268 void *
1269 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1270 {
1271   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1272   struct GNUNET_TRANSPORT_PluginFunctions *api;
1273   struct Plugin *plugin;
1274   int res;
1275
1276   plugin = GNUNET_malloc (sizeof (struct Plugin));
1277   plugin->env = env;
1278   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1279   api->cls = plugin;
1280   api->send = &http_plugin_send;
1281   api->disconnect = &http_plugin_disconnect;
1282   api->address_pretty_printer = &http_plugin_address_pretty_printer;
1283   api->check_address = &http_plugin_address_suggested;
1284   api->address_to_string = &http_plugin_address_to_string;
1285
1286 #if BUILD_HTTPS
1287   plugin->name = "transport-https";
1288   plugin->protocol = "https";
1289 #else
1290   plugin->name = "transport-http";
1291   plugin->protocol = "http";
1292 #endif
1293   /* Configure plugin from configuration */
1294   res = configure_plugin (plugin);
1295   if (res == GNUNET_SYSERR)
1296   {
1297     GNUNET_free_non_null (plugin->server_addr_v4);
1298     GNUNET_free_non_null (plugin->server_addr_v6);
1299     GNUNET_free (plugin);
1300     GNUNET_free (api);
1301     return NULL;
1302   }
1303
1304   /* checking IPv6 support */
1305   http_check_ipv6 (plugin);
1306
1307   /* Start client */
1308   res = client_start (plugin);
1309   if (res == GNUNET_SYSERR)
1310   {
1311     GNUNET_free_non_null (plugin->server_addr_v4);
1312     GNUNET_free_non_null (plugin->server_addr_v6);
1313     GNUNET_free (plugin);
1314     GNUNET_free (api);
1315     return NULL;
1316   }
1317
1318   /* Start server */
1319   if (plugin->client_only == GNUNET_NO)
1320   {
1321     res = server_start (plugin);
1322     if (res == GNUNET_SYSERR)
1323     {
1324       server_stop (plugin);
1325       client_stop (plugin);
1326
1327       GNUNET_free_non_null (plugin->server_addr_v4);
1328       GNUNET_free_non_null (plugin->server_addr_v6);
1329       GNUNET_free (plugin);
1330       GNUNET_free (api);
1331       return NULL;
1332     }
1333   }
1334   /* Report addresses to transport service */
1335   start_report_addresses (plugin);
1336
1337 #if DEBUG_HTTP
1338   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1339                    "Plugin `%s' loaded\n", plugin->name);
1340 #endif
1341
1342   return api;
1343 }
1344
1345
1346 /**
1347  * Exit point from the plugin.
1348  */
1349 void *
1350 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1351 {
1352   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1353   struct Plugin *plugin = api->cls;
1354   struct Session *s = NULL;
1355
1356   /* Stop reporting addresses to transport service */
1357   stop_report_addresses (plugin);
1358
1359   /* cleaning up sessions */
1360   s = plugin->head;
1361   while (s != NULL)
1362   {
1363 #if DEBUG_HTTP
1364     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1365                      "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1366 #endif
1367     if (s->inbound == GNUNET_NO)
1368       GNUNET_assert (GNUNET_OK == client_disconnect (s));
1369     else
1370       GNUNET_assert (GNUNET_OK == server_disconnect (s));
1371     s = s->next;
1372   }
1373
1374 #if DEBUG_HTTP
1375   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping server\n");
1376 #endif
1377   /* Stop server */
1378   server_stop (plugin);
1379
1380 #if DEBUG_HTTP
1381   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping client\n");
1382 #endif
1383   /* Stop client */
1384   client_stop (plugin);
1385
1386   /* deleting up sessions */
1387   s = plugin->head;
1388   while (s != NULL)
1389   {
1390     struct Session *t = s->next;
1391
1392     GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
1393     delete_session (s);
1394     s = t;
1395   }
1396
1397
1398 #if DEBUG_HTTP
1399   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1400                    "Plugin `%s' unloaded\n", plugin->name);
1401 #endif
1402
1403   GNUNET_free_non_null (plugin->server_addr_v4);
1404   GNUNET_free_non_null (plugin->server_addr_v6);
1405   GNUNET_free (plugin);
1406   GNUNET_free (api);
1407
1408   return NULL;
1409 }
1410
1411 /* end of plugin_transport_http.c */