GNUnet debian-0.24.3-29-g453fda2cf
 
Loading...
Searching...
No Matches
gnunet-communicator-tcp.c File Reference

Transport plugin using TCP. More...

Include dependency graph for gnunet-communicator-tcp.c:

Go to the source code of this file.

Data Structures

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

Macros

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

Functions

static void listen_cb (void *cls)
 We have been notified that our listen socket has something to read.
 
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.
 
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.
 
static void queue_finish (struct Queue *queue)
 Append a 'finish' message to the outgoing transmission.
 
static void queue_read (void *cls)
 Queue read task.
 
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.
 
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 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 void rekey_monotime_store_cb (void *cls, int success)
 Callback called when peerstore store operation for rekey monotime value is finished.
 
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.
 
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.
 
static void setup_in_cipher (const struct GNUNET_CRYPTO_HpkeEncapsulation *ephemeral, struct Queue *queue)
 Setup cipher of queue for decryption.
 
static void do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
 Handle rekey message on queue.
 
static void handshake_ack_monotime_store_cb (void *cls, int success)
 Callback called when peerstore store operation for handshake ack monotime value is finished.
 
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.
 
static void send_challenge (struct GNUNET_CRYPTO_ChallengeNonceP challenge, struct Queue *queue)
 Sending challenge with TcpConfirmationAck back to sender of ephemeral key.
 
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.
 
static void inject_rekey (struct Queue *queue)
 Inject a struct TCPRekey message into the queue's plaintext buffer.
 
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.
 
static void proto_queue_write (void *cls)
 We have been notified that our socket is ready to write.
 
static void queue_write (void *cls)
 We have been notified that our socket is ready to write.
 
static size_t try_handle_plaintext (struct Queue *queue)
 Test if we have received a full message in plaintext.
 
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 struct PortOnlyIpv4Ipv6tcp_address_to_sockaddr_port_only (const char *bindto, unsigned int *port)
 Convert TCP bind specification to a struct PortOnlyIpv4Ipv6 *
 
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.
 
static struct sockaddr * tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
 Convert TCP bind specification to a struct sockaddr *
 
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.
 
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 creation of the message queue.
 
static void boot_queue (struct Queue *queue)
 Add the given queue to our internal data structure.
 
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.
 
static void start_initial_kx_out (struct Queue *queue)
 Initialize our key material for outgoing transmissions and inform the other peer about it.
 
static void handshake_monotime_store_cb (void *cls, int success)
 Callback called when peerstore store operation for handshake monotime is finished.
 
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.
 
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 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.
 
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 struct ProtoQueuecreate_proto_queue (struct GNUNET_NETWORK_Handle *sock, struct sockaddr *in, socklen_t addrlen)
 
static void try_connection_reversal (void *cls, const struct sockaddr *addr, socklen_t addrlen)
 
static void pending_reversal_timeout (void *cls)
 
static int mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
 Function called by the transport service to initialize a message queue given address information about another peer.
 
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 void do_shutdown (void *cls)
 Shutdown the UNIX 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 (!) via a different return path.
 
static void nat_address_cb (void *cls, void **app_ctx, int add_remove, enum GNUNET_NAT_AddressClass ac, const struct sockaddr *addr, socklen_t addrlen)
 Signature of the callback passed to GNUNET_NAT_register() for a function to call whenever our set of 'valid' addresses changes.
 
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.
 
static enum GNUNET_GenericReturnValue load_ikm ()
 FIXME: We could alternatively ask PILS for de/encaps, but at a high cost wrt async RPC calls...
 
static int init_socket (struct sockaddr *addr, socklen_t in_len)
 This method launch network interactions for each address we like to bind to.
 
static void nat_register ()
 This method reads from the DLL addrs_head to register them at the NAT service.
 
static void init_socket_resolv (void *cls, const struct sockaddr *addr, socklen_t in_len)
 This method is the callback called by the resolver API, and wraps method init_socket.
 
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.
 
int main (int argc, char *const *argv)
 The main function for the UNIX communicator.
 

Variables

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

Detailed Description

Transport plugin using TCP.

Author
Christian Grothoff

TODO:

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

Definition in file gnunet-communicator-tcp.c.

Macro Definition Documentation

◆ LOG

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

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

◆ NAT_TIMEOUT

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

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

◆ ADDRESS_VALIDITY_PERIOD

#define ADDRESS_VALIDITY_PERIOD    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)

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

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

