GNUnet 0.21.1
gnunet-communicator-tcp.c File Reference

Transport plugin using TCP. More...

#include "gnunet_common.h"
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_core_service.h"
#include "gnunet_peerstore_service.h"
#include "gnunet_protocols.h"
#include "gnunet_signatures.h"
#include "gnunet_constants.h"
#include "gnunet_nat_service.h"
#include "gnunet_statistics_service.h"
#include "gnunet_transport_communication_service.h"
#include "gnunet_resolver_service.h"
Include dependency graph for gnunet-communicator-tcp.c:

Go to the source code of this file.

Data Structures

struct  TcpHandshakeSignature
 Signature we use to verify that the ephemeral key was really chosen by the specified sender. More...
 
struct  TcpHandshakeAckSignature
 Signature we use to verify that the ack from the receiver of the ephemeral key was really send by the specified sender. More...
 
struct  TCPConfirmation
 Encrypted continuation of TCP initial handshake. More...
 
struct  TCPConfirmationAck
 Ack for the encrypted continuation of TCP initial handshake. More...
 
struct  TCPBox
 TCP message box. More...
 
struct  TCPRekey
 TCP rekey message box. More...
 
struct  TcpRekeySignature
 Signature we use to verify that the ephemeral key was really chosen by the specified sender. More...
 
struct  TCPFinish
 TCP finish. More...
 
struct  TCPNATProbeMessage
 Basically a WELCOME message, but with the purpose of giving the waiting peer a client handle to use. More...
 
struct  PendingReversal
 Struct for pending nat reversals. More...
 
struct  ListenTask
 Struct to use as closure. More...
 
struct  Queue
 Handle for a queue. More...
 
struct  ProtoQueue
 Handle for an incoming connection where we do not yet have enough information to setup a full queue. More...
 
struct  PortOnlyIpv4Ipv6
 In case of port only configuration we like to bind to ipv4 and ipv6 addresses. More...
 
struct  Addresses
 DLL to store the addresses we like to register at NAT service. More...
 

Macros

#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 ADDRESS_VALIDITY_PERIOD    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
 How long do we believe our addresses to remain up (before the other peer should revalidate). More...
 
#define DEFAULT_MAX_QUEUE_LENGTH   8
 How many messages do we keep at most in the queue to the transport service before we start to drop (default, can be changed via the configuration file). More...
 
#define BUF_SIZE   (2 * 64 * 1024 + sizeof(struct TCPBox))
 Size of our IO buffers for ciphertext data. More...
 
#define DEFAULT_REKEY_INTERVAL   GNUNET_TIME_UNIT_DAYS
 How often do we rekey based on time (at least) More...
 
#define PROTO_QUEUE_TIMEOUT   GNUNET_TIME_UNIT_MINUTES
 How long do we wait until we must have received the initial KX? More...
 
#define REKEY_MAX_BYTES   (1024LLU * 1024 * 400)
 How often do we rekey based on number of bytes transmitted? (additionally randomized). More...
 
#define INITIAL_KX_SIZE
 Size of the initial key exchange message sent first in both directions. More...
 
#define INITIAL_CORE_KX_SIZE
 Size of the initial core key exchange messages. More...
 
#define COMMUNICATOR_ADDRESS_PREFIX   "tcp"
 Address prefix used by the communicator. More...
 
#define COMMUNICATOR_CONFIG_SECTION   "communicator-tcp"
 Configuration section used by the communicator. More...
 

Functions

static void listen_cb (void *cls)
 We have been notified that our listen socket has something to read. More...
 
static void queue_destroy (struct Queue *queue)
 Functions with this signature are called whenever we need to close a queue due to a disconnect or failure to establish a connection. More...
 
static void calculate_hmac (struct GNUNET_HashCode *hmac_secret, const void *buf, size_t buf_size, struct GNUNET_ShortHashCode *smac)
 Compute mac over buf, and ratched the hmac_secret. More...
 
static void queue_finish (struct Queue *queue)
 Append a 'finish' message to the outgoing transmission. More...
 
static void queue_read (void *cls)
 Queue read task. More...
 
static void core_read_finished_cb (void *cls, int success)
 Core tells us it is done processing a message that transport received on a queue with status success. More...
 
static void pass_plaintext_to_core (struct Queue *queue, const void *plaintext, size_t plaintext_len)
 We received plaintext_len bytes of plaintext on queue. More...
 
static void setup_cipher (const struct GNUNET_HashCode *dh, const struct GNUNET_PeerIdentity *pid, gcry_cipher_hd_t *cipher, struct GNUNET_HashCode *hmac_key)
 Setup cipher based on shared secret dh and decrypting peer pid. More...
 
static void rekey_monotime_store_cb (void *cls, int success)
 Callback called when peerstore store operation for rekey monotime value is finished. More...
 
static void rekey_monotime_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
 Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY where found. More...
 
static void setup_in_cipher (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral, struct Queue *queue)
 Setup cipher of queue for decryption. More...
 
static void do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
 Handle rekey message on queue. More...
 
static void handshake_ack_monotime_store_cb (void *cls, int success)
 Callback called when peerstore store operation for handshake ack monotime value is finished. More...
 
static void handshake_ack_monotime_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
 Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE_ACK where found. More...
 
static void send_challenge (struct GNUNET_CRYPTO_ChallengeNonceP challenge, struct Queue *queue)
 Sending challenge with TcpConfirmationAck back to sender of ephemeral key. More...
 
static void setup_out_cipher (struct Queue *queue, struct GNUNET_HashCode *dh)
 Setup cipher for outgoing data stream based on target and our ephemeral private key. More...
 
static void inject_rekey (struct Queue *queue)
 Inject a struct TCPRekey message into the queue's plaintext buffer. More...
 
static int pending_reversals_delete_it (void *cls, const struct GNUNET_HashCode *key, void *value)
 
static void check_and_remove_pending_reversal (struct sockaddr *in, sa_family_t sa_family, struct GNUNET_PeerIdentity *sender)
 
static void free_proto_queue (struct ProtoQueue *pq)
 Closes socket and frees memory associated with pq. More...
 
static void proto_queue_write (void *cls)
 We have been notified that our socket is ready to write. More...
 
static void queue_write (void *cls)
 We have been notified that our socket is ready to write. More...
 
static size_t try_handle_plaintext (struct Queue *queue)
 Test if we have received a full message in plaintext. More...
 
static struct sockaddr * tcp_address_to_sockaddr_numeric_v6 (socklen_t *sock_len, struct sockaddr_in6 v6, unsigned int port)
 Convert a struct sockaddr_in6 to astruct sockaddr *`. More...
 
static struct sockaddr * tcp_address_to_sockaddr_numeric_v4 (socklen_t *sock_len, struct sockaddr_in v4, unsigned int port)
 Convert a struct sockaddr_in4 to astruct sockaddr *`. More...
 
static struct PortOnlyIpv4Ipv6tcp_address_to_sockaddr_port_only (const char *bindto, unsigned int *port)
 Convert TCP bind specification to a struct PortOnlyIpv4Ipv6 * More...
 
static char * extract_address (const char *bindto)
 This Method extracts the address part of the BINDTO string. More...
 
static unsigned int extract_port (const char *addr_and_port)
 This Method extracts the port part of the BINDTO string. More...
 
static struct sockaddr * tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
 Convert TCP bind specification to a struct sockaddr * More...
 
static void mq_send (struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *msg, void *impl_state)
 Signature of functions implementing the sending functionality of a message queue. More...
 
static void mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
 Signature of functions implementing the destruction of a message queue. More...
 
static void mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
 Implementation function that cancels the currently sent message. More...
 
static void mq_error (void *cls, enum GNUNET_MQ_Error error)
 Generic error handler, called with the appropriate error code and the same closure specified at the creation of the message queue. More...
 
static void boot_queue (struct Queue *queue)
 Add the given queue to our internal data structure. More...
 
static void transmit_kx (struct Queue *queue, const struct GNUNET_CRYPTO_EcdhePublicKey *epub)
 Generate and transmit our ephemeral key and the signature for the initial KX with the other peer. More...
 
static void start_initial_kx_out (struct Queue *queue)
 Initialize our key material for outgoing transmissions and inform the other peer about it. More...
 
static void handshake_monotime_store_cb (void *cls, int success)
 Callback called when peerstore store operation for handshake monotime is finished. More...
 
static void handshake_monotime_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
 Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE where found. More...
 
static int decrypt_and_check_tc (struct Queue *queue, struct TCPConfirmation *tc, char *ibuf)
 We have received the first bytes from the other side on a queue. More...
 
static void queue_read_kx (void *cls)
 Read from the socket of the queue until we have enough data to initialize the decryption logic and can switch to regular reading. More...
 
static void proto_read_kx (void *cls)
 Read from the socket of the proto queue until we have enough data to upgrade to full queue. More...
 
static struct ProtoQueuecreate_proto_queue (struct GNUNET_NETWORK_Handle *sock, struct sockaddr *in, socklen_t addrlen)
 
static void try_connection_reversal (void *cls, const struct sockaddr *addr, socklen_t addrlen)
 
static void pending_reversal_timeout (void *cls)
 
static int mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
 Function called by the transport service to initialize a message queue given address information about another peer. More...
 
static int get_lt_delete_it (void *cls, const struct GNUNET_HashCode *key, void *value)
 Iterator over all ListenTasks to clean up. More...
 
static int get_queue_delete_it (void *cls, const struct GNUNET_HashCode *target, void *value)
 Iterator over all message queues to clean up. More...
 
static void do_shutdown (void *cls)
 Shutdown the UNIX communicator. More...
 
static void enc_notify_cb (void *cls, const struct GNUNET_PeerIdentity *sender, const struct GNUNET_MessageHeader *msg)
 Function called when the transport service has received an acknowledgement for this communicator (!) via a different return path. More...
 
static void nat_address_cb (void *cls, void **app_ctx, int add_remove, enum GNUNET_NAT_AddressClass ac, const struct sockaddr *addr, socklen_t addrlen)
 Signature of the callback passed to GNUNET_NAT_register() for a function to call whenever our set of 'valid' addresses changes. More...
 
static void add_addr (struct sockaddr *in, socklen_t in_len)
 This method adds addresses to the DLL, that are later register at the NAT service. More...
 
static int init_socket (struct sockaddr *addr, socklen_t in_len)
 This method launch network interactions for each address we like to bind to. More...
 
static void nat_register ()
 This method reads from the DLL addrs_head to register them at the NAT service. More...
 
static void init_socket_resolv (void *cls, const struct sockaddr *addr, socklen_t in_len)
 This method is the callback called by the resolver API, and wraps method init_socket. More...
 
static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
 Setup communicator and launch network interactions. More...
 
int main (int argc, char *const *argv)
 The main function for the UNIX communicator. More...
 

Variables

static unsigned long long max_queue_length
 Maximum queue length before we stop reading towards the transport service. More...
 
static struct GNUNET_STATISTICS_Handlestats
 For logging statistics. More...
 
static struct GNUNET_TRANSPORT_CommunicatorHandlech
 Our environment. More...
 
static struct GNUNET_CONTAINER_MultiHashMapqueue_map
 Queues (map from peer identity to struct Queue) More...
 
static struct GNUNET_CONTAINER_MultiHashMaplt_map
 ListenTasks (map from socket to struct ListenTask) More...
 
static struct GNUNET_PeerIdentity my_identity
 Our public key. More...
 
static unsigned long long rekey_max_bytes
 The rekey byte maximum. More...
 
static struct GNUNET_TIME_Relative rekey_interval
 The rekey interval. More...
 
static struct GNUNET_CRYPTO_EddsaPrivateKeymy_private_key
 Our private key. More...
 
static const struct GNUNET_CONFIGURATION_Handlecfg
 Our configuration. More...
 
static struct GNUNET_NT_InterfaceScanneris
 Network scanner to determine network types. More...
 
static struct GNUNET_NAT_Handlenat
 Connection to NAT service. More...
 
static struct ProtoQueueproto_head
 Protoqueues DLL head. More...
 
static struct ProtoQueueproto_tail
 Protoqueues DLL tail. More...
 
struct GNUNET_RESOLVER_RequestHandleresolve_request_handle
 Handle for DNS lookup of bindto address. More...
 
static struct Addressesaddrs_head
 Head of DLL with addresses we like to register at NAT servcie. More...
 
static struct Addressesaddrs_tail
 Head of DLL with addresses we like to register at NAT servcie. More...
 
static int addrs_lens
 Number of addresses in the DLL for register at NAT service. More...
 
static struct GNUNET_PEERSTORE_Handlepeerstore
 Database for peer's HELLOs. More...
 
static int shutdown_running = GNUNET_NO
 A flag indicating we are already doing a shutdown. More...
 
static int disable_v6
 IPv6 disabled. More...
 
static unsigned int bind_port
 The port the communicator should be assigned to. More...
 
static struct GNUNET_CONTAINER_MultiHashMappending_reversals
 Map of pending reversals. More...
 

Detailed Description

Transport plugin using TCP.

Author
Christian Grothoff

TODO:

  • support NAT connection reversal method (#5529)
  • support other TCP-specific NAT traversal methods (#5531)

Definition in file gnunet-communicator-tcp.c.

Macro Definition Documentation

◆ NAT_TIMEOUT

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

Definition at line 48 of file gnunet-communicator-tcp.c.

◆ ADDRESS_VALIDITY_PERIOD

#define ADDRESS_VALIDITY_PERIOD    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)

How long do we believe our addresses to remain up (before the other peer should revalidate).

Definition at line 54 of file gnunet-communicator-tcp.c.

◆ DEFAULT_MAX_QUEUE_LENGTH

#define DEFAULT_MAX_QUEUE_LENGTH   8

How many messages do we keep at most in the queue to the transport service before we start to drop (default, can be changed via the configuration file).

Should be below the level of the communicator API, as otherwise we may read messages just to have them dropped by the communicator API.

Definition at line 65 of file gnunet-communicator-tcp.c.

◆ BUF_SIZE

#define BUF_SIZE   (2 * 64 * 1024 + sizeof(struct TCPBox))

Size of our IO buffers for ciphertext data.

Must be at least UINT_MAX + sizeof (struct TCPBox).

Definition at line 71 of file gnunet-communicator-tcp.c.

◆ DEFAULT_REKEY_INTERVAL

#define DEFAULT_REKEY_INTERVAL   GNUNET_TIME_UNIT_DAYS

How often do we rekey based on time (at least)

Definition at line 76 of file gnunet-communicator-tcp.c.

◆ PROTO_QUEUE_TIMEOUT

#define PROTO_QUEUE_TIMEOUT   GNUNET_TIME_UNIT_MINUTES

How long do we wait until we must have received the initial KX?

Definition at line 81 of file gnunet-communicator-tcp.c.

◆ REKEY_MAX_BYTES

#define REKEY_MAX_BYTES   (1024LLU * 1024 * 400)

How often do we rekey based on number of bytes transmitted? (additionally randomized).

Currently 400 MB

Definition at line 87 of file gnunet-communicator-tcp.c.

◆ INITIAL_KX_SIZE

#define INITIAL_KX_SIZE
Value:
(sizeof(struct GNUNET_CRYPTO_EcdhePublicKey) \
+ sizeof(struct TCPConfirmation))
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
Encrypted continuation of TCP initial handshake.

Size of the initial key exchange message sent first in both directions.

Definition at line 93 of file gnunet-communicator-tcp.c.

◆ INITIAL_CORE_KX_SIZE

#define INITIAL_CORE_KX_SIZE
Value:
(sizeof(struct EphemeralKeyMessage) \
+ sizeof(struct PingMessage) \
+ sizeof(struct PongMessage))
Message transmitted with the signed ephemeral key of a peer.
We're sending an (encrypted) PING to the other peer to check if it can decrypt.
Response to a PING.

Size of the initial core key exchange messages.

Definition at line 100 of file gnunet-communicator-tcp.c.

◆ COMMUNICATOR_ADDRESS_PREFIX

#define COMMUNICATOR_ADDRESS_PREFIX   "tcp"

Address prefix used by the communicator.

Definition at line 108 of file gnunet-communicator-tcp.c.

◆ COMMUNICATOR_CONFIG_SECTION

#define COMMUNICATOR_CONFIG_SECTION   "communicator-tcp"

Configuration section used by the communicator.

Definition at line 113 of file gnunet-communicator-tcp.c.

Function Documentation

◆ listen_cb()

static void listen_cb ( void *  cls)
static

We have been notified that our listen socket has something to read.

Do the read and reschedule this function to be called again once more is available.

Parameters
clsNULL

Do the read and reschedule this function to be called again once more is available.

Parameters
clsListenTask with listening socket and task

Definition at line 3181 of file gnunet-communicator-tcp.c.

3182{
3183 struct sockaddr_storage in;
3184 socklen_t addrlen;
3185 struct GNUNET_NETWORK_Handle *sock;
3186 struct ListenTask *lt;
3187 struct sockaddr *in_addr;
3188
3190 "listen_cb\n");
3191
3192 lt = cls;
3193
3194 lt->listen_task = NULL;
3195 GNUNET_assert (NULL != lt->listen_sock);
3196 addrlen = sizeof(in);
3197 memset (&in, 0, sizeof(in));
3199 (struct sockaddr*) &in,
3200 &addrlen);
3201 if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno)))
3202 return; /* system limit reached, wait until connection goes down */
3204 lt->listen_sock,
3205 &listen_cb,
3206 lt);
3207 if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno)))
3208 return;
3209 if (NULL == sock)
3210 {
3212 return;
3213 }
3214 in_addr = GNUNET_memdup (&in, addrlen);
3215 create_proto_queue (sock, in_addr, addrlen);
3216}
static struct ProtoQueue * create_proto_queue(struct GNUNET_NETWORK_Handle *sock, struct sockaddr *in, socklen_t addrlen)
static void listen_cb(void *cls)
We have been notified that our listen socket has something to read.
#define GNUNET_log(kind,...)
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#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...
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc, struct sockaddr *address, socklen_t *address_len)
Accept a new connection on a socket.
Definition: network.c:392
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1512
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
handle to a socket
Definition: network.c:53
Struct to use as closure.
struct GNUNET_NETWORK_Handle * listen_sock
Listen socket.
struct GNUNET_SCHEDULER_Task * listen_task
ID of listen task.

References create_proto_queue(), GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_log_strerror, GNUNET_memdup, GNUNET_NETWORK_socket_accept(), GNUNET_SCHEDULER_add_read_net(), GNUNET_TIME_UNIT_FOREVER_REL, listen_cb(), ListenTask::listen_sock, and ListenTask::listen_task.

Referenced by GNUNET_SET_listen(), GNUNET_SETI_listen(), GNUNET_SETU_listen(), init_socket(), listen_cb(), and queue_destroy().

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

◆ queue_destroy()

static void queue_destroy ( struct Queue queue)
static

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

Parameters
queuequeue to close down

Definition at line 944 of file gnunet-communicator-tcp.c.

