renaming plugin
[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 sockaddr_in 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   /**
70    * IPv6 address.
71    */
72   struct sockaddr_in6 addr GNUNET_PACKED;
73 };
74
75
76 /**
77  * Context for address to string conversion.
78  */
79 struct PrettyPrinterContext
80 {
81   /**
82    * Function to call with the result.
83    */
84   GNUNET_TRANSPORT_AddressStringCallback asc;
85
86   /**
87    * Plugin
88    */
89   struct Plugin *plugin;
90
91   /**
92    * Clsoure for 'asc'.
93    */
94   void *asc_cls;
95
96   /**
97    * Port to add after the IP address.
98    */
99   uint16_t port;
100 };
101
102
103 /**
104  * Encapsulation of all of the state of the plugin.
105  */
106 struct Plugin;
107
108
109
110 /**
111  * Append our port and forward the result.
112  *
113  * @param cls the 'struct PrettyPrinterContext*'
114  * @param hostname hostname part of the address
115  */
116 static void
117 append_port (void *cls, const char *hostname)
118 {
119   struct PrettyPrinterContext *ppc = cls;
120   char *ret;
121
122   if (hostname == NULL)
123   {
124     ppc->asc (ppc->asc_cls, NULL);
125     GNUNET_free (ppc);
126     return;
127   }
128   GNUNET_asprintf (&ret, "%s://%s:%d", ppc->plugin->protocol, hostname,
129                    ppc->plugin->port);
130   ppc->asc (ppc->asc_cls, ret);
131   GNUNET_free (ret);
132 }
133
134
135 /**
136  * Convert the transports address to a nice, human-readable
137  * format.
138  *
139  * @param cls closure
140  * @param type name of the transport that generated the address
141  * @param addr one of the addresses of the host, NULL for the last address
142  *        the specific address format depends on the transport
143  * @param addrlen length of the address
144  * @param numeric should (IP) addresses be displayed in numeric form?
145  * @param timeout after how long should we give up?
146  * @param asc function to call on each string
147  * @param asc_cls closure for asc
148  */
149 static void
150 http_plugin_address_pretty_printer (void *cls, const char *type,
151                                     const void *addr, size_t addrlen,
152                                     int numeric,
153                                     struct GNUNET_TIME_Relative timeout,
154                                     GNUNET_TRANSPORT_AddressStringCallback asc,
155                                     void *asc_cls)
156 {
157   GNUNET_assert (cls != NULL);
158   struct PrettyPrinterContext *ppc;
159   const void *sb;
160   size_t sbs;
161   uint16_t port = 0 ;
162
163   if (addrlen == sizeof (struct sockaddr_in6))
164   {
165     sb = addr;
166     sbs = sizeof (struct sockaddr_in6);
167
168     port = ntohs (((struct sockaddr_in6 *)addr)->sin6_port);
169   }
170   else if (addrlen == sizeof (struct sockaddr_in))
171   {
172     sb = &addr;
173     sbs = sizeof (struct sockaddr_in);
174     port = ntohs (((struct sockaddr_in *)addr)->sin_port);
175   }
176   else
177   {
178     /* invalid address */
179     GNUNET_break_op (0);
180     asc (asc_cls, NULL);
181     return;
182   }
183   ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
184   ppc->asc = asc;
185   ppc->asc_cls = asc_cls;
186   ppc->port = port;
187   ppc->plugin = cls;
188   GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
189 }
190
191
192
193 /**
194  * Another peer has suggested an address for this
195  * peer and transport plugin.  Check that this could be a valid
196  * address.  If so, consider adding it to the list
197  * of addresses.
198  *
199  * @param cls closure
200  * @param addr pointer to the address
201  * @param addrlen length of addr
202  * @return GNUNET_OK if this is a plausible address for this peer
203  *         and transport
204  */
205 static int
206 http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
207 {
208
209   struct Plugin *plugin = cls;
210   struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
211   struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
212
213   GNUNET_assert (cls != NULL);
214   if ((addrlen != sizeof (struct sockaddr_in)) ||
215       (addrlen != sizeof (struct sockaddr_in6)))
216     return GNUNET_SYSERR;
217
218   if (addrlen == sizeof (struct sockaddr_in))
219   {
220     while (w_tv4 != NULL)
221     {
222       if (0 == memcmp (&w_tv4->addr, addr, sizeof (struct sockaddr_in)))
223         break;
224       w_tv4 = w_tv4->next;
225     }
226     if (w_tv4 != NULL)
227       return GNUNET_OK;
228     else
229       return GNUNET_SYSERR;
230   }
231   if (addrlen == sizeof (struct sockaddr_in6))
232   {
233     while (w_tv6 != NULL)
234     {
235       if (0 ==
236           memcmp (&w_tv6->addr, addr,
237                   sizeof (struct sockaddr_in6)))
238         break;
239       w_tv6 = w_tv6->next;
240     }
241     if (w_tv6 != NULL)
242       return GNUNET_OK;
243     else
244       return GNUNET_SYSERR;
245   }
246   return GNUNET_OK;
247 }
248
249 struct GNUNET_TIME_Relative
250 http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity * peer,
251     const struct  GNUNET_MessageHeader * message,
252     struct Session * session,
253     const char *sender_address,
254     uint16_t sender_address_len)
255 {
256   struct Session *s = cls;
257   struct Plugin *plugin = s->plugin;
258   struct GNUNET_TRANSPORT_ATS_Information distance[2];
259   struct GNUNET_TIME_Relative delay;
260
261   distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
262   distance[0].value = htonl (1);
263   distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
264   distance[1].value = htonl (0);
265
266   delay = plugin->env->receive (plugin->env->cls, &s->target, message, (const struct GNUNET_TRANSPORT_ATS_Information*) &distance, 2, s, s->addr, s->addrlen);
267   return delay;
268 }
269
270 /**
271  * Function called for a quick conversion of the binary address to
272  * a numeric address.  Note that the caller must not free the
273  * address and that the next call to this function is allowed
274  * to override the address again.
275  *
276  * @param cls closure
277  * @param addr binary address
278  * @param addrlen length of the address
279  * @return string representing the same address
280  */
281 const char *
282 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
283 {
284
285   struct sockaddr_in *a4;
286   struct sockaddr_in6 *a6;
287   char *address;
288   static char rbuf[INET6_ADDRSTRLEN + 13];
289   uint16_t port;
290   int res = 0;
291
292   if (addrlen == sizeof (struct sockaddr_in6))
293   {
294     a6 = (struct sockaddr_in6 *) addr;
295     address = GNUNET_malloc (INET6_ADDRSTRLEN);
296     inet_ntop (AF_INET6, &(a6->sin6_addr), address, INET6_ADDRSTRLEN);
297     port = ntohs (a6->sin6_port);
298   }
299   else if (addrlen == sizeof (struct sockaddr_in))
300   {
301     a4 = (struct sockaddr_in *) addr;
302     address = GNUNET_malloc (INET_ADDRSTRLEN);
303     inet_ntop (AF_INET, &(a4->sin_addr), address, INET_ADDRSTRLEN);
304     port = ntohs (a4->sin_port);
305   }
306   else
307   {
308     /* invalid address */
309     return NULL;
310   }
311 #if !BUILD_HTTPS  
312   char * protocol = "http";
313 #else
314   char * protocol = "https";
315 #endif
316
317   GNUNET_assert (strlen (address) + 7 < (INET6_ADDRSTRLEN + 13));
318   if (addrlen == sizeof (struct sockaddr_in6))
319     res = GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol, address, port);
320   else if (addrlen == sizeof (struct sockaddr_in))
321     res = GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, address, port);
322
323   GNUNET_free (address);
324   GNUNET_assert (res != 0);
325   return rbuf;
326 }
327
328 struct Session *
329 lookup_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
330                 struct Session * session,
331                 const void *addr, size_t addrlen, int force_address)
332 {
333   struct Session *s = NULL;
334   struct Session *t = NULL;
335   int e_peer;
336   int e_addr;
337
338   t = plugin->head;
339   if (t == NULL)
340     return NULL;
341   while (t != NULL)
342   {
343 #if 0
344     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
345                      "Comparing peer `%s' address `%s' len %i session %X to \n", GNUNET_i2s(target), GNUNET_a2s(addr,addrlen), addrlen, session);
346     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,"peer `%s' address `%s' len %i session %X \n\n", GNUNET_i2s(&t->target), GNUNET_a2s(t->addr,t->addrlen), t->addrlen, t);
347     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,"memcmp %i \n", memcmp (addr, t->addr, addrlen));
348 #endif
349     e_peer = GNUNET_NO;
350     e_addr = GNUNET_NO;
351
352     if (0 == memcmp (target, &t->target, sizeof (struct GNUNET_PeerIdentity)))
353     {
354       e_peer = GNUNET_YES;
355       if (addrlen == t->addrlen)
356       {
357         if (0 == memcmp (addr, t->addr, addrlen))
358         {
359           e_addr = GNUNET_YES;
360         }
361       }
362       if ((t == session))
363       {
364        if(t->addrlen == session->addrlen)
365        {
366         if (0 == memcmp (session->addr, t->addr, t->addrlen))
367         {
368           e_addr = GNUNET_YES;
369         }
370        }
371       }
372     }
373
374     if ((e_peer == GNUNET_YES) && (force_address == GNUNET_NO))
375     {
376       s = t;
377       break;
378     }
379     if ((e_peer == GNUNET_YES) && (force_address == GNUNET_YES) && (e_addr == GNUNET_YES))
380     {
381       s = t;
382       break;
383     }
384     if ((e_peer == GNUNET_YES) && (force_address == GNUNET_SYSERR))
385     {
386       s = t;
387       break;
388     }
389     if (s != NULL)
390       break;
391     t = t->next;
392   }
393
394
395   return s;
396 }
397
398 void
399 delete_session (struct Session *s)
400 {
401   if (s->msg_tk != NULL)
402   {
403     GNUNET_SERVER_mst_destroy (s->msg_tk);
404     s->msg_tk = NULL;
405   }
406   GNUNET_free (s->addr);
407   GNUNET_free_non_null(s->server_recv);
408   GNUNET_free_non_null(s->server_send);
409   GNUNET_free (s);
410 }
411
412 struct Session *
413 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
414                 const void *addr, size_t addrlen,
415                 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
416 {
417   struct Session *s = NULL;
418
419   s = GNUNET_malloc (sizeof (struct Session));
420   memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
421   s->plugin = plugin;
422   s->addr = GNUNET_malloc (addrlen);
423   memcpy (s->addr, addr, addrlen);
424   s->addrlen = addrlen;
425   s->transmit_cont = cont;
426   s->transmit_cont_cls = cont_cls;
427   s->next = NULL;
428   s->next_receive = GNUNET_TIME_absolute_get_forever();
429   return s;
430 }
431
432 void
433 notify_session_end (void *cls,
434                     const struct GNUNET_PeerIdentity *
435                     peer, struct Session * s)
436 {
437   struct Plugin *plugin = cls;
438
439   plugin->env->session_end (NULL, peer, s);
440   GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
441   delete_session (s);
442 }
443
444
445 /**
446  * Function that can be used by the transport service to transmit
447  * a message using the plugin.   Note that in the case of a
448  * peer disconnecting, the continuation MUST be called
449  * prior to the disconnect notification itself.  This function
450  * will be called with this peer's HELLO message to initiate
451  * a fresh connection to another peer.
452  *
453  * @param cls closure
454  * @param target who should receive this message
455  * @param msgbuf the message to transmit
456  * @param msgbuf_size number of bytes in 'msgbuf'
457  * @param priority how important is the message (most plugins will
458  *                 ignore message priority and just FIFO)
459  * @param to how long to wait at most for the transmission (does not
460  *                require plugins to discard the message after the timeout,
461  *                just advisory for the desired delay; most plugins will ignore
462  *                this as well)
463  * @param session which session must be used (or NULL for "any")
464  * @param addr the address to use (can be NULL if the plugin
465  *                is "on its own" (i.e. re-use existing TCP connection))
466  * @param addrlen length of the address in bytes
467  * @param force_address GNUNET_YES if the plugin MUST use the given address,
468  *                GNUNET_NO means the plugin may use any other address and
469  *                GNUNET_SYSERR means that only reliable existing
470  *                bi-directional connections should be used (regardless
471  *                of address)
472  * @param cont continuation to call once the message has
473  *        been transmitted (or if the transport is ready
474  *        for the next transmission call; or if the
475  *        peer disconnected...); can be NULL
476  * @param cont_cls closure for cont
477  * @return number of bytes used (on the physical network, with overheads);
478  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
479  *         and does NOT mean that the message was not transmitted (DV)
480  */
481 static ssize_t
482 http_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
483                   const char *msgbuf, size_t msgbuf_size, unsigned int priority,
484                   struct GNUNET_TIME_Relative to, struct Session *session,
485                   const void *addr, size_t addrlen, int force_address,
486                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
487 {
488   struct Plugin *plugin = cls;
489   struct HTTP_Message *msg;
490   GNUNET_assert (plugin != NULL);
491
492   int res = GNUNET_SYSERR;
493
494 #if DEBUG_HTTP
495   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
496                    "Sending %u bytes to peer `%s' on address `%s' %X %i\n", msgbuf_size,
497                    GNUNET_i2s (target), ((addr != NULL) && (addrlen != 0)) ? http_plugin_address_to_string(plugin, addr, addrlen) : "<inbound>", session, force_address);
498 #endif
499
500   struct Session *s = NULL;
501
502   /* look for existing connection */
503   s = lookup_session (plugin, target, session, addr, addrlen, 1);
504 #if DEBUG_HTTP
505   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
506                    "%s existing session: %s\n", (s!=NULL) ? "Found" : "NOT Found", ((s != NULL) && (s->inbound == GNUNET_YES)) ? "inbound" : "outbound");
507 #endif
508
509   /* create new outbound connection */
510   if (s == NULL)
511   {
512     if (plugin->max_connections <= plugin->cur_connections)
513     {
514       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
515                        "Maximum number of connections reached, "
516                        "cannot connect to peer `%s'\n",
517                        GNUNET_i2s (target));
518       return res;
519     }
520
521 #if DEBUG_HTTP
522     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
523                      "Initiiating new connection to peer `%s'\n",
524                      GNUNET_i2s (target));
525 #endif
526     s = create_session (plugin, target, addr, addrlen, cont, cont_cls);
527     GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
528     // initiate new connection
529     if (GNUNET_SYSERR == (res = client_connect (s)))
530     {
531       GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
532       delete_session (s);
533       return GNUNET_SYSERR;
534     }
535   }
536
537   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
538   msg->next = NULL;
539   msg->size = msgbuf_size;
540   msg->pos = 0;
541   msg->buf = (char *) &msg[1];
542   msg->transmit_cont = cont;
543   msg->transmit_cont_cls = cont_cls;
544   memcpy (msg->buf, msgbuf, msgbuf_size);
545
546   if (s->inbound == GNUNET_NO)
547   {
548 #if DEBUG_HTTP
549     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
550                      "Using outbound client session to send to `%s'\n",
551                      GNUNET_i2s (target));
552 #endif
553      client_send (s, msg);
554      res = msgbuf_size;
555   }
556   if (s->inbound == GNUNET_YES)
557   {
558     server_send (s, msg);
559     res = msgbuf_size;
560 #if DEBUG_HTTP
561     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
562                      "Using inbound server session to send to `%s'\n",
563                      GNUNET_i2s (target));
564 #endif
565
566   }
567   return res;
568 }
569
570
571 /**
572  * Function that can be used to force the plugin to disconnect
573  * from the given peer and cancel all previous transmissions
574  * (and their continuationc).
575  *
576  * @param cls closure
577  * @param target peer from which to disconnect
578  */
579 static void
580 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
581 {
582   struct Plugin *plugin = cls;
583   struct Session *next = NULL;
584   struct Session *s = plugin->head;
585
586   while (s != NULL)
587   {
588     next = s->next;
589     if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
590     {
591       if (s->inbound == GNUNET_NO)
592         GNUNET_assert (GNUNET_OK == client_disconnect (s));
593       else
594         GNUNET_assert (GNUNET_OK == server_disconnect (s));
595       GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
596       delete_session (s);
597     }
598     s = next;
599   }
600 }
601
602 static void
603 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
604                  socklen_t addrlen)
605 {
606   struct Plugin *plugin = cls;
607   struct IPv4HttpAddressWrapper *w_t4 = NULL;
608   struct IPv6HttpAddressWrapper *w_t6 = NULL;
609   int af;
610
611   af = addr->sa_family;
612   switch (af)
613   {
614   case AF_INET:
615     w_t4 = plugin->ipv4_addr_head;
616     while (w_t4 != NULL)
617     {
618       int res = memcmp (&w_t4->addr,
619                         (struct sockaddr_in *) addr,
620                         sizeof (struct sockaddr_in));
621
622       if (0 == res)
623         break;
624       w_t4 = w_t4->next;
625     }
626     if (w_t4 == NULL)
627     {
628       w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
629       memcpy (&w_t4->addr, (struct sockaddr_in *) addr,
630               sizeof (struct sockaddr_in));
631
632       GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
633                                    plugin->ipv4_addr_tail, w_t4);
634     }
635 #if DEBUG_HTTP
636     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
637                      "Notifying transport to add IPv4 address `%s'\n",
638                      http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct sockaddr_in)));
639 #endif
640     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr, sizeof (struct sockaddr_in));
641
642     break;
643   case AF_INET6:
644     w_t6 = plugin->ipv6_addr_head;
645     while (w_t6)
646     {
647       int res = memcmp (&w_t6->addr,
648                         (struct sockaddr_in6 *) addr,
649                         sizeof (struct sockaddr_in6));
650
651       if (0 == res)
652         break;
653       w_t6 = w_t6->next;
654     }
655     if (w_t6 == NULL)
656     {
657       w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
658       memcpy (&w_t6->addr, (struct sockaddr_in6 *) addr,
659               sizeof (struct sockaddr_in6));
660
661       GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
662                                    plugin->ipv6_addr_tail, w_t6);
663     }
664 #if DEBUG_HTTP
665     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
666                      "Notifying transport to add IPv6 address `%s'\n",
667                      http_plugin_address_to_string(NULL, &w_t6->addr, sizeof (struct sockaddr_in6)));
668 #endif
669     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr, sizeof (struct sockaddr_in6));
670     break;
671   default:
672     return;
673   }
674
675 }
676
677 static void
678 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
679                     socklen_t addrlen)
680 {
681   struct Plugin *plugin = cls;
682   struct IPv4HttpAddressWrapper *w_t4 = NULL;
683   struct IPv6HttpAddressWrapper *w_t6 = NULL;
684   int af;
685
686   af = addr->sa_family;
687   switch (af)
688   {
689   case AF_INET:
690     w_t4 = plugin->ipv4_addr_head;
691     while (w_t4 != NULL)
692     {
693       int res = memcmp (&(w_t4->addr.sin_addr),
694                         &((struct sockaddr_in *) addr)->sin_addr,
695                         sizeof (struct in_addr));
696
697       if (0 == res)
698         break;
699       w_t4 = w_t4->next;
700     }
701     if (w_t4 == NULL)
702       return;
703
704 #if DEBUG_HTTP
705     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
706                      "Notifying transport to remove IPv4 address `%s'\n",
707                      http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct sockaddr_in)));
708 #endif
709     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
710                                  sizeof (struct sockaddr_in));
711
712     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
713                                  w_t4);
714     GNUNET_free (w_t4);
715     break;
716   case AF_INET6:
717     w_t6 = plugin->ipv6_addr_head;
718     while (w_t6 != NULL)
719     {
720       int res = memcmp (&(w_t6->addr.sin6_addr),
721                         &((struct sockaddr_in6 *) addr)->sin6_addr,
722                         sizeof (struct in6_addr));
723
724       if (0 == res)
725         break;
726       w_t6 = w_t6->next;
727     }
728     if (w_t6 == NULL)
729       return;
730 #if DEBUG_HTTP
731     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
732                      "Notifying transport to remove IPv6 address `%s'\n",
733                      http_plugin_address_to_string(NULL, &w_t6->addr, sizeof (struct sockaddr_in6)));
734 #endif
735     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr,
736                                  sizeof (struct sockaddr_in6 ));
737
738     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
739                                  w_t6);
740     GNUNET_free (w_t6);
741     break;
742   default:
743     return;
744   }
745
746 }
747
748 /**
749  * Our external IP address/port mapping has changed.
750  *
751  * @param cls closure, the 'struct LocalAddrList'
752  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
753  *     the previous (now invalid) one
754  * @param addr either the previous or the new public IP address
755  * @param addrlen actual lenght of the address
756  */
757 static void
758 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
759                        socklen_t addrlen)
760 {
761   GNUNET_assert (cls != NULL);
762 #if DEBUG_HTTP
763   struct Plugin *plugin = cls;
764 #endif
765   static int limit;
766 #if DEBUG_HTTP
767   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
768                    "NPMC called %s to address `%s'\n",
769                    (add_remove == GNUNET_NO) ? "remove" : "add",
770                    GNUNET_a2s (addr, addrlen));
771 #endif
772   /* convert 'addr' to our internal format */
773   switch (add_remove)
774   {
775   case GNUNET_YES:
776     // FIXME DEBUGGING
777     if (limit < 1)
778       nat_add_address (cls, add_remove, addr, addrlen);
779     limit++;
780     // FIXME END
781     break;
782   case GNUNET_NO:
783     nat_remove_address (cls, add_remove, addr, addrlen);
784     break;
785   }
786 }
787
788
789 static void
790 start_report_addresses (struct Plugin *plugin)
791 {
792   int res = GNUNET_OK;
793   struct sockaddr **addrs;
794   socklen_t *addrlens;
795
796   res =
797       GNUNET_SERVICE_get_server_addresses (plugin->name, plugin->env->cfg,
798                                            &addrs, &addrlens);
799 #if 0
800   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
801                    _("FOUND %u addresses\n"),res);
802 #endif
803
804   if (res != GNUNET_SYSERR)
805   {
806     plugin->nat =
807         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
808                              (unsigned int) res,
809                              (const struct sockaddr **) addrs, addrlens,
810                              &nat_port_map_callback, NULL,
811                              plugin);
812     while (res > 0)
813     {
814       res--;
815 #if 0
816       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
817                        _("FREEING %s\n"),
818                        GNUNET_a2s (addrs[res], addrlens[res]));
819 #endif
820       GNUNET_assert (addrs[res] != NULL);
821       GNUNET_free (addrs[res]);
822     }
823     GNUNET_free_non_null (addrs);
824     GNUNET_free_non_null (addrlens);
825   }
826   else
827   {
828     plugin->nat =
829         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
830                              NULL, NULL, plugin);
831   }
832 }
833
834 static void
835 stop_report_addresses (struct Plugin *plugin)
836 {
837   /* Stop NAT handle */
838   GNUNET_NAT_unregister (plugin->nat);
839
840   /* Clean up addresses */
841   struct IPv4HttpAddressWrapper *w_t4;
842   struct IPv6HttpAddressWrapper *w_t6;
843
844   while (plugin->ipv4_addr_head != NULL)
845   {
846     w_t4 = plugin->ipv4_addr_head;
847     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
848                                  w_t4);
849     GNUNET_free (w_t4);
850   }
851
852   while (plugin->ipv6_addr_head != NULL)
853   {
854     w_t6 = plugin->ipv6_addr_head;
855     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
856                                  w_t6);
857     GNUNET_free (w_t6);
858   }
859 }
860
861 static int
862 configure_plugin (struct Plugin *plugin)
863 {
864   int res = GNUNET_OK;
865
866   /* Use IPv4? */
867   if (GNUNET_CONFIGURATION_have_value
868       (plugin->env->cfg, plugin->name, "USE_IPv4"))
869   {
870     plugin->ipv4 =
871         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
872                                               "USE_IPv4");
873   }
874   else
875     plugin->ipv4 = GNUNET_YES;
876
877   /* Use IPv6? */
878   if (GNUNET_CONFIGURATION_have_value
879       (plugin->env->cfg, plugin->name, "USE_IPv6"))
880   {
881     plugin->ipv6 =
882         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
883                                               "USE_IPv6");
884   }
885   else
886     plugin->ipv6 = GNUNET_YES;
887
888   if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
889   {
890     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
891                      _
892                      ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
893                      plugin->name);
894     res = GNUNET_SYSERR;
895   }
896
897   /* Reading port number from config file */
898   unsigned long long port;
899
900   if ((GNUNET_OK !=
901        GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
902                                               "PORT", &port)) || (port > 65535))
903   {
904     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
905                      _("Port is required! Fix in configuration\n"),
906                      plugin->name);
907     res = GNUNET_SYSERR;
908     goto fail;
909   }
910   plugin->port = port;
911
912
913   char * bind4_address = NULL;
914   if ((plugin->ipv4 == GNUNET_YES) && (GNUNET_YES ==
915       GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
916                                              "BINDTO", &bind4_address)))
917   {
918     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
919                 "Binding %s plugin to specific IPv4 address: `%s'\n",
920                 plugin->protocol, bind4_address);
921     plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
922     if (1 != inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
923     {
924       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
925                   _("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
926                   bind4_address, plugin->protocol);
927       GNUNET_free (plugin->server_addr_v4);
928       plugin->server_addr_v4 = NULL;
929     }
930     else
931     {
932       plugin->server_addr_v4->sin_family = AF_INET;
933       plugin->server_addr_v4->sin_port = htons (plugin->port);
934     }
935     GNUNET_free (bind4_address);
936   }
937
938
939   char * bind6_address = NULL;
940   if ((plugin->ipv6 == GNUNET_YES) && (GNUNET_YES ==
941       GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
942                                              "BINDTO6", &bind6_address)))
943   {
944     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
945                 "Binding %s plugin to specific IPv6 address: `%s'\n",
946                 plugin->protocol, bind6_address);
947     plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
948     if (1 != inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
949     {
950       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
951                   _("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
952                   bind6_address, plugin->protocol);
953       GNUNET_free (plugin->server_addr_v6);
954       plugin->server_addr_v6 = NULL;
955     }
956     else
957     {
958       plugin->server_addr_v6->sin6_family = AF_INET6;
959       plugin->server_addr_v6->sin6_port = htons (plugin->port);
960     }
961     GNUNET_free (bind6_address);
962   }
963
964
965   /* Optional parameters */
966   unsigned long long maxneigh;
967   if (GNUNET_OK !=
968       GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
969                                              "MAX_CONNECTIONS", &maxneigh))
970     maxneigh = 128;
971   plugin->max_connections = maxneigh;
972
973 fail:
974   return res;
975 }
976
977 /**
978  * Entry point for the plugin.
979  */
980 void *
981 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
982 {
983   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
984   struct GNUNET_TRANSPORT_PluginFunctions *api;
985   struct Plugin *plugin;
986   int res;
987
988   plugin = GNUNET_malloc (sizeof (struct Plugin));
989   plugin->env = env;
990   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
991   api->cls = plugin;
992   api->send = &http_plugin_send;
993   api->disconnect = &http_plugin_disconnect;
994   api->address_pretty_printer = &http_plugin_address_pretty_printer;
995   api->check_address = &http_plugin_address_suggested;
996   api->address_to_string = &http_plugin_address_to_string;
997
998 #if BUILD_HTTPS
999   plugin->name = "transport-https";
1000   plugin->protocol = "https";
1001 #else
1002   plugin->name = "transport-http";
1003   plugin->protocol = "http";
1004 #endif
1005   /* Configure plugin from configuration */
1006
1007   res = configure_plugin (plugin);
1008   if (res == GNUNET_SYSERR)
1009   {
1010     GNUNET_free_non_null (plugin->server_addr_v4);
1011     GNUNET_free_non_null (plugin->server_addr_v6);
1012     GNUNET_free (plugin);
1013     GNUNET_free (api);
1014     return NULL;
1015   }
1016
1017   /* Start client */
1018   res = client_start (plugin);
1019   if (res == GNUNET_SYSERR)
1020   {
1021     GNUNET_free_non_null (plugin->server_addr_v4);
1022     GNUNET_free_non_null (plugin->server_addr_v6);
1023     GNUNET_free (plugin);
1024     GNUNET_free (api);
1025     return NULL;
1026   }
1027
1028   /* Start server */
1029   res = server_start (plugin);
1030   if (res == GNUNET_SYSERR)
1031   {
1032     server_stop (plugin);
1033     client_stop (plugin);
1034
1035     GNUNET_free_non_null (plugin->server_addr_v4);
1036     GNUNET_free_non_null (plugin->server_addr_v6);
1037     GNUNET_free (plugin);
1038     GNUNET_free (api);
1039     return NULL;
1040   }
1041
1042   /* Report addresses to transport service */
1043   start_report_addresses (plugin);
1044
1045 #if DEBUG_HTTP
1046   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1047                    "Plugin `%s' loaded\n", plugin->name);
1048 #endif
1049
1050   return api;
1051 }
1052
1053
1054 /**
1055  * Exit point from the plugin.
1056  */
1057 void *
1058 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1059 {
1060   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1061   struct Plugin *plugin = api->cls;
1062   struct Session *s = NULL;
1063
1064   /* Stop reporting addresses to transport service */
1065   stop_report_addresses (plugin);
1066
1067   /* cleaning up sessions */
1068   s = plugin->head;
1069   while (s != NULL)
1070   {
1071     struct Session *t = s->next;
1072 #if DEBUG_HTTP
1073   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1074                    "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1075 #endif
1076     if (s->inbound == GNUNET_NO)
1077       GNUNET_assert (GNUNET_OK == client_disconnect (s));
1078     else
1079       GNUNET_assert (GNUNET_OK == server_disconnect (s));
1080
1081     GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
1082     delete_session (s);
1083     s = t;
1084   }
1085
1086 #if DEBUG_HTTP
1087   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1088                    "Stopping server\n");
1089 #endif
1090   /* Stop server */
1091   server_stop (plugin);
1092
1093 #if DEBUG_HTTP
1094   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1095                    "Stopping client\n");
1096 #endif
1097   /* Stop client */
1098   client_stop (plugin);
1099
1100
1101 #if DEBUG_HTTP
1102   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1103                    "Plugin `%s' unloaded\n", plugin->name);
1104 #endif
1105
1106   GNUNET_free_non_null (plugin->server_addr_v4);
1107   GNUNET_free_non_null (plugin->server_addr_v6);
1108   GNUNET_free (plugin);
1109   GNUNET_free (api);
1110
1111   return NULL;
1112 }
1113
1114 /* end of plugin_transport_http.c */