60eefd419d30e56141ed98029aea32f9a35e7b67
[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->next = NULL;
426   s->next_receive = GNUNET_TIME_absolute_get_zero();
427   return s;
428 }
429
430 void
431 notify_session_end (void *cls,
432                     const struct GNUNET_PeerIdentity *
433                     peer, struct Session * s)
434 {
435   struct Plugin *plugin = cls;
436
437   plugin->env->session_end (NULL, peer, s);
438   GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
439   delete_session (s);
440 }
441
442
443 /**
444  * Function that can be used by the transport service to transmit
445  * a message using the plugin.   Note that in the case of a
446  * peer disconnecting, the continuation MUST be called
447  * prior to the disconnect notification itself.  This function
448  * will be called with this peer's HELLO message to initiate
449  * a fresh connection to another peer.
450  *
451  * @param cls closure
452  * @param target who should receive this message
453  * @param msgbuf the message to transmit
454  * @param msgbuf_size number of bytes in 'msgbuf'
455  * @param priority how important is the message (most plugins will
456  *                 ignore message priority and just FIFO)
457  * @param to how long to wait at most for the transmission (does not
458  *                require plugins to discard the message after the timeout,
459  *                just advisory for the desired delay; most plugins will ignore
460  *                this as well)
461  * @param session which session must be used (or NULL for "any")
462  * @param addr the address to use (can be NULL if the plugin
463  *                is "on its own" (i.e. re-use existing TCP connection))
464  * @param addrlen length of the address in bytes
465  * @param force_address GNUNET_YES if the plugin MUST use the given address,
466  *                GNUNET_NO means the plugin may use any other address and
467  *                GNUNET_SYSERR means that only reliable existing
468  *                bi-directional connections should be used (regardless
469  *                of address)
470  * @param cont continuation to call once the message has
471  *        been transmitted (or if the transport is ready
472  *        for the next transmission call; or if the
473  *        peer disconnected...); can be NULL
474  * @param cont_cls closure for cont
475  * @return number of bytes used (on the physical network, with overheads);
476  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
477  *         and does NOT mean that the message was not transmitted (DV)
478  */
479 static ssize_t
480 http_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
481                   const char *msgbuf, size_t msgbuf_size, unsigned int priority,
482                   struct GNUNET_TIME_Relative to, struct Session *session,
483                   const void *addr, size_t addrlen, int force_address,
484                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
485 {
486   struct Plugin *plugin = cls;
487   struct HTTP_Message *msg;
488   GNUNET_assert (plugin != NULL);
489
490   int res = GNUNET_SYSERR;
491
492 #if DEBUG_HTTP
493   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
494                    "Sending %u bytes to peer `%s' on address `%s' %X %i\n", msgbuf_size,
495                    GNUNET_i2s (target), ((addr != NULL) && (addrlen != 0)) ? http_plugin_address_to_string(plugin, addr, addrlen) : "<inbound>", session, force_address);
496 #endif
497
498   struct Session *s = NULL;
499
500   /* look for existing connection */
501   s = lookup_session (plugin, target, session, addr, addrlen, 1);
502 #if DEBUG_HTTP
503   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
504                    "%s existing session: %s\n", (s!=NULL) ? "Found" : "NOT Found", ((s != NULL) && (s->inbound == GNUNET_YES)) ? "inbound" : "outbound");
505 #endif
506
507   /* create new outbound connection */
508   if (s == NULL)
509   {
510     if (plugin->max_connections <= plugin->cur_connections)
511     {
512       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
513                        "Maximum number of connections reached, "
514                        "cannot connect to peer `%s'\n",
515                        GNUNET_i2s (target));
516       return res;
517     }
518
519 #if DEBUG_HTTP
520     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
521                      "Initiiating new connection to peer `%s'\n",
522                      GNUNET_i2s (target));
523 #endif
524     s = create_session (plugin, target, addr, addrlen, cont, cont_cls);
525     GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
526     // initiate new connection
527     if (GNUNET_SYSERR == (res = client_connect (s)))
528     {
529       GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
530       delete_session (s);
531       return GNUNET_SYSERR;
532     }
533   }
534
535   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
536   msg->next = NULL;
537   msg->size = msgbuf_size;
538   msg->pos = 0;
539   msg->buf = (char *) &msg[1];
540   msg->transmit_cont = cont;
541   msg->transmit_cont_cls = cont_cls;
542   memcpy (msg->buf, msgbuf, msgbuf_size);
543
544   if (s->inbound == GNUNET_NO)
545   {
546 #if DEBUG_HTTP
547     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
548                      "Using outbound client session to send to `%s'\n",
549                      GNUNET_i2s (target));
550 #endif
551      client_send (s, msg);
552      res = msgbuf_size;
553   }
554   if (s->inbound == GNUNET_YES)
555   {
556     server_send (s, msg);
557     res = msgbuf_size;
558 #if DEBUG_HTTP
559     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
560                      "Using inbound server session to send to `%s'\n",
561                      GNUNET_i2s (target));
562 #endif
563
564   }
565   return res;
566 }
567
568
569 /**
570  * Function that can be used to force the plugin to disconnect
571  * from the given peer and cancel all previous transmissions
572  * (and their continuationc).
573  *
574  * @param cls closure
575  * @param target peer from which to disconnect
576  */
577 static void
578 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
579 {
580   struct Plugin *plugin = cls;
581   struct Session *next = NULL;
582   struct Session *s = plugin->head;
583
584   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
585                    "Transport tells me to disconnect `%s'\n",
586                    GNUNET_i2s (target));
587   while (s != NULL)
588   {
589     next = s->next;
590     if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
591     {
592       if (s->inbound == GNUNET_NO)
593         GNUNET_assert (GNUNET_OK == client_disconnect (s));
594       else
595         GNUNET_assert (GNUNET_OK == server_disconnect (s));
596       GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
597       delete_session (s);
598     }
599     s = next;
600   }
601 }
602
603 static void
604 nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
605                  socklen_t addrlen)
606 {
607   struct Plugin *plugin = cls;
608   struct IPv4HttpAddressWrapper *w_t4 = NULL;
609   struct IPv6HttpAddressWrapper *w_t6 = NULL;
610   int af;
611
612   af = addr->sa_family;
613   switch (af)
614   {
615   case AF_INET:
616     w_t4 = plugin->ipv4_addr_head;
617     while (w_t4 != NULL)
618     {
619       int res = memcmp (&w_t4->addr,
620                         (struct sockaddr_in *) addr,
621                         sizeof (struct sockaddr_in));
622
623       if (0 == res)
624         break;
625       w_t4 = w_t4->next;
626     }
627     if (w_t4 == NULL)
628     {
629       w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
630       memcpy (&w_t4->addr, (struct sockaddr_in *) addr,
631               sizeof (struct sockaddr_in));
632
633       GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
634                                    plugin->ipv4_addr_tail, w_t4);
635     }
636 #if DEBUG_HTTP
637     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
638                      "Notifying transport to add IPv4 address `%s'\n",
639                      http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct sockaddr_in)));
640 #endif
641     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr, sizeof (struct sockaddr_in));
642
643     break;
644   case AF_INET6:
645     w_t6 = plugin->ipv6_addr_head;
646     while (w_t6)
647     {
648       int res = memcmp (&w_t6->addr,
649                         (struct sockaddr_in6 *) addr,
650                         sizeof (struct sockaddr_in6));
651
652       if (0 == res)
653         break;
654       w_t6 = w_t6->next;
655     }
656     if (w_t6 == NULL)
657     {
658       w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
659       memcpy (&w_t6->addr, (struct sockaddr_in6 *) addr,
660               sizeof (struct sockaddr_in6));
661
662       GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
663                                    plugin->ipv6_addr_tail, w_t6);
664     }
665 #if DEBUG_HTTP
666     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
667                      "Notifying transport to add IPv6 address `%s'\n",
668                      http_plugin_address_to_string(NULL, &w_t6->addr, sizeof (struct sockaddr_in6)));
669 #endif
670     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr, sizeof (struct sockaddr_in6));
671     break;
672   default:
673     return;
674   }
675
676 }
677
678 static void
679 nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
680                     socklen_t addrlen)
681 {
682   struct Plugin *plugin = cls;
683   struct IPv4HttpAddressWrapper *w_t4 = NULL;
684   struct IPv6HttpAddressWrapper *w_t6 = NULL;
685   int af;
686
687   af = addr->sa_family;
688   switch (af)
689   {
690   case AF_INET:
691     w_t4 = plugin->ipv4_addr_head;
692     while (w_t4 != NULL)
693     {
694       int res = memcmp (&(w_t4->addr.sin_addr),
695                         &((struct sockaddr_in *) addr)->sin_addr,
696                         sizeof (struct in_addr));
697
698       if (0 == res)
699         break;
700       w_t4 = w_t4->next;
701     }
702     if (w_t4 == NULL)
703       return;
704
705 #if DEBUG_HTTP
706     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
707                      "Notifying transport to remove IPv4 address `%s'\n",
708                      http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct sockaddr_in)));
709 #endif
710     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
711                                  sizeof (struct sockaddr_in));
712
713     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
714                                  w_t4);
715     GNUNET_free (w_t4);
716     break;
717   case AF_INET6:
718     w_t6 = plugin->ipv6_addr_head;
719     while (w_t6 != NULL)
720     {
721       int res = memcmp (&(w_t6->addr.sin6_addr),
722                         &((struct sockaddr_in6 *) addr)->sin6_addr,
723                         sizeof (struct in6_addr));
724
725       if (0 == res)
726         break;
727       w_t6 = w_t6->next;
728     }
729     if (w_t6 == NULL)
730       return;
731 #if DEBUG_HTTP
732     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
733                      "Notifying transport to remove IPv6 address `%s'\n",
734                      http_plugin_address_to_string(NULL, &w_t6->addr, sizeof (struct sockaddr_in6)));
735 #endif
736     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr,
737                                  sizeof (struct sockaddr_in6 ));
738
739     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
740                                  w_t6);
741     GNUNET_free (w_t6);
742     break;
743   default:
744     return;
745   }
746
747 }
748
749 /**
750  * Our external IP address/port mapping has changed.
751  *
752  * @param cls closure, the 'struct LocalAddrList'
753  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
754  *     the previous (now invalid) one
755  * @param addr either the previous or the new public IP address
756  * @param addrlen actual lenght of the address
757  */
758 static void
759 nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
760                        socklen_t addrlen)
761 {
762   GNUNET_assert (cls != NULL);
763 #if DEBUG_HTTP
764   struct Plugin *plugin = cls;
765 #endif
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   switch (add_remove)
773   {
774   case GNUNET_YES:
775       nat_add_address (cls, add_remove, addr, addrlen);
776     break;
777   case GNUNET_NO:
778     nat_remove_address (cls, add_remove, addr, addrlen);
779     break;
780   }
781 }
782
783 int
784 http_get_addresses (struct Plugin *plugin,
785                     const char *serviceName,
786                     const struct GNUNET_CONFIGURATION_Handle
787                     *cfg, struct sockaddr ***addrs,
788                     socklen_t ** addr_lens)
789 {
790   int disablev6;
791   struct GNUNET_NETWORK_Handle *desc;
792   unsigned long long port;
793   struct addrinfo hints;
794   struct addrinfo *res;
795   struct addrinfo *pos;
796   struct addrinfo *next;
797   unsigned int i;
798   int resi;
799   int ret;
800   struct sockaddr **saddrs;
801   socklen_t *saddrlens;
802   char *hostname;
803
804   *addrs = NULL;
805   *addr_lens = NULL;
806   desc = NULL;
807
808   disablev6 = !plugin->ipv6;
809
810   if (!disablev6)
811   {
812     /* probe IPv6 support */
813     desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
814     if (NULL == desc)
815     {
816       if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
817           (errno == EACCES))
818       {
819         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
820         return GNUNET_SYSERR;
821       }
822       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
823                   _
824                   ("Disabling IPv6 since it is not supported on this system\n"),
825                   serviceName, STRERROR (errno));
826       disablev6 = GNUNET_YES;
827       plugin->ipv6 = GNUNET_NO;
828     }
829     else
830     {
831       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
832       desc = NULL;
833     }
834   }
835
836   port = 0;
837   if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
838   {
839     GNUNET_break (GNUNET_OK ==
840                   GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
841                                                          "PORT", &port));
842     if (port > 65535)
843     {
844       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
845                   _
846                   ("Require valid port number for service in configuration!\n"));
847       return GNUNET_SYSERR;
848     }
849   }
850
851   if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
852   {
853     GNUNET_break (GNUNET_OK ==
854                   GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
855                                                          "BINDTO", &hostname));
856   }
857   else
858     hostname = NULL;
859
860   if (hostname != NULL)
861   {
862     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
863                 "Resolving `%s' since that is where `%s' will bind to.\n",
864                 hostname, serviceName);
865     memset (&hints, 0, sizeof (struct addrinfo));
866     if (disablev6)
867       hints.ai_family = AF_INET;
868     if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
869         (res == NULL))
870     {
871       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
872                   hostname, gai_strerror (ret));
873       GNUNET_free (hostname);
874       return GNUNET_SYSERR;
875     }
876     next = res;
877     i = 0;
878     while (NULL != (pos = next))
879     {
880       next = pos->ai_next;
881       if ((disablev6) && (pos->ai_family == AF_INET6))
882         continue;
883       i++;
884     }
885     if (0 == i)
886     {
887       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
888                   _("Failed to find %saddress for `%s'.\n"),
889                   disablev6 ? "IPv4 " : "", hostname);
890       freeaddrinfo (res);
891       GNUNET_free (hostname);
892       return GNUNET_SYSERR;
893     }
894     resi = i;
895     saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
896     saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
897     i = 0;
898     next = res;
899     while (NULL != (pos = next))
900     {
901       next = pos->ai_next;
902       if ((disablev6) && (pos->ai_family == AF_INET6))
903         continue;
904       if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
905         continue;               /* not TCP */
906       if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
907         continue;               /* huh? */
908       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
909                   "Service will bind to `%s'\n",
910                   GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
911       if (pos->ai_family == AF_INET)
912       {
913         GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
914         saddrlens[i] = pos->ai_addrlen;
915         saddrs[i] = GNUNET_malloc (saddrlens[i]);
916         memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
917         ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
918       }
919       else
920       {
921         GNUNET_assert (pos->ai_family == AF_INET6);
922         GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
923         saddrlens[i] = pos->ai_addrlen;
924         saddrs[i] = GNUNET_malloc (saddrlens[i]);
925         memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
926         ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
927       }
928       i++;
929     }
930     GNUNET_free (hostname);
931     freeaddrinfo (res);
932     resi = i;
933   }
934   else
935   {
936     /* will bind against everything, just set port */
937     if (disablev6)
938     {
939       /* V4-only */
940       resi = 1;
941       i = 0;
942       saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
943       saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
944
945       saddrlens[i] = sizeof (struct sockaddr_in);
946       saddrs[i] = GNUNET_malloc (saddrlens[i]);
947 #if HAVE_SOCKADDR_IN_SIN_LEN
948       ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
949 #endif
950       ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
951       ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
952     }
953     else
954     {
955       /* dual stack */
956       resi = 2;
957       saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
958       saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
959       i = 0;
960       saddrlens[i] = sizeof (struct sockaddr_in6);
961       saddrs[i] = GNUNET_malloc (saddrlens[i]);
962 #if HAVE_SOCKADDR_IN_SIN_LEN
963       ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
964 #endif
965       ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
966       ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
967       i++;
968       saddrlens[i] = sizeof (struct sockaddr_in);
969       saddrs[i] = GNUNET_malloc (saddrlens[i]);
970 #if HAVE_SOCKADDR_IN_SIN_LEN
971       ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
972 #endif
973       ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
974       ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
975     }
976   }
977   *addrs = saddrs;
978   *addr_lens = saddrlens;
979   return resi;
980 }
981
982 static void
983 start_report_addresses (struct Plugin *plugin)
984 {
985   int res = GNUNET_OK;
986   struct sockaddr **addrs;
987   socklen_t *addrlens;
988
989   res =
990       http_get_addresses (plugin, plugin->name, plugin->env->cfg,
991                                            &addrs, &addrlens);
992   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
993                    _("Found %u addresses to report to NAT service\n"),res);
994
995   if (res != GNUNET_SYSERR)
996   {
997     plugin->nat =
998         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
999                              (unsigned int) res,
1000                              (const struct sockaddr **) addrs, addrlens,
1001                              &nat_port_map_callback, NULL,
1002                              plugin);
1003     while (res > 0)
1004     {
1005       res--;
1006 #if 0
1007       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1008                        _("FREEING %s\n"),
1009                        GNUNET_a2s (addrs[res], addrlens[res]));
1010 #endif
1011       GNUNET_assert (addrs[res] != NULL);
1012       GNUNET_free (addrs[res]);
1013     }
1014     GNUNET_free_non_null (addrs);
1015     GNUNET_free_non_null (addrlens);
1016   }
1017   else
1018   {
1019     plugin->nat =
1020         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
1021                              NULL, NULL, plugin);
1022   }
1023 }
1024
1025 static void
1026 stop_report_addresses (struct Plugin *plugin)
1027 {
1028   /* Stop NAT handle */
1029   GNUNET_NAT_unregister (plugin->nat);
1030
1031   /* Clean up addresses */
1032   struct IPv4HttpAddressWrapper *w_t4;
1033   struct IPv6HttpAddressWrapper *w_t6;
1034
1035   while (plugin->ipv4_addr_head != NULL)
1036   {
1037     w_t4 = plugin->ipv4_addr_head;
1038     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
1039                                  w_t4);
1040     GNUNET_free (w_t4);
1041   }
1042
1043   while (plugin->ipv6_addr_head != NULL)
1044   {
1045     w_t6 = plugin->ipv6_addr_head;
1046     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
1047                                  w_t6);
1048     GNUNET_free (w_t6);
1049   }
1050 }
1051
1052 static int
1053 configure_plugin (struct Plugin *plugin)
1054 {
1055   int res = GNUNET_OK;
1056
1057   /* Use IPv4? */
1058   if (GNUNET_CONFIGURATION_have_value
1059       (plugin->env->cfg, plugin->name, "USE_IPv4"))
1060   {
1061     plugin->ipv4 =
1062         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1063                                               "USE_IPv4");
1064   }
1065   else
1066     plugin->ipv4 = GNUNET_YES;
1067
1068   /* Use IPv6? */
1069   if (GNUNET_CONFIGURATION_have_value
1070       (plugin->env->cfg, plugin->name, "USE_IPv6"))
1071   {
1072     plugin->ipv6 =
1073         GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
1074                                               "USE_IPv6");
1075   }
1076   else
1077     plugin->ipv6 = GNUNET_YES;
1078
1079   if ((plugin->ipv4 == GNUNET_NO) && (plugin->ipv6 == GNUNET_NO))
1080   {
1081     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1082                      _
1083                      ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
1084                      plugin->name);
1085     res = GNUNET_SYSERR;
1086   }
1087
1088   /* Reading port number from config file */
1089   unsigned long long port;
1090
1091   if ((GNUNET_OK !=
1092        GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1093                                               "PORT", &port)) || (port > 65535))
1094   {
1095     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1096                      _("Port is required! Fix in configuration\n"),
1097                      plugin->name);
1098     res = GNUNET_SYSERR;
1099     goto fail;
1100   }
1101   plugin->port = port;
1102
1103   plugin->client_only = GNUNET_NO;
1104   if (plugin->port == 0)
1105   {
1106     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1107                    _("Port 0, client only mode\n"));
1108     plugin->client_only = GNUNET_YES;
1109   }
1110
1111   char * bind4_address = NULL;
1112   if ((plugin->ipv4 == GNUNET_YES) && (GNUNET_YES ==
1113       GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1114                                              "BINDTO", &bind4_address)))
1115   {
1116     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1117                 "Binding %s plugin to specific IPv4 address: `%s'\n",
1118                 plugin->protocol, bind4_address);
1119     plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1120     if (1 != inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
1121     {
1122       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1123                   _("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1124                   bind4_address, plugin->protocol);
1125       GNUNET_free (plugin->server_addr_v4);
1126       plugin->server_addr_v4 = NULL;
1127     }
1128     else
1129     {
1130       plugin->server_addr_v4->sin_family = AF_INET;
1131       plugin->server_addr_v4->sin_port = htons (plugin->port);
1132     }
1133     GNUNET_free (bind4_address);
1134   }
1135
1136
1137   char * bind6_address = NULL;
1138   if ((plugin->ipv6 == GNUNET_YES) && (GNUNET_YES ==
1139       GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
1140                                              "BINDTO6", &bind6_address)))
1141   {
1142     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1143                 "Binding %s plugin to specific IPv6 address: `%s'\n",
1144                 plugin->protocol, bind6_address);
1145     plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1146     if (1 != inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
1147     {
1148       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1149                   _("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
1150                   bind6_address, plugin->protocol);
1151       GNUNET_free (plugin->server_addr_v6);
1152       plugin->server_addr_v6 = NULL;
1153     }
1154     else
1155     {
1156       plugin->server_addr_v6->sin6_family = AF_INET6;
1157       plugin->server_addr_v6->sin6_port = htons (plugin->port);
1158     }
1159     GNUNET_free (bind6_address);
1160   }
1161
1162
1163   /* Optional parameters */
1164   unsigned long long maxneigh;
1165   if (GNUNET_OK !=
1166       GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
1167                                              "MAX_CONNECTIONS", &maxneigh))
1168     maxneigh = 128;
1169   plugin->max_connections = maxneigh;
1170
1171 fail:
1172   return res;
1173 }
1174
1175 /**
1176  * Entry point for the plugin.
1177  */
1178 void *
1179 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
1180 {
1181   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1182   struct GNUNET_TRANSPORT_PluginFunctions *api;
1183   struct Plugin *plugin;
1184   int res;
1185
1186   plugin = GNUNET_malloc (sizeof (struct Plugin));
1187   plugin->env = env;
1188   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1189   api->cls = plugin;
1190   api->send = &http_plugin_send;
1191   api->disconnect = &http_plugin_disconnect;
1192   api->address_pretty_printer = &http_plugin_address_pretty_printer;
1193   api->check_address = &http_plugin_address_suggested;
1194   api->address_to_string = &http_plugin_address_to_string;
1195
1196 #if BUILD_HTTPS
1197   plugin->name = "transport-https";
1198   plugin->protocol = "https";
1199 #else
1200   plugin->name = "transport-http";
1201   plugin->protocol = "http";
1202 #endif
1203   /* Configure plugin from configuration */
1204
1205   res = configure_plugin (plugin);
1206   if (res == GNUNET_SYSERR)
1207   {
1208     GNUNET_free_non_null (plugin->server_addr_v4);
1209     GNUNET_free_non_null (plugin->server_addr_v6);
1210     GNUNET_free (plugin);
1211     GNUNET_free (api);
1212     return NULL;
1213   }
1214
1215   /* Start client */
1216   res = client_start (plugin);
1217   if (res == GNUNET_SYSERR)
1218   {
1219     GNUNET_free_non_null (plugin->server_addr_v4);
1220     GNUNET_free_non_null (plugin->server_addr_v6);
1221     GNUNET_free (plugin);
1222     GNUNET_free (api);
1223     return NULL;
1224   }
1225
1226   /* Start server */
1227   if (plugin->client_only == GNUNET_NO)
1228   {
1229     res = server_start (plugin);
1230     if (res == GNUNET_SYSERR)
1231     {
1232       server_stop (plugin);
1233       client_stop (plugin);
1234
1235       GNUNET_free_non_null (plugin->server_addr_v4);
1236       GNUNET_free_non_null (plugin->server_addr_v6);
1237       GNUNET_free (plugin);
1238       GNUNET_free (api);
1239       return NULL;
1240     }
1241   }
1242   /* Report addresses to transport service */
1243   start_report_addresses (plugin);
1244
1245 #if DEBUG_HTTP
1246   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1247                    "Plugin `%s' loaded\n", plugin->name);
1248 #endif
1249
1250   return api;
1251 }
1252
1253
1254 /**
1255  * Exit point from the plugin.
1256  */
1257 void *
1258 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1259 {
1260   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1261   struct Plugin *plugin = api->cls;
1262   struct Session *s = NULL;
1263
1264   /* Stop reporting addresses to transport service */
1265   stop_report_addresses (plugin);
1266
1267   /* cleaning up sessions */
1268   s = plugin->head;
1269   while (s != NULL)
1270   {
1271     struct Session *t = s->next;
1272 #if DEBUG_HTTP
1273   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1274                    "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
1275 #endif
1276     if (s->inbound == GNUNET_NO)
1277       GNUNET_assert (GNUNET_OK == client_disconnect (s));
1278     else
1279       GNUNET_assert (GNUNET_OK == server_disconnect (s));
1280
1281     GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
1282     delete_session (s);
1283     s = t;
1284   }
1285
1286 #if DEBUG_HTTP
1287   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1288                    "Stopping server\n");
1289 #endif
1290   /* Stop server */
1291   server_stop (plugin);
1292
1293 #if DEBUG_HTTP
1294   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1295                    "Stopping client\n");
1296 #endif
1297   /* Stop client */
1298   client_stop (plugin);
1299
1300
1301 #if DEBUG_HTTP
1302   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1303                    "Plugin `%s' unloaded\n", plugin->name);
1304 #endif
1305
1306   GNUNET_free_non_null (plugin->server_addr_v4);
1307   GNUNET_free_non_null (plugin->server_addr_v6);
1308   GNUNET_free (plugin);
1309   GNUNET_free (api);
1310
1311   return NULL;
1312 }
1313
1314 /* end of plugin_transport_http.c */