GNUnet  0.20.0
plugin_transport_tcp.c File Reference

Implementation of the TCP transport service. More...

#include "platform.h"
#include "gnunet_hello_lib.h"
#include "gnunet_constants.h"
#include "gnunet_util_lib.h"
#include "gnunet_nat_service.h"
#include "gnunet_protocols.h"
#include "gnunet_resolver_service.h"
#include "gnunet_signatures.h"
#include "gnunet_statistics_service.h"
#include "gnunet_transport_service.h"
#include "gnunet_transport_plugin.h"
#include "transport.h"
#include "tcp_connection_legacy.c"
#include "tcp_server_mst_legacy.c"
#include "tcp_server_legacy.c"
#include "tcp_service_legacy.c"
Include dependency graph for plugin_transport_tcp.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_CONNECTION_Credentials
 Credentials for UNIX domain sockets. More...
 
struct  GNUNET_SERVER_MessageHandler
 Message handler. More...
 
struct  WelcomeMessage
 Initial handshake message for a session. More...
 
struct  TCP_NAT_ProbeMessage
 Basically a WELCOME message, but with the purpose of giving the waiting peer a client handle to use. More...
 
struct  TCPProbeContext
 Context for sending a NAT probe via TCP. More...
 
struct  IPv4TcpAddress
 Network format for IPv4 addresses. More...
 
struct  IPv6TcpAddress
 Network format for IPv6 addresses. More...
 
struct  PendingMessage
 List containing all messages that are yet to be send. More...
 
struct  GNUNET_ATS_Session
 Session handle for connections. More...
 
struct  PrettyPrinterContext
 Closure for append_port(). More...
 
struct  Plugin
 Handle for a plugin. More...
 
struct  GNUNET_ATS_SessionItCtx
 Closure for session_lookup_it(). More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "transport-tcp", __VA_ARGS__)
 
#define PLUGIN_NAME   "tcp"
 
#define NAT_TIMEOUT   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
 How long until we give up on establishing an NAT connection? Must be > 4 RTT. More...
 
#define GNUNET_SERVER_client_get_user_context(client, type)    (type *) GNUNET_SERVER_client_get_user_context_ (client, sizeof(type))
 Return user context associated with the given client. More...
 
#define GNUNET_SERVER_client_set_user_context(client, value)    GNUNET_SERVER_client_set_user_context_ (client, value, sizeof(*value))
 Set user context to be associated with the given client. More...
 

Typedefs

typedef size_t(* GNUNET_CONNECTION_TransmitReadyNotify) (void *cls, size_t size, void *buf)
 Function called to notify a client about the connection begin ready to queue more data. More...
 
typedef void(* GNUNET_SERVER_DisconnectCallback) (void *cls, struct GNUNET_SERVER_Client *client)
 Functions with this signature are called whenever a client is disconnected on the network level. More...
 
typedef void(* GNUNET_SERVER_ConnectCallback) (void *cls, struct GNUNET_SERVER_Client *client)
 Functions with this signature are called whenever a client is connected on the network level. More...
 
typedef int(* GNUNET_CONNECTION_AccessCheck) (void *cls, const struct GNUNET_CONNECTION_Credentials *ucred, const struct sockaddr *addr, socklen_t addrlen)
 Function to call for access control checks. More...
 
typedef void(* GNUNET_CONNECTION_Receiver) (void *cls, const void *buf, size_t available, const struct sockaddr *addr, socklen_t addrlen, int errCode)
 Callback function for data received from the network. More...
 
typedef void *(* GNUNET_SERVER_MstCreateCallback) (void *cls, struct GNUNET_SERVER_Client *client)
 Signature of a function to create a custom tokenizer. More...
 
typedef void(* GNUNET_SERVER_MstDestroyCallback) (void *cls, void *mst)
 Signature of a function to destroy a custom tokenizer. More...
 
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)
 Signature of a function to receive data for a custom tokenizer. More...
 
typedef void(* GNUNET_SERVER_MessageCallback) (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message)
 Functions with this signature are called whenever a message is received. More...
 
typedef int(* GNUNET_SERVER_MessageTokenizerCallback) (void *cls, void *client, const struct GNUNET_MessageHeader *message)
 Functions with this signature are called whenever a complete message is received by the tokenizer. More...
 
typedef void(* LEGACY_SERVICE_Main) (void *cls, struct GNUNET_SERVER_Handle *server, const struct GNUNET_CONFIGURATION_Handle *cfg)
 Function called by the service's run method to run service-specific setup code. More...
 

Enumerations

enum  LEGACY_SERVICE_Options { LEGACY_SERVICE_OPTION_NONE = 0 , LEGACY_SERVICE_OPTION_MANUAL_SHUTDOWN = 1 , LEGACY_SERVICE_OPTION_SOFT_SHUTDOWN = 2 }
 Options for the service (bitmask). More...
 
enum  TcpAddressOptions { TCP_OPTIONS_NONE = 0 , TCP_OPTIONS_RESERVED = 1 , TCP_OPTIONS_TCP_STEALTH = 2 }
 Bits in the options field of TCP addresses. More...
 

Functions

void LEGACY_SERVICE_stop (struct LEGACY_SERVICE_Context *srv)
 Stops a service that was started with GNUNET_SERVICE_start(). More...
 
void GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection)
 Close the connection and free associated resources. More...
 
void GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
 Ask the server to disconnect from the given client. More...
 
void * GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client, size_t size)
 Return user context associated with the given client. More...
 
struct GNUNET_SERVER_MessageStreamTokenizerGNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb, void *cb_cls)
 Create a message stream tokenizer. More...
 
int GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst, void *client_identity, const char *buf, size_t size, int purge, int one_shot)
 Add incoming data to the receive buffer and call the callback for all complete messages. More...
 
void GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst)
 Destroys a tokenizer. More...
 
void GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client, void *ptr, size_t size)
 Set user context to be associated with the given client. More...
 
struct GNUNET_SERVER_TransmitHandleGNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client, size_t size, struct GNUNET_TIME_Relative timeout, GNUNET_CONNECTION_TransmitReadyNotify callback, void *callback_cls)
 Notify us when the server has enough space to transmit a message of the given size to the given client. More...
 
void GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th)
 Abort transmission request. More...
 
void GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
 Notify the server that the given client handle should be kept (keeps the connection up if possible, increments the internal reference counter). More...
 
void GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
 Notify the server that the given client handle is no longer required. More...
 
void GNUNET_SERVER_suspend (struct GNUNET_SERVER_Handle *server)
 Suspend accepting connections from the listen socket temporarily. More...
 
struct GNUNET_SERVER_ClientGNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server, struct GNUNET_CONNECTION_Handle *connection)
 Add a TCP socket-based connection to the set of handles managed by this server. More...
 
void GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server)
 Resume accepting connections from the listen socket. More...
 
void GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server)
 Free resources held by this server. More...
 
static int get_server_addresses (const char *service_name, const struct GNUNET_CONFIGURATION_Handle *cfg, struct sockaddr ***addrs, socklen_t **addr_lens)
 Get the list of addresses that a server for the given service should bind to. More...
 
static void notify_session_monitor (struct Plugin *plugin, struct GNUNET_ATS_Session *session, enum GNUNET_TRANSPORT_SessionState state)
 If a session monitor is attached, notify it about the new session state. More...
 
static void tcp_nat_port_map_callback (void *cls, void **app_ctx, int add_remove, enum GNUNET_NAT_AddressClass ac, const struct sockaddr *addr, socklen_t addrlen)
 Our external IP address/port mapping has changed. More...
 
static const char * tcp_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
 Function called for a quick conversion of the binary address to a numeric address. More...
 
static int tcp_plugin_string_to_address (void *cls, const char *addr, uint16_t addrlen, void **buf, size_t *added)
 Function called to convert a string address to a binary address. More...
 
static struct GNUNET_ATS_Sessionlookup_session_by_client (struct Plugin *plugin, struct GNUNET_SERVER_Client *client)
 Find the session handle for the given client. More...
 
static int tcp_plugin_disconnect_session (void *cls, struct GNUNET_ATS_Session *session)
 Functions with this signature are called whenever we need to close a session due to a disconnect or failure to establish a connection. More...
 
static unsigned int tcp_plugin_query_keepalive_factor (void *cls)
 Function that is called to get the keepalive factor. More...
 
static void session_timeout (void *cls)
 Session was idle for too long, so disconnect it. More...
 
static void reschedule_session_timeout (struct GNUNET_ATS_Session *s)
 Increment session timeout due to activity. More...
 
static struct GNUNET_ATS_Sessioncreate_session (struct Plugin *plugin, const struct GNUNET_HELLO_Address *address, enum GNUNET_NetworkType scope, struct GNUNET_SERVER_Client *client, int is_nat)
 Create a new session. More...
 
static void process_pending_messages (struct GNUNET_ATS_Session *session)
 If we have pending messages, ask the server to transmit them (schedule the respective tasks, etc.) More...
 
static size_t do_transmit (void *cls, size_t size, void *buf)
 Function called to notify a client about the socket being ready to queue more data. More...
 
static ssize_t tcp_plugin_send (void *cls, struct GNUNET_ATS_Session *session, const char *msgbuf, size_t msgbuf_size, unsigned int priority, struct GNUNET_TIME_Relative to, GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
 Function that can be used by the transport service to transmit a message using the plugin. More...
 
static int session_lookup_it (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
 Look for a session by address. More...
 
static void nat_connect_timeout (void *cls)
 Task cleaning up a NAT connection attempt after timeout. More...
 
static void tcp_plugin_update_session_timeout (void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_ATS_Session *session)
 Function that will be called whenever the transport service wants to notify the plugin that a session is still active and in use and therefore the session timeout for this session has to be updated. More...
 
static void delayed_done (void *cls)
 Task to signal the server that we can continue receiving from the TCP client now. More...
 
static void tcp_plugin_update_inbound_delay (void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_ATS_Session *session, struct GNUNET_TIME_Relative delay)
 Function that will be called whenever the transport service wants to notify the plugin that the inbound quota changed and that the plugin should update it's delay for the next receive value. More...
 
static struct GNUNET_ATS_Sessiontcp_plugin_get_session (void *cls, const struct GNUNET_HELLO_Address *address)
 Create a new session to transmit data to the target This session will used to send data to this peer and the plugin will notify us by calling the env->session_end function. More...
 
static int session_disconnect_it (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
 We have been asked to destroy all connections to a particular peer. More...
 
static void tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
 Function that can be called to force a disconnect from the specified neighbour. More...
 
static void append_port (void *cls, const char *hostname)
 We are processing an address pretty printing request and finished the IP resolution (if applicable). More...
 
static void tcp_plugin_address_pretty_printer (void *cls, const char *type, const void *addr, size_t addrlen, int numeric, struct GNUNET_TIME_Relative timeout, GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
 Convert the transports address to a nice, human-readable format. More...
 
static int tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
 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 (as per our configuration). More...
 
static void handle_tcp_nat_probe (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message)
 We've received a nat probe from this peer via TCP. More...
 
static void handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message)
 We've received a welcome from this peer via TCP. More...
 
static void handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message)
 We've received data for this peer via TCP. More...
 
static void connect_notify (void *cls, struct GNUNET_SERVER_Client *client)
 Function called whenever a peer is connected on the "SERVER" level. More...
 
static void disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
 Function called whenever a peer is disconnected on the "SERVER" level. More...
 
static size_t notify_send_probe (void *cls, size_t size, void *buf)
 We can now send a probe message, copy into buffer to really send. More...
 
static void try_connection_reversal (void *cls, const struct sockaddr *addr, socklen_t addrlen)
 Function called by the NAT subsystem suggesting another peer wants to connect to us via connection reversal. More...
 
static enum GNUNET_NetworkType tcp_plugin_get_network (void *cls, struct GNUNET_ATS_Session *session)
 Function obtain the network type for a session. More...
 
static enum GNUNET_NetworkType tcp_plugin_get_network_for_address (void *cls, const struct GNUNET_HELLO_Address *address)
 Function obtain the network type for an address. More...
 
static int send_session_info_iter (void *cls, const struct GNUNET_PeerIdentity *peer, void *value)
 Return information about the given session to the monitor callback. More...
 
static void tcp_plugin_setup_monitor (void *cls, GNUNET_TRANSPORT_SessionInfoCallback sic, void *sic_cls)
 Begin monitoring sessions of a plugin. More...
 
void * libgnunet_plugin_transport_tcp_init (void *cls)
 Entry point for the plugin. More...
 
void * libgnunet_plugin_transport_tcp_done (void *cls)
 Exit point from the plugin. More...
 

Detailed Description

Implementation of the TCP transport service.

Author
Christian Grothoff

Definition in file plugin_transport_tcp.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "transport-tcp", __VA_ARGS__)

Definition at line 38 of file plugin_transport_tcp.c.

◆ PLUGIN_NAME

#define PLUGIN_NAME   "tcp"

Definition at line 40 of file plugin_transport_tcp.c.

◆ NAT_TIMEOUT

How long until we give up on establishing an NAT connection? Must be > 4 RTT.

Definition at line 46 of file plugin_transport_tcp.c.

◆ GNUNET_SERVER_client_get_user_context

#define GNUNET_SERVER_client_get_user_context (   client,
  type 
)     (type *) GNUNET_SERVER_client_get_user_context_ (client, sizeof(type))

Return user context associated with the given client.

Parameters
clientclient to query
typeexpected return type (i.e. 'struct Foo')
Returns
pointer to user context of type 'type *'.

Definition at line 413 of file plugin_transport_tcp.c.

◆ GNUNET_SERVER_client_set_user_context

#define GNUNET_SERVER_client_set_user_context (   client,
  value 
)     GNUNET_SERVER_client_set_user_context_ (client, value, sizeof(*value))

Set user context to be associated with the given client.

Parameters
clientclient to query
valuepointer to user context

Definition at line 422 of file plugin_transport_tcp.c.

Typedef Documentation

◆ GNUNET_CONNECTION_TransmitReadyNotify

typedef size_t(* GNUNET_CONNECTION_TransmitReadyNotify) (void *cls, size_t size, void *buf)

Function called to notify a client about the connection begin ready to queue more data.

buf will be NULL and size zero if the connection was closed for writing in the meantime.

Parameters
clsclosure
sizenumber of bytes available in buf
bufwhere the callee should write the message
Returns
number of bytes written to buf

Definition at line 99 of file plugin_transport_tcp.c.

◆ GNUNET_SERVER_DisconnectCallback

typedef void(* GNUNET_SERVER_DisconnectCallback) (void *cls, struct GNUNET_SERVER_Client *client)

Functions with this signature are called whenever a client is disconnected on the network level.

Parameters
clsclosure
clientidentification of the client; NULL for the last call when the server is destroyed

Definition at line 128 of file plugin_transport_tcp.c.

◆ GNUNET_SERVER_ConnectCallback

typedef void(* GNUNET_SERVER_ConnectCallback) (void *cls, struct GNUNET_SERVER_Client *client)

Functions with this signature are called whenever a client is connected on the network level.

Parameters
clsclosure
clientidentification of the client

Definition at line 140 of file plugin_transport_tcp.c.

◆ GNUNET_CONNECTION_AccessCheck

typedef int(* GNUNET_CONNECTION_AccessCheck) (void *cls, const struct GNUNET_CONNECTION_Credentials *ucred, const struct sockaddr *addr, socklen_t addrlen)

Function to call for access control checks.

Parameters
clsclosure
ucredcredentials, if available, otherwise NULL
addraddress
addrlenlength of address
Returns
GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR for unknown address family (will be denied).

Definition at line 155 of file plugin_transport_tcp.c.

◆ GNUNET_CONNECTION_Receiver

typedef void(* GNUNET_CONNECTION_Receiver) (void *cls, const void *buf, size_t available, const struct sockaddr *addr, socklen_t addrlen, int errCode)

Callback function for data received from the network.

Note that both "available" and "err" would be 0 if the read simply timed out.

Parameters
clsclosure
bufpointer to received data
availablenumber of bytes available in "buf", possibly 0 (on errors)
addraddress of the sender
addrlensize of addr
errCodevalue of errno (on errors receiving)

Definition at line 173 of file plugin_transport_tcp.c.

◆ GNUNET_SERVER_MstCreateCallback

typedef void*(* GNUNET_SERVER_MstCreateCallback) (void *cls, struct GNUNET_SERVER_Client *client)

Signature of a function to create a custom tokenizer.

Parameters
clsclosure from GNUNET_SERVER_set_callbacks
clienthandle to client the tokenzier will be used for
Returns
handle to custom tokenizer ('mst')

Definition at line 199 of file plugin_transport_tcp.c.

◆ GNUNET_SERVER_MstDestroyCallback

typedef void(* GNUNET_SERVER_MstDestroyCallback) (void *cls, void *mst)

Signature of a function to destroy a custom tokenizer.

Parameters
clsclosure from GNUNET_SERVER_set_callbacks
mstcustom tokenizer handle

Definition at line 210 of file plugin_transport_tcp.c.

◆ GNUNET_SERVER_MstReceiveCallback

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)

Signature of a function to receive data for a custom tokenizer.

Parameters
clsclosure from GNUNET_SERVER_set_callbacks
mstcustom tokenizer handle
client_identityID of client for which this is a buffer, can be NULL (will be passed back to 'cb')
bufinput data to add
sizenumber of bytes in buf
purgeshould any excess bytes in the buffer be discarded (i.e. for packet-based services like UDP)
one_shotonly call callback once, keep rest of message in buffer
Returns
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

Definition at line 228 of file plugin_transport_tcp.c.

◆ GNUNET_SERVER_MessageCallback

typedef void(* GNUNET_SERVER_MessageCallback) (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message)

Functions with this signature are called whenever a message is received.

Parameters
clsclosure
clientidentification of the client
messagethe actual message

Definition at line 244 of file plugin_transport_tcp.c.

◆ GNUNET_SERVER_MessageTokenizerCallback

typedef int(* GNUNET_SERVER_MessageTokenizerCallback) (void *cls, void *client, const struct GNUNET_MessageHeader *message)

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.

Parameters
clsclosure
clientidentification of the client
messagethe actual message
Returns
GNUNET_OK on success, GNUNET_SYSERR to stop further processing

Definition at line 342 of file plugin_transport_tcp.c.

◆ LEGACY_SERVICE_Main

typedef void(* LEGACY_SERVICE_Main) (void *cls, struct GNUNET_SERVER_Handle *server, const struct GNUNET_CONFIGURATION_Handle *cfg)

Function called by the service's run method to run service-specific setup code.

Parameters
clsclosure
serverthe initialized server
cfgconfiguration to use

Definition at line 490 of file plugin_transport_tcp.c.

Enumeration Type Documentation

◆ LEGACY_SERVICE_Options

Options for the service (bitmask).

Enumerator
LEGACY_SERVICE_OPTION_NONE 

Use defaults.

Terminates all client connections and the listen sockets immediately upon receiving the shutdown signal.

LEGACY_SERVICE_OPTION_MANUAL_SHUTDOWN 

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_SOFT_SHUTDOWN 

Trigger a SOFT server shutdown on signals, allowing active non-monitor clients to complete their transactions.

Definition at line 283 of file plugin_transport_tcp.c.

284 {
290 
297 
303 };
@ LEGACY_SERVICE_OPTION_NONE
Use defaults.
@ LEGACY_SERVICE_OPTION_SOFT_SHUTDOWN
Trigger a SOFT server shutdown on signals, allowing active non-monitor clients to complete their tran...
@ LEGACY_SERVICE_OPTION_MANUAL_SHUTDOWN
Do not trigger server shutdown on signal at all; instead, allow for the user to terminate the server ...

◆ TcpAddressOptions

Bits in the options field of TCP addresses.

Enumerator
TCP_OPTIONS_NONE 

No bits set.

TCP_OPTIONS_RESERVED 

See HTTP_OPTIONS_VERIFY_CERTIFICATE.

TCP_OPTIONS_TCP_STEALTH 

Enable TCP Stealth-style port knocking.

Definition at line 642 of file plugin_transport_tcp.c.

643 {
647  TCP_OPTIONS_NONE = 0,
648 
653 
658 };
@ TCP_OPTIONS_NONE
No bits set.
@ TCP_OPTIONS_TCP_STEALTH
Enable TCP Stealth-style port knocking.
@ TCP_OPTIONS_RESERVED
See HTTP_OPTIONS_VERIFY_CERTIFICATE.

Function Documentation

◆ LEGACY_SERVICE_stop()

void LEGACY_SERVICE_stop ( struct LEGACY_SERVICE_Context sctx)

Stops a service that was started with GNUNET_SERVICE_start().

Parameters
srvservice to stop

Stops a service that was started with GNUNET_SERVICE_start().

