GNUnet debian-0.24.3-23-g589b01d60
gnunet-communicator-libp2p.c File Reference

Very first draft of a gnunet libp2p communicator. More...

#include "platform.h"
#include "gnunet_common.h"
#include "gnunet_util_lib.h"
#include "gnunet_pils_service.h"
#include "gnunet_core_service.h"
#include "gnunet_peerstore_service.h"
#include "gnunet_protocols.h"
#include "gnunet_signatures.h"
#include "gnunet_constants.h"
#include "gnunet_nat_service.h"
#include "gnunet_statistics_service.h"
#include "gnunet_transport_communication_service.h"
#include "gnunet_resolver_service.h"
#include <libp2p/basic/scheduler.hpp>
#include <libp2p/common/literals.hpp>
#include <libp2p/injector/host_injector.hpp>
#include <libp2p/layer/websocket/ws_adaptor.hpp>
#include <libp2p/log/configurator.hpp>
#include <libp2p/log/logger.hpp>
#include <libp2p/protocol/echo.hpp>
Include dependency graph for gnunet-communicator-libp2p.c:

Go to the source code of this file.

Data Structures

struct  Queue
 Handle for a queue. More...
 
struct  Addresses
 DLL to store the addresses we like to register at NAT service. More...
 

Macros

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

Functions

static void listen_cb (void *cls)
 We have been notified that our listen socket has something to read. More...
 
static void eddsa_priv_to_hpke_key (struct GNUNET_CRYPTO_EddsaPrivateKey *edpk, struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
 
static void eddsa_pub_to_hpke_key (struct GNUNET_CRYPTO_EddsaPublicKey *edpk, struct GNUNET_CRYPTO_EcdhePublicKey *pk)
 
static void queue_destroy (struct Queue *queue)
 Functions with this signature are called whenever we need to close a queue due to a disconnect or failure to establish a connection. More...
 
static void calculate_hmac (struct GNUNET_HashCode *hmac_secret, const void *buf, size_t buf_size, struct GNUNET_ShortHashCode *smac)
 Compute mac over buf, and ratched the hmac_secret. More...
 
static void queue_finish (struct Queue *queue)
 Append a 'finish' message to the outgoing transmission. More...
 
static void queue_read (void *cls)
 Queue read task. More...
 
static void core_read_finished_cb (void *cls, int success)
 Core tells us it is done processing a message that transport received on a queue with status success. More...
 
static void pass_plaintext_to_core (struct Queue *queue, const void *plaintext, size_t plaintext_len)
 We received plaintext_len bytes of plaintext on queue. More...
 
static void setup_cipher (const struct GNUNET_ShortHashCode *prk, const struct GNUNET_PeerIdentity *pid, gcry_cipher_hd_t *cipher, struct GNUNET_HashCode *hmac_key)
 Setup cipher based on shared secret dh and decrypting peer pid. More...
 
static void rekey_monotime_store_cb (void *cls, int success)
 Callback called when peerstore store operation for rekey monotime value is finished. More...
 
static void rekey_monotime_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
 Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY where found. More...
 
static void setup_in_cipher_elligator (const struct GNUNET_CRYPTO_HpkeEncapsulation *c, struct Queue *queue)
 Setup cipher of queue for decryption from an elligator representative. More...
 
static void setup_in_cipher (const struct GNUNET_CRYPTO_HpkeEncapsulation *ephemeral, struct Queue *queue)
 Setup cipher of queue for decryption. More...
 
static void do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
 Handle rekey message on queue. More...
 
static void handshake_ack_monotime_store_cb (void *cls, int success)
 Callback called when peerstore store operation for handshake ack monotime value is finished. More...
 
static void handshake_ack_monotime_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
 Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE_ACK where found. More...
 
static void send_challenge (struct GNUNET_CRYPTO_ChallengeNonceP challenge, struct Queue *queue)
 Sending challenge with TcpConfirmationAck back to sender of ephemeral key. More...
 
static void setup_out_cipher (struct Queue *queue, struct GNUNET_ShortHashCode *dh)
 Setup cipher for outgoing data stream based on target and our ephemeral private key. More...
 
static void inject_rekey (struct Queue *queue)
 Inject a struct TCPRekey message into the queue's plaintext buffer. More...
 
static int pending_reversals_delete_it (void *cls, const struct GNUNET_HashCode *key, void *value)
 
static void check_and_remove_pending_reversal (struct sockaddr *in, sa_family_t sa_family, struct GNUNET_PeerIdentity *sender)
 
static void free_proto_queue (struct ProtoQueue *pq)
 Closes socket and frees memory associated with pq. More...
 
static void proto_queue_write (void *cls)
 We have been notified that our socket is ready to write. More...
 
static void queue_write (void *cls)
 We have been notified that our socket is ready to write. More...
 
static size_t try_handle_plaintext (struct Queue *queue)
 Test if we have received a full message in plaintext. More...
 
static struct sockaddr * tcp_address_to_sockaddr_numeric_v6 (socklen_t *sock_len, struct sockaddr_in6 v6, unsigned int port)
 Convert a struct sockaddr_in6 to astruct sockaddr *`. More...
 
static struct sockaddr * tcp_address_to_sockaddr_numeric_v4 (socklen_t *sock_len, struct sockaddr_in v4, unsigned int port)
 Convert a struct sockaddr_in4 to astruct sockaddr *`. More...
 
static struct PortOnlyIpv4Ipv6tcp_address_to_sockaddr_port_only (const char *bindto, unsigned int *port)
 Convert TCP bind specification to a struct PortOnlyIpv4Ipv6 * More...
 
static char * extract_address (const char *bindto)
 This Method extracts the address part of the BINDTO string. More...
 
static unsigned int extract_port (const char *addr_and_port)
 This Method extracts the port part of the BINDTO string. More...
 
static struct sockaddr * tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
 Convert TCP bind specification to a struct sockaddr * More...
 
static void mq_send (struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *msg, void *impl_state)
 Signature of functions implementing the sending functionality of a message queue. More...
 
static void mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
 Signature of functions implementing the destruction of a message queue. More...
 
static void mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
 Implementation function that cancels the currently sent message. More...
 
static void mq_error (void *cls, enum GNUNET_MQ_Error error)
 Generic error handler, called with the appropriate error code and the same closure specified at the creation of the message queue. More...
 
static void boot_queue (struct Queue *queue)
 Add the given queue to our internal data structure. More...
 
static void transmit_kx (struct Queue *queue, const struct GNUNET_CRYPTO_HpkeEncapsulation *c)
 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 pending_reversal_timeout (void *cls)
 
static int mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
 Function called by the transport service to initialize a message queue given address information about another peer. More...
 
static int get_lt_delete_it (void *cls, const struct GNUNET_HashCode *key, void *value)
 Iterator over all ListenTasks to clean up. More...
 
static int get_queue_delete_it (void *cls, const struct GNUNET_HashCode *target, void *value)
 Iterator over all message queues to clean up. More...
 
static void do_shutdown (void *cls)
 Shutdown the UNIX communicator. More...
 
static void enc_notify_cb (void *cls, const struct GNUNET_PeerIdentity *sender, const struct GNUNET_MessageHeader *msg)
 Function called when the transport service has received an acknowledgement for this communicator (!) via a different return path. More...
 
static void 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 enum GNUNET_GenericReturnValue load_ikm ()
 FIXME: We could alternatively ask PILS for de/encaps, but at a high cost wrt async RPC calls... 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...
 
void pid_change_cb (void *cls, const struct GNUNET_HELLO_Parser *parser, const struct GNUNET_HashCode *addr_hash)
 
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 struct GNUNET_PILS_Handlepils
 For PILS. More...
 
static struct GNUNET_STATISTICS_Handlestats
 For logging statistics. More...
 
static struct GNUNET_TRANSPORT_CommunicatorHandlech
 Our environment. More...
 
static struct GNUNET_CONTAINER_MultiHashMapqueue_map
 Queues (map from peer identity to struct Queue) More...
 
static struct GNUNET_CONTAINER_MultiHashMaplt_map
 ListenTasks (map from socket to struct ListenTask) More...
 
static struct GNUNET_PeerIdentity my_identity
 Our public key. More...
 
static struct GNUNET_CRYPTO_EddsaPrivateKeymy_private_key
 Our private key. More...
 
static struct GNUNET_CRYPTO_EcdhePrivateKey my_x25519_private_key
 Our private key. More...
 
static const struct GNUNET_CONFIGURATION_Handlecfg
 Our configuration. More...
 
static struct Addressesaddrs_head
 Head of DLL with addresses we like to register at NAT service. More...
 
static struct Addressesaddrs_tail
 Head of DLL with addresses we like to register at NAT service. More...
 
static struct GNUNET_PEERSTORE_Handlepeerstore
 Database for peer's HELLOs. More...
 
static int shutdown_running = GNUNET_NO
 A flag indicating we are already doing a shutdown. More...
 

Detailed Description

Very first draft of a gnunet libp2p communicator.

This is not in any way in a working or compiling state

Author
ch3

Definition in file gnunet-communicator-libp2p.c.

Macro Definition Documentation

◆ LOG

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

Definition at line 53 of file gnunet-communicator-libp2p.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 2481 of file gnunet-communicator-libp2p.c.

2482{
2483 struct sockaddr_storage in;
2484 socklen_t addrlen;
2485 struct GNUNET_NETWORK_Handle *sock;
2486 struct ListenTask *lt;
2487 struct sockaddr *in_addr;
2488
2490 "listen_cb\n");
2491
2492 lt = cls;
2493
2494 lt->listen_task = NULL;
2495 GNUNET_assert (NULL != lt->listen_sock);
2496 addrlen = sizeof(in);
2497 memset (&in, 0, sizeof(in));
2499 (struct sockaddr*) &in,
2500 &addrlen);
2501 if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno)))
2502 return; /* system limit reached, wait until connection goes down */
2504 lt->listen_sock,
2505 &listen_cb,
2506 lt);
2507 if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno)))
2508 return;
2509 if (NULL == sock)
2510 {
2512 return;
2513 }
2514 in_addr = GNUNET_memdup (&in, addrlen);
2515 create_proto_queue (sock, in_addr, addrlen);
2516}
static struct ProtoQueue * create_proto_queue(struct GNUNET_NETWORK_Handle *sock, struct sockaddr *in, socklen_t addrlen)
static void listen_cb(void *cls)
We have been notified that our listen socket has something to read.
#define GNUNET_log(kind,...)
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc, struct sockaddr *address, socklen_t *address_len)
Accept a new connection on a socket.
Definition: network.c:392
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1511
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
handle to a socket
Definition: network.c:53
Struct to use as closure.
struct GNUNET_NETWORK_Handle * listen_sock
Listen socket.
struct GNUNET_SCHEDULER_Task * listen_task
ID of listen task.

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

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

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

◆ eddsa_priv_to_hpke_key()

static void eddsa_priv_to_hpke_key ( struct GNUNET_CRYPTO_EddsaPrivateKey edpk,
struct GNUNET_CRYPTO_EcdhePrivateKey pk 
)
static

Definition at line 194 of file gnunet-communicator-libp2p.c.

196{
198 key.type = htonl (GNUNET_PUBLIC_KEY_TYPE_EDDSA);
199 key.eddsa_key = *edpk;
201}
struct GNUNET_HashCode key
The key used in the DHT.
struct GNUNET_CRYPTO_PrivateKey pk
Private key from command line option, or NULL.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hpke_sk_to_x25519(const struct GNUNET_CRYPTO_PrivateKey *sk, struct GNUNET_CRYPTO_EcdhePrivateKey *x25519)
Convert a GNUnet identity key to a key sutiable for HPKE (X25519)
Definition: crypto_hpke.c:832
@ GNUNET_PUBLIC_KEY_TYPE_EDDSA
EDDSA identity.
A private key for an identity as per LSD0001.

References GNUNET_CRYPTO_hpke_sk_to_x25519(), GNUNET_PUBLIC_KEY_TYPE_EDDSA, key, and pk.

Referenced by pid_change_cb().

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

◆ eddsa_pub_to_hpke_key()

static void eddsa_pub_to_hpke_key ( struct GNUNET_CRYPTO_EddsaPublicKey edpk,
struct GNUNET_CRYPTO_EcdhePublicKey pk 
)
static

Definition at line 205 of file gnunet-communicator-libp2p.c.

207{
209 key.type = htonl (GNUNET_PUBLIC_KEY_TYPE_EDDSA);
210 key.eddsa_key = *edpk;
212}
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hpke_pk_to_x25519(const struct GNUNET_CRYPTO_PublicKey *pk, struct GNUNET_CRYPTO_EcdhePublicKey *x25519)
Convert a GNUnet identity key to a key sutiable for HPKE (X25519)
Definition: crypto_hpke.c:808
An identity key as per LSD0001.

References GNUNET_CRYPTO_hpke_pk_to_x25519(), GNUNET_PUBLIC_KEY_TYPE_EDDSA, key, and pk.

Referenced by mq_init(), and proto_read_kx().

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 223 of file gnunet-communicator-libp2p.c.

