GNUnet 0.21.2
gnunet-communicator-tcp.c File Reference

Transport plugin using TCP. More...

#include "platform.h"
#include "gnunet_common.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_elligator (const struct GNUNET_CRYPTO_ElligatorRepresentative *repr, struct Queue *queue)
 Setup cipher of queue for decryption from an elligator representative. 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_ElligatorRepresentative *repr)
 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 service. More...
 
static struct Addressesaddrs_tail
 Head of DLL with addresses we like to register at NAT service. 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 3204 of file gnunet-communicator-tcp.c.

3205{
3206 struct sockaddr_storage in;
3207 socklen_t addrlen;
3208 struct GNUNET_NETWORK_Handle *sock;
3209 struct ListenTask *lt;
3210 struct sockaddr *in_addr;
3211
3213 "listen_cb\n");
3214
3215 lt = cls;
3216
3217 lt->listen_task = NULL;
3218 GNUNET_assert (NULL != lt->listen_sock);
3219 addrlen = sizeof(in);
3220 memset (&in, 0, sizeof(in));
3222 (struct sockaddr*) &in,
3223 &addrlen);
3224 if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno)))
3225 return; /* system limit reached, wait until connection goes down */
3227 lt->listen_sock,
3228 &listen_cb,
3229 lt);
3230 if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno)))
3231 return;
3232 if (NULL == sock)
3233 {
3235 return;
3236 }
3237 in_addr = GNUNET_memdup (&in, addrlen);
3238 create_proto_queue (sock, in_addr, addrlen);
3239}
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 2151 of file gnunet-communicator-tcp.c.

2152{
2153 struct Queue *queue = cls;
2154 struct GNUNET_TIME_Relative left;
2155 ssize_t rcvd;
2156
2157 queue->read_task = NULL;
2158 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2159 &queue->cread_buf[queue->cread_off],
2160 BUF_SIZE - queue->cread_off);
2162 "Received %zd bytes from TCP queue\n", rcvd);
2163 if (-1 == rcvd)
2164 {
2165 if ((EAGAIN != errno) && (EINTR != errno))
2166 {
2169 return;
2170 }
2171 /* try again */
2172 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2173 if (0 != left.rel_value_us)
2174 {
2175 queue->read_task =
2177 return;
2178 }
2180 "Queue %p was idle for %s, disconnecting\n",
2181 queue,
2184 GNUNET_YES));
2186 return;
2187 }
2188 if (0 == rcvd)
2189 {
2190 /* Orderly shutdown of connection */
2192 "Socket for queue %p seems to have been closed\n", queue);
2194 return;
2195 }
2196 queue->timeout =
2198 queue->cread_off += rcvd;
2199 while ((queue->pread_off < sizeof(queue->pread_buf)) &&
2200 (queue->cread_off > 0))
2201 {
2202 size_t max = GNUNET_MIN (sizeof(queue->pread_buf) - queue->pread_off,
2203 queue->cread_off);
2204 size_t done;
2205 size_t total;
2206 size_t old_pread_off = queue->pread_off;
2207
2208 GNUNET_assert (0 ==
2209 gcry_cipher_decrypt (queue->in_cipher,
2210 &queue->pread_buf[queue->pread_off],
2211 max,
2212 queue->cread_buf,
2213 max));
2214 queue->pread_off += max;
2215 total = 0;
2216 while (0 != (done = try_handle_plaintext (queue)))
2217 {
2218 /* 'done' bytes of plaintext were used, shift buffer */
2219 GNUNET_assert (done <= queue->pread_off);
2220 /* NOTE: this memmove() could possibly sometimes be
2221 avoided if we pass 'total' into try_handle_plaintext()
2222 and use it at an offset into the buffer there! */
2223 memmove (queue->pread_buf,
2224 &queue->pread_buf[done],
2225 queue->pread_off - done);
2226 queue->pread_off -= done;
2227 total += done;
2228 /* The last plaintext was a rekey, abort for now */
2229 if (GNUNET_YES == queue->rekeyed)
2230 break;
2231 }
2232 /* when we encounter a rekey message, the decryption above uses the
2233 wrong key for everything after the rekey; in that case, we have
2234 to re-do the decryption at 'total' instead of at 'max'.
2235 However, we have to take into account that the plaintext buffer may have
2236 already contained data and not jumped too far ahead in the ciphertext.
2237 If there is no rekey and the last message is incomplete (max > total),
2238 it is safe to keep the decryption so we shift by 'max' */
2239 if (GNUNET_YES == queue->rekeyed)
2240 {
2241 max = total - old_pread_off;
2242 queue->rekeyed = GNUNET_NO;
2243 queue->pread_off = 0;
2244 }
2245 memmove (queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
2246 queue->cread_off -= max;
2247 }
2248 if (BUF_SIZE == queue->cread_off)
2249 return; /* buffer full, suspend reading */
2250 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2251 if (0 != left.rel_value_us)
2252 {
2253 if (max_queue_length > queue->backpressure)
2254 {
2255 /* continue reading */
2256 queue->read_task =
2258 }
2259 return;
2260 }
2262 "Queue %p was idle for %s, disconnecting\n",
2263 queue,
2266 GNUNET_YES));
2268}
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.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
#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.

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 char ikm[sizeof (*pid) + sizeof (*dh)];
1225 struct GNUNET_ShortHashCode prk;
1226
1227 GNUNET_assert (0 == gcry_cipher_open (cipher,
1228 GCRY_CIPHER_AES256 /* low level: go for speed */
1229 ,
1230 GCRY_CIPHER_MODE_CTR,
1231 0 /* flags */));
1232 memcpy (ikm, pid, sizeof (*pid));
1233 memcpy (ikm + sizeof (*pid), dh, sizeof (*dh));
1235 NULL, 0,
1236 ikm, sizeof (ikm)));
1239 sizeof(key),
1240 &prk,
1241 "gnunet-communicator-tcp-key",
1242 strlen (
1243 "gnunet-communicator-tcp-key"),
1244 pid,
1245 sizeof(*pid),
1246 NULL,
1247 0));
1248 GNUNET_assert (0 == gcry_cipher_setkey (*cipher, key, sizeof(key)));
1251 sizeof(ctr),
1252 &prk,
1253 "gnunet-communicator-tcp-ctr",
1254 strlen (
1255 "gnunet-communicator-tcp-ctr"),
1256 NULL,
1257 0));
1258 gcry_cipher_setctr (*cipher, ctr, sizeof(ctr));
1260 GNUNET_CRYPTO_hkdf_expand (hmac_key,
1261 sizeof(struct GNUNET_HashCode),
1262 &prk,
1263 "gnunet-communicator-hmac",
1264 strlen ("gnunet-communicator-hmac"),
1265 NULL,
1266 0));
1267}
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_hkdf_extract(struct GNUNET_ShortHashCode *prk, const void *salt, size_t salt_len, const void *ikm, size_t ikm_len)
HKDF-Extract using SHA256.
Definition: crypto_hkdf.c:224
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hkdf_expand(void *result, size_t out_len, const struct GNUNET_ShortHashCode *prk,...)
HKDF-Expand using SHA256.
Definition: crypto_hkdf.c:156

References GNUNET_assert, GNUNET_CRYPTO_hkdf_expand(), GNUNET_CRYPTO_hkdf_extract(), GNUNET_YES, key, and pid.

Referenced by setup_in_cipher(), setup_in_cipher_elligator(), 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 1276 of file gnunet-communicator-tcp.c.

1277{
1278 struct Queue *queue = cls;
1279 if (GNUNET_OK != success)
1280 {
1282 "Failed to store rekey monotonic time in PEERSTORE!\n");
1283 }
1284 queue->rekey_monotime_sc = NULL;
1285 GNUNET_PEERSTORE_iteration_next (queue->rekey_monotime_get, 1);
1286}
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 1297 of file gnunet-communicator-tcp.c.

1300{
1301 struct Queue *queue = cls;
1302 struct GNUNET_TIME_AbsoluteNBO *mtbe;
1303 struct GNUNET_TIME_Absolute mt;
1304 const struct GNUNET_PeerIdentity *pid;
1305 struct GNUNET_TIME_AbsoluteNBO *rekey_monotonic_time;
1306
1307 (void) emsg;
1308
1309 rekey_monotonic_time = &queue->rekey_monotonic_time;
1310 pid = &queue->target;
1311 if (NULL == record)
1312 {
1313 queue->rekey_monotime_get = NULL;
1314 return;
1315 }
1316 if (sizeof(*mtbe) != record->value_size)
1317 {
1318 GNUNET_PEERSTORE_iteration_next (queue->rekey_monotime_get, 1);
1319 GNUNET_break (0);
1320 return;
1321 }
1322 mtbe = record->value;
1323 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1324 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
1325 queue->rekey_monotonic_time).abs_value_us)
1326 {
1328 "Queue from %s dropped, rekey monotime in the past\n",
1329 GNUNET_i2s (&queue->target));
1330 GNUNET_break (0);
1331 GNUNET_PEERSTORE_iteration_stop (queue->rekey_monotime_get);
1332 queue->rekey_monotime_get = NULL;
1333 // FIXME: Why should we try to gracefully finish here??
1335 return;
1336 }
1337 queue->rekey_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
1338 "transport_tcp_communicator",
1339 pid,
1341 rekey_monotonic_time,
1342 sizeof(*
1343 rekey_monotonic_time),
1347 queue);
1348}
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_elligator()

