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