- clang
[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 (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 *s = NULL;
394   struct Session *t = NULL;
395   int e_peer;
396   int e_addr;
397
398   t = plugin->head;
399   if (t == NULL)
400     return NULL;
401   while (t != NULL)
402   {
403 #if 0
404     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
405                      "Comparing peer `%s' address `%s' len %i session %X to \n",
406                      GNUNET_i2s (target), GNUNET_a2s (addr, addrlen), addrlen,
407                      session);
408     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
409                      "peer `%s' address `%s' len %i session %X \n\n",
410                      GNUNET_i2s (&t->target), GNUNET_a2s (t->addr, t->addrlen),
411                      t->addrlen, t);
412     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, "memcmp %i \n",
413                      memcmp (addr, t->addr, addrlen));
414 #endif
415     e_peer = GNUNET_NO;
416     e_addr = GNUNET_NO;
417
418     if (0 == memcmp (target, &t->target, sizeof (struct GNUNET_PeerIdentity)))
419     {
420       e_peer = GNUNET_YES;
421       if (addrlen == t->addrlen)
422       {
423         if (0 == memcmp (addr, t->addr, addrlen))
424         {
425           e_addr = GNUNET_YES;
426         }
427       }
428       if ((t == session))
429       {
430         if (t->addrlen == session->addrlen)
431         {
432           if (0 == memcmp (session->addr, t->addr, t->addrlen))
433           {
434             e_addr = GNUNET_YES;
435           }
436         }
437       }
438     }
439
440     if ((e_peer == GNUNET_YES) && (force_address == GNUNET_NO))
441     {
442       s = t;
443       break;
444     }
445     if ((e_peer == GNUNET_YES) && (force_address == GNUNET_YES) &&
446         (e_addr == GNUNET_YES))
447     {
448       s = t;
449       break;
450     }
451     if ((e_peer == GNUNET_YES) && (force_address == GNUNET_SYSERR))
452     {
453       s = t;
454       break;
455     }
456     if (s != NULL)
457       break;
458     t = t->next;
459   }
460
461
462   return s;
463 }
464
465 void
466 delete_session (struct Session *s)
467 {
468   if (s->msg_tk != NULL)
469   {
470     GNUNET_SERVER_mst_destroy (s->msg_tk);
471     s->msg_tk = NULL;
472   }
473   GNUNET_free (s->addr);
474   GNUNET_free_non_null (s->server_recv);
475   GNUNET_free_non_null (s->server_send);
476   GNUNET_free (s);
477 }
478
479 struct Session *
480 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
481                 const void *addr, size_t addrlen,
482                 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
483 {
484   struct Session *s = NULL;
485
486   GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) ||
487                  (addrlen == sizeof (struct IPv4HttpAddress)));
488   s = GNUNET_malloc (sizeof (struct Session));
489   memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
490   s->plugin = plugin;
491   s->addr = GNUNET_malloc (addrlen);
492   memcpy (s->addr, addr, addrlen);
493   s->addrlen = addrlen;
494   s->next = NULL;
495   s->next_receive = GNUNET_TIME_absolute_get_zero ();
496   s->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
497   return s;
498 }
499
500 void
501 notify_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
502                     struct Session *s)
503 {
504   struct Plugin *plugin = cls;
505
506   plugin->env->session_end (plugin->env->cls, peer, s);
507   GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
508   delete_session (s);
509 }
510
511
512 /**
513  * Function that can be used by the transport service to transmit
514  * a message using the plugin.   Note that in the case of a
515  * peer disconnecting, the continuation MUST be called
516  * prior to the disconnect notification itself.  This function
517  * will be called with this peer's HELLO message to initiate
518  * a fresh connection to another peer.
519  *
520  * @param cls closure
521  * @param target who should receive this message
522  * @param msgbuf the message to transmit
523  * @param msgbuf_size number of bytes in 'msgbuf'
524  * @param priority how important is the message (most plugins will
525  *                 ignore message priority and just FIFO)
526  * @param to how long to wait at most for the transmission (does not
527  *                require plugins to discard the message after the timeout,
528  *                just advisory for the desired delay; most plugins will ignore
529  *                this as well)
530  * @param session which session must be used (or NULL for "any")
531  * @param addr the address to use (can be NULL if the plugin
532  *                is "on its own" (i.e. re-use existing TCP connection))
533  * @param addrlen length of the address in bytes
534  * @param force_address GNUNET_YES if the plugin MUST use the given address,
535  *                GNUNET_NO means the plugin may use any other address and
536  *                GNUNET_SYSERR means that only reliable existing
537  *                bi-directional connections should be used (regardless
538  *                of address)
539  * @param cont continuation to call once the message has
540  *        been transmitted (or if the transport is ready
541  *        for the next transmission call; or if the
542  *        peer disconnected...); can be NULL
543  * @param cont_cls closure for cont
544  * @return number of bytes used (on the physical network, with overheads);
545  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
546  *         and does NOT mean that the message was not transmitted (DV)
547  */
548 static ssize_t
549 http_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
550                   const char *msgbuf, size_t msgbuf_size, unsigned int priority,
551                   struct GNUNET_TIME_Relative to, struct Session *session,
552                   const void *addr, size_t addrlen, int force_address,
553                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
554 {
555   struct Plugin *plugin = cls;
556   struct HTTP_Message *msg;
557   struct Session *s;
558
559   GNUNET_assert (plugin != NULL);
560
561   int res = GNUNET_SYSERR;
562
563 #if DEBUG_HTTP
564   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
565                    "Sending %u bytes to peer `%s' on address `%s' %X %i\n",
566                    msgbuf_size, GNUNET_i2s (target), ((addr != NULL) &&
567                                                       (addrlen !=
568                                                        0)) ?
569                    http_plugin_address_to_string (plugin, addr,
570                                                   addrlen) : "<inbound>",
571                    session, force_address);
572 #endif
573
574
575
576   if (addrlen != 0)
577     GNUNET_assert ((addrlen == sizeof (struct IPv4HttpAddress)) ||
578                    (addrlen == sizeof (struct IPv6HttpAddress)));
579
580
581   /* look for existing connection */
582   s = lookup_session (plugin, target, session, addr, addrlen, 1);
583 #if DEBUG_HTTP
584   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
585                    "%s existing session: %s\n",
586                    (s != NULL) ? "Found" : "NOT Found", ((s != NULL) &&
587                                                          (s->inbound ==
588                                                           GNUNET_YES)) ?
589                    "inbound" : "outbound");
590 #endif
591
592   /* create new outbound connection */
593   if (s == NULL)
594   {
595     if (plugin->max_connections <= plugin->cur_connections)
596     {
597       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
598                        "Maximum number of connections reached, "
599                        "cannot connect to peer `%s'\n", GNUNET_i2s (target));
600       return res;
601     }
602
603 #if DEBUG_HTTP
604     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
605                      "Initiiating new connection to peer `%s'\n",
606                      GNUNET_i2s (target));
607 #endif
608     int res = GNUNET_OK;
609     struct GNUNET_ATS_Information ats;
610     if ((addrlen == sizeof (struct IPv4HttpAddress)) && (addr != NULL))
611     {
612       struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
613       struct sockaddr_in s4;
614
615       s4.sin_family = AF_INET;
616       s4.sin_addr.s_addr = a4->ipv4_addr;
617       s4.sin_port = a4->u4_port;
618 #if HAVE_SOCKADDR_IN_SIN_LEN
619       s4.sin_len = sizeof (struct sockaddr_in);
620 #endif
621       ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s4, sizeof (struct sockaddr_in));
622
623       if ((ntohs (a4->u4_port) == 0) || (plugin->ipv4 == GNUNET_NO))
624         res = GNUNET_SYSERR;
625     }
626     if ((addrlen == sizeof (struct IPv6HttpAddress)) && (addr != NULL))
627     {
628       struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
629       struct sockaddr_in6 s6;
630
631       s6.sin6_family = AF_INET6;
632       s6.sin6_addr = a6->ipv6_addr;
633       s6.sin6_port = a6->u6_port;
634 #if HAVE_SOCKADDR_IN_SIN_LEN
635       s6.sin6_len = sizeof (struct sockaddr_in6);
636 #endif
637       ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s6, sizeof (struct sockaddr_in6));
638
639       if ((ntohs (a6->u6_port) == 0) || (plugin->ipv6 == GNUNET_NO))
640         res = GNUNET_SYSERR;
641     }
642     if (res == GNUNET_OK)
643     {
644       s = create_session (plugin, target, addr, addrlen, cont, cont_cls);
645       s->ats_address_network_type = ats.value;
646       GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
647       // initiate new connection
648       res = client_connect (s);
649     }
650     if (res == GNUNET_SYSERR)
651     {
652       if (s != NULL)
653       {
654         GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
655         delete_session (s);
656       }
657       return GNUNET_SYSERR;
658     }
659   }
660
661   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
662   msg->next = NULL;
663   msg->size = msgbuf_size;
664   msg->pos = 0;
665   msg->buf = (char *) &msg[1];
666   msg->transmit_cont = cont;
667   msg->transmit_cont_cls = cont_cls;
668   memcpy (msg->buf, msgbuf, msgbuf_size);
669
670   if (s->inbound == GNUNET_NO)
671   {
672 #if DEBUG_HTTP
673     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
674                      "Using outbound client session %p to send to `%s'\n", s,
675                      GNUNET_i2s (target));
676 #endif
677
678     client_send (s, msg);
679     res = msgbuf_size;
680   }
681   if (s->inbound == GNUNET_YES)
682   {
683 #if DEBUG_HTTP
684     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
685                      "Using inbound server %p session to send to `%s'\n", s,
686                      GNUNET_i2s (target));
687 #endif
688
689     server_send (s, msg);
690     res = msgbuf_size;
691   }
692   return res;
693 }
694
695
696 /**
697  * Function that can be used to force the plugin to disconnect
698  * from the given peer and cancel all previous transmissions
699  * (and their continuationc).
700  *
701  * @param cls closure
702  * @param target peer from which to disconnect
703  */
704 static void
705 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
706 {
707   struct Plugin *plugin = cls;
708   struct Session *next = NULL;
709   struct Session *s = plugin->head;
710
711   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
712                    "Transport tells me to disconnect `%s'\n",
713                    GNUNET_i2s (target));
714   while (s != NULL)
715   {
716     next = s->next;
717     if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
718     {
719       if (s->inbound == GNUNET_NO)
720         GNUNET_assert (GNUNET_OK == client_disconnect (s));
721       else
722         GNUNET_assert (GNUNET_OK == server_disconnect (s));
723       GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
724
725       struct HTTP_Message *msg = s->msg_head;
726       struct HTTP_Message *tmp = NULL;
727
728       while (msg != NULL)
729       {
730         tmp = msg->next;
731
732         GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
733         if (msg->transmit_cont != NULL)
734         {
735           msg->transmit_cont (msg->transmit_cont_cls, target, GNUNET_SYSERR);
736         }
737         GNUNET_free (msg);
738         msg = tmp;
739       }
740
741       delete_session (s);
742     }
743     s = next;
744   }
745 }
746
747 static void
748 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
749                  socklen_t addrlen)
750 {
751   struct Plugin *plugin = cls;
752   struct IPv4HttpAddressWrapper *w_t4 = NULL;
753   struct IPv6HttpAddressWrapper *w_t6 = NULL;
754   int af;
755
756   af = addr->sa_family;
757   switch (af)
758   {
759   case AF_INET:
760     w_t4 = plugin->ipv4_addr_head;
761     struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
762
763     while (w_t4 != NULL)
764     {
765       int res = memcmp (&w_t4->addr.ipv4_addr,
766                         &a4->sin_addr,
767                         sizeof (struct in_addr));
768
769       if (res == 0)
770       {
771         if (a4->sin_port != w_t4->addr.u4_port)
772           res = -1;
773       }
774
775       if (0 == res)
776         break;
777       w_t4 = w_t4->next;
778     }
779     if (w_t4 == NULL)
780     {
781       w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
782       memcpy (&w_t4->addr.ipv4_addr, &a4->sin_addr, sizeof (struct in_addr));
783       w_t4->addr.u4_port = a4->sin_port;
784
785       GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
786                                    plugin->ipv4_addr_tail, w_t4);
787     }
788 #if DEBUG_HTTP
789     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
790                      "Notifying transport to add IPv4 address `%s'\n",
791                      http_plugin_address_to_string (NULL, &w_t4->addr,
792                                                     sizeof (struct
793                                                             IPv4HttpAddress)));
794 #endif
795     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
796                                  sizeof (struct IPv4HttpAddress));
797
798     break;
799   case AF_INET6:
800     w_t6 = plugin->ipv6_addr_head;
801     struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
802
803     while (w_t6)
804     {
805       int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
806                         sizeof (struct in6_addr));
807
808       if (res == 0)
809       {
810         if (a6->sin6_port != w_t6->addr6.u6_port)
811           res = -1;
812       }
813       if (0 == res)
814         break;
815       w_t6 = w_t6->next;
816     }
817     if (w_t6 == NULL)
818     {
819       w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
820
821       memcpy (&w_t6->addr6.ipv6_addr, &a6->sin6_addr, sizeof (struct in6_addr));
822       w_t6->addr6.u6_port = a6->sin6_port;
823
824       GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
825                                    plugin->ipv6_addr_tail, w_t6);
826     }
827 #if DEBUG_HTTP
828     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
829                      "Notifying transport to add IPv6 address `%s'\n",
830                      http_plugin_address_to_string (NULL, &w_t6->addr6,
831                                                     sizeof (struct
832                                                             IPv6HttpAddress)));
833 #endif
834     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
835                                  sizeof (struct IPv6HttpAddress));
836     break;
837   default:
838     return;
839   }
840
841 }
842
843 static void
844 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
845                     socklen_t addrlen)
846 {
847   struct Plugin *plugin = cls;
848   struct IPv4HttpAddressWrapper *w_t4 = NULL;
849   struct IPv6HttpAddressWrapper *w_t6 = NULL;
850   int af;
851
852   af = addr->sa_family;
853   switch (af)
854   {
855   case AF_INET:
856     w_t4 = plugin->ipv4_addr_head;
857     struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
858
859     while (w_t4 != NULL)
860     {
861       int res = memcmp (&w_t4->addr.ipv4_addr,
862                         &a4->sin_addr,
863                         sizeof (struct in_addr));
864
865       if (res == 0)
866       {
867         if (a4->sin_port != w_t4->addr.u4_port)
868           res = -1;
869       }
870
871       if (0 == res)
872         break;
873       w_t4 = w_t4->next;
874     }
875     if (w_t4 == NULL)
876       return;
877
878 #if DEBUG_HTTP
879     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
880                      "Notifying transport to remove IPv4 address `%s'\n",
881                      http_plugin_address_to_string (NULL, &w_t4->addr,
882                                                     sizeof (struct
883                                                             IPv4HttpAddress)));
884 #endif
885     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
886                                  sizeof (struct IPv4HttpAddress));
887
888     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
889                                  w_t4);
890     GNUNET_free (w_t4);
891     break;
892   case AF_INET6:
893     w_t6 = plugin->ipv6_addr_head;
894     struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
895
896     while (w_t6)
897     {
898       int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
899                         sizeof (struct in6_addr));
900
901       if (res == 0)
902       {
903         if (a6->sin6_port != w_t6->addr6.u6_port)
904           res = -1;
905       }
906       if (0 == res)
907         break;
908       w_t6 = w_t6->next;
909     }
910     if (w_t6 == NULL)
911       return;
912 #if DEBUG_HTTP
913     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
914                      "Notifying transport to remove IPv6 address `%s'\n",
915                      http_plugin_address_to_string (NULL, &w_t6->addr6,
916                                                     sizeof (struct
917                                                             IPv6HttpAddress)));
918 #endif
919     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
920                                  sizeof (struct IPv6HttpAddress));
921
922     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
923                                  w_t6);
924     GNUNET_free (w_t6);
925     break;
926   default:
927     return;
928   }
929
930 }
931
932 /**
933  * Our external IP address/port mapping has changed.
934  *
935  * @param cls closure, the 'struct LocalAddrList'
936  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
937  *     the previous (now invalid) one
938  * @param addr either the previous or the new public IP address
939  * @param addrlen actual lenght of the address
940  */
941 static void
942 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
943                        socklen_t addrlen)
944 {
945   GNUNET_assert (cls != NULL);
946 #if DEBUG_HTTP
947   struct Plugin *plugin = cls;
948 #endif
949 #if DEBUG_HTTP
950   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
951                    "NPMC called %s to address `%s'\n",
952                    (add_remove == GNUNET_NO) ? "remove" : "add",
953                    GNUNET_a2s (addr, addrlen));
954 #endif
955   switch (add_remove)
956   {
957   case GNUNET_YES:
958     nat_add_address (cls, add_remove, addr, addrlen);
959     break;
960   case GNUNET_NO:
961     nat_remove_address (cls, add_remove, addr, addrlen);
962     break;
963   }
964 }
965
966 void
967 http_check_ipv6 (struct Plugin *plugin)
968 {
969   struct GNUNET_NETWORK_Handle *desc = NULL;
970
971   if (plugin->ipv6 == GNUNET_YES)
972   {
973     /* probe IPv6 support */
974     desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
975     if (NULL == desc)
976     {
977       if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
978           (errno == EACCES))
979       {
980         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
981       }
982       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
983                        _
984                        ("Disabling IPv6 since it is not supported on this system!\n"));
985       plugin->ipv6 = GNUNET_NO;
986     }
987     else
988     {
989       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
990       desc = NULL;
991     }
992
993     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
994                      "Testing IPv6 on this system: %s\n",
995                      (plugin->ipv6 == GNUNET_YES) ? "successful" : "failed");
996   }
997 }
998
999 int
1000 http_get_addresses (struct Plugin *plugin, const char *serviceName,
1001                     const struct GNUNET_CONFIGURATION_Handle *cfg,
1002                     struct sockaddr ***addrs, socklen_t ** addr_lens)
1003 {
1004   int disablev6;
1005   unsigned long long port;
1006   struct addrinfo hints;
1007   struct addrinfo *res;
1008   struct addrinfo *pos;
1009   struct addrinfo *next;
1010   unsigned int i;
1011   int resi;
1012   int ret;
1013   struct sockaddr **saddrs;
1014   socklen_t *saddrlens;
1015   char *hostname;
1016
1017   *addrs = NULL;
1018   *addr_lens = NULL;
1019
1020   disablev6 = !plugin->ipv6;
1021
1022   port = 0;
1023   if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
1024   {
1025     GNUNET_break (GNUNET_OK ==
1026                   GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
1027                                                          "PORT", &port));
1028     if (port > 65535)
1029     {
1030       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1031                   _
1032                   ("Require valid port number for service in configuration!\n"));
1033       return GNUNET_SYSERR;
1034     }
1035   }
1036
1037   if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
1038   {
1039     GNUNET_break (GNUNET_OK ==
1040                   GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
1041                                                          "BINDTO", &hostname));
1042   }
1043   else
1044     hostname = NULL;
1045
1046   if (hostname != NULL)
1047   {
1048     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1049                      "Resolving `%s' since that is where `%s' will bind to.\n",
1050                      hostname, serviceName);
1051     memset (&hints, 0, sizeof (struct addrinfo));
1052     if (disablev6)
1053       hints.ai_family = AF_INET;
1054     if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1055         (res == NULL))
1056     {
1057       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
1058                   hostname, gai_strerror (ret));
1059       GNUNET_free (hostname);
1060       return GNUNET_SYSERR;
1061     }
1062     next = res;
1063     i = 0;
1064     while (NULL != (pos = next))
1065     {
1066       next = pos->ai_next;
1067       if ((disablev6) && (pos->ai_family == AF_INET6))
1068         continue;
1069       i++;
1070     }
1071     if (0 == i)
1072     {
1073       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1074                   _("Failed to find %saddress for `%s'.\n"),
1075                   disablev6 ? "IPv4 " : "", hostname);
1076       freeaddrinfo (res);
1077       GNUNET_free (hostname);
1078       return GNUNET_SYSERR;
1079     }
1080     resi = i;
1081     saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1082     saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1083     i = 0;
1084     next = res;
1085     while (NULL != (pos = next))
1086     {
1087       next = pos->ai_next;
1088       if ((disablev6) && (pos->ai_family == AF_INET6))
1089         continue;
1090       if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
1091         continue;               /* not TCP */
1092       if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
1093         continue;               /* huh? */
1094       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1095                        "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
1096                                                                   pos->ai_addrlen));
1097       if (pos->ai_family == AF_INET)
1098       {
1099         GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
1100         saddrlens[i] = pos->ai_addrlen;
1101         saddrs[i] = GNUNET_malloc (saddrlens[i]);
1102         memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1103         ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1104       }
1105       else
1106       {
1107         GNUNET_assert (pos->ai_family == AF_INET6);
1108         GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
1109         saddrlens[i] = pos->ai_addrlen;
1110         saddrs[i] = GNUNET_malloc (saddrlens[i]);
1111         memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1112         ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1113       }
1114       i++;
1115     }
1116     GNUNET_free (hostname);
1117     freeaddrinfo (res);
1118     resi = i;
1119   }
1120   else
1121   {
1122     /* will bind against everything, just set port */
1123     if (disablev6)
1124     {
1125       /* V4-only */
1126       resi = 1;
1127       i = 0;
1128       saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1129       saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1130
1131       saddrlens[i] = sizeof (struct sockaddr_in);
1132       saddrs[i] = GNUNET_malloc (saddrlens[i]);
1133 #if HAVE_SOCKADDR_IN_SIN_LEN
1134       ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1135 #endif
1136       ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1137       ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1138     }
1139     else
1140     {
1141       /* dual stack */
1142       resi = 2;
1143       saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1144       saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1145       i = 0;
1146       saddrlens[i] = sizeof (struct sockaddr_in6);
1147       saddrs[i] = GNUNET_malloc (saddrlens[i]);
1148 #if HAVE_SOCKADDR_IN_SIN_LEN
1149       ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1150 #endif
1151       ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1152       ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1153       i++;
1154       saddrlens[i] = sizeof (struct sockaddr_in);
1155       saddrs[i] = GNUNET_malloc (saddrlens[i]);
1156 #if HAVE_SOCKADDR_IN_SIN_LEN
1157       ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1158 #endif
1159       ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1160       ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1161     }
1162   }
1163   *addrs = saddrs;
1164   *addr_lens = saddrlens;
1165   return resi;
1166 }
1167
1168 static void
1169 start_report_addresses (struct Plugin *plugin)
1170 {
1171   int res = GNUNET_OK;
1172   struct sockaddr **addrs;
1173   socklen_t *addrlens;
1174
1175   res =
1176       http_get_addresses (plugin, plugin->name, plugin->env->cfg, &addrs,
1177                           &addrlens);
1178   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1179                    _("Found %u addresses to report to NAT service\n"), res);
1180
1181   if (res != GNUNET_SYSERR)
1182   {
1183     plugin->nat =
1184         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
1185                              (unsigned int) res,
1186                              (const struct sockaddr **) addrs, addrlens,
1187                              &nat_port_map_callback, NULL, plugin);
1188     while (res > 0)
1189     {
1190       res--;
1191 #if 0
1192       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, _("FREEING %s\n"),
1193                        GNUNET_a2s (addrs[res], addrlens[res]));
1194 #endif
1195       GNUNET_assert (addrs[res] != NULL);
1196       GNUNET_free (addrs[res]);
1197     }
1198     GNUNET_free_non_null (addrs);
1199     GNUNET_free_non_null (addrlens);
1200   }
1201   else
1202   {
1203     plugin->nat =
1204         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
1205                              NULL, NULL, plugin);
1206   }
1207 }
1208
1209 static void
1210 stop_report_addresses (struct Plugin *plugin)
1211 {
1212   /* Stop NAT handle */
1213   GNUNET_NAT_unregister (plugin->nat);
1214
1215   /* Clean up addresses */
1216   struct IPv4HttpAddressWrapper *w_t4;
1217   struct IPv6HttpAddressWrapper *w_t6;
1218
1219   while (plugin->ipv4_addr_head != NULL)
1220   {
1221     w_t4 = plugin->ipv4_addr_head;
1222     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
1223                                  w_t4);
1224     GNUNET_free (w_t4);
1225   }
1226
1227   while (plugin->ipv6_addr_head != NULL)
1228   {
1229     w_t6 = plugin->ipv6_addr_head;
1230     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1231                                  w_t6);
1232     GNUNET_free (w_t6);
1233   }
1234 }
1235
1236 static int
1237 configure_plugin (struct Plugin *plugin)
1238 {
1239   int res = GNUNET_OK;
1240
1241   /* Use IPv4? */
1242   if (GNUNET_CONFIGURATION_have_value
1243       (plugin->env->cfg, plugin->name, "USE_IPv4"))
1244   {
1245     plugin->ipv4 =
1246         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1247                                               "USE_IPv4");
1248   }
1249   else
1250     plugin->ipv4 = GNUNET_YES;
1251
1252   /* Use IPv6? */
1253   if (GNUNET_CONFIGURATION_have_value
1254       (plugin->env->cfg, plugin->name, "USE_IPv6"))
1255   {
1256     plugin->ipv6 =
1257         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1258                                               "USE_IPv6");
1259   }
1260   else
1261     plugin->ipv6 = GNUNET_YES;
1262
1263   if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
1264   {
1265     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1266                      _
1267                      ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
1268                      plugin->name);
1269     res = GNUNET_SYSERR;
1270   }
1271
1272   /* Reading port number from config file */
1273   unsigned long long port;
1274
1275   if ((GNUNET_OK !=
1276        GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1277                                               "PORT", &port)) || (port > 65535))
1278   {
1279     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1280                      _("Port is required! Fix in configuration\n"),
1281                      plugin->name);
1282     res = GNUNET_SYSERR;
1283     goto fail;
1284   }
1285   plugin->port = port;
1286
1287   plugin->client_only = GNUNET_NO;
1288   if (plugin->port == 0)
1289   {
1290     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1291                      _("Port 0, client only mode\n"));
1292     plugin->client_only = GNUNET_YES;
1293   }
1294
1295   char *bind4_address = NULL;
1296
1297   if ((plugin->ipv4 == GNUNET_YES) &&
1298       (GNUNET_YES ==
1299        GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1300                                               "BINDTO", &bind4_address)))
1301   {
1302     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1303                      "Binding %s plugin to specific IPv4 address: `%s'\n",
1304                      plugin->protocol, bind4_address);
1305     plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1306     if (1 !=
1307         inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
1308     {
1309       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1310                        _
1311                        ("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1312                        bind4_address, plugin->protocol);
1313       GNUNET_free (plugin->server_addr_v4);
1314       plugin->server_addr_v4 = NULL;
1315     }
1316     else
1317     {
1318       plugin->server_addr_v4->sin_family = AF_INET;
1319       plugin->server_addr_v4->sin_port = htons (plugin->port);
1320     }
1321     GNUNET_free (bind4_address);
1322   }
1323
1324
1325   char *bind6_address = NULL;
1326
1327   if ((plugin->ipv6 == GNUNET_YES) &&
1328       (GNUNET_YES ==
1329        GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1330                                               "BINDTO6", &bind6_address)))
1331   {
1332     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1333                      "Binding %s plugin to specific IPv6 address: `%s'\n",
1334                      plugin->protocol, bind6_address);
1335     plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1336     if (1 !=
1337         inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
1338     {
1339       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1340                        _
1341                        ("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1342                        bind6_address, plugin->protocol);
1343       GNUNET_free (plugin->server_addr_v6);
1344       plugin->server_addr_v6 = NULL;
1345     }
1346     else
1347     {
1348       plugin->server_addr_v6->sin6_family = AF_INET6;
1349       plugin->server_addr_v6->sin6_port = htons (plugin->port);
1350     }
1351     GNUNET_free (bind6_address);
1352   }
1353
1354
1355   /* Optional parameters */
1356   unsigned long long maxneigh;
1357
1358   if (GNUNET_OK !=
1359       GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1360                                              "MAX_CONNECTIONS", &maxneigh))
1361     maxneigh = 128;
1362   plugin->max_connections = maxneigh;
1363
1364 fail:
1365   return res;
1366 }
1367
1368 /**
1369  * Entry point for the plugin.
1370  */
1371 void *
1372 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1373 {
1374   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1375   struct GNUNET_TRANSPORT_PluginFunctions *api;
1376   struct Plugin *plugin;
1377   int res;
1378
1379   plugin = GNUNET_malloc (sizeof (struct Plugin));
1380   plugin->env = env;
1381   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1382   api->cls = plugin;
1383   api->send = &http_plugin_send;
1384   api->disconnect = &http_plugin_disconnect;
1385   api->address_pretty_printer = &http_plugin_address_pretty_printer;
1386   api->check_address = &http_plugin_address_suggested;
1387   api->address_to_string = &http_plugin_address_to_string;
1388
1389 #if BUILD_HTTPS
1390   plugin->name = "transport-https";
1391   plugin->protocol = "https";
1392 #else
1393   plugin->name = "transport-http";
1394   plugin->protocol = "http";
1395 #endif
1396   /* Configure plugin from configuration */
1397   res = configure_plugin (plugin);
1398   if (res == GNUNET_SYSERR)
1399   {
1400     GNUNET_free_non_null (plugin->server_addr_v4);
1401     GNUNET_free_non_null (plugin->server_addr_v6);
1402     GNUNET_free (plugin);
1403     GNUNET_free (api);
1404     return NULL;
1405   }
1406
1407   /* checking IPv6 support */
1408   http_check_ipv6 (plugin);
1409
1410   /* Start client */
1411   res = client_start (plugin);
1412   if (res == GNUNET_SYSERR)
1413   {
1414     GNUNET_free_non_null (plugin->server_addr_v4);
1415     GNUNET_free_non_null (plugin->server_addr_v6);
1416     GNUNET_free (plugin);
1417     GNUNET_free (api);
1418     return NULL;
1419   }
1420
1421   /* Start server */
1422   if (plugin->client_only == GNUNET_NO)
1423   {
1424     res = server_start (plugin);
1425     if (res == GNUNET_SYSERR)
1426     {
1427       server_stop (plugin);
1428       client_stop (plugin);
1429
1430       GNUNET_free_non_null (plugin->server_addr_v4);
1431       GNUNET_free_non_null (plugin->server_addr_v6);
1432       GNUNET_free (plugin);
1433       GNUNET_free (api);
1434       return NULL;
1435     }
1436   }
1437   /* Report addresses to transport service */
1438   start_report_addresses (plugin);
1439
1440 #if DEBUG_HTTP
1441   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1442                    "Plugin `%s' loaded\n", plugin->name);
1443 #endif
1444
1445   return api;
1446 }
1447
1448
1449 /**
1450  * Exit point from the plugin.
1451  */
1452 void *
1453 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1454 {
1455   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1456   struct Plugin *plugin = api->cls;
1457   struct Session *s = NULL;
1458
1459   /* Stop reporting addresses to transport service */
1460   stop_report_addresses (plugin);
1461
1462   /* cleaning up sessions */
1463   s = plugin->head;
1464   while (s != NULL)
1465   {
1466 #if DEBUG_HTTP
1467     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1468                      "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1469 #endif
1470     if (s->inbound == GNUNET_NO)
1471       GNUNET_assert (GNUNET_OK == client_disconnect (s));
1472     else
1473       GNUNET_assert (GNUNET_OK == server_disconnect (s));
1474     s = s->next;
1475   }
1476
1477 #if DEBUG_HTTP
1478   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping server\n");
1479 #endif
1480   /* Stop server */
1481   server_stop (plugin);
1482
1483 #if DEBUG_HTTP
1484   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping client\n");
1485 #endif
1486   /* Stop client */
1487   client_stop (plugin);
1488
1489   /* deleting up sessions */
1490   s = plugin->head;
1491   while (s != NULL)
1492   {
1493     struct Session *t = s->next;
1494
1495     GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
1496
1497     struct HTTP_Message *msg = s->msg_head;
1498     struct HTTP_Message *tmp = NULL;
1499
1500     while (msg != NULL)
1501     {
1502       tmp = msg->next;
1503
1504       GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1505       if (msg->transmit_cont != NULL)
1506       {
1507         msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
1508       }
1509       GNUNET_free (msg);
1510       msg = tmp;
1511     }
1512
1513     delete_session (s);
1514     s = t;
1515   }
1516
1517
1518 #if DEBUG_HTTP
1519   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1520                    "Plugin `%s' unloaded\n", plugin->name);
1521 #endif
1522
1523   GNUNET_free_non_null (plugin->server_addr_v4);
1524   GNUNET_free_non_null (plugin->server_addr_v6);
1525   GNUNET_free (plugin);
1526   GNUNET_free (api);
1527
1528   return NULL;
1529 }
1530
1531 /* end of plugin_transport_http.c */