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