GNUnet  0.19.4
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_util_lib.h"
32 #include "gnunet_core_service.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_constants.h"
37 #include "gnunet_nt_lib.h"
38 #include "gnunet_nat_service.h"
42 
43 
48 #define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
49 
54 #define ADDRESS_VALIDITY_PERIOD \
55  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
56 
65 #define DEFAULT_MAX_QUEUE_LENGTH 8
66 
71 #define BUF_SIZE (2 * 64 * 1024 + sizeof(struct TCPBox))
72 
76 #define DEFAULT_REKEY_INTERVAL GNUNET_TIME_UNIT_DAYS
77 
81 #define PROTO_QUEUE_TIMEOUT GNUNET_TIME_UNIT_MINUTES
82 
87 #define REKEY_MAX_BYTES (1024LLU * 1024 * 1024 * 4LLU)
88 
93 #define INITIAL_KX_SIZE \
94  (sizeof(struct GNUNET_CRYPTO_EcdhePublicKey) \
95  + sizeof(struct TCPConfirmation))
96 
100 #define INITIAL_CORE_KX_SIZE \
101  (sizeof(struct EphemeralKeyMessage) \
102  + sizeof(struct PingMessage) \
103  + sizeof(struct PongMessage))
104 
108 #define COMMUNICATOR_ADDRESS_PREFIX "tcp"
109 
113 #define COMMUNICATOR_CONFIG_SECTION "communicator-tcp"
114 
116 
117 
123 {
128 
133 
138 
143 
149 
154 };
155 
161 {
166 
171 
176 
182 
187 };
188 
193 {
198 
203 
209 
214 
215 };
216 
221 {
222 
223 
228 
233 
238 
244 
249 
250 };
251 
255 struct TCPBox
256 {
264 
273  struct GNUNET_ShortHashCode hmac;
274 
275  /* followed by as may bytes of payload as indicated in @e header,
276  excluding the TCPBox itself! */
277 };
278 
279 
284 struct TCPRekey
285 {
290 
299  struct GNUNET_ShortHashCode hmac;
300 
305 
310 
316 };
317 
323 {
328 
333 
338 
343 
349 };
350 
356 struct TCPFinish
357 {
362 
371  struct GNUNET_ShortHashCode hmac;
372 };
373 
379 {
384 
389 };
390 
392 
397 {
398  /*
399  * Timeout task.
400  */
402 
407 
411  struct sockaddr *in;
412 };
413 
418 {
423 
428 };
429 
433 struct Queue
434 {
439 
444 
449 
453  gcry_cipher_hd_t in_cipher;
454 
458  gcry_cipher_hd_t out_cipher;
459 
463  struct GNUNET_HashCode in_hmac;
464 
469  struct GNUNET_HashCode out_hmac;
470 
476 
481 
486 
490  struct sockaddr *address;
491 
497 
503 
507  socklen_t address_len;
508 
513 
518 
522  unsigned long long bytes_in_queue;
523 
528 
533 
537  char pread_buf[UINT16_MAX + 1 + sizeof(struct TCPBox)];
538 
542  char pwrite_buf[UINT16_MAX + 1 + sizeof(struct TCPBox)];
543 
548  size_t cread_off;
549 
554  size_t cwrite_off;
555 
560  size_t pread_off;
561 
566  size_t pwrite_off;
567 
572 
580  unsigned int backpressure;
581 
585  enum GNUNET_NetworkType nt;
586 
591 
596 
601 
609 
614  int rekeyed;
615 
620 
625 
630 
635 
640 
645 
650 
655 
660 
665 
670 };
671 
672 
678 {
682  struct ProtoQueue *next;
683 
687  struct ProtoQueue *prev;
688 
693 
698 
703 
707  char write_buf[sizeof (struct TCPNATProbeMessage)];
708 
712  size_t write_off;
713 
718 
722  struct sockaddr *address;
723 
727  socklen_t address_len;
728 
733 
739 
743  size_t ibuf_off;
744 };
745 
750 {
754  struct sockaddr *addr_ipv4;
755 
759  socklen_t addr_len_ipv4;
760 
764  struct sockaddr *addr_ipv6;
765 
769  socklen_t addr_len_ipv6;
770 
771 };
772 
776 struct Addresses
777 {
781  struct Addresses *next;
782 
786  struct Addresses *prev;
787 
791  struct sockaddr *addr;
792 
796  socklen_t addr_len;
797 
798 };
799 
800 
804 static unsigned long long max_queue_length;
805 
810 
815 
820 
825 
829 static struct GNUNET_PeerIdentity my_identity;
830 
835 
840 
844 static const struct GNUNET_CONFIGURATION_Handle *cfg;
845 
850 
854 static struct GNUNET_NAT_Handle *nat;
855 
859 static struct ProtoQueue *proto_head;
860 
864 static struct ProtoQueue *proto_tail;
865 
870 
875 
880 
885 
890 
895 
899 // TODO remove?
901 
906 
911 
915 unsigned int bind_port;
916 
921 
929 static void
930 listen_cb (void *cls);
931 
939 static void
941 {
942  struct ListenTask *lt = NULL;
943  struct GNUNET_HashCode h_sock;
944  int sockfd;
945 
946  if (NULL != queue->listen_sock)
947  {
948  sockfd = GNUNET_NETWORK_get_fd (queue->listen_sock);
949  GNUNET_CRYPTO_hash (&sockfd,
950  sizeof(int),
951  &h_sock);
952 
954  }
955 
957  "Disconnecting queue for peer `%s'\n",
958  GNUNET_i2s (&queue->target));
959  if (NULL != queue->rekey_monotime_sc)
960  {
961  GNUNET_PEERSTORE_store_cancel (queue->rekey_monotime_sc);
962  queue->rekey_monotime_sc = NULL;
963  }
964  if (NULL != queue->handshake_monotime_sc)
965  {
966  GNUNET_PEERSTORE_store_cancel (queue->handshake_monotime_sc);
967  queue->handshake_monotime_sc = NULL;
968  }
969  if (NULL != queue->handshake_ack_monotime_sc)
970  {
971  GNUNET_PEERSTORE_store_cancel (queue->handshake_ack_monotime_sc);
972  queue->handshake_ack_monotime_sc = NULL;
973  }
974  if (NULL != queue->rekey_monotime_get)
975  {
976  GNUNET_PEERSTORE_iterate_cancel (queue->rekey_monotime_get);
977  queue->rekey_monotime_get = NULL;
978  }
979  if (NULL != queue->handshake_monotime_get)
980  {
981  GNUNET_PEERSTORE_iterate_cancel (queue->handshake_monotime_get);
982  queue->handshake_monotime_get = NULL;
983  }
984  if (NULL != queue->handshake_ack_monotime_get)
985  {
986  GNUNET_PEERSTORE_iterate_cancel (queue->handshake_ack_monotime_get);
987  queue->handshake_ack_monotime_get = NULL;
988  }
989  if (NULL != queue->qh)
990  {
992  queue->qh = NULL;
993  }
994  GNUNET_assert (
995  GNUNET_YES ==
998  "# queues active",
1000  GNUNET_NO);
1001  if (NULL != queue->read_task)
1002  {
1003  GNUNET_SCHEDULER_cancel (queue->read_task);
1004  queue->read_task = NULL;
1005  }
1006  if (NULL != queue->write_task)
1007  {
1008  GNUNET_SCHEDULER_cancel (queue->write_task);
1009  queue->write_task = NULL;
1010  }
1012  {
1014  "closing socket failed\n");
1015  }
1016  gcry_cipher_close (queue->in_cipher);
1017  gcry_cipher_close (queue->out_cipher);
1018  GNUNET_free (queue->address);
1019  if (0 != queue->backpressure)
1020  queue->destroyed = GNUNET_YES;
1021  else
1022  GNUNET_free (queue);
1023 
1024  if (NULL == lt)
1025  return;
1026 
1027  if ((! shutdown_running) && (NULL == lt->listen_task))
1028  {
1030  "add read net listen\n");
1033  lt->listen_sock,
1034  &listen_cb,
1035  lt);
1036  }
1037  else
1038  GNUNET_free (lt);
1039 }
1040 
1041 
1050 static void
1051 calculate_hmac (struct GNUNET_HashCode *hmac_secret,
1052  const void *buf,
1053  size_t buf_size,
1054  struct GNUNET_ShortHashCode *smac)
1055 {
1056  struct GNUNET_HashCode mac;
1057 
1058  GNUNET_CRYPTO_hmac_raw (hmac_secret,
1059  sizeof(struct GNUNET_HashCode),
1060  buf,
1061  buf_size,
1062  &mac);
1063  /* truncate to `struct GNUNET_ShortHashCode` */
1064  memcpy (smac, &mac, sizeof(struct GNUNET_ShortHashCode));
1065  /* ratchet hmac key */
1066  GNUNET_CRYPTO_hash (hmac_secret,
1067  sizeof(struct GNUNET_HashCode),
1068  hmac_secret);
1069 }
1070 
1071 
1078 static void
1080 {
1081  struct TCPFinish fin;
1082 
1083  memset (&fin, 0, sizeof(fin));
1084  fin.header.size = htons (sizeof(fin));
1086  calculate_hmac (&queue->out_hmac, &fin, sizeof(fin), &fin.hmac);
1087  /* if there is any message left in pwrite_buf, we
1088  overwrite it (possibly dropping the last message
1089  from CORE hard here) */
1090  memcpy (queue->pwrite_buf, &fin, sizeof(fin));
1091  queue->pwrite_off = sizeof(fin);
1092  /* This flag will ensure that #queue_write() no longer
1093  notifies CORE about the possibility of sending
1094  more data, and that #queue_write() will call
1095  #queue_destroy() once the @c fin was fully written. */
1096  queue->finishing = GNUNET_YES;
1097 }
1098 
1099 
1107 static void
1109 {
1110  queue->timeout =
1112 }
1113 
1114 
1120 static void
1121 queue_read (void *cls);
1122 
1123 
1131 static void
1132 core_read_finished_cb (void *cls, int success)
1133 {
1134  struct Queue *queue = cls;
1135  if (GNUNET_OK != success)
1137  "# messages lost in communicator API towards CORE",
1138  1,
1139  GNUNET_NO);
1140  if (NULL == queue)
1141  return;
1142 
1144  "backpressure %u\n",
1145  queue->backpressure);
1146 
1147  queue->backpressure--;
1148  /* handle deferred queue destruction */
1149  if ((queue->destroyed) && (0 == queue->backpressure))
1150  {
1151  GNUNET_free (queue);
1152  return;
1153  }
1154  else if (GNUNET_YES != queue->destroyed)
1155  {
1157  /* possibly unchoke reading, now that CORE made progress */
1158  if (NULL == queue->read_task)
1159  queue->read_task =
1161  queue->timeout),
1162  queue->sock,
1163  &queue_read,
1164  queue);
1165  }
1166 }
1167 
1168 
1178 static void
1180  const void *plaintext,
1181  size_t plaintext_len)
1182 {
1183  const struct GNUNET_MessageHeader *hdr = plaintext;
1184  int ret;
1185 
1187  "pass message from %s to core\n",
1188  GNUNET_i2s (&queue->target));
1189 
1190  if (ntohs (hdr->size) != plaintext_len)
1191  {
1192  /* NOTE: If we ever allow multiple CORE messages in one
1193  BOX, this will have to change! */
1194  GNUNET_break (0);
1195  return;
1196  }
1198  &queue->target,
1199  hdr,
1202  queue);
1204  "passed to core\n");
1205  if (GNUNET_OK == ret)
1206  queue->backpressure++;
1207  GNUNET_break (GNUNET_NO != ret); /* backpressure not working!? */
1208  if (GNUNET_SYSERR == ret)
1210  "# bytes lost due to CORE not running",
1211  plaintext_len,
1212  GNUNET_NO);
1213 }
1214 
1215 
1225 static void
1226 setup_cipher (const struct GNUNET_HashCode *dh,
1227  const struct GNUNET_PeerIdentity *pid,
1228  gcry_cipher_hd_t *cipher,
1229  struct GNUNET_HashCode *hmac_key)
1230 {
1231  char key[256 / 8];
1232  char ctr[128 / 8];
1233 
1234  GNUNET_assert (0 == gcry_cipher_open (cipher,
1235  GCRY_CIPHER_AES256 /* low level: go for speed */,
1236  GCRY_CIPHER_MODE_CTR,
1237  0 /* flags */));
1239  sizeof(key),
1240  "TCP-key",
1241  strlen ("TCP-key"),
1242  dh,
1243  sizeof(*dh),
1244  pid,
1245  sizeof(*pid),
1246  NULL,
1247  0));
1248  GNUNET_assert (0 == gcry_cipher_setkey (*cipher, key, sizeof(key)));
1250  sizeof(ctr),
1251  "TCP-ctr",
1252  strlen ("TCP-ctr"),
1253  dh,
1254  sizeof(*dh),
1255  pid,
1256  sizeof(*pid),
1257  NULL,
1258  0));
1259  gcry_cipher_setctr (*cipher, ctr, sizeof(ctr));
1261  GNUNET_CRYPTO_kdf (hmac_key,
1262  sizeof(struct GNUNET_HashCode),
1263  "TCP-hmac",
1264  strlen ("TCP-hmac"),
1265  dh,
1266  sizeof(*dh),
1267  pid,
1268  sizeof(*pid),
1269  NULL,
1270  0));
1271 }
1272 
1273 
1279 static void
1280 rekey_monotime_store_cb (void *cls, int success)
1281 {
1282  struct Queue *queue = cls;
1283  if (GNUNET_OK != success)
1284  {
1286  "Failed to store rekey monotonic time in PEERSTORE!\n");
1287  }
1288  queue->rekey_monotime_sc = NULL;
1289 }
1290 
1291 
1299 static void
1301  const struct GNUNET_PEERSTORE_Record *record,
1302  const char *emsg)
1303 {
1304  struct Queue *queue = cls;
1305  struct GNUNET_TIME_AbsoluteNBO *mtbe;
1306  struct GNUNET_TIME_Absolute mt;
1307  const struct GNUNET_PeerIdentity *pid;
1308  struct GNUNET_TIME_AbsoluteNBO *rekey_monotonic_time;
1309 
1310  (void) emsg;
1311 
1312  rekey_monotonic_time = &queue->rekey_monotonic_time;
1313  pid = &queue->target;
1314  if (NULL == record)
1315  {
1316  queue->rekey_monotime_get = NULL;
1317  return;
1318  }
1319  if (sizeof(*mtbe) != record->value_size)
1320  {
1321  GNUNET_break (0);
1322  return;
1323  }
1324  mtbe = record->value;
1325  mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1327  queue->rekey_monotonic_time).abs_value_us)
1328  {
1330  "Queue from %s dropped, rekey monotime in the past\n",
1331  GNUNET_i2s (&queue->target));
1332  GNUNET_break (0);
1333  queue_finish (queue);
1334  return;
1335  }
1336  queue->rekey_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
1337  "transport_tcp_communicator",
1338  pid,
1340  rekey_monotonic_time,
1341  sizeof(*
1342  rekey_monotonic_time),
1346  queue);
1347 }
1348 
1349 
1356 static void
1358  struct Queue *queue)
1359 {
1360  struct GNUNET_HashCode dh;
1361 
1362  GNUNET_CRYPTO_eddsa_ecdh (my_private_key, ephemeral, &dh);
1363  setup_cipher (&dh, &my_identity, &queue->in_cipher, &queue->in_hmac);
1364 }
1365 
1366 
1375 static void
1376 do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
1377 {
1378  struct TcpRekeySignature thp;
1379 
1381  thp.purpose.size = htonl (sizeof(thp));
1383  "do_rekey size %u\n",
1384  thp.purpose.size);
1385  thp.sender = queue->target;
1387  "sender %s\n",
1388  GNUNET_p2s (&thp.sender.public_key));
1390  "sender %s\n",
1391  GNUNET_p2s (&queue->target.public_key));
1392  thp.receiver = my_identity;
1394  "receiver %s\n",
1395  GNUNET_p2s (&thp.receiver.public_key));
1396  thp.ephemeral = rekey->ephemeral;
1398  "ephemeral %s\n",
1399  GNUNET_e2s (&thp.ephemeral));
1400  thp.monotonic_time = rekey->monotonic_time;
1402  "time %s\n",
1405  GNUNET_assert (ntohl ((&thp)->purpose.size) == sizeof (*(&thp)));
1406  if (GNUNET_OK !=
1409  &thp,
1410  &rekey->sender_sig,
1411  &queue->target.public_key))
1412  {
1413  GNUNET_break (0);
1414  queue_finish (queue);
1415  return;
1416  }
1417  queue->rekey_monotonic_time = rekey->monotonic_time;
1418  queue->rekey_monotime_get = GNUNET_PEERSTORE_iterate (peerstore,
1419  "transport_tcp_communicator",
1420  &queue->target,
1423  queue);
1424  gcry_cipher_close (queue->in_cipher);
1425  queue->rekeyed = GNUNET_YES;
1426  setup_in_cipher (&rekey->ephemeral, queue);
1427 }
1428 
1429 
1435 static void
1436 handshake_ack_monotime_store_cb (void *cls, int success)
1437 {
1438  struct Queue *queue = cls;
1439 
1440  if (GNUNET_OK != success)
1441  {
1443  "Failed to store handshake ack monotonic time in PEERSTORE!\n");
1444  }
1445  queue->handshake_ack_monotime_sc = NULL;
1446 }
1447 
1448 
1456 static void
1458  const struct GNUNET_PEERSTORE_Record *record,
1459  const char *emsg)
1460 {
1461  struct Queue *queue = cls;
1462  struct GNUNET_TIME_AbsoluteNBO *mtbe;
1463  struct GNUNET_TIME_Absolute mt;
1464  const struct GNUNET_PeerIdentity *pid;
1465  struct GNUNET_TIME_AbsoluteNBO *handshake_ack_monotonic_time;
1466 
1467  (void) emsg;
1468 
1469  handshake_ack_monotonic_time = &queue->handshake_ack_monotonic_time;
1470  pid = &queue->target;
1471  if (NULL == record)
1472  {
1473  queue->handshake_ack_monotime_get = NULL;
1474  return;
1475  }
1476  if (sizeof(*mtbe) != record->value_size)
1477  {
1478  GNUNET_break (0);
1479  return;
1480  }
1481  mtbe = record->value;
1482  mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1484  queue->handshake_ack_monotonic_time).abs_value_us)
1485  {
1487  "Queue from %s dropped, handshake ack monotime in the past\n",
1488  GNUNET_i2s (&queue->target));
1489  GNUNET_break (0);
1490  queue_finish (queue);
1491  return;
1492  }
1493  queue->handshake_ack_monotime_sc =
1495  "transport_tcp_communicator",
1496  pid,
1498  handshake_ack_monotonic_time,
1499  sizeof(*handshake_ack_monotonic_time),
1502  &
1504  queue);
1505 }
1506 
1507 
1514 static void
1516  struct Queue *queue)
1517 {
1518  struct TCPConfirmationAck tca;
1519  struct TcpHandshakeAckSignature thas;
1520 
1522  "sending challenge\n");
1523 
1524  tca.header.type = ntohs (
1526  tca.header.size = ntohs (sizeof(tca));
1527  tca.challenge = challenge;
1528  tca.sender = my_identity;
1529  tca.monotonic_time =
1531  thas.purpose.purpose = htonl (
1533  thas.purpose.size = htonl (sizeof(thas));
1534  thas.sender = my_identity;
1535  thas.receiver = queue->target;
1536  thas.monotonic_time = tca.monotonic_time;
1537  thas.challenge = tca.challenge;
1539  &thas,
1540  &tca.sender_sig);
1541  GNUNET_assert (0 ==
1542  gcry_cipher_encrypt (queue->out_cipher,
1543  &queue->cwrite_buf[queue->cwrite_off],
1544  sizeof(tca),
1545  &tca,
1546  sizeof(tca)));
1547  queue->cwrite_off += sizeof(tca);
1549  "sending challenge done\n");
1550 }
1551 
1552 
1559 static void
1561 {
1562  struct GNUNET_HashCode dh;
1563 
1564  GNUNET_CRYPTO_ecdh_eddsa (&queue->ephemeral, &queue->target.public_key, &dh);
1565  /* we don't need the private key anymore, drop it! */
1566  memset (&queue->ephemeral, 0, sizeof(queue->ephemeral));
1567  setup_cipher (&dh, &queue->target, &queue->out_cipher, &queue->out_hmac);
1569  queue->rekey_left_bytes =
1571 }
1572 
1573 
1580 static void
1582 {
1583  struct TCPRekey rekey;
1584  struct TcpRekeySignature thp;
1585 
1586  GNUNET_assert (0 == queue->pwrite_off);
1587  memset (&rekey, 0, sizeof(rekey));
1588  GNUNET_CRYPTO_ecdhe_key_create (&queue->ephemeral);
1590  rekey.header.size = ntohs (sizeof(rekey));
1591  GNUNET_CRYPTO_ecdhe_key_get_public (&queue->ephemeral, &rekey.ephemeral);
1592  rekey.monotonic_time =
1595  thp.purpose.size = htonl (sizeof(thp));
1597  "inject_rekey size %u\n",
1598  thp.purpose.size);
1599  thp.sender = my_identity;
1601  "sender %s\n",
1602  GNUNET_p2s (&thp.sender.public_key));
1603  thp.receiver = queue->target;
1605  "receiver %s\n",
1606  GNUNET_p2s (&thp.receiver.public_key));
1607  thp.ephemeral = rekey.ephemeral;
1609  "ephemeral %s\n",
1610  GNUNET_e2s (&thp.ephemeral));
1611  thp.monotonic_time = rekey.monotonic_time;
1613  "time %s\n",
1617  &thp,
1618  &rekey.sender_sig);
1619  calculate_hmac (&queue->out_hmac, &rekey, sizeof(rekey), &rekey.hmac);
1620  /* Encrypt rekey message with 'old' cipher */
1621  GNUNET_assert (0 ==
1622  gcry_cipher_encrypt (queue->out_cipher,
1623  &queue->cwrite_buf[queue->cwrite_off],
1624  sizeof(rekey),
1625  &rekey,
1626  sizeof(rekey)));
1627  queue->cwrite_off += sizeof(rekey);
1628  /* Setup new cipher for successive messages */
1629  gcry_cipher_close (queue->out_cipher);
1631 }
1632 
1633 static int
1635  const struct GNUNET_HashCode *key,
1636  void *value)
1637 {
1638  (void) cls;
1639  struct PendingReversal *pending_reversal = value;
1640 
1641  if (NULL != pending_reversal->timeout_task)
1642  {
1643  GNUNET_SCHEDULER_cancel (pending_reversal->timeout_task);
1644  pending_reversal->timeout_task = NULL;
1645  }
1648  key,
1649  pending_reversal));
1650  GNUNET_free (pending_reversal->in);
1651  GNUNET_free (pending_reversal);
1652  return GNUNET_OK;
1653 }
1654 
1655 
1656 static void
1657 check_and_remove_pending_reversal (struct sockaddr *in, sa_family_t sa_family,
1658  struct GNUNET_PeerIdentity *sender)
1659 {
1660  if (AF_INET == sa_family)
1661  {
1662  struct PendingReversal *pending_reversal;
1663  struct GNUNET_HashCode key;
1664  struct sockaddr_in *natted_address;
1665 
1666  natted_address = GNUNET_memdup (in, sizeof (struct sockaddr));
1667  natted_address->sin_port = 0;
1668  GNUNET_CRYPTO_hash (natted_address,
1669  sizeof(struct sockaddr),
1670  &key);
1671 
1673  &key);
1674  if (NULL != pending_reversal && (NULL == sender ||
1675  0 != memcmp (sender,
1676  &pending_reversal->target,
1677  sizeof(struct
1679  {
1681  "Removing invalid pending reversal for `%s'at `%s'\n",
1682  GNUNET_i2s (&pending_reversal->target),
1683  GNUNET_a2s (in, sizeof (struct sockaddr)));
1684  pending_reversals_delete_it (NULL, &key, pending_reversal);
1685  }
1686  GNUNET_free (natted_address);
1687  }
1688 }
1689 
1690 
1696 static void
1698 {
1699  if (NULL != pq->listen_sock)
1700  {
1702  pq->listen_sock = NULL;
1703  }
1704  if (NULL != pq->read_task)
1705  {
1707  pq->read_task = NULL;
1708  }
1709  if (NULL != pq->write_task)
1710  {
1712  pq->write_task = NULL;
1713  }
1714  check_and_remove_pending_reversal (pq->address, pq->address->sa_family, NULL);
1716  GNUNET_free (pq->address);
1718  GNUNET_free (pq);
1719 }
1720 
1721 
1728 static void
1730 {
1731  struct ProtoQueue *pq = cls;
1732  ssize_t sent;
1733  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In proto queue write\n");
1734  pq->write_task = NULL;
1735  if (0 != pq->write_off)
1736  {
1737  sent = GNUNET_NETWORK_socket_send (pq->sock,
1738  pq->write_buf,
1739  pq->write_off);
1741  "Sent %lu bytes to TCP queue\n", sent);
1742  if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1743  {
1745  free_proto_queue (pq);
1746  return;
1747  }
1748  if (sent > 0)
1749  {
1750  size_t usent = (size_t) sent;
1751  pq->write_off -= usent;
1752  memmove (pq->write_buf,
1753  &pq->write_buf[usent],
1754  pq->write_off);
1755  }
1756  }
1757  /* do we care to write more? */
1758  if ((0 < pq->write_off))
1759  pq->write_task =
1761  pq->sock,
1763  pq);
1764 }
1765 
1766 
1773 static void
1774 queue_write (void *cls)
1775 {
1776  struct Queue *queue = cls;
1777  ssize_t sent;
1778  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In queue write\n");
1779  queue->write_task = NULL;
1780  if (0 != queue->cwrite_off)
1781  {
1782  sent = GNUNET_NETWORK_socket_send (queue->sock,
1783  queue->cwrite_buf,
1784  queue->cwrite_off);
1786  "Sent %lu bytes to TCP queue\n", sent);
1787  if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1788  {
1790  queue_destroy (queue);
1791  return;
1792  }
1793  if (sent > 0)
1794  {
1795  size_t usent = (size_t) sent;
1796  queue->cwrite_off -= usent;
1797  memmove (queue->cwrite_buf,
1798  &queue->cwrite_buf[usent],
1799  queue->cwrite_off);
1801  }
1802  }
1803  /* can we encrypt more? (always encrypt full messages, needed
1804  such that #mq_cancel() can work!) */
1805  unsigned int we_do_not_need_to_rekey = (0 < queue->rekey_left_bytes
1806  - (queue->cwrite_off
1807  + queue->pwrite_off
1808  + sizeof (struct TCPRekey)));
1809  if (we_do_not_need_to_rekey &&
1810  (queue->pwrite_off > 0) &&
1811  (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE))
1812  {
1814  "Encrypting %lu bytes\n", queue->pwrite_off);
1815  GNUNET_assert (0 ==
1816  gcry_cipher_encrypt (queue->out_cipher,
1817  &queue->cwrite_buf[queue->cwrite_off],
1818  queue->pwrite_off,
1819  queue->pwrite_buf,
1820  queue->pwrite_off));
1821  if (queue->rekey_left_bytes > queue->pwrite_off)
1822  queue->rekey_left_bytes -= queue->pwrite_off;
1823  else
1824  queue->rekey_left_bytes = 0;
1825  queue->cwrite_off += queue->pwrite_off;
1826  queue->pwrite_off = 0;
1827  }
1828  // if ((-1 != unverified_size)&& ((0 == queue->pwrite_off) &&
1829  if (((0 == queue->rekey_left_bytes) ||
1831  queue->rekey_time).rel_value_us)) &&
1832  (((0 == queue->pwrite_off) || ! we_do_not_need_to_rekey)&&
1833  (queue->cwrite_off + sizeof (struct TCPRekey) <= BUF_SIZE)))
1834  {
1835  inject_rekey (queue);
1836  }
1837  if ((0 == queue->pwrite_off) && (! queue->finishing) &&
1838  (GNUNET_YES == queue->mq_awaits_continue))
1839  {
1840  queue->mq_awaits_continue = GNUNET_NO;
1842  }
1843  /* did we just finish writing 'finish'? */
1844  if ((0 == queue->cwrite_off) && (GNUNET_YES == queue->finishing))
1845  {
1847  "Finishing queue\n");
1848  queue_destroy (queue);
1849  return;
1850  }
1851  /* do we care to write more? */
1852  if ((0 < queue->cwrite_off) || (0 < queue->pwrite_off))
1853  queue->write_task =
1855  queue->sock,
1856  &queue_write,
1857  queue);
1858 }
1859 
1860 
1868 static size_t
1870 {
1871  const struct GNUNET_MessageHeader *hdr =
1872  (const struct GNUNET_MessageHeader *) queue->pread_buf;
1873  const struct TCPConfirmationAck *tca = (const struct
1874  TCPConfirmationAck *) queue->pread_buf;
1875  const struct TCPBox *box = (const struct TCPBox *) queue->pread_buf;
1876  const struct TCPRekey *rekey = (const struct TCPRekey *) queue->pread_buf;
1877  const struct TCPFinish *fin = (const struct TCPFinish *) queue->pread_buf;
1878  struct TCPRekey rekeyz;
1879  struct TCPFinish finz;
1880  struct GNUNET_ShortHashCode tmac;
1881  uint16_t type;
1882  size_t size = 0; /* make compiler happy */
1883  struct TcpHandshakeAckSignature thas;
1884  const struct GNUNET_CRYPTO_ChallengeNonceP challenge = queue->challenge;
1885 
1887  "try handle plaintext!\n");
1888 
1889  if ((sizeof(*hdr) > queue->pread_off))
1890  {
1892  "Handling plaintext, not even a header!\n");
1893  return 0; /* not even a header */
1894  }
1895 
1897  {
1899  "Already received data of size %lu bigger than KX size %lu!\n",
1902  GNUNET_break_op (0);
1903  queue_finish (queue);
1904  return 0;
1905  }
1906 
1907  type = ntohs (hdr->type);
1908  switch (type)
1909  {
1912  "start processing ack\n");
1913  if (sizeof(*tca) > queue->pread_off)
1914  {
1916  "Handling plaintext size of tca greater than pread offset.\n");
1917  return 0;
1918  }
1919  if (ntohs (hdr->size) != sizeof(*tca))
1920  {
1922  "Handling plaintext size does not match message type.\n");
1923  GNUNET_break_op (0);
1924  queue_finish (queue);
1925  return 0;
1926  }
1927 
1928  thas.purpose.purpose = htonl (
1930  thas.purpose.size = htonl (sizeof(thas));
1931  thas.sender = tca->sender;
1932  thas.receiver = my_identity;
1933  thas.monotonic_time = tca->monotonic_time;
1934  thas.challenge = tca->challenge;
1935 
1938  &thas,
1939  &tca->sender_sig,
1940  &tca->sender.public_key))
1941  {
1943  "Verification of signature failed!\n");
1944  GNUNET_break (0);
1945  queue_finish (queue);
1946  return 0;
1947  }
1948  if (0 != GNUNET_memcmp (&tca->challenge, &challenge))
1949  {
1951  "Challenge in TCPConfirmationAck not correct!\n");
1952  GNUNET_break (0);
1953  queue_finish (queue);
1954  return 0;
1955  }
1956 
1957  queue->handshake_ack_monotime_get = GNUNET_PEERSTORE_iterate (peerstore,
1958  "transport_tcp_communicator",
1959  &queue->target,
1961  &
1963  queue);
1964 
1966  "Handling plaintext, ack processed!\n");
1967 
1969  {
1970  send_challenge (queue->challenge_received, queue);
1971  queue->write_task =
1973  queue->sock,
1974  &queue_write,
1975  queue);
1976  }
1977  else if (GNUNET_TRANSPORT_CS_OUTBOUND == queue->cs)
1978  {
1980  queue->address->sa_family, NULL);
1981  }
1982 
1983  unverified_size = -1;
1984 
1985  char *foreign_addr;
1986 
1987  switch (queue->address->sa_family)
1988  {
1989  case AF_INET:
1990  GNUNET_asprintf (&foreign_addr,
1991  "%s-%s",
1993  GNUNET_a2s (queue->address, queue->address_len));
1994  break;
1995 
1996  case AF_INET6:
1997  GNUNET_asprintf (&foreign_addr,
1998  "%s-%s",
2000  GNUNET_a2s (queue->address, queue->address_len));
2001  break;
2002 
2003  default:
2004  GNUNET_assert (0);
2005  }
2006 
2008  &queue->target,
2009  foreign_addr,
2010  UINT16_MAX, /* no MTU */
2012  0, /* Priority */
2013  queue->nt,
2014  queue->cs,
2015  queue->mq);
2016 
2017  GNUNET_free (foreign_addr);
2018 
2019  size = ntohs (hdr->size);
2020  break;
2022  /* Special case: header size excludes box itself! */
2023  if (ntohs (hdr->size) + sizeof(struct TCPBox) > queue->pread_off)
2024  return 0;
2025  calculate_hmac (&queue->in_hmac, &box[1], ntohs (hdr->size), &tmac);
2026  if (0 != memcmp (&tmac, &box->hmac, sizeof(tmac)))
2027  {
2028  GNUNET_break_op (0);
2029  queue_finish (queue);
2030  return 0;
2031  }
2032  pass_plaintext_to_core (queue, (const void *) &box[1], ntohs (hdr->size));
2033  size = ntohs (hdr->size) + sizeof(*box);
2035  "Handling plaintext, box processed!\n");
2036  break;
2037 
2039  if (sizeof(*rekey) > queue->pread_off)
2040  return 0;
2041  if (ntohs (hdr->size) != sizeof(*rekey))
2042  {
2043  GNUNET_break_op (0);
2044  queue_finish (queue);
2045  return 0;
2046  }
2047  rekeyz = *rekey;
2048  memset (&rekeyz.hmac, 0, sizeof(rekeyz.hmac));
2049  calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
2050  if (0 != memcmp (&tmac, &rekey->hmac, sizeof(tmac)))
2051  {
2052  GNUNET_break_op (0);
2053  queue_finish (queue);
2054  return 0;
2055  }
2056  do_rekey (queue, rekey);
2057  size = ntohs (hdr->size);
2059  "Handling plaintext, rekey processed!\n");
2060  break;
2061 
2063  if (sizeof(*fin) > queue->pread_off)
2064  return 0;
2065  if (ntohs (hdr->size) != sizeof(*fin))
2066  {
2067  GNUNET_break_op (0);
2068  queue_finish (queue);
2069  return 0;
2070  }
2071  finz = *fin;
2072  memset (&finz.hmac, 0, sizeof(finz.hmac));
2073  calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
2074  if (0 != memcmp (&tmac, &fin->hmac, sizeof(tmac)))
2075  {
2076  GNUNET_break_op (0);
2077  queue_finish (queue);
2078  return 0;
2079  }
2080  /* handle FINISH by destroying queue */
2081  queue_destroy (queue);
2083  "Handling plaintext, finish processed!\n");
2084  break;
2085 
2086  default:
2088  "Handling plaintext, nothing processed!\n");
2089  GNUNET_break_op (0);
2090  queue_finish (queue);
2091  return 0;
2092  }
2093  GNUNET_assert (0 != size);
2094  if (-1 != unverified_size)
2095  unverified_size += size;
2096  return size;
2097 }
2098 
2099 
2105 static void
2106 queue_read (void *cls)
2107 {
2108  struct Queue *queue = cls;
2109  struct GNUNET_TIME_Relative left;
2110  ssize_t rcvd;
2111 
2112  queue->read_task = NULL;
2113  rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2114  &queue->cread_buf[queue->cread_off],
2115  BUF_SIZE - queue->cread_off);
2117  "Received %zd bytes from TCP queue\n", rcvd);
2118  if (-1 == rcvd)
2119  {
2120  if ((EAGAIN != errno) && (EINTR != errno))
2121  {
2123  queue_finish (queue);
2124  return;
2125  }
2126  /* try again */
2127  left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2128  queue->read_task =
2130  return;
2131  }
2132  if (0 != rcvd)
2134  queue->cread_off += rcvd;
2135  while ((queue->pread_off < sizeof(queue->pread_buf)) &&
2136  (queue->cread_off > 0))
2137  {
2138  size_t max = GNUNET_MIN (sizeof(queue->pread_buf) - queue->pread_off,
2139  queue->cread_off);
2140  size_t done;
2141  size_t total;
2142  size_t old_pread_off = queue->pread_off;
2143 
2144  GNUNET_assert (0 ==
2145  gcry_cipher_decrypt (queue->in_cipher,
2146  &queue->pread_buf[queue->pread_off],
2147  max,
2148  queue->cread_buf,
2149  max));
2150  queue->pread_off += max;
2151  total = 0;
2152  while (0 != (done = try_handle_plaintext (queue)))
2153  {
2154  /* 'done' bytes of plaintext were used, shift buffer */
2155  GNUNET_assert (done <= queue->pread_off);
2156  /* NOTE: this memmove() could possibly sometimes be
2157  avoided if we pass 'total' into try_handle_plaintext()
2158  and use it at an offset into the buffer there! */
2159  memmove (queue->pread_buf,
2160  &queue->pread_buf[done],
2161  queue->pread_off - done);
2162  queue->pread_off -= done;
2163  total += done;
2164  /* The last plaintext was a rekey, abort for now */
2165  if (GNUNET_YES == queue->rekeyed)
2166  break;
2167  }
2168  /* when we encounter a rekey message, the decryption above uses the
2169  wrong key for everything after the rekey; in that case, we have
2170  to re-do the decryption at 'total' instead of at 'max'.
2171  However, we have to take into account that the plaintext buffer may have
2172  already contained data and not jumped too far ahead in the ciphertext.
2173  If there is no rekey and the last message is incomplete (max > total),
2174  it is safe to keep the decryption so we shift by 'max' */
2175  if (GNUNET_YES == queue->rekeyed)
2176  {
2177  max = total - old_pread_off;
2178  queue->rekeyed = GNUNET_NO;
2179  queue->pread_off = 0;
2180  }
2181  memmove (queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
2182  queue->cread_off -= max;
2183  }
2184  if (BUF_SIZE == queue->cread_off)
2185  return; /* buffer full, suspend reading */
2186  left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2187  if (0 != left.rel_value_us)
2188  {
2189  if (max_queue_length > queue->backpressure)
2190  {
2191  /* continue reading */
2192  left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2193  queue->read_task =
2195  }
2196  return;
2197  }
2199  "Queue %p was idle for %s, disconnecting\n",
2200  queue,
2203  GNUNET_YES));
2204  queue_finish (queue);
2205 }
2206 
2207 
2215 static struct sockaddr *
2217  struct sockaddr_in6 v6,
2218  unsigned int port)
2219 {
2220  struct sockaddr *in;
2221 
2222  v6.sin6_family = AF_INET6;
2223  v6.sin6_port = htons ((uint16_t) port);
2224 #if HAVE_SOCKADDR_IN_SIN_LEN
2225  v6.sin6_len = sizeof(sizeof(struct sockaddr_in6));
2226 #endif
2227  v6.sin6_flowinfo = 0;
2228  v6.sin6_scope_id = 0;
2229  in = GNUNET_memdup (&v6, sizeof(v6));
2230  *sock_len = sizeof(struct sockaddr_in6);
2231 
2232  return in;
2233 }
2234 
2235 
2243 static struct sockaddr *
2245  struct sockaddr_in v4,
2246  unsigned int port)
2247 {
2248  struct sockaddr *in;
2249 
2250  v4.sin_family = AF_INET;
2251  v4.sin_port = htons ((uint16_t) port);
2252 #if HAVE_SOCKADDR_IN_SIN_LEN
2253  v4.sin_len = sizeof(struct sockaddr_in);
2254 #endif
2255  in = GNUNET_memdup (&v4, sizeof(v4));
2256  *sock_len = sizeof(struct sockaddr_in);
2257  return in;
2258 }
2259 
2260 
2267 static struct PortOnlyIpv4Ipv6 *
2268 tcp_address_to_sockaddr_port_only (const char *bindto, unsigned int *port)
2269 {
2270  struct PortOnlyIpv4Ipv6 *po;
2271  struct sockaddr_in *i4;
2272  struct sockaddr_in6 *i6;
2273  socklen_t sock_len_ipv4;
2274  socklen_t sock_len_ipv6;
2275 
2276  /* interpreting value as just a PORT number */
2277  if (*port > UINT16_MAX)
2278  {
2280  "BINDTO specification `%s' invalid: value too large for port\n",
2281  bindto);
2282  return NULL;
2283  }
2284 
2285  po = GNUNET_new (struct PortOnlyIpv4Ipv6);
2286 
2287  if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
2288  (GNUNET_YES ==
2291  "DISABLE_V6")))
2292  {
2293  i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2294  po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2295  *port);
2296  po->addr_len_ipv4 = sock_len_ipv4;
2297  }
2298  else
2299  {
2300 
2301  i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
2302  po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
2303  *port);
2304  po->addr_len_ipv4 = sock_len_ipv4;
2305 
2306  i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
2307  po->addr_ipv6 = tcp_address_to_sockaddr_numeric_v6 (&sock_len_ipv6, *i6,
2308  *port);
2309 
2310  po->addr_len_ipv6 = sock_len_ipv6;
2311 
2312  GNUNET_free (i6);
2313  }
2314 
2315  GNUNET_free (i4);
2316 
2317  return po;
2318 }
2319 
2320 
2327 static char *
2328 extract_address (const char *bindto)
2329 {
2330  char *addr;
2331  char *start;
2332  char *token;
2333  char *cp;
2334  char *rest = NULL;
2335  char *res;
2336 
2338  "extract address with bindto %s\n",
2339  bindto);
2340 
2341  if (NULL == bindto)
2343  "bindto is NULL\n");
2344 
2345  cp = GNUNET_strdup (bindto);
2346 
2348  "extract address 2\n");
2349 
2350  start = cp;
2351  if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
2352  {
2353  start++; /* skip over '['*/
2354  cp[strlen (cp) - 1] = '\0'; /* eat ']'*/
2355  addr = GNUNET_strdup (start);
2356  }
2357  else
2358  {
2359  token = strtok_r (cp, "]", &rest);
2360  if (strlen (bindto) == strlen (token))
2361  {
2362  token = strtok_r (cp, ":", &rest);
2363  addr = GNUNET_strdup (token);
2364  }
2365  else
2366  {
2367  token++;
2368  res = GNUNET_strdup (token);
2369  addr = GNUNET_strdup (res);
2370  }
2371  }
2372 
2374  "tcp address: %s\n",
2375  addr);
2376  GNUNET_free (cp);
2377  return addr;
2378 }
2379 
2380 
2387 static unsigned int
2388 extract_port (const char *addr_and_port)
2389 {
2390  unsigned int port;
2391  char dummy[2];
2392  char *token;
2393  char *addr;
2394  char *colon;
2395  char *cp;
2396  char *rest = NULL;
2397 
2398  if (NULL != addr_and_port)
2399  {
2400  cp = GNUNET_strdup (addr_and_port);
2401  token = strtok_r (cp, "]", &rest);
2402  if (strlen (addr_and_port) == strlen (token))
2403  {
2404  colon = strrchr (cp, ':');
2405  if (NULL == colon)
2406  {
2407  GNUNET_free (cp);
2408  return 0;
2409  }
2410  addr = colon;
2411  addr++;
2412  }
2413  else
2414  {
2415  token = strtok_r (NULL, "]", &rest);
2416  if (NULL == token)
2417  {
2418  GNUNET_free (cp);
2419  return 0;
2420  }
2421  else
2422  {
2423  addr = token;
2424  addr++;
2425  }
2426  }
2427 
2428 
2429  if (1 == sscanf (addr, "%u%1s", &port, dummy))
2430  {
2431  /* interpreting value as just a PORT number */
2432  if (port > UINT16_MAX)
2433  {
2435  "Port `%u' invalid: value too large for port\n",
2436  port);
2437  GNUNET_free (cp);
2438  return 0;
2439  }
2440  }
2441  else
2442  {
2444  "BINDTO specification invalid: last ':' not followed by number\n");
2445  GNUNET_free (cp);
2446  return 0;
2447  }
2448  GNUNET_free (cp);
2449  }
2450  else
2451  {
2453  "return 0\n");
2454  /* interpret missing port as 0, aka pick any free one */
2455  port = 0;
2456  }
2457 
2458  return port;
2459 }
2460 
2461 
2469 static struct sockaddr *
2470 tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
2471 {
2472  struct sockaddr *in;
2473  unsigned int port;
2474  struct sockaddr_in v4;
2475  struct sockaddr_in6 v6;
2476  char *start;
2477 
2478  memset (&v4, 0, sizeof(v4));
2479  start = extract_address (bindto);
2480  GNUNET_assert (NULL != start);
2482  "start %s\n",
2483  start);
2484 
2486  "!bindto %s\n",
2487  bindto);
2488 
2489 
2490  if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
2491  {
2492  // colon = strrchr (cp, ':');
2493  port = extract_port (bindto);
2494 
2496  "port %u\n",
2497  port);
2498 
2499  in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port);
2500  }
2501  else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
2502  {
2503  // colon = strrchr (cp, ':');
2504  port = extract_port (bindto);
2505  in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port);
2506  }
2507  else
2508  {
2509  GNUNET_assert (0);
2510  }
2511 
2512  GNUNET_free (start);
2513  return in;
2514 }
2515 
2516 
2525 static void
2527  const struct GNUNET_MessageHeader *msg,
2528  void *impl_state)
2529 {
2530  struct Queue *queue = impl_state;
2531  uint16_t msize = ntohs (msg->size);
2532  struct TCPBox box;
2534  "In MQ send. Queue finishing: %s; write task running: %s\n",
2535  (GNUNET_YES == queue->finishing) ? "yes" : "no",
2536  (NULL == queue->write_task) ? "yes" : "no");
2537  GNUNET_assert (mq == queue->mq);
2538  queue->mq_awaits_continue = GNUNET_YES;
2539  if (GNUNET_YES == queue->finishing)
2540  return; /* this queue is dying, drop msg */
2541  GNUNET_assert (0 == queue->pwrite_off);
2543  box.header.size = htons (msize);
2544  calculate_hmac (&queue->out_hmac, msg, msize, &box.hmac);
2545  memcpy (&queue->pwrite_buf[queue->pwrite_off], &box, sizeof(box));
2546  queue->pwrite_off += sizeof(box);
2547  memcpy (&queue->pwrite_buf[queue->pwrite_off], msg, msize);
2548  queue->pwrite_off += msize;
2550  "%lu bytes of plaintext to send\n", queue->pwrite_off);
2551  GNUNET_assert (NULL != queue->sock);
2552  if (NULL == queue->write_task)
2553  queue->write_task =
2555  queue->sock,
2556  &queue_write,
2557  queue);
2558 }
2559 
2560 
2569 static void
2570 mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
2571 {
2572  struct Queue *queue = impl_state;
2573 
2574  if (mq == queue->mq)
2575  {
2576  queue->mq = NULL;
2577  queue_finish (queue);
2578  }
2579 }
2580 
2581 
2588 static void
2589 mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
2590 {
2591  struct Queue *queue = impl_state;
2592 
2593  GNUNET_assert (0 != queue->pwrite_off);
2594  queue->pwrite_off = 0;
2595 }
2596 
2597 
2607 static void
2608 mq_error (void *cls, enum GNUNET_MQ_Error error)
2609 {
2610  struct Queue *queue = cls;
2611 
2613  "MQ error in queue to %s: %d\n",
2614  GNUNET_i2s (&queue->target),
2615  (int) error);
2616  queue_finish (queue);
2617 }
2618 
2619 
2627 static void
2629 {
2630  queue->nt =
2631  GNUNET_NT_scanner_get_type (is, queue->address, queue->address_len);
2633  queue_map,
2634  &queue->target,
2635  queue,
2638  "# queues active",
2640  GNUNET_NO);
2641  queue->timeout =
2644  &mq_destroy,
2645  &mq_cancel,
2646  queue,
2647  NULL,
2648  &mq_error,
2649  queue);
2650 }
2651 
2652 
2663 static void
2665  const struct GNUNET_CRYPTO_EcdhePublicKey *epub)
2666 {
2667  struct TcpHandshakeSignature ths;
2668  struct TCPConfirmation tc;
2669 
2670  memcpy (queue->cwrite_buf, epub, sizeof(*epub));
2671  queue->cwrite_off = sizeof(*epub);
2672  /* compute 'tc' and append in encrypted format to cwrite_buf */
2673  tc.sender = my_identity;
2674  tc.monotonic_time =
2677  &tc.challenge,
2678  sizeof(tc.challenge));
2679  ths.purpose.purpose = htonl (
2681  ths.purpose.size = htonl (sizeof(ths));
2682  ths.sender = my_identity;
2683  ths.receiver = queue->target;
2684  ths.ephemeral = *epub;
2685  ths.monotonic_time = tc.monotonic_time;
2686  ths.challenge = tc.challenge;
2688  &ths,
2689  &tc.sender_sig);
2690  GNUNET_assert (0 ==
2691  gcry_cipher_encrypt (queue->out_cipher,
2692  &queue->cwrite_buf[queue->cwrite_off],
2693  sizeof(tc),
2694  &tc,
2695  sizeof(tc)));
2696  queue->challenge = tc.challenge;
2697  queue->cwrite_off += sizeof(tc);
2698 
2700  "handshake written\n");
2701 }
2702 
2703 
2711 static void
2713 {
2714  struct GNUNET_CRYPTO_EcdhePublicKey epub;
2715 
2716  GNUNET_CRYPTO_ecdhe_key_create (&queue->ephemeral);
2717  GNUNET_CRYPTO_ecdhe_key_get_public (&queue->ephemeral, &epub);
2719  transmit_kx (queue, &epub);
2720 }
2721 
2722 
2728 static void
2729 handshake_monotime_store_cb (void *cls, int success)
2730 {
2731  struct Queue *queue = cls;
2732  if (GNUNET_OK != success)
2733  {
2735  "Failed to store handshake monotonic time in PEERSTORE!\n");
2736  }
2737  queue->handshake_monotime_sc = NULL;
2738 }
2739 
2740 
2748 static void
2750  const struct GNUNET_PEERSTORE_Record *record,
2751  const char *emsg)
2752 {
2753  struct Queue *queue = cls;
2754  struct GNUNET_TIME_AbsoluteNBO *mtbe;
2755  struct GNUNET_TIME_Absolute mt;
2756  const struct GNUNET_PeerIdentity *pid;
2757  struct GNUNET_TIME_AbsoluteNBO *handshake_monotonic_time;
2758 
2759  (void) emsg;
2760 
2761  handshake_monotonic_time = &queue->handshake_monotonic_time;
2762  pid = &queue->target;
2764  "tcp handshake with us %s\n",
2765  GNUNET_i2s (&my_identity));
2766  if (NULL == record)
2767  {
2768  queue->handshake_monotime_get = NULL;
2769  return;
2770  }
2772  "tcp handshake from peer %s\n",
2773  GNUNET_i2s (pid));
2774  if (sizeof(*mtbe) != record->value_size)
2775  {
2776  GNUNET_break (0);
2777  return;
2778  }
2779  mtbe = record->value;
2780  mt = GNUNET_TIME_absolute_ntoh (*mtbe);
2782  queue->handshake_monotonic_time).abs_value_us)
2783  {
2785  "Queue from %s dropped, handshake monotime in the past\n",
2786  GNUNET_i2s (&queue->target));
2787  GNUNET_break (0);
2788  queue_finish (queue);
2789  return;
2790  }
2791  queue->handshake_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
2792  "transport_tcp_communicator",
2793  pid,
2795  handshake_monotonic_time,
2796  sizeof(*
2797  handshake_monotonic_time),
2800  &
2802  queue);
2803 }
2804 
2805 
2817 static int
2819  struct TCPConfirmation *tc,
2820  char *ibuf)
2821 {
2822  struct TcpHandshakeSignature ths;
2824 
2825  GNUNET_assert (
2826  0 ==
2827  gcry_cipher_decrypt (queue->in_cipher,
2828  tc,
2829  sizeof(*tc),
2830  &ibuf[sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)],
2831  sizeof(*tc)));
2832  ths.purpose.purpose = htonl (
2834  ths.purpose.size = htonl (sizeof(ths));
2835  ths.sender = tc->sender;
2836  ths.receiver = my_identity;
2837  memcpy (&ths.ephemeral, ibuf, sizeof(struct GNUNET_CRYPTO_EcdhePublicKey));
2838  ths.monotonic_time = tc->monotonic_time;
2839  ths.challenge = tc->challenge;
2842  &ths,
2843  &tc->sender_sig,
2844  &tc->sender.public_key);
2845  if (GNUNET_YES == ret)
2846  queue->handshake_monotime_get =
2848  "transport_tcp_communicator",
2849  &queue->target,
2852  queue);
2853  return ret;
2854 }
2855 
2856 
2864 static void
2865 queue_read_kx (void *cls)
2866 {
2867  struct Queue *queue = cls;
2868  ssize_t rcvd;
2869  struct GNUNET_TIME_Relative left;
2870  struct TCPConfirmation tc;
2871 
2872  queue->read_task = NULL;
2873  left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
2874  if (0 == left.rel_value_us)
2875  {
2876  queue_destroy (queue);
2877  return;
2878  }
2879  rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2880  &queue->cread_buf[queue->cread_off],
2881  BUF_SIZE - queue->cread_off);
2883  "Received %lu bytes for KX\n",
2884  rcvd);
2885  if (-1 == rcvd)
2886  {
2887  if ((EAGAIN != errno) && (EINTR != errno))
2888  {
2890  queue_destroy (queue);
2891  return;
2892  }
2893  queue->read_task =
2895  return;
2896  }
2897  queue->cread_off += rcvd;
2898  if (queue->cread_off < INITIAL_KX_SIZE)
2899  {
2900  /* read more */
2901  queue->read_task =
2903  return;
2904  }
2905  /* we got all the data, let's find out who we are talking to! */
2907  queue->cread_buf,
2908  queue);
2909  if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, queue->cread_buf))
2910  {
2912  "Invalid TCP KX received from %s\n",
2913  GNUNET_a2s (queue->address, queue->address_len));
2914  queue_destroy (queue);
2915  return;
2916  }
2917  if (0 !=
2918  memcmp (&tc.sender, &queue->target, sizeof(struct GNUNET_PeerIdentity)))
2919  {
2921  "Invalid sender in TCP KX received from %s\n",
2922  GNUNET_a2s (queue->address, queue->address_len));
2923  queue_destroy (queue);
2924  return;
2925  }
2926  send_challenge (tc.challenge, queue);
2927  queue->write_task =
2929  queue->sock,
2930  &queue_write,
2931  queue);
2932 
2933  /* update queue timeout */
2935  /* prepare to continue with regular read task immediately */
2936  memmove (queue->cread_buf,
2937  &queue->cread_buf[INITIAL_KX_SIZE],
2938  queue->cread_off - (INITIAL_KX_SIZE));
2940  "cread_off is %lu bytes before adjusting\n",
2941  queue->cread_off);
2942  queue->cread_off -= INITIAL_KX_SIZE;
2944  "cread_off set to %lu bytes\n",
2945  queue->cread_off);
2947 }
2948 
2949 
2956 static void
2957 proto_read_kx (void *cls)
2958 {
2959  struct ProtoQueue *pq = cls;
2960  ssize_t rcvd;
2961  struct GNUNET_TIME_Relative left;
2962  struct Queue *queue;
2963  struct TCPConfirmation tc;
2965 
2966  pq->read_task = NULL;
2968  if (0 == left.rel_value_us)
2969  {
2970  free_proto_queue (pq);
2971  return;
2972  }
2973  rcvd = GNUNET_NETWORK_socket_recv (pq->sock,
2974  &pq->ibuf[pq->ibuf_off],
2975  sizeof(pq->ibuf) - pq->ibuf_off);
2977  "Proto received %lu bytes for KX\n", rcvd);
2978  if (-1 == rcvd)
2979  {
2980  if ((EAGAIN != errno) && (EINTR != errno))
2981  {
2983  free_proto_queue (pq);
2984  return;
2985  }
2986  /* try again */
2987  pq->read_task =
2989  return;
2990  }
2991  pq->ibuf_off += rcvd;
2992  if (sizeof (struct TCPNATProbeMessage) == pq->ibuf_off)
2993  {
2994  struct TCPNATProbeMessage *pm = (struct TCPNATProbeMessage *) pq->ibuf;
2995 
2996  check_and_remove_pending_reversal (pq->address, pq->address->sa_family,
2997  &pm->clientIdentity);
2998 
2999  queue = GNUNET_new (struct Queue);
3000  queue->target = pm->clientIdentity;
3003  }
3004  else if (pq->ibuf_off > sizeof(pq->ibuf))
3005  {
3006  /* read more */
3007  pq->read_task =
3009  return;
3010  }
3011  else
3012  {
3013  /* we got all the data, let's find out who we are talking to! */
3014  queue = GNUNET_new (struct Queue);
3015  setup_in_cipher ((const struct GNUNET_CRYPTO_EcdhePublicKey *) pq->ibuf,
3016  queue);
3017  if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, pq->ibuf))
3018  {
3020  "Invalid TCP KX received from %s\n",
3021  GNUNET_a2s (pq->address, pq->address_len));
3022  gcry_cipher_close (queue->in_cipher);
3023  GNUNET_free (queue);
3024  free_proto_queue (pq);
3025  return;
3026  }
3027  queue->target = tc.sender;
3029  read_task = &queue_read;
3030  }
3031  queue->address = pq->address; /* steals reference */
3032  queue->address_len = pq->address_len;
3033  queue->listen_sock = pq->listen_sock;
3034  queue->sock = pq->sock;
3035 
3037  "created queue with target %s\n",
3038  GNUNET_i2s (&queue->target));
3039 
3041  "start kx proto\n");
3042 
3044  boot_queue (queue);
3045  queue->read_task =
3047  queue->sock,
3048  read_task,
3049  queue);
3050  queue->write_task =
3052  queue->sock,
3053  &queue_write,
3054  queue);
3055  // TODO To early! Move it somewhere else.
3056  // send_challenge (tc.challenge, queue);
3057  queue->challenge_received = tc.challenge;
3058 
3060  GNUNET_free (pq);
3061 }
3062 
3063 static struct ProtoQueue *
3065  struct sockaddr *in,
3066  socklen_t addrlen)
3067 {
3068  struct ProtoQueue *pq = GNUNET_new (struct ProtoQueue);
3069 
3070  if (NULL == sock)
3071  {
3072  // sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
3073  sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, 0);
3074  if (NULL == sock)
3075  {
3077  "socket(%d) failed: %s",
3078  in->sa_family,
3079  strerror (errno));
3080  GNUNET_free (in);
3081  GNUNET_free (pq);
3082  return NULL;
3083  }
3084  if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, addrlen)) &&
3085  (errno != EINPROGRESS))
3086  {
3088  "connect to `%s' failed: %s",
3089  GNUNET_a2s (in, addrlen),
3090  strerror (errno));
3092  GNUNET_free (in);
3093  return NULL;
3094  }
3095  }
3096  pq->address_len = addrlen;
3097  pq->address = in;
3099  pq->sock = sock;
3101  pq->sock,
3102  &proto_read_kx,
3103  pq);
3105 
3106  return pq;
3107 }
3108 
3109 
3117 static void
3118 listen_cb (void *cls)
3119 {
3120  struct sockaddr_storage in;
3121  socklen_t addrlen;
3122  struct GNUNET_NETWORK_Handle *sock;
3123  struct ProtoQueue *pq;
3124  struct ListenTask *lt;
3125  struct sockaddr *in_addr;
3126 
3128  "listen_cb\n");
3129 
3130  lt = cls;
3131 
3132  lt->listen_task = NULL;
3133  GNUNET_assert (NULL != lt->listen_sock);
3134  addrlen = sizeof(in);
3135  memset (&in, 0, sizeof(in));
3137  (struct sockaddr*) &in,
3138  &addrlen);
3139  if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno)))
3140  return; /* system limit reached, wait until connection goes down */
3142  lt->listen_sock,
3143  &listen_cb,
3144  lt);
3145  if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno)))
3146  return;
3147  if (NULL == sock)
3148  {
3150  return;
3151  }
3152  in_addr = GNUNET_memdup (&in, addrlen);
3153  create_proto_queue (sock, in_addr, addrlen);
3154 }
3155 
3156 
3157 static void
3159  const struct sockaddr *addr,
3160  socklen_t addrlen)
3161 {
3162  (void) cls;
3163  struct TCPNATProbeMessage pm;
3164  struct ProtoQueue *pq;
3165  struct sockaddr *in_addr;
3166 
3168  "addr->sa_family %d\n",
3169  addr->sa_family);
3171  "Try to connect back\n");
3172  in_addr = GNUNET_memdup (addr, addrlen);
3174  "in_addr->sa_family %d\n",
3175  in_addr->sa_family);
3176  pq = create_proto_queue (NULL, in_addr, addrlen);
3177  if (NULL != pq)
3178  {
3179  pm.header.size = htons (sizeof(struct TCPNATProbeMessage));
3180  pm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
3181  pm.clientIdentity = my_identity;
3182  memcpy (pq->write_buf, &pm, sizeof(struct TCPNATProbeMessage));
3183  pq->write_off = sizeof(struct TCPNATProbeMessage);
3185  pq->sock,
3187  pq);
3188  }
3189  else
3190  {
3192  "Couldn't create ProtoQueue for sending TCPNATProbeMessage\n");
3193  }
3194 }
3195 
3196 
3197 static void
3199 {
3200  struct sockaddr *in = cls;
3201  struct PendingReversal *pending_reversal;
3202  struct GNUNET_HashCode key;
3203 
3204  GNUNET_CRYPTO_hash (in,
3205  sizeof(struct sockaddr),
3206  &key);
3208  &key);
3209 
3210  GNUNET_assert (NULL != pending_reversal);
3211 
3213  &key,
3214  pending_reversal))
3216  "No pending reversal found for address %s\n",
3217  GNUNET_a2s (in, sizeof (struct sockaddr)));
3218  GNUNET_free (pending_reversal->in);
3219  GNUNET_free (pending_reversal);
3220 }
3221 
3222 
3241 static int
3242 mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
3243 {
3244  struct sockaddr *in;
3245  socklen_t in_len = 0;
3246  const char *path;
3247  struct sockaddr_in *v4;
3248  struct sockaddr_in6 *v6;
3249  unsigned int is_natd = GNUNET_NO;
3250  struct GNUNET_HashCode key;
3251 
3253  "Connecting to %s at %s\n",
3254  GNUNET_i2s (peer),
3255  address);
3256  if (0 != strncmp (address,
3258  strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
3259  {
3260  GNUNET_break_op (0);
3261  return GNUNET_SYSERR;
3262  }
3263  path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
3264  in = tcp_address_to_sockaddr (path, &in_len);
3265 
3266  if (NULL == in)
3267  {
3269  "Failed to setup TCP socket address\n");
3270  return GNUNET_SYSERR;
3271  }
3272 
3274  "in %s\n",
3275  GNUNET_a2s (in, in_len));
3276 
3277  switch (in->sa_family)
3278  {
3279  case AF_INET:
3280  v4 = (struct sockaddr_in *) in;
3281  if (0 == v4->sin_port)
3282  {
3283  is_natd = GNUNET_YES;
3284  GNUNET_CRYPTO_hash (in,
3285  sizeof(struct sockaddr),
3286  &key);
3289  &key))
3290  {
3292  "There is already a request reversal for `%s'at `%s'\n",
3293  GNUNET_i2s (peer),
3294  address);
3295  GNUNET_free (in);
3296  return GNUNET_SYSERR;
3297  }
3298  }
3299  break;
3300 
3301  case AF_INET6:
3302  v6 = (struct sockaddr_in6 *) in;
3303  if (0 == v6->sin6_port)
3304  {
3306  "Request reversal for `%s' at `%s' not possible for an IPv6 address\n",
3307  GNUNET_i2s (peer),
3308  address);
3309  GNUNET_free (in);
3310  return GNUNET_SYSERR;
3311  }
3312  break;
3313 
3314  default:
3315  GNUNET_assert (0);
3316  }
3317 
3318  if (GNUNET_YES == is_natd)
3319  {
3320  struct sockaddr_in local_sa;
3321  struct PendingReversal *pending_reversal;
3322 
3323  memset (&local_sa, 0, sizeof(local_sa));
3324  local_sa.sin_family = AF_INET;
3325  local_sa.sin_port = htons (bind_port);
3326  /* We leave sin_address at 0, let the kernel figure it out,
3327  even if our bind() is more specific. (May want to reconsider
3328  later.) */
3329  if (GNUNET_OK != GNUNET_NAT_request_reversal (nat, &local_sa, v4))
3330  {
3332  "request reversal for `%s' at `%s' failed\n",
3333  GNUNET_i2s (peer),
3334  address);
3335  GNUNET_free (in);
3336  return GNUNET_SYSERR;
3337  }
3338  pending_reversal = GNUNET_new (struct PendingReversal);
3339  pending_reversal->in = in;
3342  &key,
3343  pending_reversal,
3345  pending_reversal->target = *peer;
3347  &
3349  in);
3351  "Created NAT WAIT connection to `%s' at `%s'\n",
3352  GNUNET_i2s (peer),
3353  GNUNET_a2s (in, sizeof (struct sockaddr)));
3354  }
3355  else
3356  {
3357  struct GNUNET_NETWORK_Handle *sock;
3358  struct Queue *queue;
3359 
3360  sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM,
3361  IPPROTO_TCP);
3362  if (NULL == sock)
3363  {
3365  "socket(%d) failed: %s",
3366  in->sa_family,
3367  strerror (errno));
3368  GNUNET_free (in);
3369  return GNUNET_SYSERR;
3370  }
3371  if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, in_len)) &&
3372  (errno != EINPROGRESS))
3373  {
3375  "connect to `%s' failed: %s",
3376  address,
3377  strerror (errno));
3379  GNUNET_free (in);
3380  return GNUNET_SYSERR;
3381  }
3382 
3383  queue = GNUNET_new (struct Queue);
3384  queue->target = *peer;
3385  queue->address = in;
3386  queue->address_len = in_len;
3387  queue->sock = sock;
3389  boot_queue (queue);
3391  "booted queue with target %s\n",
3392  GNUNET_i2s (&queue->target));
3393  // queue->mq_awaits_continue = GNUNET_YES;
3394  queue->read_task =
3396  queue->sock,
3397  &queue_read_kx,
3398  queue);
3399 
3400 
3402  "start kx mq_init\n");
3403 
3405  queue->write_task =
3407  queue->sock,
3408  &queue_write,
3409  queue);
3410  }
3411 
3412  return GNUNET_OK;
3413 }
3414 
3415 
3424 static int
3425 get_lt_delete_it (void *cls,
3426  const struct GNUNET_HashCode *key,
3427  void *value)
3428 {
3429  struct ListenTask *lt = value;
3430 
3431  (void) cls;
3432  (void) key;
3433  if (NULL != lt->listen_task)
3434  {
3436  lt->listen_task = NULL;
3437  }
3438  if (NULL != lt->listen_sock)
3439  {
3441  lt->listen_sock = NULL;
3442  }
3443  GNUNET_free (lt);
3444  return GNUNET_OK;
3445 }
3446 
3447 
3456 static int
3458  const struct GNUNET_PeerIdentity *target,
3459  void *value)
3460 {
3461  struct Queue *queue = value;
3462 
3463  (void) cls;
3464  (void) target;
3465  queue_destroy (queue);
3466  return GNUNET_OK;
3467 }
3468 
3469 
3475 static void
3476 do_shutdown (void *cls)
3477 {
3479  "Shutdown %s!\n",
3480  shutdown_running ? "running" : "not running");
3481 
3483  return;
3484  else
3486 
3487  while (NULL != proto_head)
3489  if (NULL != nat)
3490  {
3492  nat = NULL;
3493  }
3501  if (NULL != ch)
3502  {
3505  ch = NULL;
3506  }
3507  if (NULL != stats)
3508  {
3510  stats = NULL;
3511  }
3512  if (NULL != my_private_key)
3513  {
3515  my_private_key = NULL;
3516  }
3517  if (NULL != is)
3518  {
3520  is = NULL;
3521  }
3522  if (NULL != peerstore)
3523  {
3525  peerstore = NULL;
3526  }
3527  if (NULL != resolve_request_handle)
3528  {
3530  resolve_request_handle = NULL;
3531  }
3533  "Shutdown done!\n");
3534 }
3535 
3536 
3548 static void
3549 enc_notify_cb (void *cls,
3550  const struct GNUNET_PeerIdentity *sender,
3551  const struct GNUNET_MessageHeader *msg)
3552 {
3553  (void) cls;
3554  (void) sender;
3555  (void) msg;
3556  GNUNET_break_op (0);
3557 }
3558 
3559 
3573 static void
3574 nat_address_cb (void *cls,
3575  void **app_ctx,
3576  int add_remove,
3578  const struct sockaddr *addr,
3579  socklen_t addrlen)
3580 {
3581  char *my_addr;
3583 
3585  "nat address cb %s %s\n",
3586  add_remove ? "add" : "remove",
3587  GNUNET_a2s (addr, addrlen));
3588 
3589  if (GNUNET_YES == add_remove)
3590  {
3591  enum GNUNET_NetworkType nt;
3592 
3593  GNUNET_asprintf (&my_addr,
3594  "%s-%s",
3596  GNUNET_a2s (addr, addrlen));
3597  nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
3598  ai =
3600  my_addr,
3601  nt,
3603  GNUNET_free (my_addr);
3604  *app_ctx = ai;
3605  }
3606  else
3607  {
3608  ai = *app_ctx;
3610  *app_ctx = NULL;
3611  }
3612 }
3613 
3614 
3618 static void
3619 add_addr (struct sockaddr *in, socklen_t in_len)
3620 {
3621 
3622  struct Addresses *saddrs;
3623 
3625  "add address %s\n",
3626  GNUNET_a2s (in, in_len));
3627 
3628  saddrs = GNUNET_new (struct Addresses);
3629  saddrs->addr = in;
3630  saddrs->addr_len = in_len;
3632 
3634  "after add address %s\n",
3635  GNUNET_a2s (in, in_len));
3636 
3638  "add address %s\n",
3639  GNUNET_a2s (saddrs->addr, saddrs->addr_len));
3640 
3641  addrs_lens++;
3642 }
3643 
3644 
3652 static int
3653 init_socket (struct sockaddr *addr,
3654  socklen_t in_len)
3655 {
3656  struct sockaddr_storage in_sto;
3657  socklen_t sto_len;
3658  struct GNUNET_NETWORK_Handle *listen_sock;
3659  struct ListenTask *lt;
3660  int sockfd;
3661  struct GNUNET_HashCode h_sock;
3662 
3663  if (NULL == addr)
3664  {
3666  "Address is NULL.\n");
3667  return GNUNET_SYSERR;
3668  }
3669 
3671  "address %s\n",
3672  GNUNET_a2s (addr, in_len));
3673 
3674  listen_sock =
3675  GNUNET_NETWORK_socket_create (addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
3676  if (NULL == listen_sock)
3677  {
3679  return GNUNET_SYSERR;
3680  }
3681 
3682  if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, addr, in_len))
3683  {
3685  GNUNET_NETWORK_socket_close (listen_sock);
3686  listen_sock = NULL;
3687  return GNUNET_SYSERR;
3688  }
3689 
3690  if (GNUNET_OK !=
3691  GNUNET_NETWORK_socket_listen (listen_sock,
3692  5))
3693  {
3695  "listen");
3696  GNUNET_NETWORK_socket_close (listen_sock);
3697  listen_sock = NULL;
3698  return GNUNET_SYSERR;
3699  }
3700 
3701  /* We might have bound to port 0, allowing the OS to figure it out;
3702  thus, get the real IN-address from the socket */
3703  sto_len = sizeof(in_sto);
3704 
3705  if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock),
3706  (struct sockaddr *) &in_sto,
3707  &sto_len))
3708  {
3709  memcpy (&in_sto, addr, in_len);
3710  sto_len = in_len;
3711  }
3712 
3713  // addr = (struct sockaddr *) &in_sto;
3714  in_len = sto_len;
3716  "Bound to `%s'\n",
3717  GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
3718  stats = GNUNET_STATISTICS_create ("C-TCP", cfg);
3719 
3720  if (NULL == is)
3722 
3723  if (NULL == my_private_key)
3725  if (NULL == my_private_key)
3726  {
3727  GNUNET_log (
3729  _ (
3730  "Transport service is lacking key configuration settings. Exiting.\n"));
3731  if (NULL != resolve_request_handle)
3734  return GNUNET_SYSERR;
3735  }
3737  /* start listening */
3738 
3739  lt = GNUNET_new (struct ListenTask);
3740  lt->listen_sock = listen_sock;
3741 
3743  listen_sock,
3744  &listen_cb,
3745  lt);
3746 
3748  "creating hash\n");
3749  sockfd = GNUNET_NETWORK_get_fd (lt->listen_sock);
3750  GNUNET_CRYPTO_hash (&sockfd,
3751  sizeof(int),
3752  &h_sock);
3753 
3755  "creating map\n");
3756  if (NULL == lt_map)
3758 
3760  "creating map entry\n");
3763  &h_sock,
3764  lt,
3766 
3768  "map entry created\n");
3769 
3770  if (NULL == queue_map)
3772 
3773  if (NULL == ch)
3778  &mq_init,
3779  NULL,
3780  &enc_notify_cb,
3781  NULL);
3782 
3783  if (NULL == ch)
3784  {
3785  GNUNET_break (0);
3786  if (NULL != resolve_request_handle)
3789  return GNUNET_SYSERR;
3790  }
3791 
3792  add_addr (addr, in_len);
3793  return GNUNET_OK;
3794 
3795 }
3796 
3797 
3801 static void
3803 {
3804  struct sockaddr **saddrs;
3805  socklen_t *saddr_lens;
3806  int i;
3807  size_t len;
3808 
3810  "starting nat register!\n");
3811  len = 0;
3812  i = 0;
3813  saddrs = GNUNET_malloc ((addrs_lens) * sizeof(struct sockaddr *));
3814  saddr_lens = GNUNET_malloc ((addrs_lens) * sizeof(socklen_t));
3815  for (struct Addresses *pos = addrs_head; NULL != pos; pos = pos->next)
3816  {
3818  "registering address %s\n",
3820 
3821  saddr_lens[i] = addrs_head->addr_len;
3822  len += saddr_lens[i];
3823  saddrs[i] = GNUNET_memdup (addrs_head->addr, saddr_lens[i]);
3824  i++;
3825  }
3826 
3828  "registering addresses %lu %lu %lu %lu\n",
3829  (addrs_lens) * sizeof(struct sockaddr *),
3830  (addrs_lens) * sizeof(socklen_t),
3831  len,
3832  sizeof(COMMUNICATOR_CONFIG_SECTION));
3835  IPPROTO_TCP,
3836  addrs_lens,
3837  (const struct sockaddr **) saddrs,
3838  saddr_lens,
3839  &nat_address_cb,
3841  NULL /* closure */);
3842  for (i = addrs_lens - 1; i >= 0; i--)
3843  GNUNET_free (saddrs[i]);
3844  GNUNET_free (saddrs);
3845  GNUNET_free (saddr_lens);
3846 
3847  if (NULL == nat)
3848  {
3849  GNUNET_break (0);
3850  if (NULL != resolve_request_handle)
3853  }
3854 }
3855 
3856 
3864 static void
3866  const struct sockaddr *addr,
3867  socklen_t in_len)
3868 {
3869  struct sockaddr_in *v4;
3870  struct sockaddr_in6 *v6;
3871  struct sockaddr *in;
3872 
3873  (void) cls;
3874  if (NULL != addr)
3875  {
3876  if (AF_INET == addr->sa_family)
3877  {
3878  v4 = (struct sockaddr_in *) addr;
3879  in = tcp_address_to_sockaddr_numeric_v4 (&in_len, *v4, bind_port);// _global);
3880  }
3881  else if (AF_INET6 == addr->sa_family)
3882  {
3883  v6 = (struct sockaddr_in6 *) addr;
3884  in = tcp_address_to_sockaddr_numeric_v6 (&in_len, *v6, bind_port);// _global);
3885  }
3886  else
3887  {
3889  "Address family %u not suitable (not AF_INET %u nor AF_INET6 %u \n",
3890  addr->sa_family,
3891  AF_INET,
3892  AF_INET6);
3893  return;
3894  }
3895  init_socket (in, in_len);
3896  }
3897  else
3898  {
3900  "Address is NULL. This might be an error or the resolver finished resolving.\n");
3901  if (NULL == addrs_head)
3902  {
3904  "Resolver finished resolving, but we do not listen to an address!.\n");
3905  return;
3906  }
3907  nat_register ();
3908  }
3909 }
3910 
3911 
3920 static void
3921 run (void *cls,
3922  char *const *args,
3923  const char *cfgfile,
3924  const struct GNUNET_CONFIGURATION_Handle *c)
3925 {
3926  char *bindto;
3927  struct sockaddr *in;
3928  socklen_t in_len;
3929  struct sockaddr_in v4;
3930  struct sockaddr_in6 v6;
3931  char *start;
3932  unsigned int port;
3933  char dummy[2];
3934  char *rest = NULL;
3935  struct PortOnlyIpv4Ipv6 *po;
3936  socklen_t addr_len_ipv4;
3937  socklen_t addr_len_ipv6;
3938 
3939  (void) cls;
3940 
3942  memset (&v4,0,sizeof(struct sockaddr_in));
3943  memset (&v6,0,sizeof(struct sockaddr_in6));
3944  cfg = c;
3945  if (GNUNET_OK !=
3948  "BINDTO",
3949  &bindto))
3950  {
3953  "BINDTO");
3954  return;
3955  }
3956  if (GNUNET_OK !=
3959  "MAX_QUEUE_LENGTH",
3960  &max_queue_length))
3962  if (GNUNET_OK !=
3965  "REKEY_INTERVAL",
3966  &rekey_interval))
3968 
3970  if (NULL == peerstore)
3971  {
3972  GNUNET_free (bindto);
3973  GNUNET_break (0);
3975  return;
3976  }
3977 
3979 
3980  if (1 == sscanf (bindto, "%u%1s", &bind_port, dummy))
3981  {
3985  "address po %s\n",
3987  if (NULL != po->addr_ipv4)
3988  {
3990  }
3991  if (NULL != po->addr_ipv6)
3992  {
3995  }
3996  GNUNET_free (po);
3997  nat_register ();
3998  GNUNET_free (bindto);
3999  return;
4000  }
4001 
4002  start = extract_address (bindto);
4003  // FIXME: check for NULL == start...
4004  if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
4005  {
4006  bind_port = extract_port (bindto);
4007 
4008  in = tcp_address_to_sockaddr_numeric_v4 (&in_len, v4, bind_port);
4009  init_socket (in, in_len);
4010  nat_register ();
4011  GNUNET_free (start);
4012  GNUNET_free (bindto);
4013  return;
4014  }
4015 
4016  if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
4017  {
4018  bind_port = extract_port (bindto);
4019  in = tcp_address_to_sockaddr_numeric_v6 (&in_len, v6, bind_port);
4020  init_socket (in, in_len);
4021  nat_register ();
4022  GNUNET_free (start);
4023  GNUNET_free (bindto);
4024  return;
4025  }
4026 
4027  bind_port = extract_port (bindto);
4028  resolve_request_handle = GNUNET_RESOLVER_ip_get (strtok_r (bindto,
4029  ":",
4030  &rest),
4031  AF_UNSPEC,
4034  &port);
4035 
4036  GNUNET_free (bindto);
4037  GNUNET_free (start);
4038 }
4039 
4040 
4048 int
4049 main (int argc, char *const *argv)
4050 {
4051  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
4053  };
4054  int ret;
4055 
4057  "Starting tcp communicator\n");
4058  if (GNUNET_OK !=
4059  GNUNET_STRINGS_get_utf8_args (argc, argv,
4060  &argc, &argv))
4061  return 2;
4062 
4063  ret = (GNUNET_OK ==
4064  GNUNET_PROGRAM_run (argc,
4065  argv,
4066  "gnunet-communicator-tcp",
4067  _ ("GNUnet TCP communicator"),
4068  options,
4069  &run,
4070  NULL))
4071  ? 0
4072  : 1;
4073  GNUNET_free_nz ((void *) argv);
4074  return ret;
4075 }
4076 
4077 
4078 /* 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
#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.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
static int res
static void done()
static uint16_t port
Port number.
Definition: gnunet-bcd.c:147
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 struct GNUNET_CONTAINER_MultiPeerMap * queue_map
Queues (map from peer identity to struct Queue)
static void boot_queue(struct Queue *queue)
Add the given queue to our internal data structure.
unsigned int bind_port
The port the communicator should be assigned to.
struct ListenTask * lts_tail
Head of DLL with ListenTask.
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.
size_t unverified_size
Size of data received without KX challenge played back.
static struct GNUNET_PeerIdentity my_identity
Our public key.
static int get_queue_delete_it(void *cls, const struct GNUNET_PeerIdentity *target, void *value)
Iterator over all message queues to clean up.
#define COMMUNICATOR_ADDRESS_PREFIX
Address prefix used by the communicator.
static void mq_destroy(struct GNUNET_MQ_Handle *mq, void *impl_state)
Signature of functions implementing the destruction of a message queue.
#define ADDRESS_VALIDITY_PERIOD
How long do we believe our addresses to remain up (before the other peer should revalidate).
#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 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...
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.
static struct PortOnlyIpv4Ipv6 * tcp_address_to_sockaddr_port_only(const char *bindto, unsigned int *port)
Convert TCP bind specification to a struct PortOnlyIpv4Ipv6 *
#define BUF_SIZE
Size of our IO buffers for ciphertext data.
static void reschedule_queue_timeout(struct Queue *queue)
Increment queue timeout due to activity.
static void do_shutdown(void *cls)
Shutdown the UNIX communicator.
#define COMMUNICATOR_CONFIG_SECTION
Configuration section used by the communicator.
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 char * extract_address(const char *bindto)
This Method extracts the address part of the BINDTO string.
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 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.
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.
int addrs_lens
Number of addresses in the DLL for 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 run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Setup communicator and launch network interactions.
struct ListenTask * lts_head
Head of DLL with ListenTasks.
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 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 setup_out_cipher(struct Queue *queue)
Setup cipher for outgoing data stream based on target and our ephemeral private key.
static void proto_queue_write(void *cls)
We have been notified that our socket is ready to write.
static void setup_cipher(const struct GNUNET_HashCode *dh, const struct GNUNET_PeerIdentity *pid, gcry_cipher_hd_t *cipher, struct GNUNET_HashCode *hmac_key)
Setup cipher based on shared secret dh and decrypting peer pid.
static struct 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 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 struct ProtoQueue * proto_head
Protoqueues DLL head.
static void setup_in_cipher(const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral, struct Queue *queue)
Setup cipher of queue for decryption.
int shutdown_running
A flag indicating we are already doing a shutdown.
static void transmit_kx(struct Queue *queue, const struct GNUNET_CRYPTO_EcdhePublicKey *epub)
Generate and transmit our ephemeral key and the signature for the initial KX with the other peer.
static int decrypt_and_check_tc(struct Queue *queue, struct TCPConfirmation *tc, char *ibuf)
We have received the first bytes from the other side on a queue.
#define DEFAULT_REKEY_INTERVAL
How often do we rekey based on time (at least)
int main(int argc, char *const *argv)
The main function for the UNIX communicator.
static struct GNUNET_CRYPTO_EddsaPrivateKey * my_private_key
Our private key.
static void queue_write(void *cls)
We have been notified that our socket is ready to write.
static void add_addr(struct sockaddr *in, socklen_t in_len)
This method adds addresses to the DLL, that are later register at the NAT service.
static void handshake_monotime_cb(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE w...
struct Addresses * addrs_head
Head of DLL with addresses we like to register at NAT servcie.
struct Addresses * addrs_tail
Head of DLL with addresses we like to register at NAT servcie.
#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 void mq_send(struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *msg, void *impl_state)
Signature of functions implementing the sending functionality of a message queue.
static void start_initial_kx_out(struct Queue *queue)
Initialize our key material for outgoing transmissions and inform the other peer about it.
static void free_proto_queue(struct ProtoQueue *pq)
Closes socket and frees memory associated with pq.
static struct GNUNET_CONTAINER_MultiHashMap * lt_map
ListenTasks (map from socket to struct ListenTask)
static struct ProtoQueue * create_proto_queue(struct GNUNET_NETWORK_Handle *sock, struct sockaddr *in, socklen_t addrlen)
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 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_TRANSPORT_AddressIdentifier * ai
Handle to the operation that publishes our address.
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
static char * address
GNS address for this phone.
struct GNUNET_HashCode key
The key used in the DHT.
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
struct GNUNET_SCHEDULER_Task * read_task
static char * value
Value of the record to add/remove.
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
static struct GNUNET_MQ_Envelope * ac
Handle to current GNUNET_PEERINFO_add_peer() operation.
static char buf[2048]
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.
Core service; the main API for encrypted P2P communications.
API to the peerstore service.
Constants for network protocols.
Functions related to doing DNS lookups.
API to create, modify and access statistics.
API of the transport service towards the communicator processes.
void GNUNET_TRANSPORT_communicator_address_remove(struct GNUNET_TRANSPORT_AddressIdentifier *ai)
Notify transport service about an address that this communicator no longer provides for this peer.
int GNUNET_TRANSPORT_communicator_receive(struct GNUNET_TRANSPORT_CommunicatorHandle *handle, const struct GNUNET_PeerIdentity *sender, const struct GNUNET_MessageHeader *msg, struct GNUNET_TIME_Relative expected_addr_validity, GNUNET_TRANSPORT_MessageCompletedCallback cb, void *cb_cls)
Notify transport service that the communicator has received a message.
void GNUNET_TRANSPORT_communicator_mq_del(struct GNUNET_TRANSPORT_QueueHandle *qh)
Notify transport service that an MQ became unavailable due to a disconnect or timeout.
#define GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED
Queue length.
struct GNUNET_TRANSPORT_QueueHandle * GNUNET_TRANSPORT_communicator_mq_add(struct GNUNET_TRANSPORT_CommunicatorHandle *ch, const struct GNUNET_PeerIdentity *peer, const char *address, uint32_t mtu, uint64_t q_len, uint32_t priority, enum GNUNET_NetworkType nt, enum GNUNET_TRANSPORT_ConnectionStatus cs, struct GNUNET_MQ_Handle *mq)
Notify transport service that a MQ became available due to an "inbound" connection or because the com...
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_disconnect(struct GNUNET_TRANSPORT_CommunicatorHandle *ch)
Disconnect from the transport service.
GNUNET_TRANSPORT_ConnectionStatus
Possible states of a connection.
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.
struct GNUNET_TRANSPORT_CommunicatorHandle * GNUNET_TRANSPORT_communicator_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *config_section_name, const char *addr_prefix, enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc, GNUNET_TRANSPORT_CommunicatorMqInit mq_init, void *mq_init_cls, GNUNET_TRANSPORT_CommunicatorNotify notify_cb, void *notify_cb_cls)
Connect to the transport service.
@ GNUNET_TRANSPORT_CC_RELIABLE
Transmission is reliabile (with ACKs), e.g.
@ GNUNET_TRANSPORT_CS_INBOUND
this is an inbound connection (communicator initiated)
@ GNUNET_TRANSPORT_CS_OUTBOUND
this is an outbound connection (transport initiated)
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_time(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, struct GNUNET_TIME_Relative *time)
Get a configuration value that should be a relative time.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
#define GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
After how long do we consider a connection to a peer dead if we don't receive messages from the peer?
void GNUNET_CRYPTO_ecdhe_key_create(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
Create a new private key.
Definition: crypto_ecc.c:435
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Generate a random unsigned 64-bit value.
struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_CRYPTO_eddsa_key_create_from_configuration(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a new private key by reading our peer's key from the file specified in the configuration.
void GNUNET_CRYPTO_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:197
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_ecdh(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a ECDH public key and a private EdDSA key.
Definition: crypto_ecc.c:739
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_ecdh_eddsa(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a EdDSA public key and a private ECDH key.
Definition: crypto_ecc.c:779
#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.
void GNUNET_CRYPTO_ecdhe_key_get_public(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, struct GNUNET_CRYPTO_EcdhePublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:213
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
@ GNUNET_CRYPTO_QUALITY_NONCE
Randomness for IVs etc.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_kdf(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
Derive key.
Definition: crypto_kdf.c:70
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_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
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.
int GNUNET_CONTAINER_multipeermap_iterate(struct GNUNET_CONTAINER_MultiPeerMap *map, GNUNET_CONTAINER_PeerMapIterator it, void *it_cls)
Iterate over all entries in 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.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
struct GNUNET_CONTAINER_MultiPeerMap * GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash 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.
unsigned int GNUNET_CONTAINER_multipeermap_size(const struct GNUNET_CONTAINER_MultiPeerMap *map)
Get the number of key-value pairs in the map.
int GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multipeermap_remove(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, const void *value)
Remove the given key-value pair from the map.
@ GNUNET_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.
#define GNUNET_log(kind,...)
#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.
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_MIN(a, b)
void GNUNET_CRYPTO_hmac_raw(const void *key, size_t key_len, const void *plaintext, size_t plaintext_len, struct GNUNET_HashCode *hmac)
Calculate HMAC of a message (RFC 2104) TODO: Shouldn't this be the standard hmac function and the abo...
Definition: crypto_hash.c:299
@ 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_e2s(const struct GNUNET_CRYPTO_EcdhePublicKey *p)
Convert a public key value to a string (for printing debug messages).
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity 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_p2s(const struct GNUNET_CRYPTO_EddsaPublicKey *p)
Convert a public key value 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_a2s(const struct sockaddr *addr, socklen_t addrlen)
Convert a "struct sockaddr*" (IPv4 or IPv6 address) 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_free_nz(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:465
void GNUNET_MQ_impl_send_continue(struct GNUNET_MQ_Handle *mq)
Call the send implementation for the next queued message, if any.
Definition: mq.c:421
int GNUNET_NAT_request_reversal(struct GNUNET_NAT_Handle *nh, const struct sockaddr_in *local_sa, const struct sockaddr_in *remote_sa)
We learned about a peer (possibly behind NAT) so run the gnunet-nat-client to send dummy ICMP respons...
Definition: nat_api.c:646
void GNUNET_NAT_unregister(struct GNUNET_NAT_Handle *nh)
Stop port redirection and public IP address detection for the given handle.
Definition: nat_api.c:674
GNUNET_NAT_AddressClass
Some addresses contain sensitive information or are not suitable for global distribution.
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
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:508
int GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
Return file descriptor for this network handle.
Definition: network.c:1000
ssize_t GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length)
Read data from a connected socket (always non-blocking).
Definition: network.c:716
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_connect(const struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Connect a socket to some remote address.
Definition: network.c:601
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc, struct sockaddr *address, socklen_t *address_len)
Accept a new connection on a socket.
Definition: network.c:392
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:832
ssize_t GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc, const void *buffer, size_t length)
Send data (always non-blocking).
Definition: network.c:737
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:651
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
Definition: gnunet_nt_lib.h:39
void GNUNET_NT_scanner_done(struct GNUNET_NT_InterfaceScanner *is)
Terminate interface scanner.
Definition: nt.c:427
struct GNUNET_NT_InterfaceScanner * GNUNET_NT_scanner_init(void)
Initialize the address characterization client handle.
Definition: nt.c:406
enum GNUNET_NetworkType GNUNET_NT_scanner_get_type(struct GNUNET_NT_InterfaceScanner *is, const struct sockaddr *addr, socklen_t addrlen)
Returns where the address is located: loopback, LAN or WAN.
Definition: nt.c:308
struct GNUNET_PEERSTORE_Handle * GNUNET_PEERSTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the PEERSTORE service.
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.
void GNUNET_PEERSTORE_store_cancel(struct GNUNET_PEERSTORE_StoreContext *sc)
Cancel a store request.
void GNUNET_PEERSTORE_iterate_cancel(struct GNUNET_PEERSTORE_IterateContext *ic)
Cancel an iterate request Please do not call after the iterate request is done.
#define GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE
Key used to store sender's monotonic time from handshake message.
void GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h, int sync_first)
Disconnect from the PEERSTORE service.
#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.
struct GNUNET_PEERSTORE_IterateContext * GNUNET_PEERSTORE_iterate(struct GNUNET_PEERSTORE_Handle *h, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, GNUNET_PEERSTORE_Processor callback, void *callback_cls)
Iterate over records matching supplied key information.
@ GNUNET_PEERSTORE_STOREOPTION_REPLACE
Delete any previous values for the given key before storing the given value.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(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:400
#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.
void GNUNET_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh)
Cancel a request that is still pending with the resolver.
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_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:562
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:1299
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:1577
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:1506
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:1334
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:975
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:1272
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).
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1222
#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:405
#define GNUNET_TIME_UNIT_MINUTES
One minute.
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:616
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:737
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:860
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:569
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:638
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
#define max(x, y)
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition: scheduler.c:431
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.
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.
an ECC signature using EdDSA.
Definition of a command line option.
A 512-bit hashcode.
Handle to a message queue.
Definition: mq.c:87
Header for all communications.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
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:105
Handle to the PEERSTORE service.
Definition: peerstore_api.c:41
Context for a iterate request.
Single PEERSTORE record.
Context for a store request.
Definition: peerstore_api.c:97
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.
unsigned int backpressure
How may messages did we pass from this queue to CORE for which we have yet to receive an acknoweldgem...
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.
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?
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_CRYPTO_EcdhePrivateKey ephemeral
Our ephemeral key.
struct GNUNET_PEERSTORE_StoreContext * rekey_monotime_sc
Store Context for retrieving the monotonic time send with key for rekeying.
size_t cwrite_off
At which offset in the ciphertext write buffer should we append more ciphertext from reading next?
struct GNUNET_PEERSTORE_IterateContext * rekey_monotime_get
Iteration Context for retrieving the monotonic time send with key for rekeying.
char cread_buf[(2 *64 *1024+sizeof(struct TCPBox))]
Buffer for reading ciphertext from network into.
enum GNUNET_NetworkType nt
Which network type does this queue use?
struct GNUNET_CRYPTO_ChallengeNonceP challenge_received
Challenge value received.
struct GNUNET_SCHEDULER_Task * read_task
ID of read task for this connection.
uint64_t rekey_left_bytes
How many more bytes may we sent with the current out_cipher before we should rekey?
int mq_awaits_continue
Is MQ awaiting a GNUNET_MQ_impl_send_continue() call?
gcry_cipher_hd_t in_cipher
cipher for decryption of incoming data.
struct GNUNET_PEERSTORE_StoreContext * handshake_ack_monotime_sc
Store Context for retrieving the monotonic time send with the handshake ack.
size_t pwrite_off
At which offset in the plaintext output buffer should we append more plaintext for encryption next?
struct GNUNET_TIME_Absolute timeout
Timeout for this queue.
struct sockaddr * address
Address of the other peer.
size_t cread_off
At which offset in the ciphertext read buffer should we append more ciphertext for transmission next?
struct GNUNET_TIME_Absolute rekey_time
Until what time may we sent with the current out_cipher before we should rekey?
struct GNUNET_PeerIdentity target
To whom are we talking to.
char cwrite_buf[(2 *64 *1024+sizeof(struct TCPBox))]
buffer for writing ciphertext to network.
struct GNUNET_PEERSTORE_StoreContext * handshake_monotime_sc
Store Context for retrieving the monotonic time send with the handshake.
size_t pread_off
At which offset in the plaintext input buffer should we append more plaintext from decryption next?
enum GNUNET_TRANSPORT_ConnectionStatus cs
The connection status of this queue.
gcry_cipher_hd_t out_cipher
cipher for encryption of outgoing data.
char pwrite_buf[UINT16_MAX+1+sizeof(struct TCPBox)]
Plaintext buffer for messages to be encrypted.
struct GNUNET_HashCode in_hmac
Shared secret for HMAC verification on incoming data.
struct GNUNET_PEERSTORE_IterateContext * handshake_monotime_get
Iteration Context for retrieving the monotonic time send with the handshake.
struct GNUNET_NETWORK_Handle * listen_sock
Listen socket.
struct GNUNET_SCHEDULER_Task * write_task
ID of write task for this connection.
struct GNUNET_TIME_AbsoluteNBO handshake_ack_monotonic_time
Monotonic time value for handshake ack message.
struct GNUNET_TIME_AbsoluteNBO rekey_monotonic_time
Monotonic time value for rekey message.
struct GNUNET_TIME_AbsoluteNBO handshake_monotonic_time
Monotonic time value for handshake message.
TCP message box.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
Ack for the encrypted continuation of TCP initial handshake.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender's signature of type GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE_ACK.
struct GNUNET_CRYPTO_ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_CONFIRMATION_ACK.
struct GNUNET_PeerIdentity sender
Sender's identity.
Encrypted continuation of TCP initial handshake.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender's signature of type GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_HANDSHAKE.
struct GNUNET_PeerIdentity sender
Sender's identity.
struct GNUNET_CRYPTO_ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
Basically a WELCOME message, but with the purpose of giving the waiting peer a client handle to use.
struct GNUNET_PeerIdentity clientIdentity
Identity of the sender of the message.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
TCP rekey message box.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
struct GNUNET_CRYPTO_EcdhePublicKey ephemeral
New ephemeral key.
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender's signature of type GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_TCP_REKEY.
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_PeerIdentity sender
Identity of the inititor of the TCP connection (TCP client).
struct GNUNET_CRYPTO_EcdhePublicKey ephemeral
Ephemeral key used by the sender.
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_EcdhePublicKey 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...
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.