static void setup_in_cipher_elligator ( const struct GNUNET_CRYPTO_ElligatorRepresentative repr,
struct Queue queue 
)
static

Setup cipher of queue for decryption from an elligator representative.

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

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

1361{
1362 struct GNUNET_HashCode k;
1363
1365 setup_cipher (&k, &my_identity, &queue->in_cipher, &queue->in_hmac);
1366}
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_elligator_kem_decaps(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_ElligatorRepresentative *r, struct GNUNET_HashCode *key_material)
Carries out ecdh decapsulation with own private key and the representative of the received public key...

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

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:

◆ 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 1376 of file gnunet-communicator-tcp.c.

1378{
1379 struct GNUNET_HashCode k;
1380
1382 setup_cipher (&k, &my_identity, &queue->in_cipher, &queue->in_hmac);
1383}
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:834

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

Referenced by do_rekey().

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 1395 of file gnunet-communicator-tcp.c.

1396{
1397 struct TcpRekeySignature thp;
1398
1400 thp.purpose.size = htonl (sizeof(thp));
1402 "do_rekey size %u\n",
1403 thp.purpose.size);
1404 thp.sender = queue->target;
1406 "sender %s\n",
1407 GNUNET_p2s (&thp.sender.public_key));
1409 "sender %s\n",
1410 GNUNET_p2s (&queue->target.public_key));
1411 thp.receiver = my_identity;
1413 "receiver %s\n",
1414 GNUNET_p2s (&thp.receiver.public_key));
1415 thp.ephemeral = rekey->ephemeral;
1417 "ephemeral %s\n",
1418 GNUNET_e2s (&thp.ephemeral));
1419 thp.monotonic_time = rekey->monotonic_time;
1421 "time %s\n",
1423 GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
1424 GNUNET_assert (ntohl ((&thp)->purpose.size) == sizeof (*(&thp)));
1425 if (GNUNET_OK !=
1428 &thp,
1429 &rekey->sender_sig,
1430 &queue->target.public_key))
1431 {
1432 GNUNET_break (0);
1433 // FIXME Why should we try to gracefully finish here?
1435 return;
1436 }
1437 queue->rekey_monotonic_time = rekey->monotonic_time;
1438 queue->rekey_monotime_get = GNUNET_PEERSTORE_iteration_start (peerstore,
1439 "transport_tcp_communicator",
1440 &queue->target,
1442 &
1444 queue);
1445 gcry_cipher_close (queue->in_cipher);
1446 queue->rekeyed = GNUNET_YES;
1447 setup_in_cipher (&rekey->ephemeral, queue);
1448}
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 1457 of file gnunet-communicator-tcp.c.

1458{
1459 struct Queue *queue = cls;
1460
1461 if (GNUNET_OK != success)
1462 {
1464 "Failed to store handshake ack monotonic time in PEERSTORE!\n");
1465 }
1466 queue->handshake_ack_monotime_sc = NULL;
1467 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
1468}

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 1479 of file gnunet-communicator-tcp.c.

1482{
1483 struct Queue *queue = cls;
1484 struct GNUNET_TIME_AbsoluteNBO *mtbe;
1485 struct GNUNET_TIME_Absolute mt;
1486 const struct GNUNET_PeerIdentity *pid;
1487 struct GNUNET_TIME_AbsoluteNBO *handshake_ack_monotonic_time;
1488
1489 (void) emsg;
1490
1491 handshake_ack_monotonic_time = &queue->handshake_ack_monotonic_time;
1492 pid = &queue->target;
1493 if (NULL == record)
1494 {
1495 queue->handshake_ack_monotime_get = NULL;
1496 return;
1497 }
1498 if (sizeof(*mtbe) != record->value_size)
1499 {
1500 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
1501 GNUNET_break (0);
1502 return;
1503 }
1504 mtbe = record->value;
1505 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1506 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
1507 queue->handshake_ack_monotonic_time).abs_value_us)
1508 {
1510 "Queue from %s dropped, handshake ack monotime in the past\n",
1511 GNUNET_i2s (&queue->target));
1512 GNUNET_break (0);
1513 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
1514 queue->handshake_ack_monotime_get = NULL;
1515 // FIXME: Why should we try to gracefully finish here?
1517 return;
1518 }
1519 queue->handshake_ack_monotime_sc =
1521 "transport_tcp_communicator",
1522 pid,
1524 handshake_ack_monotonic_time,
1525 sizeof(*handshake_ack_monotonic_time),
1529 queue);
1530}
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 1540 of file gnunet-communicator-tcp.c.

1542{
1543 struct TCPConfirmationAck tca;
1544 struct TcpHandshakeAckSignature thas;
1545
1547 "sending challenge\n");
1548
1549 tca.header.type = ntohs (
1551 tca.header.size = ntohs (sizeof(tca));
1552 tca.challenge = challenge;
1553 tca.sender = my_identity;
1554 tca.monotonic_time =
1556 thas.purpose.purpose = htonl (
1558 thas.purpose.size = htonl (sizeof(thas));
1559 thas.sender = my_identity;
1560 thas.receiver = queue->target;
1561 thas.monotonic_time = tca.monotonic_time;
1562 thas.challenge = tca.challenge;
1564 &thas,
1565 &tca.sender_sig);
1566 GNUNET_assert (0 ==
1567 gcry_cipher_encrypt (queue->out_cipher,
1568 &queue->cwrite_buf[queue->cwrite_off],
1569 sizeof(tca),
1570 &tca,
1571 sizeof(tca)));
1572 queue->cwrite_off += sizeof(tca);
1574 "sending challenge done\n");
1575}
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 1585 of file gnunet-communicator-tcp.c.

1586{
1587 setup_cipher (dh, &queue->target, &queue->out_cipher, &queue->out_hmac);
1589 queue->rekey_left_bytes =
1591}
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 1601 of file gnunet-communicator-tcp.c.

1602{
1603 struct TCPRekey rekey;
1604 struct TcpRekeySignature thp;
1605 struct GNUNET_HashCode k;
1606
1607 GNUNET_assert (0 == queue->pwrite_off);
1608 memset (&rekey, 0, sizeof(rekey));
1609 GNUNET_CRYPTO_eddsa_kem_encaps (&queue->target.public_key, &rekey.ephemeral,
1610 &k);
1611 rekey.header.type = ntohs (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY);
1612 rekey.header.size = ntohs (sizeof(rekey));
1613 rekey.monotonic_time =
1615 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY);
1616 thp.purpose.size = htonl (sizeof(thp));
1618 "inject_rekey size %u\n",
1619 thp.purpose.size);
1620 thp.sender = my_identity;
1622 "sender %s\n",
1623 GNUNET_p2s (&thp.sender.public_key));
1624 thp.receiver = queue->target;
1626 "receiver %s\n",
1627 GNUNET_p2s (&thp.receiver.public_key));
1628 thp.ephemeral = rekey.ephemeral;
1630 "ephemeral %s\n",
1631 GNUNET_e2s (&thp.ephemeral));
1632 thp.monotonic_time = rekey.monotonic_time;
1634 "time %s\n",
1636 GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
1638 &thp,
1639 &rekey.sender_sig);
1640 calculate_hmac (&queue->out_hmac, &rekey, sizeof(rekey), &rekey.hmac);
1641 /* Encrypt rekey message with 'old' cipher */
1642 GNUNET_assert (0 ==
1643 gcry_cipher_encrypt (queue->out_cipher,
1644 &queue->cwrite_buf[queue->cwrite_off],
1645 sizeof(rekey),
1646 &rekey,
1647 sizeof(rekey)));
1648 queue->cwrite_off += sizeof(rekey);
1649 /* Setup new cipher for successive messages */
1650 gcry_cipher_close (queue->out_cipher);
1651 setup_out_cipher (queue, &k);
1652}
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:879
#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 1656 of file gnunet-communicator-tcp.c.

1659{
1660 (void) cls;
1661 struct PendingReversal *pending_reversal = value;
1662
1663 if (NULL != pending_reversal->timeout_task)
1664 {
1665 GNUNET_SCHEDULER_cancel (pending_reversal->timeout_task);
1666 pending_reversal->timeout_task = NULL;
1667 }
1670 key,
1671 pending_reversal));
1672 GNUNET_free (pending_reversal->in);
1673 GNUNET_free (pending_reversal);
1674 return GNUNET_OK;
1675}
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 1679 of file gnunet-communicator-tcp.c.