945{
946 struct ListenTask *lt = NULL;
947 struct GNUNET_HashCode h_sock;
948 int sockfd;
949
950 if (NULL != queue->listen_sock)
951 {
952 sockfd = GNUNET_NETWORK_get_fd (queue->listen_sock);
953 GNUNET_CRYPTO_hash (&sockfd,
954 sizeof(int),
955 &h_sock);
956
958 }
959
961 "Disconnecting queue for peer `%s'\n",
962 GNUNET_i2s (&queue->target));
963 if (NULL != queue->rekey_monotime_sc)
964 {
965 GNUNET_PEERSTORE_store_cancel (queue->rekey_monotime_sc);
966 queue->rekey_monotime_sc = NULL;
967 }
968 if (NULL != queue->handshake_monotime_sc)
969 {
970 GNUNET_PEERSTORE_store_cancel (queue->handshake_monotime_sc);
971 queue->handshake_monotime_sc = NULL;
972 }
973 if (NULL != queue->handshake_ack_monotime_sc)
974 {
975 GNUNET_PEERSTORE_store_cancel (queue->handshake_ack_monotime_sc);
976 queue->handshake_ack_monotime_sc = NULL;
977 }
978 if (NULL != queue->rekey_monotime_get)
979 {
980 GNUNET_PEERSTORE_iteration_stop (queue->rekey_monotime_get);
981 queue->rekey_monotime_get = NULL;
982 }
983 if (NULL != queue->handshake_monotime_get)
984 {
985 GNUNET_PEERSTORE_iteration_stop (queue->handshake_monotime_get);
986 queue->handshake_monotime_get = NULL;
987 }
988 if (NULL != queue->handshake_ack_monotime_get)
989 {
990 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
991 queue->handshake_ack_monotime_get = NULL;
992 }
993 if (NULL != queue->qh)
994 {
996 queue->qh = NULL;
997 }
999 GNUNET_YES ==
1002 "# queues active",
1004 GNUNET_NO);
1005 if (NULL != queue->read_task)
1006 {
1007 GNUNET_SCHEDULER_cancel (queue->read_task);
1008 queue->read_task = NULL;
1009 }
1010 if (NULL != queue->write_task)
1011 {
1012 GNUNET_SCHEDULER_cancel (queue->write_task);
1013 queue->write_task = NULL;
1014 }
1016 {
1018 "closing socket failed\n");
1019 }
1020 gcry_cipher_close (queue->in_cipher);
1021 gcry_cipher_close (queue->out_cipher);
1022 GNUNET_free (queue->address);
1023 if (0 != queue->backpressure)
1024 queue->destroyed = GNUNET_YES;
1025 else
1027
1028 if (NULL == lt)
1029 return;
1030
1031 if ((! shutdown_running) && (NULL == lt->listen_task))
1032 {
1034 "add read net listen\n");
1037 lt->listen_sock,
1038 &listen_cb,
1039 lt);
1040 }
1041 else
1042 GNUNET_free (lt);
1043}
static struct GNUNET_STATISTICS_Handle * stats
For logging statistics.
static struct GNUNET_CONTAINER_MultiHashMap * queue_map
Queues (map from peer identity to struct Queue)
static int shutdown_running
A flag indicating we are already doing a shutdown.
static struct GNUNET_CONTAINER_MultiHashMap * lt_map
ListenTasks (map from socket to struct ListenTask)
static void queue(const char *hostname)
Add hostname to the list of requests to be made.
void GNUNET_TRANSPORT_communicator_mq_del(struct GNUNET_TRANSPORT_QueueHandle *qh)
Notify transport service that an MQ became unavailable due to a disconnect or timeout.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
void * GNUNET_CONTAINER_multihashmap_get(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Given a key find a value in the map matching the key.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
Remove the given key-value pair from the map.
unsigned int GNUNET_CONTAINER_multihashmap_size(const struct GNUNET_CONTAINER_MultiHashMap *map)
Get the number of key-value pairs in the map.
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:508
int GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
Return file descriptor for this network handle.
Definition: network.c:1000
void GNUNET_PEERSTORE_store_cancel(struct GNUNET_PEERSTORE_StoreContext *sc)
Cancel a store request.
void GNUNET_PEERSTORE_iteration_stop(struct GNUNET_PEERSTORE_IterateContext *ic)
Cancel an iteration.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:981
void GNUNET_STATISTICS_set(struct GNUNET_STATISTICS_Handle *handle, const char *name, uint64_t value, int make_persistent)
Set statistic value for the peer.
A 512-bit hashcode.

References GNUNET_assert, GNUNET_CONTAINER_multihashmap_get(), GNUNET_CONTAINER_multihashmap_remove(), GNUNET_CONTAINER_multihashmap_size(), GNUNET_CRYPTO_hash(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_i2s(), GNUNET_log, GNUNET_NETWORK_get_fd(), GNUNET_NETWORK_socket_close(), GNUNET_NO, GNUNET_PEERSTORE_iteration_stop(), GNUNET_PEERSTORE_store_cancel(), GNUNET_SCHEDULER_add_read_net(), GNUNET_SCHEDULER_cancel(), GNUNET_STATISTICS_set(), GNUNET_SYSERR, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TRANSPORT_communicator_mq_del(), GNUNET_YES, listen_cb(), ListenTask::listen_sock, ListenTask::listen_task, lt_map, queue(), queue_map, shutdown_running, and stats.

Referenced by get_queue_delete_it(), queue_read(), queue_read_kx(), queue_write(), and try_handle_plaintext().

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

◆ calculate_hmac()

static void calculate_hmac ( struct GNUNET_HashCode hmac_secret,
const void *  buf,
size_t  buf_size,
struct GNUNET_ShortHashCode smac 
)
static

Compute mac over buf, and ratched the hmac_secret.

Parameters
[in,out]hmac_secretsecret for HMAC calculation
bufbuffer to MAC
buf_sizenumber of bytes in buf
[out]smacwhere to write the HMAC

Definition at line 1055 of file gnunet-communicator-tcp.c.

1059{
1060 struct GNUNET_HashCode mac;
1061
1062 GNUNET_CRYPTO_hmac_raw (hmac_secret,
1063 sizeof(struct GNUNET_HashCode),
1064 buf,
1065 buf_size,
1066 &mac);
1067 /* truncate to `struct GNUNET_ShortHashCode` */
1068 memcpy (smac, &mac, sizeof(struct GNUNET_ShortHashCode));
1069 /* ratchet hmac key */
1070 GNUNET_CRYPTO_hash (hmac_secret,
1071 sizeof(struct GNUNET_HashCode),
1072 hmac_secret);
1073}
void GNUNET_CRYPTO_hmac_raw(const void *key, size_t key_len, const void *plaintext, size_t plaintext_len, struct GNUNET_HashCode *hmac)
Calculate HMAC of a message (RFC 2104) TODO: Shouldn't this be the standard hmac function and the abo...
Definition: crypto_hash.c:300
A 256-bit hashcode.

References GNUNET_CRYPTO_hash(), and GNUNET_CRYPTO_hmac_raw().

Referenced by inject_rekey(), mq_send(), queue_finish(), and try_handle_plaintext().

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

◆ queue_finish()

static void queue_finish ( struct Queue queue)
static

Append a 'finish' message to the outgoing transmission.

Once the finish has been transmitted, destroy the queue.

Parameters
queuequeue to shut down nicely

Definition at line 1083 of file gnunet-communicator-tcp.c.

1084{
1085 struct TCPFinish fin;
1086
1087 memset (&fin, 0, sizeof(fin));
1088 fin.header.size = htons (sizeof(fin));
1089 fin.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH);
1090 calculate_hmac (&queue->out_hmac, &fin, sizeof(fin), &fin.hmac);
1091 /* if there is any message left in pwrite_buf, we
1092 overwrite it (possibly dropping the last message
1093 from CORE hard here) */
1094 memcpy (queue->pwrite_buf, &fin, sizeof(fin));
1095 queue->pwrite_off = sizeof(fin);
1096 /* This flag will ensure that #queue_write() no longer
1097 notifies CORE about the possibility of sending
1098 more data, and that #queue_write() will call
1099 #queue_destroy() once the @c fin was fully written. */
1100 queue->finishing = GNUNET_YES;
1101}
static void calculate_hmac(struct GNUNET_HashCode *hmac_secret, const void *buf, size_t buf_size, struct GNUNET_ShortHashCode *smac)
Compute mac over buf, and ratched the hmac_secret.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH
TCP communicator end of stream.

References calculate_hmac(), GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH, GNUNET_YES, TCPFinish::header, TCPFinish::hmac, queue(), GNUNET_MessageHeader::size, and GNUNET_MessageHeader::type.

Referenced by do_rekey(), handshake_ack_monotime_cb(), handshake_monotime_cb(), mq_destroy(), mq_error(), rekey_monotime_cb(), and try_handle_plaintext().

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

◆ queue_read()

static void queue_read ( void *  cls)
static

Queue read task.

If we hit the timeout, disconnect it

Parameters
clsthe struct Queue * to disconnect

Definition at line 2129 of file gnunet-communicator-tcp.c.

2130{
2131 struct Queue *queue = cls;
2132 struct GNUNET_TIME_Relative left;
2133 ssize_t rcvd;
2134
2135 queue->read_task = NULL;
2136 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2137 &queue->cread_buf[queue->cread_off],
2138 BUF_SIZE - queue->cread_off);
2140 "Received %zd bytes from TCP queue\n", rcvd);
2141 if (-1 == rcvd)
2142 {
2143 if ((EAGAIN != errno) && (EINTR != errno))
2144 {
2147 return;
2148 }
2149 /* try again */
2150 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2151 if (0 != left.rel_value_us)
2152 {
2153 queue->read_task =
2155 return;
2156 }
2158 "Queue %p was idle for %s, disconnecting\n",
2159 queue,
2162 GNUNET_YES));
2164 return;
2165 }
2166 if (0 == rcvd)
2167 {
2168 /* Orderly shutdown of connection */
2170 "Socket for queue %p seems to have been closed\n", queue);
2172 return;
2173 }
2174 queue->timeout =
2176 queue->cread_off += rcvd;
2177 while ((queue->pread_off < sizeof(queue->pread_buf)) &&
2178 (queue->cread_off > 0))
2179 {
2180 size_t max = GNUNET_MIN (sizeof(queue->pread_buf) - queue->pread_off,
2181 queue->cread_off);
2182 size_t done;
2183 size_t total;
2184 size_t old_pread_off = queue->pread_off;
2185
2186 GNUNET_assert (0 ==
2187 gcry_cipher_decrypt (queue->in_cipher,
2188 &queue->pread_buf[queue->pread_off],
2189 max,
2190 queue->cread_buf,
2191 max));
2192 queue->pread_off += max;
2193 total = 0;
2194 while (0 != (done = try_handle_plaintext (queue)))
2195 {
2196 /* 'done' bytes of plaintext were used, shift buffer */
2197 GNUNET_assert (done <= queue->pread_off);
2198 /* NOTE: this memmove() could possibly sometimes be
2199 avoided if we pass 'total' into try_handle_plaintext()
2200 and use it at an offset into the buffer there! */
2201 memmove (queue->pread_buf,
2202 &queue->pread_buf[done],
2203 queue->pread_off - done);
2204 queue->pread_off -= done;
2205 total += done;
2206 /* The last plaintext was a rekey, abort for now */
2207 if (GNUNET_YES == queue->rekeyed)
2208 break;
2209 }
2210 /* when we encounter a rekey message, the decryption above uses the
2211 wrong key for everything after the rekey; in that case, we have
2212 to re-do the decryption at 'total' instead of at 'max'.
2213 However, we have to take into account that the plaintext buffer may have
2214 already contained data and not jumped too far ahead in the ciphertext.
2215 If there is no rekey and the last message is incomplete (max > total),
2216 it is safe to keep the decryption so we shift by 'max' */
2217 if (GNUNET_YES == queue->rekeyed)
2218 {
2219 max = total - old_pread_off;
2220 queue->rekeyed = GNUNET_NO;
2221 queue->pread_off = 0;
2222 }
2223 memmove (queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
2224 queue->cread_off -= max;
2225 }
2226 if (BUF_SIZE == queue->cread_off)
2227 return; /* buffer full, suspend reading */
2228 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2229 if (0 != left.rel_value_us)
2230 {
2231 if (max_queue_length > queue->backpressure)
2232 {
2233 /* continue reading */
2234 queue->read_task =
2236 }
2237 return;
2238 }
2240 "Queue %p was idle for %s, disconnecting\n",
2241 queue,
2244 GNUNET_YES));
2246}
static size_t try_handle_plaintext(struct Queue *queue)
Test if we have received a full message in plaintext.
static void queue_destroy(struct Queue *queue)
Functions with this signature are called whenever we need to close a queue due to a disconnect or fai...
#define BUF_SIZE
Size of our IO buffers for ciphertext data.
static unsigned long long max_queue_length
Maximum queue length before we stop reading towards the transport service.
static void queue_read(void *cls)
Queue read task.
#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?
#define GNUNET_MIN(a, b)
ssize_t GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length)
Read data from a connected socket (always non-blocking).
Definition: network.c:716
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:570
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
#define max(x, y)
Time for relative time used by GNUnet, in microseconds.
Handle for a queue.

References BUF_SIZE, GNUNET_assert, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_log_strerror, GNUNET_MIN, GNUNET_NETWORK_socket_recv(), GNUNET_NO, GNUNET_SCHEDULER_add_read_net(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_TIME_absolute_get_remaining(), GNUNET_TIME_relative_to_absolute(), GNUNET_YES, max, max_queue_length, queue(), queue_destroy(), queue_read(), GNUNET_TIME_Relative::rel_value_us, and try_handle_plaintext().

Referenced by core_read_finished_cb(), proto_read_kx(), queue_read(), and queue_read_kx().

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

◆ core_read_finished_cb()

static void core_read_finished_cb ( void *  cls,
int  success 
)
static

Core tells us it is done processing a message that transport received on a queue with status success.

Parameters
clsa struct Queue * where the message originally came from
successGNUNET_OK on success

Definition at line 1121 of file gnunet-communicator-tcp.c.

1122{
1123 struct Queue *queue = cls;
1124 if (GNUNET_OK != success)
1126 "# messages lost in communicator API towards CORE",
1127 1,
1128 GNUNET_NO);
1129 if (NULL == queue)
1130 return;
1131
1133 "backpressure %u\n",
1134 queue->backpressure);
1135
1136 queue->backpressure--;
1137 /* handle deferred queue destruction */
1138 if ((queue->destroyed) && (0 == queue->backpressure))
1139 {
1141 return;
1142 }
1143 else if (GNUNET_YES != queue->destroyed)
1144 {
1145 queue->timeout =
1147 );
1148 /* possibly unchoke reading, now that CORE made progress */
1149 if (NULL == queue->read_task)
1150 queue->read_task =
1152 queue->timeout),
1153 queue->sock,
1154 &queue_read,
1155 queue);
1156 }
1157}
@ GNUNET_OK
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.

References GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_add_read_net(), GNUNET_STATISTICS_update(), GNUNET_TIME_absolute_get_remaining(), GNUNET_TIME_relative_to_absolute(), GNUNET_YES, queue(), queue_read(), and stats.

Referenced by pass_plaintext_to_core().

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

◆ pass_plaintext_to_core()

static void pass_plaintext_to_core ( struct Queue queue,
const void *  plaintext,
size_t  plaintext_len 
)
static

We received plaintext_len bytes of plaintext on queue.

Pass it on to CORE. If transmission is actually happening, increase backpressure counter.

Parameters
queuethe queue that received the plaintext
plaintextthe plaintext that was received
plaintext_lennumber of bytes of plaintext received

Definition at line 1170 of file gnunet-communicator-tcp.c.

1173{
1174 const struct GNUNET_MessageHeader *hdr = plaintext;
1175 int ret;
1176
1178 "pass message from %s to core\n",
1179 GNUNET_i2s (&queue->target));
1180
1181 if (ntohs (hdr->size) != plaintext_len)
1182 {
1183 /* NOTE: If we ever allow multiple CORE messages in one
1184 BOX, this will have to change! */
1185 GNUNET_break (0);
1186 return;
1187 }
1189 &queue->target,
1190 hdr,
1193 queue);
1195 "passed to core\n");
1196 if (GNUNET_OK == ret)
1197 queue->backpressure++;
1198 GNUNET_break (GNUNET_NO != ret); /* backpressure not working!? */
1199 if (GNUNET_SYSERR == ret)
1201 "# bytes lost due to CORE not running",
1202 plaintext_len,
1203 GNUNET_NO);
1204}
static int ret
Final status code.
Definition: gnunet-arm.c:94
#define ADDRESS_VALIDITY_PERIOD
How long do we believe our addresses to remain up (before the other peer should revalidate).
static struct GNUNET_TRANSPORT_CommunicatorHandle * ch
Our environment.
static void core_read_finished_cb(void *cls, int success)
Core tells us it is done processing a message that transport received on a queue with status success.
int GNUNET_TRANSPORT_communicator_receive(struct GNUNET_TRANSPORT_CommunicatorHandle *handle, const struct GNUNET_PeerIdentity *sender, const struct GNUNET_MessageHeader *msg, struct GNUNET_TIME_Relative expected_addr_validity, GNUNET_TRANSPORT_MessageCompletedCallback cb, void *cb_cls)
Notify transport service that the communicator has received a message.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
Header for all communications.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.

References ADDRESS_VALIDITY_PERIOD, ch, core_read_finished_cb(), GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_i2s(), GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_STATISTICS_update(), GNUNET_SYSERR, GNUNET_TRANSPORT_communicator_receive(), queue(), ret, GNUNET_MessageHeader::size, and stats.

Referenced by try_handle_plaintext().

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

◆ setup_cipher()

static void setup_cipher ( const struct GNUNET_HashCode dh,
const struct GNUNET_PeerIdentity pid,
gcry_cipher_hd_t *  cipher,
struct GNUNET_HashCode hmac_key 
)
static

Setup cipher based on shared secret dh and decrypting peer pid.

Parameters
dhshared secret
piddecrypting peer's identity
[out]ciphercipher to initialize
[out]hmac_keyHMAC key to initialize

Definition at line 1217 of file gnunet-communicator-tcp.c.

1221{
1222 char key[256 / 8];
1223 char ctr[128 / 8];
1224
1225 GNUNET_assert (0 == gcry_cipher_open (cipher,
1226 GCRY_CIPHER_AES256 /* low level: go for speed */
1227 ,
1228 GCRY_CIPHER_MODE_CTR,
1229 0 /* flags */));
1231 sizeof(key),
1232 "TCP-key",
1233 strlen ("TCP-key"),
1234 dh,
1235 sizeof(*dh),
1236 pid,
1237 sizeof(*pid),
1238 NULL,
1239 0));
1240 GNUNET_assert (0 == gcry_cipher_setkey (*cipher, key, sizeof(key)));
1242 sizeof(ctr),
1243 "TCP-ctr",
1244 strlen ("TCP-ctr"),
1245 dh,
1246 sizeof(*dh),
1247 pid,
1248 sizeof(*pid),
1249 NULL,
1250 0));
1251 gcry_cipher_setctr (*cipher, ctr, sizeof(ctr));
1253 GNUNET_CRYPTO_kdf (hmac_key,
1254 sizeof(struct GNUNET_HashCode),
1255 "TCP-hmac",
1256 strlen ("TCP-hmac"),
1257 dh,
1258 sizeof(*dh),
1259 pid,
1260 sizeof(*pid),
1261 NULL,
1262 0));
1263}
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_kdf(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
Derive key.
Definition: crypto_kdf.c:70

References GNUNET_assert, GNUNET_CRYPTO_kdf(), GNUNET_YES, key, and pid.

Referenced by setup_in_cipher(), and setup_out_cipher().

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

◆ rekey_monotime_store_cb()

static void rekey_monotime_store_cb ( void *  cls,
int  success 
)
static

Callback called when peerstore store operation for rekey monotime value is finished.

Parameters
clsQueue context the store operation was executed.
successStore operation was successful (GNUNET_OK) or not.

Definition at line 1272 of file gnunet-communicator-tcp.c.

1273{
1274 struct Queue *queue = cls;
1275 if (GNUNET_OK != success)
1276 {
1278 "Failed to store rekey monotonic time in PEERSTORE!\n");
1279 }
1280 queue->rekey_monotime_sc = NULL;
1281 GNUNET_PEERSTORE_iteration_next (queue->rekey_monotime_get, 1);
1282}
void GNUNET_PEERSTORE_iteration_next(struct GNUNET_PEERSTORE_IterateContext *ic, uint64_t limit)
Continue an iteration.

References GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, GNUNET_PEERSTORE_iteration_next(), and queue().

Referenced by rekey_monotime_cb().

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

◆ rekey_monotime_cb()

static void rekey_monotime_cb ( void *  cls,
const struct GNUNET_PEERSTORE_Record record,
const char *  emsg 
)
static

Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY where found.

Parameters
clsQueue context the store operation was executed.
recordThe record found or NULL if there is no record left.
emsgMessage from peerstore.

Definition at line 1293 of file gnunet-communicator-tcp.c.