224{
225 struct ListenTask *lt = NULL;
226 struct GNUNET_HashCode h_sock;
227 int sockfd;
228
229 if (NULL != queue->listen_sock)
230 {
231 sockfd = GNUNET_NETWORK_get_fd (queue->listen_sock);
232 GNUNET_CRYPTO_hash (&sockfd,
233 sizeof(int),
234 &h_sock);
235
237 }
238
240 "Disconnecting queue for peer `%s'\n",
241 GNUNET_i2s (&queue->target));
242 if (NULL != queue->rekey_monotime_sc)
243 {
244 GNUNET_PEERSTORE_store_cancel (queue->rekey_monotime_sc);
245 queue->rekey_monotime_sc = NULL;
246 }
247 if (NULL != queue->handshake_monotime_sc)
248 {
249 GNUNET_PEERSTORE_store_cancel (queue->handshake_monotime_sc);
250 queue->handshake_monotime_sc = NULL;
251 }
252 if (NULL != queue->handshake_ack_monotime_sc)
253 {
254 GNUNET_PEERSTORE_store_cancel (queue->handshake_ack_monotime_sc);
255 queue->handshake_ack_monotime_sc = NULL;
256 }
257 if (NULL != queue->rekey_monotime_get)
258 {
259 GNUNET_PEERSTORE_iteration_stop (queue->rekey_monotime_get);
260 queue->rekey_monotime_get = NULL;
261 }
262 if (NULL != queue->handshake_monotime_get)
263 {
264 GNUNET_PEERSTORE_iteration_stop (queue->handshake_monotime_get);
265 queue->handshake_monotime_get = NULL;
266 }
267 if (NULL != queue->handshake_ack_monotime_get)
268 {
269 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
270 queue->handshake_ack_monotime_get = NULL;
271 }
272 if (NULL != queue->qh)
273 {
275 queue->qh = NULL;
276 }
278 GNUNET_YES ==
281 "# queues active",
283 GNUNET_NO);
284 if (NULL != queue->read_task)
285 {
286 GNUNET_SCHEDULER_cancel (queue->read_task);
287 queue->read_task = NULL;
288 }
289 if (NULL != queue->write_task)
290 {
291 GNUNET_SCHEDULER_cancel (queue->write_task);
292 queue->write_task = NULL;
293 }
295 {
297 "closing socket failed\n");
298 }
299 gcry_cipher_close (queue->in_cipher);
300 gcry_cipher_close (queue->out_cipher);
301 GNUNET_free (queue->address);
302 if (0 != queue->backpressure)
303 queue->destroyed = GNUNET_YES;
304 else
306
307 if (NULL == lt)
308 return;
309
310 if ((! shutdown_running) && (NULL == lt->listen_task))
311 {
313 "add read net listen\n");
316 lt->listen_sock,
317 &listen_cb,
318 lt);
319 }
320 else
321 GNUNET_free (lt);
322}
static struct GNUNET_STATISTICS_Handle * stats
For logging statistics.
static struct GNUNET_CONTAINER_MultiHashMap * queue_map
Queues (map from peer identity to struct Queue)
static int shutdown_running
A flag indicating we are already doing a shutdown.
static struct GNUNET_CONTAINER_MultiHashMap * lt_map
ListenTasks (map from socket to struct ListenTask)
static void queue(const char *hostname)
Add hostname to the list of requests to be made.
void GNUNET_TRANSPORT_communicator_mq_del(struct GNUNET_TRANSPORT_QueueHandle *qh)
Notify transport service that an MQ became unavailable due to a disconnect or timeout.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
void * GNUNET_CONTAINER_multihashmap_get(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Given a key find a value in the map matching the key.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
Remove the given key-value pair from the map.
unsigned int GNUNET_CONTAINER_multihashmap_size(const struct GNUNET_CONTAINER_MultiHashMap *map)
Get the number of key-value pairs in the map.
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:508
int GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
Return file descriptor for this network handle.
Definition: network.c:1001
void GNUNET_PEERSTORE_store_cancel(struct GNUNET_PEERSTORE_StoreContext *sc)
Cancel a store request.
void GNUNET_PEERSTORE_iteration_stop(struct GNUNET_PEERSTORE_IterateContext *ic)
Cancel an iteration.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:980
void GNUNET_STATISTICS_set(struct GNUNET_STATISTICS_Handle *handle, const char *name, uint64_t value, int make_persistent)
Set statistic value for the peer.
A 512-bit hashcode.

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

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

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

◆ calculate_hmac()

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

Compute mac over buf, and ratched the hmac_secret.

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

Definition at line 334 of file gnunet-communicator-libp2p.c.

338{
339 struct GNUNET_HashCode mac;
340
341 GNUNET_CRYPTO_hmac_raw (hmac_secret,
342 sizeof(struct GNUNET_HashCode),
343 buf,
344 buf_size,
345 &mac);
346 /* truncate to `struct GNUNET_ShortHashCode` */
347 memcpy (smac, &mac, sizeof(struct GNUNET_ShortHashCode));
348 /* ratchet hmac key */
349 GNUNET_CRYPTO_hash (hmac_secret,
350 sizeof(struct GNUNET_HashCode),
351 hmac_secret);
352}
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:297
A 256-bit hashcode.

References GNUNET_CRYPTO_hash(), and GNUNET_CRYPTO_hmac_raw().

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

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

◆ queue_finish()

static void queue_finish ( struct Queue queue)
static

Append a 'finish' message to the outgoing transmission.

Once the finish has been transmitted, destroy the queue.

Parameters
queuequeue to shut down nicely

Definition at line 362 of file gnunet-communicator-libp2p.c.

363{
364 struct TCPFinish fin;
365
366 memset (&fin, 0, sizeof(fin));
367 fin.header.size = htons (sizeof(fin));
368 fin.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH);
369 calculate_hmac (&queue->out_hmac, &fin, sizeof(fin), &fin.hmac);
370 /* if there is any message left in pwrite_buf, we
371 overwrite it (possibly dropping the last message
372 from CORE hard here) */
373 memcpy (queue->pwrite_buf, &fin, sizeof(fin));
374 queue->pwrite_off = sizeof(fin);
375 /* This flag will ensure that #queue_write() no longer
376 notifies CORE about the possibility of sending
377 more data, and that #queue_write() will call
378 #queue_destroy() once the @c fin was fully written. */
379 queue->finishing = GNUNET_YES;
380}
static void calculate_hmac(struct GNUNET_HashCode *hmac_secret, const void *buf, size_t buf_size, struct GNUNET_ShortHashCode *smac)
Compute mac over buf, and ratched the hmac_secret.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH
TCP communicator end of stream.

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

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

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

◆ queue_read()

static void queue_read ( void *  cls)
static

Queue read task.

If we hit the timeout, disconnect it

Parameters
clsthe struct Queue * to disconnect

Definition at line 1426 of file gnunet-communicator-libp2p.c.

1427{
1428 struct Queue *queue = cls;
1429 struct GNUNET_TIME_Relative left;
1430 ssize_t rcvd;
1431
1432 queue->read_task = NULL;
1433 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
1434 &queue->cread_buf[queue->cread_off],
1435 BUF_SIZE - queue->cread_off);
1437 "Received %zd bytes from TCP queue\n", rcvd);
1438 if (-1 == rcvd)
1439 {
1440 if ((EAGAIN != errno) && (EINTR != errno))
1441 {
1444 return;
1445 }
1446 /* try again */
1447 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
1448 if (0 != left.rel_value_us)
1449 {
1450 queue->read_task =
1452 return;
1453 }
1455 "Queue %p was idle for %s, disconnecting\n",
1456 queue,
1459 GNUNET_YES));
1461 return;
1462 }
1463 if (0 == rcvd)
1464 {
1465 /* Orderly shutdown of connection */
1467 "Socket for queue %p seems to have been closed\n", queue);
1469 return;
1470 }
1471 queue->timeout =
1473 queue->cread_off += rcvd;
1474 while ((queue->pread_off < sizeof(queue->pread_buf)) &&
1475 (queue->cread_off > 0))
1476 {
1477 size_t max = GNUNET_MIN (sizeof(queue->pread_buf) - queue->pread_off,
1478 queue->cread_off);
1479 size_t done;
1480 size_t total;
1481 size_t old_pread_off = queue->pread_off;
1482
1483 GNUNET_assert (0 ==
1484 gcry_cipher_decrypt (queue->in_cipher,
1485 &queue->pread_buf[queue->pread_off],
1486 max,
1487 queue->cread_buf,
1488 max));
1489 queue->pread_off += max;
1490 total = 0;
1491 while (0 != (done = try_handle_plaintext (queue)))
1492 {
1493 /* 'done' bytes of plaintext were used, shift buffer */
1494 GNUNET_assert (done <= queue->pread_off);
1495 /* NOTE: this memmove() could possibly sometimes be
1496 avoided if we pass 'total' into try_handle_plaintext()
1497 and use it at an offset into the buffer there! */
1498 memmove (queue->pread_buf,
1499 &queue->pread_buf[done],
1500 queue->pread_off - done);
1501 queue->pread_off -= done;
1502 total += done;
1503 /* The last plaintext was a rekey, abort for now */
1504 if (GNUNET_YES == queue->rekeyed)
1505 break;
1506 }
1507 /* when we encounter a rekey message, the decryption above uses the
1508 wrong key for everything after the rekey; in that case, we have
1509 to re-do the decryption at 'total' instead of at 'max'.
1510 However, we have to take into account that the plaintext buffer may have
1511 already contained data and not jumped too far ahead in the ciphertext.
1512 If there is no rekey and the last message is incomplete (max > total),
1513 it is safe to keep the decryption so we shift by 'max' */
1514 if (GNUNET_YES == queue->rekeyed)
1515 {
1516 max = total - old_pread_off;
1517 queue->rekeyed = GNUNET_NO;
1518 queue->pread_off = 0;
1519 }
1520 memmove (queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
1521 queue->cread_off -= max;
1522 }
1523 if (BUF_SIZE == queue->cread_off)
1524 return; /* buffer full, suspend reading */
1525 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
1526 if (0 != left.rel_value_us)
1527 {
1528 if (max_queue_length > queue->backpressure)
1529 {
1530 /* continue reading */
1531 queue->read_task =
1533 }
1534 return;
1535 }
1537 "Queue %p was idle for %s, disconnecting\n",
1538 queue,
1541 GNUNET_YES));
1543}
static size_t try_handle_plaintext(struct Queue *queue)
Test if we have received a full message in plaintext.
static void queue_destroy(struct Queue *queue)
Functions with this signature are called whenever we need to close a queue due to a disconnect or fai...
static void queue_read(void *cls)
Queue read task.
#define BUF_SIZE
Size of our IO buffers for ciphertext data.
static unsigned long long max_queue_length
Maximum queue length before we stop reading towards the transport service.
#define GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
After how long do we consider a connection to a peer dead if we don't receive messages from the peer?
#define GNUNET_MIN(a, b)
ssize_t GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length)
Read data from a connected socket (always non-blocking).
Definition: network.c:717
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:406
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:599
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
Definition: time.c:316
#define max(x, y)
Time for relative time used by GNUnet, in microseconds.
Handle for a queue.

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

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

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

◆ core_read_finished_cb()

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

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

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

Definition at line 400 of file gnunet-communicator-libp2p.c.

401{
402 struct Queue *queue = cls;
403 if (GNUNET_OK != success)
405 "# messages lost in communicator API towards CORE",
406 1,
407 GNUNET_NO);
408 if (NULL == queue)
409 return;
410
412 "backpressure %u\n",
413 queue->backpressure);
414
415 queue->backpressure--;
416 /* handle deferred queue destruction */
417 if ((queue->destroyed) && (0 == queue->backpressure))
418 {
420 return;
421 }
422 else if (GNUNET_YES != queue->destroyed)
423 {
424 queue->timeout =
426 );
427 /* possibly unchoke reading, now that CORE made progress */
428 if (NULL == queue->read_task)
429 queue->read_task =
431 queue->timeout),
432 queue->sock,
433 &queue_read,
434 queue);
435 }
436}
@ GNUNET_OK
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.

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

Referenced by pass_plaintext_to_core().

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

◆ pass_plaintext_to_core()

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

We received plaintext_len bytes of plaintext on queue.

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

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

Definition at line 449 of file gnunet-communicator-libp2p.c.

452{
453 const struct GNUNET_MessageHeader *hdr = plaintext;
454 int ret;
455
457 "pass message from %s to core\n",
458 GNUNET_i2s (&queue->target));
459
460 if (ntohs (hdr->size) != plaintext_len)
461 {
462 /* NOTE: If we ever allow multiple CORE messages in one
463 BOX, this will have to change! */
464 GNUNET_break (0);
465 return;
466 }
468 &queue->target,
469 hdr,
472 queue);
474 "passed to core\n");
475 if (GNUNET_OK == ret)
476 queue->backpressure++;
477 GNUNET_break (GNUNET_NO != ret); /* backpressure not working!? */
478 if (GNUNET_SYSERR == ret)
480 "# bytes lost due to CORE not running",
481 plaintext_len,
482 GNUNET_NO);
483}
static int ret
Final status code.
Definition: gnunet-arm.c:93
#define ADDRESS_VALIDITY_PERIOD
How long do we believe our addresses to remain up (before the other peer should revalidate).
static struct GNUNET_TRANSPORT_CommunicatorHandle * ch
Our environment.
static void core_read_finished_cb(void *cls, int success)
Core tells us it is done processing a message that transport received on a queue with status success.
int GNUNET_TRANSPORT_communicator_receive(struct GNUNET_TRANSPORT_CommunicatorHandle *handle, const struct GNUNET_PeerIdentity *sender, const struct GNUNET_MessageHeader *msg, struct GNUNET_TIME_Relative expected_addr_validity, GNUNET_TRANSPORT_MessageCompletedCallback cb, void *cb_cls)
Notify transport service that the communicator has received a message.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
Header for all communications.

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

Referenced by try_handle_plaintext().

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

◆ setup_cipher()

static void setup_cipher ( const struct GNUNET_ShortHashCode prk,
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 496 of file gnunet-communicator-libp2p.c.

500{
501 char key[256 / 8];
502 char ctr[128 / 8];
503
504 GNUNET_assert (0 == gcry_cipher_open (cipher,
505 GCRY_CIPHER_AES256 /* low level: go for speed */
506 ,
507 GCRY_CIPHER_MODE_CTR,
508 0 /* flags */));
511 sizeof(key),
512 prk,
513 "gnunet-communicator-tcp-key",
514 strlen (
515 "gnunet-communicator-tcp-key"),
516 NULL,
517 0));
518 GNUNET_assert (0 == gcry_cipher_setkey (*cipher, key, sizeof(key)));
521 sizeof(ctr),
522 prk,
523 "gnunet-communicator-tcp-ctr",
524 strlen (
525 "gnunet-communicator-tcp-ctr"),
526 NULL,
527 0));
528 gcry_cipher_setctr (*cipher, ctr, sizeof(ctr));
531 sizeof(struct GNUNET_HashCode),
532 prk,
533 "gnunet-communicator-hmac",
534 strlen ("gnunet-communicator-hmac"),
535 NULL,
536 0));
537}
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hkdf_expand(void *result, size_t out_len, const struct GNUNET_ShortHashCode *prk,...)
HKDF-Expand using SHA256.
Definition: crypto_hkdf.c:156

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

Referenced by setup_in_cipher(), setup_in_cipher_elligator(), and setup_out_cipher().

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

◆ rekey_monotime_store_cb()

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

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

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

Definition at line 546 of file gnunet-communicator-libp2p.c.

547{
548 struct Queue *queue = cls;
549 if (GNUNET_OK != success)
550 {
552 "Failed to store rekey monotonic time in PEERSTORE!\n");
553 }
554 queue->rekey_monotime_sc = NULL;
555 GNUNET_PEERSTORE_iteration_next (queue->rekey_monotime_get, 1);
556}
void GNUNET_PEERSTORE_iteration_next(struct GNUNET_PEERSTORE_IterateContext *ic, uint64_t limit)
Continue an iteration.

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