1681{
1682 if (AF_INET == sa_family)
1683 {
1684 struct PendingReversal *pending_reversal;
1685 struct GNUNET_HashCode key;
1686 struct sockaddr_in *natted_address;
1687
1688 natted_address = GNUNET_memdup (in, sizeof (struct sockaddr));
1689 natted_address->sin_port = 0;
1690 GNUNET_CRYPTO_hash (natted_address,
1691 sizeof(struct sockaddr),
1692 &key);
1693
1695 &key);
1696 if (NULL != pending_reversal && (NULL == sender ||
1697 0 != memcmp (sender,
1698 &pending_reversal->target,
1699 sizeof(struct
1701 {
1703 "Removing invalid pending reversal for `%s'at `%s'\n",
1704 GNUNET_i2s (&pending_reversal->target),
1705 GNUNET_a2s (in, sizeof (struct sockaddr)));
1706 pending_reversals_delete_it (NULL, &key, pending_reversal);
1707 }
1708 GNUNET_free (natted_address);
1709 }
1710}
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 1719 of file gnunet-communicator-tcp.c.

1720{
1721 if (NULL != pq->listen_sock)
1722 {
1724 pq->listen_sock = NULL;
1725 }
1726 if (NULL != pq->read_task)
1727 {
1729 pq->read_task = NULL;
1730 }
1731 if (NULL != pq->write_task)
1732 {
1734 pq->write_task = NULL;
1735 }
1736 check_and_remove_pending_reversal (pq->address, pq->address->sa_family, NULL);
1738 GNUNET_free (pq->address);
1740 GNUNET_free (pq);
1741}
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 1751 of file gnunet-communicator-tcp.c.

1752{
1753 struct ProtoQueue *pq = cls;
1754 ssize_t sent;
1755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In proto queue write\n");
1756 pq->write_task = NULL;
1757 if (0 != pq->write_off)
1758 {
1759 sent = GNUNET_NETWORK_socket_send (pq->sock,
1760 pq->write_buf,
1761 pq->write_off);
1763 "Sent %lu bytes to TCP queue\n", sent);
1764 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1765 {
1767 free_proto_queue (pq);
1768 return;
1769 }
1770 if (sent > 0)
1771 {
1772 size_t usent = (size_t) sent;
1773 pq->write_off -= usent;
1774 memmove (pq->write_buf,
1775 &pq->write_buf[usent],
1776 pq->write_off);
1777 }
1778 }
1779 /* do we care to write more? */
1780 if ((0 < pq->write_off))
1781 pq->write_task =
1783 pq->sock,
1785 pq);
1786}
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 1796 of file gnunet-communicator-tcp.c.

1797{
1798 struct Queue *queue = cls;
1799 ssize_t sent;
1800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In queue write\n");
1801 queue->write_task = NULL;
1802 if (0 != queue->cwrite_off)
1803 {
1804 sent = GNUNET_NETWORK_socket_send (queue->sock,
1805 queue->cwrite_buf,
1806 queue->cwrite_off);
1808 "Sent %lu bytes to TCP queue\n", sent);
1809 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1810 {
1813 return;
1814 }
1815 if (sent > 0)
1816 {
1817 size_t usent = (size_t) sent;
1818 queue->cwrite_off -= usent;
1819 memmove (queue->cwrite_buf,
1820 &queue->cwrite_buf[usent],
1821 queue->cwrite_off);
1822 queue->timeout =
1825 }
1826 }
1827 /* can we encrypt more? (always encrypt full messages, needed
1828 such that #mq_cancel() can work!) */
1829 unsigned int we_do_not_need_to_rekey = (0 < queue->rekey_left_bytes
1830 - (queue->cwrite_off
1831 + queue->pwrite_off
1832 + sizeof (struct TCPRekey)));
1833 if (we_do_not_need_to_rekey &&
1834 (queue->pwrite_off > 0) &&
1835 (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE))
1836 {
1838 "Encrypting %lu bytes\n", queue->pwrite_off);
1839 GNUNET_assert (0 ==
1840 gcry_cipher_encrypt (queue->out_cipher,
1841 &queue->cwrite_buf[queue->cwrite_off],
1842 queue->pwrite_off,
1843 queue->pwrite_buf,
1844 queue->pwrite_off));
1845 if (queue->rekey_left_bytes > queue->pwrite_off)
1846 queue->rekey_left_bytes -= queue->pwrite_off;
1847 else
1848 queue->rekey_left_bytes = 0;
1849 queue->cwrite_off += queue->pwrite_off;
1850 queue->pwrite_off = 0;
1851 }
1852 // if ((-1 != unverified_size)&& ((0 == queue->pwrite_off) &&
1853 if (((0 == queue->rekey_left_bytes) ||
1855 queue->rekey_time).rel_value_us)) &&
1856 (((0 == queue->pwrite_off) || ! we_do_not_need_to_rekey) &&
1857 (queue->cwrite_off + sizeof (struct TCPRekey) <= BUF_SIZE)))
1858 {
1860 }
1861 if ((0 == queue->pwrite_off) && (! queue->finishing) &&
1862 (GNUNET_YES == queue->mq_awaits_continue))
1863 {
1864 queue->mq_awaits_continue = GNUNET_NO;
1866 }
1867 /* did we just finish writing 'finish'? */
1868 if ((0 == queue->cwrite_off) && (GNUNET_YES == queue->finishing))
1869 {
1871 "Finishing queue\n");
1873 return;
1874 }
1875 /* do we care to write more? */
1876 if ((0 < queue->cwrite_off) || (0 < queue->pwrite_off))
1877 queue->write_task =
1879 queue->sock,
1880 &queue_write,
1881 queue);
1882}
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:437
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 1893 of file gnunet-communicator-tcp.c.

