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