Parameters
sctxthe service context returned from the start function

Definition at line 1598 of file tcp_service_legacy.c.

1599 {
1600  unsigned int i;
1601 
1602 #if HAVE_MALLINFO2
1603  {
1604  char *counter;
1605 
1607  sctx->service_name,
1608  "GAUGER_HEAP")) &&
1610  sctx->service_name,
1611  "GAUGER_HEAP",
1612  &counter)))
1613  {
1614  struct mallinfo2 mi;
1615 
1616  mi = mallinfo2 ();
1617  GAUGER (sctx->service_name, counter, mi.usmblks, "blocks");
1618  GNUNET_free (counter);
1619  }
1620  }
1621 #endif
1622  if (NULL != sctx->shutdown_task)
1623  {
1625  sctx->shutdown_task = NULL;
1626  }
1627  if (NULL != sctx->server)
1628  GNUNET_SERVER_destroy (sctx->server);
1629  GNUNET_free (sctx->my_handlers);
1630  if (NULL != sctx->addrs)
1631  {
1632  i = 0;
1633  while (NULL != sctx->addrs[i])
1634  GNUNET_free_nz (sctx->addrs[i++]);
1635  GNUNET_free (sctx->addrs);
1636  }
1637  GNUNET_free (sctx->addrlens);
1638  GNUNET_free (sctx->v4_denied);
1639  GNUNET_free (sctx->v6_denied);
1640  GNUNET_free (sctx->v4_allowed);
1641  GNUNET_free (sctx->v6_allowed);
1642  GNUNET_free (sctx);
1643 }
#define GAUGER(category, counter, value, unit)
Definition: gauger.h:19
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_have_value(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Test if we have a value for a particular option.
@ GNUNET_OK
@ GNUNET_YES
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_free_nz(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:975
void GNUNET_SERVER_destroy(struct GNUNET_SERVER_Handle *server)
Free resources held by this server.
struct GNUNET_STRINGS_IPv4NetworkPolicy * v4_allowed
IPv4 addresses that are allowed to connect (if not set, all are allowed).
struct sockaddr ** addrs
NULL-terminated array of addresses to bind to, NULL if we got pre-bound listen sockets.
struct GNUNET_STRINGS_IPv4NetworkPolicy * v4_denied
IPv4 addresses that are not allowed to connect.
struct GNUNET_STRINGS_IPv6NetworkPolicy * v6_denied
IPv6 addresses that are not allowed to connect.
struct GNUNET_STRINGS_IPv6NetworkPolicy * v6_allowed
IPv6 addresses that are allowed to connect (if not set, all are allowed).
struct GNUNET_SERVER_Handle * server
Handle for the server.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
const char * service_name
Name of our service.
struct GNUNET_SCHEDULER_Task * shutdown_task
Task ID of the shutdown task.
struct GNUNET_SERVER_MessageHandler * my_handlers
My (default) message handlers.
socklen_t * addrlens
Array of the lengths of the entries in addrs.

References LEGACY_SERVICE_Context::addrlens, LEGACY_SERVICE_Context::addrs, LEGACY_SERVICE_Context::cfg, GAUGER, GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_have_value(), GNUNET_free, GNUNET_free_nz, GNUNET_OK, GNUNET_SCHEDULER_cancel(), GNUNET_SERVER_destroy(), GNUNET_YES, LEGACY_SERVICE_Context::my_handlers, LEGACY_SERVICE_Context::server, LEGACY_SERVICE_Context::service_name, LEGACY_SERVICE_Context::shutdown_task, LEGACY_SERVICE_Context::v4_allowed, LEGACY_SERVICE_Context::v4_denied, LEGACY_SERVICE_Context::v6_allowed, and LEGACY_SERVICE_Context::v6_denied.

Referenced by LEGACY_SERVICE_start(), libgnunet_plugin_transport_tcp_done(), and libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CONNECTION_destroy()

void GNUNET_CONNECTION_destroy ( struct GNUNET_CONNECTION_Handle connection)

Close the connection and free associated resources.

There must not be any pending requests for reading or writing to the connection at this time.

Parameters
connectionconnection to destroy

Definition at line 1023 of file tcp_connection_legacy.c.

1024 {
1025  struct AddressProbe *pos;
1026 
1027  if (0 != connection->destroy_later)
1028  {
1029  connection->destroy_later = -1;
1030  return;
1031  }
1032  LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connection (%p)\n", connection);
1034  GNUNET_assert (NULL == connection->receiver);
1035  if (NULL != connection->write_task)
1036  {
1038  connection->write_task = NULL;
1040  }
1041  if (NULL != connection->read_task)
1042  {
1044  connection->read_task = NULL;
1045  }
1046  if (NULL != connection->nth.timeout_task)
1047  {
1049  connection->nth.timeout_task = NULL;
1050  }
1051  connection->nth.notify_ready = NULL;
1052  if (NULL != connection->dns_active)
1053  {
1055  connection->dns_active = NULL;
1056  }
1057  if (NULL != connection->proxy_handshake)
1058  {
1059  /* GNUNET_CONNECTION_destroy (connection->proxy_handshake); */
1061  connection->proxy_handshake = NULL; /* Not leaked ??? */
1062  }
1063  while (NULL != (pos = connection->ap_head))
1064  {
1068  GNUNET_free (pos);
1069  }
1070  if ((NULL != connection->sock) && (GNUNET_YES != connection->persist))
1071  {
1072  if ((GNUNET_OK !=
1074  (ENOTCONN != errno) && (ECONNRESET != errno))
1076  }
1077  if (NULL != connection->sock)
1078  {
1079  if (GNUNET_YES != connection->persist)
1080  {
1083  }
1084  else
1085  {
1087  connection->sock); /* at least no memory leak (we deliberately
1088  * leak the socket in this special case) ... */
1089  }
1090  }
1095 }
#define LOG(kind,...)
Definition: abd_api.c:38
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:509
void GNUNET_NETWORK_socket_free_memory_only_(struct GNUNET_NETWORK_Handle *desc)
Only free memory of a socket, keep the file descriptor untouched.
Definition: network.c:566
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_shutdown(struct GNUNET_NETWORK_Handle *desc, int how)
Shut down socket operations.
Definition: network.c:861
void GNUNET_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh)
Cancel a request that is still pending with the resolver.
During connect, we try multiple possible IP addresses to find out which one might work.
struct GNUNET_CONNECTION_Handle * connection
Connection for which we are probing.
struct GNUNET_SCHEDULER_Task * task
Task waiting for the connection to finish connecting.
struct GNUNET_NETWORK_Handle * sock
Underlying OS's socket.
int8_t persist
When shutdown, do not ever actually close the socket, but free resources.
char * hostname
Pointer to the hostname if connection was created using DNS lookup, otherwise NULL.
struct GNUNET_RESOLVER_RequestHandle * dns_active
Handle to a pending DNS lookup request.
struct sockaddr * addr
Network address of the other end-point, may be NULL.
GNUNET_CONNECTION_Receiver receiver
Function to call on data received, NULL if no receive is pending.
size_t write_buffer_off
Current write-offset in write_buffer (where would we write next).
char * write_buffer
Pointer to our write buffer.
struct AddressProbe * ap_head
Linked list of sockets we are currently trying out (during connect).
struct GNUNET_CONNECTION_TransmitHandle nth
The handle we return for GNUNET_CONNECTION_notify_transmit_ready().
struct AddressProbe * ap_tail
Linked list of sockets we are currently trying out (during connect).
struct GNUNET_SCHEDULER_Task * read_task
Read task that we may need to wait for.
struct GNUNET_NETWORK_Handle * sock
Underlying OS's socket, set to NULL after fatal errors.
struct GNUNET_CONNECTION_Handle * proxy_handshake
Handle to subsequent connection after proxy handshake completes,.
struct GNUNET_SCHEDULER_Task * write_task
Write task that we may need to wait for.
struct GNUNET_SCHEDULER_Task * timeout_task
Task called on timeout.
GNUNET_CONNECTION_TransmitReadyNotify notify_ready
Function to call if the send buffer has notify_size bytes available.
#define LOG_STRERROR(kind, syscall)

References GNUNET_CONNECTION_Handle::addr, GNUNET_CONNECTION_Handle::ap_head, GNUNET_CONNECTION_Handle::ap_tail, AddressProbe::connection, GNUNET_CONNECTION_Handle::destroy_later, GNUNET_CONNECTION_Handle::dns_active, GNUNET_assert, GNUNET_break, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_free_memory_only_(), GNUNET_NETWORK_socket_shutdown(), GNUNET_OK, GNUNET_RESOLVER_request_cancel(), GNUNET_SCHEDULER_cancel(), GNUNET_YES, GNUNET_CONNECTION_Handle::hostname, LOG, LOG_STRERROR, GNUNET_CONNECTION_TransmitHandle::notify_ready, GNUNET_CONNECTION_Handle::nth, GNUNET_CONNECTION_Handle::persist, GNUNET_CONNECTION_Handle::proxy_handshake, GNUNET_CONNECTION_Handle::read_task, GNUNET_CONNECTION_Handle::receiver, AddressProbe::sock, GNUNET_CONNECTION_Handle::sock, AddressProbe::task, GNUNET_CONNECTION_TransmitHandle::timeout_task, GNUNET_CONNECTION_Handle::write_buffer, GNUNET_CONNECTION_Handle::write_buffer_off, and GNUNET_CONNECTION_Handle::write_task.

Referenced by connect_fail_continuation(), GNUNET_CONNECTION_acivate_proxied(), GNUNET_SERVER_client_disconnect(), libgnunet_plugin_transport_tcp_done(), and notify_send_probe().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_SERVER_client_disconnect()

void GNUNET_SERVER_client_disconnect ( struct GNUNET_SERVER_Client client)

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.

Parameters
clientthe client to disconnect from

This is the same as returning GNUNET_SYSERR from a message handler, except that it allows dropping of a client even when not handling a message from that client.

Parameters
clientthe client to disconnect from

Definition at line 1497 of file tcp_server_legacy.c.

1498 {
1499  struct GNUNET_SERVER_Handle *server = client->server;
1500  struct NotifyList *n;
1501 
1503  "Client is being disconnected from the server.\n");
1504  if (NULL != client->restart_task)
1505  {
1507  client->restart_task = NULL;
1508  }
1509  if (NULL != client->warn_task)
1510  {
1512  client->warn_task = NULL;
1513  }
1514  if (GNUNET_YES == client->receive_pending)
1515  {
1517  client->receive_pending = GNUNET_NO;
1518  }
1519  client->shutdown_now = GNUNET_YES;
1520  client->reference_count++; /* make sure nobody else clean up client... */
1521  if ((NULL != client->mst) &&
1522  (NULL != server))
1523  {
1525  server->clients_tail,
1526  client);
1527  if (NULL != server->mst_destroy)
1528  server->mst_destroy (server->mst_cls,
1529  client->mst);
1530  else
1531  GNUNET_SERVER_mst_destroy (client->mst);
1532  client->mst = NULL;
1533  for (n = server->disconnect_notify_list_head; NULL != n; n = n->next)
1534  n->callback (n->callback_cls,
1535  client);
1536  }
1537  client->reference_count--;
1538  if (client->reference_count > 0)
1539  {
1541  "RC of %p still positive, not destroying everything.\n",
1542  client);
1543  client->server = NULL;
1544  return;
1545  }
1546  if (GNUNET_YES == client->in_process_client_buffer)
1547  {
1549  "Still processing inputs of %p, not destroying everything.\n",
1550  client);
1551  return;
1552  }
1554  "RC of %p now zero, destroying everything.\n",
1555  client);
1556  if (GNUNET_YES == client->persist)
1558  if (NULL != client->th.cth)
1561  /* need to cancel again, as it might have been re-added
1562  in the meantime (i.e. during callbacks) */
1563  if (NULL != client->warn_task)
1564  {
1566  client->warn_task = NULL;
1567  }
1568  if (GNUNET_YES == client->receive_pending)
1569  {
1571  client->receive_pending = GNUNET_NO;
1572  }
1573  GNUNET_free (client);
1574  /* we might be in soft-shutdown, test if we're done */
1575  if (NULL != server)
1576  test_monitor_clients (server);
1577 }
@ GNUNET_NO
void GNUNET_SERVER_mst_destroy(struct GNUNET_SERVER_MessageStreamTokenizer *mst)
Destroys a tokenizer.
void GNUNET_CONNECTION_destroy(struct GNUNET_CONNECTION_Handle *connection)
Close the connection and free associated resources.
int receive_pending
Are we currently trying to receive? (GNUNET_YES if we are, GNUNET_NO if we are not,...
unsigned int reference_count
Number of external entities with a reference to this client object.
int persist
Persist the file handle for this client no matter what happens, force the OS to close once the proces...
int shutdown_now
We're about to close down this client.
struct GNUNET_SCHEDULER_Task * warn_task
Task that warns about missing calls to GNUNET_SERVER_receive_done.
struct GNUNET_CONNECTION_Handle * connection
Client closure for callbacks.
struct GNUNET_SERVER_TransmitHandle th
Transmission handle we return for this client from GNUNET_SERVER_notify_transmit_ready.
void * mst
Processing of incoming data.
struct GNUNET_SCHEDULER_Task * restart_task
ID of task used to restart processing.
int in_process_client_buffer
Are we currently in the "process_client_buffer" function (and will hence restart the receive job on e...
struct GNUNET_SERVER_Handle * server
Server that this client belongs to.
handle for a server
void * mst_cls
Closure for 'mst_'-callbacks.
struct GNUNET_SERVER_Client * clients_tail
Head of list of our current clients.
struct GNUNET_SERVER_Client * clients_head
Head of list of our current clients.
GNUNET_SERVER_MstDestroyCallback mst_destroy
Alternative function to destroy a MST instance.
struct NotifyList * disconnect_notify_list_head
Head of linked list of functions to call on disconnects by clients.
struct GNUNET_CONNECTION_TransmitHandle * cth
Active connection transmission handle.
List of arrays of message handlers.
void * callback_cls
Closure for callback.
GNUNET_SERVER_DisconnectCallback callback
Function to call.
struct NotifyList * next
This is a doubly linked list.
void * GNUNET_CONNECTION_receive_cancel(struct GNUNET_CONNECTION_Handle *connection)
Cancel receive job on the given connection.
void GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *connection)
Set the persist option on this connection handle.
static void test_monitor_clients(struct GNUNET_SERVER_Handle *server)
Check if only 'monitor' clients are left.
void GNUNET_SERVER_notify_transmit_ready_cancel(struct GNUNET_SERVER_TransmitHandle *th)
Abort transmission request.

References NotifyList::callback, NotifyList::callback_cls, GNUNET_SERVER_Handle::clients_head, GNUNET_SERVER_Handle::clients_tail, GNUNET_SERVER_Client::connection, GNUNET_SERVER_TransmitHandle::cth, GNUNET_SERVER_Handle::disconnect_notify_list_head, GNUNET_CONNECTION_destroy(), GNUNET_CONNECTION_persist_(), GNUNET_CONNECTION_receive_cancel(), GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_NO, GNUNET_SCHEDULER_cancel(), GNUNET_SERVER_mst_destroy(), GNUNET_SERVER_notify_transmit_ready_cancel(), GNUNET_YES, GNUNET_SERVER_Client::in_process_client_buffer, LOG, GNUNET_SERVER_Client::mst, GNUNET_SERVER_Handle::mst_cls, GNUNET_SERVER_Handle::mst_destroy, NotifyList::next, GNUNET_SERVER_Client::persist, GNUNET_SERVER_Client::receive_pending, GNUNET_SERVER_Client::reference_count, GNUNET_SERVER_Client::restart_task, GNUNET_SERVER_Client::server, GNUNET_SERVER_Client::shutdown_now, test_monitor_clients(), GNUNET_SERVER_Client::th, and GNUNET_SERVER_Client::warn_task.

Referenced by client_message_tokenizer_callback(), GNUNET_SERVER_client_drop(), GNUNET_SERVER_destroy(), GNUNET_SERVER_receive_done(), process_incoming(), process_mst(), and tcp_plugin_disconnect_session().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_SERVER_client_get_user_context_()

void* GNUNET_SERVER_client_get_user_context_ ( struct GNUNET_SERVER_Client client,
size_t  size 
)

Return user context associated with the given client.

Note: you should probably use the macro (call without the underscore).

Parameters
clientclient to query
sizenumber of bytes in user context struct (for verification only)
Returns
pointer to user context

Definition at line 349 of file tcp_server_legacy.c.

351 {
352  if ((0 == client->user_context_size) &&
353  (NULL == client->user_context))
354  return NULL; /* never set */
355  GNUNET_assert (size == client->user_context_size);
356  return client->user_context;
357 }
static unsigned int size
Size of the "table".
Definition: peer.c:68
void * user_context
User context value, manipulated using 'GNUNET_SERVER_client_{get/set}_user_context' functions.
size_t user_context_size
Last size given when user context was initialized; used for sanity check.

References GNUNET_assert, size, GNUNET_SERVER_Client::user_context, and GNUNET_SERVER_Client::user_context_size.

◆ GNUNET_SERVER_mst_create()

struct GNUNET_SERVER_MessageStreamTokenizer* GNUNET_SERVER_mst_create ( GNUNET_SERVER_MessageTokenizerCallback  cb,
void *  cb_cls 
)

Create a message stream tokenizer.

Parameters
cbfunction to call on completed messages
cb_clsclosure for cb
Returns
handle to tokenizer

Definition at line 83 of file tcp_server_mst_legacy.c.

85 {
87 
90  ret->curr_buf = GNUNET_MIN_MESSAGE_SIZE;
91  ret->cb = cb;
92  ret->cb_cls = cb_cls;
93  return ret;
94 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_MIN_MESSAGE_SIZE
Smallest supported message.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
Handle to a message stream tokenizer.
GNUNET_SERVER_MessageTokenizerCallback cb
Function to call on completed messages.

References GNUNET_SERVER_MessageStreamTokenizer::cb, GNUNET_SERVER_MessageStreamTokenizer::cb_cls, GNUNET_malloc, GNUNET_MIN_MESSAGE_SIZE, GNUNET_new, and ret.

Referenced by GNUNET_SERVER_connect_socket().

Here is the caller graph for this function:

◆ GNUNET_SERVER_mst_receive()

int GNUNET_SERVER_mst_receive ( struct GNUNET_SERVER_MessageStreamTokenizer mst,
void *  client_identity,
const char *  buf,
size_t  size,
int  purge,
int  one_shot 
)

Add incoming data to the receive buffer and call the callback for all complete messages.

Parameters
msttokenizer to use
client_identityID of client for which this is a buffer, can be NULL (will be passed back to 'cb')
bufinput data to add
sizenumber of bytes in buf
purgeshould any excess bytes in the buffer be discarded (i.e. for packet-based services like UDP)
one_shotonly call callback once, keep rest of message in buffer
Returns
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
Parameters
msttokenizer to use
client_identityID of client for which this is a buffer
bufinput data to add
sizenumber of bytes in buf
purgeshould any excess bytes in the buffer be discarded (i.e. for packet-based services like UDP)
one_shotonly call callback once, keep rest of message in buffer
Returns
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

Definition at line 113 of file tcp_server_mst_legacy.c.

117 {
118  const struct GNUNET_MessageHeader *hdr;
119  size_t delta;
120  uint16_t want;
121  char *ibuf;
122  int need_align;
123  unsigned long offset;
124  int ret;
125 
126  GNUNET_assert (mst->off <= mst->pos);
127  GNUNET_assert (mst->pos <= mst->curr_buf);
129  "Server-mst receives %u bytes with %u bytes already in private buffer\n",
130  (unsigned int) size, (unsigned int) (mst->pos - mst->off));
131  ret = GNUNET_OK;
132  ibuf = (char *) mst->hdr;
133  while (mst->pos > 0)
134  {
135 do_align:
136  GNUNET_assert (mst->pos >= mst->off);
137  if ((mst->curr_buf - mst->off < sizeof(struct GNUNET_MessageHeader)) ||
138  (0 != (mst->off % ALIGN_FACTOR)))
139  {
140  /* need to align or need more space */
141  mst->pos -= mst->off;
142  memmove (ibuf, &ibuf[mst->off], mst->pos);
143  mst->off = 0;
144  }
145  if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader))
146  {
147  delta =
148  GNUNET_MIN (sizeof(struct GNUNET_MessageHeader)
149  - (mst->pos - mst->off), size);
150  GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
151  mst->pos += delta;
152  buf += delta;
153  size -= delta;
154  }
155  if (mst->pos - mst->off < sizeof(struct GNUNET_MessageHeader))
156  {
157  if (purge)
158  {
159  mst->off = 0;
160  mst->pos = 0;
161  }
162  return GNUNET_OK;
163  }
164  hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
165  want = ntohs (hdr->size);
166  if (want < sizeof(struct GNUNET_MessageHeader))
167  {
168  GNUNET_break_op (0);
169  return GNUNET_SYSERR;
170  }
171  if ((mst->curr_buf - mst->off < want) &&
172  (mst->off > 0))
173  {
174  /* can get more space by moving */
175  mst->pos -= mst->off;
176  memmove (ibuf, &ibuf[mst->off], mst->pos);
177  mst->off = 0;
178  }
179  if (mst->curr_buf < want)
180  {
181  /* need to get more space by growing buffer */
182  GNUNET_assert (0 == mst->off);
183  mst->hdr = GNUNET_realloc (mst->hdr, want);
184  ibuf = (char *) mst->hdr;
185  mst->curr_buf = want;
186  }
187  hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
188  if (mst->pos - mst->off < want)
189  {
190  delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
191  GNUNET_assert (mst->pos + delta <= mst->curr_buf);
192  GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
193  mst->pos += delta;
194  buf += delta;
195  size -= delta;
196  }
197  if (mst->pos - mst->off < want)
198  {
199  if (purge)
200  {
201  mst->off = 0;
202  mst->pos = 0;
203  }
204  return GNUNET_OK;
205  }
206  if (one_shot == GNUNET_SYSERR)
207  {
208  /* cannot call callback again, but return value saying that
209  * we have another full message in the buffer */
210  ret = GNUNET_NO;
211  goto copy;
212  }
213  if (one_shot == GNUNET_YES)
214  one_shot = GNUNET_SYSERR;
215  mst->off += want;
216  if (GNUNET_SYSERR == mst->cb (mst->cb_cls, client_identity, hdr))
217  return GNUNET_SYSERR;
218  if (mst->off == mst->pos)
219  {
220  /* reset to beginning of buffer, it's free right now! */
221  mst->off = 0;
222  mst->pos = 0;
223  }
224  }
225  GNUNET_assert (0 == mst->pos);
226  while (size > 0)
227  {
229  "Server-mst has %u bytes left in inbound buffer\n",
230  (unsigned int) size);
231  if (size < sizeof(struct GNUNET_MessageHeader))
232  break;
233  offset = (unsigned long) buf;
234  need_align = (0 != (offset % ALIGN_FACTOR)) ? GNUNET_YES : GNUNET_NO;
235  if (GNUNET_NO == need_align)
236  {
237  /* can try to do zero-copy and process directly from original buffer */
238  hdr = (const struct GNUNET_MessageHeader *) buf;
239  want = ntohs (hdr->size);
240  if (want < sizeof(struct GNUNET_MessageHeader))
241  {
242  GNUNET_break_op (0);
243  mst->off = 0;
244  return GNUNET_SYSERR;
245  }
246  if (size < want)
247  break; /* or not: buffer incomplete, so copy to private buffer... */
248  if (one_shot == GNUNET_SYSERR)
249  {
250  /* cannot call callback again, but return value saying that
251  * we have another full message in the buffer */
252  ret = GNUNET_NO;
253  goto copy;
254  }
255  if (one_shot == GNUNET_YES)
256  one_shot = GNUNET_SYSERR;
257  if (GNUNET_SYSERR == mst->cb (mst->cb_cls, client_identity, hdr))
258  return GNUNET_SYSERR;
259  buf += want;
260  size -= want;
261  }
262  else
263  {
264  /* need to copy to private buffer to align;
265  * yes, we go a bit more spaghetti than usual here */
266  goto do_align;
267  }
268  }
269 copy:
270  if ((size > 0) && (! purge))
271  {
272  if (size + mst->pos > mst->curr_buf)
273  {
274  mst->hdr = GNUNET_realloc (mst->hdr, size + mst->pos);
275  ibuf = (char *) mst->hdr;
276  mst->curr_buf = size + mst->pos;
277  }
278  GNUNET_assert (size + mst->pos <= mst->curr_buf);
279  GNUNET_memcpy (&ibuf[mst->pos], buf, size);
280  mst->pos += size;
281  }
282  if (purge)
283  {
284  mst->off = 0;
285  mst->pos = 0;
286  }
288  "Server-mst leaves %u bytes in private buffer\n",
289  (unsigned int) (mst->pos - mst->off));
290  return ret;
291 }
static size_t do_align(size_t start_position, size_t end_position)
Given the start and end position of a block of data, return the end position of that data after align...
Definition: fs_directory.c:488
static char buf[2048]
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_MIN(a, b)
@ GNUNET_SYSERR
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define GNUNET_realloc(ptr, size)
Wrapper around realloc.
static struct GNUNET_TIME_Relative delta
Definition: speedup.c:36
Header for all communications.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
size_t off
How many bytes in buffer have we already processed?
size_t curr_buf
Size of the buffer (starting at hdr).
size_t pos
How many bytes in buffer are valid right now?
struct GNUNET_MessageHeader * hdr
Beginning of the buffer.
#define ALIGN_FACTOR

