GNUnet  0.19.3
gnunet-communicator-tcp.c File Reference

Transport plugin using TCP. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_core_service.h"
#include "gnunet_peerstore_service.h"
#include "gnunet_protocols.h"
#include "gnunet_signatures.h"
#include "gnunet_constants.h"
#include "gnunet_nt_lib.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 * 1024 * 4LLU)
 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 reschedule_queue_timeout (struct Queue *queue)
 Increment queue timeout due to activity. More...
 
static void queue_read (void *cls)
 Queue read task. More...
 
static void core_read_finished_cb (void *cls, int success)
 Core tells us it is done processing a message that transport received on a queue with status success. More...
 
static void pass_plaintext_to_core (struct Queue *queue, const void *plaintext, size_t plaintext_len)
 We received plaintext_len bytes of plaintext on queue. More...
 
static void setup_cipher (const struct GNUNET_HashCode *dh, const struct GNUNET_PeerIdentity *pid, gcry_cipher_hd_t *cipher, struct GNUNET_HashCode *hmac_key)
 Setup cipher based on shared secret dh and decrypting peer pid. More...
 
static void rekey_monotime_store_cb (void *cls, int success)
 Callback called when peerstore store operation for rekey monotime value is finished. More...
 
static void rekey_monotime_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
 Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY where found. More...
 
static void setup_in_cipher (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral, struct Queue *queue)
 Setup cipher of queue for decryption. More...
 
static void do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
 Handle rekey message on queue. More...
 
static void handshake_ack_monotime_store_cb (void *cls, int success)
 Callback called when peerstore store operation for handshake ack monotime value is finished. More...
 
static void handshake_ack_monotime_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
 Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE_ACK where found. More...
 
static void send_challenge (struct GNUNET_CRYPTO_ChallengeNonceP challenge, struct Queue *queue)
 Sending challenge with TcpConfirmationAck back to sender of ephemeral key. More...
 
static void setup_out_cipher (struct Queue *queue)
 Setup cipher for outgoing data stream based on target and our ephemeral private key. More...
 
static void inject_rekey (struct Queue *queue)
 Inject a struct TCPRekey message into the queue's plaintext buffer. More...
 
static int pending_reversals_delete_it (void *cls, const struct GNUNET_HashCode *key, void *value)
 
static void check_and_remove_pending_reversal (struct sockaddr *in, sa_family_t sa_family, struct GNUNET_PeerIdentity *sender)
 
static void free_proto_queue (struct ProtoQueue *pq)
 Closes socket and frees memory associated with pq. More...
 
static void proto_queue_write (void *cls)
 We have been notified that our socket is ready to write. More...
 
static void queue_write (void *cls)
 We have been notified that our socket is ready to write. More...
 
static size_t try_handle_plaintext (struct Queue *queue)
 Test if we have received a full message in plaintext. More...
 
static struct sockaddr * tcp_address_to_sockaddr_numeric_v6 (socklen_t *sock_len, struct sockaddr_in6 v6, unsigned int port)
 Convert a struct sockaddr_in6 to astruct sockaddr *`. More...
 
static struct sockaddr * tcp_address_to_sockaddr_numeric_v4 (socklen_t *sock_len, struct sockaddr_in v4, unsigned int port)
 Convert a struct sockaddr_in4 to astruct sockaddr *`. More...
 
static struct PortOnlyIpv4Ipv6tcp_address_to_sockaddr_port_only (const char *bindto, unsigned int *port)
 Convert TCP bind specification to a struct PortOnlyIpv4Ipv6 * More...
 
static char * extract_address (const char *bindto)
 This Method extracts the address part of the BINDTO string. More...
 
static unsigned int extract_port (const char *addr_and_port)
 This Method extracts the port part of the BINDTO string. More...
 
static struct sockaddr * tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
 Convert TCP bind specification to a struct sockaddr * More...
 
static void mq_send (struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *msg, void *impl_state)
 Signature of functions implementing the sending functionality of a message queue. More...
 
static void mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
 Signature of functions implementing the destruction of a message queue. More...
 
static void mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
 Implementation function that cancels the currently sent message. More...
 
static void mq_error (void *cls, enum GNUNET_MQ_Error error)
 Generic error handler, called with the appropriate error code and the same closure specified at the creation of the message queue. More...
 
static void boot_queue (struct Queue *queue)
 Add the given queue to our internal data structure. More...
 
static void transmit_kx (struct Queue *queue, const struct GNUNET_CRYPTO_EcdhePublicKey *epub)
 Generate and transmit our ephemeral key and the signature for the initial KX with the other peer. More...
 
static void start_initial_kx_out (struct Queue *queue)
 Initialize our key material for outgoing transmissions and inform the other peer about it. More...
 
static void handshake_monotime_store_cb (void *cls, int success)
 Callback called when peerstore store operation for handshake monotime is finished. More...
 
static void handshake_monotime_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
 Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE where found. More...
 
static int decrypt_and_check_tc (struct Queue *queue, struct TCPConfirmation *tc, char *ibuf)
 We have received the first bytes from the other side on a queue. More...
 
static void queue_read_kx (void *cls)
 Read from the socket of the queue until we have enough data to initialize the decryption logic and can switch to regular reading. More...
 
static void proto_read_kx (void *cls)
 Read from the socket of the proto queue until we have enough data to upgrade to full queue. More...
 
static struct ProtoQueuecreate_proto_queue (struct GNUNET_NETWORK_Handle *sock, struct sockaddr *in, socklen_t addrlen)
 
static void try_connection_reversal (void *cls, const struct sockaddr *addr, socklen_t addrlen)
 
static void pending_reversal_timeout (void *cls)
 
static int mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
 Function called by the transport service to initialize a message queue given address information about another peer. More...
 
static int get_lt_delete_it (void *cls, const struct GNUNET_HashCode *key, void *value)
 Iterator over all ListenTasks to clean up. More...
 
static int get_queue_delete_it (void *cls, const struct GNUNET_PeerIdentity *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_MultiPeerMapqueue_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 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...
 
struct Addressesaddrs_head
 Head of DLL with addresses we like to register at NAT servcie. More...
 
struct Addressesaddrs_tail
 Head of DLL with addresses we like to register at NAT servcie. More...
 
struct ListenTasklts_head
 Head of DLL with ListenTasks. More...
 
struct ListenTasklts_tail
 Head of DLL with ListenTask. More...
 
int addrs_lens
 Number of addresses in the DLL for register at NAT service. More...
 
size_t unverified_size
 Size of data received without KX challenge played back. More...
 
static struct GNUNET_PEERSTORE_Handlepeerstore
 Database for peer's HELLOs. More...
 
int shutdown_running = GNUNET_NO
 A flag indicating we are already doing a shutdown. More...
 
unsigned int bind_port
 The port the communicator should be assigned to. More...
 
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 * 1024 * 4LLU)

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

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

3124 {
3125  struct sockaddr_storage in;
3126  socklen_t addrlen;
3127  struct GNUNET_NETWORK_Handle *sock;
3128  struct ProtoQueue *pq;
3129  struct ListenTask *lt;
3130  struct sockaddr *in_addr;
3131 
3133  "listen_cb\n");
3134 
3135  lt = cls;
3136 
3137  lt->listen_task = NULL;
3138  GNUNET_assert (NULL != lt->listen_sock);
3139  addrlen = sizeof(in);
3140  memset (&in, 0, sizeof(in));
3142  (struct sockaddr*) &in,
3143  &addrlen);
3144  if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno)))
3145  return; /* system limit reached, wait until connection goes down */
3147  lt->listen_sock,
3148  &listen_cb,
3149  lt);
3150  if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno)))
3151  return;
3152  if (NULL == sock)
3153  {
3155  return;
3156  }
3157  in_addr = GNUNET_memdup (&in, addrlen);
3158  create_proto_queue (sock, in_addr, addrlen);
3159 }
static void listen_cb(void *cls)
We have been notified that our listen socket has something to read.
static struct ProtoQueue * create_proto_queue(struct GNUNET_NETWORK_Handle *sock, struct sockaddr *in, socklen_t addrlen)
#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:1475
#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.
Handle for an incoming connection where we do not yet have enough information to setup a full queue.

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, ListenTask::listen_sock, and ListenTask::listen_task.

Referenced by GNUNET_SET_listen(), GNUNET_SETI_listen(), GNUNET_SETU_listen(), init_socket(), 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 940 of file gnunet-communicator-tcp.c.

941 {
942  struct ListenTask *lt = NULL;
943  struct GNUNET_HashCode h_sock;
944  int sockfd;
945 
946  if (NULL != queue->listen_sock)
947  {
948  sockfd = GNUNET_NETWORK_get_fd (queue->listen_sock);
949  GNUNET_CRYPTO_hash (&sockfd,
950  sizeof(int),
951  &h_sock);
952 
954  }
955 
957  "Disconnecting queue for peer `%s'\n",
958  GNUNET_i2s (&queue->target));
959  if (NULL != queue->rekey_monotime_sc)
960  {
961  GNUNET_PEERSTORE_store_cancel (queue->rekey_monotime_sc);
962  queue->rekey_monotime_sc = NULL;
963  }
964  if (NULL != queue->handshake_monotime_sc)
965  {
966  GNUNET_PEERSTORE_store_cancel (queue->handshake_monotime_sc);
967  queue->handshake_monotime_sc = NULL;
968  }
969  if (NULL != queue->handshake_ack_monotime_sc)
970  {
971  GNUNET_PEERSTORE_store_cancel (queue->handshake_ack_monotime_sc);
972  queue->handshake_ack_monotime_sc = NULL;
973  }
974  if (NULL != queue->rekey_monotime_get)
975  {
976  GNUNET_PEERSTORE_iterate_cancel (queue->rekey_monotime_get);
977  queue->rekey_monotime_get = NULL;
978  }
979  if (NULL != queue->handshake_monotime_get)
980  {
981  GNUNET_PEERSTORE_iterate_cancel (queue->handshake_monotime_get);
982  queue->handshake_monotime_get = NULL;
983  }
984  if (NULL != queue->handshake_ack_monotime_get)
985  {
986  GNUNET_PEERSTORE_iterate_cancel (queue->handshake_ack_monotime_get);
987  queue->handshake_ack_monotime_get = NULL;
988  }
989  if (NULL != queue->qh)
990  {
992  queue->qh = NULL;
993  }
994  GNUNET_assert (
995  GNUNET_YES ==
998  "# queues active",
1000  GNUNET_NO);
1001  if (NULL != queue->read_task)
1002  {
1003  GNUNET_SCHEDULER_cancel (queue->read_task);
1004  queue->read_task = NULL;
1005  }
1006  if (NULL != queue->write_task)
1007  {
1008  GNUNET_SCHEDULER_cancel (queue->write_task);
1009  queue->write_task = NULL;
1010  }
1012  {
1014  "closing socket failed\n");
1015  }
1016  gcry_cipher_close (queue->in_cipher);
1017  gcry_cipher_close (queue->out_cipher);
1018  GNUNET_free (queue->address);
1019  if (0 != queue->backpressure)
1020  queue->destroyed = GNUNET_YES;
1021  else
1022  GNUNET_free (queue);
1023 
1024  if (NULL == lt)
1025  return;
1026 
1027  if ((! shutdown_running) && (NULL == lt->listen_task))
1028  {
1030  "add read net listen\n");
1033  lt->listen_sock,
1034  &listen_cb,
1035  lt);
1036  }
1037  else
1038  GNUNET_free (lt);
1039 }
static struct GNUNET_CONTAINER_MultiPeerMap * queue_map
Queues (map from peer identity to struct Queue)
static struct GNUNET_STATISTICS_Handle * stats
For logging statistics.
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_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.
unsigned int GNUNET_CONTAINER_multipeermap_size(const struct GNUNET_CONTAINER_MultiPeerMap *map)
Get the number of key-value pairs in the map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multipeermap_remove(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, const void *value)
Remove the given key-value pair from the map.
@ 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_iterate_cancel(struct GNUNET_PEERSTORE_IterateContext *ic)
Cancel an iterate request Please do not call after the iterate request is done.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:944
void GNUNET_STATISTICS_set(struct GNUNET_STATISTICS_Handle *handle, const char *name, uint64_t value, int make_persistent)
Set statistic value for the peer.
void GNUNET_TRANSPORT_communicator_mq_del(struct GNUNET_TRANSPORT_QueueHandle *qh)
Notify transport service that an MQ became unavailable due to a disconnect or timeout.
A 512-bit hashcode.