126{
131
135 struct GNUNET_PeerIdentity sender;
136
141
145 struct GNUNET_CRYPTO_HpkeEncapsulation ephemeral;
146
151 struct GNUNET_TIME_AbsoluteNBO monotonic_time;
152
156 struct GNUNET_CRYPTO_ChallengeNonceP challenge;
157};
158
164{
169
174
179
185
190};
191
195struct TCPConfirmation
196{
201
206
212
217
218};
219
224{
225
226
231
236
241
247
252
253};
254
258struct TCPBox
259{
267
277
278 /* followed by as may bytes of payload as indicated in @e header,
279 excluding the TCPBox itself! */
280};
281
282
287struct TCPRekey
288{
293
303
308
313
319};
320
326{
331
336
341
346
352};
353
359struct TCPFinish
360{
365
375};
376
382{
387
392};
393
395
399struct PendingReversal
400{
401 /*
402 * Timeout task.
403 */
405
410
414 struct sockaddr *in;
415};
416
420struct ListenTask
421{
426
431};
432
436struct Queue
437{
442
447
452
457
461 gcry_cipher_hd_t in_cipher;
462
466 gcry_cipher_hd_t out_cipher;
467
471 struct GNUNET_HashCode key;
472
477
483
488
493
497 struct sockaddr *address;
498
503 uint64_t rekey_left_bytes;
504
510
514 socklen_t address_len;
515
519 struct GNUNET_MQ_Handle *mq;
520
525
529 unsigned long long bytes_in_queue;
530
534 char cread_buf[BUF_SIZE];
535
539 char cwrite_buf[BUF_SIZE];
540
544 char pread_buf[UINT16_MAX + 1 + sizeof(struct TCPBox)];
545
549 char pwrite_buf[UINT16_MAX + 1 + sizeof(struct TCPBox)];
550
555 size_t cread_off;
556
561 size_t cwrite_off;
562
567 size_t pread_off;
568
573 size_t pwrite_off;
574
579
587 unsigned int backpressure;
588
593
598
603
607 int finishing;
608
615 int destroyed;
616
621 int rekeyed;
622
627
632
637
642
647
652
657
662
667
672
677
681 // TODO remove?
682 size_t unverified_size;
683
688};
689
690
695struct ProtoQueue
696{
700 struct ProtoQueue *next;
701
705 struct ProtoQueue *prev;
706
711
716
721
725 char write_buf[sizeof (struct TCPNATProbeMessage)];
726
730 size_t write_off;
731
736
740 struct sockaddr *address;
741
745 socklen_t address_len;
746
751
756 char ibuf[INITIAL_KX_SIZE];
757
761 size_t ibuf_off;
762};
763
767struct PortOnlyIpv4Ipv6
768{
772 struct sockaddr *addr_ipv4;
773
777 socklen_t addr_len_ipv4;
778
782 struct sockaddr *addr_ipv6;
783
787 socklen_t addr_len_ipv6;
788
789};
790
794struct Addresses
795{
799 struct Addresses *next;
800
804 struct Addresses *prev;
805
809 struct sockaddr *addr;
810
814 socklen_t addr_len;
815
816};
817
818
822static unsigned long long max_queue_length;
823
827static struct GNUNET_PILS_Handle *pils;
828
832static struct GNUNET_STATISTICS_Handle *stats;
833
838
843
848
852static struct GNUNET_PeerIdentity my_identity;
853
857static unsigned long long rekey_max_bytes;
858
863
868
873
877static const struct GNUNET_CONFIGURATION_Handle *cfg;
878
882static struct GNUNET_NT_InterfaceScanner *is;
883
887static struct GNUNET_NAT_Handle *nat;
888
892static struct ProtoQueue *proto_head;
893
897static struct ProtoQueue *proto_tail;
898
903
907static struct Addresses *addrs_head;
908
912static struct Addresses *addrs_tail;
913
917static int addrs_lens;
918
923
927static int shutdown_running = GNUNET_NO;
928
932static int disable_v6;
933
937static unsigned int bind_port;
938
943
947static unsigned char ikm[256 / 8];
948
956static void
957listen_cb (void *cls);
958
959static void
962{
964 key.type = htonl (GNUNET_PUBLIC_KEY_TYPE_EDDSA);
965 key.eddsa_key = *edpk;
967}
968
969
970static void
973{
975 key.type = htonl (GNUNET_PUBLIC_KEY_TYPE_EDDSA);
976 key.eddsa_key = *edpk;
978}
979
980
988static void
989queue_destroy (struct Queue *queue)
990{
991 struct ListenTask *lt = NULL;
992 struct GNUNET_HashCode h_sock;
993 int sockfd;
994
995 if (NULL != queue->listen_sock)
996 {
997 sockfd = GNUNET_NETWORK_get_fd (queue->listen_sock);
998 GNUNET_CRYPTO_hash (&sockfd,
999 sizeof(int),
1000 &h_sock);
1001
1003 }
1004
1006 "Disconnecting queue for peer `%s'\n",
1007 GNUNET_i2s (&queue->target));
1008 if (NULL != queue->rekey_monotime_sc)
1009 {
1010 GNUNET_PEERSTORE_store_cancel (queue->rekey_monotime_sc);
1011 queue->rekey_monotime_sc = NULL;
1012 }
1013 if (NULL != queue->handshake_monotime_sc)
1014 {
1015 GNUNET_PEERSTORE_store_cancel (queue->handshake_monotime_sc);
1016 queue->handshake_monotime_sc = NULL;
1017 }
1018 if (NULL != queue->handshake_ack_monotime_sc)
1019 {
1020 GNUNET_PEERSTORE_store_cancel (queue->handshake_ack_monotime_sc);
1021 queue->handshake_ack_monotime_sc = NULL;
1022 }
1023 if (NULL != queue->rekey_monotime_get)
1024 {
1025 GNUNET_PEERSTORE_iteration_stop (queue->rekey_monotime_get);
1026 queue->rekey_monotime_get = NULL;
1027 }
1028 if (NULL != queue->handshake_monotime_get)
1029 {
1030 GNUNET_PEERSTORE_iteration_stop (queue->handshake_monotime_get);
1031 queue->handshake_monotime_get = NULL;
1032 }
1033 if (NULL != queue->handshake_ack_monotime_get)
1034 {
1035 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
1036 queue->handshake_ack_monotime_get = NULL;
1037 }
1038 if (NULL != queue->qh)
1039 {
1041 queue->qh = NULL;
1042 }
1044 GNUNET_YES ==
1047 "# queues active",
1049 GNUNET_NO);
1050 if (NULL != queue->read_task)
1051 {
1052 GNUNET_SCHEDULER_cancel (queue->read_task);
1053 queue->read_task = NULL;
1054 }
1055 if (NULL != queue->write_task)
1056 {
1057 GNUNET_SCHEDULER_cancel (queue->write_task);
1058 queue->write_task = NULL;
1059 }
1061 {
1063 "closing socket failed\n");
1064 }
1065 gcry_cipher_close (queue->in_cipher);
1066 gcry_cipher_close (queue->out_cipher);
1067 GNUNET_free (queue->address);
1068 if (0 != queue->backpressure)
1069 queue->destroyed = GNUNET_YES;
1070 else
1072
1073 if (NULL == lt)
1074 return;
1075
1076 if ((! shutdown_running) && (NULL == lt->listen_task))
1077 {
1079 "add read net listen\n");
1082 lt->listen_sock,
1083 &listen_cb,
1084 lt);
1085 }
1086 else
1087 GNUNET_free (lt);
1088}
1089
1090
1099static void
1100calculate_hmac (struct GNUNET_HashCode *hmac_secret,
1101 const void *buf,
1102 size_t buf_size,
1103 struct GNUNET_ShortHashCode *smac)
1104{
1105 struct GNUNET_HashCode mac;
1106
1107 GNUNET_CRYPTO_hmac_raw (hmac_secret,
1108 sizeof(struct GNUNET_HashCode),
1109 buf,
1110 buf_size,
1111 &mac);
1112 /* truncate to `struct GNUNET_ShortHashCode` */
1113 memcpy (smac, &mac, sizeof(struct GNUNET_ShortHashCode));
1114 /* ratchet hmac key */
1115 GNUNET_CRYPTO_hash (hmac_secret,
1116 sizeof(struct GNUNET_HashCode),
1117 hmac_secret);
1118}
1119
1120
1127static void
1128queue_finish (struct Queue *queue)
1129{
1130 struct TCPFinish fin;
1131
1132 memset (&fin, 0, sizeof(fin));
1133 fin.header.size = htons (sizeof(fin));
1134 fin.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH);
1135 calculate_hmac (&queue->out_hmac, &fin, sizeof(fin), &fin.hmac);
1136 /* if there is any message left in pwrite_buf, we
1137 overwrite it (possibly dropping the last message
1138 from CORE hard here) */
1139 memcpy (queue->pwrite_buf, &fin, sizeof(fin));
1140 queue->pwrite_off = sizeof(fin);
1141 /* This flag will ensure that #queue_write() no longer
1142 notifies CORE about the possibility of sending
1143 more data, and that #queue_write() will call
1144 #queue_destroy() once the @c fin was fully written. */
1145 queue->finishing = GNUNET_YES;
1146}
1147
1148
1154static void
1155queue_read (void *cls);
1156
1157
1165static void
1166core_read_finished_cb (void *cls, int success)
1167{
1168 struct Queue *queue = cls;
1169 if (GNUNET_OK != success)
1171 "# messages lost in communicator API towards CORE",
1172 1,
1173 GNUNET_NO);
1174 if (NULL == queue)
1175 return;
1176
1178 "backpressure %u\n",
1179 queue->backpressure);
1180
1181 queue->backpressure--;
1182 /* handle deferred queue destruction */
1183 if ((queue->destroyed) && (0 == queue->backpressure))
1184 {
1186 return;
1187 }
1188 else if (GNUNET_YES != queue->destroyed)
1189 {
1190 queue->timeout =
1192 );
1193 /* possibly unchoke reading, now that CORE made progress */
1194 if (NULL == queue->read_task)
1195 queue->read_task =
1197 queue->timeout),
1198 queue->sock,
1199 &queue_read,
1200 queue);
1201 }
1202}
1203
1204
1214static void
1216 const void *plaintext,
1217 size_t plaintext_len)
1218{
1219 const struct GNUNET_MessageHeader *hdr = plaintext;
1220 int ret;
1221
1223 "pass message from %s to core\n",
1224 GNUNET_i2s (&queue->target));
1225
1226 if (ntohs (hdr->size) != plaintext_len)
1227 {
1228 /* NOTE: If we ever allow multiple CORE messages in one
1229 BOX, this will have to change! */
1230 GNUNET_break (0);
1231 return;
1232 }
1234 &queue->target,
1235 hdr,
1238 queue);
1240 "passed to core\n");
1241 if (GNUNET_OK == ret)
1242 queue->backpressure++;
1243 GNUNET_break (GNUNET_NO != ret); /* backpressure not working!? */
1244 if (GNUNET_SYSERR == ret)
1246 "# bytes lost due to CORE not running",
1247 plaintext_len,
1248 GNUNET_NO);
1249}
1250
1251
1261static void
1262setup_cipher (const struct GNUNET_ShortHashCode *prk,
1263 const struct GNUNET_PeerIdentity *pid,
1264 gcry_cipher_hd_t *cipher,
1265 struct GNUNET_HashCode *hmac_key)
1266{
1267 char key[256 / 8];
1268 char ctr[128 / 8];
1269
1270 GNUNET_assert (0 == gcry_cipher_open (cipher,
1271 GCRY_CIPHER_AES256 /* low level: go for speed */
1272 ,
1273 GCRY_CIPHER_MODE_CTR,
1274 0 /* flags */));
1277 sizeof(key),
1278 prk,
1279 "gnunet-communicator-tcp-key",
1280 strlen (
1281 "gnunet-communicator-tcp-key"),
1282 NULL,
1283 0));
1284 GNUNET_assert (0 == gcry_cipher_setkey (*cipher, key, sizeof(key)));
1287 sizeof(ctr),
1288 prk,
1289 "gnunet-communicator-tcp-ctr",
1290 strlen (
1291 "gnunet-communicator-tcp-ctr"),
1292 NULL,
1293 0));
1294 gcry_cipher_setctr (*cipher, ctr, sizeof(ctr));
1296 GNUNET_CRYPTO_hkdf_expand (hmac_key,
1297 sizeof(struct GNUNET_HashCode),
1298 prk,
1299 "gnunet-communicator-hmac",
1300 strlen ("gnunet-communicator-hmac"),
1301 NULL,
1302 0));
1303}
1304
1305
1311static void
1312rekey_monotime_store_cb (void *cls, int success)
1313{
1314 struct Queue *queue = cls;
1315 if (GNUNET_OK != success)
1316 {
1318 "Failed to store rekey monotonic time in PEERSTORE!\n");
1319 }
1320 queue->rekey_monotime_sc = NULL;
1321 GNUNET_PEERSTORE_iteration_next (queue->rekey_monotime_get, 1);
1322}
1323
1324
1332static void
1333rekey_monotime_cb (void *cls,
1334 const struct GNUNET_PEERSTORE_Record *record,
1335 const char *emsg)
1336{
1337 struct Queue *queue = cls;
1338 struct GNUNET_TIME_AbsoluteNBO *mtbe;
1339 struct GNUNET_TIME_Absolute mt;
1340 const struct GNUNET_PeerIdentity *pid;
1341 struct GNUNET_TIME_AbsoluteNBO *rekey_monotonic_time;
1342
1343 (void) emsg;
1344
1345 rekey_monotonic_time = &queue->rekey_monotonic_time;
1346 pid = &queue->target;
1347 if (NULL == record)
1348 {
1349 queue->rekey_monotime_get = NULL;
1350 return;
1351 }
1352 if (sizeof(*mtbe) != record->value_size)
1353 {
1354 GNUNET_PEERSTORE_iteration_next (queue->rekey_monotime_get, 1);
1355 GNUNET_break (0);
1356 return;
1357 }
1358 mtbe = record->value;
1359 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1360 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
1361 queue->rekey_monotonic_time).abs_value_us)
1362 {
1364 "Queue from %s dropped, rekey monotime in the past\n",
1365 GNUNET_i2s (&queue->target));
1366 GNUNET_break (0);
1367 GNUNET_PEERSTORE_iteration_stop (queue->rekey_monotime_get);
1368 queue->rekey_monotime_get = NULL;
1369 // FIXME: Why should we try to gracefully finish here??
1371 return;
1372 }
1373 queue->rekey_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
1374 "transport_tcp_communicator",
1375 pid,
1377 rekey_monotonic_time,
1378 sizeof(*
1379 rekey_monotonic_time),
1383 queue);
1384}
1385
1386
1393static void
1395 const struct GNUNET_CRYPTO_HpkeEncapsulation *c,
1396 struct Queue *queue)
1397{
1398 struct GNUNET_ShortHashCode k;
1399
1401 setup_cipher (&k, &my_identity, &queue->in_cipher, &queue->in_hmac);
1402}
1403
1404
1411static void
1412setup_in_cipher (const struct GNUNET_CRYPTO_HpkeEncapsulation *ephemeral,
1413 struct Queue *queue)
1414{
1415 struct GNUNET_ShortHashCode k;
1416
1418 setup_cipher (&k, &my_identity, &queue->in_cipher, &queue->in_hmac);
1419}
1420
1421
1430static void
1431do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
1432{
1433 struct TcpRekeySignature thp;
1434
1436 thp.purpose.size = htonl (sizeof(thp));
1438 "do_rekey size %u\n",
1439 thp.purpose.size);
1440 thp.sender = queue->target;
1442 "sender %s\n",
1443 GNUNET_p2s (&thp.sender.public_key));
1445 "sender %s\n",
1446 GNUNET_p2s (&queue->target.public_key));
1447 thp.receiver = my_identity;
1449 "receiver %s\n",
1450 GNUNET_p2s (&thp.receiver.public_key));
1451 thp.ephemeral = rekey->ephemeral;
1453 "ephemeral %s\n",
1454 GNUNET_e2s ((struct GNUNET_CRYPTO_EcdhePublicKey*) &thp.ephemeral)
1455 );
1456 thp.monotonic_time = rekey->monotonic_time;
1458 "time %s\n",
1460 GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
1461 GNUNET_assert (ntohl ((&thp)->purpose.size) == sizeof (*(&thp)));
1462 if (GNUNET_OK !=
1465 &thp,
1466 &rekey->sender_sig,
1467 &queue->target.public_key))
1468 {
1469 GNUNET_break (0);
1470 // FIXME Why should we try to gracefully finish here?
1472 return;
1473 }
1474 queue->rekey_monotonic_time = rekey->monotonic_time;
1475 queue->rekey_monotime_get = GNUNET_PEERSTORE_iteration_start (peerstore,
1476 "transport_tcp_communicator",
1477 &queue->target,
1479 &
1481 queue);
1482 gcry_cipher_close (queue->in_cipher);
1483 queue->rekeyed = GNUNET_YES;
1484 setup_in_cipher (&rekey->ephemeral, queue);
1485}
1486
1487
1493static void
1494handshake_ack_monotime_store_cb (void *cls, int success)
1495{
1496 struct Queue *queue = cls;
1497
1498 if (GNUNET_OK != success)
1499 {
1501 "Failed to store handshake ack monotonic time in PEERSTORE!\n");
1502 }
1503 queue->handshake_ack_monotime_sc = NULL;
1504 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
1505}
1506
1507
1515static void
1516handshake_ack_monotime_cb (void *cls,
1517 const struct GNUNET_PEERSTORE_Record *record,
1518 const char *emsg)
1519{
1520 struct Queue *queue = cls;
1521 struct GNUNET_TIME_AbsoluteNBO *mtbe;
1522 struct GNUNET_TIME_Absolute mt;
1523 const struct GNUNET_PeerIdentity *pid;
1524 struct GNUNET_TIME_AbsoluteNBO *handshake_ack_monotonic_time;
1525
1526 (void) emsg;
1527
1528 handshake_ack_monotonic_time = &queue->handshake_ack_monotonic_time;
1529 pid = &queue->target;
1530 if (NULL == record)
1531 {
1532 queue->handshake_ack_monotime_get = NULL;
1533 return;
1534 }
1535 if (sizeof(*mtbe) != record->value_size)
1536 {
1537 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
1538 GNUNET_break (0);
1539 return;
1540 }
1541 mtbe = record->value;
1542 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1543 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
1544 queue->handshake_ack_monotonic_time).abs_value_us)
1545 {
1547 "Queue from %s dropped, handshake ack monotime in the past\n",
1548 GNUNET_i2s (&queue->target));
1549 GNUNET_break (0);
1550 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
1551 queue->handshake_ack_monotime_get = NULL;
1552 // FIXME: Why should we try to gracefully finish here?
1554 return;
1555 }
1556 queue->handshake_ack_monotime_sc =
1558 "transport_tcp_communicator",
1559 pid,
1561 handshake_ack_monotonic_time,
1562 sizeof(*handshake_ack_monotonic_time),
1566 queue);
1567}
1568
1569
1576static void
1578 struct Queue *queue)
1579{
1580 struct TCPConfirmationAck tca;
1581 struct TcpHandshakeAckSignature thas;
1582
1584 "sending challenge\n");
1585
1586 tca.header.type = ntohs (
1588 tca.header.size = ntohs (sizeof(tca));
1589 tca.challenge = challenge;
1590 tca.sender = my_identity;
1591 tca.monotonic_time =
1593 thas.purpose.purpose = htonl (
1595 thas.purpose.size = htonl (sizeof(thas));
1596 thas.sender = my_identity;
1597 thas.receiver = queue->target;
1598 thas.monotonic_time = tca.monotonic_time;
1599 thas.challenge = tca.challenge;
1601 &thas,
1602 &tca.sender_sig);
1603 GNUNET_assert (0 ==
1604 gcry_cipher_encrypt (queue->out_cipher,
1605 &queue->cwrite_buf[queue->cwrite_off],
1606 sizeof(tca),
1607 &tca,
1608 sizeof(tca)));
1609 queue->cwrite_off += sizeof(tca);
1611 "sending challenge done\n");
1612}
1613
1614
1621static void
1623{
1624 setup_cipher (dh, &queue->target, &queue->out_cipher, &queue->out_hmac);
1626 queue->rekey_left_bytes =
1628}
1629
1630
1637static void
1638inject_rekey (struct Queue *queue)
1639{
1640 struct TCPRekey rekey;
1641 struct TcpRekeySignature thp;
1642 struct GNUNET_ShortHashCode k;
1643
1644 GNUNET_assert (0 == queue->pwrite_off);
1645 memset (&rekey, 0, sizeof(rekey));
1646 GNUNET_CRYPTO_eddsa_kem_encaps (&queue->target.public_key, &rekey.ephemeral,
1647 &k);
1649 rekey.header.size = ntohs (sizeof(rekey));
1650 rekey.monotonic_time =
1652 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY);
1653 thp.purpose.size = htonl (sizeof(thp));
1655 "inject_rekey size %u\n",
1656 thp.purpose.size);
1657 thp.sender = my_identity;
1659 "sender %s\n",
1660 GNUNET_p2s (&thp.sender.public_key));
1661 thp.receiver = queue->target;
1663 "receiver %s\n",
1664 GNUNET_p2s (&thp.receiver.public_key));
1665 thp.ephemeral = rekey.ephemeral;
1667 "ephemeral %s\n",
1668 GNUNET_e2s ((struct GNUNET_CRYPTO_EcdhePublicKey*) &thp.ephemeral)
1669 );
1670 thp.monotonic_time = rekey.monotonic_time;
1672 "time %s\n",
1674 GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
1676 &thp,
1677 &rekey.sender_sig);
1678 calculate_hmac (&queue->out_hmac, &rekey, sizeof(rekey), &rekey.hmac);
1679 /* Encrypt rekey message with 'old' cipher */
1680 GNUNET_assert (0 ==
1681 gcry_cipher_encrypt (queue->out_cipher,
1682 &queue->cwrite_buf[queue->cwrite_off],
1683 sizeof(rekey),
1684 &rekey,
1685 sizeof(rekey)));
1686 queue->cwrite_off += sizeof(rekey);
1687 /* Setup new cipher for successive messages */
1688 gcry_cipher_close (queue->out_cipher);
1689 setup_out_cipher (queue, &k);
1690}
1691
1692
1693static int
1695 const struct GNUNET_HashCode *key,
1696 void *value)
1697{
1698 struct PendingReversal *pending_reversal = value;
1699 (void) cls;
1700
1701 if (NULL != pending_reversal->timeout_task)
1702 {
1703 GNUNET_SCHEDULER_cancel (pending_reversal->timeout_task);
1704 pending_reversal->timeout_task = NULL;
1705 }
1708 key,
1709 pending_reversal));
1710 GNUNET_free (pending_reversal->in);
1711 GNUNET_free (pending_reversal);
1712 return GNUNET_OK;
1713}
1714
1715
1716static void
1717check_and_remove_pending_reversal (struct sockaddr *in, sa_family_t sa_family,
1718 struct GNUNET_PeerIdentity *sender)
1719{
1720 if (AF_INET == sa_family)
1721 {
1722 struct PendingReversal *pending_reversal;
1723 struct GNUNET_HashCode key;
1724 struct sockaddr_in *natted_address;
1725
1726 natted_address = GNUNET_memdup (in, sizeof (struct sockaddr));
1727 natted_address->sin_port = 0;
1728 GNUNET_CRYPTO_hash (natted_address,
1729 sizeof(struct sockaddr),
1730 &key);
1731
1733 &key);
1734 if (NULL != pending_reversal && (NULL == sender ||
1735 0 != memcmp (sender,
1736 &pending_reversal->target,
1737 sizeof(struct
1739 {
1741 "Removing invalid pending reversal for `%s'at `%s'\n",
1742 GNUNET_i2s (&pending_reversal->target),
1743 GNUNET_a2s (in, sizeof (struct sockaddr)));
1744 pending_reversals_delete_it (NULL, &key, pending_reversal);
1745 }
1746 GNUNET_free (natted_address);
1747 }
1748}
1749
1750
1756static void
1757free_proto_queue (struct ProtoQueue *pq)
1758{
1759 if (NULL != pq->listen_sock)
1760 {
1762 pq->listen_sock = NULL;
1763 }
1764 if (NULL != pq->read_task)
1765 {
1767 pq->read_task = NULL;
1768 }
1769 if (NULL != pq->write_task)
1770 {
1772 pq->write_task = NULL;
1773 }
1774 check_and_remove_pending_reversal (pq->address, pq->address->sa_family, NULL);
1776 GNUNET_free (pq->address);
1778 GNUNET_free (pq);
1779}
1780
1781
1788static void
1789proto_queue_write (void *cls)
1790{
1791 struct ProtoQueue *pq = cls;
1792 ssize_t sent;
1793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In proto queue write\n");
1794 pq->write_task = NULL;
1795 if (0 != pq->write_off)
1796 {
1797 sent = GNUNET_NETWORK_socket_send (pq->sock,
1798 pq->write_buf,
1799 pq->write_off);
1801 "Sent %lu bytes to TCP queue\n", sent);
1802 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1803 {
1805 free_proto_queue (pq);
1806 return;
1807 }
1808 if (sent > 0)
1809 {
1810 size_t usent = (size_t) sent;
1811 pq->write_off -= usent;
1812 memmove (pq->write_buf,
1813 &pq->write_buf[usent],
1814 pq->write_off);
1815 }
1816 }
1817 /* do we care to write more? */
1818 if ((0 < pq->write_off))
1819 pq->write_task =
1821 pq->sock,
1823 pq);
1824}
1825
1826
1833static void
1834queue_write (void *cls)
1835{
1836 struct Queue *queue = cls;
1837 ssize_t sent;
1838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In queue write\n");
1839 queue->write_task = NULL;
1840 if (0 != queue->cwrite_off)
1841 {
1842 sent = GNUNET_NETWORK_socket_send (queue->sock,
1843 queue->cwrite_buf,
1844 queue->cwrite_off);
1846 "Sent %lu bytes to TCP queue\n", sent);
1847 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1848 {
1851 return;
1852 }
1853 if (sent > 0)
1854 {
1855 size_t usent = (size_t) sent;
1856 queue->cwrite_off -= usent;
1857 memmove (queue->cwrite_buf,
1858 &queue->cwrite_buf[usent],
1859 queue->cwrite_off);
1860 queue->timeout =
1863 }
1864 }
1865 {
1866 /* can we encrypt more? (always encrypt full messages, needed
1867 such that #mq_cancel() can work!) */
1868 unsigned int we_do_not_need_to_rekey = (0 < queue->rekey_left_bytes
1869 - (queue->cwrite_off
1870 + queue->pwrite_off
1871 + sizeof (struct TCPRekey)));
1872 if (we_do_not_need_to_rekey &&
1873 (queue->pwrite_off > 0) &&
1874 (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE))
1875 {
1877 "Encrypting %lu bytes\n", queue->pwrite_off);
1878 GNUNET_assert (0 ==
1879 gcry_cipher_encrypt (queue->out_cipher,
1880 &queue->cwrite_buf[queue->cwrite_off],
1881 queue->pwrite_off,
1882 queue->pwrite_buf,
1883 queue->pwrite_off));
1884 if (queue->rekey_left_bytes > queue->pwrite_off)
1885 queue->rekey_left_bytes -= queue->pwrite_off;
1886 else
1887 queue->rekey_left_bytes = 0;
1888 queue->cwrite_off += queue->pwrite_off;
1889 queue->pwrite_off = 0;
1890 }
1891 // if ((-1 != unverified_size)&& ((0 == queue->pwrite_off) &&
1892 if (((0 == queue->rekey_left_bytes) ||
1894 queue->rekey_time).rel_value_us)) &&
1895 (((0 == queue->pwrite_off) || ! we_do_not_need_to_rekey) &&
1896 (queue->cwrite_off + sizeof (struct TCPRekey) <= BUF_SIZE)))
1897 {
1899 }
1900 }
1901 if ((0 == queue->pwrite_off) && (! queue->finishing) &&
1902 (GNUNET_YES == queue->mq_awaits_continue))
1903 {
1904 queue->mq_awaits_continue = GNUNET_NO;
1906 }
1907 /* did we just finish writing 'finish'? */
1908 if ((0 == queue->cwrite_off) && (GNUNET_YES == queue->finishing))
1909 {
1911 "Finishing queue\n");
1913 return;
1914 }
1915 /* do we care to write more? */
1916 if ((0 < queue->cwrite_off) || (0 < queue->pwrite_off))
1917 queue->write_task =
1919 queue->sock,
1920 &queue_write,
1921 queue);
1922}
1923
1924
1932static size_t
1934{
1935 const struct GNUNET_MessageHeader *hdr;
1936 const struct TCPConfirmationAck *tca;
1937 const struct TCPBox *box;
1938 const struct TCPRekey *rekey;
1939 const struct TCPFinish *fin;
1940 struct TCPRekey rekeyz;
1941 struct TCPFinish finz;
1942 struct GNUNET_ShortHashCode tmac;
1943 uint16_t type;
1944 size_t size = 0;
1945 struct TcpHandshakeAckSignature thas;
1946 const struct GNUNET_CRYPTO_ChallengeNonceP challenge = queue->challenge;
1947
1949 "try handle plaintext!\n");
1950
1951 hdr = (const struct GNUNET_MessageHeader *) queue->pread_buf;
1952 if ((sizeof(*hdr) > queue->pread_off))
1953 {
1955 "Handling plaintext, not even a header!\n");
1956 return 0; /* not even a header */
1957 }
1958
1959 if ((GNUNET_YES != queue->initial_core_kx_done) && (queue->unverified_size >
1961 {
1963 "Already received data of size %lu bigger than KX size %lu!\n",
1964 queue->unverified_size,
1966 GNUNET_break_op (0);
1968 return 0;
1969 }
1970
1971 type = ntohs (hdr->type);
1972 switch (type)
1973 {
1975 tca = (const struct TCPConfirmationAck *) queue->pread_buf;
1977 "start processing ack\n");
1978 if (sizeof(*tca) > queue->pread_off)
1979 {
1981 "Handling plaintext size of tca greater than pread offset.\n")
1982 ;
1983 return 0;
1984 }
1985 if (ntohs (hdr->size) != sizeof(*tca))
1986 {
1988 "Handling plaintext size does not match message type.\n");
1989 GNUNET_break_op (0);
1991 return 0;
1992 }
1993
1994 thas.purpose.purpose = htonl (
1996 thas.purpose.size = htonl (sizeof(thas));
1997 thas.sender = tca->sender;
1998 thas.receiver = my_identity;
1999 thas.monotonic_time = tca->monotonic_time;
2000 thas.challenge = tca->challenge;
2001
2004 &thas,
2005 &tca->sender_sig,
2006 &tca->sender.public_key))
2007 {
2009 "Verification of signature failed!\n");
2010 GNUNET_break (0);
2012 return 0;
2013 }
2014 if (0 != GNUNET_memcmp (&tca->challenge, &challenge))
2015 {
2017 "Challenge in TCPConfirmationAck not correct!\n");
2018 GNUNET_break (0);
2020 return 0;
2021 }
2022
2023 queue->handshake_ack_monotime_get = GNUNET_PEERSTORE_iteration_start (
2024 peerstore,
2025 "transport_tcp_communicator",
2026 &queue->target,
2029 queue);
2030
2032 "Handling plaintext, ack processed!\n");
2033
2035 {
2036 send_challenge (queue->challenge_received, queue);
2037 queue->write_task =
2039 queue->sock,
2040 &queue_write,
2041 queue);
2042 }
2043 else if (GNUNET_TRANSPORT_CS_OUTBOUND == queue->cs)
2044 {
2046 queue->address->sa_family, NULL);
2047 }
2048
2053 queue->initial_core_kx_done = GNUNET_YES;
2054
2055 {
2056 char *foreign_addr;
2057
2058 switch (queue->address->sa_family)
2059 {
2060 case AF_INET:
2061 GNUNET_asprintf (&foreign_addr,
2062 "%s-%s",
2064 GNUNET_a2s (queue->address, queue->address_len));
2065 break;
2066
2067 case AF_INET6:
2068 GNUNET_asprintf (&foreign_addr,
2069 "%s-%s",
2071 GNUNET_a2s (queue->address, queue->address_len));
2072 break;
2073
2074 default:
2075 GNUNET_assert (0);
2076 }
2078 &queue->target,
2079 foreign_addr,
2080 UINT16_MAX, /* no MTU */
2082 0, /* Priority */
2083 queue->nt,
2084 queue->cs,
2085 queue->mq);
2086
2087 GNUNET_free (foreign_addr);
2088 }
2089
2090 size = ntohs (hdr->size);
2091 break;
2093 /* Special case: header size excludes box itself! */
2094 box = (const struct TCPBox *) queue->pread_buf;
2095 if (ntohs (hdr->size) + sizeof(struct TCPBox) > queue->pread_off)
2096 return 0;
2097 calculate_hmac (&queue->in_hmac, &box[1], ntohs (hdr->size), &tmac);
2098 if (0 != memcmp (&tmac, &box->hmac, sizeof(tmac)))
2099 {
2100 GNUNET_break_op (0);
2102 return 0;
2103 }
2104 pass_plaintext_to_core (queue, (const void *) &box[1], ntohs (hdr->size));
2105 size = ntohs (hdr->size) + sizeof(*box);
2107 "Handling plaintext, box processed!\n");
2109 "# bytes decrypted with BOX",
2110 size,
2111 GNUNET_NO);
2113 "# messages decrypted with BOX",
2114 1,
2115 GNUNET_NO);
2116 break;
2117
2119 rekey = (const struct TCPRekey *) queue->pread_buf;
2120 if (sizeof(*rekey) > queue->pread_off)
2121 return 0;
2122 if (ntohs (hdr->size) != sizeof(*rekey))
2123 {
2124 GNUNET_break_op (0);
2126 return 0;
2127 }
2128 rekeyz = *rekey;
2129 memset (&rekeyz.hmac, 0, sizeof(rekeyz.hmac));
2130 calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
2131 if (0 != memcmp (&tmac, &rekey->hmac, sizeof(tmac)))
2132 {
2133 GNUNET_break_op (0);
2135 return 0;
2136 }
2137 do_rekey (queue, rekey);
2138 size = ntohs (hdr->size);
2140 "Handling plaintext, rekey processed!\n");
2142 "# rekeying successful",
2143 1,
2144 GNUNET_NO);
2145 break;
2146
2148 fin = (const struct TCPFinish *) queue->pread_buf;
2149 if (sizeof(*fin) > queue->pread_off)
2150 return 0;
2151 if (ntohs (hdr->size) != sizeof(*fin))
2152 {
2153 GNUNET_break_op (0);
2155 return 0;
2156 }
2157 finz = *fin;
2158 memset (&finz.hmac, 0, sizeof(finz.hmac));
2159 calculate_hmac (&queue->in_hmac, &finz, sizeof(finz), &tmac);
2160 if (0 != memcmp (&tmac, &fin->hmac, sizeof(tmac)))
2161 {
2162 GNUNET_break_op (0);
2164 return 0;
2165 }
2166 /* handle FINISH by destroying queue */
2169 "Handling plaintext, finish processed!\n");
2170 break;
2171
2172 default:
2174 "Handling plaintext, nothing processed!\n");
2175 GNUNET_break_op (0);
2177 return 0;
2178 }
2179 GNUNET_assert (0 != size);
2180 if (-1 != queue->unverified_size)
2181 queue->unverified_size += size;
2182 return size;
2183}
2184
2185
2191static void
2192queue_read (void *cls)
2193{
2194 struct Queue *queue = cls;
2195 struct GNUNET_TIME_Relative left;
2196 ssize_t rcvd;
2197
2198 queue->read_task = NULL;
2199 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2200 &queue->cread_buf[queue->cread_off],
2201 BUF_SIZE - queue->cread_off);
2203 "Received %zd bytes from TCP queue\n", rcvd);
2204 if (-1 == rcvd)
2205 {
2206 if ((EAGAIN != errno) && (EINTR != errno))
2207 {
2210 return;
2211 }
2212 /* try again */
2213 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2214 if (0 != left.rel_value_us)
2215 {
2216 queue->read_task =
2218 return;
2219 }
2221 "Queue %p was idle for %s, disconnecting\n",
2222 queue,
2225 GNUNET_YES));
2227 return;
2228 }
2229 if (0 == rcvd)
2230 {
2231 /* Orderly shutdown of connection */
2233 "Socket for queue %p seems to have been closed\n", queue);
2235 return;
2236 }
2237 queue->timeout =
2239 queue->cread_off += rcvd;
2240 while ((queue->pread_off < sizeof(queue->pread_buf)) &&
2241 (queue->cread_off > 0))
2242 {
2243 size_t max = GNUNET_MIN (sizeof(queue->pread_buf) - queue->pread_off,
2244 queue->cread_off);
2245 size_t done;
2246 size_t total;
2247 size_t old_pread_off = queue->pread_off;
2248
2249 GNUNET_assert (0 ==
2250 gcry_cipher_decrypt (queue->in_cipher,
2251 &queue->pread_buf[queue->pread_off],
2252 max,
2253 queue->cread_buf,
2254 max));
2255 queue->pread_off += max;
2256 total = 0;
2257 while (0 != (done = try_handle_plaintext (queue)))
2258 {
2259 /* 'done' bytes of plaintext were used, shift buffer */
2260 GNUNET_assert (done <= queue->pread_off);
2261 /* NOTE: this memmove() could possibly sometimes be
2262 avoided if we pass 'total' into try_handle_plaintext()
2263 and use it at an offset into the buffer there! */
2264 memmove (queue->pread_buf,
2265 &queue->pread_buf[done],
2266 queue->pread_off - done);
2267 queue->pread_off -= done;
2268 total += done;
2269 /* The last plaintext was a rekey, abort for now */
2270 if (GNUNET_YES == queue->rekeyed)
2271 break;
2272 }
2273 /* when we encounter a rekey message, the decryption above uses the
2274 wrong key for everything after the rekey; in that case, we have
2275 to re-do the decryption at 'total' instead of at 'max'.
2276 However, we have to take into account that the plaintext buffer may have
2277 already contained data and not jumped too far ahead in the ciphertext.
2278 If there is no rekey and the last message is incomplete (max > total),
2279 it is safe to keep the decryption so we shift by 'max' */
2280 if (GNUNET_YES == queue->rekeyed)
2281 {
2282 max = total - old_pread_off;
2283 queue->rekeyed = GNUNET_NO;
2284 queue->pread_off = 0;
2285 }
2286 memmove (queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
2287 queue->cread_off -= max;
2288 }
2289 if (BUF_SIZE == queue->cread_off)
2290 return; /* buffer full, suspend reading */
2291 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2292 if (0 != left.rel_value_us)
2293 {
2294 if (max_queue_length > queue->backpressure)
2295 {
2296 /* continue reading */
2297 queue->read_task =
2299 }
2300 return;
2301 }
2303 "Queue %p was idle for %s, disconnecting\n",
2304 queue,
2307 GNUNET_YES));
2309}
2310
2311
2319static struct sockaddr *
2320tcp_address_to_sockaddr_numeric_v6 (socklen_t *sock_len,
2321 struct sockaddr_in6 v6,
2322 unsigned int port)
2323{
2324 struct sockaddr *in;
2325
2326 v6.sin6_family = AF_INET6;
2327 v6.sin6_port = htons ((uint16_t) port);
2328#if HAVE_SOCKADDR_IN_SIN_LEN
2329 v6.sin6_len = sizeof(struct sockaddr_in6);
2330#endif
2331 v6.sin6_flowinfo = 0;
2332 v6.sin6_scope_id = 0;
2333 in = GNUNET_memdup (&v6, sizeof(v6));
2334 *sock_len = sizeof(struct sockaddr_in6);
2335
2336 return in;
2337}
2338
2339
2347static struct sockaddr *
2348tcp_address_to_sockaddr_numeric_v4 (socklen_t *sock_len,
2349 struct sockaddr_in v4,
2350 unsigned int port)
2351{
2352 struct sockaddr *in;
2353
2354 v4.sin_family = AF_INET;
2355 v4.sin_port = htons ((uint16_t) port);
2356#if HAVE_SOCKADDR_IN_SIN_LEN
2357 v4.sin_len = sizeof(struct sockaddr_in);
2358#endif
2359 in = GNUNET_memdup (&v4, sizeof(v4));
2360 *sock_len = sizeof(struct sockaddr_in);
2361 return in;
2362}
2363
2364
2371static struct PortOnlyIpv4Ipv6 *
2372tcp_address_to_sockaddr_port_only (const char *bindto, unsigned int *port)
2373{
2374 struct PortOnlyIpv4Ipv6 *po;
2375 struct sockaddr_in *i4;
2376 struct sockaddr_in6 *i6;
2377 socklen_t sock_len_ipv4;
2378 socklen_t sock_len_ipv6;
2379
2380 /* interpreting value as just a PORT number */
2381 if (*port > UINT16_MAX)
2382 {
2384 "BINDTO specification `%s' invalid: value too large for port\n",
2385 bindto);
2386 return NULL;
2387 }
2388
2389 po = GNUNET_new (struct PortOnlyIpv4Ipv6);
2390
2391 if (GNUNET_YES == disable_v6)
2392 {
2393 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2394 po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2395 *port);
2396 po->addr_len_ipv4 = sock_len_ipv4;
2397 }
2398 else
2399 {
2400
2401 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2402 po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2403 *port);
2404 po->addr_len_ipv4 = sock_len_ipv4;
2405
2406 i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
2407 po->addr_ipv6 = tcp_address_to_sockaddr_numeric_v6 (&sock_len_ipv6, *i6,
2408 *port);
2409
2410 po->addr_len_ipv6 = sock_len_ipv6;
2411
2412 GNUNET_free (i6);
2413 }
2414
2415 GNUNET_free (i4);
2416
2417 return po;
2418}
2419
2420
2427static char *
2428extract_address (const char *bindto)
2429{
2430 char *addr;
2431 char *start;
2432 char *token;
2433 char *cp;
2434 char *rest = NULL;
2435
2437 "extract address with bindto %s\n",
2438 bindto);
2439
2440 if (NULL == bindto)
2442 "bindto is NULL\n");
2443
2444 cp = GNUNET_strdup (bindto);
2445
2447 "extract address 2\n");
2448
2449 start = cp;
2450 if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
2451 {
2452 start++; /* skip over '['*/
2453 cp[strlen (cp) - 1] = '\0'; /* eat ']'*/
2454 addr = GNUNET_strdup (start);
2455 }
2456 else
2457 {
2458 token = strtok_r (cp, "]", &rest);
2459 if (strlen (bindto) == strlen (token))
2460 {
2461 token = strtok_r (cp, ":", &rest);
2462 addr = GNUNET_strdup (token);
2463 }
2464 else
2465 {
2466 token++;
2467 addr = GNUNET_strdup (token);
2468 }
2469 }
2470
2472 "tcp address: %s\n",
2473 addr);
2474 GNUNET_free (cp);
2475 return addr;
2476}
2477
2478
2485static unsigned int
2486extract_port (const char *addr_and_port)
2487{
2488 unsigned int port;
2489 char dummy[2];
2490 char *token;
2491 char *addr;
2492 char *colon;
2493 char *cp;
2494 char *rest = NULL;
2495
2496 if (NULL != addr_and_port)
2497 {
2498 cp = GNUNET_strdup (addr_and_port);
2499 token = strtok_r (cp, "]", &rest);
2500 if (strlen (addr_and_port) == strlen (token))
2501 {
2502 colon = strrchr (cp, ':');
2503 if (NULL == colon)
2504 {
2505 GNUNET_free (cp);
2506 return 0;
2507 }
2508 addr = colon;
2509 addr++;
2510 }
2511 else
2512 {
2513 token = strtok_r (NULL, "]", &rest);
2514 if (NULL == token)
2515 {
2516 GNUNET_free (cp);
2517 return 0;
2518 }
2519 else
2520 {
2521 addr = token;
2522 addr++;
2523 }
2524 }
2525
2526
2527 if (1 == sscanf (addr, "%u%1s", &port, dummy))
2528 {
2529 /* interpreting value as just a PORT number */
2530 if (port > UINT16_MAX)
2531 {
2533 "Port `%u' invalid: value too large for port\n",
2534 port);
2535 GNUNET_free (cp);
2536 return 0;
2537 }
2538 }
2539 else
2540 {
2542 "BINDTO specification invalid: last ':' not followed by number\n");
2543 GNUNET_free (cp);
2544 return 0;
2545 }
2546 GNUNET_free (cp);
2547 }
2548 else
2549 {
2551 "return 0\n");
2552 /* interpret missing port as 0, aka pick any free one */
2553 port = 0;
2554 }
2555
2556 return port;
2557}
2558
2559
2567static struct sockaddr *
2568tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
2569{
2570 struct sockaddr *in;
2571 unsigned int port;
2572 struct sockaddr_in v4;
2573 struct sockaddr_in6 v6;
2574 char *start;
2575
2576 memset (&v4, 0, sizeof(v4));
2577 start = extract_address (bindto);
2578 GNUNET_assert (NULL != start);
2580 "start %s\n",
2581 start);
2582
2584 "!bindto %s\n",
2585 bindto);
2586
2587
2588 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
2589 {
2590 port = extract_port (bindto);
2591
2593 "port %u\n",
2594 port);
2595
2596 in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port);
2597 }
2598 else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
2599 {
2600 port = extract_port (bindto);
2601 in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port);
2602 }
2603 else
2604 {
2605 GNUNET_assert (0);
2606 }
2607
2609 return in;
2610}
2611
2612
2621static void
2622mq_send (struct GNUNET_MQ_Handle *mq,
2623 const struct GNUNET_MessageHeader *msg,
2624 void *impl_state)
2625{
2626 struct Queue *queue = impl_state;
2627 uint16_t msize = ntohs (msg->size);
2628 struct TCPBox box;
2630 "In MQ send. Queue finishing: %s; write task running: %s\n",
2631 (GNUNET_YES == queue->finishing) ? "yes" : "no",
2632 (NULL == queue->write_task) ? "yes" : "no");
2633 GNUNET_assert (mq == queue->mq);
2634 queue->mq_awaits_continue = GNUNET_YES;
2635 if (GNUNET_YES == queue->finishing)
2636 return; /* this queue is dying, drop msg */
2637 GNUNET_assert (0 == queue->pwrite_off);
2638 box.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX);
2639 box.header.size = htons (msize);
2640 calculate_hmac (&queue->out_hmac, msg, msize, &box.hmac);
2641 memcpy (&queue->pwrite_buf[queue->pwrite_off], &box, sizeof(box));
2642 queue->pwrite_off += sizeof(box);
2643 memcpy (&queue->pwrite_buf[queue->pwrite_off], msg, msize);
2644 queue->pwrite_off += msize;
2646 "%lu bytes of plaintext to send\n", queue->pwrite_off);
2647 GNUNET_assert (NULL != queue->sock);
2648 if (NULL == queue->write_task)
2649 queue->write_task =
2651 queue->sock,
2652 &queue_write,
2653 queue);
2654}
2655
2656
2665static void
2666mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
2667{
2668 struct Queue *queue = impl_state;
2669
2670 if (mq == queue->mq)
2671 {
2672 queue->mq = NULL;
2674 }
2675}
2676
2677
2684static void
2685mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
2686{
2687 struct Queue *queue = impl_state;
2688
2689 GNUNET_assert (0 != queue->pwrite_off);
2690 queue->pwrite_off = 0;
2691}
2692
2693
2703static void
2704mq_error (void *cls, enum GNUNET_MQ_Error error)
2705{
2706 struct Queue *queue = cls;
2707
2709 "MQ error in queue to %s: %d\n",
2710 GNUNET_i2s (&queue->target),
2711 (int) error);
2713}
2714
2715
2723static void
2724boot_queue (struct Queue *queue)
2725{
2726 queue->nt =
2727 GNUNET_NT_scanner_get_type (is, queue->address, queue->address_len);
2729 queue_map,
2730 &queue->key,
2731 queue,
2734 "# queues active",
2736 GNUNET_NO);
2737 queue->timeout =
2740 &mq_destroy,
2741 &mq_cancel,
2742 queue,
2743 NULL,
2744 &mq_error,
2745 queue);
2746}
2747
2748
2759static void
2760transmit_kx (struct Queue *queue,
2761 const struct GNUNET_CRYPTO_HpkeEncapsulation *c)
2762{
2763 struct TcpHandshakeSignature ths;
2764 struct TCPConfirmation tc;
2765
2766 memcpy (queue->cwrite_buf, c, sizeof(*c));
2767 queue->cwrite_off = sizeof(*c);
2768 /* compute 'tc' and append in encrypted format to cwrite_buf */
2769 tc.sender = my_identity;
2770 tc.monotonic_time =
2773 &tc.challenge,
2774 sizeof(tc.challenge));
2775 ths.purpose.purpose = htonl (
2777 ths.purpose.size = htonl (sizeof(ths));
2778 ths.sender = my_identity;
2779 ths.receiver = queue->target;
2780 ths.ephemeral = *c;
2781 ths.monotonic_time = tc.monotonic_time;
2782 ths.challenge = tc.challenge;
2784 &ths,
2785 &tc.sender_sig);
2786 GNUNET_assert (0 ==
2787 gcry_cipher_encrypt (queue->out_cipher,
2788 &queue->cwrite_buf[queue->cwrite_off],
2789 sizeof(tc),
2790 &tc,
2791 sizeof(tc)));
2792 queue->challenge = tc.challenge;
2793 queue->cwrite_off += sizeof(tc);
2794
2796 "handshake written\n");
2797}
2798
2799
2807static void
2809{
2811 struct GNUNET_ShortHashCode k;
2812
2814 &c, &k);
2815 setup_out_cipher (queue, &k);
2816 transmit_kx (queue, &c);
2817}
2818
2819
2825static void
2826handshake_monotime_store_cb (void *cls, int success)
2827{
2828 struct Queue *queue = cls;
2829 if (GNUNET_OK != success)
2830 {
2832 "Failed to store handshake monotonic time in PEERSTORE!\n");
2833 }
2834 queue->handshake_monotime_sc = NULL;
2835 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
2836}
2837
2838
2846static void
2847handshake_monotime_cb (void *cls,
2848 const struct GNUNET_PEERSTORE_Record *record,
2849 const char *emsg)
2850{
2851 struct Queue *queue = cls;
2852 struct GNUNET_TIME_AbsoluteNBO *mtbe;
2853 struct GNUNET_TIME_Absolute mt;
2854 const struct GNUNET_PeerIdentity *pid;
2855 struct GNUNET_TIME_AbsoluteNBO *handshake_monotonic_time;
2856
2857 (void) emsg;
2858
2859 handshake_monotonic_time = &queue->handshake_monotonic_time;
2860 pid = &queue->target;
2862 "tcp handshake with us %s\n",
2864 if (NULL == record)
2865 {
2866 queue->handshake_monotime_get = NULL;
2867 return;
2868 }
2870 "tcp handshake from peer %s\n",
2871 GNUNET_i2s (pid));
2872 if (sizeof(*mtbe) != record->value_size)
2873 {
2874 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
2875 GNUNET_break (0);
2876 return;
2877 }
2878 mtbe = record->value;
2879 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
2880 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
2881 queue->handshake_monotonic_time).abs_value_us)
2882 {
2884 "Queue from %s dropped, handshake monotime in the past\n",
2885 GNUNET_i2s (&queue->target));
2886 GNUNET_break (0);
2887 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
2888 queue->handshake_ack_monotime_get = NULL;
2890 return;
2891 }
2892 queue->handshake_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
2893 "transport_tcp_communicator",
2894 pid,
2896 handshake_monotonic_time,
2897 sizeof(*
2898 handshake_monotonic_time),
2901 &
2903 queue);
2904}
2905
2906
2918static int
2920 struct TCPConfirmation *tc,
2921 char *ibuf)
2922{
2923 struct TcpHandshakeSignature ths;
2925
2927 0 ==
2928 gcry_cipher_decrypt (queue->in_cipher,
2929 tc,
2930 sizeof(*tc),
2931 &ibuf[sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)],
2932 sizeof(*tc)));
2933 ths.purpose.purpose = htonl (
2935 ths.purpose.size = htonl (sizeof(ths));
2936 ths.sender = tc->sender;
2937 ths.receiver = my_identity;
2938 memcpy (&ths.ephemeral, ibuf, sizeof(struct GNUNET_CRYPTO_EcdhePublicKey));
2939 ths.monotonic_time = tc->monotonic_time;
2940 ths.challenge = tc->challenge;
2943 &ths,
2944 &tc->sender_sig,
2945 &tc->sender.public_key);
2946 if (GNUNET_YES == ret)
2947 queue->handshake_monotime_get =
2949 "transport_tcp_communicator",
2950 &queue->target,
2953 queue);
2954 return ret;
2955}
2956
2957
2965static void
2966queue_read_kx (void *cls)
2967{
2968 struct Queue *queue = cls;
2969 ssize_t rcvd;
2970 struct GNUNET_TIME_Relative left;
2971 struct TCPConfirmation tc;
2972
2973 queue->read_task = NULL;
2974 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2975 if (0 == left.rel_value_us)
2976 {
2978 return;
2979 }
2980 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2981 &queue->cread_buf[queue->cread_off],
2982 BUF_SIZE - queue->cread_off);
2984 "Received %lu bytes to write in buffer of size %lu for KX from queue %p (expires in %"
2985 PRIu64 ")\n",
2986 rcvd, BUF_SIZE - queue->cread_off, queue, left.rel_value_us);
2987 if (-1 == rcvd)
2988 {
2989 if ((EAGAIN != errno) && (EINTR != errno))
2990 {
2993 return;
2994 }
2995 queue->read_task =
2997 return;
2998 }
2999 if (0 == rcvd)
3000 {
3001 /* Orderly shutdown of connection */
3003 "Socket for queue %p seems to have been closed\n", queue);
3005 return;
3006 }
3007 queue->cread_off += rcvd;
3008 if (queue->cread_off < INITIAL_KX_SIZE)
3009 {
3010 /* read more */
3012 "%lu/%lu bytes of KX read. Rescheduling...\n",
3013 queue->cread_off, INITIAL_KX_SIZE);
3014 queue->read_task =
3016 return;
3017 }
3018 /* we got all the data, let's find out who we are talking to! */
3020 (const struct GNUNET_CRYPTO_HpkeEncapsulation*)
3021 queue->cread_buf,
3022 queue);
3023 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, queue->cread_buf))
3024 {
3026 "Invalid TCP KX received from %s\n",
3027 GNUNET_a2s (queue->address, queue->address_len));
3029 return;
3030 }
3031 if (0 !=
3032 memcmp (&tc.sender, &queue->target, sizeof(struct GNUNET_PeerIdentity)))
3033 {
3035 "Invalid sender in TCP KX received from %s\n",
3036 GNUNET_a2s (queue->address, queue->address_len));
3038 return;
3039 }
3040 send_challenge (tc.challenge, queue);
3041 queue->write_task =
3043 queue->sock,
3044 &queue_write,
3045 queue);
3046
3047 /* update queue timeout */
3048 queue->timeout =
3050 /* prepare to continue with regular read task immediately */
3051 memmove (queue->cread_buf,
3052 &queue->cread_buf[INITIAL_KX_SIZE],
3053 queue->cread_off - (INITIAL_KX_SIZE));
3055 "cread_off is %lu bytes before adjusting\n",
3056 queue->cread_off);
3057 queue->cread_off -= INITIAL_KX_SIZE;
3059 "cread_off set to %lu bytes\n",
3060 queue->cread_off);
3062}
3063
3064
3071static void
3072proto_read_kx (void *cls)
3073{
3074 struct ProtoQueue *pq = cls;
3075 ssize_t rcvd;
3076 struct GNUNET_TIME_Relative left;
3077 struct Queue *queue;
3078 struct TCPConfirmation tc;
3080
3081 pq->read_task = NULL;
3083 if (0 == left.rel_value_us)
3084 {
3085 free_proto_queue (pq);
3086 return;
3087 }
3088 rcvd = GNUNET_NETWORK_socket_recv (pq->sock,
3089 &pq->ibuf[pq->ibuf_off],
3090 sizeof(pq->ibuf) - pq->ibuf_off);
3092 "Proto received %lu bytes for KX\n", rcvd);
3093 if (-1 == rcvd)
3094 {
3095 if ((EAGAIN != errno) && (EINTR != errno))
3096 {
3098 free_proto_queue (pq);
3099 return;
3100 }
3101 /* try again */
3102 pq->read_task =
3104 return;
3105 }
3106 if (0 == rcvd)
3107 {
3108 /* Orderly shutdown of connection */
3110 "Socket for proto queue %p seems to have been closed\n", pq);
3111 free_proto_queue (pq);
3112 return;
3113 }
3114 pq->ibuf_off += rcvd;
3115 if (sizeof (struct TCPNATProbeMessage) == pq->ibuf_off)
3116 {
3117 struct TCPNATProbeMessage *pm = (struct TCPNATProbeMessage *) pq->ibuf;
3118
3120 &pm->clientIdentity);
3121
3122 queue = GNUNET_new (struct Queue);
3123 queue->target = pm->clientIdentity;
3124 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
3127 }
3128 else if (pq->ibuf_off > sizeof(pq->ibuf))
3129 {
3130 /* read more */
3131 pq->read_task =
3133 return;
3134 }
3135 else
3136 {
3137 /* we got all the data, let's find out who we are talking to! */
3138 queue = GNUNET_new (struct Queue);
3140 (const struct GNUNET_CRYPTO_HpkeEncapsulation *) pq->
3141 ibuf,
3142 queue);
3143 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, pq->ibuf))
3144 {
3146 "Invalid TCP KX received from %s\n",
3147 GNUNET_a2s (pq->address, pq->address_len));
3148 gcry_cipher_close (queue->in_cipher);
3150 free_proto_queue (pq);
3151 return;
3152 }
3153 queue->target = tc.sender;
3154 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
3157 }
3158 queue->address = pq->address; /* steals reference */
3159 queue->address_len = pq->address_len;
3160 queue->listen_sock = pq->listen_sock;
3161 queue->sock = pq->sock;
3162
3164 "created queue with target %s\n",
3165 GNUNET_i2s (&queue->target));
3166
3168 "start kx proto\n");
3169
3171 boot_queue (queue);
3172 queue->read_task =
3174 queue->sock,
3175 read_task,
3176 queue);
3177 queue->write_task =
3179 queue->sock,
3180 &queue_write,
3181 queue);
3182 // TODO To early! Move it somewhere else.
3183 // send_challenge (tc.challenge, queue);
3184 queue->challenge_received = tc.challenge;
3185
3187 GNUNET_free (pq);
3188}
3189
3190
3191static struct ProtoQueue *
3193 struct sockaddr *in,
3194 socklen_t addrlen)
3195{
3196 struct ProtoQueue *pq = GNUNET_new (struct ProtoQueue);
3197
3198 if (NULL == sock)
3199 {
3200 // sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
3201 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, 0);
3202 if (NULL == sock)
3203 {
3205 "socket(%d) failed: %s",
3206 in->sa_family,
3207 strerror (errno));
3208 GNUNET_free (in);
3209 GNUNET_free (pq);
3210 return NULL;
3211 }
3212 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, addrlen)) &&
3213 (errno != EINPROGRESS))
3214 {
3216 "connect to `%s' failed: %s",
3217 GNUNET_a2s (in, addrlen),
3218 strerror (errno));
3220 GNUNET_free (in);
3221 GNUNET_free (pq);
3222 return NULL;
3223 }
3224 }
3225 pq->address_len = addrlen;
3226 pq->address = in;
3228 pq->sock = sock;
3230 pq->sock,
3232 pq);
3234
3235 return pq;
3236}
3237
3238
3246static void
3247listen_cb (void *cls)
3248{
3249 struct sockaddr_storage in;
3250 socklen_t addrlen;
3251 struct GNUNET_NETWORK_Handle *sock;
3252 struct ListenTask *lt;
3253 struct sockaddr *in_addr;
3254
3256 "listen_cb\n");
3257
3258 lt = cls;
3259
3260 lt->listen_task = NULL;
3261 GNUNET_assert (NULL != lt->listen_sock);
3262 addrlen = sizeof(in);
3263 memset (&in, 0, sizeof(in));
3265 (struct sockaddr*) &in,
3266 &addrlen);
3267 if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno)))
3268 return; /* system limit reached, wait until connection goes down */
3270 lt->listen_sock,
3271 &listen_cb,
3272 lt);
3273 if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno)))
3274 return;
3275 if (NULL == sock)
3276 {
3278 return;
3279 }
3280 in_addr = GNUNET_memdup (&in, addrlen);
3281 create_proto_queue (sock, in_addr, addrlen);
3282}
3283
3284
3285static void
3286try_connection_reversal (void *cls,
3287 const struct sockaddr *addr,
3288 socklen_t addrlen)
3289{
3290 struct TCPNATProbeMessage pm;
3291 struct ProtoQueue *pq;
3292 struct sockaddr *in_addr;
3293 (void) cls;
3294
3296 "addr->sa_family %d\n",
3297 addr->sa_family);
3299 "Try to connect back\n");
3300 in_addr = GNUNET_memdup (addr, addrlen);
3302 "in_addr->sa_family %d\n",
3303 in_addr->sa_family);
3304 pq = create_proto_queue (NULL, in_addr, addrlen);
3305 if (NULL != pq)
3306 {
3307 pm.header.size = htons (sizeof(struct TCPNATProbeMessage));
3308 pm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
3309 pm.clientIdentity = my_identity;
3310 memcpy (pq->write_buf, &pm, sizeof(struct TCPNATProbeMessage));
3311 pq->write_off = sizeof(struct TCPNATProbeMessage);
3313 pq->sock,
3315 pq);
3316 }
3317 else
3318 {
3320 "Couldn't create ProtoQueue for sending TCPNATProbeMessage\n");
3321 }
3322}
3323
3324
3325static void
3326pending_reversal_timeout (void *cls)
3327{
3328 struct sockaddr *in = cls;
3329 struct PendingReversal *pending_reversal;
3330 struct GNUNET_HashCode key;
3331
3333 sizeof(struct sockaddr),
3334 &key);
3336 &key);
3337
3338 GNUNET_assert (NULL != pending_reversal);
3339
3341 &key,
3342 pending_reversal))
3344 "No pending reversal found for address %s\n",
3345 GNUNET_a2s (in, sizeof (struct sockaddr)));
3346 GNUNET_free (pending_reversal->in);
3347 GNUNET_free (pending_reversal);
3348}
3349
3350
3369static int
3370mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
3371{
3372 struct sockaddr *in;
3373 socklen_t in_len = 0;
3374 const char *path;
3375 struct sockaddr_in *v4;
3376 struct sockaddr_in6 *v6;
3377 unsigned int is_natd = GNUNET_NO;
3378 struct GNUNET_HashCode key;
3379 struct GNUNET_HashCode queue_map_key;
3380 struct GNUNET_HashContext *hsh;
3381 struct Queue *queue;
3382
3384 "Connecting to %s at %s\n",
3385 GNUNET_i2s (peer),
3386 address);
3387 if (0 != strncmp (address,
3389 strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
3390 {
3391 GNUNET_break_op (0);
3392 return GNUNET_SYSERR;
3393 }
3394 path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
3395 in = tcp_address_to_sockaddr (path, &in_len);
3396
3397 if (NULL == in)
3398 {
3400 "Failed to setup TCP socket address\n");
3401 return GNUNET_SYSERR;
3402 }
3403
3405 "in %s\n",
3406 GNUNET_a2s (in, in_len));
3407
3410 GNUNET_CRYPTO_hash_context_read (hsh, peer, sizeof (*peer));
3411 GNUNET_CRYPTO_hash_context_finish (hsh, &queue_map_key);
3413
3414 if (NULL != queue)
3415 {
3417 "Queue for %s already exists or is in construction\n", address);
3418 GNUNET_free (in);
3419 return GNUNET_NO;
3420 }
3421 switch (in->sa_family)
3422 {
3423 case AF_INET:
3424 v4 = (struct sockaddr_in *) in;
3425 if (0 == v4->sin_port)
3426 {
3427 is_natd = GNUNET_YES;
3429 sizeof(struct sockaddr),
3430 &key);
3433 &key))
3434 {
3436 "There is already a request reversal for `%s'at `%s'\n",
3437 GNUNET_i2s (peer),
3438 address);
3439 GNUNET_free (in);
3440 return GNUNET_SYSERR;
3441 }
3442 }
3443 break;
3444
3445 case AF_INET6:
3446 if (GNUNET_YES == disable_v6)
3447 {
3449 "IPv6 disabled, skipping %s\n", address);
3450 GNUNET_free (in);
3451 return GNUNET_SYSERR;
3452 }
3453 v6 = (struct sockaddr_in6 *) in;
3454 if (0 == v6->sin6_port)
3455 {
3457 "Request reversal for `%s' at `%s' not possible for an IPv6 address\n",
3458 GNUNET_i2s (peer),
3459 address);
3460 GNUNET_free (in);
3461 return GNUNET_SYSERR;
3462 }
3463 break;
3464
3465 default:
3466 GNUNET_assert (0);
3467 }
3468
3469 if (GNUNET_YES == is_natd)
3470 {
3471 struct sockaddr_in local_sa;
3472 struct PendingReversal *pending_reversal;
3473
3474 memset (&local_sa, 0, sizeof(local_sa));
3475 local_sa.sin_family = AF_INET;
3476 local_sa.sin_port = htons (bind_port);
3477 /* We leave sin_address at 0, let the kernel figure it out,
3478 even if our bind() is more specific. (May want to reconsider
3479 later.) */
3480 if (GNUNET_OK != GNUNET_NAT_request_reversal (nat, &local_sa, v4))
3481 {
3483 "request reversal for `%s' at `%s' failed\n",
3484 GNUNET_i2s (peer),
3485 address);
3486 GNUNET_free (in);
3487 return GNUNET_SYSERR;
3488 }
3489 pending_reversal = GNUNET_new (struct PendingReversal);
3490 pending_reversal->in = in;
3493 &key,
3494 pending_reversal,
3496 pending_reversal->target = *peer;
3498 &
3500 in);
3502 "Created NAT WAIT connection to `%s' at `%s'\n",
3503 GNUNET_i2s (peer),
3504 GNUNET_a2s (in, sizeof (struct sockaddr)));
3505 }
3506 else
3507 {
3508 struct GNUNET_NETWORK_Handle *sock;
3509
3510 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM,
3511 IPPROTO_TCP);
3512 if (NULL == sock)
3513 {
3515 "socket(%d) failed: %s",
3516 in->sa_family,
3517 strerror (errno));
3518 GNUNET_free (in);
3519 return GNUNET_SYSERR;
3520 }
3521 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, in_len)) &&
3522 (errno != EINPROGRESS))
3523 {
3525 "connect to `%s' failed: %s",
3526 address,
3527 strerror (errno));
3529 GNUNET_free (in);
3530 return GNUNET_SYSERR;
3531 }
3532
3533 queue = GNUNET_new (struct Queue);
3534 queue->target = *peer;
3535 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
3536 queue->key = queue_map_key;
3537 queue->address = in;
3538 queue->address_len = in_len;
3539 queue->sock = sock;
3541 boot_queue (queue);
3543 "booted queue with target %s\n",
3544 GNUNET_i2s (&queue->target));
3545 // queue->mq_awaits_continue = GNUNET_YES;
3546 queue->read_task =
3548 queue->sock,
3550 queue);
3551
3552
3554 "start kx mq_init\n");
3555
3557 queue->write_task =
3559 queue->sock,
3560 &queue_write,
3561 queue);
3562 }
3563
3564 return GNUNET_OK;
3565}
3566
3567
3576static int
3577get_lt_delete_it (void *cls,
3578 const struct GNUNET_HashCode *key,
3579 void *value)
3580{
3581 struct ListenTask *lt = value;
3582
3583 (void) cls;
3584 (void) key;
3585 if (NULL != lt->listen_task)
3586 {
3588 lt->listen_task = NULL;
3589 }
3590 if (NULL != lt->listen_sock)
3591 {
3593 lt->listen_sock = NULL;
3594 }
3595 GNUNET_free (lt);
3596 return GNUNET_OK;
3597}
3598
3599
3608static int
3609get_queue_delete_it (void *cls,
3610 const struct GNUNET_HashCode *target,
3611 void *value)
3612{
3613 struct Queue *queue = value;
3614
3615 (void) cls;
3616 (void) target;
3618 return GNUNET_OK;
3619}
3620
3621
3627static void
3628do_shutdown (void *cls)
3629{
3631 "Shutdown %s!\n",
3632 shutdown_running ? "running" : "not running");
3633
3635 return;
3636 else
3638
3639 while (NULL != proto_head)
3641 if (NULL != nat)
3642 {
3644 nat = NULL;
3645 }
3653 if (NULL != ch)
3654 {
3657 ch = NULL;
3658 }
3659 if (NULL != stats)
3660 {
3662 stats = NULL;
3663 }
3664 if (NULL != my_private_key)
3665 {
3667 my_private_key = NULL;
3668 }
3669 if (NULL != is)
3670 {
3672 is = NULL;
3673 }
3674 if (NULL != pils)
3675 {
3677 pils = NULL;
3678 }
3679 if (NULL != peerstore)
3680 {
3682 peerstore = NULL;
3683 }
3684 if (NULL != resolve_request_handle)
3685 {
3688 }
3690 "Shutdown done!\n");
3691}
3692
3693
3705static void
3706enc_notify_cb (void *cls,
3707 const struct GNUNET_PeerIdentity *sender,
3708 const struct GNUNET_MessageHeader *msg)
3709{
3710 (void) cls;
3711 (void) sender;
3712 (void) msg;
3713 GNUNET_break_op (0);
3714}
3715
3716
3730static void
3731nat_address_cb (void *cls,
3732 void **app_ctx,
3733 int add_remove,
3735 const struct sockaddr *addr,
3736 socklen_t addrlen)
3737{
3738 char *my_addr;
3740
3742 "nat address cb %s %s\n",
3743 add_remove ? "add" : "remove",
3744 GNUNET_a2s (addr, addrlen));
3745
3746 if (GNUNET_YES == add_remove)
3747 {
3749
3750 GNUNET_asprintf (&my_addr,
3751 "%s-%s",
3753 GNUNET_a2s (addr, addrlen));
3754 nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
3755 ai =
3757 my_addr,
3758 nt,
3760 GNUNET_free (my_addr);
3761 *app_ctx = ai;
3762 }
3763 else
3764 {
3765 ai = *app_ctx;
3767 *app_ctx = NULL;
3768 }
3769}
3770
3771
3775static void
3776add_addr (struct sockaddr *in, socklen_t in_len)
3777{
3778
3779 struct Addresses *saddrs;
3780
3782 "add address %s\n",
3783 GNUNET_a2s (in, in_len));
3784
3785 saddrs = GNUNET_new (struct Addresses);
3786 saddrs->addr = in;
3787 saddrs->addr_len = in_len;
3789
3791 "after add address %s\n",
3792 GNUNET_a2s (in, in_len));
3793
3795 "add address %s\n",
3796 GNUNET_a2s (saddrs->addr, saddrs->addr_len));
3797
3798 addrs_lens++;
3799}
3800
3801
3815static enum GNUNET_GenericReturnValue
3816load_ikm ()
3817{
3818 char *filename;
3819 struct GNUNET_DISK_FileHandle *filehandle;
3820 int ret;
3821
3822 if (GNUNET_OK !=
3824 "pils",
3825 "SECRET_KEY_FILE",
3826 &filename))
3827 {
3829 "PILS service is lacking initial secret key file configuration setting. Exiting\n");
3830 return GNUNET_SYSERR;
3831 }
3832 if (NULL == filename)
3833 return GNUNET_SYSERR;
3835 if (GNUNET_SYSERR == ret)
3836 return GNUNET_SYSERR;
3837 if (GNUNET_NO == ret)
3838 {
3839 /* File does not exist - generate a new initial secret key and save it */
3840 // TODO consider the case that the file exists and ist not readable
3842 ikm,
3843 sizeof ikm);
3845 {
3847 "PILS service cannot create dir for saving initial secret key file. Exiting\n");
3848 return GNUNET_SYSERR;
3849 }
3850 filehandle = GNUNET_DISK_file_open (filename,
3854 | // would
3855 // the
3856 // group
3857 // need
3858 // read
3859 // perm?
3861 if (NULL == filehandle)
3862 {
3864 "PILS service had an issue with opening the initial secret key file. Exiting\n");
3865 GNUNET_DISK_file_close (filehandle);
3866 return GNUNET_SYSERR;
3867 }
3868 ret = GNUNET_DISK_file_write (filehandle,
3869 ikm,
3870 sizeof ikm);
3871 GNUNET_DISK_file_close (filehandle);
3872 if (sizeof ikm != ret)
3873 {
3875 "PILS service had an issue with writing the initial secret key to file. Exiting\n")
3876 ;
3877 return GNUNET_SYSERR;
3878 }
3879 }
3880 else
3881 {
3882 /* File existes - just read from it */
3883 off_t size;
3885 "PILS is going to read initial secret key from file %s\n",
3886 filename);
3887 filehandle = GNUNET_DISK_file_open (filename,
3890 if (NULL == filehandle)
3891 {
3893 " Not able to open file\n");
3894 return GNUNET_SYSERR;
3895 }
3896 if (GNUNET_OK != GNUNET_DISK_file_handle_size (filehandle, &size))
3897 {
3899 " File has the wrong size %lu\n",
3900 size);
3901 GNUNET_DISK_file_close (filehandle);
3902 return GNUNET_SYSERR;
3903 }
3904 if (sizeof ikm != size)
3905 {
3907 " Something is wrong with the file size, expected: %lu size, got: %lu\n",
3908 size,
3909 sizeof ikm);
3910 GNUNET_DISK_file_close (filehandle);
3911 return GNUNET_SYSERR;
3912 }
3913 ret = GNUNET_DISK_file_read (filehandle,
3914 ikm,
3915 sizeof ikm);
3916 GNUNET_DISK_file_close (filehandle);
3917 if (sizeof ikm != ret)
3918 {
3920 " Read initial secret key with wrong size %u, expected %lu\n", ret,
3921 sizeof ikm);
3922 return GNUNET_SYSERR;
3923 }
3924
3925 }
3926 return GNUNET_OK;
3927}
3928
3929
3937static int
3938init_socket (struct sockaddr *addr,
3939 socklen_t in_len)
3940{
3941 struct sockaddr_storage in_sto;
3942 socklen_t sto_len;
3943 struct GNUNET_NETWORK_Handle *listen_sock;
3944 struct ListenTask *lt;
3945 int sockfd;
3946 struct GNUNET_HashCode h_sock;
3947
3948 if (NULL == addr)
3949 {
3951 "Address is NULL.\n");
3952 return GNUNET_SYSERR;
3953 }
3954
3956 "address %s\n",
3957 GNUNET_a2s (addr, in_len));
3958
3959 listen_sock =
3960 GNUNET_NETWORK_socket_create (addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
3961 if (NULL == listen_sock)
3962 {
3964 return GNUNET_SYSERR;
3965 }
3966
3967 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, addr, in_len))
3968 {
3970 GNUNET_NETWORK_socket_close (listen_sock);
3971 listen_sock = NULL;
3972 return GNUNET_SYSERR;
3973 }
3974
3975 if (GNUNET_OK !=
3976 GNUNET_NETWORK_socket_listen (listen_sock,
3977 5))
3978 {
3980 "listen");
3981 GNUNET_NETWORK_socket_close (listen_sock);
3982 listen_sock = NULL;
3983 return GNUNET_SYSERR;
3984 }
3985
3986 /* We might have bound to port 0, allowing the OS to figure it out;
3987 thus, get the real IN-address from the socket */
3988 sto_len = sizeof(in_sto);
3989
3990 if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock),
3991 (struct sockaddr *) &in_sto,
3992 &sto_len))
3993 {
3994 memcpy (&in_sto, addr, in_len);
3995 sto_len = in_len;
3996 }
3997
3998 // addr = (struct sockaddr *) &in_sto;
3999 in_len = sto_len;
4001 "Bound to `%s'\n",
4002 GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
4003 if (NULL == stats)
4004 stats = GNUNET_STATISTICS_create ("communicator-tcp", cfg);
4005
4006 if (NULL == is)
4008
4009 /* start listening */
4010
4011 lt = GNUNET_new (struct ListenTask);
4012 lt->listen_sock = listen_sock;
4013
4015 listen_sock,
4016 &listen_cb,
4017 lt);
4018
4020 "creating hash\n");
4021 sockfd = GNUNET_NETWORK_get_fd (lt->listen_sock);
4022 GNUNET_CRYPTO_hash (&sockfd,
4023 sizeof(int),
4024 &h_sock);
4025
4027 "creating map\n");
4028 if (NULL == lt_map)
4030
4032 "creating map entry\n");
4035 &h_sock,
4036 lt,
4038
4040 "map entry created\n");
4041
4042 if (NULL == queue_map)
4044
4045 if (NULL == ch)
4050 &mq_init,
4051 NULL,
4053 NULL,
4054 NULL);
4055
4056 if (NULL == ch)
4057 {
4058 GNUNET_break (0);
4059 if (NULL != resolve_request_handle)
4062 return GNUNET_SYSERR;
4063 }
4064
4065 add_addr (addr, in_len);
4066 return GNUNET_OK;
4067
4068}
4069
4070
4074static void
4075nat_register ()
4076{
4077 struct sockaddr **saddrs;
4078 socklen_t *saddr_lens;
4079 int i;
4080 size_t len;
4081
4083 "starting nat register!\n");
4084 len = 0;
4085 i = 0;
4086 saddrs = GNUNET_malloc ((addrs_lens) * sizeof(struct sockaddr *));
4087 saddr_lens = GNUNET_malloc ((addrs_lens) * sizeof(socklen_t));
4088 for (struct Addresses *pos = addrs_head; NULL != pos; pos = pos->next)
4089 {
4091 "registering address %s\n",
4092 GNUNET_a2s (pos->addr, pos->addr_len));
4093
4094 saddr_lens[i] = pos->addr_len;
4095 len += saddr_lens[i];
4096 saddrs[i] = GNUNET_memdup (pos->addr, saddr_lens[i]);
4097 i++;
4098 }
4099
4101 "registering addresses %lu %lu %lu %lu\n",
4102 (addrs_lens) * sizeof(struct sockaddr *),
4103 (addrs_lens) * sizeof(socklen_t),
4104 len,
4108 IPPROTO_TCP,
4109 addrs_lens,
4110 (const struct sockaddr **) saddrs,
4111 saddr_lens,
4114 NULL /* closure */);
4115 for (i = addrs_lens - 1; i >= 0; i--)
4116 GNUNET_free (saddrs[i]);
4117 GNUNET_free (saddrs);
4118 GNUNET_free (saddr_lens);
4119
4120 if (NULL == nat)
4121 {
4122 GNUNET_break (0);
4123 if (NULL != resolve_request_handle)
4126 }
4127}
4128
4129
4137static void
4138init_socket_resolv (void *cls,
4139 const struct sockaddr *addr,
4140 socklen_t in_len)
4141{
4142 struct sockaddr_in *v4;
4143 struct sockaddr_in6 *v6;
4144 struct sockaddr *in;
4145
4146 (void) cls;
4147 if (NULL != addr)
4148 {
4149 if (AF_INET == addr->sa_family)
4150 {
4151 v4 = (struct sockaddr_in *) addr;
4152 in = tcp_address_to_sockaddr_numeric_v4 (&in_len, *v4, bind_port);// _global);
4153 }
4154 else if (AF_INET6 == addr->sa_family)
4155 {
4156 v6 = (struct sockaddr_in6 *) addr;
4157 in = tcp_address_to_sockaddr_numeric_v6 (&in_len, *v6, bind_port);// _global);
4158 }
4159 else
4160 {
4162 "Address family %u not suitable (not AF_INET %u nor AF_INET6 %u \n",
4163 addr->sa_family,
4164 AF_INET,
4165 AF_INET6);
4166 return;
4167 }
4168 init_socket (in, in_len);
4169 }
4170 else
4171 {
4173 "Address is NULL. This might be an error or the resolver finished resolving.\n");
4174 if (NULL == addrs_head)
4175 {
4177 "Resolver finished resolving, but we do not listen to an address!.\n");
4178 return;
4179 }
4180 nat_register ();
4181 }
4182}
4183
4184
4185void
4186pid_change_cb (void *cls,
4187 const struct GNUNET_HELLO_Parser *parser,
4188 const struct GNUNET_HashCode *addr_hash)
4189{
4191 "Got PID to derive from `%s':\n",
4192 GNUNET_h2s (addr_hash));
4193 if (NULL == my_private_key)
4195
4197 (uint8_t*) ikm,
4198 addr_hash,
4204}
4205
4206
4215static void
4216run (void *cls,
4217 char *const *args,
4218 const char *cfgfile,
4219 const struct GNUNET_CONFIGURATION_Handle *c)
4220{
4221 char *bindto;
4222 struct sockaddr *in;
4223 socklen_t in_len;
4224 struct sockaddr_in v4;
4225 struct sockaddr_in6 v6;
4226 char *start;
4227 unsigned int port;
4228 char dummy[2];
4229 char *rest = NULL;
4230 struct PortOnlyIpv4Ipv6 *po;
4231 socklen_t addr_len_ipv4;
4232 socklen_t addr_len_ipv6;
4233
4234 (void) cls;
4235
4237 memset (&v4,0,sizeof(struct sockaddr_in));
4238 memset (&v6,0,sizeof(struct sockaddr_in6));
4239 cfg = c;
4240 if (GNUNET_OK !=
4243 "BINDTO",
4244 &bindto))
4245 {
4248 "BINDTO");
4249 return;
4250 }
4251 if (GNUNET_OK !=
4254 "MAX_QUEUE_LENGTH",
4256 {
4258 }
4259 if (GNUNET_OK !=
4262 "REKEY_INTERVAL",
4264 {
4266 }
4267 if (GNUNET_OK !=
4270 "REKEY_MAX_BYTES",
4272 {
4274 }
4276 if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
4277 (GNUNET_YES ==
4280 "DISABLE_V6")))
4281 {
4283 }
4284 load_ikm ();
4286 GNUNET_assert (NULL != pils);
4288 if (NULL == peerstore)
4289 {
4290 GNUNET_free (bindto);
4291 GNUNET_break (0);
4293 return;
4294 }
4295
4297
4298 if (1 == sscanf (bindto, "%u%1s", &bind_port, dummy))
4299 {
4303 "address po %s\n",
4305 if (NULL != po->addr_ipv4)
4306 {
4308 }
4309 if (NULL != po->addr_ipv6)
4310 {
4313 }
4314 GNUNET_free (po);
4315 nat_register ();
4316 GNUNET_free (bindto);
4317 return;
4318 }
4319
4320 start = extract_address (bindto);
4321 // FIXME: check for NULL == start...
4322 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
4323 {
4324 bind_port = extract_port (bindto);
4325
4327 init_socket (in, in_len);
4328 nat_register ();
4330 GNUNET_free (bindto);
4331 return;
4332 }
4333
4334 if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
4335 {
4336 bind_port = extract_port (bindto);
4338 init_socket (in, in_len);
4339 nat_register ();
4341 GNUNET_free (bindto);
4342 return;
4343 }
4344
4345 bind_port = extract_port (bindto);
4347 ":",
4348 &rest),
4349 AF_UNSPEC,
4352 &port);
4353
4354 GNUNET_free (bindto);
4356}
4357
4358
4366int
4367main (int argc, char *const *argv)
4368{
4369 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
4371 };
4372 int ret;
4373
4375 "Starting tcp communicator\n");
4376
4377 ret = (GNUNET_OK ==
4379 argc,
4380 argv,
4381 "gnunet-communicator-tcp",
4382 _ ("GNUnet TCP communicator"),
4383 options,
4384 &run,
4385 NULL))
4386 ? 0
4387 : 1;
4388 return ret;
4389}
4390
4391
4392/* end of gnunet-communicator-tcp.c */
struct GNUNET_GETOPT_CommandLineOption options[]
Definition 002.c:5
struct GNUNET_MessageHeader * msg
Definition 005.c:2
int main()
Program to simulate results from GCP_get_desirability_of_path() for various plausible inputs.
static int start
Set if we are to start default services (including ARM).
Definition gnunet-arm.c:38
static int ret
Final status code.
Definition gnunet-arm.c:93
static int do_shutdown
Set to GNUNET_YES if we are shutting down.
static uint16_t port
Port number.
Definition gnunet-bcd.c:146
void pid_change_cb(void *cls, const struct GNUNET_HELLO_Parser *parser, const struct GNUNET_HashCode *addr_hash)
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.
static void queue_finish(struct Queue *queue)
Append a 'finish' message to the outgoing transmission.
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.
static void boot_queue(struct Queue *queue)
Add the given queue to our internal data structure.
static unsigned int bind_port
The port the communicator should be assigned to.
static size_t try_handle_plaintext(struct Queue *queue)
Test if we have received a full message in plaintext.
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 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 GNUNET_PeerIdentity my_identity
Our public key.
#define COMMUNICATOR_ADDRESS_PREFIX
Address prefix used by the communicator.
static unsigned char ikm[256/8]
The initial key material for the peer.
static void mq_destroy(struct GNUNET_MQ_Handle *mq, void *impl_state)
Signature of functions implementing the destruction of a message queue.
#define ADDRESS_VALIDITY_PERIOD
How long do we believe our addresses to remain up (before the other peer should revalidate).
static struct PortOnlyIpv4Ipv6 * tcp_address_to_sockaddr_port_only(const char *bindto, unsigned int *port)
Convert TCP bind specification to a struct PortOnlyIpv4Ipv6 *
static void setup_in_cipher(const struct GNUNET_CRYPTO_HpkeEncapsulation *ephemeral, struct Queue *queue)
Setup cipher of queue for decryption.
#define INITIAL_CORE_KX_SIZE
Size of the initial core key exchange messages.
static void do_rekey(struct Queue *queue, const struct TCPRekey *rekey)
Handle rekey message on queue.
static void eddsa_priv_to_hpke_key(struct GNUNET_CRYPTO_EddsaPrivateKey *edpk, struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
static struct ProtoQueue * create_proto_queue(struct GNUNET_NETWORK_Handle *sock, struct sockaddr *in, socklen_t addrlen)
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 try_connection_reversal(void *cls, const struct sockaddr *addr, socklen_t addrlen)
static struct GNUNET_NT_InterfaceScanner * is
Network scanner to determine network types.
static void listen_cb(void *cls)
We have been notified that our listen socket has something to read.
static void rekey_monotime_cb(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY where...
static int disable_v6
IPv6 disabled.
static struct GNUNET_CONTAINER_MultiHashMap * pending_reversals
Map of pending reversals.
static struct GNUNET_STATISTICS_Handle * stats
For logging statistics.
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 const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
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 void handshake_ack_monotime_store_cb(void *cls, int success)
Callback called when peerstore store operation for handshake ack monotime value is finished.
static struct GNUNET_PEERSTORE_Handle * peerstore
Database for peer's HELLOs.
static int pending_reversals_delete_it(void *cls, const struct GNUNET_HashCode *key, void *value)
static struct ProtoQueue * proto_tail
Protoqueues DLL tail.
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 get_lt_delete_it(void *cls, const struct GNUNET_HashCode *key, void *value)
Iterator over all ListenTasks to clean up.
static void inject_rekey(struct Queue *queue)
Inject a struct TCPRekey message into the queue's plaintext buffer.
static struct GNUNET_TIME_Relative rekey_interval
The rekey interval.
#define NAT_TIMEOUT
How long until we give up on establishing an NAT connection? Must be > 4 RTT.
#define BUF_SIZE
Size of our IO buffers for ciphertext data.
#define COMMUNICATOR_CONFIG_SECTION
Configuration section used by the communicator.
static struct GNUNET_CONTAINER_MultiHashMap * queue_map
Queues (map from peer identity to struct Queue)
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 nat_address_cb(void *cls, void **app_ctx, int add_remove, enum GNUNET_NAT_AddressClass ac, const struct sockaddr *addr, socklen_t addrlen)
Signature of the callback passed to GNUNET_NAT_register() for a function to call whenever our set of ...
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 void check_and_remove_pending_reversal(struct sockaddr *in, sa_family_t sa_family, struct GNUNET_PeerIdentity *sender)
static void init_socket_resolv(void *cls, const struct sockaddr *addr, socklen_t in_len)
This method is the callback called by the resolver API, and wraps method init_socket.
static struct GNUNET_TRANSPORT_CommunicatorHandle * ch
Our environment.
static unsigned long long max_queue_length
Maximum queue length before we stop reading towards the transport service.
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.
struct GNUNET_RESOLVER_RequestHandle * resolve_request_handle
Handle for DNS lookup of bindto address.
static void nat_register()
This method reads from the DLL addrs_head to register them at the NAT service.
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.
static int addrs_lens
Number of addresses in the DLL for register at NAT service.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Setup communicator and launch network interactions.
static struct GNUNET_NAT_Handle * nat
Connection to NAT service.
static void pending_reversal_timeout(void *cls)
#define PROTO_QUEUE_TIMEOUT
How long do we wait until we must have received the initial KX?
static void queue_read(void *cls)
Queue read task.
#define REKEY_MAX_BYTES
How often do we rekey based on number of bytes transmitted? (additionally randomized).
static int get_queue_delete_it(void *cls, const struct GNUNET_HashCode *target, void *value)
Iterator over all message queues to clean up.
static void mq_cancel(struct GNUNET_MQ_Handle *mq, void *impl_state)
Implementation function that cancels the currently sent message.
#define INITIAL_KX_SIZE
Size of the initial key exchange message sent first in both directions.
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 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 proto_queue_write(void *cls)
We have been notified that our socket is ready to write.
static void handshake_monotime_store_cb(void *cls, int success)
Callback called when peerstore store operation for handshake monotime is finished.
static void send_challenge(struct GNUNET_CRYPTO_ChallengeNonceP challenge, struct Queue *queue)
Sending challenge with TcpConfirmationAck back to sender of ephemeral key.
static void rekey_monotime_store_cb(void *cls, int success)
Callback called when peerstore store operation for rekey monotime value is finished.
static char * extract_address(const char *bindto)
This Method extracts the address part of the BINDTO string.
static struct ProtoQueue * proto_head
Protoqueues DLL head.
static int shutdown_running
A flag indicating we are already doing a shutdown.
static enum GNUNET_GenericReturnValue load_ikm()
FIXME: We could alternatively ask PILS for de/encaps, but at a high cost wrt async RPC calls....
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.
#define LOG(kind,...)
#define DEFAULT_REKEY_INTERVAL
How often do we rekey based on time (at least)
static struct GNUNET_CRYPTO_EddsaPrivateKey * my_private_key
Our private key.
static void queue_write(void *cls)
We have been notified that our socket is ready to write.
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.
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...
static struct Addresses * addrs_head
Head of DLL with addresses we like to register at NAT service.
static struct sockaddr * tcp_address_to_sockaddr(const char *bindto, socklen_t *sock_len)
Convert TCP bind specification to a struct sockaddr *
static void eddsa_pub_to_hpke_key(struct GNUNET_CRYPTO_EddsaPublicKey *edpk, struct GNUNET_CRYPTO_EcdhePublicKey *pk)
static struct Addresses * addrs_tail
Head of DLL with addresses we like to register at NAT service.
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 DEFAULT_MAX_QUEUE_LENGTH
How many messages do we keep at most in the queue to the transport service before we start to drop (d...
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 unsigned int extract_port(const char *addr_and_port)
This Method extracts the port part of the BINDTO string.
static struct GNUNET_CRYPTO_EcdhePrivateKey my_x25519_private_key
Our private key.
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.
static struct GNUNET_PILS_Handle * pils
For PILS.
static void start_initial_kx_out(struct Queue *queue)
Initialize our key material for outgoing transmissions and inform the other peer about it.
static void free_proto_queue(struct ProtoQueue *pq)
Closes socket and frees memory associated with pq.
static struct GNUNET_CONTAINER_MultiHashMap * lt_map
ListenTasks (map from socket to struct ListenTask)
static unsigned long long rekey_max_bytes
The rekey byte maximum.
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.
static struct GNUNET_SCHEDULER_Task * write_task
ID of write task.
static struct GNUNET_TRANSPORT_AddressIdentifier * ai
Handle to the operation that publishes our address.
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
static char * address
GNS address for this phone.
struct GNUNET_HashCode key
The key used in the DHT.
static char * filename
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
struct GNUNET_CRYPTO_PrivateKey pk
Private key from command line option, or NULL.
struct GNUNET_SCHEDULER_Task * read_task
static char * value
Value of the record to add/remove.
static uint32_t type
Type string converted to DNS type value.
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
static struct GNUNET_TRANSPORT_PluginMonitor * pm
Handle if we are monitoring plugin session activity.
static void queue(const char *hostname)
Add hostname to the list of requests to be made.
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
void GNUNET_PILS_disconnect(struct GNUNET_PILS_Handle *handle)
Disconnect from the PILS service.
Definition pils_api.c:390
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
#define GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE_ACK
Signature by a peer sending back the nonce received at initial handshake.
#define GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY
Signature used by TCP communicator rekey.
#define GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE
Signature used by TCP communicator handshake.
void GNUNET_TRANSPORT_communicator_address_remove(struct GNUNET_TRANSPORT_AddressIdentifier *ai)
Notify transport service about an address that this communicator no longer provides for this peer.
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.
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.
#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...
void GNUNET_TRANSPORT_communicator_disconnect(struct GNUNET_TRANSPORT_CommunicatorHandle *ch)
Disconnect from the transport service.
GNUNET_TRANSPORT_ConnectionStatus
Possible states of a connection.
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.
struct GNUNET_TRANSPORT_AddressIdentifier * GNUNET_TRANSPORT_communicator_address_add(struct GNUNET_TRANSPORT_CommunicatorHandle *ch, const char *address, enum GNUNET_NetworkType nt, struct GNUNET_TIME_Relative expiration)
Notify transport service about an address that this communicator provides for this peer.
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.
@ GNUNET_TRANSPORT_CC_RELIABLE
Transmission is reliabile (with ACKs), e.g.
@ GNUNET_TRANSPORT_CS_INBOUND
this is an inbound connection (communicator initiated)
@ GNUNET_TRANSPORT_CS_OUTBOUND
this is an outbound connection (transport initiated)
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_time(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, struct GNUNET_TIME_Relative *time)
Get a configuration value that should be a relative time.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
#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?
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.
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Generate a random unsigned 64-bit value.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_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...
void GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
Fill block with a random values.
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
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...
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.
#define GNUNET_CRYPTO_eddsa_sign(priv, ps, sig)
EdDSA sign a given block.
#define GNUNET_CRYPTO_eddsa_verify(purp, ps, sig, pub)
Verify EdDSA signature.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
@ GNUNET_CRYPTO_QUALITY_NONCE
Randomness for IVs etc.
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.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_GETOPT_OPTION_END
Marker for the end of the list of options.
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
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hkdf_expand(void *result, size_t out_len, const struct GNUNET_ShortHashCode *prk,...)
HKDF-Expand using SHA256.
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).
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_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.
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.
unsigned int GNUNET_CONTAINER_multihashmap_size(const struct GNUNET_CONTAINER_MultiHashMap *map)
Get the number of key-value pairs in the map.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE
Allow multiple values with the same key.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
#define GNUNET_log(kind,...)
void GNUNET_CRYPTO_hash_context_read(struct GNUNET_HashContext *hc, const void *buf, size_t size)
Add data to be hashed.
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
void GNUNET_CRYPTO_hash_context_finish(struct GNUNET_HashContext *hc, struct GNUNET_HashCode *r_hash)
Finish the hash computation.
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)
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)
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_MIN(a, b)
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
struct GNUNET_HashContext * GNUNET_CRYPTO_hash_context_start(void)
Start incremental hashing operation.
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...
@ GNUNET_PUBLIC_KEY_TYPE_EDDSA
EDDSA identity.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
const char * GNUNET_e2s(const struct GNUNET_CRYPTO_EcdhePublicKey *p)
Convert a public key value to a string (for printing debug messages).
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
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).
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
const char * GNUNET_p2s(const struct GNUNET_CRYPTO_EddsaPublicKey *p)
Convert a public key value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
GNUNET_MQ_Error
Error codes for the queue.
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
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
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_NAT_Handle * GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *config_section, uint8_t proto, unsigned int num_addrs, const struct sockaddr **addrs, const socklen_t *addrlens, GNUNET_NAT_AddressCallback address_callback, GNUNET_NAT_ReversalCallback reversal_callback, void *callback_cls)
Attempt to enable port redirection and detect public IP address contacting UPnP or NAT-PMP routers on...
Definition nat_api.c:366
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
GNUNET_NAT_AddressClass
Some addresses contain sensitive information or are not suitable for global distribution.
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
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
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_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
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
enum GNUNET_GenericReturnValue GNUNET_NETWORK_test_pf(int pf)
Test if the given protocol family is supported by this system.
Definition network.c:79
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
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
void GNUNET_NT_scanner_done(struct GNUNET_NT_InterfaceScanner *is)
Terminate interface scanner.
Definition nt.c:428
struct GNUNET_NT_InterfaceScanner * GNUNET_NT_scanner_init(void)
Initialize the address characterization client handle.
Definition nt.c:407
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
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
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.
void GNUNET_PEERSTORE_iteration_next(struct GNUNET_PEERSTORE_IterateContext *ic, uint64_t limit)
Continue an iteration.
void GNUNET_PEERSTORE_store_cancel(struct GNUNET_PEERSTORE_StoreContext *sc)
Cancel a store request.
void GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
Disconnect from the PEERSTORE service.
struct GNUNET_PEERSTORE_Handle * GNUNET_PEERSTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the PEERSTORE service.
#define GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE
Key used to store sender's monotonic time from handshake message.
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_HANDSHAKE_ACK
Key used to store sender's monotonic time from handshake ack message.
#define GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY
Key used to store sender's monotonic time from rekey message.
void GNUNET_PEERSTORE_iteration_stop(struct GNUNET_PEERSTORE_IterateContext *ic)
Cancel an iteration.
@ GNUNET_PEERSTORE_STOREOPTION_REPLACE
Delete any previous values for the given key before storing the given value.
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 GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_CONFIRMATION_ACK
TCP communicator confirmation ack.
#define GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE
TCP NAT probe message, send from NAT'd peer to other peer to establish bi-directional communication.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH
TCP communicator end of stream.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX
TCP communicator payload box.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY
TCP communicator rekey message.
struct GNUNET_RESOLVER_RequestHandle * GNUNET_RESOLVER_ip_get(const char *hostname, int af, struct GNUNET_TIME_Relative timeout, GNUNET_RESOLVER_AddressCallback callback, void *callback_cls)
Convert a string to one or more IP addresses.
void GNUNET_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh)
Cancel a request that is still pending with the resolver.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition scheduler.c:567
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
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
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
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition scheduler.c:980
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
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
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
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
void GNUNET_STATISTICS_set(struct GNUNET_STATISTICS_Handle *handle, const char *name, uint64_t value, int make_persistent)
Set statistic value for the peer.
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
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
#define GNUNET_TIME_UNIT_MINUTES
One minute.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition time.c:741
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
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
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition strings.c:660
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
#define max(x, y)
static unsigned int size
Size of the "table".
Definition peer.c:68
#define _(String)
GNU gettext support macro.
Definition platform.h:179
static struct GNUNET_MQ_Handle * mq
Our connection to the resolver service, created on-demand, but then persists until error or shutdown.
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition scheduler.c:431
void receiver(void *cls, const void *buf, size_t available, const struct sockaddr *addr, socklen_t addrlen, int errCode)
Callback to read from the SOCKS5 proxy.
Definition socks.c:330
DLL to store the addresses we like to register at NAT service.
struct Addresses * next
Kept in a DLL.
struct Addresses * prev
Kept in a DLL.
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.
Internal representation of the hash map.
Type of a nonce used for challenges.
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
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...
Private ECC key encoded for transmission.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
Private ECC key encoded for transmission.
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...
an ECC signature using EdDSA.
HPKE DHKEM encapsulation (X25519) See RFC 9180.
A private key for an identity as per LSD0001.
An identity key as per LSD0001.
Handle used to access files (and pipes).
Definition of a command line option.
Context for parsing HELLOs.
Definition hello-uri.c:232
A 512-bit hashcode.
Handle to a message queue.
Definition mq.c:87
Header for all communications.
Handle for active NAT registrations.
Definition nat_api.c:72
handle to a socket
Definition network.c:53
Handle to the interface scanner.
Definition nt.c:104
Handle to the PEERSTORE service.
Context for a iterate request.
Context for a store request.
A handle for the PILS service.
Definition pils_api.c:82
The identity of the host (wraps the signing key of the peer).
struct GNUNET_CRYPTO_EddsaPublicKey public_key
Handle to a request given to the resolver.
Entry in list of pending tasks.
Definition scheduler.c:136
Handle for the service.
A 256-bit hashcode.
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.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.
Internal representation of an address a communicator is currently providing for the transport service...
Opaque handle to the transport service for communicators.
struct GNUNET_MQ_Handle * mq
Queue to talk to the transport service.
Handle returned to identify the internal data structure the transport API has created to manage a mes...
Struct to use as closure.
struct GNUNET_NETWORK_Handle * listen_sock
Listen socket.
struct GNUNET_SCHEDULER_Task * listen_task
ID of listen task.
Struct for pending nat reversals.
struct sockaddr * in
Address the reversal was send to.
struct GNUNET_SCHEDULER_Task * timeout_task
struct GNUNET_PeerIdentity target
To whom are we like to talk to.
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.
Handle for an incoming connection where we do not yet have enough information to setup a full queue.
socklen_t address_len
Length of the address.
struct GNUNET_SCHEDULER_Task * write_task
ID of write task for this connection.
char write_buf[sizeof(struct TCPNATProbeMessage)]
buffer for writing struct TCPNATProbeMessage to network.
struct GNUNET_NETWORK_Handle * listen_sock
Listen socket.
size_t ibuf_off
Current offset for reading into ibuf.
struct GNUNET_TIME_Absolute timeout
Timeout for this protoqueue.
struct GNUNET_NETWORK_Handle * sock
socket that we transmit all data with on this queue
struct sockaddr * address
Address of the other peer.
struct ProtoQueue * prev
Kept in a DLL.
char ibuf[(sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)+sizeof(struct TCPConfirmation))]
Buffer for reading all the information we need to upgrade from protoqueue to queue.
size_t write_off
Offset of the buffer?
struct GNUNET_SCHEDULER_Task * read_task
ID of read task for this connection.
struct ProtoQueue * next
Kept in a DLL.
Handle for a queue.
struct GNUNET_CRYPTO_EcdhePublicKey target_hpke_key
To whom are we talking to.
unsigned int backpressure
How may messages did we pass from this queue to CORE for which we have yet to receive an acknowledgem...
struct GNUNET_NETWORK_Handle * sock
socket that we transmit all data with on this queue
struct GNUNET_PEERSTORE_IterateContext * handshake_ack_monotime_get
Iteration Context for retrieving the monotonic time send with the handshake ack.
int rekeyed
GNUNET_YES if we just rekeyed and must thus possibly re-decrypt ciphertext.
struct GNUNET_CRYPTO_ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value.
size_t unverified_size
Size of data received without KX challenge played back.
struct GNUNET_TRANSPORT_QueueHandle * qh
handle for this queue with the ch.
struct GNUNET_HashCode out_hmac
Shared secret for HMAC generation on outgoing data, ratcheted after each operation.
int finishing
Did we enqueue a finish message and are closing down the queue?
int initial_core_kx_done
Has the initial (core) handshake already happened?
struct GNUNET_HashCode key
Key in hash map.
socklen_t address_len
Length of the address.
struct GNUNET_MQ_Handle * mq
Message queue we are providing for the ch.
unsigned long long bytes_in_queue
Number of bytes we currently have in our write queue.
char pread_buf[UINT16_MAX+1+sizeof(struct TCPBox)]
Plaintext buffer for decrypted plaintext.
int destroyed
Did we technically destroy this queue, but kept the allocation around because of backpressure not bei...
struct GNUNET_PEERSTORE_StoreContext * rekey_monotime_sc
Store Context for retrieving the monotonic time send with key for rekeying.
size_t cwrite_off
At which offset in the ciphertext write buffer should we append more ciphertext from reading next?
struct GNUNET_PEERSTORE_IterateContext * rekey_monotime_get
Iteration Context for retrieving the monotonic time send with key for rekeying.
char cread_buf[(2 *64 *1024+sizeof(struct TCPBox))]
Buffer for reading ciphertext from network into.
enum GNUNET_NetworkType nt
Which network type does this queue use?
struct GNUNET_CRYPTO_ChallengeNonceP challenge_received
Challenge value received.
uint64_t rekey_left_bytes
How many more bytes may we sent with the current out_cipher before we should rekey?
int mq_awaits_continue
Is MQ awaiting a GNUNET_MQ_impl_send_continue() call?
gcry_cipher_hd_t in_cipher
cipher for decryption of incoming data.
struct GNUNET_PEERSTORE_StoreContext * handshake_ack_monotime_sc
Store Context for retrieving the monotonic time send with the handshake ack.
struct GNUNET_SCHEDULER_Task * write_task
ID of write task for this connection.
size_t pwrite_off
At which offset in the plaintext output buffer should we append more plaintext for encryption next?
struct GNUNET_TIME_Absolute timeout
Timeout for this queue.
size_t cread_off
At which offset in the ciphertext read buffer should we append more ciphertext for transmission next?
struct GNUNET_TIME_Absolute rekey_time
Until what time may we sent with the current out_cipher before we should rekey?
struct GNUNET_PeerIdentity target
To whom are we talking to.
char cwrite_buf[(2 *64 *1024+sizeof(struct TCPBox))]
buffer for writing ciphertext to network.
struct GNUNET_PEERSTORE_StoreContext * handshake_monotime_sc
Store Context for retrieving the monotonic time send with the handshake.
size_t pread_off
At which offset in the plaintext input buffer should we append more plaintext from decryption next?
struct GNUNET_SCHEDULER_Task * read_task
ID of read task for this connection.
enum GNUNET_TRANSPORT_ConnectionStatus cs
The connection status of this queue.
gcry_cipher_hd_t out_cipher
cipher for encryption of outgoing data.
char pwrite_buf[UINT16_MAX+1+sizeof(struct TCPBox)]
Plaintext buffer for messages to be encrypted.
struct GNUNET_HashCode in_hmac
Shared secret for HMAC verification on incoming data.
struct GNUNET_PEERSTORE_IterateContext * handshake_monotime_get
Iteration Context for retrieving the monotonic time send with the handshake.
struct GNUNET_NETWORK_Handle * listen_sock
Listen socket.
struct sockaddr * address
Address of the other peer.
struct GNUNET_TIME_AbsoluteNBO handshake_ack_monotonic_time
Monotonic time value for handshake ack message.
struct GNUNET_TIME_AbsoluteNBO rekey_monotonic_time
Monotonic time value for rekey message.
struct GNUNET_TIME_AbsoluteNBO handshake_monotonic_time
Monotonic time value for handshake message.
TCP message box.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
Ack for the encrypted continuation of TCP initial handshake.
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_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_CONFIRMATION_ACK.
struct GNUNET_PeerIdentity sender
Sender's identity.
Encrypted continuation of TCP initial handshake.
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.
struct GNUNET_PeerIdentity sender
Sender's identity.
struct GNUNET_CRYPTO_ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
Basically a WELCOME message, but with the purpose of giving the waiting peer a client handle to use.
struct GNUNET_PeerIdentity clientIdentity
Identity of the sender of the message.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
TCP rekey message box.
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.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY.
Signature we use to verify that the ack from the receiver of the ephemeral key was really send by the...
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Purpose must be GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE_ACK.
struct GNUNET_PeerIdentity sender
Identity of the inititor of the TCP connection (TCP client).
struct GNUNET_CRYPTO_ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value.
struct GNUNET_PeerIdentity receiver
Presumed identity of the target of the TCP connection (TCP server)
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
Signature we use to verify that the ephemeral key was really chosen by the specified sender.
Signature we use to verify that the ephemeral key was really chosen by the specified sender.
struct GNUNET_CRYPTO_HpkeEncapsulation ephemeral
Ephemeral key used by the sender.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Purpose must be GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY.
struct GNUNET_PeerIdentity sender
Identity of the inititor of the TCP connection (TCP client).
struct GNUNET_PeerIdentity receiver
Presumed identity of the target of the TCP connection (TCP server)
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...

