GNUnet 0.22.2
gnunet-communicator-tcp.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet
3 Copyright (C) 2010-2014, 2018, 2019 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
30#include "platform.h"
31#include "gnunet_common.h"
32#include "gnunet_util_lib.h"
33#include "gnunet_core_service.h"
35#include "gnunet_protocols.h"
36#include "gnunet_signatures.h"
37#include "gnunet_constants.h"
38#include "gnunet_nat_service.h"
42
43
48#define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
49
54#define ADDRESS_VALIDITY_PERIOD \
55 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
56
65#define DEFAULT_MAX_QUEUE_LENGTH 8
66
71#define BUF_SIZE (2 * 64 * 1024 + sizeof(struct TCPBox))
72
76#define DEFAULT_REKEY_INTERVAL GNUNET_TIME_UNIT_DAYS
77
81#define PROTO_QUEUE_TIMEOUT GNUNET_TIME_UNIT_MINUTES
82
87#define REKEY_MAX_BYTES (1024LLU * 1024 * 400)
88
93#define INITIAL_KX_SIZE \
94 (sizeof(struct GNUNET_CRYPTO_EcdhePublicKey) \
95 + sizeof(struct TCPConfirmation))
96
100#define INITIAL_CORE_KX_SIZE \
101 (sizeof(struct EphemeralKeyMessage) \
102 + sizeof(struct PingMessage) \
103 + sizeof(struct PongMessage))
104
108#define COMMUNICATOR_ADDRESS_PREFIX "tcp"
109
113#define COMMUNICATOR_CONFIG_SECTION "communicator-tcp"
114
116
117
123{
128
133
138
143
149
154};
155
161{
166
171
176
182
187};
188
193{
198
203
209
214
215};
216
221{
222
223
228
233
238
244
249
250};
251
255struct TCPBox
256{
264
274
275 /* followed by as may bytes of payload as indicated in @e header,
276 excluding the TCPBox itself! */
277};
278
279
285{
290
300
305
310
316};
317
323{
328
333
338
343
349};
350
357{
362
372};
373
379{
384
389};
390
392
397{
398 /*
399 * Timeout task.
400 */
402
407
411 struct sockaddr *in;
412};
413
418{
423
428};
429
433struct Queue
434{
439
444
449
454
458 gcry_cipher_hd_t in_cipher;
459
463 gcry_cipher_hd_t out_cipher;
464
469
474
480
485
490
494 struct sockaddr *address;
495
501
507
511 socklen_t address_len;
512
517
522
526 unsigned long long bytes_in_queue;
527
532
537
541 char pread_buf[UINT16_MAX + 1 + sizeof(struct TCPBox)];
542
546 char pwrite_buf[UINT16_MAX + 1 + sizeof(struct TCPBox)];
547
552 size_t cread_off;
553
559
564 size_t pread_off;
565
571
576
584 unsigned int backpressure;
585
590
595
600
605
613
619
624
629
634
639
644
649
654
659
664
669
674
678 // TODO remove?
680
685};
686
687
693{
698
703
708
713
718
722 char write_buf[sizeof (struct TCPNATProbeMessage)];
723
727 size_t write_off;
728
733
737 struct sockaddr *address;
738
742 socklen_t address_len;
743
748
754
758 size_t ibuf_off;
759};
760
765{
769 struct sockaddr *addr_ipv4;
770
774 socklen_t addr_len_ipv4;
775
779 struct sockaddr *addr_ipv6;
780
784 socklen_t addr_len_ipv6;
785
786};
787
792{
797
802
806 struct sockaddr *addr;
807
811 socklen_t addr_len;
812
813};
814
815
819static unsigned long long max_queue_length;
820
825
830
835
840
845
849static unsigned long long rekey_max_bytes;
850
855
860
865
869static const struct GNUNET_CONFIGURATION_Handle *cfg;
870
875
879static struct GNUNET_NAT_Handle *nat;
880
884static struct ProtoQueue *proto_head;
885
889static struct ProtoQueue *proto_tail;
890
895
899static struct Addresses *addrs_head;
900
904static struct Addresses *addrs_tail;
905
909static int addrs_lens;
910
915
920
924static int disable_v6;
925
929static unsigned int bind_port;
930
935
943static void
944listen_cb (void *cls);
945
946static void
949{
951 key.type = htonl (GNUNET_PUBLIC_KEY_TYPE_EDDSA);
952 key.eddsa_key = *edpk;
954}
955
956
957static void
960{
962 key.type = htonl (GNUNET_PUBLIC_KEY_TYPE_EDDSA);
963 key.eddsa_key = *edpk;
965}
966
967
975static void
977{
978 struct ListenTask *lt = NULL;
979 struct GNUNET_HashCode h_sock;
980 int sockfd;
981
982 if (NULL != queue->listen_sock)
983 {
984 sockfd = GNUNET_NETWORK_get_fd (queue->listen_sock);
985 GNUNET_CRYPTO_hash (&sockfd,
986 sizeof(int),
987 &h_sock);
988
990 }
991
993 "Disconnecting queue for peer `%s'\n",
994 GNUNET_i2s (&queue->target));
995 if (NULL != queue->rekey_monotime_sc)
996 {
997 GNUNET_PEERSTORE_store_cancel (queue->rekey_monotime_sc);
998 queue->rekey_monotime_sc = NULL;
999 }
1000 if (NULL != queue->handshake_monotime_sc)
1001 {
1002 GNUNET_PEERSTORE_store_cancel (queue->handshake_monotime_sc);
1003 queue->handshake_monotime_sc = NULL;
1004 }
1005 if (NULL != queue->handshake_ack_monotime_sc)
1006 {
1007 GNUNET_PEERSTORE_store_cancel (queue->handshake_ack_monotime_sc);
1008 queue->handshake_ack_monotime_sc = NULL;
1009 }
1010 if (NULL != queue->rekey_monotime_get)
1011 {
1012 GNUNET_PEERSTORE_iteration_stop (queue->rekey_monotime_get);
1013 queue->rekey_monotime_get = NULL;
1014 }
1015 if (NULL != queue->handshake_monotime_get)
1016 {
1017 GNUNET_PEERSTORE_iteration_stop (queue->handshake_monotime_get);
1018 queue->handshake_monotime_get = NULL;
1019 }
1020 if (NULL != queue->handshake_ack_monotime_get)
1021 {
1022 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
1023 queue->handshake_ack_monotime_get = NULL;
1024 }
1025 if (NULL != queue->qh)
1026 {
1028 queue->qh = NULL;
1029 }
1031 GNUNET_YES ==
1034 "# queues active",
1036 GNUNET_NO);
1037 if (NULL != queue->read_task)
1038 {
1039 GNUNET_SCHEDULER_cancel (queue->read_task);
1040 queue->read_task = NULL;
1041 }
1042 if (NULL != queue->write_task)
1043 {
1044 GNUNET_SCHEDULER_cancel (queue->write_task);
1045 queue->write_task = NULL;
1046 }
1048 {
1050 "closing socket failed\n");
1051 }
1052 gcry_cipher_close (queue->in_cipher);
1053 gcry_cipher_close (queue->out_cipher);
1054 GNUNET_free (queue->address);
1055 if (0 != queue->backpressure)
1056 queue->destroyed = GNUNET_YES;
1057 else
1059
1060 if (NULL == lt)
1061 return;
1062
1063 if ((! shutdown_running) && (NULL == lt->listen_task))
1064 {
1066 "add read net listen\n");
1069 lt->listen_sock,
1070 &listen_cb,
1071 lt);
1072 }
1073 else
1074 GNUNET_free (lt);
1075}
1076
1077
1086static void
1087calculate_hmac (struct GNUNET_HashCode *hmac_secret,
1088 const void *buf,
1089 size_t buf_size,
1090 struct GNUNET_ShortHashCode *smac)
1091{
1092 struct GNUNET_HashCode mac;
1093
1094 GNUNET_CRYPTO_hmac_raw (hmac_secret,
1095 sizeof(struct GNUNET_HashCode),
1096 buf,
1097 buf_size,
1098 &mac);
1099 /* truncate to `struct GNUNET_ShortHashCode` */
1100 memcpy (smac, &mac, sizeof(struct GNUNET_ShortHashCode));
1101 /* ratchet hmac key */
1102 GNUNET_CRYPTO_hash (hmac_secret,
1103 sizeof(struct GNUNET_HashCode),
1104 hmac_secret);
1105}
1106
1107
1114static void
1116{
1117 struct TCPFinish fin;
1118
1119 memset (&fin, 0, sizeof(fin));
1120 fin.header.size = htons (sizeof(fin));
1122 calculate_hmac (&queue->out_hmac, &fin, sizeof(fin), &fin.hmac);
1123 /* if there is any message left in pwrite_buf, we
1124 overwrite it (possibly dropping the last message
1125 from CORE hard here) */
1126 memcpy (queue->pwrite_buf, &fin, sizeof(fin));
1127 queue->pwrite_off = sizeof(fin);
1128 /* This flag will ensure that #queue_write() no longer
1129 notifies CORE about the possibility of sending
1130 more data, and that #queue_write() will call
1131 #queue_destroy() once the @c fin was fully written. */
1132 queue->finishing = GNUNET_YES;
1133}
1134
1135
1141static void
1142queue_read (void *cls);
1143
1144
1152static void
1153core_read_finished_cb (void *cls, int success)
1154{
1155 struct Queue *queue = cls;
1156 if (GNUNET_OK != success)
1158 "# messages lost in communicator API towards CORE",
1159 1,
1160 GNUNET_NO);
1161 if (NULL == queue)
1162 return;
1163
1165 "backpressure %u\n",
1166 queue->backpressure);
1167
1168 queue->backpressure--;
1169 /* handle deferred queue destruction */
1170 if ((queue->destroyed) && (0 == queue->backpressure))
1171 {
1173 return;
1174 }
1175 else if (GNUNET_YES != queue->destroyed)
1176 {
1177 queue->timeout =
1179 );
1180 /* possibly unchoke reading, now that CORE made progress */
1181 if (NULL == queue->read_task)
1182 queue->read_task =
1184 queue->timeout),
1185 queue->sock,
1186 &queue_read,
1187 queue);
1188 }
1189}
1190
1191
1201static void
1203 const void *plaintext,
1204 size_t plaintext_len)
1205{
1206 const struct GNUNET_MessageHeader *hdr = plaintext;
1207 int ret;
1208
1210 "pass message from %s to core\n",
1211 GNUNET_i2s (&queue->target));
1212
1213 if (ntohs (hdr->size) != plaintext_len)
1214 {
1215 /* NOTE: If we ever allow multiple CORE messages in one
1216 BOX, this will have to change! */
1217 GNUNET_break (0);
1218 return;
1219 }
1221 &queue->target,
1222 hdr,
1225 queue);
1227 "passed to core\n");
1228 if (GNUNET_OK == ret)
1229 queue->backpressure++;
1230 GNUNET_break (GNUNET_NO != ret); /* backpressure not working!? */
1231 if (GNUNET_SYSERR == ret)
1233 "# bytes lost due to CORE not running",
1234 plaintext_len,
1235 GNUNET_NO);
1236}
1237
1238
1248static void
1250 const struct GNUNET_PeerIdentity *pid,
1251 gcry_cipher_hd_t *cipher,
1252 struct GNUNET_HashCode *hmac_key)
1253{
1254 char key[256 / 8];
1255 char ctr[128 / 8];
1256
1257 GNUNET_assert (0 == gcry_cipher_open (cipher,
1258 GCRY_CIPHER_AES256 /* low level: go for speed */
1259 ,
1260 GCRY_CIPHER_MODE_CTR,
1261 0 /* flags */));
1264 sizeof(key),
1265 prk,
1266 "gnunet-communicator-tcp-key",
1267 strlen (
1268 "gnunet-communicator-tcp-key"),
1269 NULL,
1270 0));
1271 GNUNET_assert (0 == gcry_cipher_setkey (*cipher, key, sizeof(key)));
1274 sizeof(ctr),
1275 prk,
1276 "gnunet-communicator-tcp-ctr",
1277 strlen (
1278 "gnunet-communicator-tcp-ctr"),
1279 NULL,
1280 0));
1281 gcry_cipher_setctr (*cipher, ctr, sizeof(ctr));
1283 GNUNET_CRYPTO_hkdf_expand (hmac_key,
1284 sizeof(struct GNUNET_HashCode),
1285 prk,
1286 "gnunet-communicator-hmac",
1287 strlen ("gnunet-communicator-hmac"),
1288 NULL,
1289 0));
1290}
1291
1292
1298static void
1299rekey_monotime_store_cb (void *cls, int success)
1300{
1301 struct Queue *queue = cls;
1302 if (GNUNET_OK != success)
1303 {
1305 "Failed to store rekey monotonic time in PEERSTORE!\n");
1306 }
1307 queue->rekey_monotime_sc = NULL;
1308 GNUNET_PEERSTORE_iteration_next (queue->rekey_monotime_get, 1);
1309}
1310
1311
1319static void
1321 const struct GNUNET_PEERSTORE_Record *record,
1322 const char *emsg)
1323{
1324 struct Queue *queue = cls;
1325 struct GNUNET_TIME_AbsoluteNBO *mtbe;
1326 struct GNUNET_TIME_Absolute mt;
1327 const struct GNUNET_PeerIdentity *pid;
1328 struct GNUNET_TIME_AbsoluteNBO *rekey_monotonic_time;
1329
1330 (void) emsg;
1331
1332 rekey_monotonic_time = &queue->rekey_monotonic_time;
1333 pid = &queue->target;
1334 if (NULL == record)
1335 {
1336 queue->rekey_monotime_get = NULL;
1337 return;
1338 }
1339 if (sizeof(*mtbe) != record->value_size)
1340 {
1341 GNUNET_PEERSTORE_iteration_next (queue->rekey_monotime_get, 1);
1342 GNUNET_break (0);
1343 return;
1344 }
1345 mtbe = record->value;
1346 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1348 queue->rekey_monotonic_time).abs_value_us)
1349 {
1351 "Queue from %s dropped, rekey monotime in the past\n",
1352 GNUNET_i2s (&queue->target));
1353 GNUNET_break (0);
1354 GNUNET_PEERSTORE_iteration_stop (queue->rekey_monotime_get);
1355 queue->rekey_monotime_get = NULL;
1356 // FIXME: Why should we try to gracefully finish here??
1358 return;
1359 }
1360 queue->rekey_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
1361 "transport_tcp_communicator",
1362 pid,
1364 rekey_monotonic_time,
1365 sizeof(*
1366 rekey_monotonic_time),
1370 queue);
1371}
1372
1373
1380static void
1382 const struct GNUNET_CRYPTO_HpkeEncapsulation *c,
1383 struct Queue *queue)
1384{
1385 struct GNUNET_ShortHashCode k;
1386
1388 setup_cipher (&k, &my_identity, &queue->in_cipher, &queue->in_hmac);
1389}
1390
1391
1398static void
1400 struct Queue *queue)
1401{
1402 struct GNUNET_ShortHashCode k;
1403
1405 setup_cipher (&k, &my_identity, &queue->in_cipher, &queue->in_hmac);
1406}
1407
1408
1417static void
1418do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
1419{
1420 struct TcpRekeySignature thp;
1421
1423 thp.purpose.size = htonl (sizeof(thp));
1425 "do_rekey size %u\n",
1426 thp.purpose.size);
1427 thp.sender = queue->target;
1429 "sender %s\n",
1432 "sender %s\n",
1433 GNUNET_p2s (&queue->target.public_key));
1434 thp.receiver = my_identity;
1436 "receiver %s\n",
1438 thp.ephemeral = rekey->ephemeral;
1440 "ephemeral %s\n",
1442 );
1443 thp.monotonic_time = rekey->monotonic_time;
1445 "time %s\n",
1448 GNUNET_assert (ntohl ((&thp)->purpose.size) == sizeof (*(&thp)));
1449 if (GNUNET_OK !=
1452 &thp,
1453 &rekey->sender_sig,
1454 &queue->target.public_key))
1455 {
1456 GNUNET_break (0);
1457 // FIXME Why should we try to gracefully finish here?
1459 return;
1460 }
1461 queue->rekey_monotonic_time = rekey->monotonic_time;
1462 queue->rekey_monotime_get = GNUNET_PEERSTORE_iteration_start (peerstore,
1463 "transport_tcp_communicator",
1464 &queue->target,
1466 &
1468 queue);
1469 gcry_cipher_close (queue->in_cipher);
1470 queue->rekeyed = GNUNET_YES;
1471 setup_in_cipher (&rekey->ephemeral, queue);
1472}
1473
1474
1480static void
1481handshake_ack_monotime_store_cb (void *cls, int success)
1482{
1483 struct Queue *queue = cls;
1484
1485 if (GNUNET_OK != success)
1486 {
1488 "Failed to store handshake ack monotonic time in PEERSTORE!\n");
1489 }
1490 queue->handshake_ack_monotime_sc = NULL;
1491 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
1492}
1493
1494
1502static void
1504 const struct GNUNET_PEERSTORE_Record *record,
1505 const char *emsg)
1506{
1507 struct Queue *queue = cls;
1508 struct GNUNET_TIME_AbsoluteNBO *mtbe;
1509 struct GNUNET_TIME_Absolute mt;
1510 const struct GNUNET_PeerIdentity *pid;
1511 struct GNUNET_TIME_AbsoluteNBO *handshake_ack_monotonic_time;
1512
1513 (void) emsg;
1514
1515 handshake_ack_monotonic_time = &queue->handshake_ack_monotonic_time;
1516 pid = &queue->target;
1517 if (NULL == record)
1518 {
1519 queue->handshake_ack_monotime_get = NULL;
1520 return;
1521 }
1522 if (sizeof(*mtbe) != record->value_size)
1523 {
1524 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
1525 GNUNET_break (0);
1526 return;
1527 }
1528 mtbe = record->value;
1529 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1531 queue->handshake_ack_monotonic_time).abs_value_us)
1532 {
1534 "Queue from %s dropped, handshake ack monotime in the past\n",
1535 GNUNET_i2s (&queue->target));
1536 GNUNET_break (0);
1537 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
1538 queue->handshake_ack_monotime_get = NULL;
1539 // FIXME: Why should we try to gracefully finish here?
1541 return;
1542 }
1543 queue->handshake_ack_monotime_sc =
1545 "transport_tcp_communicator",
1546 pid,
1548 handshake_ack_monotonic_time,
1549 sizeof(*handshake_ack_monotonic_time),
1553 queue);
1554}
1555
1556
1563static void
1565 struct Queue *queue)
1566{
1567 struct TCPConfirmationAck tca;
1568 struct TcpHandshakeAckSignature thas;
1569
1571 "sending challenge\n");
1572
1573 tca.header.type = ntohs (
1575 tca.header.size = ntohs (sizeof(tca));
1576 tca.challenge = challenge;
1577 tca.sender = my_identity;
1578 tca.monotonic_time =
1580 thas.purpose.purpose = htonl (
1582 thas.purpose.size = htonl (sizeof(thas));
1583 thas.sender = my_identity;
1584 thas.receiver = queue->target;
1585 thas.monotonic_time = tca.monotonic_time;
1586 thas.challenge = tca.challenge;
1588 &thas,
1589 &tca.sender_sig);
1590 GNUNET_assert (0 ==
1591 gcry_cipher_encrypt (queue->out_cipher,
1592 &queue->cwrite_buf[queue->cwrite_off],
1593 sizeof(tca),
1594 &tca,
1595 sizeof(tca)));
1596 queue->cwrite_off += sizeof(tca);
1598 "sending challenge done\n");
1599}
1600
1601
1608static void
1610{
1611 setup_cipher (dh, &queue->target, &queue->out_cipher, &queue->out_hmac);
1613 queue->rekey_left_bytes =
1615}
1616
1617
1624static void
1626{
1627 struct TCPRekey rekey;
1628 struct TcpRekeySignature thp;
1629 struct GNUNET_ShortHashCode k;
1630
1631 GNUNET_assert (0 == queue->pwrite_off);
1632 memset (&rekey, 0, sizeof(rekey));
1633 GNUNET_CRYPTO_eddsa_kem_encaps (&queue->target.public_key, &rekey.ephemeral,
1634 &k);
1636 rekey.header.size = ntohs (sizeof(rekey));
1637 rekey.monotonic_time =
1640 thp.purpose.size = htonl (sizeof(thp));
1642 "inject_rekey size %u\n",
1643 thp.purpose.size);
1644 thp.sender = my_identity;
1646 "sender %s\n",
1648 thp.receiver = queue->target;
1650 "receiver %s\n",
1652 thp.ephemeral = rekey.ephemeral;
1654 "ephemeral %s\n",
1656 );
1657 thp.monotonic_time = rekey.monotonic_time;
1659 "time %s\n",
1663 &thp,
1664 &rekey.sender_sig);
1665 calculate_hmac (&queue->out_hmac, &rekey, sizeof(rekey), &rekey.hmac);
1666 /* Encrypt rekey message with 'old' cipher */
1667 GNUNET_assert (0 ==
1668 gcry_cipher_encrypt (queue->out_cipher,
1669 &queue->cwrite_buf[queue->cwrite_off],
1670 sizeof(rekey),
1671 &rekey,
1672 sizeof(rekey)));
1673 queue->cwrite_off += sizeof(rekey);
1674 /* Setup new cipher for successive messages */
1675 gcry_cipher_close (queue->out_cipher);
1676 setup_out_cipher (queue, &k);
1677}
1678
1679
1680static int
1682 const struct GNUNET_HashCode *key,
1683 void *value)
1684{
1685 struct PendingReversal *pending_reversal = value;
1686 (void) cls;
1687
1688 if (NULL != pending_reversal->timeout_task)
1689 {
1690 GNUNET_SCHEDULER_cancel (pending_reversal->timeout_task);
1691 pending_reversal->timeout_task = NULL;
1692 }
1695 key,
1696 pending_reversal));
1697 GNUNET_free (pending_reversal->in);
1698 GNUNET_free (pending_reversal);
1699 return GNUNET_OK;
1700}
1701
1702
1703static void
1704check_and_remove_pending_reversal (struct sockaddr *in, sa_family_t sa_family,
1705 struct GNUNET_PeerIdentity *sender)
1706{
1707 if (AF_INET == sa_family)
1708 {
1709 struct PendingReversal *pending_reversal;
1710 struct GNUNET_HashCode key;
1711 struct sockaddr_in *natted_address;
1712
1713 natted_address = GNUNET_memdup (in, sizeof (struct sockaddr));
1714 natted_address->sin_port = 0;
1715 GNUNET_CRYPTO_hash (natted_address,
1716 sizeof(struct sockaddr),
1717 &key);
1718
1720 &key);
1721 if (NULL != pending_reversal && (NULL == sender ||
1722 0 != memcmp (sender,
1723 &pending_reversal->target,
1724 sizeof(struct
1726 {
1728 "Removing invalid pending reversal for `%s'at `%s'\n",
1729 GNUNET_i2s (&pending_reversal->target),
1730 GNUNET_a2s (in, sizeof (struct sockaddr)));
1731 pending_reversals_delete_it (NULL, &key, pending_reversal);
1732 }
1733 GNUNET_free (natted_address);
1734 }
1735}
1736
1737
1743static void
1745{
1746 if (NULL != pq->listen_sock)
1747 {
1749 pq->listen_sock = NULL;
1750 }
1751 if (NULL != pq->read_task)
1752 {
1754 pq->read_task = NULL;
1755 }
1756 if (NULL != pq->write_task)
1757 {
1759 pq->write_task = NULL;
1760 }
1761 check_and_remove_pending_reversal (pq->address, pq->address->sa_family, NULL);
1763 GNUNET_free (pq->address);
1765 GNUNET_free (pq);
1766}
1767
1768
1775static void
1777{
1778 struct ProtoQueue *pq = cls;
1779 ssize_t sent;
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In proto queue write\n");
1781 pq->write_task = NULL;
1782 if (0 != pq->write_off)
1783 {
1784 sent = GNUNET_NETWORK_socket_send (pq->sock,
1785 pq->write_buf,
1786 pq->write_off);
1788 "Sent %lu bytes to TCP queue\n", sent);
1789 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1790 {
1792 free_proto_queue (pq);
1793 return;
1794 }
1795 if (sent > 0)
1796 {
1797 size_t usent = (size_t) sent;
1798 pq->write_off -= usent;
1799 memmove (pq->write_buf,
1800 &pq->write_buf[usent],
1801 pq->write_off);
1802 }
1803 }
1804 /* do we care to write more? */
1805 if ((0 < pq->write_off))
1806 pq->write_task =
1808 pq->sock,
1810 pq);
1811}
1812
1813
1820static void
1821queue_write (void *cls)
1822{
1823 struct Queue *queue = cls;
1824 ssize_t sent;
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In queue write\n");
1826 queue->write_task = NULL;
1827 if (0 != queue->cwrite_off)
1828 {
1829 sent = GNUNET_NETWORK_socket_send (queue->sock,
1830 queue->cwrite_buf,
1831 queue->cwrite_off);
1833 "Sent %lu bytes to TCP queue\n", sent);
1834 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1835 {
1838 return;
1839 }
1840 if (sent > 0)
1841 {
1842 size_t usent = (size_t) sent;
1843 queue->cwrite_off -= usent;
1844 memmove (queue->cwrite_buf,
1845 &queue->cwrite_buf[usent],
1846 queue->cwrite_off);
1847 queue->timeout =
1850 }
1851 }
1852 {
1853 /* can we encrypt more? (always encrypt full messages, needed
1854 such that #mq_cancel() can work!) */
1855 unsigned int we_do_not_need_to_rekey = (0 < queue->rekey_left_bytes
1856 - (queue->cwrite_off
1857 + queue->pwrite_off
1858 + sizeof (struct TCPRekey)));
1859 if (we_do_not_need_to_rekey &&
1860 (queue->pwrite_off > 0) &&
1861 (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE))
1862 {
1864 "Encrypting %lu bytes\n", queue->pwrite_off);
1865 GNUNET_assert (0 ==
1866 gcry_cipher_encrypt (queue->out_cipher,
1867 &queue->cwrite_buf[queue->cwrite_off],
1868 queue->pwrite_off,
1869 queue->pwrite_buf,
1870 queue->pwrite_off));
1871 if (queue->rekey_left_bytes > queue->pwrite_off)
1872 queue->rekey_left_bytes -= queue->pwrite_off;
1873 else
1874 queue->rekey_left_bytes = 0;
1875 queue->cwrite_off += queue->pwrite_off;
1876 queue->pwrite_off = 0;
1877 }
1878 // if ((-1 != unverified_size)&& ((0 == queue->pwrite_off) &&
1879 if (((0 == queue->rekey_left_bytes) ||
1881 queue->rekey_time).rel_value_us)) &&
1882 (((0 == queue->pwrite_off) || ! we_do_not_need_to_rekey) &&
1883 (queue->cwrite_off + sizeof (struct TCPRekey) <= BUF_SIZE)))
1884 {
1886 }
1887 }
1888 if ((0 == queue->pwrite_off) && (! queue->finishing) &&
1889 (GNUNET_YES == queue->mq_awaits_continue))
1890 {
1891 queue->mq_awaits_continue = GNUNET_NO;
1893 }
1894 /* did we just finish writing 'finish'? */
1895 if ((0 == queue->cwrite_off) && (GNUNET_YES == queue->finishing))
1896 {
1898 "Finishing queue\n");
1900 return;
1901 }
1902 /* do we care to write more? */
1903 if ((0 < queue->cwrite_off) || (0 < queue->pwrite_off))
1904 queue->write_task =
1906 queue->sock,
1907 &queue_write,
1908 queue);
1909}
1910
1911
1919static size_t
1921{
1922 const struct GNUNET_MessageHeader *hdr;
1923 const struct TCPConfirmationAck *tca;
1924 const struct TCPBox *box;
1925 const struct TCPRekey *rekey;
1926 const struct TCPFinish *fin;
1927 struct TCPRekey rekeyz;
1928 struct TCPFinish finz;
1929 struct GNUNET_ShortHashCode tmac;
1930 uint16_t type;
1931 size_t size = 0;
1932 struct TcpHandshakeAckSignature thas;
1933 const struct GNUNET_CRYPTO_ChallengeNonceP challenge = queue->challenge;
1934
1936 "try handle plaintext!\n");
1937
1938 hdr = (const struct GNUNET_MessageHeader *) queue->pread_buf;
1939 if ((sizeof(*hdr) > queue->pread_off))
1940 {
1942 "Handling plaintext, not even a header!\n");
1943 return 0; /* not even a header */
1944 }
1945
1946 if ((GNUNET_YES != queue->initial_core_kx_done) && (queue->unverified_size >
1948 {
1950 "Already received data of size %lu bigger than KX size %lu!\n",
1951 queue->unverified_size,
1953 GNUNET_break_op (0);
1955 return 0;
1956 }
1957
1958 type = ntohs (hdr->type);
1959 switch (type)
1960 {
1962 tca = (const struct TCPConfirmationAck *) queue->pread_buf;
1964 "start processing ack\n");
1965 if (sizeof(*tca) > queue->pread_off)
1966 {
1968 "Handling plaintext size of tca greater than pread offset.\n")
1969 ;
1970 return 0;
1971 }
1972 if (ntohs (hdr->size) != sizeof(*tca))
1973 {
1975 "Handling plaintext size does not match message type.\n");
1976 GNUNET_break_op (0);
1978 return 0;
1979 }
1980
1981 thas.purpose.purpose = htonl (
1983 thas.purpose.size = htonl (sizeof(thas));
1984 thas.sender = tca->sender;
1985 thas.receiver = my_identity;
1986 thas.monotonic_time = tca->monotonic_time;
1987 thas.challenge = tca->challenge;
1988
1991 &thas,
1992 &tca->sender_sig,
1993 &tca->sender.public_key))
1994 {
1996 "Verification of signature failed!\n");
1997 GNUNET_break (0);
1999 return 0;
2000 }
2001 if (0 != GNUNET_memcmp (&tca->challenge, &challenge))
2002 {
2004 "Challenge in TCPConfirmationAck not correct!\n");
2005 GNUNET_break (0);
2007 return 0;
2008 }
2009
2010 queue->handshake_ack_monotime_get = GNUNET_PEERSTORE_iteration_start (
2011 peerstore,
2012 "transport_tcp_communicator",
2013 &queue->target,
2016 queue);
2017
2019 "Handling plaintext, ack processed!\n");
2020
2022 {
2023 send_challenge (queue->challenge_received, queue);
2024 queue->write_task =
2026 queue->sock,
2027 &queue_write,
2028 queue);
2029 }
2030 else if (GNUNET_TRANSPORT_CS_OUTBOUND == queue->cs)
2031 {
2033 queue->address->sa_family, NULL);
2034 }
2035
2040 queue->initial_core_kx_done = GNUNET_YES;
2041
2042 {
2043 char *foreign_addr;
2044
2045 switch (queue->address->sa_family)
2046 {
2047 case AF_INET:
2048 GNUNET_asprintf (&foreign_addr,
2049 "%s-%s",
2051 GNUNET_a2s (queue->address, queue->address_len));
2052 break;
2053
2054 case AF_INET6:
2055 GNUNET_asprintf (&foreign_addr,
2056 "%s-%s",
2058 GNUNET_a2s (queue->address, queue->address_len));
2059 break;
2060
2061 default:
2062 GNUNET_assert (0);
2063 }
2065 &queue->target,
2066 foreign_addr,
2067 UINT16_MAX, /* no MTU */
2069 0, /* Priority */
2070 queue->nt,
2071 queue->cs,
2072 queue->mq);
2073
2074 GNUNET_free (foreign_addr);
2075 }
2076
2077 size = ntohs (hdr->size);
2078 break;
2080 /* Special case: header size excludes box itself! */
2081 box = (const struct TCPBox *) queue->pread_buf;
2082 if (ntohs (hdr->size) + sizeof(struct TCPBox) > queue->pread_off)
2083 return 0;
2084 calculate_hmac (&queue->in_hmac, &box[1], ntohs (hdr->size), &tmac);
2085 if (0 != memcmp (&tmac, &box->hmac, sizeof(tmac)))
2086 {
2087 GNUNET_break_op (0);
2089 return 0;
2090 }
2091 pass_plaintext_to_core (queue, (const void *) &box[1], ntohs (hdr->size));
2092 size = ntohs (hdr->size) + sizeof(*box);
2094 "Handling plaintext, box processed!\n");
2096 "# bytes decrypted with BOX",
2097 size,
2098 GNUNET_NO);
2100 "# messages decrypted with BOX",
2101 1,
2102 GNUNET_NO);
2103 break;
2104
2106 rekey = (const struct TCPRekey *) queue->pread_buf;
2107 if (sizeof(*rekey) > queue->pread_off)
2108 return 0;
2109 if (ntohs (hdr->size) != sizeof(*rekey))
2110 {
2111 GNUNET_break_op (0);
2113 return 0;
2114 }
2115 rekeyz = *rekey;
2116 memset (&rekeyz.hmac, 0, sizeof(rekeyz.hmac));
2117 calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
2118 if (0 != memcmp (&tmac, &rekey->hmac, sizeof(tmac)))
2119 {
2120 GNUNET_break_op (0);
2122 return 0;
2123 }
2124 do_rekey (queue, rekey);
2125 size = ntohs (hdr->size);
2127 "Handling plaintext, rekey processed!\n");
2129 "# rekeying successful",
2130 1,
2131 GNUNET_NO);
2132 break;
2133
2135 fin = (const struct TCPFinish *) queue->pread_buf;
2136 if (sizeof(*fin) > queue->pread_off)
2137 return 0;
2138 if (ntohs (hdr->size) != sizeof(*fin))
2139 {
2140 GNUNET_break_op (0);
2142 return 0;
2143 }
2144 finz = *fin;
2145 memset (&finz.hmac, 0, sizeof(finz.hmac));
2146 calculate_hmac (&queue->in_hmac, &finz, sizeof(finz), &tmac);
2147 if (0 != memcmp (&tmac, &fin->hmac, sizeof(tmac)))
2148 {
2149 GNUNET_break_op (0);
2151 return 0;
2152 }
2153 /* handle FINISH by destroying queue */
2156 "Handling plaintext, finish processed!\n");
2157 break;
2158
2159 default:
2161 "Handling plaintext, nothing processed!\n");
2162 GNUNET_break_op (0);
2164 return 0;
2165 }
2166 GNUNET_assert (0 != size);
2167 if (-1 != queue->unverified_size)
2168 queue->unverified_size += size;
2169 return size;
2170}
2171
2172
2178static void
2179queue_read (void *cls)
2180{
2181 struct Queue *queue = cls;
2182 struct GNUNET_TIME_Relative left;
2183 ssize_t rcvd;
2184
2185 queue->read_task = NULL;
2186 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2187 &queue->cread_buf[queue->cread_off],
2188 BUF_SIZE - queue->cread_off);
2190 "Received %zd bytes from TCP queue\n", rcvd);
2191 if (-1 == rcvd)
2192 {
2193 if ((EAGAIN != errno) && (EINTR != errno))
2194 {
2197 return;
2198 }
2199 /* try again */
2200 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2201 if (0 != left.rel_value_us)
2202 {
2203 queue->read_task =
2205 return;
2206 }
2208 "Queue %p was idle for %s, disconnecting\n",
2209 queue,
2212 GNUNET_YES));
2214 return;
2215 }
2216 if (0 == rcvd)
2217 {
2218 /* Orderly shutdown of connection */
2220 "Socket for queue %p seems to have been closed\n", queue);
2222 return;
2223 }
2224 queue->timeout =
2226 queue->cread_off += rcvd;
2227 while ((queue->pread_off < sizeof(queue->pread_buf)) &&
2228 (queue->cread_off > 0))
2229 {
2230 size_t max = GNUNET_MIN (sizeof(queue->pread_buf) - queue->pread_off,
2231 queue->cread_off);
2232 size_t done;
2233 size_t total;
2234 size_t old_pread_off = queue->pread_off;
2235
2236 GNUNET_assert (0 ==
2237 gcry_cipher_decrypt (queue->in_cipher,
2238 &queue->pread_buf[queue->pread_off],
2239 max,
2240 queue->cread_buf,
2241 max));
2242 queue->pread_off += max;
2243 total = 0;
2244 while (0 != (done = try_handle_plaintext (queue)))
2245 {
2246 /* 'done' bytes of plaintext were used, shift buffer */
2247 GNUNET_assert (done <= queue->pread_off);
2248 /* NOTE: this memmove() could possibly sometimes be
2249 avoided if we pass 'total' into try_handle_plaintext()
2250 and use it at an offset into the buffer there! */
2251 memmove (queue->pread_buf,
2252 &queue->pread_buf[done],
2253 queue->pread_off - done);
2254 queue->pread_off -= done;
2255 total += done;
2256 /* The last plaintext was a rekey, abort for now */
2257 if (GNUNET_YES == queue->rekeyed)
2258 break;
2259 }
2260 /* when we encounter a rekey message, the decryption above uses the
2261 wrong key for everything after the rekey; in that case, we have
2262 to re-do the decryption at 'total' instead of at 'max'.
2263 However, we have to take into account that the plaintext buffer may have
2264 already contained data and not jumped too far ahead in the ciphertext.
2265 If there is no rekey and the last message is incomplete (max > total),
2266 it is safe to keep the decryption so we shift by 'max' */
2267 if (GNUNET_YES == queue->rekeyed)
2268 {
2269 max = total - old_pread_off;
2270 queue->rekeyed = GNUNET_NO;
2271 queue->pread_off = 0;
2272 }
2273 memmove (queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
2274 queue->cread_off -= max;
2275 }
2276 if (BUF_SIZE == queue->cread_off)
2277 return; /* buffer full, suspend reading */
2278 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2279 if (0 != left.rel_value_us)
2280 {
2281 if (max_queue_length > queue->backpressure)
2282 {
2283 /* continue reading */
2284 queue->read_task =
2286 }
2287 return;
2288 }
2290 "Queue %p was idle for %s, disconnecting\n",
2291 queue,
2294 GNUNET_YES));
2296}
2297
2298
2306static struct sockaddr *
2308 struct sockaddr_in6 v6,
2309 unsigned int port)
2310{
2311 struct sockaddr *in;
2312
2313 v6.sin6_family = AF_INET6;
2314 v6.sin6_port = htons ((uint16_t) port);
2315#if HAVE_SOCKADDR_IN_SIN_LEN
2316 v6.sin6_len = sizeof(struct sockaddr_in6);
2317#endif
2318 v6.sin6_flowinfo = 0;
2319 v6.sin6_scope_id = 0;
2320 in = GNUNET_memdup (&v6, sizeof(v6));
2321 *sock_len = sizeof(struct sockaddr_in6);
2322
2323 return in;
2324}
2325
2326
2334static struct sockaddr *
2336 struct sockaddr_in v4,
2337 unsigned int port)
2338{
2339 struct sockaddr *in;
2340
2341 v4.sin_family = AF_INET;
2342 v4.sin_port = htons ((uint16_t) port);
2343#if HAVE_SOCKADDR_IN_SIN_LEN
2344 v4.sin_len = sizeof(struct sockaddr_in);
2345#endif
2346 in = GNUNET_memdup (&v4, sizeof(v4));
2347 *sock_len = sizeof(struct sockaddr_in);
2348 return in;
2349}
2350
2351
2358static struct PortOnlyIpv4Ipv6 *
2359tcp_address_to_sockaddr_port_only (const char *bindto, unsigned int *port)
2360{
2361 struct PortOnlyIpv4Ipv6 *po;
2362 struct sockaddr_in *i4;
2363 struct sockaddr_in6 *i6;
2364 socklen_t sock_len_ipv4;
2365 socklen_t sock_len_ipv6;
2366
2367 /* interpreting value as just a PORT number */
2368 if (*port > UINT16_MAX)
2369 {
2371 "BINDTO specification `%s' invalid: value too large for port\n",
2372 bindto);
2373 return NULL;
2374 }
2375
2376 po = GNUNET_new (struct PortOnlyIpv4Ipv6);
2377
2378 if (GNUNET_YES == disable_v6)
2379 {
2380 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2381 po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2382 *port);
2383 po->addr_len_ipv4 = sock_len_ipv4;
2384 }
2385 else
2386 {
2387
2388 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2389 po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2390 *port);
2391 po->addr_len_ipv4 = sock_len_ipv4;
2392
2393 i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
2394 po->addr_ipv6 = tcp_address_to_sockaddr_numeric_v6 (&sock_len_ipv6, *i6,
2395 *port);
2396
2397 po->addr_len_ipv6 = sock_len_ipv6;
2398
2399 GNUNET_free (i6);
2400 }
2401
2402 GNUNET_free (i4);
2403
2404 return po;
2405}
2406
2407
2414static char *
2415extract_address (const char *bindto)
2416{
2417 char *addr;
2418 char *start;
2419 char *token;
2420 char *cp;
2421 char *rest = NULL;
2422
2424 "extract address with bindto %s\n",
2425 bindto);
2426
2427 if (NULL == bindto)
2429 "bindto is NULL\n");
2430
2431 cp = GNUNET_strdup (bindto);
2432
2434 "extract address 2\n");
2435
2436 start = cp;
2437 if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
2438 {
2439 start++; /* skip over '['*/
2440 cp[strlen (cp) - 1] = '\0'; /* eat ']'*/
2441 addr = GNUNET_strdup (start);
2442 }
2443 else
2444 {
2445 token = strtok_r (cp, "]", &rest);
2446 if (strlen (bindto) == strlen (token))
2447 {
2448 token = strtok_r (cp, ":", &rest);
2449 addr = GNUNET_strdup (token);
2450 }
2451 else
2452 {
2453 token++;
2454 addr = GNUNET_strdup (token);
2455 }
2456 }
2457
2459 "tcp address: %s\n",
2460 addr);
2461 GNUNET_free (cp);
2462 return addr;
2463}
2464
2465
2472static unsigned int
2473extract_port (const char *addr_and_port)
2474{
2475 unsigned int port;
2476 char dummy[2];
2477 char *token;
2478 char *addr;
2479 char *colon;
2480 char *cp;
2481 char *rest = NULL;
2482
2483 if (NULL != addr_and_port)
2484 {
2485 cp = GNUNET_strdup (addr_and_port);
2486 token = strtok_r (cp, "]", &rest);
2487 if (strlen (addr_and_port) == strlen (token))
2488 {
2489 colon = strrchr (cp, ':');
2490 if (NULL == colon)
2491 {
2492 GNUNET_free (cp);
2493 return 0;
2494 }
2495 addr = colon;
2496 addr++;
2497 }
2498 else
2499 {
2500 token = strtok_r (NULL, "]", &rest);
2501 if (NULL == token)
2502 {
2503 GNUNET_free (cp);
2504 return 0;
2505 }
2506 else
2507 {
2508 addr = token;
2509 addr++;
2510 }
2511 }
2512
2513
2514 if (1 == sscanf (addr, "%u%1s", &port, dummy))
2515 {
2516 /* interpreting value as just a PORT number */
2517 if (port > UINT16_MAX)
2518 {
2520 "Port `%u' invalid: value too large for port\n",
2521 port);
2522 GNUNET_free (cp);
2523 return 0;
2524 }
2525 }
2526 else
2527 {
2529 "BINDTO specification invalid: last ':' not followed by number\n");
2530 GNUNET_free (cp);
2531 return 0;
2532 }
2533 GNUNET_free (cp);
2534 }
2535 else
2536 {
2538 "return 0\n");
2539 /* interpret missing port as 0, aka pick any free one */
2540 port = 0;
2541 }
2542
2543 return port;
2544}
2545
2546
2554static struct sockaddr *
2555tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
2556{
2557 struct sockaddr *in;
2558 unsigned int port;
2559 struct sockaddr_in v4;
2560 struct sockaddr_in6 v6;
2561 char *start;
2562
2563 memset (&v4, 0, sizeof(v4));
2564 start = extract_address (bindto);
2565 GNUNET_assert (NULL != start);
2567 "start %s\n",
2568 start);
2569
2571 "!bindto %s\n",
2572 bindto);
2573
2574
2575 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
2576 {
2577 port = extract_port (bindto);
2578
2580 "port %u\n",
2581 port);
2582
2583 in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port);
2584 }
2585 else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
2586 {
2587 port = extract_port (bindto);
2588 in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port);
2589 }
2590 else
2591 {
2592 GNUNET_assert (0);
2593 }
2594
2596 return in;
2597}
2598
2599
2608static void
2610 const struct GNUNET_MessageHeader *msg,
2611 void *impl_state)
2612{
2613 struct Queue *queue = impl_state;
2614 uint16_t msize = ntohs (msg->size);
2615 struct TCPBox box;
2617 "In MQ send. Queue finishing: %s; write task running: %s\n",
2618 (GNUNET_YES == queue->finishing) ? "yes" : "no",
2619 (NULL == queue->write_task) ? "yes" : "no");
2620 GNUNET_assert (mq == queue->mq);
2621 queue->mq_awaits_continue = GNUNET_YES;
2622 if (GNUNET_YES == queue->finishing)
2623 return; /* this queue is dying, drop msg */
2624 GNUNET_assert (0 == queue->pwrite_off);
2626 box.header.size = htons (msize);
2627 calculate_hmac (&queue->out_hmac, msg, msize, &box.hmac);
2628 memcpy (&queue->pwrite_buf[queue->pwrite_off], &box, sizeof(box));
2629 queue->pwrite_off += sizeof(box);
2630 memcpy (&queue->pwrite_buf[queue->pwrite_off], msg, msize);
2631 queue->pwrite_off += msize;
2633 "%lu bytes of plaintext to send\n", queue->pwrite_off);
2634 GNUNET_assert (NULL != queue->sock);
2635 if (NULL == queue->write_task)
2636 queue->write_task =
2638 queue->sock,
2639 &queue_write,
2640 queue);
2641}
2642
2643
2652static void
2653mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
2654{
2655 struct Queue *queue = impl_state;
2656
2657 if (mq == queue->mq)
2658 {
2659 queue->mq = NULL;
2661 }
2662}
2663
2664
2671static void
2672mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
2673{
2674 struct Queue *queue = impl_state;
2675
2676 GNUNET_assert (0 != queue->pwrite_off);
2677 queue->pwrite_off = 0;
2678}
2679
2680
2690static void
2691mq_error (void *cls, enum GNUNET_MQ_Error error)
2692{
2693 struct Queue *queue = cls;
2694
2696 "MQ error in queue to %s: %d\n",
2697 GNUNET_i2s (&queue->target),
2698 (int) error);
2700}
2701
2702
2710static void
2712{
2713 queue->nt =
2714 GNUNET_NT_scanner_get_type (is, queue->address, queue->address_len);
2716 queue_map,
2717 &queue->key,
2718 queue,
2721 "# queues active",
2723 GNUNET_NO);
2724 queue->timeout =
2727 &mq_destroy,
2728 &mq_cancel,
2729 queue,
2730 NULL,
2731 &mq_error,
2732 queue);
2733}
2734
2735
2746static void
2748 const struct GNUNET_CRYPTO_HpkeEncapsulation *c)
2749{
2750 struct TcpHandshakeSignature ths;
2751 struct TCPConfirmation tc;
2752
2753 memcpy (queue->cwrite_buf, c, sizeof(*c));
2754 queue->cwrite_off = sizeof(*c);
2755 /* compute 'tc' and append in encrypted format to cwrite_buf */
2756 tc.sender = my_identity;
2757 tc.monotonic_time =
2760 &tc.challenge,
2761 sizeof(tc.challenge));
2762 ths.purpose.purpose = htonl (
2764 ths.purpose.size = htonl (sizeof(ths));
2765 ths.sender = my_identity;
2766 ths.receiver = queue->target;
2767 ths.ephemeral = *c;
2768 ths.monotonic_time = tc.monotonic_time;
2769 ths.challenge = tc.challenge;
2771 &ths,
2772 &tc.sender_sig);
2773 GNUNET_assert (0 ==
2774 gcry_cipher_encrypt (queue->out_cipher,
2775 &queue->cwrite_buf[queue->cwrite_off],
2776 sizeof(tc),
2777 &tc,
2778 sizeof(tc)));
2779 queue->challenge = tc.challenge;
2780 queue->cwrite_off += sizeof(tc);
2781
2783 "handshake written\n");
2784}
2785
2786
2794static void
2796{
2798 struct GNUNET_ShortHashCode k;
2799
2801 &c, &k);
2802 setup_out_cipher (queue, &k);
2803 transmit_kx (queue, &c);
2804}
2805
2806
2812static void
2813handshake_monotime_store_cb (void *cls, int success)
2814{
2815 struct Queue *queue = cls;
2816 if (GNUNET_OK != success)
2817 {
2819 "Failed to store handshake monotonic time in PEERSTORE!\n");
2820 }
2821 queue->handshake_monotime_sc = NULL;
2822 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
2823}
2824
2825
2833static void
2835 const struct GNUNET_PEERSTORE_Record *record,
2836 const char *emsg)
2837{
2838 struct Queue *queue = cls;
2839 struct GNUNET_TIME_AbsoluteNBO *mtbe;
2840 struct GNUNET_TIME_Absolute mt;
2841 const struct GNUNET_PeerIdentity *pid;
2842 struct GNUNET_TIME_AbsoluteNBO *handshake_monotonic_time;
2843
2844 (void) emsg;
2845
2846 handshake_monotonic_time = &queue->handshake_monotonic_time;
2847 pid = &queue->target;
2849 "tcp handshake with us %s\n",
2851 if (NULL == record)
2852 {
2853 queue->handshake_monotime_get = NULL;
2854 return;
2855 }
2857 "tcp handshake from peer %s\n",
2858 GNUNET_i2s (pid));
2859 if (sizeof(*mtbe) != record->value_size)
2860 {
2861 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
2862 GNUNET_break (0);
2863 return;
2864 }
2865 mtbe = record->value;
2866 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
2868 queue->handshake_monotonic_time).abs_value_us)
2869 {
2871 "Queue from %s dropped, handshake monotime in the past\n",
2872 GNUNET_i2s (&queue->target));
2873 GNUNET_break (0);
2874 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
2875 queue->handshake_ack_monotime_get = NULL;
2877 return;
2878 }
2879 queue->handshake_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
2880 "transport_tcp_communicator",
2881 pid,
2883 handshake_monotonic_time,
2884 sizeof(*
2885 handshake_monotonic_time),
2888 &
2890 queue);
2891}
2892
2893
2905static int
2907 struct TCPConfirmation *tc,
2908 char *ibuf)
2909{
2910 struct TcpHandshakeSignature ths;
2912
2914 0 ==
2915 gcry_cipher_decrypt (queue->in_cipher,
2916 tc,
2917 sizeof(*tc),
2918 &ibuf[sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)],
2919 sizeof(*tc)));
2920 ths.purpose.purpose = htonl (
2922 ths.purpose.size = htonl (sizeof(ths));
2923 ths.sender = tc->sender;
2924 ths.receiver = my_identity;
2925 memcpy (&ths.ephemeral, ibuf, sizeof(struct GNUNET_CRYPTO_EcdhePublicKey));
2926 ths.monotonic_time = tc->monotonic_time;
2927 ths.challenge = tc->challenge;
2930 &ths,
2931 &tc->sender_sig,
2932 &tc->sender.public_key);
2933 if (GNUNET_YES == ret)
2934 queue->handshake_monotime_get =
2936 "transport_tcp_communicator",
2937 &queue->target,
2940 queue);
2941 return ret;
2942}
2943
2944
2952static void
2953queue_read_kx (void *cls)
2954{
2955 struct Queue *queue = cls;
2956 ssize_t rcvd;
2957 struct GNUNET_TIME_Relative left;
2958 struct TCPConfirmation tc;
2959
2960 queue->read_task = NULL;
2961 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2962 if (0 == left.rel_value_us)
2963 {
2965 return;
2966 }
2967 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2968 &queue->cread_buf[queue->cread_off],
2969 BUF_SIZE - queue->cread_off);
2971 "Received %lu bytes to write in buffer of size %lu for KX from queue %p (expires in %"
2972 PRIu64 ")\n",
2973 rcvd, BUF_SIZE - queue->cread_off, queue, left.rel_value_us);
2974 if (-1 == rcvd)
2975 {
2976 if ((EAGAIN != errno) && (EINTR != errno))
2977 {
2980 return;
2981 }
2982 queue->read_task =
2984 return;
2985 }
2986 if (0 == rcvd)
2987 {
2988 /* Orderly shutdown of connection */
2990 "Socket for queue %p seems to have been closed\n", queue);
2992 return;
2993 }
2994 queue->cread_off += rcvd;
2995 if (queue->cread_off < INITIAL_KX_SIZE)
2996 {
2997 /* read more */
2999 "%lu/%lu bytes of KX read. Rescheduling...\n",
3000 queue->cread_off, INITIAL_KX_SIZE);
3001 queue->read_task =
3003 return;
3004 }
3005 /* we got all the data, let's find out who we are talking to! */
3007 (const struct GNUNET_CRYPTO_HpkeEncapsulation*)
3008 queue->cread_buf,
3009 queue);
3010 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, queue->cread_buf))
3011 {
3013 "Invalid TCP KX received from %s\n",
3014 GNUNET_a2s (queue->address, queue->address_len));
3016 return;
3017 }
3018 if (0 !=
3019 memcmp (&tc.sender, &queue->target, sizeof(struct GNUNET_PeerIdentity)))
3020 {
3022 "Invalid sender in TCP KX received from %s\n",
3023 GNUNET_a2s (queue->address, queue->address_len));
3025 return;
3026 }
3027 send_challenge (tc.challenge, queue);
3028 queue->write_task =
3030 queue->sock,
3031 &queue_write,
3032 queue);
3033
3034 /* update queue timeout */
3035 queue->timeout =
3037 /* prepare to continue with regular read task immediately */
3038 memmove (queue->cread_buf,
3039 &queue->cread_buf[INITIAL_KX_SIZE],
3040 queue->cread_off - (INITIAL_KX_SIZE));
3042 "cread_off is %lu bytes before adjusting\n",
3043 queue->cread_off);
3044 queue->cread_off -= INITIAL_KX_SIZE;
3046 "cread_off set to %lu bytes\n",
3047 queue->cread_off);
3049}
3050
3051
3058static void
3059proto_read_kx (void *cls)
3060{
3061 struct ProtoQueue *pq = cls;
3062 ssize_t rcvd;
3063 struct GNUNET_TIME_Relative left;
3064 struct Queue *queue;
3065 struct TCPConfirmation tc;
3067
3068 pq->read_task = NULL;
3070 if (0 == left.rel_value_us)
3071 {
3072 free_proto_queue (pq);
3073 return;
3074 }
3075 rcvd = GNUNET_NETWORK_socket_recv (pq->sock,
3076 &pq->ibuf[pq->ibuf_off],
3077 sizeof(pq->ibuf) - pq->ibuf_off);
3079 "Proto received %lu bytes for KX\n", rcvd);
3080 if (-1 == rcvd)
3081 {
3082 if ((EAGAIN != errno) && (EINTR != errno))
3083 {
3085 free_proto_queue (pq);
3086 return;
3087 }
3088 /* try again */
3089 pq->read_task =
3091 return;
3092 }
3093 if (0 == rcvd)
3094 {
3095 /* Orderly shutdown of connection */
3097 "Socket for proto queue %p seems to have been closed\n", pq);
3098 free_proto_queue (pq);
3099 return;
3100 }
3101 pq->ibuf_off += rcvd;
3102 if (sizeof (struct TCPNATProbeMessage) == pq->ibuf_off)
3103 {
3104 struct TCPNATProbeMessage *pm = (struct TCPNATProbeMessage *) pq->ibuf;
3105
3107 &pm->clientIdentity);
3108
3109 queue = GNUNET_new (struct Queue);
3110 queue->target = pm->clientIdentity;
3111 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
3114 }
3115 else if (pq->ibuf_off > sizeof(pq->ibuf))
3116 {
3117 /* read more */
3118 pq->read_task =
3120 return;
3121 }
3122 else
3123 {
3124 /* we got all the data, let's find out who we are talking to! */
3125 queue = GNUNET_new (struct Queue);
3127 (const struct GNUNET_CRYPTO_HpkeEncapsulation *) pq->
3128 ibuf,
3129 queue);
3130 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, pq->ibuf))
3131 {
3133 "Invalid TCP KX received from %s\n",
3134 GNUNET_a2s (pq->address, pq->address_len));
3135 gcry_cipher_close (queue->in_cipher);
3137 free_proto_queue (pq);
3138 return;
3139 }
3140 queue->target = tc.sender;
3141 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
3144 }
3145 queue->address = pq->address; /* steals reference */
3146 queue->address_len = pq->address_len;
3147 queue->listen_sock = pq->listen_sock;
3148 queue->sock = pq->sock;
3149
3151 "created queue with target %s\n",
3152 GNUNET_i2s (&queue->target));
3153
3155 "start kx proto\n");
3156
3158 boot_queue (queue);
3159 queue->read_task =
3161 queue->sock,
3162 read_task,
3163 queue);
3164 queue->write_task =
3166 queue->sock,
3167 &queue_write,
3168 queue);
3169 // TODO To early! Move it somewhere else.
3170 // send_challenge (tc.challenge, queue);
3171 queue->challenge_received = tc.challenge;
3172
3174 GNUNET_free (pq);
3175}
3176
3177
3178static struct ProtoQueue *
3180 struct sockaddr *in,
3181 socklen_t addrlen)
3182{
3183 struct ProtoQueue *pq = GNUNET_new (struct ProtoQueue);
3184
3185 if (NULL == sock)
3186 {
3187 // sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
3188 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, 0);
3189 if (NULL == sock)
3190 {
3192 "socket(%d) failed: %s",
3193 in->sa_family,
3194 strerror (errno));
3195 GNUNET_free (in);
3196 GNUNET_free (pq);
3197 return NULL;
3198 }
3199 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, addrlen)) &&
3200 (errno != EINPROGRESS))
3201 {
3203 "connect to `%s' failed: %s",
3204 GNUNET_a2s (in, addrlen),
3205 strerror (errno));
3207 GNUNET_free (in);
3208 GNUNET_free (pq);
3209 return NULL;
3210 }
3211 }
3212 pq->address_len = addrlen;
3213 pq->address = in;
3215 pq->sock = sock;
3217 pq->sock,
3219 pq);
3221
3222 return pq;
3223}
3224
3225
3233static void
3234listen_cb (void *cls)
3235{
3236 struct sockaddr_storage in;
3237 socklen_t addrlen;
3238 struct GNUNET_NETWORK_Handle *sock;
3239 struct ListenTask *lt;
3240 struct sockaddr *in_addr;
3241
3243 "listen_cb\n");
3244
3245 lt = cls;
3246
3247 lt->listen_task = NULL;
3248 GNUNET_assert (NULL != lt->listen_sock);
3249 addrlen = sizeof(in);
3250 memset (&in, 0, sizeof(in));
3252 (struct sockaddr*) &in,
3253 &addrlen);
3254 if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno)))
3255 return; /* system limit reached, wait until connection goes down */
3257 lt->listen_sock,
3258 &listen_cb,
3259 lt);
3260 if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno)))
3261 return;
3262 if (NULL == sock)
3263 {
3265 return;
3266 }
3267 in_addr = GNUNET_memdup (&in, addrlen);
3268 create_proto_queue (sock, in_addr, addrlen);
3269}
3270
3271
3272static void
3274 const struct sockaddr *addr,
3275 socklen_t addrlen)
3276{
3277 struct TCPNATProbeMessage pm;
3278 struct ProtoQueue *pq;
3279 struct sockaddr *in_addr;
3280 (void) cls;
3281
3283 "addr->sa_family %d\n",
3284 addr->sa_family);
3286 "Try to connect back\n");
3287 in_addr = GNUNET_memdup (addr, addrlen);
3289 "in_addr->sa_family %d\n",
3290 in_addr->sa_family);
3291 pq = create_proto_queue (NULL, in_addr, addrlen);
3292 if (NULL != pq)
3293 {
3294 pm.header.size = htons (sizeof(struct TCPNATProbeMessage));
3295 pm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
3296 pm.clientIdentity = my_identity;
3297 memcpy (pq->write_buf, &pm, sizeof(struct TCPNATProbeMessage));
3298 pq->write_off = sizeof(struct TCPNATProbeMessage);
3300 pq->sock,
3302 pq);
3303 }
3304 else
3305 {
3307 "Couldn't create ProtoQueue for sending TCPNATProbeMessage\n");
3308 }
3309}
3310
3311
3312static void
3314{
3315 struct sockaddr *in = cls;
3316 struct PendingReversal *pending_reversal;
3317 struct GNUNET_HashCode key;
3318
3320 sizeof(struct sockaddr),
3321 &key);
3323 &key);
3324
3325 GNUNET_assert (NULL != pending_reversal);
3326
3328 &key,
3329 pending_reversal))
3331 "No pending reversal found for address %s\n",
3332 GNUNET_a2s (in, sizeof (struct sockaddr)));
3333 GNUNET_free (pending_reversal->in);
3334 GNUNET_free (pending_reversal);
3335}
3336
3337
3356static int
3357mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
3358{
3359 struct sockaddr *in;
3360 socklen_t in_len = 0;
3361 const char *path;
3362 struct sockaddr_in *v4;
3363 struct sockaddr_in6 *v6;
3364 unsigned int is_natd = GNUNET_NO;
3365 struct GNUNET_HashCode key;
3366 struct GNUNET_HashCode queue_map_key;
3367 struct GNUNET_HashContext *hsh;
3368 struct Queue *queue;
3369
3371 "Connecting to %s at %s\n",
3372 GNUNET_i2s (peer),
3373 address);
3374 if (0 != strncmp (address,
3376 strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
3377 {
3378 GNUNET_break_op (0);
3379 return GNUNET_SYSERR;
3380 }
3381 path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
3382 in = tcp_address_to_sockaddr (path, &in_len);
3383
3384 if (NULL == in)
3385 {
3387 "Failed to setup TCP socket address\n");
3388 return GNUNET_SYSERR;
3389 }
3390
3392 "in %s\n",
3393 GNUNET_a2s (in, in_len));
3394
3397 GNUNET_CRYPTO_hash_context_read (hsh, peer, sizeof (*peer));
3398 GNUNET_CRYPTO_hash_context_finish (hsh, &queue_map_key);
3400
3401 if (NULL != queue)
3402 {
3404 "Queue for %s already exists or is in construction\n", address);
3405 GNUNET_free (in);
3406 return GNUNET_NO;
3407 }
3408 switch (in->sa_family)
3409 {
3410 case AF_INET:
3411 v4 = (struct sockaddr_in *) in;
3412 if (0 == v4->sin_port)
3413 {
3414 is_natd = GNUNET_YES;
3416 sizeof(struct sockaddr),
3417 &key);
3420 &key))
3421 {
3423 "There is already a request reversal for `%s'at `%s'\n",
3424 GNUNET_i2s (peer),
3425 address);
3426 GNUNET_free (in);
3427 return GNUNET_SYSERR;
3428 }
3429 }
3430 break;
3431
3432 case AF_INET6:
3433 if (GNUNET_YES == disable_v6)
3434 {
3436 "IPv6 disabled, skipping %s\n", address);
3437 GNUNET_free (in);
3438 return GNUNET_SYSERR;
3439 }
3440 v6 = (struct sockaddr_in6 *) in;
3441 if (0 == v6->sin6_port)
3442 {
3444 "Request reversal for `%s' at `%s' not possible for an IPv6 address\n",
3445 GNUNET_i2s (peer),
3446 address);
3447 GNUNET_free (in);
3448 return GNUNET_SYSERR;
3449 }
3450 break;
3451
3452 default:
3453 GNUNET_assert (0);
3454 }
3455
3456 if (GNUNET_YES == is_natd)
3457 {
3458 struct sockaddr_in local_sa;
3459 struct PendingReversal *pending_reversal;
3460
3461 memset (&local_sa, 0, sizeof(local_sa));
3462 local_sa.sin_family = AF_INET;
3463 local_sa.sin_port = htons (bind_port);
3464 /* We leave sin_address at 0, let the kernel figure it out,
3465 even if our bind() is more specific. (May want to reconsider
3466 later.) */
3467 if (GNUNET_OK != GNUNET_NAT_request_reversal (nat, &local_sa, v4))
3468 {
3470 "request reversal for `%s' at `%s' failed\n",
3471 GNUNET_i2s (peer),
3472 address);
3473 GNUNET_free (in);
3474 return GNUNET_SYSERR;
3475 }
3476 pending_reversal = GNUNET_new (struct PendingReversal);
3477 pending_reversal->in = in;
3480 &key,
3481 pending_reversal,
3483 pending_reversal->target = *peer;
3485 &
3487 in);
3489 "Created NAT WAIT connection to `%s' at `%s'\n",
3490 GNUNET_i2s (peer),
3491 GNUNET_a2s (in, sizeof (struct sockaddr)));
3492 }
3493 else
3494 {
3495 struct GNUNET_NETWORK_Handle *sock;
3496
3497 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM,
3498 IPPROTO_TCP);
3499 if (NULL == sock)
3500 {
3502 "socket(%d) failed: %s",
3503 in->sa_family,
3504 strerror (errno));
3505 GNUNET_free (in);
3506 return GNUNET_SYSERR;
3507 }
3508 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, in_len)) &&
3509 (errno != EINPROGRESS))
3510 {
3512 "connect to `%s' failed: %s",
3513 address,
3514 strerror (errno));
3516 GNUNET_free (in);
3517 return GNUNET_SYSERR;
3518 }
3519
3520 queue = GNUNET_new (struct Queue);
3521 queue->target = *peer;
3522 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
3523 queue->key = queue_map_key;
3524 queue->address = in;
3525 queue->address_len = in_len;
3526 queue->sock = sock;
3528 boot_queue (queue);
3530 "booted queue with target %s\n",
3531 GNUNET_i2s (&queue->target));
3532 // queue->mq_awaits_continue = GNUNET_YES;
3533 queue->read_task =
3535 queue->sock,
3537 queue);
3538
3539
3541 "start kx mq_init\n");
3542
3544 queue->write_task =
3546 queue->sock,
3547 &queue_write,
3548 queue);
3549 }
3550
3551 return GNUNET_OK;
3552}
3553
3554
3563static int
3565 const struct GNUNET_HashCode *key,
3566 void *value)
3567{
3568 struct ListenTask *lt = value;
3569
3570 (void) cls;
3571 (void) key;
3572 if (NULL != lt->listen_task)
3573 {
3575 lt->listen_task = NULL;
3576 }
3577 if (NULL != lt->listen_sock)
3578 {
3580 lt->listen_sock = NULL;
3581 }
3582 GNUNET_free (lt);
3583 return GNUNET_OK;
3584}
3585
3586
3595static int
3597 const struct GNUNET_HashCode *target,
3598 void *value)
3599{
3600 struct Queue *queue = value;
3601
3602 (void) cls;
3603 (void) target;
3605 return GNUNET_OK;
3606}
3607
3608
3614static void
3615do_shutdown (void *cls)
3616{
3618 "Shutdown %s!\n",
3619 shutdown_running ? "running" : "not running");
3620
3622 return;
3623 else
3625
3626 while (NULL != proto_head)
3628 if (NULL != nat)
3629 {
3631 nat = NULL;
3632 }
3640 if (NULL != ch)
3641 {
3644 ch = NULL;
3645 }
3646 if (NULL != stats)
3647 {
3649 stats = NULL;
3650 }
3651 if (NULL != my_private_key)
3652 {
3654 my_private_key = NULL;
3655 }
3656 if (NULL != is)
3657 {
3659 is = NULL;
3660 }
3661 if (NULL != peerstore)
3662 {
3664 peerstore = NULL;
3665 }
3666 if (NULL != resolve_request_handle)
3667 {
3670 }
3672 "Shutdown done!\n");
3673}
3674
3675
3687static void
3688enc_notify_cb (void *cls,
3689 const struct GNUNET_PeerIdentity *sender,
3690 const struct GNUNET_MessageHeader *msg)
3691{
3692 (void) cls;
3693 (void) sender;
3694 (void) msg;
3695 GNUNET_break_op (0);
3696}
3697
3698
3712static void
3714 void **app_ctx,
3715 int add_remove,
3717 const struct sockaddr *addr,
3718 socklen_t addrlen)
3719{
3720 char *my_addr;
3722
3724 "nat address cb %s %s\n",
3725 add_remove ? "add" : "remove",
3726 GNUNET_a2s (addr, addrlen));
3727
3728 if (GNUNET_YES == add_remove)
3729 {
3731
3732 GNUNET_asprintf (&my_addr,
3733 "%s-%s",
3735 GNUNET_a2s (addr, addrlen));
3736 nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
3737 ai =
3739 my_addr,
3740 nt,
3742 GNUNET_free (my_addr);
3743 *app_ctx = ai;
3744 }
3745 else
3746 {
3747 ai = *app_ctx;
3749 *app_ctx = NULL;
3750 }
3751}
3752
3753
3757static void
3758add_addr (struct sockaddr *in, socklen_t in_len)
3759{
3760
3761 struct Addresses *saddrs;
3762
3764 "add address %s\n",
3765 GNUNET_a2s (in, in_len));
3766
3767 saddrs = GNUNET_new (struct Addresses);
3768 saddrs->addr = in;
3769 saddrs->addr_len = in_len;
3771
3773 "after add address %s\n",
3774 GNUNET_a2s (in, in_len));
3775
3777 "add address %s\n",
3778 GNUNET_a2s (saddrs->addr, saddrs->addr_len));
3779
3780 addrs_lens++;
3781}
3782
3783
3791static int
3792init_socket (struct sockaddr *addr,
3793 socklen_t in_len)
3794{
3795 struct sockaddr_storage in_sto;
3796 socklen_t sto_len;
3797 struct GNUNET_NETWORK_Handle *listen_sock;
3798 struct ListenTask *lt;
3799 int sockfd;
3800 struct GNUNET_HashCode h_sock;
3801
3802 if (NULL == addr)
3803 {
3805 "Address is NULL.\n");
3806 return GNUNET_SYSERR;
3807 }
3808
3810 "address %s\n",
3811 GNUNET_a2s (addr, in_len));
3812
3813 listen_sock =
3814 GNUNET_NETWORK_socket_create (addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
3815 if (NULL == listen_sock)
3816 {
3818 return GNUNET_SYSERR;
3819 }
3820
3821 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, addr, in_len))
3822 {
3824 GNUNET_NETWORK_socket_close (listen_sock);
3825 listen_sock = NULL;
3826 return GNUNET_SYSERR;
3827 }
3828
3829 if (GNUNET_OK !=
3830 GNUNET_NETWORK_socket_listen (listen_sock,
3831 5))
3832 {
3834 "listen");
3835 GNUNET_NETWORK_socket_close (listen_sock);
3836 listen_sock = NULL;
3837 return GNUNET_SYSERR;
3838 }
3839
3840 /* We might have bound to port 0, allowing the OS to figure it out;
3841 thus, get the real IN-address from the socket */
3842 sto_len = sizeof(in_sto);
3843
3844 if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock),
3845 (struct sockaddr *) &in_sto,
3846 &sto_len))
3847 {
3848 memcpy (&in_sto, addr, in_len);
3849 sto_len = in_len;
3850 }
3851
3852 // addr = (struct sockaddr *) &in_sto;
3853 in_len = sto_len;
3855 "Bound to `%s'\n",
3856 GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
3857 if (NULL == stats)
3858 stats = GNUNET_STATISTICS_create ("communicator-tcp", cfg);
3859
3860 if (NULL == is)
3862
3863 if (NULL == my_private_key)
3865 if (NULL == my_private_key)
3866 {
3867 GNUNET_log (
3869 _ (
3870 "Transport service is lacking key configuration settings. Exiting.\n"));
3871 if (NULL != resolve_request_handle)
3874 return GNUNET_SYSERR;
3875 }
3879 /* start listening */
3880
3881 lt = GNUNET_new (struct ListenTask);
3882 lt->listen_sock = listen_sock;
3883
3885 listen_sock,
3886 &listen_cb,
3887 lt);
3888
3890 "creating hash\n");
3891 sockfd = GNUNET_NETWORK_get_fd (lt->listen_sock);
3892 GNUNET_CRYPTO_hash (&sockfd,
3893 sizeof(int),
3894 &h_sock);
3895
3897 "creating map\n");
3898 if (NULL == lt_map)
3900
3902 "creating map entry\n");
3905 &h_sock,
3906 lt,
3908
3910 "map entry created\n");
3911
3912 if (NULL == queue_map)
3914
3915 if (NULL == ch)
3920 &mq_init,
3921 NULL,
3923 NULL,
3924 NULL);
3925
3926 if (NULL == ch)
3927 {
3928 GNUNET_break (0);
3929 if (NULL != resolve_request_handle)
3932 return GNUNET_SYSERR;
3933 }
3934
3935 add_addr (addr, in_len);
3936 return GNUNET_OK;
3937
3938}
3939
3940
3944static void
3946{
3947 struct sockaddr **saddrs;
3948 socklen_t *saddr_lens;
3949 int i;
3950 size_t len;
3951
3953 "starting nat register!\n");
3954 len = 0;
3955 i = 0;
3956 saddrs = GNUNET_malloc ((addrs_lens) * sizeof(struct sockaddr *));
3957 saddr_lens = GNUNET_malloc ((addrs_lens) * sizeof(socklen_t));
3958 for (struct Addresses *pos = addrs_head; NULL != pos; pos = pos->next)
3959 {
3961 "registering address %s\n",
3963
3964 saddr_lens[i] = addrs_head->addr_len;
3965 len += saddr_lens[i];
3966 saddrs[i] = GNUNET_memdup (addrs_head->addr, saddr_lens[i]);
3967 i++;
3968 }
3969
3971 "registering addresses %lu %lu %lu %lu\n",
3972 (addrs_lens) * sizeof(struct sockaddr *),
3973 (addrs_lens) * sizeof(socklen_t),
3974 len,
3978 IPPROTO_TCP,
3979 addrs_lens,
3980 (const struct sockaddr **) saddrs,
3981 saddr_lens,
3984 NULL /* closure */);
3985 for (i = addrs_lens - 1; i >= 0; i--)
3986 GNUNET_free (saddrs[i]);
3987 GNUNET_free (saddrs);
3988 GNUNET_free (saddr_lens);
3989
3990 if (NULL == nat)
3991 {
3992 GNUNET_break (0);
3993 if (NULL != resolve_request_handle)
3996 }
3997}
3998
3999
4007static void
4009 const struct sockaddr *addr,
4010 socklen_t in_len)
4011{
4012 struct sockaddr_in *v4;
4013 struct sockaddr_in6 *v6;
4014 struct sockaddr *in;
4015
4016 (void) cls;
4017 if (NULL != addr)
4018 {
4019 if (AF_INET == addr->sa_family)
4020 {
4021 v4 = (struct sockaddr_in *) addr;
4022 in = tcp_address_to_sockaddr_numeric_v4 (&in_len, *v4, bind_port);// _global);
4023 }
4024 else if (AF_INET6 == addr->sa_family)
4025 {
4026 v6 = (struct sockaddr_in6 *) addr;
4027 in = tcp_address_to_sockaddr_numeric_v6 (&in_len, *v6, bind_port);// _global);
4028 }
4029 else
4030 {
4032 "Address family %u not suitable (not AF_INET %u nor AF_INET6 %u \n",
4033 addr->sa_family,
4034 AF_INET,
4035 AF_INET6);
4036 return;
4037 }
4038 init_socket (in, in_len);
4039 }
4040 else
4041 {
4043 "Address is NULL. This might be an error or the resolver finished resolving.\n");
4044 if (NULL == addrs_head)
4045 {
4047 "Resolver finished resolving, but we do not listen to an address!.\n");
4048 return;
4049 }
4050 nat_register ();
4051 }
4052}
4053
4054
4063static void
4064run (void *cls,
4065 char *const *args,
4066 const char *cfgfile,
4067 const struct GNUNET_CONFIGURATION_Handle *c)
4068{
4069 char *bindto;
4070 struct sockaddr *in;
4071 socklen_t in_len;
4072 struct sockaddr_in v4;
4073 struct sockaddr_in6 v6;
4074 char *start;
4075 unsigned int port;
4076 char dummy[2];
4077 char *rest = NULL;
4078 struct PortOnlyIpv4Ipv6 *po;
4079 socklen_t addr_len_ipv4;
4080 socklen_t addr_len_ipv6;
4081
4082 (void) cls;
4083
4085 memset (&v4,0,sizeof(struct sockaddr_in));
4086 memset (&v6,0,sizeof(struct sockaddr_in6));
4087 cfg = c;
4088 if (GNUNET_OK !=
4091 "BINDTO",
4092 &bindto))
4093 {
4096 "BINDTO");
4097 return;
4098 }
4099 if (GNUNET_OK !=
4102 "MAX_QUEUE_LENGTH",
4104 {
4106 }
4107 if (GNUNET_OK !=
4110 "REKEY_INTERVAL",
4112 {
4114 }
4115 if (GNUNET_OK !=
4118 "REKEY_MAX_BYTES",
4120 {
4122 }
4124 if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
4125 (GNUNET_YES ==
4128 "DISABLE_V6")))
4129 {
4131 }
4133 if (NULL == peerstore)
4134 {
4135 GNUNET_free (bindto);
4136 GNUNET_break (0);
4138 return;
4139 }
4140
4142
4143 if (1 == sscanf (bindto, "%u%1s", &bind_port, dummy))
4144 {
4148 "address po %s\n",
4150 if (NULL != po->addr_ipv4)
4151 {
4153 }
4154 if (NULL != po->addr_ipv6)
4155 {
4158 }
4159 GNUNET_free (po);
4160 nat_register ();
4161 GNUNET_free (bindto);
4162 return;
4163 }
4164
4165 start = extract_address (bindto);
4166 // FIXME: check for NULL == start...
4167 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
4168 {
4169 bind_port = extract_port (bindto);
4170
4172 init_socket (in, in_len);
4173 nat_register ();
4175 GNUNET_free (bindto);
4176 return;
4177 }
4178
4179 if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
4180 {
4181 bind_port = extract_port (bindto);
4183 init_socket (in, in_len);
4184 nat_register ();
4186 GNUNET_free (bindto);
4187 return;
4188 }
4189
4190 bind_port = extract_port (bindto);
4192 ":",
4193 &rest),
4194 AF_UNSPEC,
4197 &port);
4198
4199 GNUNET_free (bindto);
4201}
4202
4203
4211int
4212main (int argc, char *const *argv)
4213{
4214 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
4216 };
4217 int ret;
4218
4220 "Starting tcp communicator\n");
4221
4222 ret = (GNUNET_OK ==
4224 argc,
4225 argv,
4226 "gnunet-communicator-tcp",
4227 _ ("GNUnet TCP communicator"),
4228 options,
4229 &run,
4230 NULL))
4231 ? 0
4232 : 1;
4233 return ret;
4234}
4235
4236
4237/* end of gnunet-communicator-tcp.c */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
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 uint16_t port
Port number.
Definition: gnunet-bcd.c:146
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 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.
static void do_shutdown(void *cls)
Shutdown the UNIX communicator.
#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 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 DEFAULT_REKEY_INTERVAL
How often do we rekey based on time (at least)
int main(int argc, char *const *argv)
The main function for the UNIX communicator.
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 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_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 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.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
Core service; the main API for encrypted P2P communications.
API to the peerstore service.
Constants for network protocols.
Functions related to doing DNS lookups.
API to create, modify and access statistics.
API of the transport service towards the communicator processes.
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_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.
Definition: crypto_hpke.c:407
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...
Definition: crypto_hpke.c:470
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...
Definition: crypto_hpke.c:449
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_kem_encaps(const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_CRYPTO_HpkeEncapsulation *c, struct GNUNET_ShortHashCode *prk)
Encapsulate key material for a EdDSA public key.
Definition: crypto_hpke.c:312
#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.
struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_CRYPTO_eddsa_key_create_from_configuration(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a new private key by reading our peer's key from the file specified in the configuration.
@ 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.
#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.
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.
Definition: crypto_hkdf.c:156
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...
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32.
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.
Definition: crypto_hash.c:363
#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.
Definition: crypto_hash.c:387
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hpke_sk_to_x25519(const struct GNUNET_CRYPTO_PrivateKey *sk, struct GNUNET_CRYPTO_EcdhePrivateKey *x25519)
Convert a GNUnet identity key to a key sutiable for HPKE (X25519)
Definition: crypto_hpke.c:1013
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hpke_pk_to_x25519(const struct GNUNET_CRYPTO_PublicKey *pk, struct GNUNET_CRYPTO_EcdhePublicKey *x25519)
Convert a GNUnet identity key to a key sutiable for HPKE (X25519)
Definition: crypto_hpke.c:989
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.
Definition: crypto_hash.c:347
void GNUNET_CRYPTO_hmac_raw(const void *key, size_t key_len, const void *plaintext, size_t plaintext_len, struct GNUNET_HashCode *hmac)
Calculate HMAC of a message (RFC 2104) TODO: Shouldn't this be the standard hmac function and the abo...
Definition: crypto_hash.c:297
@ 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_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.
Definition: gnunet_nt_lib.h:44
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.
Definition: resolver_api.c:940
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:579
#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:640
#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:178
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition: scheduler.c:431
#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.
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.
Definition of a command line option.
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.
Definition: peerstore_api.c:44
Context for a iterate request.
Single PEERSTORE record.
Context for a store request.
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.
Definition: resolver_api.c:104
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.
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.
struct GNUNET_SCHEDULER_Task * read_task
ID of read task for this connection.
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.
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.
struct sockaddr * address
Address of the other peer.
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?
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 GNUNET_SCHEDULER_Task * write_task
ID of write task for this connection.
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.
struct GNUNET_CRYPTO_ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Purpose must be GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE.
struct GNUNET_CRYPTO_HpkeEncapsulation ephemeral
Ephemeral key used by the sender (as Elligator representative).
struct GNUNET_PeerIdentity sender
Identity of the inititor of the TCP connection (TCP client).
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
struct GNUNET_PeerIdentity receiver
Presumed identity of the target of the TCP connection (TCP server)
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...