References GNUNET_assert, GNUNET_CONTAINER_multihashmap_get(), GNUNET_CONTAINER_multipeermap_remove(), GNUNET_CONTAINER_multipeermap_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_iterate_cancel(), 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_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 1051 of file gnunet-communicator-tcp.c.

1055 {
1056  struct GNUNET_HashCode mac;
1057 
1058  GNUNET_CRYPTO_hmac_raw (hmac_secret,
1059  sizeof(struct GNUNET_HashCode),
1060  buf,
1061  buf_size,
1062  &mac);
1063  /* truncate to `struct GNUNET_ShortHashCode` */
1064  memcpy (smac, &mac, sizeof(struct GNUNET_ShortHashCode));
1065  /* ratchet hmac key */
1066  GNUNET_CRYPTO_hash (hmac_secret,
1067  sizeof(struct GNUNET_HashCode),
1068  hmac_secret);
1069 }
static char buf[2048]
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:299
A 256-bit hashcode.

References buf, 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 1079 of file gnunet-communicator-tcp.c.

1080 {
1081  struct TCPFinish fin;
1082 
1083  memset (&fin, 0, sizeof(fin));
1084  fin.header.size = htons (sizeof(fin));
1085  fin.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH);
1086  calculate_hmac (&queue->out_hmac, &fin, sizeof(fin), &fin.hmac);
1087  /* if there is any message left in pwrite_buf, we
1088  overwrite it (possibly dropping the last message
1089  from CORE hard here) */
1090  memcpy (queue->pwrite_buf, &fin, sizeof(fin));
1091  queue->pwrite_off = sizeof(fin);
1092  /* This flag will ensure that #queue_write() no longer
1093  notifies CORE about the possibility of sending
1094  more data, and that #queue_write() will call
1095  #queue_destroy() once the @c fin was fully written. */
1096  queue->finishing = GNUNET_YES;
1097 }
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(), queue_read(), rekey_monotime_cb(), and try_handle_plaintext().

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

◆ reschedule_queue_timeout()

static void reschedule_queue_timeout ( struct Queue queue)
static

Increment queue timeout due to activity.

We do not immediately notify the monitor here as that might generate excessive signalling.

Parameters
queuequeue for which the timeout should be rescheduled

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

1109 {
1110  queue->timeout =
1112 }
#define GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
After how long do we consider a connection to a peer dead if we don't receive messages from the peer?
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
Definition: time.c:316

References GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_TIME_relative_to_absolute(), and queue().

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

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

2103 {
2104  struct Queue *queue = cls;
2105  struct GNUNET_TIME_Relative left;
2106  ssize_t rcvd;
2107 
2108  queue->read_task = NULL;
2109  rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2110  &queue->cread_buf[queue->cread_off],
2111  BUF_SIZE - queue->cread_off);
2113  "Received %zd bytes from TCP queue\n", rcvd);
2115  "transport",
2116  "Received %zd bytes from TCP queue\n", rcvd);
2117  if (-1 == rcvd)
2118  {
2119  if ((EAGAIN != errno) && (EINTR != errno))
2120  {
2122  queue_finish (queue);
2123  return;
2124  }
2125  /* try again */
2126  left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2127  queue->read_task =
2129  return;
2130  }
2131  if (0 != rcvd)
2133  queue->cread_off += rcvd;
2134  while ((queue->pread_off < sizeof(queue->pread_buf)) &&
2135  (queue->cread_off > 0))
2136  {
2137  size_t max = GNUNET_MIN (sizeof(queue->pread_buf) - queue->pread_off,
2138  queue->cread_off);
2139  size_t done;
2140  size_t total;
2141  size_t old_pread_off = queue->pread_off;
2142 
2143  GNUNET_assert (0 ==
2144  gcry_cipher_decrypt (queue->in_cipher,
2145  &queue->pread_buf[queue->pread_off],
2146  max,
2147  queue->cread_buf,
2148  max));
2149  queue->pread_off += max;
2150  total = 0;
2151  while (0 != (done = try_handle_plaintext (queue)))
2152  {
2153  /* 'done' bytes of plaintext were used, shift buffer */
2154  GNUNET_assert (done <= queue->pread_off);
2155  /* NOTE: this memmove() could possibly sometimes be
2156  avoided if we pass 'total' into try_handle_plaintext()
2157  and use it at an offset into the buffer there! */
2158  memmove (queue->pread_buf,
2159  &queue->pread_buf[done],
2160  queue->pread_off - done);
2161  queue->pread_off -= done;
2162  total += done;
2163  /* The last plaintext was a rekey, abort for now */
2164  if (GNUNET_YES == queue->rekeyed)
2165  break;
2166  }
2167  /* when we encounter a rekey message, the decryption above uses the
2168  wrong key for everything after the rekey; in that case, we have
2169  to re-do the decryption at 'total' instead of at 'max'.
2170  However, we have to take into account that the plaintext buffer may have
2171  already contained data and not jumped too far ahead in the ciphertext.
2172  If there is no rekey and the last message is incomplete (max > total),
2173  it is safe to keep the decryption so we shift by 'max' */
2174  if (GNUNET_YES == queue->rekeyed)
2175  {
2176  max = total - old_pread_off;
2177  queue->rekeyed = GNUNET_NO;
2178  queue->pread_off = 0;
2179  }
2180  memmove (queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
2181  queue->cread_off -= max;
2182  }
2183  if (BUF_SIZE == queue->cread_off)
2184  return; /* buffer full, suspend reading */
2185  left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2186  if (0 != left.rel_value_us)
2187  {
2188  if (max_queue_length > queue->backpressure)
2189  {
2190  /* continue reading */
2191  left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2192  queue->read_task =
2194  }
2195  return;
2196  }
2198  "Queue %p was idle for %s, disconnecting\n",
2199  queue,
2202  GNUNET_YES));
2203  queue_finish (queue);
2204 }
static void done()
static void queue_finish(struct Queue *queue)
Append a 'finish' message to the outgoing transmission.
static size_t try_handle_plaintext(struct Queue *queue)
Test if we have received a full message in plaintext.
#define BUF_SIZE
Size of our IO buffers for ciphertext data.
static void reschedule_queue_timeout(struct Queue *queue)
Increment queue timeout due to activity.
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_MIN(a, b)
void GNUNET_log_from_nocheck(enum GNUNET_ErrorType kind, const char *comp, const char *message,...) __attribute__((format(printf
Log function that specifies an alternative component.
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:569
#define max(x, y)
Time for relative time used by GNUnet, in microseconds.
Handle for a queue.

References BUF_SIZE, done(), GNUNET_assert, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_log_from_nocheck(), 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_YES, max, max_queue_length, queue(), queue_finish(), GNUNET_TIME_Relative::rel_value_us, reschedule_queue_timeout(), and try_handle_plaintext().

Referenced by core_read_finished_cb(), proto_read_kx(), 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 1132 of file gnunet-communicator-tcp.c.

1133 {
1134  struct Queue *queue = cls;
1135  if (GNUNET_OK != success)
1137  "# messages lost in communicator API towards CORE",
1138  1,
1139  GNUNET_NO);
1140  if (NULL == queue)
1141  return;
1142 
1144  "backpressure %u\n",
1145  queue->backpressure);
1146 
1147  queue->backpressure--;
1148  /* handle deferred queue destruction */
1149  if ((queue->destroyed) && (0 == queue->backpressure))
1150  {
1151  GNUNET_free (queue);
1152  return;
1153  }
1154  else if (GNUNET_YES != queue->destroyed)
1155  {
1157  /* possibly unchoke reading, now that CORE made progress */
1158  if (NULL == queue->read_task)
1159  queue->read_task =
1161  queue->timeout),
1162  queue->sock,
1163  &queue_read,
1164  queue);
1165  }
1166 }
@ 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_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_YES, queue(), queue_read(), reschedule_queue_timeout(), 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 1179 of file gnunet-communicator-tcp.c.

1182 {
1183  const struct GNUNET_MessageHeader *hdr = plaintext;
1184  int ret;
1185 
1187  "pass message from %s to core\n",
1188  GNUNET_i2s (&queue->target));
1189 
1190  if (ntohs (hdr->size) != plaintext_len)
1191  {
1192  /* NOTE: If we ever allow multiple CORE messages in one
1193  BOX, this will have to change! */
1194  GNUNET_break (0);
1195  return;
1196  }
1198  &queue->target,
1199  hdr,
1202  queue);
1204  "passed to core\n");
1205  if (GNUNET_OK == ret)
1206  queue->backpressure++;
1207  GNUNET_break (GNUNET_NO != ret); /* backpressure not working!? */
1208  if (GNUNET_SYSERR == ret)
1210  "# bytes lost due to CORE not running",
1211  plaintext_len,
1212  GNUNET_NO);
1213 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#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.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
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.
Header for all communications.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.

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

Referenced by try_handle_plaintext().

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

◆ setup_cipher()

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

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

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

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

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

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

Referenced by setup_in_cipher(), and setup_out_cipher().

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

◆ rekey_monotime_store_cb()

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

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

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

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

1281 {
1282  struct Queue *queue = cls;
1283  if (GNUNET_OK != success)
1284  {
1286  "Failed to store rekey monotonic time in PEERSTORE!\n");
1287  }
1288  queue->rekey_monotime_sc = NULL;
1289 }

References GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, 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 1300 of file gnunet-communicator-tcp.c.

1303 {
1304  struct Queue *queue = cls;
1305  struct GNUNET_TIME_AbsoluteNBO *mtbe;
1306  struct GNUNET_TIME_Absolute mt;
1307  const struct GNUNET_PeerIdentity *pid;
1308  struct GNUNET_TIME_AbsoluteNBO *rekey_monotonic_time;
1309 
1310  (void) emsg;
1311 
1312  rekey_monotonic_time = &queue->rekey_monotonic_time;
1313  pid = &queue->target;
1314  if (NULL == record)
1315  {
1316  queue->rekey_monotime_get = NULL;
1317  return;
1318  }
1319  if (sizeof(*mtbe) != record->value_size)
1320  {
1321  GNUNET_break (0);
1322  return;
1323  }
1324  mtbe = record->value;
1325  mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1326  if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
1327  queue->rekey_monotonic_time).abs_value_us)
1328  {
1330  "Queue from %s dropped, rekey monotime in the past\n",
1331  GNUNET_i2s (&queue->target));
1332  GNUNET_break (0);
1333  queue_finish (queue);
1334  return;
1335  }
1336  queue->rekey_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
1337  "transport_tcp_communicator",
1338  pid,
1340  rekey_monotonic_time,
1341  sizeof(*
1342  rekey_monotonic_time),
1346  queue);
1347 }
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_store(), GNUNET_PEERSTORE_STOREOPTION_REPLACE, GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY, GNUNET_TIME_absolute_ntoh(), GNUNET_TIME_UNIT_FOREVER_ABS, peerstore, pid, queue(), queue_finish(), record(), and rekey_monotime_store_cb().

