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