Referenced by rekey_monotime_cb().

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

◆ rekey_monotime_cb()

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

Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY where found.

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

Definition at line 567 of file gnunet-communicator-libp2p.c.

570{
571 struct Queue *queue = cls;
572 struct GNUNET_TIME_AbsoluteNBO *mtbe;
573 struct GNUNET_TIME_Absolute mt;
574 const struct GNUNET_PeerIdentity *pid;
575 struct GNUNET_TIME_AbsoluteNBO *rekey_monotonic_time;
576
577 (void) emsg;
578
579 rekey_monotonic_time = &queue->rekey_monotonic_time;
580 pid = &queue->target;
581 if (NULL == record)
582 {
583 queue->rekey_monotime_get = NULL;
584 return;
585 }
586 if (sizeof(*mtbe) != record->value_size)
587 {
588 GNUNET_PEERSTORE_iteration_next (queue->rekey_monotime_get, 1);
589 GNUNET_break (0);
590 return;
591 }
592 mtbe = record->value;
593 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
594 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
595 queue->rekey_monotonic_time).abs_value_us)
596 {
598 "Queue from %s dropped, rekey monotime in the past\n",
599 GNUNET_i2s (&queue->target));
600 GNUNET_break (0);
601 GNUNET_PEERSTORE_iteration_stop (queue->rekey_monotime_get);
602 queue->rekey_monotime_get = NULL;
603 // FIXME: Why should we try to gracefully finish here??
605 return;
606 }
607 queue->rekey_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
608 "transport_tcp_communicator",
609 pid,
611 rekey_monotonic_time,
612 sizeof(*
613 rekey_monotonic_time),
617 queue);
618}
static void queue_finish(struct Queue *queue)
Append a 'finish' message to the outgoing transmission.
static struct GNUNET_PEERSTORE_Handle * peerstore
Database for peer's HELLOs.
static void rekey_monotime_store_cb(void *cls, int success)
Callback called when peerstore store operation for rekey monotime value is finished.
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
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:741
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
The identity of the host (wraps the signing key of the peer).
Time for absolute time used by GNUnet, in microseconds and in network byte order.
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.

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

Referenced by do_rekey().

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

◆ setup_in_cipher_elligator()

static void setup_in_cipher_elligator ( const struct GNUNET_CRYPTO_HpkeEncapsulation c,
struct Queue queue 
)
static

Setup cipher of queue for decryption from an elligator representative.

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

Definition at line 628 of file gnunet-communicator-libp2p.c.

631{
632 struct GNUNET_ShortHashCode k;
633
635 setup_cipher (&k, &my_identity, &queue->in_cipher, &queue->in_hmac);
636}
static struct GNUNET_PeerIdentity my_identity
Our public key.
static void setup_cipher(const struct GNUNET_ShortHashCode *prk, 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_EcdhePrivateKey my_x25519_private_key
Our private key.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hpke_elligator_kem_decaps(const struct GNUNET_CRYPTO_EcdhePrivateKey *skR, const struct GNUNET_CRYPTO_HpkeEncapsulation *c, struct GNUNET_ShortHashCode *shared_secret)
Carries out ecdh decapsulation with own private key and the representative of the received public key...
Definition: crypto_hpke.c:348

References GNUNET_CRYPTO_hpke_elligator_kem_decaps(), my_identity, my_x25519_private_key, queue(), and setup_cipher().

Referenced by proto_read_kx(), and queue_read_kx().

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

◆ setup_in_cipher()

static void setup_in_cipher ( const struct GNUNET_CRYPTO_HpkeEncapsulation 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 646 of file gnunet-communicator-libp2p.c.

648{
649 struct GNUNET_ShortHashCode k;
650
652 setup_cipher (&k, &my_identity, &queue->in_cipher, &queue->in_hmac);
653}
static struct GNUNET_CRYPTO_EddsaPrivateKey * my_private_key
Our private key.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_kem_decaps(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_HpkeEncapsulation *c, struct GNUNET_ShortHashCode *prk)
Decapsulate a key for a private EdDSA key.
Definition: crypto_hpke.c:285

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

Referenced by do_rekey().

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

◆ do_rekey()

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

Handle rekey message on queue.

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

Parameters
queuethe queue rekey was received on
rekeythe rekey message

Definition at line 665 of file gnunet-communicator-libp2p.c.

666{
667 struct TcpRekeySignature thp;
668
670 thp.purpose.size = htonl (sizeof(thp));
672 "do_rekey size %u\n",
673 thp.purpose.size);
674 thp.sender = queue->target;
676 "sender %s\n",
677 GNUNET_p2s (&thp.sender.public_key));
679 "sender %s\n",
680 GNUNET_p2s (&queue->target.public_key));
681 thp.receiver = my_identity;
683 "receiver %s\n",
684 GNUNET_p2s (&thp.receiver.public_key));
685 thp.ephemeral = rekey->ephemeral;
687 "ephemeral %s\n",
688 GNUNET_e2s ((struct GNUNET_CRYPTO_EcdhePublicKey*) &thp.ephemeral)
689 );
690 thp.monotonic_time = rekey->monotonic_time;
692 "time %s\n",
694 GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
695 GNUNET_assert (ntohl ((&thp)->purpose.size) == sizeof (*(&thp)));
696 if (GNUNET_OK !=
699 &thp,
700 &rekey->sender_sig,
701 &queue->target.public_key))
702 {
703 GNUNET_break (0);
704 // FIXME Why should we try to gracefully finish here?
706 return;
707 }
708 queue->rekey_monotonic_time = rekey->monotonic_time;
709 queue->rekey_monotime_get = GNUNET_PEERSTORE_iteration_start (peerstore,
710 "transport_tcp_communicator",
711 &queue->target,
713 &
715 queue);
716 gcry_cipher_close (queue->in_cipher);
717 queue->rekeyed = GNUNET_YES;
719}
static void setup_in_cipher(const struct GNUNET_CRYPTO_HpkeEncapsulation *ephemeral, struct Queue *queue)
Setup cipher of queue for decryption.
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...
#define GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY
Signature used by TCP communicator rekey.
#define GNUNET_CRYPTO_eddsa_verify(purp, ps, sig, pub)
Verify EdDSA signature.
const char * GNUNET_e2s(const struct GNUNET_CRYPTO_EcdhePublicKey *p)
Convert a public key value to a string (for printing debug messages).
const char * GNUNET_p2s(const struct GNUNET_CRYPTO_EddsaPublicKey *p)
Convert a public key value to a string (for printing debug messages).
struct GNUNET_PEERSTORE_IterateContext * GNUNET_PEERSTORE_iteration_start(struct GNUNET_PEERSTORE_Handle *h, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, GNUNET_PEERSTORE_Processor callback, void *callback_cls)
Iterate over peerstore entries.
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:660
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...
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender's signature of type GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY.
struct GNUNET_CRYPTO_HpkeEncapsulation ephemeral
New ephemeral key.
Signature we use to verify that the ephemeral key was really chosen by the specified sender.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Purpose must be GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY.

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

Referenced by try_handle_plaintext().

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

◆ handshake_ack_monotime_store_cb()

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

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

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

Definition at line 728 of file gnunet-communicator-libp2p.c.

729{
730 struct Queue *queue = cls;
731
732 if (GNUNET_OK != success)
733 {
735 "Failed to store handshake ack monotonic time in PEERSTORE!\n");
736 }
737 queue->handshake_ack_monotime_sc = NULL;
738 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
739}

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

Referenced by handshake_ack_monotime_cb().

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

◆ handshake_ack_monotime_cb()

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

Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE_ACK where found.

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

Definition at line 750 of file gnunet-communicator-libp2p.c.

753{
754 struct Queue *queue = cls;
755 struct GNUNET_TIME_AbsoluteNBO *mtbe;
756 struct GNUNET_TIME_Absolute mt;
757 const struct GNUNET_PeerIdentity *pid;
758 struct GNUNET_TIME_AbsoluteNBO *handshake_ack_monotonic_time;
759
760 (void) emsg;
761
762 handshake_ack_monotonic_time = &queue->handshake_ack_monotonic_time;
763 pid = &queue->target;
764 if (NULL == record)
765 {
766 queue->handshake_ack_monotime_get = NULL;
767 return;
768 }
769 if (sizeof(*mtbe) != record->value_size)
770 {
771 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
772 GNUNET_break (0);
773 return;
774 }
775 mtbe = record->value;
776 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
777 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
778 queue->handshake_ack_monotonic_time).abs_value_us)
779 {
781 "Queue from %s dropped, handshake ack monotime in the past\n",
782 GNUNET_i2s (&queue->target));
783 GNUNET_break (0);
784 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
785 queue->handshake_ack_monotime_get = NULL;
786 // FIXME: Why should we try to gracefully finish here?
788 return;
789 }
790 queue->handshake_ack_monotime_sc =
792 "transport_tcp_communicator",
793 pid,
795 handshake_ack_monotonic_time,
796 sizeof(*handshake_ack_monotonic_time),
800 queue);
801}
static void handshake_ack_monotime_store_cb(void *cls, int success)
Callback called when peerstore store operation for handshake ack monotime value is finished.
#define GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE_ACK
Key used to store sender's monotonic time from handshake ack message.

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

Referenced by try_handle_plaintext().

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

◆ send_challenge()

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

Sending challenge with TcpConfirmationAck back to sender of ephemeral key.

Parameters
tcThe TCPConfirmation originally send.
queueThe queue context.

Definition at line 811 of file gnunet-communicator-libp2p.c.

813{
814 struct TCPConfirmationAck tca;
815 struct TcpHandshakeAckSignature thas;
816
818 "sending challenge\n");
819
820 tca.header.type = ntohs (
822 tca.header.size = ntohs (sizeof(tca));
823 tca.challenge = challenge;
824 tca.sender = my_identity;
825 tca.monotonic_time =
827 thas.purpose.purpose = htonl (
829 thas.purpose.size = htonl (sizeof(thas));
830 thas.sender = my_identity;
831 thas.receiver = queue->target;
832 thas.monotonic_time = tca.monotonic_time;
833 thas.challenge = tca.challenge;
835 &thas,
836 &tca.sender_sig);
837 GNUNET_assert (0 ==
838 gcry_cipher_encrypt (queue->out_cipher,
839 &queue->cwrite_buf[queue->cwrite_off],
840 sizeof(tca),
841 &tca,
842 sizeof(tca)));
843 queue->cwrite_off += sizeof(tca);
845 "sending challenge done\n");
846}
static const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
#define GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE_ACK
Signature by a peer sending back the nonce received at initial handshake.
#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:864
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:640
Ack for the encrypted continuation of TCP initial handshake.
Signature we use to verify that the ack from the receiver of the ephemeral key was really send by the...
struct GNUNET_CRYPTO_ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value.

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

Referenced by queue_read_kx(), and try_handle_plaintext().

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

◆ setup_out_cipher()

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

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

Parameters
queuequeue to setup outgoing (encryption) cipher for

Definition at line 856 of file gnunet-communicator-libp2p.c.

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

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

Referenced by inject_rekey(), and start_initial_kx_out().

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

◆ inject_rekey()

static void inject_rekey ( struct Queue queue)
static

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

Parameters
queuequeue to perform rekeying on

Definition at line 872 of file gnunet-communicator-libp2p.c.

873{
874 struct TCPRekey rekey;
875 struct TcpRekeySignature thp;
876 struct GNUNET_ShortHashCode k;
877
878 GNUNET_assert (0 == queue->pwrite_off);
879 memset (&rekey, 0, sizeof(rekey));
880 GNUNET_CRYPTO_eddsa_kem_encaps (&queue->target.public_key, &rekey.ephemeral,
881 &k);
882 rekey.header.type = ntohs (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY);
883 rekey.header.size = ntohs (sizeof(rekey));
884 rekey.monotonic_time =
886 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY);
887 thp.purpose.size = htonl (sizeof(thp));
889 "inject_rekey size %u\n",
890 thp.purpose.size);
891 thp.sender = my_identity;
893 "sender %s\n",
894 GNUNET_p2s (&thp.sender.public_key));
895 thp.receiver = queue->target;
897 "receiver %s\n",
898 GNUNET_p2s (&thp.receiver.public_key));
899 thp.ephemeral = rekey.ephemeral;
901 "ephemeral %s\n",
902 GNUNET_e2s ((struct GNUNET_CRYPTO_EcdhePublicKey*) &thp.ephemeral)
903 );
904 thp.monotonic_time = rekey.monotonic_time;
906 "time %s\n",
908 GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
910 &thp,
911 &rekey.sender_sig);
912 calculate_hmac (&queue->out_hmac, &rekey, sizeof(rekey), &rekey.hmac);
913 /* Encrypt rekey message with 'old' cipher */
914 GNUNET_assert (0 ==
915 gcry_cipher_encrypt (queue->out_cipher,
916 &queue->cwrite_buf[queue->cwrite_off],
917 sizeof(rekey),
918 &rekey,
919 sizeof(rekey)));
920 queue->cwrite_off += sizeof(rekey);
921 /* Setup new cipher for successive messages */
922 gcry_cipher_close (queue->out_cipher);
924}
static void setup_out_cipher(struct Queue *queue, struct GNUNET_ShortHashCode *dh)
Setup cipher for outgoing data stream based on target and our ephemeral private key.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_kem_encaps(const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_CRYPTO_HpkeEncapsulation *c, struct GNUNET_ShortHashCode *prk)
Encapsulate key material for a EdDSA public key.
Definition: crypto_hpke.c:233
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY
TCP communicator rekey message.
TCP rekey message box.

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

Referenced by mq_send_d(), and queue_write().

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

◆ pending_reversals_delete_it()

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

Definition at line 928 of file gnunet-communicator-libp2p.c.

931{
932 struct PendingReversal *pending_reversal = value;
933 (void) cls;
934
935 if (NULL != pending_reversal->timeout_task)
936 {
937 GNUNET_SCHEDULER_cancel (pending_reversal->timeout_task);
938 pending_reversal->timeout_task = NULL;
939 }
942 key,
943 pending_reversal));
944 GNUNET_free (pending_reversal->in);
945 GNUNET_free (pending_reversal);
946 return GNUNET_OK;
947}
static struct GNUNET_CONTAINER_MultiHashMap * pending_reversals
Map of pending reversals.
static char * value
Value of the record to add/remove.
Struct for pending nat reversals.
struct sockaddr * in
Address the reversal was send to.
struct GNUNET_SCHEDULER_Task * timeout_task

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