Referenced by do_rekey().

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

◆ setup_in_cipher()

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

Setup cipher of queue for decryption.

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

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

1359 {
1360  struct GNUNET_HashCode dh;
1361 
1362  GNUNET_CRYPTO_eddsa_ecdh (my_private_key, ephemeral, &dh);
1363  setup_cipher (&dh, &my_identity, &queue->in_cipher, &queue->in_hmac);
1364 }
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_ecdh(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a ECDH public key and a private EdDSA key.
Definition: crypto_ecc.c:739

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

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

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

◆ do_rekey()

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

Handle rekey message on queue.

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

Parameters
queuethe queue rekey was received on
rekeythe rekey message

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

1377 {
1378  struct TcpRekeySignature thp;
1379 
1381  thp.purpose.size = htonl (sizeof(thp));
1383  "do_rekey size %u\n",
1384  thp.purpose.size);
1385  thp.sender = queue->target;
1387  "sender %s\n",
1388  GNUNET_p2s (&thp.sender.public_key));
1390  "sender %s\n",
1391  GNUNET_p2s (&queue->target.public_key));
1392  thp.receiver = my_identity;
1394  "receiver %s\n",
1395  GNUNET_p2s (&thp.receiver.public_key));
1396  thp.ephemeral = rekey->ephemeral;
1398  "ephemeral %s\n",
1399  GNUNET_e2s (&thp.ephemeral));
1400  thp.monotonic_time = rekey->monotonic_time;
1402  "time %s\n",
1404  GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
1405  GNUNET_assert (ntohl ((&thp)->purpose.size) == sizeof (*(&thp)));
1406  if (GNUNET_OK !=
1409  &thp,
1410  &rekey->sender_sig,
1411  &queue->target.public_key))
1412  {
1413  GNUNET_break (0);
1414  queue_finish (queue);
1415  return;
1416  }
1417  queue->rekey_monotonic_time = rekey->monotonic_time;
1418  queue->rekey_monotime_get = GNUNET_PEERSTORE_iterate (peerstore,
1419  "transport_tcp_communicator",
1420  &queue->target,
1423  queue);
1424  gcry_cipher_close (queue->in_cipher);
1425  queue->rekeyed = GNUNET_YES;
1426  setup_in_cipher (&rekey->ephemeral, queue);
1427 }
#define GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY
Signature used by TCP communicator rekey.
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_iterate(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 records matching supplied key information.
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:616
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_iterate(), GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY, GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY, GNUNET_STRINGS_absolute_time_to_string(), GNUNET_TIME_absolute_ntoh(), GNUNET_YES, TcpRekeySignature::monotonic_time, TCPRekey::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 1436 of file gnunet-communicator-tcp.c.

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

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

Referenced by handshake_ack_monotime_cb().

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

◆ handshake_ack_monotime_cb()

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

Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE_ACK where found.

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

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

1460 {
1461  struct Queue *queue = cls;
1462  struct GNUNET_TIME_AbsoluteNBO *mtbe;
1463  struct GNUNET_TIME_Absolute mt;
1464  const struct GNUNET_PeerIdentity *pid;
1465  struct GNUNET_TIME_AbsoluteNBO *handshake_ack_monotonic_time;
1466 
1467  (void) emsg;
1468 
1469  handshake_ack_monotonic_time = &queue->handshake_ack_monotonic_time;
1470  pid = &queue->target;
1471  if (NULL == record)
1472  {
1473  queue->handshake_ack_monotime_get = NULL;
1474  return;
1475  }
1476  if (sizeof(*mtbe) != record->value_size)
1477  {
1478  GNUNET_break (0);
1479  return;
1480  }
1481  mtbe = record->value;
1482  mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1483  if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
1484  queue->handshake_ack_monotonic_time).abs_value_us)
1485  {
1487  "Queue from %s dropped, handshake ack monotime in the past\n",
1488  GNUNET_i2s (&queue->target));
1489  GNUNET_break (0);
1490  queue_finish (queue);
1491  return;
1492  }
1493  queue->handshake_ack_monotime_sc =
1495  "transport_tcp_communicator",
1496  pid,
1498  handshake_ack_monotonic_time,
1499  sizeof(*handshake_ack_monotonic_time),
1502  &
1504  queue);
1505 }
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_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 1515 of file gnunet-communicator-tcp.c.

1517 {
1518  struct TCPConfirmationAck tca;
1519  struct TcpHandshakeAckSignature thas;
1520 
1522  "transport",
1523  "sending challenge\n");
1524 
1525  tca.header.type = ntohs (
1527  tca.header.size = ntohs (sizeof(tca));
1528  tca.challenge = challenge;
1529  tca.sender = my_identity;
1530  tca.monotonic_time =
1532  thas.purpose.purpose = htonl (
1534  thas.purpose.size = htonl (sizeof(thas));
1535  thas.sender = my_identity;
1536  thas.receiver = queue->target;
1537  thas.monotonic_time = tca.monotonic_time;
1538  thas.challenge = tca.challenge;
1540  &thas,
1541  &tca.sender_sig);
1542  GNUNET_assert (0 ==
1543  gcry_cipher_encrypt (queue->out_cipher,
1544  &queue->cwrite_buf[queue->cwrite_off],
1545  sizeof(tca),
1546  &tca,
1547  sizeof(tca)));
1548  queue->cwrite_off += sizeof(tca);
1550  "transport",
1551  "sending challenge done\n");
1552 }
#define GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE_ACK
Signature by a peer sending back the nonce received at initial handshake.
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
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_from_nocheck(), 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, TCPConfirmationAck::monotonic_time, TcpHandshakeAckSignature::monotonic_time, my_identity, my_private_key, GNUNET_CRYPTO_EccSignaturePurpose::purpose, TcpHandshakeAckSignature::purpose, queue(), TcpHandshakeAckSignature::receiver, TCPConfirmationAck::sender, TcpHandshakeAckSignature::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)
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 1562 of file gnunet-communicator-tcp.c.

1563 {
1564  struct GNUNET_HashCode dh;
1565 
1566  GNUNET_CRYPTO_ecdh_eddsa (&queue->ephemeral, &queue->target.public_key, &dh);
1567  /* we don't need the private key anymore, drop it! */
1568  memset (&queue->ephemeral, 0, sizeof(queue->ephemeral));
1569  setup_cipher (&dh, &queue->target, &queue->out_cipher, &queue->out_hmac);
1571  queue->rekey_left_bytes =
1573 }
static struct GNUNET_TIME_Relative rekey_interval
The rekey interval.
#define REKEY_MAX_BYTES
How often do we rekey based on number of bytes transmitted? (additionally randomized).
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Generate a random unsigned 64-bit value.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdh_eddsa(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a EdDSA public key and a private ECDH key.
Definition: crypto_ecc.c:779
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).

References GNUNET_CRYPTO_ecdh_eddsa(), 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 1583 of file gnunet-communicator-tcp.c.

1584 {
1585  struct TCPRekey rekey;
1586  struct TcpRekeySignature thp;
1587 
1588  GNUNET_assert (0 == queue->pwrite_off);
1589  memset (&rekey, 0, sizeof(rekey));
1590  GNUNET_CRYPTO_ecdhe_key_create (&queue->ephemeral);
1591  rekey.header.type = ntohs (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY);
1592  rekey.header.size = ntohs (sizeof(rekey));
1593  GNUNET_CRYPTO_ecdhe_key_get_public (&queue->ephemeral, &rekey.ephemeral);
1594  rekey.monotonic_time =
1596  thp.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY);
1597  thp.purpose.size = htonl (sizeof(thp));
1599  "inject_rekey size %u\n",
1600  thp.purpose.size);
1601  thp.sender = my_identity;
1603  "sender %s\n",
1604  GNUNET_p2s (&thp.sender.public_key));
1605  thp.receiver = queue->target;
1607  "receiver %s\n",
1608  GNUNET_p2s (&thp.receiver.public_key));
1609  thp.ephemeral = rekey.ephemeral;
1611  "ephemeral %s\n",
1612  GNUNET_e2s (&thp.ephemeral));
1613  thp.monotonic_time = rekey.monotonic_time;
1615  "time %s\n",
1617  GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
1619  &thp,
1620  &rekey.sender_sig);
1621  calculate_hmac (&queue->out_hmac, &rekey, sizeof(rekey), &rekey.hmac);
1622  /* Encrypt rekey message with 'old' cipher */
1623  GNUNET_assert (0 ==
1624  gcry_cipher_encrypt (queue->out_cipher,
1625  &queue->cwrite_buf[queue->cwrite_off],
1626  sizeof(rekey),
1627  &rekey,
1628  sizeof(rekey)));
1629  queue->cwrite_off += sizeof(rekey);
1630  /* Setup new cipher for successive messages */
1631  gcry_cipher_close (queue->out_cipher);
1633 }
static void setup_out_cipher(struct Queue *queue)
Setup cipher for outgoing data stream based on target and our ephemeral private key.
void GNUNET_CRYPTO_ecdhe_key_create(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
Create a new private key.
Definition: crypto_ecc.c:435
void GNUNET_CRYPTO_ecdhe_key_get_public(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, struct GNUNET_CRYPTO_EcdhePublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:213
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY
TCP communicator rekey message.
TCP rekey message box.

References calculate_hmac(), cfg, TcpRekeySignature::ephemeral, TCPRekey::ephemeral, GNUNET_assert, GNUNET_CRYPTO_ecdhe_key_create(), GNUNET_CRYPTO_ecdhe_key_get_public(), 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 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 1636 of file gnunet-communicator-tcp.c.

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

References GNUNET_CONTAINER_multihashmap_remove(), GNUNET_free, GNUNET_OK, GNUNET_SCHEDULER_cancel(), 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 1658 of file gnunet-communicator-tcp.c.

1659 {
1660  if (AF_INET == sa_family)
1661  {
1662  struct PendingReversal *pending_reversal;
1663  struct GNUNET_HashCode key;
1664  struct sockaddr_in *natted_address;
1665 
1666  natted_address = GNUNET_memdup (in, sizeof (struct sockaddr));
1667  natted_address->sin_port = 0;
1668  GNUNET_CRYPTO_hash (natted_address,
1669  sizeof(struct sockaddr),
1670  &key);
1671 
1673  &key);
1674  if (NULL != pending_reversal && (NULL == sender ||
1675  0 != memcmp (sender, &pending_reversal->target, sizeof(struct GNUNET_PeerIdentity))))
1676  {
1678  "Removing invalid pending reversal for `%s'at `%s'\n",
1679  GNUNET_i2s (&pending_reversal->target),
1680  GNUNET_a2s (in, sizeof (in)));
1681  pending_reversals_delete_it (NULL, &key, pending_reversal);
1682  }
1683  GNUNET_free (natted_address);
1684  }
1685 }
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 1694 of file gnunet-communicator-tcp.c.

