backup errno
[oweals/gnunet.git] / src / transport / plugin_transport_tcp.c
1 /*
2      This file is part of GNUnet
3      (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file transport/plugin_transport_tcp.c
23  * @brief Implementation of the TCP transport service
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_connection_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_resolver_service.h"
34 #include "gnunet_server_lib.h"
35 #include "gnunet_service_lib.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_transport_service.h"
39 #include "plugin_transport.h"
40 #include "transport.h"
41
42 #define DEBUG_TCP GNUNET_NO
43
44 /**
45  * After how long do we expire an address that we
46  * learned from another peer if it is not reconfirmed
47  * by anyone?
48  */
49 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
50
51 /**
52  * How long until we give up on transmitting the welcome message?
53  */
54 #define WELCOME_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
55
56 /**
57  * How long until we give up on transmitting the welcome message?
58  */
59 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
60
61 /**
62  * For how many messages back to we keep transmission times?
63  */
64 #define ACK_LOG_SIZE 32
65
66
67
68 /**
69  * Message used to ask a peer to validate receipt (to check an address
70  * from a HELLO).  Followed by the address used.  Note that the
71  * recipients response does not affirm that he has this address,
72  * only that he got the challenge message.
73  */
74 struct ValidationChallengeMessage
75 {
76
77   /**
78    * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PING
79    */
80   struct GNUNET_MessageHeader header;
81
82   /**
83    * Random challenge number (in network byte order).
84    */
85   uint32_t challenge GNUNET_PACKED;
86
87   /**
88    * Who is the intended recipient?
89    */
90   struct GNUNET_PeerIdentity target;
91
92 };
93
94
95 /**
96  * Message used to validate a HELLO.  The challenge is included in the
97  * confirmation to make matching of replies to requests possible.  The
98  * signature signs the original challenge number, our public key, the
99  * sender's address (so that the sender can check that the address we
100  * saw is plausible for him and possibly detect a MiM attack) and a
101  * timestamp (to limit replay).<p>
102  *
103  * This message is followed by the address of the
104  * client that we are observing (which is part of what
105  * is being signed).
106  */
107 struct ValidationChallengeResponse
108 {
109
110   /**
111    * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PONG
112    */
113   struct GNUNET_MessageHeader header;
114
115   /**
116    * For padding, always zero.
117    */
118   uint32_t reserved GNUNET_PACKED;
119
120   /**
121    * Signature.
122    */
123   struct GNUNET_CRYPTO_RsaSignature signature;
124
125   /**
126    * What are we signing and why?
127    */
128   struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
129
130   /**
131    * Random challenge number (in network byte order).
132    */
133   uint32_t challenge GNUNET_PACKED;
134
135   /**
136    * Who signed this message?
137    */
138   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded signer;
139
140 };
141
142
143
144 /**
145  * Initial handshake message for a session.  This header
146  * is followed by the address that the other peer used to
147  * connect to us (so that we may learn it) or the address
148  * that the other peer got from the accept call.
149  */
150 struct WelcomeMessage
151 {
152   struct GNUNET_MessageHeader header;
153
154   /**
155    * Identity of the node connecting (TCP client)
156    */
157   struct GNUNET_PeerIdentity clientIdentity;
158
159 };
160
161
162 /**
163  * Encapsulation for normal TCP traffic.
164  */
165 struct DataMessage
166 {
167   struct GNUNET_MessageHeader header;
168
169   /**
170    * For alignment.
171    */
172   uint32_t reserved GNUNET_PACKED;
173
174   /**
175    * Number of the last message that was received from the other peer.
176    */
177   uint64_t ack_in GNUNET_PACKED;
178
179   /**
180    * Number of this outgoing message.
181    */
182   uint64_t ack_out GNUNET_PACKED;
183
184   /**
185    * How long was sending this ack delayed by the other peer
186    * (estimate).  The receiver of this message can use the delay
187    * between sending his message number 'ack' and receiving this ack
188    * minus the delay as an estimate of the round-trip time.
189    */
190   struct GNUNET_TIME_RelativeNBO delay;
191
192 };
193
194
195 /**
196  * Encapsulation of all of the state of the plugin.
197  */
198 struct Plugin;
199
200
201 /**
202  * Information kept for each message that is yet to
203  * be transmitted.
204  */
205 struct PendingMessage
206 {
207
208   /**
209    * This is a linked list.
210    */
211   struct PendingMessage *next;
212
213   /**
214    * The pending message, pointer to the end
215    * of this struct, do not free!
216    */
217   struct GNUNET_MessageHeader *msg;
218
219   /**
220    * Continuation function to call once the message
221    * has been sent.  Can be  NULL if there is no
222    * continuation to call.
223    */
224   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
225
226   /**
227    * Closure for transmit_cont.
228    */
229   void *transmit_cont_cls;
230
231   /**
232    * Timeout value for the pending message.
233    */
234   struct GNUNET_TIME_Absolute timeout;
235
236   /**
237    * GNUNET_YES if this is a welcome message;
238    * otherwise this should be a DATA message.
239    */
240   int is_welcome;
241
242 };
243
244
245 /**
246  * Session handle for TCP connections.
247  */
248 struct Session
249 {
250
251   /**
252    * Stored in a linked list.
253    */
254   struct Session *next;
255
256   /**
257    * Pointer to the global plugin struct.
258    */
259   struct Plugin *plugin;
260
261   /**
262    * The client (used to identify this connection)
263    */
264   struct GNUNET_SERVER_Client *client;
265
266   /**
267    * gnunet-service-transport context for this connection.
268    */
269   struct ReadyList *service_context;
270
271   /**
272    * Messages currently pending for transmission
273    * to this peer, if any.
274    */
275   struct PendingMessage *pending_messages;
276
277   /**
278    * Handle for pending transmission request.
279    */
280   struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
281
282   /**
283    * To whom are we talking to (set to our identity
284    * if we are still waiting for the welcome message)
285    */
286   struct GNUNET_PeerIdentity target;
287
288   /**
289    * At what time did we reset last_received last?
290    */
291   struct GNUNET_TIME_Absolute last_quota_update;
292
293   /**
294    * Address of the other peer if WE initiated the connection
295    * (and hence can be sure what it is), otherwise NULL.
296    */
297   void *connect_addr;
298
299   /**
300    * How many bytes have we received since the "last_quota_update"
301    * timestamp?
302    */
303   uint64_t last_received;
304
305   /**
306    * Our current latency estimate (in ms).
307    */
308   double latency_estimate;
309
310   /**
311    * Time when we generated the last ACK_LOG_SIZE acks.
312    * (the "last" refers to the "out_msg_counter" here)
313    */
314   struct GNUNET_TIME_Absolute gen_time[ACK_LOG_SIZE];
315
316   /**
317    * Our current sequence number.
318    */
319   uint64_t out_msg_counter;
320
321   /**
322    * Highest received incoming sequence number.
323    */
324   uint64_t max_in_msg_counter;
325
326   /**
327    * Number of bytes per ms that this peer is allowed
328    * to send to us.
329    */
330   uint32_t quota_in;
331
332   /**
333    * Length of connect_addr, can be 0.
334    */
335   size_t connect_alen;
336
337   /**
338    * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
339    * GNUNET_SYSERR is used to mark non-welcoming connections (HELLO
340    * validation only).
341    */
342   int expecting_welcome;
343
344 };
345
346
347 /**
348  * Encapsulation of all of the state of the plugin.
349  */
350 struct Plugin
351 {
352   /**
353    * Our environment.
354    */
355   struct GNUNET_TRANSPORT_PluginEnvironment *env;
356
357   /**
358    * The listen socket.
359    */
360   struct GNUNET_CONNECTION_Handle *lsock;
361
362   /**
363    * List of open TCP sessions.
364    */
365   struct Session *sessions;
366
367   /**
368    * Handle for the statistics service.
369    */
370   struct GNUNET_STATISTICS_Handle *statistics;
371
372   /**
373    * Handle to the network service.
374    */
375   struct GNUNET_SERVICE_Context *service;
376
377   /**
378    * Handle to the server for this service.
379    */
380   struct GNUNET_SERVER_Handle *server;
381
382   /**
383    * Copy of the handler array where the closures are
384    * set to this struct's instance.
385    */
386   struct GNUNET_SERVER_MessageHandler *handlers;
387
388   /**
389    * ID of task used to update our addresses when one expires.
390    */
391   GNUNET_SCHEDULER_TaskIdentifier address_update_task;
392
393   /**
394    * Port that we are actually listening on.
395    */
396   uint16_t open_port;
397
398   /**
399    * Port that the user said we would have visible to the
400    * rest of the world.
401    */
402   uint16_t adv_port;
403
404 };
405
406
407 /**
408  * Find the session handle for the given peer.
409  */
410 static struct Session *
411 find_session_by_target (struct Plugin *plugin,
412                         const struct GNUNET_PeerIdentity *target)
413 {
414   struct Session *ret;
415
416   ret = plugin->sessions;
417   while ((ret != NULL) &&
418          ( (GNUNET_SYSERR == ret->expecting_welcome) ||
419            (0 != memcmp (target,
420                          &ret->target, sizeof (struct GNUNET_PeerIdentity)))))
421     ret = ret->next;
422   return ret;
423 }
424
425
426 /**
427  * Find the session handle for the given peer.
428  */
429 static struct Session *
430 find_session_by_client (struct Plugin *plugin,
431                         const struct GNUNET_SERVER_Client *client)
432 {
433   struct Session *ret;
434
435   ret = plugin->sessions;
436   while ((ret != NULL) && (client != ret->client))
437     ret = ret->next;
438   return ret;
439 }
440
441
442 /**
443  * Create a welcome message.
444  */
445 static struct PendingMessage *
446 create_welcome (size_t addrlen, const void *addr, struct Plugin *plugin)
447 {
448   struct PendingMessage *pm;
449   struct WelcomeMessage *welcome;
450
451   pm = GNUNET_malloc (sizeof (struct PendingMessage) +
452                       sizeof (struct WelcomeMessage) + addrlen);
453   pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
454   welcome = (struct WelcomeMessage *) &pm[1];
455   welcome->header.size = htons (sizeof (struct WelcomeMessage) + addrlen);
456   welcome->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
457   GNUNET_CRYPTO_hash (plugin->env->my_public_key,
458                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
459                       &welcome->clientIdentity.hashPubKey);
460   memcpy (&welcome[1], addr, addrlen);
461   pm->timeout = GNUNET_TIME_relative_to_absolute (WELCOME_TIMEOUT);
462   pm->is_welcome = GNUNET_YES;
463   return pm;
464 }
465
466
467 /**
468  * Create a new session using the specified address
469  * for the welcome message.
470  *
471  * @param plugin us
472  * @param target peer to connect to
473  * @param client client to use
474  * @param addrlen IPv4 or IPv6
475  * @param addr either struct sockaddr_in or struct sockaddr_in6
476  * @return NULL connection failed / invalid address
477  */
478 static struct Session *
479 create_session (struct Plugin *plugin,
480                 const struct GNUNET_PeerIdentity *target,
481                 struct GNUNET_SERVER_Client *client,
482                 const void *addr, size_t addrlen)
483 {
484   struct Session *ret;
485
486   ret = GNUNET_malloc (sizeof (struct Session));
487   ret->plugin = plugin;
488   ret->next = plugin->sessions;
489   plugin->sessions = ret;
490   ret->client = client;
491   ret->target = *target;
492   ret->last_quota_update = GNUNET_TIME_absolute_get ();
493   ret->quota_in = plugin->env->default_quota_in;
494   ret->expecting_welcome = GNUNET_YES;
495   ret->pending_messages = create_welcome (addrlen, addr, plugin);
496   return ret;
497 }
498
499
500 /**
501  * If we have pending messages, ask the server to
502  * transmit them (schedule the respective tasks, etc.)
503  *
504  * @param session for which session should we do this
505  */
506 static void process_pending_messages (struct Session *session);
507
508
509 /**
510  * Function called to notify a client about the socket
511  * begin ready to queue more data.  "buf" will be
512  * NULL and "size" zero if the socket was closed for
513  * writing in the meantime.
514  *
515  * @param cls closure
516  * @param size number of bytes available in buf
517  * @param buf where the callee should write the message
518  * @return number of bytes written to buf
519  */
520 static size_t
521 do_transmit (void *cls, size_t size, void *buf)
522 {
523   struct Session *session = cls;
524   struct PendingMessage *pm;
525   char *cbuf;
526   uint16_t msize;
527   size_t ret;
528   struct DataMessage *dm;
529
530   session->transmit_handle = NULL;
531   if (buf == NULL)
532     {
533 #if DEBUG_TCP
534       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
535                        "tcp", 
536                        "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
537                        GNUNET_i2s(&session->target));
538 #endif
539       /* timeout */
540       while (NULL != (pm = session->pending_messages))
541         {
542           session->pending_messages = pm->next;
543 #if DEBUG_TCP
544           GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
545                            "tcp", 
546                            "Failed to transmit message of type %u to `%4s'.\n",
547                            ntohs(pm->msg->type),
548                            GNUNET_i2s(&session->target));
549 #endif
550           if (pm->transmit_cont != NULL)
551             pm->transmit_cont (pm->transmit_cont_cls,
552                                session->service_context,
553                                &session->target, GNUNET_SYSERR);            
554           GNUNET_free (pm);
555         }
556       return 0;
557     }
558   ret = 0;
559   cbuf = buf;
560   while (NULL != (pm = session->pending_messages))
561     {
562       if (pm->is_welcome)
563         {
564           if (size < (msize = htons (pm->msg->size)))
565             break;
566           memcpy (cbuf, pm->msg, msize);
567           cbuf += msize;
568           ret += msize;
569           size -= msize;
570         }
571       else
572         {
573           if (size <
574               sizeof (struct DataMessage) + (msize = htons (pm->msg->size)))
575             break;
576           dm = (struct DataMessage *) cbuf;
577           dm->header.size = htons (sizeof (struct DataMessage) + msize);
578           dm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_DATA);
579           dm->ack_out = GNUNET_htonll (++session->out_msg_counter);
580           dm->ack_in = GNUNET_htonll (session->max_in_msg_counter);
581           cbuf += sizeof (struct DataMessage);
582           ret += sizeof (struct DataMessage);
583           size -= sizeof (struct DataMessage);
584           memcpy (cbuf, pm->msg, msize);
585           cbuf += msize;
586           ret += msize;
587           size -= msize;
588         }
589       session->pending_messages = pm->next;
590       if (pm->transmit_cont != NULL)
591         pm->transmit_cont (pm->transmit_cont_cls,
592                            session->service_context,
593                            &session->target, GNUNET_OK);
594       GNUNET_free (pm);
595       session->gen_time[session->out_msg_counter % ACK_LOG_SIZE]
596         = GNUNET_TIME_absolute_get ();
597     }
598   process_pending_messages (session);
599 #if DEBUG_TCP
600   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
601                    "tcp", "Transmitting %u bytes\n", ret);
602 #endif
603   return ret;
604 }
605
606
607 /**
608  * If we have pending messages, ask the server to
609  * transmit them (schedule the respective tasks, etc.)
610  *
611  * @param session for which session should we do this
612  */
613 static void
614 process_pending_messages (struct Session *session)
615 {
616   GNUNET_assert (session->client != NULL);
617   if (session->pending_messages == NULL)
618     return;
619   if (session->transmit_handle != NULL)
620     return;
621   session->transmit_handle
622     = GNUNET_SERVER_notify_transmit_ready (session->client,
623                                            htons (session->
624                                                   pending_messages->msg->
625                                                   size) +
626                                            (session->
627                                             pending_messages->is_welcome ? 0 :
628                                             sizeof (struct DataMessage)),
629                                            GNUNET_TIME_absolute_get_remaining
630                                            (session->
631                                             pending_messages[0].timeout),
632                                            &do_transmit, session);
633 }
634
635
636
637 /**
638  * Create a new session connecting to the specified
639  * target at the specified address.  The session will
640  * be used to verify an address in a HELLO and should
641  * not expect to receive a WELCOME.
642  *
643  * @param plugin us
644  * @param target peer to connect to
645  * @param addrlen IPv4 or IPv6
646  * @param addr either struct sockaddr_in or struct sockaddr_in6
647  * @return NULL connection failed / invalid address
648  */
649 static struct Session *
650 connect_and_create_validation_session (struct Plugin *plugin,
651                                        const struct GNUNET_PeerIdentity *target,
652                                        const void *addr, size_t addrlen)
653 {
654   struct GNUNET_SERVER_Client *client;
655   struct GNUNET_CONNECTION_Handle *conn;
656   struct Session *session;
657   int af;
658
659   if (addrlen == sizeof (struct sockaddr_in))
660     af = AF_INET;
661   else if (addrlen == sizeof (struct sockaddr_in6))
662     af = AF_INET6;    
663   else
664     {
665       GNUNET_break_op (0);
666       return NULL;              /* invalid address */
667     }
668   conn = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
669                                                      af,
670                                                      addr,
671                                                      addrlen,
672                                                      GNUNET_SERVER_MAX_MESSAGE_SIZE);
673   if (conn == NULL)
674     {
675 #if DEBUG_TCP
676       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
677                        "tcp",
678                        "Failed to create connection to peer at `%s'.\n",
679                        GNUNET_a2s(addr, addrlen));
680 #endif
681       return NULL;
682     }
683   client = GNUNET_SERVER_connect_socket (plugin->server, conn);
684   GNUNET_assert (client != NULL);
685   session = create_session (plugin, target, client, addr, addrlen);
686   /* kill welcome */
687   GNUNET_free (session->pending_messages);
688   session->pending_messages = NULL;
689   session->connect_alen = addrlen;
690   session->connect_addr = GNUNET_malloc (addrlen);
691   session->expecting_welcome = GNUNET_SYSERR;  
692   memcpy (session->connect_addr, addr, addrlen);
693 #if DEBUG_TCP
694   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
695                    "tcp",
696                    "Creating new session %p with `%s' for `%4s' based on `%s' request.\n",
697                    session, 
698                    GNUNET_a2s (addr, addrlen),
699                    GNUNET_i2s (&session->target),
700                    "VALIDATE");
701 #endif
702   return session;
703 }
704
705
706 /**
707  * Function that can be used by the transport service to validate that
708  * another peer is reachable at a particular address (even if we
709  * already have a connection to this peer, this function is required
710  * to establish a new one).
711  *
712  * @param cls closure
713  * @param target who should receive this message
714  * @param challenge challenge code to use
715  * @param addrlen length of the address
716  * @param addr the address
717  * @param timeout how long should we try to transmit these?
718  * @return GNUNET_OK if the transmission has been scheduled
719  */
720 static int
721 tcp_plugin_validate (void *cls,
722                      const struct GNUNET_PeerIdentity *target,
723                      uint32_t challenge,
724                      struct GNUNET_TIME_Relative timeout,
725                      const void *addr, size_t addrlen)
726 {
727   struct Plugin *plugin = cls;
728   struct Session *session;
729   struct PendingMessage *pm;
730   struct ValidationChallengeMessage *vcm;
731
732   session = connect_and_create_validation_session (plugin, target, addr, addrlen);
733   if (session == NULL)
734     {
735 #if DEBUG_TCP
736       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
737                        "tcp", "Failed to create fresh session.\n");
738 #endif
739       return GNUNET_SYSERR;
740     }
741   pm = GNUNET_malloc (sizeof (struct PendingMessage) +
742                       sizeof (struct ValidationChallengeMessage) + addrlen);
743   pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
744   pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
745   pm->is_welcome = GNUNET_YES;
746   vcm = (struct ValidationChallengeMessage*) &pm[1];
747   vcm->header.size =
748     htons (sizeof (struct ValidationChallengeMessage) + addrlen);
749   vcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PING);
750   vcm->challenge = htonl(challenge);
751   vcm->target = *target;
752   memcpy (&vcm[1], addr, addrlen);
753   GNUNET_assert (session->pending_messages == NULL);
754   session->pending_messages = pm;
755   process_pending_messages (session);
756   return GNUNET_OK;
757 }
758
759
760 /**
761  * Functions with this signature are called whenever we need
762  * to close a session due to a disconnect or failure to
763  * establish a connection.
764  *
765  * @param session session to close down
766  */
767 static void
768 disconnect_session (struct Session *session)
769 {
770   struct Session *prev;
771   struct Session *pos;
772   struct PendingMessage *pm;
773
774 #if DEBUG_TCP
775   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
776                    "tcp",
777                    "Disconnecting from `%4s' at %s (session %p).\n", 
778                    GNUNET_i2s(&session->target),
779                    (session->connect_addr != NULL) ? 
780                    GNUNET_a2s(session->connect_addr,
781                               session->connect_alen) : "*",
782                    session);
783 #endif
784   /* remove from session list */
785   prev = NULL;
786   pos = session->plugin->sessions;
787   while (pos != session)
788     {
789       prev = pos;
790       pos = pos->next;
791     }
792   if (prev == NULL)
793     session->plugin->sessions = session->next;
794   else
795     prev->next = session->next;
796   /* clean up state */
797   if (session->transmit_handle != NULL)
798     {
799       GNUNET_CONNECTION_notify_transmit_ready_cancel (session->transmit_handle);
800       session->transmit_handle = NULL;
801     }
802   while (NULL != (pm = session->pending_messages))
803     {
804 #if DEBUG_TCP
805       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
806                        "tcp",
807                        pm->transmit_cont != NULL 
808                        ? "Could not deliver message of type %u to `%4s'.\n" 
809                        : "Could not deliver message of type %u to `%4s', notifying.\n",
810                        ntohs(pm->msg->type),                   
811                        GNUNET_i2s(&session->target));
812 #endif
813       session->pending_messages = pm->next;
814       if (NULL != pm->transmit_cont)
815         pm->transmit_cont (pm->transmit_cont_cls,
816                            session->service_context,
817                            &session->target, GNUNET_SYSERR);
818       GNUNET_free (pm);
819     }
820   if (GNUNET_NO == session->expecting_welcome)
821     {
822 #if DEBUG_TCP
823       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
824                        "tcp",
825                        "Notifying transport service about loss of data connection with `%4s'.\n",
826                        GNUNET_i2s(&session->target));
827 #endif
828       /* Data session that actually went past the 
829          initial handshake; transport service may
830          know about this one, so we need to 
831          notify transport service about disconnect */
832       session->plugin->env->receive (session->plugin->env->cls,
833                                      session,
834                                      session->service_context,
835                                      GNUNET_TIME_UNIT_ZERO,
836                                      &session->target, NULL);
837     }
838   if (session->client != NULL)
839     {
840       GNUNET_SERVER_client_drop (session->client);
841       session->client = NULL;
842     }
843   GNUNET_free_non_null (session->connect_addr);
844   GNUNET_free (session);
845 }
846
847
848 /**
849  * Iterator callback to go over all addresses.  If we get
850  * a TCP address, increment the counter
851  *
852  * @param cls closure, points to the counter
853  * @param tname name of the transport
854  * @param expiration expiration time
855  * @param addr the address
856  * @param addrlen length of the address
857  * @return GNUNET_OK to keep the address,
858  *         GNUNET_NO to delete it from the HELLO
859  *         GNUNET_SYSERR to stop iterating (but keep current address)
860  */
861 static int
862 count_tcp_addresses (void *cls,
863                      const char *tname,
864                      struct GNUNET_TIME_Absolute expiration,
865                      const void *addr, size_t addrlen)
866 {
867   unsigned int *counter = cls;
868
869   if (0 != strcmp (tname, "tcp"))
870     return GNUNET_OK;           /* not one of ours */
871   (*counter)++;
872   return GNUNET_OK;             /* failed to connect */
873 }
874
875
876 struct ConnectContext
877 {
878   struct Plugin *plugin;
879
880   struct GNUNET_CONNECTION_Handle *sa;
881
882   struct PendingMessage *welcome;
883
884   unsigned int pos;
885 };
886
887
888 /**
889  * Iterator callback to go over all addresses.  If we get
890  * the "pos" TCP address, try to connect to it.
891  *
892  * @param cls closure
893  * @param tname name of the transport
894  * @param expiration expiration time
895  * @param addrlen length of the address
896  * @param addr the address
897  * @return GNUNET_OK to keep the address,
898  *         GNUNET_NO to delete it from the HELLO
899  *         GNUNET_SYSERR to stop iterating (but keep current address)
900  */
901 static int
902 try_connect_to_address (void *cls,
903                         const char *tname,
904                         struct GNUNET_TIME_Absolute expiration,
905                         const void *addr, size_t addrlen)
906 {
907   struct ConnectContext *cc = cls;
908   int af;
909
910   if (0 != strcmp (tname, "tcp"))
911     return GNUNET_OK;           /* not one of ours */
912   if (sizeof (struct sockaddr_in) == addrlen)
913     af = AF_INET;
914   else if (sizeof (struct sockaddr_in6) == addrlen)
915     af = AF_INET6;
916   else
917     {
918       /* not a valid address */
919       GNUNET_break (0);
920       return GNUNET_NO;
921     }
922   if (0 == cc->pos--)
923     {
924       cc->welcome = create_welcome (addrlen, addr, cc->plugin);
925       cc->sa =
926         GNUNET_CONNECTION_create_from_sockaddr (cc->plugin->env->sched,
927                                                     af, addr, addrlen,
928                                                     GNUNET_SERVER_MAX_MESSAGE_SIZE);
929 #if DEBUG_TCP
930       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
931                        "tcp", 
932                        "Connecting using address %s.\n",
933                        GNUNET_a2s(addr, addrlen));
934 #endif
935       return GNUNET_SYSERR;
936     }
937   return GNUNET_OK;             /* failed to connect */
938 }
939
940
941 /**
942  * Type of an iterator over the hosts.  Note that each
943  * host will be called with each available protocol.
944  *
945  * @param cls closure
946  * @param peer id of the peer, NULL for last call
947  * @param hello hello message for the peer (can be NULL)
948  * @param trust amount of trust we have in the peer
949  */
950 static void
951 session_try_connect (void *cls,
952                      const struct GNUNET_PeerIdentity *peer,
953                      const struct GNUNET_HELLO_Message *hello, uint32_t trust)
954 {
955   struct Session *session = cls;
956   unsigned int count;
957   struct ConnectContext cctx;
958   struct PendingMessage *pm;
959
960   if (peer == NULL)
961     {
962       /* last call, destroy session if we are still not
963          connected */
964       if (session->client != NULL)
965         {
966 #if DEBUG_TCP
967           GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
968                            "tcp",
969                            "Now connected to `%4s', now processing messages.\n",
970                            GNUNET_i2s(&session->target));
971 #endif
972           process_pending_messages (session);
973         }
974       else
975         {
976 #if DEBUG_TCP
977           GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
978                            "tcp",
979                            "Failed to connect to `%4s' (no working `%s'), closing session.\n",
980                            GNUNET_i2s(&session->target),
981                            "HELLO");
982 #endif
983           disconnect_session (session);
984         }
985       return;
986     }
987   if ((hello == NULL) || (session->client != NULL))
988     {
989       GNUNET_break (0);         /* should this ever happen!? */
990       return;
991     }
992   count = 0;
993   GNUNET_HELLO_iterate_addresses (hello,
994                                   GNUNET_NO, &count_tcp_addresses, &count);
995   if (count == 0)
996     {
997 #if DEBUG_TCP
998       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
999                        "tcp",
1000                        "Asked to connect to `%4s', but have no addresses to try.\n",
1001                        GNUNET_i2s(&session->target));
1002 #endif
1003       return;
1004     }
1005   cctx.plugin = session->plugin;
1006   cctx.sa = NULL;
1007   cctx.welcome = NULL;
1008   cctx.pos = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
1009   GNUNET_HELLO_iterate_addresses (hello,
1010                                   GNUNET_NO, &try_connect_to_address, &cctx);
1011   if (cctx.sa == NULL)
1012     {
1013 #if DEBUG_TCP
1014       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1015                        "tcp",
1016                        "Asked to connect, but all addresses failed.\n");
1017 #endif
1018       GNUNET_free_non_null (cctx.welcome);
1019       return;
1020     }
1021   session->client = GNUNET_SERVER_connect_socket (session->plugin->server,
1022                                                   cctx.sa);
1023 #if DEBUG_TCP
1024   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025               "Connected to `%4s' for session %p\n",
1026               GNUNET_i2s(&session->target),
1027               session->client);
1028 #endif
1029   if (session->client == NULL)
1030     {
1031       GNUNET_break (0);         /* how could this happen? */
1032       GNUNET_free_non_null (cctx.welcome);
1033       return;
1034     }
1035   pm = cctx.welcome;
1036   /* prepend (!) */
1037   pm->next = session->pending_messages;
1038   session->pending_messages = pm;
1039 #if DEBUG_TCP
1040   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1041                    "tcp",
1042                    "Connected to `%4s', now sending `%s' message.\n",
1043                    GNUNET_i2s(&session->target),
1044                    "WELCOME");
1045 #endif
1046 }
1047
1048
1049 /**
1050  * Function that can be used by the transport service to transmit
1051  * a message using the plugin.
1052  *
1053  * @param cls closure
1054  * @param plugin_context value we were asked to pass to this plugin
1055  *        to respond to the given peer (use is optional,
1056  *        but may speed up processing), can be NULL
1057  * @param service_context value passed to the transport-service
1058  *        to identify the neighbour
1059  * @param target who should receive this message
1060  * @param priority how important is the message
1061  * @param msg the message to transmit
1062  * @param cont continuation to call once the message has
1063  *        been transmitted (or if the transport is ready
1064  *        for the next transmission call; or if the
1065  *        peer disconnected...)
1066  * @param cont_cls closure for cont
1067  * @return plugin_context that should be used next time for
1068  *         sending messages to the specified peer
1069  */
1070 static void *
1071 tcp_plugin_send (void *cls,
1072                  void *plugin_context,
1073                  struct ReadyList *service_context,
1074                  const struct GNUNET_PeerIdentity *target,   
1075                  unsigned int priority,
1076                  const struct GNUNET_MessageHeader *msg,
1077                  struct GNUNET_TIME_Relative timeout,
1078                  GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1079 {
1080   struct Plugin *plugin = cls;
1081   struct Session *session = plugin_context;
1082   struct PendingMessage *pm;
1083   struct PendingMessage *pme;
1084
1085   if (session == NULL)
1086     session = find_session_by_target (plugin, target);  
1087   pm = GNUNET_malloc (sizeof (struct PendingMessage) + ntohs (msg->size));
1088   pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
1089   memcpy (pm->msg, msg, ntohs (msg->size));
1090   pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1091   pm->transmit_cont = cont;
1092   pm->transmit_cont_cls = cont_cls;
1093   if (session == NULL)
1094     {
1095       session = GNUNET_malloc (sizeof (struct Session));
1096 #if DEBUG_TCP
1097       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1098                        "tcp",
1099                        "Asked to transmit, creating fresh session %p.\n",
1100                        session);
1101 #endif
1102       session->next = plugin->sessions;
1103       plugin->sessions = session;
1104       session->plugin = plugin;
1105       session->target = *target;
1106       session->last_quota_update = GNUNET_TIME_absolute_get ();
1107       session->quota_in = plugin->env->default_quota_in;
1108       session->expecting_welcome = GNUNET_YES;
1109       session->pending_messages = pm;
1110       session->service_context = service_context;
1111       GNUNET_PEERINFO_for_all (plugin->env->cfg,
1112                                plugin->env->sched,
1113                                target,
1114                                0, timeout, &session_try_connect, session);
1115       return session;
1116     }
1117   GNUNET_assert (session != NULL);
1118   GNUNET_assert (session->client != NULL);
1119   session->service_context = service_context;
1120   /* append pm to pending_messages list */
1121   pme = session->pending_messages;
1122   if (pme == NULL)
1123     {
1124       session->pending_messages = pm;
1125     }
1126   else
1127     {
1128       while (NULL != pme->next)
1129         pme = pme->next;
1130       pme->next = pm;
1131     }
1132 #if DEBUG_TCP
1133   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1134                    "tcp", "Asked to transmit, added message to list.\n");
1135 #endif
1136   process_pending_messages (session);
1137   return session;
1138 }
1139
1140
1141
1142 /**
1143  * Function that can be called to force a disconnect from the
1144  * specified neighbour.  This should also cancel all previously
1145  * scheduled transmissions.  Obviously the transmission may have been
1146  * partially completed already, which is OK.  The plugin is supposed
1147  * to close the connection (if applicable) and no longer call the
1148  * transmit continuation(s).
1149  *
1150  * Finally, plugin MUST NOT call the services's receive function to
1151  * notify the service that the connection to the specified target was
1152  * closed after a getting this call.
1153  *
1154  * @param cls closure
1155  * @param plugin_context value we were asked to pass to this plugin
1156  *        to respond to the given peer (use is optional,
1157  *        but may speed up processing), can be NULL (if
1158  *        NULL was returned from the transmit function); note
1159  *        that use of NULL is dangerous since then this call may
1160  *        cancel any session with the target peer (including
1161  *        HELLO validation sessions), which is likely not what
1162  *        is intended.
1163  * @param service_context must correspond to the service context
1164  *        of the corresponding Transmit call; the plugin should
1165  *        not cancel a send call made with a different service
1166  *        context pointer!  Never NULL.
1167  * @param target peer for which the last transmission is
1168  *        to be cancelled
1169  */
1170 static void
1171 tcp_plugin_cancel (void *cls,
1172                    void *plugin_context,
1173                    struct ReadyList *service_context,
1174                    const struct GNUNET_PeerIdentity *target)
1175 {
1176   struct Plugin *plugin = cls;
1177   struct Session *session = plugin_context;
1178   struct PendingMessage *pm;
1179   
1180   if (session == NULL)
1181     {
1182 #if DEBUG_TCP
1183       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1184                        "tcp",
1185                        "Asked to cancel with `%4s' without specification of specifics; will try to find an applicable session\n",
1186                        GNUNET_i2s(target));
1187 #endif
1188       session = find_session_by_target (plugin, target);
1189     }
1190   if (session == NULL)
1191     {
1192       GNUNET_break (0);
1193       return;
1194     }
1195 #if DEBUG_TCP
1196   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1197                    "tcp",
1198                    "Asked to cancel session %p with `%4s'\n",
1199                    plugin_context,
1200                    GNUNET_i2s(target));
1201 #endif
1202   pm = session->pending_messages;
1203   while (pm != NULL)
1204     {
1205       pm->transmit_cont = NULL;
1206       pm->transmit_cont_cls = NULL;
1207       pm = pm->next;
1208     }
1209   session->service_context = NULL;
1210   if (session->client != NULL)
1211     {
1212       GNUNET_SERVER_client_drop (session->client);
1213       session->client = NULL;
1214     }
1215   /* rest of the clean-up of the session will be done as part of
1216      disconnect_notify which should be triggered any time now 
1217      (or which may be triggering this call in the first place) */
1218 }
1219
1220
1221 struct PrettyPrinterContext
1222 {
1223   GNUNET_TRANSPORT_AddressStringCallback asc;
1224   void *asc_cls;
1225   uint16_t port;
1226 };
1227
1228
1229 /**
1230  * Append our port and forward the result.
1231  */
1232 static void
1233 append_port (void *cls, const char *hostname)
1234 {
1235   struct PrettyPrinterContext *ppc = cls;
1236   char *ret;
1237
1238   if (hostname == NULL)
1239     {
1240       ppc->asc (ppc->asc_cls, NULL);
1241       GNUNET_free (ppc);
1242       return;
1243     }
1244   GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1245   ppc->asc (ppc->asc_cls, ret);
1246   GNUNET_free (ret);
1247 }
1248
1249
1250 /**
1251  * Convert the transports address to a nice, human-readable
1252  * format.
1253  *
1254  * @param cls closure
1255  * @param name name of the transport that generated the address
1256  * @param addr one of the addresses of the host, NULL for the last address
1257  *        the specific address format depends on the transport
1258  * @param addrlen length of the address
1259  * @param numeric should (IP) addresses be displayed in numeric form?
1260  * @param timeout after how long should we give up?
1261  * @param asc function to call on each string
1262  * @param asc_cls closure for asc
1263  */
1264 static void
1265 tcp_plugin_address_pretty_printer (void *cls,
1266                                    const char *type,
1267                                    const void *addr,
1268                                    size_t addrlen,
1269                                    int numeric,
1270                                    struct GNUNET_TIME_Relative timeout,
1271                                    GNUNET_TRANSPORT_AddressStringCallback asc,
1272                                    void *asc_cls)
1273 {
1274   struct Plugin *plugin = cls;
1275   const struct sockaddr_in *v4;
1276   const struct sockaddr_in6 *v6;
1277   struct PrettyPrinterContext *ppc;
1278
1279   if ((addrlen != sizeof (struct sockaddr_in)) &&
1280       (addrlen != sizeof (struct sockaddr_in6)))
1281     {
1282       /* invalid address */
1283       GNUNET_break_op (0);
1284       asc (asc_cls, NULL);
1285       return;
1286     }
1287   ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1288   ppc->asc = asc;
1289   ppc->asc_cls = asc_cls;
1290   if (addrlen == sizeof (struct sockaddr_in))
1291     {
1292       v4 = (const struct sockaddr_in *) addr;
1293       ppc->port = ntohs (v4->sin_port);
1294     }
1295   else
1296     {
1297       v6 = (const struct sockaddr_in6 *) addr;
1298       ppc->port = ntohs (v6->sin6_port);
1299
1300     }
1301   GNUNET_RESOLVER_hostname_get (plugin->env->sched,
1302                                 plugin->env->cfg,
1303                                 addr,
1304                                 addrlen,
1305                                 !numeric, timeout, &append_port, ppc);
1306 }
1307
1308
1309 /**
1310  * Update the last-received and bandwidth quota values
1311  * for this session.
1312  *
1313  * @param session session to update
1314  * @param force set to GNUNET_YES if we should update even
1315  *        though the minimum refresh time has not yet expired
1316  */
1317 static void
1318 update_quota (struct Session *session, int force)
1319 {
1320   struct GNUNET_TIME_Absolute now;
1321   unsigned long long delta;
1322   unsigned long long total_allowed;
1323   unsigned long long total_remaining;
1324
1325   now = GNUNET_TIME_absolute_get ();
1326   delta = now.value - session->last_quota_update.value;
1327   if ((delta < MIN_QUOTA_REFRESH_TIME) && (!force))
1328     return;                     /* too early, not enough data */
1329
1330   total_allowed = session->quota_in * delta;
1331   if (total_allowed > session->last_received)
1332     {
1333       /* got less than acceptable */
1334       total_remaining = total_allowed - session->last_received;
1335       session->last_received = 0;
1336       delta = total_remaining / session->quota_in;      /* bonus seconds */
1337       if (delta > MAX_BANDWIDTH_CARRY)
1338         delta = MAX_BANDWIDTH_CARRY;    /* limit amount of carry-over */
1339     }
1340   else
1341     {
1342       /* got more than acceptable */
1343       session->last_received -= total_allowed;
1344       delta = 0;
1345     }
1346   session->last_quota_update.value = now.value - delta;
1347 }
1348
1349
1350 /**
1351  * Set a quota for receiving data from the given peer; this is a
1352  * per-transport limit.  The transport should limit its read/select
1353  * calls to stay below the quota (in terms of incoming data).
1354  *
1355  * @param cls closure
1356  * @param peer the peer for whom the quota is given
1357  * @param quota_in quota for receiving/sending data in bytes per ms
1358  */
1359 static void
1360 tcp_plugin_set_receive_quota (void *cls,
1361                               const struct GNUNET_PeerIdentity *target,
1362                               uint32_t quota_in)
1363 {
1364   struct Plugin *plugin = cls;
1365   struct Session *session;
1366
1367   session = find_session_by_target (plugin, target);
1368   if (session == NULL)
1369     return; /* peer must have disconnected, ignore */
1370   if (session->quota_in != quota_in)
1371     {
1372       update_quota (session, GNUNET_YES);
1373       if (session->quota_in > quota_in)
1374         session->last_quota_update = GNUNET_TIME_absolute_get ();
1375       session->quota_in = quota_in;
1376     }
1377 }
1378
1379
1380 /**
1381  * Check if the given port is plausible (must be either
1382  * our listen port or our advertised port).  If it is
1383  * neither, we return one of these two ports at random.
1384  *
1385  * @return either in_port or a more plausible port
1386  */
1387 static uint16_t
1388 check_port (struct Plugin *plugin, uint16_t in_port)
1389 {
1390   if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1391     return in_port;
1392   return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1393                                     2) == 0)
1394     ? plugin->open_port : plugin->adv_port;
1395 }
1396
1397
1398 /**
1399  * Another peer has suggested an address for this
1400  * peer and transport plugin.  Check that this could be a valid
1401  * address.  If so, consider adding it to the list
1402  * of addresses.
1403  *
1404  * @param cls closure
1405  * @param addr pointer to the address
1406  * @param addrlen length of addr
1407  * @return GNUNET_OK if this is a plausible address for this peer
1408  *         and transport
1409  */
1410 static int
1411 tcp_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1412 {
1413   struct Plugin *plugin = cls;
1414   char buf[sizeof (struct sockaddr_in6)];
1415   struct sockaddr_in *v4;
1416   struct sockaddr_in6 *v6;
1417
1418   if ((addrlen != sizeof (struct sockaddr_in)) &&
1419       (addrlen != sizeof (struct sockaddr_in6)))
1420     {
1421       GNUNET_break_op (0);
1422       return GNUNET_SYSERR;
1423     }
1424   memcpy (buf, addr, sizeof (struct sockaddr_in6));
1425   if (addrlen == sizeof (struct sockaddr_in))
1426     {
1427       v4 = (struct sockaddr_in *) buf;
1428       v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
1429     }
1430   else
1431     {
1432       v6 = (struct sockaddr_in6 *) buf;
1433       v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
1434     }
1435 #if DEBUG_TCP
1436   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1437                    "tcp",
1438                    "Informing transport service about my address `%s'.\n",
1439                    GNUNET_a2s(addr, addrlen));
1440 #endif
1441   plugin->env->notify_address (plugin->env->cls,
1442                                "tcp",
1443                                buf, addrlen, LEARNED_ADDRESS_EXPIRATION);
1444   return GNUNET_OK;
1445 }
1446
1447
1448 /**
1449  * Send a validation challenge response.
1450  */
1451 static size_t
1452 send_vcr (void *cls,
1453           size_t size,
1454           void *buf)
1455 {
1456   struct ValidationChallengeResponse *vcr = cls;
1457   uint16_t msize;
1458
1459   if (NULL == buf)
1460     {
1461       GNUNET_free (vcr);
1462       return 0;
1463     }
1464   msize = ntohs(vcr->header.size);
1465   GNUNET_assert (size >= msize);
1466   memcpy (buf, vcr, msize);
1467   GNUNET_free (vcr);
1468   return msize;
1469 }
1470
1471
1472 /**
1473  * We've received a PING from this peer via TCP.
1474  * Send back our PONG.
1475  *
1476  * @param cls closure
1477  * @param client identification of the client
1478  * @param message the actual message
1479  */
1480 static void
1481 handle_tcp_ping (void *cls,
1482                  struct GNUNET_SERVER_Client *client,
1483                  const struct GNUNET_MessageHeader *message)
1484 {
1485   struct Plugin *plugin = cls;
1486   const struct ValidationChallengeMessage *vcm;
1487   struct ValidationChallengeResponse *vcr;
1488   uint16_t msize;
1489   size_t addrlen;
1490   void *addr;
1491
1492 #if DEBUG_TRANSPORT
1493   if (GNUNET_OK ==
1494       GNUNET_SERVER_client_get_address (client,
1495                                         (void **) &addr,
1496                                         &addrlen))
1497     {
1498       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1499                   "Processing `%s' from `%s'\n",
1500                   "PING",
1501                   GNUNET_a2s (addr, addrlen));
1502       GNUNET_free (addr);
1503     }
1504 #endif
1505   msize = ntohs (message->size);
1506   if (msize < sizeof (struct ValidationChallengeMessage))
1507     {
1508       GNUNET_break_op (0);
1509       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);  
1510       return;
1511     }
1512   vcm = (const struct ValidationChallengeMessage *) message;
1513   if (0 != memcmp (&vcm->target,
1514                    plugin->env->my_identity, sizeof (struct GNUNET_PeerIdentity)))
1515     {
1516       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1517                   _("Received `%s' message not destined for me!\n"), "PING");
1518       /* TODO: call statistics */
1519       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);  
1520       return;
1521     }
1522   msize -= sizeof (struct ValidationChallengeMessage);
1523   if (GNUNET_OK !=
1524       tcp_plugin_address_suggested (plugin, &vcm[1], msize))
1525     {
1526       GNUNET_break_op (0);
1527       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);  
1528       return;
1529     }
1530   if (GNUNET_OK !=
1531       GNUNET_SERVER_client_get_address (client,
1532                                         &addr,
1533                                         &addrlen))
1534     {
1535       GNUNET_break (0);
1536       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);  
1537       return;
1538     }
1539   vcr = GNUNET_malloc (sizeof (struct ValidationChallengeResponse) + addrlen);
1540   vcr->header.size = htons (sizeof (struct ValidationChallengeResponse) + addrlen);
1541   vcr->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PONG);
1542   vcr->purpose.size =
1543     htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1544            sizeof (uint32_t) +
1545            sizeof ( struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1546            addrlen);
1547   vcr->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING);
1548   vcr->challenge = vcm->challenge;
1549   vcr->signer = *plugin->env->my_public_key;
1550   memcpy (&vcr[1],
1551           addr,
1552           addrlen);
1553   GNUNET_assert (GNUNET_OK ==
1554                  GNUNET_CRYPTO_rsa_sign (plugin->env->my_private_key,
1555                                          &vcr->purpose,
1556                                          &vcr->signature));
1557 #if EXTRA_CHECKS
1558   GNUNET_assert (GNUNET_OK ==
1559                  GNUNET_CRYPTO_rsa_verify
1560                  (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING,
1561                   &vcr->purpose,
1562                   &vcr->signature,
1563                   plugin->env->my_public_key));
1564 #endif
1565   GNUNET_free (addr);
1566   if (NULL ==
1567       GNUNET_SERVER_notify_transmit_ready (client,
1568                                            sizeof (struct ValidationChallengeResponse) + addrlen,
1569                                            GNUNET_TIME_UNIT_SECONDS,
1570                                            &send_vcr,
1571                                            vcr))
1572     {
1573       GNUNET_break (0);
1574       GNUNET_free (vcr);
1575     }
1576   /* after a PING, we always close the connection */
1577   GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);  
1578 }
1579
1580
1581 /**
1582  * Handle PONG-message.
1583  *
1584  * @param plugin handle for this plugin
1585  * @param sender claimed sender of the PONG
1586  * @param message the actual message
1587  */
1588 static void
1589 handle_tcp_pong (void *cls,
1590                  struct GNUNET_SERVER_Client *client,
1591                  const struct GNUNET_MessageHeader *message)
1592 {
1593   struct Plugin *plugin = cls;
1594   const struct ValidationChallengeResponse *vcr;
1595   struct GNUNET_PeerIdentity peer;
1596   char *sender_addr;
1597   size_t addrlen;
1598   const struct sockaddr *addr;
1599   struct sockaddr_in v4;
1600   struct sockaddr_in6 v6;
1601
1602 #if DEBUG_TRANSPORT
1603   struct sockaddr *claddr;
1604
1605   if (GNUNET_OK ==
1606       GNUNET_SERVER_client_get_address (client,
1607                                         (void**) &claddr,
1608                                         &addrlen))
1609     {
1610       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1611                   "Processing `%s' from `%s'\n",
1612                   "PONG",
1613                   GNUNET_a2s (claddr, addrlen));
1614       GNUNET_free (claddr);
1615     }
1616 #endif
1617   if (ntohs(message->size) < sizeof(struct ValidationChallengeResponse))
1618     {
1619       GNUNET_break_op (0);
1620       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);  
1621       return;
1622     }
1623   addrlen = ntohs(message->size) - sizeof(struct ValidationChallengeResponse);
1624   vcr = (const struct ValidationChallengeResponse *) message;
1625   if ( (ntohl(vcr->purpose.size) !=
1626         sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1627         sizeof (uint32_t) +
1628         sizeof ( struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1629         addrlen))
1630     {
1631       GNUNET_break_op (0);
1632       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);  
1633       return;
1634     }
1635   if (GNUNET_OK !=
1636       GNUNET_CRYPTO_rsa_verify
1637       (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING, 
1638        &vcr->purpose,
1639        &vcr->signature, 
1640        &vcr->signer))
1641     {
1642       GNUNET_break_op (0);
1643       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);  
1644       return;
1645     }
1646   GNUNET_CRYPTO_hash (&vcr->signer,
1647                       sizeof(  struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1648                       &peer.hashPubKey);
1649   addr = (const struct sockaddr*) &vcr[1];
1650   if (addrlen == sizeof (struct sockaddr_in))
1651     {
1652       memcpy (&v4, addr, sizeof (struct sockaddr_in));
1653       v4.sin_port = htons(check_port (plugin, ntohs (v4.sin_port)));
1654       sender_addr = GNUNET_strdup (GNUNET_a2s((const struct sockaddr*) &v4,
1655                                               addrlen));
1656     }
1657   else if (addrlen == sizeof (struct sockaddr_in6))
1658     {
1659       memcpy (&v6, addr, sizeof (struct sockaddr_in6));
1660       v6.sin6_port = htons(check_port (plugin, ntohs (v6.sin6_port)));
1661       sender_addr = GNUNET_strdup (GNUNET_a2s((const struct sockaddr*) &v6,
1662                                               addrlen));
1663     }
1664   else
1665     {
1666       GNUNET_break_op (0); 
1667       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);  
1668       return;
1669     }
1670   plugin->env->notify_validation (plugin->env->cls,
1671                                   "tcp",
1672                                   &peer,
1673                                   ntohl(vcr->challenge),
1674                                   sender_addr);
1675   GNUNET_free (sender_addr);
1676   /* after a PONG, we always close the connection */
1677   GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);  
1678 }
1679
1680
1681 /**
1682  * We've received a welcome from this peer via TCP.
1683  * Possibly create a fresh client record and send back
1684  * our welcome.
1685  *
1686  * @param cls closure
1687  * @param client identification of the client
1688  * @param message the actual message
1689  */
1690 static void
1691 handle_tcp_welcome (void *cls,
1692                     struct GNUNET_SERVER_Client *client,
1693                     const struct GNUNET_MessageHeader *message)
1694 {
1695   struct Plugin *plugin = cls;
1696   struct Session *session_c;
1697   const struct WelcomeMessage *wm;
1698   uint16_t msize;
1699   uint32_t addrlen;
1700   size_t alen;
1701   void *vaddr;
1702   const struct sockaddr *addr;
1703
1704   msize = ntohs (message->size);
1705   if (msize < sizeof (struct WelcomeMessage))
1706     {
1707       GNUNET_break_op (0);
1708       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1709       return;
1710     }
1711   wm = (const struct WelcomeMessage *) message;
1712 #if DEBUG_TCP
1713   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1714                    "tcp",
1715                    "Received `%s' message from `%4s/%p'.\n", "WELCOME",
1716                    GNUNET_i2s(&wm->clientIdentity),
1717                    client);
1718 #endif
1719   session_c = find_session_by_client (plugin, client);
1720   if (session_c == NULL)
1721     {
1722       vaddr = NULL;
1723       GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
1724       GNUNET_SERVER_client_keep (client);
1725       session_c = create_session (plugin,
1726                                   &wm->clientIdentity, client, vaddr, alen);
1727 #if DEBUG_TCP
1728       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1729                        "tcp",
1730                        "Creating new session %p for incoming `%s' message.\n",
1731                        session_c, "WELCOME");
1732 #endif
1733       GNUNET_free_non_null (vaddr);
1734       process_pending_messages (session_c);
1735     }
1736   if (session_c->expecting_welcome != GNUNET_YES)
1737     {
1738       GNUNET_break_op (0);
1739       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1740       return;
1741     }
1742   session_c->expecting_welcome = GNUNET_NO;
1743   if (0 < (addrlen = msize - sizeof (struct WelcomeMessage)))
1744     {
1745       addr = (const struct sockaddr *) &wm[1];
1746       tcp_plugin_address_suggested (plugin, addr, addrlen);
1747     }
1748   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1749 }
1750
1751
1752 /**
1753  * Calculate how long we should delay reading from the TCP socket to
1754  * ensure that we stay within our bandwidth limits (push back).
1755  *
1756  * @param session for which client should this be calculated
1757  */
1758 static struct GNUNET_TIME_Relative
1759 calculate_throttle_delay (struct Session *session)
1760 {
1761   struct GNUNET_TIME_Relative ret;
1762   struct GNUNET_TIME_Absolute now;
1763   uint64_t del;
1764   uint64_t avail;
1765   uint64_t excess;
1766
1767   now = GNUNET_TIME_absolute_get ();
1768   del = now.value - session->last_quota_update.value;
1769   if (del > MAX_BANDWIDTH_CARRY)
1770     {
1771       update_quota (session, GNUNET_YES);
1772       del = now.value - session->last_quota_update.value;
1773       GNUNET_assert (del <= MAX_BANDWIDTH_CARRY);
1774     }
1775   if (session->quota_in == 0)
1776     session->quota_in = 1;      /* avoid divison by zero */
1777   avail = del * session->quota_in;
1778   if (avail > session->last_received)
1779     return GNUNET_TIME_UNIT_ZERO;       /* can receive right now */
1780   excess = session->last_received - avail;
1781   ret.value = excess / session->quota_in;
1782   return ret;
1783 }
1784
1785
1786 /**
1787  * Task to signal the server that we can continue
1788  * receiving from the TCP client now.
1789  */
1790 static void
1791 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1792 {
1793   struct Session *session = cls;
1794   GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1795 }
1796
1797
1798 /**
1799  * We've received data for this peer via TCP.  Unbox,
1800  * compute latency and forward.
1801  *
1802  * @param cls closure
1803  * @param client identification of the client
1804  * @param message the actual message
1805  */
1806 static void
1807 handle_tcp_data (void *cls,
1808                  struct GNUNET_SERVER_Client *client,
1809                  const struct GNUNET_MessageHeader *message)
1810 {
1811   struct Plugin *plugin = cls;
1812   struct Session *session;
1813   const struct DataMessage *dm;
1814   uint16_t msize;
1815   const struct GNUNET_MessageHeader *msg;
1816   struct GNUNET_TIME_Relative latency;
1817   struct GNUNET_TIME_Absolute ttime;
1818   struct GNUNET_TIME_Absolute now;
1819   struct GNUNET_TIME_Relative delay;
1820   uint64_t ack_in;
1821
1822   msize = ntohs (message->size);
1823   if ((msize <
1824        sizeof (struct DataMessage) + sizeof (struct GNUNET_MessageHeader)))
1825     {
1826       GNUNET_break_op (0);
1827       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1828       return;
1829     }
1830   session = find_session_by_client (plugin, client);
1831   if ((NULL == session) || (GNUNET_NO != session->expecting_welcome))
1832     {
1833       GNUNET_break_op (0);
1834       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1835       return;
1836     }
1837 #if DEBUG_TCP
1838   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1839                    "tcp", "Receiving %u bytes from `%4s'.\n",
1840                    msize,
1841                    GNUNET_i2s(&session->target));
1842 #endif
1843   dm = (const struct DataMessage *) message;
1844   session->max_in_msg_counter = GNUNET_MAX (session->max_in_msg_counter,
1845                                             GNUNET_ntohll (dm->ack_out));
1846   msg = (const struct GNUNET_MessageHeader *) &dm[1];
1847   if (msize != sizeof (struct DataMessage) + ntohs (msg->size))
1848     {
1849       GNUNET_break_op (0);
1850       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1851       return;
1852     }
1853   /* estimate latency */
1854   ack_in = GNUNET_ntohll (dm->ack_in);
1855   if ((ack_in <= session->out_msg_counter) &&
1856       (session->out_msg_counter - ack_in < ACK_LOG_SIZE))
1857     {
1858       delay = GNUNET_TIME_relative_ntoh (dm->delay);
1859       ttime = session->gen_time[ack_in % ACK_LOG_SIZE];
1860       now = GNUNET_TIME_absolute_get ();
1861       if (delay.value > now.value - ttime.value)
1862         delay.value = 0;        /* not plausible */
1863       /* update (round-trip) latency using ageing; we
1864          use 7:1 so that we can reasonably quickly react
1865          to changes, but not so fast that latency is largely
1866          jitter... */
1867       session->latency_estimate
1868         = ((7 * session->latency_estimate) +
1869            (now.value - ttime.value - delay.value)) / 8;
1870     }
1871   latency.value = (uint64_t) session->latency_estimate;
1872   /* deliver on */
1873 #if DEBUG_TCP
1874   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1875                    "tcp",
1876                    "Forwarding data of type %u to transport service.\n",
1877                    ntohs (msg->type));
1878 #endif
1879   session->service_context
1880     = plugin->env->receive (plugin->env->cls,
1881                             session,
1882                             session->service_context,
1883                             latency, &session->target, msg);
1884   /* update bandwidth used */
1885   session->last_received += msize;
1886   update_quota (session, GNUNET_NO);
1887
1888   delay = calculate_throttle_delay (session);
1889   if (delay.value == 0)
1890     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1891   else
1892     GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1893                                   GNUNET_NO,
1894                                   GNUNET_SCHEDULER_PRIORITY_HIGH,
1895                                   GNUNET_SCHEDULER_NO_TASK,
1896                                   delay, &delayed_done, session);
1897 }
1898
1899
1900 /**
1901  * Handlers for the various TCP messages.
1902  */
1903 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1904   {&handle_tcp_ping, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PING, 0},
1905   {&handle_tcp_pong, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PONG, 0},
1906   {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME, 0},
1907   {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_DATA, 0},
1908   {NULL, NULL, 0, 0}
1909 };
1910
1911
1912 static void
1913 create_tcp_handlers (struct Plugin *plugin)
1914 {
1915   unsigned int i;
1916   plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1917   memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1918   for (i = 0;
1919        i <
1920        sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1921        i++)
1922     plugin->handlers[i].callback_cls = plugin;
1923   GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1924 }
1925
1926
1927 /**
1928  * Functions with this signature are called whenever a peer
1929  * is disconnected on the network level.
1930  *
1931  * @param cls closure
1932  * @param client identification of the client
1933  */
1934 static void
1935 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1936 {
1937   struct Plugin *plugin = cls;
1938   struct Session *session;
1939
1940   session = find_session_by_client (plugin, client);
1941   if (session == NULL)
1942     return;                     /* unknown, nothing to do */
1943 #if DEBUG_TCP
1944   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1945                    "tcp",
1946                    "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1947                    GNUNET_i2s(&session->target),
1948                    (session->connect_addr != NULL) ? 
1949                    GNUNET_a2s(session->connect_addr,
1950                               session->connect_alen) : "*",
1951                    client);
1952 #endif
1953   disconnect_session (session);
1954 }
1955
1956
1957 /**
1958  * Add the IP of our network interface to the list of
1959  * our external IP addresses.
1960  */
1961 static int
1962 process_interfaces (void *cls,
1963                     const char *name,
1964                     int isDefault,
1965                     const struct sockaddr *addr, socklen_t addrlen)
1966 {
1967   struct Plugin *plugin = cls;
1968   int af;
1969   struct sockaddr_in *v4;
1970   struct sockaddr_in6 *v6;
1971
1972   af = addr->sa_family;
1973   if (af == AF_INET)
1974     {
1975       v4 = (struct sockaddr_in *) addr;
1976       v4->sin_port = htons (plugin->adv_port);
1977     }
1978   else
1979     {
1980       GNUNET_assert (af == AF_INET6);
1981       v6 = (struct sockaddr_in6 *) addr;
1982       v6->sin6_port = htons (plugin->adv_port);
1983     }
1984   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1985                    GNUNET_ERROR_TYPE_BULK,
1986                    "tcp", _("Found address `%s' (%s)\n"), 
1987                    GNUNET_a2s(addr, addrlen), name);
1988   plugin->env->notify_address (plugin->env->cls,
1989                                "tcp",
1990                                addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1991   return GNUNET_OK;
1992 }
1993
1994
1995 /**
1996  * Function called by the resolver for each address obtained from DNS
1997  * for our own hostname.  Add the addresses to the list of our
1998  * external IP addresses.
1999  *
2000  * @param cls closure
2001  * @param addr one of the addresses of the host, NULL for the last address
2002  * @param addrlen length of the address
2003  */
2004 static void
2005 process_hostname_ips (void *cls,
2006                       const struct sockaddr *addr, socklen_t addrlen)
2007 {
2008   struct Plugin *plugin = cls;
2009
2010   if (addr == NULL)
2011     return;
2012   process_interfaces (plugin,
2013                       "<hostname>",
2014                       GNUNET_YES,
2015                       addr,
2016                       addrlen);
2017 }
2018
2019
2020 /**
2021  * Entry point for the plugin.
2022  */
2023 void *
2024 libgnunet_plugin_transport_tcp_init (void *cls)
2025 {
2026   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2027   struct GNUNET_TRANSPORT_PluginFunctions *api;
2028   struct Plugin *plugin;
2029   struct GNUNET_SERVICE_Context *service;
2030   unsigned long long aport;
2031   unsigned long long bport;
2032
2033   service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
2034   if (service == NULL)
2035     {
2036       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2037                        "tcp",
2038                        _
2039                        ("Failed to start service for `%s' transport plugin.\n"),
2040                        "tcp");
2041       return NULL;
2042     }
2043   aport = 0;
2044   if ((GNUNET_OK !=
2045        GNUNET_CONFIGURATION_get_value_number (env->cfg,
2046                                               "transport-tcp",
2047                                               "PORT",
2048                                               &bport)) ||
2049       (bport > 65535) ||
2050       ((GNUNET_OK ==
2051         GNUNET_CONFIGURATION_get_value_number (env->cfg,
2052                                                "transport-tcp",
2053                                                "ADVERTISED-PORT",
2054                                                &aport)) && (aport > 65535)))
2055     {
2056       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2057                        "tcp",
2058                        _
2059                        ("Require valid port number for service `%s' in configuration!\n"),
2060                        "transport-tcp");
2061       GNUNET_SERVICE_stop (service);
2062       return NULL;
2063     }
2064   if (aport == 0)
2065     aport = bport;
2066   plugin = GNUNET_malloc (sizeof (struct Plugin));
2067   plugin->open_port = bport;
2068   plugin->adv_port = aport;
2069   plugin->env = env;
2070   plugin->lsock = NULL;
2071   plugin->statistics = NULL;
2072   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2073   api->cls = plugin;
2074   api->validate = &tcp_plugin_validate;
2075   api->send = &tcp_plugin_send;
2076   api->cancel = &tcp_plugin_cancel;
2077   api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2078   api->set_receive_quota = &tcp_plugin_set_receive_quota;
2079   api->address_suggested = &tcp_plugin_address_suggested;
2080   api->cost_estimate = 42;      /* TODO: ATS */
2081   plugin->service = service;
2082   plugin->server = GNUNET_SERVICE_get_server (service);
2083   create_tcp_handlers (plugin);
2084   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2085                    "tcp", _("TCP transport listening on port %u\n"), bport);
2086   if (aport != bport)
2087     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2088                      "tcp",
2089                      _
2090                      ("TCP transport advertises itself as being on port %u\n"),
2091                      aport);
2092   GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
2093                                    plugin);
2094   GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2095   GNUNET_RESOLVER_hostname_resolve (env->sched,
2096                                     env->cfg,
2097                                     AF_UNSPEC,
2098                                     HOSTNAME_RESOLVE_TIMEOUT,
2099                                     &process_hostname_ips, plugin);
2100   return api;
2101 }
2102
2103
2104 /**
2105  * Exit point from the plugin.
2106  */
2107 void *
2108 libgnunet_plugin_transport_tcp_done (void *cls)
2109 {
2110   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2111   struct Plugin *plugin = api->cls;
2112   struct Session *session;
2113
2114   while (NULL != (session = plugin->sessions))
2115     disconnect_session (session);    
2116   GNUNET_SERVICE_stop (plugin->service);
2117   GNUNET_free (plugin->handlers);
2118   GNUNET_free (plugin);
2119   GNUNET_free (api);
2120   return NULL;
2121 }
2122
2123 /* end of plugin_transport_tcp.c */