Referenced by check_and_remove_pending_reversal(), and do_shutdown().

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

◆ check_and_remove_pending_reversal()

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

Definition at line 951 of file gnunet-communicator-libp2p.c.

953{
954 if (AF_INET == sa_family)
955 {
956 struct PendingReversal *pending_reversal;
957 struct GNUNET_HashCode key;
958 struct sockaddr_in *natted_address;
959
960 natted_address = GNUNET_memdup (in, sizeof (struct sockaddr));
961 natted_address->sin_port = 0;
962 GNUNET_CRYPTO_hash (natted_address,
963 sizeof(struct sockaddr),
964 &key);
965
967 &key);
968 if (NULL != pending_reversal && (NULL == sender ||
969 0 != memcmp (sender,
970 &pending_reversal->target,
971 sizeof(struct
973 {
975 "Removing invalid pending reversal for `%s'at `%s'\n",
976 GNUNET_i2s (&pending_reversal->target),
977 GNUNET_a2s (in, sizeof (struct sockaddr)));
978 pending_reversals_delete_it (NULL, &key, pending_reversal);
979 }
980 GNUNET_free (natted_address);
981 }
982}
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 991 of file gnunet-communicator-libp2p.c.

992{
993 if (NULL != pq->listen_sock)
994 {
996 pq->listen_sock = NULL;
997 }
998 if (NULL != pq->read_task)
999 {
1001 pq->read_task = NULL;
1002 }
1003 if (NULL != pq->write_task)
1004 {
1006 pq->write_task = NULL;
1007 }
1008 check_and_remove_pending_reversal (pq->address, pq->address->sa_family, NULL);
1010 GNUNET_free (pq->address);
1012 GNUNET_free (pq);
1013}
static void check_and_remove_pending_reversal(struct sockaddr *in, sa_family_t sa_family, struct GNUNET_PeerIdentity *sender)
static struct ProtoQueue * proto_tail
Protoqueues DLL tail.
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 1023 of file gnunet-communicator-libp2p.c.

1024{
1025 struct ProtoQueue *pq = cls;
1026 ssize_t sent;
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In proto queue write\n");
1028 pq->write_task = NULL;
1029 if (0 != pq->write_off)
1030 {
1031 sent = GNUNET_NETWORK_socket_send (pq->sock,
1032 pq->write_buf,
1033 pq->write_off);
1035 "Sent %lu bytes to TCP queue\n", sent);
1036 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1037 {
1039 free_proto_queue (pq);
1040 return;
1041 }
1042 if (sent > 0)
1043 {
1044 size_t usent = (size_t) sent;
1045 pq->write_off -= usent;
1046 memmove (pq->write_buf,
1047 &pq->write_buf[usent],
1048 pq->write_off);
1049 }
1050 }
1051 /* do we care to write more? */
1052 if ((0 < pq->write_off))
1053 pq->write_task =
1055 pq->sock,
1057 pq);
1058}
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:738
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:1582
Handle for an incoming connection where we do not yet have enough information to setup a full queue.
char write_buf[sizeof(struct TCPNATProbeMessage)]
buffer for writing struct TCPNATProbeMessage to network.
size_t write_off
Offset of the buffer?

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

Referenced by proto_queue_write().

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 1068 of file gnunet-communicator-libp2p.c.

1069{
1070 struct Queue *queue = cls;
1071 ssize_t sent;
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In queue write\n");
1073 queue->write_task = NULL;
1074 if (0 != queue->cwrite_off)
1075 {
1076 sent = GNUNET_NETWORK_socket_send (queue->sock,
1077 queue->cwrite_buf,
1078 queue->cwrite_off);
1080 "Sent %lu bytes to TCP queue\n", sent);
1081 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1082 {
1085 return;
1086 }
1087 if (sent > 0)
1088 {
1089 size_t usent = (size_t) sent;
1090 queue->cwrite_off -= usent;
1091 memmove (queue->cwrite_buf,
1092 &queue->cwrite_buf[usent],
1093 queue->cwrite_off);
1094 queue->timeout =
1097 }
1098 }
1099 {
1100 /* can we encrypt more? (always encrypt full messages, needed
1101 such that #mq_cancel() can work!) */
1102 unsigned int we_do_not_need_to_rekey = (0 < queue->rekey_left_bytes
1103 - (queue->cwrite_off
1104 + queue->pwrite_off
1105 + sizeof (struct TCPRekey)));
1106 if (we_do_not_need_to_rekey &&
1107 (queue->pwrite_off > 0) &&
1108 (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE))
1109 {
1111 "Encrypting %lu bytes\n", queue->pwrite_off);
1112 GNUNET_assert (0 ==
1113 gcry_cipher_encrypt (queue->out_cipher,
1114 &queue->cwrite_buf[queue->cwrite_off],
1115 queue->pwrite_off,
1116 queue->pwrite_buf,
1117 queue->pwrite_off));
1118 if (queue->rekey_left_bytes > queue->pwrite_off)
1119 queue->rekey_left_bytes -= queue->pwrite_off;
1120 else
1121 queue->rekey_left_bytes = 0;
1122 queue->cwrite_off += queue->pwrite_off;
1123 queue->pwrite_off = 0;
1124 }
1125 // if ((-1 != unverified_size)&& ((0 == queue->pwrite_off) &&
1126 if (((0 == queue->rekey_left_bytes) ||
1128 queue->rekey_time).rel_value_us)) &&
1129 (((0 == queue->pwrite_off) || ! we_do_not_need_to_rekey) &&
1130 (queue->cwrite_off + sizeof (struct TCPRekey) <= BUF_SIZE)))
1131 {
1133 }
1134 }
1135 if ((0 == queue->pwrite_off) && (! queue->finishing) &&
1136 (GNUNET_YES == queue->mq_awaits_continue))
1137 {
1138 queue->mq_awaits_continue = GNUNET_NO;
1140 }
1141 /* did we just finish writing 'finish'? */
1142 if ((0 == queue->cwrite_off) && (GNUNET_YES == queue->finishing))
1143 {
1145 "Finishing queue\n");
1147 return;
1148 }
1149 /* do we care to write more? */
1150 if ((0 < queue->cwrite_off) || (0 < queue->pwrite_off))
1151 queue->write_task =
1153 queue->sock,
1154 &queue_write,
1155 queue);
1156}
static void inject_rekey(struct Queue *queue)
Inject a struct TCPRekey message into the queue's plaintext buffer.
static void queue_write(void *cls)
We have been notified that our socket is ready to write.
void GNUNET_MQ_impl_send_continue(struct GNUNET_MQ_Handle *mq)
Call the send implementation for the next queued message, if any.
Definition: mq.c:437
uint64_t rel_value_us
The actual value.

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

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

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

◆ try_handle_plaintext()

static size_t try_handle_plaintext ( struct Queue queue)
static

Test if we have received a full message in plaintext.

If so, handle it.

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

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

Definition at line 1167 of file gnunet-communicator-libp2p.c.

1168{
1169 const struct GNUNET_MessageHeader *hdr;
1170 const struct TCPConfirmationAck *tca;
1171 const struct TCPBox *box;
1172 const struct TCPRekey *rekey;
1173 const struct TCPFinish *fin;
1174 struct TCPRekey rekeyz;
1175 struct TCPFinish finz;
1176 struct GNUNET_ShortHashCode tmac;
1177 uint16_t type;
1178 size_t size = 0;
1179 struct TcpHandshakeAckSignature thas;
1180 const struct GNUNET_CRYPTO_ChallengeNonceP challenge = queue->challenge;
1181
1183 "try handle plaintext!\n");
1184
1185 hdr = (const struct GNUNET_MessageHeader *) queue->pread_buf;
1186 if ((sizeof(*hdr) > queue->pread_off))
1187 {
1189 "Handling plaintext, not even a header!\n");
1190 return 0; /* not even a header */
1191 }
1192
1193 if ((GNUNET_YES != queue->initial_core_kx_done) && (queue->unverified_size >
1195 {
1197 "Already received data of size %lu bigger than KX size %lu!\n",
1198 queue->unverified_size,
1200 GNUNET_break_op (0);
1202 return 0;
1203 }
1204
1205 type = ntohs (hdr->type);
1206 switch (type)
1207 {
1209 tca = (const struct TCPConfirmationAck *) queue->pread_buf;
1211 "start processing ack\n");
1212 if (sizeof(*tca) > queue->pread_off)
1213 {
1215 "Handling plaintext size of tca greater than pread offset.\n")
1216 ;
1217 return 0;
1218 }
1219 if (ntohs (hdr->size) != sizeof(*tca))
1220 {
1222 "Handling plaintext size does not match message type.\n");
1223 GNUNET_break_op (0);
1225 return 0;
1226 }
1227
1228 thas.purpose.purpose = htonl (
1230 thas.purpose.size = htonl (sizeof(thas));
1231 thas.sender = tca->sender;
1232 thas.receiver = my_identity;
1233 thas.monotonic_time = tca->monotonic_time;
1234 thas.challenge = tca->challenge;
1235
1238 &thas,
1239 &tca->sender_sig,
1240 &tca->sender.public_key))
1241 {
1243 "Verification of signature failed!\n");
1244 GNUNET_break (0);
1246 return 0;
1247 }
1248 if (0 != GNUNET_memcmp (&tca->challenge, &challenge))
1249 {
1251 "Challenge in TCPConfirmationAck not correct!\n");
1252 GNUNET_break (0);
1254 return 0;
1255 }
1256
1257 queue->handshake_ack_monotime_get = GNUNET_PEERSTORE_iteration_start (
1258 peerstore,
1259 "transport_tcp_communicator",
1260 &queue->target,
1263 queue);
1264
1266 "Handling plaintext, ack processed!\n");
1267
1269 {
1270 send_challenge (queue->challenge_received, queue);
1271 queue->write_task =
1273 queue->sock,
1274 &queue_write,
1275 queue);
1276 }
1277 else if (GNUNET_TRANSPORT_CS_OUTBOUND == queue->cs)
1278 {
1280 queue->address->sa_family, NULL);
1281 }
1282
1287 queue->initial_core_kx_done = GNUNET_YES;
1288
1289 {
1290 char *foreign_addr;
1291
1292 switch (queue->address->sa_family)
1293 {
1294 case AF_INET:
1295 GNUNET_asprintf (&foreign_addr,
1296 "%s-%s",
1298 GNUNET_a2s (queue->address, queue->address_len));
1299 break;
1300
1301 case AF_INET6:
1302 GNUNET_asprintf (&foreign_addr,
1303 "%s-%s",
1305 GNUNET_a2s (queue->address, queue->address_len));
1306 break;
1307
1308 default:
1309 GNUNET_assert (0);
1310 }
1312 &queue->target,
1313 foreign_addr,
1314 UINT16_MAX, /* no MTU */
1316 0, /* Priority */
1317 queue->nt,
1318 queue->cs,
1319 queue->mq);
1320
1321 GNUNET_free (foreign_addr);
1322 }
1323
1324 size = ntohs (hdr->size);
1325 break;
1327 /* Special case: header size excludes box itself! */
1328 box = (const struct TCPBox *) queue->pread_buf;
1329 if (ntohs (hdr->size) + sizeof(struct TCPBox) > queue->pread_off)
1330 return 0;
1331 calculate_hmac (&queue->in_hmac, &box[1], ntohs (hdr->size), &tmac);
1332 if (0 != memcmp (&tmac, &box->hmac, sizeof(tmac)))
1333 {
1334 GNUNET_break_op (0);
1336 return 0;
1337 }
1338 pass_plaintext_to_core (queue, (const void *) &box[1], ntohs (hdr->size));
1339 size = ntohs (hdr->size) + sizeof(*box);
1341 "Handling plaintext, box processed!\n");
1343 "# bytes decrypted with BOX",
1344 size,
1345 GNUNET_NO);
1347 "# messages decrypted with BOX",
1348 1,
1349 GNUNET_NO);
1350 break;
1351
1353 rekey = (const struct TCPRekey *) queue->pread_buf;
1354 if (sizeof(*rekey) > queue->pread_off)
1355 return 0;
1356 if (ntohs (hdr->size) != sizeof(*rekey))
1357 {
1358 GNUNET_break_op (0);
1360 return 0;
1361 }
1362 rekeyz = *rekey;
1363 memset (&rekeyz.hmac, 0, sizeof(rekeyz.hmac));
1364 calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
1365 if (0 != memcmp (&tmac, &rekey->hmac, sizeof(tmac)))
1366 {
1367 GNUNET_break_op (0);
1369 return 0;
1370 }
1371 do_rekey (queue, rekey);
1372 size = ntohs (hdr->size);
1374 "Handling plaintext, rekey processed!\n");
1376 "# rekeying successful",
1377 1,
1378 GNUNET_NO);
1379 break;
1380
1382 fin = (const struct TCPFinish *) queue->pread_buf;
1383 if (sizeof(*fin) > queue->pread_off)
1384 return 0;
1385 if (ntohs (hdr->size) != sizeof(*fin))
1386 {
1387 GNUNET_break_op (0);
1389 return 0;
1390 }
1391 finz = *fin;
1392 memset (&finz.hmac, 0, sizeof(finz.hmac));
1393 calculate_hmac (&queue->in_hmac, &finz, sizeof(finz), &tmac);
1394 if (0 != memcmp (&tmac, &fin->hmac, sizeof(tmac)))
1395 {
1396 GNUNET_break_op (0);
1398 return 0;
1399 }
1400 /* handle FINISH by destroying queue */
1403 "Handling plaintext, finish processed!\n");
1404 break;
1405
1406 default:
1408 "Handling plaintext, nothing processed!\n");
1409 GNUNET_break_op (0);
1411 return 0;
1412 }
1413 GNUNET_assert (0 != size);
1414 if (-1 != queue->unverified_size)
1415 queue->unverified_size += size;
1416 return size;
1417}
#define COMMUNICATOR_ADDRESS_PREFIX
Address prefix used by the communicator.
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.
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 INITIAL_CORE_KX_SIZE
Size of the initial core key exchange messages.
static uint32_t type
Type string converted to DNS type value.
#define GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED
Queue length.
struct GNUNET_TRANSPORT_QueueHandle * GNUNET_TRANSPORT_communicator_mq_add(struct GNUNET_TRANSPORT_CommunicatorHandle *ch, const struct GNUNET_PeerIdentity *peer, const char *address, uint32_t mtu, uint64_t q_len, uint32_t priority, enum GNUNET_NetworkType nt, enum GNUNET_TRANSPORT_ConnectionStatus cs, struct GNUNET_MQ_Handle *mq)
Notify transport service that a MQ became available due to an "inbound" connection or because the com...
@ GNUNET_TRANSPORT_CS_INBOUND
this is an inbound connection (communicator initiated)
@ GNUNET_TRANSPORT_CS_OUTBOUND
this is an outbound connection (transport initiated)
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX
TCP communicator payload box.
static unsigned int size
Size of the "table".
Definition: peer.c:68
Type of a nonce used for challenges.
struct GNUNET_CRYPTO_EddsaPublicKey public_key
TCP message box.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender's signature of type GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE_ACK.
struct GNUNET_CRYPTO_ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value.
struct GNUNET_PeerIdentity sender
Sender's identity.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.

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

