comment
[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  * Deleting the session
590  * Must not be used afterwards
591  */
592
593 void
594 delete_session (struct Session *s)
595 {
596   struct Plugin *plugin = s->plugin;
597   stop_session_timeout(s);
598
599   GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
600   struct HTTP_Message *msg = s->msg_head;
601   struct HTTP_Message *tmp = NULL;
602
603   while (msg != NULL)
604   {
605     tmp = msg->next;
606
607     GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
608     if (msg->transmit_cont != NULL)
609     {
610       msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
611     }
612     GNUNET_free (msg);
613     msg = tmp;
614   }
615
616   if (s->msg_tk != NULL)
617   {
618     GNUNET_SERVER_mst_destroy (s->msg_tk);
619     s->msg_tk = NULL;
620   }
621   GNUNET_free (s->addr);
622   GNUNET_free_non_null (s->server_recv);
623   GNUNET_free_non_null (s->server_send);
624   GNUNET_free (s);
625 }
626
627
628 struct Session *
629 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
630                 const void *addr, size_t addrlen)
631 {
632   struct Session *s = NULL;
633   struct GNUNET_ATS_Information ats;
634
635   GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) ||
636                  (addrlen == sizeof (struct IPv4HttpAddress)));
637
638
639
640
641   if (addrlen == sizeof (struct IPv4HttpAddress))
642   {
643     struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
644     struct sockaddr_in s4;
645
646     if (0 == ntohs(a4->u4_port))
647       return NULL;
648
649     s4.sin_family = AF_INET;
650     s4.sin_addr.s_addr = a4->ipv4_addr;
651     s4.sin_port = a4->u4_port;
652 #if HAVE_SOCKADDR_IN_SIN_LEN
653     s4.sin_len = sizeof (struct sockaddr_in);
654 #endif
655     ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s4, sizeof (struct sockaddr_in));
656   }
657   if (addrlen == sizeof (struct IPv6HttpAddress))
658   {
659     struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
660     struct sockaddr_in6 s6;
661
662     if (0 == ntohs(a6->u6_port))
663         return NULL;
664
665     s6.sin6_family = AF_INET6;
666     s6.sin6_addr = a6->ipv6_addr;
667     s6.sin6_port = a6->u6_port;
668 #if HAVE_SOCKADDR_IN_SIN_LEN
669     s6.sin6_len = sizeof (struct sockaddr_in6);
670 #endif
671     ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s6, sizeof (struct sockaddr_in6));
672   }
673
674   s = GNUNET_malloc (sizeof (struct Session));
675   memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
676   s->plugin = plugin;
677   s->addr = GNUNET_malloc (addrlen);
678   memcpy (s->addr, addr, addrlen);
679   s->addrlen = addrlen;
680   s->ats_address_network_type = ats.value;
681
682
683   start_session_timeout(s);
684   return s;
685 }
686
687
688 void
689 notify_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
690                     struct Session *s)
691 {
692   struct Plugin *plugin = cls;
693
694   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
695                    "Notifying transport about ending session %p (`%s')\n",
696                    s,
697                    http_plugin_address_to_string(NULL, s->addr,s->addrlen));
698
699   plugin->env->session_end (plugin->env->cls, peer, s);
700   delete_session (s);
701 }
702
703
704 /**
705  * Creates a new outbound session the transport service will use to send data to the
706  * peer
707  *
708  * @param cls the plugin
709  * @param address the address
710  * @return the session or NULL of max connections exceeded
711  */
712 static struct Session *
713 http_get_session (void *cls,
714                   const struct GNUNET_HELLO_Address *address)
715 {
716   struct Plugin *plugin = cls;
717   struct Session * s = NULL;
718   size_t addrlen;
719
720   GNUNET_assert (plugin != NULL);
721   GNUNET_assert (address != NULL);
722   GNUNET_assert (address->address != NULL);
723
724   /* find existing session */
725   s = lookup_session (plugin, address);
726   if (s != NULL)
727     return s;
728
729   if (plugin->max_connections <= plugin->cur_connections)
730   {
731     GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
732                      "Maximum number of connections reached, "
733                      "cannot connect to peer `%s'\n", GNUNET_i2s (&address->peer));
734     return NULL;
735   }
736
737   /* create new session */
738   addrlen = address->address_length;
739
740   GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) ||
741                  (addrlen == sizeof (struct IPv4HttpAddress)));
742
743   s = create_session (plugin, &address->peer, address->address, address->address_length);
744
745   /* add new session */
746   GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
747   /* initiate new connection */
748   if (GNUNET_SYSERR == client_connect (s))
749   {
750     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
751                      "Cannot connect to peer `%s' address `%s''\n",
752                      http_plugin_address_to_string(NULL, s->addr, s->addrlen),
753                      GNUNET_i2s (&s->target));
754     GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
755     delete_session (s);
756     return NULL;
757   }
758
759   return s;
760 }
761
762
763 /**
764  * Function that can be used by the transport service to transmit
765  * a message using the plugin.   Note that in the case of a
766  * peer disconnecting, the continuation MUST be called
767  * prior to the disconnect notification itself.  This function
768  * will be called with this peer's HELLO message to initiate
769  * a fresh connection to another peer.
770  *
771  * @param cls closure
772  * @param session which session must be used
773  * @param msgbuf the message to transmit
774  * @param msgbuf_size number of bytes in 'msgbuf'
775  * @param priority how important is the message (most plugins will
776  *                 ignore message priority and just FIFO)
777  * @param to how long to wait at most for the transmission (does not
778  *                require plugins to discard the message after the timeout,
779  *                just advisory for the desired delay; most plugins will ignore
780  *                this as well)
781  * @param cont continuation to call once the message has
782  *        been transmitted (or if the transport is ready
783  *        for the next transmission call; or if the
784  *        peer disconnected...); can be NULL
785  * @param cont_cls closure for cont
786  * @return number of bytes used (on the physical network, with overheads);
787  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
788  *         and does NOT mean that the message was not transmitted (DV)
789  */
790 static ssize_t
791 http_plugin_send (void *cls,
792                   struct Session *session,
793                   const char *msgbuf, size_t msgbuf_size,
794                   unsigned int priority,
795                   struct GNUNET_TIME_Relative to,
796                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
797 {
798   struct Plugin *plugin = cls;
799   struct HTTP_Message *msg;
800   struct Session *tmp;
801   size_t res = -1;
802
803   GNUNET_assert (plugin != NULL);
804   GNUNET_assert (session != NULL);
805
806   /* lookup if session is really existing */
807   tmp = plugin->head;
808   while (tmp != NULL)
809   {
810     if ((tmp == session) &&
811        (0 == memcmp (&session->target, &tmp->target, sizeof (struct GNUNET_PeerIdentity))) &&
812        (session->addrlen == tmp->addrlen) &&
813        (0 == memcmp (session->addr, tmp->addr, tmp->addrlen)))
814       break;
815     tmp = tmp->next;
816   }
817   if (tmp == NULL)
818   {
819     GNUNET_break_op (0);
820     return res;
821   }
822
823   /* create new message and schedule */
824   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
825   msg->next = NULL;
826   msg->size = msgbuf_size;
827   msg->pos = 0;
828   msg->buf = (char *) &msg[1];
829   msg->transmit_cont = cont;
830   msg->transmit_cont_cls = cont_cls;
831   memcpy (msg->buf, msgbuf, msgbuf_size);
832
833   reschedule_session_timeout (session);
834
835   if (session->inbound == GNUNET_NO)
836   {
837     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
838                      "Using outbound client session %p to send to `%s'\n", session,
839                      GNUNET_i2s (&session->target));
840     client_send (session, msg);
841     res = msgbuf_size;
842   }
843   if (session->inbound == GNUNET_YES)
844   {
845     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
846                      "Using inbound server %p session to send to `%s'\n", session,
847                      GNUNET_i2s (&session->target));
848     server_send (session, msg);
849     res = msgbuf_size;
850   }
851   return res;
852
853 }
854
855
856 /**
857  * Function that can be used to force the plugin to disconnect
858  * from the given peer and cancel all previous transmissions
859  * (and their continuationc).
860  *
861  * @param cls closure
862  * @param target peer from which to disconnect
863  */
864 static void
865 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
866 {
867   struct Plugin *plugin = cls;
868   struct Session *next = NULL;
869   struct Session *s = plugin->head;
870
871   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
872                    "Transport tells me to disconnect `%s'\n",
873                    GNUNET_i2s (target));
874   while (s != NULL)
875   {
876     next = s->next;
877     if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
878     {
879       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
880                        "Disconnecting %s session %p to `%s'\n",
881                        (s->inbound == GNUNET_NO) ? "outbound" : "inbound",
882                        s, GNUNET_i2s (target));
883
884       if (s->inbound == GNUNET_NO)
885         GNUNET_assert (GNUNET_OK == client_disconnect (s));
886       else
887         GNUNET_assert (GNUNET_OK == server_disconnect (s));
888     }
889     s = next;
890   }
891 }
892
893
894 static void *
895 find_address (struct Plugin *plugin, const struct sockaddr *addr, socklen_t addrlen)
896 {
897   int af;
898   struct IPv4HttpAddressWrapper *w_t4 = NULL;
899   struct IPv6HttpAddressWrapper *w_t6 = NULL;
900
901   af = addr->sa_family;
902   switch (af)
903   {
904   case AF_INET:
905     w_t4 = plugin->ipv4_addr_head;
906     struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
907
908     while (w_t4 != NULL)
909     {
910       int res = memcmp (&w_t4->addr.ipv4_addr,
911                         &a4->sin_addr,
912                         sizeof (struct in_addr));
913
914       if (res == 0)
915       {
916         if (a4->sin_port != w_t4->addr.u4_port)
917           res = -1;
918       }
919
920       if (0 == res)
921         break;
922       w_t4 = w_t4->next;
923     }
924     return w_t4;
925     break;
926   case AF_INET6:
927     w_t6 = plugin->ipv6_addr_head;
928     struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
929
930     while (w_t6)
931     {
932       int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
933                         sizeof (struct in6_addr));
934
935       if (res == 0)
936       {
937         if (a6->sin6_port != w_t6->addr6.u6_port)
938           res = -1;
939       }
940       if (0 == res)
941         break;
942       w_t6 = w_t6->next;
943     }
944     return w_t6;
945     break;
946   default:
947     return NULL;
948   }
949   return NULL;
950 }
951
952
953 static void
954 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
955                  socklen_t addrlen)
956 {
957   struct Plugin *plugin = cls;
958   struct IPv4HttpAddressWrapper *w_t4 = NULL;
959   struct IPv6HttpAddressWrapper *w_t6 = NULL;
960   int af;
961
962   af = addr->sa_family;
963   switch (af)
964   {
965   case AF_INET:
966     w_t4 = find_address (plugin, addr, addrlen);
967     if (w_t4 == NULL)
968     {
969       struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
970       w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
971       memcpy (&w_t4->addr.ipv4_addr, &a4->sin_addr, sizeof (struct in_addr));
972       w_t4->addr.u4_port = a4->sin_port;
973
974       GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
975                                    plugin->ipv4_addr_tail, w_t4);
976
977     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
978                      "Notifying transport to add IPv4 address `%s'\n",
979                      http_plugin_address_to_string (NULL, &w_t4->addr,
980                                                     sizeof (struct
981                                                             IPv4HttpAddress)));
982     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
983                                  sizeof (struct IPv4HttpAddress));
984     }
985     break;
986   case AF_INET6:
987     w_t6 = find_address (plugin, addr, addrlen);
988     if (w_t6 == NULL)
989     {
990       w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
991       struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
992       memcpy (&w_t6->addr6.ipv6_addr, &a6->sin6_addr, sizeof (struct in6_addr));
993       w_t6->addr6.u6_port = a6->sin6_port;
994
995       GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
996                                    plugin->ipv6_addr_tail, w_t6);
997
998       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
999                      "Notifying transport to add IPv6 address `%s'\n",
1000                      http_plugin_address_to_string (NULL, &w_t6->addr6,
1001                                                     sizeof (struct
1002                                                             IPv6HttpAddress)));
1003       plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
1004                                  sizeof (struct IPv6HttpAddress));
1005     }
1006     break;
1007   default:
1008     return;
1009   }
1010
1011 }
1012
1013
1014 static void
1015 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
1016                     socklen_t addrlen)
1017 {
1018   struct Plugin *plugin = cls;
1019   struct IPv4HttpAddressWrapper *w_t4 = NULL;
1020   struct IPv6HttpAddressWrapper *w_t6 = NULL;
1021   int af;
1022
1023   af = addr->sa_family;
1024   switch (af)
1025   {
1026   case AF_INET:
1027     w_t4 = find_address (plugin, addr, addrlen);
1028     if (w_t4 == NULL)
1029       return;
1030
1031
1032     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1033                      "Notifying transport to remove IPv4 address `%s'\n",
1034                      http_plugin_address_to_string (NULL, &w_t4->addr,
1035                                                     sizeof (struct
1036                                                             IPv4HttpAddress)));
1037     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
1038                                  sizeof (struct IPv4HttpAddress));
1039
1040     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
1041                                  w_t4);
1042     GNUNET_free (w_t4);
1043     break;
1044   case AF_INET6:
1045     w_t6 = find_address (plugin, addr, addrlen);
1046     if (w_t6 == NULL)
1047       return;
1048
1049     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1050                      "Notifying transport to remove IPv6 address `%s'\n",
1051                      http_plugin_address_to_string (NULL, &w_t6->addr6,
1052                                                     sizeof (struct
1053                                                             IPv6HttpAddress)));
1054
1055     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
1056                                  sizeof (struct IPv6HttpAddress));
1057
1058     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1059                                  w_t6);
1060     GNUNET_free (w_t6);
1061     break;
1062   default:
1063     return;
1064   }
1065 }
1066
1067
1068 /**
1069  * Our external IP address/port mapping has changed.
1070  *
1071  * @param cls closure, the 'struct LocalAddrList'
1072  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1073  *     the previous (now invalid) one
1074  * @param addr either the previous or the new public IP address
1075  * @param addrlen actual lenght of the address
1076  */
1077 static void
1078 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
1079                        socklen_t addrlen)
1080 {
1081   GNUNET_assert (cls != NULL);
1082   struct Plugin *plugin = cls;
1083
1084   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1085                    "NPMC called %s to address `%s'\n",
1086                    (add_remove == GNUNET_NO) ? "remove" : "add",
1087                    GNUNET_a2s (addr, addrlen));
1088
1089   switch (add_remove)
1090   {
1091   case GNUNET_YES:
1092     nat_add_address (cls, add_remove, addr, addrlen);
1093     break;
1094   case GNUNET_NO:
1095     nat_remove_address (cls, add_remove, addr, addrlen);
1096     break;
1097   }
1098 }
1099
1100
1101 void
1102 http_check_ipv6 (struct Plugin *plugin)
1103 {
1104   struct GNUNET_NETWORK_Handle *desc = NULL;
1105
1106   if (plugin->ipv6 == GNUNET_YES)
1107   {
1108     /* probe IPv6 support */
1109     desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
1110     if (NULL == desc)
1111     {
1112       if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
1113           (errno == EACCES))
1114       {
1115         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1116       }
1117       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1118                        _
1119                        ("Disabling IPv6 since it is not supported on this system!\n"));
1120       plugin->ipv6 = GNUNET_NO;
1121     }
1122     else
1123     {
1124       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
1125       desc = NULL;
1126     }
1127
1128     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1129                      "Testing IPv6 on this system: %s\n",
1130                      (plugin->ipv6 == GNUNET_YES) ? "successful" : "failed");
1131   }
1132 }
1133
1134
1135 int
1136 http_get_addresses (struct Plugin *plugin, const char *serviceName,
1137                     const struct GNUNET_CONFIGURATION_Handle *cfg,
1138                     struct sockaddr ***addrs, socklen_t ** addr_lens)
1139 {
1140   int disablev6;
1141   unsigned long long port;
1142   struct addrinfo hints;
1143   struct addrinfo *res;
1144   struct addrinfo *pos;
1145   struct addrinfo *next;
1146   unsigned int i;
1147   int resi;
1148   int ret;
1149   struct sockaddr **saddrs;
1150   socklen_t *saddrlens;
1151   char *hostname;
1152
1153   *addrs = NULL;
1154   *addr_lens = NULL;
1155
1156   disablev6 = !plugin->ipv6;
1157
1158   port = 0;
1159   if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
1160   {
1161     GNUNET_break (GNUNET_OK ==
1162                   GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
1163                                                          "PORT", &port));
1164     if (port > 65535)
1165     {
1166       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1167                   _
1168                   ("Require valid port number for service in configuration!\n"));
1169       return GNUNET_SYSERR;
1170     }
1171   }
1172   if (0 == port)
1173   {
1174     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
1175                      "Starting in listen only mode\n");
1176     return -1; /* listen only */
1177   }
1178
1179
1180   if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
1181   {
1182     GNUNET_break (GNUNET_OK ==
1183                   GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
1184                                                          "BINDTO", &hostname));
1185   }
1186   else
1187     hostname = NULL;
1188
1189   if (hostname != NULL)
1190   {
1191     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1192                      "Resolving `%s' since that is where `%s' will bind to.\n",
1193                      hostname, serviceName);
1194     memset (&hints, 0, sizeof (struct addrinfo));
1195     if (disablev6)
1196       hints.ai_family = AF_INET;
1197     if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1198         (res == NULL))
1199     {
1200       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
1201                   hostname, gai_strerror (ret));
1202       GNUNET_free (hostname);
1203       return GNUNET_SYSERR;
1204     }
1205     next = res;
1206     i = 0;
1207     while (NULL != (pos = next))
1208     {
1209       next = pos->ai_next;
1210       if ((disablev6) && (pos->ai_family == AF_INET6))
1211         continue;
1212       i++;
1213     }
1214     if (0 == i)
1215     {
1216       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1217                   _("Failed to find %saddress for `%s'.\n"),
1218                   disablev6 ? "IPv4 " : "", hostname);
1219       freeaddrinfo (res);
1220       GNUNET_free (hostname);
1221       return GNUNET_SYSERR;
1222     }
1223     resi = i;
1224     saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1225     saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1226     i = 0;
1227     next = res;
1228     while (NULL != (pos = next))
1229     {
1230       next = pos->ai_next;
1231       if ((disablev6) && (pos->ai_family == AF_INET6))
1232         continue;
1233       if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
1234         continue;               /* not TCP */
1235       if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
1236         continue;               /* huh? */
1237       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1238                        "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
1239                                                                   pos->ai_addrlen));
1240       if (pos->ai_family == AF_INET)
1241       {
1242         GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
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_in *) saddrs[i])->sin_port = htons (port);
1247       }
1248       else
1249       {
1250         GNUNET_assert (pos->ai_family == AF_INET6);
1251         GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
1252         saddrlens[i] = pos->ai_addrlen;
1253         saddrs[i] = GNUNET_malloc (saddrlens[i]);
1254         memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1255         ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1256       }
1257       i++;
1258     }
1259     GNUNET_free (hostname);
1260     freeaddrinfo (res);
1261     resi = i;
1262   }
1263   else
1264   {
1265     /* will bind against everything, just set port */
1266     if (disablev6)
1267     {
1268       /* V4-only */
1269       resi = 1;
1270       i = 0;
1271       saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1272       saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1273
1274       saddrlens[i] = sizeof (struct sockaddr_in);
1275       saddrs[i] = GNUNET_malloc (saddrlens[i]);
1276 #if HAVE_SOCKADDR_IN_SIN_LEN
1277       ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1278 #endif
1279       ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1280       ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1281     }
1282     else
1283     {
1284       /* dual stack */
1285       resi = 2;
1286       saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1287       saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1288       i = 0;
1289       saddrlens[i] = sizeof (struct sockaddr_in6);
1290       saddrs[i] = GNUNET_malloc (saddrlens[i]);
1291 #if HAVE_SOCKADDR_IN_SIN_LEN
1292       ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1293 #endif
1294       ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1295       ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1296       i++;
1297       saddrlens[i] = sizeof (struct sockaddr_in);
1298       saddrs[i] = GNUNET_malloc (saddrlens[i]);
1299 #if HAVE_SOCKADDR_IN_SIN_LEN
1300       ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1301 #endif
1302       ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1303       ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1304     }
1305   }
1306   *addrs = saddrs;
1307   *addr_lens = saddrlens;
1308   return resi;
1309 }
1310
1311 static void
1312 start_report_addresses (struct Plugin *plugin)
1313 {
1314   int res = GNUNET_OK;
1315   struct sockaddr **addrs;
1316   socklen_t *addrlens;
1317
1318   res =
1319       http_get_addresses (plugin, plugin->name, plugin->env->cfg, &addrs,
1320                           &addrlens);
1321   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1322                    _("Found %u addresses to report to NAT service\n"), res);
1323
1324   if (GNUNET_SYSERR == res)
1325   {
1326     plugin->nat = NULL;
1327     return;
1328   }
1329
1330   plugin->nat =
1331       GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
1332                            (unsigned int) res,
1333                            (const struct sockaddr **) addrs, addrlens,
1334                            &nat_port_map_callback, NULL, plugin);
1335   while (res > 0)
1336   {
1337     res--;
1338     GNUNET_assert (addrs[res] != NULL);
1339     GNUNET_free (addrs[res]);
1340   }
1341   GNUNET_free_non_null (addrs);
1342   GNUNET_free_non_null (addrlens);
1343 }
1344
1345
1346 static void
1347 stop_report_addresses (struct Plugin *plugin)
1348 {
1349
1350   /* Stop NAT handle */
1351   if (NULL != plugin->nat)
1352     GNUNET_NAT_unregister (plugin->nat);
1353
1354   /* Clean up addresses */
1355   struct IPv4HttpAddressWrapper *w_t4;
1356   struct IPv6HttpAddressWrapper *w_t6;
1357
1358   while (plugin->ipv4_addr_head != NULL)
1359   {
1360     w_t4 = plugin->ipv4_addr_head;
1361     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
1362                                  w_t4);
1363     GNUNET_free (w_t4);
1364   }
1365
1366   while (plugin->ipv6_addr_head != NULL)
1367   {
1368     w_t6 = plugin->ipv6_addr_head;
1369     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1370                                  w_t6);
1371     GNUNET_free (w_t6);
1372   }
1373 }
1374
1375
1376 static int
1377 configure_plugin (struct Plugin *plugin)
1378 {
1379   int res = GNUNET_OK;
1380
1381   /* Use IPv4? */
1382   if (GNUNET_CONFIGURATION_have_value
1383       (plugin->env->cfg, plugin->name, "USE_IPv4"))
1384   {
1385     plugin->ipv4 =
1386         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1387                                               "USE_IPv4");
1388   }
1389   else
1390     plugin->ipv4 = GNUNET_YES;
1391
1392   /* Use IPv6? */
1393   if (GNUNET_CONFIGURATION_have_value
1394       (plugin->env->cfg, plugin->name, "USE_IPv6"))
1395   {
1396     plugin->ipv6 =
1397         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1398                                               "USE_IPv6");
1399   }
1400   else
1401     plugin->ipv6 = GNUNET_YES;
1402
1403   if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
1404   {
1405     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1406                      _
1407                      ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
1408                      plugin->name);
1409     res = GNUNET_SYSERR;
1410   }
1411
1412   /* Reading port number from config file */
1413   unsigned long long port;
1414
1415   if ((GNUNET_OK !=
1416        GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1417                                               "PORT", &port)) || (port > 65535))
1418   {
1419     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1420                      _("Port is required! Fix in configuration\n"),
1421                      plugin->name);
1422     res = GNUNET_SYSERR;
1423     goto fail;
1424   }
1425   plugin->port = port;
1426
1427   plugin->client_only = GNUNET_NO;
1428   if (plugin->port == 0)
1429   {
1430     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1431                      _("Port 0, client only mode\n"));
1432     plugin->client_only = GNUNET_YES;
1433   }
1434
1435   char *bind4_address = NULL;
1436
1437   if ((plugin->ipv4 == GNUNET_YES) &&
1438       (GNUNET_YES ==
1439        GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1440                                               "BINDTO", &bind4_address)))
1441   {
1442     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1443                      "Binding %s plugin to specific IPv4 address: `%s'\n",
1444                      plugin->protocol, bind4_address);
1445     plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1446     if (1 !=
1447         inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
1448     {
1449       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1450                        _
1451                        ("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1452                        bind4_address, plugin->protocol);
1453       GNUNET_free (plugin->server_addr_v4);
1454       plugin->server_addr_v4 = NULL;
1455     }
1456     else
1457     {
1458       plugin->server_addr_v4->sin_family = AF_INET;
1459       plugin->server_addr_v4->sin_port = htons (plugin->port);
1460     }
1461     GNUNET_free (bind4_address);
1462   }
1463
1464
1465   char *bind6_address = NULL;
1466
1467   if ((plugin->ipv6 == GNUNET_YES) &&
1468       (GNUNET_YES ==
1469        GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1470                                               "BINDTO6", &bind6_address)))
1471   {
1472     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1473                      "Binding %s plugin to specific IPv6 address: `%s'\n",
1474                      plugin->protocol, bind6_address);
1475     plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1476     if (1 !=
1477         inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
1478     {
1479       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1480                        _
1481                        ("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1482                        bind6_address, plugin->protocol);
1483       GNUNET_free (plugin->server_addr_v6);
1484       plugin->server_addr_v6 = NULL;
1485     }
1486     else
1487     {
1488       plugin->server_addr_v6->sin6_family = AF_INET6;
1489       plugin->server_addr_v6->sin6_port = htons (plugin->port);
1490     }
1491     GNUNET_free (bind6_address);
1492   }
1493
1494
1495   /* Optional parameters */
1496   unsigned long long maxneigh;
1497
1498   if (GNUNET_OK !=
1499       GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1500                                              "MAX_CONNECTIONS", &maxneigh))
1501     maxneigh = 128;
1502   plugin->max_connections = maxneigh;
1503
1504 fail:
1505   return res;
1506 }
1507
1508 #define TESTING GNUNET_NO
1509
1510 #if TESTING
1511 #define TIMEOUT_LOG GNUNET_ERROR_TYPE_ERROR
1512 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
1513 #else
1514 #define TIMEOUT_LOG GNUNET_ERROR_TYPE_DEBUG
1515 #define TIMEOUT GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
1516 #endif
1517
1518
1519 /**
1520  * Session was idle, so disconnect it
1521  */
1522 static void
1523 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1524 {
1525   GNUNET_assert (NULL != cls);
1526   struct Session *s = cls;
1527
1528   s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1529   GNUNET_log (TIMEOUT_LOG,
1530               "Session %p was idle for %llu ms, disconnecting\n",
1531               s, (unsigned long long) TIMEOUT.rel_value);
1532
1533   /* call session destroy function */
1534   if (s->inbound == GNUNET_NO)
1535     GNUNET_assert (GNUNET_OK == client_disconnect (s));
1536   else
1537     GNUNET_assert (GNUNET_OK == server_disconnect (s));
1538 }
1539
1540
1541 /**
1542 * Start session timeout
1543 */
1544 static void
1545 start_session_timeout (struct Session *s)
1546 {
1547  GNUNET_assert (NULL != s);
1548  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1549  s->timeout_task =  GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1550                                                   &session_timeout,
1551                                                   s);
1552  GNUNET_log (TIMEOUT_LOG,
1553              "Timeout for session %p set to %llu ms\n",
1554              s,  (unsigned long long) TIMEOUT.rel_value);
1555 }
1556
1557
1558 /**
1559 * Increment session timeout due to activity
1560 */
1561 static void
1562 reschedule_session_timeout (struct Session *s)
1563 {
1564  GNUNET_assert (NULL != s);
1565  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1566
1567  GNUNET_SCHEDULER_cancel (s->timeout_task);
1568  s->timeout_task =  GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1569                                                   &session_timeout,
1570                                                   s);
1571  GNUNET_log (TIMEOUT_LOG,
1572              "Timeout rescheduled for session %p set to %llu ms\n",
1573              s, (unsigned long long) TIMEOUT.rel_value);
1574 }
1575
1576
1577 /**
1578 * Cancel timeout
1579 */
1580 static void
1581 stop_session_timeout (struct Session *s)
1582 {
1583  GNUNET_assert (NULL != s);
1584
1585  if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1586  {
1587    GNUNET_SCHEDULER_cancel (s->timeout_task);
1588    s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1589    GNUNET_log (TIMEOUT_LOG,
1590                "Timeout stopped for session %p\n",
1591                s);
1592  }
1593 }
1594
1595 /**
1596  * Entry point for the plugin.
1597  */
1598 void *
1599 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1600 {
1601   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1602   struct GNUNET_TRANSPORT_PluginFunctions *api;
1603   struct Plugin *plugin;
1604   int res;
1605
1606   if (NULL == env->receive)
1607   {
1608     /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1609        initialze the plugin or the API */
1610     api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1611     api->cls = NULL;
1612     api->address_pretty_printer = &http_plugin_address_pretty_printer;
1613     api->address_to_string = &http_plugin_address_to_string;
1614     api->string_to_address = &http_string_to_address;
1615     return api;
1616   }
1617
1618   plugin = GNUNET_malloc (sizeof (struct Plugin));
1619   plugin->env = env;
1620   plugin->outbound_sessions = 0;
1621   plugin->inbound_sessions = 0;
1622   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1623   api->cls = plugin;
1624   api->disconnect = &http_plugin_disconnect;
1625   api->address_pretty_printer = &http_plugin_address_pretty_printer;
1626   api->check_address = &http_plugin_address_suggested;
1627   api->address_to_string = &http_plugin_address_to_string;
1628   api->string_to_address = &http_string_to_address;
1629   api->get_session = &http_get_session;
1630   api->send = &http_plugin_send;
1631
1632 #if BUILD_HTTPS
1633   plugin->name = "transport-https";
1634   plugin->protocol = "https";
1635 #else
1636   plugin->name = "transport-http";
1637   plugin->protocol = "http";
1638 #endif
1639   /* Configure plugin from configuration */
1640   res = configure_plugin (plugin);
1641   if (res == GNUNET_SYSERR)
1642   {
1643     GNUNET_free_non_null (plugin->server_addr_v4);
1644     GNUNET_free_non_null (plugin->server_addr_v6);
1645     GNUNET_free (plugin);
1646     GNUNET_free (api);
1647     return NULL;
1648   }
1649
1650   /* checking IPv6 support */
1651   http_check_ipv6 (plugin);
1652
1653   /* Start client */
1654   res = client_start (plugin);
1655   if (res == GNUNET_SYSERR)
1656   {
1657     GNUNET_free_non_null (plugin->server_addr_v4);
1658     GNUNET_free_non_null (plugin->server_addr_v6);
1659     GNUNET_free (plugin);
1660     GNUNET_free (api);
1661     return NULL;
1662   }
1663
1664   /* Start server */
1665   if (plugin->client_only == GNUNET_NO)
1666   {
1667     res = server_start (plugin);
1668     if (res == GNUNET_SYSERR)
1669     {
1670       server_stop (plugin);
1671       client_stop (plugin);
1672
1673       GNUNET_free_non_null (plugin->server_addr_v4);
1674       GNUNET_free_non_null (plugin->server_addr_v6);
1675       GNUNET_free (plugin);
1676       GNUNET_free (api);
1677       return NULL;
1678     }
1679   }
1680   /* Report addresses to transport service */
1681   start_report_addresses (plugin);
1682
1683   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1684                    "Plugin `%s' loaded\n", plugin->name);
1685   return api;
1686 }
1687
1688
1689 /**
1690  * Exit point from the plugin.
1691  */
1692 void *
1693 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1694 {
1695   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1696   struct Plugin *plugin = api->cls;
1697   struct Session *s;
1698   struct Session *next;
1699
1700   if (NULL == plugin)
1701   {
1702     GNUNET_free (api);
1703     return NULL;
1704   }
1705
1706   /* Stop reporting addresses to transport service */
1707   stop_report_addresses (plugin);
1708
1709   /* cleaning up sessions */
1710   s = plugin->head;
1711   while (s != NULL)
1712   {
1713     next = s->next;
1714     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1715                      "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1716     if (s->inbound == GNUNET_NO)
1717       GNUNET_assert (GNUNET_OK == client_disconnect (s));
1718     else
1719       GNUNET_assert (GNUNET_OK == server_disconnect (s));
1720     s = next;
1721   }
1722
1723   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping server\n");
1724   /* Stop server */
1725   server_stop (plugin);
1726
1727   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping client\n");
1728   /* Stop client */
1729   client_stop (plugin);
1730
1731   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1732                    "Plugin `%s' unloaded\n", plugin->name);
1733   GNUNET_free_non_null (plugin->server_addr_v4);
1734   GNUNET_free_non_null (plugin->server_addr_v6);
1735   GNUNET_free (plugin);
1736   GNUNET_free (api);
1737   return NULL;
1738 }
1739
1740 /* end of plugin_transport_http.c */