1894{
1895 const struct GNUNET_MessageHeader *hdr;
1896 const struct TCPConfirmationAck *tca;
1897 const struct TCPBox *box;
1898 const struct TCPRekey *rekey;
1899 const struct TCPFinish *fin;
1900 struct TCPRekey rekeyz;
1901 struct TCPFinish finz;
1902 struct GNUNET_ShortHashCode tmac;
1903 uint16_t type;
1904 size_t size = 0;
1905 struct TcpHandshakeAckSignature thas;
1906 const struct GNUNET_CRYPTO_ChallengeNonceP challenge = queue->challenge;
1907
1909 "try handle plaintext!\n");
1910
1911 hdr = (const struct GNUNET_MessageHeader *) queue->pread_buf;
1912 if ((sizeof(*hdr) > queue->pread_off))
1913 {
1915 "Handling plaintext, not even a header!\n");
1916 return 0; /* not even a header */
1917 }
1918
1919 if ((GNUNET_YES != queue->initial_core_kx_done) && (queue->unverified_size >
1921 {
1923 "Already received data of size %lu bigger than KX size %lu!\n",
1924 queue->unverified_size,
1926 GNUNET_break_op (0);
1928 return 0;
1929 }
1930
1931 type = ntohs (hdr->type);
1932 switch (type)
1933 {
1935 tca = (const struct TCPConfirmationAck *) queue->pread_buf;
1937 "start processing ack\n");
1938 if (sizeof(*tca) > queue->pread_off)
1939 {
1941 "Handling plaintext size of tca greater than pread offset.\n")
1942 ;
1943 return 0;
1944 }
1945 if (ntohs (hdr->size) != sizeof(*tca))
1946 {
1948 "Handling plaintext size does not match message type.\n");
1949 GNUNET_break_op (0);
1951 return 0;
1952 }
1953
1954 thas.purpose.purpose = htonl (
1956 thas.purpose.size = htonl (sizeof(thas));
1957 thas.sender = tca->sender;
1958 thas.receiver = my_identity;
1959 thas.monotonic_time = tca->monotonic_time;
1960 thas.challenge = tca->challenge;
1961
1964 &thas,
1965 &tca->sender_sig,
1966 &tca->sender.public_key))
1967 {
1969 "Verification of signature failed!\n");
1970 GNUNET_break (0);
1972 return 0;
1973 }
1974 if (0 != GNUNET_memcmp (&tca->challenge, &challenge))
1975 {
1977 "Challenge in TCPConfirmationAck not correct!\n");
1978 GNUNET_break (0);
1980 return 0;
1981 }
1982
1983 queue->handshake_ack_monotime_get = GNUNET_PEERSTORE_iteration_start (
1984 peerstore,
1985 "transport_tcp_communicator",
1986 &queue->target,
1989 queue);
1990
1992 "Handling plaintext, ack processed!\n");
1993
1995 {
1996 send_challenge (queue->challenge_received, queue);
1997 queue->write_task =
1999 queue->sock,
2000 &queue_write,
2001 queue);
2002 }
2003 else if (GNUNET_TRANSPORT_CS_OUTBOUND == queue->cs)
2004 {
2006 queue->address->sa_family, NULL);
2007 }
2008
2013 queue->initial_core_kx_done = GNUNET_YES;
2014
2015 char *foreign_addr;
2016
2017 switch (queue->address->sa_family)
2018 {
2019 case AF_INET:
2020 GNUNET_asprintf (&foreign_addr,
2021 "%s-%s",
2023 GNUNET_a2s (queue->address, queue->address_len));
2024 break;
2025
2026 case AF_INET6:
2027 GNUNET_asprintf (&foreign_addr,
2028 "%s-%s",
2030 GNUNET_a2s (queue->address, queue->address_len));
2031 break;
2032
2033 default:
2034 GNUNET_assert (0);
2035 }
2036
2038 &queue->target,
2039 foreign_addr,
2040 UINT16_MAX, /* no MTU */
2042 0, /* Priority */
2043 queue->nt,
2044 queue->cs,
2045 queue->mq);
2046
2047 GNUNET_free (foreign_addr);
2048
2049 size = ntohs (hdr->size);
2050 break;
2052 /* Special case: header size excludes box itself! */
2053 box = (const struct TCPBox *) queue->pread_buf;
2054 if (ntohs (hdr->size) + sizeof(struct TCPBox) > queue->pread_off)
2055 return 0;
2056 calculate_hmac (&queue->in_hmac, &box[1], ntohs (hdr->size), &tmac);
2057 if (0 != memcmp (&tmac, &box->hmac, sizeof(tmac)))
2058 {
2059 GNUNET_break_op (0);
2061 return 0;
2062 }
2063 pass_plaintext_to_core (queue, (const void *) &box[1], ntohs (hdr->size));
2064 size = ntohs (hdr->size) + sizeof(*box);
2066 "Handling plaintext, box processed!\n");
2068 "# bytes decrypted with BOX",
2069 size,
2070 GNUNET_NO);
2072 "# messages decrypted with BOX",
2073 1,
2074 GNUNET_NO);
2075 break;
2076
2078 rekey = (const struct TCPRekey *) queue->pread_buf;
2079 if (sizeof(*rekey) > queue->pread_off)
2080 return 0;
2081 if (ntohs (hdr->size) != sizeof(*rekey))
2082 {
2083 GNUNET_break_op (0);
2085 return 0;
2086 }
2087 rekeyz = *rekey;
2088 memset (&rekeyz.hmac, 0, sizeof(rekeyz.hmac));
2089 calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
2090 if (0 != memcmp (&tmac, &rekey->hmac, sizeof(tmac)))
2091 {
2092 GNUNET_break_op (0);
2094 return 0;
2095 }
2096 do_rekey (queue, rekey);
2097 size = ntohs (hdr->size);
2099 "Handling plaintext, rekey processed!\n");
2101 "# rekeying successful",
2102 1,
2103 GNUNET_NO);
2104 break;
2105
2107 fin = (const struct TCPFinish *) queue->pread_buf;
2108 if (sizeof(*fin) > queue->pread_off)
2109 return 0;
2110 if (ntohs (hdr->size) != sizeof(*fin))
2111 {
2112 GNUNET_break_op (0);
2114 return 0;
2115 }
2116 finz = *fin;
2117 memset (&finz.hmac, 0, sizeof(finz.hmac));
2118 calculate_hmac (&queue->in_hmac, &finz, sizeof(finz), &tmac);
2119 if (0 != memcmp (&tmac, &fin->hmac, sizeof(tmac)))
2120 {
2121 GNUNET_break_op (0);
2123 return 0;
2124 }
2125 /* handle FINISH by destroying queue */
2128 "Handling plaintext, finish processed!\n");
2129 break;
2130
2131 default:
2133 "Handling plaintext, nothing processed!\n");
2134 GNUNET_break_op (0);
2136 return 0;
2137 }
2138 GNUNET_assert (0 != size);
2139 if (-1 != queue->unverified_size)
2140 queue->unverified_size += size;
2141 return size;
2142}
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)
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
#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.
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 2279 of file gnunet-communicator-tcp.c.

2282{
2283 struct sockaddr *in;
2284
2285 v6.sin6_family = AF_INET6;
2286 v6.sin6_port = htons ((uint16_t) port);
2287#if HAVE_SOCKADDR_IN_SIN_LEN
2288 v6.sin6_len = sizeof(struct sockaddr_in6);
2289#endif
2290 v6.sin6_flowinfo = 0;
2291 v6.sin6_scope_id = 0;
2292 in = GNUNET_memdup (&v6, sizeof(v6));
2293 *sock_len = sizeof(struct sockaddr_in6);
2294
2295 return in;
2296}
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 2307 of file gnunet-communicator-tcp.c.

2310{
2311 struct sockaddr *in;
2312
2313 v4.sin_family = AF_INET;
2314 v4.sin_port = htons ((uint16_t) port);
2315#if HAVE_SOCKADDR_IN_SIN_LEN
2316 v4.sin_len = sizeof(struct sockaddr_in);
2317#endif
2318 in = GNUNET_memdup (&v4, sizeof(v4));
2319 *sock_len = sizeof(struct sockaddr_in);
2320 return in;
2321}

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 2331 of file gnunet-communicator-tcp.c.

2332{
2333 struct PortOnlyIpv4Ipv6 *po;
2334 struct sockaddr_in *i4;
2335 struct sockaddr_in6 *i6;
2336 socklen_t sock_len_ipv4;
2337 socklen_t sock_len_ipv6;
2338
2339 /* interpreting value as just a PORT number */
2340 if (*port > UINT16_MAX)
2341 {
2343 "BINDTO specification `%s' invalid: value too large for port\n",
2344 bindto);
2345 return NULL;
2346 }
2347
2348 po = GNUNET_new (struct PortOnlyIpv4Ipv6);
2349
2350 if (GNUNET_YES == disable_v6)
2351 {
2352 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2353 po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2354 *port);
2355 po->addr_len_ipv4 = sock_len_ipv4;
2356 }
2357 else
2358 {
2359
2360 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2361 po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2362 *port);
2363 po->addr_len_ipv4 = sock_len_ipv4;
2364
2365 i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
2366 po->addr_ipv6 = tcp_address_to_sockaddr_numeric_v6 (&sock_len_ipv6, *i6,
2367 *port);
2368
2369 po->addr_len_ipv6 = sock_len_ipv6;
2370
2371 GNUNET_free (i6);
2372 }
2373
2374 GNUNET_free (i4);
2375
2376 return po;
2377}
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 2387 of file gnunet-communicator-tcp.c.

2388{
2389 char *addr;
2390 char *start;
2391 char *token;
2392 char *cp;
2393 char *rest = NULL;
2394
2396 "extract address with bindto %s\n",
2397 bindto);
2398
2399 if (NULL == bindto)
2401 "bindto is NULL\n");
2402
2403 cp = GNUNET_strdup (bindto);
2404
2406 "extract address 2\n");
2407
2408 start = cp;
2409 if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
2410 {
2411 start++; /* skip over '['*/
2412 cp[strlen (cp) - 1] = '\0'; /* eat ']'*/
2413 addr = GNUNET_strdup (start);
2414 }
2415 else
2416 {
2417 token = strtok_r (cp, "]", &rest);
2418 if (strlen (bindto) == strlen (token))
2419 {
2420 token = strtok_r (cp, ":", &rest);
2421 addr = GNUNET_strdup (token);
2422 }
2423 else
2424 {
2425 token++;
2426 addr = GNUNET_strdup (token);
2427 }
2428 }
2429
2431 "tcp address: %s\n",
2432 addr);
2433 GNUNET_free (cp);
2434 return addr;
2435}
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.

References GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_strdup, 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 2445 of file gnunet-communicator-tcp.c.

2446{
2447 unsigned int port;
2448 char dummy[2];
2449 char *token;
2450 char *addr;
2451 char *colon;
2452 char *cp;
2453 char *rest = NULL;
2454
2455 if (NULL != addr_and_port)
2456 {
2457 cp = GNUNET_strdup (addr_and_port);
2458 token = strtok_r (cp, "]", &rest);
2459 if (strlen (addr_and_port) == strlen (token))
2460 {
2461 colon = strrchr (cp, ':');
2462 if (NULL == colon)
2463 {
2464 GNUNET_free (cp);
2465 return 0;
2466 }
2467 addr = colon;
2468 addr++;
2469 }
2470 else
2471 {
2472 token = strtok_r (NULL, "]", &rest);
2473 if (NULL == token)
2474 {
2475 GNUNET_free (cp);
2476 return 0;
2477 }
2478 else
2479 {
2480 addr = token;
2481 addr++;
2482 }
2483 }
2484
2485
2486 if (1 == sscanf (addr, "%u%1s", &port, dummy))
2487 {
2488 /* interpreting value as just a PORT number */
2489 if (port > UINT16_MAX)
2490 {
2492 "Port `%u' invalid: value too large for port\n",
2493 port);
2494 GNUNET_free (cp);
2495 return 0;
2496 }
2497 }
2498 else
2499 {
2501 "BINDTO specification invalid: last ':' not followed by number\n");
2502 GNUNET_free (cp);
2503 return 0;
2504 }
2505 GNUNET_free (cp);
2506 }
2507 else
2508 {
2510 "return 0\n");
2511 /* interpret missing port as 0, aka pick any free one */
2512 port = 0;
2513 }
2514
2515 return port;
2516}
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 2527 of file gnunet-communicator-tcp.c.