Referenced by queue_read().

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

◆ tcp_address_to_sockaddr_numeric_v6()

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

Convert a struct sockaddr_in6 to astruct sockaddr *`.

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

Definition at line 1554 of file gnunet-communicator-libp2p.c.

1557{
1558 struct sockaddr *in;
1559
1560 v6.sin6_family = AF_INET6;
1561 v6.sin6_port = htons ((uint16_t) port);
1562#if HAVE_SOCKADDR_IN_SIN_LEN
1563 v6.sin6_len = sizeof(struct sockaddr_in6);
1564#endif
1565 v6.sin6_flowinfo = 0;
1566 v6.sin6_scope_id = 0;
1567 in = GNUNET_memdup (&v6, sizeof(v6));
1568 *sock_len = sizeof(struct sockaddr_in6);
1569
1570 return in;
1571}
static uint16_t port
Port number.
Definition: gnunet-bcd.c:146

References GNUNET_memdup, and port.

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

1585{
1586 struct sockaddr *in;
1587
1588 v4.sin_family = AF_INET;
1589 v4.sin_port = htons ((uint16_t) port);
1590#if HAVE_SOCKADDR_IN_SIN_LEN
1591 v4.sin_len = sizeof(struct sockaddr_in);
1592#endif
1593 in = GNUNET_memdup (&v4, sizeof(v4));
1594 *sock_len = sizeof(struct sockaddr_in);
1595 return in;
1596}

References GNUNET_memdup, and port.

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

1607{
1608 struct PortOnlyIpv4Ipv6 *po;
1609 struct sockaddr_in *i4;
1610 struct sockaddr_in6 *i6;
1611 socklen_t sock_len_ipv4;
1612 socklen_t sock_len_ipv6;
1613
1614 /* interpreting value as just a PORT number */
1615 if (*port > UINT16_MAX)
1616 {
1618 "BINDTO specification `%s' invalid: value too large for port\n",
1619 bindto);
1620 return NULL;
1621 }
1622
1623 po = GNUNET_new (struct PortOnlyIpv4Ipv6);
1624
1625 if (GNUNET_YES == disable_v6)
1626 {
1627 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
1628 po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
1629 *port);
1630 po->addr_len_ipv4 = sock_len_ipv4;
1631 }
1632 else
1633 {
1634
1635 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
1636 po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
1637 *port);
1638 po->addr_len_ipv4 = sock_len_ipv4;
1639
1640 i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
1641 po->addr_ipv6 = tcp_address_to_sockaddr_numeric_v6 (&sock_len_ipv6, *i6,
1642 *port);
1643
1644 po->addr_len_ipv6 = sock_len_ipv6;
1645
1646 GNUNET_free (i6);
1647 }
1648
1649 GNUNET_free (i4);
1650
1651 return po;
1652}
static struct sockaddr * tcp_address_to_sockaddr_numeric_v6(socklen_t *sock_len, struct sockaddr_in6 v6, unsigned int port)
Convert a struct sockaddr_in6 to astruct sockaddr *`.
static 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 int disable_v6
Disable IPv6.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
In case of port only configuration we like to bind to ipv4 and ipv6 addresses.
struct sockaddr * addr_ipv4
Ipv4 address we like to bind to.
struct sockaddr * addr_ipv6
Ipv6 address we like to bind to.
socklen_t addr_len_ipv6
Length of ipv6 address.
socklen_t addr_len_ipv4
Length of ipv4 address.

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

Referenced by run().

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

◆ extract_address()

static char * extract_address ( const char *  bindto)
static

This Method extracts the address part of the BINDTO string.

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

Definition at line 1662 of file gnunet-communicator-libp2p.c.

1663{
1664 char *addr;
1665 char *start;
1666 char *token;
1667 char *cp;
1668 char *rest = NULL;
1669
1671 "extract address with bindto %s\n",
1672 bindto);
1673
1674 if (NULL == bindto)
1676 "bindto is NULL\n");
1677
1678 cp = GNUNET_strdup (bindto);
1679
1681 "extract address 2\n");
1682
1683 start = cp;
1684 if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
1685 {
1686 start++; /* skip over '['*/
1687 cp[strlen (cp) - 1] = '\0'; /* eat ']'*/
1688 addr = GNUNET_strdup (start);
1689 }
1690 else
1691 {
1692 token = strtok_r (cp, "]", &rest);
1693 if (strlen (bindto) == strlen (token))
1694 {
1695 token = strtok_r (cp, ":", &rest);
1696 addr = GNUNET_strdup (token);
1697 }
1698 else
1699 {
1700 token++;
1701 addr = GNUNET_strdup (token);
1702 }
1703 }
1704
1706 "tcp address: %s\n",
1707 addr);
1708 GNUNET_free (cp);
1709 return addr;
1710}
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:38
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.

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

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

1721{
1722 unsigned int port;
1723 char dummy[2];
1724 char *token;
1725 char *addr;
1726 char *colon;
1727 char *cp;
1728 char *rest = NULL;
1729
1730 if (NULL != addr_and_port)
1731 {
1732 cp = GNUNET_strdup (addr_and_port);
1733 token = strtok_r (cp, "]", &rest);
1734 if (strlen (addr_and_port) == strlen (token))
1735 {
1736 colon = strrchr (cp, ':');
1737 if (NULL == colon)
1738 {
1739 GNUNET_free (cp);
1740 return 0;
1741 }
1742 addr = colon;
1743 addr++;
1744 }
1745 else
1746 {
1747 token = strtok_r (NULL, "]", &rest);
1748 if (NULL == token)
1749 {
1750 GNUNET_free (cp);
1751 return 0;
1752 }
1753 else
1754 {
1755 addr = token;
1756 addr++;
1757 }
1758 }
1759
1760
1761 if (1 == sscanf (addr, "%u%1s", &port, dummy))
1762 {
1763 /* interpreting value as just a PORT number */
1764 if (port > UINT16_MAX)
1765 {
1767 "Port `%u' invalid: value too large for port\n",
1768 port);
1769 GNUNET_free (cp);
1770 return 0;
1771 }
1772 }
1773 else
1774 {
1776 "BINDTO specification invalid: last ':' not followed by number\n");
1777 GNUNET_free (cp);
1778 return 0;
1779 }
1780 GNUNET_free (cp);
1781 }
1782 else
1783 {
1785 "return 0\n");
1786 /* interpret missing port as 0, aka pick any free one */
1787 port = 0;
1788 }
1789
1790 return port;
1791}
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 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 1802 of file gnunet-communicator-libp2p.c.

1803{
1804 struct sockaddr *in;
1805 unsigned int port;
1806 struct sockaddr_in v4;
1807 struct sockaddr_in6 v6;
1808 char *start;
1809
1810 memset (&v4, 0, sizeof(v4));
1811 start = extract_address (bindto);
1812 GNUNET_assert (NULL != start);
1814 "start %s\n",
1815 start);
1816
1818 "!bindto %s\n",
1819 bindto);
1820
1821
1822 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
1823 {
1824 port = extract_port (bindto);
1825
1827 "port %u\n",
1828 port);
1829
1830 in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port);
1831 }
1832 else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
1833 {
1834 port = extract_port (bindto);
1835 in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port);
1836 }
1837 else
1838 {
1839 GNUNET_assert (0);
1840 }
1841
1843 return in;
1844}
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 1856 of file gnunet-communicator-libp2p.c.

1859{
1860 struct Queue *queue = impl_state;
1861 uint16_t msize = ntohs (msg->size);
1862 struct TCPBox box;
1864 "In MQ send. Queue finishing: %s; write task running: %s\n",
1865 (GNUNET_YES == queue->finishing) ? "yes" : "no",
1866 (NULL == queue->write_task) ? "yes" : "no");
1867 GNUNET_assert (mq == queue->mq);
1868 queue->mq_awaits_continue = GNUNET_YES;
1869 if (GNUNET_YES == queue->finishing)
1870 return; /* this queue is dying, drop msg */
1871 GNUNET_assert (0 == queue->pwrite_off);
1872 box.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX);
1873 box.header.size = htons (msize);
1874 calculate_hmac (&queue->out_hmac, msg, msize, &box.hmac);
1875 memcpy (&queue->pwrite_buf[queue->pwrite_off], &box, sizeof(box));
1876 queue->pwrite_off += sizeof(box);
1877 memcpy (&queue->pwrite_buf[queue->pwrite_off], msg, msize);
1878 queue->pwrite_off += msize;
1880 "%lu bytes of plaintext to send\n", queue->pwrite_off);
1881 GNUNET_assert (NULL != queue->sock);
1882 if (NULL == queue->write_task)
1883 queue->write_task =
1885 queue->sock,
1886 &queue_write,
1887 queue);
1888}
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 1900 of file gnunet-communicator-libp2p.c.

1901{
1902 struct Queue *queue = impl_state;
1903
1904 if (mq == queue->mq)
1905 {
1906 queue->mq = NULL;
1908 }
1909}

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 1919 of file gnunet-communicator-libp2p.c.

1920{
1921 struct Queue *queue = impl_state;
1922
1923 GNUNET_assert (0 != queue->pwrite_off);
1924 queue->pwrite_off = 0;
1925}

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 1938 of file gnunet-communicator-libp2p.c.

1939{
1940 struct Queue *queue = cls;
1941
1943 "MQ error in queue to %s: %d\n",
1944 GNUNET_i2s (&queue->target),
1945 (int) error);
1947}

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 1958 of file gnunet-communicator-libp2p.c.

1959{
1960 queue->nt =
1961 GNUNET_NT_scanner_get_type (is, queue->address, queue->address_len);
1963 queue_map,
1964 &queue->key,
1965 queue,
1968 "# queues active",
1970 GNUNET_NO);
1971 queue->timeout =
1974 &mq_destroy,
1975 &mq_cancel,
1976 queue,
1977 NULL,
1978 &mq_error,
1979 queue);
1980}
static struct GNUNET_TESTING_Interpreter * is
static void mq_destroy(struct GNUNET_MQ_Handle *mq, void *impl_state)
Signature of functions implementing the destruction of a message queue.
static void mq_cancel(struct GNUNET_MQ_Handle *mq, void *impl_state)
Implementation function that cancels the currently sent message.
static void mq_error(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
static void mq_send(struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *msg, void *impl_state)
Signature of functions implementing the sending functionality of a message queue.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE
Allow multiple values with the same key.
struct GNUNET_MQ_Handle * GNUNET_MQ_queue_for_callbacks(GNUNET_MQ_SendImpl send, GNUNET_MQ_DestroyImpl destroy, GNUNET_MQ_CancelImpl cancel, void *impl_state, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *cls)
Create a message queue for the specified handlers.
Definition: mq.c:482
enum GNUNET_NetworkType GNUNET_NT_scanner_get_type(struct GNUNET_NT_InterfaceScanner *is, const struct sockaddr *addr, socklen_t addrlen)
Returns where the address is located: loopback, LAN or WAN.
Definition: nt.c:309

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

Referenced by mq_init(), and proto_read_kx().

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

◆ transmit_kx()

static void transmit_kx ( struct Queue queue,
const struct GNUNET_CRYPTO_HpkeEncapsulation c 
)
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 1994 of file gnunet-communicator-libp2p.c.

1996{
1997 struct TcpHandshakeSignature ths;
1998 struct TCPConfirmation tc;
1999
2000 memcpy (queue->cwrite_buf, c, sizeof(*c));
2001 queue->cwrite_off = sizeof(*c);
2002 /* compute 'tc' and append in encrypted format to cwrite_buf */
2003 tc.sender = my_identity;
2004 tc.monotonic_time =
2007 &tc.challenge,
2008 sizeof(tc.challenge));
2009 ths.purpose.purpose = htonl (
2011 ths.purpose.size = htonl (sizeof(ths));
2012 ths.sender = my_identity;
2013 ths.receiver = queue->target;
2014 ths.ephemeral = *c;
2015 ths.monotonic_time = tc.monotonic_time;
2016 ths.challenge = tc.challenge;
2018 &ths,
2019 &tc.sender_sig);
2020 GNUNET_assert (0 ==
2021 gcry_cipher_encrypt (queue->out_cipher,
2022 &queue->cwrite_buf[queue->cwrite_off],
2023 sizeof(tc),
2024 &tc,
2025 sizeof(tc)));
2026 queue->challenge = tc.challenge;
2027 queue->cwrite_off += sizeof(tc);
2028
2030 "handshake written\n");
2031}
#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:431
Encrypted continuation of TCP initial handshake.
Signature we use to verify that the ephemeral key was really chosen by the specified sender.

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

Referenced by start_initial_kx_out().

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

◆ start_initial_kx_out()

static void start_initial_kx_out ( struct Queue queue)
static

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

Must be called first before any data is sent.

Parameters
queuethe queue to setup

Definition at line 2042 of file gnunet-communicator-libp2p.c.

2043{
2045 struct GNUNET_ShortHashCode k;
2046
2048 &c, &k);
2049 setup_out_cipher (queue, &k);
2050 transmit_kx (queue, &c);
2051}
static void transmit_kx(struct Queue *queue, const struct GNUNET_CRYPTO_HpkeEncapsulation *c)
Generate and transmit our ephemeral key and the signature for the initial KX with the other peer.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hpke_elligator_kem_encaps(const struct GNUNET_CRYPTO_EcdhePublicKey *pkR, struct GNUNET_CRYPTO_HpkeEncapsulation *c, struct GNUNET_ShortHashCode *shared_secret)
Carries out ecdh encapsulation with given public key and the private key from a freshly created ephem...
Definition: crypto_hpke.c:327
HPKE DHKEM encapsulation (X25519) See RFC 9180.

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

Referenced by mq_init(), and proto_read_kx().

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