1296{
1297 struct Queue *queue = cls;
1298 struct GNUNET_TIME_AbsoluteNBO *mtbe;
1299 struct GNUNET_TIME_Absolute mt;
1300 const struct GNUNET_PeerIdentity *pid;
1301 struct GNUNET_TIME_AbsoluteNBO *rekey_monotonic_time;
1302
1303 (void) emsg;
1304
1305 rekey_monotonic_time = &queue->rekey_monotonic_time;
1306 pid = &queue->target;
1307 if (NULL == record)
1308 {
1309 queue->rekey_monotime_get = NULL;
1310 return;
1311 }
1312 if (sizeof(*mtbe) != record->value_size)
1313 {
1314 GNUNET_PEERSTORE_iteration_next (queue->rekey_monotime_get, 1);
1315 GNUNET_break (0);
1316 return;
1317 }
1318 mtbe = record->value;
1319 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1320 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
1321 queue->rekey_monotonic_time).abs_value_us)
1322 {
1324 "Queue from %s dropped, rekey monotime in the past\n",
1325 GNUNET_i2s (&queue->target));
1326 GNUNET_break (0);
1327 GNUNET_PEERSTORE_iteration_stop (queue->rekey_monotime_get);
1328 queue->rekey_monotime_get = NULL;
1329 // FIXME: Why should we try to gracefully finish here??
1331 return;
1332 }
1333 queue->rekey_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
1334 "transport_tcp_communicator",
1335 pid,
1337 rekey_monotonic_time,
1338 sizeof(*
1339 rekey_monotonic_time),
1343 queue);
1344}
static void queue_finish(struct Queue *queue)
Append a 'finish' message to the outgoing transmission.
static struct GNUNET_PEERSTORE_Handle * peerstore
Database for peer's HELLOs.
static void rekey_monotime_store_cb(void *cls, int success)
Callback called when peerstore store operation for rekey monotime value is finished.
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
struct GNUNET_PEERSTORE_StoreContext * GNUNET_PEERSTORE_store(struct GNUNET_PEERSTORE_Handle *h, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, const void *value, size_t size, struct GNUNET_TIME_Absolute expiry, enum GNUNET_PEERSTORE_StoreOption options, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
Store a new entry in the PEERSTORE.
#define GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY
Key used to store sender's monotonic time from rekey message.
@ GNUNET_PEERSTORE_STOREOPTION_REPLACE
Delete any previous values for the given key before storing the given value.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:737
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
The identity of the host (wraps the signing key of the peer).
Time for absolute time used by GNUnet, in microseconds and in network byte order.
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_break, GNUNET_ERROR_TYPE_ERROR, GNUNET_i2s(), GNUNET_log, GNUNET_PEERSTORE_iteration_next(), GNUNET_PEERSTORE_iteration_stop(), GNUNET_PEERSTORE_store(), GNUNET_PEERSTORE_STOREOPTION_REPLACE, GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY, GNUNET_TIME_absolute_ntoh(), GNUNET_TIME_UNIT_FOREVER_ABS, peerstore, pid, queue(), queue_finish(), record(), and rekey_monotime_store_cb().

Referenced by do_rekey().

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

◆ setup_in_cipher()

static void setup_in_cipher ( const struct GNUNET_CRYPTO_EcdhePublicKey ephemeral,
struct Queue queue 
)
static

Setup cipher of queue for decryption.

Parameters
ephemeralephemeral key we received from the other peer
[in,out]queuequeue to initialize decryption cipher for

Definition at line 1354 of file gnunet-communicator-tcp.c.

1356{
1357 struct GNUNET_HashCode k;
1358
1360 setup_cipher (&k, &my_identity, &queue->in_cipher, &queue->in_hmac);
1361}
static struct GNUNET_PeerIdentity my_identity
Our public key.
static void setup_cipher(const struct GNUNET_HashCode *dh, const struct GNUNET_PeerIdentity *pid, gcry_cipher_hd_t *cipher, struct GNUNET_HashCode *hmac_key)
Setup cipher based on shared secret dh and decrypting peer pid.
static struct GNUNET_CRYPTO_EddsaPrivateKey * my_private_key
Our private key.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_kem_decaps(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *c, struct GNUNET_HashCode *key_material)
Decapsulate a key for a private EdDSA key.
Definition: crypto_ecc.c:749

References GNUNET_CRYPTO_eddsa_kem_decaps(), my_identity, my_private_key, queue(), and setup_cipher().

Referenced by do_rekey(), proto_read_kx(), and queue_read_kx().

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

◆ do_rekey()

static void do_rekey ( struct Queue queue,
const struct TCPRekey rekey 
)
static

Handle rekey message on queue.

The message was already HMAC'ed, but we should additionally still check the signature. Then we need to stop the old cipher and start afresh.

Parameters
queuethe queue rekey was received on
rekeythe rekey message

Definition at line 1373 of file gnunet-communicator-tcp.c.

1374{
1375 struct TcpRekeySignature thp;
1376
1378 thp.purpose.size = htonl (sizeof(thp));
1380 "do_rekey size %u\n",
1381 thp.purpose.size);
1382 thp.sender = queue->target;
1384 "sender %s\n",
1385 GNUNET_p2s (&thp.sender.public_key));
1387 "sender %s\n",
1388 GNUNET_p2s (&queue->target.public_key));
1389 thp.receiver = my_identity;
1391 "receiver %s\n",
1392 GNUNET_p2s (&thp.receiver.public_key));
1393 thp.ephemeral = rekey->ephemeral;
1395 "ephemeral %s\n",
1396 GNUNET_e2s (&thp.ephemeral));
1397 thp.monotonic_time = rekey->monotonic_time;
1399 "time %s\n",
1401 GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
1402 GNUNET_assert (ntohl ((&thp)->purpose.size) == sizeof (*(&thp)));
1403 if (GNUNET_OK !=
1406 &thp,
1407 &rekey->sender_sig,
1408 &queue->target.public_key))
1409 {
1410 GNUNET_break (0);
1411 // FIXME Why should we try to gracefully finish here?
1413 return;
1414 }
1415 queue->rekey_monotonic_time = rekey->monotonic_time;
1416 queue->rekey_monotime_get = GNUNET_PEERSTORE_iteration_start (peerstore,
1417 "transport_tcp_communicator",
1418 &queue->target,
1420 &
1422 queue);
1423 gcry_cipher_close (queue->in_cipher);
1424 queue->rekeyed = GNUNET_YES;
1425 setup_in_cipher (&rekey->ephemeral, queue);
1426}
static void rekey_monotime_cb(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY where...
static void setup_in_cipher(const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral, struct Queue *queue)
Setup cipher of queue for decryption.
#define GNUNET_CRYPTO_eddsa_verify(purp, ps, sig, pub)
Verify EdDSA signature.
const char * GNUNET_e2s(const struct GNUNET_CRYPTO_EcdhePublicKey *p)
Convert a public key value to a string (for printing debug messages).
const char * GNUNET_p2s(const struct GNUNET_CRYPTO_EddsaPublicKey *p)
Convert a public key value to a string (for printing debug messages).
struct GNUNET_PEERSTORE_IterateContext * GNUNET_PEERSTORE_iteration_start(struct GNUNET_PEERSTORE_Handle *h, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, GNUNET_PEERSTORE_Processor callback, void *callback_cls)
Iterate over peerstore entries.
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:617
#define GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY
Signature used by TCP communicator rekey.
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!...
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
struct GNUNET_CRYPTO_EcdhePublicKey ephemeral
New ephemeral key.
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender's signature of type GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY.
Signature we use to verify that the ephemeral key was really chosen by the specified sender.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Purpose must be GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY.

References TCPRekey::ephemeral, TcpRekeySignature::ephemeral, GNUNET_assert, GNUNET_break, GNUNET_CRYPTO_eddsa_verify, GNUNET_e2s(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_OK, GNUNET_p2s(), GNUNET_PEERSTORE_iteration_start(), GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY, GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY, GNUNET_STRINGS_absolute_time_to_string(), GNUNET_TIME_absolute_ntoh(), GNUNET_YES, TCPRekey::monotonic_time, TcpRekeySignature::monotonic_time, my_identity, peerstore, GNUNET_PeerIdentity::public_key, GNUNET_CRYPTO_EccSignaturePurpose::purpose, TcpRekeySignature::purpose, queue(), queue_finish(), TcpRekeySignature::receiver, rekey_monotime_cb(), TcpRekeySignature::sender, TCPRekey::sender_sig, setup_in_cipher(), and GNUNET_CRYPTO_EccSignaturePurpose::size.

Referenced by try_handle_plaintext().

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

◆ handshake_ack_monotime_store_cb()

static void handshake_ack_monotime_store_cb ( void *  cls,
int  success 
)
static

Callback called when peerstore store operation for handshake ack monotime value is finished.

Parameters
clsQueue context the store operation was executed.
successStore operation was successful (GNUNET_OK) or not.

Definition at line 1435 of file gnunet-communicator-tcp.c.

1436{
1437 struct Queue *queue = cls;
1438
1439 if (GNUNET_OK != success)
1440 {
1442 "Failed to store handshake ack monotonic time in PEERSTORE!\n");
1443 }
1444 queue->handshake_ack_monotime_sc = NULL;
1445 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
1446}

References GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, GNUNET_PEERSTORE_iteration_next(), and queue().

Referenced by handshake_ack_monotime_cb().

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

◆ handshake_ack_monotime_cb()

static void handshake_ack_monotime_cb ( void *  cls,
const struct GNUNET_PEERSTORE_Record record,
const char *  emsg 
)
static

Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE_ACK where found.

Parameters
clsQueue context the store operation was executed.
recordThe record found or NULL if there is no record left.
emsgMessage from peerstore.

Definition at line 1457 of file gnunet-communicator-tcp.c.

1460{
1461 struct Queue *queue = cls;
1462 struct GNUNET_TIME_AbsoluteNBO *mtbe;
1463 struct GNUNET_TIME_Absolute mt;
1464 const struct GNUNET_PeerIdentity *pid;
1465 struct GNUNET_TIME_AbsoluteNBO *handshake_ack_monotonic_time;
1466
1467 (void) emsg;
1468
1469 handshake_ack_monotonic_time = &queue->handshake_ack_monotonic_time;
1470 pid = &queue->target;
1471 if (NULL == record)
1472 {
1473 queue->handshake_ack_monotime_get = NULL;
1474 return;
1475 }
1476 if (sizeof(*mtbe) != record->value_size)
1477 {
1478 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
1479 GNUNET_break (0);
1480 return;
1481 }
1482 mtbe = record->value;
1483 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1484 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
1485 queue->handshake_ack_monotonic_time).abs_value_us)
1486 {
1488 "Queue from %s dropped, handshake ack monotime in the past\n",
1489 GNUNET_i2s (&queue->target));
1490 GNUNET_break (0);
1491 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
1492 queue->handshake_ack_monotime_get = NULL;
1493 // FIXME: Why should we try to gracefully finish here?
1495 return;
1496 }
1497 queue->handshake_ack_monotime_sc =
1499 "transport_tcp_communicator",
1500 pid,
1502 handshake_ack_monotonic_time,
1503 sizeof(*handshake_ack_monotonic_time),
1507 queue);
1508}
static void handshake_ack_monotime_store_cb(void *cls, int success)
Callback called when peerstore store operation for handshake ack monotime value is finished.
#define GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE_ACK
Key used to store sender's monotonic time from handshake ack message.

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_break, GNUNET_ERROR_TYPE_ERROR, GNUNET_i2s(), GNUNET_log, GNUNET_PEERSTORE_iteration_next(), GNUNET_PEERSTORE_iteration_stop(), GNUNET_PEERSTORE_store(), GNUNET_PEERSTORE_STOREOPTION_REPLACE, GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE_ACK, GNUNET_TIME_absolute_ntoh(), GNUNET_TIME_UNIT_FOREVER_ABS, handshake_ack_monotime_store_cb(), peerstore, pid, queue(), queue_finish(), and record().

Referenced by try_handle_plaintext().

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

◆ send_challenge()

static void send_challenge ( struct GNUNET_CRYPTO_ChallengeNonceP  challenge,
struct Queue queue 
)
static

Sending challenge with TcpConfirmationAck back to sender of ephemeral key.

Parameters
tcThe TCPConfirmation originally send.
queueThe queue context.

Definition at line 1518 of file gnunet-communicator-tcp.c.

1520{
1521 struct TCPConfirmationAck tca;
1522 struct TcpHandshakeAckSignature thas;
1523
1525 "sending challenge\n");
1526
1527 tca.header.type = ntohs (
1529 tca.header.size = ntohs (sizeof(tca));
1530 tca.challenge = challenge;
1531 tca.sender = my_identity;
1532 tca.monotonic_time =
1534 thas.purpose.purpose = htonl (
1536 thas.purpose.size = htonl (sizeof(thas));
1537 thas.sender = my_identity;
1538 thas.receiver = queue->target;
1539 thas.monotonic_time = tca.monotonic_time;
1540 thas.challenge = tca.challenge;
1542 &thas,
1543 &tca.sender_sig);
1544 GNUNET_assert (0 ==
1545 gcry_cipher_encrypt (queue->out_cipher,
1546 &queue->cwrite_buf[queue->cwrite_off],
1547 sizeof(tca),
1548 &tca,
1549 sizeof(tca)));
1550 queue->cwrite_off += sizeof(tca);
1552 "sending challenge done\n");
1553}
static const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
#define GNUNET_CRYPTO_eddsa_sign(priv, ps, sig)
EdDSA sign a given block.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_CONFIRMATION_ACK
TCP communicator confirmation ack.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_monotonic(const struct GNUNET_CONFIGURATION_Handle *cfg)
Obtain the current time and make sure it is monotonically increasing.
Definition: time.c:860
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:638
#define GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE_ACK
Signature by a peer sending back the nonce received at initial handshake.
Ack for the encrypted continuation of TCP initial handshake.
Signature we use to verify that the ack from the receiver of the ephemeral key was really send by the...
struct GNUNET_CRYPTO_ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value.

References cfg, TcpHandshakeAckSignature::challenge, TCPConfirmationAck::challenge, GNUNET_assert, GNUNET_CRYPTO_eddsa_sign, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_CONFIRMATION_ACK, GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE_ACK, GNUNET_TIME_absolute_get_monotonic(), GNUNET_TIME_absolute_hton(), TCPConfirmationAck::header, TcpHandshakeAckSignature::monotonic_time, TCPConfirmationAck::monotonic_time, my_identity, my_private_key, GNUNET_CRYPTO_EccSignaturePurpose::purpose, TcpHandshakeAckSignature::purpose, queue(), TcpHandshakeAckSignature::receiver, TcpHandshakeAckSignature::sender, TCPConfirmationAck::sender, TCPConfirmationAck::sender_sig, GNUNET_MessageHeader::size, GNUNET_CRYPTO_EccSignaturePurpose::size, and GNUNET_MessageHeader::type.

Referenced by queue_read_kx(), and try_handle_plaintext().

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

◆ setup_out_cipher()

static void setup_out_cipher ( struct Queue queue,
struct GNUNET_HashCode dh 
)
static

Setup cipher for outgoing data stream based on target and our ephemeral private key.

Parameters
queuequeue to setup outgoing (encryption) cipher for

Definition at line 1563 of file gnunet-communicator-tcp.c.

1564{
1565 setup_cipher (dh, &queue->target, &queue->out_cipher, &queue->out_hmac);
1567 queue->rekey_left_bytes =
1569}
static struct GNUNET_TIME_Relative rekey_interval
The rekey interval.
static unsigned long long rekey_max_bytes
The rekey byte maximum.
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Generate a random unsigned 64-bit value.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).

References GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u64(), GNUNET_TIME_relative_to_absolute(), queue(), rekey_interval, rekey_max_bytes, and setup_cipher().

Referenced by inject_rekey(), and start_initial_kx_out().

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

◆ inject_rekey()

static void inject_rekey ( struct Queue queue)
static

Inject a struct TCPRekey message into the queue's plaintext buffer.

Parameters
queuequeue to perform rekeying on

Definition at line 1579 of file gnunet-communicator-tcp.c.

1580{
1581 struct TCPRekey rekey;
1582 struct TcpRekeySignature thp;
1583 struct GNUNET_HashCode k;
1584
1585 GNUNET_assert (0 == queue->pwrite_off);
1586 memset (&rekey, 0, sizeof(rekey));
1587 GNUNET_CRYPTO_eddsa_kem_encaps (&queue->target.public_key, &rekey.ephemeral,
1588 &k);
1589 rekey.header.type = ntohs (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY);
1590 rekey.header.size = ntohs (sizeof(rekey));
1591 rekey.monotonic_time =
1593 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY);
1594 thp.purpose.size = htonl (sizeof(thp));
1596 "inject_rekey size %u\n",
1597 thp.purpose.size);
1598 thp.sender = my_identity;
1600 "sender %s\n",
1601 GNUNET_p2s (&thp.sender.public_key));
1602 thp.receiver = queue->target;
1604 "receiver %s\n",
1605 GNUNET_p2s (&thp.receiver.public_key));
1606 thp.ephemeral = rekey.ephemeral;
1608 "ephemeral %s\n",
1609 GNUNET_e2s (&thp.ephemeral));
1610 thp.monotonic_time = rekey.monotonic_time;
1612 "time %s\n",
1614 GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
1616 &thp,
1617 &rekey.sender_sig);
1618 calculate_hmac (&queue->out_hmac, &rekey, sizeof(rekey), &rekey.hmac);
1619 /* Encrypt rekey message with 'old' cipher */
1620 GNUNET_assert (0 ==
1621 gcry_cipher_encrypt (queue->out_cipher,
1622 &queue->cwrite_buf[queue->cwrite_off],
1623 sizeof(rekey),
1624 &rekey,
1625 sizeof(rekey)));
1626 queue->cwrite_off += sizeof(rekey);
1627 /* Setup new cipher for successive messages */
1628 gcry_cipher_close (queue->out_cipher);
1629 setup_out_cipher (queue, &k);
1630}
static void setup_out_cipher(struct Queue *queue, struct GNUNET_HashCode *dh)
Setup cipher for outgoing data stream based on target and our ephemeral private key.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_kem_encaps(const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_CRYPTO_EcdhePublicKey *c, struct GNUNET_HashCode *key_material)
Encapsulate key material for a EdDSA public key.
Definition: crypto_ecc.c:794
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY
TCP communicator rekey message.
TCP rekey message box.

References calculate_hmac(), cfg, TCPRekey::ephemeral, TcpRekeySignature::ephemeral, GNUNET_assert, GNUNET_CRYPTO_eddsa_kem_encaps(), GNUNET_CRYPTO_eddsa_sign, GNUNET_e2s(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY, GNUNET_p2s(), GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY, GNUNET_STRINGS_absolute_time_to_string(), GNUNET_TIME_absolute_get_monotonic(), GNUNET_TIME_absolute_hton(), GNUNET_TIME_absolute_ntoh(), TCPRekey::header, TCPRekey::hmac, TCPRekey::monotonic_time, TcpRekeySignature::monotonic_time, my_identity, my_private_key, GNUNET_PeerIdentity::public_key, GNUNET_CRYPTO_EccSignaturePurpose::purpose, TcpRekeySignature::purpose, queue(), TcpRekeySignature::receiver, TcpRekeySignature::sender, TCPRekey::sender_sig, setup_out_cipher(), GNUNET_MessageHeader::size, GNUNET_CRYPTO_EccSignaturePurpose::size, and GNUNET_MessageHeader::type.

Referenced by mq_send_d(), and queue_write().

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

◆ pending_reversals_delete_it()

static int pending_reversals_delete_it ( void *  cls,
const struct GNUNET_HashCode key,
void *  value 
)
static

Definition at line 1634 of file gnunet-communicator-tcp.c.

1637{
1638 (void) cls;
1639 struct PendingReversal *pending_reversal = value;
1640
1641 if (NULL != pending_reversal->timeout_task)
1642 {
1643 GNUNET_SCHEDULER_cancel (pending_reversal->timeout_task);
1644 pending_reversal->timeout_task = NULL;
1645 }
1648 key,
1649 pending_reversal));
1650 GNUNET_free (pending_reversal->in);
1651 GNUNET_free (pending_reversal);
1652 return GNUNET_OK;
1653}
static struct GNUNET_CONTAINER_MultiHashMap * pending_reversals
Map of pending reversals.
static char * value
Value of the record to add/remove.
Struct for pending nat reversals.
struct sockaddr * in
Address the reversal was send to.
struct GNUNET_SCHEDULER_Task * timeout_task

References GNUNET_assert, GNUNET_CONTAINER_multihashmap_remove(), GNUNET_free, GNUNET_OK, GNUNET_SCHEDULER_cancel(), GNUNET_YES, PendingReversal::in, key, pending_reversals, PendingReversal::timeout_task, and value.