2528{
2529 struct sockaddr *in;
2530 unsigned int port;
2531 struct sockaddr_in v4;
2532 struct sockaddr_in6 v6;
2533 char *start;
2534
2535 memset (&v4, 0, sizeof(v4));
2536 start = extract_address (bindto);
2537 GNUNET_assert (NULL != start);
2539 "start %s\n",
2540 start);
2541
2543 "!bindto %s\n",
2544 bindto);
2545
2546
2547 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
2548 {
2549 port = extract_port (bindto);
2550
2552 "port %u\n",
2553 port);
2554
2555 in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port);
2556 }
2557 else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
2558 {
2559 port = extract_port (bindto);
2560 in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port);
2561 }
2562 else
2563 {
2564 GNUNET_assert (0);
2565 }
2566
2568 return in;
2569}
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 2581 of file gnunet-communicator-tcp.c.

2584{
2585 struct Queue *queue = impl_state;
2586 uint16_t msize = ntohs (msg->size);
2587 struct TCPBox box;
2589 "In MQ send. Queue finishing: %s; write task running: %s\n",
2590 (GNUNET_YES == queue->finishing) ? "yes" : "no",
2591 (NULL == queue->write_task) ? "yes" : "no");
2592 GNUNET_assert (mq == queue->mq);
2593 queue->mq_awaits_continue = GNUNET_YES;
2594 if (GNUNET_YES == queue->finishing)
2595 return; /* this queue is dying, drop msg */
2596 GNUNET_assert (0 == queue->pwrite_off);
2597 box.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX);
2598 box.header.size = htons (msize);
2599 calculate_hmac (&queue->out_hmac, msg, msize, &box.hmac);
2600 memcpy (&queue->pwrite_buf[queue->pwrite_off], &box, sizeof(box));
2601 queue->pwrite_off += sizeof(box);
2602 memcpy (&queue->pwrite_buf[queue->pwrite_off], msg, msize);
2603 queue->pwrite_off += msize;
2605 "%lu bytes of plaintext to send\n", queue->pwrite_off);
2606 GNUNET_assert (NULL != queue->sock);
2607 if (NULL == queue->write_task)
2608 queue->write_task =
2610 queue->sock,
2611 &queue_write,
2612 queue);
2613}
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 2625 of file gnunet-communicator-tcp.c.

2626{
2627 struct Queue *queue = impl_state;
2628
2629 if (mq == queue->mq)
2630 {
2631 queue->mq = NULL;
2633 }
2634}

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 2644 of file gnunet-communicator-tcp.c.

2645{
2646 struct Queue *queue = impl_state;
2647
2648 GNUNET_assert (0 != queue->pwrite_off);
2649 queue->pwrite_off = 0;
2650}

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 2663 of file gnunet-communicator-tcp.c.

2664{
2665 struct Queue *queue = cls;
2666
2668 "MQ error in queue to %s: %d\n",
2669 GNUNET_i2s (&queue->target),
2670 (int) error);
2672}

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 2683 of file gnunet-communicator-tcp.c.

2684{
2685 queue->nt =
2686 GNUNET_NT_scanner_get_type (is, queue->address, queue->address_len);
2688 queue_map,
2689 &queue->key,
2690 queue,
2693 "# queues active",
2695 GNUNET_NO);
2696 queue->timeout =
2699 &mq_destroy,
2700 &mq_cancel,
2701 queue,
2702 NULL,
2703 &mq_error,
2704 queue);
2705}
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:482
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_ElligatorRepresentative repr 
)
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 2719 of file gnunet-communicator-tcp.c.

2721{
2722 struct TcpHandshakeSignature ths;
2723 struct TCPConfirmation tc;
2724
2725 memcpy (queue->cwrite_buf, repr, sizeof(*repr));
2726 queue->cwrite_off = sizeof(*repr);
2727 /* compute 'tc' and append in encrypted format to cwrite_buf */
2728 tc.sender = my_identity;
2729 tc.monotonic_time =
2732 &tc.challenge,
2733 sizeof(tc.challenge));
2734 ths.purpose.purpose = htonl (
2736 ths.purpose.size = htonl (sizeof(ths));
2737 ths.sender = my_identity;
2738 ths.receiver = queue->target;
2739 ths.ephemeral = *repr;
2740 ths.monotonic_time = tc.monotonic_time;
2741 ths.challenge = tc.challenge;
2743 &ths,
2744 &tc.sender_sig);
2745 GNUNET_assert (0 ==
2746 gcry_cipher_encrypt (queue->out_cipher,
2747 &queue->cwrite_buf[queue->cwrite_off],
2748 sizeof(tc),
2749 &tc,
2750 sizeof(tc)));
2751 queue->challenge = tc.challenge;
2752 queue->cwrite_off += sizeof(tc);
2753
2755 "handshake written\n");
2756}
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 2767 of file gnunet-communicator-tcp.c.

2768{
2770 struct GNUNET_HashCode k;
2771
2773 &repr, &k);
2774 setup_out_cipher (queue, &k);
2775 transmit_kx (queue, &repr);
2776}
static void transmit_kx(struct Queue *queue, const struct GNUNET_CRYPTO_ElligatorRepresentative *repr)
Generate and transmit our ephemeral key and the signature for the initial KX with the other peer.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_elligator_kem_encaps(const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_CRYPTO_ElligatorRepresentative *r, struct GNUNET_HashCode *key_material)
Carries out ecdh encapsulation with given public key and the private key from a freshly created ephem...
Elligator representative (always for Curve25519)

References GNUNET_CRYPTO_eddsa_elligator_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 2785 of file gnunet-communicator-tcp.c.

2786{
2787 struct Queue *queue = cls;
2788 if (GNUNET_OK != success)
2789 {
2791 "Failed to store handshake monotonic time in PEERSTORE!\n");
2792 }
2793 queue->handshake_monotime_sc = NULL;
2794 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
2795}

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 2806 of file gnunet-communicator-tcp.c.

2809{
2810 struct Queue *queue = cls;
2811 struct GNUNET_TIME_AbsoluteNBO *mtbe;
2812 struct GNUNET_TIME_Absolute mt;
2813 const struct GNUNET_PeerIdentity *pid;
2814 struct GNUNET_TIME_AbsoluteNBO *handshake_monotonic_time;
2815
2816 (void) emsg;
2817
2818 handshake_monotonic_time = &queue->handshake_monotonic_time;
2819 pid = &queue->target;
2821 "tcp handshake with us %s\n",
2823 if (NULL == record)
2824 {
2825 queue->handshake_monotime_get = NULL;
2826 return;
2827 }
2829 "tcp handshake from peer %s\n",
2830 GNUNET_i2s (pid));
2831 if (sizeof(*mtbe) != record->value_size)
2832 {
2833 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
2834 GNUNET_break (0);
2835 return;
2836 }
2837 mtbe = record->value;
2838 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
2839 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
2840 queue->handshake_monotonic_time).abs_value_us)
2841 {
2843 "Queue from %s dropped, handshake monotime in the past\n",
2844 GNUNET_i2s (&queue->target));
2845 GNUNET_break (0);
2846 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
2847 queue->handshake_ack_monotime_get = NULL;
2849 return;
2850 }
2851 queue->handshake_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
2852 "transport_tcp_communicator",
2853 pid,
2855 handshake_monotonic_time,
2856 sizeof(*
2857 handshake_monotonic_time),
2860 &
2862 queue);
2863}
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 2878 of file gnunet-communicator-tcp.c.

2881{
2882 struct TcpHandshakeSignature ths;
2884
2886 0 ==
2887 gcry_cipher_decrypt (queue->in_cipher,
2888 tc,
2889 sizeof(*tc),
2890 &ibuf[sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)],
2891 sizeof(*tc)));
2892 ths.purpose.purpose = htonl (
2894 ths.purpose.size = htonl (sizeof(ths));
2895 ths.sender = tc->sender;
2896 ths.receiver = my_identity;
2897 memcpy (&ths.ephemeral, ibuf, sizeof(struct GNUNET_CRYPTO_EcdhePublicKey));
2898 ths.monotonic_time = tc->monotonic_time;
2899 ths.challenge = tc->challenge;
2902 &ths,
2903 &tc->sender_sig,
2904 &tc->sender.public_key);
2905 if (GNUNET_YES == ret)
2906 queue->handshake_monotime_get =
2908 "transport_tcp_communicator",
2909 &queue->target,
2912 queue);
2913 return ret;
2914}
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 2925 of file gnunet-communicator-tcp.c.