◆ DEFAULT_MAX_QUEUE_LENGTH

#define DEFAULT_MAX_QUEUE_LENGTH   8

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

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

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

◆ BUF_SIZE

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

Size of our IO buffers for ciphertext data.

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

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

◆ DEFAULT_REKEY_INTERVAL

#define DEFAULT_REKEY_INTERVAL   GNUNET_TIME_UNIT_DAYS

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

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

◆ PROTO_QUEUE_TIMEOUT

#define PROTO_QUEUE_TIMEOUT   GNUNET_TIME_UNIT_MINUTES

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

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

◆ REKEY_MAX_BYTES

#define REKEY_MAX_BYTES   (1024LLU * 1024 * 400)

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

Currently 400 MB

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

◆ INITIAL_KX_SIZE

#define INITIAL_KX_SIZE
Value:
(sizeof(struct GNUNET_CRYPTO_EcdhePublicKey) \
+ sizeof(struct TCPConfirmation))

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

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

◆ INITIAL_CORE_KX_SIZE

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

Size of the initial core key exchange messages.

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

◆ COMMUNICATOR_ADDRESS_PREFIX

#define COMMUNICATOR_ADDRESS_PREFIX   "tcp"

Address prefix used by the communicator.

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

◆ COMMUNICATOR_CONFIG_SECTION

