GNUnet debian-0.24.3-26-g2202bbd4d
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_pils_service.h"
34#include "gnunet_core_service.h"
36#include "gnunet_protocols.h"
37#include "gnunet_signatures.h"
38#include "gnunet_constants.h"
39#include "gnunet_nat_service.h"
43
44/* Shorthand for Logging */
45#define LOG(kind, ...) GNUNET_log_from (kind, "communicator-tcp", __VA_ARGS__)
46
47
52#define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
53
58#define ADDRESS_VALIDITY_PERIOD \
59 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
60
69#define DEFAULT_MAX_QUEUE_LENGTH 8
70
75#define BUF_SIZE (2 * 64 * 1024 + sizeof(struct TCPBox))
76
80#define DEFAULT_REKEY_INTERVAL GNUNET_TIME_UNIT_DAYS
81
85#define PROTO_QUEUE_TIMEOUT GNUNET_TIME_UNIT_MINUTES
86
91#define REKEY_MAX_BYTES (1024LLU * 1024 * 400)
92
97#define INITIAL_KX_SIZE \
98 (sizeof(struct GNUNET_CRYPTO_EcdhePublicKey) \
99 + sizeof(struct TCPConfirmation))
100
104#define INITIAL_CORE_KX_SIZE \
105 (sizeof(struct EphemeralKeyMessage) \
106 + sizeof(struct PingMessage) \
107 + sizeof(struct PongMessage))
108
112#define COMMUNICATOR_ADDRESS_PREFIX "tcp"
113
117#define COMMUNICATOR_CONFIG_SECTION "communicator-tcp"
118
120
121
127{
132
137
142
147
153
158};
159
165{
170
175
180
186
191};
192
197{
202
207
213
218
219};
220
225{
226
227
232
237
242
248
253
254};
255
259struct TCPBox
260{
268
278
279 /* followed by as may bytes of payload as indicated in @e header,
280 excluding the TCPBox itself! */
281};
282
283
289{
294
304
309
314
320};
321
327{
332
337
342
347
353};
354
361{
366
376};
377
383{
388
393};
394
396
401{
402 /*
403 * Timeout task.
404 */
406
411
415 struct sockaddr *in;
416};
417
422{
427
432};
433
437struct Queue
438{
443
448
453
458
462 gcry_cipher_hd_t in_cipher;
463
467 gcry_cipher_hd_t out_cipher;
468
473
478
484
489
494
498 struct sockaddr *address;
499
505
511
515 socklen_t address_len;
516
521
526
530 unsigned long long bytes_in_queue;
531
536
541
545 char pread_buf[UINT16_MAX + 1 + sizeof(struct TCPBox)];
546
550 char pwrite_buf[UINT16_MAX + 1 + sizeof(struct TCPBox)];
551
556 size_t cread_off;
557
563
568 size_t pread_off;
569
575
580
588 unsigned int backpressure;
589
594
599
604
609
617
623
628
633
638
643
648
653
658
663
668
673
678
682 // TODO remove?
684
689};
690
691
697{
702
707
712
717
722
726 char write_buf[sizeof (struct TCPNATProbeMessage)];
727
731 size_t write_off;
732
737
741 struct sockaddr *address;
742
746 socklen_t address_len;
747
752
758
762 size_t ibuf_off;
763};
764
769{
773 struct sockaddr *addr_ipv4;
774
778 socklen_t addr_len_ipv4;
779
783 struct sockaddr *addr_ipv6;
784
788 socklen_t addr_len_ipv6;
789
790};
791
795struct Addresses
796{
800 struct Addresses *next;
801
805 struct Addresses *prev;
806
810 struct sockaddr *addr;
811
815 socklen_t addr_len;
816
817};
818
819
823static unsigned long long max_queue_length;
824
829
834
839
844
849
854
858static unsigned long long rekey_max_bytes;
859
864
869
874
878static const struct GNUNET_CONFIGURATION_Handle *cfg;
879
884
888static struct GNUNET_NAT_Handle *nat;
889
893static struct ProtoQueue *proto_head;
894
898static struct ProtoQueue *proto_tail;
899
904
908static struct Addresses *addrs_head;
909
913static struct Addresses *addrs_tail;
914
918static int addrs_lens;
919
924
929
933static int disable_v6;
934
938static unsigned int bind_port;
939
944
948static unsigned char ikm[256 / 8];
949
957static void
958listen_cb (void *cls);
959
960static void
963{
965 key.type = htonl (GNUNET_PUBLIC_KEY_TYPE_EDDSA);
966 key.eddsa_key = *edpk;
968}
969
970
971static void
974{
976 key.type = htonl (GNUNET_PUBLIC_KEY_TYPE_EDDSA);
977 key.eddsa_key = *edpk;
979}
980
981
989static void
991{
992 struct ListenTask *lt = NULL;
993 struct GNUNET_HashCode h_sock;
994 int sockfd;
995
996 if (NULL != queue->listen_sock)
997 {
998 sockfd = GNUNET_NETWORK_get_fd (queue->listen_sock);
999 GNUNET_CRYPTO_hash (&sockfd,
1000 sizeof(int),
1001 &h_sock);
1002
1004 }
1005
1007 "Disconnecting queue for peer `%s'\n",
1008 GNUNET_i2s (&queue->target));
1009 if (NULL != queue->rekey_monotime_sc)
1010 {
1011 GNUNET_PEERSTORE_store_cancel (queue->rekey_monotime_sc);
1012 queue->rekey_monotime_sc = NULL;
1013 }
1014 if (NULL != queue->handshake_monotime_sc)
1015 {
1016 GNUNET_PEERSTORE_store_cancel (queue->handshake_monotime_sc);
1017 queue->handshake_monotime_sc = NULL;
1018 }
1019 if (NULL != queue->handshake_ack_monotime_sc)
1020 {
1021 GNUNET_PEERSTORE_store_cancel (queue->handshake_ack_monotime_sc);
1022 queue->handshake_ack_monotime_sc = NULL;
1023 }
1024 if (NULL != queue->rekey_monotime_get)
1025 {
1026 GNUNET_PEERSTORE_iteration_stop (queue->rekey_monotime_get);
1027 queue->rekey_monotime_get = NULL;
1028 }
1029 if (NULL != queue->handshake_monotime_get)
1030 {
1031 GNUNET_PEERSTORE_iteration_stop (queue->handshake_monotime_get);
1032 queue->handshake_monotime_get = NULL;
1033 }
1034 if (NULL != queue->handshake_ack_monotime_get)
1035 {
1036 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
1037 queue->handshake_ack_monotime_get = NULL;
1038 }
1039 if (NULL != queue->qh)
1040 {
1042 queue->qh = NULL;
1043 }
1045 GNUNET_YES ==
1048 "# queues active",
1050 GNUNET_NO);
1051 if (NULL != queue->read_task)
1052 {
1053 GNUNET_SCHEDULER_cancel (queue->read_task);
1054 queue->read_task = NULL;
1055 }
1056 if (NULL != queue->write_task)
1057 {
1058 GNUNET_SCHEDULER_cancel (queue->write_task);
1059 queue->write_task = NULL;
1060 }
1062 {
1064 "closing socket failed\n");
1065 }
1066 gcry_cipher_close (queue->in_cipher);
1067 gcry_cipher_close (queue->out_cipher);
1068 GNUNET_free (queue->address);
1069 if (0 != queue->backpressure)
1070 queue->destroyed = GNUNET_YES;
1071 else
1073
1074 if (NULL == lt)
1075 return;
1076
1077 if ((! shutdown_running) && (NULL == lt->listen_task))
1078 {
1080 "add read net listen\n");
1083 lt->listen_sock,
1084 &listen_cb,
1085 lt);
1086 }
1087 else
1088 GNUNET_free (lt);
1089}
1090
1091
1100static void
1101calculate_hmac (struct GNUNET_HashCode *hmac_secret,
1102 const void *buf,
1103 size_t buf_size,
1104 struct GNUNET_ShortHashCode *smac)
1105{
1106 struct GNUNET_HashCode mac;
1107
1108 GNUNET_CRYPTO_hmac_raw (hmac_secret,
1109 sizeof(struct GNUNET_HashCode),
1110 buf,
1111 buf_size,
1112 &mac);
1113 /* truncate to `struct GNUNET_ShortHashCode` */
1114 memcpy (smac, &mac, sizeof(struct GNUNET_ShortHashCode));
1115 /* ratchet hmac key */
1116 GNUNET_CRYPTO_hash (hmac_secret,
1117 sizeof(struct GNUNET_HashCode),
1118 hmac_secret);
1119}
1120
1121
1128static void
1130{
1131 struct TCPFinish fin;
1132
1133 memset (&fin, 0, sizeof(fin));
1134 fin.header.size = htons (sizeof(fin));
1136 calculate_hmac (&queue->out_hmac, &fin, sizeof(fin), &fin.hmac);
1137 /* if there is any message left in pwrite_buf, we
1138 overwrite it (possibly dropping the last message
1139 from CORE hard here) */
1140 memcpy (queue->pwrite_buf, &fin, sizeof(fin));
1141 queue->pwrite_off = sizeof(fin);
1142 /* This flag will ensure that #queue_write() no longer
1143 notifies CORE about the possibility of sending
1144 more data, and that #queue_write() will call
1145 #queue_destroy() once the @c fin was fully written. */
1146 queue->finishing = GNUNET_YES;
1147}
1148
1149
1155static void
1156queue_read (void *cls);
1157
1158
1166static void
1167core_read_finished_cb (void *cls, int success)
1168{
1169 struct Queue *queue = cls;
1170 if (GNUNET_OK != success)
1172 "# messages lost in communicator API towards CORE",
1173 1,
1174 GNUNET_NO);
1175 if (NULL == queue)
1176 return;
1177
1179 "backpressure %u\n",
1180 queue->backpressure);
1181
1182 queue->backpressure--;
1183 /* handle deferred queue destruction */
1184 if ((queue->destroyed) && (0 == queue->backpressure))
1185 {
1187 return;
1188 }
1189 else if (GNUNET_YES != queue->destroyed)
1190 {
1191 queue->timeout =
1193 );
1194 /* possibly unchoke reading, now that CORE made progress */
1195 if (NULL == queue->read_task)
1196 queue->read_task =
1198 queue->timeout),
1199 queue->sock,
1200 &queue_read,
1201 queue);
1202 }
1203}
1204
1205
1215static void
1217 const void *plaintext,
1218 size_t plaintext_len)
1219{
1220 const struct GNUNET_MessageHeader *hdr = plaintext;
1221 int ret;
1222
1224 "pass message from %s to core\n",
1225 GNUNET_i2s (&queue->target));
1226
1227 if (ntohs (hdr->size) != plaintext_len)
1228 {
1229 /* NOTE: If we ever allow multiple CORE messages in one
1230 BOX, this will have to change! */
1231 GNUNET_break (0);
1232 return;
1233 }
1235 &queue->target,
1236 hdr,
1239 queue);
1241 "passed to core\n");
1242 if (GNUNET_OK == ret)
1243 queue->backpressure++;
1244 GNUNET_break (GNUNET_NO != ret); /* backpressure not working!? */
1245 if (GNUNET_SYSERR == ret)
1247 "# bytes lost due to CORE not running",
1248 plaintext_len,
1249 GNUNET_NO);
1250}
1251
1252
1262static void
1264 const struct GNUNET_PeerIdentity *pid,
1265 gcry_cipher_hd_t *cipher,
1266 struct GNUNET_HashCode *hmac_key)
1267{
1268 char key[256 / 8];
1269 char ctr[128 / 8];
1270
1271 GNUNET_assert (0 == gcry_cipher_open (cipher,
1272 GCRY_CIPHER_AES256 /* low level: go for speed */
1273 ,
1274 GCRY_CIPHER_MODE_CTR,
1275 0 /* flags */));
1278 sizeof(key),
1279 prk,
1280 "gnunet-communicator-tcp-key",
1281 strlen (
1282 "gnunet-communicator-tcp-key"),
1283 NULL,
1284 0));
1285 GNUNET_assert (0 == gcry_cipher_setkey (*cipher, key, sizeof(key)));
1288 sizeof(ctr),
1289 prk,
1290 "gnunet-communicator-tcp-ctr",
1291 strlen (
1292 "gnunet-communicator-tcp-ctr"),
1293 NULL,
1294 0));
1295 gcry_cipher_setctr (*cipher, ctr, sizeof(ctr));
1297 GNUNET_CRYPTO_hkdf_expand (hmac_key,
1298 sizeof(struct GNUNET_HashCode),
1299 prk,
1300 "gnunet-communicator-hmac",
1301 strlen ("gnunet-communicator-hmac"),
1302 NULL,
1303 0));
1304}
1305
1306
1312static void
1313rekey_monotime_store_cb (void *cls, int success)
1314{
1315 struct Queue *queue = cls;
1316 if (GNUNET_OK != success)
1317 {
1319 "Failed to store rekey monotonic time in PEERSTORE!\n");
1320 }
1321 queue->rekey_monotime_sc = NULL;
1322 GNUNET_PEERSTORE_iteration_next (queue->rekey_monotime_get, 1);
1323}
1324
1325
1333static void
1335 const struct GNUNET_PEERSTORE_Record *record,
1336 const char *emsg)
1337{
1338 struct Queue *queue = cls;
1339 struct GNUNET_TIME_AbsoluteNBO *mtbe;
1340 struct GNUNET_TIME_Absolute mt;
1341 const struct GNUNET_PeerIdentity *pid;
1342 struct GNUNET_TIME_AbsoluteNBO *rekey_monotonic_time;
1343
1344 (void) emsg;
1345
1346 rekey_monotonic_time = &queue->rekey_monotonic_time;
1347 pid = &queue->target;
1348 if (NULL == record)
1349 {
1350 queue->rekey_monotime_get = NULL;
1351 return;
1352 }
1353 if (sizeof(*mtbe) != record->value_size)
1354 {
1355 GNUNET_PEERSTORE_iteration_next (queue->rekey_monotime_get, 1);
1356 GNUNET_break (0);
1357 return;
1358 }
1359 mtbe = record->value;
1360 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1362 queue->rekey_monotonic_time).abs_value_us)
1363 {
1365 "Queue from %s dropped, rekey monotime in the past\n",
1366 GNUNET_i2s (&queue->target));
1367 GNUNET_break (0);
1368 GNUNET_PEERSTORE_iteration_stop (queue->rekey_monotime_get);
1369 queue->rekey_monotime_get = NULL;
1370 // FIXME: Why should we try to gracefully finish here??
1372 return;
1373 }
1374 queue->rekey_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
1375 "transport_tcp_communicator",
1376 pid,
1378 rekey_monotonic_time,
1379 sizeof(*
1380 rekey_monotonic_time),
1384 queue);
1385}
1386
1387
1394static void
1396 const struct GNUNET_CRYPTO_HpkeEncapsulation *c,
1397 struct Queue *queue)
1398{
1399 struct GNUNET_ShortHashCode k;
1400
1402 setup_cipher (&k, &my_identity, &queue->in_cipher, &queue->in_hmac);
1403}
1404
1405
1412static void
1414 struct Queue *queue)
1415{
1416 struct GNUNET_ShortHashCode k;
1417
1419 setup_cipher (&k, &my_identity, &queue->in_cipher, &queue->in_hmac);
1420}
1421
1422
1431static void
1432do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
1433{
1434 struct TcpRekeySignature thp;
1435
1437 thp.purpose.size = htonl (sizeof(thp));
1439 "do_rekey size %u\n",
1440 thp.purpose.size);
1441 thp.sender = queue->target;
1443 "sender %s\n",
1446 "sender %s\n",
1447 GNUNET_p2s (&queue->target.public_key));
1448 thp.receiver = my_identity;
1450 "receiver %s\n",
1452 thp.ephemeral = rekey->ephemeral;
1454 "ephemeral %s\n",
1456 );
1457 thp.monotonic_time = rekey->monotonic_time;
1459 "time %s\n",
1462 GNUNET_assert (ntohl ((&thp)->purpose.size) == sizeof (*(&thp)));
1463 if (GNUNET_OK !=
1466 &thp,
1467 &rekey->sender_sig,
1468 &queue->target.public_key))
1469 {
1470 GNUNET_break (0);
1471 // FIXME Why should we try to gracefully finish here?
1473 return;
1474 }
1475 queue->rekey_monotonic_time = rekey->monotonic_time;
1476 queue->rekey_monotime_get = GNUNET_PEERSTORE_iteration_start (peerstore,
1477 "transport_tcp_communicator",
1478 &queue->target,
1480 &
1482 queue);
1483 gcry_cipher_close (queue->in_cipher);
1484 queue->rekeyed = GNUNET_YES;
1485 setup_in_cipher (&rekey->ephemeral, queue);
1486}
1487
1488
1494static void
1495handshake_ack_monotime_store_cb (void *cls, int success)
1496{
1497 struct Queue *queue = cls;
1498
1499 if (GNUNET_OK != success)
1500 {
1502 "Failed to store handshake ack monotonic time in PEERSTORE!\n");
1503 }
1504 queue->handshake_ack_monotime_sc = NULL;
1505 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
1506}
1507
1508
1516static void
1518 const struct GNUNET_PEERSTORE_Record *record,
1519 const char *emsg)
1520{
1521 struct Queue *queue = cls;
1522 struct GNUNET_TIME_AbsoluteNBO *mtbe;
1523 struct GNUNET_TIME_Absolute mt;
1524 const struct GNUNET_PeerIdentity *pid;
1525 struct GNUNET_TIME_AbsoluteNBO *handshake_ack_monotonic_time;
1526
1527 (void) emsg;
1528
1529 handshake_ack_monotonic_time = &queue->handshake_ack_monotonic_time;
1530 pid = &queue->target;
1531 if (NULL == record)
1532 {
1533 queue->handshake_ack_monotime_get = NULL;
1534 return;
1535 }
1536 if (sizeof(*mtbe) != record->value_size)
1537 {
1538 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
1539 GNUNET_break (0);
1540 return;
1541 }
1542 mtbe = record->value;
1543 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1545 queue->handshake_ack_monotonic_time).abs_value_us)
1546 {
1548 "Queue from %s dropped, handshake ack monotime in the past\n",
1549 GNUNET_i2s (&queue->target));
1550 GNUNET_break (0);
1551 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
1552 queue->handshake_ack_monotime_get = NULL;
1553 // FIXME: Why should we try to gracefully finish here?
1555 return;
1556 }
1557 queue->handshake_ack_monotime_sc =
1559 "transport_tcp_communicator",
1560 pid,
1562 handshake_ack_monotonic_time,
1563 sizeof(*handshake_ack_monotonic_time),
1567 queue);
1568}
1569
1570
1577static void
1579 struct Queue *queue)
1580{
1581 struct TCPConfirmationAck tca;
1582 struct TcpHandshakeAckSignature thas;
1583
1585 "sending challenge\n");
1586
1587 tca.header.type = ntohs (
1589 tca.header.size = ntohs (sizeof(tca));
1590 tca.challenge = challenge;
1591 tca.sender = my_identity;
1592 tca.monotonic_time =
1594 thas.purpose.purpose = htonl (
1596 thas.purpose.size = htonl (sizeof(thas));
1597 thas.sender = my_identity;
1598 thas.receiver = queue->target;
1599 thas.monotonic_time = tca.monotonic_time;
1600 thas.challenge = tca.challenge;
1602 &thas,
1603 &tca.sender_sig);
1604 GNUNET_assert (0 ==
1605 gcry_cipher_encrypt (queue->out_cipher,
1606 &queue->cwrite_buf[queue->cwrite_off],
1607 sizeof(tca),
1608 &tca,
1609 sizeof(tca)));
1610 queue->cwrite_off += sizeof(tca);
1612 "sending challenge done\n");
1613}
1614
1615
1622static void
1624{
1625 setup_cipher (dh, &queue->target, &queue->out_cipher, &queue->out_hmac);
1627 queue->rekey_left_bytes =
1629}
1630
1631
1638static void
1640{
1641 struct TCPRekey rekey;
1642 struct TcpRekeySignature thp;
1643 struct GNUNET_ShortHashCode k;
1644
1645 GNUNET_assert (0 == queue->pwrite_off);
1646 memset (&rekey, 0, sizeof(rekey));
1647 GNUNET_CRYPTO_eddsa_kem_encaps (&queue->target.public_key, &rekey.ephemeral,
1648 &k);
1650 rekey.header.size = ntohs (sizeof(rekey));
1651 rekey.monotonic_time =
1654 thp.purpose.size = htonl (sizeof(thp));
1656 "inject_rekey size %u\n",
1657 thp.purpose.size);
1658 thp.sender = my_identity;
1660 "sender %s\n",
1662 thp.receiver = queue->target;
1664 "receiver %s\n",
1666 thp.ephemeral = rekey.ephemeral;
1668 "ephemeral %s\n",
1670 );
1671 thp.monotonic_time = rekey.monotonic_time;
1673 "time %s\n",
1677 &thp,
1678 &rekey.sender_sig);
1679 calculate_hmac (&queue->out_hmac, &rekey, sizeof(rekey), &rekey.hmac);
1680 /* Encrypt rekey message with 'old' cipher */
1681 GNUNET_assert (0 ==
1682 gcry_cipher_encrypt (queue->out_cipher,
1683 &queue->cwrite_buf[queue->cwrite_off],
1684 sizeof(rekey),
1685 &rekey,
1686 sizeof(rekey)));
1687 queue->cwrite_off += sizeof(rekey);
1688 /* Setup new cipher for successive messages */
1689 gcry_cipher_close (queue->out_cipher);
1690 setup_out_cipher (queue, &k);
1691}
1692
1693
1694static int
1696 const struct GNUNET_HashCode *key,
1697 void *value)
1698{
1699 struct PendingReversal *pending_reversal = value;
1700 (void) cls;
1701
1702 if (NULL != pending_reversal->timeout_task)
1703 {
1704 GNUNET_SCHEDULER_cancel (pending_reversal->timeout_task);
1705 pending_reversal->timeout_task = NULL;
1706 }
1709 key,
1710 pending_reversal));
1711 GNUNET_free (pending_reversal->in);
1712 GNUNET_free (pending_reversal);
1713 return GNUNET_OK;
1714}
1715
1716
1717static void
1718check_and_remove_pending_reversal (struct sockaddr *in, sa_family_t sa_family,
1719 struct GNUNET_PeerIdentity *sender)
1720{
1721 if (AF_INET == sa_family)
1722 {
1723 struct PendingReversal *pending_reversal;
1724 struct GNUNET_HashCode key;
1725 struct sockaddr_in *natted_address;
1726
1727 natted_address = GNUNET_memdup (in, sizeof (struct sockaddr));
1728 natted_address->sin_port = 0;
1729 GNUNET_CRYPTO_hash (natted_address,
1730 sizeof(struct sockaddr),
1731 &key);
1732
1734 &key);
1735 if (NULL != pending_reversal && (NULL == sender ||
1736 0 != memcmp (sender,
1737 &pending_reversal->target,
1738 sizeof(struct
1740 {
1742 "Removing invalid pending reversal for `%s'at `%s'\n",
1743 GNUNET_i2s (&pending_reversal->target),
1744 GNUNET_a2s (in, sizeof (struct sockaddr)));
1745 pending_reversals_delete_it (NULL, &key, pending_reversal);
1746 }
1747 GNUNET_free (natted_address);
1748 }
1749}
1750
1751
1757static void
1759{
1760 if (NULL != pq->listen_sock)
1761 {
1763 pq->listen_sock = NULL;
1764 }
1765 if (NULL != pq->read_task)
1766 {
1768 pq->read_task = NULL;
1769 }
1770 if (NULL != pq->write_task)
1771 {
1773 pq->write_task = NULL;
1774 }
1775 check_and_remove_pending_reversal (pq->address, pq->address->sa_family, NULL);
1777 GNUNET_free (pq->address);
1779 GNUNET_free (pq);
1780}
1781
1782
1789static void
1791{
1792 struct ProtoQueue *pq = cls;
1793 ssize_t sent;
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In proto queue write\n");
1795 pq->write_task = NULL;
1796 if (0 != pq->write_off)
1797 {
1798 sent = GNUNET_NETWORK_socket_send (pq->sock,
1799 pq->write_buf,
1800 pq->write_off);
1802 "Sent %lu bytes to TCP queue\n", sent);
1803 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1804 {
1806 free_proto_queue (pq);
1807 return;
1808 }
1809 if (sent > 0)
1810 {
1811 size_t usent = (size_t) sent;
1812 pq->write_off -= usent;
1813 memmove (pq->write_buf,
1814 &pq->write_buf[usent],
1815 pq->write_off);
1816 }
1817 }
1818 /* do we care to write more? */
1819 if ((0 < pq->write_off))
1820 pq->write_task =
1822 pq->sock,
1824 pq);
1825}
1826
1827
1834static void
1835queue_write (void *cls)
1836{
1837 struct Queue *queue = cls;
1838 ssize_t sent;
1839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In queue write\n");
1840 queue->write_task = NULL;
1841 if (0 != queue->cwrite_off)
1842 {
1843 sent = GNUNET_NETWORK_socket_send (queue->sock,
1844 queue->cwrite_buf,
1845 queue->cwrite_off);
1847 "Sent %lu bytes to TCP queue\n", sent);
1848 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1849 {
1852 return;
1853 }
1854 if (sent > 0)
1855 {
1856 size_t usent = (size_t) sent;
1857 queue->cwrite_off -= usent;
1858 memmove (queue->cwrite_buf,
1859 &queue->cwrite_buf[usent],
1860 queue->cwrite_off);
1861 queue->timeout =
1864 }
1865 }
1866 {
1867 /* can we encrypt more? (always encrypt full messages, needed
1868 such that #mq_cancel() can work!) */
1869 unsigned int we_do_not_need_to_rekey = (0 < queue->rekey_left_bytes
1870 - (queue->cwrite_off
1871 + queue->pwrite_off
1872 + sizeof (struct TCPRekey)));
1873 if (we_do_not_need_to_rekey &&
1874 (queue->pwrite_off > 0) &&
1875 (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE))
1876 {
1878 "Encrypting %lu bytes\n", queue->pwrite_off);
1879 GNUNET_assert (0 ==
1880 gcry_cipher_encrypt (queue->out_cipher,
1881 &queue->cwrite_buf[queue->cwrite_off],
1882 queue->pwrite_off,
1883 queue->pwrite_buf,
1884 queue->pwrite_off));
1885 if (queue->rekey_left_bytes > queue->pwrite_off)
1886 queue->rekey_left_bytes -= queue->pwrite_off;
1887 else
1888 queue->rekey_left_bytes = 0;
1889 queue->cwrite_off += queue->pwrite_off;
1890 queue->pwrite_off = 0;
1891 }
1892 // if ((-1 != unverified_size)&& ((0 == queue->pwrite_off) &&
1893 if (((0 == queue->rekey_left_bytes) ||
1895 queue->rekey_time).rel_value_us)) &&
1896 (((0 == queue->pwrite_off) || ! we_do_not_need_to_rekey) &&
1897 (queue->cwrite_off + sizeof (struct TCPRekey) <= BUF_SIZE)))
1898 {
1900 }
1901 }
1902 if ((0 == queue->pwrite_off) && (! queue->finishing) &&
1903 (GNUNET_YES == queue->mq_awaits_continue))
1904 {
1905 queue->mq_awaits_continue = GNUNET_NO;
1907 }
1908 /* did we just finish writing 'finish'? */
1909 if ((0 == queue->cwrite_off) && (GNUNET_YES == queue->finishing))
1910 {
1912 "Finishing queue\n");
1914 return;
1915 }
1916 /* do we care to write more? */
1917 if ((0 < queue->cwrite_off) || (0 < queue->pwrite_off))
1918 queue->write_task =
1920 queue->sock,
1921 &queue_write,
1922 queue);
1923}
1924
1925
1933static size_t
1935{
1936 const struct GNUNET_MessageHeader *hdr;
1937 const struct TCPConfirmationAck *tca;
1938 const struct TCPBox *box;
1939 const struct TCPRekey *rekey;
1940 const struct TCPFinish *fin;
1941 struct TCPRekey rekeyz;
1942 struct TCPFinish finz;
1943 struct GNUNET_ShortHashCode tmac;
1944 uint16_t type;
1945 size_t size = 0;
1946 struct TcpHandshakeAckSignature thas;
1947 const struct GNUNET_CRYPTO_ChallengeNonceP challenge = queue->challenge;
1948
1950 "try handle plaintext!\n");
1951
1952 hdr = (const struct GNUNET_MessageHeader *) queue->pread_buf;
1953 if ((sizeof(*hdr) > queue->pread_off))
1954 {
1956 "Handling plaintext, not even a header!\n");
1957 return 0; /* not even a header */
1958 }
1959
1960 if ((GNUNET_YES != queue->initial_core_kx_done) && (queue->unverified_size >
1962 {
1964 "Already received data of size %lu bigger than KX size %lu!\n",
1965 queue->unverified_size,
1967 GNUNET_break_op (0);
1969 return 0;
1970 }
1971
1972 type = ntohs (hdr->type);
1973 switch (type)
1974 {
1976 tca = (const struct TCPConfirmationAck *) queue->pread_buf;
1978 "start processing ack\n");
1979 if (sizeof(*tca) > queue->pread_off)
1980 {
1982 "Handling plaintext size of tca greater than pread offset.\n")
1983 ;
1984 return 0;
1985 }
1986 if (ntohs (hdr->size) != sizeof(*tca))
1987 {
1989 "Handling plaintext size does not match message type.\n");
1990 GNUNET_break_op (0);
1992 return 0;
1993 }
1994
1995 thas.purpose.purpose = htonl (
1997 thas.purpose.size = htonl (sizeof(thas));
1998 thas.sender = tca->sender;
1999 thas.receiver = my_identity;
2000 thas.monotonic_time = tca->monotonic_time;
2001 thas.challenge = tca->challenge;
2002
2005 &thas,
2006 &tca->sender_sig,
2007 &tca->sender.public_key))
2008 {
2010 "Verification of signature failed!\n");
2011 GNUNET_break (0);
2013 return 0;
2014 }
2015 if (0 != GNUNET_memcmp (&tca->challenge, &challenge))
2016 {
2018 "Challenge in TCPConfirmationAck not correct!\n");
2019 GNUNET_break (0);
2021 return 0;
2022 }
2023
2024 queue->handshake_ack_monotime_get = GNUNET_PEERSTORE_iteration_start (
2025 peerstore,
2026 "transport_tcp_communicator",
2027 &queue->target,
2030 queue);
2031
2033 "Handling plaintext, ack processed!\n");
2034
2036 {
2037 send_challenge (queue->challenge_received, queue);
2038 queue->write_task =
2040 queue->sock,
2041 &queue_write,
2042 queue);
2043 }
2044 else if (GNUNET_TRANSPORT_CS_OUTBOUND == queue->cs)
2045 {
2047 queue->address->sa_family, NULL);
2048 }
2049
2054 queue->initial_core_kx_done = GNUNET_YES;
2055
2056 {
2057 char *foreign_addr;
2058
2059 switch (queue->address->sa_family)
2060 {
2061 case AF_INET:
2062 GNUNET_asprintf (&foreign_addr,
2063 "%s-%s",
2065 GNUNET_a2s (queue->address, queue->address_len));
2066 break;
2067
2068 case AF_INET6:
2069 GNUNET_asprintf (&foreign_addr,
2070 "%s-%s",
2072 GNUNET_a2s (queue->address, queue->address_len));
2073 break;
2074
2075 default:
2076 GNUNET_assert (0);
2077 }
2079 &queue->target,
2080 foreign_addr,
2081 UINT16_MAX, /* no MTU */
2083 0, /* Priority */
2084 queue->nt,
2085 queue->cs,
2086 queue->mq);
2087
2088 GNUNET_free (foreign_addr);
2089 }
2090
2091 size = ntohs (hdr->size);
2092 break;
2094 /* Special case: header size excludes box itself! */
2095 box = (const struct TCPBox *) queue->pread_buf;
2096 if (ntohs (hdr->size) + sizeof(struct TCPBox) > queue->pread_off)
2097 return 0;
2098 calculate_hmac (&queue->in_hmac, &box[1], ntohs (hdr->size), &tmac);
2099 if (0 != memcmp (&tmac, &box->hmac, sizeof(tmac)))
2100 {
2101 GNUNET_break_op (0);
2103 return 0;
2104 }
2105 pass_plaintext_to_core (queue, (const void *) &box[1], ntohs (hdr->size));
2106 size = ntohs (hdr->size) + sizeof(*box);
2108 "Handling plaintext, box processed!\n");
2110 "# bytes decrypted with BOX",
2111 size,
2112 GNUNET_NO);
2114 "# messages decrypted with BOX",
2115 1,
2116 GNUNET_NO);
2117 break;
2118
2120 rekey = (const struct TCPRekey *) queue->pread_buf;
2121 if (sizeof(*rekey) > queue->pread_off)
2122 return 0;
2123 if (ntohs (hdr->size) != sizeof(*rekey))
2124 {
2125 GNUNET_break_op (0);
2127 return 0;
2128 }
2129 rekeyz = *rekey;
2130 memset (&rekeyz.hmac, 0, sizeof(rekeyz.hmac));
2131 calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
2132 if (0 != memcmp (&tmac, &rekey->hmac, sizeof(tmac)))
2133 {
2134 GNUNET_break_op (0);
2136 return 0;
2137 }
2138 do_rekey (queue, rekey);
2139 size = ntohs (hdr->size);
2141 "Handling plaintext, rekey processed!\n");
2143 "# rekeying successful",
2144 1,
2145 GNUNET_NO);
2146 break;
2147
2149 fin = (const struct TCPFinish *) queue->pread_buf;
2150 if (sizeof(*fin) > queue->pread_off)
2151 return 0;
2152 if (ntohs (hdr->size) != sizeof(*fin))
2153 {
2154 GNUNET_break_op (0);
2156 return 0;
2157 }
2158 finz = *fin;
2159 memset (&finz.hmac, 0, sizeof(finz.hmac));
2160 calculate_hmac (&queue->in_hmac, &finz, sizeof(finz), &tmac);
2161 if (0 != memcmp (&tmac, &fin->hmac, sizeof(tmac)))
2162 {
2163 GNUNET_break_op (0);
2165 return 0;
2166 }
2167 /* handle FINISH by destroying queue */
2170 "Handling plaintext, finish processed!\n");
2171 break;
2172
2173 default:
2175 "Handling plaintext, nothing processed!\n");
2176 GNUNET_break_op (0);
2178 return 0;
2179 }
2180 GNUNET_assert (0 != size);
2181 if (-1 != queue->unverified_size)
2182 queue->unverified_size += size;
2183 return size;
2184}
2185
2186
2192static void
2193queue_read (void *cls)
2194{
2195 struct Queue *queue = cls;
2196 struct GNUNET_TIME_Relative left;
2197 ssize_t rcvd;
2198
2199 queue->read_task = NULL;
2200 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2201 &queue->cread_buf[queue->cread_off],
2202 BUF_SIZE - queue->cread_off);
2204 "Received %zd bytes from TCP queue\n", rcvd);
2205 if (-1 == rcvd)
2206 {
2207 if ((EAGAIN != errno) && (EINTR != errno))
2208 {
2211 return;
2212 }
2213 /* try again */
2214 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2215 if (0 != left.rel_value_us)
2216 {
2217 queue->read_task =
2219 return;
2220 }
2222 "Queue %p was idle for %s, disconnecting\n",
2223 queue,
2226 GNUNET_YES));
2228 return;
2229 }
2230 if (0 == rcvd)
2231 {
2232 /* Orderly shutdown of connection */
2234 "Socket for queue %p seems to have been closed\n", queue);
2236 return;
2237 }
2238 queue->timeout =
2240 queue->cread_off += rcvd;
2241 while ((queue->pread_off < sizeof(queue->pread_buf)) &&
2242 (queue->cread_off > 0))
2243 {
2244 size_t max = GNUNET_MIN (sizeof(queue->pread_buf) - queue->pread_off,
2245 queue->cread_off);
2246 size_t done;
2247 size_t total;
2248 size_t old_pread_off = queue->pread_off;
2249
2250 GNUNET_assert (0 ==
2251 gcry_cipher_decrypt (queue->in_cipher,
2252 &queue->pread_buf[queue->pread_off],
2253 max,
2254 queue->cread_buf,
2255 max));
2256 queue->pread_off += max;
2257 total = 0;
2258 while (0 != (done = try_handle_plaintext (queue)))
2259 {
2260 /* 'done' bytes of plaintext were used, shift buffer */
2261 GNUNET_assert (done <= queue->pread_off);
2262 /* NOTE: this memmove() could possibly sometimes be
2263 avoided if we pass 'total' into try_handle_plaintext()
2264 and use it at an offset into the buffer there! */
2265 memmove (queue->pread_buf,
2266 &queue->pread_buf[done],
2267 queue->pread_off - done);
2268 queue->pread_off -= done;
2269 total += done;
2270 /* The last plaintext was a rekey, abort for now */
2271 if (GNUNET_YES == queue->rekeyed)
2272 break;
2273 }
2274 /* when we encounter a rekey message, the decryption above uses the
2275 wrong key for everything after the rekey; in that case, we have
2276 to re-do the decryption at 'total' instead of at 'max'.
2277 However, we have to take into account that the plaintext buffer may have
2278 already contained data and not jumped too far ahead in the ciphertext.
2279 If there is no rekey and the last message is incomplete (max > total),
2280 it is safe to keep the decryption so we shift by 'max' */
2281 if (GNUNET_YES == queue->rekeyed)
2282 {
2283 max = total - old_pread_off;
2284 queue->rekeyed = GNUNET_NO;
2285 queue->pread_off = 0;
2286 }
2287 memmove (queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
2288 queue->cread_off -= max;
2289 }
2290 if (BUF_SIZE == queue->cread_off)
2291 return; /* buffer full, suspend reading */
2292 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2293 if (0 != left.rel_value_us)
2294 {
2295 if (max_queue_length > queue->backpressure)
2296 {
2297 /* continue reading */
2298 queue->read_task =
2300 }
2301 return;
2302 }
2304 "Queue %p was idle for %s, disconnecting\n",
2305 queue,
2308 GNUNET_YES));
2310}
2311
2312
2320static struct sockaddr *
2322 struct sockaddr_in6 v6,
2323 unsigned int port)
2324{
2325 struct sockaddr *in;
2326
2327 v6.sin6_family = AF_INET6;
2328 v6.sin6_port = htons ((uint16_t) port);
2329#if HAVE_SOCKADDR_IN_SIN_LEN
2330 v6.sin6_len = sizeof(struct sockaddr_in6);
2331#endif
2332 v6.sin6_flowinfo = 0;
2333 v6.sin6_scope_id = 0;
2334 in = GNUNET_memdup (&v6, sizeof(v6));
2335 *sock_len = sizeof(struct sockaddr_in6);
2336
2337 return in;
2338}
2339
2340
2348static struct sockaddr *
2350 struct sockaddr_in v4,
2351 unsigned int port)
2352{
2353 struct sockaddr *in;
2354
2355 v4.sin_family = AF_INET;
2356 v4.sin_port = htons ((uint16_t) port);
2357#if HAVE_SOCKADDR_IN_SIN_LEN
2358 v4.sin_len = sizeof(struct sockaddr_in);
2359#endif
2360 in = GNUNET_memdup (&v4, sizeof(v4));
2361 *sock_len = sizeof(struct sockaddr_in);
2362 return in;
2363}
2364
2365
2372static struct PortOnlyIpv4Ipv6 *
2373tcp_address_to_sockaddr_port_only (const char *bindto, unsigned int *port)
2374{
2375 struct PortOnlyIpv4Ipv6 *po;
2376 struct sockaddr_in *i4;
2377 struct sockaddr_in6 *i6;
2378 socklen_t sock_len_ipv4;
2379 socklen_t sock_len_ipv6;
2380
2381 /* interpreting value as just a PORT number */
2382 if (*port > UINT16_MAX)
2383 {
2385 "BINDTO specification `%s' invalid: value too large for port\n",
2386 bindto);
2387 return NULL;
2388 }
2389
2390 po = GNUNET_new (struct PortOnlyIpv4Ipv6);
2391
2392 if (GNUNET_YES == disable_v6)
2393 {
2394 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2395 po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2396 *port);
2397 po->addr_len_ipv4 = sock_len_ipv4;
2398 }
2399 else
2400 {
2401
2402 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2403 po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2404 *port);
2405 po->addr_len_ipv4 = sock_len_ipv4;
2406
2407 i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
2408 po->addr_ipv6 = tcp_address_to_sockaddr_numeric_v6 (&sock_len_ipv6, *i6,
2409 *port);
2410
2411 po->addr_len_ipv6 = sock_len_ipv6;
2412
2413 GNUNET_free (i6);
2414 }
2415
2416 GNUNET_free (i4);
2417
2418 return po;
2419}
2420
2421
2428static char *
2429extract_address (const char *bindto)
2430{
2431 char *addr;
2432 char *start;
2433 char *token;
2434 char *cp;
2435 char *rest = NULL;
2436
2438 "extract address with bindto %s\n",
2439 bindto);
2440
2441 if (NULL == bindto)
2443 "bindto is NULL\n");
2444
2445 cp = GNUNET_strdup (bindto);
2446
2448 "extract address 2\n");
2449
2450 start = cp;
2451 if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
2452 {
2453 start++; /* skip over '['*/
2454 cp[strlen (cp) - 1] = '\0'; /* eat ']'*/
2455 addr = GNUNET_strdup (start);
2456 }
2457 else
2458 {
2459 token = strtok_r (cp, "]", &rest);
2460 if (strlen (bindto) == strlen (token))
2461 {
2462 token = strtok_r (cp, ":", &rest);
2463 addr = GNUNET_strdup (token);
2464 }
2465 else
2466 {
2467 token++;
2468 addr = GNUNET_strdup (token);
2469 }
2470 }
2471
2473 "tcp address: %s\n",
2474 addr);
2475 GNUNET_free (cp);
2476 return addr;
2477}
2478
2479
2486static unsigned int
2487extract_port (const char *addr_and_port)
2488{
2489 unsigned int port;
2490 char dummy[2];
2491 char *token;
2492 char *addr;
2493 char *colon;
2494 char *cp;
2495 char *rest = NULL;
2496
2497 if (NULL != addr_and_port)
2498 {
2499 cp = GNUNET_strdup (addr_and_port);
2500 token = strtok_r (cp, "]", &rest);
2501 if (strlen (addr_and_port) == strlen (token))
2502 {
2503 colon = strrchr (cp, ':');
2504 if (NULL == colon)
2505 {
2506 GNUNET_free (cp);
2507 return 0;
2508 }
2509 addr = colon;
2510 addr++;
2511 }
2512 else
2513 {
2514 token = strtok_r (NULL, "]", &rest);
2515 if (NULL == token)
2516 {
2517 GNUNET_free (cp);
2518 return 0;
2519 }
2520 else
2521 {
2522 addr = token;
2523 addr++;
2524 }
2525 }
2526
2527
2528 if (1 == sscanf (addr, "%u%1s", &port, dummy))
2529 {
2530 /* interpreting value as just a PORT number */
2531 if (port > UINT16_MAX)
2532 {
2534 "Port `%u' invalid: value too large for port\n",
2535 port);
2536 GNUNET_free (cp);
2537 return 0;
2538 }
2539 }
2540 else
2541 {
2543 "BINDTO specification invalid: last ':' not followed by number\n");
2544 GNUNET_free (cp);
2545 return 0;
2546 }
2547 GNUNET_free (cp);
2548 }
2549 else
2550 {
2552 "return 0\n");
2553 /* interpret missing port as 0, aka pick any free one */
2554 port = 0;
2555 }
2556
2557 return port;
2558}
2559
2560
2568static struct sockaddr *
2569tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
2570{
2571 struct sockaddr *in;
2572 unsigned int port;
2573 struct sockaddr_in v4;
2574 struct sockaddr_in6 v6;
2575 char *start;
2576
2577 memset (&v4, 0, sizeof(v4));
2578 start = extract_address (bindto);
2579 GNUNET_assert (NULL != start);
2581 "start %s\n",
2582 start);
2583
2585 "!bindto %s\n",
2586 bindto);
2587
2588
2589 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
2590 {
2591 port = extract_port (bindto);
2592
2594 "port %u\n",
2595 port);
2596
2597 in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port);
2598 }
2599 else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
2600 {
2601 port = extract_port (bindto);
2602 in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port);
2603 }
2604 else
2605 {
2606 GNUNET_assert (0);
2607 }
2608
2610 return in;
2611}
2612
2613
2622static void
2624 const struct GNUNET_MessageHeader *msg,
2625 void *impl_state)
2626{
2627 struct Queue *queue = impl_state;
2628 uint16_t msize = ntohs (msg->size);
2629 struct TCPBox box;
2631 "In MQ send. Queue finishing: %s; write task running: %s\n",
2632 (GNUNET_YES == queue->finishing) ? "yes" : "no",
2633 (NULL == queue->write_task) ? "yes" : "no");
2634 GNUNET_assert (mq == queue->mq);
2635 queue->mq_awaits_continue = GNUNET_YES;
2636 if (GNUNET_YES == queue->finishing)
2637 return; /* this queue is dying, drop msg */
2638 GNUNET_assert (0 == queue->pwrite_off);
2640 box.header.size = htons (msize);
2641 calculate_hmac (&queue->out_hmac, msg, msize, &box.hmac);
2642 memcpy (&queue->pwrite_buf[queue->pwrite_off], &box, sizeof(box));
2643 queue->pwrite_off += sizeof(box);
2644 memcpy (&queue->pwrite_buf[queue->pwrite_off], msg, msize);
2645 queue->pwrite_off += msize;
2647 "%lu bytes of plaintext to send\n", queue->pwrite_off);
2648 GNUNET_assert (NULL != queue->sock);
2649 if (NULL == queue->write_task)
2650 queue->write_task =
2652 queue->sock,
2653 &queue_write,
2654 queue);
2655}
2656
2657
2666static void
2667mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
2668{
2669 struct Queue *queue = impl_state;
2670
2671 if (mq == queue->mq)
2672 {
2673 queue->mq = NULL;
2675 }
2676}
2677
2678
2685static void
2686mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
2687{
2688 struct Queue *queue = impl_state;
2689
2690 GNUNET_assert (0 != queue->pwrite_off);
2691 queue->pwrite_off = 0;
2692}
2693
2694
2704static void
2705mq_error (void *cls, enum GNUNET_MQ_Error error)
2706{
2707 struct Queue *queue = cls;
2708
2710 "MQ error in queue to %s: %d\n",
2711 GNUNET_i2s (&queue->target),
2712 (int) error);
2714}
2715
2716
2724static void
2726{
2727 queue->nt =
2728 GNUNET_NT_scanner_get_type (is, queue->address, queue->address_len);
2730 queue_map,
2731 &queue->key,
2732 queue,
2735 "# queues active",
2737 GNUNET_NO);
2738 queue->timeout =
2741 &mq_destroy,
2742 &mq_cancel,
2743 queue,
2744 NULL,
2745 &mq_error,
2746 queue);
2747}
2748
2749
2760static void
2762 const struct GNUNET_CRYPTO_HpkeEncapsulation *c)
2763{
2764 struct TcpHandshakeSignature ths;
2765 struct TCPConfirmation tc;
2766
2767 memcpy (queue->cwrite_buf, c, sizeof(*c));
2768 queue->cwrite_off = sizeof(*c);
2769 /* compute 'tc' and append in encrypted format to cwrite_buf */
2770 tc.sender = my_identity;
2771 tc.monotonic_time =
2774 &tc.challenge,
2775 sizeof(tc.challenge));
2776 ths.purpose.purpose = htonl (
2778 ths.purpose.size = htonl (sizeof(ths));
2779 ths.sender = my_identity;
2780 ths.receiver = queue->target;
2781 ths.ephemeral = *c;
2782 ths.monotonic_time = tc.monotonic_time;
2783 ths.challenge = tc.challenge;
2785 &ths,
2786 &tc.sender_sig);
2787 GNUNET_assert (0 ==
2788 gcry_cipher_encrypt (queue->out_cipher,
2789 &queue->cwrite_buf[queue->cwrite_off],
2790 sizeof(tc),
2791 &tc,
2792 sizeof(tc)));
2793 queue->challenge = tc.challenge;
2794 queue->cwrite_off += sizeof(tc);
2795
2797 "handshake written\n");
2798}
2799
2800
2808static void
2810{
2812 struct GNUNET_ShortHashCode k;
2813
2815 &c, &k);
2816 setup_out_cipher (queue, &k);
2817 transmit_kx (queue, &c);
2818}
2819
2820
2826static void
2827handshake_monotime_store_cb (void *cls, int success)
2828{
2829 struct Queue *queue = cls;
2830 if (GNUNET_OK != success)
2831 {
2833 "Failed to store handshake monotonic time in PEERSTORE!\n");
2834 }
2835 queue->handshake_monotime_sc = NULL;
2836 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
2837}
2838
2839
2847static void
2849 const struct GNUNET_PEERSTORE_Record *record,
2850 const char *emsg)
2851{
2852 struct Queue *queue = cls;
2853 struct GNUNET_TIME_AbsoluteNBO *mtbe;
2854 struct GNUNET_TIME_Absolute mt;
2855 const struct GNUNET_PeerIdentity *pid;
2856 struct GNUNET_TIME_AbsoluteNBO *handshake_monotonic_time;
2857
2858 (void) emsg;
2859
2860 handshake_monotonic_time = &queue->handshake_monotonic_time;
2861 pid = &queue->target;
2863 "tcp handshake with us %s\n",
2865 if (NULL == record)
2866 {
2867 queue->handshake_monotime_get = NULL;
2868 return;
2869 }
2871 "tcp handshake from peer %s\n",
2872 GNUNET_i2s (pid));
2873 if (sizeof(*mtbe) != record->value_size)
2874 {
2875 GNUNET_PEERSTORE_iteration_next (queue->handshake_ack_monotime_get, 1);
2876 GNUNET_break (0);
2877 return;
2878 }
2879 mtbe = record->value;
2880 mt = GNUNET_TIME_absolute_ntoh (*mtbe);
2882 queue->handshake_monotonic_time).abs_value_us)
2883 {
2885 "Queue from %s dropped, handshake monotime in the past\n",
2886 GNUNET_i2s (&queue->target));
2887 GNUNET_break (0);
2888 GNUNET_PEERSTORE_iteration_stop (queue->handshake_ack_monotime_get);
2889 queue->handshake_ack_monotime_get = NULL;
2891 return;
2892 }
2893 queue->handshake_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
2894 "transport_tcp_communicator",
2895 pid,
2897 handshake_monotonic_time,
2898 sizeof(*
2899 handshake_monotonic_time),
2902 &
2904 queue);
2905}
2906
2907
2919static int
2921 struct TCPConfirmation *tc,
2922 char *ibuf)
2923{
2924 struct TcpHandshakeSignature ths;
2926
2928 0 ==
2929 gcry_cipher_decrypt (queue->in_cipher,
2930 tc,
2931 sizeof(*tc),
2932 &ibuf[sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)],
2933 sizeof(*tc)));
2934 ths.purpose.purpose = htonl (
2936 ths.purpose.size = htonl (sizeof(ths));
2937 ths.sender = tc->sender;
2938 ths.receiver = my_identity;
2939 memcpy (&ths.ephemeral, ibuf, sizeof(struct GNUNET_CRYPTO_EcdhePublicKey));
2940 ths.monotonic_time = tc->monotonic_time;
2941 ths.challenge = tc->challenge;
2944 &ths,
2945 &tc->sender_sig,
2946 &tc->sender.public_key);
2947 if (GNUNET_YES == ret)
2948 queue->handshake_monotime_get =
2950 "transport_tcp_communicator",
2951 &queue->target,
2954 queue);
2955 return ret;
2956}
2957
2958
2966static void
2967queue_read_kx (void *cls)
2968{
2969 struct Queue *queue = cls;
2970 ssize_t rcvd;
2971 struct GNUNET_TIME_Relative left;
2972 struct TCPConfirmation tc;
2973
2974 queue->read_task = NULL;
2975 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2976 if (0 == left.rel_value_us)
2977 {
2979 return;
2980 }
2981 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2982 &queue->cread_buf[queue->cread_off],
2983 BUF_SIZE - queue->cread_off);
2985 "Received %lu bytes to write in buffer of size %lu for KX from queue %p (expires in %"
2986 PRIu64 ")\n",
2987 rcvd, BUF_SIZE - queue->cread_off, queue, left.rel_value_us);
2988 if (-1 == rcvd)
2989 {
2990 if ((EAGAIN != errno) && (EINTR != errno))
2991 {
2994 return;
2995 }
2996 queue->read_task =
2998 return;
2999 }
3000 if (0 == rcvd)
3001 {
3002 /* Orderly shutdown of connection */
3004 "Socket for queue %p seems to have been closed\n", queue);
3006 return;
3007 }
3008 queue->cread_off += rcvd;
3009 if (queue->cread_off < INITIAL_KX_SIZE)
3010 {
3011 /* read more */
3013 "%lu/%lu bytes of KX read. Rescheduling...\n",
3014 queue->cread_off, INITIAL_KX_SIZE);
3015 queue->read_task =
3017 return;
3018 }
3019 /* we got all the data, let's find out who we are talking to! */
3021 (const struct GNUNET_CRYPTO_HpkeEncapsulation*)
3022 queue->cread_buf,
3023 queue);
3024 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, queue->cread_buf))
3025 {
3027 "Invalid TCP KX received from %s\n",
3028 GNUNET_a2s (queue->address, queue->address_len));
3030 return;
3031 }
3032 if (0 !=
3033 memcmp (&tc.sender, &queue->target, sizeof(struct GNUNET_PeerIdentity)))
3034 {
3036 "Invalid sender in TCP KX received from %s\n",
3037 GNUNET_a2s (queue->address, queue->address_len));
3039 return;
3040 }
3041 send_challenge (tc.challenge, queue);
3042 queue->write_task =
3044 queue->sock,
3045 &queue_write,
3046 queue);
3047
3048 /* update queue timeout */
3049 queue->timeout =
3051 /* prepare to continue with regular read task immediately */
3052 memmove (queue->cread_buf,
3053 &queue->cread_buf[INITIAL_KX_SIZE],
3054 queue->cread_off - (INITIAL_KX_SIZE));
3056 "cread_off is %lu bytes before adjusting\n",
3057 queue->cread_off);
3058 queue->cread_off -= INITIAL_KX_SIZE;
3060 "cread_off set to %lu bytes\n",
3061 queue->cread_off);
3063}
3064
3065
3072static void
3073proto_read_kx (void *cls)
3074{
3075 struct ProtoQueue *pq = cls;
3076 ssize_t rcvd;
3077 struct GNUNET_TIME_Relative left;
3078 struct Queue *queue;
3079 struct TCPConfirmation tc;
3081
3082 pq->read_task = NULL;
3084 if (0 == left.rel_value_us)
3085 {
3086 free_proto_queue (pq);
3087 return;
3088 }
3089 rcvd = GNUNET_NETWORK_socket_recv (pq->sock,
3090 &pq->ibuf[pq->ibuf_off],
3091 sizeof(pq->ibuf) - pq->ibuf_off);
3093 "Proto received %lu bytes for KX\n", rcvd);
3094 if (-1 == rcvd)
3095 {
3096 if ((EAGAIN != errno) && (EINTR != errno))
3097 {
3099 free_proto_queue (pq);
3100 return;
3101 }
3102 /* try again */
3103 pq->read_task =
3105 return;
3106 }
3107 if (0 == rcvd)
3108 {
3109 /* Orderly shutdown of connection */
3111 "Socket for proto queue %p seems to have been closed\n", pq);
3112 free_proto_queue (pq);
3113 return;
3114 }
3115 pq->ibuf_off += rcvd;
3116 if (sizeof (struct TCPNATProbeMessage) == pq->ibuf_off)
3117 {
3118 struct TCPNATProbeMessage *pm = (struct TCPNATProbeMessage *) pq->ibuf;
3119
3121 &pm->clientIdentity);
3122
3123 queue = GNUNET_new (struct Queue);
3124 queue->target = pm->clientIdentity;
3125 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
3128 }
3129 else if (pq->ibuf_off > sizeof(pq->ibuf))
3130 {
3131 /* read more */
3132 pq->read_task =
3134 return;
3135 }
3136 else
3137 {
3138 /* we got all the data, let's find out who we are talking to! */
3139 queue = GNUNET_new (struct Queue);
3141 (const struct GNUNET_CRYPTO_HpkeEncapsulation *) pq->
3142 ibuf,
3143 queue);
3144 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, pq->ibuf))
3145 {
3147 "Invalid TCP KX received from %s\n",
3148 GNUNET_a2s (pq->address, pq->address_len));
3149 gcry_cipher_close (queue->in_cipher);
3151 free_proto_queue (pq);
3152 return;
3153 }
3154 queue->target = tc.sender;
3155 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
3158 }
3159 queue->address = pq->address; /* steals reference */
3160 queue->address_len = pq->address_len;
3161 queue->listen_sock = pq->listen_sock;
3162 queue->sock = pq->sock;
3163
3165 "created queue with target %s\n",
3166 GNUNET_i2s (&queue->target));
3167
3169 "start kx proto\n");
3170
3172 boot_queue (queue);
3173 queue->read_task =
3175 queue->sock,
3176 read_task,
3177 queue);
3178 queue->write_task =
3180 queue->sock,
3181 &queue_write,
3182 queue);
3183 // TODO To early! Move it somewhere else.
3184 // send_challenge (tc.challenge, queue);
3185 queue->challenge_received = tc.challenge;
3186
3188 GNUNET_free (pq);
3189}
3190
3191
3192static struct ProtoQueue *
3194 struct sockaddr *in,
3195 socklen_t addrlen)
3196{
3197 struct ProtoQueue *pq = GNUNET_new (struct ProtoQueue);
3198
3199 if (NULL == sock)
3200 {
3201 // sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
3202 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, 0);
3203 if (NULL == sock)
3204 {
3206 "socket(%d) failed: %s",
3207 in->sa_family,
3208 strerror (errno));
3209 GNUNET_free (in);
3210 GNUNET_free (pq);
3211 return NULL;
3212 }
3213 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, addrlen)) &&
3214 (errno != EINPROGRESS))
3215 {
3217 "connect to `%s' failed: %s",
3218 GNUNET_a2s (in, addrlen),
3219 strerror (errno));
3221 GNUNET_free (in);
3222 GNUNET_free (pq);
3223 return NULL;
3224 }
3225 }
3226 pq->address_len = addrlen;
3227 pq->address = in;
3229 pq->sock = sock;
3231 pq->sock,
3233 pq);
3235
3236 return pq;
3237}
3238
3239
3247static void
3248listen_cb (void *cls)
3249{
3250 struct sockaddr_storage in;
3251 socklen_t addrlen;
3252 struct GNUNET_NETWORK_Handle *sock;
3253 struct ListenTask *lt;
3254 struct sockaddr *in_addr;
3255
3257 "listen_cb\n");
3258
3259 lt = cls;
3260
3261 lt->listen_task = NULL;
3262 GNUNET_assert (NULL != lt->listen_sock);
3263 addrlen = sizeof(in);
3264 memset (&in, 0, sizeof(in));
3266 (struct sockaddr*) &in,
3267 &addrlen);
3268 if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno)))
3269 return; /* system limit reached, wait until connection goes down */
3271 lt->listen_sock,
3272 &listen_cb,
3273 lt);
3274 if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno)))
3275 return;
3276 if (NULL == sock)
3277 {
3279 return;
3280 }
3281 in_addr = GNUNET_memdup (&in, addrlen);
3282 create_proto_queue (sock, in_addr, addrlen);
3283}
3284
3285
3286static void
3288 const struct sockaddr *addr,
3289 socklen_t addrlen)
3290{
3291 struct TCPNATProbeMessage pm;
3292 struct ProtoQueue *pq;
3293 struct sockaddr *in_addr;
3294 (void) cls;
3295
3297 "addr->sa_family %d\n",
3298 addr->sa_family);
3300 "Try to connect back\n");
3301 in_addr = GNUNET_memdup (addr, addrlen);
3303 "in_addr->sa_family %d\n",
3304 in_addr->sa_family);
3305 pq = create_proto_queue (NULL, in_addr, addrlen);
3306 if (NULL != pq)
3307 {
3308 pm.header.size = htons (sizeof(struct TCPNATProbeMessage));
3309 pm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
3310 pm.clientIdentity = my_identity;
3311 memcpy (pq->write_buf, &pm, sizeof(struct TCPNATProbeMessage));
3312 pq->write_off = sizeof(struct TCPNATProbeMessage);
3314 pq->sock,
3316 pq);
3317 }
3318 else
3319 {
3321 "Couldn't create ProtoQueue for sending TCPNATProbeMessage\n");
3322 }
3323}
3324
3325
3326static void
3328{
3329 struct sockaddr *in = cls;
3330 struct PendingReversal *pending_reversal;
3331 struct GNUNET_HashCode key;
3332
3334 sizeof(struct sockaddr),
3335 &key);
3337 &key);
3338
3339 GNUNET_assert (NULL != pending_reversal);
3340
3342 &key,
3343 pending_reversal))
3345 "No pending reversal found for address %s\n",
3346 GNUNET_a2s (in, sizeof (struct sockaddr)));
3347 GNUNET_free (pending_reversal->in);
3348 GNUNET_free (pending_reversal);
3349}
3350
3351
3370static int
3371mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
3372{
3373 struct sockaddr *in;
3374 socklen_t in_len = 0;
3375 const char *path;
3376 struct sockaddr_in *v4;
3377 struct sockaddr_in6 *v6;
3378 unsigned int is_natd = GNUNET_NO;
3379 struct GNUNET_HashCode key;
3380 struct GNUNET_HashCode queue_map_key;
3381 struct GNUNET_HashContext *hsh;
3382 struct Queue *queue;
3383
3385 "Connecting to %s at %s\n",
3386 GNUNET_i2s (peer),
3387 address);
3388 if (0 != strncmp (address,
3390 strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
3391 {
3392 GNUNET_break_op (0);
3393 return GNUNET_SYSERR;
3394 }
3395 path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
3396 in = tcp_address_to_sockaddr (path, &in_len);
3397
3398 if (NULL == in)
3399 {
3401 "Failed to setup TCP socket address\n");
3402 return GNUNET_SYSERR;
3403 }
3404
3406 "in %s\n",
3407 GNUNET_a2s (in, in_len));
3408
3411 GNUNET_CRYPTO_hash_context_read (hsh, peer, sizeof (*peer));
3412 GNUNET_CRYPTO_hash_context_finish (hsh, &queue_map_key);
3414
3415 if (NULL != queue)
3416 {
3418 "Queue for %s already exists or is in construction\n", address);
3419 GNUNET_free (in);
3420 return GNUNET_NO;
3421 }
3422 switch (in->sa_family)
3423 {
3424 case AF_INET:
3425 v4 = (struct sockaddr_in *) in;
3426 if (0 == v4->sin_port)
3427 {
3428 is_natd = GNUNET_YES;
3430 sizeof(struct sockaddr),
3431 &key);
3434 &key))
3435 {
3437 "There is already a request reversal for `%s'at `%s'\n",
3438 GNUNET_i2s (peer),
3439 address);
3440 GNUNET_free (in);
3441 return GNUNET_SYSERR;
3442 }
3443 }
3444 break;
3445
3446 case AF_INET6:
3447 if (GNUNET_YES == disable_v6)
3448 {
3450 "IPv6 disabled, skipping %s\n", address);
3451 GNUNET_free (in);
3452 return GNUNET_SYSERR;
3453 }
3454 v6 = (struct sockaddr_in6 *) in;
3455 if (0 == v6->sin6_port)
3456 {
3458 "Request reversal for `%s' at `%s' not possible for an IPv6 address\n",
3459 GNUNET_i2s (peer),
3460 address);
3461 GNUNET_free (in);
3462 return GNUNET_SYSERR;
3463 }
3464 break;
3465
3466 default:
3467 GNUNET_assert (0);
3468 }
3469
3470 if (GNUNET_YES == is_natd)
3471 {
3472 struct sockaddr_in local_sa;
3473 struct PendingReversal *pending_reversal;
3474
3475 memset (&local_sa, 0, sizeof(local_sa));
3476 local_sa.sin_family = AF_INET;
3477 local_sa.sin_port = htons (bind_port);
3478 /* We leave sin_address at 0, let the kernel figure it out,
3479 even if our bind() is more specific. (May want to reconsider
3480 later.) */
3481 if (GNUNET_OK != GNUNET_NAT_request_reversal (nat, &local_sa, v4))
3482 {
3484 "request reversal for `%s' at `%s' failed\n",
3485 GNUNET_i2s (peer),
3486 address);
3487 GNUNET_free (in);
3488 return GNUNET_SYSERR;
3489 }
3490 pending_reversal = GNUNET_new (struct PendingReversal);
3491 pending_reversal->in = in;
3494 &key,
3495 pending_reversal,
3497 pending_reversal->target = *peer;
3499 &
3501 in);
3503 "Created NAT WAIT connection to `%s' at `%s'\n",
3504 GNUNET_i2s (peer),
3505 GNUNET_a2s (in, sizeof (struct sockaddr)));
3506 }
3507 else
3508 {
3509 struct GNUNET_NETWORK_Handle *sock;
3510
3511 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM,
3512 IPPROTO_TCP);
3513 if (NULL == sock)
3514 {
3516 "socket(%d) failed: %s",
3517 in->sa_family,
3518 strerror (errno));
3519 GNUNET_free (in);
3520 return GNUNET_SYSERR;
3521 }
3522 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, in_len)) &&
3523 (errno != EINPROGRESS))
3524 {
3526 "connect to `%s' failed: %s",
3527 address,
3528 strerror (errno));
3530 GNUNET_free (in);
3531 return GNUNET_SYSERR;
3532 }
3533
3534 queue = GNUNET_new (struct Queue);
3535 queue->target = *peer;
3536 eddsa_pub_to_hpke_key (&queue->target.public_key, &queue->target_hpke_key);
3537 queue->key = queue_map_key;
3538 queue->address = in;
3539 queue->address_len = in_len;
3540 queue->sock = sock;
3542 boot_queue (queue);
3544 "booted queue with target %s\n",
3545 GNUNET_i2s (&queue->target));
3546 // queue->mq_awaits_continue = GNUNET_YES;
3547 queue->read_task =
3549 queue->sock,
3551 queue);
3552
3553
3555 "start kx mq_init\n");
3556
3558 queue->write_task =
3560 queue->sock,
3561 &queue_write,
3562 queue);
3563 }
3564
3565 return GNUNET_OK;
3566}
3567
3568
3577static int
3579 const struct GNUNET_HashCode *key,
3580 void *value)
3581{
3582 struct ListenTask *lt = value;
3583
3584 (void) cls;
3585 (void) key;
3586 if (NULL != lt->listen_task)
3587 {
3589 lt->listen_task = NULL;
3590 }
3591 if (NULL != lt->listen_sock)
3592 {
3594 lt->listen_sock = NULL;
3595 }
3596 GNUNET_free (lt);
3597 return GNUNET_OK;
3598}
3599
3600
3609static int
3611 const struct GNUNET_HashCode *target,
3612 void *value)
3613{
3614 struct Queue *queue = value;
3615
3616 (void) cls;
3617 (void) target;
3619 return GNUNET_OK;
3620}
3621
3622
3628static void
3629do_shutdown (void *cls)
3630{
3632 "Shutdown %s!\n",
3633 shutdown_running ? "running" : "not running");
3634
3636 return;
3637 else
3639
3640 while (NULL != proto_head)
3642 if (NULL != nat)
3643 {
3645 nat = NULL;
3646 }
3654 if (NULL != ch)
3655 {
3658 ch = NULL;
3659 }
3660 if (NULL != stats)
3661 {
3663 stats = NULL;
3664 }
3665 if (NULL != my_private_key)
3666 {
3668 my_private_key = NULL;
3669 }
3670 if (NULL != is)
3671 {
3673 is = NULL;
3674 }
3675 if (NULL != pils)
3676 {
3678 pils = NULL;
3679 }
3680 if (NULL != peerstore)
3681 {
3683 peerstore = NULL;
3684 }
3685 if (NULL != resolve_request_handle)
3686 {
3689 }
3691 "Shutdown done!\n");
3692}
3693
3694
3706static void
3707enc_notify_cb (void *cls,
3708 const struct GNUNET_PeerIdentity *sender,
3709 const struct GNUNET_MessageHeader *msg)
3710{
3711 (void) cls;
3712 (void) sender;
3713 (void) msg;
3714 GNUNET_break_op (0);
3715}
3716
3717
3731static void
3733 void **app_ctx,
3734 int add_remove,
3736 const struct sockaddr *addr,
3737 socklen_t addrlen)
3738{
3739 char *my_addr;
3741
3743 "nat address cb %s %s\n",
3744 add_remove ? "add" : "remove",
3745 GNUNET_a2s (addr, addrlen));
3746
3747 if (GNUNET_YES == add_remove)
3748 {
3750
3751 GNUNET_asprintf (&my_addr,
3752 "%s-%s",
3754 GNUNET_a2s (addr, addrlen));
3755 nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
3756 ai =
3758 my_addr,
3759 nt,
3761 GNUNET_free (my_addr);
3762 *app_ctx = ai;
3763 }
3764 else
3765 {
3766 ai = *app_ctx;
3768 *app_ctx = NULL;
3769 }
3770}
3771
3772
3776static void
3777add_addr (struct sockaddr *in, socklen_t in_len)
3778{
3779
3780 struct Addresses *saddrs;
3781
3783 "add address %s\n",
3784 GNUNET_a2s (in, in_len));
3785
3786 saddrs = GNUNET_new (struct Addresses);
3787 saddrs->addr = in;
3788 saddrs->addr_len = in_len;
3790
3792 "after add address %s\n",
3793 GNUNET_a2s (in, in_len));
3794
3796 "add address %s\n",
3797 GNUNET_a2s (saddrs->addr, saddrs->addr_len));
3798
3799 addrs_lens++;
3800}
3801
3802
3816static enum GNUNET_GenericReturnValue
3818{
3819 char *filename;
3820 struct GNUNET_DISK_FileHandle *filehandle;
3821 int ret;
3822
3823 if (GNUNET_OK !=
3825 "pils",
3826 "SECRET_KEY_FILE",
3827 &filename))
3828 {
3830 "PILS service is lacking initial secret key file configuration setting. Exiting\n");
3831 return GNUNET_SYSERR;
3832 }
3833 if (NULL == filename)
3834 return GNUNET_SYSERR;
3836 if (GNUNET_SYSERR == ret)
3837 return GNUNET_SYSERR;
3838 if (GNUNET_NO == ret)
3839 {
3840 /* File does not exist - generate a new initial secret key and save it */
3841 // TODO consider the case that the file exists and ist not readable
3843 ikm,
3844 sizeof ikm);
3846 {
3848 "PILS service cannot create dir for saving initial secret key file. Exiting\n");
3849 return GNUNET_SYSERR;
3850 }
3851 filehandle = GNUNET_DISK_file_open (filename,
3855 | // would
3856 // the
3857 // group
3858 // need
3859 // read
3860 // perm?
3862 if (NULL == filehandle)
3863 {
3865 "PILS service had an issue with opening the initial secret key file. Exiting\n");
3866 GNUNET_DISK_file_close (filehandle);
3867 return GNUNET_SYSERR;
3868 }
3869 ret = GNUNET_DISK_file_write (filehandle,
3870 ikm,
3871 sizeof ikm);
3872 GNUNET_DISK_file_close (filehandle);
3873 if (sizeof ikm != ret)
3874 {
3876 "PILS service had an issue with writing the initial secret key to file. Exiting\n")
3877 ;
3878 return GNUNET_SYSERR;
3879 }
3880 }
3881 else
3882 {
3883 /* File existes - just read from it */
3884 off_t size;
3886 "PILS is going to read initial secret key from file %s\n",
3887 filename);
3888 filehandle = GNUNET_DISK_file_open (filename,
3891 if (NULL == filehandle)
3892 {
3894 " Not able to open file\n");
3895 return GNUNET_SYSERR;
3896 }
3897 if (GNUNET_OK != GNUNET_DISK_file_handle_size (filehandle, &size))
3898 {
3900 " File has the wrong size %lu\n",
3901 size);
3902 GNUNET_DISK_file_close (filehandle);
3903 return GNUNET_SYSERR;
3904 }
3905 if (sizeof ikm != size)
3906 {
3908 " Something is wrong with the file size, expected: %lu size, got: %lu\n",
3909 size,
3910 sizeof ikm);
3911 GNUNET_DISK_file_close (filehandle);
3912 return GNUNET_SYSERR;
3913 }
3914 ret = GNUNET_DISK_file_read (filehandle,
3915 ikm,
3916 sizeof ikm);
3917 GNUNET_DISK_file_close (filehandle);
3918 if (sizeof ikm != ret)
3919 {
3921 " Read initial secret key with wrong size %u, expected %lu\n", ret,
3922 sizeof ikm);
3923 return GNUNET_SYSERR;
3924 }
3925
3926 }
3927 return GNUNET_OK;
3928}
3929
3930
3938static int
3939init_socket (struct sockaddr *addr,
3940 socklen_t in_len)
3941{
3942 struct sockaddr_storage in_sto;
3943 socklen_t sto_len;
3944 struct GNUNET_NETWORK_Handle *listen_sock;
3945 struct ListenTask *lt;
3946 int sockfd;
3947 struct GNUNET_HashCode h_sock;
3948
3949 if (NULL == addr)
3950 {
3952 "Address is NULL.\n");
3953 return GNUNET_SYSERR;
3954 }
3955
3957 "address %s\n",
3958 GNUNET_a2s (addr, in_len));
3959
3960 listen_sock =
3961 GNUNET_NETWORK_socket_create (addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
3962 if (NULL == listen_sock)
3963 {
3965 return GNUNET_SYSERR;
3966 }
3967
3968 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, addr, in_len))
3969 {
3971 GNUNET_NETWORK_socket_close (listen_sock);
3972 listen_sock = NULL;
3973 return GNUNET_SYSERR;
3974 }
3975
3976 if (GNUNET_OK !=
3977 GNUNET_NETWORK_socket_listen (listen_sock,
3978 5))
3979 {
3981 "listen");
3982 GNUNET_NETWORK_socket_close (listen_sock);
3983 listen_sock = NULL;
3984 return GNUNET_SYSERR;
3985 }
3986
3987 /* We might have bound to port 0, allowing the OS to figure it out;
3988 thus, get the real IN-address from the socket */
3989 sto_len = sizeof(in_sto);
3990
3991 if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock),
3992 (struct sockaddr *) &in_sto,
3993 &sto_len))
3994 {
3995 memcpy (&in_sto, addr, in_len);
3996 sto_len = in_len;
3997 }
3998
3999 // addr = (struct sockaddr *) &in_sto;
4000 in_len = sto_len;
4002 "Bound to `%s'\n",
4003 GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
4004 if (NULL == stats)
4005 stats = GNUNET_STATISTICS_create ("communicator-tcp", cfg);
4006
4007 if (NULL == is)
4009
4010 /* start listening */
4011
4012 lt = GNUNET_new (struct ListenTask);
4013 lt->listen_sock = listen_sock;
4014
4016 listen_sock,
4017 &listen_cb,
4018 lt);
4019
4021 "creating hash\n");
4022 sockfd = GNUNET_NETWORK_get_fd (lt->listen_sock);
4023 GNUNET_CRYPTO_hash (&sockfd,
4024 sizeof(int),
4025 &h_sock);
4026
4028 "creating map\n");
4029 if (NULL == lt_map)
4031
4033 "creating map entry\n");
4036 &h_sock,
4037 lt,
4039
4041 "map entry created\n");
4042
4043 if (NULL == queue_map)
4045
4046 if (NULL == ch)
4051 &mq_init,
4052 NULL,
4054 NULL,
4055 NULL);
4056
4057 if (NULL == ch)
4058 {
4059 GNUNET_break (0);
4060 if (NULL != resolve_request_handle)
4063 return GNUNET_SYSERR;
4064 }
4065
4066 add_addr (addr, in_len);
4067 return GNUNET_OK;
4068
4069}
4070
4071
4075static void
4077{
4078 struct sockaddr **saddrs;
4079 socklen_t *saddr_lens;
4080 int i;
4081 size_t len;
4082
4084 "starting nat register!\n");
4085 len = 0;
4086 i = 0;
4087 saddrs = GNUNET_malloc ((addrs_lens) * sizeof(struct sockaddr *));
4088 saddr_lens = GNUNET_malloc ((addrs_lens) * sizeof(socklen_t));
4089 for (struct Addresses *pos = addrs_head; NULL != pos; pos = pos->next)
4090 {
4092 "registering address %s\n",
4093 GNUNET_a2s (pos->addr, pos->addr_len));
4094
4095 saddr_lens[i] = pos->addr_len;
4096 len += saddr_lens[i];
4097 saddrs[i] = GNUNET_memdup (pos->addr, saddr_lens[i]);
4098 i++;
4099 }
4100
4102 "registering addresses %lu %lu %lu %lu\n",
4103 (addrs_lens) * sizeof(struct sockaddr *),
4104 (addrs_lens) * sizeof(socklen_t),
4105 len,
4109 IPPROTO_TCP,
4110 addrs_lens,
4111 (const struct sockaddr **) saddrs,
4112 saddr_lens,
4115 NULL /* closure */);
4116 for (i = addrs_lens - 1; i >= 0; i--)
4117 GNUNET_free (saddrs[i]);
4118 GNUNET_free (saddrs);
4119 GNUNET_free (saddr_lens);
4120
4121 if (NULL == nat)
4122 {
4123 GNUNET_break (0);
4124 if (NULL != resolve_request_handle)
4127 }
4128}
4129
4130
4138static void
4140 const struct sockaddr *addr,
4141 socklen_t in_len)
4142{
4143 struct sockaddr_in *v4;
4144 struct sockaddr_in6 *v6;
4145 struct sockaddr *in;
4146
4147 (void) cls;
4148 if (NULL != addr)
4149 {
4150 if (AF_INET == addr->sa_family)
4151 {
4152 v4 = (struct sockaddr_in *) addr;
4153 in = tcp_address_to_sockaddr_numeric_v4 (&in_len, *v4, bind_port);// _global);
4154 }
4155 else if (AF_INET6 == addr->sa_family)
4156 {
4157 v6 = (struct sockaddr_in6 *) addr;
4158 in = tcp_address_to_sockaddr_numeric_v6 (&in_len, *v6, bind_port);// _global);
4159 }
4160 else
4161 {
4163 "Address family %u not suitable (not AF_INET %u nor AF_INET6 %u \n",
4164 addr->sa_family,
4165 AF_INET,
4166 AF_INET6);
4167 return;
4168 }
4169 init_socket (in, in_len);
4170 }
4171 else
4172 {
4174 "Address is NULL. This might be an error or the resolver finished resolving.\n");
4175 if (NULL == addrs_head)
4176 {
4178 "Resolver finished resolving, but we do not listen to an address!.\n");
4179 return;
4180 }
4181 nat_register ();
4182 }
4183}
4184
4185
4186void
4187pid_change_cb (void *cls,
4188 const struct GNUNET_HELLO_Parser *parser,
4189 const struct GNUNET_HashCode *addr_hash)
4190{
4192 "Got PID to derive from `%s':\n",
4193 GNUNET_h2s (addr_hash));
4194 if (NULL == my_private_key)
4196
4198 (uint8_t*) ikm,
4199 addr_hash,
4205}
4206
4207
4216static void
4217run (void *cls,
4218 char *const *args,
4219 const char *cfgfile,
4220 const struct GNUNET_CONFIGURATION_Handle *c)
4221{
4222 char *bindto;
4223 struct sockaddr *in;
4224 socklen_t in_len;
4225 struct sockaddr_in v4;
4226 struct sockaddr_in6 v6;
4227 char *start;
4228 unsigned int port;
4229 char dummy[2];
4230 char *rest = NULL;
4231 struct PortOnlyIpv4Ipv6 *po;
4232 socklen_t addr_len_ipv4;
4233 socklen_t addr_len_ipv6;
4234
4235 (void) cls;
4236
4238 memset (&v4,0,sizeof(struct sockaddr_in));
4239 memset (&v6,0,sizeof(struct sockaddr_in6));
4240 cfg = c;
4241 if (GNUNET_OK !=
4244 "BINDTO",
4245 &bindto))
4246 {
4249 "BINDTO");
4250 return;
4251 }
4252 if (GNUNET_OK !=
4255 "MAX_QUEUE_LENGTH",
4257 {
4259 }
4260 if (GNUNET_OK !=
4263 "REKEY_INTERVAL",
4265 {
4267 }
4268 if (GNUNET_OK !=
4271 "REKEY_MAX_BYTES",
4273 {
4275 }
4277 if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
4278 (GNUNET_YES ==
4281 "DISABLE_V6")))
4282 {
4284 }
4285 load_ikm ();
4287 GNUNET_assert (NULL != pils);
4289 if (NULL == peerstore)
4290 {
4291 GNUNET_free (bindto);
4292 GNUNET_break (0);
4294 return;
4295 }
4296
4298
4299 if (1 == sscanf (bindto, "%u%1s", &bind_port, dummy))
4300 {
4304 "address po %s\n",
4306 if (NULL != po->addr_ipv4)
4307 {
4309 }
4310 if (NULL != po->addr_ipv6)
4311 {
4314 }
4315 GNUNET_free (po);
4316 nat_register ();
4317 GNUNET_free (bindto);
4318 return;
4319 }
4320
4321 start = extract_address (bindto);
4322 // FIXME: check for NULL == start...
4323 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
4324 {
4325 bind_port = extract_port (bindto);
4326
4328 init_socket (in, in_len);
4329 nat_register ();
4331 GNUNET_free (bindto);
4332 return;
4333 }
4334
4335 if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
4336 {
4337 bind_port = extract_port (bindto);
4339 init_socket (in, in_len);
4340 nat_register ();
4342 GNUNET_free (bindto);
4343 return;
4344 }
4345
4346 bind_port = extract_port (bindto);
4348 ":",
4349 &rest),
4350 AF_UNSPEC,
4353 &port);
4354
4355 GNUNET_free (bindto);
4357}
4358
4359
4367int
4368main (int argc, char *const *argv)
4369{
4370 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
4372 };
4373 int ret;
4374
4376 "Starting tcp communicator\n");
4377
4378 ret = (GNUNET_OK ==
4380 argc,
4381 argv,
4382 "gnunet-communicator-tcp",
4383 _ ("GNUnet TCP communicator"),
4384 options,
4385 &run,
4386 NULL))
4387 ? 0
4388 : 1;
4389 return ret;
4390}
4391
4392
4393/* 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
void pid_change_cb(void *cls, const struct GNUNET_HELLO_Parser *parser, const struct GNUNET_HashCode *addr_hash)
static void calculate_hmac(struct GNUNET_HashCode *hmac_secret, const void *buf, size_t buf_size, struct GNUNET_ShortHashCode *smac)
Compute mac over buf, and ratched the hmac_secret.
static void queue_finish(struct Queue *queue)
Append a 'finish' message to the outgoing transmission.
static void transmit_kx(struct Queue *queue, const struct GNUNET_CRYPTO_HpkeEncapsulation *c)
Generate and transmit our ephemeral key and the signature for the initial KX with the other peer.
static void boot_queue(struct Queue *queue)
Add the given queue to our internal data structure.
static unsigned int bind_port
The port the communicator should be assigned to.
static size_t try_handle_plaintext(struct Queue *queue)
Test if we have received a full message in plaintext.
static void pass_plaintext_to_core(struct Queue *queue, const void *plaintext, size_t plaintext_len)
We received plaintext_len bytes of plaintext on queue.
static struct sockaddr * tcp_address_to_sockaddr_numeric_v6(socklen_t *sock_len, struct sockaddr_in6 v6, unsigned int port)
Convert a struct sockaddr_in6 to astruct sockaddr *`.
static struct GNUNET_PeerIdentity my_identity
Our public key.
#define COMMUNICATOR_ADDRESS_PREFIX
Address prefix used by the communicator.
static unsigned char ikm[256/8]
The initial key material for the peer.
static void mq_destroy(struct GNUNET_MQ_Handle *mq, void *impl_state)
Signature of functions implementing the destruction of a message queue.
#define ADDRESS_VALIDITY_PERIOD
How long do we believe our addresses to remain up (before the other peer should revalidate).
static struct PortOnlyIpv4Ipv6 * tcp_address_to_sockaddr_port_only(const char *bindto, unsigned int *port)
Convert TCP bind specification to a struct PortOnlyIpv4Ipv6 *
static void setup_in_cipher(const struct GNUNET_CRYPTO_HpkeEncapsulation *ephemeral, struct Queue *queue)
Setup cipher of queue for decryption.
#define INITIAL_CORE_KX_SIZE
Size of the initial core key exchange messages.
static void do_rekey(struct Queue *queue, const struct TCPRekey *rekey)
Handle rekey message on queue.
static void eddsa_priv_to_hpke_key(struct GNUNET_CRYPTO_EddsaPrivateKey *edpk, struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
static struct ProtoQueue * create_proto_queue(struct GNUNET_NETWORK_Handle *sock, struct sockaddr *in, socklen_t addrlen)
static int init_socket(struct sockaddr *addr, socklen_t in_len)
This method launch network interactions for each address we like to bind to.
static void try_connection_reversal(void *cls, const struct sockaddr *addr, socklen_t addrlen)
static struct GNUNET_NT_InterfaceScanner * is
Network scanner to determine network types.
static void listen_cb(void *cls)
We have been notified that our listen socket has something to read.
static void rekey_monotime_cb(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY where...
static int disable_v6
IPv6 disabled.
static struct GNUNET_CONTAINER_MultiHashMap * pending_reversals
Map of pending reversals.
static struct GNUNET_STATISTICS_Handle * stats
For logging statistics.
static void queue_destroy(struct Queue *queue)
Functions with this signature are called whenever we need to close a queue due to a disconnect or fai...
static const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static void enc_notify_cb(void *cls, const struct GNUNET_PeerIdentity *sender, const struct GNUNET_MessageHeader *msg)
Function called when the transport service has received an acknowledgement for this communicator (!...
static void handshake_ack_monotime_store_cb(void *cls, int success)
Callback called when peerstore store operation for handshake ack monotime value is finished.
static struct GNUNET_PEERSTORE_Handle * peerstore
Database for peer's HELLOs.
static int pending_reversals_delete_it(void *cls, const struct GNUNET_HashCode *key, void *value)
static struct ProtoQueue * proto_tail
Protoqueues DLL tail.
static void queue_read_kx(void *cls)
Read from the socket of the queue until we have enough data to initialize the decryption logic and ca...
static int get_lt_delete_it(void *cls, const struct GNUNET_HashCode *key, void *value)
Iterator over all ListenTasks to clean up.
static void inject_rekey(struct Queue *queue)
Inject a struct TCPRekey message into the queue's plaintext buffer.
static struct GNUNET_TIME_Relative rekey_interval
The rekey interval.
#define NAT_TIMEOUT
How long until we give up on establishing an NAT connection? Must be > 4 RTT.
#define BUF_SIZE
Size of our IO buffers for ciphertext data.
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 enum GNUNET_GenericReturnValue load_ikm()
FIXME: We could alternatively ask PILS for de/encaps, but at a high cost wrt async RPC calls....
static int decrypt_and_check_tc(struct Queue *queue, struct TCPConfirmation *tc, char *ibuf)
We have received the first bytes from the other side on a queue.
#define LOG(kind,...)
#define DEFAULT_REKEY_INTERVAL
How often do we rekey based on time (at least)
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 struct GNUNET_PILS_Handle * pils
For PILS.
static void start_initial_kx_out(struct Queue *queue)
Initialize our key material for outgoing transmissions and inform the other peer about it.
static void free_proto_queue(struct ProtoQueue *pq)
Closes socket and frees memory associated with pq.
static struct GNUNET_CONTAINER_MultiHashMap * lt_map
ListenTasks (map from socket to struct ListenTask)
static unsigned long long rekey_max_bytes
The rekey byte maximum.
static void core_read_finished_cb(void *cls, int success)
Core tells us it is done processing a message that transport received on a queue with status success.
static struct GNUNET_TRANSPORT_AddressIdentifier * ai
Handle to the operation that publishes our address.
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
static char * address
GNS address for this phone.
struct GNUNET_HashCode key
The key used in the DHT.
static char * filename
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
struct GNUNET_CRYPTO_PrivateKey pk
Private key from command line option, or NULL.
struct GNUNET_SCHEDULER_Task * read_task
static char * value
Value of the record to add/remove.
static uint32_t type
Type string converted to DNS type value.
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
static struct GNUNET_TRANSPORT_PluginMonitor * pm
Handle if we are monitoring plugin session activity.
static void queue(const char *hostname)
Add hostname to the list of requests to be made.
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.
struct GNUNET_PILS_Handle * GNUNET_PILS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_PILS_PidChangeCallback pid_change_cb, void *cls)
Connect to the PILS service.
Definition: pils_api.c:367
void GNUNET_PILS_disconnect(struct GNUNET_PILS_Handle *handle)
Disconnect from the PILS service.
Definition: pils_api.c:390
void GNUNET_PILS_derive_pid(size_t seed_key_bytes, const uint8_t seed_key[seed_key_bytes], const struct GNUNET_HashCode *addrs_hash, struct GNUNET_CRYPTO_EddsaPrivateKey *outkey)
Generate the peer id from the addresses hash and the initial secret key.
Definition: pils_api.c:507
Constants for network protocols.
Functions related to doing DNS lookups.
#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.
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_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_time(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, struct GNUNET_TIME_Relative *time)
Get a configuration value that should be a relative time.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
#define GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
After how long do we consider a connection to a peer dead if we don't receive messages from the peer?
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_kem_decaps(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_HpkeEncapsulation *c, struct GNUNET_ShortHashCode *prk)
Decapsulate a key for a private EdDSA key.
Definition: crypto_hpke.c:285
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:348
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:327
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_kem_encaps(const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_CRYPTO_HpkeEncapsulation *c, struct GNUNET_ShortHashCode *prk)
Encapsulate key material for a EdDSA public key.
Definition: crypto_hpke.c:233
#define GNUNET_CRYPTO_eddsa_sign(priv, ps, sig)
EdDSA sign a given block.
#define GNUNET_CRYPTO_eddsa_verify(purp, ps, sig, pub)
Verify EdDSA signature.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
@ GNUNET_CRYPTO_QUALITY_NONCE
Randomness for IVs etc.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1258
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:710
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test_read(const char *fil)
Check that fil corresponds to a filename and the file has read permissions.
Definition: disk.c:540
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1332
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:633
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:673
enum GNUNET_GenericReturnValue GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size)
Get the size of an open file.
Definition: disk.c:206
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_NONE
Nobody is allowed to do anything to the file.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
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:832
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hpke_pk_to_x25519(const struct GNUNET_CRYPTO_PublicKey *pk, struct GNUNET_CRYPTO_EcdhePublicKey *x25519)
Convert a GNUnet identity key to a key sutiable for HPKE (X25519)
Definition: crypto_hpke.c:808
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_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
const char * GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string (for printing debug messages).
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
const char * GNUNET_p2s(const struct GNUNET_CRYPTO_EddsaPublicKey *p)
Convert a public key value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
GNUNET_MQ_Error
Error codes for the queue.
struct GNUNET_MQ_Handle * GNUNET_MQ_queue_for_callbacks(GNUNET_MQ_SendImpl send, GNUNET_MQ_DestroyImpl destroy, GNUNET_MQ_CancelImpl cancel, void *impl_state, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *cls)
Create a message queue for the specified handlers.
Definition: mq.c:482
void GNUNET_MQ_impl_send_continue(struct GNUNET_MQ_Handle *mq)
Call the send implementation for the next queued message, if any.
Definition: mq.c:437
int GNUNET_NAT_request_reversal(struct GNUNET_NAT_Handle *nh, const struct sockaddr_in *local_sa, const struct sockaddr_in *remote_sa)
We learned about a peer (possibly behind NAT) so run the gnunet-nat-client to send dummy ICMP respons...
Definition: nat_api.c:675
struct GNUNET_NAT_Handle * GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *config_section, uint8_t proto, unsigned int num_addrs, const struct sockaddr **addrs, const socklen_t *addrlens, GNUNET_NAT_AddressCallback address_callback, GNUNET_NAT_ReversalCallback reversal_callback, void *callback_cls)
Attempt to enable port redirection and detect public IP address contacting UPnP or NAT-PMP routers on...
Definition: nat_api.c:366
void GNUNET_NAT_unregister(struct GNUNET_NAT_Handle *nh)
Stop port redirection and public IP address detection for the given handle.
Definition: nat_api.c:703
GNUNET_NAT_AddressClass
Some addresses contain sensitive information or are not suitable for global distribution.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc, struct sockaddr *address, socklen_t *address_len)
Accept a new connection on a socket.
Definition: network.c:392
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:508
int GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
Return file descriptor for this network handle.
Definition: network.c:1001
ssize_t GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length)
Read data from a connected socket (always non-blocking).
Definition: network.c:717
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:833
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_connect(const struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Connect a socket to some remote address.
Definition: network.c:602
ssize_t GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc, const void *buffer, size_t length)
Send data (always non-blocking).
Definition: network.c:738
enum GNUNET_GenericReturnValue GNUNET_NETWORK_test_pf(int pf)
Test if the given protocol family is supported by this system.
Definition: network.c:79
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Bind a socket to a particular address.
Definition: network.c:439
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, int backlog)
Listen on a socket.
Definition: network.c:652
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
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:599
#define GNUNET_TIME_UNIT_MINUTES
One minute.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:741
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
Definition: time.c:316
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_monotonic(const struct GNUNET_CONFIGURATION_Handle *cfg)
Obtain the current time and make sure it is monotonically increasing.
Definition: time.c:864
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:640
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:660
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
#define max(x, y)
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:179
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition: scheduler.c:431
DLL to store the addresses we like to register at NAT service.
struct Addresses * next
Kept in a DLL.
struct Addresses * prev
Kept in a DLL.
struct sockaddr * addr
Address we like to register at NAT service.
socklen_t addr_len
Length of address we like to register at NAT service.
Internal representation of the hash map.
Type of a nonce used for challenges.
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!...
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
Private ECC key encoded for transmission.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
Private ECC key encoded for transmission.
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...
an ECC signature using EdDSA.
HPKE DHKEM encapsulation (X25519) See RFC 9180.
A private key for an identity as per LSD0001.
An identity key as per LSD0001.
Handle used to access files (and pipes).
Definition of a command line option.
Context for parsing HELLOs.
Definition: hello-uri.c:232
A 512-bit hashcode.
Handle to a message queue.
Definition: mq.c:87
Header for all communications.
Handle for active NAT registrations.
Definition: nat_api.c:72
handle to a socket
Definition: network.c:53
Handle to the interface scanner.
Definition: nt.c:104
Handle to the PEERSTORE service.
Definition: peerstore_api.c:44
Context for a iterate request.
Single PEERSTORE record.
Context for a store request.
A handle for the PILS service.
Definition: pils_api.c:82
The identity of the host (wraps the signing key of the peer).
struct GNUNET_CRYPTO_EddsaPublicKey public_key
Handle to a request given to the resolver.
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.
uint64_t rekey_left_bytes
How many more bytes may we sent with the current out_cipher before we should rekey?
int mq_awaits_continue
Is MQ awaiting a GNUNET_MQ_impl_send_continue() call?
gcry_cipher_hd_t in_cipher
cipher for decryption of incoming data.
struct GNUNET_PEERSTORE_StoreContext * handshake_ack_monotime_sc
Store Context for retrieving the monotonic time send with the handshake ack.
struct GNUNET_SCHEDULER_Task * write_task
ID of write task for this connection.
size_t pwrite_off
At which offset in the plaintext output buffer should we append more plaintext for encryption next?
struct GNUNET_TIME_Absolute timeout
Timeout for this queue.
size_t cread_off
At which offset in the ciphertext read buffer should we append more ciphertext for transmission next?
struct GNUNET_TIME_Absolute rekey_time
Until what time may we sent with the current out_cipher before we should rekey?
struct GNUNET_PeerIdentity target
To whom are we talking to.
char cwrite_buf[(2 *64 *1024+sizeof(struct TCPBox))]
buffer for writing ciphertext to network.
struct GNUNET_PEERSTORE_StoreContext * handshake_monotime_sc
Store Context for retrieving the monotonic time send with the handshake.
size_t pread_off
At which offset in the plaintext input buffer should we append more plaintext from decryption next?
struct GNUNET_SCHEDULER_Task * read_task
ID of read task for this connection.
enum GNUNET_TRANSPORT_ConnectionStatus cs
The connection status of this queue.
gcry_cipher_hd_t out_cipher
cipher for encryption of outgoing data.
char pwrite_buf[UINT16_MAX+1+sizeof(struct TCPBox)]
Plaintext buffer for messages to be encrypted.
struct GNUNET_HashCode in_hmac
Shared secret for HMAC verification on incoming data.
struct GNUNET_PEERSTORE_IterateContext * handshake_monotime_get
Iteration Context for retrieving the monotonic time send with the handshake.
struct GNUNET_NETWORK_Handle * listen_sock
Listen socket.
struct sockaddr * address
Address of the other peer.
struct GNUNET_TIME_AbsoluteNBO handshake_ack_monotonic_time
Monotonic time value for handshake ack message.
struct GNUNET_TIME_AbsoluteNBO rekey_monotonic_time
Monotonic time value for rekey message.
struct GNUNET_TIME_AbsoluteNBO handshake_monotonic_time
Monotonic time value for handshake message.
TCP message box.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
Ack for the encrypted continuation of TCP initial handshake.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender's signature of type GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE_ACK.
struct GNUNET_CRYPTO_ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_CONFIRMATION_ACK.
struct GNUNET_PeerIdentity sender
Sender's identity.
Encrypted continuation of TCP initial handshake.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender's signature of type GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE.
struct GNUNET_PeerIdentity sender
Sender's identity.
struct GNUNET_CRYPTO_ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
Basically a WELCOME message, but with the purpose of giving the waiting peer a client handle to use.
struct GNUNET_PeerIdentity clientIdentity
Identity of the sender of the message.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
TCP rekey message box.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender's signature of type GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY.
struct GNUNET_CRYPTO_HpkeEncapsulation ephemeral
New ephemeral key.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY.
Signature we use to verify that the ack from the receiver of the ephemeral key was really send by the...
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Purpose must be GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE_ACK.
struct GNUNET_PeerIdentity sender
Identity of the inititor of the TCP connection (TCP client).
struct GNUNET_CRYPTO_ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value.
struct GNUNET_PeerIdentity receiver
Presumed identity of the target of the TCP connection (TCP server)
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
Signature we use to verify that the ephemeral key was really chosen by the specified sender.
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...