2926{
2927 struct Queue *queue = cls;
2928 ssize_t rcvd;
2929 struct GNUNET_TIME_Relative left;
2930 struct TCPConfirmation tc;
2931
2932 queue->read_task = NULL;
2933 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2934 if (0 == left.rel_value_us)
2935 {
2937 return;
2938 }
2939 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2940 &queue->cread_buf[queue->cread_off],
2941 BUF_SIZE - queue->cread_off);
2943 "Received %lu bytes to write in buffer of size %lu for KX from queue %p (expires in %"
2944 PRIu64 ")\n",
2945 rcvd, BUF_SIZE - queue->cread_off, queue, left.rel_value_us);
2946 if (-1 == rcvd)
2947 {
2948 if ((EAGAIN != errno) && (EINTR != errno))
2949 {
2952 return;
2953 }
2954 queue->read_task =
2956 return;
2957 }
2958 if (0 == rcvd)
2959 {
2960 /* Orderly shutdown of connection */
2962 "Socket for queue %p seems to have been closed\n", queue);
2964 return;
2965 }
2966 queue->cread_off += rcvd;
2967 if (queue->cread_off < INITIAL_KX_SIZE)
2968 {
2969 /* read more */
2971 "%lu/%lu bytes of KX read. Rescheduling...\n",
2972 queue->cread_off, INITIAL_KX_SIZE);
2973 queue->read_task =
2975 return;
2976 }
2977 /* we got all the data, let's find out who we are talking to! */
2979 *)
2980 queue->cread_buf,
2981 queue);
2982 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, queue->cread_buf))
2983 {
2985 "Invalid TCP KX received from %s\n",
2986 GNUNET_a2s (queue->address, queue->address_len));
2988 return;
2989 }
2990 if (0 !=
2991 memcmp (&tc.sender, &queue->target, sizeof(struct GNUNET_PeerIdentity)))
2992 {
2994 "Invalid sender in TCP KX received from %s\n",
2995 GNUNET_a2s (queue->address, queue->address_len));
2997 return;
2998 }
2999 send_challenge (tc.challenge, queue);
3000 queue->write_task =
3002 queue->sock,
3003 &queue_write,
3004 queue);
3005
3006 /* update queue timeout */
3007 queue->timeout =
3009 /* prepare to continue with regular read task immediately */
3010 memmove (queue->cread_buf,
3011 &queue->cread_buf[INITIAL_KX_SIZE],
3012 queue->cread_off - (INITIAL_KX_SIZE));
3014 "cread_off is %lu bytes before adjusting\n",
3015 queue->cread_off);
3016 queue->cread_off -= INITIAL_KX_SIZE;
3018 "cread_off set to %lu bytes\n",
3019 queue->cread_off);
3021}
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 void setup_in_cipher_elligator(const struct GNUNET_CRYPTO_ElligatorRepresentative *repr, struct Queue *queue)
Setup cipher of queue for decryption from an elligator representative.
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_elligator(), 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 3031 of file gnunet-communicator-tcp.c.

3032{
3033 struct ProtoQueue *pq = cls;
3034 ssize_t rcvd;
3035 struct GNUNET_TIME_Relative left;
3036 struct Queue *queue;
3037 struct TCPConfirmation tc;
3039
3040 pq->read_task = NULL;
3042 if (0 == left.rel_value_us)
3043 {
3044 free_proto_queue (pq);
3045 return;
3046 }
3047 rcvd = GNUNET_NETWORK_socket_recv (pq->sock,
3048 &pq->ibuf[pq->ibuf_off],
3049 sizeof(pq->ibuf) - pq->ibuf_off);
3051 "Proto received %lu bytes for KX\n", rcvd);
3052 if (-1 == rcvd)
3053 {
3054 if ((EAGAIN != errno) && (EINTR != errno))
3055 {
3057 free_proto_queue (pq);
3058 return;
3059 }
3060 /* try again */
3061 pq->read_task =
3063 return;
3064 }
3065 if (0 == rcvd)
3066 {
3067 /* Orderly shutdown of connection */
3069 "Socket for proto queue %p seems to have been closed\n", pq);
3070 free_proto_queue (pq);
3071 return;
3072 }
3073 pq->ibuf_off += rcvd;
3074 if (sizeof (struct TCPNATProbeMessage) == pq->ibuf_off)
3075 {
3076 struct TCPNATProbeMessage *pm = (struct TCPNATProbeMessage *) pq->ibuf;
3077
3079 &pm->clientIdentity);
3080
3081 queue = GNUNET_new (struct Queue);
3082 queue->target = pm->clientIdentity;
3085 }
3086 else if (pq->ibuf_off > sizeof(pq->ibuf))
3087 {
3088 /* read more */
3089 pq->read_task =
3091 return;
3092 }
3093 else
3094 {
3095 /* we got all the data, let's find out who we are talking to! */
3096 queue = GNUNET_new (struct Queue);
3097 setup_in_cipher_elligator ((const struct
3099 ibuf,
3100 queue);
3101 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, pq->ibuf))
3102 {
3104 "Invalid TCP KX received from %s\n",
3105 GNUNET_a2s (pq->address, pq->address_len));
3106 gcry_cipher_close (queue->in_cipher);
3108 free_proto_queue (pq);
3109 return;
3110 }
3111 queue->target = tc.sender;
3114 }
3115 queue->address = pq->address; /* steals reference */
3116 queue->address_len = pq->address_len;
3117 queue->listen_sock = pq->listen_sock;
3118 queue->sock = pq->sock;
3119
3121 "created queue with target %s\n",
3122 GNUNET_i2s (&queue->target));
3123
3125 "start kx proto\n");
3126
3128 boot_queue (queue);
3129 queue->read_task =
3131 queue->sock,
3132 read_task,
3133 queue);
3134 queue->write_task =
3136 queue->sock,
3137 &queue_write,
3138 queue);
3139 // TODO To early! Move it somewhere else.
3140 // send_challenge (tc.challenge, queue);
3141 queue->challenge_received = tc.challenge;
3142
3144 GNUNET_free (pq);
3145}
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_elligator(), 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 3149 of file gnunet-communicator-tcp.c.

3152{
3153 struct ProtoQueue *pq = GNUNET_new (struct ProtoQueue);
3154
3155 if (NULL == sock)
3156 {
3157 // sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
3158 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, 0);
3159 if (NULL == sock)
3160 {
3162 "socket(%d) failed: %s",
3163 in->sa_family,
3164 strerror (errno));
3165 GNUNET_free (in);
3166 GNUNET_free (pq);
3167 return NULL;
3168 }
3169 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, addrlen)) &&
3170 (errno != EINPROGRESS))
3171 {
3173 "connect to `%s' failed: %s",
3174 GNUNET_a2s (in, addrlen),
3175 strerror (errno));
3177 GNUNET_free (in);
3178 GNUNET_free (pq);
3179 return NULL;
3180 }
3181 }
3182 pq->address_len = addrlen;
3183 pq->address = in;
3185 pq->sock = sock;
3187 pq->sock,
3189 pq);
3191
3192 return pq;
3193}
#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 3243 of file gnunet-communicator-tcp.c.

3246{
3247 (void) cls;
3248 struct TCPNATProbeMessage pm;
3249 struct ProtoQueue *pq;
3250 struct sockaddr *in_addr;
3251
3253 "addr->sa_family %d\n",
3254 addr->sa_family);
3256 "Try to connect back\n");
3257 in_addr = GNUNET_memdup (addr, addrlen);
3259 "in_addr->sa_family %d\n",
3260 in_addr->sa_family);
3261 pq = create_proto_queue (NULL, in_addr, addrlen);
3262 if (NULL != pq)
3263 {
3264 pm.header.size = htons (sizeof(struct TCPNATProbeMessage));
3265 pm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
3266 pm.clientIdentity = my_identity;
3267 memcpy (pq->write_buf, &pm, sizeof(struct TCPNATProbeMessage));
3268 pq->write_off = sizeof(struct TCPNATProbeMessage);
3270 pq->sock,
3272 pq);
3273 }
3274 else
3275 {
3277 "Couldn't create ProtoQueue for sending TCPNATProbeMessage\n");
3278 }
3279}
#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 3283 of file gnunet-communicator-tcp.c.

3284{
3285 struct sockaddr *in = cls;
3286 struct PendingReversal *pending_reversal;
3287 struct GNUNET_HashCode key;
3288
3290 sizeof(struct sockaddr),
3291 &key);
3293 &key);
3294
3295 GNUNET_assert (NULL != pending_reversal);
3296
3298 &key,
3299 pending_reversal))
3301 "No pending reversal found for address %s\n",
3302 GNUNET_a2s (in, sizeof (struct sockaddr)));
3303 GNUNET_free (pending_reversal->in);
3304 GNUNET_free (pending_reversal);
3305}

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 3327 of file gnunet-communicator-tcp.c.

