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