2 This file is part of GNUnet
3 Copyright (C) 2002--2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file transport/plugin_transport_xt.c
22 * @brief Implementation of the TCP transport service
23 * @author Christian Grothoff
26 #include "gnunet_hello_lib.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_nat_service.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"
38 #define LOG(kind,...) GNUNET_log_from (kind, "transport-xt",__VA_ARGS__)
40 #define PLUGIN_NAME "xt"
43 * How long until we give up on establishing an NAT connection?
46 #define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
49 * Opaque handle that can be used to cancel
50 * a transmit-ready notification.
52 struct GNUNET_CONNECTION_TransmitHandle;
55 * @brief handle for a server
57 struct GNUNET_SERVER_Handle;
60 * @brief opaque handle for a client of the server
62 struct GNUNET_SERVER_Client;
65 * @brief opaque handle server returns for aborting transmission to a client.
67 struct GNUNET_SERVER_TransmitHandle;
70 * @brief handle for a network connection
72 struct GNUNET_CONNECTION_Handle;
75 * @brief handle for a network service
77 struct LEGACY_SERVICE_Context;
81 * Stops a service that was started with #GNUNET_SERVICE_start().
83 * @param srv service to stop
86 LEGACY_SERVICE_stop (struct LEGACY_SERVICE_Context *srv);
91 * Function called to notify a client about the connection begin ready
92 * to queue more data. @a buf will be NULL and @a size zero if the
93 * connection was closed for writing in the meantime.
96 * @param size number of bytes available in @a buf
97 * @param buf where the callee should write the message
98 * @return number of bytes written to @a buf
101 (*GNUNET_CONNECTION_TransmitReadyNotify) (void *cls,
106 * Credentials for UNIX domain sockets.
108 struct GNUNET_CONNECTION_Credentials
111 * UID of the other end of the connection.
116 * GID of the other end of the connection.
123 * Functions with this signature are called whenever a client
124 * is disconnected on the network level.
127 * @param client identification of the client; NULL
128 * for the last call when the server is destroyed
131 (*GNUNET_SERVER_DisconnectCallback) (void *cls,
132 struct GNUNET_SERVER_Client *client);
136 * Functions with this signature are called whenever a client
137 * is connected on the network level.
140 * @param client identification of the client
143 (*GNUNET_SERVER_ConnectCallback) (void *cls,
144 struct GNUNET_SERVER_Client *client);
150 * Function to call for access control checks.
153 * @param ucred credentials, if available, otherwise NULL
154 * @param addr address
155 * @param addrlen length of address
156 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
157 * for unknown address family (will be denied).
160 (*GNUNET_CONNECTION_AccessCheck) (void *cls,
162 GNUNET_CONNECTION_Credentials *
164 const struct sockaddr * addr,
168 * Callback function for data received from the network. Note that
169 * both "available" and "err" would be 0 if the read simply timed out.
172 * @param buf pointer to received data
173 * @param available number of bytes availabe in "buf",
174 * possibly 0 (on errors)
175 * @param addr address of the sender
176 * @param addrlen size of addr
177 * @param errCode value of errno (on errors receiving)
180 (*GNUNET_CONNECTION_Receiver) (void *cls, const void *buf,
182 const struct sockaddr * addr,
183 socklen_t addrlen, int errCode);
188 * Close the connection and free associated resources. There must
189 * not be any pending requests for reading or writing to the
190 * connection at this time.
192 * @param connection connection to destroy
195 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection);
199 * Signature of a function to create a custom tokenizer.
201 * @param cls closure from #GNUNET_SERVER_set_callbacks
202 * @param client handle to client the tokenzier will be used for
203 * @return handle to custom tokenizer ('mst')
206 (*GNUNET_SERVER_MstCreateCallback) (void *cls,
207 struct GNUNET_SERVER_Client *client);
211 * Signature of a function to destroy a custom tokenizer.
213 * @param cls closure from #GNUNET_SERVER_set_callbacks
214 * @param mst custom tokenizer handle
217 (*GNUNET_SERVER_MstDestroyCallback) (void *cls,
221 * Signature of a function to receive data for a custom tokenizer.
223 * @param cls closure from #GNUNET_SERVER_set_callbacks
224 * @param mst custom tokenizer handle
225 * @param client_identity ID of client for which this is a buffer,
226 * can be NULL (will be passed back to 'cb')
227 * @param buf input data to add
228 * @param size number of bytes in @a buf
229 * @param purge should any excess bytes in the buffer be discarded
230 * (i.e. for packet-based services like UDP)
231 * @param one_shot only call callback once, keep rest of message in buffer
232 * @return #GNUNET_OK if we are done processing (need more data)
233 * #GNUNET_NO if one_shot was set and we have another message ready
234 * #GNUNET_SYSERR if the data stream is corrupt
237 (*GNUNET_SERVER_MstReceiveCallback) (void *cls, void *mst,
238 struct GNUNET_SERVER_Client *client,
244 * Functions with this signature are called whenever a message is
248 * @param client identification of the client
249 * @param message the actual message
252 (*GNUNET_SERVER_MessageCallback) (void *cls,
253 struct GNUNET_SERVER_Client *client,
254 const struct GNUNET_MessageHeader *message);
257 * Message handler. Each struct specifies how to handle on particular
258 * type of message received.
260 struct GNUNET_SERVER_MessageHandler
263 * Function to call for messages of "type".
265 GNUNET_SERVER_MessageCallback callback;
268 * Closure argument for @e callback.
273 * Type of the message this handler covers.
278 * Expected size of messages of this type. Use 0 for
279 * variable-size. If non-zero, messages of the given
280 * type will be discarded (and the connection closed)
281 * if they do not have the right size.
283 uint16_t expected_size;
289 * Options for the service (bitmask).
291 enum LEGACY_SERVICE_Options
294 * Use defaults. Terminates all client connections and the listen
295 * sockets immediately upon receiving the shutdown signal.
297 LEGACY_SERVICE_OPTION_NONE = 0,
300 * Do not trigger server shutdown on signal at all; instead, allow
301 * for the user to terminate the server explicitly when needed
302 * by calling #LEGACY_SERVICE_shutdown().
304 LEGACY_SERVICE_OPTION_MANUAL_SHUTDOWN = 1,
307 * Trigger a SOFT server shutdown on signals, allowing active
308 * non-monitor clients to complete their transactions.
310 LEGACY_SERVICE_OPTION_SOFT_SHUTDOWN = 2
316 * Ask the server to disconnect from the given client. This is the
317 * same as passing #GNUNET_SYSERR to #GNUNET_SERVER_receive_done,
318 * except that it allows dropping of a client even when not handling a
319 * message from that client.
321 * @param client the client to disconnect from
324 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
327 * Return user context associated with the given client.
328 * Note: you should probably use the macro (call without the underscore).
330 * @param client client to query
331 * @param size number of bytes in user context struct (for verification only)
332 * @return pointer to user context
335 GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client,
340 * Functions with this signature are called whenever a
341 * complete message is received by the tokenizer.
343 * Do not call #GNUNET_SERVER_mst_destroy from within
344 * the scope of this callback.
347 * @param client identification of the client
348 * @param message the actual message
349 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
352 (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls,
354 const struct GNUNET_MessageHeader *message);
358 * Create a message stream tokenizer.
360 * @param cb function to call on completed messages
361 * @param cb_cls closure for @a cb
362 * @return handle to tokenizer
364 struct GNUNET_SERVER_MessageStreamTokenizer *
365 GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
369 * Add incoming data to the receive buffer and call the
370 * callback for all complete messages.
372 * @param mst tokenizer to use
373 * @param client_identity ID of client for which this is a buffer,
374 * can be NULL (will be passed back to 'cb')
375 * @param buf input data to add
376 * @param size number of bytes in @a buf
377 * @param purge should any excess bytes in the buffer be discarded
378 * (i.e. for packet-based services like UDP)
379 * @param one_shot only call callback once, keep rest of message in buffer
380 * @return #GNUNET_OK if we are done processing (need more data)
381 * #GNUNET_NO if one_shot was set and we have another message ready
382 * #GNUNET_SYSERR if the data stream is corrupt
385 GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
386 void *client_identity,
387 const char *buf, size_t size,
388 int purge, int one_shot);
393 * Destroys a tokenizer.
395 * @param mst tokenizer to destroy
398 GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
402 * Set user context to be associated with the given client.
403 * Note: you should probably use the macro (call without the underscore).
405 * @param client client to query
406 * @param ptr pointer to user context
407 * @param size number of bytes in user context struct (for verification only)
410 GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client,
414 * Return user context associated with the given client.
416 * @param client client to query
417 * @param type expected return type (i.e. 'struct Foo')
418 * @return pointer to user context of type 'type *'.
420 #define GNUNET_SERVER_client_get_user_context(client,type) \
421 (type *) GNUNET_SERVER_client_get_user_context_ (client, sizeof (type))
424 * Set user context to be associated with the given client.
426 * @param client client to query
427 * @param value pointer to user context
429 #define GNUNET_SERVER_client_set_user_context(client,value) \
430 GNUNET_SERVER_client_set_user_context_ (client, value, sizeof (*value))
435 * Notify us when the server has enough space to transmit
436 * a message of the given size to the given client.
438 * @param client client to transmit message to
439 * @param size requested amount of buffer space
440 * @param timeout after how long should we give up (and call
441 * notify with buf NULL and size 0)?
442 * @param callback function to call when space is available
443 * @param callback_cls closure for @a callback
444 * @return non-NULL if the notify callback was queued; can be used
445 * to cancel the request using
446 * #GNUNET_SERVER_notify_transmit_ready_cancel.
447 * NULL if we are already going to notify someone else (busy)
449 struct GNUNET_SERVER_TransmitHandle *
450 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
452 struct GNUNET_TIME_Relative timeout,
453 GNUNET_CONNECTION_TransmitReadyNotify callback,
457 * Abort transmission request.
459 * @param th request to abort
462 GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th);
468 * Notify the server that the given client handle should
469 * be kept (keeps the connection up if possible, increments
470 * the internal reference counter).
472 * @param client the client to keep
475 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
479 * Notify the server that the given client handle is no
480 * longer required. Decrements the reference counter. If
481 * that counter reaches zero an inactive connection maybe
484 * @param client the client to drop
487 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
491 * Function called by the service's run
492 * method to run service-specific setup code.
495 * @param server the initialized server
496 * @param cfg configuration to use
499 (*LEGACY_SERVICE_Main) (void *cls,
500 struct GNUNET_SERVER_Handle *server,
501 const struct GNUNET_CONFIGURATION_Handle *cfg);
506 * Suspend accepting connections from the listen socket temporarily.
507 * Resume activity using #GNUNET_SERVER_resume.
509 * @param server server to stop accepting connections.
512 GNUNET_SERVER_suspend (struct GNUNET_SERVER_Handle *server);
515 * Notify us when the server has enough space to transmit
516 * a message of the given size to the given client.
518 * @param client client to transmit message to
519 * @param size requested amount of buffer space
520 * @param timeout after how long should we give up (and call
521 * notify with buf NULL and size 0)?
522 * @param callback function to call when space is available
523 * @param callback_cls closure for @a callback
524 * @return non-NULL if the notify callback was queued; can be used
525 * to cancel the request using
526 * #GNUNET_SERVER_notify_transmit_ready_cancel.
527 * NULL if we are already going to notify someone else (busy)
529 struct GNUNET_SERVER_TransmitHandle *
530 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
532 struct GNUNET_TIME_Relative timeout,
533 GNUNET_CONNECTION_TransmitReadyNotify callback,
538 * Add a TCP socket-based connection to the set of handles managed by
539 * this server. Use this function for outgoing (P2P) connections that
540 * we initiated (and where this server should process incoming
543 * @param server the server to use
544 * @param connection the connection to manage (client must
545 * stop using this connection from now on)
546 * @return the client handle
548 struct GNUNET_SERVER_Client *
549 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
550 struct GNUNET_CONNECTION_Handle *connection);
554 * Resume accepting connections from the listen socket.
556 * @param server server to resume accepting connections.
559 GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server);
562 * Free resources held by this server.
564 * @param server server to destroy
567 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server);
572 #include "tcp_connection_legacy.c"
573 #include "tcp_server_mst_legacy.c"
574 #include "tcp_server_legacy.c"
575 #include "tcp_service_legacy.c"
577 GNUNET_NETWORK_STRUCT_BEGIN
580 * Initial handshake message for a session.
582 struct WelcomeMessage
585 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
587 struct GNUNET_MessageHeader header;
590 * Identity of the node connecting (TCP client)
592 struct GNUNET_PeerIdentity clientIdentity;
597 * Basically a WELCOME message, but with the purpose
598 * of giving the waiting peer a client handle to use
600 struct TCP_NAT_ProbeMessage
603 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
605 struct GNUNET_MessageHeader header;
608 * Identity of the sender of the message.
610 struct GNUNET_PeerIdentity clientIdentity;
613 GNUNET_NETWORK_STRUCT_END
616 * Context for sending a NAT probe via TCP.
618 struct TCPProbeContext
622 * Active probes are kept in a DLL.
624 struct TCPProbeContext *next;
627 * Active probes are kept in a DLL.
629 struct TCPProbeContext *prev;
634 struct GNUNET_CONNECTION_Handle *sock;
637 * Message to be sent.
639 struct TCP_NAT_ProbeMessage message;
642 * Handle to the transmission.
644 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
647 * Transport plugin handle.
649 struct Plugin *plugin;
653 * Bits in the `options` field of TCP addresses.
655 enum TcpAddressOptions
661 TCP_OPTIONS_NONE = 0,
664 * See #HTTP_OPTIONS_VERIFY_CERTIFICATE.
666 TCP_OPTIONS_RESERVED = 1,
669 * Enable TCP Stealth-style port knocking.
671 TCP_OPTIONS_TCP_STEALTH = 2
674 GNUNET_NETWORK_STRUCT_BEGIN
677 * Network format for IPv4 addresses.
679 struct IPv4TcpAddress
682 * Optional options and flags for this address,
683 * see `enum TcpAddressOptions`
685 uint32_t options GNUNET_PACKED;
688 * IPv4 address, in network byte order.
690 uint32_t ipv4_addr GNUNET_PACKED;
693 * Port number, in network byte order.
695 uint16_t t4_port GNUNET_PACKED;
700 * Network format for IPv6 addresses.
702 struct IPv6TcpAddress
705 * Optional flags for this address
706 * see `enum TcpAddressOptions`
708 uint32_t options GNUNET_PACKED;
713 struct in6_addr ipv6_addr GNUNET_PACKED;
716 * Port number, in network byte order.
718 uint16_t t6_port GNUNET_PACKED;
721 GNUNET_NETWORK_STRUCT_END
724 * Encapsulation of all of the state of the plugin.
729 * Information kept for each message that is yet to
732 struct PendingMessage
736 * This is a doubly-linked list.
738 struct PendingMessage *next;
741 * This is a doubly-linked list.
743 struct PendingMessage *prev;
746 * The pending message
751 * Continuation function to call once the message
752 * has been sent. Can be NULL if there is no
753 * continuation to call.
755 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
758 * Closure for @e transmit_cont.
760 void *transmit_cont_cls;
763 * Timeout value for the pending message.
765 struct GNUNET_TIME_Absolute timeout;
768 * So that the gnunet-service-transport can group messages together,
769 * these pending messages need to accept a message buffer and size
770 * instead of just a `struct GNUNET_MessageHeader`.
777 * Session handle for TCP connections.
779 struct GNUNET_ATS_Session
782 * To whom are we talking to (set to our identity
783 * if we are still waiting for the welcome message)
785 struct GNUNET_PeerIdentity target;
788 * Pointer to the global plugin struct.
790 struct Plugin *plugin;
793 * The client (used to identify this connection)
795 struct GNUNET_SERVER_Client *client;
798 * Task cleaning up a NAT client connection establishment attempt;
800 struct GNUNET_SCHEDULER_Task *nat_connection_timeout;
803 * Messages currently pending for transmission
804 * to this peer, if any.
806 struct PendingMessage *pending_messages_head;
809 * Messages currently pending for transmission
810 * to this peer, if any.
812 struct PendingMessage *pending_messages_tail;
815 * Handle for pending transmission request.
817 struct GNUNET_SERVER_TransmitHandle *transmit_handle;
820 * Address of the other peer.
822 struct GNUNET_HELLO_Address *address;
825 * ID of task used to delay receiving more to throttle sender.
827 struct GNUNET_SCHEDULER_Task *receive_delay_task;
830 * Session timeout task
832 struct GNUNET_SCHEDULER_Task *timeout_task;
835 * When will this session time out?
837 struct GNUNET_TIME_Absolute timeout;
840 * When will we continue to read from the socket?
841 * (used to enforce inbound quota).
843 struct GNUNET_TIME_Absolute receive_delay;
846 * Last activity on this connection. Used to select preferred
849 struct GNUNET_TIME_Absolute last_activity;
852 * Number of bytes waiting for transmission to this peer.
854 unsigned long long bytes_in_queue;
857 * Number of messages waiting for transmission to this peer.
859 unsigned int msgs_in_queue;
862 * Network type of the address.
864 enum GNUNET_NetworkType scope;
867 * Are we still expecting the welcome message? (#GNUNET_YES/#GNUNET_NO)
869 int expecting_welcome;
872 * Was this session created using NAT traversal?
880 * Context for address to string conversion, closure
881 * for #append_port().
883 struct PrettyPrinterContext
888 struct PrettyPrinterContext *next;
893 struct PrettyPrinterContext *prev;
898 struct Plugin *plugin;
903 struct GNUNET_SCHEDULER_Task *timeout_task;
908 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
911 * Function to call with the result.
913 GNUNET_TRANSPORT_AddressStringCallback asc;
916 * Clsoure for @e asc.
931 * Port to add after the IP address.
938 * Encapsulation of all of the state of the plugin.
945 struct GNUNET_TRANSPORT_PluginEnvironment *env;
950 struct GNUNET_CONNECTION_Handle *lsock;
953 * Our handle to the NAT module.
955 struct GNUNET_NAT_Handle *nat;
958 * Map from peer identities to sessions for the given peer.
960 struct GNUNET_CONTAINER_MultiPeerMap *sessionmap;
963 * Handle to the network service.
965 struct LEGACY_SERVICE_Context *service;
968 * Handle to the server for this service.
970 struct GNUNET_SERVER_Handle *server;
973 * Copy of the handler array where the closures are
974 * set to this struct's instance.
976 struct GNUNET_SERVER_MessageHandler *handlers;
979 * Map of peers we have tried to contact behind a NAT
981 struct GNUNET_CONTAINER_MultiPeerMap *nat_wait_conns;
984 * List of active TCP probes.
986 struct TCPProbeContext *probe_head;
989 * List of active TCP probes.
991 struct TCPProbeContext *probe_tail;
994 * Function to call about session status changes.
996 GNUNET_TRANSPORT_SessionInfoCallback sic;
999 * Closure for @e sic.
1004 * ID of task used to update our addresses when one expires.
1006 struct GNUNET_SCHEDULER_Task *address_update_task;
1009 * Running pretty printers: head
1011 struct PrettyPrinterContext *ppc_dll_head;
1014 * Running pretty printers: tail
1016 struct PrettyPrinterContext *ppc_dll_tail;
1019 * Welcome message used by this peer.
1021 struct WelcomeMessage my_welcome;
1024 * How many more TCP sessions are we allowed to open right now?
1026 unsigned long long max_connections;
1029 * How many more TCP sessions do we have right now?
1031 unsigned long long cur_connections;
1039 * Port that we are actually listening on.
1044 * Port that the user said we would have visible to the
1045 * rest of the world.
1053 * Get the list of addresses that a server for the given service
1056 * @param service_name name of the service
1057 * @param cfg configuration (which specifies the addresses)
1058 * @param addrs set (call by reference) to an array of pointers to the
1059 * addresses the server should bind to and listen on; the
1060 * array will be NULL-terminated (on success)
1061 * @param addr_lens set (call by reference) to an array of the lengths
1062 * of the respective `struct sockaddr` struct in the @a addrs
1063 * array (on success)
1064 * @return number of addresses found on success,
1065 * #GNUNET_SYSERR if the configuration
1066 * did not specify reasonable finding information or
1067 * if it specified a hostname that could not be resolved;
1068 * #GNUNET_NO if the number of addresses configured is
1069 * zero (in this case, `*addrs` and `*addr_lens` will be
1073 get_server_addresses (const char *service_name,
1074 const struct GNUNET_CONFIGURATION_Handle *cfg,
1075 struct sockaddr ***addrs,
1076 socklen_t ** addr_lens)
1079 struct GNUNET_NETWORK_Handle *desc;
1080 unsigned long long port;
1082 struct addrinfo hints;
1083 struct addrinfo *res;
1084 struct addrinfo *pos;
1085 struct addrinfo *next;
1090 struct sockaddr **saddrs;
1091 socklen_t *saddrlens;
1097 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
1099 if (GNUNET_SYSERR ==
1101 GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
1102 return GNUNET_SYSERR;
1105 disablev6 = GNUNET_NO;
1109 /* probe IPv6 support */
1110 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
1113 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
1116 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1117 return GNUNET_SYSERR;
1119 LOG (GNUNET_ERROR_TYPE_INFO,
1120 _("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
1121 service_name, STRERROR (errno));
1122 disablev6 = GNUNET_YES;
1126 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
1132 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
1135 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
1138 LOG (GNUNET_ERROR_TYPE_ERROR,
1139 _("Require valid port number for service `%s' in configuration!\n"),
1144 LOG (GNUNET_ERROR_TYPE_ERROR,
1145 _("Require valid port number for service `%s' in configuration!\n"),
1147 return GNUNET_SYSERR;
1151 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
1153 GNUNET_break (GNUNET_OK ==
1154 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
1155 "BINDTO", &hostname));
1161 abstract = GNUNET_NO;
1164 GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
1166 GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "UNIXPATH",
1168 (0 < strlen (unixpath)))
1170 /* probe UNIX support */
1171 struct sockaddr_un s_un;
1173 if (strlen (unixpath) >= sizeof (s_un.sun_path))
1175 LOG (GNUNET_ERROR_TYPE_WARNING,
1176 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
1177 (unsigned long long) sizeof (s_un.sun_path));
1178 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
1179 LOG (GNUNET_ERROR_TYPE_INFO,
1180 _("Using `%s' instead\n"),
1184 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1186 "USE_ABSTRACT_SOCKETS");
1187 if (GNUNET_SYSERR == abstract)
1188 abstract = GNUNET_NO;
1190 if ((GNUNET_YES != abstract)
1192 GNUNET_DISK_directory_create_for_file (unixpath)))
1193 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1197 if (NULL != unixpath)
1199 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
1202 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
1205 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1206 GNUNET_free_non_null (hostname);
1207 GNUNET_free (unixpath);
1208 return GNUNET_SYSERR;
1210 LOG (GNUNET_ERROR_TYPE_INFO,
1211 _("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
1214 GNUNET_free (unixpath);
1219 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
1225 if ((0 == port) && (NULL == unixpath))
1227 LOG (GNUNET_ERROR_TYPE_ERROR,
1228 _("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
1230 GNUNET_free_non_null (hostname);
1231 return GNUNET_SYSERR;
1235 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
1236 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
1237 add_unixpath (saddrs, saddrlens, unixpath, abstract);
1238 GNUNET_free_non_null (unixpath);
1239 GNUNET_free_non_null (hostname);
1241 *addr_lens = saddrlens;
1245 if (NULL != hostname)
1247 LOG (GNUNET_ERROR_TYPE_DEBUG,
1248 "Resolving `%s' since that is where `%s' will bind to.\n",
1251 memset (&hints, 0, sizeof (struct addrinfo));
1253 hints.ai_family = AF_INET;
1254 hints.ai_protocol = IPPROTO_TCP;
1255 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1258 LOG (GNUNET_ERROR_TYPE_ERROR,
1259 _("Failed to resolve `%s': %s\n"),
1261 gai_strerror (ret));
1262 GNUNET_free (hostname);
1263 GNUNET_free_non_null (unixpath);
1264 return GNUNET_SYSERR;
1268 while (NULL != (pos = next))
1270 next = pos->ai_next;
1271 if ((disablev6) && (pos->ai_family == AF_INET6))
1277 LOG (GNUNET_ERROR_TYPE_ERROR,
1278 _("Failed to find %saddress for `%s'.\n"),
1279 disablev6 ? "IPv4 " : "",
1282 GNUNET_free (hostname);
1283 GNUNET_free_non_null (unixpath);
1284 return GNUNET_SYSERR;
1287 if (NULL != unixpath)
1289 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1290 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1292 if (NULL != unixpath)
1294 add_unixpath (saddrs, saddrlens, unixpath, abstract);
1298 while (NULL != (pos = next))
1300 next = pos->ai_next;
1301 if ((disablev6) && (AF_INET6 == pos->ai_family))
1303 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
1304 continue; /* not TCP */
1305 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
1306 continue; /* huh? */
1307 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
1308 service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
1309 if (AF_INET == pos->ai_family)
1311 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
1312 saddrlens[i] = pos->ai_addrlen;
1313 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1314 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1315 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1319 GNUNET_assert (AF_INET6 == pos->ai_family);
1320 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
1321 saddrlens[i] = pos->ai_addrlen;
1322 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1323 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1324 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1328 GNUNET_free (hostname);
1334 /* will bind against everything, just set port */
1339 if (NULL != unixpath)
1342 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1343 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1344 if (NULL != unixpath)
1346 add_unixpath (saddrs, saddrlens, unixpath, abstract);
1349 saddrlens[i] = sizeof (struct sockaddr_in);
1350 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1351 #if HAVE_SOCKADDR_IN_SIN_LEN
1352 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1354 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1355 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1361 if (NULL != unixpath)
1363 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1364 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1366 if (NULL != unixpath)
1368 add_unixpath (saddrs, saddrlens, unixpath, abstract);
1371 saddrlens[i] = sizeof (struct sockaddr_in6);
1372 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1373 #if HAVE_SOCKADDR_IN_SIN_LEN
1374 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1376 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1377 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1379 saddrlens[i] = sizeof (struct sockaddr_in);
1380 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1381 #if HAVE_SOCKADDR_IN_SIN_LEN
1382 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1384 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1385 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1388 GNUNET_free_non_null (unixpath);
1390 *addr_lens = saddrlens;
1393 /* end ancient copy-and-paste */
1397 * If a session monitor is attached, notify it about the new
1400 * @param plugin our plugin
1401 * @param session session that changed state
1402 * @param state new state of the session
1405 notify_session_monitor (struct Plugin *plugin,
1406 struct GNUNET_ATS_Session *session,
1407 enum GNUNET_TRANSPORT_SessionState state)
1409 struct GNUNET_TRANSPORT_SessionInfo info;
1411 if (NULL == plugin->sic)
1413 memset (&info, 0, sizeof (info));
1415 info.is_inbound = GNUNET_HELLO_address_check_option (session->address,
1416 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1417 info.num_msg_pending = session->msgs_in_queue;
1418 info.num_bytes_pending = session->bytes_in_queue;
1419 if (NULL != session->receive_delay_task)
1420 info.receive_delay = session->receive_delay;
1421 info.session_timeout = session->timeout;
1422 info.address = session->address;
1423 plugin->sic (plugin->sic_cls,
1430 * Our external IP address/port mapping has changed.
1432 * @param cls closure, the `struct Plugin`
1433 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
1434 * the previous (now invalid) one
1435 * @param ac address class the address belongs to
1436 * @param addr either the previous or the new public IP address
1437 * @param addrlen actual length of @a addr
1440 tcp_nat_port_map_callback (void *cls,
1442 enum GNUNET_NAT_AddressClass ac,
1443 const struct sockaddr *addr,
1446 struct Plugin *plugin = cls;
1447 struct GNUNET_HELLO_Address *address;
1448 struct IPv4TcpAddress t4;
1449 struct IPv6TcpAddress t6;
1453 if (GNUNET_NAT_AC_LOOPBACK == ac)
1455 if (GNUNET_NAT_AC_LAN == ac)
1457 if (GNUNET_NAT_AC_LAN_PRIVATE == ac)
1459 LOG (GNUNET_ERROR_TYPE_INFO,
1460 "NAT notification to %s address `%s'\n",
1461 (GNUNET_YES == add_remove) ? "add" : "remove",
1462 GNUNET_a2s (addr, addrlen));
1463 /* convert 'addr' to our internal format */
1464 switch (addr->sa_family)
1467 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
1468 memset (&t4, 0, sizeof(t4));
1469 t4.options = htonl (plugin->myoptions);
1470 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1471 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
1476 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
1477 memset (&t6, 0, sizeof(t6));
1478 GNUNET_memcpy (&t6.ipv6_addr,
1479 &((struct sockaddr_in6 *) addr)->sin6_addr,
1480 sizeof(struct in6_addr));
1481 t6.options = htonl (plugin->myoptions);
1482 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1490 /* modify our published address list */
1491 GNUNET_assert ((args == sizeof (struct IPv4TcpAddress)) ||
1492 (args == sizeof (struct IPv6TcpAddress)));
1493 /* TODO: use 'ac' here in the future... */
1494 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1498 GNUNET_HELLO_ADDRESS_INFO_NONE);
1499 plugin->env->notify_address (plugin->env->cls,
1502 GNUNET_HELLO_address_free (address);
1507 * Function called for a quick conversion of the binary address to
1508 * a numeric address. Note that the caller must not free the
1509 * address and that the next call to this function is allowed
1510 * to override the address again.
1512 * @param cls closure (`struct Plugin*`)
1513 * @param addr binary address
1514 * @param addrlen length of @a addr
1515 * @return string representing the same address
1518 tcp_plugin_address_to_string (void *cls,
1522 static char rbuf[INET6_ADDRSTRLEN + 12];
1523 char buf[INET6_ADDRSTRLEN];
1527 const struct IPv4TcpAddress *t4;
1528 const struct IPv6TcpAddress *t6;
1535 case sizeof(struct IPv6TcpAddress):
1538 port = ntohs (t6->t6_port);
1539 options = ntohl (t6->options);
1540 GNUNET_memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
1543 case sizeof(struct IPv4TcpAddress):
1546 port = ntohs (t4->t4_port);
1547 options = ntohl (t4->options);
1548 GNUNET_memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
1552 LOG (GNUNET_ERROR_TYPE_WARNING,
1553 _("Unexpected address length: %u bytes\n"),
1554 (unsigned int) addrlen);
1557 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
1559 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1563 GNUNET_snprintf (rbuf, sizeof(rbuf),
1564 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
1574 * Function called to convert a string address to
1577 * @param cls closure (`struct Plugin*`)
1578 * @param addr string address
1579 * @param addrlen length of the address
1580 * @param buf location to store the buffer
1581 * @param added location to store the number of bytes in the buffer.
1582 * If the function returns #GNUNET_SYSERR, its contents are undefined.
1583 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1586 tcp_plugin_string_to_address (void *cls,
1592 struct sockaddr_storage socket_address;
1598 /* Format tcp.options.address:port */
1602 if ((NULL == addr) || (0 == addrlen))
1605 return GNUNET_SYSERR;
1607 if ('\0' != addr[addrlen - 1])
1610 return GNUNET_SYSERR;
1612 if (strlen (addr) != addrlen - 1)
1615 return GNUNET_SYSERR;
1617 plugin = GNUNET_strdup (addr);
1618 optionstr = strchr (plugin, '.');
1619 if (NULL == optionstr)
1622 GNUNET_free(plugin);
1623 return GNUNET_SYSERR;
1625 optionstr[0] = '\0';
1627 options = atol (optionstr);
1628 address = strchr (optionstr, '.');
1629 if (NULL == address)
1632 GNUNET_free(plugin);
1633 return GNUNET_SYSERR;
1639 GNUNET_STRINGS_to_address_ip (address,
1644 GNUNET_free(plugin);
1645 return GNUNET_SYSERR;
1648 GNUNET_free(plugin);
1649 switch (socket_address.ss_family)
1653 struct IPv4TcpAddress *t4;
1654 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
1655 t4 = GNUNET_new (struct IPv4TcpAddress);
1656 t4->options = htonl (options);
1657 t4->ipv4_addr = in4->sin_addr.s_addr;
1658 t4->t4_port = in4->sin_port;
1660 *added = sizeof(struct IPv4TcpAddress);
1665 struct IPv6TcpAddress *t6;
1666 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
1667 t6 = GNUNET_new (struct IPv6TcpAddress);
1668 t6->options = htonl (options);
1669 t6->ipv6_addr = in6->sin6_addr;
1670 t6->t6_port = in6->sin6_port;
1672 *added = sizeof(struct IPv6TcpAddress);
1676 return GNUNET_SYSERR;
1682 * Find the session handle for the given client.
1683 * Currently uses both the hashmap and the client
1684 * context, as the client context is new and the
1685 * logic still needs to be tested.
1687 * @param plugin the plugin
1688 * @param client which client to find the session handle for
1689 * @return NULL if no matching session exists
1691 static struct GNUNET_ATS_Session *
1692 lookup_session_by_client (struct Plugin *plugin,
1693 struct GNUNET_SERVER_Client *client)
1695 return GNUNET_SERVER_client_get_user_context (client,
1696 struct GNUNET_ATS_Session);
1701 * Functions with this signature are called whenever we need
1702 * to close a session due to a disconnect or failure to
1703 * establish a connection.
1705 * @param cls the `struct Plugin`
1706 * @param session session to close down
1707 * @return #GNUNET_OK on success
1710 tcp_plugin_disconnect_session (void *cls,
1711 struct GNUNET_ATS_Session *session)
1713 struct Plugin *plugin = cls;
1714 struct PendingMessage *pm;
1716 LOG (GNUNET_ERROR_TYPE_DEBUG,
1717 "Disconnecting session of peer `%s' address `%s'\n",
1718 GNUNET_i2s (&session->target),
1719 tcp_plugin_address_to_string (session->plugin,
1720 session->address->address,
1721 session->address->address_length));
1723 if (NULL != session->timeout_task)
1725 GNUNET_SCHEDULER_cancel (session->timeout_task);
1726 session->timeout_task = NULL;
1727 session->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
1731 GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap,
1735 GNUNET_STATISTICS_update (session->plugin->env->stats,
1736 gettext_noop ("# TCP sessions active"),
1742 GNUNET_assert (GNUNET_YES ==
1743 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
1747 if (NULL != session->client)
1748 GNUNET_SERVER_client_set_user_context (session->client,
1751 /* clean up state */
1752 if (NULL != session->transmit_handle)
1754 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
1755 session->transmit_handle = NULL;
1757 session->plugin->env->session_end (session->plugin->env->cls,
1761 if (NULL != session->nat_connection_timeout)
1763 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1764 session->nat_connection_timeout = NULL;
1767 while (NULL != (pm = session->pending_messages_head))
1769 LOG (GNUNET_ERROR_TYPE_DEBUG,
1770 (NULL != pm->transmit_cont)
1771 ? "Could not deliver message to `%s' at %s.\n"
1772 : "Could not deliver message to `%s' at %s, notifying.\n",
1773 GNUNET_i2s (&session->target),
1774 tcp_plugin_address_to_string (session->plugin,
1775 session->address->address,
1776 session->address->address_length));
1777 GNUNET_STATISTICS_update (session->plugin->env->stats,
1778 gettext_noop ("# bytes currently in TCP buffers"),
1779 -(int64_t) pm->message_size, GNUNET_NO);
1780 GNUNET_STATISTICS_update (session->plugin->env->stats,
1781 gettext_noop ("# bytes discarded by TCP (disconnect)"),
1784 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1785 session->pending_messages_tail,
1787 GNUNET_assert (0 < session->msgs_in_queue);
1788 session->msgs_in_queue--;
1789 GNUNET_assert (pm->message_size <= session->bytes_in_queue);
1790 session->bytes_in_queue -= pm->message_size;
1791 if (NULL != pm->transmit_cont)
1792 pm->transmit_cont (pm->transmit_cont_cls,
1799 GNUNET_assert (0 == session->msgs_in_queue);
1800 GNUNET_assert (0 == session->bytes_in_queue);
1801 notify_session_monitor (session->plugin,
1803 GNUNET_TRANSPORT_SS_DONE);
1805 if (NULL != session->receive_delay_task)
1807 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1808 session->receive_delay_task = NULL;
1810 if (NULL != session->client)
1812 GNUNET_SERVER_client_disconnect (session->client);
1813 session->client = NULL;
1815 GNUNET_HELLO_address_free (session->address);
1816 GNUNET_assert (NULL == session->transmit_handle);
1817 GNUNET_free (session);
1823 * Function that is called to get the keepalive factor.
1824 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1825 * calculate the interval between keepalive packets.
1827 * @param cls closure with the `struct Plugin`
1828 * @return keepalive factor
1831 tcp_plugin_query_keepalive_factor (void *cls)
1838 * Session was idle for too long, so disconnect it
1840 * @param cls the `struct GNUNET_ATS_Session` of the idle session
1843 session_timeout (void *cls)
1845 struct GNUNET_ATS_Session *s = cls;
1846 struct GNUNET_TIME_Relative left;
1848 s->timeout_task = NULL;
1849 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
1850 if (0 != left.rel_value_us)
1852 /* not actually our turn yet, but let's at least update
1853 the monitor, it may think we're about to die ... */
1854 notify_session_monitor (s->plugin,
1856 GNUNET_TRANSPORT_SS_UPDATE);
1857 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
1862 LOG (GNUNET_ERROR_TYPE_DEBUG,
1863 "Session %p was idle for %s, disconnecting\n",
1865 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1867 /* call session destroy function */
1868 tcp_plugin_disconnect_session (s->plugin,
1874 * Increment session timeout due to activity.
1876 * @param s session to increment timeout for
1879 reschedule_session_timeout (struct GNUNET_ATS_Session *s)
1881 GNUNET_assert (NULL != s->timeout_task);
1882 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1887 * Create a new session. Also queues a welcome message.
1889 * @param plugin the plugin
1890 * @param address the address to create the session for
1891 * @param scope network scope the address is from
1892 * @param client client to use, reference counter must have already been increased
1893 * @param is_nat this a NAT session, we should wait for a client to
1894 * connect to us from an address, then assign that to
1896 * @return new session object
1898 static struct GNUNET_ATS_Session *
1899 create_session (struct Plugin *plugin,
1900 const struct GNUNET_HELLO_Address *address,
1901 enum GNUNET_NetworkType scope,
1902 struct GNUNET_SERVER_Client *client,
1905 struct GNUNET_ATS_Session *session;
1906 struct PendingMessage *pm;
1908 if (GNUNET_YES != is_nat)
1909 GNUNET_assert (NULL != client);
1911 GNUNET_assert (NULL == client);
1913 LOG (GNUNET_ERROR_TYPE_DEBUG,
1914 "Creating new session for peer `%s' at address %s\n",
1915 GNUNET_i2s (&address->peer),
1916 tcp_plugin_address_to_string (plugin,
1918 address->address_length));
1919 session = GNUNET_new (struct GNUNET_ATS_Session);
1920 session->last_activity = GNUNET_TIME_absolute_get ();
1921 session->plugin = plugin;
1922 session->is_nat = is_nat;
1925 session->client = client;
1926 GNUNET_SERVER_client_set_user_context (client,
1929 session->address = GNUNET_HELLO_address_copy (address);
1930 session->target = address->peer;
1931 session->expecting_welcome = GNUNET_YES;
1932 session->scope = scope;
1933 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
1934 sizeof (struct WelcomeMessage));
1935 pm->msg = (const char *) &pm[1];
1936 pm->message_size = sizeof(struct WelcomeMessage);
1937 GNUNET_memcpy (&pm[1],
1938 &plugin->my_welcome,
1939 sizeof(struct WelcomeMessage));
1940 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1941 GNUNET_STATISTICS_update (plugin->env->stats,
1942 gettext_noop ("# bytes currently in TCP buffers"),
1945 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
1946 session->pending_messages_tail,
1948 session->msgs_in_queue++;
1949 session->bytes_in_queue += pm->message_size;
1950 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1951 session->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1954 notify_session_monitor (session->plugin,
1956 GNUNET_TRANSPORT_SS_INIT);
1957 if (GNUNET_YES != is_nat)
1959 GNUNET_STATISTICS_update (plugin->env->stats,
1960 gettext_noop ("# TCP sessions active"),
1963 notify_session_monitor (session->plugin,
1965 GNUNET_TRANSPORT_SS_UP);
1969 notify_session_monitor (session->plugin,
1971 GNUNET_TRANSPORT_SS_HANDSHAKE);
1978 * If we have pending messages, ask the server to
1979 * transmit them (schedule the respective tasks, etc.)
1981 * @param session for which session should we do this
1984 process_pending_messages (struct GNUNET_ATS_Session *session);
1988 * Function called to notify a client about the socket
1989 * being ready to queue more data. "buf" will be
1990 * NULL and "size" zero if the socket was closed for
1991 * writing in the meantime.
1993 * @param cls closure
1994 * @param size number of bytes available in @a buf
1995 * @param buf where the callee should write the message
1996 * @return number of bytes written to @a buf
1999 do_transmit (void *cls,
2003 struct GNUNET_ATS_Session *session = cls;
2004 struct GNUNET_PeerIdentity pid;
2005 struct Plugin *plugin;
2006 struct PendingMessage *pos;
2007 struct PendingMessage *hd;
2008 struct PendingMessage *tl;
2009 struct GNUNET_TIME_Absolute now;
2013 session->transmit_handle = NULL;
2014 plugin = session->plugin;
2017 LOG (GNUNET_ERROR_TYPE_DEBUG,
2018 "Timeout trying to transmit to peer `%s', discarding message queue.\n",
2019 GNUNET_i2s (&session->target));
2020 /* timeout; cancel all messages that have already expired */
2024 now = GNUNET_TIME_absolute_get ();
2025 while ( (NULL != (pos = session->pending_messages_head)) &&
2026 (pos->timeout.abs_value_us <= now.abs_value_us) )
2028 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
2029 session->pending_messages_tail,
2031 GNUNET_assert (0 < session->msgs_in_queue);
2032 session->msgs_in_queue--;
2033 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
2034 session->bytes_in_queue -= pos->message_size;
2035 LOG (GNUNET_ERROR_TYPE_DEBUG,
2036 "Failed to transmit %u byte message to `%s'.\n",
2038 GNUNET_i2s (&session->target));
2039 ret += pos->message_size;
2040 GNUNET_CONTAINER_DLL_insert_after (hd,
2045 /* do this call before callbacks (so that if callbacks destroy
2046 * session, they have a chance to cancel actions done by this
2048 process_pending_messages (session);
2049 pid = session->target;
2050 /* no do callbacks and do not use session again since
2051 * the callbacks may abort the session */
2052 while (NULL != (pos = hd))
2054 GNUNET_CONTAINER_DLL_remove (hd,
2057 if (NULL != pos->transmit_cont)
2058 pos->transmit_cont (pos->transmit_cont_cls,
2065 GNUNET_STATISTICS_update (plugin->env->stats,
2066 gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
2068 GNUNET_STATISTICS_update (plugin->env->stats,
2069 gettext_noop ("# bytes discarded by TCP (timeout)"),
2073 notify_session_monitor (session->plugin,
2075 GNUNET_TRANSPORT_SS_UPDATE);
2078 /* copy all pending messages that would fit */
2083 while (NULL != (pos = session->pending_messages_head))
2085 if (ret + pos->message_size > size)
2087 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
2088 session->pending_messages_tail,
2090 GNUNET_assert (0 < session->msgs_in_queue);
2091 session->msgs_in_queue--;
2092 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
2093 session->bytes_in_queue -= pos->message_size;
2094 GNUNET_assert(size >= pos->message_size);
2095 LOG (GNUNET_ERROR_TYPE_DEBUG,
2096 "Transmitting message of type %u size %u to peer %s at %s\n",
2097 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type),
2099 GNUNET_i2s (&session->target),
2100 tcp_plugin_address_to_string (session->plugin,
2101 session->address->address,
2102 session->address->address_length));
2103 /* FIXME: this GNUNET_memcpy can be up to 7% of our total runtime */
2104 GNUNET_memcpy (cbuf,
2107 cbuf += pos->message_size;
2108 ret += pos->message_size;
2109 size -= pos->message_size;
2110 GNUNET_CONTAINER_DLL_insert_tail (hd,
2114 notify_session_monitor (session->plugin,
2116 GNUNET_TRANSPORT_SS_UPDATE);
2117 /* schedule 'continuation' before callbacks so that callbacks that
2118 * cancel everything don't cause us to use a session that no longer
2120 process_pending_messages (session);
2121 session->last_activity = GNUNET_TIME_absolute_get ();
2122 pid = session->target;
2123 /* we'll now call callbacks that may cancel the session; hence
2124 * we should not use 'session' after this point */
2125 while (NULL != (pos = hd))
2127 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
2128 if (NULL != pos->transmit_cont)
2129 pos->transmit_cont (pos->transmit_cont_cls,
2133 pos->message_size); /* FIXME: include TCP overhead */
2136 GNUNET_assert (NULL == hd);
2137 GNUNET_assert (NULL == tl);
2138 GNUNET_STATISTICS_update (plugin->env->stats,
2139 gettext_noop ("# bytes currently in TCP buffers"),
2142 GNUNET_STATISTICS_update (plugin->env->stats,
2143 gettext_noop ("# bytes transmitted via TCP"),
2151 * If we have pending messages, ask the server to
2152 * transmit them (schedule the respective tasks, etc.)
2154 * @param session for which session should we do this
2157 process_pending_messages (struct GNUNET_ATS_Session *session)
2159 struct PendingMessage *pm;
2161 GNUNET_assert (NULL != session->client);
2162 if (NULL != session->transmit_handle)
2164 if (NULL == (pm = session->pending_messages_head))
2167 session->transmit_handle
2168 = GNUNET_SERVER_notify_transmit_ready (session->client,
2170 GNUNET_TIME_absolute_get_remaining (pm->timeout),
2177 * Function that can be used by the transport service to transmit
2178 * a message using the plugin. Note that in the case of a
2179 * peer disconnecting, the continuation MUST be called
2180 * prior to the disconnect notification itself. This function
2181 * will be called with this peer's HELLO message to initiate
2182 * a fresh connection to another peer.
2184 * @param cls closure
2185 * @param session which session must be used
2186 * @param msgbuf the message to transmit
2187 * @param msgbuf_size number of bytes in @a msgbuf
2188 * @param priority how important is the message (most plugins will
2189 * ignore message priority and just FIFO)
2190 * @param to how long to wait at most for the transmission (does not
2191 * require plugins to discard the message after the timeout,
2192 * just advisory for the desired delay; most plugins will ignore
2194 * @param cont continuation to call once the message has
2195 * been transmitted (or if the transport is ready
2196 * for the next transmission call; or if the
2197 * peer disconnected...); can be NULL
2198 * @param cont_cls closure for @a cont
2199 * @return number of bytes used (on the physical network, with overheads);
2200 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2201 * and does NOT mean that the message was not transmitted (DV)
2204 tcp_plugin_send (void *cls,
2205 struct GNUNET_ATS_Session *session,
2208 unsigned int priority,
2209 struct GNUNET_TIME_Relative to,
2210 GNUNET_TRANSPORT_TransmitContinuation cont,
2213 struct Plugin * plugin = cls;
2214 struct PendingMessage *pm;
2216 /* create new message entry */
2217 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
2218 pm->msg = (const char *) &pm[1];
2219 GNUNET_memcpy (&pm[1], msgbuf, msgbuf_size);
2220 pm->message_size = msgbuf_size;
2221 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
2222 pm->transmit_cont = cont;
2223 pm->transmit_cont_cls = cont_cls;
2225 LOG (GNUNET_ERROR_TYPE_DEBUG,
2226 "Asked to transmit %u bytes to `%s', added message to list.\n",
2228 GNUNET_i2s (&session->target));
2231 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2235 GNUNET_assert (NULL != session->client);
2236 GNUNET_SERVER_client_set_timeout (session->client,
2237 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2238 GNUNET_STATISTICS_update (plugin->env->stats,
2239 gettext_noop ("# bytes currently in TCP buffers"),
2243 /* append pm to pending_messages list */
2244 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
2245 session->pending_messages_tail,
2247 notify_session_monitor (session->plugin,
2249 GNUNET_TRANSPORT_SS_UPDATE);
2250 session->msgs_in_queue++;
2251 session->bytes_in_queue += pm->message_size;
2252 process_pending_messages (session);
2256 GNUNET_CONTAINER_multipeermap_contains_value (plugin->nat_wait_conns,
2260 LOG (GNUNET_ERROR_TYPE_DEBUG,
2261 "This NAT WAIT session for peer `%s' is not yet ready!\n",
2262 GNUNET_i2s (&session->target));
2263 GNUNET_STATISTICS_update (plugin->env->stats,
2264 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
2266 /* append pm to pending_messages list */
2267 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
2268 session->pending_messages_tail,
2270 session->msgs_in_queue++;
2271 session->bytes_in_queue += pm->message_size;
2272 notify_session_monitor (session->plugin,
2274 GNUNET_TRANSPORT_SS_HANDSHAKE);
2277 LOG (GNUNET_ERROR_TYPE_ERROR,
2278 "Invalid session %p\n",
2288 return GNUNET_SYSERR; /* session does not exist here */
2293 * Closure for #session_lookup_it().
2295 struct GNUNET_ATS_SessionItCtx
2298 * Address we are looking for.
2300 const struct GNUNET_HELLO_Address *address;
2303 * Where to store the session (if we found it).
2305 struct GNUNET_ATS_Session *result;
2311 * Look for a session by address.
2313 * @param cls the `struct GNUNET_ATS_SessionItCtx`
2315 * @param value a `struct GNUNET_ATS_Session`
2316 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
2319 session_lookup_it (void *cls,
2320 const struct GNUNET_PeerIdentity *key,
2323 struct GNUNET_ATS_SessionItCtx *si_ctx = cls;
2324 struct GNUNET_ATS_Session *session = value;
2327 GNUNET_HELLO_address_cmp (si_ctx->address,
2330 si_ctx->result = session;
2336 * Task cleaning up a NAT connection attempt after timeout
2338 * @param cls the `struct GNUNET_ATS_Session`
2341 nat_connect_timeout (void *cls)
2343 struct GNUNET_ATS_Session *session = cls;
2345 session->nat_connection_timeout = NULL;
2346 LOG (GNUNET_ERROR_TYPE_DEBUG,
2347 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
2348 GNUNET_i2s (&session->target),
2349 tcp_plugin_address_to_string (session->plugin,
2350 session->address->address,
2351 session->address->address_length));
2352 tcp_plugin_disconnect_session (session->plugin,
2358 * Function that will be called whenever the transport service wants to
2359 * notify the plugin that a session is still active and in use and
2360 * therefore the session timeout for this session has to be updated
2362 * @param cls closure
2363 * @param peer which peer was the session for
2364 * @param session which session is being updated
2367 tcp_plugin_update_session_timeout (void *cls,
2368 const struct GNUNET_PeerIdentity *peer,
2369 struct GNUNET_ATS_Session *session)
2371 reschedule_session_timeout (session);
2376 * Task to signal the server that we can continue
2377 * receiving from the TCP client now.
2379 * @param cls the `struct GNUNET_ATS_Session *`
2382 delayed_done (void *cls)
2384 struct GNUNET_ATS_Session *session = cls;
2386 session->receive_delay_task = NULL;
2387 reschedule_session_timeout (session);
2388 GNUNET_SERVER_receive_done (session->client,
2394 * Function that will be called whenever the transport service wants to
2395 * notify the plugin that the inbound quota changed and that the plugin
2396 * should update it's delay for the next receive value
2398 * @param cls closure
2399 * @param peer which peer was the session for
2400 * @param session which session is being updated
2401 * @param delay new delay to use for receiving
2404 tcp_plugin_update_inbound_delay (void *cls,
2405 const struct GNUNET_PeerIdentity *peer,
2406 struct GNUNET_ATS_Session *session,
2407 struct GNUNET_TIME_Relative delay)
2409 if (NULL == session->receive_delay_task)
2411 LOG (GNUNET_ERROR_TYPE_DEBUG,
2412 "New inbound delay %s\n",
2413 GNUNET_STRINGS_relative_time_to_string (delay,
2415 session->receive_delay = GNUNET_TIME_relative_to_absolute (delay);
2416 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
2417 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
2424 * Create a new session to transmit data to the target
2425 * This session will used to send data to this peer and the plugin will
2426 * notify us by calling the env->session_end function
2428 * @param cls closure
2429 * @param address the address to use
2430 * @return the session if the address is valid, NULL otherwise
2432 static struct GNUNET_ATS_Session *
2433 tcp_plugin_get_session (void *cls,
2434 const struct GNUNET_HELLO_Address *address)
2436 struct Plugin *plugin = cls;
2437 struct GNUNET_ATS_Session *session = NULL;
2441 struct GNUNET_CONNECTION_Handle *sa;
2442 struct sockaddr_in a4;
2443 struct sockaddr_in6 a6;
2444 const struct IPv4TcpAddress *t4;
2445 const struct IPv6TcpAddress *t6;
2446 unsigned int options;
2447 enum GNUNET_NetworkType net_type;
2448 unsigned int is_natd = GNUNET_NO;
2451 struct GNUNET_NETWORK_Handle *s;
2454 addrlen = address->address_length;
2455 LOG (GNUNET_ERROR_TYPE_DEBUG,
2456 "Trying to get session for `%s' address of peer `%s'\n",
2457 tcp_plugin_address_to_string (plugin,
2459 address->address_length),
2460 GNUNET_i2s (&address->peer));
2462 if (GNUNET_HELLO_address_check_option (address,
2463 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
2469 /* look for existing session */
2471 GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
2474 struct GNUNET_ATS_SessionItCtx si_ctx;
2476 si_ctx.address = address;
2477 si_ctx.result = NULL;
2478 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
2482 if (NULL != si_ctx.result)
2484 session = si_ctx.result;
2485 LOG (GNUNET_ERROR_TYPE_DEBUG,
2486 "Found existing session for `%s' address `%s'\n",
2487 GNUNET_i2s (&address->peer),
2488 tcp_plugin_address_to_string (plugin,
2490 address->address_length));
2493 /* This is a bit of a hack, limiting TCP to never allow more than
2494 one TCP connection to any given peer at the same time.
2495 Without this, peers sometimes disagree about which of the TCP
2496 connections they should use, causing one side to believe that
2497 they transmit successfully, while the other receives nothing. */
2498 return NULL; /* Refuse to have more than one TCP connection per
2499 peer pair at the same time. */
2502 if (addrlen == sizeof(struct IPv6TcpAddress))
2504 GNUNET_assert (NULL != address->address); /* make static analysis happy */
2505 t6 = address->address;
2506 options = t6->options;
2508 memset (&a6, 0, sizeof(a6));
2509 #if HAVE_SOCKADDR_IN_SIN_LEN
2510 a6.sin6_len = sizeof (a6);
2512 a6.sin6_family = AF_INET6;
2513 a6.sin6_port = t6->t6_port;
2514 if (t6->t6_port == 0)
2515 is_natd = GNUNET_YES;
2516 GNUNET_memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
2520 else if (addrlen == sizeof(struct IPv4TcpAddress))
2522 GNUNET_assert(NULL != address->address); /* make static analysis happy */
2523 t4 = address->address;
2524 options = t4->options;
2526 memset (&a4, 0, sizeof(a4));
2527 #if HAVE_SOCKADDR_IN_SIN_LEN
2528 a4.sin_len = sizeof (a4);
2530 a4.sin_family = AF_INET;
2531 a4.sin_port = t4->t4_port;
2532 if (t4->t4_port == 0)
2533 is_natd = GNUNET_YES;
2534 a4.sin_addr.s_addr = t4->ipv4_addr;
2540 GNUNET_STATISTICS_update (plugin->env->stats,
2541 gettext_noop ("# requests to create session with invalid address"),
2547 net_type = plugin->env->get_address_type (plugin->env->cls,
2550 GNUNET_break (net_type != GNUNET_NT_UNSPECIFIED);
2552 if ( (is_natd == GNUNET_YES) &&
2553 (addrlen == sizeof(struct IPv6TcpAddress)) )
2555 /* NAT client only works with IPv4 addresses */
2559 if (plugin->cur_connections >= plugin->max_connections)
2565 if ( (is_natd == GNUNET_YES) &&
2567 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
2570 /* Only do one NAT punch attempt per peer identity */
2574 if ( (is_natd == GNUNET_YES) &&
2575 (NULL != plugin->nat) &&
2577 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
2580 struct sockaddr_in local_sa;
2582 LOG (GNUNET_ERROR_TYPE_DEBUG,
2583 "Found valid IPv4 NAT address (creating session)!\n");
2584 session = create_session (plugin,
2589 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
2590 &nat_connect_timeout,
2592 GNUNET_assert (GNUNET_OK ==
2593 GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
2596 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2598 LOG (GNUNET_ERROR_TYPE_DEBUG,
2599 "Created NAT WAIT connection to `%s' at `%s'\n",
2600 GNUNET_i2s (&session->target),
2601 GNUNET_a2s (sb, sbs));
2605 local_sa.sin_family = AF_INET;
2606 local_sa.sin_port = htons (plugin->open_port);
2607 /* We leave sin_address at 0, let the kernel figure it out,
2608 even if our bind() is more specific. (May want to reconsider
2611 GNUNET_NAT_request_reversal (plugin->nat,
2615 LOG (GNUNET_ERROR_TYPE_DEBUG,
2616 "Running NAT client for `%s' at `%s' failed\n",
2617 GNUNET_i2s (&session->target),
2618 GNUNET_a2s (sb, sbs));
2619 tcp_plugin_disconnect_session (plugin,
2624 /* create new outbound session */
2625 if (0 != (options & TCP_OPTIONS_TCP_STEALTH))
2628 s = GNUNET_NETWORK_socket_create (af, SOCK_STREAM, 0);
2631 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2638 GNUNET_NETWORK_socket_setsockopt (s,
2642 sizeof (struct GNUNET_PeerIdentity))) ||
2644 GNUNET_NETWORK_socket_setsockopt (s,
2646 TCP_STEALTH_INTEGRITY,
2647 &plugin->my_welcome,
2648 sizeof (struct WelcomeMessage))) )
2650 /* TCP STEALTH not supported by kernel */
2651 GNUNET_break (GNUNET_OK ==
2652 GNUNET_NETWORK_socket_close (s));
2657 sa = GNUNET_CONNECTION_connect_socket (s, sb, sbs);
2666 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
2670 LOG (GNUNET_ERROR_TYPE_DEBUG,
2671 "Failed to create connection to `%s' at `%s'\n",
2672 GNUNET_i2s (&address->peer),
2673 GNUNET_a2s (sb, sbs));
2676 LOG (GNUNET_ERROR_TYPE_DEBUG,
2677 "Asked to transmit to `%s', creating fresh session using address `%s'.\n",
2678 GNUNET_i2s (&address->peer),
2679 GNUNET_a2s (sb, sbs));
2681 session = create_session (plugin,
2684 GNUNET_SERVER_connect_socket (plugin->server,
2687 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2690 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2691 /* Send TCP Welcome */
2692 process_pending_messages (session);
2699 * We have been asked to destroy all connections to a particular peer.
2700 * This function is called on each applicable session and must tear it
2703 * @param cls the `struct Plugin *`
2704 * @param key the peer which the session belongs to (unused)
2705 * @param value the `struct GNUNET_ATS_Session`
2706 * @return #GNUNET_YES (continue to iterate)
2709 session_disconnect_it (void *cls,
2710 const struct GNUNET_PeerIdentity *key,
2713 struct Plugin *plugin = cls;
2714 struct GNUNET_ATS_Session *session = value;
2716 GNUNET_STATISTICS_update (session->plugin->env->stats,
2717 gettext_noop ("# transport-service disconnect requests for TCP"),
2720 tcp_plugin_disconnect_session (plugin,
2727 * Function that can be called to force a disconnect from the
2728 * specified neighbour. This should also cancel all previously
2729 * scheduled transmissions. Obviously the transmission may have been
2730 * partially completed already, which is OK. The plugin is supposed
2731 * to close the connection (if applicable) and no longer call the
2732 * transmit continuation(s).
2734 * Finally, plugin MUST NOT call the services's receive function to
2735 * notify the service that the connection to the specified target was
2736 * closed after a getting this call.
2738 * @param cls closure
2739 * @param target peer for which the last transmission is
2743 tcp_plugin_disconnect (void *cls,
2744 const struct GNUNET_PeerIdentity *target)
2746 struct Plugin *plugin = cls;
2748 LOG (GNUNET_ERROR_TYPE_DEBUG,
2749 "Disconnecting peer `%s'\n",
2750 GNUNET_i2s (target));
2751 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
2753 &session_disconnect_it,
2755 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns,
2757 &session_disconnect_it,
2763 * We are processing an address pretty printing request and finished
2764 * the IP resolution (if applicable). Append our port and forward the
2765 * result. If called with @a hostname NULL, we are done and should
2766 * clean up the pretty printer (otherwise, there might be multiple
2767 * hostnames for the IP address and we might receive more).
2769 * @param cls the `struct PrettyPrinterContext *`
2770 * @param hostname hostname part of the address
2773 append_port (void *cls,
2774 const char *hostname)
2776 struct PrettyPrinterContext *ppc = cls;
2777 struct Plugin *plugin = ppc->plugin;
2780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2781 "append_port called with hostname `%s'\n",
2783 if (NULL == hostname)
2785 /* Final call, done */
2786 ppc->resolver_handle = NULL;
2787 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
2788 plugin->ppc_dll_tail,
2790 ppc->asc (ppc->asc_cls,
2796 if (GNUNET_YES == ppc->ipv6)
2797 GNUNET_asprintf (&ret,
2804 GNUNET_asprintf (&ret,
2810 ppc->asc (ppc->asc_cls,
2818 * Convert the transports address to a nice, human-readable format.
2820 * @param cls closure with the `struct Plugin`
2821 * @param type name of the transport that generated the address
2822 * @param addr one of the addresses of the host, NULL for the last address
2823 * the specific address format depends on the transport
2824 * @param addrlen length of the @a addr
2825 * @param numeric should (IP) addresses be displayed in numeric form?
2826 * @param timeout after how long should we give up?
2827 * @param asc function to call on each string
2828 * @param asc_cls closure for @a asc
2831 tcp_plugin_address_pretty_printer (void *cls,
2836 struct GNUNET_TIME_Relative timeout,
2837 GNUNET_TRANSPORT_AddressStringCallback asc,
2840 struct Plugin *plugin = cls;
2841 struct PrettyPrinterContext *ppc;
2844 struct sockaddr_in a4;
2845 struct sockaddr_in6 a6;
2846 const struct IPv4TcpAddress *t4;
2847 const struct IPv6TcpAddress *t6;
2851 if (sizeof(struct IPv6TcpAddress) == addrlen)
2854 memset (&a6, 0, sizeof(a6));
2855 a6.sin6_family = AF_INET6;
2856 a6.sin6_port = t6->t6_port;
2857 GNUNET_memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
2858 port = ntohs (t6->t6_port);
2859 options = ntohl (t6->options);
2863 else if (sizeof(struct IPv4TcpAddress) == addrlen)
2866 memset (&a4, 0, sizeof(a4));
2867 a4.sin_family = AF_INET;
2868 a4.sin_port = t4->t4_port;
2869 a4.sin_addr.s_addr = t4->ipv4_addr;
2870 port = ntohs (t4->t4_port);
2871 options = ntohl (t4->options);
2877 /* invalid address */
2878 LOG (GNUNET_ERROR_TYPE_WARNING,
2879 _("Unexpected address length: %u bytes\n"),
2880 (unsigned int) addrlen);
2881 asc (asc_cls, NULL, GNUNET_SYSERR);
2882 asc (asc_cls, NULL, GNUNET_OK);
2885 ppc = GNUNET_new (struct PrettyPrinterContext);
2886 ppc->plugin = plugin;
2887 if (addrlen == sizeof(struct IPv6TcpAddress))
2888 ppc->ipv6 = GNUNET_YES;
2890 ppc->ipv6 = GNUNET_NO;
2892 ppc->asc_cls = asc_cls;
2894 ppc->options = options;
2895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2896 "Starting DNS reverse lookup\n");
2897 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb,
2903 if (NULL == ppc->resolver_handle)
2909 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
2910 plugin->ppc_dll_tail,
2916 * Function that will be called to check if a binary address for this
2917 * plugin is well-formed and corresponds to an address for THIS peer
2918 * (as per our configuration). Naturally, if absolutely necessary,
2919 * plugins can be a bit conservative in their answer, but in general
2920 * plugins should make sure that the address does not redirect
2921 * traffic to a 3rd party that might try to man-in-the-middle our
2924 * @param cls closure, our `struct Plugin *`
2925 * @param addr pointer to the address
2926 * @param addrlen length of @a addr
2927 * @return #GNUNET_OK if this is a plausible address for this peer
2928 * and transport, #GNUNET_SYSERR if not
2931 tcp_plugin_check_address (void *cls,
2935 struct Plugin *plugin = cls;
2936 const struct IPv4TcpAddress *v4;
2937 const struct IPv6TcpAddress *v6;
2939 if ( (addrlen != sizeof(struct IPv4TcpAddress)) &&
2940 (addrlen != sizeof(struct IPv6TcpAddress)) )
2942 GNUNET_break_op (0);
2943 return GNUNET_SYSERR;
2946 if (addrlen == sizeof(struct IPv4TcpAddress))
2948 struct sockaddr_in s4;
2950 v4 = (const struct IPv4TcpAddress *) addr;
2951 if (0 != memcmp (&v4->options,
2956 return GNUNET_SYSERR;
2958 memset (&s4, 0, sizeof (s4));
2959 s4.sin_family = AF_INET;
2960 #if HAVE_SOCKADDR_IN_SIN_LEN
2961 s4.sin_len = sizeof (s4);
2963 s4.sin_port = v4->t4_port;
2964 s4.sin_addr.s_addr = v4->ipv4_addr;
2967 GNUNET_NAT_test_address (plugin->nat,
2969 sizeof (struct sockaddr_in)))
2970 return GNUNET_SYSERR;
2974 struct sockaddr_in6 s6;
2976 v6 = (const struct IPv6TcpAddress *) addr;
2977 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2979 GNUNET_break_op (0);
2980 return GNUNET_SYSERR;
2982 if (0 != memcmp (&v6->options,
2987 return GNUNET_SYSERR;
2989 memset (&s6, 0, sizeof (s6));
2990 s6.sin6_family = AF_INET6;
2991 #if HAVE_SOCKADDR_IN_SIN_LEN
2992 s6.sin6_len = sizeof (s6);
2994 s6.sin6_port = v6->t6_port;
2995 s6.sin6_addr = v6->ipv6_addr;
2998 GNUNET_NAT_test_address (plugin->nat,
3000 sizeof(struct sockaddr_in6)))
3001 return GNUNET_SYSERR;
3008 * We've received a nat probe from this peer via TCP. Finish
3009 * creating the client session and resume sending of queued
3012 * @param cls closure
3013 * @param client identification of the client
3014 * @param message the actual message
3017 handle_tcp_nat_probe (void *cls,
3018 struct GNUNET_SERVER_Client *client,
3019 const struct GNUNET_MessageHeader *message)
3021 struct Plugin *plugin = cls;
3022 struct GNUNET_ATS_Session *session;
3023 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
3026 struct IPv4TcpAddress *t4;
3027 struct IPv6TcpAddress *t6;
3028 const struct sockaddr_in *s4;
3029 const struct sockaddr_in6 *s6;
3031 LOG (GNUNET_ERROR_TYPE_DEBUG,
3032 "Received NAT probe\n");
3033 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
3034 * a connection to this peer by running gnunet-nat-client. This peer
3035 * received the punch message and now wants us to use the new connection
3036 * as the default for that peer. Do so and then send a WELCOME message
3037 * so we can really be connected!
3039 if (ntohs (message->size) != sizeof(struct TCP_NAT_ProbeMessage))
3042 GNUNET_SERVER_receive_done (client,
3047 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
3048 if (0 == memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
3049 sizeof(struct GNUNET_PeerIdentity)))
3051 /* refuse connections from ourselves */
3052 GNUNET_SERVER_receive_done (client,
3057 session = GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
3058 &tcp_nat_probe->clientIdentity);
3059 if (NULL == session)
3061 LOG (GNUNET_ERROR_TYPE_DEBUG,
3062 "Did NOT find session for NAT probe!\n");
3063 GNUNET_SERVER_receive_done (client,
3067 LOG (GNUNET_ERROR_TYPE_DEBUG,
3068 "Found session for NAT probe!\n");
3070 if (NULL != session->nat_connection_timeout)
3072 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
3073 session->nat_connection_timeout = NULL;
3077 GNUNET_SERVER_client_get_address (client,
3082 GNUNET_SERVER_receive_done (client,
3084 tcp_plugin_disconnect_session (plugin,
3088 GNUNET_assert (GNUNET_YES ==
3089 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
3090 &tcp_nat_probe->clientIdentity,
3092 GNUNET_SERVER_client_set_user_context (client,
3094 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
3097 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3098 session->last_activity = GNUNET_TIME_absolute_get ();
3099 LOG (GNUNET_ERROR_TYPE_DEBUG,
3100 "Found address `%s' for incoming connection\n",
3101 GNUNET_a2s (vaddr, alen));
3102 switch (((const struct sockaddr *) vaddr)->sa_family)
3106 t4 = GNUNET_new (struct IPv4TcpAddress);
3107 t4->options = htonl (TCP_OPTIONS_NONE);
3108 t4->t4_port = s4->sin_port;
3109 t4->ipv4_addr = s4->sin_addr.s_addr;
3110 session->address = GNUNET_HELLO_address_allocate (&tcp_nat_probe->clientIdentity,
3113 sizeof(struct IPv4TcpAddress),
3114 GNUNET_HELLO_ADDRESS_INFO_NONE);
3118 t6 = GNUNET_new (struct IPv6TcpAddress);
3119 t6->options = htonl (TCP_OPTIONS_NONE);
3120 t6->t6_port = s6->sin6_port;
3121 GNUNET_memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
3122 session->address = GNUNET_HELLO_address_allocate (&tcp_nat_probe->clientIdentity,
3125 sizeof(struct IPv6TcpAddress),
3126 GNUNET_HELLO_ADDRESS_INFO_NONE);
3130 LOG(GNUNET_ERROR_TYPE_DEBUG,
3131 "Bad address for incoming connection!\n");
3133 GNUNET_SERVER_receive_done (client,
3135 tcp_plugin_disconnect_session (plugin,
3139 GNUNET_free (vaddr);
3140 GNUNET_break (NULL == session->client);
3141 session->client = client;
3142 GNUNET_STATISTICS_update (plugin->env->stats,
3143 gettext_noop ("# TCP sessions active"),
3146 process_pending_messages (session);
3147 GNUNET_SERVER_receive_done (client,
3153 * We've received a welcome from this peer via TCP. Possibly create a
3154 * fresh client record and send back our welcome.
3156 * @param cls closure
3157 * @param client identification of the client
3158 * @param message the actual message
3161 handle_tcp_welcome (void *cls,
3162 struct GNUNET_SERVER_Client *client,
3163 const struct GNUNET_MessageHeader *message)
3165 struct Plugin *plugin = cls;
3166 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
3167 struct GNUNET_HELLO_Address *address;
3168 struct GNUNET_ATS_Session *session;
3171 struct IPv4TcpAddress t4;
3172 struct IPv6TcpAddress t6;
3173 const struct sockaddr_in *s4;
3174 const struct sockaddr_in6 *s6;
3176 if (0 == memcmp (&wm->clientIdentity,
3177 plugin->env->my_identity,
3178 sizeof(struct GNUNET_PeerIdentity)))
3180 /* refuse connections from ourselves */
3182 GNUNET_SERVER_client_get_address (client,
3186 LOG (GNUNET_ERROR_TYPE_INFO,
3187 "Received WELCOME message from my own identity `%s' on address `%s'\n",
3188 GNUNET_i2s (&wm->clientIdentity),
3189 GNUNET_a2s (vaddr, alen));
3190 GNUNET_free (vaddr);
3192 GNUNET_SERVER_receive_done (client,
3198 GNUNET_SERVER_client_get_address (client,
3202 LOG(GNUNET_ERROR_TYPE_DEBUG,
3203 "Received WELCOME message from `%s' on address `%s'\n",
3204 GNUNET_i2s (&wm->clientIdentity),
3205 GNUNET_a2s (vaddr, alen));
3206 GNUNET_free (vaddr);
3208 GNUNET_STATISTICS_update (plugin->env->stats,
3209 gettext_noop ("# TCP WELCOME messages received"),
3212 session = lookup_session_by_client (plugin,
3214 if (NULL != session)
3217 GNUNET_SERVER_client_get_address (client,
3221 LOG (GNUNET_ERROR_TYPE_DEBUG,
3222 "Found existing session %p for peer `%s'\n",
3224 GNUNET_a2s (vaddr, alen));
3225 GNUNET_free (vaddr);
3231 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
3233 if (alen == sizeof(struct sockaddr_in))
3236 memset (&t4, '\0', sizeof (t4));
3237 t4.options = htonl (TCP_OPTIONS_NONE);
3238 t4.t4_port = s4->sin_port;
3239 t4.ipv4_addr = s4->sin_addr.s_addr;
3240 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
3244 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
3246 else if (alen == sizeof(struct sockaddr_in6))
3249 memset (&t6, '\0', sizeof (t6));
3250 t6.options = htonl (TCP_OPTIONS_NONE);
3251 t6.t6_port = s6->sin6_port;
3252 GNUNET_memcpy (&t6.ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
3253 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
3257 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
3262 GNUNET_free_non_null (vaddr);
3263 GNUNET_SERVER_receive_done (client,
3267 session = create_session (plugin,
3269 plugin->env->get_address_type (plugin->env->cls,
3274 GNUNET_break (GNUNET_NT_UNSPECIFIED != session->scope);
3275 GNUNET_HELLO_address_free (address);
3276 LOG (GNUNET_ERROR_TYPE_DEBUG,
3277 "Creating new%s session %p for peer `%s' client %p\n",
3278 GNUNET_HELLO_address_check_option (session->address,
3279 GNUNET_HELLO_ADDRESS_INFO_INBOUND)
3282 tcp_plugin_address_to_string (plugin,
3283 session->address->address,
3284 session->address->address_length),
3286 GNUNET_free (vaddr);
3287 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
3290 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3291 /* Notify transport and ATS about new session */
3292 plugin->env->session_start (plugin->env->cls,
3299 LOG(GNUNET_ERROR_TYPE_DEBUG,
3300 "Did not obtain TCP socket address for incoming connection\n");
3302 GNUNET_SERVER_receive_done (client,
3308 if (GNUNET_YES != session->expecting_welcome)
3310 GNUNET_break_op (0);
3311 GNUNET_SERVER_receive_done (client,
3315 session->last_activity = GNUNET_TIME_absolute_get ();
3316 session->expecting_welcome = GNUNET_NO;
3318 process_pending_messages (session);
3319 GNUNET_SERVER_client_set_timeout (client,
3320 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3321 GNUNET_SERVER_receive_done (client,
3327 * We've received data for this peer via TCP. Unbox,
3328 * compute latency and forward.
3330 * @param cls closure
3331 * @param client identification of the client
3332 * @param message the actual message
3335 handle_tcp_data (void *cls,
3336 struct GNUNET_SERVER_Client *client,
3337 const struct GNUNET_MessageHeader *message)
3339 struct Plugin *plugin = cls;
3340 struct GNUNET_ATS_Session *session;
3341 struct GNUNET_TIME_Relative delay;
3344 type = ntohs (message->type);
3345 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
3346 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type) )
3348 /* We don't want to propagate WELCOME and NAT Probe messages up! */
3349 GNUNET_SERVER_receive_done (client,
3353 session = lookup_session_by_client (plugin, client);
3354 if (NULL == session)
3356 /* No inbound session found */
3360 GNUNET_assert (GNUNET_OK ==
3361 GNUNET_SERVER_client_get_address (client,
3364 LOG (GNUNET_ERROR_TYPE_ERROR,
3365 "Received unexpected %u bytes of type %u from `%s'\n",
3366 (unsigned int) ntohs (message->size),
3367 (unsigned int) ntohs (message->type),
3371 GNUNET_SERVER_receive_done (client,
3373 GNUNET_free_non_null (vaddr);
3376 if (GNUNET_YES == session->expecting_welcome)
3378 /* Session is expecting WELCOME message */
3382 GNUNET_SERVER_client_get_address (client,
3385 LOG (GNUNET_ERROR_TYPE_ERROR,
3386 "Received unexpected %u bytes of type %u from `%s'\n",
3387 (unsigned int) ntohs (message->size),
3388 (unsigned int) ntohs (message->type),
3389 GNUNET_a2s (vaddr, alen));
3391 GNUNET_SERVER_receive_done (client,
3393 GNUNET_free_non_null (vaddr);
3397 session->last_activity = GNUNET_TIME_absolute_get ();
3402 GNUNET_SERVER_client_get_address (client,
3405 LOG (GNUNET_ERROR_TYPE_DEBUG,
3406 "Passing %u bytes of type %u from `%s' at %s to transport service.\n",
3407 (unsigned int) ntohs (message->size),
3408 (unsigned int) ntohs (message->type),
3409 GNUNET_i2s (&session->target),
3410 GNUNET_a2s (vaddr, alen));
3411 GNUNET_free_non_null (vaddr);
3414 GNUNET_STATISTICS_update (plugin->env->stats,
3415 gettext_noop ("# bytes received via TCP"),
3416 ntohs (message->size),
3419 GNUNET_assert (GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
3422 delay = plugin->env->receive (plugin->env->cls,
3426 reschedule_session_timeout (session);
3427 if (0 == delay.rel_value_us)
3429 GNUNET_SERVER_receive_done (client,
3434 LOG (GNUNET_ERROR_TYPE_DEBUG,
3435 "Throttling receiving from `%s' for %s\n",
3436 GNUNET_i2s (&session->target),
3437 GNUNET_STRINGS_relative_time_to_string (delay,
3439 GNUNET_SERVER_disable_receive_done_warning (client);
3440 GNUNET_assert (NULL == session->receive_delay_task);
3441 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
3449 * Function called whenever a peer is connected on the "SERVER" level.
3450 * Increments number of active connections and suspends server if we
3451 * have reached the limit.
3453 * @param cls closure
3454 * @param client identification of the client
3457 connect_notify (void *cls,
3458 struct GNUNET_SERVER_Client *client)
3460 struct Plugin *plugin = cls;
3464 plugin->cur_connections++;
3465 GNUNET_STATISTICS_set (plugin->env->stats,
3466 gettext_noop ("# TCP server connections active"),
3467 plugin->cur_connections,
3469 GNUNET_STATISTICS_update (plugin->env->stats,
3470 gettext_noop ("# TCP server connect events"),
3473 if (plugin->cur_connections != plugin->max_connections)
3475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3476 _("TCP connection limit reached, suspending server\n"));
3477 GNUNET_STATISTICS_update (plugin->env->stats,
3478 gettext_noop ("# TCP service suspended"),
3481 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
3486 * Function called whenever a peer is disconnected on the "SERVER"
3487 * level. Cleans up the connection, decrements number of active
3488 * connections and if applicable resumes listening.
3490 * @param cls closure
3491 * @param client identification of the client
3494 disconnect_notify (void *cls,
3495 struct GNUNET_SERVER_Client *client)
3497 struct Plugin *plugin = cls;
3498 struct GNUNET_ATS_Session *session;
3502 GNUNET_assert (plugin->cur_connections >= 1);
3503 plugin->cur_connections--;
3504 session = lookup_session_by_client (plugin,
3506 if (NULL == session)
3507 return; /* unknown, nothing to do */
3508 LOG (GNUNET_ERROR_TYPE_DEBUG,
3509 "Destroying session of `%s' with %s due to network-level disconnect.\n",
3510 GNUNET_i2s (&session->target),
3511 tcp_plugin_address_to_string (session->plugin,
3512 session->address->address,
3513 session->address->address_length));
3515 if (plugin->cur_connections == plugin->max_connections)
3517 GNUNET_STATISTICS_update (session->plugin->env->stats,
3518 gettext_noop ("# TCP service resumed"),
3521 GNUNET_SERVER_resume (plugin->server); /* Resume server */
3523 GNUNET_STATISTICS_set (plugin->env->stats,
3524 gettext_noop ("# TCP server connections active"),
3525 plugin->cur_connections,
3527 GNUNET_STATISTICS_update (session->plugin->env->stats,
3528 gettext_noop ("# network-level TCP disconnect events"),
3531 tcp_plugin_disconnect_session (plugin,
3537 * We can now send a probe message, copy into buffer to really send.
3539 * @param cls closure, a `struct TCPProbeContext`
3540 * @param size max size to copy
3541 * @param buf buffer to copy message to
3542 * @return number of bytes copied into @a buf
3545 notify_send_probe (void *cls,
3549 struct TCPProbeContext *tcp_probe_ctx = cls;
3550 struct Plugin *plugin = tcp_probe_ctx->plugin;
3553 tcp_probe_ctx->transmit_handle = NULL;
3554 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3559 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
3560 GNUNET_free(tcp_probe_ctx);
3563 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
3565 &tcp_probe_ctx->message,
3566 sizeof(tcp_probe_ctx->message));
3567 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
3568 tcp_probe_ctx->sock);
3569 ret = sizeof(tcp_probe_ctx->message);
3570 GNUNET_free (tcp_probe_ctx);
3576 * Function called by the NAT subsystem suggesting another peer wants
3577 * to connect to us via connection reversal. Try to connect back to the
3580 * @param cls closure
3581 * @param addr address to try
3582 * @param addrlen number of bytes in @a addr
3585 try_connection_reversal (void *cls,
3586 const struct sockaddr *addr,
3589 struct Plugin *plugin = cls;
3590 struct GNUNET_CONNECTION_Handle *sock;
3591 struct TCPProbeContext *tcp_probe_ctx;
3594 * We have received an ICMP response, ostensibly from a peer
3595 * that wants to connect to us! Send a message to establish a connection.
3597 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
3602 /* failed for some odd reason (out of sockets?); ignore attempt */
3606 tcp_probe_ctx = GNUNET_new (struct TCPProbeContext);
3607 tcp_probe_ctx->message.header.size
3608 = htons (sizeof (struct TCP_NAT_ProbeMessage));
3609 tcp_probe_ctx->message.header.type
3610 = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
3611 tcp_probe_ctx->message.clientIdentity
3612 = *plugin->env->my_identity;
3613 tcp_probe_ctx->plugin = plugin;
3614 tcp_probe_ctx->sock = sock;
3615 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
3618 tcp_probe_ctx->transmit_handle
3619 = GNUNET_CONNECTION_notify_transmit_ready (sock,
3620 ntohs (tcp_probe_ctx->message.header.size),
3621 GNUNET_TIME_UNIT_FOREVER_REL,
3628 * Function obtain the network type for a session
3630 * @param cls closure (`struct Plugin *`)
3631 * @param session the session
3632 * @return the network type in HBO or #GNUNET_SYSERR
3634 static enum GNUNET_NetworkType
3635 tcp_plugin_get_network (void *cls,
3636 struct GNUNET_ATS_Session *session)
3638 return session->scope;
3643 * Function obtain the network type for an address.
3645 * @param cls closure (`struct Plugin *`)
3646 * @param address the address
3647 * @return the network type
3649 static enum GNUNET_NetworkType
3650 tcp_plugin_get_network_for_address (void *cls,
3651 const struct GNUNET_HELLO_Address *address)
3653 struct Plugin *plugin = cls;
3655 struct sockaddr_in a4;
3656 struct sockaddr_in6 a6;
3657 const struct IPv4TcpAddress *t4;
3658 const struct IPv6TcpAddress *t6;
3662 addrlen = address->address_length;
3663 if (addrlen == sizeof(struct IPv6TcpAddress))
3665 GNUNET_assert (NULL != address->address); /* make static analysis happy */
3666 t6 = address->address;
3667 memset (&a6, 0, sizeof(a6));
3668 #if HAVE_SOCKADDR_IN_SIN_LEN
3669 a6.sin6_len = sizeof (a6);
3671 a6.sin6_family = AF_INET6;
3672 a6.sin6_port = t6->t6_port;
3673 GNUNET_memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
3677 else if (addrlen == sizeof(struct IPv4TcpAddress))
3679 GNUNET_assert (NULL != address->address); /* make static analysis happy */
3680 t4 = address->address;
3681 memset (&a4, 0, sizeof(a4));
3682 #if HAVE_SOCKADDR_IN_SIN_LEN
3683 a4.sin_len = sizeof (a4);
3685 a4.sin_family = AF_INET;
3686 a4.sin_port = t4->t4_port;
3687 a4.sin_addr.s_addr = t4->ipv4_addr;
3694 return GNUNET_NT_UNSPECIFIED;
3696 return plugin->env->get_address_type (plugin->env->cls,
3703 * Return information about the given session to the
3706 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3707 * @param peer peer we send information about
3708 * @param value our `struct GNUNET_ATS_Session` to send information about
3709 * @return #GNUNET_OK (continue to iterate)
3712 send_session_info_iter (void *cls,
3713 const struct GNUNET_PeerIdentity *peer,
3716 struct Plugin *plugin = cls;
3717 struct GNUNET_ATS_Session *session = value;
3719 notify_session_monitor (plugin,
3721 GNUNET_TRANSPORT_SS_INIT);
3722 /* FIXME: cannot tell if this is up or not from current
3724 notify_session_monitor (plugin,
3726 GNUNET_TRANSPORT_SS_UP);
3732 * Begin monitoring sessions of a plugin. There can only
3733 * be one active monitor per plugin (i.e. if there are
3734 * multiple monitors, the transport service needs to
3735 * multiplex the generated events over all of them).
3737 * @param cls closure of the plugin
3738 * @param sic callback to invoke, NULL to disable monitor;
3739 * plugin will being by iterating over all active
3740 * sessions immediately and then enter monitor mode
3741 * @param sic_cls closure for @a sic
3744 tcp_plugin_setup_monitor (void *cls,
3745 GNUNET_TRANSPORT_SessionInfoCallback sic,
3748 struct Plugin *plugin = cls;
3751 plugin->sic_cls = sic_cls;
3754 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
3755 &send_session_info_iter,
3757 /* signal end of first iteration */
3758 sic (sic_cls, NULL, NULL);
3764 * Entry point for the plugin.
3766 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
3767 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
3770 libgnunet_plugin_transport_xt_init (void *cls)
3772 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
3773 { &handle_tcp_welcome, NULL,
3774 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
3775 sizeof(struct WelcomeMessage) },
3776 { &handle_tcp_nat_probe, NULL,
3777 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
3778 sizeof(struct TCP_NAT_ProbeMessage) },
3779 { &handle_tcp_data, NULL,
3780 GNUNET_MESSAGE_TYPE_ALL, 0 },
3781 { NULL, NULL, 0, 0 }
3783 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3784 struct GNUNET_TRANSPORT_PluginFunctions *api;
3785 struct Plugin *plugin;
3786 struct LEGACY_SERVICE_Context *service;
3787 unsigned long long aport;
3788 unsigned long long bport;
3789 unsigned long long max_connections;
3791 struct GNUNET_TIME_Relative idle_timeout;
3793 struct GNUNET_NETWORK_Handle *const*lsocks;
3797 struct sockaddr **addrs;
3798 socklen_t *addrlens;
3800 if (NULL == env->receive)
3802 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3803 initialze the plugin or the API */
3804 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3806 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
3807 api->address_to_string = &tcp_plugin_address_to_string;
3808 api->string_to_address = &tcp_plugin_string_to_address;
3812 GNUNET_assert (NULL != env->cfg);
3814 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3818 max_connections = 128;
3822 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3827 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3829 "ADVERTISED-PORT", &aport)) &&
3832 LOG(GNUNET_ERROR_TYPE_ERROR,
3833 _("Require valid port number for service `%s' in configuration!\n"),
3843 service = LEGACY_SERVICE_start ("transport-xt",
3845 LEGACY_SERVICE_OPTION_NONE);
3846 if (NULL == service)
3848 LOG (GNUNET_ERROR_TYPE_WARNING,
3849 _("Failed to start service.\n"));
3857 plugin = GNUNET_new (struct Plugin);
3858 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections,
3860 plugin->max_connections = max_connections;
3861 plugin->open_port = bport;
3862 plugin->adv_port = aport;
3864 plugin->my_welcome.header.size = htons (sizeof(struct WelcomeMessage));
3865 plugin->my_welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
3866 plugin->my_welcome.clientIdentity = *plugin->env->my_identity;
3868 if ( (NULL != service) &&
3870 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3875 plugin->myoptions |= TCP_OPTIONS_TCP_STEALTH;
3876 lsocks = LEGACY_SERVICE_get_listen_sockets (service);
3879 uint32_t len = sizeof (struct WelcomeMessage);
3881 for (i=0;NULL!=lsocks[i];i++)
3884 GNUNET_NETWORK_socket_setsockopt (lsocks[i],
3888 sizeof (struct GNUNET_PeerIdentity))) ||
3890 GNUNET_NETWORK_socket_setsockopt (lsocks[i],
3892 TCP_STEALTH_INTEGRITY_LEN,
3896 /* TCP STEALTH not supported by kernel */
3897 GNUNET_assert (0 == i);
3898 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3899 _("TCP_STEALTH not supported on this platform.\n"));
3905 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3906 _("TCP_STEALTH not supported on this platform.\n"));
3911 if ( (NULL != service) &&
3914 get_server_addresses ("transport-xt",
3919 for (ret = ret_s-1; ret >= 0; ret--)
3920 LOG (GNUNET_ERROR_TYPE_INFO,
3921 "Binding to address `%s'\n",
3922 GNUNET_a2s (addrs[ret], addrlens[ret]));
3924 = GNUNET_NAT_register (env->cfg,
3927 (unsigned int) ret_s,
3928 (const struct sockaddr **) addrs,
3930 &tcp_nat_port_map_callback,
3931 &try_connection_reversal,
3933 for (ret = ret_s -1; ret >= 0; ret--)
3934 GNUNET_free (addrs[ret]);
3935 GNUNET_free_non_null (addrs);
3936 GNUNET_free_non_null (addrlens);
3940 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3947 &try_connection_reversal,
3950 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3952 api->send = &tcp_plugin_send;
3953 api->get_session = &tcp_plugin_get_session;
3954 api->disconnect_session = &tcp_plugin_disconnect_session;
3955 api->query_keepalive_factor = &tcp_plugin_query_keepalive_factor;
3956 api->disconnect_peer = &tcp_plugin_disconnect;
3957 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
3958 api->check_address = &tcp_plugin_check_address;
3959 api->address_to_string = &tcp_plugin_address_to_string;
3960 api->string_to_address = &tcp_plugin_string_to_address;
3961 api->get_network = &tcp_plugin_get_network;
3962 api->get_network_for_address = &tcp_plugin_get_network_for_address;
3963 api->update_session_timeout = &tcp_plugin_update_session_timeout;
3964 api->update_inbound_delay = &tcp_plugin_update_inbound_delay;
3965 api->setup_monitor = &tcp_plugin_setup_monitor;
3966 plugin->service = service;
3967 if (NULL != service)
3969 plugin->server = LEGACY_SERVICE_get_server (service);
3974 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3979 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3985 = GNUNET_SERVER_create_with_sockets (NULL,
3991 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
3992 GNUNET_memcpy (plugin->handlers,
3994 sizeof(my_handlers));
3995 for (i = 0;i < sizeof(my_handlers) / sizeof(struct GNUNET_SERVER_MessageHandler);i++)
3996 plugin->handlers[i].callback_cls = plugin;
3998 GNUNET_SERVER_add_handlers (plugin->server,
4000 GNUNET_SERVER_connect_notify (plugin->server,
4003 GNUNET_SERVER_disconnect_notify (plugin->server,
4006 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16,
4009 LOG (GNUNET_ERROR_TYPE_INFO,
4010 _("XT transport listening on port %llu\n"),
4013 LOG (GNUNET_ERROR_TYPE_INFO,
4014 _("XT transport not listening on any port (client only)\n"));
4015 if ( (aport != bport) &&
4017 LOG (GNUNET_ERROR_TYPE_INFO,
4018 _("XT transport advertises itself as being on port %llu\n"),
4020 /* Initially set connections to 0 */
4021 GNUNET_STATISTICS_set (plugin->env->stats,
4022 gettext_noop ("# XT sessions active"),
4028 if (NULL != plugin->nat)
4029 GNUNET_NAT_unregister (plugin->nat);
4030 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
4031 if (NULL != service)
4032 LEGACY_SERVICE_stop (service);
4033 GNUNET_free (plugin);
4034 GNUNET_free_non_null (api);
4040 * Exit point from the plugin.
4042 * @param cls the `struct GNUNET_TRANSPORT_PluginFunctions`
4046 libgnunet_plugin_transport_xt_done (void *cls)
4048 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
4049 struct Plugin *plugin = api->cls;
4050 struct TCPProbeContext *tcp_probe;
4051 struct PrettyPrinterContext *cur;
4052 struct PrettyPrinterContext *next;
4059 LOG (GNUNET_ERROR_TYPE_DEBUG,
4060 "Shutting down XT plugin\n");
4062 /* Removing leftover sessions */
4063 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
4064 &session_disconnect_it,
4066 /* Removing leftover NAT sessions */
4067 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
4068 &session_disconnect_it,
4071 for (cur = plugin->ppc_dll_head; NULL != cur; cur = next)
4074 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
4075 plugin->ppc_dll_tail,
4077 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
4078 cur->asc (cur->asc_cls,
4084 if (NULL != plugin->service)
4085 LEGACY_SERVICE_stop (plugin->service);
4087 GNUNET_SERVER_destroy (plugin->server);
4088 GNUNET_free (plugin->handlers);
4089 if (NULL != plugin->nat)
4090 GNUNET_NAT_unregister (plugin->nat);
4091 while (NULL != (tcp_probe = plugin->probe_head))
4093 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
4096 GNUNET_CONNECTION_destroy (tcp_probe->sock);
4097 GNUNET_free (tcp_probe);
4099 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
4100 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
4101 GNUNET_break (0 == plugin->cur_connections);
4102 GNUNET_free (plugin);
4107 /* end of plugin_transport_xt.c */