3328{
3329 struct sockaddr *in;
3330 socklen_t in_len = 0;
3331 const char *path;
3332 struct sockaddr_in *v4;
3333 struct sockaddr_in6 *v6;
3334 unsigned int is_natd = GNUNET_NO;
3335 struct GNUNET_HashCode key;
3336 struct GNUNET_HashCode queue_map_key;
3337 struct GNUNET_HashContext *hsh;
3338 struct Queue *queue;
3339
3341 "Connecting to %s at %s\n",
3342 GNUNET_i2s (peer),
3343 address);
3344 if (0 != strncmp (address,
3346 strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
3347 {
3348 GNUNET_break_op (0);
3349 return GNUNET_SYSERR;
3350 }
3351 path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
3352 in = tcp_address_to_sockaddr (path, &in_len);
3353
3354 if (NULL == in)
3355 {
3357 "Failed to setup TCP socket address\n");
3358 return GNUNET_SYSERR;
3359 }
3360
3362 "in %s\n",
3363 GNUNET_a2s (in, in_len));
3364
3367 GNUNET_CRYPTO_hash_context_read (hsh, peer, sizeof (*peer));
3368 GNUNET_CRYPTO_hash_context_finish (hsh, &queue_map_key);
3370
3371 if (NULL != queue)
3372 {
3374 "Queue for %s already exists or is in construction\n", address);
3375 GNUNET_free (in);
3376 return GNUNET_NO;
3377 }
3378 switch (in->sa_family)
3379 {
3380 case AF_INET:
3381 v4 = (struct sockaddr_in *) in;
3382 if (0 == v4->sin_port)
3383 {
3384 is_natd = GNUNET_YES;
3386 sizeof(struct sockaddr),
3387 &key);
3390 &key))
3391 {
3393 "There is already a request reversal for `%s'at `%s'\n",
3394 GNUNET_i2s (peer),
3395 address);
3396 GNUNET_free (in);
3397 return GNUNET_SYSERR;
3398 }
3399 }
3400 break;
3401
3402 case AF_INET6:
3403 if (GNUNET_YES == disable_v6)
3404 {
3406 "IPv6 disabled, skipping %s\n", address);
3407 GNUNET_free (in);
3408 return GNUNET_SYSERR;
3409 }
3410 v6 = (struct sockaddr_in6 *) in;
3411 if (0 == v6->sin6_port)
3412 {
3414 "Request reversal for `%s' at `%s' not possible for an IPv6 address\n",
3415 GNUNET_i2s (peer),
3416 address);
3417 GNUNET_free (in);
3418 return GNUNET_SYSERR;
3419 }
3420 break;
3421
3422 default:
3423 GNUNET_assert (0);
3424 }
3425
3426 if (GNUNET_YES == is_natd)
3427 {
3428 struct sockaddr_in local_sa;
3429 struct PendingReversal *pending_reversal;
3430
3431 memset (&local_sa, 0, sizeof(local_sa));
3432 local_sa.sin_family = AF_INET;
3433 local_sa.sin_port = htons (bind_port);
3434 /* We leave sin_address at 0, let the kernel figure it out,
3435 even if our bind() is more specific. (May want to reconsider
3436 later.) */
3437 if (GNUNET_OK != GNUNET_NAT_request_reversal (nat, &local_sa, v4))
3438 {
3440 "request reversal for `%s' at `%s' failed\n",
3441 GNUNET_i2s (peer),
3442 address);
3443 GNUNET_free (in);
3444 return GNUNET_SYSERR;
3445 }
3446 pending_reversal = GNUNET_new (struct PendingReversal);
3447 pending_reversal->in = in;
3450 &key,
3451 pending_reversal,
3453 pending_reversal->target = *peer;
3455 &
3457 in);
3459 "Created NAT WAIT connection to `%s' at `%s'\n",
3460 GNUNET_i2s (peer),
3461 GNUNET_a2s (in, sizeof (struct sockaddr)));
3462 }
3463 else
3464 {
3465 struct GNUNET_NETWORK_Handle *sock;
3466
3467 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM,
3468 IPPROTO_TCP);
3469 if (NULL == sock)
3470 {
3472 "socket(%d) failed: %s",
3473 in->sa_family,
3474 strerror (errno));
3475 GNUNET_free (in);
3476 return GNUNET_SYSERR;
3477 }
3478 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, in_len)) &&
3479 (errno != EINPROGRESS))
3480 {
3482 "connect to `%s' failed: %s",
3483 address,
3484 strerror (errno));
3486 GNUNET_free (in);
3487 return GNUNET_SYSERR;
3488 }
3489
3490 queue = GNUNET_new (struct Queue);
3491 queue->target = *peer;
3492 queue->key = queue_map_key;
3493 queue->address = in;
3494 queue->address_len = in_len;
3495 queue->sock = sock;
3497 boot_queue (queue);
3499 "booted queue with target %s\n",
3500 GNUNET_i2s (&queue->target));
3501 // queue->mq_awaits_continue = GNUNET_YES;
3502 queue->read_task =
3504 queue->sock,
3506 queue);
3507
3508
3510 "start kx mq_init\n");
3511
3513 queue->write_task =
3515 queue->sock,
3516 &queue_write,
3517 queue);
3518 }
3519
3520 return GNUNET_OK;
3521}
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:674
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 3533 of file gnunet-communicator-tcp.c.

3536{
3537 struct ListenTask *lt = value;
3538
3539 (void) cls;
3540 (void) key;
3541 if (NULL != lt->listen_task)
3542 {
3544 lt->listen_task = NULL;
3545 }
3546 if (NULL != lt->listen_sock)
3547 {
3549 lt->listen_sock = NULL;
3550 }
3551 GNUNET_free (lt);
3552 return GNUNET_OK;
3553}

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 3565 of file gnunet-communicator-tcp.c.

3568{
3569 struct Queue *queue = value;
3570
3571 (void) cls;
3572 (void) target;
3574 return GNUNET_OK;
3575}
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 3584 of file gnunet-communicator-tcp.c.

3585{
3587 "Shutdown %s!\n",
3588 shutdown_running ? "running" : "not running");
3589
3591 return;
3592 else
3594
3595 while (NULL != proto_head)
3597 if (NULL != nat)
3598 {
3600 nat = NULL;
3601 }
3609 if (NULL != ch)
3610 {
3613 ch = NULL;
3614 }
3615 if (NULL != stats)
3616 {
3618 stats = NULL;
3619 }
3620 if (NULL != my_private_key)
3621 {
3623 my_private_key = NULL;
3624 }
3625 if (NULL != is)
3626 {
3628 is = NULL;
3629 }
3630 if (NULL != peerstore)
3631 {
3633 peerstore = NULL;
3634 }
3635 if (NULL != resolve_request_handle)
3636 {
3639 }
3641 "Shutdown done!\n");
3642}
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:702
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 3657 of file gnunet-communicator-tcp.c.

3660{
3661 (void) cls;
3662 (void) sender;
3663 (void) msg;
3664 GNUNET_break_op (0);
3665}

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 3682 of file gnunet-communicator-tcp.c.

3688{
3689 char *my_addr;
3691
3693 "nat address cb %s %s\n",
3694 add_remove ? "add" : "remove",
3695 GNUNET_a2s (addr, addrlen));
3696
3697 if (GNUNET_YES == add_remove)
3698 {
3700
3701 GNUNET_asprintf (&my_addr,
3702 "%s-%s",
3704 GNUNET_a2s (addr, addrlen));
3705 nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
3706 ai =
3708 my_addr,
3709 nt,
3711 GNUNET_free (my_addr);
3712 *app_ctx = ai;
3713 }
3714 else
3715 {
3716 ai = *app_ctx;
3718 *app_ctx = NULL;
3719 }
3720}
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 3727 of file gnunet-communicator-tcp.c.

3728{
3729
3730 struct Addresses *saddrs;
3731
3733 "add address %s\n",
3734 GNUNET_a2s (in, in_len));
3735
3736 saddrs = GNUNET_new (struct Addresses);
3737 saddrs->addr = in;
3738 saddrs->addr_len = in_len;
3740
3742 "after add address %s\n",
3743 GNUNET_a2s (in, in_len));
3744
3746 "add address %s\n",
3747 GNUNET_a2s (saddrs->addr, saddrs->addr_len));
3748
3749 addrs_lens++;
3750}
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 service.
static struct Addresses * addrs_tail
Head of DLL with addresses we like to register at NAT service.
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 3761 of file gnunet-communicator-tcp.c.