Referenced by check_and_remove_pending_reversal(), and do_shutdown().

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

◆ check_and_remove_pending_reversal()

static void check_and_remove_pending_reversal ( struct sockaddr *  in,
sa_family_t  sa_family,
struct GNUNET_PeerIdentity sender 
)
static

Definition at line 1657 of file gnunet-communicator-tcp.c.

1659{
1660 if (AF_INET == sa_family)
1661 {
1662 struct PendingReversal *pending_reversal;
1663 struct GNUNET_HashCode key;
1664 struct sockaddr_in *natted_address;
1665
1666 natted_address = GNUNET_memdup (in, sizeof (struct sockaddr));
1667 natted_address->sin_port = 0;
1668 GNUNET_CRYPTO_hash (natted_address,
1669 sizeof(struct sockaddr),
1670 &key);
1671
1673 &key);
1674 if (NULL != pending_reversal && (NULL == sender ||
1675 0 != memcmp (sender,
1676 &pending_reversal->target,
1677 sizeof(struct
1679 {
1681 "Removing invalid pending reversal for `%s'at `%s'\n",
1682 GNUNET_i2s (&pending_reversal->target),
1683 GNUNET_a2s (in, sizeof (struct sockaddr)));
1684 pending_reversals_delete_it (NULL, &key, pending_reversal);
1685 }
1686 GNUNET_free (natted_address);
1687 }
1688}
static int pending_reversals_delete_it(void *cls, const struct GNUNET_HashCode *key, void *value)
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).
struct GNUNET_PeerIdentity target
To whom are we like to talk to.

References GNUNET_a2s(), GNUNET_CONTAINER_multihashmap_get(), GNUNET_CRYPTO_hash(), GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_i2s(), GNUNET_log, GNUNET_memdup, key, pending_reversals, pending_reversals_delete_it(), and PendingReversal::target.

Referenced by free_proto_queue(), proto_read_kx(), and try_handle_plaintext().

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

◆ free_proto_queue()

static void free_proto_queue ( struct ProtoQueue pq)
static

Closes socket and frees memory associated with pq.

Parameters
pqproto queue to free

Definition at line 1697 of file gnunet-communicator-tcp.c.

1698{
1699 if (NULL != pq->listen_sock)
1700 {
1702 pq->listen_sock = NULL;
1703 }
1704 if (NULL != pq->read_task)
1705 {
1707 pq->read_task = NULL;
1708 }
1709 if (NULL != pq->write_task)
1710 {
1712 pq->write_task = NULL;
1713 }
1714 check_and_remove_pending_reversal (pq->address, pq->address->sa_family, NULL);
1716 GNUNET_free (pq->address);
1718 GNUNET_free (pq);
1719}
static struct ProtoQueue * proto_tail
Protoqueues DLL tail.
static void check_and_remove_pending_reversal(struct sockaddr *in, sa_family_t sa_family, struct GNUNET_PeerIdentity *sender)
static struct ProtoQueue * proto_head
Protoqueues DLL head.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct GNUNET_SCHEDULER_Task * write_task
ID of write task for this connection.
struct GNUNET_NETWORK_Handle * listen_sock
Listen socket.
struct GNUNET_NETWORK_Handle * sock
socket that we transmit all data with on this queue
struct sockaddr * address
Address of the other peer.
struct GNUNET_SCHEDULER_Task * read_task
ID of read task for this connection.

References ProtoQueue::address, check_and_remove_pending_reversal(), GNUNET_break, GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_NETWORK_socket_close(), GNUNET_OK, GNUNET_SCHEDULER_cancel(), ProtoQueue::listen_sock, proto_head, proto_tail, ProtoQueue::read_task, ProtoQueue::sock, and ProtoQueue::write_task.

Referenced by do_shutdown(), proto_queue_write(), and proto_read_kx().

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

◆ proto_queue_write()

static void proto_queue_write ( void *  cls)
static

We have been notified that our socket is ready to write.

Then reschedule this function to be called again once more is available.

Parameters
clsa struct ProtoQueue

Definition at line 1729 of file gnunet-communicator-tcp.c.

1730{
1731 struct ProtoQueue *pq = cls;
1732 ssize_t sent;
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In proto queue write\n");
1734 pq->write_task = NULL;
1735 if (0 != pq->write_off)
1736 {
1737 sent = GNUNET_NETWORK_socket_send (pq->sock,
1738 pq->write_buf,
1739 pq->write_off);
1741 "Sent %lu bytes to TCP queue\n", sent);
1742 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1743 {
1745 free_proto_queue (pq);
1746 return;
1747 }
1748 if (sent > 0)
1749 {
1750 size_t usent = (size_t) sent;
1751 pq->write_off -= usent;
1752 memmove (pq->write_buf,
1753 &pq->write_buf[usent],
1754 pq->write_off);
1755 }
1756 }
1757 /* do we care to write more? */
1758 if ((0 < pq->write_off))
1759 pq->write_task =
1761 pq->sock,
1763 pq);
1764}
static void proto_queue_write(void *cls)
We have been notified that our socket is ready to write.
static void free_proto_queue(struct ProtoQueue *pq)
Closes socket and frees memory associated with pq.
ssize_t GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc, const void *buffer, size_t length)
Send data (always non-blocking).
Definition: network.c:737
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_write_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *wfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1583
Handle for an incoming connection where we do not yet have enough information to setup a full queue.
char write_buf[sizeof(struct TCPNATProbeMessage)]
buffer for writing struct TCPNATProbeMessage to network.
size_t write_off
Offset of the buffer?

References free_proto_queue(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_log_strerror, GNUNET_NETWORK_socket_send(), GNUNET_SCHEDULER_add_write_net(), GNUNET_TIME_UNIT_FOREVER_REL, proto_queue_write(), ProtoQueue::sock, ProtoQueue::write_buf, ProtoQueue::write_off, and ProtoQueue::write_task.

Referenced by proto_queue_write(), and try_connection_reversal().

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

◆ queue_write()

static void queue_write ( void *  cls)
static

We have been notified that our socket is ready to write.

Then reschedule this function to be called again once more is available.

Parameters
clsa struct Queue

Definition at line 1774 of file gnunet-communicator-tcp.c.

1775{
1776 struct Queue *queue = cls;
1777 ssize_t sent;
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In queue write\n");
1779 queue->write_task = NULL;
1780 if (0 != queue->cwrite_off)
1781 {
1782 sent = GNUNET_NETWORK_socket_send (queue->sock,
1783 queue->cwrite_buf,
1784 queue->cwrite_off);
1786 "Sent %lu bytes to TCP queue\n", sent);
1787 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1788 {
1791 return;
1792 }
1793 if (sent > 0)
1794 {
1795 size_t usent = (size_t) sent;
1796 queue->cwrite_off -= usent;
1797 memmove (queue->cwrite_buf,
1798 &queue->cwrite_buf[usent],
1799 queue->cwrite_off);
1800 queue->timeout =
1803 }
1804 }
1805 /* can we encrypt more? (always encrypt full messages, needed
1806 such that #mq_cancel() can work!) */
1807 unsigned int we_do_not_need_to_rekey = (0 < queue->rekey_left_bytes
1808 - (queue->cwrite_off
1809 + queue->pwrite_off
1810 + sizeof (struct TCPRekey)));
1811 if (we_do_not_need_to_rekey &&
1812 (queue->pwrite_off > 0) &&
1813 (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE))
1814 {
1816 "Encrypting %lu bytes\n", queue->pwrite_off);
1817 GNUNET_assert (0 ==
1818 gcry_cipher_encrypt (queue->out_cipher,
1819 &queue->cwrite_buf[queue->cwrite_off],
1820 queue->pwrite_off,
1821 queue->pwrite_buf,
1822 queue->pwrite_off));
1823 if (queue->rekey_left_bytes > queue->pwrite_off)
1824 queue->rekey_left_bytes -= queue->pwrite_off;
1825 else
1826 queue->rekey_left_bytes = 0;
1827 queue->cwrite_off += queue->pwrite_off;
1828 queue->pwrite_off = 0;
1829 }
1830 // if ((-1 != unverified_size)&& ((0 == queue->pwrite_off) &&
1831 if (((0 == queue->rekey_left_bytes) ||
1833 queue->rekey_time).rel_value_us)) &&
1834 (((0 == queue->pwrite_off) || ! we_do_not_need_to_rekey) &&
1835 (queue->cwrite_off + sizeof (struct TCPRekey) <= BUF_SIZE)))
1836 {
1838 }
1839 if ((0 == queue->pwrite_off) && (! queue->finishing) &&
1840 (GNUNET_YES == queue->mq_awaits_continue))
1841 {
1842 queue->mq_awaits_continue = GNUNET_NO;
1844 }
1845 /* did we just finish writing 'finish'? */
1846 if ((0 == queue->cwrite_off) && (GNUNET_YES == queue->finishing))
1847 {
1849 "Finishing queue\n");
1851 return;
1852 }
1853 /* do we care to write more? */
1854 if ((0 < queue->cwrite_off) || (0 < queue->pwrite_off))
1855 queue->write_task =
1857 queue->sock,
1858 &queue_write,
1859 queue);
1860}
static void inject_rekey(struct Queue *queue)
Inject a struct TCPRekey message into the queue's plaintext buffer.
static void queue_write(void *cls)
We have been notified that our socket is ready to write.
void GNUNET_MQ_impl_send_continue(struct GNUNET_MQ_Handle *mq)
Call the send implementation for the next queued message, if any.
Definition: mq.c:421
uint64_t rel_value_us
The actual value.

References BUF_SIZE, GNUNET_assert, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_log_strerror, GNUNET_MQ_impl_send_continue(), GNUNET_NETWORK_socket_send(), GNUNET_NO, GNUNET_SCHEDULER_add_write_net(), GNUNET_TIME_absolute_get_remaining(), GNUNET_TIME_relative_to_absolute(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, inject_rekey(), queue(), queue_destroy(), queue_write(), and GNUNET_TIME_Relative::rel_value_us.

Referenced by mq_init(), mq_send(), proto_read_kx(), queue_read_kx(), queue_write(), and try_handle_plaintext().

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

◆ try_handle_plaintext()

static size_t try_handle_plaintext ( struct Queue queue)
static

Test if we have received a full message in plaintext.

If so, handle it.

Parameters
queuequeue to process inbound plaintext for
Returns
number of bytes of plaintext handled, 0 for none

Once we received this ack, we consider this a verified connection. FIXME: I am not sure this logic is sane here.

Definition at line 1871 of file gnunet-communicator-tcp.c.

1872{
1873 const struct GNUNET_MessageHeader *hdr;
1874 const struct TCPConfirmationAck *tca;
1875 const struct TCPBox *box;
1876 const struct TCPRekey *rekey;
1877 const struct TCPFinish *fin;
1878 struct TCPRekey rekeyz;
1879 struct TCPFinish finz;
1880 struct GNUNET_ShortHashCode tmac;
1881 uint16_t type;
1882 size_t size = 0;
1883 struct TcpHandshakeAckSignature thas;
1884 const struct GNUNET_CRYPTO_ChallengeNonceP challenge = queue->challenge;
1885
1887 "try handle plaintext!\n");
1888
1889 hdr = (const struct GNUNET_MessageHeader *) queue->pread_buf;
1890 if ((sizeof(*hdr) > queue->pread_off))
1891 {
1893 "Handling plaintext, not even a header!\n");
1894 return 0; /* not even a header */
1895 }
1896
1897 if ((GNUNET_YES != queue->initial_core_kx_done) && (queue->unverified_size >
1899 {
1901 "Already received data of size %lu bigger than KX size %lu!\n",
1902 queue->unverified_size,
1904 GNUNET_break_op (0);
1906 return 0;
1907 }
1908
1909 type = ntohs (hdr->type);
1910 switch (type)
1911 {
1913 tca = (const struct TCPConfirmationAck *) queue->pread_buf;
1915 "start processing ack\n");
1916 if (sizeof(*tca) > queue->pread_off)
1917 {
1919 "Handling plaintext size of tca greater than pread offset.\n")
1920 ;
1921 return 0;
1922 }
1923 if (ntohs (hdr->size) != sizeof(*tca))
1924 {
1926 "Handling plaintext size does not match message type.\n");
1927 GNUNET_break_op (0);
1929 return 0;
1930 }
1931
1932 thas.purpose.purpose = htonl (
1934 thas.purpose.size = htonl (sizeof(thas));
1935 thas.sender = tca->sender;
1936 thas.receiver = my_identity;
1937 thas.monotonic_time = tca->monotonic_time;
1938 thas.challenge = tca->challenge;
1939
1942 &thas,
1943 &tca->sender_sig,
1944 &tca->sender.public_key))
1945 {
1947 "Verification of signature failed!\n");
1948 GNUNET_break (0);
1950 return 0;
1951 }
1952 if (0 != GNUNET_memcmp (&tca->challenge, &challenge))
1953 {
1955 "Challenge in TCPConfirmationAck not correct!\n");
1956 GNUNET_break (0);
1958 return 0;
1959 }
1960
1961 queue->handshake_ack_monotime_get = GNUNET_PEERSTORE_iteration_start (
1962 peerstore,
1963 "transport_tcp_communicator",
1964 &queue->target,
1967 queue);
1968
1970 "Handling plaintext, ack processed!\n");
1971
1973 {
1974 send_challenge (queue->challenge_received, queue);
1975 queue->write_task =
1977 queue->sock,
1978 &queue_write,
1979 queue);
1980 }
1981 else if (GNUNET_TRANSPORT_CS_OUTBOUND == queue->cs)
1982 {
1984 queue->address->sa_family, NULL);
1985 }
1986
1991 queue->initial_core_kx_done = GNUNET_YES;
1992
1993 char *foreign_addr;
1994
1995 switch (queue->address->sa_family)
1996 {
1997 case AF_INET:
1998 GNUNET_asprintf (&foreign_addr,
1999 "%s-%s",
2001 GNUNET_a2s (queue->address, queue->address_len));
2002 break;
2003
2004 case AF_INET6:
2005 GNUNET_asprintf (&foreign_addr,
2006 "%s-%s",
2008 GNUNET_a2s (queue->address, queue->address_len));
2009 break;
2010
2011 default:
2012 GNUNET_assert (0);
2013 }
2014
2016 &queue->target,
2017 foreign_addr,
2018 UINT16_MAX, /* no MTU */
2020 0, /* Priority */
2021 queue->nt,
2022 queue->cs,
2023 queue->mq);
2024
2025 GNUNET_free (foreign_addr);
2026
2027 size = ntohs (hdr->size);
2028 break;
2030 /* Special case: header size excludes box itself! */
2031 box = (const struct TCPBox *) queue->pread_buf;
2032 if (ntohs (hdr->size) + sizeof(struct TCPBox) > queue->pread_off)
2033 return 0;
2034 calculate_hmac (&queue->in_hmac, &box[1], ntohs (hdr->size), &tmac);
2035 if (0 != memcmp (&tmac, &box->hmac, sizeof(tmac)))
2036 {
2037 GNUNET_break_op (0);
2039 return 0;
2040 }
2041 pass_plaintext_to_core (queue, (const void *) &box[1], ntohs (hdr->size));
2042 size = ntohs (hdr->size) + sizeof(*box);
2044 "Handling plaintext, box processed!\n");
2046 "# bytes decrypted with BOX",
2047 size,
2048 GNUNET_NO);
2050 "# messages decrypted with BOX",
2051 1,
2052 GNUNET_NO);
2053 break;
2054
2056 rekey = (const struct TCPRekey *) queue->pread_buf;
2057 if (sizeof(*rekey) > queue->pread_off)
2058 return 0;
2059 if (ntohs (hdr->size) != sizeof(*rekey))
2060 {
2061 GNUNET_break_op (0);
2063 return 0;
2064 }
2065 rekeyz = *rekey;
2066 memset (&rekeyz.hmac, 0, sizeof(rekeyz.hmac));
2067 calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
2068 if (0 != memcmp (&tmac, &rekey->hmac, sizeof(tmac)))
2069 {
2070 GNUNET_break_op (0);
2072 return 0;
2073 }
2074 do_rekey (queue, rekey);
2075 size = ntohs (hdr->size);
2077 "Handling plaintext, rekey processed!\n");
2079 "# rekeying successful",
2080 1,
2081 GNUNET_NO);
2082 break;
2083
2085 fin = (const struct TCPFinish *) queue->pread_buf;
2086 if (sizeof(*fin) > queue->pread_off)
2087 return 0;
2088 if (ntohs (hdr->size) != sizeof(*fin))
2089 {
2090 GNUNET_break_op (0);
2092 return 0;
2093 }
2094 finz = *fin;
2095 memset (&finz.hmac, 0, sizeof(finz.hmac));
2096 calculate_hmac (&queue->in_hmac, &finz, sizeof(finz), &tmac);
2097 if (0 != memcmp (&tmac, &fin->hmac, sizeof(tmac)))
2098 {
2099 GNUNET_break_op (0);
2101 return 0;
2102 }
2103 /* handle FINISH by destroying queue */
2106 "Handling plaintext, finish processed!\n");
2107 break;
2108
2109 default:
2111 "Handling plaintext, nothing processed!\n");
2112 GNUNET_break_op (0);
2114 return 0;
2115 }
2116 GNUNET_assert (0 != size);
2117 if (-1 != queue->unverified_size)
2118 queue->unverified_size += size;
2119 return size;
2120}
static void pass_plaintext_to_core(struct Queue *queue, const void *plaintext, size_t plaintext_len)
We received plaintext_len bytes of plaintext on queue.
#define COMMUNICATOR_ADDRESS_PREFIX
Address prefix used by the communicator.
#define INITIAL_CORE_KX_SIZE
Size of the initial core key exchange messages.
static void do_rekey(struct Queue *queue, const struct TCPRekey *rekey)
Handle rekey message on queue.
static void handshake_ack_monotime_cb(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE_A...
static void send_challenge(struct GNUNET_CRYPTO_ChallengeNonceP challenge, struct Queue *queue)
Sending challenge with TcpConfirmationAck back to sender of ephemeral key.
static uint32_t type
Type string converted to DNS type value.
#define GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED
Queue length.
struct GNUNET_TRANSPORT_QueueHandle * GNUNET_TRANSPORT_communicator_mq_add(struct GNUNET_TRANSPORT_CommunicatorHandle *ch, const struct GNUNET_PeerIdentity *peer, const char *address, uint32_t mtu, uint64_t q_len, uint32_t priority, enum GNUNET_NetworkType nt, enum GNUNET_TRANSPORT_ConnectionStatus cs, struct GNUNET_MQ_Handle *mq)
Notify transport service that a MQ became available due to an "inbound" connection or because the com...
@ GNUNET_TRANSPORT_CS_INBOUND
this is an inbound connection (communicator initiated)
@ GNUNET_TRANSPORT_CS_OUTBOUND
this is an outbound connection (transport initiated)
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX
TCP communicator payload box.
static unsigned int size
Size of the "table".
Definition: peer.c:68
Type of a nonce used for challenges.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
struct GNUNET_CRYPTO_EddsaPublicKey public_key
TCP message box.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender's signature of type GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE_ACK.
struct GNUNET_CRYPTO_ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value.
struct GNUNET_PeerIdentity sender
Sender's identity.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.

References calculate_hmac(), ch, TcpHandshakeAckSignature::challenge, TCPConfirmationAck::challenge, check_and_remove_pending_reversal(), COMMUNICATOR_ADDRESS_PREFIX, do_rekey(), GNUNET_a2s(), GNUNET_asprintf(), GNUNET_assert, GNUNET_break, GNUNET_break_op, GNUNET_CRYPTO_eddsa_verify, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_memcmp, GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX, GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_CONFIRMATION_ACK, GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH, GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY, GNUNET_NO, GNUNET_PEERSTORE_iteration_start(), GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE_ACK, GNUNET_SCHEDULER_add_write_net(), GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE_ACK, GNUNET_STATISTICS_update(), GNUNET_SYSERR, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TRANSPORT_communicator_mq_add(), GNUNET_TRANSPORT_CS_INBOUND, GNUNET_TRANSPORT_CS_OUTBOUND, GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED, GNUNET_YES, handshake_ack_monotime_cb(), TCPBox::hmac, TCPRekey::hmac, TCPFinish::hmac, INITIAL_CORE_KX_SIZE, TcpHandshakeAckSignature::monotonic_time, TCPConfirmationAck::monotonic_time, my_identity, pass_plaintext_to_core(), peerstore, GNUNET_PeerIdentity::public_key, GNUNET_CRYPTO_EccSignaturePurpose::purpose, TcpHandshakeAckSignature::purpose, queue(), queue_destroy(), queue_finish(), queue_write(), TcpHandshakeAckSignature::receiver, send_challenge(), TcpHandshakeAckSignature::sender, TCPConfirmationAck::sender, TCPConfirmationAck::sender_sig, GNUNET_MessageHeader::size, GNUNET_CRYPTO_EccSignaturePurpose::size, size, stats, type, and GNUNET_MessageHeader::type.

Referenced by queue_read().

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

◆ tcp_address_to_sockaddr_numeric_v6()

static struct sockaddr * tcp_address_to_sockaddr_numeric_v6 ( socklen_t *  sock_len,
struct sockaddr_in6  v6,
unsigned int  port 
)
static

Convert a struct sockaddr_in6 to astruct sockaddr *`.

Parameters
[out]sock_lenset to the length of the address.
v6The sockaddr_in6 to be converted.
Returns
The struct sockaddr *.

Definition at line 2257 of file gnunet-communicator-tcp.c.

2260{
2261 struct sockaddr *in;
2262
2263 v6.sin6_family = AF_INET6;
2264 v6.sin6_port = htons ((uint16_t) port);
2265#if HAVE_SOCKADDR_IN_SIN_LEN
2266 v6.sin6_len = sizeof(struct sockaddr_in6);
2267#endif
2268 v6.sin6_flowinfo = 0;
2269 v6.sin6_scope_id = 0;
2270 in = GNUNET_memdup (&v6, sizeof(v6));
2271 *sock_len = sizeof(struct sockaddr_in6);
2272
2273 return in;
2274}
static uint16_t port
Port number.
Definition: gnunet-bcd.c:147

References GNUNET_memdup, and port.

Referenced by init_socket_resolv(), run(), tcp_address_to_sockaddr(), and tcp_address_to_sockaddr_port_only().

Here is the caller graph for this function:

◆ tcp_address_to_sockaddr_numeric_v4()

static struct sockaddr * tcp_address_to_sockaddr_numeric_v4 ( socklen_t *  sock_len,
struct sockaddr_in  v4,
unsigned int  port 
)
static

Convert a struct sockaddr_in4 to astruct sockaddr *`.

Parameters
[out]sock_lenset to the length of the address.
v4The sockaddr_in4 to be converted.
Returns
The struct sockaddr *.

Definition at line 2285 of file gnunet-communicator-tcp.c.

2288{
2289 struct sockaddr *in;
2290
2291 v4.sin_family = AF_INET;
2292 v4.sin_port = htons ((uint16_t) port);
2293#if HAVE_SOCKADDR_IN_SIN_LEN
2294 v4.sin_len = sizeof(struct sockaddr_in);
2295#endif
2296 in = GNUNET_memdup (&v4, sizeof(v4));
2297 *sock_len = sizeof(struct sockaddr_in);
2298 return in;
2299}

References GNUNET_memdup, and port.

Referenced by init_socket_resolv(), run(), tcp_address_to_sockaddr(), and tcp_address_to_sockaddr_port_only().

Here is the caller graph for this function:

◆ tcp_address_to_sockaddr_port_only()

static struct PortOnlyIpv4Ipv6 * tcp_address_to_sockaddr_port_only ( const char *  bindto,
unsigned int *  port 
)
static

Convert TCP bind specification to a struct PortOnlyIpv4Ipv6 *

Parameters
bindtobind specification to convert.
Returns
The converted bindto specification.

Definition at line 2309 of file gnunet-communicator-tcp.c.

2310{
2311 struct PortOnlyIpv4Ipv6 *po;
2312 struct sockaddr_in *i4;
2313 struct sockaddr_in6 *i6;
2314 socklen_t sock_len_ipv4;
2315 socklen_t sock_len_ipv6;
2316
2317 /* interpreting value as just a PORT number */
2318 if (*port > UINT16_MAX)
2319 {
2321 "BINDTO specification `%s' invalid: value too large for port\n",
2322 bindto);
2323 return NULL;
2324 }
2325
2326 po = GNUNET_new (struct PortOnlyIpv4Ipv6);
2327
2328 if (GNUNET_YES == disable_v6)
2329 {
2330 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2331 po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2332 *port);
2333 po->addr_len_ipv4 = sock_len_ipv4;
2334 }
2335 else
2336 {
2337
2338 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2339 po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2340 *port);
2341 po->addr_len_ipv4 = sock_len_ipv4;
2342
2343 i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
2344 po->addr_ipv6 = tcp_address_to_sockaddr_numeric_v6 (&sock_len_ipv6, *i6,
2345 *port);
2346
2347 po->addr_len_ipv6 = sock_len_ipv6;
2348
2349 GNUNET_free (i6);
2350 }
2351
2352 GNUNET_free (i4);
2353
2354 return po;
2355}
static struct sockaddr * tcp_address_to_sockaddr_numeric_v6(socklen_t *sock_len, struct sockaddr_in6 v6, unsigned int port)
Convert a struct sockaddr_in6 to astruct sockaddr *`.
static int disable_v6
IPv6 disabled.
static struct sockaddr * tcp_address_to_sockaddr_numeric_v4(socklen_t *sock_len, struct sockaddr_in v4, unsigned int port)
Convert a struct sockaddr_in4 to astruct sockaddr *`.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
In case of port only configuration we like to bind to ipv4 and ipv6 addresses.
struct sockaddr * addr_ipv4
Ipv4 address we like to bind to.
struct sockaddr * addr_ipv6
Ipv6 address we like to bind to.
socklen_t addr_len_ipv6
Length of ipv6 address.
socklen_t addr_len_ipv4
Length of ipv4 address.

