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