◆ handshake_monotime_store_cb()

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

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

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

Definition at line 2060 of file gnunet-communicator-libp2p.c.

2061{
2062 struct Queue *queue = cls;
2063 if (GNUNET_OK != success)
2064 {
2066 "Failed to store handshake monotonic time in PEERSTORE!\n");
2067 }
2068 queue->handshake_monotime_sc = NULL;
2069 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
2070}

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

Referenced by handshake_monotime_cb().

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

◆ handshake_monotime_cb()

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

Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE where found.

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

Definition at line 2081 of file gnunet-communicator-libp2p.c.

2084{
2085 struct Queue *queue = cls;
2086 struct GNUNET_TIME_AbsoluteNBO *mtbe;
2087 struct GNUNET_TIME_Absolute mt;
2088 const struct GNUNET_PeerIdentity *pid;
2089 struct GNUNET_TIME_AbsoluteNBO *handshake_monotonic_time;
2090
2091 (void) emsg;
2092
2093 handshake_monotonic_time = &queue->handshake_monotonic_time;
2094 pid = &queue->target;
2096 "tcp handshake with us %s\n",
2098 if (NULL == record)
2099 {
2100 queue->handshake_monotime_get = NULL;
2101 return;
2102 }
2104 "tcp handshake from peer %s\n",
2105 GNUNET_i2s (pid));
2106 if (sizeof(*mtbe) != record->value_size)
2107 {
2108 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
2109 GNUNET_break (0);
2110 return;
2111 }
2112 mtbe = record->value;
2113 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
2114 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
2115 queue->handshake_monotonic_time).abs_value_us)
2116 {
2118 "Queue from %s dropped, handshake monotime in the past\n",
2119 GNUNET_i2s (&queue->target));
2120 GNUNET_break (0);
2121 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
2122 queue->handshake_ack_monotime_get = NULL;
2124 return;
2125 }
2126 queue->handshake_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
2127 "transport_tcp_communicator",
2128 pid,
2130 handshake_monotonic_time,
2131 sizeof(*
2132 handshake_monotonic_time),
2135 &
2137 queue);
2138}
static void handshake_monotime_store_cb(void *cls, int success)
Callback called when peerstore store operation for handshake monotime is finished.
#define GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE
Key used to store sender's monotonic time from handshake message.

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

Referenced by decrypt_and_check_tc().

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

◆ decrypt_and_check_tc()

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

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

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

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

Definition at line 2153 of file gnunet-communicator-libp2p.c.

2156{
2157 struct TcpHandshakeSignature ths;
2159
2161 0 ==
2162 gcry_cipher_decrypt (queue->in_cipher,
2163 tc,
2164 sizeof(*tc),
2165 &ibuf[sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)],
2166 sizeof(*tc)));
2167 ths.purpose.purpose = htonl (
2169 ths.purpose.size = htonl (sizeof(ths));
2170 ths.sender = tc->sender;
2171 ths.receiver = my_identity;
2172 memcpy (&ths.ephemeral, ibuf, sizeof(struct GNUNET_CRYPTO_EcdhePublicKey));
2173 ths.monotonic_time = tc->monotonic_time;
2174 ths.challenge = tc->challenge;
2177 &ths,
2178 &tc->sender_sig,
2179 &tc->sender.public_key);
2180 if (GNUNET_YES == ret)
2181 queue->handshake_monotime_get =
2183 "transport_tcp_communicator",
2184 &queue->target,
2187 queue);
2188 return ret;
2189}
static void handshake_monotime_cb(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE w...
GNUNET_GenericReturnValue
Named constants for return values.

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

Referenced by proto_read_kx(), and queue_read_kx().

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

◆ queue_read_kx()

static void queue_read_kx ( void *  cls)
static

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

Parameters
clsa struct Queue

Definition at line 2200 of file gnunet-communicator-libp2p.c.

2201{
2202 struct Queue *queue = cls;
2203 ssize_t rcvd;
2204 struct GNUNET_TIME_Relative left;
2205 struct TCPConfirmation tc;
2206
2207 queue->read_task = NULL;
2208 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2209 if (0 == left.rel_value_us)
2210 {
2212 return;
2213 }
2214 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2215 &queue->cread_buf[queue->cread_off],
2216 BUF_SIZE - queue->cread_off);
2218 "Received %lu bytes to write in buffer of size %lu for KX from queue %p (expires in %"
2219 PRIu64 ")\n",
2220 rcvd, BUF_SIZE - queue->cread_off, queue, left.rel_value_us);
2221 if (-1 == rcvd)
2222 {
2223 if ((EAGAIN != errno) && (EINTR != errno))
2224 {
2227 return;
2228 }
2229 queue->read_task =
2231 return;
2232 }
2233 if (0 == rcvd)
2234 {
2235 /* Orderly shutdown of connection */
2237 "Socket for queue %p seems to have been closed\n", queue);
2239 return;
2240 }
2241 queue->cread_off += rcvd;
2242 if (queue->cread_off < INITIAL_KX_SIZE)
2243 {
2244 /* read more */
2246 "%lu/%lu bytes of KX read. Rescheduling...\n",
2247 queue->cread_off, INITIAL_KX_SIZE);
2248 queue->read_task =
2250 return;
2251 }
2252 /* we got all the data, let's find out who we are talking to! */
2254 (const struct GNUNET_CRYPTO_HpkeEncapsulation*)
2255 queue->cread_buf,
2256 queue);
2257 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, queue->cread_buf))
2258 {
2260 "Invalid TCP KX received from %s\n",
2261 GNUNET_a2s (queue->address, queue->address_len));
2263 return;
2264 }
2265 if (0 !=
2266 memcmp (&tc.sender, &queue->target, sizeof(struct GNUNET_PeerIdentity)))
2267 {
2269 "Invalid sender in TCP KX received from %s\n",
2270 GNUNET_a2s (queue->address, queue->address_len));
2272 return;
2273 }
2274 send_challenge (tc.challenge, queue);
2275 queue->write_task =
2277 queue->sock,
2278 &queue_write,
2279 queue);
2280
2281 /* update queue timeout */
2282 queue->timeout =
2284 /* prepare to continue with regular read task immediately */
2285 memmove (queue->cread_buf,
2286 &queue->cread_buf[INITIAL_KX_SIZE],
2287 queue->cread_off - (INITIAL_KX_SIZE));
2289 "cread_off is %lu bytes before adjusting\n",
2290 queue->cread_off);
2291 queue->cread_off -= INITIAL_KX_SIZE;
2293 "cread_off set to %lu bytes\n",
2294 queue->cread_off);
2296}
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...
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.
static void setup_in_cipher_elligator(const struct GNUNET_CRYPTO_HpkeEncapsulation *c, struct Queue *queue)
Setup cipher of queue for decryption from an elligator representative.
#define INITIAL_KX_SIZE
Size of the initial key exchange message sent first in both directions.
@ 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:1304

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

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

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

◆ proto_read_kx()

static void proto_read_kx ( void *  cls)
static

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

Parameters
clsa struct ProtoQueue

Definition at line 2306 of file gnunet-communicator-libp2p.c.

2307{
2308 struct ProtoQueue *pq = cls;
2309 ssize_t rcvd;
2310 struct GNUNET_TIME_Relative left;
2311 struct Queue *queue;
2312 struct TCPConfirmation tc;
2314
2315 pq->read_task = NULL;
2317 if (0 == left.rel_value_us)
2318 {
2319 free_proto_queue (pq);
2320 return;
2321 }
2322 rcvd = GNUNET_NETWORK_socket_recv (pq->sock,
2323 &pq->ibuf[pq->ibuf_off],
2324 sizeof(pq->ibuf) - pq->ibuf_off);
2326 "Proto received %lu bytes for KX\n", rcvd);
2327 if (-1 == rcvd)
2328 {
2329 if ((EAGAIN != errno) && (EINTR != errno))
2330 {
2332 free_proto_queue (pq);
2333 return;
2334 }
2335 /* try again */
2336 pq->read_task =
2338 return;
2339 }
2340 if (0 == rcvd)
2341 {
2342 /* Orderly shutdown of connection */
2344 "Socket for proto queue %p seems to have been closed\n", pq);
2345 free_proto_queue (pq);
2346 return;
2347 }
2348 pq->ibuf_off += rcvd;
2349 if (sizeof (struct TCPNATProbeMessage) == pq->ibuf_off)
2350 {
2351 struct TCPNATProbeMessage *pm = (struct TCPNATProbeMessage *) pq->ibuf;
2352
2354 &pm->clientIdentity);
2355
2356 queue = GNUNET_new (struct Queue);
2357 queue->target = pm->clientIdentity;
2358 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
2361 }
2362 else if (pq->ibuf_off > sizeof(pq->ibuf))
2363 {
2364 /* read more */
2365 pq->read_task =
2367 return;
2368 }
2369 else
2370 {
2371 /* we got all the data, let's find out who we are talking to! */
2372 queue = GNUNET_new (struct Queue);
2374 (const struct GNUNET_CRYPTO_HpkeEncapsulation *) pq->
2375 ibuf,
2376 queue);
2377 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, pq->ibuf))
2378 {
2380 "Invalid TCP KX received from %s\n",
2381 GNUNET_a2s (pq->address, pq->address_len));
2382 gcry_cipher_close (queue->in_cipher);
2384 free_proto_queue (pq);
2385 return;
2386 }
2387 queue->target = tc.sender;
2388 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
2391 }
2392 queue->address = pq->address; /* steals reference */
2393 queue->address_len = pq->address_len;
2394 queue->listen_sock = pq->listen_sock;
2395 queue->sock = pq->sock;
2396
2398 "created queue with target %s\n",
2399 GNUNET_i2s (&queue->target));
2400
2402 "start kx proto\n");
2403
2405 boot_queue (queue);
2406 queue->read_task =
2408 queue->sock,
2409 read_task,
2410 queue);
2411 queue->write_task =
2413 queue->sock,
2414 &queue_write,
2415 queue);
2416 // TODO To early! Move it somewhere else.
2417 // send_challenge (tc.challenge, queue);
2418 queue->challenge_received = tc.challenge;
2419
2421 GNUNET_free (pq);
2422}
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 eddsa_pub_to_hpke_key(struct GNUNET_CRYPTO_EddsaPublicKey *edpk, struct GNUNET_CRYPTO_EcdhePublicKey *pk)
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(), eddsa_pub_to_hpke_key(), free_proto_queue(), GNUNET_a2s(), GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_CONTAINER_DLL_remove, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_i2s(), GNUNET_log, GNUNET_log_strerror, GNUNET_NETWORK_socket_recv(), GNUNET_new, GNUNET_OK, GNUNET_SCHEDULER_add_read_net(), GNUNET_SCHEDULER_add_write_net(), GNUNET_TIME_absolute_get_remaining(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TRANSPORT_CS_INBOUND, GNUNET_TRANSPORT_CS_OUTBOUND, ProtoQueue::ibuf, ProtoQueue::ibuf_off, ProtoQueue::listen_sock, pm, proto_head, proto_read_kx(), proto_tail, queue(), queue_read(), queue_read_kx(), queue_write(), read_task, ProtoQueue::read_task, GNUNET_TIME_Relative::rel_value_us, setup_in_cipher_elligator(), ProtoQueue::sock, start_initial_kx_out(), tc, and ProtoQueue::timeout.

Referenced by create_proto_queue(), and proto_read_kx().

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

◆ create_proto_queue()

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

Definition at line 2426 of file gnunet-communicator-libp2p.c.

2429{
2430 struct ProtoQueue *pq = GNUNET_new (struct ProtoQueue);
2431
2432 if (NULL == sock)
2433 {
2434 // sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2435 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, 0);
2436 if (NULL == sock)
2437 {
2439 "socket(%d) failed: %s",
2440 in->sa_family,
2441 strerror (errno));
2442 GNUNET_free (in);
2443 GNUNET_free (pq);
2444 return NULL;
2445 }
2446 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, addrlen)) &&
2447 (errno != EINPROGRESS))
2448 {
2450 "connect to `%s' failed: %s",
2451 GNUNET_a2s (in, addrlen),
2452 strerror (errno));
2454 GNUNET_free (in);
2455 GNUNET_free (pq);
2456 return NULL;
2457 }
2458 }
2459 pq->address_len = addrlen;
2460 pq->address = in;
2462 pq->sock = sock;
2464 pq->sock,
2466 pq);
2468
2469 return pq;
2470}
#define PROTO_QUEUE_TIMEOUT
How long do we wait until we must have received the initial KX?
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:833
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:602

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

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 2520 of file gnunet-communicator-libp2p.c.

2521{
2522 struct sockaddr *in = cls;
2523 struct PendingReversal *pending_reversal;
2524 struct GNUNET_HashCode key;
2525
2527 sizeof(struct sockaddr),
2528 &key);
2530 &key);
2531
2532 GNUNET_assert (NULL != pending_reversal);
2533
2535 &key,
2536 pending_reversal))
2538 "No pending reversal found for address %s\n",
2539 GNUNET_a2s (in, sizeof (struct sockaddr)));
2540 GNUNET_free (pending_reversal->in);
2541 GNUNET_free (pending_reversal);
2542}

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

Referenced by mq_init().

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

◆ mq_init()

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

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

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

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

Definition at line 2564 of file gnunet-communicator-libp2p.c.