References PortOnlyIpv4Ipv6::addr_ipv4, PortOnlyIpv4Ipv6::addr_ipv6, PortOnlyIpv4Ipv6::addr_len_ipv4, PortOnlyIpv4Ipv6::addr_len_ipv6, disable_v6, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_new, GNUNET_YES, port, tcp_address_to_sockaddr_numeric_v4(), and tcp_address_to_sockaddr_numeric_v6().

Referenced by run().

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

◆ extract_address()

static char * extract_address ( const char *  bindto)
static

This Method extracts the address part of the BINDTO string.

Parameters
bindtoString we extract the address part from.
Returns
The extracted address string.

Definition at line 2365 of file gnunet-communicator-tcp.c.

2366{
2367 char *addr;
2368 char *start;
2369 char *token;
2370 char *cp;
2371 char *rest = NULL;
2372 char *res;
2373
2375 "extract address with bindto %s\n",
2376 bindto);
2377
2378 if (NULL == bindto)
2380 "bindto is NULL\n");
2381
2382 cp = GNUNET_strdup (bindto);
2383
2385 "extract address 2\n");
2386
2387 start = cp;
2388 if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
2389 {
2390 start++; /* skip over '['*/
2391 cp[strlen (cp) - 1] = '\0'; /* eat ']'*/
2392 addr = GNUNET_strdup (start);
2393 }
2394 else
2395 {
2396 token = strtok_r (cp, "]", &rest);
2397 if (strlen (bindto) == strlen (token))
2398 {
2399 token = strtok_r (cp, ":", &rest);
2400 addr = GNUNET_strdup (token);
2401 }
2402 else
2403 {
2404 token++;
2405 res = GNUNET_strdup (token);
2406 addr = GNUNET_strdup (res);
2407 }
2408 }
2409
2411 "tcp address: %s\n",
2412 addr);
2413 GNUNET_free (cp);
2414 return addr;
2415}
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
static char * res
Currently read line or NULL on EOF.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.

References GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_strdup, res, and start.

Referenced by run(), and tcp_address_to_sockaddr().

Here is the caller graph for this function:

◆ extract_port()

static unsigned int extract_port ( const char *  addr_and_port)
static

This Method extracts the port part of the BINDTO string.

Parameters
addr_and_portString we extract the port from.
Returns
The extracted port as unsigned int.

Definition at line 2425 of file gnunet-communicator-tcp.c.

2426{
2427 unsigned int port;
2428 char dummy[2];
2429 char *token;
2430 char *addr;
2431 char *colon;
2432 char *cp;
2433 char *rest = NULL;
2434
2435 if (NULL != addr_and_port)
2436 {
2437 cp = GNUNET_strdup (addr_and_port);
2438 token = strtok_r (cp, "]", &rest);
2439 if (strlen (addr_and_port) == strlen (token))
2440 {
2441 colon = strrchr (cp, ':');
2442 if (NULL == colon)
2443 {
2444 GNUNET_free (cp);
2445 return 0;
2446 }
2447 addr = colon;
2448 addr++;
2449 }
2450 else
2451 {
2452 token = strtok_r (NULL, "]", &rest);
2453 if (NULL == token)
2454 {
2455 GNUNET_free (cp);
2456 return 0;
2457 }
2458 else
2459 {
2460 addr = token;
2461 addr++;
2462 }
2463 }
2464
2465
2466 if (1 == sscanf (addr, "%u%1s", &port, dummy))
2467 {
2468 /* interpreting value as just a PORT number */
2469 if (port > UINT16_MAX)
2470 {
2472 "Port `%u' invalid: value too large for port\n",
2473 port);
2474 GNUNET_free (cp);
2475 return 0;
2476 }
2477 }
2478 else
2479 {
2481 "BINDTO specification invalid: last ':' not followed by number\n");
2482 GNUNET_free (cp);
2483 return 0;
2484 }
2485 GNUNET_free (cp);
2486 }
2487 else
2488 {
2490 "return 0\n");
2491 /* interpret missing port as 0, aka pick any free one */
2492 port = 0;
2493 }
2494
2495 return port;
2496}
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.

References dummy, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_strdup, and port.

Referenced by run(), and tcp_address_to_sockaddr().

Here is the caller graph for this function:

◆ tcp_address_to_sockaddr()

static struct sockaddr * tcp_address_to_sockaddr ( const char *  bindto,
socklen_t *  sock_len 
)
static

Convert TCP bind specification to a struct sockaddr *

Parameters
bindtobind specification to convert
[out]sock_lenset to the length of the address
Returns
converted bindto specification

Definition at line 2507 of file gnunet-communicator-tcp.c.

2508{
2509 struct sockaddr *in;
2510 unsigned int port;
2511 struct sockaddr_in v4;
2512 struct sockaddr_in6 v6;
2513 char *start;
2514
2515 memset (&v4, 0, sizeof(v4));
2516 start = extract_address (bindto);
2517 GNUNET_assert (NULL != start);
2519 "start %s\n",
2520 start);
2521
2523 "!bindto %s\n",
2524 bindto);
2525
2526
2527 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
2528 {
2529 port = extract_port (bindto);
2530
2532 "port %u\n",
2533 port);
2534
2535 in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port);
2536 }
2537 else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
2538 {
2539 port = extract_port (bindto);
2540 in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port);
2541 }
2542 else
2543 {
2544 GNUNET_assert (0);
2545 }
2546
2548 return in;
2549}
static char * extract_address(const char *bindto)
This Method extracts the address part of the BINDTO string.
static unsigned int extract_port(const char *addr_and_port)
This Method extracts the port part of the BINDTO string.

References extract_address(), extract_port(), GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, port, start, tcp_address_to_sockaddr_numeric_v4(), and tcp_address_to_sockaddr_numeric_v6().

Referenced by mq_init().

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

◆ mq_send()

static void mq_send ( struct GNUNET_MQ_Handle mq,
const struct GNUNET_MessageHeader msg,
void *  impl_state 
)
static

Signature of functions implementing the sending functionality of a message queue.

Parameters
mqthe message queue
msgthe message to send
impl_stateour struct Queue

Definition at line 2561 of file gnunet-communicator-tcp.c.

2564{
2565 struct Queue *queue = impl_state;
2566 uint16_t msize = ntohs (msg->size);
2567 struct TCPBox box;
2569 "In MQ send. Queue finishing: %s; write task running: %s\n",
2570 (GNUNET_YES == queue->finishing) ? "yes" : "no",
2571 (NULL == queue->write_task) ? "yes" : "no");
2572 GNUNET_assert (mq == queue->mq);
2573 queue->mq_awaits_continue = GNUNET_YES;
2574 if (GNUNET_YES == queue->finishing)
2575 return; /* this queue is dying, drop msg */
2576 GNUNET_assert (0 == queue->pwrite_off);
2577 box.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX);
2578 box.header.size = htons (msize);
2579 calculate_hmac (&queue->out_hmac, msg, msize, &box.hmac);
2580 memcpy (&queue->pwrite_buf[queue->pwrite_off], &box, sizeof(box));
2581 queue->pwrite_off += sizeof(box);
2582 memcpy (&queue->pwrite_buf[queue->pwrite_off], msg, msize);
2583 queue->pwrite_off += msize;
2585 "%lu bytes of plaintext to send\n", queue->pwrite_off);
2586 GNUNET_assert (NULL != queue->sock);
2587 if (NULL == queue->write_task)
2588 queue->write_task =
2590 queue->sock,
2591 &queue_write,
2592 queue);
2593}
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
struct GNUNET_MessageHeader * msg
Definition: 005.c:2

References calculate_hmac(), GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX, GNUNET_SCHEDULER_add_write_net(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, TCPBox::header, TCPBox::hmac, mq, msg, queue(), queue_write(), GNUNET_MessageHeader::size, and GNUNET_MessageHeader::type.

Referenced by boot_queue().

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

◆ mq_destroy()

static void mq_destroy ( struct GNUNET_MQ_Handle mq,
void *  impl_state 
)
static

Signature of functions implementing the destruction of a message queue.

Implementations must not free mq, but should take care of impl_state.

Parameters
mqthe message queue to destroy
impl_stateour struct Queue

Definition at line 2605 of file gnunet-communicator-tcp.c.

2606{
2607 struct Queue *queue = impl_state;
2608
2609 if (mq == queue->mq)
2610 {
2611 queue->mq = NULL;
2613 }
2614}

References mq, queue(), and queue_finish().

Referenced by boot_queue().

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

◆ mq_cancel()

static void mq_cancel ( struct GNUNET_MQ_Handle mq,
void *  impl_state 
)
static

Implementation function that cancels the currently sent message.

Parameters
mqmessage queue
impl_stateour struct Queue

Definition at line 2624 of file gnunet-communicator-tcp.c.

2625{
2626 struct Queue *queue = impl_state;
2627
2628 GNUNET_assert (0 != queue->pwrite_off);
2629 queue->pwrite_off = 0;
2630}

References GNUNET_assert, and queue().

Referenced by boot_queue().

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

◆ mq_error()

static void mq_error ( void *  cls,
enum GNUNET_MQ_Error  error 
)
static

Generic error handler, called with the appropriate error code and the same closure specified at the creation of the message queue.

Not every message queue implementation supports an error handler.

Parameters
clsour struct Queue
errorerror code

Definition at line 2643 of file gnunet-communicator-tcp.c.

2644{
2645 struct Queue *queue = cls;
2646
2648 "MQ error in queue to %s: %d\n",
2649 GNUNET_i2s (&queue->target),
2650 (int) error);
2652}

References GNUNET_ERROR_TYPE_ERROR, GNUNET_i2s(), GNUNET_log, queue(), and queue_finish().

Referenced by boot_queue().

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

◆ boot_queue()

static void boot_queue ( struct Queue queue)
static

Add the given queue to our internal data structure.

Setup the MQ processing and inform transport that the queue is ready. Must be called after the KX for outgoing messages has been bootstrapped.

Parameters
queuequeue to boot

Definition at line 2663 of file gnunet-communicator-tcp.c.

2664{
2665 queue->nt =
2666 GNUNET_NT_scanner_get_type (is, queue->address, queue->address_len);
2668 queue_map,
2669 &queue->key,
2670 queue,
2673 "# queues active",
2675 GNUNET_NO);
2676 queue->timeout =
2679 &mq_destroy,
2680 &mq_cancel,
2681 queue,
2682 NULL,
2683 &mq_error,
2684 queue);
2685}
static void mq_destroy(struct GNUNET_MQ_Handle *mq, void *impl_state)
Signature of functions implementing the destruction of a message queue.
static struct GNUNET_NT_InterfaceScanner * is
Network scanner to determine network types.
static void mq_cancel(struct GNUNET_MQ_Handle *mq, void *impl_state)
Implementation function that cancels the currently sent message.
static void mq_error(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
static void mq_send(struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *msg, void *impl_state)
Signature of functions implementing the sending functionality of a message queue.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *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.
struct GNUNET_MQ_Handle * GNUNET_MQ_queue_for_callbacks(GNUNET_MQ_SendImpl send, GNUNET_MQ_DestroyImpl destroy, GNUNET_MQ_CancelImpl cancel, void *impl_state, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *cls)
Create a message queue for the specified handlers.
Definition: mq.c:465
enum GNUNET_NetworkType GNUNET_NT_scanner_get_type(struct GNUNET_NT_InterfaceScanner *is, const struct sockaddr *addr, socklen_t addrlen)
Returns where the address is located: loopback, LAN or WAN.
Definition: nt.c:307

References GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_multihashmap_size(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE, GNUNET_MQ_queue_for_callbacks(), GNUNET_NO, GNUNET_NT_scanner_get_type(), GNUNET_STATISTICS_set(), GNUNET_TIME_relative_to_absolute(), is, mq_cancel(), mq_destroy(), mq_error(), mq_send(), queue(), queue_map, and stats.

Referenced by mq_init(), and proto_read_kx().

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

◆ transmit_kx()

static void transmit_kx ( struct Queue queue,
const struct GNUNET_CRYPTO_EcdhePublicKey epub 
)
static

Generate and transmit our ephemeral key and the signature for the initial KX with the other peer.

Must be called first, before any other bytes are ever written to the output buffer. Note that our cipher must already be initialized when calling this function. Helper function for start_initial_kx_out().

Parameters
queuequeue to do KX for
epubour public key for the KX

Definition at line 2699 of file gnunet-communicator-tcp.c.

2701{
2702 struct TcpHandshakeSignature ths;
2703 struct TCPConfirmation tc;
2704
2705 memcpy (queue->cwrite_buf, epub, sizeof(*epub));
2706 queue->cwrite_off = sizeof(*epub);
2707 /* compute 'tc' and append in encrypted format to cwrite_buf */
2708 tc.sender = my_identity;
2709 tc.monotonic_time =
2712 &tc.challenge,
2713 sizeof(tc.challenge));
2714 ths.purpose.purpose = htonl (
2716 ths.purpose.size = htonl (sizeof(ths));
2717 ths.sender = my_identity;
2718 ths.receiver = queue->target;
2719 ths.ephemeral = *epub;
2720 ths.monotonic_time = tc.monotonic_time;
2721 ths.challenge = tc.challenge;
2723 &ths,
2724 &tc.sender_sig);
2725 GNUNET_assert (0 ==
2726 gcry_cipher_encrypt (queue->out_cipher,
2727 &queue->cwrite_buf[queue->cwrite_off],
2728 sizeof(tc),
2729 &tc,
2730 sizeof(tc)));
2731 queue->challenge = tc.challenge;
2732 queue->cwrite_off += sizeof(tc);
2733
2735 "handshake written\n");
2736}
void GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
Fill block with a random values.
@ GNUNET_CRYPTO_QUALITY_NONCE
Randomness for IVs etc.
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition: scheduler.c:431
#define GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE
Signature used by TCP communicator handshake.
Signature we use to verify that the ephemeral key was really chosen by the specified sender.