1695 {
1696  if (NULL != pq->listen_sock)
1697  {
1699  pq->listen_sock = NULL;
1700  }
1701  if (NULL != pq->read_task)
1702  {
1704  pq->read_task = NULL;
1705  }
1706  if (NULL != pq->write_task)
1707  {
1709  pq->write_task = NULL;
1710  }
1711  check_and_remove_pending_reversal (pq->address, pq->address->sa_family, NULL);
1713  GNUNET_free (pq->address);
1715  GNUNET_free (pq);
1716 }
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 1726 of file gnunet-communicator-tcp.c.

1727 {
1728  struct ProtoQueue *pq = cls;
1729  ssize_t sent;
1730  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In proto queue write\n");
1731  pq->write_task = NULL;
1732  if (0 != pq->write_off)
1733  {
1734  sent = GNUNET_NETWORK_socket_send (pq->sock,
1735  pq->write_buf,
1736  pq->write_off);
1738  "Sent %lu bytes to TCP queue\n", sent);
1739  if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1740  {
1742  free_proto_queue (pq);
1743  return;
1744  }
1745  if (sent > 0)
1746  {
1747  size_t usent = (size_t) sent;
1748  pq->write_off -= usent;
1749  memmove (pq->write_buf,
1750  &pq->write_buf[usent],
1751  pq->write_off);
1752  }
1753  }
1754  /* do we care to write more? */
1755  if ((0 < pq->write_off))
1756  pq->write_task =
1758  pq->sock,
1760  pq);
1761 }
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:1546
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, ProtoQueue::sock, ProtoQueue::write_buf, ProtoQueue::write_off, and ProtoQueue::write_task.

Referenced by 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 1771 of file gnunet-communicator-tcp.c.

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

References BUF_SIZE, GNUNET_assert, GNUNET_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_UNIT_FOREVER_REL, GNUNET_YES, inject_rekey(), queue(), queue_destroy(), GNUNET_TIME_Relative::rel_value_us, and reschedule_queue_timeout().

Referenced by mq_init(), mq_send(), proto_read_kx(), queue_read_kx(), 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

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

1867 {
1868  const struct GNUNET_MessageHeader *hdr =
1869  (const struct GNUNET_MessageHeader *) queue->pread_buf;
1870  const struct TCPConfirmationAck *tca = (const struct
1871  TCPConfirmationAck *) queue->pread_buf;
1872  const struct TCPBox *box = (const struct TCPBox *) queue->pread_buf;
1873  const struct TCPRekey *rekey = (const struct TCPRekey *) queue->pread_buf;
1874  const struct TCPFinish *fin = (const struct TCPFinish *) queue->pread_buf;
1875  struct TCPRekey rekeyz;
1876  struct TCPFinish finz;
1877  struct GNUNET_ShortHashCode tmac;
1878  uint16_t type;
1879  size_t size = 0; /* make compiler happy */
1880  struct TcpHandshakeAckSignature thas;
1881  const struct GNUNET_CRYPTO_ChallengeNonceP challenge = queue->challenge;
1882 
1884  "try handle plaintext!\n");
1885 
1886  if ((sizeof(*hdr) > queue->pread_off))
1887  {
1889  "Handling plaintext, not even a header!\n");
1890  return 0; /* not even a header */
1891  }
1892 
1894  {
1896  "Already received data of size %lu bigger than KX size %lu!\n",
1899  GNUNET_break_op (0);
1900  queue_finish (queue);
1901  return 0;
1902  }
1903 
1904  type = ntohs (hdr->type);
1905  switch (type)
1906  {
1909  "start processing ack\n");
1910  if (sizeof(*tca) > queue->pread_off)
1911  {
1913  "Handling plaintext size of tca greater than pread offset.\n");
1914  return 0;
1915  }
1916  if (ntohs (hdr->size) != sizeof(*tca))
1917  {
1919  "Handling plaintext size does not match message type.\n");
1920  GNUNET_break_op (0);
1921  queue_finish (queue);
1922  return 0;
1923  }
1924 
1925  thas.purpose.purpose = htonl (
1927  thas.purpose.size = htonl (sizeof(thas));
1928  thas.sender = tca->sender;
1929  thas.receiver = my_identity;
1930  thas.monotonic_time = tca->monotonic_time;
1931  thas.challenge = tca->challenge;
1932 
1935  &thas,
1936  &tca->sender_sig,
1937  &tca->sender.public_key))
1938  {
1940  "Verification of signature failed!\n");
1941  GNUNET_break (0);
1942  queue_finish (queue);
1943  return 0;
1944  }
1945  if (0 != GNUNET_memcmp (&tca->challenge, &challenge))
1946  {
1948  "Challenge in TCPConfirmationAck not correct!\n");
1949  GNUNET_break (0);
1950  queue_finish (queue);
1951  return 0;
1952  }
1953 
1954  queue->handshake_ack_monotime_get = GNUNET_PEERSTORE_iterate (peerstore,
1955  "transport_tcp_communicator",
1956  &queue->target,
1958  &
1960  queue);
1961 
1963  "Handling plaintext, ack processed!\n");
1964 
1966  {
1967  send_challenge (queue->challenge_received, queue);
1968  queue->write_task =
1970  queue->sock,
1971  &queue_write,
1972  queue);
1973  }
1974  else if (GNUNET_TRANSPORT_CS_OUTBOUND == queue->cs)
1975  {
1976  check_and_remove_pending_reversal (queue->address, queue->address->sa_family, NULL);
1977  }
1978 
1979  unverified_size = -1;
1980 
1981  char *foreign_addr;
1982 
1983  switch (queue->address->sa_family)
1984  {
1985  case AF_INET:
1986  GNUNET_asprintf (&foreign_addr,
1987  "%s-%s",
1989  GNUNET_a2s (queue->address, queue->address_len));
1990  break;
1991 
1992  case AF_INET6:
1993  GNUNET_asprintf (&foreign_addr,
1994  "%s-%s",
1996  GNUNET_a2s (queue->address, queue->address_len));
1997  break;
1998 
1999  default:
2000  GNUNET_assert (0);
2001  }
2002 
2004  &queue->target,
2005  foreign_addr,
2006  UINT16_MAX, /* no MTU */
2008  0, /* Priority */
2009  queue->nt,
2010  queue->cs,
2011  queue->mq);
2012 
2013  GNUNET_free (foreign_addr);
2014 
2015  size = ntohs (hdr->size);
2016  break;
2018  /* Special case: header size excludes box itself! */
2019  if (ntohs (hdr->size) + sizeof(struct TCPBox) > queue->pread_off)
2020  return 0;
2021  calculate_hmac (&queue->in_hmac, &box[1], ntohs (hdr->size), &tmac);
2022  if (0 != memcmp (&tmac, &box->hmac, sizeof(tmac)))
2023  {
2024  GNUNET_break_op (0);
2025  queue_finish (queue);
2026  return 0;
2027  }
2028  pass_plaintext_to_core (queue, (const void *) &box[1], ntohs (hdr->size));
2029  size = ntohs (hdr->size) + sizeof(*box);
2031  "Handling plaintext, box processed!\n");
2032  break;
2033 
2035  if (sizeof(*rekey) > queue->pread_off)
2036  return 0;
2037  if (ntohs (hdr->size) != sizeof(*rekey))
2038  {
2039  GNUNET_break_op (0);
2040  queue_finish (queue);
2041  return 0;
2042  }
2043  rekeyz = *rekey;
2044  memset (&rekeyz.hmac, 0, sizeof(rekeyz.hmac));
2045  calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
2046  if (0 != memcmp (&tmac, &rekey->hmac, sizeof(tmac)))
2047  {
2048  GNUNET_break_op (0);
2049  queue_finish (queue);
2050  return 0;
2051  }
2052  do_rekey (queue, rekey);
2053  size = ntohs (hdr->size);
2055  "Handling plaintext, rekey processed!\n");
2056  break;
2057 
2059  if (sizeof(*fin) > queue->pread_off)
2060  return 0;
2061  if (ntohs (hdr->size) != sizeof(*fin))
2062  {
2063  GNUNET_break_op (0);
2064  queue_finish (queue);
2065  return 0;
2066  }
2067  finz = *fin;
2068  memset (&finz.hmac, 0, sizeof(finz.hmac));
2069  calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
2070  if (0 != memcmp (&tmac, &fin->hmac, sizeof(tmac)))
2071  {
2072  GNUNET_break_op (0);
2073  queue_finish (queue);
2074  return 0;
2075  }
2076  /* handle FINISH by destroying queue */
2077  queue_destroy (queue);
2079  "Handling plaintext, finish processed!\n");
2080  break;
2081 
2082  default:
2084  "Handling plaintext, nothing processed!\n");
2085  GNUNET_break_op (0);
2086  queue_finish (queue);
2087  return 0;
2088  }
2089  GNUNET_assert (0 != size);
2090  if (-1 != unverified_size)
2091  unverified_size += size;
2092  return size;
2093 }
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.
size_t unverified_size
Size of data received without KX challenge played back.
#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.
#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.
#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)
static unsigned int size
Size of the "table".
Definition: peer.c:68
Type of a nonce used for challenges.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
TCP message box.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model

References calculate_hmac(), ch, 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_PEERSTORE_iterate(), GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE_ACK, GNUNET_SCHEDULER_add_write_net(), GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE_ACK, 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, handshake_ack_monotime_cb(), TCPFinish::hmac, TCPBox::hmac, INITIAL_CORE_KX_SIZE, my_identity, pass_plaintext_to_core(), peerstore, queue(), queue_destroy(), queue_finish(), queue_write(), send_challenge(), GNUNET_MessageHeader::size, size, GNUNET_MessageHeader::type, type, and unverified_size.

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

2218 {
2219  struct sockaddr *in;
2220 
2221  v6.sin6_family = AF_INET6;
2222  v6.sin6_port = htons ((uint16_t) port);
2223 #if HAVE_SOCKADDR_IN_SIN_LEN
2224  v6.sin6_len = sizeof(sizeof(struct sockaddr_in6));
2225 #endif
2226  v6.sin6_flowinfo = 0;
2227  v6.sin6_scope_id = 0;
2228  in = GNUNET_memdup (&v6, sizeof(v6));
2229  *sock_len = sizeof(struct sockaddr_in6);
2230 
2231  return in;
2232 }
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 2243 of file gnunet-communicator-tcp.c.

2246 {
2247  struct sockaddr *in;
2248 
2249  v4.sin_family = AF_INET;
2250  v4.sin_port = htons ((uint16_t) port);
2251 #if HAVE_SOCKADDR_IN_SIN_LEN
2252  v4.sin_len = sizeof(struct sockaddr_in);
2253 #endif
2254  in = GNUNET_memdup (&v4, sizeof(v4));
2255  *sock_len = sizeof(struct sockaddr_in);
2256  return in;
2257 }

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

