042c977f555eacd141b40f0d809c5a2c7ef03058
[oweals/gnunet.git] / src / transport / plugin_transport_http_new.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   GNUNET_free (s->addr);
402   GNUNET_free_non_null(s->server_recv);
403   GNUNET_free_non_null(s->server_send);
404   GNUNET_free (s);
405 }
406
407 struct Session *
408 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
409                 const void *addr, size_t addrlen,
410                 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
411 {
412   struct Session *s = NULL;
413
414   s = GNUNET_malloc (sizeof (struct Session));
415   memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
416   s->plugin = plugin;
417   s->addr = GNUNET_malloc (addrlen);
418   memcpy (s->addr, addr, addrlen);
419   s->addrlen = addrlen;
420   s->transmit_cont = cont;
421   s->transmit_cont_cls = cont_cls;
422   s->next = NULL;
423   s->delay = GNUNET_TIME_absolute_get_forever();
424   return s;
425 }
426
427 void
428 notify_session_end (void *cls,
429                     const struct GNUNET_PeerIdentity *
430                     peer, struct Session * s)
431 {
432   struct Plugin *plugin = cls;
433
434   plugin->env->session_end (NULL, peer, s);
435   GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
436   delete_session (s);
437 }
438
439
440 /**
441  * Function that can be used by the transport service to transmit
442  * a message using the plugin.   Note that in the case of a
443  * peer disconnecting, the continuation MUST be called
444  * prior to the disconnect notification itself.  This function
445  * will be called with this peer's HELLO message to initiate
446  * a fresh connection to another peer.
447  *
448  * @param cls closure
449  * @param target who should receive this message
450  * @param msgbuf the message to transmit
451  * @param msgbuf_size number of bytes in 'msgbuf'
452  * @param priority how important is the message (most plugins will
453  *                 ignore message priority and just FIFO)
454  * @param to how long to wait at most for the transmission (does not
455  *                require plugins to discard the message after the timeout,
456  *                just advisory for the desired delay; most plugins will ignore
457  *                this as well)
458  * @param session which session must be used (or NULL for "any")
459  * @param addr the address to use (can be NULL if the plugin
460  *                is "on its own" (i.e. re-use existing TCP connection))
461  * @param addrlen length of the address in bytes
462  * @param force_address GNUNET_YES if the plugin MUST use the given address,
463  *                GNUNET_NO means the plugin may use any other address and
464  *                GNUNET_SYSERR means that only reliable existing
465  *                bi-directional connections should be used (regardless
466  *                of address)
467  * @param cont continuation to call once the message has
468  *        been transmitted (or if the transport is ready
469  *        for the next transmission call; or if the
470  *        peer disconnected...); can be NULL
471  * @param cont_cls closure for cont
472  * @return number of bytes used (on the physical network, with overheads);
473  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
474  *         and does NOT mean that the message was not transmitted (DV)
475  */
476 static ssize_t
477 http_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
478                   const char *msgbuf, size_t msgbuf_size, unsigned int priority,
479                   struct GNUNET_TIME_Relative to, struct Session *session,
480                   const void *addr, size_t addrlen, int force_address,
481                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
482 {
483   struct Plugin *plugin = cls;
484   struct HTTP_Message *msg;
485   GNUNET_assert (plugin != NULL);
486
487   int res = GNUNET_SYSERR;
488
489 #if DEBUG_HTTP
490   GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
491                    "Sending %u bytes to peer `%s' on address `%s' %X %i\n", msgbuf_size,
492                    GNUNET_i2s (target), ((addr != NULL) && (addrlen != 0)) ? http_plugin_address_to_string(plugin, addr, addrlen) : "<inbound>", session, force_address);
493 #endif
494
495   struct Session *s = NULL;
496
497   /* look for existing connection */
498   s = lookup_session (plugin, target, session, addr, addrlen, 1);
499 #if DEBUG_HTTP
500   GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
501                    "%s existing session: %s\n", (s!=NULL) ? "Found" : "NOT Found", ((s != NULL) && (s->inbound == GNUNET_YES)) ? "inbound" : "outbound");
502 #endif
503
504   /* create new outbound connection */
505   if (s == NULL)
506   {
507     if (plugin->max_connections <= plugin->cur_connections)
508     {
509       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
510                        "Maximum number of connections reached, "
511                        "cannot connect to peer `%s'\n",
512                        GNUNET_i2s (target));
513       return res;
514     }
515
516 #if DEBUG_HTTP
517     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
518                      "Initiiating new connection to peer `%s'\n",
519                      GNUNET_i2s (target));
520 #endif
521     s = create_session (plugin, target, addr, addrlen, cont, cont_cls);
522     GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
523     // initiate new connection
524     if (GNUNET_SYSERR == (res = client_connect (s)))
525     {
526       GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
527       delete_session (s);
528       return GNUNET_SYSERR;
529     }
530   }
531
532   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
533   msg->next = NULL;
534   msg->size = msgbuf_size;
535   msg->pos = 0;
536   msg->buf = (char *) &msg[1];
537   msg->transmit_cont = cont;
538   msg->transmit_cont_cls = cont_cls;
539   memcpy (msg->buf, msgbuf, msgbuf_size);
540
541   if (s->inbound == GNUNET_NO)
542   {
543 #if DEBUG_HTTP
544     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
545                      "Using outbound client session to send to `%s'\n",
546                      GNUNET_i2s (target));
547 #endif
548      client_send (s, msg);
549      res = msgbuf_size;
550   }
551   if (s->inbound == GNUNET_YES)
552   {
553     server_send (s, msg);
554     res = msgbuf_size;
555 #if DEBUG_HTTP
556     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
557                      "Using inbound server session to send to `%s'\n",
558                      GNUNET_i2s (target));
559 #endif
560
561   }
562   return res;
563 }
564
565
566 /**
567  * Function that can be used to force the plugin to disconnect
568  * from the given peer and cancel all previous transmissions
569  * (and their continuationc).
570  *
571  * @param cls closure
572  * @param target peer from which to disconnect
573  */
574 static void
575 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
576 {
577   struct Plugin *plugin = cls;
578   struct Session *next = NULL;
579   struct Session *s = plugin->head;
580
581   while (s != NULL)
582   {
583     next = s->next;
584     if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
585     {
586       if (s->inbound == GNUNET_NO)
587         GNUNET_assert (GNUNET_OK == client_disconnect (s));
588       else
589         GNUNET_assert (GNUNET_OK == server_disconnect (s));
590       GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
591       delete_session (s);
592     }
593     s = next;
594   }
595 }
596
597 static void
598 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
599                  socklen_t addrlen)
600 {
601   struct Plugin *plugin = cls;
602   struct IPv4HttpAddressWrapper *w_t4 = NULL;
603   struct IPv6HttpAddressWrapper *w_t6 = NULL;
604   int af;
605
606   af = addr->sa_family;
607   switch (af)
608   {
609   case AF_INET:
610     w_t4 = plugin->ipv4_addr_head;
611     while (w_t4 != NULL)
612     {
613       int res = memcmp (&w_t4->addr,
614                         (struct sockaddr_in *) addr,
615                         sizeof (struct sockaddr_in));
616
617       if (0 == res)
618         break;
619       w_t4 = w_t4->next;
620     }
621     if (w_t4 == NULL)
622     {
623       w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
624       memcpy (&w_t4->addr, (struct sockaddr_in *) addr,
625               sizeof (struct sockaddr_in));
626
627       GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
628                                    plugin->ipv4_addr_tail, w_t4);
629     }
630 #if DEBUG_HTTP
631     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
632                      "Notifying transport to add IPv4 address `%s'\n",
633                      http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct sockaddr_in)));
634 #endif
635     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr, sizeof (struct sockaddr_in));
636
637     break;
638   case AF_INET6:
639     w_t6 = plugin->ipv6_addr_head;
640     while (w_t6)
641     {
642       int res = memcmp (&w_t6->addr,
643                         (struct sockaddr_in6 *) addr,
644                         sizeof (struct sockaddr_in6));
645
646       if (0 == res)
647         break;
648       w_t6 = w_t6->next;
649     }
650     if (w_t6 == NULL)
651     {
652       w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
653       memcpy (&w_t6->addr, (struct sockaddr_in6 *) addr,
654               sizeof (struct sockaddr_in6));
655
656       GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
657                                    plugin->ipv6_addr_tail, w_t6);
658     }
659 #if DEBUG_HTTP
660     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
661                      "Notifying transport to add IPv6 address `%s'\n",
662                      http_plugin_address_to_string(NULL, &w_t6->addr, sizeof (struct sockaddr_in6)));
663 #endif
664     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr, sizeof (struct sockaddr_in6));
665     break;
666   default:
667     return;
668   }
669
670 }
671
672 static void
673 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
674                     socklen_t addrlen)
675 {
676   struct Plugin *plugin = cls;
677   struct IPv4HttpAddressWrapper *w_t4 = NULL;
678   struct IPv6HttpAddressWrapper *w_t6 = NULL;
679   int af;
680
681   af = addr->sa_family;
682   switch (af)
683   {
684   case AF_INET:
685     w_t4 = plugin->ipv4_addr_head;
686     while (w_t4 != NULL)
687     {
688       int res = memcmp (&(w_t4->addr.sin_addr),
689                         &((struct sockaddr_in *) addr)->sin_addr,
690                         sizeof (struct in_addr));
691
692       if (0 == res)
693         break;
694       w_t4 = w_t4->next;
695     }
696     if (w_t4 == NULL)
697       return;
698
699 #if DEBUG_HTTP
700     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
701                      "Notifying transport to remove IPv4 address `%s'\n",
702                      http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct sockaddr_in)));
703 #endif
704     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
705                                  sizeof (struct sockaddr_in));
706
707     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
708                                  w_t4);
709     GNUNET_free (w_t4);
710     break;
711   case AF_INET6:
712     w_t6 = plugin->ipv6_addr_head;
713     while (w_t6 != NULL)
714     {
715       int res = memcmp (&(w_t6->addr.sin6_addr),
716                         &((struct sockaddr_in6 *) addr)->sin6_addr,
717                         sizeof (struct in6_addr));
718
719       if (0 == res)
720         break;
721       w_t6 = w_t6->next;
722     }
723     if (w_t6 == NULL)
724       return;
725 #if DEBUG_HTTP
726     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
727                      "Notifying transport to remove IPv6 address `%s'\n",
728                      http_plugin_address_to_string(NULL, &w_t6->addr, sizeof (struct sockaddr_in6)));
729 #endif
730     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr,
731                                  sizeof (struct sockaddr_in6 ));
732
733     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
734                                  w_t6);
735     GNUNET_free (w_t6);
736     break;
737   default:
738     return;
739   }
740
741 }
742
743 /**
744  * Our external IP address/port mapping has changed.
745  *
746  * @param cls closure, the 'struct LocalAddrList'
747  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
748  *     the previous (now invalid) one
749  * @param addr either the previous or the new public IP address
750  * @param addrlen actual lenght of the address
751  */
752 static void
753 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
754                        socklen_t addrlen)
755 {
756   GNUNET_assert (cls != NULL);
757   struct Plugin *plugin = cls;
758   static int limit;
759 #if DEBUG_HTTP
760   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
761                    "NPMC called %s to address `%s'\n",
762                    (add_remove == GNUNET_NO) ? "remove" : "add",
763                    GNUNET_a2s (addr, addrlen));
764 #endif
765   /* convert 'addr' to our internal format */
766   switch (add_remove)
767   {
768   case GNUNET_YES:
769     // FIXME DEBUGGING
770     if (limit < 1)
771       nat_add_address (cls, add_remove, addr, addrlen);
772     limit++;
773     // FIXME END
774     break;
775   case GNUNET_NO:
776     nat_remove_address (cls, add_remove, addr, addrlen);
777     break;
778   }
779 }
780
781
782 static void
783 start_report_addresses (struct Plugin *plugin)
784 {
785   int res = GNUNET_OK;
786   struct sockaddr **addrs;
787   socklen_t *addrlens;
788
789   res =
790       GNUNET_SERVICE_get_server_addresses (plugin->name, plugin->env->cfg,
791                                            &addrs, &addrlens);
792 #if 0
793   GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
794                    _("FOUND %u addresses\n"),res);
795 #endif
796
797   if (res != GNUNET_SYSERR)
798   {
799     plugin->nat =
800         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
801                              (unsigned int) res,
802                              (const struct sockaddr **) addrs, addrlens,
803                              &nat_port_map_callback, NULL,
804                              plugin);
805     while (res > 0)
806     {
807       res--;
808 #if 0
809       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
810                        _("FREEING %s\n"),
811                        GNUNET_a2s (addrs[res], addrlens[res]));
812 #endif
813       GNUNET_assert (addrs[res] != NULL);
814       GNUNET_free (addrs[res]);
815     }
816     GNUNET_free_non_null (addrs);
817     GNUNET_free_non_null (addrlens);
818   }
819   else
820   {
821     plugin->nat =
822         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
823                              NULL, NULL, plugin);
824   }
825 }
826
827 static void
828 stop_report_addresses (struct Plugin *plugin)
829 {
830   /* Stop NAT handle */
831   GNUNET_NAT_unregister (plugin->nat);
832
833   /* Clean up addresses */
834   struct IPv4HttpAddressWrapper *w_t4;
835   struct IPv6HttpAddressWrapper *w_t6;
836
837   while (plugin->ipv4_addr_head != NULL)
838   {
839     w_t4 = plugin->ipv4_addr_head;
840     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
841                                  w_t4);
842     GNUNET_free (w_t4);
843   }
844
845   while (plugin->ipv6_addr_head != NULL)
846   {
847     w_t6 = plugin->ipv6_addr_head;
848     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
849                                  w_t6);
850     GNUNET_free (w_t6);
851   }
852 }
853
854 static int
855 configure_plugin (struct Plugin *plugin)
856 {
857   int res = GNUNET_OK;
858
859   /* Use IPv4? */
860   if (GNUNET_CONFIGURATION_have_value
861       (plugin->env->cfg, plugin->name, "USE_IPv4"))
862   {
863     plugin->ipv4 =
864         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
865                                               "USE_IPv4");
866   }
867   else
868     plugin->ipv4 = GNUNET_YES;
869
870   /* Use IPv6? */
871   if (GNUNET_CONFIGURATION_have_value
872       (plugin->env->cfg, plugin->name, "USE_IPv6"))
873   {
874     plugin->ipv6 =
875         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
876                                               "USE_IPv6");
877   }
878   else
879     plugin->ipv6 = GNUNET_YES;
880
881   if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
882   {
883     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
884                      _
885                      ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
886                      plugin->name);
887     res = GNUNET_SYSERR;
888   }
889
890   /* Reading port number from config file */
891   unsigned long long port;
892
893   if ((GNUNET_OK !=
894        GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
895                                               "PORT", &port)) || (port > 65535))
896   {
897     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
898                      _("Port is required! Fix in configuration\n"),
899                      plugin->name);
900     res = GNUNET_SYSERR;
901     goto fail;
902   }
903   plugin->port = port;
904
905
906   char * bind4_address = NULL;
907   if ((plugin->ipv4 == GNUNET_YES) && (GNUNET_YES ==
908       GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
909                                              "BINDTO", &bind4_address)))
910   {
911     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
912                 "Binding %s plugin to specific IPv4 address: `%s'\n",
913                 plugin->protocol, bind4_address);
914     plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
915     if (1 != inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
916     {
917       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
918                   _("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
919                   bind4_address, plugin->protocol);
920       GNUNET_free (plugin->server_addr_v4);
921       plugin->server_addr_v4 = NULL;
922     }
923     else
924     {
925       plugin->server_addr_v4->sin_family = AF_INET;
926       plugin->server_addr_v4->sin_port = htons (plugin->port);
927     }
928     GNUNET_free (bind4_address);
929   }
930
931
932   char * bind6_address = NULL;
933   if ((plugin->ipv6 == GNUNET_YES) && (GNUNET_YES ==
934       GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
935                                              "BINDTO6", &bind6_address)))
936   {
937     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
938                 "Binding %s plugin to specific IPv6 address: `%s'\n",
939                 plugin->protocol, bind6_address);
940     plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
941     if (1 != inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
942     {
943       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
944                   _("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
945                   bind6_address, plugin->protocol);
946       GNUNET_free (plugin->server_addr_v6);
947       plugin->server_addr_v6 = NULL;
948     }
949     else
950     {
951       plugin->server_addr_v6->sin6_family = AF_INET6;
952       plugin->server_addr_v6->sin6_port = htons (plugin->port);
953     }
954     GNUNET_free (bind6_address);
955   }
956
957
958   /* Optional parameters */
959   unsigned long long maxneigh;
960   if (GNUNET_OK !=
961       GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
962                                              "MAX_CONNECTIONS", &maxneigh))
963     maxneigh = 128;
964   plugin->max_connections = maxneigh;
965
966 fail:
967   return res;
968 }
969
970 /**
971  * Entry point for the plugin.
972  */
973 void *
974 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
975 {
976   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
977   struct GNUNET_TRANSPORT_PluginFunctions *api;
978   struct Plugin *plugin;
979   int res;
980
981   plugin = GNUNET_malloc (sizeof (struct Plugin));
982   plugin->env = env;
983   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
984   api->cls = plugin;
985   api->send = &http_plugin_send;
986   api->disconnect = &http_plugin_disconnect;
987   api->address_pretty_printer = &http_plugin_address_pretty_printer;
988   api->check_address = &http_plugin_address_suggested;
989   api->address_to_string = &http_plugin_address_to_string;
990
991 #if BUILD_HTTPS
992   plugin->name = "transport-https";
993   plugin->protocol = "https";
994 #else
995   plugin->name = "transport-http";
996   plugin->protocol = "http";
997 #endif
998   /* Configure plugin from configuration */
999
1000   res = configure_plugin (plugin);
1001   if (res == GNUNET_SYSERR)
1002   {
1003     GNUNET_free_non_null (plugin->server_addr_v4);
1004     GNUNET_free_non_null (plugin->server_addr_v6);
1005     GNUNET_free (plugin);
1006     GNUNET_free (api);
1007     return NULL;
1008   }
1009
1010   /* Start client */
1011   res = client_start (plugin);
1012   if (res == GNUNET_SYSERR)
1013   {
1014     GNUNET_free_non_null (plugin->server_addr_v4);
1015     GNUNET_free_non_null (plugin->server_addr_v6);
1016     GNUNET_free (plugin);
1017     GNUNET_free (api);
1018     return NULL;
1019   }
1020
1021   /* Start server */
1022   res = server_start (plugin);
1023   if (res == GNUNET_SYSERR)
1024   {
1025     server_stop (plugin);
1026     client_stop (plugin);
1027
1028     GNUNET_free_non_null (plugin->server_addr_v4);
1029     GNUNET_free_non_null (plugin->server_addr_v6);
1030     GNUNET_free (plugin);
1031     GNUNET_free (api);
1032     return NULL;
1033   }
1034
1035   /* Report addresses to transport service */
1036   start_report_addresses (plugin);
1037
1038 #if DEBUG_HTTP
1039   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1040                    "Plugin `%s' loaded\n", plugin->name);
1041 #endif
1042
1043   return api;
1044 }
1045
1046
1047 /**
1048  * Exit point from the plugin.
1049  */
1050 void *
1051 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1052 {
1053   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1054   struct Plugin *plugin = api->cls;
1055   struct Session *s = NULL;
1056
1057   /* Stop reporting addresses to transport service */
1058   stop_report_addresses (plugin);
1059
1060   /* cleaning up sessions */
1061   s = plugin->head;
1062   while (s != NULL)
1063   {
1064     struct Session *t = s->next;
1065 #if DEBUG_HTTP
1066   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1067                    "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1068 #endif
1069     if (s->inbound == GNUNET_NO)
1070       GNUNET_assert (GNUNET_OK == client_disconnect (s));
1071     else
1072       GNUNET_assert (GNUNET_OK == server_disconnect (s));
1073
1074     GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
1075     delete_session (s);
1076     s = t;
1077   }
1078
1079 #if DEBUG_HTTP
1080   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1081                    "Stopping server\n");
1082 #endif
1083   /* Stop server */
1084   server_stop (plugin);
1085
1086 #if DEBUG_HTTP
1087   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1088                    "Stopping client\n");
1089 #endif
1090   /* Stop client */
1091   client_stop (plugin);
1092
1093
1094 #if DEBUG_HTTP
1095   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1096                    "Plugin `%s' unloaded\n", plugin->name);
1097 #endif
1098
1099   GNUNET_free_non_null (plugin->server_addr_v4);
1100   GNUNET_free_non_null (plugin->server_addr_v6);
1101   GNUNET_free (plugin);
1102   GNUNET_free (api);
1103
1104   return NULL;
1105 }
1106
1107 /* end of plugin_transport_http.c */