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