#define COMMUNICATOR_CONFIG_SECTION   "communicator-tcp"

Configuration section used by the communicator.

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

Function Documentation

◆ listen_cb()

static void listen_cb ( void *  cls)
static

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

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

Parameters
clsNULL

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

Parameters
clsListenTask with listening socket and task

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

3249{
3250 struct sockaddr_storage in;
3251 socklen_t addrlen;
3252 struct GNUNET_NETWORK_Handle *sock;
3253 struct ListenTask *lt;
3254 struct sockaddr *in_addr;
3255
3257 "listen_cb\n");
3258
3259 lt = cls;
3260
3261 lt->listen_task = NULL;
3262 GNUNET_assert (NULL != lt->listen_sock);
3263 addrlen = sizeof(in);
3264 memset (&in, 0, sizeof(in));
3266 (struct sockaddr*) &in,
3267 &addrlen);
3268 if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno)))
3269 return; /* system limit reached, wait until connection goes down */
3271 lt->listen_sock,
3272 &listen_cb,
3273 lt);
3274 if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno)))
3275 return;
3276 if (NULL == sock)
3277 {
3279 return;
3280 }
3281 in_addr = GNUNET_memdup (&in, addrlen);
3282 create_proto_queue (sock, in_addr, addrlen);
3283}

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