References ALIGN_FACTOR, buf, GNUNET_SERVER_MessageStreamTokenizer::cb, GNUNET_SERVER_MessageStreamTokenizer::cb_cls, GNUNET_SERVER_MessageStreamTokenizer::curr_buf, delta, do_align(), GNUNET_assert, GNUNET_break_op, GNUNET_ERROR_TYPE_DEBUG, GNUNET_memcpy, GNUNET_MIN, GNUNET_NO, GNUNET_OK, GNUNET_realloc, GNUNET_SYSERR, GNUNET_YES, GNUNET_SERVER_MessageStreamTokenizer::hdr, LOG, GNUNET_SERVER_MessageStreamTokenizer::off, GNUNET_SERVER_MessageStreamTokenizer::pos, ret, GNUNET_MessageHeader::size, and size.

Referenced by handle_helper_message(), process_data(), process_incoming(), process_mst(), and wlan_data_message_handler().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_SERVER_mst_destroy()

void GNUNET_SERVER_mst_destroy ( struct GNUNET_SERVER_MessageStreamTokenizer mst)

Destroys a tokenizer.

Parameters
msttokenizer to destroy

Definition at line 300 of file tcp_server_mst_legacy.c.

301 {
302  GNUNET_free (mst->hdr);
303  GNUNET_free (mst);
304 }

References GNUNET_free, and GNUNET_SERVER_MessageStreamTokenizer::hdr.

Referenced by GNUNET_SERVER_client_disconnect(), and LIBGNUNET_PLUGIN_TRANSPORT_DONE().

Here is the caller graph for this function:

◆ GNUNET_SERVER_client_set_user_context_()

void GNUNET_SERVER_client_set_user_context_ ( struct GNUNET_SERVER_Client client,
void *  ptr,
size_t  size 
)

Set user context to be associated with the given client.

Note: you should probably use the macro (call without the underscore).

Parameters
clientclient to query
ptrpointer to user context
sizenumber of bytes in user context struct (for verification only)

Definition at line 369 of file tcp_server_legacy.c.

372 {
373  if (NULL == ptr)
374  {
375  client->user_context_size = 0;
376  client->user_context = ptr;
377  return;
378  }
379  client->user_context_size = size;
380  client->user_context = ptr;
381 }

References size, GNUNET_SERVER_Client::user_context, and GNUNET_SERVER_Client::user_context_size.

◆ GNUNET_SERVER_notify_transmit_ready()

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 
)

Notify us when the server has enough space to transmit a message of the given size to the given client.

Parameters
clientclient to transmit message to
sizerequested amount of buffer space
timeoutafter how long should we give up (and call notify with buf NULL and size 0)?
callbackfunction to call when space is available
callback_clsclosure for callback
Returns
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)

Definition at line 1619 of file tcp_server_legacy.c.

1625 {
1626  if (NULL != client->th.callback)
1627  return NULL;
1628  client->th.callback_cls = callback_cls;
1629  client->th.callback = callback;
1631  size,
1632  timeout,
1633  &
1635  client);
1636  return &client->th;
1637 }
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
void * callback_cls
Closure for callback.
GNUNET_CONNECTION_TransmitReadyNotify callback
Function to call to get the message.
struct GNUNET_CONNECTION_TransmitHandle * GNUNET_CONNECTION_notify_transmit_ready(struct GNUNET_CONNECTION_Handle *connection, size_t size, struct GNUNET_TIME_Relative timeout, GNUNET_CONNECTION_TransmitReadyNotify notify, void *notify_cls)
Ask the connection to call us once the specified number of bytes are free in the transmission buffer.
static size_t transmit_ready_callback_wrapper(void *cls, size_t size, void *buf)
Wrapper for transmission notification that calls the original callback and update the last activity t...

References GNUNET_SERVER_TransmitHandle::callback, GNUNET_SERVER_TransmitHandle::callback_cls, GNUNET_SERVER_Client::connection, GNUNET_SERVER_TransmitHandle::cth, GNUNET_CONNECTION_notify_transmit_ready(), size, GNUNET_SERVER_Client::th, timeout, and transmit_ready_callback_wrapper().

Referenced by process_pending_messages().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_SERVER_notify_transmit_ready_cancel()

void GNUNET_SERVER_notify_transmit_ready_cancel ( struct GNUNET_SERVER_TransmitHandle th)

Abort transmission request.

Parameters
threquest to abort

Definition at line 1646 of file tcp_server_legacy.c.

1648 {
1650  th->cth = NULL;
1651  th->callback = NULL;
1652 }
void GNUNET_CONNECTION_notify_transmit_ready_cancel(struct GNUNET_CONNECTION_TransmitHandle *th)
Cancel the specified transmission-ready notification.

References GNUNET_SERVER_TransmitHandle::callback, GNUNET_SERVER_TransmitHandle::cth, and GNUNET_CONNECTION_notify_transmit_ready_cancel().

Referenced by GNUNET_SERVER_client_disconnect(), and tcp_plugin_disconnect_session().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_SERVER_client_keep()

void GNUNET_SERVER_client_keep ( struct GNUNET_SERVER_Client client)

Notify the server that the given client handle should be kept (keeps the connection up if possible, increments the internal reference counter).

Parameters
clientthe client to keep

Definition at line 1331 of file tcp_server_legacy.c.

1332 {
1333  client->reference_count++;
1334 }

References GNUNET_SERVER_Client::reference_count.

Referenced by process_incoming(), and restart_processing().

Here is the caller graph for this function:

◆ GNUNET_SERVER_client_drop()

void GNUNET_SERVER_client_drop ( 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.

Parameters
clientthe client to drop

Definition at line 1346 of file tcp_server_legacy.c.

1347 {
1348  GNUNET_assert (client->reference_count > 0);
1349  client->reference_count--;
1350  if ((GNUNET_YES == client->shutdown_now) && (0 == client->reference_count))
1352 }
void GNUNET_SERVER_client_disconnect(struct GNUNET_SERVER_Client *client)
Ask the server to disconnect from the given client.

References GNUNET_assert, GNUNET_SERVER_client_disconnect(), GNUNET_YES, GNUNET_SERVER_Client::reference_count, and GNUNET_SERVER_Client::shutdown_now.

Referenced by process_incoming(), and restart_processing().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_SERVER_suspend()

void GNUNET_SERVER_suspend ( struct GNUNET_SERVER_Handle server)

Suspend accepting connections from the listen socket temporarily.

Resume activity using GNUNET_SERVER_resume.

Parameters
serverserver to stop accepting connections.
serverserver to stop accepting connections.

Definition at line 692 of file tcp_server_legacy.c.

693 {
694  if (NULL != server->listen_task)
695  {
697  server->listen_task = NULL;
698  }
699 }
struct GNUNET_SCHEDULER_Task * listen_task
Task scheduled to do the listening.

References GNUNET_SCHEDULER_cancel(), GNUNET_SERVER_Handle::listen_task, and GNUNET_SERVER_Client::server.

Referenced by connect_notify().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_SERVER_connect_socket()

struct GNUNET_SERVER_Client* GNUNET_SERVER_connect_socket ( struct GNUNET_SERVER_Handle server,
struct GNUNET_CONNECTION_Handle connection 
)

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).

Parameters
serverthe server to use
connectionthe connection to manage (client must stop using this connection from now on)
Returns
the client handle

Definition at line 1271 of file tcp_server_legacy.c.

1273 {
1274  struct GNUNET_SERVER_Client *client;
1275  struct NotifyList *n;
1276 
1277  client = GNUNET_new (struct GNUNET_SERVER_Client);
1278  client->connection = connection;
1279  client->server = server;
1281  client->idle_timeout = server->idle_timeout;
1283  server->clients_tail,
1284  client);
1285  if (NULL != server->mst_create)
1286  client->mst =
1287  server->mst_create (server->mst_cls, client);
1288  else
1289  client->mst =
1291  server);
1292  GNUNET_assert (NULL != client->mst);
1293  for (n = server->connect_notify_list_head; NULL != n; n = n->next)
1294  n->callback (n->callback_cls, client);
1295  client->receive_pending = GNUNET_YES;
1296  if (GNUNET_SYSERR ==
1299  client->idle_timeout,
1301  client))
1302  return NULL;
1303  return client;
1304 }
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
struct GNUNET_SERVER_MessageStreamTokenizer * GNUNET_SERVER_mst_create(GNUNET_SERVER_MessageTokenizerCallback cb, void *cb_cls)
Create a message stream tokenizer.
handle for a client of the server
struct GNUNET_TIME_Relative idle_timeout
After how long should an idle connection time out (on write).
struct GNUNET_TIME_Absolute last_activity
Last activity on this socket (used to time it out if reference_count == 0).
GNUNET_SERVER_MstCreateCallback mst_create
Alternative function to create a MST instance.
struct NotifyList * connect_notify_list_head
Head of linked list of functions to call on connects by clients.
struct GNUNET_TIME_Relative idle_timeout
After how long should an idle connection time out (on write).
int GNUNET_CONNECTION_receive(struct GNUNET_CONNECTION_Handle *connection, size_t max, struct GNUNET_TIME_Relative timeout, GNUNET_CONNECTION_Receiver receiver, void *receiver_cls)
Receive data from the given connection.
static void process_incoming(void *cls, const void *buf, size_t available, const struct sockaddr *addr, socklen_t addrlen, int errCode)
We are receiving an incoming message.
static int client_message_tokenizer_callback(void *cls, void *client, const struct GNUNET_MessageHeader *message)
This function is called whenever our inbound message tokenizer has received a complete message.

References NotifyList::callback, NotifyList::callback_cls, client_message_tokenizer_callback(), GNUNET_SERVER_Handle::clients_head, GNUNET_SERVER_Handle::clients_tail, GNUNET_SERVER_Handle::connect_notify_list_head, GNUNET_SERVER_Client::connection, GNUNET_assert, GNUNET_CONNECTION_receive(), GNUNET_CONTAINER_DLL_insert, GNUNET_MAX_MESSAGE_SIZE, GNUNET_new, GNUNET_SERVER_mst_create(), GNUNET_SYSERR, GNUNET_TIME_absolute_get(), GNUNET_YES, GNUNET_SERVER_Handle::idle_timeout, GNUNET_SERVER_Client::idle_timeout, GNUNET_SERVER_Client::last_activity, GNUNET_SERVER_Client::mst, GNUNET_SERVER_Handle::mst_cls, GNUNET_SERVER_Handle::mst_create, NotifyList::next, process_incoming(), GNUNET_SERVER_Client::receive_pending, and GNUNET_SERVER_Client::server.

Referenced by notify_send_probe(), process_listen_socket(), and tcp_plugin_get_session().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_SERVER_resume()

void GNUNET_SERVER_resume ( struct GNUNET_SERVER_Handle server)

Resume accepting connections from the listen socket.

Parameters
serverserver to resume accepting connections.

Definition at line 703 of file tcp_server_legacy.c.

704 {
705  struct GNUNET_NETWORK_FDSet *r;
706  unsigned int i;
707 
708  if (NULL == server->listen_sockets)
709  return;
710  if (NULL == server->listen_sockets[0])
711  return; /* nothing to do, no listen sockets! */
712  if (NULL == server->listen_sockets[1])
713  {
714  /* simplified method: no fd set needed; this is then much simpler
715  and much more efficient */
716  server->listen_task =
719  server->listen_sockets[0],
721  server);
722  return;
723  }
725  i = 0;
726  while (NULL != server->listen_sockets[i])
727  GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
728  server->listen_task =
731  &process_listen_socket, server);
733 }
@ GNUNET_SCHEDULER_PRIORITY_HIGH
Run with high priority (important requests).
struct GNUNET_NETWORK_FDSet * GNUNET_NETWORK_fdset_create(void)
Creates an fd set.
Definition: network.c:1171
void GNUNET_NETWORK_fdset_destroy(struct GNUNET_NETWORK_FDSet *fds)
Releases the associated memory of an fd set.
Definition: network.c:1187
void GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_NETWORK_Handle *desc)
Add a socket to the FD set.
Definition: network.c:932
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net_with_priority(struct GNUNET_TIME_Relative delay, enum GNUNET_SCHEDULER_Priority priority, struct GNUNET_NETWORK_Handle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified priority and to be run after the specified delay or wh...
Definition: scheduler.c:1540
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio, struct GNUNET_TIME_Relative delay, const struct GNUNET_NETWORK_FDSet *rs, const struct GNUNET_NETWORK_FDSet *ws, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when any of the specified file descriptor set...
Definition: scheduler.c:1830
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
collection of IO descriptors
struct GNUNET_NETWORK_Handle ** listen_sockets
NULL-terminated array of sockets used to listen for new connections.
static void process_listen_socket(void *cls)
Scheduler says our listen socket is ready.

References GNUNET_NETWORK_fdset_create(), GNUNET_NETWORK_fdset_destroy(), GNUNET_NETWORK_fdset_set(), GNUNET_SCHEDULER_add_read_net_with_priority(), GNUNET_SCHEDULER_add_select(), GNUNET_SCHEDULER_PRIORITY_HIGH, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_SERVER_Handle::listen_sockets, GNUNET_SERVER_Handle::listen_task, and process_listen_socket().

Referenced by disconnect_notify(), GNUNET_SERVER_create_with_sockets(), and process_listen_socket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_SERVER_destroy()

void GNUNET_SERVER_destroy ( struct GNUNET_SERVER_Handle server)

Free resources held by this server.

Parameters
serverserver to destroy

Definition at line 775 of file tcp_server_legacy.c.

776 {
777  struct HandlerList *hpos;
778  struct NotifyList *npos;
779  unsigned int i;
780 
782  "Server shutting down.\n");
783  if (NULL != server->listen_task)
784  {
786  server->listen_task = NULL;
787  }
788  if (NULL != server->listen_sockets)
789  {
790  i = 0;
791  while (NULL != server->listen_sockets[i])
794  GNUNET_free (server->listen_sockets);
795  server->listen_sockets = NULL;
796  }
797  while (NULL != server->clients_head)
799  while (NULL != (hpos = server->handlers))
800  {
801  server->handlers = hpos->next;
802  GNUNET_free (hpos);
803  }
804  while (NULL != (npos = server->disconnect_notify_list_head))
805  {
806  npos->callback (npos->callback_cls,
807  NULL);
810  npos);
811  GNUNET_free (npos);
812  }
813  while (NULL != (npos = server->connect_notify_list_head))
814  {
815  npos->callback (npos->callback_cls,
816  NULL);
818  server->connect_notify_list_tail,
819  npos);
820  GNUNET_free (npos);
821  }
822  GNUNET_free (server);
823 }
struct NotifyList * disconnect_notify_list_tail
Tail of linked list of functions to call on disconnects by clients.
struct NotifyList * connect_notify_list_tail
Tail of linked list of functions to call on connects by clients.
struct HandlerList * handlers
List of handlers for incoming messages.
List of arrays of message handlers.
struct HandlerList * next
This is a linked list.

References NotifyList::callback, NotifyList::callback_cls, GNUNET_SERVER_Handle::clients_head, GNUNET_SERVER_Handle::connect_notify_list_head, GNUNET_SERVER_Handle::connect_notify_list_tail, GNUNET_SERVER_Handle::disconnect_notify_list_head, GNUNET_SERVER_Handle::disconnect_notify_list_tail, GNUNET_break, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_NETWORK_socket_close(), GNUNET_OK, GNUNET_SCHEDULER_cancel(), GNUNET_SERVER_client_disconnect(), GNUNET_SERVER_Handle::handlers, GNUNET_SERVER_Handle::listen_sockets, GNUNET_SERVER_Handle::listen_task, LOG, and HandlerList::next.

Referenced by do_destroy(), LEGACY_SERVICE_stop(), libgnunet_plugin_transport_tcp_done(), and shutdown_task().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_server_addresses()

