89d270cc8432721b9cb0605029ef7106fe74779b
[oweals/gnunet.git] / src / transport / plugin_transport_tcp.c
1 /*
2   This file is part of GNUnet
3   (C) 2002--2014 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  * @file transport/plugin_transport_tcp.c
22  * @brief Implementation of the TCP transport service
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_hello_lib.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_nat_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_resolver_service.h"
32 #include "gnunet_signatures.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_transport_plugin.h"
36 #include "transport.h"
37
38 #define LOG(kind,...) GNUNET_log_from (kind, "transport-tcp",__VA_ARGS__)
39
40 #define PLUGIN_NAME "tcp"
41
42 /**
43  * How long until we give up on establishing an NAT connection?
44  * Must be > 4 RTT
45  */
46 #define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
47
48 GNUNET_NETWORK_STRUCT_BEGIN
49
50
51 /**
52  * Initial handshake message for a session.
53  */
54 struct WelcomeMessage
55 {
56   /**
57    * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
58    */
59   struct GNUNET_MessageHeader header;
60
61   /**
62    * Identity of the node connecting (TCP client)
63    */
64   struct GNUNET_PeerIdentity clientIdentity;
65
66 };
67
68 /**
69  * Basically a WELCOME message, but with the purpose
70  * of giving the waiting peer a client handle to use
71  */
72 struct TCP_NAT_ProbeMessage
73 {
74   /**
75    * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
76    */
77   struct GNUNET_MessageHeader header;
78
79   /**
80    * Identity of the sender of the message.
81    */
82   struct GNUNET_PeerIdentity clientIdentity;
83
84 };
85 GNUNET_NETWORK_STRUCT_END
86
87 /**
88  * Context for sending a NAT probe via TCP.
89  */
90 struct TCPProbeContext
91 {
92
93   /**
94    * Active probes are kept in a DLL.
95    */
96   struct TCPProbeContext *next;
97
98   /**
99    * Active probes are kept in a DLL.
100    */
101   struct TCPProbeContext *prev;
102
103   /**
104    * Probe connection.
105    */
106   struct GNUNET_CONNECTION_Handle *sock;
107
108   /**
109    * Message to be sent.
110    */
111   struct TCP_NAT_ProbeMessage message;
112
113   /**
114    * Handle to the transmission.
115    */
116   struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
117
118   /**
119    * Transport plugin handle.
120    */
121   struct Plugin *plugin;
122 };
123
124 /**
125  * Bits in the `options` field of TCP addresses.
126  */
127 enum TcpAddressOptions
128 {
129
130   /**
131    * No bits set.
132    */
133   TCP_OPTIONS_NONE = 0,
134
135   /**
136    * See #HTTP_OPTIONS_VERIFY_CERTIFICATE.
137    */
138   TCP_OPTIONS_RESERVED = 1,
139
140   /**
141    * Enable TCP Stealth-style port knocking.
142    */
143   TCP_OPTIONS_TCP_STEALTH = 2
144 };
145
146 GNUNET_NETWORK_STRUCT_BEGIN
147
148 /**
149  * Network format for IPv4 addresses.
150  */
151 struct IPv4TcpAddress
152 {
153   /**
154    * Optional options and flags for this address,
155    * see `enum TcpAddressOptions`
156    */
157   uint32_t options;
158
159   /**
160    * IPv4 address, in network byte order.
161    */
162   uint32_t ipv4_addr GNUNET_PACKED;
163
164   /**
165    * Port number, in network byte order.
166    */
167   uint16_t t4_port GNUNET_PACKED;
168
169 };
170
171 /**
172  * Network format for IPv6 addresses.
173  */
174 struct IPv6TcpAddress
175 {
176   /**
177    * Optional flags for this address
178    * see `enum TcpAddressOptions`
179    */
180   uint32_t options;
181
182   /**
183    * IPv6 address.
184    */
185   struct in6_addr ipv6_addr GNUNET_PACKED;
186
187   /**
188    * Port number, in network byte order.
189    */
190   uint16_t t6_port GNUNET_PACKED;
191
192 };
193 GNUNET_NETWORK_STRUCT_END
194
195 /**
196  * Encapsulation of all of the state of the plugin.
197  */
198 struct Plugin;
199
200 /**
201  * Information kept for each message that is yet to
202  * be transmitted.
203  */
204 struct PendingMessage
205 {
206
207   /**
208    * This is a doubly-linked list.
209    */
210   struct PendingMessage *next;
211
212   /**
213    * This is a doubly-linked list.
214    */
215   struct PendingMessage *prev;
216
217   /**
218    * The pending message
219    */
220   const char *msg;
221
222   /**
223    * Continuation function to call once the message
224    * has been sent.  Can be NULL if there is no
225    * continuation to call.
226    */
227   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
228
229   /**
230    * Closure for transmit_cont.
231    */
232   void *transmit_cont_cls;
233
234   /**
235    * Timeout value for the pending message.
236    */
237   struct GNUNET_TIME_Absolute timeout;
238
239   /**
240    * So that the gnunet-service-transport can group messages together,
241    * these pending messages need to accept a message buffer and size
242    * instead of just a GNUNET_MessageHeader.
243    */
244   size_t message_size;
245
246 };
247
248 /**
249  * Session handle for TCP connections.
250  */
251 struct Session
252 {
253   /**
254    * To whom are we talking to (set to our identity
255    * if we are still waiting for the welcome message)
256    */
257   struct GNUNET_PeerIdentity target;
258
259   /**
260    * Pointer to the global plugin struct.
261    */
262   struct Plugin *plugin;
263
264   /**
265    * The client (used to identify this connection)
266    */
267   struct GNUNET_SERVER_Client *client;
268
269   /**
270    * Task cleaning up a NAT client connection establishment attempt;
271    */
272   struct GNUNET_SCHEDULER_Task * nat_connection_timeout;
273
274   /**
275    * Messages currently pending for transmission
276    * to this peer, if any.
277    */
278   struct PendingMessage *pending_messages_head;
279
280   /**
281    * Messages currently pending for transmission
282    * to this peer, if any.
283    */
284   struct PendingMessage *pending_messages_tail;
285
286   /**
287    * Handle for pending transmission request.
288    */
289   struct GNUNET_SERVER_TransmitHandle *transmit_handle;
290
291   /**
292    * Address of the other peer.
293    */
294   struct GNUNET_HELLO_Address *address;
295
296   /**
297    * ID of task used to delay receiving more to throttle sender.
298    */
299   struct GNUNET_SCHEDULER_Task * receive_delay_task;
300
301   /**
302    * Session timeout task
303    */
304   struct GNUNET_SCHEDULER_Task * timeout_task;
305
306   /**
307    * When will this session time out?
308    */
309   struct GNUNET_TIME_Absolute timeout;
310
311   /**
312    * When will we continue to read from the socket?
313    * (used to enforce inbound quota).
314    */
315   struct GNUNET_TIME_Absolute receive_delay;
316
317   /**
318    * Last activity on this connection.  Used to select preferred
319    * connection.
320    */
321   struct GNUNET_TIME_Absolute last_activity;
322
323   /**
324    * Number of bytes waiting for transmission to this peer.
325    */
326   unsigned long long bytes_in_queue;
327
328   /**
329    * Number of messages waiting for transmission to this peer.
330    */
331   unsigned int msgs_in_queue;
332
333   /**
334    * Are we still expecting the welcome message? (#GNUNET_YES/#GNUNET_NO)
335    */
336   int expecting_welcome;
337
338   /**
339    * Was this session created using NAT traversal?
340    */
341   int is_nat;
342
343   /**
344    * ATS network type in NBO
345    */
346   enum GNUNET_ATS_Network_Type ats_address_network_type;
347 };
348
349 /**
350  * Encapsulation of all of the state of the plugin.
351  */
352 struct Plugin
353 {
354   /**
355    * Our environment.
356    */
357   struct GNUNET_TRANSPORT_PluginEnvironment *env;
358
359   /**
360    * The listen socket.
361    */
362   struct GNUNET_CONNECTION_Handle *lsock;
363
364   /**
365    * Our handle to the NAT module.
366    */
367   struct GNUNET_NAT_Handle *nat;
368
369   /**
370    * Map from peer identities to sessions for the given peer.
371    */
372   struct GNUNET_CONTAINER_MultiPeerMap *sessionmap;
373
374   /**
375    * Handle to the network service.
376    */
377   struct GNUNET_SERVICE_Context *service;
378
379   /**
380    * Handle to the server for this service.
381    */
382   struct GNUNET_SERVER_Handle *server;
383
384   /**
385    * Copy of the handler array where the closures are
386    * set to this struct's instance.
387    */
388   struct GNUNET_SERVER_MessageHandler *handlers;
389
390   /**
391    * Map of peers we have tried to contact behind a NAT
392    */
393   struct GNUNET_CONTAINER_MultiPeerMap *nat_wait_conns;
394
395   /**
396    * List of active TCP probes.
397    */
398   struct TCPProbeContext *probe_head;
399
400   /**
401    * List of active TCP probes.
402    */
403   struct TCPProbeContext *probe_tail;
404
405   /**
406    * Handle for (DYN)DNS lookup of our external IP.
407    */
408   struct GNUNET_RESOLVER_RequestHandle *ext_dns;
409
410   /**
411    * Function to call about session status changes.
412    */
413   GNUNET_TRANSPORT_SessionInfoCallback sic;
414
415   /**
416    * Closure for @e sic.
417    */
418   void *sic_cls;
419
420   /**
421    * Welcome message used by this peer.
422    */
423   struct WelcomeMessage my_welcome;
424
425   /**
426    * How many more TCP sessions are we allowed to open right now?
427    */
428   unsigned long long max_connections;
429
430   /**
431    * How many more TCP sessions do we have right now?
432    */
433   unsigned long long cur_connections;
434
435   /**
436    * ID of task used to update our addresses when one expires.
437    */
438   struct GNUNET_SCHEDULER_Task * address_update_task;
439
440   /**
441    * Address options
442    */
443   uint32_t myoptions;
444
445   /**
446    * Port that we are actually listening on.
447    */
448   uint16_t open_port;
449
450   /**
451    * Port that the user said we would have visible to the
452    * rest of the world.
453    */
454   uint16_t adv_port;
455
456 };
457
458
459 /**
460  * If a session monitor is attached, notify it about the new
461  * session state.
462  *
463  * @param plugin our plugin
464  * @param session session that changed state
465  * @param state new state of the session
466  */
467 static void
468 notify_session_monitor (struct Plugin *plugin,
469                         struct Session *session,
470                         enum GNUNET_TRANSPORT_SessionState state)
471 {
472   struct GNUNET_TRANSPORT_SessionInfo info;
473
474   if (NULL == plugin->sic)
475     return;
476   memset (&info, 0, sizeof (info));
477   info.state = state;
478   info.is_inbound = (0 != (GNUNET_HELLO_ADDRESS_INFO_INBOUND & session->address->local_info))
479     ? GNUNET_YES
480     : GNUNET_NO;
481   info.num_msg_pending = session->msgs_in_queue;
482   info.num_bytes_pending = session->bytes_in_queue;
483   if (NULL != session->receive_delay_task)
484     info.receive_delay = session->receive_delay;
485   info.session_timeout = session->timeout;
486   info.address = session->address;
487   plugin->sic (plugin->sic_cls,
488                session,
489                &info);
490 }
491
492
493 /**
494  * Function called for a quick conversion of the binary address to
495  * a numeric address.  Note that the caller must not free the
496  * address and that the next call to this function is allowed
497  * to override the address again.
498  *
499  * @param cls closure (`struct Plugin *`)
500  * @param addr binary address
501  * @param addrlen length of @a addr
502  * @return string representing the same address
503  */
504 static const char *
505 tcp_plugin_address_to_string (void *cls,
506                               const void *addr,
507                               size_t addrlen);
508
509
510 /**
511  * Function to check if an inbound connection is acceptable.
512  * Mostly used to limit the total number of open connections
513  * we can have.
514  *
515  * @param cls the `struct Plugin`
516  * @param ucred credentials, if available, otherwise NULL
517  * @param addr address
518  * @param addrlen length of @a addr
519  * @return #GNUNET_YES to allow, #GNUNET_NO to deny, #GNUNET_SYSERR
520  *   for unknown address family (will be denied).
521  */
522 static int
523 plugin_tcp_access_check (void *cls,
524                          const struct GNUNET_CONNECTION_Credentials *ucred,
525                          const struct sockaddr *addr,
526                          socklen_t addrlen)
527 {
528   struct Plugin *plugin = cls;
529
530   LOG (GNUNET_ERROR_TYPE_DEBUG,
531        "Accepting new incoming TCP connection from `%s'\n",
532        GNUNET_a2s (addr, addrlen));
533   if (plugin->cur_connections >= plugin->max_connections)
534     return GNUNET_NO;
535   plugin->cur_connections++;
536   return GNUNET_YES;
537 }
538
539
540 /**
541  * Our external IP address/port mapping has changed.
542  *
543  * @param cls closure, the `struct Plugin`
544  * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
545  *     the previous (now invalid) one
546  * @param addr either the previous or the new public IP address
547  * @param addrlen actual length of @a addr
548  */
549 static void
550 tcp_nat_port_map_callback (void *cls,
551                            int add_remove,
552                            const struct sockaddr *addr,
553                            socklen_t addrlen)
554 {
555   struct Plugin *plugin = cls;
556   struct GNUNET_HELLO_Address *address;
557   struct IPv4TcpAddress t4;
558   struct IPv6TcpAddress t6;
559   void *arg;
560   size_t args;
561
562   LOG(GNUNET_ERROR_TYPE_INFO, "NAT notification to %s address `%s'\n",
563       (GNUNET_YES == add_remove) ? "add" : "remove",
564       GNUNET_a2s (addr, addrlen));
565   /* convert 'addr' to our internal format */
566   switch (addr->sa_family)
567   {
568   case AF_INET:
569     GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
570     memset (&t4, 0, sizeof(t4));
571     t4.options = htonl (plugin->myoptions);
572     t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
573     t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
574     arg = &t4;
575     args = sizeof (t4);
576     break;
577   case AF_INET6:
578     GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
579     memset (&t6, 0, sizeof(t6));
580     memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
581         sizeof(struct in6_addr));
582     t6.options = htonl (plugin->myoptions);
583     t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
584     arg = &t6;
585     args = sizeof (t6);
586     break;
587   default:
588     GNUNET_break(0);
589     return;
590   }
591   /* modify our published address list */
592   GNUNET_assert ((args == sizeof (struct IPv4TcpAddress)) ||
593       (args == sizeof (struct IPv6TcpAddress)));
594   address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
595       PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
596   plugin->env->notify_address (plugin->env->cls, add_remove, address);
597   GNUNET_HELLO_address_free(address);
598 }
599
600
601 /**
602  * Function called for a quick conversion of the binary address to
603  * a numeric address.  Note that the caller must not free the
604  * address and that the next call to this function is allowed
605  * to override the address again.
606  *
607  * @param cls closure (`struct Plugin*`)
608  * @param addr binary address
609  * @param addrlen length of @a addr
610  * @return string representing the same address
611  */
612 static const char *
613 tcp_plugin_address_to_string (void *cls,
614                        const void *addr,
615                        size_t addrlen)
616 {
617   static char rbuf[INET6_ADDRSTRLEN + 12];
618   char buf[INET6_ADDRSTRLEN];
619   const void *sb;
620   struct in_addr a4;
621   struct in6_addr a6;
622   const struct IPv4TcpAddress *t4;
623   const struct IPv6TcpAddress *t6;
624   int af;
625   uint16_t port;
626   uint32_t options;
627
628   switch (addrlen)
629   {
630   case sizeof(struct IPv6TcpAddress):
631     t6 = addr;
632     af = AF_INET6;
633     port = ntohs (t6->t6_port);
634     options = ntohl (t6->options);
635     memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
636     sb = &a6;
637     break;
638   case sizeof(struct IPv4TcpAddress):
639     t4 = addr;
640     af = AF_INET;
641     port = ntohs (t4->t4_port);
642     options = ntohl (t4->options);
643     memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
644     sb = &a4;
645     break;
646   default:
647     LOG (GNUNET_ERROR_TYPE_WARNING,
648          _("Unexpected address length: %u bytes\n"),
649          (unsigned int) addrlen);
650     return NULL ;
651   }
652   if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
653   {
654     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
655                          "inet_ntop");
656     return NULL ;
657   }
658   GNUNET_snprintf (rbuf, sizeof(rbuf),
659                    (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
660                    PLUGIN_NAME,
661                    options,
662                    buf,
663                    port);
664   return rbuf;
665 }
666
667
668 /**
669  * Function called to convert a string address to
670  * a binary address.
671  *
672  * @param cls closure (`struct Plugin*`)
673  * @param addr string address
674  * @param addrlen length of the address
675  * @param buf location to store the buffer
676  * @param added location to store the number of bytes in the buffer.
677  *        If the function returns #GNUNET_SYSERR, its contents are undefined.
678  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
679  */
680 static int
681 tcp_plugin_string_to_address (void *cls,
682                               const char *addr,
683                               uint16_t addrlen,
684                               void **buf,
685                               size_t *added)
686 {
687   struct sockaddr_storage socket_address;
688   char *address;
689   char *plugin;
690   char *optionstr;
691   uint32_t options;
692
693   /* Format tcp.options.address:port */
694   address = NULL;
695   plugin = NULL;
696   optionstr = NULL;
697   if ((NULL == addr) || (addrlen == 0))
698   {
699     GNUNET_break(0);
700     return GNUNET_SYSERR;
701   }
702   if ('\0' != addr[addrlen - 1])
703   {
704     GNUNET_break(0);
705     return GNUNET_SYSERR;
706   }
707   if (strlen (addr) != addrlen - 1)
708   {
709     GNUNET_break(0);
710     return GNUNET_SYSERR;
711   }
712   plugin = GNUNET_strdup (addr);
713   optionstr = strchr (plugin, '.');
714   if (NULL == optionstr)
715   {
716     GNUNET_break(0);
717     GNUNET_free(plugin);
718     return GNUNET_SYSERR;
719   }
720   optionstr[0] = '\0';
721   optionstr++;
722   options = atol (optionstr);
723   address = strchr (optionstr, '.');
724   if (NULL == address)
725   {
726     GNUNET_break(0);
727     GNUNET_free(plugin);
728     return GNUNET_SYSERR;
729   }
730   address[0] = '\0';
731   address++;
732
733   if (GNUNET_OK
734       != GNUNET_STRINGS_to_address_ip (address, strlen (address),
735           &socket_address))
736   {
737     GNUNET_break(0);
738     GNUNET_free(plugin);
739     return GNUNET_SYSERR;
740   }
741
742   GNUNET_free(plugin);
743   switch (socket_address.ss_family)
744   {
745   case AF_INET:
746   {
747     struct IPv4TcpAddress *t4;
748     struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
749     t4 = GNUNET_new (struct IPv4TcpAddress);
750     t4->options = htonl (options);
751     t4->ipv4_addr = in4->sin_addr.s_addr;
752     t4->t4_port = in4->sin_port;
753     *buf = t4;
754     *added = sizeof(struct IPv4TcpAddress);
755     return GNUNET_OK;
756   }
757   case AF_INET6:
758   {
759     struct IPv6TcpAddress *t6;
760     struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
761     t6 = GNUNET_new (struct IPv6TcpAddress);
762     t6->options = htonl (options);
763     t6->ipv6_addr = in6->sin6_addr;
764     t6->t6_port = in6->sin6_port;
765     *buf = t6;
766     *added = sizeof(struct IPv6TcpAddress);
767     return GNUNET_OK;
768   }
769   default:
770     return GNUNET_SYSERR;
771   }
772 }
773
774
775 /**
776  * Find the session handle for the given client.
777  * Currently uses both the hashmap and the client
778  * context, as the client context is new and the
779  * logic still needs to be tested.
780  *
781  * @param plugin the plugin
782  * @param client which client to find the session handle for
783  * @return NULL if no matching session exists
784  */
785 static struct Session *
786 lookup_session_by_client (struct Plugin *plugin,
787                           struct GNUNET_SERVER_Client *client)
788 {
789   return GNUNET_SERVER_client_get_user_context (client, struct Session);
790 }
791
792
793 /**
794  * Functions with this signature are called whenever we need
795  * to close a session due to a disconnect or failure to
796  * establish a connection.
797  *
798  * @param cls the `struct Plugin`
799  * @param session session to close down
800  * @return #GNUNET_OK on success
801  */
802 static int
803 tcp_plugin_disconnect_session (void *cls,
804                                struct Session *session)
805 {
806   struct Plugin *plugin = cls;
807   struct PendingMessage *pm;
808
809   LOG (GNUNET_ERROR_TYPE_DEBUG,
810        "Disconnecting session of peer `%s' address `%s'\n",
811        GNUNET_i2s (&session->target),
812        tcp_plugin_address_to_string (NULL,
813                              session->address->address,
814                              session->address->address_length));
815
816   if (NULL != session->timeout_task)
817   {
818     GNUNET_SCHEDULER_cancel (session->timeout_task);
819     session->timeout_task = NULL;
820     session->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
821   }
822
823   if (GNUNET_YES ==
824       GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap,
825                                             &session->target,
826                                             session))
827   {
828     GNUNET_STATISTICS_update (session->plugin->env->stats,
829         gettext_noop ("# TCP sessions active"), -1, GNUNET_NO);
830   }
831   else
832   {
833     GNUNET_assert(GNUNET_YES ==
834                   GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
835                                                         &session->target,
836                                                         session));
837   }
838   if (NULL != session->client)
839     GNUNET_SERVER_client_set_user_context (session->client,
840                                            NULL);
841
842   /* clean up state */
843   if (NULL != session->transmit_handle)
844   {
845     GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
846     session->transmit_handle = NULL;
847   }
848   plugin->env->unregister_quota_notification (plugin->env->cls,
849                                               &session->target,
850                                               PLUGIN_NAME,
851                                               session);
852   session->plugin->env->session_end (session->plugin->env->cls,
853                                      session->address,
854                                      session);
855
856   if (NULL != session->nat_connection_timeout)
857   {
858     GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
859     session->nat_connection_timeout = NULL;
860   }
861
862   while (NULL != (pm = session->pending_messages_head))
863   {
864     LOG (GNUNET_ERROR_TYPE_DEBUG,
865          (NULL != pm->transmit_cont)
866          ? "Could not deliver message to `%4s'.\n"
867          : "Could not deliver message to `%4s', notifying.\n",
868          GNUNET_i2s (&session->target));
869     GNUNET_STATISTICS_update (session->plugin->env->stats,
870                               gettext_noop ("# bytes currently in TCP buffers"),
871                               -(int64_t) pm->message_size, GNUNET_NO);
872     GNUNET_STATISTICS_update (session->plugin->env->stats,
873                               gettext_noop ("# bytes discarded by TCP (disconnect)"),
874                               pm->message_size,
875                               GNUNET_NO);
876     GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
877                                  session->pending_messages_tail,
878                                  pm);
879     GNUNET_assert (0 < session->msgs_in_queue);
880     session->msgs_in_queue--;
881     GNUNET_assert (pm->message_size <= session->bytes_in_queue);
882     session->bytes_in_queue -= pm->message_size;
883     if (NULL != pm->transmit_cont)
884       pm->transmit_cont (pm->transmit_cont_cls,
885                          &session->target,
886                          GNUNET_SYSERR,
887                          pm->message_size,
888                          0);
889     GNUNET_free (pm);
890   }
891   GNUNET_assert (0 == session->msgs_in_queue);
892   GNUNET_assert (0 == session->bytes_in_queue);
893   notify_session_monitor (session->plugin,
894                           session,
895                           GNUNET_TRANSPORT_SS_DONE);
896
897   if (session->receive_delay_task != NULL)
898   {
899     GNUNET_SCHEDULER_cancel (session->receive_delay_task);
900     if (NULL != session->client)
901       GNUNET_SERVER_receive_done (session->client, GNUNET_SYSERR);
902   }
903   if (NULL != session->client)
904   {
905     GNUNET_SERVER_client_disconnect (session->client);
906     GNUNET_SERVER_client_drop (session->client);
907     session->client = NULL;
908   }
909   GNUNET_HELLO_address_free (session->address);
910   GNUNET_assert(NULL == session->transmit_handle);
911   GNUNET_free(session);
912   return GNUNET_OK;
913 }
914
915
916 /**
917  * Function that is called to get the keepalive factor.
918  * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
919  * calculate the interval between keepalive packets.
920  *
921  * @param cls closure with the `struct Plugin`
922  * @return keepalive factor
923  */
924 static unsigned int
925 tcp_plugin_query_keepalive_factor (void *cls)
926 {
927   return 3;
928 }
929
930
931 /**
932  * Session was idle, so disconnect it
933  *
934  * @param cls the `struct Session` of the idle session
935  * @param tc scheduler context
936  */
937 static void
938 session_timeout (void *cls,
939                  const struct GNUNET_SCHEDULER_TaskContext *tc)
940 {
941   struct Session *s = cls;
942   struct GNUNET_TIME_Relative left;
943
944   s->timeout_task = NULL;
945   left = GNUNET_TIME_absolute_get_remaining (s->timeout);
946   if (0 != left.rel_value_us)
947   {
948     /* not actually our turn yet, but let's at least update
949        the monitor, it may think we're about to die ... */
950     notify_session_monitor (s->plugin,
951                             s,
952                             GNUNET_TRANSPORT_SS_UPDATE);
953     s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
954                                                     &session_timeout,
955                                                     s);
956     return;
957   }
958   LOG (GNUNET_ERROR_TYPE_DEBUG,
959        "Session %p was idle for %s, disconnecting\n",
960        s,
961        GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
962                                                GNUNET_YES));
963   /* call session destroy function */
964   tcp_plugin_disconnect_session (s->plugin, s);
965 }
966
967
968 /**
969  * Increment session timeout due to activity
970  *
971  * @param s session to increment timeout for
972  */
973 static void
974 reschedule_session_timeout (struct Session *s)
975 {
976   GNUNET_assert(NULL != s->timeout_task);
977   s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
978 }
979
980
981 /**
982  * Create a new session.  Also queues a welcome message.
983  *
984  * @param plugin the plugin
985  * @param address the address to create the session for
986  * @param client client to use, reference counter must have already been increased
987  * @param is_nat this a NAT session, we should wait for a client to
988  *               connect to us from an address, then assign that to
989  *               the session
990  * @return new session object
991  */
992 static struct Session *
993 create_session (struct Plugin *plugin,
994                 const struct GNUNET_HELLO_Address *address,
995                 struct GNUNET_SERVER_Client *client,
996                 int is_nat)
997 {
998   struct Session *session;
999   struct PendingMessage *pm;
1000
1001   if (GNUNET_YES != is_nat)
1002     GNUNET_assert (NULL != client);
1003   else
1004     GNUNET_assert (NULL == client);
1005
1006   LOG (GNUNET_ERROR_TYPE_DEBUG,
1007        "Creating new session for peer `%4s'\n",
1008        GNUNET_i2s (&address->peer));
1009   session = GNUNET_new (struct Session);
1010   session->last_activity = GNUNET_TIME_absolute_get ();
1011   session->plugin = plugin;
1012   session->is_nat = is_nat;
1013   session->client = client;
1014   session->address = GNUNET_HELLO_address_copy (address);
1015   session->target = address->peer;
1016   session->expecting_welcome = GNUNET_YES;
1017   session->ats_address_network_type = GNUNET_ATS_NET_UNSPECIFIED;
1018   pm = GNUNET_malloc (sizeof (struct PendingMessage) +
1019       sizeof (struct WelcomeMessage));
1020   pm->msg = (const char *) &pm[1];
1021   pm->message_size = sizeof(struct WelcomeMessage);
1022   memcpy (&pm[1],
1023           &plugin->my_welcome,
1024           sizeof(struct WelcomeMessage));
1025   pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1026   GNUNET_STATISTICS_update (plugin->env->stats,
1027                             gettext_noop ("# bytes currently in TCP buffers"), pm->message_size,
1028                             GNUNET_NO);
1029   GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
1030                                session->pending_messages_tail,
1031                                pm);
1032   session->msgs_in_queue++;
1033   session->bytes_in_queue += pm->message_size;
1034   session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1035   session->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1036                                                         &session_timeout,
1037                                                         session);
1038   notify_session_monitor (session->plugin,
1039                           session,
1040                           GNUNET_TRANSPORT_SS_INIT);
1041   if (GNUNET_YES != is_nat)
1042   {
1043     GNUNET_STATISTICS_update (plugin->env->stats,
1044                               gettext_noop ("# TCP sessions active"),
1045                               1,
1046                               GNUNET_NO);
1047     notify_session_monitor (session->plugin,
1048                             session,
1049                             GNUNET_TRANSPORT_SS_UP);
1050   }
1051   else
1052   {
1053     notify_session_monitor (session->plugin,
1054                             session,
1055                             GNUNET_TRANSPORT_SS_HANDSHAKE);
1056   }
1057   plugin->env->register_quota_notification (plugin->env->cls,
1058                                             &address->peer,
1059                                             PLUGIN_NAME,
1060                                             session);
1061   return session;
1062 }
1063
1064
1065 /**
1066  * If we have pending messages, ask the server to
1067  * transmit them (schedule the respective tasks, etc.)
1068  *
1069  * @param session for which session should we do this
1070  */
1071 static void
1072 process_pending_messages (struct Session *session);
1073
1074
1075 /**
1076  * Function called to notify a client about the socket
1077  * being ready to queue more data.  "buf" will be
1078  * NULL and "size" zero if the socket was closed for
1079  * writing in the meantime.
1080  *
1081  * @param cls closure
1082  * @param size number of bytes available in @a buf
1083  * @param buf where the callee should write the message
1084  * @return number of bytes written to @a buf
1085  */
1086 static size_t
1087 do_transmit (void *cls, size_t size, void *buf)
1088 {
1089   struct Session *session = cls;
1090   struct GNUNET_PeerIdentity pid;
1091   struct Plugin *plugin;
1092   struct PendingMessage *pos;
1093   struct PendingMessage *hd;
1094   struct PendingMessage *tl;
1095   struct GNUNET_TIME_Absolute now;
1096   char *cbuf;
1097   size_t ret;
1098
1099   session->transmit_handle = NULL;
1100   plugin = session->plugin;
1101   if (NULL == buf)
1102   {
1103     LOG (GNUNET_ERROR_TYPE_DEBUG,
1104          "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
1105          GNUNET_i2s (&session->target));
1106     /* timeout; cancel all messages that have already expired */
1107     hd = NULL;
1108     tl = NULL;
1109     ret = 0;
1110     now = GNUNET_TIME_absolute_get ();
1111     while ((NULL != (pos = session->pending_messages_head))
1112         && (pos->timeout.abs_value_us <= now.abs_value_us))
1113     {
1114       GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1115                                    session->pending_messages_tail,
1116                                    pos);
1117       GNUNET_assert (0 < session->msgs_in_queue);
1118       session->msgs_in_queue--;
1119       GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1120       session->bytes_in_queue -= pos->message_size;
1121       LOG (GNUNET_ERROR_TYPE_DEBUG,
1122            "Failed to transmit %u byte message to `%4s'.\n",
1123            pos->message_size,
1124            GNUNET_i2s (&session->target));
1125       ret += pos->message_size;
1126       GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
1127     }
1128     /* do this call before callbacks (so that if callbacks destroy
1129      * session, they have a chance to cancel actions done by this
1130      * call) */
1131     process_pending_messages (session);
1132     pid = session->target;
1133     /* no do callbacks and do not use session again since
1134      * the callbacks may abort the session */
1135     while (NULL != (pos = hd))
1136     {
1137       GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
1138       if (pos->transmit_cont != NULL)
1139         pos->transmit_cont (pos->transmit_cont_cls,
1140                             &pid,
1141                             GNUNET_SYSERR,
1142                             pos->message_size, 0);
1143       GNUNET_free (pos);
1144     }
1145     GNUNET_STATISTICS_update (plugin->env->stats,
1146                               gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
1147                               GNUNET_NO);
1148     GNUNET_STATISTICS_update (plugin->env->stats,
1149                               gettext_noop ("# bytes discarded by TCP (timeout)"),
1150                               ret,
1151                               GNUNET_NO);
1152     if (0 < ret)
1153       notify_session_monitor (session->plugin,
1154                               session,
1155                               GNUNET_TRANSPORT_SS_UPDATE);
1156     return 0;
1157   }
1158   /* copy all pending messages that would fit */
1159   ret = 0;
1160   cbuf = buf;
1161   hd = NULL;
1162   tl = NULL;
1163   while (NULL != (pos = session->pending_messages_head))
1164   {
1165     if (ret + pos->message_size > size)
1166       break;
1167     GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1168                                  session->pending_messages_tail,
1169                                  pos);
1170     GNUNET_assert (0 < session->msgs_in_queue);
1171     session->msgs_in_queue--;
1172     GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1173     session->bytes_in_queue -= pos->message_size;
1174     GNUNET_assert(size >= pos->message_size);
1175     LOG(GNUNET_ERROR_TYPE_DEBUG,
1176         "Transmitting message of type %u size %u\n",
1177         ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type),
1178         pos->message_size);
1179     /* FIXME: this memcpy can be up to 7% of our total runtime */
1180     memcpy (cbuf, pos->msg, pos->message_size);
1181     cbuf += pos->message_size;
1182     ret += pos->message_size;
1183     size -= pos->message_size;
1184     GNUNET_CONTAINER_DLL_insert_tail (hd, tl, pos);
1185   }
1186   notify_session_monitor (session->plugin,
1187                           session,
1188                           GNUNET_TRANSPORT_SS_UPDATE);
1189   /* schedule 'continuation' before callbacks so that callbacks that
1190    * cancel everything don't cause us to use a session that no longer
1191    * exists... */
1192   process_pending_messages (session);
1193   session->last_activity = GNUNET_TIME_absolute_get ();
1194   pid = session->target;
1195   /* we'll now call callbacks that may cancel the session; hence
1196    * we should not use 'session' after this point */
1197   while (NULL != (pos = hd))
1198   {
1199     GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
1200     if (pos->transmit_cont != NULL)
1201       pos->transmit_cont (pos->transmit_cont_cls,
1202                           &pid,
1203                           GNUNET_OK,
1204                           pos->message_size,
1205                           pos->message_size); /* FIXME: include TCP overhead */
1206     GNUNET_free(pos);
1207   }
1208   GNUNET_assert (NULL == hd);
1209   GNUNET_assert (NULL == tl);
1210   LOG(GNUNET_ERROR_TYPE_DEBUG,
1211       "Transmitting %u bytes\n",
1212       ret);
1213   GNUNET_STATISTICS_update (plugin->env->stats,
1214                             gettext_noop ("# bytes currently in TCP buffers"),
1215                             - (int64_t) ret,
1216                             GNUNET_NO);
1217   GNUNET_STATISTICS_update (plugin->env->stats,
1218                             gettext_noop ("# bytes transmitted via TCP"),
1219                             ret,
1220                             GNUNET_NO);
1221   return ret;
1222 }
1223
1224
1225 /**
1226  * If we have pending messages, ask the server to
1227  * transmit them (schedule the respective tasks, etc.)
1228  *
1229  * @param session for which session should we do this
1230  */
1231 static void
1232 process_pending_messages (struct Session *session)
1233 {
1234   struct PendingMessage *pm;
1235
1236   GNUNET_assert (NULL != session->client);
1237   if (NULL != session->transmit_handle)
1238     return;
1239   if (NULL == (pm = session->pending_messages_head))
1240     return;
1241
1242   session->transmit_handle = GNUNET_SERVER_notify_transmit_ready (session->client,
1243                                                                   pm->message_size,
1244                                                                   GNUNET_TIME_absolute_get_remaining (pm->timeout),
1245                                                                   &do_transmit,
1246                                                                   session);
1247 }
1248
1249
1250 /**
1251  * Function that can be used by the transport service to transmit
1252  * a message using the plugin.   Note that in the case of a
1253  * peer disconnecting, the continuation MUST be called
1254  * prior to the disconnect notification itself.  This function
1255  * will be called with this peer's HELLO message to initiate
1256  * a fresh connection to another peer.
1257  *
1258  * @param cls closure
1259  * @param session which session must be used
1260  * @param msgbuf the message to transmit
1261  * @param msgbuf_size number of bytes in 'msgbuf'
1262  * @param priority how important is the message (most plugins will
1263  *                 ignore message priority and just FIFO)
1264  * @param to how long to wait at most for the transmission (does not
1265  *                require plugins to discard the message after the timeout,
1266  *                just advisory for the desired delay; most plugins will ignore
1267  *                this as well)
1268  * @param cont continuation to call once the message has
1269  *        been transmitted (or if the transport is ready
1270  *        for the next transmission call; or if the
1271  *        peer disconnected...); can be NULL
1272  * @param cont_cls closure for @a cont
1273  * @return number of bytes used (on the physical network, with overheads);
1274  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
1275  *         and does NOT mean that the message was not transmitted (DV)
1276  */
1277 static ssize_t
1278 tcp_plugin_send (void *cls,
1279                  struct Session *session,
1280                  const char *msgbuf,
1281                  size_t msgbuf_size,
1282                  unsigned int priority,
1283                  struct GNUNET_TIME_Relative to,
1284                  GNUNET_TRANSPORT_TransmitContinuation cont,
1285                  void *cont_cls)
1286 {
1287   struct Plugin * plugin = cls;
1288   struct PendingMessage *pm;
1289
1290   /* create new message entry */
1291   pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1292   pm->msg = (const char *) &pm[1];
1293   memcpy (&pm[1], msgbuf, msgbuf_size);
1294   pm->message_size = msgbuf_size;
1295   pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1296   pm->transmit_cont = cont;
1297   pm->transmit_cont_cls = cont_cls;
1298
1299   LOG(GNUNET_ERROR_TYPE_DEBUG,
1300       "Asked to transmit %u bytes to `%s', added message to list.\n",
1301       msgbuf_size, GNUNET_i2s (&session->target));
1302
1303   if (GNUNET_YES ==
1304       GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
1305                                                     &session->target,
1306                                                     session))
1307   {
1308     GNUNET_assert (NULL != session->client);
1309     GNUNET_SERVER_client_set_timeout (session->client,
1310         GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1311     GNUNET_STATISTICS_update (plugin->env->stats,
1312         gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1313         GNUNET_NO);
1314
1315     /* append pm to pending_messages list */
1316     GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1317                                       session->pending_messages_tail,
1318                                       pm);
1319     notify_session_monitor (session->plugin,
1320                             session,
1321                             GNUNET_TRANSPORT_SS_UPDATE);
1322     session->msgs_in_queue++;
1323     session->bytes_in_queue += pm->message_size;
1324     process_pending_messages (session);
1325     return msgbuf_size;
1326   }
1327   else if (GNUNET_YES ==
1328            GNUNET_CONTAINER_multipeermap_contains_value (plugin->nat_wait_conns,
1329                                                          &session->target,
1330                                                          session))
1331   {
1332     LOG (GNUNET_ERROR_TYPE_DEBUG,
1333          "This NAT WAIT session for peer `%s' is not yet ready!\n",
1334          GNUNET_i2s (&session->target));
1335     GNUNET_STATISTICS_update (plugin->env->stats,
1336                               gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1337                               GNUNET_NO);
1338     /* append pm to pending_messages list */
1339     GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1340                                       session->pending_messages_tail,
1341                                       pm);
1342     session->msgs_in_queue++;
1343     session->bytes_in_queue += pm->message_size;
1344     notify_session_monitor (session->plugin,
1345                             session,
1346                             GNUNET_TRANSPORT_SS_HANDSHAKE);
1347     return msgbuf_size;
1348   }
1349   else
1350   {
1351     LOG(GNUNET_ERROR_TYPE_ERROR,
1352         "Invalid session %p\n",
1353         session);
1354     if (NULL != cont)
1355       cont (cont_cls,
1356             &session->target,
1357             GNUNET_SYSERR,
1358             pm->message_size,
1359             0);
1360     GNUNET_break (0);
1361     GNUNET_free (pm);
1362     return GNUNET_SYSERR; /* session does not exist here */
1363   }
1364 }
1365
1366 /**
1367  * Closure for #session_lookup_it().
1368  */
1369 struct SessionItCtx
1370 {
1371   /**
1372    * Address we are looking for.
1373    */
1374   const struct GNUNET_HELLO_Address *address;
1375
1376   /**
1377    * Where to store the session (if we found it).
1378    */
1379   struct Session *result;
1380
1381 };
1382
1383
1384 /**
1385  * Look for a session by address.
1386  *
1387  * @param cls the `struct SessionItCtx`
1388  * @param key unused
1389  * @param value a `struct Session`
1390  * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
1391  */
1392 static int
1393 session_lookup_it (void *cls,
1394                    const struct GNUNET_PeerIdentity *key,
1395                    void *value)
1396 {
1397   struct SessionItCtx * si_ctx = cls;
1398   struct Session * session = value;
1399
1400   if (0 != GNUNET_HELLO_address_cmp (si_ctx->address, session->address))
1401     return GNUNET_YES;
1402   /* Found existing session */
1403   si_ctx->result = session;
1404   return GNUNET_NO;
1405 }
1406
1407
1408 /**
1409  * Task cleaning up a NAT connection attempt after timeout
1410  *
1411  * @param cls the `struct Session`
1412  * @param tc scheduler context (unused)
1413  */
1414 static void
1415 nat_connect_timeout (void *cls,
1416                      const struct GNUNET_SCHEDULER_TaskContext *tc)
1417 {
1418   struct Session *session = cls;
1419
1420   session->nat_connection_timeout = NULL;
1421   LOG (GNUNET_ERROR_TYPE_DEBUG,
1422        "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1423        GNUNET_i2s (&session->target),
1424        tcp_plugin_address_to_string (NULL,
1425                                      session->address->address,
1426                                      session->address->address_length));
1427   tcp_plugin_disconnect_session (session->plugin,
1428                                  session);
1429 }
1430
1431
1432 /**
1433  * Function that will be called whenever the transport service wants to
1434  * notify the plugin that a session is still active and in use and
1435  * therefore the session timeout for this session has to be updated
1436  *
1437  * @param cls closure
1438  * @param peer which peer was the session for
1439  * @param session which session is being updated
1440  */
1441 static void
1442 tcp_plugin_update_session_timeout (void *cls,
1443                                    const struct GNUNET_PeerIdentity *peer,
1444                                    struct Session *session)
1445 {
1446   reschedule_session_timeout (session);
1447 }
1448
1449
1450 /**
1451  * Task to signal the server that we can continue
1452  * receiving from the TCP client now.
1453  *
1454  * @param cls the `struct Session*`
1455  * @param tc task context (unused)
1456  */
1457 static void
1458 delayed_done (void *cls,
1459               const struct GNUNET_SCHEDULER_TaskContext *tc)
1460 {
1461   struct Session *session = cls;
1462
1463   session->receive_delay_task = NULL;
1464   reschedule_session_timeout (session);
1465
1466   GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1467 }
1468
1469
1470 /**
1471  * Function that will be called whenever the transport service wants to
1472  * notify the plugin that the inbound quota changed and that the plugin
1473  * should update it's delay for the next receive value
1474  *
1475  * @param cls closure
1476  * @param peer which peer was the session for
1477  * @param session which session is being updated
1478  * @param delay new delay to use for receiving
1479  */
1480 static void
1481 tcp_plugin_update_inbound_delay (void *cls,
1482                                  const struct GNUNET_PeerIdentity *peer,
1483                                  struct Session *session,
1484                                  struct GNUNET_TIME_Relative delay)
1485 {
1486   if (NULL == session->receive_delay_task)
1487     return;
1488   LOG (GNUNET_ERROR_TYPE_DEBUG,
1489        "New inbound delay %s\n",
1490        GNUNET_STRINGS_relative_time_to_string (delay,
1491                                                GNUNET_NO));
1492   session->receive_delay = GNUNET_TIME_relative_to_absolute (delay);
1493   GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1494   session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
1495                                                               &delayed_done,
1496                                                               session);
1497 }
1498
1499
1500 /**
1501  * Create a new session to transmit data to the target
1502  * This session will used to send data to this peer and the plugin will
1503  * notify us by calling the env->session_end function
1504  *
1505  * @param cls closure
1506  * @param address the address to use
1507  * @return the session if the address is valid, NULL otherwise
1508  */
1509 static struct Session *
1510 tcp_plugin_get_session (void *cls,
1511                         const struct GNUNET_HELLO_Address *address)
1512 {
1513   struct Plugin *plugin = cls;
1514   struct Session *session = NULL;
1515   int af;
1516   const void *sb;
1517   size_t sbs;
1518   struct GNUNET_CONNECTION_Handle *sa;
1519   struct sockaddr_in a4;
1520   struct sockaddr_in6 a6;
1521   const struct IPv4TcpAddress *t4;
1522   const struct IPv6TcpAddress *t6;
1523   unsigned int options;
1524   enum GNUNET_ATS_Network_Type net_type;
1525   unsigned int is_natd = GNUNET_NO;
1526   size_t addrlen;
1527 #ifdef TCP_STEALTH
1528   struct GNUNET_NETWORK_Handle *s;
1529 #endif
1530
1531   addrlen = address->address_length;
1532   LOG (GNUNET_ERROR_TYPE_DEBUG,
1533        "Trying to get session for `%s' address of peer `%s'\n",
1534        tcp_plugin_address_to_string(NULL, address->address, address->address_length),
1535        GNUNET_i2s (&address->peer));
1536
1537   if (GNUNET_HELLO_address_check_option (address,
1538                                          GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1539   {
1540     GNUNET_break (0);
1541     return NULL;
1542   }
1543
1544   /* look for existing session */
1545   if (GNUNET_YES ==
1546       GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
1547                                               &address->peer))
1548   {
1549     struct SessionItCtx si_ctx;
1550
1551     si_ctx.address = address;
1552     si_ctx.result = NULL;
1553
1554     GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1555                                                 &address->peer,
1556                                                 &session_lookup_it, &si_ctx);
1557     if (NULL != si_ctx.result)
1558     {
1559       session = si_ctx.result;
1560       LOG (GNUNET_ERROR_TYPE_DEBUG,
1561            "Found existing session for `%s' address `%s' session %p\n",
1562            GNUNET_i2s (&address->peer),
1563            tcp_plugin_address_to_string (NULL,
1564                                          address->address,
1565                                          address->address_length),
1566            session);
1567       return session;
1568     }
1569     LOG (GNUNET_ERROR_TYPE_DEBUG,
1570          "Existing sessions did not match address `%s' or peer `%s'\n",
1571          tcp_plugin_address_to_string (NULL,
1572                                        address->address,
1573                                        address->address_length),
1574         GNUNET_i2s (&address->peer));
1575   }
1576
1577   if (addrlen == sizeof(struct IPv6TcpAddress))
1578   {
1579     GNUNET_assert(NULL != address->address); /* make static analysis happy */
1580     t6 = address->address;
1581     options = t6->options;
1582     af = AF_INET6;
1583     memset (&a6, 0, sizeof(a6));
1584 #if HAVE_SOCKADDR_IN_SIN_LEN
1585     a6.sin6_len = sizeof (a6);
1586 #endif
1587     a6.sin6_family = AF_INET6;
1588     a6.sin6_port = t6->t6_port;
1589     if (t6->t6_port == 0)
1590       is_natd = GNUNET_YES;
1591     memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1592     sb = &a6;
1593     sbs = sizeof(a6);
1594   }
1595   else if (addrlen == sizeof(struct IPv4TcpAddress))
1596   {
1597     GNUNET_assert(NULL != address->address); /* make static analysis happy */
1598     t4 = address->address;
1599     options = t4->options;
1600     af = AF_INET;
1601     memset (&a4, 0, sizeof(a4));
1602 #if HAVE_SOCKADDR_IN_SIN_LEN
1603     a4.sin_len = sizeof (a4);
1604 #endif
1605     a4.sin_family = AF_INET;
1606     a4.sin_port = t4->t4_port;
1607     if (t4->t4_port == 0)
1608       is_natd = GNUNET_YES;
1609     a4.sin_addr.s_addr = t4->ipv4_addr;
1610     sb = &a4;
1611     sbs = sizeof(a4);
1612   }
1613   else
1614   {
1615     GNUNET_STATISTICS_update (plugin->env->stats, gettext_noop
1616     ("# requests to create session with invalid address"), 1, GNUNET_NO);
1617     return NULL;
1618   }
1619
1620   net_type = plugin->env->get_address_type (plugin->env->cls, sb, sbs);
1621
1622
1623   if ((is_natd == GNUNET_YES) && (addrlen == sizeof(struct IPv6TcpAddress)))
1624   {
1625     /* NAT client only works with IPv4 addresses */
1626     return NULL;
1627   }
1628
1629   if (plugin->cur_connections >= plugin->max_connections)
1630   {
1631     /* saturated */
1632     return NULL;
1633   }
1634
1635   if ((is_natd == GNUNET_YES)
1636       && (GNUNET_YES
1637           == GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1638               &address->peer)))
1639   {
1640     /* Only do one NAT punch attempt per peer identity */
1641     return NULL;
1642   }
1643
1644   if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1645       (GNUNET_NO ==
1646        GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1647                                                &address->peer)))
1648   {
1649     LOG (GNUNET_ERROR_TYPE_DEBUG,
1650          "Found valid IPv4 NAT address (creating session)!\n");
1651     session = create_session (plugin,
1652                               address,
1653                               NULL,
1654                               GNUNET_YES);
1655     session->ats_address_network_type = net_type;
1656     GNUNET_break (session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1657     session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1658                                                                     &nat_connect_timeout,
1659                                                                     session);
1660     GNUNET_assert(GNUNET_OK ==
1661                   GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
1662                                                      &session->target,
1663                                                      session,
1664                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1665
1666     LOG (GNUNET_ERROR_TYPE_DEBUG,
1667          "Created NAT WAIT connection to `%4s' at `%s'\n",
1668          GNUNET_i2s (&session->target),
1669          GNUNET_a2s (sb, sbs));
1670     if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1671     {
1672       return session;
1673     }
1674     else
1675     {
1676       LOG(GNUNET_ERROR_TYPE_DEBUG,
1677           "Running NAT client for `%4s' at `%s' failed\n",
1678           GNUNET_i2s (&session->target),
1679           GNUNET_a2s (sb, sbs));
1680       tcp_plugin_disconnect_session (plugin,
1681                                      session);
1682       return NULL;
1683     }
1684   }
1685
1686   /* create new outbound session */
1687   GNUNET_assert(plugin->cur_connections <= plugin->max_connections);
1688
1689   if (0 != (options & TCP_OPTIONS_TCP_STEALTH))
1690   {
1691 #ifdef TCP_STEALTH
1692     s = GNUNET_NETWORK_socket_create (af, SOCK_STREAM, 0);
1693     if (NULL == s)
1694     {
1695       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1696                            "socket");
1697       sa = NULL;
1698     }
1699     else
1700     {
1701       if ( (GNUNET_OK !=
1702             GNUNET_NETWORK_socket_setsockopt (s,
1703                                               IPPROTO_TCP,
1704                                               TCP_STEALTH,
1705                                               &session->target,
1706                                               sizeof (struct GNUNET_PeerIdentity))) ||
1707            (GNUNET_OK !=
1708             GNUNET_NETWORK_socket_setsockopt (s,
1709                                               IPPROTO_TCP,
1710                                               TCP_STEALTH_INTEGRITY,
1711                                               &plugin->my_welcome,
1712                                               sizeof (struct WelcomeMessage))) )
1713       {
1714         /* TCP STEALTH not supported by kernel */
1715         GNUNET_break (GNUNET_OK ==
1716                       GNUNET_NETWORK_socket_close (s));
1717         sa = NULL;
1718       }
1719       else
1720       {
1721         sa = GNUNET_CONNECTION_connect_socket (s, sb, sbs);
1722       }
1723     }
1724 #else
1725     sa = NULL;
1726 #endif
1727   }
1728   else
1729   {
1730     sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1731   }
1732   if (NULL == sa)
1733   {
1734     LOG (GNUNET_ERROR_TYPE_DEBUG,
1735          "Failed to create connection to `%4s' at `%s'\n",
1736          GNUNET_i2s (&address->peer),
1737          GNUNET_a2s (sb, sbs));
1738     return NULL;
1739   }
1740   plugin->cur_connections++;
1741   if (plugin->cur_connections == plugin->max_connections)
1742     GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1743
1744   LOG (GNUNET_ERROR_TYPE_DEBUG,
1745        "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1746        GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1747
1748   session = create_session (plugin,
1749                             address,
1750                             GNUNET_SERVER_connect_socket (plugin->server, sa),
1751                             GNUNET_NO);
1752   session->ats_address_network_type = net_type;
1753   GNUNET_break (session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1754   GNUNET_SERVER_client_set_user_context(session->client, session);
1755   GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
1756                                      &session->target,
1757                                      session,
1758                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1759   LOG (GNUNET_ERROR_TYPE_DEBUG,
1760        "Creating new session for `%s' address `%s' session %p\n",
1761        GNUNET_i2s (&address->peer),
1762        tcp_plugin_address_to_string (NULL,
1763                                      address->address,
1764                                      address->address_length),
1765        session);
1766   /* Send TCP Welcome */
1767   process_pending_messages (session);
1768
1769   return session;
1770 }
1771
1772
1773 static int
1774 session_disconnect_it (void *cls,
1775                        const struct GNUNET_PeerIdentity *key,
1776                        void *value)
1777 {
1778   struct Plugin *plugin = cls;
1779   struct Session *session = value;
1780
1781   GNUNET_STATISTICS_update (session->plugin->env->stats,
1782                             gettext_noop ("# transport-service disconnect requests for TCP"),
1783                             1,
1784                             GNUNET_NO);
1785   tcp_plugin_disconnect_session (plugin,
1786                                  session);
1787   return GNUNET_YES;
1788 }
1789
1790
1791 /**
1792  * Function that can be called to force a disconnect from the
1793  * specified neighbour.  This should also cancel all previously
1794  * scheduled transmissions.  Obviously the transmission may have been
1795  * partially completed already, which is OK.  The plugin is supposed
1796  * to close the connection (if applicable) and no longer call the
1797  * transmit continuation(s).
1798  *
1799  * Finally, plugin MUST NOT call the services's receive function to
1800  * notify the service that the connection to the specified target was
1801  * closed after a getting this call.
1802  *
1803  * @param cls closure
1804  * @param target peer for which the last transmission is
1805  *        to be cancelled
1806  */
1807 static void
1808 tcp_plugin_disconnect (void *cls,
1809                        const struct GNUNET_PeerIdentity *target)
1810 {
1811   struct Plugin *plugin = cls;
1812
1813   LOG(GNUNET_ERROR_TYPE_DEBUG,
1814       "Disconnecting peer `%4s'\n",
1815       GNUNET_i2s (target));
1816   GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1817                                               target,
1818                                               &session_disconnect_it,
1819                                               plugin);
1820   GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns,
1821                                               target,
1822                                               &session_disconnect_it,
1823                                               plugin);
1824 }
1825
1826
1827 /**
1828  * Running pretty printers: head
1829  */
1830 static struct PrettyPrinterContext *ppc_dll_head;
1831
1832 /**
1833  * Running pretty printers: tail
1834  */
1835 static struct PrettyPrinterContext *ppc_dll_tail;
1836
1837 /**
1838  * Context for address to string conversion, closure
1839  * for #append_port().
1840  */
1841 struct PrettyPrinterContext
1842 {
1843   /**
1844    * DLL
1845    */
1846   struct PrettyPrinterContext *next;
1847
1848   /**
1849    * DLL
1850    */
1851   struct PrettyPrinterContext *prev;
1852
1853   /**
1854    * Timeout task
1855    */
1856   struct GNUNET_SCHEDULER_Task * timeout_task;
1857
1858   /**
1859    * Resolver handle
1860    */
1861   struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
1862
1863   /**
1864    * Function to call with the result.
1865    */
1866   GNUNET_TRANSPORT_AddressStringCallback asc;
1867
1868   /**
1869    * Clsoure for @e asc.
1870    */
1871   void *asc_cls;
1872
1873   /**
1874    * Port to add after the IP address.
1875    */
1876   uint16_t port;
1877
1878   /**
1879    * IPv6 address
1880    */
1881   int ipv6;
1882
1883   /**
1884    * Options
1885    */
1886   uint32_t options;
1887 };
1888
1889
1890 /**
1891  * Append our port and forward the result.
1892  *
1893  * @param cls the `struct PrettyPrinterContext *`
1894  * @param hostname hostname part of the address
1895  */
1896 static void
1897 append_port (void *cls,
1898              const char *hostname)
1899 {
1900   struct PrettyPrinterContext *ppc = cls;
1901   char *ret;
1902
1903   if (NULL == hostname)
1904   {
1905     /* Final call, done */
1906     ppc->resolver_handle = NULL;
1907     GNUNET_CONTAINER_DLL_remove (ppc_dll_head,
1908                                  ppc_dll_tail,
1909                                  ppc);
1910     ppc->asc (ppc->asc_cls,
1911               NULL,
1912               GNUNET_OK);
1913     GNUNET_free (ppc);
1914     return;
1915   }
1916   if (GNUNET_YES == ppc->ipv6)
1917     GNUNET_asprintf (&ret,
1918                      "%s.%u.[%s]:%d",
1919                      PLUGIN_NAME,
1920                      ppc->options,
1921                      hostname,
1922                      ppc->port);
1923   else
1924     GNUNET_asprintf (&ret,
1925                      "%s.%u.%s:%d",
1926                      PLUGIN_NAME,
1927                      ppc->options,
1928                      hostname,
1929                      ppc->port);
1930   ppc->asc (ppc->asc_cls,
1931             ret,
1932             GNUNET_OK);
1933   GNUNET_free (ret);
1934 }
1935
1936
1937 /**
1938  * Convert the transports address to a nice, human-readable
1939  * format.
1940  *
1941  * @param cls closure
1942  * @param type name of the transport that generated the address
1943  * @param addr one of the addresses of the host, NULL for the last address
1944  *        the specific address format depends on the transport
1945  * @param addrlen length of the @a addr
1946  * @param numeric should (IP) addresses be displayed in numeric form?
1947  * @param timeout after how long should we give up?
1948  * @param asc function to call on each string
1949  * @param asc_cls closure for @a asc
1950  */
1951 static void
1952 tcp_plugin_address_pretty_printer (void *cls,
1953                                    const char *type,
1954                                    const void *addr,
1955                                    size_t addrlen,
1956                                    int numeric,
1957                                    struct GNUNET_TIME_Relative timeout,
1958                                    GNUNET_TRANSPORT_AddressStringCallback asc,
1959                                    void *asc_cls)
1960 {
1961   struct PrettyPrinterContext *ppc;
1962   const void *sb;
1963   size_t sbs;
1964   struct sockaddr_in a4;
1965   struct sockaddr_in6 a6;
1966   const struct IPv4TcpAddress *t4;
1967   const struct IPv6TcpAddress *t6;
1968   uint16_t port;
1969   uint32_t options;
1970
1971   if (sizeof(struct IPv6TcpAddress) == addrlen)
1972   {
1973     t6 = addr;
1974     memset (&a6, 0, sizeof(a6));
1975     a6.sin6_family = AF_INET6;
1976     a6.sin6_port = t6->t6_port;
1977     memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1978     port = ntohs (t6->t6_port);
1979     options = ntohl (t6->options);
1980     sb = &a6;
1981     sbs = sizeof(a6);
1982   }
1983   else if (sizeof(struct IPv4TcpAddress) == addrlen)
1984   {
1985     t4 = addr;
1986     memset (&a4, 0, sizeof(a4));
1987     a4.sin_family = AF_INET;
1988     a4.sin_port = t4->t4_port;
1989     a4.sin_addr.s_addr = t4->ipv4_addr;
1990     port = ntohs (t4->t4_port);
1991     options = ntohl (t4->options);
1992     sb = &a4;
1993     sbs = sizeof(a4);
1994   }
1995   else
1996   {
1997     /* invalid address */
1998     asc (asc_cls, NULL, GNUNET_SYSERR);
1999     asc (asc_cls, NULL, GNUNET_OK);
2000     return;
2001   }
2002   ppc = GNUNET_new (struct PrettyPrinterContext);
2003   if (addrlen == sizeof(struct IPv6TcpAddress))
2004     ppc->ipv6 = GNUNET_YES;
2005   else
2006     ppc->ipv6 = GNUNET_NO;
2007   ppc->asc = asc;
2008   ppc->asc_cls = asc_cls;
2009   ppc->port = port;
2010   ppc->options = options;
2011   ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb,
2012                                                        sbs,
2013                                                        ! numeric,
2014                                                        timeout,
2015                                                        &append_port, ppc);
2016   if (NULL == ppc->resolver_handle)
2017   {
2018     GNUNET_break (0);
2019     GNUNET_free (ppc);
2020     return;
2021   }
2022   GNUNET_CONTAINER_DLL_insert (ppc_dll_head,
2023                                ppc_dll_tail,
2024                                ppc);
2025 }
2026
2027
2028 /**
2029  * Check if the given port is plausible (must be either our listen
2030  * port or our advertised port), or any port if we are behind NAT
2031  * and do not have a port open.  If it is neither, we return
2032  * #GNUNET_SYSERR.
2033  *
2034  * @param plugin global variables
2035  * @param in_port port number to check
2036  * @return #GNUNET_OK if port is either open_port or adv_port
2037  */
2038 static int
2039 check_port (struct Plugin *plugin, uint16_t in_port)
2040 {
2041   if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
2042     return GNUNET_OK;
2043   return GNUNET_SYSERR;
2044 }
2045
2046 /**
2047  * Function that will be called to check if a binary address for this
2048  * plugin is well-formed and corresponds to an address for THIS peer
2049  * (as per our configuration).  Naturally, if absolutely necessary,
2050  * plugins can be a bit conservative in their answer, but in general
2051  * plugins should make sure that the address does not redirect
2052  * traffic to a 3rd party that might try to man-in-the-middle our
2053  * traffic.
2054  *
2055  * @param cls closure, our `struct Plugin *`
2056  * @param addr pointer to the address
2057  * @param addrlen length of addr
2058  * @return #GNUNET_OK if this is a plausible address for this peer
2059  *         and transport, #GNUNET_SYSERR if not
2060  */
2061 static int
2062 tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
2063 {
2064   struct Plugin *plugin = cls;
2065   struct IPv4TcpAddress *v4;
2066   struct IPv6TcpAddress *v6;
2067
2068   if ((addrlen != sizeof(struct IPv4TcpAddress))
2069       && (addrlen != sizeof(struct IPv6TcpAddress)))
2070   {
2071     GNUNET_break_op(0);
2072     return GNUNET_SYSERR;
2073   }
2074
2075   if (addrlen == sizeof(struct IPv4TcpAddress))
2076   {
2077     v4 = (struct IPv4TcpAddress *) addr;
2078     if (0 != memcmp (&v4->options,
2079                      &plugin->myoptions,
2080                      sizeof(uint32_t)))
2081     {
2082       GNUNET_break(0);
2083       return GNUNET_SYSERR;
2084     }
2085     if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
2086       return GNUNET_SYSERR;
2087     if (GNUNET_OK
2088         != GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
2089             sizeof(struct in_addr)))
2090       return GNUNET_SYSERR;
2091   }
2092   else
2093   {
2094     v6 = (struct IPv6TcpAddress *) addr;
2095     if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2096     {
2097       GNUNET_break_op(0);
2098       return GNUNET_SYSERR;
2099     }
2100     if (0 != memcmp (&v6->options,
2101                      &plugin->myoptions,
2102                      sizeof (uint32_t)))
2103     {
2104       GNUNET_break(0);
2105       return GNUNET_SYSERR;
2106     }
2107     if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
2108       return GNUNET_SYSERR;
2109     if (GNUNET_OK
2110         != GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
2111             sizeof(struct in6_addr)))
2112       return GNUNET_SYSERR;
2113   }
2114   return GNUNET_OK;
2115 }
2116
2117 /**
2118  * We've received a nat probe from this peer via TCP.  Finish
2119  * creating the client session and resume sending of queued
2120  * messages.
2121  *
2122  * @param cls closure
2123  * @param client identification of the client
2124  * @param message the actual message
2125  */
2126 static void
2127 handle_tcp_nat_probe (void *cls,
2128                       struct GNUNET_SERVER_Client *client,
2129                       const struct GNUNET_MessageHeader *message)
2130 {
2131   struct Plugin *plugin = cls;
2132   struct Session *session;
2133   const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
2134   size_t alen;
2135   void *vaddr;
2136   struct IPv4TcpAddress *t4;
2137   struct IPv6TcpAddress *t6;
2138   const struct sockaddr_in *s4;
2139   const struct sockaddr_in6 *s6;
2140
2141   LOG(GNUNET_ERROR_TYPE_DEBUG, "Received NAT probe\n");
2142   /* We have received a TCP NAT probe, meaning we (hopefully) initiated
2143    * a connection to this peer by running gnunet-nat-client.  This peer
2144    * received the punch message and now wants us to use the new connection
2145    * as the default for that peer.  Do so and then send a WELCOME message
2146    * so we can really be connected!
2147    */
2148   if (ntohs (message->size) != sizeof(struct TCP_NAT_ProbeMessage))
2149   {
2150     GNUNET_break_op(0);
2151     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2152     return;
2153   }
2154
2155   tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
2156   if (0 == memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
2157           sizeof(struct GNUNET_PeerIdentity)))
2158   {
2159     /* refuse connections from ourselves */
2160     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2161     return;
2162   }
2163
2164   session = GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
2165       &tcp_nat_probe->clientIdentity);
2166   if (session == NULL)
2167   {
2168     LOG (GNUNET_ERROR_TYPE_DEBUG,
2169          "Did NOT find session for NAT probe!\n");
2170     GNUNET_SERVER_receive_done (client, GNUNET_OK);
2171     return;
2172   }
2173   LOG (GNUNET_ERROR_TYPE_DEBUG,
2174        "Found session for NAT probe!\n");
2175
2176   if (session->nat_connection_timeout != NULL)
2177   {
2178     GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
2179     session->nat_connection_timeout = NULL;
2180   }
2181
2182   if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2183   {
2184     GNUNET_break(0);
2185     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2186     tcp_plugin_disconnect_session (plugin,
2187                                    session);
2188     return;
2189   }
2190   GNUNET_assert(
2191       GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns, &tcp_nat_probe->clientIdentity, session) == GNUNET_YES);
2192   GNUNET_SERVER_client_set_user_context(client, session);
2193   GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
2194       session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2195   session->last_activity = GNUNET_TIME_absolute_get ();
2196   LOG(GNUNET_ERROR_TYPE_DEBUG, "Found address `%s' for incoming connection\n",
2197       GNUNET_a2s (vaddr, alen));
2198   switch (((const struct sockaddr *) vaddr)->sa_family)
2199   {
2200   case AF_INET:
2201     s4 = vaddr;
2202     t4 = GNUNET_new (struct IPv4TcpAddress);
2203     t4->options = htonl (TCP_OPTIONS_NONE);
2204     t4->t4_port = s4->sin_port;
2205     t4->ipv4_addr = s4->sin_addr.s_addr;
2206     session->address = GNUNET_HELLO_address_allocate (&tcp_nat_probe->clientIdentity,
2207                                                       PLUGIN_NAME,
2208                                                       &t4,
2209                                                       sizeof(struct IPv4TcpAddress),
2210                                                       GNUNET_HELLO_ADDRESS_INFO_NONE);
2211     break;
2212   case AF_INET6:
2213     s6 = vaddr;
2214     t6 = GNUNET_new (struct IPv6TcpAddress);
2215     t6->options = htonl (TCP_OPTIONS_NONE);
2216     t6->t6_port = s6->sin6_port;
2217     memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2218     session->address = GNUNET_HELLO_address_allocate (&tcp_nat_probe->clientIdentity,
2219                                                       PLUGIN_NAME,
2220                                                       &t6,
2221                                                       sizeof(struct IPv6TcpAddress),
2222                                                       GNUNET_HELLO_ADDRESS_INFO_NONE);
2223     break;
2224   default:
2225     GNUNET_break_op(0);
2226     LOG(GNUNET_ERROR_TYPE_DEBUG,
2227         "Bad address for incoming connection!\n");
2228     GNUNET_free(vaddr);
2229     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2230     tcp_plugin_disconnect_session (plugin,
2231                                    session);
2232     return;
2233   }
2234   GNUNET_free(vaddr);
2235   GNUNET_break(NULL == session->client);
2236   GNUNET_SERVER_client_keep (client);
2237   session->client = client;
2238   GNUNET_STATISTICS_update (plugin->env->stats,
2239       gettext_noop ("# TCP sessions active"), 1, GNUNET_NO);
2240   process_pending_messages (session);
2241   GNUNET_SERVER_receive_done (client, GNUNET_OK);
2242 }
2243
2244 /**
2245  * We've received a welcome from this peer via TCP.  Possibly create a
2246  * fresh client record and send back our welcome.
2247  *
2248  * @param cls closure
2249  * @param client identification of the client
2250  * @param message the actual message
2251  */
2252 static void
2253 handle_tcp_welcome (void *cls,
2254                     struct GNUNET_SERVER_Client *client,
2255                     const struct GNUNET_MessageHeader *message)
2256 {
2257   struct Plugin *plugin = cls;
2258   const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2259   struct GNUNET_HELLO_Address *address;
2260   struct Session *session;
2261   size_t alen;
2262   void *vaddr;
2263   struct IPv4TcpAddress t4;
2264   struct IPv6TcpAddress t6;
2265   const struct sockaddr_in *s4;
2266   const struct sockaddr_in6 *s6;
2267   struct GNUNET_ATS_Information ats;
2268
2269
2270   if (0 == memcmp (&wm->clientIdentity,
2271                    plugin->env->my_identity,
2272                    sizeof(struct GNUNET_PeerIdentity)))
2273   {
2274     /* refuse connections from ourselves */
2275     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2276     if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2277     {
2278       LOG(GNUNET_ERROR_TYPE_INFO,
2279           "Received %s message from my own identity `%4s' on address `%s'\n",
2280           "WELCOME", GNUNET_i2s (&wm->clientIdentity),
2281           GNUNET_a2s (vaddr, alen));
2282       GNUNET_free(vaddr);
2283     }
2284     return;
2285   }
2286
2287   LOG(GNUNET_ERROR_TYPE_DEBUG,
2288       "Received %s message from `%4s' %p\n",
2289       "WELCOME",
2290       GNUNET_i2s (&wm->clientIdentity), client);
2291   GNUNET_STATISTICS_update (plugin->env->stats,
2292                             gettext_noop ("# TCP WELCOME messages received"),
2293                             1,
2294                             GNUNET_NO);
2295   session = lookup_session_by_client (plugin, client);
2296   if (NULL != session)
2297   {
2298     if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2299     {
2300       LOG (GNUNET_ERROR_TYPE_DEBUG,
2301            "Found existing session %p for peer `%s'\n",
2302            session,
2303            GNUNET_a2s (vaddr, alen));
2304       GNUNET_free (vaddr);
2305     }
2306   }
2307   else
2308   {
2309     GNUNET_SERVER_client_keep (client);
2310     if (NULL != plugin->service) /* Otherwise value is incremented in tcp_access_check */
2311       plugin->cur_connections++;
2312     if (plugin->cur_connections == plugin->max_connections)
2313       GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2314
2315     if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2316     {
2317       if (alen == sizeof(struct sockaddr_in))
2318       {
2319         s4 = vaddr;
2320         memset (&t4, '\0', sizeof (t4));
2321         t4.options = htonl (TCP_OPTIONS_NONE);
2322         t4.t4_port = s4->sin_port;
2323         t4.ipv4_addr = s4->sin_addr.s_addr;
2324         address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2325                                                  PLUGIN_NAME,
2326                                                  &t4,
2327                                                  sizeof(t4),
2328                                                  GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2329       }
2330       else if (alen == sizeof(struct sockaddr_in6))
2331       {
2332         s6 = vaddr;
2333         memset (&t6, '\0', sizeof (t6));
2334         t6.options = htonl (TCP_OPTIONS_NONE);
2335         t6.t6_port = s6->sin6_port;
2336         memcpy (&t6.ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2337         address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2338             PLUGIN_NAME, &t6, sizeof (t6),
2339             GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2340       }
2341       else
2342       {
2343         GNUNET_break (0);
2344         GNUNET_free_non_null (vaddr);
2345         return;
2346       }
2347       session = create_session (plugin, address, client, GNUNET_NO);
2348       GNUNET_HELLO_address_free (address);
2349       session->ats_address_network_type = plugin->env->get_address_type (plugin->env->cls, vaddr, alen);
2350       ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2351       ats.value = htonl (session->ats_address_network_type);
2352       LOG (GNUNET_ERROR_TYPE_DEBUG,
2353            "Creating new%s session %p for peer `%s' client %p \n",
2354            GNUNET_HELLO_address_check_option (session->address,
2355                                               GNUNET_HELLO_ADDRESS_INFO_INBOUND)
2356            ? " inbound" : "",
2357            session,
2358            tcp_plugin_address_to_string (NULL,
2359                                          (void *) session->address->address,
2360                                          session->address->address_length),
2361            client);
2362       GNUNET_free (vaddr);
2363       GNUNET_SERVER_client_set_user_context (session->client, session);
2364       GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2365                                          &session->target,
2366                                          session,
2367                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2368       /* Notify transport and ATS about new session */
2369       plugin->env->session_start (plugin->env->cls,
2370                                   session->address,
2371                                   session,
2372                                   &ats, 1);
2373       notify_session_monitor (plugin,
2374                               session,
2375                               GNUNET_TRANSPORT_SS_INIT);
2376     }
2377     else
2378     {
2379       LOG(GNUNET_ERROR_TYPE_DEBUG,
2380           "Did not obtain TCP socket address for incoming connection\n");
2381       GNUNET_break(0);
2382       return;
2383     }
2384   }
2385
2386   if (session->expecting_welcome != GNUNET_YES)
2387   {
2388     GNUNET_break_op(0);
2389     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2390     GNUNET_break(0);
2391     return;
2392   }
2393   session->last_activity = GNUNET_TIME_absolute_get ();
2394   session->expecting_welcome = GNUNET_NO;
2395
2396   process_pending_messages (session);
2397   GNUNET_SERVER_client_set_timeout (client,
2398       GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2399   GNUNET_SERVER_receive_done (client, GNUNET_OK);
2400 }
2401
2402
2403 /**
2404  * We've received data for this peer via TCP.  Unbox,
2405  * compute latency and forward.
2406  *
2407  * @param cls closure
2408  * @param client identification of the client
2409  * @param message the actual message
2410  */
2411 static void
2412 handle_tcp_data (void *cls,
2413                  struct GNUNET_SERVER_Client *client,
2414                  const struct GNUNET_MessageHeader *message)
2415 {
2416   struct Plugin *plugin = cls;
2417   struct Session *session;
2418   struct GNUNET_TIME_Relative delay;
2419   uint16_t type;
2420
2421   type = ntohs (message->type);
2422   if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type)
2423       || (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type))
2424   {
2425     /* We don't want to propagate WELCOME and NAT Probe messages up! */
2426     GNUNET_SERVER_receive_done (client, GNUNET_OK);
2427     return;
2428   }
2429   session = lookup_session_by_client (plugin, client);
2430   if (NULL == session)
2431   {
2432     /* No inbound session found */
2433     void *vaddr;
2434     size_t alen;
2435
2436     GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2437     LOG(GNUNET_ERROR_TYPE_ERROR,
2438         "Received unexpected %u bytes of type %u from `%s'\n",
2439         (unsigned int) ntohs (message->size),
2440         (unsigned int) ntohs (message->type), GNUNET_a2s (vaddr, alen));
2441     GNUNET_break_op(0);
2442     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2443     GNUNET_free_non_null(vaddr);
2444     return;
2445   }
2446   else if (GNUNET_YES == session->expecting_welcome)
2447   {
2448     /* Session is expecting WELCOME message */
2449     void *vaddr;
2450     size_t alen;
2451
2452     GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2453     LOG(GNUNET_ERROR_TYPE_ERROR,
2454         "Received unexpected %u bytes of type %u from `%s'\n",
2455         (unsigned int) ntohs (message->size),
2456         (unsigned int) ntohs (message->type), GNUNET_a2s (vaddr, alen));
2457     GNUNET_break_op(0);
2458     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2459     GNUNET_free_non_null(vaddr);
2460     return;
2461   }
2462
2463   session->last_activity = GNUNET_TIME_absolute_get ();
2464   LOG(GNUNET_ERROR_TYPE_DEBUG,
2465       "Passing %u bytes of type %u from `%4s' to transport service.\n",
2466       (unsigned int) ntohs (message->size),
2467       (unsigned int) ntohs (message->type), GNUNET_i2s (&session->target));
2468
2469   GNUNET_STATISTICS_update (plugin->env->stats,
2470       gettext_noop ("# bytes received via TCP"), ntohs (message->size),
2471       GNUNET_NO);
2472   struct GNUNET_ATS_Information distance;
2473
2474   distance.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2475   distance.value = htonl ((uint32_t) session->ats_address_network_type);
2476   GNUNET_break(session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
2477
2478   GNUNET_assert(GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2479                                                               &session->target,
2480                                                               session));
2481
2482   delay = plugin->env->receive (plugin->env->cls,
2483                                 session->address,
2484                                 session,
2485                                 message);
2486   plugin->env->update_address_metrics (plugin->env->cls,
2487                                        session->address,
2488                                        session,
2489                                        &distance, 1);
2490   reschedule_session_timeout (session);
2491   if (0 == delay.rel_value_us)
2492   {
2493     GNUNET_SERVER_receive_done (client, GNUNET_OK);
2494   }
2495   else
2496   {
2497     LOG(GNUNET_ERROR_TYPE_DEBUG,
2498         "Throttling receiving from `%s' for %s\n",
2499         GNUNET_i2s (&session->target),
2500         GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
2501     GNUNET_SERVER_disable_receive_done_warning (client);
2502     session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
2503         &delayed_done, session);
2504   }
2505 }
2506
2507
2508 /**
2509  * Functions with this signature are called whenever a peer
2510  * is disconnected on the network level.
2511  *
2512  * @param cls closure
2513  * @param client identification of the client
2514  */
2515 static void
2516 disconnect_notify (void *cls,
2517                    struct GNUNET_SERVER_Client *client)
2518 {
2519   struct Plugin *plugin = cls;
2520   struct Session *session;
2521
2522   if (NULL == client)
2523     return;
2524   session = lookup_session_by_client (plugin, client);
2525   if (NULL == session)
2526     return; /* unknown, nothing to do */
2527   LOG (GNUNET_ERROR_TYPE_DEBUG,
2528        "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2529        GNUNET_i2s (&session->target),
2530        tcp_plugin_address_to_string (session->plugin, session->address->address,
2531                               session->address->address_length));
2532
2533   if (plugin->cur_connections == plugin->max_connections)
2534     GNUNET_SERVER_resume (plugin->server); /* Resume server  */
2535
2536   if (plugin->cur_connections < 1)
2537     GNUNET_break(0);
2538   else
2539     plugin->cur_connections--;
2540
2541   GNUNET_STATISTICS_update (session->plugin->env->stats, gettext_noop
2542                             ("# network-level TCP disconnect events"),
2543                             1,
2544                             GNUNET_NO);
2545   tcp_plugin_disconnect_session (plugin, session);
2546 }
2547
2548
2549 /**
2550  * We can now send a probe message, copy into buffer to really send.
2551  *
2552  * @param cls closure, a struct TCPProbeContext
2553  * @param size max size to copy
2554  * @param buf buffer to copy message to
2555  * @return number of bytes copied into @a buf
2556  */
2557 static size_t
2558 notify_send_probe (void *cls,
2559                    size_t size,
2560                    void *buf)
2561 {
2562   struct TCPProbeContext *tcp_probe_ctx = cls;
2563   struct Plugin *plugin = tcp_probe_ctx->plugin;
2564   size_t ret;
2565
2566   tcp_probe_ctx->transmit_handle = NULL;
2567   GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2568                                plugin->probe_tail,
2569                                tcp_probe_ctx);
2570   if (buf == NULL)
2571   {
2572     GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2573     GNUNET_free(tcp_probe_ctx);
2574     return 0;
2575   }
2576   GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2577   memcpy (buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2578   GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2579                                 tcp_probe_ctx->sock);
2580   ret = sizeof(tcp_probe_ctx->message);
2581   GNUNET_free(tcp_probe_ctx);
2582   return ret;
2583 }
2584
2585
2586 /**
2587  * Function called by the NAT subsystem suggesting another peer wants
2588  * to connect to us via connection reversal.  Try to connect back to the
2589  * given IP.
2590  *
2591  * @param cls closure
2592  * @param addr address to try
2593  * @param addrlen number of bytes in @a addr
2594  */
2595 static void
2596 try_connection_reversal (void *cls,
2597                          const struct sockaddr *addr,
2598                          socklen_t addrlen)
2599 {
2600   struct Plugin *plugin = cls;
2601   struct GNUNET_CONNECTION_Handle *sock;
2602   struct TCPProbeContext *tcp_probe_ctx;
2603
2604   /**
2605    * We have received an ICMP response, ostensibly from a peer
2606    * that wants to connect to us! Send a message to establish a connection.
2607    */
2608   sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2609   if (sock == NULL)
2610   {
2611     /* failed for some odd reason (out of sockets?); ignore attempt */
2612     return;
2613   }
2614
2615   tcp_probe_ctx = GNUNET_new (struct TCPProbeContext);
2616   tcp_probe_ctx->message.header.size
2617     = htons (sizeof (struct TCP_NAT_ProbeMessage));
2618   tcp_probe_ctx->message.header.type
2619     = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2620   tcp_probe_ctx->message.clientIdentity
2621     = *plugin->env->my_identity;
2622   tcp_probe_ctx->plugin = plugin;
2623   tcp_probe_ctx->sock = sock;
2624   GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
2625                                plugin->probe_tail,
2626                                tcp_probe_ctx);
2627   tcp_probe_ctx->transmit_handle
2628     = GNUNET_CONNECTION_notify_transmit_ready (sock,
2629                                                ntohs (tcp_probe_ctx->message.header.size),
2630                                                GNUNET_TIME_UNIT_FOREVER_REL,
2631                                                &notify_send_probe,
2632                                                tcp_probe_ctx);
2633 }
2634
2635
2636 /**
2637  * Function obtain the network type for a session
2638  *
2639  * @param cls closure ('struct Plugin*')
2640  * @param session the session
2641  * @return the network type in HBO or #GNUNET_SYSERR
2642  */
2643 static enum GNUNET_ATS_Network_Type
2644 tcp_plugin_get_network (void *cls,
2645                         struct Session *session)
2646 {
2647   return session->ats_address_network_type;
2648 }
2649
2650
2651 /**
2652  * Return information about the given session to the
2653  * monitor callback.
2654  *
2655  * @param cls the `struct Plugin` with the monitor callback (`sic`)
2656  * @param peer peer we send information about
2657  * @param value our `struct Session` to send information about
2658  * @return #GNUNET_OK (continue to iterate)
2659  */
2660 static int
2661 send_session_info_iter (void *cls,
2662                         const struct GNUNET_PeerIdentity *peer,
2663                         void *value)
2664 {
2665   struct Plugin *plugin = cls;
2666   struct Session *session = value;
2667
2668   notify_session_monitor (plugin,
2669                           session,
2670                           GNUNET_TRANSPORT_SS_INIT);
2671   /* FIXME: cannot tell if this is up or not from current
2672      session state... */
2673   notify_session_monitor (plugin,
2674                           session,
2675                           GNUNET_TRANSPORT_SS_UP);
2676   return GNUNET_OK;
2677 }
2678
2679
2680 /**
2681  * Begin monitoring sessions of a plugin.  There can only
2682  * be one active monitor per plugin (i.e. if there are
2683  * multiple monitors, the transport service needs to
2684  * multiplex the generated events over all of them).
2685  *
2686  * @param cls closure of the plugin
2687  * @param sic callback to invoke, NULL to disable monitor;
2688  *            plugin will being by iterating over all active
2689  *            sessions immediately and then enter monitor mode
2690  * @param sic_cls closure for @a sic
2691  */
2692 static void
2693 tcp_plugin_setup_monitor (void *cls,
2694                           GNUNET_TRANSPORT_SessionInfoCallback sic,
2695                           void *sic_cls)
2696 {
2697   struct Plugin *plugin = cls;
2698
2699   plugin->sic = sic;
2700   plugin->sic_cls = sic_cls;
2701   if (NULL != sic)
2702   {
2703     GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2704                                            &send_session_info_iter,
2705                                            plugin);
2706     /* signal end of first iteration */
2707     sic (sic_cls, NULL, NULL);
2708   }
2709 }
2710
2711
2712 /**
2713  * Entry point for the plugin.
2714  *
2715  * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
2716  * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
2717  */
2718 void *
2719 libgnunet_plugin_transport_tcp_init (void *cls)
2720 {
2721   static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2722     { &handle_tcp_welcome, NULL,
2723       GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME, sizeof(struct WelcomeMessage) },
2724     { &handle_tcp_nat_probe, NULL,
2725       GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, sizeof(struct TCP_NAT_ProbeMessage) },
2726     { &handle_tcp_data, NULL,
2727       GNUNET_MESSAGE_TYPE_ALL, 0 },
2728     { NULL, NULL, 0, 0 }
2729   };
2730   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2731   struct GNUNET_TRANSPORT_PluginFunctions *api;
2732   struct Plugin *plugin;
2733   struct GNUNET_SERVICE_Context *service;
2734   unsigned long long aport;
2735   unsigned long long bport;
2736   unsigned long long max_connections;
2737   unsigned int i;
2738   struct GNUNET_TIME_Relative idle_timeout;
2739 #ifdef TCP_STEALTH
2740   struct GNUNET_NETWORK_Handle *const*lsocks;
2741 #endif
2742   int ret;
2743   int ret_s;
2744   struct sockaddr **addrs;
2745   socklen_t *addrlens;
2746
2747   if (NULL == env->receive)
2748   {
2749     /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2750      initialze the plugin or the API */
2751     api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2752     api->cls = NULL;
2753     api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2754     api->address_to_string = &tcp_plugin_address_to_string;
2755     api->string_to_address = &tcp_plugin_string_to_address;
2756     return api;
2757   }
2758
2759   GNUNET_assert (NULL != env->cfg);
2760   if (GNUNET_OK !=
2761       GNUNET_CONFIGURATION_get_value_number (env->cfg,
2762                                              "transport-tcp",
2763                                              "MAX_CONNECTIONS",
2764                                              &max_connections))
2765     max_connections = 128;
2766
2767   aport = 0;
2768   if ((GNUNET_OK !=
2769        GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2770                                               "PORT", &bport)) ||
2771       (bport > 65535) ||
2772       ((GNUNET_OK ==
2773         GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2774                                                "ADVERTISED-PORT", &aport)) &&
2775        (aport > 65535) ))
2776   {
2777     LOG(GNUNET_ERROR_TYPE_ERROR,
2778         _("Require valid port number for service `%s' in configuration!\n"),
2779         "transport-tcp");
2780     return NULL ;
2781   }
2782   if (0 == aport)
2783     aport = bport;
2784   if (0 == bport)
2785     aport = 0;
2786   if (0 != bport)
2787   {
2788     service = GNUNET_SERVICE_start ("transport-tcp",
2789                                     env->cfg,
2790                                     GNUNET_SERVICE_OPTION_NONE);
2791     if (NULL == service)
2792     {
2793       LOG (GNUNET_ERROR_TYPE_WARNING,
2794            _("Failed to start service.\n"));
2795       return NULL;
2796     }
2797   }
2798   else
2799     service = NULL;
2800
2801   api = NULL;
2802   plugin = GNUNET_new (struct Plugin);
2803   plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections,
2804                                                              GNUNET_YES);
2805   plugin->max_connections = max_connections;
2806   plugin->open_port = bport;
2807   plugin->adv_port = aport;
2808   plugin->env = env;
2809   plugin->my_welcome.header.size = htons (sizeof(struct WelcomeMessage));
2810   plugin->my_welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
2811   plugin->my_welcome.clientIdentity = *plugin->env->my_identity;
2812
2813   if ( (NULL != service) &&
2814        (GNUNET_YES ==
2815         GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2816                                               "transport-tcp",
2817                                               "TCP_STEALTH")) )
2818   {
2819 #ifdef TCP_STEALTH
2820     plugin->myoptions |= TCP_OPTIONS_TCP_STEALTH;
2821     lsocks = GNUNET_SERVICE_get_listen_sockets (service);
2822     if (NULL != lsocks)
2823     {
2824       uint32_t len = sizeof (struct WelcomeMessage);
2825
2826       for (i=0;NULL!=lsocks[i];i++)
2827       {
2828         if ( (GNUNET_OK !=
2829               GNUNET_NETWORK_socket_setsockopt (lsocks[i],
2830                                                 IPPROTO_TCP,
2831                                                 TCP_STEALTH,
2832                                                 env->my_identity,
2833                                                 sizeof (struct GNUNET_PeerIdentity))) ||
2834              (GNUNET_OK !=
2835               GNUNET_NETWORK_socket_setsockopt (lsocks[i],
2836                                                 IPPROTO_TCP,
2837                                                 TCP_STEALTH_INTEGRITY_LEN,
2838                                                 &len,
2839                                                 sizeof (len))) )
2840         {
2841           /* TCP STEALTH not supported by kernel */
2842           GNUNET_assert (0 == i);
2843           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2844                       _("TCP_STEALTH not supported on this platform.\n"));
2845           goto die;
2846         }
2847       }
2848     }
2849 #else
2850     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2851                 _("TCP_STEALTH not supported on this platform.\n"));
2852     goto die;
2853 #endif
2854   }
2855
2856   if ( (NULL != service) &&
2857        (GNUNET_SYSERR !=
2858         (ret_s =
2859          GNUNET_SERVICE_get_server_addresses ("transport-tcp",
2860                                               env->cfg,
2861                                               &addrs,
2862                                               &addrlens))))
2863   {
2864     for (ret = ret_s-1; ret >= 0; ret--)
2865       LOG (GNUNET_ERROR_TYPE_INFO,
2866            "Binding to address `%s'\n",
2867            GNUNET_a2s (addrs[ret], addrlens[ret]));
2868     plugin->nat
2869       = GNUNET_NAT_register (env->cfg,
2870                              GNUNET_YES,
2871                              aport,
2872                              (unsigned int) ret_s,
2873                              (const struct sockaddr **) addrs, addrlens,
2874                              &tcp_nat_port_map_callback,
2875                              &try_connection_reversal,
2876                              plugin);
2877     for (ret = ret_s -1; ret >= 0; ret--)
2878       GNUNET_free (addrs[ret]);
2879     GNUNET_free_non_null (addrs);
2880     GNUNET_free_non_null (addrlens);
2881   }
2882   else
2883   {
2884     plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2885                                        GNUNET_YES, 0, 0, NULL, NULL, NULL,
2886                                        &try_connection_reversal, plugin);
2887   }
2888   api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2889   api->cls = plugin;
2890   api->send = &tcp_plugin_send;
2891   api->get_session = &tcp_plugin_get_session;
2892   api->disconnect_session = &tcp_plugin_disconnect_session;
2893   api->query_keepalive_factor = &tcp_plugin_query_keepalive_factor;
2894   api->disconnect_peer = &tcp_plugin_disconnect;
2895   api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2896   api->check_address = &tcp_plugin_check_address;
2897   api->address_to_string = &tcp_plugin_address_to_string;
2898   api->string_to_address = &tcp_plugin_string_to_address;
2899   api->get_network = &tcp_plugin_get_network;
2900   api->update_session_timeout = &tcp_plugin_update_session_timeout;
2901   api->update_inbound_delay = &tcp_plugin_update_inbound_delay;
2902   api->setup_monitor = &tcp_plugin_setup_monitor;
2903   plugin->service = service;
2904   if (NULL != service)
2905   {
2906     plugin->server = GNUNET_SERVICE_get_server (service);
2907   }
2908   else
2909   {
2910     if (GNUNET_OK !=
2911         GNUNET_CONFIGURATION_get_value_time (env->cfg,
2912                                              "transport-tcp",
2913                                              "TIMEOUT",
2914                                              &idle_timeout))
2915     {
2916       GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2917                                  "transport-tcp",
2918                                  "TIMEOUT");
2919       goto die;
2920     }
2921     plugin->server
2922       = GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check,
2923                                            plugin, NULL,
2924                                            idle_timeout, GNUNET_YES);
2925   }
2926   plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2927   memcpy (plugin->handlers, my_handlers, sizeof(my_handlers));
2928   for (i = 0;i < sizeof(my_handlers) / sizeof(struct GNUNET_SERVER_MessageHandler);i++)
2929     plugin->handlers[i].callback_cls = plugin;
2930
2931   GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2932   GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
2933   plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16,
2934                                                                  GNUNET_YES);
2935   if (0 != bport)
2936     LOG (GNUNET_ERROR_TYPE_INFO,
2937          _("TCP transport listening on port %llu\n"),
2938          bport);
2939   else
2940     LOG (GNUNET_ERROR_TYPE_INFO,
2941          _("TCP transport not listening on any port (client only)\n"));
2942   if ( (aport != bport) &&
2943        (0 != bport) )
2944     LOG (GNUNET_ERROR_TYPE_INFO,
2945          _("TCP transport advertises itself as being on port %llu\n"),
2946          aport);
2947   /* Initially set connections to 0 */
2948   GNUNET_assert(NULL != plugin->env->stats);
2949   GNUNET_STATISTICS_set (plugin->env->stats,
2950                          gettext_noop ("# TCP sessions active"),
2951                          0,
2952                          GNUNET_NO);
2953   return api;
2954
2955  die:
2956   if (NULL != plugin->nat)
2957     GNUNET_NAT_unregister (plugin->nat);
2958   GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
2959   if (NULL != service)
2960     GNUNET_SERVICE_stop (service);
2961   GNUNET_free (plugin);
2962   GNUNET_free_non_null (api);
2963   return NULL;
2964 }
2965
2966
2967 /**
2968  * Exit point from the plugin.
2969  *
2970  * @param cls the `struct GNUNET_TRANSPORT_PluginFunctions`
2971  * @return NULL
2972  */
2973 void *
2974 libgnunet_plugin_transport_tcp_done (void *cls)
2975 {
2976   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2977   struct Plugin *plugin = api->cls;
2978   struct TCPProbeContext *tcp_probe;
2979   struct PrettyPrinterContext *cur;
2980   struct PrettyPrinterContext *next;
2981
2982   if (NULL == plugin)
2983   {
2984     GNUNET_free(api);
2985     return NULL ;
2986   }
2987   LOG (GNUNET_ERROR_TYPE_DEBUG,
2988        "Shutting down TCP plugin\n");
2989
2990   /* Removing leftover sessions */
2991   GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2992                                          &session_disconnect_it,
2993                                          plugin);
2994   /* Removing leftover NAT sessions */
2995   GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
2996                                          &session_disconnect_it,
2997                                          plugin);
2998
2999   for (cur = ppc_dll_head; NULL != cur; cur = next)
3000   {
3001     next = cur->next;
3002     GNUNET_CONTAINER_DLL_remove (ppc_dll_head,
3003                                  ppc_dll_tail,
3004                                  cur);
3005     GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3006     cur->asc (cur->asc_cls, NULL, GNUNET_OK);
3007     GNUNET_free (cur);
3008   }
3009
3010   if (NULL != plugin->service)
3011     GNUNET_SERVICE_stop (plugin->service);
3012   else
3013     GNUNET_SERVER_destroy (plugin->server);
3014   GNUNET_free(plugin->handlers);
3015   if (NULL != plugin->nat)
3016     GNUNET_NAT_unregister (plugin->nat);
3017   while (NULL != (tcp_probe = plugin->probe_head))
3018   {
3019     GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3020                                  plugin->probe_tail,
3021                                  tcp_probe);
3022     GNUNET_CONNECTION_destroy (tcp_probe->sock);
3023     GNUNET_free(tcp_probe);
3024   }
3025   GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
3026   GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
3027   GNUNET_free(plugin);
3028   GNUNET_free(api);
3029   return NULL;
3030 }
3031
3032 /* end of plugin_transport_tcp.c */