963{
965 key.type = htonl (GNUNET_PUBLIC_KEY_TYPE_EDDSA);
966 key.eddsa_key = *edpk;
968}

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

974{
976 key.type = htonl (GNUNET_PUBLIC_KEY_TYPE_EDDSA);
977 key.eddsa_key = *edpk;
979}

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

991{
992 struct ListenTask *lt = NULL;
993 struct GNUNET_HashCode h_sock;
994 int sockfd;
995
996 if (NULL != queue->listen_sock)
997 {
998 sockfd = GNUNET_NETWORK_get_fd (queue->listen_sock);
999 GNUNET_CRYPTO_hash (&sockfd,
1000 sizeof(int),
1001 &h_sock);
1002
1004 }
1005
1007 "Disconnecting queue for peer `%s'\n",
1008 GNUNET_i2s (&queue->target));
1009 if (NULL != queue->rekey_monotime_sc)
1010 {
1011 GNUNET_PEERSTORE_store_cancel (queue->rekey_monotime_sc);
1012 queue->rekey_monotime_sc = NULL;
1013 }
1014 if (NULL != queue->handshake_monotime_sc)
1015 {
1016 GNUNET_PEERSTORE_store_cancel (queue->handshake_monotime_sc);
1017 queue->handshake_monotime_sc = NULL;
1018 }
1019 if (NULL != queue->handshake_ack_monotime_sc)
1020 {
1021 GNUNET_PEERSTORE_store_cancel (queue->handshake_ack_monotime_sc);
1022 queue->handshake_ack_monotime_sc = NULL;
1023 }
1024 if (NULL != queue->rekey_monotime_get)
1025 {
1026 GNUNET_PEERSTORE_iteration_stop (queue->rekey_monotime_get);
1027 queue->rekey_monotime_get = NULL;
1028 }
1029 if (NULL != queue->handshake_monotime_get)
1030 {
1031 GNUNET_PEERSTORE_iteration_stop (queue->handshake_monotime_get);
1032 queue->handshake_monotime_get = NULL;
1033 }
1034 if (NULL != queue->handshake_ack_monotime_get)
1035 {
1036 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
1037 queue->handshake_ack_monotime_get = NULL;
1038 }
1039 if (NULL != queue->qh)
1040 {
1042 queue->qh = NULL;
1043 }
1045 GNUNET_YES ==
1048 "# queues active",
1050 GNUNET_NO);
1051 if (NULL != queue->read_task)
1052 {
1053 GNUNET_SCHEDULER_cancel (queue->read_task);
1054 queue->read_task = NULL;
1055 }
1056 if (NULL != queue->write_task)
1057 {
1058 GNUNET_SCHEDULER_cancel (queue->write_task);
1059 queue->write_task = NULL;
1060 }
1062 {
1064 "closing socket failed\n");
1065 }
1066 gcry_cipher_close (queue->in_cipher);
1067 gcry_cipher_close (queue->out_cipher);
1068 GNUNET_free (queue->address);
1069 if (0 != queue->backpressure)
1070 queue->destroyed = GNUNET_YES;
1071 else
1073
1074 if (NULL == lt)
1075 return;
1076
1077 if ((! shutdown_running) && (NULL == lt->listen_task))
1078 {
1080 "add read net listen\n");
1083 lt->listen_sock,
1084 &listen_cb,
1085 lt);
1086 }
1087 else
1088 GNUNET_free (lt);
1089}

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

1105{
1106 struct GNUNET_HashCode mac;
1107
1108 GNUNET_CRYPTO_hmac_raw (hmac_secret,
1109 sizeof(struct GNUNET_HashCode),
1110 buf,
1111 buf_size,
1112 &mac);
1113 /* truncate to `struct GNUNET_ShortHashCode` */
1114 memcpy (smac, &mac, sizeof(struct GNUNET_ShortHashCode));
1115 /* ratchet hmac key */
1116 GNUNET_CRYPTO_hash (hmac_secret,
1117 sizeof(struct GNUNET_HashCode),
1118 hmac_secret);
1119}

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

1130{
1131 struct TCPFinish fin;
1132
1133 memset (&fin, 0, sizeof(fin));
1134 fin.header.size = htons (sizeof(fin));
1135 fin.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH);
1136 calculate_hmac (&queue->out_hmac, &fin, sizeof(fin), &fin.hmac);
1137 /* if there is any message left in pwrite_buf, we
1138 overwrite it (possibly dropping the last message
1139 from CORE hard here) */
1140 memcpy (queue->pwrite_buf, &fin, sizeof(fin));
1141 queue->pwrite_off = sizeof(fin);
1142 /* This flag will ensure that #queue_write() no longer
1143 notifies CORE about the possibility of sending
1144 more data, and that #queue_write() will call
1145 #queue_destroy() once the @c fin was fully written. */
1146 queue->finishing = GNUNET_YES;
1147}

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

