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