References cfg, TcpHandshakeSignature::challenge, TcpHandshakeSignature::ephemeral, GNUNET_assert, GNUNET_CRYPTO_eddsa_sign, GNUNET_CRYPTO_QUALITY_NONCE, GNUNET_CRYPTO_random_block(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE, GNUNET_TIME_absolute_get_monotonic(), GNUNET_TIME_absolute_hton(), TcpHandshakeSignature::monotonic_time, my_identity, my_private_key, GNUNET_CRYPTO_EccSignaturePurpose::purpose, TcpHandshakeSignature::purpose, queue(), TcpHandshakeSignature::receiver, TcpHandshakeSignature::sender, GNUNET_CRYPTO_EccSignaturePurpose::size, and tc.

Referenced by start_initial_kx_out().

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

◆ start_initial_kx_out()

static void start_initial_kx_out ( struct Queue queue)
static

Initialize our key material for outgoing transmissions and inform the other peer about it.

Must be called first before any data is sent.

Parameters
queuethe queue to setup

Definition at line 2747 of file gnunet-communicator-tcp.c.

2748{
2749 struct GNUNET_CRYPTO_EcdhePublicKey epub;
2750 struct GNUNET_HashCode k;
2751
2752 // TODO: We could use the Elligator KEM here! https://bugs.gnunet.org/view.php?id=8065
2753 GNUNET_CRYPTO_eddsa_kem_encaps (&queue->target.public_key, &epub, &k);
2754 setup_out_cipher (queue, &k);
2755 transmit_kx (queue, &epub);
2756}
static void transmit_kx(struct Queue *queue, const struct GNUNET_CRYPTO_EcdhePublicKey *epub)
Generate and transmit our ephemeral key and the signature for the initial KX with the other peer.

References GNUNET_CRYPTO_eddsa_kem_encaps(), queue(), setup_out_cipher(), and transmit_kx().

Referenced by mq_init(), and proto_read_kx().

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

◆ handshake_monotime_store_cb()

static void handshake_monotime_store_cb ( void *  cls,
int  success 
)
static

Callback called when peerstore store operation for handshake monotime is finished.

Parameters
clsQueue context the store operation was executed.
successStore operation was successful (GNUNET_OK) or not.

Definition at line 2765 of file gnunet-communicator-tcp.c.

2766{
2767 struct Queue *queue = cls;
2768 if (GNUNET_OK != success)
2769 {
2771 "Failed to store handshake monotonic time in PEERSTORE!\n");
2772 }
2773 queue->handshake_monotime_sc = NULL;
2774 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
2775}

References GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, GNUNET_PEERSTORE_iteration_next(), and queue().

Referenced by handshake_monotime_cb().

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

◆ handshake_monotime_cb()

static void handshake_monotime_cb ( void *  cls,
const struct GNUNET_PEERSTORE_Record record,
const char *  emsg 
)
static

Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE where found.

Parameters
clsQueue context the store operation was executed.
recordThe record found or NULL if there is no record left.
emsgMessage from peerstore.

Definition at line 2786 of file gnunet-communicator-tcp.c.

2789{
2790 struct Queue *queue = cls;
2791 struct GNUNET_TIME_AbsoluteNBO *mtbe;
2792 struct GNUNET_TIME_Absolute mt;
2793 const struct GNUNET_PeerIdentity *pid;
2794 struct GNUNET_TIME_AbsoluteNBO *handshake_monotonic_time;
2795
2796 (void) emsg;
2797
2798 handshake_monotonic_time = &queue->handshake_monotonic_time;
2799 pid = &queue->target;
2801 "tcp handshake with us %s\n",
2803 if (NULL == record)
2804 {
2805 queue->handshake_monotime_get = NULL;
2806 return;
2807 }
2809 "tcp handshake from peer %s\n",
2810 GNUNET_i2s (pid));
2811 if (sizeof(*mtbe) != record->value_size)
2812 {
2813 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
2814 GNUNET_break (0);
2815 return;
2816 }
2817 mtbe = record->value;
2818 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
2819 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
2820 queue->handshake_monotonic_time).abs_value_us)
2821 {
2823 "Queue from %s dropped, handshake monotime in the past\n",
2824 GNUNET_i2s (&queue->target));
2825 GNUNET_break (0);
2826 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
2827 queue->handshake_ack_monotime_get = NULL;
2829 return;
2830 }
2831 queue->handshake_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
2832 "transport_tcp_communicator",
2833 pid,
2835 handshake_monotonic_time,
2836 sizeof(*
2837 handshake_monotonic_time),
2840 &
2842 queue);
2843}
static void handshake_monotime_store_cb(void *cls, int success)
Callback called when peerstore store operation for handshake monotime is finished.
#define GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE
Key used to store sender's monotonic time from handshake message.

References GNUNET_TIME_Absolute::abs_value_us, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_i2s(), GNUNET_log, GNUNET_PEERSTORE_iteration_next(), GNUNET_PEERSTORE_iteration_stop(), GNUNET_PEERSTORE_store(), GNUNET_PEERSTORE_STOREOPTION_REPLACE, GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE, GNUNET_TIME_absolute_ntoh(), GNUNET_TIME_UNIT_FOREVER_ABS, handshake_monotime_store_cb(), my_identity, peerstore, pid, queue(), queue_finish(), and record().

Referenced by decrypt_and_check_tc().

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

◆ decrypt_and_check_tc()

static int decrypt_and_check_tc ( struct Queue queue,
struct TCPConfirmation tc,
char *  ibuf 
)
static

We have received the first bytes from the other side on a queue.

Decrypt the tc contained in ibuf and check the signature. Note that setup_in_cipher() must have already been called.

Parameters
queuequeue to decrypt initial bytes from other peer for
[out]tcwhere to store the result
ibufincoming data, of size INITIAL_KX_SIZE
Returns
GNUNET_OK if the signature was OK, GNUNET_SYSERR if not

Definition at line 2858 of file gnunet-communicator-tcp.c.

2861{
2862 struct TcpHandshakeSignature ths;
2864
2866 0 ==
2867 gcry_cipher_decrypt (queue->in_cipher,
2868 tc,
2869 sizeof(*tc),
2870 &ibuf[sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)],
2871 sizeof(*tc)));
2872 ths.purpose.purpose = htonl (
2874 ths.purpose.size = htonl (sizeof(ths));
2875 ths.sender = tc->sender;
2876 ths.receiver = my_identity;
2877 memcpy (&ths.ephemeral, ibuf, sizeof(struct GNUNET_CRYPTO_EcdhePublicKey));
2878 ths.monotonic_time = tc->monotonic_time;
2879 ths.challenge = tc->challenge;
2882 &ths,
2883 &tc->sender_sig,
2884 &tc->sender.public_key);
2885 if (GNUNET_YES == ret)
2886 queue->handshake_monotime_get =
2888 "transport_tcp_communicator",
2889 &queue->target,
2892 queue);
2893 return ret;
2894}
static void handshake_monotime_cb(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE w...
GNUNET_GenericReturnValue
Named constants for return values.

References TcpHandshakeSignature::challenge, TcpHandshakeSignature::ephemeral, GNUNET_assert, GNUNET_CRYPTO_eddsa_verify, GNUNET_PEERSTORE_iteration_start(), GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE, GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE, GNUNET_YES, handshake_monotime_cb(), TcpHandshakeSignature::monotonic_time, my_identity, peerstore, GNUNET_CRYPTO_EccSignaturePurpose::purpose, TcpHandshakeSignature::purpose, queue(), TcpHandshakeSignature::receiver, ret, TcpHandshakeSignature::sender, GNUNET_CRYPTO_EccSignaturePurpose::size, and tc.

Referenced by proto_read_kx(), and queue_read_kx().

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

◆ queue_read_kx()

static void queue_read_kx ( void *  cls)
static

Read from the socket of the queue until we have enough data to initialize the decryption logic and can switch to regular reading.

Parameters
clsa struct Queue

Definition at line 2905 of file gnunet-communicator-tcp.c.

2906{
2907 struct Queue *queue = cls;
2908 ssize_t rcvd;
2909 struct GNUNET_TIME_Relative left;
2910 struct TCPConfirmation tc;
2911
2912 queue->read_task = NULL;
2913 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2914 if (0 == left.rel_value_us)
2915 {
2917 return;
2918 }
2919 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2920 &queue->cread_buf[queue->cread_off],
2921 BUF_SIZE - queue->cread_off);
2923 "Received %lu bytes to write in buffer of size %lu for KX from queue %p (expires in %"
2924 PRIu64 ")\n",
2925 rcvd, BUF_SIZE - queue->cread_off, queue, left.rel_value_us);
2926 if (-1 == rcvd)
2927 {
2928 if ((EAGAIN != errno) && (EINTR != errno))
2929 {
2932 return;
2933 }
2934 queue->read_task =
2936 return;
2937 }
2938 if (0 == rcvd)
2939 {
2940 /* Orderly shutdown of connection */
2942 "Socket for queue %p seems to have been closed\n", queue);
2944 return;
2945 }
2946 queue->cread_off += rcvd;
2947 if (queue->cread_off < INITIAL_KX_SIZE)
2948 {
2949 /* read more */
2951 "%lu/%lu bytes of KX read. Rescheduling...\n",
2952 queue->cread_off, INITIAL_KX_SIZE);
2953 queue->read_task =
2955 return;
2956 }
2957 /* we got all the data, let's find out who we are talking to! */
2959 queue->cread_buf,
2960 queue);
2961 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, queue->cread_buf))
2962 {
2964 "Invalid TCP KX received from %s\n",
2965 GNUNET_a2s (queue->address, queue->address_len));
2967 return;
2968 }
2969 if (0 !=
2970 memcmp (&tc.sender, &queue->target, sizeof(struct GNUNET_PeerIdentity)))
2971 {
2973 "Invalid sender in TCP KX received from %s\n",
2974 GNUNET_a2s (queue->address, queue->address_len));
2976 return;
2977 }
2978 send_challenge (tc.challenge, queue);
2979 queue->write_task =
2981 queue->sock,
2982 &queue_write,
2983 queue);
2984
2985 /* update queue timeout */
2986 queue->timeout =
2988 /* prepare to continue with regular read task immediately */
2989 memmove (queue->cread_buf,
2990 &queue->cread_buf[INITIAL_KX_SIZE],
2991 queue->cread_off - (INITIAL_KX_SIZE));
2993 "cread_off is %lu bytes before adjusting\n",
2994 queue->cread_off);
2995 queue->cread_off -= INITIAL_KX_SIZE;
2997 "cread_off set to %lu bytes\n",
2998 queue->cread_off);
3000}
static void queue_read_kx(void *cls)
Read from the socket of the queue until we have enough data to initialize the decryption logic and ca...
#define INITIAL_KX_SIZE
Size of the initial key exchange message sent first in both directions.
static int decrypt_and_check_tc(struct Queue *queue, struct TCPConfirmation *tc, char *ibuf)
We have received the first bytes from the other side on a queue.
@ GNUNET_ERROR_TYPE_INFO
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1305

References BUF_SIZE, decrypt_and_check_tc(), GNUNET_a2s(), GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_log_strerror, GNUNET_NETWORK_socket_recv(), GNUNET_OK, GNUNET_SCHEDULER_add_now(), GNUNET_SCHEDULER_add_read_net(), GNUNET_SCHEDULER_add_write_net(), GNUNET_TIME_absolute_get_remaining(), GNUNET_TIME_relative_to_absolute(), GNUNET_TIME_UNIT_FOREVER_REL, INITIAL_KX_SIZE, queue(), queue_destroy(), queue_read(), queue_read_kx(), queue_write(), GNUNET_TIME_Relative::rel_value_us, send_challenge(), setup_in_cipher(), and tc.

Referenced by mq_init(), proto_read_kx(), and queue_read_kx().

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

◆ proto_read_kx()

static void proto_read_kx ( void *  cls)
static

Read from the socket of the proto queue until we have enough data to upgrade to full queue.

Parameters
clsa struct ProtoQueue

Definition at line 3010 of file gnunet-communicator-tcp.c.

3011{
3012 struct ProtoQueue *pq = cls;
3013 ssize_t rcvd;
3014 struct GNUNET_TIME_Relative left;
3015 struct Queue *queue;
3016 struct TCPConfirmation tc;
3018
3019 pq->read_task = NULL;
3021 if (0 == left.rel_value_us)
3022 {
3023 free_proto_queue (pq);
3024 return;
3025 }
3026 rcvd = GNUNET_NETWORK_socket_recv (pq->sock,
3027 &pq->ibuf[pq->ibuf_off],
3028 sizeof(pq->ibuf) - pq->ibuf_off);
3030 "Proto received %lu bytes for KX\n", rcvd);
3031 if (-1 == rcvd)
3032 {
3033 if ((EAGAIN != errno) && (EINTR != errno))
3034 {
3036 free_proto_queue (pq);
3037 return;
3038 }
3039 /* try again */
3040 pq->read_task =
3042 return;
3043 }
3044 if (0 == rcvd)
3045 {
3046 /* Orderly shutdown of connection */
3048 "Socket for proto queue %p seems to have been closed\n", pq);
3049 free_proto_queue (pq);
3050 return;
3051 }
3052 pq->ibuf_off += rcvd;
3053 if (sizeof (struct TCPNATProbeMessage) == pq->ibuf_off)
3054 {
3055 struct TCPNATProbeMessage *pm = (struct TCPNATProbeMessage *) pq->ibuf;
3056
3058 &pm->clientIdentity);
3059
3060 queue = GNUNET_new (struct Queue);
3061 queue->target = pm->clientIdentity;
3064 }
3065 else if (pq->ibuf_off > sizeof(pq->ibuf))
3066 {
3067 /* read more */
3068 pq->read_task =
3070 return;
3071 }
3072 else
3073 {
3074 /* we got all the data, let's find out who we are talking to! */
3075 queue = GNUNET_new (struct Queue);
3077 queue);
3078 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, pq->ibuf))
3079 {
3081 "Invalid TCP KX received from %s\n",
3082 GNUNET_a2s (pq->address, pq->address_len));
3083 gcry_cipher_close (queue->in_cipher);
3085 free_proto_queue (pq);
3086 return;
3087 }
3088 queue->target = tc.sender;
3091 }
3092 queue->address = pq->address; /* steals reference */
3093 queue->address_len = pq->address_len;
3094 queue->listen_sock = pq->listen_sock;
3095 queue->sock = pq->sock;
3096
3098 "created queue with target %s\n",
3099 GNUNET_i2s (&queue->target));
3100
3102 "start kx proto\n");
3103
3105 boot_queue (queue);
3106 queue->read_task =
3108 queue->sock,
3109 read_task,
3110 queue);
3111 queue->write_task =
3113 queue->sock,
3114 &queue_write,
3115 queue);
3116 // TODO To early! Move it somewhere else.
3117 // send_challenge (tc.challenge, queue);
3118 queue->challenge_received = tc.challenge;
3119
3121 GNUNET_free (pq);
3122}
static void boot_queue(struct Queue *queue)
Add the given queue to our internal data structure.
static void proto_read_kx(void *cls)
Read from the socket of the proto queue until we have enough data to upgrade to full queue.
static void start_initial_kx_out(struct Queue *queue)
Initialize our key material for outgoing transmissions and inform the other peer about it.
struct GNUNET_SCHEDULER_Task * read_task
static struct GNUNET_TRANSPORT_PluginMonitor * pm
Handle if we are monitoring plugin session activity.
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
socklen_t address_len
Length of the address.
size_t ibuf_off
Current offset for reading into ibuf.
struct GNUNET_TIME_Absolute timeout
Timeout for this protoqueue.
char ibuf[(sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)+sizeof(struct TCPConfirmation))]
Buffer for reading all the information we need to upgrade from protoqueue to queue.
Basically a WELCOME message, but with the purpose of giving the waiting peer a client handle to use.

References ProtoQueue::address, ProtoQueue::address_len, boot_queue(), check_and_remove_pending_reversal(), decrypt_and_check_tc(), free_proto_queue(), GNUNET_a2s(), GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_i2s(), GNUNET_log, GNUNET_log_strerror, GNUNET_NETWORK_socket_recv(), GNUNET_new, GNUNET_OK, GNUNET_SCHEDULER_add_read_net(), GNUNET_SCHEDULER_add_write_net(), GNUNET_TIME_absolute_get_remaining(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TRANSPORT_CS_INBOUND, GNUNET_TRANSPORT_CS_OUTBOUND, ProtoQueue::ibuf, ProtoQueue::ibuf_off, ProtoQueue::listen_sock, pm, proto_head, proto_read_kx(), proto_tail, queue(), queue_read(), queue_read_kx(), queue_write(), read_task, ProtoQueue::read_task, GNUNET_TIME_Relative::rel_value_us, setup_in_cipher(), ProtoQueue::sock, start_initial_kx_out(), tc, and ProtoQueue::timeout.

Referenced by create_proto_queue(), and proto_read_kx().

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

◆ create_proto_queue()

static struct ProtoQueue * create_proto_queue ( struct GNUNET_NETWORK_Handle sock,
struct sockaddr *  in,
socklen_t  addrlen 
)
static

Definition at line 3126 of file gnunet-communicator-tcp.c.

3129{
3130 struct ProtoQueue *pq = GNUNET_new (struct ProtoQueue);
3131
3132 if (NULL == sock)
3133 {
3134 // sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
3135 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, 0);
3136 if (NULL == sock)
3137 {
3139 "socket(%d) failed: %s",
3140 in->sa_family,
3141 strerror (errno));
3142 GNUNET_free (in);
3143 GNUNET_free (pq);
3144 return NULL;
3145 }
3146 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, addrlen)) &&
3147 (errno != EINPROGRESS))
3148 {
3150 "connect to `%s' failed: %s",
3151 GNUNET_a2s (in, addrlen),
3152 strerror (errno));
3154 GNUNET_free (in);
3155 GNUNET_free (pq);
3156 return NULL;
3157 }
3158 }
3159 pq->address_len = addrlen;
3160 pq->address = in;
3162 pq->sock = sock;
3164 pq->sock,
3166 pq);
3168
3169 return pq;
3170}
#define PROTO_QUEUE_TIMEOUT
How long do we wait until we must have received the initial KX?
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:832
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_connect(const struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Connect a socket to some remote address.
Definition: network.c:601

References ProtoQueue::address, ProtoQueue::address_len, GNUNET_a2s(), GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_connect(), GNUNET_NETWORK_socket_create(), GNUNET_new, GNUNET_OK, GNUNET_SCHEDULER_add_read_net(), GNUNET_TIME_relative_to_absolute(), proto_head, PROTO_QUEUE_TIMEOUT, proto_read_kx(), proto_tail, ProtoQueue::read_task, ProtoQueue::sock, and ProtoQueue::timeout.

Referenced by listen_cb(), and try_connection_reversal().

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

◆ try_connection_reversal()

static void try_connection_reversal ( void *  cls,
const struct sockaddr *  addr,
socklen_t  addrlen 
)
static

Definition at line 3220 of file gnunet-communicator-tcp.c.

3223{
3224 (void) cls;
3225 struct TCPNATProbeMessage pm;
3226 struct ProtoQueue *pq;
3227 struct sockaddr *in_addr;
3228
3230 "addr->sa_family %d\n",
3231 addr->sa_family);
3233 "Try to connect back\n");
3234 in_addr = GNUNET_memdup (addr, addrlen);
3236 "in_addr->sa_family %d\n",
3237 in_addr->sa_family);
3238 pq = create_proto_queue (NULL, in_addr, addrlen);
3239 if (NULL != pq)
3240 {
3241 pm.header.size = htons (sizeof(struct TCPNATProbeMessage));
3242 pm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
3243 pm.clientIdentity = my_identity;
3244 memcpy (pq->write_buf, &pm, sizeof(struct TCPNATProbeMessage));
3245 pq->write_off = sizeof(struct TCPNATProbeMessage);
3247 pq->sock,
3249 pq);
3250 }
3251 else
3252 {
3254 "Couldn't create ProtoQueue for sending TCPNATProbeMessage\n");
3255 }
3256}
#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.

References create_proto_queue(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_memdup, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, GNUNET_SCHEDULER_add_write_net(), my_identity, pm, PROTO_QUEUE_TIMEOUT, proto_queue_write(), ProtoQueue::sock, ProtoQueue::write_buf, ProtoQueue::write_off, and ProtoQueue::write_task.

Referenced by nat_register().

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

◆ pending_reversal_timeout()

static void pending_reversal_timeout ( void *  cls)
static

Definition at line 3260 of file gnunet-communicator-tcp.c.

3261{
3262 struct sockaddr *in = cls;
3263 struct PendingReversal *pending_reversal;
3264 struct GNUNET_HashCode key;
3265
3267 sizeof(struct sockaddr),
3268 &key);
3270 &key);
3271
3272 GNUNET_assert (NULL != pending_reversal);
3273
3275 &key,
3276 pending_reversal))
3278 "No pending reversal found for address %s\n",
3279 GNUNET_a2s (in, sizeof (struct sockaddr)));
3280 GNUNET_free (pending_reversal->in);
3281 GNUNET_free (pending_reversal);
3282}