2194{
2195 struct Queue *queue = cls;
2196 struct GNUNET_TIME_Relative left;
2197 ssize_t rcvd;
2198
2199 queue->read_task = NULL;
2200 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2201 &queue->cread_buf[queue->cread_off],
2202 BUF_SIZE - queue->cread_off);
2204 "Received %zd bytes from TCP queue\n", rcvd);
2205 if (-1 == rcvd)
2206 {
2207 if ((EAGAIN != errno) && (EINTR != errno))
2208 {
2211 return;
2212 }
2213 /* try again */
2214 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2215 if (0 != left.rel_value_us)
2216 {
2217 queue->read_task =
2219 return;
2220 }
2222 "Queue %p was idle for %s, disconnecting\n",
2223 queue,
2226 GNUNET_YES));
2228 return;
2229 }
2230 if (0 == rcvd)
2231 {
2232 /* Orderly shutdown of connection */
2234 "Socket for queue %p seems to have been closed\n", queue);
2236 return;
2237 }
2238 queue->timeout =
2240 queue->cread_off += rcvd;
2241 while ((queue->pread_off < sizeof(queue->pread_buf)) &&
2242 (queue->cread_off > 0))
2243 {
2244 size_t max = GNUNET_MIN (sizeof(queue->pread_buf) - queue->pread_off,
2245 queue->cread_off);
2246 size_t done;
2247 size_t total;
2248 size_t old_pread_off = queue->pread_off;
2249
2250 GNUNET_assert (0 ==
2251 gcry_cipher_decrypt (queue->in_cipher,
2252 &queue->pread_buf[queue->pread_off],
2253 max,
2254 queue->cread_buf,
2255 max));
2256 queue->pread_off += max;
2257 total = 0;
2258 while (0 != (done = try_handle_plaintext (queue)))
2259 {
2260 /* 'done' bytes of plaintext were used, shift buffer */
2261 GNUNET_assert (done <= queue->pread_off);
2262 /* NOTE: this memmove() could possibly sometimes be
2263 avoided if we pass 'total' into try_handle_plaintext()
2264 and use it at an offset into the buffer there! */
2265 memmove (queue->pread_buf,
2266 &queue->pread_buf[done],
2267 queue->pread_off - done);
2268 queue->pread_off -= done;
2269 total += done;
2270 /* The last plaintext was a rekey, abort for now */
2271 if (GNUNET_YES == queue->rekeyed)
2272 break;
2273 }
2274 /* when we encounter a rekey message, the decryption above uses the
2275 wrong key for everything after the rekey; in that case, we have
2276 to re-do the decryption at 'total' instead of at 'max'.
2277 However, we have to take into account that the plaintext buffer may have
2278 already contained data and not jumped too far ahead in the ciphertext.
2279 If there is no rekey and the last message is incomplete (max > total),
2280 it is safe to keep the decryption so we shift by 'max' */
2281 if (GNUNET_YES == queue->rekeyed)
2282 {
2283 max = total - old_pread_off;
2284 queue->rekeyed = GNUNET_NO;
2285 queue->pread_off = 0;
2286 }
2287 memmove (queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
2288 queue->cread_off -= max;
2289 }
2290 if (BUF_SIZE == queue->cread_off)
2291 return; /* buffer full, suspend reading */
2292 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2293 if (0 != left.rel_value_us)
2294 {
2295 if (max_queue_length > queue->backpressure)
2296 {
2297 /* continue reading */
2298 queue->read_task =
2300 }
2301 return;
2302 }
2304 "Queue %p was idle for %s, disconnecting\n",
2305 queue,
2308 GNUNET_YES));
2310}

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

1168{
1169 struct Queue *queue = cls;
1170 if (GNUNET_OK != success)
1172 "# messages lost in communicator API towards CORE",
1173 1,
1174 GNUNET_NO);
1175 if (NULL == queue)
1176 return;
1177
1179 "backpressure %u\n",
1180 queue->backpressure);
1181
1182 queue->backpressure--;
1183 /* handle deferred queue destruction */
1184 if ((queue->destroyed) && (0 == queue->backpressure))
1185 {
1187 return;
1188 }
1189 else if (GNUNET_YES != queue->destroyed)
1190 {
1191 queue->timeout =
1193 );
1194 /* possibly unchoke reading, now that CORE made progress */
1195 if (NULL == queue->read_task)
1196 queue->read_task =
1198 queue->timeout),
1199 queue->sock,
1200 &queue_read,
1201 queue);
1202 }
1203}

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

1219{
1220 const struct GNUNET_MessageHeader *hdr = plaintext;
1221 int ret;
1222
1224 "pass message from %s to core\n",
1225 GNUNET_i2s (&queue->target));
1226
1227 if (ntohs (hdr->size) != plaintext_len)
1228 {
1229 /* NOTE: If we ever allow multiple CORE messages in one
1230 BOX, this will have to change! */
1231 GNUNET_break (0);
1232 return;
1233 }
1235 &queue->target,
1236 hdr,
1239 queue);
1241 "passed to core\n");
1242 if (GNUNET_OK == ret)
1243 queue->backpressure++;
1244 GNUNET_break (GNUNET_NO != ret); /* backpressure not working!? */
1245 if (GNUNET_SYSERR == ret)
1247 "# bytes lost due to CORE not running",
1248 plaintext_len,
1249 GNUNET_NO);
1250}

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

1267{
1268 char key[256 / 8];
1269 char ctr[128 / 8];
1270
1271 GNUNET_assert (0 == gcry_cipher_open (cipher,
1272 GCRY_CIPHER_AES256 /* low level: go for speed */
1273 ,
1274 GCRY_CIPHER_MODE_CTR,
1275 0 /* flags */));
1278 sizeof(key),
1279 prk,
1280 "gnunet-communicator-tcp-key",
1281 strlen (
1282 "gnunet-communicator-tcp-key"),
1283 NULL,
1284 0));
1285 GNUNET_assert (0 == gcry_cipher_setkey (*cipher, key, sizeof(key)));
1288 sizeof(ctr),
1289 prk,
1290 "gnunet-communicator-tcp-ctr",
1291 strlen (
1292 "gnunet-communicator-tcp-ctr"),
1293 NULL,
1294 0));
1295 gcry_cipher_setctr (*cipher, ctr, sizeof(ctr));
1297 GNUNET_CRYPTO_hkdf_expand (hmac_key,
1298 sizeof(struct GNUNET_HashCode),
1299 prk,
1300 "gnunet-communicator-hmac",
1301 strlen ("gnunet-communicator-hmac"),
1302 NULL,
1303 0));
1304}

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

1314{
1315 struct Queue *queue = cls;
1316 if (GNUNET_OK != success)
1317 {
1319 "Failed to store rekey monotonic time in PEERSTORE!\n");
1320 }
1321 queue->rekey_monotime_sc = NULL;
1322 GNUNET_PEERSTORE_iteration_next (queue->rekey_monotime_get, 1);
1323}

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

1337{
1338 struct Queue *queue = cls;
1339 struct GNUNET_TIME_AbsoluteNBO *mtbe;
1340 struct GNUNET_TIME_Absolute mt;
1341 const struct GNUNET_PeerIdentity *pid;
1342 struct GNUNET_TIME_AbsoluteNBO *rekey_monotonic_time;
1343
1344 (void) emsg;
1345
1346 rekey_monotonic_time = &queue->rekey_monotonic_time;
1347 pid = &queue->target;
1348 if (NULL == record)
1349 {
1350 queue->rekey_monotime_get = NULL;
1351 return;
1352 }
1353 if (sizeof(*mtbe) != record->value_size)
1354 {
1355 GNUNET_PEERSTORE_iteration_next (queue->rekey_monotime_get, 1);
1356 GNUNET_break (0);
1357 return;
1358 }
1359 mtbe = record->value;
1360 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1361 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
1362 queue->rekey_monotonic_time).abs_value_us)
1363 {
1365 "Queue from %s dropped, rekey monotime in the past\n",
1366 GNUNET_i2s (&queue->target));
1367 GNUNET_break (0);
1368 GNUNET_PEERSTORE_iteration_stop (queue->rekey_monotime_get);
1369 queue->rekey_monotime_get = NULL;
1370 // FIXME: Why should we try to gracefully finish here??
1372 return;
1373 }
1374 queue->rekey_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
1375 "transport_tcp_communicator",
1376 pid,
1378 rekey_monotonic_time,
1379 sizeof(*
1380 rekey_monotonic_time),
1384 queue);
1385}

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

1398{
1399 struct GNUNET_ShortHashCode k;
1400
1402 setup_cipher (&k, &my_identity, &queue->in_cipher, &queue->in_hmac);
1403}

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

1415{
1416 struct GNUNET_ShortHashCode k;
1417
1419 setup_cipher (&k, &my_identity, &queue->in_cipher, &queue->in_hmac);
1420}

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

1433{
1434 struct TcpRekeySignature thp;
1435
1437 thp.purpose.size = htonl (sizeof(thp));
1439 "do_rekey size %u\n",
1440 thp.purpose.size);
1441 thp.sender = queue->target;
1443 "sender %s\n",
1444 GNUNET_p2s (&thp.sender.public_key));
1446 "sender %s\n",
1447 GNUNET_p2s (&queue->target.public_key));
1448 thp.receiver = my_identity;
1450 "receiver %s\n",
1451 GNUNET_p2s (&thp.receiver.public_key));
1452 thp.ephemeral = rekey->ephemeral;
1454 "ephemeral %s\n",
1455 GNUNET_e2s ((struct GNUNET_CRYPTO_EcdhePublicKey*) &thp.ephemeral)
1456 );
1457 thp.monotonic_time = rekey->monotonic_time;
1459 "time %s\n",
1461 GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
1462 GNUNET_assert (ntohl ((&thp)->purpose.size) == sizeof (*(&thp)));
1463 if (GNUNET_OK !=
1466 &thp,
1467 &rekey->sender_sig,
1468 &queue->target.public_key))
1469 {
1470 GNUNET_break (0);
1471 // FIXME Why should we try to gracefully finish here?
1473 return;
1474 }
1475 queue->rekey_monotonic_time = rekey->monotonic_time;
1476 queue->rekey_monotime_get = GNUNET_PEERSTORE_iteration_start (peerstore,
1477 "transport_tcp_communicator",
1478 &queue->target,
1480 &
1482 queue);
1483 gcry_cipher_close (queue->in_cipher);
1484 queue->rekeyed = GNUNET_YES;
1485 setup_in_cipher (&rekey->ephemeral, queue);
1486}

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

1496{
1497 struct Queue *queue = cls;
1498
1499 if (GNUNET_OK != success)
1500 {
1502 "Failed to store handshake ack monotonic time in PEERSTORE!\n");
1503 }
1504 queue->handshake_ack_monotime_sc = NULL;
1505 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
1506}

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

1520{
1521 struct Queue *queue = cls;
1522 struct GNUNET_TIME_AbsoluteNBO *mtbe;
1523 struct GNUNET_TIME_Absolute mt;
1524 const struct GNUNET_PeerIdentity *pid;
1525 struct GNUNET_TIME_AbsoluteNBO *handshake_ack_monotonic_time;
1526
1527 (void) emsg;
1528
1529 handshake_ack_monotonic_time = &queue->handshake_ack_monotonic_time;
1530 pid = &queue->target;
1531 if (NULL == record)
1532 {
1533 queue->handshake_ack_monotime_get = NULL;
1534 return;
1535 }
1536 if (sizeof(*mtbe) != record->value_size)
1537 {
1538 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
1539 GNUNET_break (0);
1540 return;
1541 }
1542 mtbe = record->value;
1543 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1544 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
1545 queue->handshake_ack_monotonic_time).abs_value_us)
1546 {
1548 "Queue from %s dropped, handshake ack monotime in the past\n",
1549 GNUNET_i2s (&queue->target));
1550 GNUNET_break (0);
1551 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
1552 queue->handshake_ack_monotime_get = NULL;
1553 // FIXME: Why should we try to gracefully finish here?
1555 return;
1556 }
1557 queue->handshake_ack_monotime_sc =
1559 "transport_tcp_communicator",
1560 pid,
1562 handshake_ack_monotonic_time,
1563 sizeof(*handshake_ack_monotonic_time),
1567 queue);
1568}

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

1580{
1581 struct TCPConfirmationAck tca;
1582 struct TcpHandshakeAckSignature thas;
1583
1585 "sending challenge\n");
1586
1587 tca.header.type = ntohs (
1589 tca.header.size = ntohs (sizeof(tca));
1590 tca.challenge = challenge;
1591 tca.sender = my_identity;
1592 tca.monotonic_time =
1594 thas.purpose.purpose = htonl (
1596 thas.purpose.size = htonl (sizeof(thas));
1597 thas.sender = my_identity;
1598 thas.receiver = queue->target;
1599 thas.monotonic_time = tca.monotonic_time;
1600 thas.challenge = tca.challenge;
1602 &thas,
1603 &tca.sender_sig);
1604 GNUNET_assert (0 ==
1605 gcry_cipher_encrypt (queue->out_cipher,
1606 &queue->cwrite_buf[queue->cwrite_off],
1607 sizeof(tca),
1608 &tca,
1609 sizeof(tca)));
1610 queue->cwrite_off += sizeof(tca);
1612 "sending challenge done\n");
1613}

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

1624{
1625 setup_cipher (dh, &queue->target, &queue->out_cipher, &queue->out_hmac);
1627 queue->rekey_left_bytes =
1629}

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

1640{
1641 struct TCPRekey rekey;
1642 struct TcpRekeySignature thp;
1643 struct GNUNET_ShortHashCode k;
1644
1645 GNUNET_assert (0 == queue->pwrite_off);
1646 memset (&rekey, 0, sizeof(rekey));
1647 GNUNET_CRYPTO_eddsa_kem_encaps (&queue->target.public_key, &rekey.ephemeral,
1648 &k);
1649 rekey.header.type = ntohs (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY);
1650 rekey.header.size = ntohs (sizeof(rekey));
1651 rekey.monotonic_time =
1653 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY);
1654 thp.purpose.size = htonl (sizeof(thp));
1656 "inject_rekey size %u\n",
1657 thp.purpose.size);
1658 thp.sender = my_identity;
1660 "sender %s\n",
1661 GNUNET_p2s (&thp.sender.public_key));
1662 thp.receiver = queue->target;
1664 "receiver %s\n",
1665 GNUNET_p2s (&thp.receiver.public_key));
1666 thp.ephemeral = rekey.ephemeral;
1668 "ephemeral %s\n",
1669 GNUNET_e2s ((struct GNUNET_CRYPTO_EcdhePublicKey*) &thp.ephemeral)
1670 );
1671 thp.monotonic_time = rekey.monotonic_time;
1673 "time %s\n",
1675 GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
1677 &thp,
1678 &rekey.sender_sig);
1679 calculate_hmac (&queue->out_hmac, &rekey, sizeof(rekey), &rekey.hmac);
1680 /* Encrypt rekey message with 'old' cipher */
1681 GNUNET_assert (0 ==
1682 gcry_cipher_encrypt (queue->out_cipher,
1683 &queue->cwrite_buf[queue->cwrite_off],
1684 sizeof(rekey),
1685 &rekey,
1686 sizeof(rekey)));
1687 queue->cwrite_off += sizeof(rekey);
1688 /* Setup new cipher for successive messages */
1689 gcry_cipher_close (queue->out_cipher);
1690 setup_out_cipher (queue, &k);
1691}

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

1698{
1699 struct PendingReversal *pending_reversal = value;
1700 (void) cls;
1701
1702 if (NULL != pending_reversal->timeout_task)
1703 {
1704 GNUNET_SCHEDULER_cancel (pending_reversal->timeout_task);
1705 pending_reversal->timeout_task = NULL;
1706 }
1709 key,
1710 pending_reversal));
1711 GNUNET_free (pending_reversal->in);
1712 GNUNET_free (pending_reversal);
1713 return GNUNET_OK;
1714}

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

1720{
1721 if (AF_INET == sa_family)
1722 {
1723 struct PendingReversal *pending_reversal;
1724 struct GNUNET_HashCode key;
1725 struct sockaddr_in *natted_address;
1726
1727 natted_address = GNUNET_memdup (in, sizeof (struct sockaddr));
1728 natted_address->sin_port = 0;
1729 GNUNET_CRYPTO_hash (natted_address,
1730 sizeof(struct sockaddr),
1731 &key);
1732
1734 &key);
1735 if (NULL != pending_reversal && (NULL == sender ||
1736 0 != memcmp (sender,
1737 &pending_reversal->target,
1738 sizeof(struct
1740 {
1742 "Removing invalid pending reversal for `%s'at `%s'\n",
1743 GNUNET_i2s (&pending_reversal->target),
1744 GNUNET_a2s (in, sizeof (struct sockaddr)));
1745 pending_reversals_delete_it (NULL, &key, pending_reversal);
1746 }
1747 GNUNET_free (natted_address);
1748 }
1749}

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

1759{
1760 if (NULL != pq->listen_sock)
1761 {
1763 pq->listen_sock = NULL;
1764 }
1765 if (NULL != pq->read_task)
1766 {
1768 pq->read_task = NULL;
1769 }
1770 if (NULL != pq->write_task)
1771 {
1773 pq->write_task = NULL;
1774 }
1775 check_and_remove_pending_reversal (pq->address, pq->address->sa_family, NULL);
1777 GNUNET_free (pq->address);
1779 GNUNET_free (pq);
1780}

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

1791{
1792 struct ProtoQueue *pq = cls;
1793 ssize_t sent;
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In proto queue write\n");
1795 pq->write_task = NULL;
1796 if (0 != pq->write_off)
1797 {
1798 sent = GNUNET_NETWORK_socket_send (pq->sock,
1799 pq->write_buf,
1800 pq->write_off);
1802 "Sent %lu bytes to TCP queue\n", sent);
1803 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1804 {
1806 free_proto_queue (pq);
1807 return;
1808 }
1809 if (sent > 0)
1810 {
1811 size_t usent = (size_t) sent;
1812 pq->write_off -= usent;
1813 memmove (pq->write_buf,
1814 &pq->write_buf[usent],
1815 pq->write_off);
1816 }
1817 }
1818 /* do we care to write more? */
1819 if ((0 < pq->write_off))
1820 pq->write_task =
1822 pq->sock,
1824 pq);
1825}

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

Referenced by proto_queue_write(), and try_connection_reversal().

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

◆ queue_write()

static void queue_write ( void *  cls)
static

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

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

Parameters
clsa struct Queue

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

1836{
1837 struct Queue *queue = cls;
1838 ssize_t sent;
1839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In queue write\n");
1840 queue->write_task = NULL;
1841 if (0 != queue->cwrite_off)
1842 {
1843 sent = GNUNET_NETWORK_socket_send (queue->sock,
1844 queue->cwrite_buf,
1845 queue->cwrite_off);
1847 "Sent %lu bytes to TCP queue\n", sent);
1848 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1849 {
1852 return;
1853 }
1854 if (sent > 0)
1855 {
1856 size_t usent = (size_t) sent;
1857 queue->cwrite_off -= usent;
1858 memmove (queue->cwrite_buf,
1859 &queue->cwrite_buf[usent],
1860 queue->cwrite_off);
1861 queue->timeout =
1864 }
1865 }
1866 {
1867 /* can we encrypt more? (always encrypt full messages, needed
1868 such that #mq_cancel() can work!) */
1869 unsigned int we_do_not_need_to_rekey = (0 < queue->rekey_left_bytes
1870 - (queue->cwrite_off
1871 + queue->pwrite_off
1872 + sizeof (struct TCPRekey)));
1873 if (we_do_not_need_to_rekey &&
1874 (queue->pwrite_off > 0) &&
1875 (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE))
1876 {
1878 "Encrypting %lu bytes\n", queue->pwrite_off);
1879 GNUNET_assert (0 ==
1880 gcry_cipher_encrypt (queue->out_cipher,
1881 &queue->cwrite_buf[queue->cwrite_off],
1882 queue->pwrite_off,
1883 queue->pwrite_buf,
1884 queue->pwrite_off));
1885 if (queue->rekey_left_bytes > queue->pwrite_off)
1886 queue->rekey_left_bytes -= queue->pwrite_off;
1887 else
1888 queue->rekey_left_bytes = 0;
1889 queue->cwrite_off += queue->pwrite_off;
1890 queue->pwrite_off = 0;
1891 }
1892 // if ((-1 != unverified_size)&& ((0 == queue->pwrite_off) &&
1893 if (((0 == queue->rekey_left_bytes) ||
1895 queue->rekey_time).rel_value_us)) &&
1896 (((0 == queue->pwrite_off) || ! we_do_not_need_to_rekey) &&
1897 (queue->cwrite_off + sizeof (struct TCPRekey) <= BUF_SIZE)))
1898 {
1900 }
1901 }
1902 if ((0 == queue->pwrite_off) && (! queue->finishing) &&
1903 (GNUNET_YES == queue->mq_awaits_continue))
1904 {
1905 queue->mq_awaits_continue = GNUNET_NO;
1907 }
1908 /* did we just finish writing 'finish'? */
1909 if ((0 == queue->cwrite_off) && (GNUNET_YES == queue->finishing))
1910 {
1912 "Finishing queue\n");
1914 return;
1915 }
1916 /* do we care to write more? */
1917 if ((0 < queue->cwrite_off) || (0 < queue->pwrite_off))
1918 queue->write_task =
1920 queue->sock,
1921 &queue_write,
1922 queue);
1923}

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

1935{
1936 const struct GNUNET_MessageHeader *hdr;
1937 const struct TCPConfirmationAck *tca;
1938 const struct TCPBox *box;
1939 const struct TCPRekey *rekey;
1940 const struct TCPFinish *fin;
1941 struct TCPRekey rekeyz;
1942 struct TCPFinish finz;
1943 struct GNUNET_ShortHashCode tmac;
1944 uint16_t type;
1945 size_t size = 0;
1946 struct TcpHandshakeAckSignature thas;
1947 const struct GNUNET_CRYPTO_ChallengeNonceP challenge = queue->challenge;
1948
1950 "try handle plaintext!\n");
1951
1952 hdr = (const struct GNUNET_MessageHeader *) queue->pread_buf;
1953 if ((sizeof(*hdr) > queue->pread_off))
1954 {
1956 "Handling plaintext, not even a header!\n");
1957 return 0; /* not even a header */
1958 }
1959
1960 if ((GNUNET_YES != queue->initial_core_kx_done) && (queue->unverified_size >
1962 {
1964 "Already received data of size %lu bigger than KX size %lu!\n",
1965 queue->unverified_size,
1967 GNUNET_break_op (0);
1969 return 0;
1970 }
1971
1972 type = ntohs (hdr->type);
1973 switch (type)
1974 {
1976 tca = (const struct TCPConfirmationAck *) queue->pread_buf;
1978 "start processing ack\n");
1979 if (sizeof(*tca) > queue->pread_off)
1980 {
1982 "Handling plaintext size of tca greater than pread offset.\n")
1983 ;
1984 return 0;
1985 }
1986 if (ntohs (hdr->size) != sizeof(*tca))
1987 {
1989 "Handling plaintext size does not match message type.\n");
1990 GNUNET_break_op (0);
1992 return 0;
1993 }
1994
1995 thas.purpose.purpose = htonl (
1997 thas.purpose.size = htonl (sizeof(thas));
1998 thas.sender = tca->sender;
1999 thas.receiver = my_identity;
2000 thas.monotonic_time = tca->monotonic_time;
2001 thas.challenge = tca->challenge;
2002
2005 &thas,
2006 &tca->sender_sig,
2007 &tca->sender.public_key))
2008 {
2010 "Verification of signature failed!\n");
2011 GNUNET_break (0);
2013 return 0;
2014 }
2015 if (0 != GNUNET_memcmp (&tca->challenge, &challenge))
2016 {
2018 "Challenge in TCPConfirmationAck not correct!\n");
2019 GNUNET_break (0);
2021 return 0;
2022 }
2023
2024 queue->handshake_ack_monotime_get = GNUNET_PEERSTORE_iteration_start (
2025 peerstore,
2026 "transport_tcp_communicator",
2027 &queue->target,
2030 queue);
2031
2033 "Handling plaintext, ack processed!\n");
2034
2036 {
2037 send_challenge (queue->challenge_received, queue);
2038 queue->write_task =
2040 queue->sock,
2041 &queue_write,
2042 queue);
2043 }
2044 else if (GNUNET_TRANSPORT_CS_OUTBOUND == queue->cs)
2045 {
2047 queue->address->sa_family, NULL);
2048 }
2049
2054 queue->initial_core_kx_done = GNUNET_YES;
2055
2056 {
2057 char *foreign_addr;
2058
2059 switch (queue->address->sa_family)
2060 {
2061 case AF_INET:
2062 GNUNET_asprintf (&foreign_addr,
2063 "%s-%s",
2065 GNUNET_a2s (queue->address, queue->address_len));
2066 break;
2067
2068 case AF_INET6:
2069 GNUNET_asprintf (&foreign_addr,
2070 "%s-%s",
2072 GNUNET_a2s (queue->address, queue->address_len));
2073 break;
2074
2075 default:
2076 GNUNET_assert (0);
2077 }
2079 &queue->target,
2080 foreign_addr,
2081 UINT16_MAX, /* no MTU */
2083 0, /* Priority */
2084 queue->nt,
2085 queue->cs,
2086 queue->mq);
2087
2088 GNUNET_free (foreign_addr);
2089 }
2090
2091 size = ntohs (hdr->size);
2092 break;
2094 /* Special case: header size excludes box itself! */
2095 box = (const struct TCPBox *) queue->pread_buf;
2096 if (ntohs (hdr->size) + sizeof(struct TCPBox) > queue->pread_off)
2097 return 0;
2098 calculate_hmac (&queue->in_hmac, &box[1], ntohs (hdr->size), &tmac);
2099 if (0 != memcmp (&tmac, &box->hmac, sizeof(tmac)))
2100 {
2101 GNUNET_break_op (0);
2103 return 0;
2104 }
2105 pass_plaintext_to_core (queue, (const void *) &box[1], ntohs (hdr->size));
2106 size = ntohs (hdr->size) + sizeof(*box);
2108 "Handling plaintext, box processed!\n");
2110 "# bytes decrypted with BOX",
2111 size,
2112 GNUNET_NO);
2114 "# messages decrypted with BOX",
2115 1,
2116 GNUNET_NO);
2117 break;
2118
2120 rekey = (const struct TCPRekey *) queue->pread_buf;
2121 if (sizeof(*rekey) > queue->pread_off)
2122 return 0;
2123 if (ntohs (hdr->size) != sizeof(*rekey))
2124 {
2125 GNUNET_break_op (0);
2127 return 0;
2128 }
2129 rekeyz = *rekey;
2130 memset (&rekeyz.hmac, 0, sizeof(rekeyz.hmac));
2131 calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
2132 if (0 != memcmp (&tmac, &rekey->hmac, sizeof(tmac)))
2133 {
2134 GNUNET_break_op (0);
2136 return 0;
2137 }
2138 do_rekey (queue, rekey);
2139 size = ntohs (hdr->size);
2141 "Handling plaintext, rekey processed!\n");
2143 "# rekeying successful",
2144 1,
2145 GNUNET_NO);
2146 break;
2147
2149 fin = (const struct TCPFinish *) queue->pread_buf;
2150 if (sizeof(*fin) > queue->pread_off)
2151 return 0;
2152 if (ntohs (hdr->size) != sizeof(*fin))
2153 {
2154 GNUNET_break_op (0);
2156 return 0;
2157 }
2158 finz = *fin;
2159 memset (&finz.hmac, 0, sizeof(finz.hmac));
2160 calculate_hmac (&queue->in_hmac, &finz, sizeof(finz), &tmac);
2161 if (0 != memcmp (&tmac, &fin->hmac, sizeof(tmac)))
2162 {
2163 GNUNET_break_op (0);
2165 return 0;
2166 }
2167 /* handle FINISH by destroying queue */
2170 "Handling plaintext, finish processed!\n");
2171 break;
2172
2173 default:
2175 "Handling plaintext, nothing processed!\n");
2176 GNUNET_break_op (0);
2178 return 0;
2179 }
2180 GNUNET_assert (0 != size);
2181 if (-1 != queue->unverified_size)
2182 queue->unverified_size += size;
2183 return size;
2184}

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, GNUNET_TRANSPORT_CommunicatorHandle::mq, 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 2321 of file gnunet-communicator-tcp.c.

