GNUnet  0.11.x
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 
47 #define ADDRESS_VALIDITY_PERIOD \
48  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
49 
58 #define DEFAULT_MAX_QUEUE_LENGTH 8
59 
64 #define BUF_SIZE (2 * 64 * 1024 + sizeof(struct TCPBox))
65 
69 #define DEFAULT_REKEY_INTERVAL GNUNET_TIME_UNIT_DAYS
70 
74 #define PROTO_QUEUE_TIMEOUT GNUNET_TIME_UNIT_MINUTES
75 
80 #define REKEY_MAX_BYTES (1024LLU * 1024 * 1024 * 4LLU)
81 
86 #define INITIAL_KX_SIZE \
87  (sizeof(struct GNUNET_CRYPTO_EcdhePublicKey) \
88  + sizeof(struct TCPConfirmation))
89 
93 #define INITIAL_CORE_KX_SIZE \
94  (sizeof(struct EphemeralKeyMessage) \
95  + sizeof(struct PingMessage) \
96  + sizeof(struct PongMessage))
97 
101 #define COMMUNICATOR_ADDRESS_PREFIX "tcp"
102 
106 #define COMMUNICATOR_CONFIG_SECTION "communicator-tcp"
107 
109 
110 
116 {
121 
126 
131 
136 
142 
147 };
148 
154 {
159 
164 
169 
175 
179  struct ChallengeNonceP challenge;
180 };
181 
186 {
191 
195  struct GNUNET_CRYPTO_EddsaSignature sender_sig;
196 
202 
206  struct ChallengeNonceP challenge;
207 
208 };
209 
214 {
215 
216 
220  struct GNUNET_MessageHeader header;
221 
226 
230  struct GNUNET_CRYPTO_EddsaSignature sender_sig;
231 
237 
241  struct ChallengeNonceP challenge;
242 
243 };
244 
248 struct TCPBox
249 {
256  struct GNUNET_MessageHeader header;
257 
266  struct GNUNET_ShortHashCode hmac;
267 
268  /* followed by as may bytes of payload as indicated in @e header,
269  excluding the TCPBox itself! */
270 };
271 
272 
277 struct TCPRekey
278 {
282  struct GNUNET_MessageHeader header;
283 
292  struct GNUNET_ShortHashCode hmac;
293 
298 
302  struct GNUNET_CRYPTO_EddsaSignature sender_sig;
303 
309 };
310 
316 {
321 
326 
331 
336 
341  struct GNUNET_TIME_AbsoluteNBO monotonic_time;
342 };
343 
349 struct TCPFinish
350 {
354  struct GNUNET_MessageHeader header;
355 
364  struct GNUNET_ShortHashCode hmac;
365 };
366 
367 
369 
374 {
379 
384 };
385 
389 struct Queue
390 {
394  struct GNUNET_PeerIdentity target;
395 
400 
405 
409  gcry_cipher_hd_t in_cipher;
410 
414  gcry_cipher_hd_t out_cipher;
415 
419  struct GNUNET_HashCode in_hmac;
420 
425  struct GNUNET_HashCode out_hmac;
426 
432 
437 
442 
446  struct sockaddr *address;
447 
453 
458  struct GNUNET_TIME_Absolute rekey_time;
459 
463  socklen_t address_len;
464 
469 
474 
478  unsigned long long bytes_in_queue;
479 
483  char cread_buf[BUF_SIZE];
484 
488  char cwrite_buf[BUF_SIZE];
489 
493  char pread_buf[UINT16_MAX + 1 + sizeof(struct TCPBox)];
494 
498  char pwrite_buf[UINT16_MAX + 1 + sizeof(struct TCPBox)];
499 
504  size_t cread_off;
505 
510  size_t cwrite_off;
511 
516  size_t pread_off;
517 
522  size_t pwrite_off;
523 
528 
536  unsigned int backpressure;
537 
542 
547 
552 
560 
565  int rekeyed;
566 
570  struct GNUNET_TIME_AbsoluteNBO rekey_monotonic_time;
571 
575  struct GNUNET_TIME_AbsoluteNBO handshake_monotonic_time;
576 
580  struct GNUNET_TIME_AbsoluteNBO handshake_ack_monotonic_time;
581 
585  struct ChallengeNonceP challenge;
586 
591 
596 
601 
606 
611 
616 };
617 
618 
624 {
628  struct ProtoQueue *next;
629 
633  struct ProtoQueue *prev;
634 
639 
644 
649 
653  struct sockaddr *address;
654 
658  socklen_t address_len;
659 
664 
669  char ibuf[INITIAL_KX_SIZE];
670 
674  size_t ibuf_off;
675 };
676 
681 {
685  struct sockaddr *addr_ipv4;
686 
690  socklen_t addr_len_ipv4;
691 
695  struct sockaddr *addr_ipv6;
696 
700  socklen_t addr_len_ipv6;
701 
702 };
703 
707 struct Addresses
708 {
712  struct Addresses *next;
713 
717  struct Addresses *prev;
718 
722  struct sockaddr *addr;
723 
727  socklen_t addr_len;
728 
729 };
730 
731 
732 
736 static unsigned long long max_queue_length;
737 
742 
747 
752 
757 
762 
767 
772 
776 static const struct GNUNET_CONFIGURATION_Handle *cfg;
777 
782 
786 static struct GNUNET_NAT_Handle *nat;
787 
791 static struct ProtoQueue *proto_head;
792 
796 static struct ProtoQueue *proto_tail;
797 
802 
807 
812 
817 
822 
827 
832 
837 
842 
846 unsigned int bind_port;
847 
855 static void
856 listen_cb (void *cls);
857 
865 static void
867 {
868  struct ListenTask *lt = NULL;
869  struct GNUNET_HashCode h_sock;
870 
872  sizeof(queue->listen_sock),
873  &h_sock);
874 
875  lt = GNUNET_CONTAINER_multihashmap_get (lt_map, &h_sock);
876 
878  "Disconnecting queue for peer `%s'\n",
879  GNUNET_i2s (&queue->target));
880  if (NULL != queue->rekey_monotime_sc)
881  {
883  queue->rekey_monotime_sc = NULL;
884  }
885  if (NULL != queue->handshake_monotime_sc)
886  {
888  queue->handshake_monotime_sc = NULL;
889  }
890  if (NULL != queue->handshake_ack_monotime_sc)
891  {
893  queue->handshake_ack_monotime_sc = NULL;
894  }
895  if (NULL != queue->rekey_monotime_get)
896  {
898  queue->rekey_monotime_get = NULL;
899  }
900  if (NULL != queue->handshake_monotime_get)
901  {
903  queue->handshake_monotime_get = NULL;
904  }
905  if (NULL != queue->handshake_ack_monotime_get)
906  {
908  queue->handshake_ack_monotime_get = NULL;
909  }
910  if (NULL != queue->qh)
911  {
913  queue->qh = NULL;
914  }
915  GNUNET_assert (
916  GNUNET_YES ==
917  GNUNET_CONTAINER_multipeermap_remove (queue_map, &queue->target, queue));
918  GNUNET_STATISTICS_set (stats,
919  "# queues active",
921  GNUNET_NO);
922  if (NULL != queue->read_task)
923  {
925  queue->read_task = NULL;
926  }
927  if (NULL != queue->write_task)
928  {
930  queue->write_task = NULL;
931  }
933  {
935  "closing socket failed\n");
936  }
937  gcry_cipher_close (queue->in_cipher);
938  gcry_cipher_close (queue->out_cipher);
939  GNUNET_free (queue->address);
940  if (0 != queue->backpressure)
941  queue->destroyed = GNUNET_YES;
942  else
943  GNUNET_free (queue);
944 
945  if ((! shutdown_running) && (NULL == lt->listen_task))
946  {
948  "add read net listen\n");
951  lt->listen_sock,
952  &listen_cb,
953  lt);
954  }
955  else
956  GNUNET_free (lt);
957 }
958 
959 
968 static void
969 calculate_hmac (struct GNUNET_HashCode *hmac_secret,
970  const void *buf,
971  size_t buf_size,
972  struct GNUNET_ShortHashCode *smac)
973 {
974  struct GNUNET_HashCode mac;
975 
976  GNUNET_CRYPTO_hmac_raw (hmac_secret,
977  sizeof(struct GNUNET_HashCode),
978  buf,
979  buf_size,
980  &mac);
981  /* truncate to `struct GNUNET_ShortHashCode` */
982  memcpy (smac, &mac, sizeof(struct GNUNET_ShortHashCode));
983  /* ratchet hmac key */
984  GNUNET_CRYPTO_hash (hmac_secret,
985  sizeof(struct GNUNET_HashCode),
986  hmac_secret);
987 }
988 
989 
996 static void
998 {
999  struct TCPFinish fin;
1000 
1001  memset (&fin, 0, sizeof(fin));
1002  fin.header.size = htons (sizeof(fin));
1004  calculate_hmac (&queue->out_hmac, &fin, sizeof(fin), &fin.hmac);
1005  /* if there is any message left in pwrite_buf, we
1006  overwrite it (possibly dropping the last message
1007  from CORE hard here) */
1008  memcpy (queue->pwrite_buf, &fin, sizeof(fin));
1009  queue->pwrite_off = sizeof(fin);
1010  /* This flag will ensure that #queue_write() no longer
1011  notifies CORE about the possibility of sending
1012  more data, and that #queue_write() will call
1013  #queue_destroy() once the @c fin was fully written. */
1014  queue->finishing = GNUNET_YES;
1015 }
1016 
1017 
1025 static void
1027 {
1028  queue->timeout =
1030 }
1031 
1032 
1038 static void
1039 queue_read (void *cls);
1040 
1041 
1049 static void
1050 core_read_finished_cb (void *cls, int success)
1051 {
1052  struct Queue *queue = cls;
1053  if (GNUNET_OK != success)
1054  GNUNET_STATISTICS_update (stats,
1055  "# messages lost in communicator API towards CORE",
1056  1,
1057  GNUNET_NO);
1058  if (NULL == queue)
1059  return;
1060 
1062  "backpressure %u\n",
1063  queue->backpressure);
1064 
1065  queue->backpressure--;
1066  /* handle deferred queue destruction */
1067  if ((queue->destroyed) && (0 == queue->backpressure))
1068  {
1069  GNUNET_free (queue);
1070  return;
1071  }
1072  else if (GNUNET_YES != queue->destroyed)
1073  {
1074  reschedule_queue_timeout (queue);
1075  /* possibly unchoke reading, now that CORE made progress */
1076  if (NULL == queue->read_task)
1077  queue->read_task =
1079  queue->timeout),
1080  queue->sock,
1081  &queue_read,
1082  queue);
1083  }
1084 }
1085 
1086 
1096 static void
1098  const void *plaintext,
1099  size_t plaintext_len)
1100 {
1101  const struct GNUNET_MessageHeader *hdr = plaintext;
1102  int ret;
1103 
1104  if (ntohs (hdr->size) != plaintext_len)
1105  {
1106  /* NOTE: If we ever allow multiple CORE messages in one
1107  BOX, this will have to change! */
1108  GNUNET_break (0);
1109  return;
1110  }
1112  &queue->target,
1113  hdr,
1116  queue);
1117  if (GNUNET_OK == ret)
1118  queue->backpressure++;
1119  GNUNET_break (GNUNET_NO != ret); /* backpressure not working!? */
1120  if (GNUNET_SYSERR == ret)
1121  GNUNET_STATISTICS_update (stats,
1122  "# bytes lost due to CORE not running",
1123  plaintext_len,
1124  GNUNET_NO);
1125 }
1126 
1127 
1137 static void
1138 setup_cipher (const struct GNUNET_HashCode *dh,
1139  const struct GNUNET_PeerIdentity *pid,
1140  gcry_cipher_hd_t *cipher,
1141  struct GNUNET_HashCode *hmac_key)
1142 {
1143  char key[256 / 8];
1144  char ctr[128 / 8];
1145 
1146  gcry_cipher_open (cipher,
1147  GCRY_CIPHER_AES256 /* low level: go for speed */,
1148  GCRY_CIPHER_MODE_CTR,
1149  0 /* flags */);
1151  sizeof(key),
1152  "TCP-key",
1153  strlen ("TCP-key"),
1154  dh,
1155  sizeof(*dh),
1156  pid,
1157  sizeof(*pid),
1158  NULL,
1159  0));
1160  gcry_cipher_setkey (*cipher, key, sizeof(key));
1162  sizeof(ctr),
1163  "TCP-ctr",
1164  strlen ("TCP-ctr"),
1165  dh,
1166  sizeof(*dh),
1167  pid,
1168  sizeof(*pid),
1169  NULL,
1170  0));
1171  gcry_cipher_setctr (*cipher, ctr, sizeof(ctr));
1173  GNUNET_CRYPTO_kdf (hmac_key,
1174  sizeof(struct GNUNET_HashCode),
1175  "TCP-hmac",
1176  strlen ("TCP-hmac"),
1177  dh,
1178  sizeof(*dh),
1179  pid,
1180  sizeof(*pid),
1181  NULL,
1182  0));
1183 }
1184 
1185 
1192 static void
1194  struct Queue *queue)
1195 {
1196  struct GNUNET_HashCode dh;
1197 
1198  GNUNET_CRYPTO_eddsa_ecdh (my_private_key, ephemeral, &dh);
1199  setup_cipher (&dh, &my_identity, &queue->in_cipher, &queue->in_hmac);
1200 }
1201 
1207 static void
1208 rekey_monotime_store_cb (void *cls, int success)
1209 {
1210  struct Queue *queue = cls;
1211  if (GNUNET_OK != success)
1212  {
1214  "Failed to store rekey monotonic time in PEERSTORE!\n");
1215  }
1216  queue->rekey_monotime_sc = NULL;
1217 }
1218 
1226 static void
1228  const struct GNUNET_PEERSTORE_Record *record,
1229  const char *emsg)
1230 {
1231  struct Queue *queue = cls;
1232  struct GNUNET_TIME_AbsoluteNBO *mtbe;
1233  struct GNUNET_TIME_Absolute mt;
1234  const struct GNUNET_PeerIdentity *pid;
1235  struct GNUNET_TIME_AbsoluteNBO *rekey_monotonic_time;
1236 
1237  (void) emsg;
1238 
1239  rekey_monotonic_time = &queue->rekey_monotonic_time;
1240  pid = &queue->target;
1241  if (NULL == record)
1242  {
1243  queue->rekey_monotime_get = NULL;
1244  return;
1245  }
1246  if (sizeof(*mtbe) != record->value_size)
1247  {
1248  GNUNET_break (0);
1249  return;
1250  }
1251  mtbe = record->value;
1252  mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1255  {
1257  "Queue from %s dropped, rekey monotime in the past\n",
1258  GNUNET_i2s (&queue->target));
1259  GNUNET_break (0);
1260  queue_finish (queue);
1261  return;
1262  }
1263  queue->rekey_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
1264  "transport_tcp_communicator",
1265  pid,
1267  rekey_monotonic_time,
1268  sizeof(rekey_monotonic_time),
1272  queue);
1273 }
1274 
1283 static void
1284 do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
1285 {
1286  struct TcpRekeySignature thp;
1287 
1289  thp.purpose.size = htonl (sizeof(thp));
1291  "do_rekey size %u\n",
1292  thp.purpose.size);
1293  thp.sender = queue->target;
1295  "sender %s\n",
1296  GNUNET_p2s (&thp.sender.public_key));
1298  "sender %s\n",
1299  GNUNET_p2s (&queue->target.public_key));
1300  thp.receiver = my_identity;
1302  "receiver %s\n",
1303  GNUNET_p2s (&thp.receiver.public_key));
1304  thp.ephemeral = rekey->ephemeral;
1306  "ephemeral %s\n",
1307  GNUNET_e2s (&thp.ephemeral));
1308  thp.monotonic_time = rekey->monotonic_time;
1310  "time %s\n",
1313  GNUNET_assert (ntohl ((&thp)->purpose.size) == sizeof (*(&thp)));
1314  if (GNUNET_OK !=
1316  &thp,
1317  &rekey->sender_sig,
1318  &queue->target.public_key))
1319  {
1320  GNUNET_break (0);
1321  queue_finish (queue);
1322  return;
1323  }
1324  queue->rekey_monotonic_time = rekey->monotonic_time;
1325  queue->rekey_monotime_get = GNUNET_PEERSTORE_iterate (peerstore,
1326  "transport_tcp_communicator",
1327  &queue->target,
1330  queue);
1331  gcry_cipher_close (queue->in_cipher);
1332  queue->rekeyed = GNUNET_YES;
1333  setup_in_cipher (&rekey->ephemeral, queue);
1334 }
1335 
1341 static void
1342 handshake_ack_monotime_store_cb (void *cls, int success)
1343 {
1344  struct Queue *queue = cls;
1345 
1346  if (GNUNET_OK != success)
1347  {
1349  "Failed to store handshake ack monotonic time in PEERSTORE!\n");
1350  }
1351  queue->handshake_ack_monotime_sc = NULL;
1352 }
1353 
1361 static void
1363  const struct GNUNET_PEERSTORE_Record *record,
1364  const char *emsg)
1365 {
1366  struct Queue *queue = cls;
1367  struct GNUNET_TIME_AbsoluteNBO *mtbe;
1368  struct GNUNET_TIME_Absolute mt;
1369  const struct GNUNET_PeerIdentity *pid;
1370  struct GNUNET_TIME_AbsoluteNBO *handshake_ack_monotonic_time;
1371 
1372  (void) emsg;
1373 
1374  handshake_ack_monotonic_time = &queue->handshake_ack_monotonic_time;
1375  pid = &queue->target;
1376  if (NULL == record)
1377  {
1378  queue->handshake_ack_monotime_get = NULL;
1379  return;
1380  }
1381  if (sizeof(*mtbe) != record->value_size)
1382  {
1383  GNUNET_break (0);
1384  return;
1385  }
1386  mtbe = record->value;
1387  mt = GNUNET_TIME_absolute_ntoh (*mtbe);
1390  {
1392  "Queue from %s dropped, handshake ack monotime in the past\n",
1393  GNUNET_i2s (&queue->target));
1394  GNUNET_break (0);
1395  queue_finish (queue);
1396  return;
1397  }
1399  "transport_tcp_communicator",
1400  pid,
1402  handshake_ack_monotonic_time,
1403  sizeof(
1404  handshake_ack_monotonic_time),
1407  &
1409  queue);
1410 }
1411 
1419 static size_t
1421 {
1422  const struct GNUNET_MessageHeader *hdr =
1423  (const struct GNUNET_MessageHeader *) queue->pread_buf;
1424  const struct TCPConfirmationAck *tca = (const struct
1425  TCPConfirmationAck *) queue->pread_buf;
1426  const struct TCPBox *box = (const struct TCPBox *) queue->pread_buf;
1427  const struct TCPRekey *rekey = (const struct TCPRekey *) queue->pread_buf;
1428  const struct TCPFinish *fin = (const struct TCPFinish *) queue->pread_buf;
1429  struct TCPRekey rekeyz;
1430  struct TCPFinish finz;
1431  struct GNUNET_ShortHashCode tmac;
1432  uint16_t type;
1433  size_t size = 0; /* make compiler happy */
1434  struct TcpHandshakeAckSignature thas;
1435  const struct ChallengeNonceP challenge = queue->challenge;
1436 
1437  if ((sizeof(*hdr) > queue->pread_off))
1438  {
1440  "Handling plaintext, not even a header!\n");
1441  return 0; /* not even a header */
1442  }
1443 
1445  {
1447  "Already received data of size %lu bigger than KX size %lu!\n",
1450  GNUNET_break_op (0);
1451  queue_finish (queue);
1452  return 0;
1453  }
1454 
1455  type = ntohs (hdr->type);
1456  switch (type)
1457  {
1460  "start processing ack\n");
1461  if (sizeof(*tca) > queue->pread_off)
1462  {
1464  "Handling plaintext size of tca greater than pread offset.\n");
1465  return 0;
1466  }
1467  if (ntohs (hdr->size) != sizeof(*tca))
1468  {
1470  "Handling plaintext size does not match message type.\n");
1471  GNUNET_break_op (0);
1472  queue_finish (queue);
1473  return 0;
1474  }
1475 
1476  thas.purpose.purpose = htonl (
1478  thas.purpose.size = htonl (sizeof(thas));
1479  thas.sender = tca->sender;
1480  thas.receiver = my_identity;
1481  thas.monotonic_time = tca->monotonic_time;
1482  thas.challenge = tca->challenge;
1483 
1486  &thas,
1487  &tca->sender_sig,
1488  &tca->sender.public_key))
1489  {
1491  "Verification of signature failed!\n");
1492  GNUNET_break (0);
1493  queue_finish (queue);
1494  return 0;
1495  }
1496  if (0 != GNUNET_memcmp (&tca->challenge, &challenge))
1497  {
1499  "Challenge in TCPConfirmationAck not correct!\n");
1500  GNUNET_break (0);
1501  queue_finish (queue);
1502  return 0;
1503  }
1504 
1506  "transport_tcp_communicator",
1507  &queue->target,
1509  &
1511  queue);
1512 
1514  "Handling plaintext, ack processed!");
1515 
1516  unverified_size = -1;
1517 
1518  size = ntohs (hdr->size);
1519  break;
1521  /* Special case: header size excludes box itself! */
1522  if (ntohs (hdr->size) + sizeof(struct TCPBox) > queue->pread_off)
1523  return 0;
1524  calculate_hmac (&queue->in_hmac, &box[1], ntohs (hdr->size), &tmac);
1525  if (0 != memcmp (&tmac, &box->hmac, sizeof(tmac)))
1526  {
1527  GNUNET_break_op (0);
1528  queue_finish (queue);
1529  return 0;
1530  }
1531  pass_plaintext_to_core (queue, (const void *) &box[1], ntohs (hdr->size));
1532  size = ntohs (hdr->size) + sizeof(*box);
1534  "Handling plaintext, box processed!\n");
1535  break;
1536 
1538  if (sizeof(*rekey) > queue->pread_off)
1539  return 0;
1540  if (ntohs (hdr->size) != sizeof(*rekey))
1541  {
1542  GNUNET_break_op (0);
1543  queue_finish (queue);
1544  return 0;
1545  }
1546  rekeyz = *rekey;
1547  memset (&rekeyz.hmac, 0, sizeof(rekeyz.hmac));
1548  calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
1549  if (0 != memcmp (&tmac, &rekey->hmac, sizeof(tmac)))
1550  {
1551  GNUNET_break_op (0);
1552  queue_finish (queue);
1553  return 0;
1554  }
1555  do_rekey (queue, rekey);
1556  size = ntohs (hdr->size);
1558  "Handling plaintext, rekey processed!\n");
1559  break;
1560 
1562  if (sizeof(*fin) > queue->pread_off)
1563  return 0;
1564  if (ntohs (hdr->size) != sizeof(*fin))
1565  {
1566  GNUNET_break_op (0);
1567  queue_finish (queue);
1568  return 0;
1569  }
1570  finz = *fin;
1571  memset (&finz.hmac, 0, sizeof(finz.hmac));
1572  calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
1573  if (0 != memcmp (&tmac, &fin->hmac, sizeof(tmac)))
1574  {
1575  GNUNET_break_op (0);
1576  queue_finish (queue);
1577  return 0;
1578  }
1579  /* handle FINISH by destroying queue */
1580  queue_destroy (queue);
1582  "Handling plaintext, finish processed!\n");
1583  break;
1584 
1585  default:
1587  "Handling plaintext, nothing processed!\n");
1588  GNUNET_break_op (0);
1589  queue_finish (queue);
1590  return 0;
1591  }
1592  GNUNET_assert (0 != size);
1593  if (-1 != unverified_size)
1594  unverified_size += size;
1595  return size;
1596 }
1597 
1598 
1604 static void
1605 queue_read (void *cls)
1606 {
1607  struct Queue *queue = cls;
1608  struct GNUNET_TIME_Relative left;
1609  ssize_t rcvd;
1610 
1611  queue->read_task = NULL;
1612  rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
1613  &queue->cread_buf[queue->cread_off],
1614  BUF_SIZE - queue->cread_off);
1616  "Received %lu bytes from TCP queue\n", rcvd);
1617  if (-1 == rcvd)
1618  {
1619  if ((EAGAIN != errno) && (EINTR != errno))
1620  {
1622  queue_finish (queue);
1623  return;
1624  }
1625  /* try again */
1627  queue->read_task =
1628  GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read, queue);
1629  return;
1630  }
1631  if (0 != rcvd)
1632  reschedule_queue_timeout (queue);
1633  queue->cread_off += rcvd;
1634  while ((queue->pread_off < sizeof(queue->pread_buf)) &&
1635  (queue->cread_off > 0))
1636  {
1637  size_t max = GNUNET_MIN (sizeof(queue->pread_buf) - queue->pread_off,
1638  queue->cread_off);
1639  size_t done;
1640  size_t total;
1641  size_t old_pread_off = queue->pread_off;
1642 
1643  GNUNET_assert (0 ==
1644  gcry_cipher_decrypt (queue->in_cipher,
1645  &queue->pread_buf[queue->pread_off],
1646  max,
1647  queue->cread_buf,
1648  max));
1649  queue->pread_off += max;
1650  total = 0;
1651  while (0 != (done = try_handle_plaintext (queue)))
1652  {
1653  /* 'done' bytes of plaintext were used, shift buffer */
1654  GNUNET_assert (done <= queue->pread_off);
1655  /* NOTE: this memmove() could possibly sometimes be
1656  avoided if we pass 'total' into try_handle_plaintext()
1657  and use it at an offset into the buffer there! */
1658  memmove (queue->pread_buf,
1659  &queue->pread_buf[done],
1660  queue->pread_off - done);
1661  queue->pread_off -= done;
1662  total += done;
1663  /* The last plaintext was a rekey, abort for now */
1664  if (GNUNET_YES == queue->rekeyed)
1665  break;
1666  }
1667  /* when we encounter a rekey message, the decryption above uses the
1668  wrong key for everything after the rekey; in that case, we have
1669  to re-do the decryption at 'total' instead of at 'max'.
1670  However, we have to take into account that the plaintext buffer may have
1671  already contained data and not jumpt too far ahead in the ciphertext.
1672  If there is no rekey and the last message is incomplete (max > total),
1673  it is safe to keep the decryption so we shift by 'max' */
1674  if (GNUNET_YES == queue->rekeyed)
1675  {
1676  max = total - old_pread_off;
1677  queue->rekeyed = GNUNET_NO;
1678  queue->pread_off = 0;
1679  }
1680  memmove (queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
1681  queue->cread_off -= max;
1682  }
1683  if (BUF_SIZE == queue->cread_off)
1684  return; /* buffer full, suspend reading */
1686  if (0 != left.rel_value_us)
1687  {
1688  if (max_queue_length > queue->backpressure)
1689  {
1690  /* continue reading */
1692  queue->read_task =
1693  GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read, queue);
1694  }
1695  return;
1696  }
1698  "Queue %p was idle for %s, disconnecting\n",
1699  queue,
1702  GNUNET_YES));
1703  queue_finish (queue);
1704 }
1705 
1713 static struct sockaddr *
1714 tcp_address_to_sockaddr_numeric_v6 (socklen_t *sock_len, struct sockaddr_in6 v6,
1715  unsigned int port)
1716 {
1717  struct sockaddr *in;
1718 
1719  v6.sin6_family = AF_INET6;
1720  v6.sin6_port = htons ((uint16_t) port);
1721 #if HAVE_SOCKADDR_IN_SIN_LEN
1722  v6.sin6_len = sizeof(sizeof(struct sockaddr_in6));
1723 #endif
1724  v6.sin6_flowinfo = 0;
1725  v6.sin6_scope_id = 0;
1726  in = GNUNET_memdup (&v6, sizeof(v6));
1727  *sock_len = sizeof(struct sockaddr_in6);
1728 
1729  return in;
1730 }
1731 
1739 static struct sockaddr *
1740 tcp_address_to_sockaddr_numeric_v4 (socklen_t *sock_len, struct sockaddr_in v4,
1741  unsigned int port)
1742 {
1743  struct sockaddr *in;
1744 
1745  v4.sin_family = AF_INET;
1746  v4.sin_port = htons ((uint16_t) port);
1747 #if HAVE_SOCKADDR_IN_SIN_LEN
1748  v4.sin_len = sizeof(struct sockaddr_in);
1749 #endif
1750  in = GNUNET_memdup (&v4, sizeof(v4));
1751  *sock_len = sizeof(struct sockaddr_in);
1752  return in;
1753 }
1754 
1761 static struct PortOnlyIpv4Ipv6 *
1762 tcp_address_to_sockaddr_port_only (const char *bindto, unsigned int *port)
1763 {
1764  struct PortOnlyIpv4Ipv6 *po;
1765  struct sockaddr_in *i4;
1766  struct sockaddr_in6 *i6;
1767  socklen_t sock_len_ipv4;
1768  socklen_t sock_len_ipv6;
1769 
1770  /* interpreting value as just a PORT number */
1771  if (*port > UINT16_MAX)
1772  {
1774  "BINDTO specification `%s' invalid: value too large for port\n",
1775  bindto);
1776  return NULL;
1777  }
1778 
1779  po = GNUNET_new (struct PortOnlyIpv4Ipv6);
1780 
1781  if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
1782  (GNUNET_YES ==
1785  "DISABLE_V6")))
1786  {
1787  i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
1788  po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
1789  *port);
1790  po->addr_len_ipv4 = sock_len_ipv4;
1791  }
1792  else
1793  {
1794 
1795  i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
1796  po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
1797  *port);
1798  po->addr_len_ipv4 = sock_len_ipv4;
1799 
1800  i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
1801  po->addr_ipv6 = tcp_address_to_sockaddr_numeric_v6 (&sock_len_ipv6, *i6,
1802  *port);
1803 
1804  po->addr_len_ipv6 = sock_len_ipv6;
1805 
1806  GNUNET_free (i6);
1807  }
1808 
1809  GNUNET_free (i4);
1810 
1811  return po;
1812 }
1813 
1820 static char *
1821 extract_address (const char *bindto)
1822 {
1823 
1824  char *start;
1825  char *token;
1826  char *cp;
1827  char *rest = NULL;
1828 
1830  "extract address with bindto %s\n",
1831  bindto);
1832 
1833  if (NULL == bindto)
1835  "bindto is NULL\n");
1836 
1837  cp = GNUNET_strdup (bindto);
1838 
1840  "extract address 2\n");
1841 
1842  start = cp;
1843  if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
1844  {
1845  start++; /* skip over '['*/
1846  cp[strlen (cp) - 1] = '\0'; /* eat ']'*/
1847  }
1848  else {
1849  token = strtok_r (cp, "]", &rest);
1850  if (strlen (bindto) == strlen (token))
1851  {
1852  token = strtok_r (cp, ":", &rest);
1853  }
1854  else
1855  {
1856  token++;
1857  return token;
1858  }
1859  }
1860 
1862  "extract address 3\n");
1864  "extract address with start %s\n",
1865  start);
1866 
1867  return start;
1868 }
1869 
1876 static unsigned int
1877 extract_port (const char *addr_and_port)
1878 {
1879  unsigned int port;
1880  char dummy[2];
1881  char *token;
1882  char *addr;
1883  char *colon;
1884  char *cp;
1885  char *rest = NULL;
1886 
1887  if (NULL != addr_and_port)
1888  {
1889  cp = GNUNET_strdup (addr_and_port);
1890  token = strtok_r (cp, "]", &rest);
1891  if (strlen (addr_and_port) == strlen (token))
1892  {
1893  colon = strrchr (cp, ':');
1894  if (NULL == colon)
1895  {
1896  return 0;
1897  }
1898  addr = colon;
1899  addr++;
1900  }
1901  else
1902  {
1903  token = strtok_r (NULL, "]", &rest);
1904  if (NULL == token)
1905  {
1906  return 0;
1907  }
1908  else
1909  {
1910  addr = token;
1911  addr++;
1912  }
1913  }
1914 
1915 
1916  if (1 == sscanf (addr, "%u%1s", &port, dummy))
1917  {
1918  /* interpreting value as just a PORT number */
1919  if (port > UINT16_MAX)
1920  {
1922  "Port `%u' invalid: value too large for port\n",
1923  port);
1924  // GNUNET_free (cp);
1925  return 0;
1926  }
1927  }
1928  else
1929  {
1931  "BINDTO specification invalid: last ':' not followed by number\n");
1932  // GNUNET_free (cp);
1933  return 0;
1934  }
1935  }
1936  else
1937  {
1939  "return 0\n");
1940  /* interpret missing port as 0, aka pick any free one */
1941  port = 0;
1942  }
1943 
1944 
1945  return port;
1946 }
1947 
1955 static struct sockaddr *
1956 tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
1957 {
1958  struct sockaddr *in;
1959  unsigned int port;
1960  struct sockaddr_in v4;
1961  struct sockaddr_in6 v6;
1962  const char *start;
1963 
1964  // cp = GNUNET_strdup (bindto);
1965  start = extract_address (bindto);
1966 
1968  "start %s\n",
1969  start);
1970 
1972  "!bindto %s\n",
1973  bindto);
1974 
1975 
1976 
1977  if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
1978  {
1979  // colon = strrchr (cp, ':');
1980  port = extract_port (bindto);
1981 
1983  "port %u\n",
1984  port);
1985 
1986  in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port);
1987  }
1988  else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
1989  {
1990  // colon = strrchr (cp, ':');
1991  port = extract_port (bindto);
1992  in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port);
1993  }
1994  else{
1995  GNUNET_assert (0);
1996  }
1997 
1998  // GNUNET_free (start);
1999 
2000  return in;
2001 }
2002 
2003 
2010 static void
2012 {
2013  struct GNUNET_HashCode dh;
2014 
2015  GNUNET_CRYPTO_ecdh_eddsa (&queue->ephemeral, &queue->target.public_key, &dh);
2016  /* we don't need the private key anymore, drop it! */
2017  memset (&queue->ephemeral, 0, sizeof(queue->ephemeral));
2018  setup_cipher (&dh, &queue->target, &queue->out_cipher, &queue->out_hmac);
2020  queue->rekey_left_bytes =
2022 }
2023 
2024 
2031 static void
2033 {
2034  struct TCPRekey rekey;
2035  struct TcpRekeySignature thp;
2036 
2037  GNUNET_assert (0 == queue->pwrite_off);
2038  memset (&rekey, 0, sizeof(rekey));
2041  rekey.header.size = ntohs (sizeof(rekey));
2043  rekey.monotonic_time =
2046  thp.purpose.size = htonl (sizeof(thp));
2048  "inject_rekey size %u\n",
2049  thp.purpose.size);
2050  thp.sender = my_identity;
2052  "sender %s\n",
2053  GNUNET_p2s (&thp.sender.public_key));
2054  thp.receiver = queue->target;
2056  "receiver %s\n",
2057  GNUNET_p2s (&thp.receiver.public_key));
2058  thp.ephemeral = rekey.ephemeral;
2060  "ephemeral %s\n",
2061  GNUNET_e2s (&thp.ephemeral));
2062  thp.monotonic_time = rekey.monotonic_time;
2064  "time %s\n",
2067  GNUNET_CRYPTO_eddsa_sign (my_private_key,
2068  &thp,
2069  &rekey.sender_sig);
2070  calculate_hmac (&queue->out_hmac, &rekey, sizeof(rekey), &rekey.hmac);
2071  /* Encrypt rekey message with 'old' cipher */
2072  GNUNET_assert (0 ==
2073  gcry_cipher_encrypt (queue->out_cipher,
2074  &queue->cwrite_buf[queue->cwrite_off],
2075  sizeof(rekey),
2076  &rekey,
2077  sizeof(rekey)));
2078  queue->cwrite_off += sizeof(rekey);
2079  /* Setup new cipher for successive messages */
2080  gcry_cipher_close (queue->out_cipher);
2081  setup_out_cipher (queue);
2082 }
2083 
2084 
2091 static void
2092 queue_write (void *cls)
2093 {
2094  struct Queue *queue = cls;
2095  ssize_t sent;
2096  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In queue write\n");
2097  queue->write_task = NULL;
2098  if (0 != queue->cwrite_off)
2099  {
2100  sent = GNUNET_NETWORK_socket_send (queue->sock,
2101  queue->cwrite_buf,
2102  queue->cwrite_off);
2104  "Sent %lu bytes to TCP queue\n", sent);
2105  if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
2106  {
2108  queue_destroy (queue);
2109  return;
2110  }
2111  if (sent > 0)
2112  {
2113  size_t usent = (size_t) sent;
2114  queue->cwrite_off -= usent;
2115  memmove (queue->cwrite_buf,
2116  &queue->cwrite_buf[usent],
2117  queue->cwrite_off);
2118  reschedule_queue_timeout (queue);
2119  }
2120  }
2121  /* can we encrypt more? (always encrypt full messages, needed
2122  such that #mq_cancel() can work!) */
2123  if ((0 < queue->rekey_left_bytes) &&
2124  (queue->pwrite_off > 0) &&
2125  (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE))
2126  {
2128  "Encrypting %lu bytes\n", queue->pwrite_off);
2129  GNUNET_assert (0 ==
2130  gcry_cipher_encrypt (queue->out_cipher,
2131  &queue->cwrite_buf[queue->cwrite_off],
2132  queue->pwrite_off,
2133  queue->pwrite_buf,
2134  queue->pwrite_off));
2135  if (queue->rekey_left_bytes > queue->pwrite_off)
2136  queue->rekey_left_bytes -= queue->pwrite_off;
2137  else
2138  queue->rekey_left_bytes = 0;
2139  queue->cwrite_off += queue->pwrite_off;
2140  queue->pwrite_off = 0;
2141  }
2142  if ((-1 != unverified_size)&& ((0 == queue->pwrite_off) &&
2143  ((0 == queue->rekey_left_bytes) ||
2144  (0 ==
2146  queue->rekey_time).rel_value_us))))
2147  {
2148  inject_rekey (queue);
2149  }
2150  if ((0 == queue->pwrite_off) && (! queue->finishing) &&
2151  (GNUNET_YES == queue->mq_awaits_continue))
2152  {
2153  queue->mq_awaits_continue = GNUNET_NO;
2155  }
2156  /* did we just finish writing 'finish'? */
2157  if ((0 == queue->cwrite_off) && (GNUNET_YES == queue->finishing))
2158  {
2160  "Finishing queue\n");
2161  queue_destroy (queue);
2162  return;
2163  }
2164  /* do we care to write more? */
2165  if ((0 < queue->cwrite_off) || (0 < queue->pwrite_off))
2166  queue->write_task =
2168  queue->sock,
2169  &queue_write,
2170  queue);
2171 }
2172 
2173 
2182 static void
2184  const struct GNUNET_MessageHeader *msg,
2185  void *impl_state)
2186 {
2187  struct Queue *queue = impl_state;
2188  uint16_t msize = ntohs (msg->size);
2189  struct TCPBox box;
2191  "In MQ send. Queue finishing: %s; write task running: %s\n",
2192  (GNUNET_YES == queue->finishing) ? "yes" : "no",
2193  (NULL == queue->write_task) ? "yes" : "no");
2194  GNUNET_assert (mq == queue->mq);
2195  queue->mq_awaits_continue = GNUNET_YES;
2196  if (GNUNET_YES == queue->finishing)
2197  return; /* this queue is dying, drop msg */
2198  GNUNET_assert (0 == queue->pwrite_off);
2200  box.header.size = htons (msize);
2201  calculate_hmac (&queue->out_hmac, msg, msize, &box.hmac);
2202  memcpy (&queue->pwrite_buf[queue->pwrite_off], &box, sizeof(box));
2203  queue->pwrite_off += sizeof(box);
2204  memcpy (&queue->pwrite_buf[queue->pwrite_off], msg, msize);
2205  queue->pwrite_off += msize;
2207  "%lu bytes of plaintext to send\n", queue->pwrite_off);
2208  GNUNET_assert (NULL != queue->sock);
2209  if (NULL == queue->write_task)
2210  queue->write_task =
2212  queue->sock,
2213  &queue_write,
2214  queue);
2215 }
2216 
2217 
2226 static void
2227 mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
2228 {
2229  struct Queue *queue = impl_state;
2230 
2231  if (mq == queue->mq)
2232  {
2233  queue->mq = NULL;
2234  queue_finish (queue);
2235  }
2236 }
2237 
2238 
2245 static void
2246 mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
2247 {
2248  struct Queue *queue = impl_state;
2249 
2250  GNUNET_assert (0 != queue->pwrite_off);
2251  queue->pwrite_off = 0;
2252 }
2253 
2254 
2264 static void
2265 mq_error (void *cls, enum GNUNET_MQ_Error error)
2266 {
2267  struct Queue *queue = cls;
2268 
2270  "MQ error in queue to %s: %d\n",
2271  GNUNET_i2s (&queue->target),
2272  (int) error);
2273  queue_finish (queue);
2274 }
2275 
2276 
2284 static void
2286 {
2287  queue->nt =
2288  GNUNET_NT_scanner_get_type (is, queue->address, queue->address_len);
2290  queue_map,
2291  &queue->target,
2292  queue,
2294  GNUNET_STATISTICS_set (stats,
2295  "# queues active",
2297  GNUNET_NO);
2298  queue->timeout =
2301  &mq_destroy,
2302  &mq_cancel,
2303  queue,
2304  NULL,
2305  &mq_error,
2306  queue);
2307  {
2308  char *foreign_addr;
2309 
2310  switch (queue->address->sa_family)
2311  {
2312  case AF_INET:
2313  GNUNET_asprintf (&foreign_addr,
2314  "%s-%s",
2316  GNUNET_a2s (queue->address, queue->address_len));
2317  break;
2318 
2319  case AF_INET6:
2320  GNUNET_asprintf (&foreign_addr,
2321  "%s-%s",
2323  GNUNET_a2s (queue->address, queue->address_len));
2324  break;
2325 
2326  default:
2327  GNUNET_assert (0);
2328  }
2330  &queue->target,
2331  foreign_addr,
2332  0 /* no MTU */,
2334  0, /* Priority */
2335  queue->nt,
2336  cs,
2337  queue->mq);
2338  GNUNET_free (foreign_addr);
2339  }
2340 }
2341 
2342 
2353 static void
2355  const struct GNUNET_CRYPTO_EcdhePublicKey *epub)
2356 {
2357  struct TcpHandshakeSignature ths;
2358  struct TCPConfirmation tc;
2359 
2360  memcpy (queue->cwrite_buf, epub, sizeof(*epub));
2361  queue->cwrite_off = sizeof(*epub);
2362  /* compute 'tc' and append in encrypted format to cwrite_buf */
2363  tc.sender = my_identity;
2364  tc.monotonic_time =
2367  &tc.challenge,
2368  sizeof(tc.challenge));
2370  ths.purpose.size = htonl (sizeof(ths));
2371  ths.sender = my_identity;
2372  ths.receiver = queue->target;
2373  ths.ephemeral = *epub;
2374  ths.monotonic_time = tc.monotonic_time;
2375  ths.challenge = tc.challenge;
2376  GNUNET_CRYPTO_eddsa_sign (my_private_key,
2377  &ths,
2378  &tc.sender_sig);
2379  GNUNET_assert (0 ==
2380  gcry_cipher_encrypt (queue->out_cipher,
2381  &queue->cwrite_buf[queue->cwrite_off],
2382  sizeof(tc),
2383  &tc,
2384  sizeof(tc)));
2385  queue->challenge = tc.challenge;
2386  queue->cwrite_off += sizeof(tc);
2387 
2389  "transport",
2390  "handshake written\n");
2391 }
2392 
2393 
2401 static void
2403 {
2404  struct GNUNET_CRYPTO_EcdhePublicKey epub;
2405 
2408  setup_out_cipher (queue);
2409  transmit_kx (queue, &epub);
2410 }
2411 
2417 static void
2418 handshake_monotime_store_cb (void *cls, int success)
2419 {
2420  struct Queue *queue = cls;
2421  if (GNUNET_OK != success)
2422  {
2424  "Failed to store handshake monotonic time in PEERSTORE!\n");
2425  }
2426  queue->handshake_monotime_sc = NULL;
2427 }
2428 
2436 static void
2438  const struct GNUNET_PEERSTORE_Record *record,
2439  const char *emsg)
2440 {
2441  struct Queue *queue = cls;
2442  struct GNUNET_TIME_AbsoluteNBO *mtbe;
2443  struct GNUNET_TIME_Absolute mt;
2444  const struct GNUNET_PeerIdentity *pid;
2445  struct GNUNET_TIME_AbsoluteNBO *handshake_monotonic_time;
2446 
2447  (void) emsg;
2448 
2449  handshake_monotonic_time = &queue->handshake_monotonic_time;
2450  pid = &queue->target;
2451  if (NULL == record)
2452  {
2453  queue->handshake_monotime_get = NULL;
2454  return;
2455  }
2456  if (sizeof(*mtbe) != record->value_size)
2457  {
2458  GNUNET_break (0);
2459  return;
2460  }
2461  mtbe = record->value;
2462  mt = GNUNET_TIME_absolute_ntoh (*mtbe);
2465  {
2467  "Queue from %s dropped, handshake monotime in the past\n",
2468  GNUNET_i2s (&queue->target));
2469  GNUNET_break (0);
2470  queue_finish (queue);
2471  return;
2472  }
2473  queue->handshake_monotime_sc = GNUNET_PEERSTORE_store (peerstore,
2474  "transport_tcp_communicator",
2475  pid,
2477  handshake_monotonic_time,
2478  sizeof(
2479  handshake_monotonic_time),
2482  &
2484  queue);
2485 }
2486 
2498 static int
2500  struct TCPConfirmation *tc,
2501  char *ibuf)
2502 {
2503  struct TcpHandshakeSignature ths;
2504 
2505  GNUNET_assert (
2506  0 ==
2507  gcry_cipher_decrypt (queue->in_cipher,
2508  tc,
2509  sizeof(*tc),
2510  &ibuf[sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)],
2511  sizeof(*tc)));
2513  ths.purpose.size = htonl (sizeof(ths));
2514  ths.sender = tc->sender;
2515  ths.receiver = my_identity;
2516  memcpy (&ths.ephemeral, ibuf, sizeof(struct GNUNET_CRYPTO_EcdhePublicKey));
2517  ths.monotonic_time = tc->monotonic_time;
2518  ths.challenge = tc->challenge;
2521  &ths,
2522  &tc->sender_sig,
2523  &tc->sender.public_key);
2525  "transport_tcp_communicator",
2526  &queue->target,
2528  &
2530  queue);
2531 }
2532 
2533 
2539 static void
2541 {
2542  if (NULL != pq->listen_sock)
2543  {
2545  pq->listen_sock = NULL;
2546  }
2548  GNUNET_free (pq->address);
2549  GNUNET_CONTAINER_DLL_remove (proto_head, proto_tail, pq);
2550  GNUNET_free (pq);
2551 }
2552 
2559 static void
2561 {
2562  struct TCPConfirmationAck tca;
2563  struct TcpHandshakeAckSignature thas;
2564 
2566  "transport",
2567  "sending challenge\n");
2568 
2569  tca.header.type = ntohs (
2571  tca.header.size = ntohs (sizeof(tca));
2572  tca.challenge = tc.challenge;
2573  tca.sender = my_identity;
2574  tca.monotonic_time =
2576  thas.purpose.purpose = htonl (
2578  thas.purpose.size = htonl (sizeof(thas));
2579  thas.sender = my_identity;
2580  thas.receiver = queue->target;
2581  thas.monotonic_time = tca.monotonic_time;
2582  thas.challenge = tca.challenge;
2583  GNUNET_CRYPTO_eddsa_sign (my_private_key,
2584  &thas,
2585  &tca.sender_sig);
2586  GNUNET_assert (0 ==
2587  gcry_cipher_encrypt (queue->out_cipher,
2588  &queue->cwrite_buf[queue->cwrite_off],
2589  sizeof(tca),
2590  &tca,
2591  sizeof(tca)));
2592  queue->cwrite_off += sizeof(tca);
2593 }
2594 
2601 static void
2602 proto_read_kx (void *cls)
2603 {
2604  struct ProtoQueue *pq = cls;
2605  ssize_t rcvd;
2606  struct GNUNET_TIME_Relative left;
2607  struct Queue *queue;
2608  struct TCPConfirmation tc;
2609 
2610  pq->read_task = NULL;
2612  if (0 == left.rel_value_us)
2613  {
2614  free_proto_queue (pq);
2615  return;
2616  }
2617  rcvd = GNUNET_NETWORK_socket_recv (pq->sock,
2618  &pq->ibuf[pq->ibuf_off],
2619  sizeof(pq->ibuf) - pq->ibuf_off);
2621  "Received %lu bytes for KX\n", rcvd);
2622  if (-1 == rcvd)
2623  {
2624  if ((EAGAIN != errno) && (EINTR != errno))
2625  {
2627  free_proto_queue (pq);
2628  return;
2629  }
2630  /* try again */
2631  pq->read_task =
2633  return;
2634  }
2635  pq->ibuf_off += rcvd;
2636  if (pq->ibuf_off > sizeof(pq->ibuf))
2637  {
2638  /* read more */
2639  pq->read_task =
2641  return;
2642  }
2643  /* we got all the data, let's find out who we are talking to! */
2644  queue = GNUNET_new (struct Queue);
2645  setup_in_cipher ((const struct GNUNET_CRYPTO_EcdhePublicKey *) pq->ibuf,
2646  queue);
2647  if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, pq->ibuf))
2648  {
2650  "Invalid TCP KX received from %s\n",
2651  GNUNET_a2s (queue->address, queue->address_len));
2652  gcry_cipher_close (queue->in_cipher);
2653  GNUNET_free (queue);
2654  free_proto_queue (pq);
2655  return;
2656  }
2657  queue->address = pq->address; /* steals reference */
2658  queue->address_len = pq->address_len;
2659  queue->target = tc.sender;
2660  queue->listen_sock = pq->listen_sock;
2661  queue->sock = pq->sock;
2662 
2663 
2665  "start kx proto\n");
2666 
2667  start_initial_kx_out (queue);
2669  queue->read_task =
2671  queue->sock,
2672  &queue_read,
2673  queue);
2674  queue->write_task =
2676  queue->sock,
2677  &queue_write,
2678  queue);
2679  send_challenge (tc, queue);
2680 
2681  GNUNET_CONTAINER_DLL_remove (proto_head, proto_tail, pq);
2682  GNUNET_free (pq);
2683 }
2684 
2685 
2693 static void
2694 listen_cb (void *cls)
2695 {
2696  struct sockaddr_storage in;
2697  socklen_t addrlen;
2698  struct GNUNET_NETWORK_Handle *sock;
2699  struct ProtoQueue *pq;
2700  struct ListenTask *lt;
2701 
2703  "listen_cb\n");
2704 
2705  lt = cls;
2706 
2707  lt->listen_task = NULL;
2708  GNUNET_assert (NULL != lt->listen_sock);
2709  addrlen = sizeof(in);
2710  memset (&in, 0, sizeof(in));
2712  (struct sockaddr*) &in,
2713  &addrlen);
2714  if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno)))
2715  return; /* system limit reached, wait until connection goes down */
2717  lt->listen_sock,
2718  &listen_cb,
2719  lt);
2720  if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno)))
2721  return;
2722  if (NULL == sock)
2723  {
2725  return;
2726  }
2727  pq = GNUNET_new (struct ProtoQueue);
2728  pq->address_len = addrlen;
2729  pq->address = GNUNET_memdup (&in, addrlen);
2731  pq->sock = sock;
2733  pq->sock,
2734  &proto_read_kx,
2735  pq);
2736  GNUNET_CONTAINER_DLL_insert (proto_head, proto_tail, pq);
2737 }
2738 
2739 
2747 static void
2748 queue_read_kx (void *cls)
2749 {
2750  struct Queue *queue = cls;
2751  ssize_t rcvd;
2752  struct GNUNET_TIME_Relative left;
2753  struct TCPConfirmation tc;
2754 
2755  queue->read_task = NULL;
2757  if (0 == left.rel_value_us)
2758  {
2759  queue_destroy (queue);
2760  return;
2761  }
2762  rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2763  &queue->cread_buf[queue->cread_off],
2764  BUF_SIZE - queue->cread_off);
2765  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %lu bytes for KX\n", rcvd);
2766  if (-1 == rcvd)
2767  {
2768  if ((EAGAIN != errno) && (EINTR != errno))
2769  {
2771  queue_destroy (queue);
2772  return;
2773  }
2774  queue->read_task =
2775  GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read_kx, queue);
2776  return;
2777  }
2778  queue->cread_off += rcvd;
2779  if (queue->cread_off < INITIAL_KX_SIZE)
2780  {
2781  /* read more */
2782  queue->read_task =
2783  GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read_kx, queue);
2784  return;
2785  }
2786  /* we got all the data, let's find out who we are talking to! */
2788  queue->cread_buf,
2789  queue);
2790  if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, queue->cread_buf))
2791  {
2793  "Invalid TCP KX received from %s\n",
2794  GNUNET_a2s (queue->address, queue->address_len));
2795  queue_destroy (queue);
2796  return;
2797  }
2798  if (0 !=
2799  memcmp (&tc.sender, &queue->target, sizeof(struct GNUNET_PeerIdentity)))
2800  {
2802  "Invalid sender in TCP KX received from %s\n",
2803  GNUNET_a2s (queue->address, queue->address_len));
2804  queue_destroy (queue);
2805  return;
2806  }
2807  send_challenge (tc, queue);
2808  /* update queue timeout */
2809  reschedule_queue_timeout (queue);
2810  /* prepare to continue with regular read task immediately */
2811  memmove (queue->cread_buf,
2812  &queue->cread_buf[INITIAL_KX_SIZE],
2813  queue->cread_off - (INITIAL_KX_SIZE));
2814  queue->cread_off -= INITIAL_KX_SIZE;
2815  if (0 < queue->cread_off)
2816  queue->read_task = GNUNET_SCHEDULER_add_now (&queue_read, queue);
2817 }
2818 
2837 static int
2838 mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
2839 {
2840  struct Queue *queue;
2841  const char *path;
2842  struct sockaddr *in;
2843  socklen_t in_len = 0;
2844  struct GNUNET_NETWORK_Handle *sock;
2845 
2847  "Connecting to %s\n", address);
2848  if (0 != strncmp (address,
2850  strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
2851  {
2852  GNUNET_break_op (0);
2853  return GNUNET_SYSERR;
2854  }
2855  path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
2856  in = tcp_address_to_sockaddr (path, &in_len);
2857 
2858  if (NULL == in)
2859  {
2861  "Failed to setup TCP socket address\n");
2862  return GNUNET_SYSERR;
2863  }
2864 
2866  "in %s\n",
2867  GNUNET_a2s (in, in_len));
2868 
2869  sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP);
2870  if (NULL == sock)
2871  {
2873  "socket(%d) failed: %s",
2874  in->sa_family,
2875  strerror (errno));
2876  GNUNET_free (in);
2877  return GNUNET_SYSERR;
2878  }
2879  if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, in_len)) &&
2880  (errno != EINPROGRESS))
2881  {
2883  "connect to `%s' failed: %s",
2884  address,
2885  strerror (errno));
2887  GNUNET_free (in);
2888  return GNUNET_SYSERR;
2889  }
2890 
2891  queue = GNUNET_new (struct Queue);
2892  queue->target = *peer;
2893  queue->address = in;
2894  queue->address_len = in_len;
2895  queue->sock = sock;
2897  // queue->mq_awaits_continue = GNUNET_YES;
2898  queue->read_task =
2900  queue->sock,
2901  &queue_read_kx,
2902  queue);
2903 
2904 
2906  "start kx mq_init\n");
2907 
2908  start_initial_kx_out (queue);
2909  queue->write_task =
2911  queue->sock,
2912  &queue_write,
2913  queue);
2914  return GNUNET_OK;
2915 }
2916 
2925 static int
2926 get_lt_delete_it (void *cls,
2927  const struct GNUNET_HashCode *key,
2928  void *value)
2929 {
2930  struct ListenTask *lt = value;
2931 
2932  (void) cls;
2933  (void) key;
2934  if (NULL != lt->listen_task)
2935  {
2937  lt->listen_task = NULL;
2938  }
2939  if (NULL != lt->listen_sock)
2940  {
2942  lt->listen_sock = NULL;
2943  }
2944  return GNUNET_OK;
2945 }
2946 
2955 static int
2957  const struct GNUNET_PeerIdentity *target,
2958  void *value)
2959 {
2960  struct Queue *queue = value;
2961 
2962  (void) cls;
2963  (void) target;
2964  queue_destroy (queue);
2965  return GNUNET_OK;
2966 }
2967 
2973 static void
2974 do_shutdown (void *cls)
2975 {
2977  "Shutdown %s!\n",
2978  shutdown_running ? "running" : "not running");
2979 
2981  return;
2982  else
2984 
2985  while (NULL != proto_head)
2986  free_proto_queue (proto_head);
2987  if (NULL != nat)
2988  {
2989  GNUNET_NAT_unregister (nat);
2990  nat = NULL;
2991  }
2996  if (NULL != ch)
2997  {
2999  ch = NULL;
3000  }
3001  if (NULL != stats)
3002  {
3004  stats = NULL;
3005  }
3006  if (NULL != my_private_key)
3007  {
3008  GNUNET_free (my_private_key);
3009  my_private_key = NULL;
3010  }
3011  if (NULL != is)
3012  {
3014  is = NULL;
3015  }
3016  if (NULL != peerstore)
3017  {
3019  peerstore = NULL;
3020  }
3021  if (NULL != resolve_request_handle)
3022  {
3023  GNUNET_RESOLVER_request_cancel (resolve_request_handle);
3024  resolve_request_handle = NULL;
3025  }
3027  "Shutdown done!\n");
3028 }
3029 
3030 
3042 static void
3043 enc_notify_cb (void *cls,
3044  const struct GNUNET_PeerIdentity *sender,
3045  const struct GNUNET_MessageHeader *msg)
3046 {
3047  (void) cls;
3048  (void) sender;
3049  (void) msg;
3050  GNUNET_break_op (0);
3051 }
3052 
3053 
3067 static void
3068 nat_address_cb (void *cls,
3069  void **app_ctx,
3070  int add_remove,
3072  const struct sockaddr *addr,
3073  socklen_t addrlen)
3074 {
3075  char *my_addr;
3077 
3079  "nat address cb %s %s\n",
3080  add_remove ? "add" : "remove",
3081  GNUNET_a2s (addr, addrlen));
3082 
3083  if (GNUNET_YES == add_remove)
3084  {
3085  enum GNUNET_NetworkType nt;
3086 
3087  GNUNET_asprintf (&my_addr,
3088  "%s-%s",
3090  GNUNET_a2s (addr, addrlen));
3091  nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
3092  ai =
3094  my_addr,
3095  nt,
3097  GNUNET_free (my_addr);
3098  *app_ctx = ai;
3099  }
3100  else
3101  {
3102  ai = *app_ctx;
3104  *app_ctx = NULL;
3105  }
3106 }
3107 
3111 static void
3112 add_addr (struct sockaddr *in, socklen_t in_len)
3113 {
3114 
3115  struct Addresses *saddrs;
3116 
3118  "add address %s\n",
3119  GNUNET_a2s (in, in_len));
3120 
3121  saddrs = GNUNET_new (struct Addresses);
3122  saddrs->addr = in;
3123  saddrs->addr_len = in_len;
3124  GNUNET_CONTAINER_DLL_insert (addrs_head, addrs_tail, saddrs);
3125 
3127  "after add address %s\n",
3128  GNUNET_a2s (in, in_len));
3129 
3131  "add address %s\n",
3132  GNUNET_a2s (saddrs->addr, saddrs->addr_len));
3133 
3134  addrs_lens++;
3135 }
3136 
3144 static int
3145 init_socket (struct sockaddr *addr,
3146  socklen_t in_len)
3147 {
3148  struct sockaddr_storage in_sto;
3149  socklen_t sto_len;
3150  struct GNUNET_NETWORK_Handle *listen_sock;
3151  struct ListenTask *lt;
3152  struct GNUNET_HashCode h_sock;
3153 
3154  if (NULL == addr)
3155  {
3157  "Address is NULL.\n");
3158  return GNUNET_SYSERR;
3159  }
3160 
3162  "address %s\n",
3163  GNUNET_a2s (addr, in_len));
3164 
3165  listen_sock =
3166  GNUNET_NETWORK_socket_create (addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
3167  if (NULL == listen_sock)
3168  {
3170  return GNUNET_SYSERR;
3171  }
3172 
3173  if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, addr, in_len))
3174  {
3176  GNUNET_NETWORK_socket_close (listen_sock);
3177  listen_sock = NULL;
3178  return GNUNET_SYSERR;
3179  }
3180 
3181  if (GNUNET_OK !=
3182  GNUNET_NETWORK_socket_listen (listen_sock,
3183  5))
3184  {
3186  "listen");
3187  GNUNET_NETWORK_socket_close (listen_sock);
3188  listen_sock = NULL;
3189  return GNUNET_SYSERR;
3190  }
3191 
3192  /* We might have bound to port 0, allowing the OS to figure it out;
3193  thus, get the real IN-address from the socket */
3194  sto_len = sizeof(in_sto);
3195 
3196  if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock),
3197  (struct sockaddr *) &in_sto,
3198  &sto_len))
3199  {
3200  memcpy (&in_sto, addr, in_len);
3201  sto_len = in_len;
3202  }
3203 
3204  // addr = (struct sockaddr *) &in_sto;
3205  in_len = sto_len;
3207  "Bound to `%s'\n",
3208  GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
3209  stats = GNUNET_STATISTICS_create ("C-TCP", cfg);
3210 
3211  if (NULL == is)
3212  is = GNUNET_NT_scanner_init ();
3213 
3214  if (NULL == my_private_key)
3216  if (NULL == my_private_key)
3217  {
3218  GNUNET_log (
3220  _ (
3221  "Transport service is lacking key configuration settings. Exiting.\n"));
3222  if (NULL != resolve_request_handle)
3223  GNUNET_RESOLVER_request_cancel (resolve_request_handle);
3225  return GNUNET_SYSERR;
3226  }
3228  /* start listening */
3229 
3230  lt = GNUNET_new (struct ListenTask);
3231  lt->listen_sock = listen_sock;
3232 
3234  listen_sock,
3235  &listen_cb,
3236  lt);
3237 
3239  "creating hash\n");
3241  sizeof(lt->listen_sock),
3242  &h_sock);
3243 
3245  "creating map\n");
3246  if (NULL == lt_map)
3248 
3250  "creating map entry\n");
3252  &h_sock,
3253  lt,
3255 
3257  "map entry created\n");
3258 
3259  if (NULL == queue_map)
3261 
3262  if (NULL == ch )
3267  &mq_init,
3268  NULL,
3269  &enc_notify_cb,
3270  NULL);
3271 
3272  if (NULL == ch)
3273  {
3274  GNUNET_break (0);
3275  if (NULL != resolve_request_handle)
3276  GNUNET_RESOLVER_request_cancel (resolve_request_handle);
3278  return GNUNET_SYSERR;
3279  }
3280 
3281  add_addr (addr, in_len);
3282  return GNUNET_OK;
3283 
3284 }
3285 
3289 static void
3291 {
3292 
3293  struct sockaddr **saddrs;
3294  socklen_t *saddr_lens;
3295  int i;
3296  struct Addresses *pos;
3297 
3298 
3300  "starting nat register!\n");
3301 
3302  i = 0;
3303  saddrs = GNUNET_malloc ((addrs_lens + 1) * sizeof(struct sockaddr *));
3304 
3305  saddr_lens = GNUNET_malloc ((addrs_lens + 1) * sizeof(socklen_t));
3306 
3307  for (pos = addrs_head; NULL != pos; pos = pos->next)
3308  {
3309 
3311  "registering address %s\n",
3312  GNUNET_a2s (addrs_head->addr, addrs_head->addr_len));
3313 
3314  saddr_lens[i] = addrs_head->addr_len;
3315  saddrs[i] = GNUNET_memdup (addrs_head->addr, saddr_lens[i]);
3316 
3317  i++;
3318 
3319  }
3320 
3321  nat = GNUNET_NAT_register (cfg,
3323  IPPROTO_TCP,
3324  addrs_lens,
3325  (const struct sockaddr **) saddrs,
3326  saddr_lens,
3327  &nat_address_cb,
3328  NULL /* FIXME: support reversal: #5529 */,
3329  NULL /* closure */);
3330 
3331  i = 0;
3332 
3333  for (i = addrs_lens - 1; i >= 0; i--)
3334  GNUNET_free (saddrs[i]);
3335  GNUNET_free (saddrs);
3336  GNUNET_free (saddr_lens);
3337 
3338  if (NULL == nat)
3339  {
3340  GNUNET_break (0);
3341  if (NULL != resolve_request_handle)
3342  GNUNET_RESOLVER_request_cancel (resolve_request_handle);
3344  }
3345 }
3346 
3354 static void
3356  const struct sockaddr *addr,
3357  socklen_t in_len)
3358 {
3359  struct sockaddr_in *v4;
3360  struct sockaddr_in6 *v6;
3361  struct sockaddr *in;
3362 
3363  (void) cls;
3364  if (NULL != addr)
3365  {
3366  if (AF_INET == addr->sa_family)
3367  {
3368  v4 = (struct sockaddr_in *) addr;
3369  in = tcp_address_to_sockaddr_numeric_v4 (&in_len, *v4, bind_port);// _global);
3370  }
3371  else if (AF_INET6 == addr->sa_family)
3372  {
3373  v6 = (struct sockaddr_in6 *) addr;
3374  in = tcp_address_to_sockaddr_numeric_v6 (&in_len, *v6, bind_port);// _global);
3375  }
3376  else
3377  {
3379  "Address family %u not suitable (not AF_INET %u nor AF_INET6 %u \n",
3380  addr->sa_family,
3381  AF_INET,
3382  AF_INET6);
3383  return;
3384  }
3385  init_socket (in, in_len);
3386  }
3387  else
3388  {
3390  "Address is NULL. This might be an error or the resolver finished resolving.\n");
3391  if (NULL == addrs_head)
3392  {
3394  "Resolver finished resolving, but we do not listen to an address!.\n");
3395  return;
3396  }
3397  nat_register ();
3398 
3399  }
3400 }
3401 
3410 static void
3411 run (void *cls,
3412  char *const *args,
3413  const char *cfgfile,
3414  const struct GNUNET_CONFIGURATION_Handle *c)
3415 {
3416  char *bindto;
3417  struct sockaddr *in;
3418  socklen_t in_len;
3419  struct sockaddr_in v4;
3420  struct sockaddr_in6 v6;
3421  char *start;
3422  unsigned int port;
3423  char dummy[2];
3424  char *rest = NULL;
3425  struct PortOnlyIpv4Ipv6 *po;
3426  socklen_t addr_len_ipv4;
3427  socklen_t addr_len_ipv6;
3428 
3429  (void) cls;
3430  cfg = c;
3431  if (GNUNET_OK !=
3434  "BINDTO",
3435  &bindto))
3436  {
3439  "BINDTO");
3440  return;
3441  }
3442  if (GNUNET_OK !=
3445  "MAX_QUEUE_LENGTH",
3446  &max_queue_length))
3448  if (GNUNET_OK !=
3451  "REKEY_INTERVAL",
3452  &rekey_interval))
3454 
3455  peerstore = GNUNET_PEERSTORE_connect (cfg);
3456  if (NULL == peerstore)
3457  {
3458  GNUNET_break (0);
3460  return;
3461  }
3462 
3464 
3465  if (1 == sscanf (bindto, "%u%1s", &bind_port, dummy))
3466  {
3468 
3469  addr_len_ipv4 = po->addr_len_ipv4;
3470 
3471 
3473  "address po %s\n",
3474  GNUNET_a2s (po->addr_ipv4, addr_len_ipv4));
3475 
3476  if (NULL != po->addr_ipv4)
3477  {
3478  init_socket (po->addr_ipv4, addr_len_ipv4);
3479  }
3480 
3481  if (NULL != po->addr_ipv6)
3482  {
3483  addr_len_ipv6 = po->addr_len_ipv6;
3484  init_socket (po->addr_ipv6, addr_len_ipv6);
3485  }
3486 
3487  GNUNET_free (po);
3488  nat_register ();
3489  GNUNET_free (bindto);
3490 
3491  return;
3492  }
3493 
3494  start = extract_address (bindto);
3495 
3496  if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
3497  {
3498  bind_port = extract_port (bindto);
3499 
3500  in = tcp_address_to_sockaddr_numeric_v4 (&in_len, v4, bind_port);
3501  init_socket (in, in_len);
3502  nat_register ();
3503  GNUNET_free (bindto);
3504 
3505  return;
3506  }
3507 
3508  if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
3509  {
3510  bind_port = extract_port (bindto);
3511  in = tcp_address_to_sockaddr_numeric_v6 (&in_len, v6, bind_port);
3512  init_socket (in, in_len);
3513  nat_register ();
3514  GNUNET_free (bindto);
3515 
3516  return;
3517  }
3518 
3519 
3520  bind_port = extract_port (bindto);
3521 
3522  resolve_request_handle = GNUNET_RESOLVER_ip_get (strtok_r (bindto, ":",
3523  &rest),
3524  AF_UNSPEC,
3527  &port);
3528  GNUNET_free (bindto);
3529 }
3530 
3531 
3539 int
3540 main (int argc, char *const *argv)
3541 {
3542  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
3544  };
3545  int ret;
3546 
3547  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
3548  return 2;
3549 
3550  ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc,
3551  argv,
3552  "gnunet-communicator-tcp",
3553  _ ("GNUnet TCP communicator"),
3554  options,
3555  &run,
3556  NULL))
3557  ? 0
3558  : 1;
3559  GNUNET_free_nz ((void *) argv);
3560  return ret;
3561 }
3562 
3563 
3564 /* end of gnunet-communicator-tcp.c */
#define GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE
Signature used by TCP communicator handshake,.
In case of port only configuration we like to bind to ipv4 and ipv6 addresses.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
int GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, int backlog)
Listen on a socket.
Definition: network.c:708
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...
#define COMMUNICATOR_CONFIG_SECTION
Configuration section used by the communicator.
static void queue(const char *hostname)
Add hostname to the list of requests to be made.
Handle to the interface scanner.
Definition: nt.c:110
int rekeyed
GNUNET_YES if we just rekeyed and must thus possibly re-decrypt ciphertext.
char ibuf[(sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)+sizeof(struct TCPConfirmation))]
Buffer for reading all the information we need to upgrade from protoqueue to queue.
static void listen_cb(void *cls)
We have been notified that our listen socket has something to read.
static int get_lt_delete_it(void *cls, const struct GNUNET_HashCode *key, void *value)
Iterator over all ListenTasks to clean up.
Transmission is reliabile (with ACKs), i.e.
int GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
Return file descriptor for this network handle.
Definition: network.c:1080
static void setup_in_cipher(const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral, struct Queue *queue)
Setup cipher of queue for decryption.
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...
char cread_buf[(2 *64 *1024+sizeof(struct TCPBox))]
Buffer for reading ciphertext from network into.
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.
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:314
static void done()
struct GNUNET_PEERSTORE_IterateContext * handshake_monotime_get
Iteration Context for retrieving the monotonic time send with the handshake.
struct sockaddr * addr_ipv4
Ipv4 address we like to bind to.
Handle for active NAT registrations.
Definition: nat_api.c:71
static void boot_queue(struct Queue *queue, enum GNUNET_TRANSPORT_ConnectionStatus cs)
Add the given queue to our internal data structure.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Purpose must be GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE_ACK.
int GNUNET_CONFIGURATION_get_value_time(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, struct GNUNET_TIME_Relative *time)
Get a configuration value that should be a relative time.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_PeerIdentity target
To whom are we talking to.
void GNUNET_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh)
Cancel a request that is still pending with the resolver.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:673
size_t unverified_size
Size of data received without KX challenge played back.
static void handshake_monotime_cb(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Callback called by peerstore when records for GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE w...
GNUNET_NAT_AddressClass
Some addresses contain sensitive information or are not suitable for global distribution.
int 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.
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...
uint64_t rel_value_us
The actual value.
int main(int argc, char *const *argv)
The main function for the UNIX communicator.
struct GNUNET_NETWORK_Handle * listen_sock
Listen socket.
#define GNUNET_CRYPTO_eddsa_sign(priv, ps, sig)
EdDSA sign a given block.
static struct GNUNET_NAT_Handle * nat
Connection to NAT service.
int finishing
Did we enqueue a finish message and are closing down the queue?
struct GNUNET_HashCode in_hmac
Shared secret for HMAC verification on incoming data.
struct GNUNET_PeerIdentity sender
Identity of the inititor of the TCP connection (TCP client).
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
socklen_t address_len
Length of the address.
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:816
static struct GNUNET_PeerIdentity my_identity
Our public key.
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
size_t cread_off
At which offset in the ciphertext read buffer should we append more ciphertext for transmission next...
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...
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Setup communicator and launch network interactions.
struct GNUNET_CRYPTO_EcdhePublicKey ephemeral
Ephemeral key used by the sender.
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&#39; this be the standard hmac function and the abov...
Definition: crypto_hash.c:420
struct GNUNET_PeerIdentity sender
Sender&#39;s identity.
Delete any previous values for the given key before storing the given value.
GNUNET_MQ_Error
Error codes for the queue.
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Random on unsigned 64-bit values.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_CONFIRMATION_ACK
TCP communicator confirmation ack.
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.
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, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1331
DLL to store the addresses we like to register at NAT service.
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender&#39;s signature of type GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE.
struct GNUNET_PEERSTORE_StoreContext * handshake_monotime_sc
Store Context for retrieving the monotonic time send with the handshake.
#define GNUNET_TIME_UNIT_MINUTES
One minute.
unsigned int backpressure
How may messages did we pass from this queue to CORE for which we have yet to receive an acknoweldgem...
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:787
unsigned int bind_port
The port the communicator should be assigned to.
struct GNUNET_SCHEDULER_Task * read_task
ID of read task for this connection.
#define GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY
Signature used by TCP communicator rekey.
int destroyed
Did we technically destroy this queue, but kept the allocation around because of backpressure not bei...
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
struct ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value...
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
size_t value_size
Size of value BLOB.
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1458
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:246
struct ListenTask * lts_tail
Head of DLL with ListenTask.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY
TCP communicator rekey message.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void proto_read_kx(void *cls)
Read from the socket of the proto queue until we have enough data to upgrade to full queue...
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition: scheduler.c:423
struct ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value...
uint64_t rekey_left_bytes
How many more bytes may we sent with the current out_cipher before we should rekey?
#define BUF_SIZE
Size of our IO buffers for ciphertext data.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_CONFIRMATION_ACK.
int 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:485
#define PROTO_QUEUE_TIMEOUT
How long do we wait until we must have received the initial KX?
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:1588
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static void pass_plaintext_to_core(struct Queue *queue, const void *plaintext, size_t plaintext_len)
We received plaintext_len bytes of plaintext on queue.
static struct PortOnlyIpv4Ipv6 * tcp_address_to_sockaddr_port_only(const char *bindto, unsigned int *port)
Convert TCP bind specification to a struct PortOnlyIpv4Ipv6 *
int 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.
struct GNUNET_TIME_Absolute rekey_time
Until what time may we sent with the current out_cipher before we should rekey?
int addrs_lens
Number of addresses in the DLL for register at NAT service.
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:939
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
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...
socklen_t addr_len_ipv4
Length of ipv4 address.
static struct GNUNET_NT_InterfaceScanner * is
Network scanner to determine network types.
struct GNUNET_CRYPTO_EcdhePrivateKey ephemeral
Our ephemeral key.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Definition of a command line option.
#define GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_REKEY
Key used to store sender&#39;s monotonic time from rekey message.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX
TCP communicator payload box.
#define GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE
Key used to store sender&#39;s monotonic time from handshake message.
struct GNUNET_RESOLVER_RequestHandle * resolve_request_handle
Handle for DNS lookup of bindto address.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
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.
struct GNUNET_SCHEDULER_Task * listen_task
ID of listen task.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
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_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:531
Handle for the service.
static void queue_read(void *cls)
Queue read task.
#define GNUNET_CRYPTO_eddsa_verify(purp, ps, sig, pub)
Verify EdDSA signature.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
static void rekey_monotime_store_cb(void *cls, int success)
Callback called when peerstore store operation for rekey monotime value is finished.
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
uint64_t abs_value_us
The actual value.
static struct GNUNET_PEERSTORE_Handle * peerstore
Database for peer&#39;s HELLOs.
struct GNUNET_MQ_Handle * mq
Message queue we are providing for the ch.
Internal representation of the hash map.
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
Definition: gnunet_nt_lib.h:35
Handle for an incoming connection where we do not yet have enough information to setup a full queue...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct Addresses * prev
Kept in a DLL.
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won&#39;t work on W32.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
static void inject_rekey(struct Queue *queue)
Inject a struct TCPRekey message into the queue&#39;s plaintext buffer.
struct GNUNET_PeerIdentity sender
Identity of the inititor of the TCP connection (TCP client).
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
A 256-bit hashcode.
Opaque handle to the transport service for communicators.
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
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.
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
void GNUNET_TRANSPORT_communicator_disconnect(struct GNUNET_TRANSPORT_CommunicatorHandle *ch)
Disconnect from the transport service.
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.
Signature we use to verify that the ack from the receiver of the ephemeral key was really send by the...
struct GNUNET_PeerIdentity sender
Identity of the inititor of the TCP connection (TCP client).
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:430
Signature we use to verify that the ephemeral key was really chosen by the specified sender...
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
static struct GNUNET_TIME_Relative rekey_interval
The rekey interval.
static unsigned long long max_queue_length
Maximum queue length before we stop reading towards the transport service.
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 INITIAL_KX_SIZE
Size of the initial key exchange message sent first in both directions.
struct GNUNET_HashCode out_hmac
Shared secret for HMAC generation on outgoing data, ratcheted after each operation.
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
void GNUNET_CRYPTO_ecdhe_key_create(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
Create a new private key.
Definition: crypto_ecc.c:508
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
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 (!) ...
int 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:658
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
unsigned long long bytes_in_queue
Number of bytes we currently have in our write queue.
static struct GNUNET_CONTAINER_MultiPeerMap * queue_map
Queues (map from peer identity to struct Queue)
struct GNUNET_PEERSTORE_IterateContext * rekey_monotime_get
Iteration Context for retrieving the monotonic time send with key for rekeying.
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 *`.
struct GNUNET_TIME_AbsoluteNBO handshake_ack_monotonic_time
Monotonic time value for handshake ack message.
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
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:565
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...
void * value
Record value BLOB.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
int 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:1024
static char * value
Value of the record to add/remove.
const char * GNUNET_p2s(const struct GNUNET_CRYPTO_EddsaPublicKey *p)
Convert a public key value to a string (for printing debug messages).
Handle for a queue.
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:48
void GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h, int sync_first)
Disconnect from the PEERSTORE service.
Context for a store request.
Definition: peerstore_api.c:96
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.
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:1296
size_t cwrite_off
At which offset in the ciphertext write buffer should we append more ciphertext from reading next...
#define COMMUNICATOR_ADDRESS_PREFIX
Address prefix used by the communicator.
#define GNUNET_MIN(a, b)
Definition: gnunet_common.h:93
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:702
#define GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE_ACK
Signature by a peer sending back the nonce received at initial handshake.
Randomness for IVs etc.
static char buf[2048]
static struct GNUNET_CRYPTO_EddsaPrivateKey * my_private_key
Our private key.
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.
void GNUNET_PEERSTORE_store_cancel(struct GNUNET_PEERSTORE_StoreContext *sc)
Cancel a store request.
struct GNUNET_PEERSTORE_StoreContext * rekey_monotime_sc
Store Context for retrieving the monotonic time send with key for rekeying.
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.
this is an inbound connection (communicator initiated)
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
static void mq_destroy(struct GNUNET_MQ_Handle *mq, void *impl_state)
Signature of functions implementing the destruction of a message queue.
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender&#39;s signature of type GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE_ACK.
static void reschedule_queue_timeout(struct Queue *queue)
Increment queue timeout due to activity.
static void queue_finish(struct Queue *queue)
Append a &#39;finish&#39; message to the outgoing transmission.
const char * GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string (for printing debug messages)...
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH.
struct GNUNET_NETWORK_Handle * sock
socket that we transmit all data with on this queue
struct GNUNET_TIME_Absolute timeout
Timeout for this protoqueue.
struct sockaddr * addr_ipv6
Ipv6 address we like to bind to.
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
Internal representation of the hash map.
struct GNUNET_SCHEDULER_Task * write_task
ID of write task for this connection.
struct sockaddr * address
Address of the other peer.
struct Addresses * addrs_head
Head of DLL with addresses we like to register at NAT servcie.
Handle to the PEERSTORE service.
Definition: peerstore_api.c:40
void GNUNET_STATISTICS_set(struct GNUNET_STATISTICS_Handle *handle, const char *name, uint64_t value, int make_persistent)
Set statistic value for the peer.
A 512-bit hashcode.
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender&#39;s signature of type GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH
TCP communicator end of stream.
struct ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value...
int 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:1084
char cwrite_buf[(2 *64 *1024+sizeof(struct TCPBox))]
buffer for writing ciphertext to network.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
static void do_rekey(struct Queue *queue, const struct TCPRekey *rekey)
Handle rekey message on queue.
char pwrite_buf[UINT16_MAX+1+sizeof(struct TCPBox)]
Plaintext buffer for messages to be encrypted.
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
void GNUNET_NT_scanner_done(struct GNUNET_NT_InterfaceScanner *is)
Terminate interface scanner.
Definition: nt.c:433
static struct sockaddr * tcp_address_to_sockaddr(const char *bindto, socklen_t *sock_len)
Convert TCP bind specification to a struct sockaddr *
int 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.
static void mq_cancel(struct GNUNET_MQ_Handle *mq, void *impl_state)
Implementation function that cancels the currently sent message.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
void GNUNET_log_from_nocheck(enum GNUNET_ErrorType kind, const char *comp, const char *message,...)
Log function that specifies an alternative component.
struct ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value...
Private ECC key encoded for transmission.
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:692
#define GNUNET_free_nz(ptr)
Wrapper around free.
struct GNUNET_CRYPTO_EcdhePublicKey ephemeral
New ephemeral key.
size_t pread_off
At which offset in the plaintext input buffer should we append more plaintext from decryption next...
struct Addresses * next
Kept in a DLL.
int mq_awaits_continue
Is MQ awaiting a GNUNET_MQ_impl_send_continue() call?
struct GNUNET_NETWORK_Handle * sock
socket that we transmit all data with on this queue
There must only be one value per key; storing a value should fail if a value under the same key alrea...
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...
static void handshake_monotime_store_cb(void *cls, int success)
Callback called when peerstore store operation for handshake monotime is finished.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
struct GNUNET_HashCode key
The key used in the DHT.
static unsigned int size
Size of the "table".
Definition: peer.c:67
static struct GNUNET_TRANSPORT_AddressIdentifier * ai
Handle to the operation that publishes our address.
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:215
struct GNUNET_PeerIdentity sender
Sender&#39;s identity.
struct GNUNET_PEERSTORE_IterateContext * handshake_ack_monotime_get
Iteration Context for retrieving the monotonic time send with the handshake ack.
static int get_queue_delete_it(void *cls, const struct GNUNET_PeerIdentity *target, void *value)
Iterator over all message queues to clean up.
struct GNUNET_PEERSTORE_Handle * GNUNET_PEERSTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the PEERSTORE service.
Handle to a request given to the resolver.
Definition: resolver_api.c:102
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 char * extract_address(const char *bindto)
This Method extracts the address part of the BINDTO string.
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:378
static struct GNUNET_MQ_Envelope * ac
Handle to current GNUNET_PEERINFO_add_peer() operation.
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. ...
struct GNUNET_NT_InterfaceScanner * GNUNET_NT_scanner_init(void)
Initialize the address characterization client handle.
Definition: nt.c:412
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 ChallengeNonceP challenge
Challenge value used to protect against replay attack, if there is no stored monotonic time value...
static struct ProtoQueue * proto_tail
Protoqueues DLL tail.
struct GNUNET_PeerIdentity receiver
Presumed identity of the target of the TCP connection (TCP server)
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_NETWORK_Handle * listen_sock
Listen socket.
#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...
int 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.
static struct GNUNET_CONTAINER_MultiHashMap * lt_map
ListenTasks (map from socket to struct ListenTask)
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:807
Type of a nonce used for challenges.
struct Addresses * addrs_tail
Head of DLL with addresses we like to register at NAT servcie.
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.
TCP message box.
static void start_initial_kx_out(struct Queue *queue)
Initialize our key material for outgoing transmissions and inform the other peer about it...
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
an ECC signature using EdDSA.
struct GNUNET_PeerIdentity receiver
Presumed identity of the target of the TCP connection (TCP server)
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:193
struct GNUNET_TIME_AbsoluteNBO rekey_monotonic_time
Monotonic time value for rekey message.
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
#define REKEY_MAX_BYTES
How often do we rekey based on number of bytes transmitted? (additionally randomized).
Allow multiple values with the same key.
#define GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
After how long do we consider a connection to a peer dead if we don&#39;t receive messages from the peer...
Handle to a message queue.
Definition: mq.c:85
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.
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)
socklen_t address_len
Length of the address.
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won&#39;t work on W32;.
Private ECC key encoded for transmission.
int GNUNET_CONTAINER_multipeermap_iterate(struct GNUNET_CONTAINER_MultiPeerMap *map, GNUNET_CONTAINER_PeerMapIterator it, void *it_cls)
Iterate over all entries in the map.
static void nat_register()
This method reads from the DLL addrs_head to register them at the NAT service.
int shutdown_running
A flag indicating we are already doing a shutdown.
The identity of the host (wraps the signing key of the peer).
static void free_proto_queue(struct ProtoQueue *pq)
Closes socket and frees memory associated with pq.
Ack for the encrypted continuation of TCP initial handshake.
struct sockaddr * address
Address of the other peer.
size_t ibuf_off
Current offset for reading into ibuf.
#define INITIAL_CORE_KX_SIZE
Size of the initial core key exchange messages.
static void setup_out_cipher(struct Queue *queue)
Setup cipher for outgoing data stream based on target and our ephemeral private key.
#define GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED
Queue length.
TCP rekey message box.
static uint16_t port
Port number.
Definition: gnunet-bcd.c:82
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
struct sockaddr * addr
Address we like to register at NAT service.
configuration data
Definition: configuration.c:84
struct GNUNET_TIME_AbsoluteNBO handshake_monotonic_time
Monotonic time value for handshake message.
this is an outbound connection (transport initiated)
struct GNUNET_CRYPTO_EcdhePublicKey ephemeral
Ephemeral key used by the sender.
struct GNUNET_PEERSTORE_StoreContext * handshake_ack_monotime_sc
Store Context for retrieving the monotonic time send with the handshake ack.
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_STATISTICS_Handle * stats
For logging statistics.
enum GNUNET_TRANSPORT_ConnectionStatus cs
Connection status for this queue.
struct GNUNET_SCHEDULER_Task * read_task
ID of read task for this connection.
Encrypted continuation of TCP initial handshake.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
Single PEERSTORE record.
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
#define ADDRESS_VALIDITY_PERIOD
How long do we believe our addresses to remain up (before the other peer should revalidate).
size_t pwrite_off
At which offset in the plaintext output buffer should we append more plaintext for encryption next...
#define GNUNET_log(kind,...)
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 ...
Entry in list of pending tasks.
Definition: scheduler.c:134
static void send_challenge(struct TCPConfirmation tc, struct Queue *queue)
Sending challenge with TcpConfirmationAck back to sender of ephemeral key.
struct ProtoQueue * next
Kept in a DLL.
struct ListenTask * lts_head
Head of DLL with ListenTasks.
void GNUNET_PEERSTORE_iterate_cancel(struct GNUNET_PEERSTORE_IterateContext *ic)
Cancel an iterate request Please do not call after the iterate request is done.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY.
static unsigned int extract_port(const char *addr_and_port)
This Method extracts the port part of the BINDTO string.
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX.
handle to a socket
Definition: network.c:52
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
void GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
Fill block with a random values.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
int 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, parse options).
Definition: program.c:367
struct ProtoQueue * prev
Kept in a DLL.
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 size_t try_handle_plaintext(struct Queue *queue)
Test if we have received a full message in plaintext.
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&#39;s key from the file specified in the configuration...
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:331
socklen_t addr_len
Length of address we like to register at NAT service.
socklen_t addr_len_ipv6
Length of ipv6 address.
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 *`.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
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.
gcry_cipher_hd_t in_cipher
cipher for decryption of incoming data.
Header for all communications.
Time for absolute times used by GNUnet, in microseconds.
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:1517
GNUNET_TRANSPORT_ConnectionStatus
Possible states of a connection.
struct GNUNET_NETWORK_Handle * listen_sock
Listen socket.
enum GNUNET_NetworkType nt
Which network type does this queue use?
unsigned int GNUNET_CONTAINER_multipeermap_size(const struct GNUNET_CONTAINER_MultiPeerMap *map)
Get the number of key-value pairs in the map.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Purpose must be GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE.
struct GNUNET_TIME_Absolute timeout
Timeout for this queue.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Purpose must be GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY.
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:757
static void do_shutdown(void *cls)
Shutdown the UNIX communicator.
const char * GNUNET_e2s(const struct GNUNET_CRYPTO_EcdhePublicKey *p)
Convert a public key value to a string (for printing debug messages).
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
#define DEFAULT_REKEY_INTERVAL
How often do we rekey based on time (at least)
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 struct ProtoQueue * proto_head
Protoqueues DLL head.
int 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".
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
static struct GNUNET_TRANSPORT_CommunicatorHandle * ch
Our environment.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
#define GNUNET_PEERSTORE_TRANSPORT_TCP_COMMUNICATOR_HANDSHAKE_ACK
Key used to store sender&#39;s monotonic time from handshake ack message.
struct GNUNET_TRANSPORT_QueueHandle * qh
handle for this queue with the ch.
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:657
static char * address
GNS address for this phone.
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:499
gcry_cipher_hd_t out_cipher
cipher for encryption of outgoing data.
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...
int 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:90
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
Handle returned to identify the internal data structure the transport API has created to manage a mes...
char pread_buf[UINT16_MAX+1+sizeof(struct TCPBox)]
Plaintext buffer for decrypted plaintext.
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:560
Signature we use to verify that the ephemeral key was really chosen by the specified sender...
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
Struct to use as closure.
#define GNUNET_malloc(size)
Wrapper around malloc.
int GNUNET_NETWORK_test_pf(int pf)
Test if the given protocol family is supported by this system.
Definition: network.c:85
Context for a iterate request.
Internal representation of an address a communicator is currently providing for the transport service...
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
struct GNUNET_CRYPTO_EddsaPublicKey public_key
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:912
static void queue_write(void *cls)
We have been notified that our socket is ready to write.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972