References GNUNET_a2s(), GNUNET_assert, GNUNET_CONTAINER_multihashmap_get(), GNUNET_CONTAINER_multihashmap_remove(), GNUNET_CRYPTO_hash(), GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_NO, PendingReversal::in, key, and pending_reversals.

Referenced by mq_init().

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

◆ mq_init()

static int mq_init ( void *  cls,
const struct GNUNET_PeerIdentity peer,
const char *  address 
)
static

Function called by the transport service to initialize a message queue given address information about another peer.

If and when the communication channel is established, the communicator must call GNUNET_TRANSPORT_communicator_mq_add() to notify the service that the channel is now up. It is the responsibility of the communicator to manage sane retries and timeouts for any peer/address combination provided by the transport service. Timeouts and retries do not need to be signalled to the transport service.

Parameters
clsclosure
peeridentity of the other peer
addresswhere to send the message, human-readable communicator-specific format, 0-terminated, UTF-8
Returns
GNUNET_OK on success, GNUNET_SYSERR if the provided address is invalid

Definition at line 3304 of file gnunet-communicator-tcp.c.

3305{
3306 struct sockaddr *in;
3307 socklen_t in_len = 0;
3308 const char *path;
3309 struct sockaddr_in *v4;
3310 struct sockaddr_in6 *v6;
3311 unsigned int is_natd = GNUNET_NO;
3312 struct GNUNET_HashCode key;
3313 struct GNUNET_HashCode queue_map_key;
3314 struct GNUNET_HashContext *hsh;
3315 struct Queue *queue;
3316
3318 "Connecting to %s at %s\n",
3319 GNUNET_i2s (peer),
3320 address);
3321 if (0 != strncmp (address,
3323 strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
3324 {
3325 GNUNET_break_op (0);
3326 return GNUNET_SYSERR;
3327 }
3328 path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
3329 in = tcp_address_to_sockaddr (path, &in_len);
3330
3331 if (NULL == in)
3332 {
3334 "Failed to setup TCP socket address\n");
3335 return GNUNET_SYSERR;
3336 }
3337
3339 "in %s\n",
3340 GNUNET_a2s (in, in_len));
3341
3344 GNUNET_CRYPTO_hash_context_read (hsh, peer, sizeof (*peer));
3345 GNUNET_CRYPTO_hash_context_finish (hsh, &queue_map_key);
3347
3348 if (NULL != queue)
3349 {
3351 "Queue for %s already exists or is in construction\n", address);
3352 return GNUNET_NO;
3353 }
3354 switch (in->sa_family)
3355 {
3356 case AF_INET:
3357 v4 = (struct sockaddr_in *) in;
3358 if (0 == v4->sin_port)
3359 {
3360 is_natd = GNUNET_YES;
3362 sizeof(struct sockaddr),
3363 &key);
3366 &key))
3367 {
3369 "There is already a request reversal for `%s'at `%s'\n",
3370 GNUNET_i2s (peer),
3371 address);
3372 GNUNET_free (in);
3373 return GNUNET_SYSERR;
3374 }
3375 }
3376 break;
3377
3378 case AF_INET6:
3379 if (GNUNET_YES == disable_v6)
3380 {
3382 "IPv6 disabled, skipping %s\n", address);
3383 GNUNET_free (in);
3384 return GNUNET_SYSERR;
3385 }
3386 v6 = (struct sockaddr_in6 *) in;
3387 if (0 == v6->sin6_port)
3388 {
3390 "Request reversal for `%s' at `%s' not possible for an IPv6 address\n",
3391 GNUNET_i2s (peer),
3392 address);
3393 GNUNET_free (in);
3394 return GNUNET_SYSERR;
3395 }
3396 break;
3397
3398 default:
3399 GNUNET_assert (0);
3400 }
3401
3402 if (GNUNET_YES == is_natd)
3403 {
3404 struct sockaddr_in local_sa;
3405 struct PendingReversal *pending_reversal;
3406
3407 memset (&local_sa, 0, sizeof(local_sa));
3408 local_sa.sin_family = AF_INET;
3409 local_sa.sin_port = htons (bind_port);
3410 /* We leave sin_address at 0, let the kernel figure it out,
3411 even if our bind() is more specific. (May want to reconsider
3412 later.) */
3413 if (GNUNET_OK != GNUNET_NAT_request_reversal (nat, &local_sa, v4))
3414 {
3416 "request reversal for `%s' at `%s' failed\n",
3417 GNUNET_i2s (peer),
3418 address);
3419 GNUNET_free (in);
3420 return GNUNET_SYSERR;
3421 }
3422 pending_reversal = GNUNET_new (struct PendingReversal);
3423 pending_reversal->in = in;
3426 &key,
3427 pending_reversal,
3429 pending_reversal->target = *peer;
3431 &
3433 in);
3435 "Created NAT WAIT connection to `%s' at `%s'\n",
3436 GNUNET_i2s (peer),
3437 GNUNET_a2s (in, sizeof (struct sockaddr)));
3438 }
3439 else
3440 {
3441 struct GNUNET_NETWORK_Handle *sock;
3442
3443 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM,
3444 IPPROTO_TCP);
3445 if (NULL == sock)
3446 {
3448 "socket(%d) failed: %s",
3449 in->sa_family,
3450 strerror (errno));
3451 GNUNET_free (in);
3452 return GNUNET_SYSERR;
3453 }
3454 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, in_len)) &&
3455 (errno != EINPROGRESS))
3456 {
3458 "connect to `%s' failed: %s",
3459 address,
3460 strerror (errno));
3462 GNUNET_free (in);
3463 return GNUNET_SYSERR;
3464 }
3465
3466 queue = GNUNET_new (struct Queue);
3467 queue->target = *peer;
3468 queue->key = queue_map_key;
3469 queue->address = in;
3470 queue->address_len = in_len;
3471 queue->sock = sock;
3473 boot_queue (queue);
3475 "booted queue with target %s\n",
3476 GNUNET_i2s (&queue->target));
3477 // queue->mq_awaits_continue = GNUNET_YES;
3478 queue->read_task =
3480 queue->sock,
3482 queue);
3483
3484
3486 "start kx mq_init\n");
3487
3489 queue->write_task =
3491 queue->sock,
3492 &queue_write,
3493 queue);
3494 }
3495
3496 return GNUNET_OK;
3497}
static unsigned int bind_port
The port the communicator should be assigned to.
#define NAT_TIMEOUT
How long until we give up on establishing an NAT connection? Must be > 4 RTT.
static struct GNUNET_NAT_Handle * nat
Connection to NAT service.
static void pending_reversal_timeout(void *cls)
static struct sockaddr * tcp_address_to_sockaddr(const char *bindto, socklen_t *sock_len)
Convert TCP bind specification to a struct sockaddr *
static char * address
GNS address for this phone.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_contains(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Check if the map contains any value under the given key (including values that are NULL).
@ 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...
void GNUNET_CRYPTO_hash_context_read(struct GNUNET_HashContext *hc, const void *buf, size_t size)
Add data to be hashed.
Definition: crypto_hash.c:366
void GNUNET_CRYPTO_hash_context_finish(struct GNUNET_HashContext *hc, struct GNUNET_HashCode *r_hash)
Finish the hash computation.
Definition: crypto_hash.c:390
struct GNUNET_HashContext * GNUNET_CRYPTO_hash_context_start(void)
Start incremental hashing operation.
Definition: crypto_hash.c:350
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:699
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:1278

References address, bind_port, boot_queue(), COMMUNICATOR_ADDRESS_PREFIX, disable_v6, GNUNET_a2s(), GNUNET_assert, GNUNET_break_op, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_CONTAINER_multihashmap_contains(), GNUNET_CONTAINER_multihashmap_get(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_CRYPTO_hash(), GNUNET_CRYPTO_hash_context_finish(), GNUNET_CRYPTO_hash_context_read(), GNUNET_CRYPTO_hash_context_start(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_i2s(), GNUNET_log, GNUNET_NAT_request_reversal(), GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_connect(), GNUNET_NETWORK_socket_create(), GNUNET_new, GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_add_delayed(), GNUNET_SCHEDULER_add_read_net(), GNUNET_SCHEDULER_add_write_net(), GNUNET_SYSERR, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TRANSPORT_CS_OUTBOUND, GNUNET_YES, PendingReversal::in, key, nat, NAT_TIMEOUT, pending_reversal_timeout(), pending_reversals, queue(), queue_map, queue_read_kx(), queue_write(), start_initial_kx_out(), PendingReversal::target, tcp_address_to_sockaddr(), and PendingReversal::timeout_task.

Referenced by init_socket().

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

◆ get_lt_delete_it()

static int get_lt_delete_it ( void *  cls,
const struct GNUNET_HashCode key,
void *  value 
)
static

Iterator over all ListenTasks to clean up.

Parameters
clsNULL
keyunused
valuethe ListenTask to cancel.
Returns
GNUNET_OK to continue to iterate

Definition at line 3509 of file gnunet-communicator-tcp.c.

3512{
3513 struct ListenTask *lt = value;
3514
3515 (void) cls;
3516 (void) key;
3517 if (NULL != lt->listen_task)
3518 {
3520 lt->listen_task = NULL;
3521 }
3522 if (NULL != lt->listen_sock)
3523 {
3525 lt->listen_sock = NULL;
3526 }
3527 GNUNET_free (lt);
3528 return GNUNET_OK;
3529}

References GNUNET_break, GNUNET_free, GNUNET_NETWORK_socket_close(), GNUNET_OK, GNUNET_SCHEDULER_cancel(), key, ListenTask::listen_sock, ListenTask::listen_task, and value.

Referenced by do_shutdown().

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

◆ get_queue_delete_it()

static int get_queue_delete_it ( void *  cls,
const struct GNUNET_HashCode target,
void *  value 
)
static

Iterator over all message queues to clean up.

Parameters
clsNULL
targetunused
valuethe queue to destroy
Returns
GNUNET_OK to continue to iterate

Definition at line 3541 of file gnunet-communicator-tcp.c.

3544{
3545 struct Queue *queue = value;
3546
3547 (void) cls;
3548 (void) target;
3550 return GNUNET_OK;
3551}
struct GNUNET_PeerIdentity target
To whom are we talking to.

References GNUNET_OK, queue(), queue_destroy(), Queue::target, and value.

Referenced by do_shutdown().

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

◆ do_shutdown()

static void do_shutdown ( void *  cls)
static

Shutdown the UNIX communicator.

Parameters
clsNULL (always)

Definition at line 3560 of file gnunet-communicator-tcp.c.

3561{
3563 "Shutdown %s!\n",
3564 shutdown_running ? "running" : "not running");
3565
3567 return;
3568 else
3570
3571 while (NULL != proto_head)
3573 if (NULL != nat)
3574 {
3576 nat = NULL;
3577 }
3585 if (NULL != ch)
3586 {
3589 ch = NULL;
3590 }
3591 if (NULL != stats)
3592 {
3594 stats = NULL;
3595 }
3596 if (NULL != my_private_key)
3597 {
3599 my_private_key = NULL;
3600 }
3601 if (NULL != is)
3602 {
3604 is = NULL;
3605 }
3606 if (NULL != peerstore)
3607 {
3609 peerstore = NULL;
3610 }
3611 if (NULL != resolve_request_handle)
3612 {
3615 }
3617 "Shutdown done!\n");
3618}
static int get_lt_delete_it(void *cls, const struct GNUNET_HashCode *key, void *value)
Iterator over all ListenTasks to clean up.
struct GNUNET_RESOLVER_RequestHandle * resolve_request_handle
Handle for DNS lookup of bindto address.
static int get_queue_delete_it(void *cls, const struct GNUNET_HashCode *target, void *value)
Iterator over all message queues to clean up.
void GNUNET_TRANSPORT_communicator_disconnect(struct GNUNET_TRANSPORT_CommunicatorHandle *ch)
Disconnect from the transport service.
void GNUNET_TRANSPORT_communicator_address_remove_all(struct GNUNET_TRANSPORT_CommunicatorHandle *ch)
Notify transport service that this communicator no longer provides all its addresses for this peer.
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MultiHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
void GNUNET_NAT_unregister(struct GNUNET_NAT_Handle *nh)
Stop port redirection and public IP address detection for the given handle.
Definition: nat_api.c:727
void GNUNET_NT_scanner_done(struct GNUNET_NT_InterfaceScanner *is)
Terminate interface scanner.
Definition: nt.c:426
void GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
Disconnect from the PEERSTORE service.
void GNUNET_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh)
Cancel a request that is still pending with the resolver.
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).

References ch, free_proto_queue(), get_lt_delete_it(), get_queue_delete_it(), GNUNET_CONTAINER_multihashmap_destroy(), GNUNET_CONTAINER_multihashmap_iterate(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_NAT_unregister(), GNUNET_NT_scanner_done(), GNUNET_PEERSTORE_disconnect(), GNUNET_RESOLVER_request_cancel(), GNUNET_STATISTICS_destroy(), GNUNET_TRANSPORT_communicator_address_remove_all(), GNUNET_TRANSPORT_communicator_disconnect(), GNUNET_YES, is, lt_map, my_private_key, nat, peerstore, pending_reversals, pending_reversals_delete_it(), proto_head, queue_map, resolve_request_handle, shutdown_running, and stats.

Referenced by run().

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

◆ enc_notify_cb()

static void enc_notify_cb ( void *  cls,
const struct GNUNET_PeerIdentity sender,
const struct GNUNET_MessageHeader msg 
)
static

Function called when the transport service has received an acknowledgement for this communicator (!) via a different return path.

Not applicable for TCP.

Parameters
clsclosure
senderwhich peer sent the notification
msgpayload

Definition at line 3633 of file gnunet-communicator-tcp.c.

3636{
3637 (void) cls;
3638 (void) sender;
3639 (void) msg;
3640 GNUNET_break_op (0);
3641}

References GNUNET_break_op, and msg.

Referenced by init_socket().

Here is the caller graph for this function:

◆ nat_address_cb()

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

Signature of the callback passed to GNUNET_NAT_register() for a function to call whenever our set of 'valid' addresses changes.

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

Definition at line 3658 of file gnunet-communicator-tcp.c.

3664{
3665 char *my_addr;
3667
3669 "nat address cb %s %s\n",
3670 add_remove ? "add" : "remove",
3671 GNUNET_a2s (addr, addrlen));
3672
3673 if (GNUNET_YES == add_remove)
3674 {
3676
3677 GNUNET_asprintf (&my_addr,
3678 "%s-%s",
3680 GNUNET_a2s (addr, addrlen));
3681 nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
3682 ai =
3684 my_addr,
3685 nt,
3687 GNUNET_free (my_addr);
3688 *app_ctx = ai;
3689 }
3690 else
3691 {
3692 ai = *app_ctx;
3694 *app_ctx = NULL;
3695 }
3696}
static struct GNUNET_TRANSPORT_AddressIdentifier * ai
Handle to the operation that publishes our address.
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
void GNUNET_TRANSPORT_communicator_address_remove(struct GNUNET_TRANSPORT_AddressIdentifier *ai)
Notify transport service about an address that this communicator no longer provides for this peer.
struct GNUNET_TRANSPORT_AddressIdentifier * GNUNET_TRANSPORT_communicator_address_add(struct GNUNET_TRANSPORT_CommunicatorHandle *ch, const char *address, enum GNUNET_NetworkType nt, struct GNUNET_TIME_Relative expiration)
Notify transport service about an address that this communicator provides for this peer.
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
Definition: gnunet_nt_lib.h:44
Internal representation of an address a communicator is currently providing for the transport service...

References ai, ch, COMMUNICATOR_ADDRESS_PREFIX, GNUNET_a2s(), GNUNET_asprintf(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_NT_scanner_get_type(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TRANSPORT_communicator_address_add(), GNUNET_TRANSPORT_communicator_address_remove(), GNUNET_YES, is, and nt.

Referenced by nat_register().

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

◆ add_addr()

static void add_addr ( struct sockaddr *  in,
socklen_t  in_len 
)
static

This method adds addresses to the DLL, that are later register at the NAT service.

Definition at line 3703 of file gnunet-communicator-tcp.c.

3704{
3705
3706 struct Addresses *saddrs;
3707
3709 "add address %s\n",
3710 GNUNET_a2s (in, in_len));
3711
3712 saddrs = GNUNET_new (struct Addresses);
3713 saddrs->addr = in;
3714 saddrs->addr_len = in_len;
3716
3718 "after add address %s\n",
3719 GNUNET_a2s (in, in_len));
3720
3722 "add address %s\n",
3723 GNUNET_a2s (saddrs->addr, saddrs->addr_len));
3724
3725 addrs_lens++;
3726}
static int addrs_lens
Number of addresses in the DLL for register at NAT service.
static struct Addresses * addrs_head
Head of DLL with addresses we like to register at NAT servcie.
static struct Addresses * addrs_tail
Head of DLL with addresses we like to register at NAT servcie.
DLL to store the addresses we like to register at NAT service.
struct sockaddr * addr
Address we like to register at NAT service.
socklen_t addr_len
Length of address we like to register at NAT service.

References Addresses::addr, Addresses::addr_len, addrs_head, addrs_lens, addrs_tail, GNUNET_a2s(), GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, and GNUNET_new.

Referenced by init_socket().

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

◆ init_socket()

static int init_socket ( struct sockaddr *  addr,
socklen_t  in_len 
)
static

This method launch network interactions for each address we like to bind to.

Parameters
addrThe address we will listen to.
in_lenThe length of the address we will listen to.
Returns
GNUNET_SYSERR in case of error. GNUNET_OK in case we are successfully listen to the address.

Definition at line 3737 of file gnunet-communicator-tcp.c.