2268 {
2269  struct PortOnlyIpv4Ipv6 *po;
2270  struct sockaddr_in *i4;
2271  struct sockaddr_in6 *i6;
2272  socklen_t sock_len_ipv4;
2273  socklen_t sock_len_ipv6;
2274 
2275  /* interpreting value as just a PORT number */
2276  if (*port > UINT16_MAX)
2277  {
2279  "BINDTO specification `%s' invalid: value too large for port\n",
2280  bindto);
2281  return NULL;
2282  }
2283 
2284  po = GNUNET_new (struct PortOnlyIpv4Ipv6);
2285 
2286  if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
2287  (GNUNET_YES ==
2290  "DISABLE_V6")))
2291  {
2292  i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2293  po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2294  *port);
2295  po->addr_len_ipv4 = sock_len_ipv4;
2296  }
2297  else
2298  {
2299 
2300  i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2301  po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2302  *port);
2303  po->addr_len_ipv4 = sock_len_ipv4;
2304 
2305  i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
2306  po->addr_ipv6 = tcp_address_to_sockaddr_numeric_v6 (&sock_len_ipv6, *i6,
2307  *port);
2308 
2309  po->addr_len_ipv6 = sock_len_ipv6;
2310 
2311  GNUNET_free (i6);
2312  }
2313 
2314  GNUNET_free (i4);
2315 
2316  return po;
2317 }
#define COMMUNICATOR_CONFIG_SECTION
Configuration section used by the communicator.
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 *`.
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 *`.
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".
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_test_pf(int pf)
Test if the given protocol family is supported by this system.
Definition: network.c:79
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, cfg, COMMUNICATOR_CONFIG_SECTION, GNUNET_CONFIGURATION_get_value_yesno(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_NETWORK_test_pf(), GNUNET_new, GNUNET_NO, 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 2327 of file gnunet-communicator-tcp.c.

2328 {
2329  char *addr;
2330  char *start;
2331  char *token;
2332  char *cp;
2333  char *rest = NULL;
2334  char *res;
2335 
2337  "extract address with bindto %s\n",
2338  bindto);
2339 
2340  if (NULL == bindto)
2342  "bindto is NULL\n");
2343 
2344  cp = GNUNET_strdup (bindto);
2345 
2347  "extract address 2\n");
2348 
2349  start = cp;
2350  if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
2351  {
2352  start++; /* skip over '['*/
2353  cp[strlen (cp) - 1] = '\0'; /* eat ']'*/
2354  addr = GNUNET_strdup (start);
2355  }
2356  else
2357  {
2358  token = strtok_r (cp, "]", &rest);
2359  if (strlen (bindto) == strlen (token))
2360  {
2361  token = strtok_r (cp, ":", &rest);
2362  addr = GNUNET_strdup (token);
2363  }
2364  else
2365  {
2366  token++;
2367  res = GNUNET_strdup (token);
2368  addr = GNUNET_strdup (res);
2369  }
2370  }
2371 
2373  "tcp address: %s\n",
2374  addr);
2375  GNUNET_free (cp);
2376  return addr;
2377 }
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
static int res
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.

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

Referenced by run(), and tcp_address_to_sockaddr().

Here is the caller graph for this function:

◆ extract_port()

static unsigned int extract_port ( const char *  addr_and_port)
static

This Method extracts the port part of the BINDTO string.

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

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

2388 {
2389  unsigned int port;
2390  char dummy[2];
2391  char *token;
2392  char *addr;
2393  char *colon;
2394  char *cp;
2395  char *rest = NULL;
2396 
2397  if (NULL != addr_and_port)
2398  {
2399  cp = GNUNET_strdup (addr_and_port);
2400  token = strtok_r (cp, "]", &rest);
2401  if (strlen (addr_and_port) == strlen (token))
2402  {
2403  colon = strrchr (cp, ':');
2404  if (NULL == colon)
2405  {
2406  GNUNET_free (cp);
2407  return 0;
2408  }
2409  addr = colon;
2410  addr++;
2411  }
2412  else
2413  {
2414  token = strtok_r (NULL, "]", &rest);
2415  if (NULL == token)
2416  {
2417  GNUNET_free (cp);
2418  return 0;
2419  }
2420  else
2421  {
2422  addr = token;
2423  addr++;
2424  }
2425  }
2426 
2427 
2428  if (1 == sscanf (addr, "%u%1s", &port, dummy))
2429  {
2430  /* interpreting value as just a PORT number */
2431  if (port > UINT16_MAX)
2432  {
2434  "Port `%u' invalid: value too large for port\n",
2435  port);
2436  GNUNET_free (cp);
2437  return 0;
2438  }
2439  }
2440  else
2441  {
2443  "BINDTO specification invalid: last ':' not followed by number\n");
2444  GNUNET_free (cp);
2445  return 0;
2446  }
2447  GNUNET_free (cp);
2448  }
2449  else
2450  {
2452  "return 0\n");
2453  /* interpret missing port as 0, aka pick any free one */
2454  port = 0;
2455  }
2456 
2457  return port;
2458 }
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 2469 of file gnunet-communicator-tcp.c.

2470 {
2471  struct sockaddr *in;
2472  unsigned int port;
2473  struct sockaddr_in v4;
2474  struct sockaddr_in6 v6;
2475  char *start;
2476 
2477  memset (&v4, 0, sizeof(v4));
2478  start = extract_address (bindto);
2479  GNUNET_assert (NULL != start);
2481  "start %s\n",
2482  start);
2483 
2485  "!bindto %s\n",
2486  bindto);
2487 
2488 
2489  if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
2490  {
2491  // colon = strrchr (cp, ':');
2492  port = extract_port (bindto);
2493 
2495  "port %u\n",
2496  port);
2497 
2498  in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port);
2499  }
2500  else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
2501  {
2502  // colon = strrchr (cp, ':');
2503  port = extract_port (bindto);
2504  in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port);
2505  }
2506  else
2507  {
2508  GNUNET_assert (0);
2509  }
2510 
2511  GNUNET_free (start);
2512  return in;
2513 }
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 2525 of file gnunet-communicator-tcp.c.

2528 {
2529  struct Queue *queue = impl_state;
2530  uint16_t msize = ntohs (msg->size);
2531  struct TCPBox box;
2533  "In MQ send. Queue finishing: %s; write task running: %s\n",
2534  (GNUNET_YES == queue->finishing) ? "yes" : "no",
2535  (NULL == queue->write_task) ? "yes" : "no");
2536  GNUNET_assert (mq == queue->mq);
2537  queue->mq_awaits_continue = GNUNET_YES;
2538  if (GNUNET_YES == queue->finishing)
2539  return; /* this queue is dying, drop msg */
2540  GNUNET_assert (0 == queue->pwrite_off);
2541  box.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX);
2542  box.header.size = htons (msize);
2543  calculate_hmac (&queue->out_hmac, msg, msize, &box.hmac);
2544  memcpy (&queue->pwrite_buf[queue->pwrite_off], &box, sizeof(box));
2545  queue->pwrite_off += sizeof(box);
2546  memcpy (&queue->pwrite_buf[queue->pwrite_off], msg, msize);
2547  queue->pwrite_off += msize;
2549  "%lu bytes of plaintext to send\n", queue->pwrite_off);
2550  GNUNET_assert (NULL != queue->sock);
2551  if (NULL == queue->write_task)
2552  queue->write_task =
2554  queue->sock,
2555  &queue_write,
2556  queue);
2557 }
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 2569 of file gnunet-communicator-tcp.c.

2570 {
2571  struct Queue *queue = impl_state;
2572 
2573  if (mq == queue->mq)
2574  {
2575  queue->mq = NULL;
2576  queue_finish (queue);
2577  }
2578 }

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

2589 {
2590  struct Queue *queue = impl_state;
2591 
2592  GNUNET_assert (0 != queue->pwrite_off);
2593  queue->pwrite_off = 0;
2594 }

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

2608 {
2609  struct Queue *queue = cls;
2610 
2612  "MQ error in queue to %s: %d\n",
2613  GNUNET_i2s (&queue->target),
2614  (int) error);
2615  queue_finish (queue);
2616 }

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

2628 {
2629  queue->nt =
2630  GNUNET_NT_scanner_get_type (is, queue->address, queue->address_len);
2632  queue_map,
2633  &queue->target,
2634  queue,
2637  "# queues active",
2639  GNUNET_NO);
2640  queue->timeout =
2643  &mq_destroy,
2644  &mq_cancel,
2645  queue,
2646  NULL,
2647  &mq_error,
2648  queue);
2649 }
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.
int GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE
Allow multiple values with the same key.
struct GNUNET_MQ_Handle * GNUNET_MQ_queue_for_callbacks(GNUNET_MQ_SendImpl send, GNUNET_MQ_DestroyImpl destroy, GNUNET_MQ_CancelImpl cancel, void *impl_state, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *cls)
Create a message queue for the specified handlers.
Definition: mq.c:465
enum GNUNET_NetworkType GNUNET_NT_scanner_get_type(struct GNUNET_NT_InterfaceScanner *is, const struct sockaddr *addr, socklen_t addrlen)
Returns where the address is located: loopback, LAN or WAN.
Definition: nt.c:308

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

Referenced by mq_init(), and proto_read_kx().

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

◆ transmit_kx()

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

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

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

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

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

2665 {
2666  struct TcpHandshakeSignature ths;
2667  struct TCPConfirmation tc;
2668 
2669  memcpy (queue->cwrite_buf, epub, sizeof(*epub));
2670  queue->cwrite_off = sizeof(*epub);
2671  /* compute 'tc' and append in encrypted format to cwrite_buf */
2672  tc.sender = my_identity;
2673  tc.monotonic_time =
2676  &tc.challenge,
2677  sizeof(tc.challenge));
2678  ths.purpose.purpose = htonl (
2680  ths.purpose.size = htonl (sizeof(ths));
2681  ths.sender = my_identity;
2682  ths.receiver = queue->target;
2683  ths.ephemeral = *epub;
2684  ths.monotonic_time = tc.monotonic_time;
2685  ths.challenge = tc.challenge;
2687  &ths,
2688  &tc.sender_sig);
2689  GNUNET_assert (0 ==
2690  gcry_cipher_encrypt (queue->out_cipher,
2691  &queue->cwrite_buf[queue->cwrite_off],
2692  sizeof(tc),
2693  &tc,
2694  sizeof(tc)));
2695  queue->challenge = tc.challenge;
2696  queue->cwrite_off += sizeof(tc);
2697 
2699  "transport",
2700  "handshake written\n");
2701 }
#define GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE
Signature used by TCP communicator handshake.
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:424
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_from_nocheck(), 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 2712 of file gnunet-communicator-tcp.c.

2713 {
2714  struct GNUNET_CRYPTO_EcdhePublicKey epub;
2715 
2716  GNUNET_CRYPTO_ecdhe_key_create (&queue->ephemeral);
2717  GNUNET_CRYPTO_ecdhe_key_get_public (&queue->ephemeral, &epub);
2719  transmit_kx (queue, &epub);
2720 }
static void transmit_kx(struct Queue *queue, const struct GNUNET_CRYPTO_EcdhePublicKey *epub)
Generate and transmit our ephemeral key and the signature for the initial KX with the other peer.

References GNUNET_CRYPTO_ecdhe_key_create(), GNUNET_CRYPTO_ecdhe_key_get_public(), 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 2729 of file gnunet-communicator-tcp.c.