static int get_server_addresses ( const char *  service_name,
const struct GNUNET_CONFIGURATION_Handle cfg,
struct sockaddr ***  addrs,
socklen_t **  addr_lens 
)
static

Get the list of addresses that a server for the given service should bind to.

Parameters
service_namename of the service
cfgconfiguration (which specifies the addresses)
addrsset (call by reference) to an array of pointers to the addresses the server should bind to and listen on; the array will be NULL-terminated (on success)
addr_lensset (call by reference) to an array of the lengths of the respective struct sockaddr struct in the addrs array (on success)
Returns
number of addresses found on success, GNUNET_SYSERR if the configuration did not specify reasonable finding information or if it specified a hostname that could not be resolved; GNUNET_NO if the number of addresses configured is zero (in this case, *addrs and *addr_lens will be set to NULL).

Definition at line 1053 of file plugin_transport_tcp.c.

1057 {
1058  int disablev6;
1059  struct GNUNET_NETWORK_Handle *desc;
1060  unsigned long long port;
1061  char *unixpath;
1062  struct addrinfo hints;
1063  struct addrinfo *res;
1064  struct addrinfo *pos;
1065  struct addrinfo *next;
1066  unsigned int i;
1067  int resi;
1068  int ret;
1069  int abstract;
1070  struct sockaddr **saddrs;
1071  socklen_t *saddrlens;
1072  char *hostname;
1073 
1074  *addrs = NULL;
1075  *addr_lens = NULL;
1076  desc = NULL;
1077  if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
1078  {
1079  if (GNUNET_SYSERR ==
1081  service_name,
1082  "DISABLEV6")))
1083  return GNUNET_SYSERR;
1084  }
1085  else
1086  disablev6 = GNUNET_NO;
1087 
1088  if (! disablev6)
1089  {
1090  /* probe IPv6 support */
1091  desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
1092  if (NULL == desc)
1093  {
1094  if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
1095  (EACCES == errno))
1096  {
1098  return GNUNET_SYSERR;
1099  }
1101  _ (
1102  "Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
1103  service_name,
1104  strerror (errno));
1105  disablev6 = GNUNET_YES;
1106  }
1107  else
1108  {
1110  desc = NULL;
1111  }
1112  }
1113 
1114  port = 0;
1116  {
1118  service_name,
1119  "PORT",
1120  &port))
1121  {
1123  _ ("Require valid port number for service `%s' in configuration!\n"),
1124  service_name);
1125  }
1126  if (port > 65535)
1127  {
1129  _ ("Require valid port number for service `%s' in configuration!\n"),
1130  service_name);
1131  return GNUNET_SYSERR;
1132  }
1133  }
1134 
1136  {
1139  service_name,
1140  "BINDTO",
1141  &hostname));
1142  }
1143  else
1144  hostname = NULL;
1145 
1146  unixpath = NULL;
1147  abstract = GNUNET_NO;
1148 #ifdef AF_UNIX
1149  if ((GNUNET_YES ==
1152  service_name,
1153  "UNIXPATH",
1154  &unixpath)) &&
1155  (0 < strlen (unixpath)))
1156  {
1157  /* probe UNIX support */
1158  struct sockaddr_un s_un;
1159 
1160  if (strlen (unixpath) >= sizeof(s_un.sun_path))
1161  {
1163  _ ("UNIXPATH `%s' too long, maximum length is %llu\n"),
1164  unixpath,
1165  (unsigned long long) sizeof(s_un.sun_path));
1166  unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
1167  LOG (GNUNET_ERROR_TYPE_INFO, _ ("Using `%s' instead\n"), unixpath);
1168  }
1169 #ifdef __linux__
1171  "TESTING",
1172  "USE_ABSTRACT_SOCKETS");
1173  if (GNUNET_SYSERR == abstract)
1174  abstract = GNUNET_NO;
1175 #endif
1176  if ((GNUNET_YES != abstract) &&
1179  }
1180  if (NULL != unixpath)
1181  {
1182  desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
1183  if (NULL == desc)
1184  {
1185  if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
1186  (EACCES == errno))
1187  {
1190  GNUNET_free (unixpath);
1191  return GNUNET_SYSERR;
1192  }
1194  _ (
1195  "Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
1196  service_name,
1197  strerror (errno));
1198  GNUNET_free (unixpath);
1199  unixpath = NULL;
1200  }
1201  else
1202  {
1204  desc = NULL;
1205  }
1206  }
1207 #endif
1208 
1209  if ((0 == port) && (NULL == unixpath))
1210  {
1212  _ (
1213  "Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
1214  service_name);
1216  return GNUNET_SYSERR;
1217  }
1218  if (0 == port)
1219  {
1220  saddrs = GNUNET_malloc (2 * sizeof(struct sockaddr *));
1221  saddrlens = GNUNET_malloc (2 * sizeof(socklen_t));
1222  add_unixpath (saddrs, saddrlens, unixpath, abstract);
1223  GNUNET_free (unixpath);
1225  *addrs = saddrs;
1226  *addr_lens = saddrlens;
1227  return 1;
1228  }
1229 
1230  if (NULL != hostname)
1231  {
1233  "Resolving `%s' since that is where `%s' will bind to.\n",
1234  hostname,
1235  service_name);
1236  memset (&hints, 0, sizeof(struct addrinfo));
1237  if (disablev6)
1238  hints.ai_family = AF_INET;
1239  hints.ai_protocol = IPPROTO_TCP;
1240  if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1241  (NULL == res))
1242  {
1244  _ ("Failed to resolve `%s': %s\n"),
1245  hostname,
1246  gai_strerror (ret));
1248  GNUNET_free (unixpath);
1249  return GNUNET_SYSERR;
1250  }
1251  next = res;
1252  i = 0;
1253  while (NULL != (pos = next))
1254  {
1255  next = pos->ai_next;
1256  if ((disablev6) && (pos->ai_family == AF_INET6))
1257  continue;
1258  i++;
1259  }
1260  if (0 == i)
1261  {
1263  _ ("Failed to find %saddress for `%s'.\n"),
1264  disablev6 ? "IPv4 " : "",
1265  hostname);
1266  freeaddrinfo (res);
1268  GNUNET_free (unixpath);
1269  return GNUNET_SYSERR;
1270  }
1271  resi = i;
1272  if (NULL != unixpath)
1273  resi++;
1274  saddrs = GNUNET_malloc ((resi + 1) * sizeof(struct sockaddr *));
1275  saddrlens = GNUNET_malloc ((resi + 1) * sizeof(socklen_t));
1276  i = 0;
1277  if (NULL != unixpath)
1278  {
1279  add_unixpath (saddrs, saddrlens, unixpath, abstract);
1280  i++;
1281  }
1282  next = res;
1283  while (NULL != (pos = next))
1284  {
1285  next = pos->ai_next;
1286  if ((disablev6) && (AF_INET6 == pos->ai_family))
1287  continue;
1288  if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
1289  continue; /* not TCP */
1290  if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
1291  continue; /* huh? */
1293  "Service `%s' will bind to `%s'\n",
1294  service_name,
1295  GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
1296  if (AF_INET == pos->ai_family)
1297  {
1298  GNUNET_assert (sizeof(struct sockaddr_in) == pos->ai_addrlen);
1299  saddrlens[i] = pos->ai_addrlen;
1300  saddrs[i] = GNUNET_malloc (saddrlens[i]);
1301  GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1302  ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1303  }
1304  else
1305  {
1306  GNUNET_assert (AF_INET6 == pos->ai_family);
1307  GNUNET_assert (sizeof(struct sockaddr_in6) == pos->ai_addrlen);
1308  saddrlens[i] = pos->ai_addrlen;
1309  saddrs[i] = GNUNET_malloc (saddrlens[i]);
1310  GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1311  ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1312  }
1313  i++;
1314  }
1316  freeaddrinfo (res);
1317  resi = i;
1318  }
1319  else
1320  {
1321  /* will bind against everything, just set port */
1322  if (disablev6)
1323  {
1324  /* V4-only */
1325  resi = 1;
1326  if (NULL != unixpath)
1327  resi++;
1328  i = 0;
1329  saddrs = GNUNET_malloc ((resi + 1) * sizeof(struct sockaddr *));
1330  saddrlens = GNUNET_malloc ((resi + 1) * sizeof(socklen_t));
1331  if (NULL != unixpath)
1332  {
1333  add_unixpath (saddrs, saddrlens, unixpath, abstract);
1334  i++;
1335  }
1336  saddrlens[i] = sizeof(struct sockaddr_in);
1337  saddrs[i] = GNUNET_malloc (saddrlens[i]);
1338 #if HAVE_SOCKADDR_IN_SIN_LEN
1339  ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1340 #endif
1341  ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1342  ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1343  }
1344  else
1345  {
1346  /* dual stack */
1347  resi = 2;
1348  if (NULL != unixpath)
1349  resi++;
1350  saddrs = GNUNET_malloc ((resi + 1) * sizeof(struct sockaddr *));
1351  saddrlens = GNUNET_malloc ((resi + 1) * sizeof(socklen_t));
1352  i = 0;
1353  if (NULL != unixpath)
1354  {
1355  add_unixpath (saddrs, saddrlens, unixpath, abstract);
1356  i++;
1357  }
1358  saddrlens[i] = sizeof(struct sockaddr_in6);
1359  saddrs[i] = GNUNET_malloc (saddrlens[i]);
1360 #if HAVE_SOCKADDR_IN_SIN_LEN
1361  ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1362 #endif
1363  ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1364  ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1365  i++;
1366  saddrlens[i] = sizeof(struct sockaddr_in);
1367  saddrs[i] = GNUNET_malloc (saddrlens[i]);
1368 #if HAVE_SOCKADDR_IN_SIN_LEN
1369  ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1370 #endif
1371  ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1372  ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1373  }
1374  }
1375  GNUNET_free (unixpath);
1376  *addrs = saddrs;
1377  *addr_lens = saddrlens;
1378  return resi;
1379 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static int res
static uint16_t port
Port number.
Definition: gnunet-bcd.c:147
static void add_unixpath(struct sockaddr **saddrs, socklen_t *saddrlens, const char *unixpath, int abstract)
Add the given UNIX domain path as an address to the list (as the first entry).
static char * hostname
Our hostname; we give this to all the peers we start.
static char * service_name
Option -s: service name (hash to get service descriptor)
Definition: gnunet-vpn.c:50
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:582
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
const char * GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string (for printing debug messages).
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_INFO
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:833
char * GNUNET_NETWORK_shorten_unixpath(char *unixpath)
Given a unixpath that is too long (larger than UNIX_PATH_MAX), shorten it to an acceptable length whi...
Definition: network.c:144
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
#define LOG(kind,...)
handle to a socket
Definition: network.c:54

References _, add_unixpath(), cfg, GNUNET_a2s(), GNUNET_assert, GNUNET_break, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_get_value_number(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_get_value_yesno(), GNUNET_CONFIGURATION_have_value(), GNUNET_DISK_directory_create_for_file(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log_strerror, GNUNET_log_strerror_file, GNUNET_malloc, GNUNET_memcpy, GNUNET_NETWORK_shorten_unixpath(), GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_create(), GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, hostname, LOG, port, res, ret, and service_name.

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ notify_session_monitor()

static void notify_session_monitor ( struct Plugin plugin,
struct GNUNET_ATS_Session session,
enum GNUNET_TRANSPORT_SessionState  state 
)
static

If a session monitor is attached, notify it about the new session state.

Parameters
pluginour plugin
sessionsession that changed state
statenew state of the session

Definition at line 1394 of file plugin_transport_tcp.c.

1397 {
1399 
1400  if (NULL == plugin->sic)
1401  return;
1402  memset (&info, 0, sizeof(info));
1403  info.state = state;
1404  info.is_inbound =
1407  info.num_msg_pending = session->msgs_in_queue;
1408  info.num_bytes_pending = session->bytes_in_queue;
1409  if (NULL != session->receive_delay_task)
1410  info.receive_delay = session->receive_delay;
1411  info.session_timeout = session->timeout;
1412  info.address = session->address;
1413  plugin->sic (plugin->sic_cls, session, &info);
1414 }
struct TestcasePlugin * plugin
The process handle to the testbed service.
enum State state
current state of profiling
#define info
int GNUNET_HELLO_address_check_option(const struct GNUNET_HELLO_Address *address, enum GNUNET_HELLO_AddressInfo option)
Check if an address has a local option set.
Definition: address.c:39
@ GNUNET_HELLO_ADDRESS_INFO_INBOUND
This is an inbound address and cannot be used to initiate an outbound connection to another peer.
unsigned int msgs_in_queue
Number of messages waiting for transmission to this peer.
struct GNUNET_TIME_Absolute timeout
When does this session time out.
struct GNUNET_TIME_Absolute receive_delay
When will we continue to read from the socket? (used to enforce inbound quota).
struct GNUNET_HELLO_Address * address
Address.
unsigned long long bytes_in_queue
Number of bytes waiting for transmission to this peer.
struct GNUNET_SCHEDULER_Task * receive_delay_task
ID of task used to delay receiving more to throttle sender.
Information about a plugin's session.

References GNUNET_ATS_Session::address, GNUNET_ATS_Session::bytes_in_queue, GNUNET_HELLO_address_check_option(), GNUNET_HELLO_ADDRESS_INFO_INBOUND, info, GNUNET_ATS_Session::msgs_in_queue, plugin, GNUNET_ATS_Session::receive_delay, GNUNET_ATS_Session::receive_delay_task, state, and GNUNET_ATS_Session::timeout.

Referenced by create_session(), do_transmit(), send_session_info_iter(), session_timeout(), tcp_plugin_disconnect_session(), and tcp_plugin_send().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_nat_port_map_callback()

static void tcp_nat_port_map_callback ( void *  cls,
void **  app_ctx,
int  add_remove,
enum GNUNET_NAT_AddressClass  ac,
const struct sockaddr *  addr,
socklen_t  addrlen 
)
static

Our external IP address/port mapping has changed.

Parameters
clsclosure, the struct Plugin
[in,out]app_ctxlocation where the app can store stuff on add and retrieve it on remove
add_removeGNUNET_YES to mean the new public IP address, GNUNET_NO to mean the previous (now invalid) one
acaddress class the address belongs to
addreither the previous or the new public IP address
addrlenactual length of addr

Definition at line 1430 of file plugin_transport_tcp.c.

1436 {
1437  struct Plugin *plugin = cls;
1438  struct GNUNET_HELLO_Address *address;
1439  struct IPv4TcpAddress t4;
1440  struct IPv6TcpAddress t6;
1441  void *arg;
1442  size_t args;
1443 
1444  (void) app_ctx;
1446  "NAT notification to %s address `%s'\n",
1447  (GNUNET_YES == add_remove) ? "add" : "remove",
1448  GNUNET_a2s (addr, addrlen));
1449  /* convert 'addr' to our internal format */
1450  switch (addr->sa_family)
1451  {
1452  case AF_INET:
1453  GNUNET_assert (addrlen == sizeof(struct sockaddr_in));
1454  memset (&t4, 0, sizeof(t4));
1455  t4.options = htonl (plugin->myoptions);
1456  t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1457  t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
1458  arg = &t4;
1459  args = sizeof(t4);
1460  break;
1461 
1462  case AF_INET6:
1463  if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
1464  {
1465  /* skip link local, we don't allow them in
1466  #tcp_plugin_check_address() */
1467  return;
1468  }
1469  GNUNET_assert (addrlen == sizeof(struct sockaddr_in6));
1470  memset (&t6, 0, sizeof(t6));
1471  GNUNET_memcpy (&t6.ipv6_addr,
1472  &((struct sockaddr_in6 *) addr)->sin6_addr,
1473  sizeof(struct in6_addr));
1474  t6.options = htonl (plugin->myoptions);
1475  t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1476  arg = &t6;
1477  args = sizeof(t6);
1478  break;
1479 
1480  default:
1481  GNUNET_break (0);
1482  return;
1483  }
1484  /* modify our published address list */
1485  GNUNET_assert ((args == sizeof(struct IPv4TcpAddress)) ||
1486  (args == sizeof(struct IPv6TcpAddress)));
1487  /* TODO: use 'ac' here in the future... */
1488  address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1489  PLUGIN_NAME,
1490  arg,
1491  args,
1493  plugin->env->notify_address (plugin->env->cls, add_remove, address);
1495 }
static char * address
GNS address for this phone.
static struct GNUNET_SCHEDULER_Task * t4
Task for IPv4 socket.
static struct GNUNET_SCHEDULER_Task * t6
Task for IPv6 socket.
struct GNUNET_HELLO_Address * GNUNET_HELLO_address_allocate(const struct GNUNET_PeerIdentity *peer, const char *transport_name, const void *address, size_t address_length, enum GNUNET_HELLO_AddressInfo local_info)
Allocate an address struct.
Definition: address.c:63
#define GNUNET_HELLO_address_free(addr)
Free an address.
@ GNUNET_HELLO_ADDRESS_INFO_NONE
No additional information.
#define PLUGIN_NAME
An address for communicating with a peer.
Network format for IPv4 addresses.
Network format for IPv6 addresses.
Handle for a plugin.
Definition: block.c:38

References address, find_typedefs::arg, consensus-simulation::args, GNUNET_a2s(), GNUNET_assert, GNUNET_break, GNUNET_ERROR_TYPE_INFO, GNUNET_HELLO_address_allocate(), GNUNET_HELLO_address_free, GNUNET_HELLO_ADDRESS_INFO_NONE, GNUNET_memcpy, GNUNET_YES, LOG, plugin, PLUGIN_NAME, t4, and t6.

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_plugin_address_to_string()

static const char* tcp_plugin_address_to_string ( void *  cls,
const void *  addr,
size_t  addrlen 
)
static

Function called for a quick conversion of the binary address to a numeric address.

Note that the caller must not free the address and that the next call to this function is allowed to override the address again.

Parameters
clsclosure (struct Plugin*)
addrbinary address
addrlenlength of addr
Returns
string representing the same address

Definition at line 1510 of file plugin_transport_tcp.c.

1511 {
1512  static char rbuf[INET6_ADDRSTRLEN + 12];
1513  char buf[INET6_ADDRSTRLEN];
1514  const void *sb;
1515  struct in_addr a4;
1516  struct in6_addr a6;
1517  const struct IPv4TcpAddress *t4;
1518  const struct IPv6TcpAddress *t6;
1519  int af;
1520  uint16_t port;
1521  uint32_t options;
1522 
1523  switch (addrlen)
1524  {
1525  case sizeof(struct IPv6TcpAddress):
1526  t6 = addr;
1527  af = AF_INET6;
1528  port = ntohs (t6->t6_port);
1529  options = ntohl (t6->options);
1530  GNUNET_memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
1531  sb = &a6;
1532  break;
1533 
1534  case sizeof(struct IPv4TcpAddress):
1535  t4 = addr;
1536  af = AF_INET;
1537  port = ntohs (t4->t4_port);
1538  options = ntohl (t4->options);
1539  GNUNET_memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
1540  sb = &a4;
1541  break;
1542 
1543  default:
1544  LOG (GNUNET_ERROR_TYPE_WARNING,
1545  _ ("Unexpected address length: %u bytes\n"),
1546  (unsigned int) addrlen);
1547  return NULL;
1548  }
1549  if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
1550  {
1552  return NULL;
1553  }
1554  GNUNET_snprintf (rbuf,
1555  sizeof(rbuf),
1556  (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
1557  PLUGIN_NAME,
1558  options,
1559  buf,
1560  port);
1561  return rbuf;
1562 }
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
struct in6_addr ipv6_addr
IPv6 address.
uint16_t t6_port
Port number, in network byte order.

References buf, GNUNET_ERROR_TYPE_WARNING, GNUNET_log_strerror, GNUNET_memcpy, GNUNET_snprintf(), IPv6TcpAddress::ipv6_addr, LOG, options, PLUGIN_NAME, port, t4, t6, and IPv6TcpAddress::t6_port.

Referenced by create_session(), disconnect_notify(), do_transmit(), handle_tcp_welcome(), libgnunet_plugin_transport_tcp_init(), nat_connect_timeout(), tcp_plugin_disconnect_session(), and tcp_plugin_get_session().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_plugin_string_to_address()

static int tcp_plugin_string_to_address ( void *  cls,
const char *  addr,
uint16_t  addrlen,
void **  buf,
size_t *  added 
)
static

Function called to convert a string address to a binary address.

Parameters
clsclosure (struct Plugin*)
addrstring address
addrlenlength of the address
buflocation to store the buffer
addedlocation to store the number of bytes in the buffer. If the function returns GNUNET_SYSERR, its contents are undefined.
Returns
GNUNET_OK on success, GNUNET_SYSERR on failure

Definition at line 1578 of file plugin_transport_tcp.c.

1583 {
1584  struct sockaddr_storage socket_address;
1585  char *address;
1586  char *plugin;
1587  char *optionstr;
1588  uint32_t options;
1589 
1590  /* Format tcp.options.address:port */
1591  address = NULL;
1592  plugin = NULL;
1593  optionstr = NULL;
1594  if ((NULL == addr) || (0 == addrlen))
1595  {
1596  GNUNET_break (0);
1597  return GNUNET_SYSERR;
1598  }
1599  if ('\0' != addr[addrlen - 1])
1600  {
1601  GNUNET_break (0);
1602  return GNUNET_SYSERR;
1603  }
1604  if (strlen (addr) != addrlen - 1)
1605  {
1606  GNUNET_break (0);
1607  return GNUNET_SYSERR;
1608  }
1609  plugin = GNUNET_strdup (addr);
1610  optionstr = strchr (plugin, '.');
1611  if (NULL == optionstr)
1612  {
1613  GNUNET_break (0);
1614  GNUNET_free (plugin);
1615  return GNUNET_SYSERR;
1616  }
1617  optionstr[0] = '\0';
1618  optionstr++;
1619  options = atol (optionstr);
1620  address = strchr (optionstr, '.');
1621  if (NULL == address)
1622  {
1623  GNUNET_break (0);
1624  GNUNET_free (plugin);
1625  return GNUNET_SYSERR;
1626  }
1627  address[0] = '\0';
1628  address++;
1629 
1630  if (GNUNET_OK !=
1631  GNUNET_STRINGS_to_address_ip (address, strlen (address), &socket_address))
1632  {
1633  GNUNET_break (0);
1634  GNUNET_free (plugin);
1635  return GNUNET_SYSERR;
1636  }
1637 
1638  GNUNET_free (plugin);
1639  switch (socket_address.ss_family)
1640  {
1641  case AF_INET: {
1642  struct IPv4TcpAddress *t4;
1643  struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
1644  t4 = GNUNET_new (struct IPv4TcpAddress);
1645  t4->options = htonl (options);
1646  t4->ipv4_addr = in4->sin_addr.s_addr;
1647  t4->t4_port = in4->sin_port;
1648  *buf = t4;
1649  *added = sizeof(struct IPv4TcpAddress);
1650  return GNUNET_OK;
1651  }
1652 
1653  case AF_INET6: {
1654  struct IPv6TcpAddress *t6;
1655  struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
1656  t6 = GNUNET_new (struct IPv6TcpAddress);
1657  t6->options = htonl (options);
1658  t6->ipv6_addr = in6->sin6_addr;
1659  t6->t6_port = in6->sin6_port;
1660  *buf = t6;
1661  *added = sizeof(struct IPv6TcpAddress);
1662  return GNUNET_OK;
1663  }
1664 
1665  default:
1666  return GNUNET_SYSERR;
1667  }
1668 }
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ip(const char *addr, uint16_t addrlen, struct sockaddr_storage *r_buf)
Tries to convert addr string to an IP (v4 or v6) address.
Definition: strings.c:1134

References address, buf, GNUNET_break, GNUNET_free, GNUNET_new, GNUNET_OK, GNUNET_strdup, GNUNET_STRINGS_to_address_ip(), GNUNET_SYSERR, options, plugin, t4, and t6.

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lookup_session_by_client()

static struct GNUNET_ATS_Session* lookup_session_by_client ( struct Plugin plugin,
struct GNUNET_SERVER_Client client 
)
static

Find the session handle for the given client.

Currently uses both the hashmap and the client context, as the client context is new and the logic still needs to be tested.

Parameters
pluginthe plugin
clientwhich client to find the session handle for
Returns
NULL if no matching session exists

Definition at line 1682 of file plugin_transport_tcp.c.

1684 {
1686  struct GNUNET_ATS_Session);
1687 }
#define GNUNET_SERVER_client_get_user_context(client, type)
Return user context associated with the given client.
Session handle for connections.

References GNUNET_ATS_Session::client, and GNUNET_SERVER_client_get_user_context.

Referenced by disconnect_notify(), handle_tcp_data(), and handle_tcp_welcome().

Here is the caller graph for this function:

◆ tcp_plugin_disconnect_session()

static int tcp_plugin_disconnect_session ( void *  cls,
struct GNUNET_ATS_Session session 
)
static

Functions with this signature are called whenever we need to close a session due to a disconnect or failure to establish a connection.

Parameters
clsthe struct Plugin
sessionsession to close down
Returns
GNUNET_OK on success

Definition at line 1700 of file plugin_transport_tcp.c.

1701 {
1702  struct Plugin *plugin = cls;
1703  struct PendingMessage *pm;
1704 
1706  "Disconnecting session of peer `%s' address `%s'\n",
1707  GNUNET_i2s (&session->target),
1709  session->address->address,
1710  session->address->address_length));
1711 
1712  if (NULL != session->timeout_task)
1713  {
1715  session->timeout_task = NULL;
1717  }
1718 
1720  &session->target,
1721  session))
1722  {
1723  GNUNET_STATISTICS_update (session->plugin->env->stats,
1724  gettext_noop ("# TCP sessions active"),
1725  -1,
1726  GNUNET_NO);
1727  }
1728  else
1729  {
1732  &session->target,
1733  session));
1734  }
1735  if (NULL != session->client)
1737 
1738  /* clean up state */
1739  if (NULL != session->transmit_handle)
1740  {
1742  session->transmit_handle = NULL;
1743  }
1744  session->plugin->env->session_end (session->plugin->env->cls,
1745  session->address,
1746  session);
1747 
1748  if (NULL != session->nat_connection_timeout)
1749  {
1751  session->nat_connection_timeout = NULL;
1752  }
1753 
1754  while (NULL != (pm = session->pending_messages_head))
1755  {
1757  (NULL != pm->transmit_cont)
1758  ? "Could not deliver message to `%s' at %s.\n"
1759  : "Could not deliver message to `%s' at %s, notifying.\n",
1760  GNUNET_i2s (&session->target),
1762  session->address->address,
1763  session->address->address_length));
1764  GNUNET_STATISTICS_update (session->plugin->env->stats,
1765  gettext_noop ("# bytes currently in TCP buffers"),
1766  -(int64_t) pm->message_size,
1767  GNUNET_NO);
1768  GNUNET_STATISTICS_update (session->plugin->env->stats,
1769  gettext_noop (
1770  "# bytes discarded by TCP (disconnect)"),
1771  pm->message_size,
1772  GNUNET_NO);
1774  session->pending_messages_tail,
1775  pm);
1776  GNUNET_assert (0 < session->msgs_in_queue);
1777  session->msgs_in_queue--;
1778  GNUNET_assert (pm->message_size <= session->bytes_in_queue);
1779  session->bytes_in_queue -= pm->message_size;
1780  if (NULL != pm->transmit_cont)
1781  pm->transmit_cont (pm->transmit_cont_cls,
1782  &session->target,
1783  GNUNET_SYSERR,
1784  pm->message_size,
1785  0);
1786  GNUNET_free (pm);
1787  }
1788  GNUNET_assert (0 == session->msgs_in_queue);
1789  GNUNET_assert (0 == session->bytes_in_queue);
1791 
1792  if (NULL != session->receive_delay_task)
1793  {
1795  session->receive_delay_task = NULL;
1796  }
1797  if (NULL != session->client)
1798  {
1800  session->client = NULL;
1801  }
1803  GNUNET_assert (NULL == session->transmit_handle);
1804  GNUNET_free (session);
1805  return GNUNET_OK;
1806 }
#define gettext_noop(String)
Definition: gettext.h:70
static struct GNUNET_TRANSPORT_PluginMonitor * pm
Handle if we are monitoring plugin session activity.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multipeermap_remove(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, const void *value)
Remove the given key-value pair from the map.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
#define GNUNET_TIME_UNIT_ZERO_ABS
Absolute time zero.
@ GNUNET_TRANSPORT_SS_DONE
Session is being torn down and about to disappear.
static const char * tcp_plugin_address_to_string(void *cls, const void *addr, size_t addrlen)
Function called for a quick conversion of the binary address to a numeric address.
#define GNUNET_SERVER_client_set_user_context(client, value)
Set user context to be associated with the given client.
void GNUNET_SERVER_client_disconnect(struct GNUNET_SERVER_Client *client)
Ask the server to disconnect from the given client.
static void notify_session_monitor(struct Plugin *plugin, struct GNUNET_ATS_Session *session, enum GNUNET_TRANSPORT_SessionState state)
If a session monitor is attached, notify it about the new session state.
void GNUNET_SERVER_notify_transmit_ready_cancel(struct GNUNET_SERVER_TransmitHandle *th)
Abort transmission request.
struct GNUNET_PeerIdentity target
To whom are we talking to.
struct Plugin * plugin
Pointer to the global plugin struct.
struct GNUNET_SERVER_Client * client
The client (used to identify this connection)
struct PendingMessage * pending_messages_tail
Messages currently pending for transmission to this peer, if any.
struct GNUNET_SCHEDULER_Task * nat_connection_timeout
Task cleaning up a NAT client connection establishment attempt;.
struct GNUNET_SERVER_TransmitHandle * transmit_handle
Handle for pending transmission request.
struct PendingMessage * pending_messages_head
Messages currently pending for transmission to this peer, if any.
struct GNUNET_SCHEDULER_Task * timeout_task
Session timeout task.
void * cls
Closure to use for callbacks.
size_t address_length
Number of bytes in address.
const void * address
Binary representation of the address (plugin-specific).
List containing all messages that are yet to be send.
struct GNUNET_DATACACHE_PluginEnvironment * env
Our execution environment.