2324{
2325 struct sockaddr *in;
2326
2327 v6.sin6_family = AF_INET6;
2328 v6.sin6_port = htons ((uint16_t) port);
2329#if HAVE_SOCKADDR_IN_SIN_LEN
2330 v6.sin6_len = sizeof(struct sockaddr_in6);
2331#endif
2332 v6.sin6_flowinfo = 0;
2333 v6.sin6_scope_id = 0;
2334 in = GNUNET_memdup (&v6, sizeof(v6));
2335 *sock_len = sizeof(struct sockaddr_in6);
2336
2337 return in;
2338}

References GNUNET_memdup, and port.

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

Here is the caller graph for this function:

◆ tcp_address_to_sockaddr_numeric_v4()

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

Convert a struct sockaddr_in4 to astruct sockaddr *`.

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

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

2352{
2353 struct sockaddr *in;
2354
2355 v4.sin_family = AF_INET;
2356 v4.sin_port = htons ((uint16_t) port);
2357#if HAVE_SOCKADDR_IN_SIN_LEN
2358 v4.sin_len = sizeof(struct sockaddr_in);
2359#endif
2360 in = GNUNET_memdup (&v4, sizeof(v4));
2361 *sock_len = sizeof(struct sockaddr_in);
2362 return in;
2363}

References GNUNET_memdup, and port.

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

Here is the caller graph for this function:

◆ tcp_address_to_sockaddr_port_only()

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

Convert TCP bind specification to a struct PortOnlyIpv4Ipv6 *

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

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

2374{
2375 struct PortOnlyIpv4Ipv6 *po;
2376 struct sockaddr_in *i4;
2377 struct sockaddr_in6 *i6;
2378 socklen_t sock_len_ipv4;
2379 socklen_t sock_len_ipv6;
2380
2381 /* interpreting value as just a PORT number */
2382 if (*port > UINT16_MAX)
2383 {
2385 "BINDTO specification `%s' invalid: value too large for port\n",
2386 bindto);
2387 return NULL;
2388 }
2389
2390 po = GNUNET_new (struct PortOnlyIpv4Ipv6);
2391
2392 if (GNUNET_YES == disable_v6)
2393 {
2394 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2395 po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2396 *port);
2397 po->addr_len_ipv4 = sock_len_ipv4;
2398 }
2399 else
2400 {
2401
2402 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2403 po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2404 *port);
2405 po->addr_len_ipv4 = sock_len_ipv4;
2406
2407 i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
2408 po->addr_ipv6 = tcp_address_to_sockaddr_numeric_v6 (&sock_len_ipv6, *i6,
2409 *port);
2410
2411 po->addr_len_ipv6 = sock_len_ipv6;
2412
2413 GNUNET_free (i6);
2414 }
2415
2416 GNUNET_free (i4);
2417
2418 return po;
2419}

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

2430{
2431 char *addr;
2432 char *start;
2433 char *token;
2434 char *cp;
2435 char *rest = NULL;
2436
2438 "extract address with bindto %s\n",
2439 bindto);
2440
2441 if (NULL == bindto)
2443 "bindto is NULL\n");
2444
2445 cp = GNUNET_strdup (bindto);
2446
2448 "extract address 2\n");
2449
2450 start = cp;
2451 if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
2452 {
2453 start++; /* skip over '['*/
2454 cp[strlen (cp) - 1] = '\0'; /* eat ']'*/
2455 addr = GNUNET_strdup (start);
2456 }
2457 else
2458 {
2459 token = strtok_r (cp, "]", &rest);
2460 if (strlen (bindto) == strlen (token))
2461 {
2462 token = strtok_r (cp, ":", &rest);
2463 addr = GNUNET_strdup (token);
2464 }
2465 else
2466 {
2467 token++;
2468 addr = GNUNET_strdup (token);
2469 }
2470 }
2471
2473 "tcp address: %s\n",
2474 addr);
2475 GNUNET_free (cp);
2476 return addr;
2477}

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

Referenced by run(), and tcp_address_to_sockaddr().

Here is the caller graph for this function:

◆ extract_port()

static unsigned int extract_port ( const char *  addr_and_port)
static

This Method extracts the port part of the BINDTO string.

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

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

2488{
2489 unsigned int port;
2490 char dummy[2];
2491 char *token;
2492 char *addr;
2493 char *colon;
2494 char *cp;
2495 char *rest = NULL;
2496
2497 if (NULL != addr_and_port)
2498 {
2499 cp = GNUNET_strdup (addr_and_port);
2500 token = strtok_r (cp, "]", &rest);
2501 if (strlen (addr_and_port) == strlen (token))
2502 {
2503 colon = strrchr (cp, ':');
2504 if (NULL == colon)
2505 {
2506 GNUNET_free (cp);
2507 return 0;
2508 }
2509 addr = colon;
2510 addr++;
2511 }
2512 else
2513 {
2514 token = strtok_r (NULL, "]", &rest);
2515 if (NULL == token)
2516 {
2517 GNUNET_free (cp);
2518 return 0;
2519 }
2520 else
2521 {
2522 addr = token;
2523 addr++;
2524 }
2525 }
2526
2527
2528 if (1 == sscanf (addr, "%u%1s", &port, dummy))
2529 {
2530 /* interpreting value as just a PORT number */
2531 if (port > UINT16_MAX)
2532 {
2534 "Port `%u' invalid: value too large for port\n",
2535 port);
2536 GNUNET_free (cp);
2537 return 0;
2538 }
2539 }
2540 else
2541 {
2543 "BINDTO specification invalid: last ':' not followed by number\n");
2544 GNUNET_free (cp);
2545 return 0;
2546 }
2547 GNUNET_free (cp);
2548 }
2549 else
2550 {
2552 "return 0\n");
2553 /* interpret missing port as 0, aka pick any free one */
2554 port = 0;
2555 }
2556
2557 return port;
2558}

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

Referenced by run(), and tcp_address_to_sockaddr().

Here is the caller graph for this function:

◆ tcp_address_to_sockaddr()

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

Convert TCP bind specification to a struct sockaddr *

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

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

2570{
2571 struct sockaddr *in;
2572 unsigned int port;
2573 struct sockaddr_in v4;
2574 struct sockaddr_in6 v6;
2575 char *start;
2576
2577 memset (&v4, 0, sizeof(v4));
2578 start = extract_address (bindto);
2579 GNUNET_assert (NULL != start);
2581 "start %s\n",
2582 start);
2583
2585 "!bindto %s\n",
2586 bindto);
2587
2588
2589 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
2590 {
2591 port = extract_port (bindto);
2592
2594 "port %u\n",
2595 port);
2596
2597 in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port);
2598 }
2599 else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
2600 {
2601 port = extract_port (bindto);
2602 in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port);
2603 }
2604 else
2605 {
2606 GNUNET_assert (0);
2607 }
2608
2610 return in;
2611}

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

2626{
2627 struct Queue *queue = impl_state;
2628 uint16_t msize = ntohs (msg->size);
2629 struct TCPBox box;
2631 "In MQ send. Queue finishing: %s; write task running: %s\n",
2632 (GNUNET_YES == queue->finishing) ? "yes" : "no",
2633 (NULL == queue->write_task) ? "yes" : "no");
2634 GNUNET_assert (mq == queue->mq);
2635 queue->mq_awaits_continue = GNUNET_YES;
2636 if (GNUNET_YES == queue->finishing)
2637 return; /* this queue is dying, drop msg */
2638 GNUNET_assert (0 == queue->pwrite_off);
2639 box.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX);
2640 box.header.size = htons (msize);
2641 calculate_hmac (&queue->out_hmac, msg, msize, &box.hmac);
2642 memcpy (&queue->pwrite_buf[queue->pwrite_off], &box, sizeof(box));
2643 queue->pwrite_off += sizeof(box);
2644 memcpy (&queue->pwrite_buf[queue->pwrite_off], msg, msize);
2645 queue->pwrite_off += msize;
2647 "%lu bytes of plaintext to send\n", queue->pwrite_off);
2648 GNUNET_assert (NULL != queue->sock);
2649 if (NULL == queue->write_task)
2650 queue->write_task =
2652 queue->sock,
2653 &queue_write,
2654 queue);
2655}

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

2668{
2669 struct Queue *queue = impl_state;
2670
2671 if (mq == queue->mq)
2672 {
2673 queue->mq = NULL;
2675 }
2676}

References mq, Queue::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 2686 of file gnunet-communicator-tcp.c.

2687{
2688 struct Queue *queue = impl_state;
2689
2690 GNUNET_assert (0 != queue->pwrite_off);
2691 queue->pwrite_off = 0;
2692}

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

2706{
2707 struct Queue *queue = cls;
2708
2710 "MQ error in queue to %s: %d\n",
2711 GNUNET_i2s (&queue->target),
2712 (int) error);
2714}

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

2726{
2727 queue->nt =
2728 GNUNET_NT_scanner_get_type (is, queue->address, queue->address_len);
2730 queue_map,
2731 &queue->key,
2732 queue,
2735 "# queues active",
2737 GNUNET_NO);
2738 queue->timeout =
2741 &mq_destroy,
2742 &mq_cancel,
2743 queue,
2744 NULL,
2745 &mq_error,
2746 queue);
2747}

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

2763{
2764 struct TcpHandshakeSignature ths;
2765 struct TCPConfirmation tc;
2766
2767 memcpy (queue->cwrite_buf, c, sizeof(*c));
2768 queue->cwrite_off = sizeof(*c);
2769 /* compute 'tc' and append in encrypted format to cwrite_buf */
2770 tc.sender = my_identity;
2771 tc.monotonic_time =
2774 &tc.challenge,
2775 sizeof(tc.challenge));
2776 ths.purpose.purpose = htonl (
2778 ths.purpose.size = htonl (sizeof(ths));
2779 ths.sender = my_identity;
2780 ths.receiver = queue->target;
2781 ths.ephemeral = *c;
2782 ths.monotonic_time = tc.monotonic_time;
2783 ths.challenge = tc.challenge;
2785 &ths,
2786 &tc.sender_sig);
2787 GNUNET_assert (0 ==
2788 gcry_cipher_encrypt (queue->out_cipher,
2789 &queue->cwrite_buf[queue->cwrite_off],
2790 sizeof(tc),
2791 &tc,
2792 sizeof(tc)));
2793 queue->challenge = tc.challenge;
2794 queue->cwrite_off += sizeof(tc);
2795
2797 "handshake written\n");
2798}

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

2810{
2812 struct GNUNET_ShortHashCode k;
2813
2815 &c, &k);
2816 setup_out_cipher (queue, &k);
2817 transmit_kx (queue, &c);
2818}

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

2828{
2829 struct Queue *queue = cls;
2830 if (GNUNET_OK != success)
2831 {
2833 "Failed to store handshake monotonic time in PEERSTORE!\n");
2834 }
2835 queue->handshake_monotime_sc = NULL;
2836 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
2837}

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

2851{
2852 struct Queue *queue = cls;
2853 struct GNUNET_TIME_AbsoluteNBO *mtbe;
2854 struct GNUNET_TIME_Absolute mt;
2855 const struct GNUNET_PeerIdentity *pid;
2856 struct GNUNET_TIME_AbsoluteNBO *handshake_monotonic_time;
2857
2858 (void) emsg;
2859
2860 handshake_monotonic_time = &queue->handshake_monotonic_time;
2861 pid = &queue->target;
2863 "tcp handshake with us %s\n",
2865 if (NULL == record)
2866 {
2867 queue->handshake_monotime_get = NULL;
2868 return;
2869 }
2871 "tcp handshake from peer %s\n",
2872 GNUNET_i2s (pid));
2873 if (sizeof(*mtbe) != record->value_size)
2874 {
2875 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
2876 GNUNET_break (0);
2877 return;
2878 }
2879 mtbe = record->value;
2880 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
2881 if (mt.abs_value_us > GNUNET_TIME_absolute_ntoh (
2882 queue->handshake_monotonic_time).abs_value_us)
2883 {
2885 "Queue from %s dropped, handshake monotime in the past\n",
2886 GNUNET_i2s (&queue->target));
2887 GNUNET_break (0);
2888 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
2889 queue->handshake_ack_monotime_get = NULL;
2891 return;
2892 }
2893 queue->handshake_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
2894 "transport_tcp_communicator",
2895 pid,
2897 handshake_monotonic_time,
2898 sizeof(*
2899 handshake_monotonic_time),
2902 &
2904 queue);
2905}

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

2923{
2924 struct TcpHandshakeSignature ths;
2926
2928 0 ==
2929 gcry_cipher_decrypt (queue->in_cipher,
2930 tc,
2931 sizeof(*tc),
2932 &ibuf[sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)],
2933 sizeof(*tc)));
2934 ths.purpose.purpose = htonl (
2936 ths.purpose.size = htonl (sizeof(ths));
2937 ths.sender = tc->sender;
2938 ths.receiver = my_identity;
2939 memcpy (&ths.ephemeral, ibuf, sizeof(struct GNUNET_CRYPTO_EcdhePublicKey));
2940 ths.monotonic_time = tc->monotonic_time;
2941 ths.challenge = tc->challenge;
2944 &ths,
2945 &tc->sender_sig,
2946 &tc->sender.public_key);
2947 if (GNUNET_YES == ret)
2948 queue->handshake_monotime_get =
2950 "transport_tcp_communicator",
2951 &queue->target,
2954 queue);
2955 return ret;
2956}

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

2968{
2969 struct Queue *queue = cls;
2970 ssize_t rcvd;
2971 struct GNUNET_TIME_Relative left;
2972 struct TCPConfirmation tc;
2973
2974 queue->read_task = NULL;
2975 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2976 if (0 == left.rel_value_us)
2977 {
2979 return;
2980 }
2981 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2982 &queue->cread_buf[queue->cread_off],
2983 BUF_SIZE - queue->cread_off);
2985 "Received %lu bytes to write in buffer of size %lu for KX from queue %p (expires in %"
2986 PRIu64 ")\n",
2987 rcvd, BUF_SIZE - queue->cread_off, queue, left.rel_value_us);
2988 if (-1 == rcvd)
2989 {
2990 if ((EAGAIN != errno) && (EINTR != errno))
2991 {
2994 return;
2995 }
2996 queue->read_task =
2998 return;
2999 }
3000 if (0 == rcvd)
3001 {
3002 /* Orderly shutdown of connection */
3004 "Socket for queue %p seems to have been closed\n", queue);
3006 return;
3007 }
3008 queue->cread_off += rcvd;
3009 if (queue->cread_off < INITIAL_KX_SIZE)
3010 {
3011 /* read more */
3013 "%lu/%lu bytes of KX read. Rescheduling...\n",
3014 queue->cread_off, INITIAL_KX_SIZE);
3015 queue->read_task =
3017 return;
3018 }
3019 /* we got all the data, let's find out who we are talking to! */
3021 (const struct GNUNET_CRYPTO_HpkeEncapsulation*)
3022 queue->cread_buf,
3023 queue);
3024 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, queue->cread_buf))
3025 {
3027 "Invalid TCP KX received from %s\n",
3028 GNUNET_a2s (queue->address, queue->address_len));
3030 return;
3031 }
3032 if (0 !=
3033 memcmp (&tc.sender, &queue->target, sizeof(struct GNUNET_PeerIdentity)))
3034 {
3036 "Invalid sender in TCP KX received from %s\n",
3037 GNUNET_a2s (queue->address, queue->address_len));
3039 return;
3040 }
3041 send_challenge (tc.challenge, queue);
3042 queue->write_task =
3044 queue->sock,
3045 &queue_write,
3046 queue);
3047
3048 /* update queue timeout */
3049 queue->timeout =
3051 /* prepare to continue with regular read task immediately */
3052 memmove (queue->cread_buf,
3053 &queue->cread_buf[INITIAL_KX_SIZE],
3054 queue->cread_off - (INITIAL_KX_SIZE));
3056 "cread_off is %lu bytes before adjusting\n",
3057 queue->cread_off);
3058 queue->cread_off -= INITIAL_KX_SIZE;
3060 "cread_off set to %lu bytes\n",
3061 queue->cread_off);
3063}

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

3074{
3075 struct ProtoQueue *pq = cls;
3076 ssize_t rcvd;
3077 struct GNUNET_TIME_Relative left;
3078 struct Queue *queue;
3079 struct TCPConfirmation tc;
3081
3082 pq->read_task = NULL;
3084 if (0 == left.rel_value_us)
3085 {
3086 free_proto_queue (pq);
3087 return;
3088 }
3089 rcvd = GNUNET_NETWORK_socket_recv (pq->sock,
3090 &pq->ibuf[pq->ibuf_off],
3091 sizeof(pq->ibuf) - pq->ibuf_off);
3093 "Proto received %lu bytes for KX\n", rcvd);
3094 if (-1 == rcvd)
3095 {
3096 if ((EAGAIN != errno) && (EINTR != errno))
3097 {
3099 free_proto_queue (pq);
3100 return;
3101 }
3102 /* try again */
3103 pq->read_task =
3105 return;
3106 }
3107 if (0 == rcvd)
3108 {
3109 /* Orderly shutdown of connection */
3111 "Socket for proto queue %p seems to have been closed\n", pq);
3112 free_proto_queue (pq);
3113 return;
3114 }
3115 pq->ibuf_off += rcvd;
3116 if (sizeof (struct TCPNATProbeMessage) == pq->ibuf_off)
3117 {
3118 struct TCPNATProbeMessage *pm = (struct TCPNATProbeMessage *) pq->ibuf;
3119
3121 &pm->clientIdentity);
3122
3123 queue = GNUNET_new (struct Queue);
3124 queue->target = pm->clientIdentity;
3125 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
3128 }
3129 else if (pq->ibuf_off > sizeof(pq->ibuf))
3130 {
3131 /* read more */
3132 pq->read_task =
3134 return;
3135 }
3136 else
3137 {
3138 /* we got all the data, let's find out who we are talking to! */
3139 queue = GNUNET_new (struct Queue);
3141 (const struct GNUNET_CRYPTO_HpkeEncapsulation *) pq->
3142 ibuf,
3143 queue);
3144 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, pq->ibuf))
3145 {
3147 "Invalid TCP KX received from %s\n",
3148 GNUNET_a2s (pq->address, pq->address_len));
3149 gcry_cipher_close (queue->in_cipher);
3151 free_proto_queue (pq);
3152 return;
3153 }
3154 queue->target = tc.sender;
3155 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
3158 }
3159 queue->address = pq->address; /* steals reference */
3160 queue->address_len = pq->address_len;
3161 queue->listen_sock = pq->listen_sock;
3162 queue->sock = pq->sock;
3163
3165 "created queue with target %s\n",
3166 GNUNET_i2s (&queue->target));
3167
3169 "start kx proto\n");
3170
3172 boot_queue (queue);
3173 queue->read_task =
3175 queue->sock,
3176 read_task,
3177 queue);
3178 queue->write_task =
3180 queue->sock,
3181 &queue_write,
3182 queue);
3183 // TODO To early! Move it somewhere else.
3184 // send_challenge (tc.challenge, queue);
3185 queue->challenge_received = tc.challenge;
3186
3188 GNUNET_free (pq);
3189}

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

3196{
3197 struct ProtoQueue *pq = GNUNET_new (struct ProtoQueue);
3198
3199 if (NULL == sock)
3200 {
3201 // sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
3202 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, 0);
3203 if (NULL == sock)
3204 {
3206 "socket(%d) failed: %s",
3207 in->sa_family,
3208 strerror (errno));
3209 GNUNET_free (in);
3210 GNUNET_free (pq);
3211 return NULL;
3212 }
3213 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, addrlen)) &&
3214 (errno != EINPROGRESS))
3215 {
3217 "connect to `%s' failed: %s",
3218 GNUNET_a2s (in, addrlen),
3219 strerror (errno));
3221 GNUNET_free (in);
3222 GNUNET_free (pq);
3223 return NULL;
3224 }
3225 }
3226 pq->address_len = addrlen;
3227 pq->address = in;
3229 pq->sock = sock;
3231 pq->sock,
3233 pq);
3235
3236 return pq;
3237}

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

Referenced by listen_cb(), and try_connection_reversal().

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

◆ try_connection_reversal()

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

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

3290{
3291 struct TCPNATProbeMessage pm;
3292 struct ProtoQueue *pq;
3293 struct sockaddr *in_addr;
3294 (void) cls;
3295
3297 "addr->sa_family %d\n",
3298 addr->sa_family);
3300 "Try to connect back\n");
3301 in_addr = GNUNET_memdup (addr, addrlen);
3303 "in_addr->sa_family %d\n",
3304 in_addr->sa_family);
3305 pq = create_proto_queue (NULL, in_addr, addrlen);
3306 if (NULL != pq)
3307 {
3308 pm.header.size = htons (sizeof(struct TCPNATProbeMessage));
3309 pm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
3310 pm.clientIdentity = my_identity;
3311 memcpy (pq->write_buf, &pm, sizeof(struct TCPNATProbeMessage));
3312 pq->write_off = sizeof(struct TCPNATProbeMessage);
3314 pq->sock,
3316 pq);
3317 }
3318 else
3319 {
3321 "Couldn't create ProtoQueue for sending TCPNATProbeMessage\n");
3322 }
3323}

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

