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