References GNUNET_HELLO_Address::address, GNUNET_ATS_Session::address, GNUNET_HELLO_Address::address_length, GNUNET_ATS_Session::bytes_in_queue, GNUNET_ATS_Session::client, GNUNET_DATACACHE_PluginEnvironment::cls, Plugin::env, gettext_noop, GNUNET_assert, GNUNET_CONTAINER_DLL_remove, GNUNET_CONTAINER_multipeermap_remove(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_HELLO_address_free, GNUNET_i2s(), GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_cancel(), GNUNET_SERVER_client_disconnect(), GNUNET_SERVER_client_set_user_context, GNUNET_SERVER_notify_transmit_ready_cancel(), GNUNET_STATISTICS_update(), GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_SS_DONE, GNUNET_YES, LOG, GNUNET_ATS_Session::msgs_in_queue, GNUNET_ATS_Session::nat_connection_timeout, notify_session_monitor(), GNUNET_ATS_Session::pending_messages_head, GNUNET_ATS_Session::pending_messages_tail, plugin, GNUNET_ATS_Session::plugin, pm, GNUNET_ATS_Session::receive_delay_task, GNUNET_ATS_Session::target, tcp_plugin_address_to_string(), GNUNET_ATS_Session::timeout, GNUNET_ATS_Session::timeout_task, and GNUNET_ATS_Session::transmit_handle.

Referenced by disconnect_notify(), handle_tcp_nat_probe(), libgnunet_plugin_transport_tcp_init(), nat_connect_timeout(), session_disconnect_it(), session_timeout(), and tcp_plugin_get_session().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_plugin_query_keepalive_factor()

static unsigned int tcp_plugin_query_keepalive_factor ( void *  cls)
static

Function that is called to get the keepalive factor.

GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to calculate the interval between keepalive packets.

Parameters
clsclosure with the struct Plugin
Returns
keepalive factor

Definition at line 1818 of file plugin_transport_tcp.c.

1819 {
1820  return 3;
1821 }

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the caller graph for this function:

◆ session_timeout()

static void session_timeout ( void *  cls)
static

Session was idle for too long, so disconnect it.

Parameters
clsthe struct GNUNET_ATS_Session of the idle session

Definition at line 1830 of file plugin_transport_tcp.c.

1831 {
1832  struct GNUNET_ATS_Session *s = cls;
1833  struct GNUNET_TIME_Relative left;
1834 
1835  s->timeout_task = NULL;
1837  if (0 != left.rel_value_us)
1838  {
1839  /* not actually our turn yet, but let's at least update
1840  the monitor, it may think we're about to die ... */
1843  return;
1844  }
1846  "Session %p was idle for %s, disconnecting\n",
1847  s,
1850  GNUNET_YES));
1851  /* call session destroy function */
1853 }
#define GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
After how long do we consider a connection to a peer dead if we don't receive messages from the peer?
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1272
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:405
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:569
@ GNUNET_TRANSPORT_SS_UPDATE
This is just an update about the session, the state did not change.
static int tcp_plugin_disconnect_session(void *cls, struct GNUNET_ATS_Session *session)
Functions with this signature are called whenever we need to close a session due to a disconnect or f...
static void session_timeout(void *cls)
Session was idle for too long, so disconnect it.
Time for relative time used by GNUnet, in microseconds.

References GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_ERROR_TYPE_DEBUG, GNUNET_SCHEDULER_add_delayed(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_TIME_absolute_get_remaining(), GNUNET_TRANSPORT_SS_UPDATE, GNUNET_YES, LOG, notify_session_monitor(), GNUNET_ATS_Session::plugin, GNUNET_TIME_Relative::rel_value_us, tcp_plugin_disconnect_session(), GNUNET_ATS_Session::timeout, and GNUNET_ATS_Session::timeout_task.

Referenced by create_session().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reschedule_session_timeout()

static void reschedule_session_timeout ( struct GNUNET_ATS_Session s)
static

Increment session timeout due to activity.

Parameters
ssession to increment timeout for

Definition at line 1862 of file plugin_transport_tcp.c.

1863 {
1864  GNUNET_assert (NULL != s->timeout_task);
1865  s->timeout =
1867 }
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
Definition: time.c:316

References GNUNET_assert, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_TIME_relative_to_absolute(), GNUNET_ATS_Session::timeout, and GNUNET_ATS_Session::timeout_task.

Referenced by delayed_done(), handle_tcp_data(), and tcp_plugin_update_session_timeout().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_session()

static struct GNUNET_ATS_Session* create_session ( struct Plugin plugin,
const struct GNUNET_HELLO_Address address,
enum GNUNET_NetworkType  scope,
struct GNUNET_SERVER_Client client,
int  is_nat 
)
static

Create a new session.

Also queues a welcome message.

Parameters
pluginthe plugin
addressthe address to create the session for
scopenetwork scope the address is from
clientclient to use, reference counter must have already been increased
is_natthis a NAT session, we should wait for a client to connect to us from an address, then assign that to the session
Returns
new session object

Definition at line 1883 of file plugin_transport_tcp.c.

1888 {
1889  struct GNUNET_ATS_Session *session;
1890  struct PendingMessage *pm;
1891 
1892  if (GNUNET_YES != is_nat)
1893  GNUNET_assert (NULL != client);
1894  else
1895  GNUNET_assert (NULL == client);
1896 
1898  "Creating new session for peer `%s' at address %s\n",
1899  GNUNET_i2s (&address->peer),
1901  address->address,
1902  address->address_length));
1903  session = GNUNET_new (struct GNUNET_ATS_Session);
1905  session->plugin = plugin;
1906  session->is_nat = is_nat;
1907  if (NULL != client)
1908  {
1909  session->client = client;
1911  }
1913  session->target = address->peer;
1914  session->expecting_welcome = GNUNET_YES;
1915  session->scope = scope;
1916  pm = GNUNET_malloc (sizeof(struct PendingMessage)
1917  + sizeof(struct WelcomeMessage));
1918  pm->msg = (const char *) &pm[1];
1919  pm->message_size = sizeof(struct WelcomeMessage);
1920  GNUNET_memcpy (&pm[1], &plugin->my_welcome, sizeof(struct WelcomeMessage));
1921  pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1922  GNUNET_STATISTICS_update (plugin->env->stats,
1923  gettext_noop ("# bytes currently in TCP buffers"),
1924  pm->message_size,
1925  GNUNET_NO);
1927  session->pending_messages_tail,
1928  pm);
1929  session->msgs_in_queue++;
1930  session->bytes_in_queue += pm->message_size;
1931  session->timeout =
1933  session->timeout_task =
1935  &session_timeout,
1936  session);
1938  if (GNUNET_YES != is_nat)
1939  {
1940  GNUNET_STATISTICS_update (plugin->env->stats,
1941  gettext_noop ("# TCP sessions active"),
1942  1,
1943  GNUNET_NO);
1945  }
1946  else
1947  {
1948  notify_session_monitor (session->plugin,
1949  session,
1951  }
1952  return session;
1953 }
struct GNUNET_HELLO_Address * GNUNET_HELLO_address_copy(const struct GNUNET_HELLO_Address *address)
Copy an address struct.
Definition: address.c:99
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
@ GNUNET_TRANSPORT_SS_INIT
The session was created (first call for each session object).
@ GNUNET_TRANSPORT_SS_HANDSHAKE
Initial session handshake is in progress.
@ GNUNET_TRANSPORT_SS_UP
Session is fully UP.
static enum GNUNET_NetworkType scope
Which network scope do we belong to?
int is_nat
Was this session created using NAT traversal?
enum GNUNET_NetworkType scope
ATS network type.
int expecting_welcome
Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
struct GNUNET_TIME_Absolute last_activity
Last activity on this connection.
struct TransportClient * client
Client that issued the transmission request, if pmt is PMT_CORE.
Initial handshake message for a session.

References address, GNUNET_ATS_Session::address, GNUNET_ATS_Session::bytes_in_queue, PendingMessage::client, GNUNET_ATS_Session::client, GNUNET_ATS_Session::expecting_welcome, gettext_noop, GNUNET_assert, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_HELLO_address_copy(), GNUNET_i2s(), GNUNET_malloc, GNUNET_memcpy, GNUNET_new, GNUNET_NO, GNUNET_SCHEDULER_add_delayed(), GNUNET_SERVER_client_set_user_context, GNUNET_STATISTICS_update(), GNUNET_TIME_absolute_get(), GNUNET_TIME_relative_to_absolute(), GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TRANSPORT_SS_HANDSHAKE, GNUNET_TRANSPORT_SS_INIT, GNUNET_TRANSPORT_SS_UP, GNUNET_YES, GNUNET_ATS_Session::is_nat, GNUNET_ATS_Session::last_activity, LOG, GNUNET_ATS_Session::msgs_in_queue, notify_session_monitor(), GNUNET_ATS_Session::pending_messages_head, GNUNET_ATS_Session::pending_messages_tail, plugin, GNUNET_ATS_Session::plugin, pm, GNUNET_ATS_Session::scope, scope, session_timeout(), GNUNET_ATS_Session::target, tcp_plugin_address_to_string(), GNUNET_ATS_Session::timeout, and GNUNET_ATS_Session::timeout_task.

Referenced by handle_tcp_welcome(), and tcp_plugin_get_session().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_pending_messages()

static void process_pending_messages ( struct GNUNET_ATS_Session session)
static

If we have pending messages, ask the server to transmit them (schedule the respective tasks, etc.)

Parameters
sessionfor which session should we do this

Definition at line 2125 of file plugin_transport_tcp.c.

2126 {
2127  struct PendingMessage *pm;
2128 
2129  GNUNET_assert (NULL != session->client);
2130  if (NULL != session->transmit_handle)
2131  return;
2132  if (NULL == (pm = session->pending_messages_head))
2133  return;
2134 
2135  session->transmit_handle =
2137  pm->message_size,
2139  pm->timeout),
2140  &do_transmit,
2141  session);
2142 }
static size_t do_transmit(void *cls, size_t size, void *buf)
Function called to notify a client about the socket being ready to queue more data.
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)
Notify us when the server has enough space to transmit a message of the given size to the given clien...

References GNUNET_ATS_Session::client, do_transmit(), GNUNET_assert, GNUNET_SERVER_notify_transmit_ready(), GNUNET_TIME_absolute_get_remaining(), GNUNET_ATS_Session::pending_messages_head, pm, and GNUNET_ATS_Session::transmit_handle.

Referenced by do_transmit(), handle_tcp_nat_probe(), handle_tcp_welcome(), tcp_plugin_get_session(), and tcp_plugin_send().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_transmit()

static size_t do_transmit ( void *  cls,
size_t  size,
void *  buf 
)
static

Function called to notify a client about the socket being ready to queue more data.

"buf" will be NULL and "size" zero if the socket was closed for writing in the meantime.

Parameters
clsclosure
sizenumber of bytes available in buf
bufwhere the callee should write the message
Returns
number of bytes written to buf

Definition at line 1978 of file plugin_transport_tcp.c.