Referenced by nat_register().

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

◆ pending_reversal_timeout()

static void pending_reversal_timeout ( void *  cls)
static

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

3328{
3329 struct sockaddr *in = cls;
3330 struct PendingReversal *pending_reversal;
3331 struct GNUNET_HashCode key;
3332
3334 sizeof(struct sockaddr),
3335 &key);
3337 &key);
3338
3339 GNUNET_assert (NULL != pending_reversal);
3340
3342 &key,
3343 pending_reversal))
3345 "No pending reversal found for address %s\n",
3346 GNUNET_a2s (in, sizeof (struct sockaddr)));
3347 GNUNET_free (pending_reversal->in);
3348 GNUNET_free (pending_reversal);
3349}

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

3372{
3373 struct sockaddr *in;
3374 socklen_t in_len = 0;
3375 const char *path;
3376 struct sockaddr_in *v4;
3377 struct sockaddr_in6 *v6;
3378 unsigned int is_natd = GNUNET_NO;
3379 struct GNUNET_HashCode key;
3380 struct GNUNET_HashCode queue_map_key;
3381 struct GNUNET_HashContext *hsh;
3382 struct Queue *queue;
3383
3385 "Connecting to %s at %s\n",
3386 GNUNET_i2s (peer),
3387 address);
3388 if (0 != strncmp (address,
3390 strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
3391 {
3392 GNUNET_break_op (0);
3393 return GNUNET_SYSERR;
3394 }
3395 path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
3396 in = tcp_address_to_sockaddr (path, &in_len);
3397
3398 if (NULL == in)
3399 {
3401 "Failed to setup TCP socket address\n");
3402 return GNUNET_SYSERR;
3403 }
3404
3406 "in %s\n",
3407 GNUNET_a2s (in, in_len));
3408
3411 GNUNET_CRYPTO_hash_context_read (hsh, peer, sizeof (*peer));
3412 GNUNET_CRYPTO_hash_context_finish (hsh, &queue_map_key);
3414
3415 if (NULL != queue)
3416 {
3418 "Queue for %s already exists or is in construction\n", address);
3419 GNUNET_free (in);
3420 return GNUNET_NO;
3421 }
3422 switch (in->sa_family)
3423 {
3424 case AF_INET:
3425 v4 = (struct sockaddr_in *) in;
3426 if (0 == v4->sin_port)
3427 {
3428 is_natd = GNUNET_YES;
3430 sizeof(struct sockaddr),
3431 &key);
3434 &key))
3435 {
3437 "There is already a request reversal for `%s'at `%s'\n",
3438 GNUNET_i2s (peer),
3439 address);
3440 GNUNET_free (in);
3441 return GNUNET_SYSERR;
3442 }
3443 }
3444 break;
3445
3446 case AF_INET6:
3447 if (GNUNET_YES == disable_v6)
3448 {
3450 "IPv6 disabled, skipping %s\n", address);
3451 GNUNET_free (in);
3452 return GNUNET_SYSERR;
3453 }
3454 v6 = (struct sockaddr_in6 *) in;
3455 if (0 == v6->sin6_port)
3456 {
3458 "Request reversal for `%s' at `%s' not possible for an IPv6 address\n",
3459 GNUNET_i2s (peer),
3460 address);
3461 GNUNET_free (in);
3462 return GNUNET_SYSERR;
3463 }
3464 break;
3465
3466 default:
3467 GNUNET_assert (0);
3468 }
3469
3470 if (GNUNET_YES == is_natd)
3471 {
3472 struct sockaddr_in local_sa;
3473 struct PendingReversal *pending_reversal;
3474
3475 memset (&local_sa, 0, sizeof(local_sa));
3476 local_sa.sin_family = AF_INET;
3477 local_sa.sin_port = htons (bind_port);
3478 /* We leave sin_address at 0, let the kernel figure it out,
3479 even if our bind() is more specific. (May want to reconsider
3480 later.) */
3481 if (GNUNET_OK != GNUNET_NAT_request_reversal (nat, &local_sa, v4))
3482 {
3484 "request reversal for `%s' at `%s' failed\n",
3485 GNUNET_i2s (peer),
3486 address);
3487 GNUNET_free (in);
3488 return GNUNET_SYSERR;
3489 }
3490 pending_reversal = GNUNET_new (struct PendingReversal);
3491 pending_reversal->in = in;
3494 &key,
3495 pending_reversal,
3497 pending_reversal->target = *peer;
3499 &
3501 in);
3503 "Created NAT WAIT connection to `%s' at `%s'\n",
3504 GNUNET_i2s (peer),
3505 GNUNET_a2s (in, sizeof (struct sockaddr)));
3506 }
3507 else
3508 {
3509 struct GNUNET_NETWORK_Handle *sock;
3510
3511 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM,
3512 IPPROTO_TCP);
3513 if (NULL == sock)
3514 {
3516 "socket(%d) failed: %s",
3517 in->sa_family,
3518 strerror (errno));
3519 GNUNET_free (in);
3520 return GNUNET_SYSERR;
3521 }
3522 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, in_len)) &&
3523 (errno != EINPROGRESS))
3524 {
3526 "connect to `%s' failed: %s",
3527 address,
3528 strerror (errno));
3530 GNUNET_free (in);
3531 return GNUNET_SYSERR;
3532 }
3533
3534 queue = GNUNET_new (struct Queue);
3535 queue->target = *peer;
3536 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
3537 queue->key = queue_map_key;
3538 queue->address = in;
3539 queue->address_len = in_len;
3540 queue->sock = sock;
3542 boot_queue (queue);
3544 "booted queue with target %s\n",
3545 GNUNET_i2s (&queue->target));
3546 // queue->mq_awaits_continue = GNUNET_YES;
3547 queue->read_task =
3549 queue->sock,
3551 queue);
3552
3553
3555 "start kx mq_init\n");
3556
3558 queue->write_task =
3560 queue->sock,
3561 &queue_write,
3562 queue);
3563 }
3564
3565 return GNUNET_OK;
3566}

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

3581{
3582 struct ListenTask *lt = value;
3583
3584 (void) cls;
3585 (void) key;
3586 if (NULL != lt->listen_task)
3587 {
3589 lt->listen_task = NULL;
3590 }
3591 if (NULL != lt->listen_sock)
3592 {
3594 lt->listen_sock = NULL;
3595 }
3596 GNUNET_free (lt);
3597 return GNUNET_OK;
3598}

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

3613{
3614 struct Queue *queue = value;
3615
3616 (void) cls;
3617 (void) target;
3619 return GNUNET_OK;
3620}

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

3630{
3632 "Shutdown %s!\n",
3633 shutdown_running ? "running" : "not running");
3634
3636 return;
3637 else
3639
3640 while (NULL != proto_head)
3642 if (NULL != nat)
3643 {
3645 nat = NULL;
3646 }
3654 if (NULL != ch)
3655 {
3658 ch = NULL;
3659 }
3660 if (NULL != stats)
3661 {
3663 stats = NULL;
3664 }
3665 if (NULL != my_private_key)
3666 {
3668 my_private_key = NULL;
3669 }
3670 if (NULL != is)
3671 {
3673 is = NULL;
3674 }
3675 if (NULL != pils)
3676 {
3678 pils = NULL;
3679 }
3680 if (NULL != peerstore)
3681 {
3683 peerstore = NULL;
3684 }
3685 if (NULL != resolve_request_handle)
3686 {
3689 }
3691 "Shutdown done!\n");
3692}

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.

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

3710{
3711 (void) cls;
3712 (void) sender;
3713 (void) msg;
3714 GNUNET_break_op (0);
3715}

References GNUNET_break_op, and msg.

Referenced by init_socket().

Here is the caller graph for this function:

◆ nat_address_cb()

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

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

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

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

3738{
3739 char *my_addr;
3741
3743 "nat address cb %s %s\n",
3744 add_remove ? "add" : "remove",
3745 GNUNET_a2s (addr, addrlen));
3746
3747 if (GNUNET_YES == add_remove)
3748 {
3750
3751 GNUNET_asprintf (&my_addr,
3752 "%s-%s",
3754 GNUNET_a2s (addr, addrlen));
3755 nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
3756 ai =
3758 my_addr,
3759 nt,
3761 GNUNET_free (my_addr);
3762 *app_ctx = ai;
3763 }
3764 else
3765 {
3766 ai = *app_ctx;
3768 *app_ctx = NULL;
3769 }
3770}

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

Referenced by nat_register().

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

◆ add_addr()

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

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

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

3778{
3779
3780 struct Addresses *saddrs;
3781
3783 "add address %s\n",
3784 GNUNET_a2s (in, in_len));
3785
3786 saddrs = GNUNET_new (struct Addresses);
3787 saddrs->addr = in;
3788 saddrs->addr_len = in_len;
3790
3792 "after add address %s\n",
3793 GNUNET_a2s (in, in_len));
3794
3796 "add address %s\n",
3797 GNUNET_a2s (saddrs->addr, saddrs->addr_len));
3798
3799 addrs_lens++;
3800}

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

3818{
3819 char *filename;
3820 struct GNUNET_DISK_FileHandle *filehandle;
3821 int ret;
3822
3823 if (GNUNET_OK !=
3825 "pils",
3826 "SECRET_KEY_FILE",
3827 &filename))
3828 {
3830 "PILS service is lacking initial secret key file configuration setting. Exiting\n");
3831 return GNUNET_SYSERR;
3832 }
3833 if (NULL == filename)
3834 return GNUNET_SYSERR;
3836 if (GNUNET_SYSERR == ret)
3837 return GNUNET_SYSERR;
3838 if (GNUNET_NO == ret)
3839 {
3840 /* File does not exist - generate a new initial secret key and save it */
3841 // TODO consider the case that the file exists and ist not readable
3843 ikm,
3844 sizeof ikm);
3846 {
3848 "PILS service cannot create dir for saving initial secret key file. Exiting\n");
3849 return GNUNET_SYSERR;
3850 }
3851 filehandle = GNUNET_DISK_file_open (filename,
3855 | // would
3856 // the
3857 // group
3858 // need
3859 // read
3860 // perm?
3862 if (NULL == filehandle)
3863 {
3865 "PILS service had an issue with opening the initial secret key file. Exiting\n");
3866 GNUNET_DISK_file_close (filehandle);
3867 return GNUNET_SYSERR;
3868 }
3869 ret = GNUNET_DISK_file_write (filehandle,
3870 ikm,
3871 sizeof ikm);
3872 GNUNET_DISK_file_close (filehandle);
3873 if (sizeof ikm != ret)
3874 {
3876 "PILS service had an issue with writing the initial secret key to file. Exiting\n")
3877 ;
3878 return GNUNET_SYSERR;
3879 }
3880 }
3881 else
3882 {
3883 /* File existes - just read from it */
3884 off_t size;
3886 "PILS is going to read initial secret key from file %s\n",
3887 filename);
3888 filehandle = GNUNET_DISK_file_open (filename,
3891 if (NULL == filehandle)
3892 {
3894 " Not able to open file\n");
3895 return GNUNET_SYSERR;
3896 }
3897 if (GNUNET_OK != GNUNET_DISK_file_handle_size (filehandle, &size))
3898 {
3900 " File has the wrong size %lu\n",
3901 size);
3902 GNUNET_DISK_file_close (filehandle);
3903 return GNUNET_SYSERR;
3904 }
3905 if (sizeof ikm != size)
3906 {
3908 " Something is wrong with the file size, expected: %lu size, got: %lu\n",
3909 size,
3910 sizeof ikm);
3911 GNUNET_DISK_file_close (filehandle);
3912 return GNUNET_SYSERR;
3913 }
3914 ret = GNUNET_DISK_file_read (filehandle,
3915 ikm,
3916 sizeof ikm);
3917 GNUNET_DISK_file_close (filehandle);
3918 if (sizeof ikm != ret)
3919 {
3921 " Read initial secret key with wrong size %u, expected %lu\n", ret,
3922 sizeof ikm);
3923 return GNUNET_SYSERR;
3924 }
3925
3926 }
3927 return GNUNET_OK;
3928}

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

3941{
3942 struct sockaddr_storage in_sto;
3943 socklen_t sto_len;
3944 struct GNUNET_NETWORK_Handle *listen_sock;
3945 struct ListenTask *lt;
3946 int sockfd;
3947 struct GNUNET_HashCode h_sock;
3948
3949 if (NULL == addr)
3950 {
3952 "Address is NULL.\n");
3953 return GNUNET_SYSERR;
3954 }
3955
3957 "address %s\n",
3958 GNUNET_a2s (addr, in_len));
3959
3960 listen_sock =
3961 GNUNET_NETWORK_socket_create (addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
3962 if (NULL == listen_sock)
3963 {
3965 return GNUNET_SYSERR;
3966 }
3967
3968 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, addr, in_len))
3969 {
3971 GNUNET_NETWORK_socket_close (listen_sock);
3972 listen_sock = NULL;
3973 return GNUNET_SYSERR;
3974 }
3975
3976 if (GNUNET_OK !=
3977 GNUNET_NETWORK_socket_listen (listen_sock,
3978 5))
3979 {
3981 "listen");
3982 GNUNET_NETWORK_socket_close (listen_sock);
3983 listen_sock = NULL;
3984 return GNUNET_SYSERR;
3985 }
3986
3987 /* We might have bound to port 0, allowing the OS to figure it out;
3988 thus, get the real IN-address from the socket */
3989 sto_len = sizeof(in_sto);
3990
3991 if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock),
3992 (struct sockaddr *) &in_sto,
3993 &sto_len))
3994 {
3995 memcpy (&in_sto, addr, in_len);
3996 sto_len = in_len;
3997 }
3998
3999 // addr = (struct sockaddr *) &in_sto;
4000 in_len = sto_len;
4002 "Bound to `%s'\n",
4003 GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
4004 if (NULL == stats)
4005 stats = GNUNET_STATISTICS_create ("communicator-tcp", cfg);
4006
4007 if (NULL == is)
4009
4010 /* start listening */
4011
4012 lt = GNUNET_new (struct ListenTask);
4013 lt->listen_sock = listen_sock;
4014
4016 listen_sock,
4017 &listen_cb,
4018 lt);
4019
4021 "creating hash\n");
4022 sockfd = GNUNET_NETWORK_get_fd (lt->listen_sock);
4023 GNUNET_CRYPTO_hash (&sockfd,
4024 sizeof(int),
4025 &h_sock);
4026
4028 "creating map\n");
4029 if (NULL == lt_map)
4031
4033 "creating map entry\n");
4036 &h_sock,
4037 lt,
4039
4041 "map entry created\n");
4042
4043 if (NULL == queue_map)
4045
4046 if (NULL == ch)
4051 &mq_init,
4052 NULL,
4054 NULL,
4055 NULL);
4056
4057 if (NULL == ch)
4058 {
4059 GNUNET_break (0);
4060 if (NULL != resolve_request_handle)
4063 return GNUNET_SYSERR;
4064 }
4065
4066 add_addr (addr, in_len);
4067 return GNUNET_OK;
4068
4069}

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 init_socket_resolv(), and run().

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

◆ nat_register()

static void nat_register ( )
static

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

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

4077{
4078 struct sockaddr **saddrs;
4079 socklen_t *saddr_lens;
4080 int i;
4081 size_t len;
4082
4084 "starting nat register!\n");
4085 len = 0;
4086 i = 0;
4087 saddrs = GNUNET_malloc ((addrs_lens) * sizeof(struct sockaddr *));
4088 saddr_lens = GNUNET_malloc ((addrs_lens) * sizeof(socklen_t));
4089 for (struct Addresses *pos = addrs_head; NULL != pos; pos = pos->next)
4090 {
4092 "registering address %s\n",
4093 GNUNET_a2s (pos->addr, pos->addr_len));
4094
4095 saddr_lens[i] = pos->addr_len;
4096 len += saddr_lens[i];
4097 saddrs[i] = GNUNET_memdup (pos->addr, saddr_lens[i]);
4098 i++;
4099 }
4100
4102 "registering addresses %lu %lu %lu %lu\n",
4103 (addrs_lens) * sizeof(struct sockaddr *),
4104 (addrs_lens) * sizeof(socklen_t),
4105 len,
4109 IPPROTO_TCP,
4110 addrs_lens,
4111 (const struct sockaddr **) saddrs,
4112 saddr_lens,
4115 NULL /* closure */);
4116 for (i = addrs_lens - 1; i >= 0; i--)
4117 GNUNET_free (saddrs[i]);
4118 GNUNET_free (saddrs);
4119 GNUNET_free (saddr_lens);
4120
4121 if (NULL == nat)
4122 {
4123 GNUNET_break (0);
4124 if (NULL != resolve_request_handle)
4127 }
4128}

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

Referenced by init_socket_resolv(), and run().

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

◆ init_socket_resolv()

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

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

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

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

4142{
4143 struct sockaddr_in *v4;
4144 struct sockaddr_in6 *v6;
4145 struct sockaddr *in;
4146
4147 (void) cls;
4148 if (NULL != addr)
4149 {
4150 if (AF_INET == addr->sa_family)
4151 {
4152 v4 = (struct sockaddr_in *) addr;
4153 in = tcp_address_to_sockaddr_numeric_v4 (&in_len, *v4, bind_port);// _global);
4154 }
4155 else if (AF_INET6 == addr->sa_family)
4156 {
4157 v6 = (struct sockaddr_in6 *) addr;
4158 in = tcp_address_to_sockaddr_numeric_v6 (&in_len, *v6, bind_port);// _global);
4159 }
4160 else
4161 {
4163 "Address family %u not suitable (not AF_INET %u nor AF_INET6 %u \n",
4164 addr->sa_family,
4165 AF_INET,
4166 AF_INET6);
4167 return;
4168 }
4169 init_socket (in, in_len);
4170 }
4171 else
4172 {
4174 "Address is NULL. This might be an error or the resolver finished resolving.\n");
4175 if (NULL == addrs_head)
4176 {
4178 "Resolver finished resolving, but we do not listen to an address!.\n");
4179 return;
4180 }
4181 nat_register ();
4182 }
4183}

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

Referenced by run().

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

◆ pid_change_cb()

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

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

4190{
4192 "Got PID to derive from `%s':\n",
4193 GNUNET_h2s (addr_hash));
4194 if (NULL == my_private_key)
4196
4198 (uint8_t*) ikm,
4199 addr_hash,
4205}

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

4221{
4222 char *bindto;
4223 struct sockaddr *in;
4224 socklen_t in_len;
4225 struct sockaddr_in v4;
4226 struct sockaddr_in6 v6;
4227 char *start;
4228 unsigned int port;
4229 char dummy[2];
4230 char *rest = NULL;
4231 struct PortOnlyIpv4Ipv6 *po;
4232 socklen_t addr_len_ipv4;
4233 socklen_t addr_len_ipv6;
4234
4235 (void) cls;
4236
4238 memset (&v4,0,sizeof(struct sockaddr_in));
4239 memset (&v6,0,sizeof(struct sockaddr_in6));
4240 cfg = c;
4241 if (GNUNET_OK !=
4244 "BINDTO",
4245 &bindto))
4246 {
4249 "BINDTO");
4250 return;
4251 }
4252 if (GNUNET_OK !=
4255 "MAX_QUEUE_LENGTH",
4257 {
4259 }
4260 if (GNUNET_OK !=
4263 "REKEY_INTERVAL",
4265 {
4267 }
4268 if (GNUNET_OK !=
4271 "REKEY_MAX_BYTES",
4273 {
4275 }
4277 if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
4278 (GNUNET_YES ==
4281 "DISABLE_V6")))
4282 {
4284 }
4285 load_ikm ();
4287 GNUNET_assert (NULL != pils);
4289 if (NULL == peerstore)
4290 {
4291 GNUNET_free (bindto);
4292 GNUNET_break (0);
4294 return;
4295 }
4296
4298
4299 if (1 == sscanf (bindto, "%u%1s", &bind_port, dummy))
4300 {
4304 "address po %s\n",
4306 if (NULL != po->addr_ipv4)
4307 {
4309 }
4310 if (NULL != po->addr_ipv6)
4311 {
4314 }
4315 GNUNET_free (po);
4316 nat_register ();
4317 GNUNET_free (bindto);
4318 return;
4319 }
4320
4321 start = extract_address (bindto);
4322 // FIXME: check for NULL == start...
4323 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
4324 {
4325 bind_port = extract_port (bindto);
4326
4328 init_socket (in, in_len);
4329 nat_register ();
4331 GNUNET_free (bindto);
4332 return;
4333 }
4334
4335 if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
4336 {
4337 bind_port = extract_port (bindto);
4339 init_socket (in, in_len);
4340 nat_register ();
4342 GNUNET_free (bindto);
4343 return;
4344 }
4345
4346 bind_port = extract_port (bindto);
4348 ":",
4349 &rest),
4350 AF_UNSPEC,
4353 &port);
4354
4355 GNUNET_free (bindto);
4357}

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

Referenced by main().

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

◆ main()

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

The main function for the UNIX communicator.

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

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

4369{
4370 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
4372 };
4373 int ret;
4374
4376 "Starting tcp communicator\n");
4377
4378 ret = (GNUNET_OK ==
4380 argc,
4381 argv,
4382 "gnunet-communicator-tcp",
4383 _ ("GNUnet TCP communicator"),
4384 options,
4385 &run,
4386 NULL))
4387 ? 0
4388 : 1;
4389 return ret;
4390}

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

◆ max_queue_length

unsigned long long max_queue_length
static

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

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

Referenced by queue_read(), queue_read(), and run().

◆ pils

struct GNUNET_PILS_Handle* pils
static

For PILS.

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

Referenced by do_shutdown(), and run().

◆ stats

◆ ch

◆ queue_map

struct GNUNET_CONTAINER_MultiHashMap* queue_map
static

Queues (map from peer identity to struct Queue)

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

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

◆ lt_map

struct GNUNET_CONTAINER_MultiHashMap* lt_map
static

ListenTasks (map from socket to struct ListenTask)

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

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

◆ my_identity

◆ rekey_max_bytes

unsigned long long rekey_max_bytes
static

The rekey byte maximum.

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

Referenced by run(), setup_out_cipher(), and setup_out_cipher().

◆ rekey_interval

struct GNUNET_TIME_Relative rekey_interval
static

The rekey interval.

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

Referenced by run(), run(), setup_out_cipher(), and setup_out_cipher().

◆ my_private_key

struct GNUNET_CRYPTO_EddsaPrivateKey* my_private_key
static

Our private key.

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

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

◆ my_x25519_private_key

struct GNUNET_CRYPTO_EcdhePrivateKey my_x25519_private_key
static

Our private key.

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

Referenced by pid_change_cb(), and setup_in_cipher_elligator().

◆ cfg

const struct GNUNET_CONFIGURATION_Handle* cfg
static

Our configuration.

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

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

◆ is

struct GNUNET_NT_InterfaceScanner* is
static

Network scanner to determine network types.

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

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

◆ nat

struct GNUNET_NAT_Handle* nat
static

Connection to NAT service.

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

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

◆ proto_head

struct ProtoQueue* proto_head
static

◆ proto_tail

struct ProtoQueue* proto_tail
static

◆ resolve_request_handle

struct GNUNET_RESOLVER_RequestHandle* resolve_request_handle

Handle for DNS lookup of bindto address.

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

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

◆ addrs_head

struct Addresses* addrs_head
static

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

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

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

◆ addrs_tail

struct Addresses* addrs_tail
static

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

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

Referenced by add_addr().

◆ addrs_lens

int addrs_lens
static

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

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

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

◆ peerstore

◆ shutdown_running

int shutdown_running = GNUNET_NO
static

A flag indicating we are already doing a shutdown.

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

Referenced by do_shutdown(), and queue_destroy().

◆ disable_v6

int disable_v6
static

IPv6 disabled.

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

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

◆ bind_port

unsigned int bind_port
static

The port the communicator should be assigned to.

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

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

◆ pending_reversals

◆ ikm

unsigned char ikm[256/8]
static

The initial key material for the peer.

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

Referenced by load_ikm(), and pid_change_cb().