2730 {
2731  struct Queue *queue = cls;
2732  if (GNUNET_OK != success)
2733  {
2735  "Failed to store handshake monotonic time in PEERSTORE!\n");
2736  }
2737  queue->handshake_monotime_sc = NULL;
2738 }

References GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_OK, 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 2749 of file gnunet-communicator-tcp.c.

2752 {
2753  struct Queue *queue = cls;
2754  struct GNUNET_TIME_AbsoluteNBO *mtbe;
2755  struct GNUNET_TIME_Absolute mt;
2756  const struct GNUNET_PeerIdentity *pid;
2757  struct GNUNET_TIME_AbsoluteNBO *handshake_monotonic_time;
2758 
2759  (void) emsg;
2760 
2761  handshake_monotonic_time = &queue->handshake_monotonic_time;
2762  pid = &queue->target;
2764  "tcp handshake with us %s\n",
2765  GNUNET_i2s (&my_identity));
2766  if (NULL == record)
2767  {
2768  queue->handshake_monotime_get = NULL;
2769  return;
2770  }
2772  "tcp handshake from peer %s\n",
2773  GNUNET_i2s (pid));
2774  if (sizeof(*mtbe) != record->value_size)
2775  {
2776  GNUNET_break (0);
2777  return;
2778  }
2779  mtbe = record->value;
2780  mt = GNUNET_TIME_absolute_ntoh (*mtbe);
2781  if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
2782  queue->handshake_monotonic_time).abs_value_us)
2783  {
2785  "Queue from %s dropped, handshake monotime in the past\n",
2786  GNUNET_i2s (&queue->target));
2787  GNUNET_break (0);
2788  queue_finish (queue);
2789  return;
2790  }
2791  queue->handshake_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
2792  "transport_tcp_communicator",
2793  pid,
2795  handshake_monotonic_time,
2796  sizeof(*
2797  handshake_monotonic_time),
2800  &
2802  queue);
2803 }
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_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 2818 of file gnunet-communicator-tcp.c.

2821 {
2822  struct TcpHandshakeSignature ths;
2824 
2825  GNUNET_assert (
2826  0 ==
2827  gcry_cipher_decrypt (queue->in_cipher,
2828  tc,
2829  sizeof(*tc),
2830  &ibuf[sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)],
2831  sizeof(*tc)));
2832  ths.purpose.purpose = htonl (
2834  ths.purpose.size = htonl (sizeof(ths));
2835  ths.sender = tc->sender;
2836  ths.receiver = my_identity;
2837  memcpy (&ths.ephemeral, ibuf, sizeof(struct GNUNET_CRYPTO_EcdhePublicKey));
2838  ths.monotonic_time = tc->monotonic_time;
2839  ths.challenge = tc->challenge;
2842  &ths,
2843  &tc->sender_sig,
2844  &tc->sender.public_key);
2845  if (GNUNET_YES == ret)
2846  queue->handshake_monotime_get =
2848  "transport_tcp_communicator",
2849  &queue->target,
2852  queue);
2853  return ret;
2854 }
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_iterate(), 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 2865 of file gnunet-communicator-tcp.c.

2866 {
2867  struct Queue *queue = cls;
2868  ssize_t rcvd;
2869  struct GNUNET_TIME_Relative left;
2870  struct TCPConfirmation tc;
2871 
2872  queue->read_task = NULL;
2873  left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2874  if (0 == left.rel_value_us)
2875  {
2876  queue_destroy (queue);
2877  return;
2878  }
2879  rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2880  &queue->cread_buf[queue->cread_off],
2881  BUF_SIZE - queue->cread_off);
2883  "Received %lu bytes for KX\n",
2884  rcvd);
2886  "transport",
2887  "Received %lu bytes for KX\n",
2888  rcvd);
2889  if (-1 == rcvd)
2890  {
2891  if ((EAGAIN != errno) && (EINTR != errno))
2892  {
2894  queue_destroy (queue);
2895  return;
2896  }
2897  queue->read_task =
2899  return;
2900  }
2901  queue->cread_off += rcvd;
2902  if (queue->cread_off < INITIAL_KX_SIZE)
2903  {
2904  /* read more */
2905  queue->read_task =
2907  return;
2908  }
2909  /* we got all the data, let's find out who we are talking to! */
2911  queue->cread_buf,
2912  queue);
2913  if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, queue->cread_buf))
2914  {
2916  "Invalid TCP KX received from %s\n",
2917  GNUNET_a2s (queue->address, queue->address_len));
2918  queue_destroy (queue);
2919  return;
2920  }
2921  if (0 !=
2922  memcmp (&tc.sender, &queue->target, sizeof(struct GNUNET_PeerIdentity)))
2923  {
2925  "Invalid sender in TCP KX received from %s\n",
2926  GNUNET_a2s (queue->address, queue->address_len));
2927  queue_destroy (queue);
2928  return;
2929  }
2930  send_challenge (tc.challenge, queue);
2931  queue->write_task =
2933  queue->sock,
2934  &queue_write,
2935  queue);
2936 
2937  /* update queue timeout */
2939  /* prepare to continue with regular read task immediately */
2940  memmove (queue->cread_buf,
2941  &queue->cread_buf[INITIAL_KX_SIZE],
2942  queue->cread_off - (INITIAL_KX_SIZE));
2944  "cread_off is %lu bytes before adjusting\n",
2945  queue->cread_off);
2946  queue->cread_off -= INITIAL_KX_SIZE;
2948  "cread_off set to %lu bytes\n",
2949  queue->cread_off);
2951 }
static void queue_read_kx(void *cls)
Read from the socket of the queue until we have enough data to initialize the decryption logic and ca...
#define INITIAL_KX_SIZE
Size of the initial key exchange message sent first in both directions.
static int decrypt_and_check_tc(struct Queue *queue, struct TCPConfirmation *tc, char *ibuf)
We have received the first bytes from the other side on a queue.
@ GNUNET_ERROR_TYPE_INFO
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1268

References BUF_SIZE, decrypt_and_check_tc(), GNUNET_a2s(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_log_from_nocheck(), 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_UNIT_FOREVER_REL, INITIAL_KX_SIZE, queue(), queue_destroy(), queue_read(), queue_write(), GNUNET_TIME_Relative::rel_value_us, reschedule_queue_timeout(), send_challenge(), setup_in_cipher(), and tc.

Referenced by mq_init(), and proto_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 2961 of file gnunet-communicator-tcp.c.

2962 {
2963  struct ProtoQueue *pq = cls;
2964  ssize_t rcvd;
2965  struct GNUNET_TIME_Relative left;
2966  struct Queue *queue;
2967  struct TCPConfirmation tc;
2969 
2970  pq->read_task = NULL;
2972  if (0 == left.rel_value_us)
2973  {
2974  free_proto_queue (pq);
2975  return;
2976  }
2977  rcvd = GNUNET_NETWORK_socket_recv (pq->sock,
2978  &pq->ibuf[pq->ibuf_off],
2979  sizeof(pq->ibuf) - pq->ibuf_off);
2981  "Proto received %lu bytes for KX\n", rcvd);
2983  "transport",
2984  "Proto received %lu bytes for KX\n", rcvd);
2985  if (-1 == rcvd)
2986  {
2987  if ((EAGAIN != errno) && (EINTR != errno))
2988  {
2990  free_proto_queue (pq);
2991  return;
2992  }
2993  /* try again */
2994  pq->read_task =
2996  return;
2997  }
2998  pq->ibuf_off += rcvd;
2999  if (sizeof (struct TCPNATProbeMessage) == pq->ibuf_off)
3000  {
3001  struct TCPNATProbeMessage *pm = (struct TCPNATProbeMessage *) pq->ibuf;
3002 
3003  check_and_remove_pending_reversal (pq->address, pq->address->sa_family, &pm->clientIdentity);
3004 
3005  queue = GNUNET_new (struct Queue);
3006  queue->target = pm->clientIdentity;
3009  }
3010  else if (pq->ibuf_off > sizeof(pq->ibuf))
3011  {
3012  /* read more */
3013  pq->read_task =
3015  return;
3016  }
3017  else
3018  {
3019  /* we got all the data, let's find out who we are talking to! */
3020  queue = GNUNET_new (struct Queue);
3021  setup_in_cipher ((const struct GNUNET_CRYPTO_EcdhePublicKey *) pq->ibuf,
3022  queue);
3023  if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, pq->ibuf))
3024  {
3026  "Invalid TCP KX received from %s\n",
3027  GNUNET_a2s (pq->address, pq->address_len));
3028  gcry_cipher_close (queue->in_cipher);
3029  GNUNET_free (queue);
3030  free_proto_queue (pq);
3031  return;
3032  }
3033  queue->target = tc.sender;
3035  read_task = &queue_read;
3036  }
3037  queue->address = pq->address; /* steals reference */
3038  queue->address_len = pq->address_len;
3039  queue->listen_sock = pq->listen_sock;
3040  queue->sock = pq->sock;
3041 
3043  "created queue with target %s\n",
3044  GNUNET_i2s (&queue->target));
3045 
3047  "start kx proto\n");
3048 
3050  boot_queue (queue);
3051  queue->read_task =
3053  queue->sock,
3054  read_task,
3055  queue);
3056  queue->write_task =
3058  queue->sock,
3059  &queue_write,
3060  queue);
3061  // TODO To early! Move it somewhere else.
3062  // send_challenge (tc.challenge, queue);
3063  queue->challenge_received = tc.challenge;
3064 
3066  GNUNET_free (pq);
3067 }
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_from_nocheck(), 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_tail, queue(), queue_read(), queue_read_kx(), queue_write(), read_task, ProtoQueue::read_task, GNUNET_TIME_Relative::rel_value_us, setup_in_cipher(), ProtoQueue::sock, start_initial_kx_out(), tc, and ProtoQueue::timeout.

Referenced by create_proto_queue().

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

3073 {
3074  struct ProtoQueue *pq = GNUNET_new (struct ProtoQueue);
3075 
3076  if (NULL == sock)
3077  {
3078  //sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
3079  sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, 0);
3080  if (NULL == sock)
3081  {
3083  "socket(%d) failed: %s",
3084  in->sa_family,
3085  strerror (errno));
3086  GNUNET_free (in);
3087  return NULL;
3088  }
3089  if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, addrlen)) &&
3090  (errno != EINPROGRESS))
3091  {
3093  "connect to `%s' failed: %s",
3094  GNUNET_a2s (in, addrlen),
3095  strerror (errno));
3097  GNUNET_free (in);
3098  return NULL;
3099  }
3100  }
3101  pq->address_len = addrlen;
3102  pq->address = in;
3104  pq->sock = sock;
3106  pq->sock,
3107  &proto_read_kx,
3108  pq);
3110 
3111  return pq;
3112 }
#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.
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
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:832

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

3166 {
3167  (void) cls;
3168  struct TCPNATProbeMessage pm;
3169  struct ProtoQueue *pq;
3170  struct sockaddr *in_addr;
3171 
3173  "addr->sa_family %d\n",
3174  addr->sa_family);
3176  "Try to connect back\n");
3177  in_addr = GNUNET_memdup (addr, addrlen);
3179  "in_addr->sa_family %d\n",
3180  in_addr->sa_family);
3181  pq = create_proto_queue (NULL, in_addr, addrlen);
3182  if (NULL != pq)
3183  {
3184  pm.header.size = htons (sizeof(struct TCPNATProbeMessage));
3185  pm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
3186  pm.clientIdentity = my_identity;
3187  memcpy (pq->write_buf, &pm, sizeof(struct TCPNATProbeMessage));
3188  pq->write_off = sizeof(struct TCPNATProbeMessage);
3190  pq->sock,
3192  pq);
3193  }
3194  else
3195  {
3197  "Couldn't create ProtoQueue for sending TCPNATProbeMessage\n");
3198  }
3199 }
#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 3203 of file gnunet-communicator-tcp.c.

