This file is part of GNUnet
Copyright (C) 2002--2015 GNUnet e.V.
- GNUnet is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 3, or (at your
- option) any later version.
+ GNUnet is free software: you can redistribute it and/or modify it
+ under the terms of the GNU Affero General Public License as published
+ by the Free Software Foundation, either version 3 of the License,
+ or (at your option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ Affero General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- Boston, MA 02110-1301, USA.
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+ SPDX-License-Identifier: AGPL3.0-or-later
*/
/**
* @file transport/plugin_transport_tcp.c
#include "gnunet_hello_lib.h"
#include "gnunet_constants.h"
#include "gnunet_util_lib.h"
-#include "gnunet_nat_lib.h"
+#include "gnunet_nat_service.h"
#include "gnunet_protocols.h"
#include "gnunet_resolver_service.h"
#include "gnunet_signatures.h"
*/
#define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
-GNUNET_NETWORK_STRUCT_BEGIN
+/**
+ * Opaque handle that can be used to cancel
+ * a transmit-ready notification.
+ */
+struct GNUNET_CONNECTION_TransmitHandle;
+
+/**
+ * @brief handle for a server
+ */
+struct GNUNET_SERVER_Handle;
+
+/**
+ * @brief opaque handle for a client of the server
+ */
+struct GNUNET_SERVER_Client;
+
+/**
+ * @brief opaque handle server returns for aborting transmission to a client.
+ */
+struct GNUNET_SERVER_TransmitHandle;
+
+/**
+ * @brief handle for a network connection
+ */
+struct GNUNET_CONNECTION_Handle;
+
+/**
+ * @brief handle for a network service
+ */
+struct LEGACY_SERVICE_Context;
+
+
+/**
+ * Stops a service that was started with #GNUNET_SERVICE_start().
+ *
+ * @param srv service to stop
+ */
+void
+LEGACY_SERVICE_stop (struct LEGACY_SERVICE_Context *srv);
+
+
+
+/**
+ * Function called to notify a client about the connection begin ready
+ * to queue more data. @a buf will be NULL and @a size zero if the
+ * connection was closed for writing in the meantime.
+ *
+ * @param cls closure
+ * @param size number of bytes available in @a buf
+ * @param buf where the callee should write the message
+ * @return number of bytes written to @a buf
+ */
+typedef size_t
+(*GNUNET_CONNECTION_TransmitReadyNotify) (void *cls,
+ size_t size,
+ void *buf);
+
+/**
+ * Credentials for UNIX domain sockets.
+ */
+struct GNUNET_CONNECTION_Credentials
+{
+ /**
+ * UID of the other end of the connection.
+ */
+ uid_t uid;
+
+ /**
+ * GID of the other end of the connection.
+ */
+ gid_t gid;
+};
+
+
+/**
+ * Functions with this signature are called whenever a client
+ * is disconnected on the network level.
+ *
+ * @param cls closure
+ * @param client identification of the client; NULL
+ * for the last call when the server is destroyed
+ */
+typedef void
+(*GNUNET_SERVER_DisconnectCallback) (void *cls,
+ struct GNUNET_SERVER_Client *client);
+
+
+/**
+ * Functions with this signature are called whenever a client
+ * is connected on the network level.
+ *
+ * @param cls closure
+ * @param client identification of the client
+ */
+typedef void
+(*GNUNET_SERVER_ConnectCallback) (void *cls,
+ struct GNUNET_SERVER_Client *client);
+
+
+
+
+/**
+ * Function to call for access control checks.
+ *
+ * @param cls closure
+ * @param ucred credentials, if available, otherwise NULL
+ * @param addr address
+ * @param addrlen length of address
+ * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
+ * for unknown address family (will be denied).
+ */
+typedef int
+(*GNUNET_CONNECTION_AccessCheck) (void *cls,
+ const struct
+ GNUNET_CONNECTION_Credentials *
+ ucred,
+ const struct sockaddr * addr,
+ socklen_t addrlen);
+
+/**
+ * Callback function for data received from the network. Note that
+ * both "available" and "err" would be 0 if the read simply timed out.
+ *
+ * @param cls closure
+ * @param buf pointer to received data
+ * @param available number of bytes availabe in "buf",
+ * possibly 0 (on errors)
+ * @param addr address of the sender
+ * @param addrlen size of addr
+ * @param errCode value of errno (on errors receiving)
+ */
+typedef void
+(*GNUNET_CONNECTION_Receiver) (void *cls, const void *buf,
+ size_t available,
+ const struct sockaddr * addr,
+ socklen_t addrlen, int errCode);
+
+
+
+/**
+ * Close the connection and free associated resources. There must
+ * not be any pending requests for reading or writing to the
+ * connection at this time.
+ *
+ * @param connection connection to destroy
+ */
+void
+GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection);
+
+
+/**
+ * Signature of a function to create a custom tokenizer.
+ *
+ * @param cls closure from #GNUNET_SERVER_set_callbacks
+ * @param client handle to client the tokenzier will be used for
+ * @return handle to custom tokenizer ('mst')
+ */
+typedef void*
+(*GNUNET_SERVER_MstCreateCallback) (void *cls,
+ struct GNUNET_SERVER_Client *client);
+
+
+/**
+ * Signature of a function to destroy a custom tokenizer.
+ *
+ * @param cls closure from #GNUNET_SERVER_set_callbacks
+ * @param mst custom tokenizer handle
+ */
+typedef void
+(*GNUNET_SERVER_MstDestroyCallback) (void *cls,
+ void *mst);
+
+/**
+ * Signature of a function to receive data for a custom tokenizer.
+ *
+ * @param cls closure from #GNUNET_SERVER_set_callbacks
+ * @param mst custom tokenizer handle
+ * @param client_identity ID of client for which this is a buffer,
+ * can be NULL (will be passed back to 'cb')
+ * @param buf input data to add
+ * @param size number of bytes in @a buf
+ * @param purge should any excess bytes in the buffer be discarded
+ * (i.e. for packet-based services like UDP)
+ * @param one_shot only call callback once, keep rest of message in buffer
+ * @return #GNUNET_OK if we are done processing (need more data)
+ * #GNUNET_NO if one_shot was set and we have another message ready
+ * #GNUNET_SYSERR if the data stream is corrupt
+ */
+typedef int
+(*GNUNET_SERVER_MstReceiveCallback) (void *cls, void *mst,
+ struct GNUNET_SERVER_Client *client,
+ const char *buf,
+ size_t size,
+ int purge,
+ int one_shot);
+/**
+ * Functions with this signature are called whenever a message is
+ * received.
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param message the actual message
+ */
+typedef void
+(*GNUNET_SERVER_MessageCallback) (void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message);
+
+/**
+ * Message handler. Each struct specifies how to handle on particular
+ * type of message received.
+ */
+struct GNUNET_SERVER_MessageHandler
+{
+ /**
+ * Function to call for messages of "type".
+ */
+ GNUNET_SERVER_MessageCallback callback;
+
+ /**
+ * Closure argument for @e callback.
+ */
+ void *callback_cls;
+
+ /**
+ * Type of the message this handler covers.
+ */
+ uint16_t type;
+
+ /**
+ * Expected size of messages of this type. Use 0 for
+ * variable-size. If non-zero, messages of the given
+ * type will be discarded (and the connection closed)
+ * if they do not have the right size.
+ */
+ uint16_t expected_size;
+
+};
+
+
+/**
+ * Options for the service (bitmask).
+ */
+enum LEGACY_SERVICE_Options
+{
+ /**
+ * Use defaults. Terminates all client connections and the listen
+ * sockets immediately upon receiving the shutdown signal.
+ */
+ LEGACY_SERVICE_OPTION_NONE = 0,
+
+ /**
+ * Do not trigger server shutdown on signal at all; instead, allow
+ * for the user to terminate the server explicitly when needed
+ * by calling #LEGACY_SERVICE_shutdown().
+ */
+ LEGACY_SERVICE_OPTION_MANUAL_SHUTDOWN = 1,
+
+ /**
+ * Trigger a SOFT server shutdown on signals, allowing active
+ * non-monitor clients to complete their transactions.
+ */
+ LEGACY_SERVICE_OPTION_SOFT_SHUTDOWN = 2
+};
+
+/**
+ * Ask the server to disconnect from the given client. This is the
+ * same as passing #GNUNET_SYSERR to #GNUNET_SERVER_receive_done,
+ * except that it allows dropping of a client even when not handling a
+ * message from that client.
+ *
+ * @param client the client to disconnect from
+ */
+void
+GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
+
+/**
+ * Return user context associated with the given client.
+ * Note: you should probably use the macro (call without the underscore).
+ *
+ * @param client client to query
+ * @param size number of bytes in user context struct (for verification only)
+ * @return pointer to user context
+ */
+void *
+GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client,
+ size_t size);
+
+
+/**
+ * Functions with this signature are called whenever a
+ * complete message is received by the tokenizer.
+ *
+ * Do not call #GNUNET_SERVER_mst_destroy from within
+ * the scope of this callback.
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param message the actual message
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
+ */
+typedef int
+(*GNUNET_SERVER_MessageTokenizerCallback) (void *cls,
+ void *client,
+ const struct GNUNET_MessageHeader *message);
+
+
+/**
+ * Create a message stream tokenizer.
+ *
+ * @param cb function to call on completed messages
+ * @param cb_cls closure for @a cb
+ * @return handle to tokenizer
+ */
+struct GNUNET_SERVER_MessageStreamTokenizer *
+GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
+ void *cb_cls);
+
+/**
+ * Add incoming data to the receive buffer and call the
+ * callback for all complete messages.
+ *
+ * @param mst tokenizer to use
+ * @param client_identity ID of client for which this is a buffer,
+ * can be NULL (will be passed back to 'cb')
+ * @param buf input data to add
+ * @param size number of bytes in @a buf
+ * @param purge should any excess bytes in the buffer be discarded
+ * (i.e. for packet-based services like UDP)
+ * @param one_shot only call callback once, keep rest of message in buffer
+ * @return #GNUNET_OK if we are done processing (need more data)
+ * #GNUNET_NO if one_shot was set and we have another message ready
+ * #GNUNET_SYSERR if the data stream is corrupt
+ */
+int
+GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
+ void *client_identity,
+ const char *buf, size_t size,
+ int purge, int one_shot);
+
+
+
+/**
+ * Destroys a tokenizer.
+ *
+ * @param mst tokenizer to destroy
+ */
+void
+GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
+
+
+/**
+ * Set user context to be associated with the given client.
+ * Note: you should probably use the macro (call without the underscore).
+ *
+ * @param client client to query
+ * @param ptr pointer to user context
+ * @param size number of bytes in user context struct (for verification only)
+ */
+void
+GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client,
+ void *ptr,
+ size_t size);
+/**
+ * Return user context associated with the given client.
+ *
+ * @param client client to query
+ * @param type expected return type (i.e. 'struct Foo')
+ * @return pointer to user context of type 'type *'.
+ */
+#define GNUNET_SERVER_client_get_user_context(client,type) \
+ (type *) GNUNET_SERVER_client_get_user_context_ (client, sizeof (type))
+
+/**
+ * Set user context to be associated with the given client.
+ *
+ * @param client client to query
+ * @param value pointer to user context
+ */
+#define GNUNET_SERVER_client_set_user_context(client,value) \
+ GNUNET_SERVER_client_set_user_context_ (client, value, sizeof (*value))
+
+
+
+/**
+ * Notify us when the server has enough space to transmit
+ * a message of the given size to the given client.
+ *
+ * @param client client to transmit message to
+ * @param size requested amount of buffer space
+ * @param timeout after how long should we give up (and call
+ * notify with buf NULL and size 0)?
+ * @param callback function to call when space is available
+ * @param callback_cls closure for @a callback
+ * @return non-NULL if the notify callback was queued; can be used
+ * to cancel the request using
+ * #GNUNET_SERVER_notify_transmit_ready_cancel.
+ * NULL if we are already going to notify someone else (busy)
+ */
+struct GNUNET_SERVER_TransmitHandle *
+GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
+ size_t size,
+ struct GNUNET_TIME_Relative timeout,
+ GNUNET_CONNECTION_TransmitReadyNotify callback,
+ void *callback_cls);
+
+/**
+ * Abort transmission request.
+ *
+ * @param th request to abort
+ */
+void
+GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th);
+
+
+
+
+/**
+ * Notify the server that the given client handle should
+ * be kept (keeps the connection up if possible, increments
+ * the internal reference counter).
+ *
+ * @param client the client to keep
+ */
+void
+GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
+
+
+/**
+ * Notify the server that the given client handle is no
+ * longer required. Decrements the reference counter. If
+ * that counter reaches zero an inactive connection maybe
+ * closed.
+ *
+ * @param client the client to drop
+ */
+void
+GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
+
+
+/**
+ * Function called by the service's run
+ * method to run service-specific setup code.
+ *
+ * @param cls closure
+ * @param server the initialized server
+ * @param cfg configuration to use
+ */
+typedef void
+(*LEGACY_SERVICE_Main) (void *cls,
+ struct GNUNET_SERVER_Handle *server,
+ const struct GNUNET_CONFIGURATION_Handle *cfg);
+
+
+
+/**
+ * Suspend accepting connections from the listen socket temporarily.
+ * Resume activity using #GNUNET_SERVER_resume.
+ *
+ * @param server server to stop accepting connections.
+ */
+void
+GNUNET_SERVER_suspend (struct GNUNET_SERVER_Handle *server);
+
+/**
+ * Notify us when the server has enough space to transmit
+ * a message of the given size to the given client.
+ *
+ * @param client client to transmit message to
+ * @param size requested amount of buffer space
+ * @param timeout after how long should we give up (and call
+ * notify with buf NULL and size 0)?
+ * @param callback function to call when space is available
+ * @param callback_cls closure for @a callback
+ * @return non-NULL if the notify callback was queued; can be used
+ * to cancel the request using
+ * #GNUNET_SERVER_notify_transmit_ready_cancel.
+ * NULL if we are already going to notify someone else (busy)
+ */
+struct GNUNET_SERVER_TransmitHandle *
+GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
+ size_t size,
+ struct GNUNET_TIME_Relative timeout,
+ GNUNET_CONNECTION_TransmitReadyNotify callback,
+ void *callback_cls);
+
+
+/**
+ * Add a TCP socket-based connection to the set of handles managed by
+ * this server. Use this function for outgoing (P2P) connections that
+ * we initiated (and where this server should process incoming
+ * messages).
+ *
+ * @param server the server to use
+ * @param connection the connection to manage (client must
+ * stop using this connection from now on)
+ * @return the client handle
+ */
+struct GNUNET_SERVER_Client *
+GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
+ struct GNUNET_CONNECTION_Handle *connection);
+
+
+/**
+ * Resume accepting connections from the listen socket.
+ *
+ * @param server server to resume accepting connections.
+ */
+void
+GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server);
+
+/**
+ * Free resources held by this server.
+ *
+ * @param server server to destroy
+ */
+void
+GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server);
+
+
+
+
+#include "tcp_connection_legacy.c"
+#include "tcp_server_mst_legacy.c"
+#include "tcp_server_legacy.c"
+#include "tcp_service_legacy.c"
+
+GNUNET_NETWORK_STRUCT_BEGIN
+
/**
* Initial handshake message for a session.
*/
/**
* Network type of the address.
*/
- enum GNUNET_ATS_Network_Type scope;
+ enum GNUNET_NetworkType scope;
/**
* Are we still expecting the welcome message? (#GNUNET_YES/#GNUNET_NO)
/**
* Handle to the network service.
*/
- struct GNUNET_SERVICE_Context *service;
+ struct LEGACY_SERVICE_Context *service;
/**
* Handle to the server for this service.
};
-/* begin of ancient copy-and-pasted code that should be
- specialized for TCP ...*/
-/**
- * Add the given UNIX domain path as an address to the
- * list (as the first entry).
- *
- * @param saddrs array to update
- * @param saddrlens where to store the address length
- * @param unixpath path to add
- * @param abstract #GNUNET_YES to add an abstract UNIX domain socket. This
- * parameter is ignore on systems other than LINUX
- */
-static void
-add_unixpath (struct sockaddr **saddrs,
- socklen_t *saddrlens,
- const char *unixpath,
- int abstract)
-{
-#ifdef AF_UNIX
- struct sockaddr_un *un;
-
- un = GNUNET_new (struct sockaddr_un);
- un->sun_family = AF_UNIX;
- strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1);
-#ifdef LINUX
- if (GNUNET_YES == abstract)
- un->sun_path[0] = '\0';
-#endif
-#if HAVE_SOCKADDR_IN_SIN_LEN
- un->sun_len = (u_char) sizeof (struct sockaddr_un);
-#endif
- *saddrs = (struct sockaddr *) un;
- *saddrlens = sizeof (struct sockaddr_un);
-#else
- /* this function should never be called
- * unless AF_UNIX is defined! */
- GNUNET_assert (0);
-#endif
-}
-
-
/**
* Get the list of addresses that a server for the given service
* should bind to.
* Our external IP address/port mapping has changed.
*
* @param cls closure, the `struct Plugin`
+ * @param app_ctx[in,out] location where the app can store stuff
+ * on add and retrieve it on remove
* @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
* the previous (now invalid) one
+ * @param ac address class the address belongs to
* @param addr either the previous or the new public IP address
* @param addrlen actual length of @a addr
*/
static void
tcp_nat_port_map_callback (void *cls,
+ void **app_ctx,
int add_remove,
- const struct sockaddr *addr,
+ enum GNUNET_NAT_AddressClass ac,
+ const struct sockaddr *addr,
socklen_t addrlen)
{
struct Plugin *plugin = cls;
void *arg;
size_t args;
- LOG(GNUNET_ERROR_TYPE_INFO,
- "NAT notification to %s address `%s'\n",
- (GNUNET_YES == add_remove) ? "add" : "remove",
- GNUNET_a2s (addr, addrlen));
+ (void) app_ctx;
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ "NAT notification to %s address `%s'\n",
+ (GNUNET_YES == add_remove) ? "add" : "remove",
+ GNUNET_a2s (addr, addrlen));
/* convert 'addr' to our internal format */
switch (addr->sa_family)
{
args = sizeof (t4);
break;
case AF_INET6:
+ if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
+ {
+ /* skip link local, we don't allow them in
+ #tcp_plugin_check_address() */
+ return;
+ }
GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
memset (&t6, 0, sizeof(t6));
- GNUNET_memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
- sizeof(struct in6_addr));
+ GNUNET_memcpy (&t6.ipv6_addr,
+ &((struct sockaddr_in6 *) addr)->sin6_addr,
+ sizeof(struct in6_addr));
t6.options = htonl (plugin->myoptions);
t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
arg = &t6;
}
/* modify our published address list */
GNUNET_assert ((args == sizeof (struct IPv4TcpAddress)) ||
- (args == sizeof (struct IPv6TcpAddress)));
+ (args == sizeof (struct IPv6TcpAddress)));
+ /* TODO: use 'ac' here in the future... */
address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
- PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
- plugin->env->notify_address (plugin->env->cls, add_remove, address);
- GNUNET_HELLO_address_free(address);
+ PLUGIN_NAME,
+ arg,
+ args,
+ GNUNET_HELLO_ADDRESS_INFO_NONE);
+ plugin->env->notify_address (plugin->env->cls,
+ add_remove,
+ address);
+ GNUNET_HELLO_address_free (address);
}
static struct GNUNET_ATS_Session *
create_session (struct Plugin *plugin,
const struct GNUNET_HELLO_Address *address,
- enum GNUNET_ATS_Network_Type scope,
+ enum GNUNET_NetworkType scope,
struct GNUNET_SERVER_Client *client,
int is_nat)
{
GNUNET_assert (NULL == client);
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Creating new session for peer `%4s' at address %s\n",
+ "Creating new session for peer `%s' at address %s\n",
GNUNET_i2s (&address->peer),
tcp_plugin_address_to_string (plugin,
address->address,
if (NULL == buf)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
+ "Timeout trying to transmit to peer `%s', discarding message queue.\n",
GNUNET_i2s (&session->target));
/* timeout; cancel all messages that have already expired */
hd = NULL;
GNUNET_assert (pos->message_size <= session->bytes_in_queue);
session->bytes_in_queue -= pos->message_size;
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Failed to transmit %u byte message to `%4s'.\n",
+ "Failed to transmit %u byte message to `%s'.\n",
pos->message_size,
GNUNET_i2s (&session->target));
ret += pos->message_size;
pm->transmit_cont = cont;
pm->transmit_cont_cls = cont_cls;
- LOG(GNUNET_ERROR_TYPE_DEBUG,
- "Asked to transmit %u bytes to `%s', added message to list.\n",
- msgbuf_size,
- GNUNET_i2s (&session->target));
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Asked to transmit %u bytes to `%s', added message to list.\n",
+ msgbuf_size,
+ GNUNET_i2s (&session->target));
if (GNUNET_YES ==
GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
const struct IPv4TcpAddress *t4;
const struct IPv6TcpAddress *t6;
unsigned int options;
- enum GNUNET_ATS_Network_Type net_type;
+ enum GNUNET_NetworkType net_type;
unsigned int is_natd = GNUNET_NO;
size_t addrlen;
#ifdef TCP_STEALTH
address->address_length));
return session;
}
+ /* This is a bit of a hack, limiting TCP to never allow more than
+ one TCP connection to any given peer at the same time.
+ Without this, peers sometimes disagree about which of the TCP
+ connections they should use, causing one side to believe that
+ they transmit successfully, while the other receives nothing. */
+ return NULL; /* Refuse to have more than one TCP connection per
+ peer pair at the same time. */
}
if (addrlen == sizeof(struct IPv6TcpAddress))
net_type = plugin->env->get_address_type (plugin->env->cls,
sb,
sbs);
- GNUNET_break (net_type != GNUNET_ATS_NET_UNSPECIFIED);
+ GNUNET_break (net_type != GNUNET_NT_UNSPECIFIED);
if ( (is_natd == GNUNET_YES) &&
(addrlen == sizeof(struct IPv6TcpAddress)) )
GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
&address->peer)))
{
+ struct sockaddr_in local_sa;
+
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Found valid IPv4 NAT address (creating session)!\n");
session = create_session (plugin,
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Created NAT WAIT connection to `%4s' at `%s'\n",
+ "Created NAT WAIT connection to `%s' at `%s'\n",
GNUNET_i2s (&session->target),
GNUNET_a2s (sb, sbs));
- if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
- {
+ memset (&local_sa,
+ 0,
+ sizeof (local_sa));
+ local_sa.sin_family = AF_INET;
+ local_sa.sin_port = htons (plugin->open_port);
+ /* We leave sin_address at 0, let the kernel figure it out,
+ even if our bind() is more specific. (May want to reconsider
+ later.) */
+ if (GNUNET_OK ==
+ GNUNET_NAT_request_reversal (plugin->nat,
+ &local_sa,
+ &a4))
return session;
- }
- else
- {
- LOG(GNUNET_ERROR_TYPE_DEBUG,
- "Running NAT client for `%4s' at `%s' failed\n",
- GNUNET_i2s (&session->target),
- GNUNET_a2s (sb, sbs));
- tcp_plugin_disconnect_session (plugin,
- session);
- return NULL;
- }
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Running NAT client for `%s' at `%s' failed\n",
+ GNUNET_i2s (&session->target),
+ GNUNET_a2s (sb, sbs));
+ tcp_plugin_disconnect_session (plugin,
+ session);
+ return NULL;
}
/* create new outbound session */
if (NULL == sa)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Failed to create connection to `%4s' at `%s'\n",
+ "Failed to create connection to `%s' at `%s'\n",
GNUNET_i2s (&address->peer),
GNUNET_a2s (sb, sbs));
return NULL;
}
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
+ "Asked to transmit to `%s', creating fresh session using address `%s'.\n",
GNUNET_i2s (&address->peer),
GNUNET_a2s (sb, sbs));
struct Plugin *plugin = cls;
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Disconnecting peer `%4s'\n",
+ "Disconnecting peer `%s'\n",
GNUNET_i2s (target));
GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
target,
}
-/**
- * Check if the given port is plausible (must be either our listen
- * port or our advertised port), or any port if we are behind NAT
- * and do not have a port open. If it is neither, we return
- * #GNUNET_SYSERR.
- *
- * @param plugin global variables
- * @param in_port port number to check
- * @return #GNUNET_OK if port is either open_port or adv_port
- */
-static int
-check_port (struct Plugin *plugin,
- uint16_t in_port)
-{
- if ( (in_port == plugin->adv_port) ||
- (in_port == plugin->open_port) )
- return GNUNET_OK;
- return GNUNET_SYSERR;
-}
-
-
/**
* Function that will be called to check if a binary address for this
* plugin is well-formed and corresponds to an address for THIS peer
if (addrlen == sizeof(struct IPv4TcpAddress))
{
+ struct sockaddr_in s4;
+
v4 = (const struct IPv4TcpAddress *) addr;
if (0 != memcmp (&v4->options,
&plugin->myoptions,
GNUNET_break (0);
return GNUNET_SYSERR;
}
- if (GNUNET_OK != check_port (plugin,
- ntohs (v4->t4_port)))
- return GNUNET_SYSERR;
+ memset (&s4, 0, sizeof (s4));
+ s4.sin_family = AF_INET;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ s4.sin_len = sizeof (s4);
+#endif
+ s4.sin_port = v4->t4_port;
+ s4.sin_addr.s_addr = v4->ipv4_addr;
+
if (GNUNET_OK !=
GNUNET_NAT_test_address (plugin->nat,
- &v4->ipv4_addr,
- sizeof (struct in_addr)))
+ &s4,
+ sizeof (struct sockaddr_in)))
return GNUNET_SYSERR;
}
else
{
+ struct sockaddr_in6 s6;
+
v6 = (const struct IPv6TcpAddress *) addr;
if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
{
GNUNET_break (0);
return GNUNET_SYSERR;
}
- if (GNUNET_OK != check_port (plugin,
- ntohs (v6->t6_port)))
- return GNUNET_SYSERR;
+ memset (&s6, 0, sizeof (s6));
+ s6.sin6_family = AF_INET6;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ s6.sin6_len = sizeof (s6);
+#endif
+ s6.sin6_port = v6->t6_port;
+ s6.sin6_addr = v6->ipv6_addr;
+
if (GNUNET_OK !=
GNUNET_NAT_test_address (plugin->nat,
- &v6->ipv6_addr,
- sizeof(struct in6_addr)))
+ &s6,
+ sizeof(struct sockaddr_in6)))
return GNUNET_SYSERR;
}
return GNUNET_OK;
sizeof(struct GNUNET_PeerIdentity)))
{
/* refuse connections from ourselves */
- GNUNET_SERVER_receive_done (client,
- GNUNET_SYSERR);
if (GNUNET_OK ==
GNUNET_SERVER_client_get_address (client,
&vaddr,
&alen))
{
LOG (GNUNET_ERROR_TYPE_INFO,
- "Received WELCOME message from my own identity `%4s' on address `%s'\n",
+ "Received WELCOME message from my own identity `%s' on address `%s'\n",
GNUNET_i2s (&wm->clientIdentity),
GNUNET_a2s (vaddr, alen));
- GNUNET_free(vaddr);
+ GNUNET_free (vaddr);
}
+ GNUNET_SERVER_receive_done (client,
+ GNUNET_SYSERR);
return;
}
- LOG(GNUNET_ERROR_TYPE_DEBUG,
- "Received WELCOME message from `%4s' %p\n",
- GNUNET_i2s (&wm->clientIdentity),
- client);
+ if (GNUNET_OK ==
+ GNUNET_SERVER_client_get_address (client,
+ &vaddr,
+ &alen))
+ {
+ LOG(GNUNET_ERROR_TYPE_DEBUG,
+ "Received WELCOME message from `%s' on address `%s'\n",
+ GNUNET_i2s (&wm->clientIdentity),
+ GNUNET_a2s (vaddr, alen));
+ GNUNET_free (vaddr);
+ }
GNUNET_STATISTICS_update (plugin->env->stats,
gettext_noop ("# TCP WELCOME messages received"),
1,
GNUNET_NO);
- session = lookup_session_by_client (plugin, client);
+ session = lookup_session_by_client (plugin,
+ client);
if (NULL != session)
{
if (GNUNET_OK ==
- GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
+ GNUNET_SERVER_client_get_address (client,
+ &vaddr,
+ &alen))
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Found existing session %p for peer `%s'\n",
alen),
client,
GNUNET_NO);
- GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != session->scope);
+ GNUNET_break (GNUNET_NT_UNSPECIFIED != session->scope);
GNUNET_HELLO_address_free (address);
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Creating new%s session %p for peer `%s' client %p\n",
}
}
- if (session->expecting_welcome != GNUNET_YES)
+ if (GNUNET_YES != session->expecting_welcome)
{
- GNUNET_break_op(0);
+ GNUNET_break_op (0);
GNUNET_SERVER_receive_done (client,
GNUNET_SYSERR);
- GNUNET_break(0);
return;
}
session->last_activity = GNUNET_TIME_absolute_get ();
if (NULL == session)
{
/* No inbound session found */
- void *vaddr;
+ void *vaddr = NULL;
size_t alen;
- GNUNET_SERVER_client_get_address (client,
- &vaddr,
- &alen);
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_SERVER_client_get_address (client,
+ &vaddr,
+ &alen));
LOG (GNUNET_ERROR_TYPE_ERROR,
"Received unexpected %u bytes of type %u from `%s'\n",
(unsigned int) ntohs (message->size),
if (GNUNET_YES == session->expecting_welcome)
{
/* Session is expecting WELCOME message */
- void *vaddr;
+ void *vaddr = NULL;
size_t alen;
- GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
+ GNUNET_SERVER_client_get_address (client,
+ &vaddr,
+ &alen);
LOG (GNUNET_ERROR_TYPE_ERROR,
"Received unexpected %u bytes of type %u from `%s'\n",
(unsigned int) ntohs (message->size),
}
session->last_activity = GNUNET_TIME_absolute_get ();
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Passing %u bytes of type %u from `%4s' to transport service.\n",
- (unsigned int) ntohs (message->size),
- (unsigned int) ntohs (message->type),
- GNUNET_i2s (&session->target));
+ {
+ void *vaddr = NULL;
+ size_t alen;
+
+ GNUNET_SERVER_client_get_address (client,
+ &vaddr,
+ &alen);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Passing %u bytes of type %u from `%s' at %s to transport service.\n",
+ (unsigned int) ntohs (message->size),
+ (unsigned int) ntohs (message->type),
+ GNUNET_i2s (&session->target),
+ GNUNET_a2s (vaddr, alen));
+ GNUNET_free_non_null (vaddr);
+ }
GNUNET_STATISTICS_update (plugin->env->stats,
gettext_noop ("# bytes received via TCP"),
if (NULL == session)
return; /* unknown, nothing to do */
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Destroying session of `%4s' with %s due to network-level disconnect.\n",
+ "Destroying session of `%s' with %s due to network-level disconnect.\n",
GNUNET_i2s (&session->target),
tcp_plugin_address_to_string (session->plugin,
session->address->address,
* @param session the session
* @return the network type in HBO or #GNUNET_SYSERR
*/
-static enum GNUNET_ATS_Network_Type
+static enum GNUNET_NetworkType
tcp_plugin_get_network (void *cls,
struct GNUNET_ATS_Session *session)
{
* @param address the address
* @return the network type
*/
-static enum GNUNET_ATS_Network_Type
+static enum GNUNET_NetworkType
tcp_plugin_get_network_for_address (void *cls,
const struct GNUNET_HELLO_Address *address)
{
else
{
GNUNET_break (0);
- return GNUNET_ATS_NET_UNSPECIFIED;
+ return GNUNET_NT_UNSPECIFIED;
}
return plugin->env->get_address_type (plugin->env->cls,
sb,
struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
struct GNUNET_TRANSPORT_PluginFunctions *api;
struct Plugin *plugin;
- struct GNUNET_SERVICE_Context *service;
+ struct LEGACY_SERVICE_Context *service;
unsigned long long aport;
unsigned long long bport;
unsigned long long max_connections;
aport = 0;
if (0 != bport)
{
- service = GNUNET_SERVICE_start ("transport-tcp",
+ service = LEGACY_SERVICE_start ("transport-tcp",
env->cfg,
- GNUNET_SERVICE_OPTION_NONE);
+ LEGACY_SERVICE_OPTION_NONE);
if (NULL == service)
{
LOG (GNUNET_ERROR_TYPE_WARNING,
{
#ifdef TCP_STEALTH
plugin->myoptions |= TCP_OPTIONS_TCP_STEALTH;
- lsocks = GNUNET_SERVICE_get_listen_sockets (service);
+ lsocks = LEGACY_SERVICE_get_listen_sockets (service);
if (NULL != lsocks)
{
uint32_t len = sizeof (struct WelcomeMessage);
GNUNET_a2s (addrs[ret], addrlens[ret]));
plugin->nat
= GNUNET_NAT_register (env->cfg,
- GNUNET_YES,
- aport,
+ "transport-tcp",
+ IPPROTO_TCP,
(unsigned int) ret_s,
(const struct sockaddr **) addrs,
addrlens,
&tcp_nat_port_map_callback,
&try_connection_reversal,
- plugin,
- NULL);
+ plugin);
for (ret = ret_s -1; ret >= 0; ret--)
GNUNET_free (addrs[ret]);
GNUNET_free_non_null (addrs);
else
{
plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
- GNUNET_YES,
- 0,
+ "transport-tcp",
+ IPPROTO_TCP,
0,
NULL,
NULL,
NULL,
&try_connection_reversal,
- plugin,
- NULL);
+ plugin);
}
api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
api->cls = plugin;
plugin->service = service;
if (NULL != service)
{
- plugin->server = GNUNET_SERVICE_get_server (service);
+ plugin->server = LEGACY_SERVICE_get_server (service);
}
else
{
GNUNET_NAT_unregister (plugin->nat);
GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
if (NULL != service)
- GNUNET_SERVICE_stop (service);
+ LEGACY_SERVICE_stop (service);
GNUNET_free (plugin);
GNUNET_free_non_null (api);
return NULL;
}
if (NULL != plugin->service)
- GNUNET_SERVICE_stop (plugin->service);
+ LEGACY_SERVICE_stop (plugin->service);
else
GNUNET_SERVER_destroy (plugin->server);
GNUNET_free (plugin->handlers);