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