3739{
3740 struct sockaddr_storage in_sto;
3741 socklen_t sto_len;
3742 struct GNUNET_NETWORK_Handle *listen_sock;
3743 struct ListenTask *lt;
3744 int sockfd;
3745 struct GNUNET_HashCode h_sock;
3746
3747 if (NULL == addr)
3748 {
3750 "Address is NULL.\n");
3751 return GNUNET_SYSERR;
3752 }
3753
3755 "address %s\n",
3756 GNUNET_a2s (addr, in_len));
3757
3758 listen_sock =
3759 GNUNET_NETWORK_socket_create (addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
3760 if (NULL == listen_sock)
3761 {
3763 return GNUNET_SYSERR;
3764 }
3765
3766 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, addr, in_len))
3767 {
3769 GNUNET_NETWORK_socket_close (listen_sock);
3770 listen_sock = NULL;
3771 return GNUNET_SYSERR;
3772 }
3773
3774 if (GNUNET_OK !=
3775 GNUNET_NETWORK_socket_listen (listen_sock,
3776 5))
3777 {
3779 "listen");
3780 GNUNET_NETWORK_socket_close (listen_sock);
3781 listen_sock = NULL;
3782 return GNUNET_SYSERR;
3783 }
3784
3785 /* We might have bound to port 0, allowing the OS to figure it out;
3786 thus, get the real IN-address from the socket */
3787 sto_len = sizeof(in_sto);
3788
3789 if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock),
3790 (struct sockaddr *) &in_sto,
3791 &sto_len))
3792 {
3793 memcpy (&in_sto, addr, in_len);
3794 sto_len = in_len;
3795 }
3796
3797 // addr = (struct sockaddr *) &in_sto;
3798 in_len = sto_len;
3800 "Bound to `%s'\n",
3801 GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
3802 stats = GNUNET_STATISTICS_create ("communicator-tcp", cfg);
3803
3804 if (NULL == is)
3806
3807 if (NULL == my_private_key)
3809 if (NULL == my_private_key)
3810 {
3811 GNUNET_log (
3813 _ (
3814 "Transport service is lacking key configuration settings. Exiting.\n"));
3815 if (NULL != resolve_request_handle)
3818 return GNUNET_SYSERR;
3819 }
3821 /* start listening */
3822
3823 lt = GNUNET_new (struct ListenTask);
3824 lt->listen_sock = listen_sock;
3825
3827 listen_sock,
3828 &listen_cb,
3829 lt);
3830
3832 "creating hash\n");
3833 sockfd = GNUNET_NETWORK_get_fd (lt->listen_sock);
3834 GNUNET_CRYPTO_hash (&sockfd,
3835 sizeof(int),
3836 &h_sock);
3837
3839 "creating map\n");
3840 if (NULL == lt_map)
3842
3844 "creating map entry\n");
3847 &h_sock,
3848 lt,
3850
3852 "map entry created\n");
3853
3854 if (NULL == queue_map)
3856
3857 if (NULL == ch)
3862 &mq_init,
3863 NULL,
3865 NULL);
3866
3867 if (NULL == ch)
3868 {
3869 GNUNET_break (0);
3870 if (NULL != resolve_request_handle)
3873 return GNUNET_SYSERR;
3874 }
3875
3876 add_addr (addr, in_len);
3877 return GNUNET_OK;
3878
3879}
static void enc_notify_cb(void *cls, const struct GNUNET_PeerIdentity *sender, const struct GNUNET_MessageHeader *msg)
Function called when the transport service has received an acknowledgement for this communicator (!...
#define COMMUNICATOR_CONFIG_SECTION
Configuration section used by the communicator.
static int mq_init(void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
Function called by the transport service to initialize a message queue given address information abou...
static void add_addr(struct sockaddr *in, socklen_t in_len)
This method adds addresses to the DLL, that are later register at the NAT service.
struct GNUNET_TRANSPORT_CommunicatorHandle * GNUNET_TRANSPORT_communicator_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *config_section_name, const char *addr_prefix, enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc, GNUNET_TRANSPORT_CommunicatorMqInit mq_init, void *mq_init_cls, GNUNET_TRANSPORT_CommunicatorNotify notify_cb, void *notify_cb_cls)
Connect to the transport service.
@ GNUNET_TRANSPORT_CC_RELIABLE
Transmission is reliabile (with ACKs), e.g.
void GNUNET_CRYPTO_eddsa_key_get_public(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:198
struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_CRYPTO_eddsa_key_create_from_configuration(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a new private key by reading our peer's key from the file specified in the configuration.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Bind a socket to a particular address.
Definition: network.c:439
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, int backlog)
Listen on a socket.
Definition: network.c:651
struct GNUNET_NT_InterfaceScanner * GNUNET_NT_scanner_init(void)
Initialize the address characterization client handle.
Definition: nt.c:405
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:567
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
#define _(String)
GNU gettext support macro.
Definition: platform.h:178

References _, add_addr(), cfg, ch, COMMUNICATOR_ADDRESS_PREFIX, COMMUNICATOR_CONFIG_SECTION, enc_notify_cb(), GNUNET_a2s(), GNUNET_assert, GNUNET_break, GNUNET_CONTAINER_multihashmap_create(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_CRYPTO_eddsa_key_create_from_configuration(), GNUNET_CRYPTO_eddsa_key_get_public(), GNUNET_CRYPTO_hash(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_log_strerror, GNUNET_NETWORK_get_fd(), GNUNET_NETWORK_socket_bind(), GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_create(), GNUNET_NETWORK_socket_listen(), GNUNET_new, GNUNET_NO, GNUNET_NT_scanner_init(), GNUNET_OK, GNUNET_RESOLVER_request_cancel(), GNUNET_SCHEDULER_add_read_net(), GNUNET_SCHEDULER_shutdown(), GNUNET_STATISTICS_create(), GNUNET_SYSERR, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TRANSPORT_CC_RELIABLE, GNUNET_TRANSPORT_communicator_connect(), is, listen_cb(), ListenTask::listen_sock, ListenTask::listen_task, lt_map, mq_init(), my_identity, my_private_key, GNUNET_PeerIdentity::public_key, queue_map, resolve_request_handle, and stats.

Referenced by init_socket_resolv(), and run().

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

◆ nat_register()

static void nat_register ( )
static

This method reads from the DLL addrs_head to register them at the NAT service.

Definition at line 3886 of file gnunet-communicator-tcp.c.

3887{
3888 struct sockaddr **saddrs;
3889 socklen_t *saddr_lens;
3890 int i;
3891 size_t len;
3892
3894 "starting nat register!\n");
3895 len = 0;
3896 i = 0;
3897 saddrs = GNUNET_malloc ((addrs_lens) * sizeof(struct sockaddr *));
3898 saddr_lens = GNUNET_malloc ((addrs_lens) * sizeof(socklen_t));
3899 for (struct Addresses *pos = addrs_head; NULL != pos; pos = pos->next)
3900 {
3902 "registering address %s\n",
3904
3905 saddr_lens[i] = addrs_head->addr_len;
3906 len += saddr_lens[i];
3907 saddrs[i] = GNUNET_memdup (addrs_head->addr, saddr_lens[i]);
3908 i++;
3909 }
3910
3912 "registering addresses %lu %lu %lu %lu\n",
3913 (addrs_lens) * sizeof(struct sockaddr *),
3914 (addrs_lens) * sizeof(socklen_t),
3915 len,
3919 IPPROTO_TCP,
3920 addrs_lens,
3921 (const struct sockaddr **) saddrs,
3922 saddr_lens,
3925 NULL /* closure */);
3926 for (i = addrs_lens - 1; i >= 0; i--)
3927 GNUNET_free (saddrs[i]);
3928 GNUNET_free (saddrs);
3929 GNUNET_free (saddr_lens);
3930
3931 if (NULL == nat)
3932 {
3933 GNUNET_break (0);
3934 if (NULL != resolve_request_handle)
3937 }
3938}
static void try_connection_reversal(void *cls, const struct sockaddr *addr, socklen_t addrlen)
static void nat_address_cb(void *cls, void **app_ctx, int add_remove, enum GNUNET_NAT_AddressClass ac, const struct sockaddr *addr, socklen_t addrlen)
Signature of the callback passed to GNUNET_NAT_register() for a function to call whenever our set of ...
struct GNUNET_NAT_Handle * GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *config_section, uint8_t proto, unsigned int num_addrs, const struct sockaddr **addrs, const socklen_t *addrlens, GNUNET_NAT_AddressCallback address_callback, GNUNET_NAT_ReversalCallback reversal_callback, void *callback_cls)
Attempt to enable port redirection and detect public IP address contacting UPnP or NAT-PMP routers on...
Definition: nat_api.c:366
struct Addresses * next
Kept in a DLL.

References Addresses::addr, Addresses::addr_len, addrs_head, addrs_lens, cfg, COMMUNICATOR_CONFIG_SECTION, GNUNET_a2s(), GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_memdup, GNUNET_NAT_register(), GNUNET_RESOLVER_request_cancel(), GNUNET_SCHEDULER_shutdown(), nat, nat_address_cb(), Addresses::next, resolve_request_handle, and try_connection_reversal().

Referenced by init_socket_resolv(), and run().

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

◆ init_socket_resolv()

static void init_socket_resolv ( void *  cls,
const struct sockaddr *  addr,
socklen_t  in_len 
)
static

This method is the callback called by the resolver API, and wraps method init_socket.

Parameters
clsThe port we will bind to.
addrThe address we will bind to.
in_lenThe length of the address we will bind to.

Definition at line 3949 of file gnunet-communicator-tcp.c.

3952{
3953 struct sockaddr_in *v4;
3954 struct sockaddr_in6 *v6;
3955 struct sockaddr *in;
3956
3957 (void) cls;
3958 if (NULL != addr)
3959 {
3960 if (AF_INET == addr->sa_family)
3961 {
3962 v4 = (struct sockaddr_in *) addr;
3963 in = tcp_address_to_sockaddr_numeric_v4 (&in_len, *v4, bind_port);// _global);
3964 }
3965 else if (AF_INET6 == addr->sa_family)
3966 {
3967 v6 = (struct sockaddr_in6 *) addr;
3968 in = tcp_address_to_sockaddr_numeric_v6 (&in_len, *v6, bind_port);// _global);
3969 }
3970 else
3971 {
3973 "Address family %u not suitable (not AF_INET %u nor AF_INET6 %u \n",
3974 addr->sa_family,
3975 AF_INET,
3976 AF_INET6);
3977 return;
3978 }
3979 init_socket (in, in_len);
3980 }
3981 else
3982 {
3984 "Address is NULL. This might be an error or the resolver finished resolving.\n");
3985 if (NULL == addrs_head)
3986 {
3988 "Resolver finished resolving, but we do not listen to an address!.\n");
3989 return;
3990 }
3991 nat_register ();
3992 }
3993}
static int init_socket(struct sockaddr *addr, socklen_t in_len)
This method launch network interactions for each address we like to bind to.
static void nat_register()
This method reads from the DLL addrs_head to register them at the NAT service.

References addrs_head, bind_port, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, init_socket(), nat_register(), tcp_address_to_sockaddr_numeric_v4(), and tcp_address_to_sockaddr_numeric_v6().

Referenced by run().

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

◆ run()

static void run ( void *  cls,
char *const *  args,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle c 
)
static

Setup communicator and launch network interactions.

Parameters
clsNULL (always)
argsremaining command-line arguments
cfgfilename of the configuration file used (for saving, can be NULL!)
cconfiguration

Definition at line 4005 of file gnunet-communicator-tcp.c.

4009{
4010 char *bindto;
4011 struct sockaddr *in;
4012 socklen_t in_len;
4013 struct sockaddr_in v4;
4014 struct sockaddr_in6 v6;
4015 char *start;
4016 unsigned int port;
4017 char dummy[2];
4018 char *rest = NULL;
4019 struct PortOnlyIpv4Ipv6 *po;
4020 socklen_t addr_len_ipv4;
4021 socklen_t addr_len_ipv6;
4022
4023 (void) cls;
4024
4026 memset (&v4,0,sizeof(struct sockaddr_in));
4027 memset (&v6,0,sizeof(struct sockaddr_in6));
4028 cfg = c;
4029 if (GNUNET_OK !=
4032 "BINDTO",
4033 &bindto))
4034 {
4037 "BINDTO");
4038 return;
4039 }
4040 if (GNUNET_OK !=
4043 "MAX_QUEUE_LENGTH",
4045 {
4047 }
4048 if (GNUNET_OK !=
4051 "REKEY_INTERVAL",
4053 {
4055 }
4056 if (GNUNET_OK !=
4059 "REKEY_MAX_BYTES",
4061 {
4063 }
4065 if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
4066 (GNUNET_YES ==
4069 "DISABLE_V6")))
4070 {
4072 }
4074 if (NULL == peerstore)
4075 {
4076 GNUNET_free (bindto);
4077 GNUNET_break (0);
4079 return;
4080 }
4081
4083
4084 if (1 == sscanf (bindto, "%u%1s", &bind_port, dummy))
4085 {
4089 "address po %s\n",
4091 if (NULL != po->addr_ipv4)
4092 {
4094 }
4095 if (NULL != po->addr_ipv6)
4096 {
4099 }
4100 GNUNET_free (po);
4101 nat_register ();
4102 GNUNET_free (bindto);
4103 return;
4104 }
4105
4106 start = extract_address (bindto);
4107 // FIXME: check for NULL == start...
4108 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
4109 {
4110 bind_port = extract_port (bindto);
4111
4113 init_socket (in, in_len);
4114 nat_register ();
4116 GNUNET_free (bindto);
4117 return;
4118 }
4119
4120 if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
4121 {
4122 bind_port = extract_port (bindto);
4124 init_socket (in, in_len);
4125 nat_register ();
4127 GNUNET_free (bindto);
4128 return;
4129 }
4130
4131 bind_port = extract_port (bindto);
4133 ":",
4134 &rest),
4135 AF_UNSPEC,
4138 &port);
4139
4140 GNUNET_free (bindto);
4142}
static struct PortOnlyIpv4Ipv6 * tcp_address_to_sockaddr_port_only(const char *bindto, unsigned int *port)
Convert TCP bind specification to a struct PortOnlyIpv4Ipv6 *
static void do_shutdown(void *cls)
Shutdown the UNIX communicator.
static void init_socket_resolv(void *cls, const struct sockaddr *addr, socklen_t in_len)
This method is the callback called by the resolver API, and wraps method init_socket.
#define REKEY_MAX_BYTES
How often do we rekey based on number of bytes transmitted? (additionally randomized).
#define DEFAULT_REKEY_INTERVAL
How often do we rekey based on time (at least)
#define DEFAULT_MAX_QUEUE_LENGTH
How many messages do we keep at most in the queue to the transport service before we start to drop (d...
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_CONFIGURATION_get_value_time(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, struct GNUNET_TIME_Relative *time)
Get a configuration value that should be a relative time.
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.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_test_pf(int pf)
Test if the given protocol family is supported by this system.
Definition: network.c:79
struct GNUNET_PEERSTORE_Handle * GNUNET_PEERSTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the PEERSTORE service.
struct GNUNET_RESOLVER_RequestHandle * GNUNET_RESOLVER_ip_get(const char *hostname, int af, struct GNUNET_TIME_Relative timeout, GNUNET_RESOLVER_AddressCallback callback, void *callback_cls)
Convert a string to one or more IP addresses.
Definition: resolver_api.c:940
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1340
#define GNUNET_TIME_UNIT_MINUTES
One minute.

References PortOnlyIpv4Ipv6::addr_ipv4, PortOnlyIpv4Ipv6::addr_ipv6, PortOnlyIpv4Ipv6::addr_len_ipv4, PortOnlyIpv4Ipv6::addr_len_ipv6, bind_port, cfg, COMMUNICATOR_CONFIG_SECTION, DEFAULT_MAX_QUEUE_LENGTH, DEFAULT_REKEY_INTERVAL, disable_v6, do_shutdown(), dummy, extract_address(), extract_port(), GNUNET_a2s(), GNUNET_break, GNUNET_CONFIGURATION_get_value_number(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_get_value_time(), GNUNET_CONFIGURATION_get_value_yesno(), GNUNET_CONTAINER_multihashmap_create(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_log_config_missing(), GNUNET_NETWORK_test_pf(), GNUNET_NO, GNUNET_OK, GNUNET_PEERSTORE_connect(), GNUNET_RESOLVER_ip_get(), GNUNET_SCHEDULER_add_shutdown(), GNUNET_SCHEDULER_shutdown(), GNUNET_TIME_UNIT_MINUTES, GNUNET_YES, init_socket(), init_socket_resolv(), max_queue_length, nat_register(), peerstore, pending_reversals, port, rekey_interval, REKEY_MAX_BYTES, rekey_max_bytes, resolve_request_handle, start, tcp_address_to_sockaddr_numeric_v4(), tcp_address_to_sockaddr_numeric_v6(), and tcp_address_to_sockaddr_port_only().

Referenced by main().

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

◆ main()

int main ( int  argc,
char *const *  argv 
)

The main function for the UNIX communicator.

Parameters
argcnumber of arguments from the command line
argvcommand line arguments
Returns
0 ok, 1 on error

Definition at line 4153 of file gnunet-communicator-tcp.c.

4154{
4155 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
4157 };
4158 int ret;
4159
4161 "Starting tcp communicator\n");
4162 if (GNUNET_OK !=
4163 GNUNET_STRINGS_get_utf8_args (argc, argv,
4164 &argc, &argv))
4165 return 2;
4166
4167 ret = (GNUNET_OK ==
4168 GNUNET_PROGRAM_run (argc,
4169 argv,
4170 "gnunet-communicator-tcp",
4171 _ ("GNUnet TCP communicator"),
4172 options,
4173 &run,
4174 NULL))
4175 ? 0
4176 : 1;
4177 GNUNET_free_nz ((void *) argv);
4178 return ret;
4179}
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Setup communicator and launch network interactions.
#define GNUNET_free_nz(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:400
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1230
Definition of a command line option.

References _, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free_nz, GNUNET_GETOPT_OPTION_END, GNUNET_log, GNUNET_OK, GNUNET_PROGRAM_run(), GNUNET_STRINGS_get_utf8_args(), options, ret, and run().

Here is the call graph for this function:

Variable Documentation

◆ max_queue_length

unsigned long long max_queue_length
static

Maximum queue length before we stop reading towards the transport service.

Definition at line 814 of file gnunet-communicator-tcp.c.

Referenced by queue_read(), and run().

◆ stats

◆ ch

◆ queue_map

struct GNUNET_CONTAINER_MultiHashMap* queue_map
static

Queues (map from peer identity to struct Queue)

Definition at line 829 of file gnunet-communicator-tcp.c.

Referenced by boot_queue(), do_shutdown(), init_socket(), mq_init(), and queue_destroy().

◆ lt_map

struct GNUNET_CONTAINER_MultiHashMap* lt_map
static

ListenTasks (map from socket to struct ListenTask)

Definition at line 834 of file gnunet-communicator-tcp.c.

Referenced by do_shutdown(), init_socket(), and queue_destroy().

◆ my_identity

◆ rekey_max_bytes

unsigned long long rekey_max_bytes
static

The rekey byte maximum.

Definition at line 844 of file gnunet-communicator-tcp.c.

Referenced by run(), and setup_out_cipher().

◆ rekey_interval

struct GNUNET_TIME_Relative rekey_interval
static

The rekey interval.

Definition at line 849 of file gnunet-communicator-tcp.c.

Referenced by run(), and setup_out_cipher().

◆ my_private_key

struct GNUNET_CRYPTO_EddsaPrivateKey* my_private_key
static

Our private key.

Definition at line 854 of file gnunet-communicator-tcp.c.

Referenced by do_shutdown(), init_socket(), inject_rekey(), send_challenge(), setup_in_cipher(), and transmit_kx().

◆ cfg

const struct GNUNET_CONFIGURATION_Handle* cfg
static

Our configuration.

Definition at line 859 of file gnunet-communicator-tcp.c.

Referenced by init_socket(), inject_rekey(), nat_register(), run(), send_challenge(), and transmit_kx().

◆ is

struct GNUNET_NT_InterfaceScanner* is
static

Network scanner to determine network types.

Definition at line 864 of file gnunet-communicator-tcp.c.

Referenced by boot_queue(), do_shutdown(), init_socket(), and nat_address_cb().

◆ nat

struct GNUNET_NAT_Handle* nat
static

Connection to NAT service.

Definition at line 869 of file gnunet-communicator-tcp.c.

Referenced by do_shutdown(), mq_init(), and nat_register().

◆ proto_head

struct ProtoQueue* proto_head
static

Protoqueues DLL head.

Definition at line 874 of file gnunet-communicator-tcp.c.

Referenced by create_proto_queue(), do_shutdown(), free_proto_queue(), and proto_read_kx().

◆ proto_tail

struct ProtoQueue* proto_tail
static

Protoqueues DLL tail.

Definition at line 879 of file gnunet-communicator-tcp.c.

Referenced by create_proto_queue(), free_proto_queue(), and proto_read_kx().

◆ resolve_request_handle

struct GNUNET_RESOLVER_RequestHandle* resolve_request_handle

Handle for DNS lookup of bindto address.

Definition at line 884 of file gnunet-communicator-tcp.c.

Referenced by do_shutdown(), init_socket(), nat_register(), and run().

◆ addrs_head

struct Addresses* addrs_head
static

Head of DLL with addresses we like to register at NAT servcie.

Definition at line 889 of file gnunet-communicator-tcp.c.

Referenced by add_addr(), init_socket_resolv(), and nat_register().

◆ addrs_tail

struct Addresses* addrs_tail
static

Head of DLL with addresses we like to register at NAT servcie.

Definition at line 894 of file gnunet-communicator-tcp.c.

Referenced by add_addr().

◆ addrs_lens

int addrs_lens
static

Number of addresses in the DLL for register at NAT service.

Definition at line 899 of file gnunet-communicator-tcp.c.

Referenced by add_addr(), and nat_register().

◆ peerstore

◆ shutdown_running

int shutdown_running = GNUNET_NO
static

A flag indicating we are already doing a shutdown.

Definition at line 909 of file gnunet-communicator-tcp.c.

Referenced by do_shutdown(), and queue_destroy().

◆ disable_v6

int disable_v6
static

IPv6 disabled.

Definition at line 914 of file gnunet-communicator-tcp.c.

Referenced by mq_init(), run(), and tcp_address_to_sockaddr_port_only().

◆ bind_port

unsigned int bind_port
static

The port the communicator should be assigned to.

Definition at line 919 of file gnunet-communicator-tcp.c.

Referenced by init_socket_resolv(), mq_init(), and run().

◆ pending_reversals

struct GNUNET_CONTAINER_MultiHashMap* pending_reversals
static