1979 {
1980  struct GNUNET_ATS_Session *session = cls;
1981  struct GNUNET_PeerIdentity pid;
1982  struct Plugin *plugin;
1983  struct PendingMessage *pos;
1984  struct PendingMessage *hd;
1985  struct PendingMessage *tl;
1986  struct GNUNET_TIME_Absolute now;
1987  char *cbuf;
1988  size_t ret;
1989 
1990  session->transmit_handle = NULL;
1991  plugin = session->plugin;
1992  if (NULL == buf)
1993  {
1995  "Timeout trying to transmit to peer `%s', discarding message queue.\n",
1996  GNUNET_i2s (&session->target));
1997  /* timeout; cancel all messages that have already expired */
1998  hd = NULL;
1999  tl = NULL;
2000  ret = 0;
2001  now = GNUNET_TIME_absolute_get ();
2002  while ((NULL != (pos = session->pending_messages_head)) &&
2003  (pos->timeout.abs_value_us <= now.abs_value_us))
2004  {
2006  session->pending_messages_tail,
2007  pos);
2008  GNUNET_assert (0 < session->msgs_in_queue);
2009  session->msgs_in_queue--;
2010  GNUNET_assert (pos->message_size <= session->bytes_in_queue);
2011  session->bytes_in_queue -= pos->message_size;
2013  "Failed to transmit %lu byte message to `%s'.\n",
2014  (unsigned long) pos->message_size,
2015  GNUNET_i2s (&session->target));
2016  ret += pos->message_size;
2017  GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
2018  }
2019  /* do this call before callbacks (so that if callbacks destroy
2020  * session, they have a chance to cancel actions done by this
2021  * call) */
2022  process_pending_messages (session);
2023  pid = session->target;
2024  /* no do callbacks and do not use session again since
2025  * the callbacks may abort the session */
2026  while (NULL != (pos = hd))
2027  {
2028  GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
2029  if (NULL != pos->transmit_cont)
2030  pos->transmit_cont (pos->transmit_cont_cls,
2031  &pid,
2032  GNUNET_SYSERR,
2033  pos->message_size,
2034  0);
2035  GNUNET_free (pos);
2036  }
2037  GNUNET_STATISTICS_update (plugin->env->stats,
2038  gettext_noop ("# bytes currently in TCP buffers"),
2039  -(int64_t) ret,
2040  GNUNET_NO);
2041  GNUNET_STATISTICS_update (plugin->env->stats,
2042  gettext_noop (
2043  "# bytes discarded by TCP (timeout)"),
2044  ret,
2045  GNUNET_NO);
2046  if (0 < ret)
2047  notify_session_monitor (session->plugin,
2048  session,
2050  return 0;
2051  }
2052  /* copy all pending messages that would fit */
2053  ret = 0;
2054  cbuf = buf;
2055  hd = NULL;
2056  tl = NULL;
2057  while (NULL != (pos = session->pending_messages_head))
2058  {
2059  if (ret + pos->message_size > size)
2060  break;
2062  session->pending_messages_tail,
2063  pos);
2064  GNUNET_assert (0 < session->msgs_in_queue);
2065  session->msgs_in_queue--;
2066  GNUNET_assert (pos->message_size <= session->bytes_in_queue);
2067  session->bytes_in_queue -= pos->message_size;
2068  GNUNET_assert (size >= pos->message_size);
2070  "Transmitting message of type %u size %lu to peer %s at %s\n",
2071  ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type),
2072  (unsigned long) pos->message_size,
2073  GNUNET_i2s (&session->target),
2075  session->address->address,
2076  session->address->address_length));
2077  /* FIXME: this GNUNET_memcpy can be up to 7% of our total runtime */
2078  GNUNET_memcpy (cbuf, pos->msg, pos->message_size);
2079  cbuf += pos->message_size;
2080  ret += pos->message_size;
2081  size -= pos->message_size;
2082  GNUNET_CONTAINER_DLL_insert_tail (hd, tl, pos);
2083  }
2085  /* schedule 'continuation' before callbacks so that callbacks that
2086  * cancel everything don't cause us to use a session that no longer
2087  * exists... */
2088  process_pending_messages (session);
2090  pid = session->target;
2091  /* we'll now call callbacks that may cancel the session; hence
2092  * we should not use 'session' after this point */
2093  while (NULL != (pos = hd))
2094  {
2095  GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
2096  if (NULL != pos->transmit_cont)
2097  pos->transmit_cont (pos->transmit_cont_cls,
2098  &pid,
2099  GNUNET_OK,
2100  pos->message_size,
2101  pos->message_size); /* FIXME: include TCP overhead */
2102  GNUNET_free (pos);
2103  }
2104  GNUNET_assert (NULL == hd);
2105  GNUNET_assert (NULL == tl);
2106  GNUNET_STATISTICS_update (plugin->env->stats,
2107  gettext_noop ("# bytes currently in TCP buffers"),
2108  -(int64_t) ret,
2109  GNUNET_NO);
2110  GNUNET_STATISTICS_update (plugin->env->stats,
2111  gettext_noop ("# bytes transmitted via TCP"),
2112  ret,
2113  GNUNET_NO);
2114  return ret;
2115 }
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
#define GNUNET_CONTAINER_DLL_insert_after(head, tail, other, element)
Insert an element into a DLL after the given other element.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
static void process_pending_messages(struct GNUNET_ATS_Session *session)
If we have pending messages, ask the server to transmit them (schedule the respective tasks,...
The identity of the host (wraps the signing key of the peer).
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
size_t message_size
So that the gnunet-service-transport can group messages together, these pending messages need to acce...
const char * msg
The pending message.
GNUNET_TRANSPORT_TransmitContinuation transmit_cont
Continuation function to call once the message has been sent.
struct GNUNET_TIME_Absolute timeout
At what time should we give up on the transmission (and no longer retry)?
void * transmit_cont_cls
Closure for transmit_cont.

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_HELLO_Address::address, GNUNET_ATS_Session::address, GNUNET_HELLO_Address::address_length, buf, GNUNET_ATS_Session::bytes_in_queue, gettext_noop, GNUNET_assert, GNUNET_CONTAINER_DLL_insert_after, GNUNET_CONTAINER_DLL_insert_tail, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_i2s(), GNUNET_memcpy, GNUNET_NO, GNUNET_OK, GNUNET_STATISTICS_update(), GNUNET_SYSERR, GNUNET_TIME_absolute_get(), GNUNET_TRANSPORT_SS_UPDATE, GNUNET_ATS_Session::last_activity, LOG, PendingMessage::message_size, PendingMessage::msg, GNUNET_ATS_Session::msgs_in_queue, notify_session_monitor(), GNUNET_ATS_Session::pending_messages_head, GNUNET_ATS_Session::pending_messages_tail, pid, plugin, GNUNET_ATS_Session::plugin, process_pending_messages(), ret, size, GNUNET_ATS_Session::target, tcp_plugin_address_to_string(), PendingMessage::timeout, PendingMessage::transmit_cont, PendingMessage::transmit_cont_cls, and GNUNET_ATS_Session::transmit_handle.

Referenced by process_pending_messages().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_plugin_send()

static ssize_t tcp_plugin_send ( void *  cls,
struct GNUNET_ATS_Session session,
const char *  msgbuf,
size_t  msgbuf_size,
unsigned int  priority,
struct GNUNET_TIME_Relative  to,
GNUNET_TRANSPORT_TransmitContinuation  cont,
void *  cont_cls 
)
static

Function that can be used by the transport service to transmit a message using the plugin.

Note that in the case of a peer disconnecting, the continuation MUST be called prior to the disconnect notification itself. This function will be called with this peer's HELLO message to initiate a fresh connection to another peer.

Parameters
clsclosure
sessionwhich session must be used
msgbufthe message to transmit
msgbuf_sizenumber of bytes in msgbuf
priorityhow important is the message (most plugins will ignore message priority and just FIFO)
tohow long to wait at most for the transmission (does not require plugins to discard the message after the timeout, just advisory for the desired delay; most plugins will ignore this as well)
contcontinuation to call once the message has been transmitted (or if the transport is ready for the next transmission call; or if the peer disconnected...); can be NULL
cont_clsclosure for cont
Returns
number of bytes used (on the physical network, with overheads); -1 on hard errors (i.e. address invalid); 0 is a legal value and does NOT mean that the message was not transmitted (DV)

Definition at line 2173 of file plugin_transport_tcp.c.

2181 {
2182  struct Plugin *plugin = cls;
2183  struct PendingMessage *pm;
2184 
2185  /* create new message entry */
2186  pm = GNUNET_malloc (sizeof(struct PendingMessage) + msgbuf_size);
2187  pm->msg = (const char *) &pm[1];
2188  GNUNET_memcpy (&pm[1], msgbuf, msgbuf_size);
2189  pm->message_size = msgbuf_size;
2190  pm->timeout = GNUNET_TIME_relative_to_absolute (to);
2191  pm->transmit_cont = cont;
2192  pm->transmit_cont_cls = cont_cls;
2193 
2195  "Asked to transmit %lu bytes to `%s', added message to list.\n",
2196  (unsigned long) msgbuf_size,
2197  GNUNET_i2s (&session->target));
2198 
2199  if (GNUNET_YES ==
2201  &session->target,
2202  session))
2203  {
2204  GNUNET_assert (NULL != session->client);
2207  GNUNET_STATISTICS_update (plugin->env->stats,
2208  gettext_noop ("# bytes currently in TCP buffers"),
2209  msgbuf_size,
2210  GNUNET_NO);
2211 
2212  /* append pm to pending_messages list */
2214  session->pending_messages_tail,
2215  pm);
2216  notify_session_monitor (session->plugin,
2217  session,
2219  session->msgs_in_queue++;
2220  session->bytes_in_queue += pm->message_size;
2221  process_pending_messages (session);
2222  return msgbuf_size;
2223  }
2224  if (GNUNET_YES ==
2226  &session->target,
2227  session))
2228  {
2230  "This NAT WAIT session for peer `%s' is not yet ready!\n",
2231  GNUNET_i2s (&session->target));
2232  GNUNET_STATISTICS_update (plugin->env->stats,
2233  gettext_noop ("# bytes currently in TCP buffers"),
2234  msgbuf_size,
2235  GNUNET_NO);
2236  /* append pm to pending_messages list */
2238  session->pending_messages_tail,
2239  pm);
2240  session->msgs_in_queue++;
2241  session->bytes_in_queue += pm->message_size;
2242  notify_session_monitor (session->plugin,
2243  session,
2245  return msgbuf_size;
2246  }
2247  LOG (GNUNET_ERROR_TYPE_ERROR, "Invalid session %p\n", session);
2248  if (NULL != cont)
2249  cont (cont_cls, &session->target, GNUNET_SYSERR, pm->message_size, 0);
2250  GNUNET_break (0);
2251  GNUNET_free (pm);
2252  return GNUNET_SYSERR; /* session does not exist here */
2253 }
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multipeermap_contains_value(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, const void *value)
Check if the map contains the given value under the given key.
void GNUNET_SERVER_client_set_timeout(struct GNUNET_SERVER_Client *client, struct GNUNET_TIME_Relative timeout)
Change the timeout for a particular client.

References GNUNET_ATS_Session::bytes_in_queue, GNUNET_ATS_Session::client, gettext_noop, GNUNET_assert, GNUNET_break, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_CONTAINER_DLL_insert_tail, GNUNET_CONTAINER_multipeermap_contains_value(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_i2s(), GNUNET_malloc, GNUNET_memcpy, GNUNET_NO, GNUNET_SERVER_client_set_timeout(), GNUNET_STATISTICS_update(), GNUNET_SYSERR, GNUNET_TIME_relative_to_absolute(), GNUNET_TRANSPORT_SS_HANDSHAKE, GNUNET_TRANSPORT_SS_UPDATE, GNUNET_YES, LOG, GNUNET_ATS_Session::msgs_in_queue, notify_session_monitor(), GNUNET_ATS_Session::pending_messages_head, GNUNET_ATS_Session::pending_messages_tail, plugin, GNUNET_ATS_Session::plugin, pm, process_pending_messages(), and GNUNET_ATS_Session::target.

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ session_lookup_it()

static int session_lookup_it ( void *  cls,
const struct GNUNET_PeerIdentity key,
void *  value 
)
static

Look for a session by address.

Parameters
clsthe struct GNUNET_ATS_SessionItCtx
keyunused
valuea struct GNUNET_ATS_Session
Returns
GNUNET_YES to continue looking, GNUNET_NO if we found the session

Definition at line 2282 of file plugin_transport_tcp.c.

2285 {
2286  struct GNUNET_ATS_SessionItCtx *si_ctx = cls;
2287  struct GNUNET_ATS_Session *session = value;
2288 
2289  if (0 != GNUNET_HELLO_address_cmp (si_ctx->address, session->address))
2290  return GNUNET_YES;
2291  si_ctx->result = session;
2292  return GNUNET_NO;
2293 }
static char * value
Value of the record to add/remove.
int GNUNET_HELLO_address_cmp(const struct GNUNET_HELLO_Address *a1, const struct GNUNET_HELLO_Address *a2)
Compare two addresses.
Definition: address.c:112
Closure for session_lookup_it().
struct GNUNET_ATS_Session * result
Where to store the session (if we found it).
const struct GNUNET_HELLO_Address * address
Address we are looking for.

References GNUNET_ATS_Session::address, GNUNET_ATS_SessionItCtx::address, GNUNET_HELLO_address_cmp(), GNUNET_NO, GNUNET_YES, GNUNET_ATS_SessionItCtx::result, and value.

Referenced by tcp_plugin_get_session().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nat_connect_timeout()

static void nat_connect_timeout ( void *  cls)
static

Task cleaning up a NAT connection attempt after timeout.

Parameters
clsthe struct GNUNET_ATS_Session

Definition at line 2302 of file plugin_transport_tcp.c.

2303 {
2304  struct GNUNET_ATS_Session *session = cls;
2305 
2306  session->nat_connection_timeout = NULL;
2308  "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
2309  GNUNET_i2s (&session->target),
2311  session->address->address,
2312  session->address->address_length));
2313  tcp_plugin_disconnect_session (session->plugin, session);
2314 }

References GNUNET_HELLO_Address::address, GNUNET_ATS_Session::address, GNUNET_HELLO_Address::address_length, GNUNET_ERROR_TYPE_DEBUG, GNUNET_i2s(), LOG, GNUNET_ATS_Session::nat_connection_timeout, GNUNET_ATS_Session::plugin, GNUNET_ATS_Session::target, tcp_plugin_address_to_string(), and tcp_plugin_disconnect_session().

Referenced by tcp_plugin_get_session().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_plugin_update_session_timeout()

static void tcp_plugin_update_session_timeout ( void *  cls,
const struct GNUNET_PeerIdentity peer,
struct GNUNET_ATS_Session session 
)
static

Function that will be called whenever the transport service wants to notify the plugin that a session is still active and in use and therefore the session timeout for this session has to be updated.

Parameters
clsclosure
peerwhich peer was the session for
sessionwhich session is being updated

Definition at line 2327 of file plugin_transport_tcp.c.

2330 {
2331  reschedule_session_timeout (session);
2332 }
static void reschedule_session_timeout(struct GNUNET_ATS_Session *s)
Increment session timeout due to activity.

References reschedule_session_timeout().

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ delayed_done()

static void delayed_done ( void *  cls)
static

Task to signal the server that we can continue receiving from the TCP client now.

Parameters
clsthe struct GNUNET_ATS_Session *

Definition at line 2342 of file plugin_transport_tcp.c.

2343 {
2344  struct GNUNET_ATS_Session *session = cls;
2345 
2346  session->receive_delay_task = NULL;
2347  reschedule_session_timeout (session);
2349 }
void GNUNET_SERVER_receive_done(struct GNUNET_SERVER_Client *client, int success)
Resume receiving from this client, we are done processing the current request.

References GNUNET_ATS_Session::client, GNUNET_OK, GNUNET_SERVER_receive_done(), GNUNET_ATS_Session::receive_delay_task, and reschedule_session_timeout().

Referenced by handle_tcp_data(), and tcp_plugin_update_inbound_delay().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_plugin_update_inbound_delay()

static void tcp_plugin_update_inbound_delay ( void *  cls,
const struct GNUNET_PeerIdentity peer,
struct GNUNET_ATS_Session session,
struct GNUNET_TIME_Relative  delay 
)
static

Function that will be called whenever the transport service wants to notify the plugin that the inbound quota changed and that the plugin should update it's delay for the next receive value.

Parameters
clsclosure
peerwhich peer was the session for
sessionwhich session is being updated
delaynew delay to use for receiving

Definition at line 2363 of file plugin_transport_tcp.c.

2367 {
2368  if (NULL == session->receive_delay_task)
2369  return;
2371  "New inbound delay %s\n",
2375  session->receive_delay_task =
2377 }
static struct GNUNET_TIME_Relative delay
When should dkg communication start?
static void delayed_done(void *cls)
Task to signal the server that we can continue receiving from the TCP client now.