2565{
2566 struct sockaddr *in;
2567 socklen_t in_len = 0;
2568 const char *path;
2569 struct sockaddr_in *v4;
2570 struct sockaddr_in6 *v6;
2571 unsigned int is_natd = GNUNET_NO;
2572 struct GNUNET_HashCode key;
2573 struct GNUNET_HashCode queue_map_key;
2574 struct GNUNET_HashContext *hsh;
2575 struct Queue *queue;
2576
2578 "Connecting to %s at %s\n",
2579 GNUNET_i2s (peer),
2580 address);
2581 if (0 != strncmp (address,
2583 strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
2584 {
2585 GNUNET_break_op (0);
2586 return GNUNET_SYSERR;
2587 }
2588 path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
2589 in = tcp_address_to_sockaddr (path, &in_len);
2590
2591 if (NULL == in)
2592 {
2594 "Failed to setup TCP socket address\n");
2595 return GNUNET_SYSERR;
2596 }
2597
2599 "in %s\n",
2600 GNUNET_a2s (in, in_len));
2601
2604 GNUNET_CRYPTO_hash_context_read (hsh, peer, sizeof (*peer));
2605 GNUNET_CRYPTO_hash_context_finish (hsh, &queue_map_key);
2607
2608 if (NULL != queue)
2609 {
2611 "Queue for %s already exists or is in construction\n", address);
2612 GNUNET_free (in);
2613 return GNUNET_NO;
2614 }
2615 switch (in->sa_family)
2616 {
2617 case AF_INET:
2618 v4 = (struct sockaddr_in *) in;
2619 if (0 == v4->sin_port)
2620 {
2621 is_natd = GNUNET_YES;
2623 sizeof(struct sockaddr),
2624 &key);
2627 &key))
2628 {
2630 "There is already a request reversal for `%s'at `%s'\n",
2631 GNUNET_i2s (peer),
2632 address);
2633 GNUNET_free (in);
2634 return GNUNET_SYSERR;
2635 }
2636 }
2637 break;
2638
2639 case AF_INET6:
2640 if (GNUNET_YES == disable_v6)
2641 {
2643 "IPv6 disabled, skipping %s\n", address);
2644 GNUNET_free (in);
2645 return GNUNET_SYSERR;
2646 }
2647 v6 = (struct sockaddr_in6 *) in;
2648 if (0 == v6->sin6_port)
2649 {
2651 "Request reversal for `%s' at `%s' not possible for an IPv6 address\n",
2652 GNUNET_i2s (peer),
2653 address);
2654 GNUNET_free (in);
2655 return GNUNET_SYSERR;
2656 }
2657 break;
2658
2659 default:
2660 GNUNET_assert (0);
2661 }
2662
2663 if (GNUNET_YES == is_natd)
2664 {
2665 struct sockaddr_in local_sa;
2666 struct PendingReversal *pending_reversal;
2667
2668 memset (&local_sa, 0, sizeof(local_sa));
2669 local_sa.sin_family = AF_INET;
2670 local_sa.sin_port = htons (bind_port);
2671 /* We leave sin_address at 0, let the kernel figure it out,
2672 even if our bind() is more specific. (May want to reconsider
2673 later.) */
2674 if (GNUNET_OK != GNUNET_NAT_request_reversal (nat, &local_sa, v4))
2675 {
2677 "request reversal for `%s' at `%s' failed\n",
2678 GNUNET_i2s (peer),
2679 address);
2680 GNUNET_free (in);
2681 return GNUNET_SYSERR;
2682 }
2683 pending_reversal = GNUNET_new (struct PendingReversal);
2684 pending_reversal->in = in;
2687 &key,
2688 pending_reversal,
2690 pending_reversal->target = *peer;
2692 &
2694 in);
2696 "Created NAT WAIT connection to `%s' at `%s'\n",
2697 GNUNET_i2s (peer),
2698 GNUNET_a2s (in, sizeof (struct sockaddr)));
2699 }
2700 else
2701 {
2702 struct GNUNET_NETWORK_Handle *sock;
2703
2704 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM,
2705 IPPROTO_TCP);
2706 if (NULL == sock)
2707 {
2709 "socket(%d) failed: %s",
2710 in->sa_family,
2711 strerror (errno));
2712 GNUNET_free (in);
2713 return GNUNET_SYSERR;
2714 }
2715 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, in_len)) &&
2716 (errno != EINPROGRESS))
2717 {
2719 "connect to `%s' failed: %s",
2720 address,
2721 strerror (errno));
2723 GNUNET_free (in);
2724 return GNUNET_SYSERR;
2725 }
2726
2727 queue = GNUNET_new (struct Queue);
2728 queue->target = *peer;
2729 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
2730 queue->key = queue_map_key;
2731 queue->address = in;
2732 queue->address_len = in_len;
2733 queue->sock = sock;
2735 boot_queue (queue);
2737 "booted queue with target %s\n",
2738 GNUNET_i2s (&queue->target));
2739 // queue->mq_awaits_continue = GNUNET_YES;
2740 queue->read_task =
2742 queue->sock,
2744 queue);
2745
2746
2748 "start kx mq_init\n");
2749
2751 queue->write_task =
2753 queue->sock,
2754 &queue_write,
2755 queue);
2756 }
2757
2758 return GNUNET_OK;
2759}
static struct GNUNET_NAT_Handle * nat
Connection to NAT service.
static void pending_reversal_timeout(void *cls)
static struct sockaddr * tcp_address_to_sockaddr(const char *bindto, socklen_t *sock_len)
Convert TCP bind specification to a struct sockaddr *
static 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 char * address
GNS address for this phone.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_contains(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Check if the map contains any value under the given key (including values that are NULL).
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
void GNUNET_CRYPTO_hash_context_read(struct GNUNET_HashContext *hc, const void *buf, size_t size)
Add data to be hashed.
Definition: crypto_hash.c:363
void GNUNET_CRYPTO_hash_context_finish(struct GNUNET_HashContext *hc, struct GNUNET_HashCode *r_hash)
Finish the hash computation.
Definition: crypto_hash.c:387
struct GNUNET_HashContext * GNUNET_CRYPTO_hash_context_start(void)
Start incremental hashing operation.
Definition: crypto_hash.c:347
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:675
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:1277

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

Referenced by init_socket().

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

◆ get_lt_delete_it()

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

Iterator over all ListenTasks to clean up.

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

Definition at line 2771 of file gnunet-communicator-libp2p.c.

2774{
2775 struct ListenTask *lt = value;
2776
2777 (void) cls;
2778 (void) key;
2779 if (NULL != lt->listen_task)
2780 {
2782 lt->listen_task = NULL;
2783 }
2784 if (NULL != lt->listen_sock)
2785 {
2787 lt->listen_sock = NULL;
2788 }
2789 GNUNET_free (lt);
2790 return GNUNET_OK;
2791}

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

Referenced by do_shutdown().

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

◆ get_queue_delete_it()

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

Iterator over all message queues to clean up.

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

Definition at line 2803 of file gnunet-communicator-libp2p.c.

2806{
2807 struct Queue *queue = value;
2808
2809 (void) cls;
2810 (void) target;
2812 return GNUNET_OK;
2813}
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 2822 of file gnunet-communicator-libp2p.c.

2823{
2825 "Shutdown %s!\n",
2826 shutdown_running ? "running" : "not running");
2827
2829 return;
2830 else
2832
2833 while (NULL != proto_head)
2835 if (NULL != nat)
2836 {
2838 nat = NULL;
2839 }
2847 if (NULL != ch)
2848 {
2851 ch = NULL;
2852 }
2853 if (NULL != stats)
2854 {
2856 stats = NULL;
2857 }
2858 if (NULL != my_private_key)
2859 {
2861 my_private_key = NULL;
2862 }
2863 if (NULL != is)
2864 {
2866 is = NULL;
2867 }
2868 if (NULL != pils)
2869 {
2871 pils = NULL;
2872 }
2873 if (NULL != peerstore)
2874 {
2876 peerstore = NULL;
2877 }
2878 if (NULL != resolve_request_handle)
2879 {
2882 }
2884 "Shutdown done!\n");
2885}
static int get_lt_delete_it(void *cls, const struct GNUNET_HashCode *key, void *value)
Iterator over all ListenTasks to clean up.
static int get_queue_delete_it(void *cls, const struct GNUNET_HashCode *target, void *value)
Iterator over all message queues to clean up.
static struct GNUNET_PILS_Handle * pils
For PILS.
struct GNUNET_RESOLVER_RequestHandle * resolve_request_handle
Handle for DNS lookup of bindto address.
void GNUNET_PILS_disconnect(struct GNUNET_PILS_Handle *handle)
Disconnect from the PILS service.
Definition: pils_api.c:390
void GNUNET_TRANSPORT_communicator_disconnect(struct GNUNET_TRANSPORT_CommunicatorHandle *ch)
Disconnect from the transport service.
void GNUNET_TRANSPORT_communicator_address_remove_all(struct GNUNET_TRANSPORT_CommunicatorHandle *ch)
Notify transport service that this communicator no longer provides all its addresses for this peer.
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MultiHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
void GNUNET_NAT_unregister(struct GNUNET_NAT_Handle *nh)
Stop port redirection and public IP address detection for the given handle.
Definition: nat_api.c:703
void GNUNET_NT_scanner_done(struct GNUNET_NT_InterfaceScanner *is)
Terminate interface scanner.
Definition: nt.c:428
void GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
Disconnect from the PEERSTORE service.
void GNUNET_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh)
Cancel a request that is still pending with the resolver.
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).

References ch, free_proto_queue(), get_lt_delete_it(), get_queue_delete_it(), GNUNET_CONTAINER_multihashmap_destroy(), GNUNET_CONTAINER_multihashmap_iterate(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_NAT_unregister(), GNUNET_NT_scanner_done(), GNUNET_PEERSTORE_disconnect(), GNUNET_PILS_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(), pils, 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 2900 of file gnunet-communicator-libp2p.c.

2903{
2904 (void) cls;
2905 (void) sender;
2906 (void) msg;
2907 GNUNET_break_op (0);
2908}

References GNUNET_break_op, and msg.

Referenced by init_socket().

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 2915 of file gnunet-communicator-libp2p.c.

2916{
2917
2918 struct Addresses *saddrs;
2919
2921 "add address %s\n",
2922 GNUNET_a2s (in, in_len));
2923
2924 saddrs = GNUNET_new (struct Addresses);
2925 saddrs->addr = in;
2926 saddrs->addr_len = in_len;
2928
2930 "after add address %s\n",
2931 GNUNET_a2s (in, in_len));
2932
2934 "add address %s\n",
2935 GNUNET_a2s (saddrs->addr, saddrs->addr_len));
2936
2937 addrs_lens++;
2938}
static struct Addresses * addrs_head
Head of DLL with addresses we like to register at NAT service.
static struct Addresses * addrs_tail
Head of DLL with addresses we like to register at NAT service.
static int addrs_lens
Number of addresses in the DLL for register at NAT service.
DLL to store the addresses we like to register at NAT service.
struct sockaddr * addr
Address we like to register at NAT service.
socklen_t addr_len
Length of address we like to register at NAT service.

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

Referenced by init_socket().

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

◆ load_ikm()

static enum GNUNET_GenericReturnValue load_ikm ( )
static

FIXME: We could alternatively ask PILS for de/encaps, but at a high cost wrt async RPC calls...

Get the initial secret key for generating the peer id. This is supposed to be generated at random once in the lifetime of a peer, so all generated peer ids use the same initial secret key to optain the same peer id per set of addresses.

First check whether there's already a initial secret key. If so: return it. If no initial secret key exists yet, generate at random and store it where it will be found.

Parameters
initialsecret key the memory the initial secret key can be written to.

Definition at line 2955 of file gnunet-communicator-libp2p.c.

2956{
2957 char *filename;
2958 struct GNUNET_DISK_FileHandle *filehandle;
2959 int ret;
2960
2961 if (GNUNET_OK !=
2963 "pils",
2964 "SECRET_KEY_FILE",
2965 &filename))
2966 {
2968 "PILS service is lacking initial secret key file configuration setting. Exiting\n");
2969 return GNUNET_SYSERR;
2970 }
2971 if (NULL == filename)
2972 return GNUNET_SYSERR;
2974 if (GNUNET_SYSERR == ret)
2975 return GNUNET_SYSERR;
2976 if (GNUNET_NO == ret)
2977 {
2978 /* File does not exist - generate a new initial secret key and save it */
2979 // TODO consider the case that the file exists and ist not readable
2981 ikm,
2982 sizeof ikm);
2984 {
2986 "PILS service cannot create dir for saving initial secret key file. Exiting\n");
2987 return GNUNET_SYSERR;
2988 }
2989 filehandle = GNUNET_DISK_file_open (filename,
2993 | // would
2994 // the
2995 // group
2996 // need
2997 // read
2998 // perm?
3000 if (NULL == filehandle)
3001 {
3003 "PILS service had an issue with opening the initial secret key file. Exiting\n");
3004 GNUNET_DISK_file_close (filehandle);
3005 return GNUNET_SYSERR;
3006 }
3007 ret = GNUNET_DISK_file_write (filehandle,
3008 ikm,
3009 sizeof ikm);
3010 GNUNET_DISK_file_close (filehandle);
3011 if (sizeof ikm != ret)
3012 {
3014 "PILS service had an issue with writing the initial secret key to file. Exiting\n")
3015 ;
3016 return GNUNET_SYSERR;
3017 }
3018 }
3019 else
3020 {
3021 /* File existes - just read from it */
3022 off_t size;
3024 "PILS is going to read initial secret key from file %s\n",
3025 filename);
3026 filehandle = GNUNET_DISK_file_open (filename,
3029 if (NULL == filehandle)
3030 {
3032 " Not able to open file\n");
3033 return GNUNET_SYSERR;
3034 }
3035 if (GNUNET_OK != GNUNET_DISK_file_handle_size (filehandle, &size))
3036 {
3038 " File has the wrong size %lu\n",
3039 size);
3040 GNUNET_DISK_file_close (filehandle);
3041 return GNUNET_SYSERR;
3042 }
3043 if (sizeof ikm != size)
3044 {
3046 " Something is wrong with the file size, expected: %lu size, got: %lu\n",
3047 size,
3048 sizeof ikm);
3049 GNUNET_DISK_file_close (filehandle);
3050 return GNUNET_SYSERR;
3051 }
3052 ret = GNUNET_DISK_file_read (filehandle,
3053 ikm,
3054 sizeof ikm);
3055 GNUNET_DISK_file_close (filehandle);
3056 if (sizeof ikm != ret)
3057 {
3059 " Read initial secret key with wrong size %u, expected %lu\n", ret,
3060 sizeof ikm);
3061 return GNUNET_SYSERR;
3062 }
3063
3064 }
3065 return GNUNET_OK;
3066}
#define LOG(kind,...)
static char * filename
static unsigned char ikm[256/8]
The initial key material for the peer.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1258
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:710
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test_read(const char *fil)
Check that fil corresponds to a filename and the file has read permissions.
Definition: disk.c:540
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1332
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:633
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:673
enum GNUNET_GenericReturnValue GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size)
Get the size of an open file.
Definition: disk.c:206
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_NONE
Nobody is allowed to do anything to the file.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
Handle used to access files (and pipes).

References cfg, filename, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CRYPTO_QUALITY_NONCE, GNUNET_CRYPTO_random_block(), GNUNET_DISK_directory_create_for_file(), GNUNET_DISK_file_close(), GNUNET_DISK_file_handle_size(), GNUNET_DISK_file_open(), GNUNET_DISK_file_read(), GNUNET_DISK_file_test_read(), GNUNET_DISK_file_write(), GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_OPEN_READ, GNUNET_DISK_OPEN_WRITE, GNUNET_DISK_PERM_NONE, GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, ikm, LOG, ret, and size.