3204 {
3205  struct sockaddr *in = cls;
3206  struct PendingReversal *pending_reversal;
3207  struct GNUNET_HashCode key;
3208 
3209  GNUNET_CRYPTO_hash (in,
3210  sizeof(struct sockaddr),
3211  &key);
3213  &key);
3214 
3215  GNUNET_assert (NULL != pending_reversal);
3216 
3218  &key,
3219  pending_reversal);
3220  GNUNET_free (pending_reversal->in);
3221  GNUNET_free (pending_reversal);
3222 }

References GNUNET_assert, GNUNET_CONTAINER_multihashmap_get(), GNUNET_CONTAINER_multihashmap_remove(), GNUNET_CRYPTO_hash(), GNUNET_free, 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 3244 of file gnunet-communicator-tcp.c.

3245 {
3246  struct sockaddr *in;
3247  socklen_t in_len = 0;
3248  const char *path;
3249  struct sockaddr_in *v4;
3250  struct sockaddr_in6 *v6;
3251  unsigned int is_natd = GNUNET_NO;
3252  struct GNUNET_HashCode key;
3253 
3255  "Connecting to %s at %s\n",
3256  GNUNET_i2s (peer),
3257  address);
3258  if (0 != strncmp (address,
3260  strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
3261  {
3262  GNUNET_break_op (0);
3263  return GNUNET_SYSERR;
3264  }
3265  path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
3266  in = tcp_address_to_sockaddr (path, &in_len);
3267 
3268  if (NULL == in)
3269  {
3271  "Failed to setup TCP socket address\n");
3272  return GNUNET_SYSERR;
3273  }
3274 
3276  "in %s\n",
3277  GNUNET_a2s (in, in_len));
3278 
3279  switch (in->sa_family)
3280  {
3281  case AF_INET:
3282  v4 = (struct sockaddr_in *) in;
3283  if (0 == v4->sin_port){
3284  is_natd = GNUNET_YES;
3285  GNUNET_CRYPTO_hash (in,
3286  sizeof(struct sockaddr),
3287  &key);
3289  &key))
3290  {
3292  "There is already a request reversal for `%s'at `%s'\n",
3293  GNUNET_i2s (peer),
3294  address);
3295  GNUNET_free (in);
3296  return GNUNET_SYSERR;
3297  }
3298  }
3299  break;
3300 
3301  case AF_INET6:
3302  v6 = (struct sockaddr_in6 *) in;
3303  if (0 == v6->sin6_port)
3304  {
3306  "Request reversal for `%s' at `%s' not possible for an IPv6 address\n",
3307  GNUNET_i2s (peer),
3308  address);
3309  GNUNET_free (in);
3310  return GNUNET_SYSERR;
3311  }
3312  break;
3313 
3314  default:
3315  GNUNET_assert (0);
3316  }
3317 
3318  if (GNUNET_YES == is_natd)
3319  {
3320  struct sockaddr_in local_sa;
3321  struct PendingReversal *pending_reversal;
3322 
3323  memset (&local_sa, 0, sizeof(local_sa));
3324  local_sa.sin_family = AF_INET;
3325  local_sa.sin_port = htons (bind_port);
3326  /* We leave sin_address at 0, let the kernel figure it out,
3327  even if our bind() is more specific. (May want to reconsider
3328  later.) */
3329  if (GNUNET_OK != GNUNET_NAT_request_reversal (nat, &local_sa, v4))
3330  {
3332  "request reversal for `%s' at `%s' failed\n",
3333  GNUNET_i2s (peer),
3334  address);
3335  GNUNET_free (in);
3336  return GNUNET_SYSERR;
3337  }
3338  pending_reversal = GNUNET_new (struct PendingReversal);
3339  pending_reversal->in = in;
3342  &key,
3343  pending_reversal,
3345  pending_reversal->target = *peer;
3348  "Created NAT WAIT connection to `%s' at `%s'\n",
3349  GNUNET_i2s (peer),
3350  GNUNET_a2s (in, sizeof (struct sockaddr)));
3351  }
3352  else
3353  {
3354  struct GNUNET_NETWORK_Handle *sock;
3355  struct Queue *queue;
3356 
3357  sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP);
3358  if (NULL == sock)
3359  {
3361  "socket(%d) failed: %s",
3362  in->sa_family,
3363  strerror (errno));
3364  GNUNET_free (in);
3365  return GNUNET_SYSERR;
3366  }
3367  if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, in_len)) &&
3368  (errno != EINPROGRESS))
3369  {
3371  "connect to `%s' failed: %s",
3372  address,
3373  strerror (errno));
3375  GNUNET_free (in);
3376  return GNUNET_SYSERR;
3377  }
3378 
3379  queue = GNUNET_new (struct Queue);
3380  queue->target = *peer;
3381  queue->address = in;
3382  queue->address_len = in_len;
3383  queue->sock = sock;
3385  boot_queue (queue);
3387  "booted queue with target %s\n",
3388  GNUNET_i2s (&queue->target));
3389  // queue->mq_awaits_continue = GNUNET_YES;
3390  queue->read_task =
3392  queue->sock,
3393  &queue_read_kx,
3394  queue);
3395 
3396 
3398  "start kx mq_init\n");
3399 
3401  queue->write_task =
3403  queue->sock,
3404  &queue_write,
3405  queue);
3406  }
3407 
3408  return GNUNET_OK;
3409 }
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 sockaddr * tcp_address_to_sockaddr(const char *bindto, socklen_t *sock_len)
Convert TCP bind specification to a struct sockaddr *
static struct GNUNET_NAT_Handle * nat
Connection to NAT service.
static void pending_reversal_timeout(void *cls)
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).
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_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
int GNUNET_NAT_request_reversal(struct GNUNET_NAT_Handle *nh, const struct sockaddr_in *local_sa, const struct sockaddr_in *remote_sa)
We learned about a peer (possibly behind NAT) so run the gnunet-nat-client to send dummy ICMP respons...
Definition: nat_api.c:646
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:1241
struct GNUNET_NETWORK_Handle * sock
socket that we transmit all data with on this queue
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.

References address, bind_port, boot_queue(), COMMUNICATOR_ADDRESS_PREFIX, GNUNET_a2s(), GNUNET_assert, GNUNET_break_op, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_CONTAINER_multihashmap_contains(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_CRYPTO_hash(), 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, peer, pending_reversal_timeout(), pending_reversals, queue(), queue_read_kx(), queue_write(), Queue::sock, start_initial_kx_out(), PendingReversal::target, tcp_address_to_sockaddr(), and PendingReversal::timeout_task.

Referenced by GNUNET_TRANSPORT_communicator_connect(), and 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 3421 of file gnunet-communicator-tcp.c.

3424 {
3425  struct ListenTask *lt = value;
3426 
3427  (void) cls;
3428  (void) key;
3429  if (NULL != lt->listen_task)
3430  {
3432  lt->listen_task = NULL;
3433  }
3434  if (NULL != lt->listen_sock)
3435  {
3437  lt->listen_sock = NULL;
3438  }
3439  GNUNET_free (lt);
3440  return GNUNET_OK;
3441 }

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

3456 {
3457  struct Queue *queue = value;
3458 
3459  (void) cls;
3460  (void) target;
3461  queue_destroy (queue);
3462  return GNUNET_OK;
3463 }
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 3472 of file gnunet-communicator-tcp.c.

3473 {
3475  "Shutdown %s!\n",
3476  shutdown_running ? "running" : "not running");
3477 
3479  return;
3480  else
3482 
3483  while (NULL != proto_head)
3485  if (NULL != nat)
3486  {
3488  nat = NULL;
3489  }
3496  if (NULL != ch)
3497  {
3500  ch = NULL;
3501  }
3502  if (NULL != stats)
3503  {
3505  stats = NULL;
3506  }
3507  if (NULL != my_private_key)
3508  {
3510  my_private_key = NULL;
3511  }
3512  if (NULL != is)
3513  {
3515  is = NULL;
3516  }
3517  if (NULL != peerstore)
3518  {
3520  peerstore = NULL;
3521  }
3522  if (NULL != resolve_request_handle)
3523  {
3525  resolve_request_handle = NULL;
3526  }
3528  "Shutdown done!\n");
3529 }
static int get_queue_delete_it(void *cls, const struct GNUNET_PeerIdentity *target, void *value)
Iterator over all message queues to clean up.
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.
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_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
int GNUNET_CONTAINER_multipeermap_iterate(struct GNUNET_CONTAINER_MultiPeerMap *map, GNUNET_CONTAINER_PeerMapIterator 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:674
void GNUNET_NT_scanner_done(struct GNUNET_NT_InterfaceScanner *is)
Terminate interface scanner.
Definition: nt.c:427
void GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h, int sync_first)
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).
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.

References ch, free_proto_queue(), get_lt_delete_it(), get_queue_delete_it(), GNUNET_CONTAINER_multihashmap_destroy(), GNUNET_CONTAINER_multihashmap_iterate(), GNUNET_CONTAINER_multipeermap_destroy(), GNUNET_CONTAINER_multipeermap_iterate(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_NAT_unregister(), GNUNET_NO, 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 3544 of file gnunet-communicator-tcp.c.

3547 {
3548  (void) cls;
3549  (void) sender;
3550  (void) msg;
3551  GNUNET_break_op (0);
3552 }

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

3575 {
3576  char *my_addr;
3578 
3580  "nat address cb %s %s\n",
3581  add_remove ? "add" : "remove",
3582  GNUNET_a2s (addr, addrlen));
3583 
3584  if (GNUNET_YES == add_remove)
3585  {
3586  enum GNUNET_NetworkType nt;
3587 
3588  GNUNET_asprintf (&my_addr,
3589  "%s-%s",
3591  GNUNET_a2s (addr, addrlen));
3592  nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
3593  ai =
3595  my_addr,
3596  nt,
3598  GNUNET_free (my_addr);
3599  *app_ctx = ai;
3600  }
3601  else
3602  {
3603  ai = *app_ctx;
3605  *app_ctx = NULL;
3606  }
3607 }
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.
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
Definition: gnunet_nt_lib.h:39
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.
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 3614 of file gnunet-communicator-tcp.c.

3615 {
3616 
3617  struct Addresses *saddrs;
3618 
3620  "add address %s\n",
3621  GNUNET_a2s (in, in_len));
3622 
3623  saddrs = GNUNET_new (struct Addresses);
3624  saddrs->addr = in;
3625  saddrs->addr_len = in_len;
3627 
3629  "after add address %s\n",
3630  GNUNET_a2s (in, in_len));
3631 
3633  "add address %s\n",
3634  GNUNET_a2s (saddrs->addr, saddrs->addr_len));
3635 
3636  addrs_lens++;
3637 }
int addrs_lens
Number of addresses in the DLL for register at NAT service.
struct Addresses * addrs_head
Head of DLL with addresses we like to register at NAT servcie.
struct Addresses * addrs_tail
Head of DLL with addresses we like to register at NAT servcie.
DLL to store the addresses we like to register at NAT service.
struct sockaddr * addr
Address we like to register at NAT service.
socklen_t addr_len
Length of address we like to register at NAT service.

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