References delay, delayed_done(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_NO, GNUNET_SCHEDULER_add_delayed(), GNUNET_SCHEDULER_cancel(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_TIME_relative_to_absolute(), LOG, GNUNET_ATS_Session::receive_delay, and GNUNET_ATS_Session::receive_delay_task.

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_plugin_get_session()

static struct GNUNET_ATS_Session* tcp_plugin_get_session ( void *  cls,
const struct GNUNET_HELLO_Address address 
)
static

Create a new session to transmit data to the target This session will used to send data to this peer and the plugin will notify us by calling the env->session_end function.

Parameters
clsclosure
addressthe address to use
Returns
the session if the address is valid, NULL otherwise

Definition at line 2390 of file plugin_transport_tcp.c.

2391 {
2392  struct Plugin *plugin = cls;
2393  struct GNUNET_ATS_Session *session = NULL;
2394  int af;
2395  const void *sb;
2396  size_t sbs;
2397  struct GNUNET_CONNECTION_Handle *sa;
2398  struct sockaddr_in a4;
2399  struct sockaddr_in6 a6;
2400  const struct IPv4TcpAddress *t4;
2401  const struct IPv6TcpAddress *t6;
2402  unsigned int options;
2403  enum GNUNET_NetworkType net_type;
2404  unsigned int is_natd = GNUNET_NO;
2405  size_t addrlen;
2406 
2407 #ifdef TCP_STEALTH
2408  struct GNUNET_NETWORK_Handle *s;
2409 #endif
2410 
2411  addrlen = address->address_length;
2413  "Trying to get session for `%s' address of peer `%s'\n",
2415  address->address,
2416  address->address_length),
2417  GNUNET_i2s (&address->peer));
2418 
2421  {
2422  GNUNET_break (0);
2423  return NULL;
2424  }
2425 
2426  /* look for existing session */
2428  &address->peer))
2429  {
2430  struct GNUNET_ATS_SessionItCtx si_ctx;
2431 
2432  si_ctx.address = address;
2433  si_ctx.result = NULL;
2435  &address->peer,
2437  &si_ctx);
2438  if (NULL != si_ctx.result)
2439  {
2440  session = si_ctx.result;
2442  "Found existing session for `%s' address `%s'\n",
2443  GNUNET_i2s (&address->peer),
2445  address->address,
2446  address->address_length));
2447  return session;
2448  }
2449  /* This is a bit of a hack, limiting TCP to never allow more than
2450  one TCP connection to any given peer at the same time.
2451  Without this, peers sometimes disagree about which of the TCP
2452  connections they should use, causing one side to believe that
2453  they transmit successfully, while the other receives nothing. */return NULL; /* Refuse to have more than one TCP connection per
2454  peer pair at the same time. */
2455  }
2456 
2457  if (addrlen == sizeof(struct IPv6TcpAddress))
2458  {
2459  GNUNET_assert (NULL != address->address); /* make static analysis happy */
2460  t6 = address->address;
2461  options = t6->options;
2462  af = AF_INET6;
2463  memset (&a6, 0, sizeof(a6));
2464 #if HAVE_SOCKADDR_IN_SIN_LEN
2465  a6.sin6_len = sizeof(a6);
2466 #endif
2467  a6.sin6_family = AF_INET6;
2468  a6.sin6_port = t6->t6_port;
2469  if (t6->t6_port == 0)
2470  is_natd = GNUNET_YES;
2471  GNUNET_memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
2472  sb = &a6;
2473  sbs = sizeof(a6);
2474  }
2475  else if (addrlen == sizeof(struct IPv4TcpAddress))
2476  {
2477  GNUNET_assert (NULL != address->address); /* make static analysis happy */
2478  t4 = address->address;
2479  options = t4->options;
2480  af = AF_INET;
2481  memset (&a4, 0, sizeof(a4));
2482 #if HAVE_SOCKADDR_IN_SIN_LEN
2483  a4.sin_len = sizeof(a4);
2484 #endif
2485  a4.sin_family = AF_INET;
2486  a4.sin_port = t4->t4_port;
2487  if (t4->t4_port == 0)
2488  is_natd = GNUNET_YES;
2489  a4.sin_addr.s_addr = t4->ipv4_addr;
2490  sb = &a4;
2491  sbs = sizeof(a4);
2492  }
2493  else
2494  {
2496  plugin->env->stats,
2497  gettext_noop ("# requests to create session with invalid address"),
2498  1,
2499  GNUNET_NO);
2500  return NULL;
2501  }
2502 
2503  net_type = plugin->env->get_address_type (plugin->env->cls, sb, sbs);
2504  GNUNET_break (net_type != GNUNET_NT_UNSPECIFIED);
2505 
2506  if ((is_natd == GNUNET_YES) && (addrlen == sizeof(struct IPv6TcpAddress)))
2507  {
2508  /* NAT client only works with IPv4 addresses */
2509  return NULL;
2510  }
2511 
2512  if (plugin->cur_connections >= plugin->max_connections)
2513  {
2514  /* saturated */
2515  return NULL;
2516  }
2517 
2518  if ((is_natd == GNUNET_YES) &&
2519  (GNUNET_YES ==
2521  &address->peer)))
2522  {
2523  /* Only do one NAT punch attempt per peer identity */
2524  return NULL;
2525  }
2526 
2527  if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
2528  (GNUNET_NO ==
2530  &address->peer)))
2531  {
2532  struct sockaddr_in local_sa;
2533 
2535  "Found valid IPv4 NAT address (creating session)!\n");
2536  session = create_session (plugin, address, net_type, NULL, GNUNET_YES);
2537  session->nat_connection_timeout =
2541  plugin->nat_wait_conns,
2542  &session->target,
2543  session,
2545 
2547  "Created NAT WAIT connection to `%s' at `%s'\n",
2548  GNUNET_i2s (&session->target),
2549  GNUNET_a2s (sb, sbs));
2550  memset (&local_sa, 0, sizeof(local_sa));
2551  local_sa.sin_family = AF_INET;
2552  local_sa.sin_port = htons (plugin->open_port);
2553  /* We leave sin_address at 0, let the kernel figure it out,
2554  even if our bind() is more specific. (May want to reconsider
2555  later.) */
2556  if (GNUNET_OK == GNUNET_NAT_request_reversal (plugin->nat, &local_sa, &a4))
2557  return session;
2559  "Running NAT client for `%s' at `%s' failed\n",
2560  GNUNET_i2s (&session->target),
2561  GNUNET_a2s (sb, sbs));
2563  return NULL;
2564  }
2565 
2566  /* create new outbound session */
2567  if (0 != (options & TCP_OPTIONS_TCP_STEALTH))
2568  {
2569 #ifdef TCP_STEALTH
2570  s = GNUNET_NETWORK_socket_create (af, SOCK_STREAM, 0);
2571  if (NULL == s)
2572  {
2574  "socket");
2575  sa = NULL;
2576  }
2577  else
2578  {
2579  if ((GNUNET_OK !=
2581  IPPROTO_TCP,
2582  TCP_STEALTH,
2583  &session->target,
2584  sizeof(
2585  struct GNUNET_PeerIdentity))) ||
2586  (GNUNET_OK !=
2588  IPPROTO_TCP,
2589  TCP_STEALTH_INTEGRITY,
2590  &plugin->my_welcome,
2591  sizeof(struct WelcomeMessage))))
2592  {
2593  /* TCP STEALTH not supported by kernel */
2595  sa = NULL;
2596  }
2597  else
2598  {
2599  sa = GNUNET_CONNECTION_connect_socket (s, sb, sbs);
2600  }
2601  }
2602 #else
2603  sa = NULL;
2604 #endif
2605  }
2606  else
2607  {
2608  sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
2609  }
2610  if (NULL == sa)
2611  {
2613  "Failed to create connection to `%s' at `%s'\n",
2614  GNUNET_i2s (&address->peer),
2615  GNUNET_a2s (sb, sbs));
2616  return NULL;
2617  }
2619  "Asked to transmit to `%s', creating fresh session using address `%s'.\n",
2620  GNUNET_i2s (&address->peer),
2621  GNUNET_a2s (sb, sbs));
2622 
2623  session = create_session (plugin,
2624  address,
2625  net_type,
2626  GNUNET_SERVER_connect_socket (plugin->server, sa),
2627  GNUNET_NO);
2629  plugin->sessionmap,
2630  &session->target,
2631  session,
2633  /* Send TCP Welcome */
2634  process_pending_messages (session);
2635 
2636  return session;
2637 }
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multipeermap_contains(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Check if the map contains any value under the given key (including values that are NULL).
int GNUNET_CONTAINER_multipeermap_get_multiple(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, GNUNET_CONTAINER_PeerMapIterator it, void *it_cls)
Iterate over all entries in the map that match a particular key.
int GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE
Allow multiple values with the same key.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
@ GNUNET_ERROR_TYPE_BULK
int GNUNET_NAT_request_reversal(struct GNUNET_NAT_Handle *nh, const struct sockaddr_in *local_sa, const struct sockaddr_in *remote_sa)
We learned about a peer (possibly behind NAT) so run the gnunet-nat-client to send dummy ICMP respons...
Definition: nat_api.c:646
int GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, int level, int option_name, const void *option_value, socklen_t option_len)
Set socket option.
Definition: network.c:806
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
Definition: gnunet_nt_lib.h:39
@ GNUNET_NT_UNSPECIFIED
Category of last resort.
Definition: gnunet_nt_lib.h:43
#define NAT_TIMEOUT
How long until we give up on establishing an NAT connection? Must be > 4 RTT.
struct GNUNET_SERVER_Client * GNUNET_SERVER_connect_socket(struct GNUNET_SERVER_Handle *server, struct GNUNET_CONNECTION_Handle *connection)
Add a TCP socket-based connection to the set of handles managed by this server.
static void nat_connect_timeout(void *cls)
Task cleaning up a NAT connection attempt after timeout.
static struct GNUNET_ATS_Session * create_session(struct Plugin *plugin, const struct GNUNET_HELLO_Address *address, enum GNUNET_NetworkType scope, struct GNUNET_SERVER_Client *client, int is_nat)
Create a new session.
static int session_lookup_it(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
Look for a session by address.
handle for a network connection
socklen_t addrlen
Number of bytes in addr.
Definition: network.c:70
struct GNUNET_CONNECTION_Handle * GNUNET_CONNECTION_connect_socket(struct GNUNET_NETWORK_Handle *s, const struct sockaddr *serv_addr, socklen_t addrlen)
Create a connection handle by (asynchronously) connecting to a host.
struct GNUNET_CONNECTION_Handle * GNUNET_CONNECTION_create_from_sockaddr(int af_family, const struct sockaddr *serv_addr, socklen_t addrlen)
Create a connection handle by creating a socket and (asynchronously) connecting to a host.

References address, GNUNET_ATS_SessionItCtx::address, GNUNET_NETWORK_Handle::addrlen, create_session(), gettext_noop, GNUNET_a2s(), GNUNET_assert, GNUNET_break, GNUNET_CONNECTION_connect_socket(), GNUNET_CONNECTION_create_from_sockaddr(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_CONTAINER_multipeermap_contains(), GNUNET_CONTAINER_multipeermap_get_multiple(), GNUNET_CONTAINER_multipeermap_put(), GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_HELLO_address_check_option(), GNUNET_HELLO_ADDRESS_INFO_INBOUND, GNUNET_i2s(), GNUNET_log_strerror, GNUNET_memcpy, GNUNET_NAT_request_reversal(), GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_create(), GNUNET_NETWORK_socket_setsockopt(), GNUNET_NO, GNUNET_NT_UNSPECIFIED, GNUNET_OK, GNUNET_SCHEDULER_add_delayed(), GNUNET_SERVER_connect_socket(), GNUNET_STATISTICS_update(), GNUNET_YES, LOG, nat_connect_timeout(), GNUNET_ATS_Session::nat_connection_timeout, NAT_TIMEOUT, options, plugin, process_pending_messages(), GNUNET_ATS_SessionItCtx::result, session_lookup_it(), t4, t6, GNUNET_ATS_Session::target, TCP_OPTIONS_TCP_STEALTH, tcp_plugin_address_to_string(), and tcp_plugin_disconnect_session().

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ session_disconnect_it()

static int session_disconnect_it ( void *  cls,
const struct GNUNET_PeerIdentity key,
void *  value 
)
static

We have been asked to destroy all connections to a particular peer.

This function is called on each applicable session and must tear it down.

Parameters
clsthe struct Plugin *
keythe peer which the session belongs to (unused)
valuethe struct GNUNET_ATS_Session
Returns
GNUNET_YES (continue to iterate)

Definition at line 2651 of file plugin_transport_tcp.c.

2654 {
2655  struct Plugin *plugin = cls;
2656  struct GNUNET_ATS_Session *session = value;
2657 
2658  GNUNET_STATISTICS_update (session->plugin->env->stats,
2659  gettext_noop (
2660  "# transport-service disconnect requests for TCP"),
2661  1,
2662  GNUNET_NO);
2664  return GNUNET_YES;
2665 }

References Plugin::env, gettext_noop, GNUNET_NO, GNUNET_STATISTICS_update(), GNUNET_YES, plugin, GNUNET_ATS_Session::plugin, tcp_plugin_disconnect_session(), and value.

Referenced by libgnunet_plugin_transport_tcp_done(), and tcp_plugin_disconnect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_plugin_disconnect()

static void tcp_plugin_disconnect ( void *  cls,
const struct GNUNET_PeerIdentity target 
)
static

Function that can be called to force a disconnect from the specified neighbour.

This should also cancel all previously scheduled transmissions. Obviously the transmission may have been partially completed already, which is OK. The plugin is supposed to close the connection (if applicable) and no longer call the transmit continuation(s).

Finally, plugin MUST NOT call the services's receive function to notify the service that the connection to the specified target was closed after a getting this call.

Parameters
clsclosure
targetpeer for which the last transmission is to be cancelled

Definition at line 2685 of file plugin_transport_tcp.c.

2686 {
2687  struct Plugin *plugin = cls;
2688 
2690  "Disconnecting peer `%s'\n",
2691  GNUNET_i2s (target));
2693  target,
2695  plugin);
2697  target,
2699  plugin);
2700 }
static int session_disconnect_it(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
We have been asked to destroy all connections to a particular peer.

References GNUNET_CONTAINER_multipeermap_get_multiple(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_i2s(), LOG, plugin, and session_disconnect_it().

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ append_port()

static void append_port ( void *  cls,
const char *  hostname 
)
static

We are processing an address pretty printing request and finished the IP resolution (if applicable).

Append our port and forward the result. If called with hostname NULL, we are done and should clean up the pretty printer (otherwise, there might be multiple hostnames for the IP address and we might receive more).

Parameters
clsthe struct PrettyPrinterContext *
hostnamehostname part of the address

Definition at line 2714 of file plugin_transport_tcp.c.

2715 {
2716  struct PrettyPrinterContext *ppc = cls;
2717  struct Plugin *plugin = ppc->plugin;
2718  char *ret;
2719 
2721  "append_port called with hostname `%s'\n",
2722  hostname);
2723  if (NULL == hostname)
2724  {
2725  /* Final call, done */
2726  ppc->resolver_handle = NULL;
2727  GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
2728  plugin->ppc_dll_tail,
2729  ppc);
2730  ppc->asc (ppc->asc_cls, NULL, GNUNET_OK);
2731  GNUNET_free (ppc);
2732  return;
2733  }
2734  if (GNUNET_YES == ppc->ipv6)
2735  GNUNET_asprintf (&ret,
2736  "%s.%u.[%s]:%d",
2737  PLUGIN_NAME,
2738  ppc->options,
2739  hostname,
2740  ppc->port);
2741  else
2742  GNUNET_asprintf (&ret,
2743  "%s.%u.%s:%d",
2744  PLUGIN_NAME,
2745  ppc->options,
2746  hostname,
2747  ppc->port);
2748  ppc->asc (ppc->asc_cls, ret, GNUNET_OK);
2749  GNUNET_free (ret);
2750 }
#define GNUNET_log(kind,...)
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
Closure for append_port().
struct GNUNET_RESOLVER_RequestHandle * resolver_handle
Resolver handle.
uint16_t port
Port to add after the IP address.
void * asc_cls
Clsoure for asc.
GNUNET_TRANSPORT_AddressStringCallback asc
Function to call with the result.
uint32_t options
Address options.

References PrettyPrinterContext::asc, PrettyPrinterContext::asc_cls, GNUNET_asprintf(), GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_OK, GNUNET_YES, hostname, PrettyPrinterContext::ipv6, PrettyPrinterContext::options, plugin, PrettyPrinterContext::plugin, PLUGIN_NAME, PrettyPrinterContext::port, PrettyPrinterContext::resolver_handle, and ret.

Referenced by tcp_plugin_address_pretty_printer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_plugin_address_pretty_printer()

static void tcp_plugin_address_pretty_printer ( void *  cls,
const char *  type,
const void *  addr,
size_t  addrlen,
int  numeric,
struct GNUNET_TIME_Relative  timeout,
GNUNET_TRANSPORT_AddressStringCallback  asc,
void *  asc_cls 
)
static

Convert the transports address to a nice, human-readable format.

Parameters
clsclosure with the struct Plugin
typename of the transport that generated the address
addrone of the addresses of the host, NULL for the last address the specific address format depends on the transport
addrlenlength of the addr
numericshould (IP) addresses be displayed in numeric form?
timeoutafter how long should we give up?
ascfunction to call on each string
asc_clsclosure for asc

Definition at line 2767 of file plugin_transport_tcp.c.

2775 {
2776  struct Plugin *plugin = cls;
2777  struct PrettyPrinterContext *ppc;
2778  const void *sb;
2779  size_t sbs;
2780  struct sockaddr_in a4;
2781  struct sockaddr_in6 a6;
2782  const struct IPv4TcpAddress *t4;
2783  const struct IPv6TcpAddress *t6;
2784  uint16_t port;
2785  uint32_t options;
2786 
2787  if (sizeof(struct IPv6TcpAddress) == addrlen)
2788  {
2789  t6 = addr;
2790  memset (&a6, 0, sizeof(a6));
2791  a6.sin6_family = AF_INET6;
2792  a6.sin6_port = t6->t6_port;
2793  GNUNET_memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
2794  port = ntohs (t6->t6_port);
2795  options = ntohl (t6->options);
2796  sb = &a6;
2797  sbs = sizeof(a6);
2798  }
2799  else if (sizeof(struct IPv4TcpAddress) == addrlen)
2800  {
2801  t4 = addr;
2802  memset (&a4, 0, sizeof(a4));
2803  a4.sin_family = AF_INET;
2804  a4.sin_port = t4->t4_port;
2805  a4.sin_addr.s_addr = t4->ipv4_addr;
2806  port = ntohs (t4->t4_port);
2807  options = ntohl (t4->options);
2808  sb = &a4;
2809  sbs = sizeof(a4);
2810  }
2811  else
2812  {
2813  /* invalid address */
2815  _ ("Unexpected address length: %u bytes\n"),
2816  (unsigned int) addrlen);
2817  asc (asc_cls, NULL, GNUNET_SYSERR);
2818  asc (asc_cls, NULL, GNUNET_OK);
2819  return;
2820  }
2821  ppc = GNUNET_new (struct PrettyPrinterContext);
2822  ppc->plugin = plugin;
2823  if (addrlen == sizeof(struct IPv6TcpAddress))
2824  ppc->ipv6 = GNUNET_YES;
2825  else
2826  ppc->ipv6 = GNUNET_NO;
2827  ppc->asc = asc;
2828  ppc->asc_cls = asc_cls;
2829  ppc->port = port;
2830  ppc->options = options;
2831  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting DNS reverse lookup\n");
2833  sbs,
2834  ! numeric,
2835  timeout,
2836  &append_port,
2837  ppc);
2838  if (NULL == ppc->resolver_handle)
2839  {
2840  GNUNET_break (0);
2841  GNUNET_free (ppc);
2842  return;
2843  }
2844  GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head, plugin->ppc_dll_tail, ppc);
2845 }
static int numeric
Option -n.
struct GNUNET_RESOLVER_RequestHandle * GNUNET_RESOLVER_hostname_get(const struct sockaddr *sa, socklen_t salen, int do_resolve, struct GNUNET_TIME_Relative timeout, GNUNET_RESOLVER_HostnameCallback callback, void *cls)
Perform a reverse DNS lookup.
static void append_port(void *cls, const char *hostname)
We are processing an address pretty printing request and finished the IP resolution (if applicable).

References _, append_port(), PrettyPrinterContext::asc, PrettyPrinterContext::asc_cls, GNUNET_break, GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_memcpy, GNUNET_new, GNUNET_NO, GNUNET_OK, GNUNET_RESOLVER_hostname_get(), GNUNET_SYSERR, GNUNET_YES, PrettyPrinterContext::ipv6, LOG, numeric, options, PrettyPrinterContext::options, plugin, PrettyPrinterContext::plugin, port, PrettyPrinterContext::port, PrettyPrinterContext::resolver_handle, t4, t6, and timeout.

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_plugin_check_address()

static int tcp_plugin_check_address ( void *  cls,
const void *  addr,
size_t  addrlen 
)
static

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 (as per our configuration).

Naturally, if absolutely necessary, plugins can be a bit conservative in their answer, but in general plugins should make sure that the address does not redirect traffic to a 3rd party that might try to man-in-the-middle our traffic.

Parameters
clsclosure, our struct Plugin *
addrpointer to the address
addrlenlength of addr
Returns
GNUNET_OK if this is a plausible address for this peer and transport, GNUNET_SYSERR if not

Definition at line 2864 of file plugin_transport_tcp.c.

2865 {
2866  struct Plugin *plugin = cls;
2867  const struct IPv4TcpAddress *v4;
2868  const struct IPv6TcpAddress *v6;
2869 
2870  if ((addrlen != sizeof(struct IPv4TcpAddress)) &&
2871  (addrlen != sizeof(struct IPv6TcpAddress)))
2872  {
2873  GNUNET_break_op (0);
2874  return GNUNET_SYSERR;
2875  }
2876 
2877  if (addrlen == sizeof(struct IPv4TcpAddress))
2878  {
2879  struct sockaddr_in s4;
2880 
2881  v4 = (const struct IPv4TcpAddress *) addr;
2882  if (0 != memcmp (&v4->options, &plugin->myoptions, sizeof(uint32_t)))
2883  {
2884  GNUNET_break (0);
2885  return GNUNET_SYSERR;
2886  }
2887  memset (&s4, 0, sizeof(s4));
2888  s4.sin_family = AF_INET;
2889 #if HAVE_SOCKADDR_IN_SIN_LEN
2890  s4.sin_len = sizeof(s4);
2891 #endif
2892  s4.sin_port = v4->t4_port;
2893  s4.sin_addr.s_addr = v4->ipv4_addr;
2894 
2895  if (GNUNET_OK !=
2896  GNUNET_NAT_test_address (plugin->nat, &s4, sizeof(struct sockaddr_in)))
2897  return GNUNET_SYSERR;
2898  }
2899  else
2900  {
2901  struct sockaddr_in6 s6;
2902 
2903  v6 = (const struct IPv6TcpAddress *) addr;
2904  if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2905  {
2906  GNUNET_break_op (0);
2907  return GNUNET_SYSERR;
2908  }
2909  if (0 != memcmp (&v6->options, &plugin->myoptions, sizeof(uint32_t)))
2910  {
2911  GNUNET_break (0);
2912  return GNUNET_SYSERR;
2913  }
2914  memset (&s6, 0, sizeof(s6));
2915  s6.sin6_family = AF_INET6;
2916 #if HAVE_SOCKADDR_IN_SIN_LEN
2917  s6.sin6_len = sizeof(s6);
2918 #endif
2919  s6.sin6_port = v6->t6_port;
2920  s6.sin6_addr = v6->ipv6_addr;
2921 
2923  &s6,
2924  sizeof(struct sockaddr_in6)))
2925  return GNUNET_SYSERR;
2926  }
2927  return GNUNET_OK;
2928 }
int GNUNET_NAT_test_address(struct GNUNET_NAT_Handle *nh, const void *addr, socklen_t addrlen)
Test if the given address is (currently) a plausible IP address for this peer.
Definition: nat_api.c:614

References GNUNET_break, GNUNET_break_op, GNUNET_NAT_test_address(), GNUNET_OK, GNUNET_SYSERR, and plugin.

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_tcp_nat_probe()