3763{
3764 struct sockaddr_storage in_sto;
3765 socklen_t sto_len;
3766 struct GNUNET_NETWORK_Handle *listen_sock;
3767 struct ListenTask *lt;
3768 int sockfd;
3769 struct GNUNET_HashCode h_sock;
3770
3771 if (NULL == addr)
3772 {
3774 "Address is NULL.\n");
3775 return GNUNET_SYSERR;
3776 }
3777
3779 "address %s\n",
3780 GNUNET_a2s (addr, in_len));
3781
3782 listen_sock =
3783 GNUNET_NETWORK_socket_create (addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
3784 if (NULL == listen_sock)
3785 {
3787 return GNUNET_SYSERR;
3788 }
3789
3790 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, addr, in_len))
3791 {
3793 GNUNET_NETWORK_socket_close (listen_sock);
3794 listen_sock = NULL;
3795 return GNUNET_SYSERR;
3796 }
3797
3798 if (GNUNET_OK !=
3799 GNUNET_NETWORK_socket_listen (listen_sock,
3800 5))
3801 {
3803 "listen");
3804 GNUNET_NETWORK_socket_close (listen_sock);
3805 listen_sock = NULL;
3806 return GNUNET_SYSERR;
3807 }
3808
3809 /* We might have bound to port 0, allowing the OS to figure it out;
3810 thus, get the real IN-address from the socket */
3811 sto_len = sizeof(in_sto);
3812
3813 if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock),
3814 (struct sockaddr *) &in_sto,
3815 &sto_len))
3816 {
3817 memcpy (&in_sto, addr, in_len);
3818 sto_len = in_len;
3819 }
3820
3821 // addr = (struct sockaddr *) &in_sto;
3822 in_len = sto_len;
3824 "Bound to `%s'\n",
3825 GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
3826 if (NULL == stats)
3827 stats = GNUNET_STATISTICS_create ("communicator-tcp", cfg);
3828
3829 if (NULL == is)
3831
3832 if (NULL == my_private_key)
3834 if (NULL == my_private_key)
3835 {
3836 GNUNET_log (
3838 _ (
3839 "Transport service is lacking key configuration settings. Exiting.\n"));
3840 if (NULL != resolve_request_handle)
3843 return GNUNET_SYSERR;
3844 }
3846 /* start listening */
3847
3848 lt = GNUNET_new (struct ListenTask);
3849 lt->listen_sock = listen_sock;
3850
3852 listen_sock,
3853 &listen_cb,
3854 lt);
3855
3857 "creating hash\n");
3858 sockfd = GNUNET_NETWORK_get_fd (lt->listen_sock);
3859 GNUNET_CRYPTO_hash (&sockfd,
3860 sizeof(int),
3861 &h_sock);
3862
3864 "creating map\n");
3865 if (NULL == lt_map)
3867
3869 "creating map entry\n");
3872 &h_sock,
3873 lt,
3875
3877 "map entry created\n");
3878
3879 if (NULL == queue_map)
3881
3882 if (NULL == ch)
3887 &mq_init,
3888 NULL,
3890 NULL);
3891
3892 if (NULL == ch)
3893 {
3894 GNUNET_break (0);
3895 if (NULL != resolve_request_handle)
3898 return GNUNET_SYSERR;
3899 }
3900
3901 add_addr (addr, in_len);
3902 return GNUNET_OK;
3903
3904}
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:200
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 3911 of file gnunet-communicator-tcp.c.

3912{
3913 struct sockaddr **saddrs;
3914 socklen_t *saddr_lens;
3915 int i;
3916 size_t len;
3917
3919 "starting nat register!\n");
3920 len = 0;
3921 i = 0;
3922 saddrs = GNUNET_malloc ((addrs_lens) * sizeof(struct sockaddr *));
3923 saddr_lens = GNUNET_malloc ((addrs_lens) * sizeof(socklen_t));
3924 for (struct Addresses *pos = addrs_head; NULL != pos; pos = pos->next)
3925 {
3927 "registering address %s\n",
3929
3930 saddr_lens[i] = addrs_head->addr_len;
3931 len += saddr_lens[i];
3932 saddrs[i] = GNUNET_memdup (addrs_head->addr, saddr_lens[i]);
3933 i++;
3934 }
3935
3937 "registering addresses %lu %lu %lu %lu\n",
3938 (addrs_lens) * sizeof(struct sockaddr *),
3939 (addrs_lens) * sizeof(socklen_t),
3940 len,
3944 IPPROTO_TCP,
3945 addrs_lens,
3946 (const struct sockaddr **) saddrs,
3947 saddr_lens,
3950 NULL /* closure */);
3951 for (i = addrs_lens - 1; i >= 0; i--)
3952 GNUNET_free (saddrs[i]);
3953 GNUNET_free (saddrs);
3954 GNUNET_free (saddr_lens);
3955
3956 if (NULL == nat)
3957 {
3958 GNUNET_break (0);
3959 if (NULL != resolve_request_handle)
3962 }
3963}
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 3974 of file gnunet-communicator-tcp.c.

3977{
3978 struct sockaddr_in *v4;
3979 struct sockaddr_in6 *v6;
3980 struct sockaddr *in;
3981
3982 (void) cls;
3983 if (NULL != addr)
3984 {
3985 if (AF_INET == addr->sa_family)
3986 {
3987 v4 = (struct sockaddr_in *) addr;
3988 in = tcp_address_to_sockaddr_numeric_v4 (&in_len, *v4, bind_port);// _global);
3989 }
3990 else if (AF_INET6 == addr->sa_family)
3991 {
3992 v6 = (struct sockaddr_in6 *) addr;
3993 in = tcp_address_to_sockaddr_numeric_v6 (&in_len, *v6, bind_port);// _global);
3994 }
3995 else
3996 {
3998 "Address family %u not suitable (not AF_INET %u nor AF_INET6 %u \n",
3999 addr->sa_family,
4000 AF_INET,
4001 AF_INET6);
4002 return;
4003 }
4004 init_socket (in, in_len);
4005 }
4006 else
4007 {
4009 "Address is NULL. This might be an error or the resolver finished resolving.\n");
4010 if (NULL == addrs_head)
4011 {
4013 "Resolver finished resolving, but we do not listen to an address!.\n");
4014 return;
4015 }
4016 nat_register ();
4017 }
4018}
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 4030 of file gnunet-communicator-tcp.c.

4034{
4035 char *bindto;
4036 struct sockaddr *in;
4037 socklen_t in_len;
4038 struct sockaddr_in v4;
4039 struct sockaddr_in6 v6;
4040 char *start;
4041 unsigned int port;
4042 char dummy[2];
4043 char *rest = NULL;
4044 struct PortOnlyIpv4Ipv6 *po;
4045 socklen_t addr_len_ipv4;
4046 socklen_t addr_len_ipv6;
4047
4048 (void) cls;
4049
4051 memset (&v4,0,sizeof(struct sockaddr_in));
4052 memset (&v6,0,sizeof(struct sockaddr_in6));
4053 cfg = c;
4054 if (GNUNET_OK !=
4057 "BINDTO",
4058 &bindto))
4059 {
4062 "BINDTO");
4063 return;
4064 }
4065 if (GNUNET_OK !=
4068 "MAX_QUEUE_LENGTH",
4070 {
4072 }
4073 if (GNUNET_OK !=
4076 "REKEY_INTERVAL",
4078 {
4080 }
4081 if (GNUNET_OK !=
4084 "REKEY_MAX_BYTES",
4086 {
4088 }
4090 if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
4091 (GNUNET_YES ==
4094 "DISABLE_V6")))
4095 {
4097 }
4099 if (NULL == peerstore)
4100 {
4101 GNUNET_free (bindto);
4102 GNUNET_break (0);
4104 return;
4105 }
4106
4108
4109 if (1 == sscanf (bindto, "%u%1s", &bind_port, dummy))
4110 {
4114 "address po %s\n",
4116 if (NULL != po->addr_ipv4)
4117 {
4119 }
4120 if (NULL != po->addr_ipv6)
4121 {
4124 }
4125 GNUNET_free (po);
4126 nat_register ();
4127 GNUNET_free (bindto);
4128 return;
4129 }
4130
4131 start = extract_address (bindto);
4132 // FIXME: check for NULL == start...
4133 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
4134 {
4135 bind_port = extract_port (bindto);
4136
4138 init_socket (in, in_len);
4139 nat_register ();
4141 GNUNET_free (bindto);
4142 return;
4143 }
4144
4145 if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
4146 {
4147 bind_port = extract_port (bindto);
4149 init_socket (in, in_len);
4150 nat_register ();
4152 GNUNET_free (bindto);
4153 return;
4154 }
4155
4156 bind_port = extract_port (bindto);
4158 ":",
4159 &rest),
4160 AF_UNSPEC,
4163 &port);
4164
4165 GNUNET_free (bindto);
4167}
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 4178 of file gnunet-communicator-tcp.c.

4179{
4180 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
4182 };
4183 int ret;
4184
4186 "Starting tcp communicator\n");
4187 if (GNUNET_OK !=
4188 GNUNET_STRINGS_get_utf8_args (argc, argv,
4189 &argc, &argv))
4190 return 2;
4191
4192 ret = (GNUNET_OK ==
4193 GNUNET_PROGRAM_run (argc,
4194 argv,
4195 "gnunet-communicator-tcp",
4196 _ ("GNUnet TCP communicator"),
4197 options,
4198 &run,
4199 NULL))
4200 ? 0
4201 : 1;
4202 GNUNET_free_nz ((void *) argv);
4203 return ret;
4204}
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