Referenced by init_socket().

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

◆ init_socket()

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

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

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

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

3650 {
3651  struct sockaddr_storage in_sto;
3652  socklen_t sto_len;
3653  struct GNUNET_NETWORK_Handle *listen_sock;
3654  struct ListenTask *lt;
3655  int sockfd;
3656  struct GNUNET_HashCode h_sock;
3657 
3658  if (NULL == addr)
3659  {
3661  "Address is NULL.\n");
3662  return GNUNET_SYSERR;
3663  }
3664 
3666  "address %s\n",
3667  GNUNET_a2s (addr, in_len));
3668 
3669  listen_sock =
3670  GNUNET_NETWORK_socket_create (addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
3671  if (NULL == listen_sock)
3672  {
3674  return GNUNET_SYSERR;
3675  }
3676 
3677  if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, addr, in_len))
3678  {
3680  GNUNET_NETWORK_socket_close (listen_sock);
3681  listen_sock = NULL;
3682  return GNUNET_SYSERR;
3683  }
3684 
3685  if (GNUNET_OK !=
3686  GNUNET_NETWORK_socket_listen (listen_sock,
3687  5))
3688  {
3690  "listen");
3691  GNUNET_NETWORK_socket_close (listen_sock);
3692  listen_sock = NULL;
3693  return GNUNET_SYSERR;
3694  }
3695 
3696  /* We might have bound to port 0, allowing the OS to figure it out;
3697  thus, get the real IN-address from the socket */
3698  sto_len = sizeof(in_sto);
3699 
3700  if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock),
3701  (struct sockaddr *) &in_sto,
3702  &sto_len))
3703  {
3704  memcpy (&in_sto, addr, in_len);
3705  sto_len = in_len;
3706  }
3707 
3708  // addr = (struct sockaddr *) &in_sto;
3709  in_len = sto_len;
3711  "Bound to `%s'\n",
3712  GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
3713  stats = GNUNET_STATISTICS_create ("C-TCP", cfg);
3714 
3715  if (NULL == is)
3717 
3718  if (NULL == my_private_key)
3720  if (NULL == my_private_key)
3721  {
3722  GNUNET_log (
3724  _ (
3725  "Transport service is lacking key configuration settings. Exiting.\n"));
3726  if (NULL != resolve_request_handle)
3729  return GNUNET_SYSERR;
3730  }
3732  /* start listening */
3733 
3734  lt = GNUNET_new (struct ListenTask);
3735  lt->listen_sock = listen_sock;
3736 
3738  listen_sock,
3739  &listen_cb,
3740  lt);
3741 
3743  "creating hash\n");
3744  sockfd = GNUNET_NETWORK_get_fd (lt->listen_sock);
3745  GNUNET_CRYPTO_hash (&sockfd,
3746  sizeof(int),
3747  &h_sock);
3748 
3750  "creating map\n");
3751  if (NULL == lt_map)
3753 
3755  "creating map entry\n");
3758  &h_sock,
3759  lt,
3761 
3763  "map entry created\n");
3764 
3765  if (NULL == queue_map)
3767 
3768  if (NULL == ch)
3773  &mq_init,
3774  NULL,
3775  &enc_notify_cb,
3776  NULL);
3777 
3778  if (NULL == ch)
3779  {
3780  GNUNET_break (0);
3781  if (NULL != resolve_request_handle)
3784  return GNUNET_SYSERR;
3785  }
3786 
3787  add_addr (addr, in_len);
3788  return GNUNET_OK;
3789 
3790 }
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 (!...
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_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.
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:197
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
struct GNUNET_CONTAINER_MultiPeerMap * GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
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:406
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:534
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics 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.
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
struct GNUNET_CRYPTO_EddsaPublicKey public_key

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_CONTAINER_multipeermap_create(), 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 3797 of file gnunet-communicator-tcp.c.

3798 {
3799  struct sockaddr **saddrs;
3800  socklen_t *saddr_lens;
3801  int i;
3802  size_t len;
3803 
3805  "starting nat register!\n");
3806  len = 0;
3807  i = 0;
3808  saddrs = GNUNET_malloc ((addrs_lens) * sizeof(struct sockaddr *));
3809  saddr_lens = GNUNET_malloc ((addrs_lens) * sizeof(socklen_t));
3810  for (struct Addresses *pos = addrs_head; NULL != pos; pos = pos->next)
3811  {
3813  "registering address %s\n",
3815 
3816  saddr_lens[i] = addrs_head->addr_len;
3817  len += saddr_lens[i];
3818  saddrs[i] = GNUNET_memdup (addrs_head->addr, saddr_lens[i]);
3819  i++;
3820  }
3821 
3823  "registering addresses %lu %lu %lu %lu\n",
3824  (addrs_lens) * sizeof(struct sockaddr *),
3825  (addrs_lens) * sizeof(socklen_t),
3826  len,
3827  sizeof(COMMUNICATOR_CONFIG_SECTION));
3830  IPPROTO_TCP,
3831  addrs_lens,
3832  (const struct sockaddr **) saddrs,
3833  saddr_lens,
3834  &nat_address_cb,
3836  NULL /* closure */);
3837  for (i = addrs_lens - 1; i >= 0; i--)
3838  GNUNET_free (saddrs[i]);
3839  GNUNET_free (saddrs);
3840  GNUNET_free (saddr_lens);
3841 
3842  if (NULL == nat)
3843  {
3844  GNUNET_break (0);
3845  if (NULL != resolve_request_handle)
3848  }
3849 }
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 ...
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
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(), len, 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 3860 of file gnunet-communicator-tcp.c.

3863 {
3864  struct sockaddr_in *v4;
3865  struct sockaddr_in6 *v6;
3866  struct sockaddr *in;
3867 
3868  (void) cls;
3869  if (NULL != addr)
3870  {
3871  if (AF_INET == addr->sa_family)
3872  {
3873  v4 = (struct sockaddr_in *) addr;
3874  in = tcp_address_to_sockaddr_numeric_v4 (&in_len, *v4, bind_port);// _global);
3875  }
3876  else if (AF_INET6 == addr->sa_family)
3877  {
3878  v6 = (struct sockaddr_in6 *) addr;
3879  in = tcp_address_to_sockaddr_numeric_v6 (&in_len, *v6, bind_port);// _global);
3880  }
3881  else
3882  {
3884  "Address family %u not suitable (not AF_INET %u nor AF_INET6 %u \n",
3885  addr->sa_family,
3886  AF_INET,
3887  AF_INET6);
3888  return;
3889  }
3890  init_socket (in, in_len);
3891  }
3892  else
3893  {
3895  "Address is NULL. This might be an error or the resolver finished resolving.\n");
3896  if (NULL == addrs_head)
3897  {
3899  "Resolver finished resolving, but we do not listen to an address!.\n");
3900  return;
3901  }
3902  nat_register ();
3903  }
3904 }
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 3916 of file gnunet-communicator-tcp.c.

3920 {
3921  char *bindto;
3922  struct sockaddr *in;
3923  socklen_t in_len;
3924  struct sockaddr_in v4;
3925  struct sockaddr_in6 v6;
3926  char *start;
3927  unsigned int port;
3928  char dummy[2];
3929  char *rest = NULL;
3930  struct PortOnlyIpv4Ipv6 *po;
3931  socklen_t addr_len_ipv4;
3932  socklen_t addr_len_ipv6;
3933 
3934  (void) cls;
3935 
3937  memset (&v4,0,sizeof(struct sockaddr_in));
3938  memset (&v6,0,sizeof(struct sockaddr_in6));
3939  cfg = c;
3940  if (GNUNET_OK !=
3943  "BINDTO",
3944  &bindto))
3945  {
3948  "BINDTO");
3949  return;
3950  }
3951  if (GNUNET_OK !=
3954  "MAX_QUEUE_LENGTH",
3955  &max_queue_length))
3957  if (GNUNET_OK !=
3960  "REKEY_INTERVAL",
3961  &rekey_interval))
3963 
3965  if (NULL == peerstore)
3966  {
3967  GNUNET_free (bindto);
3968  GNUNET_break (0);
3970  return;
3971  }
3972 
3974 
3975  if (1 == sscanf (bindto, "%u%1s", &bind_port, dummy))
3976  {
3980  "address po %s\n",
3982  if (NULL != po->addr_ipv4)
3983  {
3985  }
3986  if (NULL != po->addr_ipv6)
3987  {
3990  }
3991  GNUNET_free (po);
3992  nat_register ();
3993  GNUNET_free (bindto);
3994  return;
3995  }
3996 
3997  start = extract_address (bindto);
3998  // FIXME: check for NULL == start...
3999  if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
4000  {
4001  bind_port = extract_port (bindto);
4002 
4003  in = tcp_address_to_sockaddr_numeric_v4 (&in_len, v4, bind_port);
4004  init_socket (in, in_len);
4005  nat_register ();
4006  GNUNET_free (start);
4007  GNUNET_free (bindto);
4008  return;
4009  }
4010 
4011  if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
4012  {
4013  bind_port = extract_port (bindto);
4014  in = tcp_address_to_sockaddr_numeric_v6 (&in_len, v6, bind_port);
4015  init_socket (in, in_len);
4016  nat_register ();
4017  GNUNET_free (start);
4018  GNUNET_free (bindto);
4019  return;
4020  }
4021 
4022  bind_port = extract_port (bindto);
4023  resolve_request_handle = GNUNET_RESOLVER_ip_get (strtok_r (bindto,
4024  ":",
4025  &rest),
4026  AF_UNSPEC,
4029  &port);
4030 
4031  GNUNET_free (bindto);
4032  GNUNET_free (start);
4033 }
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 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_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.
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:1303
#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, 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_CONTAINER_multihashmap_create(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_log_config_missing(), GNUNET_NO, GNUNET_OK, GNUNET_PEERSTORE_connect(), GNUNET_RESOLVER_ip_get(), GNUNET_SCHEDULER_add_shutdown(), GNUNET_SCHEDULER_shutdown(), GNUNET_TIME_UNIT_MINUTES, init_socket(), init_socket_resolv(), max_queue_length, nat_register(), peerstore, pending_reversals, port, rekey_interval, resolve_request_handle, start, tcp_address_to_sockaddr_numeric_v4(), tcp_address_to_sockaddr_numeric_v6(), and tcp_address_to_sockaddr_port_only().

Here is the call 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 4044 of file gnunet-communicator-tcp.c.

4045 {
4046  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
4048  };
4049  int ret;
4050 
4052  "transport",
4053  "Starting tcp communicator\n");
4054  if (GNUNET_OK !=
4055  GNUNET_STRINGS_get_utf8_args (argc, argv,
4056  &argc, &argv))
4057  return 2;
4058 
4059  ret = (GNUNET_OK ==
4060  GNUNET_PROGRAM_run (argc,
4061  argv,
4062  "gnunet-communicator-tcp",
4063  _ ("GNUnet TCP communicator"),
4064  options,
4065  &run,
4066  NULL))
4067  ? 0
4068  : 1;
4069  GNUNET_free_nz ((void *) argv);
4070  return ret;
4071 }
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
De