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