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