static void handle_tcp_nat_probe ( void *  cls,
struct GNUNET_SERVER_Client client,
const struct GNUNET_MessageHeader message 
)
static

We've received a nat probe from this peer via TCP.

Finish creating the client session and resume sending of queued messages.

Parameters
clsclosure
clientidentification of the client
messagethe actual message

Definition at line 2941 of file plugin_transport_tcp.c.

2944 {
2945  struct Plugin *plugin = cls;
2946  struct GNUNET_ATS_Session *session;
2947  const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
2948  size_t alen;
2949  void *vaddr;
2950  struct IPv4TcpAddress *t4;
2951  struct IPv6TcpAddress *t6;
2952  const struct sockaddr_in *s4;
2953  const struct sockaddr_in6 *s6;
2954 
2955  LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NAT probe\n");
2956  /* We have received a TCP NAT probe, meaning we (hopefully) initiated
2957  * a connection to this peer by running gnunet-nat-client. This peer
2958  * received the punch message and now wants us to use the new connection
2959  * as the default for that peer. Do so and then send a WELCOME message
2960  * so we can really be connected!
2961  */if (ntohs (message->size) != sizeof(struct TCP_NAT_ProbeMessage))
2962  {
2963  GNUNET_break_op (0);
2965  return;
2966  }
2967 
2968  tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
2969  if (0 == memcmp (&tcp_nat_probe->clientIdentity,
2970  plugin->env->my_identity,
2971  sizeof(struct GNUNET_PeerIdentity)))
2972  {
2973  /* refuse connections from ourselves */
2975  return;
2976  }
2977 
2978  session = GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
2979  &tcp_nat_probe->clientIdentity);
2980  if (NULL == session)
2981  {
2982  LOG (GNUNET_ERROR_TYPE_DEBUG, "Did NOT find session for NAT probe!\n");
2984  return;
2985  }
2986  LOG (GNUNET_ERROR_TYPE_DEBUG, "Found session for NAT probe!\n");
2987 
2988  if (NULL != session->nat_connection_timeout)
2989  {
2991  session->nat_connection_timeout = NULL;
2992  }
2993 
2994  if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2995  {
2996  GNUNET_break (0);
2999  return;
3000  }
3001  GNUNET_assert (
3002  GNUNET_YES ==
3004  &tcp_nat_probe->clientIdentity,
3005  session));
3006  GNUNET_SERVER_client_set_user_context (client, session);
3008  plugin->sessionmap,
3009  &session->target,
3010  session,
3014  "Found address `%s' for incoming connection\n",
3015  GNUNET_a2s (vaddr, alen));
3016  switch (((const struct sockaddr *) vaddr)->sa_family)
3017  {
3018  case AF_INET:
3019  s4 = vaddr;
3020  t4 = GNUNET_new (struct IPv4TcpAddress);
3021  t4->options = htonl (TCP_OPTIONS_NONE);
3022  t4->t4_port = s4->sin_port;
3023  t4->ipv4_addr = s4->sin_addr.s_addr;
3024  session->address =
3026  PLUGIN_NAME,
3027  &t4,
3028  sizeof(struct IPv4TcpAddress),
3030  break;
3031 
3032  case AF_INET6:
3033  s6 = vaddr;
3034  t6 = GNUNET_new (struct IPv6TcpAddress);
3035  t6->options = htonl (TCP_OPTIONS_NONE);
3036  t6->t6_port = s6->sin6_port;
3037  GNUNET_memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
3038  session->address =
3040  PLUGIN_NAME,
3041  &t6,
3042  sizeof(struct IPv6TcpAddress),
3044  break;
3045 
3046  default:
3047  GNUNET_break_op (0);
3048  LOG (GNUNET_ERROR_TYPE_DEBUG, "Bad address for incoming connection!\n");
3049  GNUNET_free (vaddr);
3052  return;
3053  }
3054  GNUNET_free (vaddr);
3055  GNUNET_break (NULL == session->client);
3056  session->client = client;
3057  GNUNET_STATISTICS_update (plugin->env->stats,
3058  gettext_noop ("# TCP sessions active"),
3059  1,
3060  GNUNET_NO);
3061  process_pending_messages (session);
3063 }
void * GNUNET_CONTAINER_multipeermap_get(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Given a key find a value in the map matching the key.
Basically a WELCOME message, but with the purpose of giving the waiting peer a client handle to use.
struct GNUNET_PeerIdentity clientIdentity
Identity of the sender of the message.
int GNUNET_SERVER_client_get_address(struct GNUNET_SERVER_Client *client, void **addr, size_t *addrlen)
Obtain the network address of the other party.

References GNUNET_ATS_Session::address, GNUNET_ATS_Session::client, TCP_NAT_ProbeMessage::clientIdentity, gettext_noop, GNUNET_a2s(), GNUNET_assert, GNUNET_break, GNUNET_break_op, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE, GNUNET_CONTAINER_multipeermap_get(), GNUNET_CONTAINER_multipeermap_put(), GNUNET_CONTAINER_multipeermap_remove(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_HELLO_address_allocate(), GNUNET_HELLO_ADDRESS_INFO_NONE, GNUNET_memcpy, GNUNET_new, GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_cancel(), GNUNET_SERVER_client_get_address(), GNUNET_SERVER_client_set_user_context, GNUNET_SERVER_receive_done(), GNUNET_STATISTICS_update(), GNUNET_SYSERR, GNUNET_TIME_absolute_get(), GNUNET_YES, GNUNET_ATS_Session::last_activity, LOG, GNUNET_ATS_Session::nat_connection_timeout, plugin, PLUGIN_NAME, process_pending_messages(), GNUNET_MessageHeader::size, t4, t6, GNUNET_ATS_Session::target, TCP_OPTIONS_NONE, and tcp_plugin_disconnect_session().

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_tcp_welcome()

static void handle_tcp_welcome ( void *  cls,
struct GNUNET_SERVER_Client client,
const struct GNUNET_MessageHeader message 
)
static

We've received a welcome from this peer via TCP.

Possibly create a fresh client record and send back our welcome.

Parameters
clsclosure
clientidentification of the client
messagethe actual message

Definition at line 3075 of file plugin_transport_tcp.c.

3078 {
3079  struct Plugin *plugin = cls;
3080  const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
3081  struct GNUNET_HELLO_Address *address;
3082  struct GNUNET_ATS_Session *session;
3083  size_t alen;
3084  void *vaddr;
3085  struct IPv4TcpAddress t4;
3086  struct IPv6TcpAddress t6;
3087  const struct sockaddr_in *s4;
3088  const struct sockaddr_in6 *s6;
3089 
3090  if (0 == memcmp (&wm->clientIdentity,
3091  plugin->env->my_identity,
3092  sizeof(struct GNUNET_PeerIdentity)))
3093  {
3094  /* refuse connections from ourselves */
3095  if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
3096  {
3098  "Received WELCOME message from my own identity `%s' on address `%s'\n",
3099  GNUNET_i2s (&wm->clientIdentity),
3100  GNUNET_a2s (vaddr, alen));
3101  GNUNET_free (vaddr);
3102  }
3104  return;
3105  }
3106 
3107  if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
3108  {
3110  "Received WELCOME message from `%s' on address `%s'\n",
3111  GNUNET_i2s (&wm->clientIdentity),
3112  GNUNET_a2s (vaddr, alen));
3113  GNUNET_free (vaddr);
3114  }
3115  GNUNET_STATISTICS_update (plugin->env->stats,
3116  gettext_noop ("# TCP WELCOME messages received"),
3117  1,
3118  GNUNET_NO);
3119  session = lookup_session_by_client (plugin, client);
3120  if (NULL != session)
3121  {
3122  if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
3123  {
3125  "Found existing session %p for peer `%s'\n",
3126  session,
3127  GNUNET_a2s (vaddr, alen));
3128  GNUNET_free (vaddr);
3129  }
3130  }
3131  else
3132  {
3133  if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
3134  {
3135  if (alen == sizeof(struct sockaddr_in))
3136  {
3137  s4 = vaddr;
3138  memset (&t4, '\0', sizeof(t4));
3139  t4.options = htonl (TCP_OPTIONS_NONE);
3140  t4.t4_port = s4->sin_port;
3141  t4.ipv4_addr = s4->sin_addr.s_addr;
3142  address =
3144  PLUGIN_NAME,
3145  &t4,
3146  sizeof(t4),
3148  }
3149  else if (alen == sizeof(struct sockaddr_in6))
3150  {
3151  s6 = vaddr;
3152  memset (&t6, '\0', sizeof(t6));
3153  t6.options = htonl (TCP_OPTIONS_NONE);
3154  t6.t6_port = s6->sin6_port;
3155  GNUNET_memcpy (&t6.ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
3156  address =
3158  PLUGIN_NAME,
3159  &t6,
3160  sizeof(t6),
3162  }
3163  else
3164  {
3165  GNUNET_break (0);
3166  GNUNET_free (vaddr);
3168  return;
3169  }
3170  session = create_session (plugin,
3171  address,
3172  plugin->env->get_address_type (plugin->env->cls,
3173  vaddr,
3174  alen),
3175  client,
3176  GNUNET_NO);
3180  "Creating new%s session %p for peer `%s' client %p\n",
3183  ? " inbound"
3184  : "",
3185  session,
3187  session->address->address,
3188  session->address->address_length),
3189  client);
3190  GNUNET_free (vaddr);
3192  plugin->sessionmap,
3193  &session->target,
3194  session,
3196  /* Notify transport and ATS about new session */
3197  plugin->env->session_start (plugin->env->cls,
3198  session->address,
3199  session,
3200  session->scope);
3201  }
3202  else
3203  {
3205  "Did not obtain TCP socket address for incoming connection\n");
3206  GNUNET_break (0);
3208  return;
3209  }
3210  }
3211 
3212  if (GNUNET_YES != session->expecting_welcome)
3213  {
3214  GNUNET_break_op (0);
3216  return;
3217  }
3219  session->expecting_welcome = GNUNET_NO;
3220 
3221  process_pending_messages (session);
3225 }
static struct GNUNET_ATS_Session * lookup_session_by_client(struct Plugin *plugin, struct GNUNET_SERVER_Client *client)
Find the session handle for the given client.
struct GNUNET_PeerIdentity clientIdentity
Identity of the node connecting (TCP client)

References address, GNUNET_HELLO_Address::address, GNUNET_ATS_Session::address, GNUNET_HELLO_Address::address_length, WelcomeMessage::clientIdentity, create_session(), GNUNET_ATS_Session::expecting_welcome, gettext_noop, GNUNET_a2s(), GNUNET_break, GNUNET_break_op, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE, GNUNET_CONTAINER_multipeermap_put(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_HELLO_address_allocate(), GNUNET_HELLO_address_check_option(), GNUNET_HELLO_address_free, GNUNET_HELLO_ADDRESS_INFO_INBOUND, GNUNET_i2s(), GNUNET_memcpy, GNUNET_NO, GNUNET_NT_UNSPECIFIED, GNUNET_OK, GNUNET_SERVER_client_get_address(), GNUNET_SERVER_client_set_timeout(), GNUNET_SERVER_receive_done(), GNUNET_STATISTICS_update(), GNUNET_SYSERR, GNUNET_TIME_absolute_get(), GNUNET_YES, GNUNET_ATS_Session::last_activity, LOG, lookup_session_by_client(), plugin, PLUGIN_NAME, process_pending_messages(), GNUNET_ATS_Session::scope, t4, t6, GNUNET_ATS_Session::target, TCP_OPTIONS_NONE, and tcp_plugin_address_to_string().

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_tcp_data()

static void handle_tcp_data ( void *  cls,
struct GNUNET_SERVER_Client client,
const struct GNUNET_MessageHeader message 
)
static

We've received data for this peer via TCP.

Unbox, compute latency and forward.

Parameters
clsclosure
clientidentification of the client
messagethe actual message

Definition at line 3237 of file plugin_transport_tcp.c.

3240 {
3241  struct Plugin *plugin = cls;
3242  struct GNUNET_ATS_Session *session;
3243  struct GNUNET_TIME_Relative delay;
3244  uint16_t type;
3245 
3246  type = ntohs (message->type);
3249  {
3250  /* We don't want to propagate WELCOME and NAT Probe messages up! */
3252  return;
3253  }
3254  session = lookup_session_by_client (plugin, client);
3255  if (NULL == session)
3256  {
3257  /* No inbound session found */
3258  void *vaddr = NULL;
3259  size_t alen;
3260 
3262  GNUNET_SERVER_client_get_address (client, &vaddr, &alen));
3264  "Received unexpected %u bytes of type %u from `%s'\n",
3265  (unsigned int) ntohs (message->size),
3266  (unsigned int) ntohs (message->type),
3267  GNUNET_a2s (vaddr, alen));
3268  GNUNET_break_op (0);
3270  GNUNET_free (vaddr);
3271  return;
3272  }
3273  if (GNUNET_YES == session->expecting_welcome)
3274  {
3275  /* Session is expecting WELCOME message */
3276  void *vaddr = NULL;
3277  size_t alen;
3278 
3279  GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
3281  "Received unexpected %u bytes of type %u from `%s'\n",
3282  (unsigned int) ntohs (message->size),
3283  (unsigned int) ntohs (message->type),
3284  GNUNET_a2s (vaddr, alen));
3285  GNUNET_break_op (0);
3287  GNUNET_free (vaddr);
3288  return;
3289  }
3290 
3292  {
3293  void *vaddr = NULL;
3294  size_t alen;
3295 
3296  GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
3298  "Passing %u bytes of type %u from `%s' at %s to transport service.\n",
3299  (unsigned int) ntohs (message->size),
3300  (unsigned int) ntohs (message->type),
3301  GNUNET_i2s (&session->target),
3302  GNUNET_a2s (vaddr, alen));
3303  GNUNET_free (vaddr);
3304  }
3305 
3306  GNUNET_STATISTICS_update (plugin->env->stats,
3307  gettext_noop ("# bytes received via TCP"),
3308  ntohs (message->size),
3309  GNUNET_NO);
3310 
3311  GNUNET_assert (
3313  &session->target,
3314  session));
3315  delay =
3316  plugin->env->receive (plugin->env->cls, session->address, session, message);
3317  reschedule_session_timeout (session);
3318  if (0 == delay.rel_value_us)
3319  {
3321  }
3322  else
3323  {
3325  "Throttling receiving from `%s' for %s\n",
3326  GNUNET_i2s (&session->target),
3329  GNUNET_assert (NULL == session->receive_delay_task);
3330  session->receive_delay_task =
3332  }
3333 }
#define GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE
TCP NAT probe message, send from NAT'd peer to other peer to establish bi-directional communication.
#define GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME
Welcome message between TCP transports.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
uint64_t rel_value_us
The actual value.
void GNUNET_SERVER_disable_receive_done_warning(struct GNUNET_SERVER_Client *client)
Disable the warning the server issues if a message is not acknowledged in a timely fashion.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model

References GNUNET_ATS_Session::address, delay, delayed_done(), GNUNET_ATS_Session::expecting_welcome, gettext_noop, GNUNET_a2s(), GNUNET_assert, GNUNET_break_op, GNUNET_CONTAINER_multipeermap_contains_value(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_i2s(), GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME, GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_add_delayed(), GNUNET_SERVER_client_get_address(), GNUNET_SERVER_disable_receive_done_warning(), GNUNET_SERVER_receive_done(), GNUNET_STATISTICS_update(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_SYSERR, GNUNET_TIME_absolute_get(), GNUNET_YES, GNUNET_ATS_Session::last_activity, LOG, lookup_session_by_client(), plugin, GNUNET_ATS_Session::receive_delay_task, GNUNET_TIME_Relative::rel_value_us, reschedule_session_timeout(), GNUNET_MessageHeader::size, GNUNET_ATS_Session::target, GNUNET_MessageHeader::type, and type.

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ connect_notify()

static void connect_notify ( void *  cls,
struct GNUNET_SERVER_Client client 
)
static

Function called whenever a peer is connected on the "SERVER" level.

Increments number of active connections and suspends server if we have reached the limit.

Parameters
clsclosure
clientidentification of the client

Definition at line 3345 of file plugin_transport_tcp.c.

3346 {
3347  struct Plugin *plugin = cls;
3348 
3349  if (NULL == client)
3350  return;
3351  plugin->cur_connections++;
3352  GNUNET_STATISTICS_set (plugin->env->stats,
3353  gettext_noop ("# TCP server connections active"),
3354  plugin->cur_connections,
3355  GNUNET_NO);
3356  GNUNET_STATISTICS_update (plugin->env->stats,
3357  gettext_noop ("# TCP server connect events"),
3358  1,
3359  GNUNET_NO);
3360  if (plugin->cur_connections != plugin->max_connections)
3361  return;
3363  _ ("TCP connection limit reached, suspending server\n"));
3364  GNUNET_STATISTICS_update (plugin->env->stats,
3365  gettext_noop ("# TCP service suspended"),
3366  1,
3367  GNUNET_NO);
3369  plugin->server); /* Maximum number of connections rechead */
3370 }
void GNUNET_STATISTICS_set(struct GNUNET_STATISTICS_Handle *handle, const char *name, uint64_t value, int make_persistent)
Set statistic value for the peer.
void GNUNET_SERVER_suspend(struct GNUNET_SERVER_Handle *server)
Suspend accepting connections from the listen socket temporarily.

References _, gettext_noop, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_NO, GNUNET_SERVER_suspend(), GNUNET_STATISTICS_set(), GNUNET_STATISTICS_update(), and plugin.

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ disconnect_notify()

static void disconnect_notify ( void *  cls,
struct GNUNET_SERVER_Client client 
)
static

Function called whenever a peer is disconnected on the "SERVER" level.

Cleans up the connection, decrements number of active connections and if applicable resumes listening.

Parameters
clsclosure
clientidentification of the client

Definition at line 3382 of file plugin_transport_tcp.c.

3383 {
3384  struct Plugin *plugin = cls;
3385  struct GNUNET_ATS_Session *session;
3386 
3387  if (NULL == client)
3388  return;
3389  GNUNET_assert (plugin->cur_connections >= 1);
3390  plugin->cur_connections--;
3391  session = lookup_session_by_client (plugin, client);
3392  if (NULL == session)
3393  return; /* unknown, nothing to do */
3395  "Destroying session of `%s' with %s due to network-level disconnect.\n",
3396  GNUNET_i2s (&session->target),
3398  session->address->address,
3399  session->address->address_length));
3400 
3401  if (plugin->cur_connections == plugin->max_connections)
3402  {
3403  GNUNET_STATISTICS_update (session->plugin->env->stats,
3404  gettext_noop ("# TCP service resumed"),
3405  1,
3406  GNUNET_NO);
3407  GNUNET_SERVER_resume (plugin->server); /* Resume server */
3408  }
3409  GNUNET_STATISTICS_set (plugin->env->stats,
3410  gettext_noop ("# TCP server connections active"),
3411  plugin->cur_connections,
3412  GNUNET_NO);
3413  GNUNET_STATISTICS_update (session->plugin->env->stats,
3414  gettext_noop (
3415  "# network-level TCP disconnect events"),
3416  1,
3417  GNUNET_NO);
3419 }
void GNUNET_SERVER_resume(struct GNUNET_SERVER_Handle *server)
Resume accepting connections from the listen socket.

References GNUNET_HELLO_Address::address, GNUNET_ATS_Session::address, GNUNET_HELLO_Address::address_length, GNUNET_ATS_Session::client, Plugin::env, gettext_noop, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_i2s(), GNUNET_NO, GNUNET_SERVER_resume(), GNUNET_STATISTICS_set(), GNUNET_STATISTICS_update(), LOG, lookup_session_by_client(), plugin, GNUNET_ATS_Session::plugin, GNUNET_ATS_Session::target, tcp_plugin_address_to_string(), and tcp_plugin_disconnect_session().

Referenced by libgnunet_plugin_transport_tcp_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ notify_send_probe()

static size_t notify_send_probe ( void *  cls,
size_t  size,
void *  buf 
)
static

We can now send a probe message, copy into buffer to really send.

Parameters
clsclosure, a struct TCPProbeContext
sizemax size to copy
bufbuffer to copy message to
Returns
number of bytes copied into buf

Definition at line 3431 of file plugin_transport_tcp.c.

3432 {
3433  struct TCPProbeContext *tcp_probe_ctx = cls;
3434  struct Plugin *plugin = tcp_probe_ctx->plugin;
3435  size_t ret;
3436 
3437  tcp_probe_ctx->transmit_handle = NULL;
3438  GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3439  plugin->probe_tail,
3440  tcp_probe_ctx);
3441  if (NULL == buf)
3442  {
3443  GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
3444  GNUNET_free (tcp_probe_ctx);
3445  return 0;
3446  }
3447