Referenced by run().

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 3077 of file gnunet-communicator-libp2p.c.

3079{
3080 struct sockaddr_storage in_sto;
3081 socklen_t sto_len;
3082 struct GNUNET_NETWORK_Handle *listen_sock;
3083 struct ListenTask *lt;
3084 int sockfd;
3085 struct GNUNET_HashCode h_sock;
3086
3087 if (NULL == addr)
3088 {
3090 "Address is NULL.\n");
3091 return GNUNET_SYSERR;
3092 }
3093
3095 "address %s\n",
3096 GNUNET_a2s (addr, in_len));
3097
3098 listen_sock =
3099 GNUNET_NETWORK_socket_create (addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
3100 if (NULL == listen_sock)
3101 {
3103 return GNUNET_SYSERR;
3104 }
3105
3106 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, addr, in_len))
3107 {
3109 GNUNET_NETWORK_socket_close (listen_sock);
3110 listen_sock = NULL;
3111 return GNUNET_SYSERR;
3112 }
3113
3114 if (GNUNET_OK !=
3115 GNUNET_NETWORK_socket_listen (listen_sock,
3116 5))
3117 {
3119 "listen");
3120 GNUNET_NETWORK_socket_close (listen_sock);
3121 listen_sock = NULL;
3122 return GNUNET_SYSERR;
3123 }
3124
3125 /* We might have bound to port 0, allowing the OS to figure it out;
3126 thus, get the real IN-address from the socket */
3127 sto_len = sizeof(in_sto);
3128
3129 if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock),
3130 (struct sockaddr *) &in_sto,
3131 &sto_len))
3132 {
3133 memcpy (&in_sto, addr, in_len);
3134 sto_len = in_len;
3135 }
3136
3137 // addr = (struct sockaddr *) &in_sto;
3138 in_len = sto_len;
3140 "Bound to `%s'\n",
3141 GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
3142 if (NULL == stats)
3143 stats = GNUNET_STATISTICS_create ("communicator-tcp", cfg);
3144
3145 if (NULL == is)
3147
3148 /* start listening */
3149
3150 lt = GNUNET_new (struct ListenTask);
3151 lt->listen_sock = listen_sock;
3152
3154 listen_sock,
3155 &listen_cb,
3156 lt);
3157
3159 "creating hash\n");
3160 sockfd = GNUNET_NETWORK_get_fd (lt->listen_sock);
3161 GNUNET_CRYPTO_hash (&sockfd,
3162 sizeof(int),
3163 &h_sock);
3164
3166 "creating map\n");
3167 if (NULL == lt_map)
3169
3171 "creating map entry\n");
3174 &h_sock,
3175 lt,
3177
3179 "map entry created\n");
3180
3181 if (NULL == queue_map)
3183
3184 if (NULL == ch)
3189 &mq_init,
3190 NULL,
3192 NULL,
3193 NULL);
3194
3195 if (NULL == ch)
3196 {
3197 GNUNET_break (0);
3198 if (NULL != resolve_request_handle)
3201 return GNUNET_SYSERR;
3202 }
3203
3204 add_addr (addr, in_len);
3205 return GNUNET_OK;
3206
3207}
#define COMMUNICATOR_CONFIG_SECTION
Configuration section used by the communicator.
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_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, GNUNET_TRANSPORT_StartBurstNotify sb)
Connect to the transport service.
@ GNUNET_TRANSPORT_CC_RELIABLE
Transmission is reliabile (with ACKs), e.g.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Bind a socket to a particular address.
Definition: network.c:439
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, int backlog)
Listen on a socket.
Definition: network.c:652
struct GNUNET_NT_InterfaceScanner * GNUNET_NT_scanner_init(void)
Initialize the address characterization client handle.
Definition: nt.c:407
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:567
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.

References add_addr(), cfg, ch, COMMUNICATOR_ADDRESS_PREFIX, COMMUNICATOR_CONFIG_SECTION, enc_notify_cb(), GNUNET_a2s(), GNUNET_assert, GNUNET_break, GNUNET_CONTAINER_multihashmap_create(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY, GNUNET_CRYPTO_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(), queue_map, resolve_request_handle, and stats.

Referenced by run().

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

◆ pid_change_cb()

void pid_change_cb ( void *  cls,
const struct GNUNET_HELLO_Parser parser,
const struct GNUNET_HashCode addr_hash 
)

Definition at line 3211 of file gnunet-communicator-libp2p.c.

3214{
3216 "Got PID to derive from `%s':\n",
3217 GNUNET_h2s (addr_hash));
3218 if (NULL == my_private_key)
3220
3222 (uint8_t*) ikm,
3223 addr_hash,
3229}
static void eddsa_priv_to_hpke_key(struct GNUNET_CRYPTO_EddsaPrivateKey *edpk, struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
void GNUNET_PILS_derive_pid(size_t seed_key_bytes, const uint8_t seed_key[seed_key_bytes], const struct GNUNET_HashCode *addrs_hash, struct GNUNET_CRYPTO_EddsaPrivateKey *outkey)
Generate the peer id from the addresses hash and the initial secret key.
Definition: pils_api.c:507
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:201
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
Private ECC key encoded for transmission.

References eddsa_priv_to_hpke_key(), GNUNET_CRYPTO_eddsa_key_get_public(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_h2s(), GNUNET_new, GNUNET_PILS_derive_pid(), ikm, LOG, my_identity, my_private_key, my_x25519_private_key, and GNUNET_PeerIdentity::public_key.

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 3241 of file gnunet-communicator-libp2p.c.

3245{
3246 struct sockaddr *in;
3247
3248 (void) cls;
3249
3250 cfg = c;
3251 if (GNUNET_OK !=
3254 "REKEY_INTERVAL",
3256 {
3258 }
3259 load_ikm ();
3261 GNUNET_assert (NULL != pils);
3263 if (NULL == peerstore)
3264 {
3265 GNUNET_free (bindto);
3266 GNUNET_break (0);
3268 return;
3269 }
3270
3272
3273 if (1 == sscanf (bindto, "%u%1s", &bind_port, dummy))
3274 {
3276 addr_len_ipv4 = po->addr_len_ipv4;
3278 "address po %s\n",
3279 GNUNET_a2s (po->addr_ipv4, addr_len_ipv4));
3280 if (NULL != po->addr_ipv4)
3281 {
3282 init_socket (po->addr_ipv4, addr_len_ipv4);
3283 }
3284 if (NULL != po->addr_ipv6)
3285 {
3286 addr_len_ipv6 = po->addr_len_ipv6;
3287 init_socket (po->addr_ipv6, addr_len_ipv6);
3288 }
3289
3290 // The following snippet is taken from https://github.com/libp2p/cpp-libp2p/blob/master/example/01-echo/libp2p_echo_client.cpp
3291 // and needs to be wrapped to c for being able to be called from within
3292 // this communicator.
3293 // It is merely there as a reminder of the needed functions and general
3294 // structure in order to send something via the c++ implementation of
3295 // libp2p.
3296 //
3297 //post(
3298 // *context,
3299 // [log,
3300 // host{std::move(host)},
3301 // &echo,
3302 // &message,
3303 // argv, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
3304 // sch] {
3305 // auto server_ma_res =
3306 // libp2p::multi::Multiaddress::create(argv[1]); // NOLINT
3307 // if (!server_ma_res) {
3308 // log->error("unable to create server multiaddress: {}",
3309 // server_ma_res.error());
3310 // std::exit(EXIT_FAILURE);
3311 // }
3312 // const auto &server_ma = server_ma_res.value();
3313
3314 // auto server_peer_id_str = server_ma.getPeerId();
3315 // if (!server_peer_id_str) {
3316 // log->error("unable to get peer id");
3317 // std::exit(EXIT_FAILURE);
3318 // }
3319
3320 // auto server_peer_id_res =
3321 // libp2p::peer::PeerId::fromBase58(*server_peer_id_str);
3322 // if (!server_peer_id_res) {
3323 // log->error("Unable to decode peer id from base 58: {}",
3324 // server_peer_id_res.error());
3325 // std::exit(EXIT_FAILURE);
3326 // }
3327
3328 // const auto &server_peer_id = server_peer_id_res.value();
3329
3330 // auto peer_info = libp2p::peer::PeerInfo{server_peer_id, {server_ma}};
3331
3332 // // create Host object and open a stream through it
3333 // host->newStream(
3334 // peer_info,
3335 // {echo.getProtocolId()},
3336 // [log, &echo, &message, sch](auto &&stream_res) {
3337 // if (!stream_res) {
3338 // log->error("Cannot connect to server: {}", stream_res.error());
3339 // std::exit(EXIT_FAILURE);
3340 // }
3341
3342 // auto stream_p = std::move(stream_res.value().stream);
3343
3344 // auto echo_client = echo.createClient(stream_p);
3345
3346 // if (message.size() < 120) {
3347 // log->info("SENDING {}", message);
3348 // } else {
3349 // log->info("SENDING {} bytes", message.size());
3350 // }
3351
3352 // sch->schedule(
3353 // [log, message, stream = std::move(stream_p), echo_client] {
3354 // echo_client->sendAnd(
3355 // message,
3356 // [log,
3357 // stream = std::move(stream)](auto &&response_result) {
3358 // if (response_result.has_error()) {
3359 // log->info("Error happened: {}",
3360 // response_result.error());
3361 // stream->close(
3362 // [log](auto &&) { std::exit(EXIT_SUCCESS); });
3363 // return;
3364 // }
3365 // auto &resp = response_result.value();
3366 // if (resp.size() < 120) {
3367 // log->info("RESPONSE {}", resp);
3368 // } else {
3369 // log->info("RESPONSE size={}", resp.size());
3370 // }
3371 // stream->close(
3372 // [](auto &&) { std::exit(EXIT_SUCCESS); });
3373 // });
3374 // },
3375 // std::chrono::milliseconds(1000));
3376 // });
3377 // });
3378
3379 GNUNET_free (po);
3380 GNUNET_free (bindto);
3381 return;
3382 }
3383}
void pid_change_cb(void *cls, const struct GNUNET_HELLO_Parser *parser, const struct GNUNET_HashCode *addr_hash)
static struct PortOnlyIpv4Ipv6 * tcp_address_to_sockaddr_port_only(const char *bindto, unsigned int *port)
Convert TCP bind specification to a struct PortOnlyIpv4Ipv6 *
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 do_shutdown(void *cls)
Shutdown the UNIX communicator.
static enum GNUNET_GenericReturnValue load_ikm()
FIXME: We could alternatively ask PILS for de/encaps, but at a high cost wrt async RPC calls....
#define DEFAULT_REKEY_INTERVAL
How often do we rekey based on time (at least)
struct GNUNET_PILS_Handle * GNUNET_PILS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_PILS_PidChangeCallback pid_change_cb, void *cls)
Connect to the PILS service.
Definition: pils_api.c:367
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.
struct GNUNET_PEERSTORE_Handle * GNUNET_PEERSTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the PEERSTORE service.
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:1339

References bind_port, cfg, COMMUNICATOR_CONFIG_SECTION, DEFAULT_REKEY_INTERVAL, do_shutdown(), dummy, GNUNET_a2s(), GNUNET_assert, GNUNET_break, GNUNET_CONFIGURATION_get_value_time(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_OK, GNUNET_PEERSTORE_connect(), GNUNET_PILS_connect(), GNUNET_SCHEDULER_add_shutdown(), GNUNET_SCHEDULER_shutdown(), init_socket(), load_ikm(), peerstore, pid_change_cb(), pils, rekey_interval, and tcp_address_to_sockaddr_port_only().

Referenced by main().

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

◆ main()

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

The main function for the UNIX communicator.

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

Definition at line 3394 of file gnunet-communicator-libp2p.c.

3395{
3396 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
3398 };
3399 int ret;
3400
3402 "Starting tcp communicator\n");
3403
3404 ret = (GNUNET_OK ==
3406 argc,
3407 argv,
3408 "gnunet-communicator-libp2p",
3409 _ ("GNUnet libp2p communicator"),
3410 options,
3411 &run,
3412 NULL))
3413 ? 0
3414 : 1;
3415 return ret;
3416}
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Setup communicator and launch network interactions.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(const struct GNUNET_OS_ProjectData *pd, int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:407
#define _(String)
GNU gettext support macro.
Definition: platform.h:179
Definition of a command line option.

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

Here is the call graph for this function:

Variable Documentation

◆ pils

struct GNUNET_PILS_Handle* pils
static

For PILS.

Definition at line 121 of file gnunet-communicator-libp2p.c.

Referenced by do_shutdown(), and run().

◆ stats

◆ ch

Our environment.

Definition at line 131 of file gnunet-communicator-libp2p.c.

Referenced by do_shutdown(), init_socket(), pass_plaintext_to_core(), and try_handle_plaintext().

◆ queue_map

struct GNUNET_CONTAINER_MultiHashMap* queue_map
static

Queues (map from peer identity to struct Queue)

Definition at line 136 of file gnunet-communicator-libp2p.c.

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

◆ lt_map

struct GNUNET_CONTAINER_MultiHashMap* lt_map
static

ListenTasks (map from socket to struct ListenTask)

Definition at line 141 of file gnunet-communicator-libp2p.c.

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

◆ my_identity

◆ my_private_key

struct GNUNET_CRYPTO_EddsaPrivateKey* my_private_key
static

◆ my_x25519_private_key

struct GNUNET_CRYPTO_EcdhePrivateKey my_x25519_private_key
static

Our private key.

Definition at line 156 of file gnunet-communicator-libp2p.c.

Referenced by pid_change_cb(), and setup_in_cipher_elligator().

◆ cfg

const struct GNUNET_CONFIGURATION_Handle* cfg
static

Our configuration.

Definition at line 161 of file gnunet-communicator-libp2p.c.

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

◆ addrs_head

struct Addresses* addrs_head
static

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

Definition at line 166 of file gnunet-communicator-libp2p.c.

Referenced by add_addr().

◆ addrs_tail

struct Addresses* addrs_tail
static

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

Definition at line 171 of file gnunet-communicator-libp2p.c.

Referenced by add_addr().

◆ peerstore

◆ shutdown_running

int shutdown_running = GNUNET_NO
static

A flag indicating we are already doing a shutdown.

Definition at line 181 of file gnunet-communicator-libp2p.c.

Referenced by do_shutdown(), and queue_destroy().