GNUnet  0.10.x
gnunet-communicator-udp.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 
40 #include "platform.h"
41 #include "gnunet_util_lib.h"
42 #include "gnunet_protocols.h"
43 #include "gnunet_signatures.h"
44 #include "gnunet_constants.h"
45 #include "gnunet_nt_lib.h"
46 #include "gnunet_nat_service.h"
50 
54 #define REKEY_TIME_INTERVAL GNUNET_TIME_UNIT_DAYS
55 
59 #define PROTO_QUEUE_TIMEOUT GNUNET_TIME_UNIT_MINUTES
60 
64 #define BROADCAST_FREQUENCY GNUNET_TIME_UNIT_MINUTES
65 
69 #define INTERFACE_SCAN_FREQUENCY \
70  GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
71 
76 #define ADDRESS_VALIDITY_PERIOD GNUNET_TIME_UNIT_HOURS
77 
81 #define AES_KEY_SIZE (256 / 8)
82 
86 #define AES_IV_SIZE (96 / 8)
87 
91 #define GCM_TAG_SIZE (128 / 8)
92 
102 #define KCN_THRESHOLD 92
103 
110 #define KCN_TARGET 128
111 
121 #define MAX_SQN_DELTA 160
122 
132 #define MAX_SECRETS 128
133 
138 #define REKEY_MAX_BYTES (1024LLU * 1024 * 1024 * 4LLU)
139 
144 #define COMMUNICATOR_ADDRESS_PREFIX "udp"
145 
149 #define COMMUNICATOR_CONFIG_SECTION "communicator-udp"
150 
152 
153 
164 
169 
174 
179 
185 };
186 
187 
192 struct InitialKX {
197 
202  char gcm_tag[GCM_TAG_SIZE];
203 };
204 
205 
215 
219  struct GNUNET_CRYPTO_EddsaSignature sender_sig;
220 
225  struct GNUNET_TIME_AbsoluteNBO monotonic_time;
226 
227  /* followed by messages */
228 
229  /* padding may follow actual messages */
230 };
231 
232 
237 struct UDPAck {
241  struct GNUNET_MessageHeader header;
242 
247  uint32_t sequence_max GNUNET_PACKED;
248 
252  struct GNUNET_HashCode cmac;
253 };
254 
255 
269 
274 
278  struct GNUNET_HashCode h_address;
279 };
280 
281 
288 struct UDPBroadcast {
293 
298  struct GNUNET_CRYPTO_EddsaSignature sender_sig;
299 };
300 
301 
306 struct UDPBox {
315 
323  char gcm_tag[GCM_TAG_SIZE];
324 };
325 
326 
328 
332 struct SharedSecret;
333 
334 
344 
349 
358 
362  struct SharedSecret *ss;
363 
367  uint32_t sequence_number;
368 };
369 
370 
375 struct SenderAddress;
376 
381 struct ReceiverAddress;
382 
386 struct SharedSecret {
391 
396 
401 
406 
411 
416 
420  struct GNUNET_HashCode master;
421 
425  struct GNUNET_HashCode cmac;
426 
431  uint32_t sequence_used;
432 
439 
443  unsigned int active_kce_count;
444 };
445 
446 
455  struct GNUNET_PeerIdentity target;
456 
461 
466 
471 
475  struct sockaddr *address;
476 
480  socklen_t address_len;
481 
486 
490  unsigned int num_secrets;
491 
496 };
497 
498 
507  struct GNUNET_PeerIdentity target;
508 
513 
518 
524 
528  struct sockaddr *address;
529 
533  socklen_t address_len;
534 
539 
544 
549 
554 
558  size_t mtu;
559 
563  unsigned int num_secrets;
564 
569  unsigned int acks_available;
570 
575 };
576 
577 
586 
591 
596 
600  struct sockaddr *sa;
601 
605  struct sockaddr *ba;
606 
610  struct UDPBroadcast bcm;
611 
616  struct ipv6_mreq mcreq;
617 
621  socklen_t salen;
622 
626  int found;
627 };
628 
629 
634 
639 
644 
649 
654 
659 
664 
669 
674 
679 
684 
689 
694 
698 static int have_v6_socket;
699 
704 
709 
713 static const struct GNUNET_CONFIGURATION_Handle *cfg;
714 
719 
724 
728 static struct GNUNET_NAT_Handle *nat;
729 
733 static uint16_t my_port;
734 
735 
741 static void
743 {
744  if (AF_INET6 == bi->sa->sa_family)
745  {
746  /* Leave the multicast group */
748  IPPROTO_IPV6,
749  IPV6_LEAVE_GROUP,
750  &bi->mcreq,
751  sizeof(bi->mcreq)))
752  {
754  }
755  }
756  GNUNET_CONTAINER_DLL_remove(bi_head, bi_tail, bi);
758  GNUNET_free(bi->sa);
760  GNUNET_free(bi);
761 }
762 
763 
769 static void
771 {
772  struct GNUNET_MQ_Handle *mq;
773 
775  "Disconnecting receiver for peer `%s'\n",
776  GNUNET_i2s(&receiver->target));
777  if (NULL != (mq = receiver->mq))
778  {
779  receiver->mq = NULL;
780  GNUNET_MQ_destroy(mq);
781  }
782  if (NULL != receiver->qh)
783  {
785  receiver->qh = NULL;
786  }
789  &receiver->target,
790  receiver));
791  GNUNET_assert(receiver == GNUNET_CONTAINER_heap_remove_node(receiver->hn));
792  GNUNET_STATISTICS_set(stats,
793  "# receivers active",
795  GNUNET_NO);
796  GNUNET_free(receiver->address);
797  GNUNET_free(receiver->foreign_addr);
798  GNUNET_free(receiver);
799 }
800 
801 
807 static void
809 {
810  struct SharedSecret *ss = kce->ss;
811 
812  ss->active_kce_count--;
815  &kce->kid,
816  kce));
817  GNUNET_free(kce);
818 }
819 
820 
828 static void
829 get_kid(const struct GNUNET_HashCode *msec,
830  uint32_t serial,
831  struct GNUNET_ShortHashCode *kid)
832 {
833  uint32_t sid = htonl(serial);
834 
835  GNUNET_CRYPTO_hkdf(kid,
836  sizeof(*kid),
837  GCRY_MD_SHA512,
838  GCRY_MD_SHA256,
839  &sid,
840  sizeof(sid),
841  msec,
842  sizeof(*msec),
843  "UDP-KID",
844  strlen("UDP-KID"),
845  NULL,
846  0);
847 }
848 
849 
856 static void
857 kce_generate(struct SharedSecret *ss, uint32_t seq)
858 {
859  struct KeyCacheEntry *kce;
860 
861  GNUNET_assert(0 < seq);
862  kce = GNUNET_new(struct KeyCacheEntry);
863  kce->ss = ss;
864  kce->sequence_number = seq;
865  get_kid(&ss->master, seq, &kce->kid);
867  ss->active_kce_count++;
869  key_cache,
870  &kce->kid,
871  kce,
873  GNUNET_STATISTICS_set(stats,
874  "# KIDs active",
876  GNUNET_NO);
877 }
878 
879 
885 static void
887 {
888  struct SenderAddress *sender;
889  struct ReceiverAddress *receiver;
890  struct KeyCacheEntry *kce;
891 
892  if (NULL != (sender = ss->sender))
893  {
894  GNUNET_CONTAINER_DLL_remove(sender->ss_head, sender->ss_tail, ss);
895  sender->num_secrets--;
896  }
897  if (NULL != (receiver = ss->receiver))
898  {
899  GNUNET_CONTAINER_DLL_remove(receiver->ss_head, receiver->ss_tail, ss);
900  receiver->num_secrets--;
901  receiver->acks_available -= (ss->sequence_allowed - ss->sequence_used);
902  }
903  while (NULL != (kce = ss->kce_head))
904  kce_destroy(kce);
905  GNUNET_STATISTICS_update(stats, "# Secrets active", -1, GNUNET_NO);
906  GNUNET_STATISTICS_set(stats,
907  "# KIDs active",
909  GNUNET_NO);
910  GNUNET_free(ss);
911 }
912 
913 
920 static void
922 {
924  GNUNET_YES ==
925  GNUNET_CONTAINER_multipeermap_remove(senders, &sender->target, sender));
927  GNUNET_STATISTICS_set(stats,
928  "# senders active",
930  GNUNET_NO);
931  GNUNET_free(sender->address);
932  GNUNET_free(sender);
933 }
934 
935 
944 static void
945 get_iv_key(const struct GNUNET_HashCode *msec,
946  uint32_t serial,
947  char key[AES_KEY_SIZE],
948  char iv[AES_IV_SIZE])
949 {
950  uint32_t sid = htonl(serial);
951  char res[AES_KEY_SIZE + AES_IV_SIZE];
952 
953  GNUNET_CRYPTO_hkdf(res,
954  sizeof(res),
955  GCRY_MD_SHA512,
956  GCRY_MD_SHA256,
957  &sid,
958  sizeof(sid),
959  msec,
960  sizeof(*msec),
961  "UDP-IV-KEY",
962  strlen("UDP-IV-KEY"),
963  NULL,
964  0);
965  memcpy(key, res, AES_KEY_SIZE);
966  memcpy(iv, &res[AES_KEY_SIZE], AES_IV_SIZE);
967 }
968 
969 
975 static void
977 {
978  sender->timeout =
981 }
982 
983 
989 static void
991 {
992  receiver->timeout =
995  receiver->timeout.abs_value_us);
996 }
997 
998 
1004 static void
1005 check_timeouts(void *cls)
1006 {
1007  struct GNUNET_TIME_Relative st;
1008  struct GNUNET_TIME_Relative rt;
1009  struct GNUNET_TIME_Relative delay;
1010  struct ReceiverAddress *receiver;
1011  struct SenderAddress *sender;
1012 
1013  (void)cls;
1014  timeout_task = NULL;
1016  while (NULL != (receiver = GNUNET_CONTAINER_heap_peek(receivers_heap)))
1017  {
1019  if (0 != rt.rel_value_us)
1020  break;
1021  receiver_destroy(receiver);
1022  }
1024  while (NULL != (sender = GNUNET_CONTAINER_heap_peek(senders_heap)))
1025  {
1027  if (0 != st.rel_value_us)
1028  break;
1029  sender_destroy(sender);
1030  }
1031  delay = GNUNET_TIME_relative_min(rt, st);
1032  if (delay.rel_value_us < GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1033  timeout_task = GNUNET_SCHEDULER_add_delayed(delay, &check_timeouts, NULL);
1034 }
1035 
1036 
1042 static void
1044 {
1045  GNUNET_CRYPTO_hkdf(&ss->cmac,
1046  sizeof(ss->cmac),
1047  GCRY_MD_SHA512,
1048  GCRY_MD_SHA256,
1049  "CMAC",
1050  strlen("CMAC"),
1051  &ss->master,
1052  sizeof(ss->master),
1053  "UDP-CMAC",
1054  strlen("UDP-CMAC"),
1055  NULL,
1056  0);
1057 }
1058 
1059 
1068 static void
1070  const void *plaintext,
1071  size_t plaintext_len)
1072 {
1073  const struct GNUNET_MessageHeader *hdr = plaintext;
1074 
1075  while (ntohs(hdr->size) < plaintext_len)
1076  {
1078  "# bytes given to core",
1079  ntohs(hdr->size),
1080  GNUNET_NO);
1081  (void)
1083  &sender->target,
1084  hdr,
1086  NULL /* no flow control possible */
1087  ,
1088  NULL);
1089  /* move on to next message, if any */
1090  plaintext_len -= ntohs(hdr->size);
1091  if (plaintext_len < sizeof(*hdr))
1092  break;
1093  hdr = plaintext + ntohs(hdr->size);
1094  }
1096  "# bytes padding discarded",
1097  plaintext_len,
1098  GNUNET_NO);
1099 }
1100 
1101 
1110 static void
1111 setup_cipher(const struct GNUNET_HashCode *msec,
1112  uint32_t serial,
1113  gcry_cipher_hd_t *cipher)
1114 {
1115  char key[AES_KEY_SIZE];
1116  char iv[AES_IV_SIZE];
1117 
1118  gcry_cipher_open(cipher,
1119  GCRY_CIPHER_AES256 /* low level: go for speed */,
1120  GCRY_CIPHER_MODE_GCM,
1121  0 /* flags */);
1122  get_iv_key(msec, serial, key, iv);
1123  gcry_cipher_setkey(*cipher, key, sizeof(key));
1124  gcry_cipher_setiv(*cipher, iv, sizeof(iv));
1125 }
1126 
1127 
1140 static int
1141 try_decrypt(const struct SharedSecret *ss,
1142  const char tag[GCM_TAG_SIZE],
1143  uint32_t serial,
1144  const char *in_buf,
1145  size_t in_buf_size,
1146  char *out_buf)
1147 {
1148  gcry_cipher_hd_t cipher;
1149 
1150  setup_cipher(&ss->master, serial, &cipher);
1151  GNUNET_assert(
1152  0 ==
1153  gcry_cipher_decrypt(cipher, out_buf, in_buf_size, in_buf, in_buf_size));
1154  if (0 != gcry_cipher_checktag(cipher, tag, GCM_TAG_SIZE))
1155  {
1156  gcry_cipher_close(cipher);
1158  "# AEAD authentication failures",
1159  1,
1160  GNUNET_NO);
1161  return GNUNET_SYSERR;
1162  }
1163  gcry_cipher_close(cipher);
1164  return GNUNET_OK;
1165 }
1166 
1167 
1174 static struct SharedSecret *
1176 {
1177  struct SharedSecret *ss;
1178 
1179  ss = GNUNET_new(struct SharedSecret);
1180  GNUNET_CRYPTO_eddsa_ecdh(my_private_key, ephemeral, &ss->master);
1181  return ss;
1182 }
1183 
1184 
1192 static struct SharedSecret *
1194  struct ReceiverAddress *receiver)
1195 {
1196  struct SharedSecret *ss;
1197 
1198  ss = GNUNET_new(struct SharedSecret);
1199  GNUNET_CRYPTO_ecdh_eddsa(ephemeral,
1200  &receiver->target.public_key,
1201  &ss->master);
1202  calculate_cmac(ss);
1203  ss->receiver = receiver;
1204  GNUNET_CONTAINER_DLL_insert(receiver->ss_head, receiver->ss_tail, ss);
1205  receiver->num_secrets++;
1206  GNUNET_STATISTICS_update(stats, "# Secrets active", 1, GNUNET_NO);
1207  return ss;
1208 }
1209 
1210 
1218 static void
1220 
1221 
1232 static int
1233 handle_ack(void *cls, const struct GNUNET_PeerIdentity *pid, void *value)
1234 {
1235  const struct UDPAck *ack = cls;
1236  struct ReceiverAddress *receiver = value;
1237 
1238  (void)pid;
1239  for (struct SharedSecret *ss = receiver->ss_head; NULL != ss; ss = ss->next)
1240  {
1241  if (0 == memcmp(&ack->cmac, &ss->cmac, sizeof(struct GNUNET_HashCode)))
1242  {
1243  uint32_t allowed;
1244 
1245  allowed = ntohl(ack->sequence_max);
1246 
1247  if (allowed > ss->sequence_allowed)
1248  {
1249  receiver->acks_available += (allowed - ss->sequence_allowed);
1250  if ((allowed - ss->sequence_allowed) == receiver->acks_available)
1251  {
1252  /* we just incremented from zero => MTU change! */
1253  setup_receiver_mq(receiver);
1254  }
1255  ss->sequence_allowed = allowed;
1256  /* move ss to head to avoid discarding it anytime soon! */
1257  GNUNET_CONTAINER_DLL_remove(receiver->ss_head, receiver->ss_tail, ss);
1258  GNUNET_CONTAINER_DLL_insert(receiver->ss_head, receiver->ss_tail, ss);
1259  }
1260  return GNUNET_NO;
1261  }
1262  }
1263  return GNUNET_YES;
1264 }
1265 
1266 
1275 static void
1277  const void *buf,
1278  size_t buf_size)
1279 {
1280  const struct GNUNET_MessageHeader *hdr =
1281  (const struct GNUNET_MessageHeader *)buf;
1282  const struct UDPAck *ack = (const struct UDPAck *)buf;
1283  uint16_t type;
1284 
1285  if (sizeof(*hdr) > buf_size)
1286  return; /* not even a header */
1287  if (ntohs(hdr->size) > buf_size)
1288  return; /* not even a header */
1289  type = ntohs(hdr->type);
1290  switch (type)
1291  {
1293  /* lookup master secret by 'cmac', then update sequence_max */
1295  &sender->target,
1296  &handle_ack,
1297  (void *)ack);
1298  /* There could be more messages after the ACK, handle those as well */
1299  buf += ntohs(hdr->size);
1300  buf_size -= ntohs(hdr->size);
1301  pass_plaintext_to_core(sender, buf, buf_size);
1302  break;
1303 
1305  /* skip padding */
1306  break;
1307 
1308  default:
1309  pass_plaintext_to_core(sender, buf, buf_size);
1310  }
1311 }
1312 
1313 
1322 static void
1324 {
1325  GNUNET_assert(NULL != ss->sender);
1326  /* drop ancient KeyCacheEntries */
1327  while ((NULL != ss->kce_head) &&
1328  (MAX_SQN_DELTA <
1330  kce_destroy(ss->kce_tail);
1331  if (ss->active_kce_count < KCN_THRESHOLD)
1332  {
1333  struct UDPAck ack;
1334 
1335  while (ss->active_kce_count < KCN_TARGET)
1336  kce_generate(ss, ++ss->sequence_allowed);
1338  ack.header.size = htons(sizeof(ack));
1339  ack.sequence_max = htonl(ss->sequence_allowed);
1340  ack.cmac = ss->cmac;
1342  &ss->sender->target,
1344  &ack.header);
1345  }
1346 }
1347 
1348 
1356 static void
1357 decrypt_box(const struct UDPBox *box,
1358  size_t box_len,
1359  struct KeyCacheEntry *kce)
1360 {
1361  struct SharedSecret *ss = kce->ss;
1362  char out_buf[box_len - sizeof(*box)];
1363 
1364  GNUNET_assert(NULL != ss->sender);
1365  if (GNUNET_OK != try_decrypt(ss,
1366  box->gcm_tag,
1367  kce->sequence_number,
1368  (const char *)&box[1],
1369  sizeof(out_buf),
1370  out_buf))
1371  {
1373  "# Decryption failures with valid KCE",
1374  1,
1375  GNUNET_NO);
1376  kce_destroy(kce);
1377  return;
1378  }
1379  kce_destroy(kce);
1381  "# bytes decrypted with BOX",
1382  sizeof(out_buf),
1383  GNUNET_NO);
1384  try_handle_plaintext(ss->sender, out_buf, sizeof(out_buf));
1385  consider_ss_ack(ss);
1386 }
1387 
1388 
1392 struct SearchContext {
1396  const struct sockaddr *address;
1397 
1401  socklen_t address_len;
1402 
1407 };
1408 
1409 
1418 static int
1420  const struct GNUNET_PeerIdentity *key,
1421  void *value)
1422 {
1423  struct SearchContext *sc = cls;
1424  struct SenderAddress *sender = value;
1425 
1426  if ((sender->address_len == sc->address_len) &&
1427  (0 == memcmp(sender->address, sc->address, sender->address_len)))
1428  {
1429  sc->sender = sender;
1430  return GNUNET_NO; /* stop iterating! */
1431  }
1432  return GNUNET_YES;
1433 }
1434 
1435 
1447 static struct SenderAddress *
1449  const struct sockaddr *address,
1450  socklen_t address_len)
1451 {
1452  struct SenderAddress *sender;
1453  struct SearchContext sc = { .address = address,
1454  .address_len = address_len,
1455  .sender = NULL };
1456 
1458  target,
1460  &sc);
1461  if (NULL != sc.sender)
1462  {
1464  return sc.sender;
1465  }
1466  sender = GNUNET_new(struct SenderAddress);
1467  sender->target = *target;
1468  sender->address = GNUNET_memdup(address, address_len);
1469  sender->address_len = address_len;
1471  senders,
1472  &sender->target,
1473  sender,
1475  GNUNET_STATISTICS_set(stats,
1476  "# senders active",
1478  GNUNET_NO);
1479  sender->timeout =
1481  sender->hn = GNUNET_CONTAINER_heap_insert(senders_heap,
1482  sender,
1483  sender->timeout.abs_value_us);
1484  sender->nt = GNUNET_NT_scanner_get_type(is, address, address_len);
1485  if (NULL == timeout_task)
1486  timeout_task = GNUNET_SCHEDULER_add_now(&check_timeouts, NULL);
1487  return sender;
1488 }
1489 
1490 
1498 static int
1500  const struct UDPConfirmation *uc)
1501 {
1502  struct UdpHandshakeSignature uhs;
1503 
1505  uhs.purpose.size = htonl(sizeof(uhs));
1506  uhs.sender = uc->sender;
1507  uhs.receiver = my_identity;
1508  uhs.ephemeral = *ephemeral;
1509  uhs.monotonic_time = uc->monotonic_time;
1511  &uhs.purpose,
1512  &uc->sender_sig,
1513  &uc->sender.public_key);
1514 }
1515 
1516 
1525 static char *
1526 sockaddr_to_udpaddr_string(const struct sockaddr *address,
1527  socklen_t address_len)
1528 {
1529  char *ret;
1530 
1531  switch (address->sa_family)
1532  {
1533  case AF_INET:
1534  GNUNET_asprintf(&ret,
1535  "%s-%s",
1537  GNUNET_a2s(address, address_len));
1538  break;
1539 
1540  case AF_INET6:
1541  GNUNET_asprintf(&ret,
1542  "%s-%s",
1544  GNUNET_a2s(address, address_len));
1545  break;
1546 
1547  default:
1548  GNUNET_assert(0);
1549  }
1550  return ret;
1551 }
1552 
1553 
1559 static void
1560 sock_read(void *cls)
1561 {
1562  struct sockaddr_storage sa;
1563  socklen_t salen = sizeof(sa);
1564  char buf[UINT16_MAX];
1565  ssize_t rcvd;
1566 
1567  (void)cls;
1569  udp_sock,
1570  &sock_read,
1571  NULL);
1572  rcvd = GNUNET_NETWORK_socket_recvfrom(udp_sock,
1573  buf,
1574  sizeof(buf),
1575  (struct sockaddr *)&sa,
1576  &salen);
1577  if (-1 == rcvd)
1578  {
1580  return;
1581  }
1582 
1583  /* first, see if it is a UDPBox */
1584  if (rcvd > sizeof(struct UDPBox))
1585  {
1586  const struct UDPBox *box;
1587  struct KeyCacheEntry *kce;
1588 
1589  box = (const struct UDPBox *)buf;
1590  kce = GNUNET_CONTAINER_multishortmap_get(key_cache, &box->kid);
1591  if (NULL != kce)
1592  {
1593  decrypt_box(box, (size_t)rcvd, kce);
1594  return;
1595  }
1596  }
1597 
1598  /* next, check if it is a broadcast */
1599  if (sizeof(struct UDPBroadcast) == rcvd)
1600  {
1601  const struct UDPBroadcast *ub;
1602  struct UdpBroadcastSignature uhs;
1603 
1604  ub = (const struct UDPBroadcast *)buf;
1606  uhs.purpose.size = htonl(sizeof(uhs));
1607  uhs.sender = ub->sender;
1608  GNUNET_CRYPTO_hash(&sa, salen, &uhs.h_address);
1609  if (GNUNET_OK ==
1611  &uhs.purpose,
1612  &ub->sender_sig,
1613  &ub->sender.public_key))
1614  {
1615  char *addr_s;
1616  enum GNUNET_NetworkType nt;
1617 
1618  addr_s =
1619  sockaddr_to_udpaddr_string((const struct sockaddr *)&sa, salen);
1620  GNUNET_STATISTICS_update(stats, "# broadcasts received", 1, GNUNET_NO);
1621  /* use our own mechanism to determine network type */
1622  nt =
1623  GNUNET_NT_scanner_get_type(is, (const struct sockaddr *)&sa, salen);
1624  GNUNET_TRANSPORT_application_validate(ah, &ub->sender, nt, addr_s);
1625  GNUNET_free(addr_s);
1626  return;
1627  }
1628  /* continue with KX, mostly for statistics... */
1629  }
1630 
1631 
1632  /* finally, test if it is a KX */
1633  if (rcvd < sizeof(struct UDPConfirmation) + sizeof(struct InitialKX))
1634  {
1636  "# messages dropped (no kid, too small for KX)",
1637  1,
1638  GNUNET_NO);
1639  return;
1640  }
1641 
1642  {
1643  const struct InitialKX *kx;
1644  struct SharedSecret *ss;
1645  char pbuf[rcvd - sizeof(struct InitialKX)];
1646  const struct UDPConfirmation *uc;
1647  struct SenderAddress *sender;
1648 
1649  kx = (const struct InitialKX *)buf;
1651  if (GNUNET_OK != try_decrypt(ss,
1652  kx->gcm_tag,
1653  0,
1654  &buf[sizeof(*kx)],
1655  sizeof(pbuf),
1656  pbuf))
1657  {
1658  GNUNET_free(ss);
1660  stats,
1661  "# messages dropped (no kid, AEAD decryption failed)",
1662  1,
1663  GNUNET_NO);
1664  return;
1665  }
1666  uc = (const struct UDPConfirmation *)pbuf;
1667  if (GNUNET_OK != verify_confirmation(&kx->ephemeral, uc))
1668  {
1669  GNUNET_break_op(0);
1670  GNUNET_free(ss);
1672  "# messages dropped (sender signature invalid)",
1673  1,
1674  GNUNET_NO);
1675  return;
1676  }
1677  calculate_cmac(ss);
1678  sender = setup_sender(&uc->sender, (const struct sockaddr *)&sa, salen);
1679  ss->sender = sender;
1680  GNUNET_CONTAINER_DLL_insert(sender->ss_head, sender->ss_tail, ss);
1681  sender->num_secrets++;
1682  GNUNET_STATISTICS_update(stats, "# Secrets active", 1, GNUNET_NO);
1684  "# messages decrypted without BOX",
1685  1,
1686  GNUNET_NO);
1687  try_handle_plaintext(sender, &uc[1], sizeof(pbuf) - sizeof(*uc));
1688  consider_ss_ack(ss);
1689  if (sender->num_secrets > MAX_SECRETS)
1690  secret_destroy(sender->ss_tail);
1691  }
1692 }
1693 
1694 
1702 static struct sockaddr *
1703 udp_address_to_sockaddr(const char *bindto, socklen_t *sock_len)
1704 {
1705  struct sockaddr *in;
1706  unsigned int port;
1707  char dummy[2];
1708  char *colon;
1709  char *cp;
1710 
1711  if (1 == sscanf(bindto, "%u%1s", &port, dummy))
1712  {
1713  /* interpreting value as just a PORT number */
1714  if (port > UINT16_MAX)
1715  {
1717  "BINDTO specification `%s' invalid: value too large for port\n",
1718  bindto);
1719  return NULL;
1720  }
1721  if ((GNUNET_NO == GNUNET_NETWORK_test_pf(PF_INET6)) ||
1722  (GNUNET_YES ==
1725  "DISABLE_V6")))
1726  {
1727  struct sockaddr_in *i4;
1728 
1729  i4 = GNUNET_malloc(sizeof(struct sockaddr_in));
1730  i4->sin_family = AF_INET;
1731  i4->sin_port = htons((uint16_t)port);
1732  *sock_len = sizeof(struct sockaddr_in);
1733  in = (struct sockaddr *)i4;
1734  }
1735  else
1736  {
1737  struct sockaddr_in6 *i6;
1738 
1739  i6 = GNUNET_malloc(sizeof(struct sockaddr_in6));
1740  i6->sin6_family = AF_INET6;
1741  i6->sin6_port = htons((uint16_t)port);
1742  *sock_len = sizeof(struct sockaddr_in6);
1743  in = (struct sockaddr *)i6;
1744  }
1745  return in;
1746  }
1747  cp = GNUNET_strdup(bindto);
1748  colon = strrchr(cp, ':');
1749  if (NULL != colon)
1750  {
1751  /* interpet value after colon as port */
1752  *colon = '\0';
1753  colon++;
1754  if (1 == sscanf(colon, "%u%1s", &port, dummy))
1755  {
1756  /* interpreting value as just a PORT number */
1757  if (port > UINT16_MAX)
1758  {
1760  "BINDTO specification `%s' invalid: value too large for port\n",
1761  bindto);
1762  GNUNET_free(cp);
1763  return NULL;
1764  }
1765  }
1766  else
1767  {
1768  GNUNET_log(
1770  "BINDTO specification `%s' invalid: last ':' not followed by number\n",
1771  bindto);
1772  GNUNET_free(cp);
1773  return NULL;
1774  }
1775  }
1776  else
1777  {
1778  /* interpret missing port as 0, aka pick any free one */
1779  port = 0;
1780  }
1781  {
1782  /* try IPv4 */
1783  struct sockaddr_in v4;
1784 
1785  if (1 == inet_pton(AF_INET, cp, &v4))
1786  {
1787  v4.sin_port = htons((uint16_t)port);
1788  in = GNUNET_memdup(&v4, sizeof(v4));
1789  *sock_len = sizeof(v4);
1790  GNUNET_free(cp);
1791  return in;
1792  }
1793  }
1794  {
1795  /* try IPv6 */
1796  struct sockaddr_in6 v6;
1797  const char *start;
1798 
1799  start = cp;
1800  if (('[' == *cp) && (']' == cp[strlen(cp) - 1]))
1801  {
1802  start++; /* skip over '[' */
1803  cp[strlen(cp) - 1] = '\0'; /* eat ']' */
1804  }
1805  if (1 == inet_pton(AF_INET6, start, &v6))
1806  {
1807  v6.sin6_port = htons((uint16_t)port);
1808  in = GNUNET_memdup(&v6, sizeof(v6));
1809  *sock_len = sizeof(v6);
1810  GNUNET_free(cp);
1811  return in;
1812  }
1813  }
1814  /* #5528 FIXME (feature!): maybe also try getnameinfo()? */
1815  GNUNET_free(cp);
1816  return NULL;
1817 }
1818 
1819 
1827 static void
1828 do_pad(gcry_cipher_hd_t out_cipher, char *dgram, size_t pad_size)
1829 {
1830  char pad[pad_size];
1831 
1833  if (sizeof(pad) > sizeof(struct GNUNET_MessageHeader))
1834  {
1835  struct GNUNET_MessageHeader hdr =
1836  { .size = htons(sizeof(pad)),
1838 
1839  memcpy(pad, &hdr, sizeof(hdr));
1840  }
1841  GNUNET_assert(
1842  0 ==
1843  gcry_cipher_encrypt(out_cipher, dgram, sizeof(pad), pad, sizeof(pad)));
1844 }
1845 
1846 
1855 static void
1857  const struct GNUNET_MessageHeader *msg,
1858  void *impl_state)
1859 {
1860  struct ReceiverAddress *receiver = impl_state;
1861  uint16_t msize = ntohs(msg->size);
1862 
1863  GNUNET_assert(mq == receiver->mq);
1864  if (msize > receiver->mtu)
1865  {
1866  GNUNET_break(0);
1867  receiver_destroy(receiver);
1868  return;
1869  }
1870  reschedule_receiver_timeout(receiver);
1871 
1872  if (0 == receiver->acks_available)
1873  {
1874  /* use KX encryption method */
1875  struct UdpHandshakeSignature uhs;
1876  struct UDPConfirmation uc;
1877  struct InitialKX kx;
1878  struct GNUNET_CRYPTO_EcdhePrivateKey epriv;
1879  char dgram[receiver->mtu + sizeof(uc) + sizeof(kx)];
1880  size_t dpos;
1881  gcry_cipher_hd_t out_cipher;
1882  struct SharedSecret *ss;
1883 
1884  /* setup key material */
1886 
1887  ss = setup_shared_secret_enc(&epriv, receiver);
1888  setup_cipher(&ss->master, 0, &out_cipher);
1889  /* compute 'uc' */
1890  uc.sender = my_identity;
1891  uc.monotonic_time =
1894  uhs.purpose.size = htonl(sizeof(uhs));
1895  uhs.sender = my_identity;
1896  uhs.receiver = receiver->target;
1898  uhs.monotonic_time = uc.monotonic_time;
1900  &uhs.purpose,
1901  &uc.sender_sig));
1902  /* Leave space for kx */
1903  dpos = sizeof(struct GNUNET_CRYPTO_EcdhePublicKey);
1904  /* Append encrypted uc to dgram */
1905  GNUNET_assert(0 == gcry_cipher_encrypt(out_cipher,
1906  &dgram[dpos],
1907  sizeof(uc),
1908  &uc,
1909  sizeof(uc)));
1910  dpos += sizeof(uc);
1911  /* Append encrypted payload to dgram */
1912  GNUNET_assert(
1913  0 == gcry_cipher_encrypt(out_cipher, &dgram[dpos], msize, msg, msize));
1914  dpos += msize;
1915  do_pad(out_cipher, &dgram[dpos], sizeof(dgram) - dpos);
1916  /* Datagram starts with kx */
1917  kx.ephemeral = uhs.ephemeral;
1918  GNUNET_assert(
1919  0 == gcry_cipher_gettag(out_cipher, kx.gcm_tag, sizeof(kx.gcm_tag)));
1920  gcry_cipher_close(out_cipher);
1921  memcpy(dgram, &kx, sizeof(kx));
1922  if (-1 == GNUNET_NETWORK_socket_sendto(udp_sock,
1923  dgram,
1924  sizeof(dgram),
1925  receiver->address,
1926  receiver->address_len))
1929  return;
1930  } /* End of KX encryption method */
1931 
1932  /* begin "BOX" encryption method, scan for ACKs from tail! */
1933  for (struct SharedSecret *ss = receiver->ss_tail; NULL != ss; ss = ss->prev)
1934  {
1935  if (ss->sequence_used < ss->sequence_allowed)
1936  {
1937  char dgram[sizeof(struct UDPBox) + receiver->mtu];
1938  struct UDPBox *box;
1939  gcry_cipher_hd_t out_cipher;
1940  size_t dpos;
1941 
1942  box = (struct UDPBox *)dgram;
1943  ss->sequence_used++;
1944  get_kid(&ss->master, ss->sequence_used, &box->kid);
1945  setup_cipher(&ss->master, ss->sequence_used, &out_cipher);
1946  /* Append encrypted payload to dgram */
1947  dpos = sizeof(struct UDPBox);
1948  GNUNET_assert(
1949  0 == gcry_cipher_encrypt(out_cipher, &dgram[dpos], msize, msg, msize));
1950  dpos += msize;
1951  do_pad(out_cipher, &dgram[dpos], sizeof(dgram) - dpos);
1952  GNUNET_assert(0 == gcry_cipher_gettag(out_cipher,
1953  box->gcm_tag,
1954  sizeof(box->gcm_tag)));
1955  gcry_cipher_close(out_cipher);
1956  if (-1 == GNUNET_NETWORK_socket_sendto(udp_sock,
1957  dgram,
1958  sizeof(dgram),
1959  receiver->address,
1960  receiver->address_len))
1963  receiver->acks_available--;
1964  if (0 == receiver->acks_available)
1965  {
1966  /* We have no more ACKs => MTU change! */
1967  setup_receiver_mq(receiver);
1968  }
1969  return;
1970  }
1971  }
1972  GNUNET_assert(0);
1973 }
1974 
1975 
1984 static void
1985 mq_destroy(struct GNUNET_MQ_Handle *mq, void *impl_state)
1986 {
1987  struct ReceiverAddress *receiver = impl_state;
1988 
1989  if (mq == receiver->mq)
1990  {
1991  receiver->mq = NULL;
1992  receiver_destroy(receiver);
1993  }
1994 }
1995 
1996 
2003 static void
2004 mq_cancel(struct GNUNET_MQ_Handle *mq, void *impl_state)
2005 {
2006  /* Cancellation is impossible with UDP; bail */
2007  GNUNET_assert(0);
2008 }
2009 
2010 
2020 static void
2021 mq_error(void *cls, enum GNUNET_MQ_Error error)
2022 {
2023  struct ReceiverAddress *receiver = cls;
2024 
2026  "MQ error in queue to %s: %d\n",
2027  GNUNET_i2s(&receiver->target),
2028  (int)error);
2029  receiver_destroy(receiver);
2030 }
2031 
2032 
2040 static void
2042 {
2043  size_t base_mtu;
2044 
2045  if (NULL != receiver->qh)
2046  {
2048  receiver->qh = NULL;
2049  }
2050  GNUNET_assert(NULL == receiver->mq);
2051  switch (receiver->address->sa_family)
2052  {
2053  case AF_INET:
2054  base_mtu = 1480 /* Ethernet MTU, 1500 - Ethernet header - VLAN tag */
2055  - sizeof(struct GNUNET_TUN_IPv4Header) /* 20 */
2056  - sizeof(struct GNUNET_TUN_UdpHeader) /* 8 */;
2057  break;
2058 
2059  case AF_INET6:
2060  base_mtu = 1280 /* Minimum MTU required by IPv6 */
2061  - sizeof(struct GNUNET_TUN_IPv6Header) /* 40 */
2062  - sizeof(struct GNUNET_TUN_UdpHeader) /* 8 */;
2063  break;
2064 
2065  default:
2066  GNUNET_assert(0);
2067  break;
2068  }
2069  if (0 == receiver->acks_available)
2070  {
2071  /* MTU based on full KX messages */
2072  receiver->mtu = base_mtu - sizeof(struct InitialKX) /* 48 */
2073  - sizeof(struct UDPConfirmation); /* 104 */
2074  }
2075  else
2076  {
2077  /* MTU based on BOXed messages */
2078  receiver->mtu = base_mtu - sizeof(struct UDPBox);
2079  }
2080  /* => Effective MTU for CORE will range from 1080 (IPv6 + KX) to
2081  1404 (IPv4 + Box) bytes, depending on circumstances... */
2082  if (NULL == receiver->mq)
2084  &mq_destroy,
2085  &mq_cancel,
2086  receiver,
2087  NULL,
2088  &mq_error,
2089  receiver);
2090  receiver->qh =
2092  &receiver->target,
2093  receiver->foreign_addr,
2094  receiver->mtu,
2095  receiver->nt,
2097  receiver->mq);
2098 }
2099 
2100 
2119 static int
2120 mq_init(void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
2121 {
2122  struct ReceiverAddress *receiver;
2123  const char *path;
2124  struct sockaddr *in;
2125  socklen_t in_len;
2126 
2127  if (0 != strncmp(address,
2129  strlen(COMMUNICATOR_ADDRESS_PREFIX "-")))
2130  {
2131  GNUNET_break_op(0);
2132  return GNUNET_SYSERR;
2133  }
2134  path = &address[strlen(COMMUNICATOR_ADDRESS_PREFIX "-")];
2135  in = udp_address_to_sockaddr(path, &in_len);
2136 
2137  receiver = GNUNET_new(struct ReceiverAddress);
2138  receiver->address = in;
2139  receiver->address_len = in_len;
2140  receiver->target = *peer;
2141  receiver->nt = GNUNET_NT_scanner_get_type(is, in, in_len);
2143  receivers,
2144  &receiver->target,
2145  receiver,
2147  receiver->timeout =
2149  receiver->hn = GNUNET_CONTAINER_heap_insert(receivers_heap,
2150  receiver,
2151  receiver->timeout.abs_value_us);
2152  GNUNET_STATISTICS_set(stats,
2153  "# receivers active",
2155  GNUNET_NO);
2156  receiver->foreign_addr =
2157  sockaddr_to_udpaddr_string(receiver->address, receiver->address_len);
2158  setup_receiver_mq(receiver);
2159  if (NULL == timeout_task)
2160  timeout_task = GNUNET_SCHEDULER_add_now(&check_timeouts, NULL);
2161  return GNUNET_OK;
2162 }
2163 
2164 
2173 static int
2175  const struct GNUNET_PeerIdentity *target,
2176  void *value)
2177 {
2178  struct ReceiverAddress *receiver = value;
2179 
2180  (void)cls;
2181  (void)target;
2182  receiver_destroy(receiver);
2183  return GNUNET_OK;
2184 }
2185 
2186 
2195 static int
2197  const struct GNUNET_PeerIdentity *target,
2198  void *value)
2199 {
2200  struct SenderAddress *sender = value;
2201 
2202  (void)cls;
2203  (void)target;
2204  sender_destroy(sender);
2205  return GNUNET_OK;
2206 }
2207 
2208 
2214 static void
2215 do_shutdown(void *cls)
2216 {
2217  if (NULL != nat)
2218  {
2219  GNUNET_NAT_unregister(nat);
2220  nat = NULL;
2221  }
2222  while (NULL != bi_head)
2223  bi_destroy(bi_head);
2224  if (NULL != broadcast_task)
2225  {
2226  GNUNET_SCHEDULER_cancel(broadcast_task);
2227  broadcast_task = NULL;
2228  }
2229  if (NULL != read_task)
2230  {
2231  GNUNET_SCHEDULER_cancel(read_task);
2232  read_task = NULL;
2233  }
2234  if (NULL != udp_sock)
2235  {
2237  udp_sock = NULL;
2238  }
2241  NULL);
2246  GNUNET_CONTAINER_heap_destroy(senders_heap);
2247  GNUNET_CONTAINER_heap_destroy(receivers_heap);
2248  if (NULL != ch)
2249  {
2251  ch = NULL;
2252  }
2253  if (NULL != ah)
2254  {
2256  ah = NULL;
2257  }
2258  if (NULL != stats)
2259  {
2261  stats = NULL;
2262  }
2263  if (NULL != my_private_key)
2264  {
2265  GNUNET_free(my_private_key);
2266  my_private_key = NULL;
2267  }
2268  if (NULL != is)
2269  {
2271  is = NULL;
2272  }
2273 }
2274 
2275 
2285 static void
2286 enc_notify_cb(void *cls,
2287  const struct GNUNET_PeerIdentity *sender,
2288  const struct GNUNET_MessageHeader *msg)
2289 {
2290  const struct UDPAck *ack;
2291 
2292  (void)cls;
2293  if ((ntohs(msg->type) != GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK) ||
2294  (ntohs(msg->size) != sizeof(struct UDPAck)))
2295  {
2296  GNUNET_break_op(0);
2297  return;
2298  }
2299  ack = (const struct UDPAck *)msg;
2301  sender,
2302  &handle_ack,
2303  (void *)ack);
2304 }
2305 
2306 
2320 static void
2321 nat_address_cb(void *cls,
2322  void **app_ctx,
2323  int add_remove,
2325  const struct sockaddr *addr,
2326  socklen_t addrlen)
2327 {
2328  char *my_addr;
2330 
2331  if (GNUNET_YES == add_remove)
2332  {
2333  enum GNUNET_NetworkType nt;
2334 
2335  GNUNET_asprintf(&my_addr,
2336  "%s-%s",
2338  GNUNET_a2s(addr, addrlen));
2339  nt = GNUNET_NT_scanner_get_type(is, addr, addrlen);
2340  ai =
2342  my_addr,
2343  nt,
2345  GNUNET_free(my_addr);
2346  *app_ctx = ai;
2347  }
2348  else
2349  {
2350  ai = *app_ctx;
2352  *app_ctx = NULL;
2353  }
2354 }
2355 
2356 
2362 static void
2363 ifc_broadcast(void *cls)
2364 {
2365  struct BroadcastInterface *bi = cls;
2366  struct GNUNET_TIME_Relative delay;
2367 
2368  delay = BROADCAST_FREQUENCY;
2369  delay.rel_value_us =
2371  bi->broadcast_task =
2373 
2374  switch (bi->sa->sa_family)
2375  {
2376  case AF_INET: {
2377  static int yes = 1;
2378  static int no = 0;
2379  ssize_t sent;
2380 
2382  SOL_SOCKET,
2383  SO_BROADCAST,
2384  &yes,
2385  sizeof(int)))
2387  sent = GNUNET_NETWORK_socket_sendto(udp_sock,
2388  &bi->bcm,
2389  sizeof(bi->bcm),
2390  bi->ba,
2391  bi->salen);
2392  if (-1 == sent)
2395  SOL_SOCKET,
2396  SO_BROADCAST,
2397  &no,
2398  sizeof(int)))
2400  break;
2401  }
2402 
2403  case AF_INET6: {
2404  ssize_t sent;
2405  struct sockaddr_in6 dst;
2406 
2407  dst.sin6_family = AF_INET6;
2408  dst.sin6_port = htons(my_port);
2409  dst.sin6_addr = bi->mcreq.ipv6mr_multiaddr;
2410  dst.sin6_scope_id = ((struct sockaddr_in6 *)bi->ba)->sin6_scope_id;
2411 
2412  sent = GNUNET_NETWORK_socket_sendto(udp_sock,
2413  &bi->bcm,
2414  sizeof(bi->bcm),
2415  (const struct sockaddr *)&dst,
2416  sizeof(dst));
2417  if (-1 == sent)
2419  break;
2420  }
2421 
2422  default:
2423  GNUNET_break(0);
2424  break;
2425  }
2426 }
2427 
2428 
2443 static int
2444 iface_proc(void *cls,
2445  const char *name,
2446  int isDefault,
2447  const struct sockaddr *addr,
2448  const struct sockaddr *broadcast_addr,
2449  const struct sockaddr *netmask,
2450  socklen_t addrlen)
2451 {
2452  struct BroadcastInterface *bi;
2453  enum GNUNET_NetworkType network;
2454  struct UdpBroadcastSignature ubs;
2455 
2456  (void)cls;
2457  (void)netmask;
2458  if (NULL == addr)
2459  return GNUNET_YES; /* need to know our address! */
2460  network = GNUNET_NT_scanner_get_type(is, addr, addrlen);
2461  if (GNUNET_NT_LOOPBACK == network)
2462  {
2463  /* Broadcasting on loopback does not make sense */
2464  return GNUNET_YES;
2465  }
2466  for (bi = bi_head; NULL != bi; bi = bi->next)
2467  {
2468  if ((bi->salen == addrlen) && (0 == memcmp(addr, bi->sa, addrlen)))
2469  {
2470  bi->found = GNUNET_YES;
2471  return GNUNET_OK;
2472  }
2473  }
2474 
2475  if ((AF_INET6 == addr->sa_family) && (NULL == broadcast_addr))
2476  return GNUNET_OK; /* broadcast_addr is required for IPv6! */
2477  if ((AF_INET6 == addr->sa_family) && (GNUNET_YES != have_v6_socket))
2478  return GNUNET_OK; /* not using IPv6 */
2479 
2480  bi = GNUNET_new(struct BroadcastInterface);
2481  bi->sa = GNUNET_memdup(addr, addrlen);
2482  if (NULL != broadcast_addr)
2483  bi->ba = GNUNET_memdup(broadcast_addr, addrlen);
2484  bi->salen = addrlen;
2485  bi->found = GNUNET_YES;
2486  bi->bcm.sender = my_identity;
2488  ubs.purpose.size = htonl(sizeof(ubs));
2489  ubs.sender = my_identity;
2490  GNUNET_CRYPTO_hash(addr, addrlen, &ubs.h_address);
2492  &ubs.purpose,
2493  &bi->bcm.sender_sig));
2495  GNUNET_CONTAINER_DLL_insert(bi_head, bi_tail, bi);
2496  if ((AF_INET6 == addr->sa_family) && (NULL != broadcast_addr))
2497  {
2498  /* Create IPv6 multicast request */
2499  const struct sockaddr_in6 *s6 =
2500  (const struct sockaddr_in6 *)broadcast_addr;
2501 
2502  GNUNET_assert(
2503  1 == inet_pton(AF_INET6, "FF05::13B", &bi->mcreq.ipv6mr_multiaddr));
2504 
2505  /* http://tools.ietf.org/html/rfc2553#section-5.2:
2506  *
2507  * IPV6_JOIN_GROUP
2508  *
2509  * Join a multicast group on a specified local interface. If the
2510  * interface index is specified as 0, the kernel chooses the local
2511  * interface. For example, some kernels look up the multicast
2512  * group in the normal IPv6 routing table and using the resulting
2513  * interface; we do this for each interface, so no need to use
2514  * zero (anymore...).
2515  */
2516  bi->mcreq.ipv6mr_interface = s6->sin6_scope_id;
2517 
2518  /* Join the multicast group */
2520  IPPROTO_IPV6,
2521  IPV6_JOIN_GROUP,
2522  &bi->mcreq,
2523  sizeof(bi->mcreq)))
2524  {
2526  }
2527  }
2528  return GNUNET_OK;
2529 }
2530 
2531 
2537 static void
2538 do_broadcast(void *cls)
2539 {
2540  struct BroadcastInterface *bin;
2541 
2542  (void)cls;
2543  for (struct BroadcastInterface *bi = bi_head; NULL != bi; bi = bi->next)
2544  bi->found = GNUNET_NO;
2546  for (struct BroadcastInterface *bi = bi_head; NULL != bi; bi = bin)
2547  {
2548  bin = bi->next;
2549  if (GNUNET_NO == bi->found)
2550  bi_destroy(bi);
2551  }
2553  &do_broadcast,
2554  NULL);
2555 }
2556 
2557 
2566 static void
2567 run(void *cls,
2568  char *const *args,
2569  const char *cfgfile,
2570  const struct GNUNET_CONFIGURATION_Handle *c)
2571 {
2572  char *bindto;
2573  struct sockaddr *in;
2574  socklen_t in_len;
2575  struct sockaddr_storage in_sto;
2576  socklen_t sto_len;
2577 
2578  (void)cls;
2579  cfg = c;
2580  if (GNUNET_OK !=
2583  "BINDTO",
2584  &bindto))
2585  {
2588  "BINDTO");
2589  return;
2590  }
2591 
2592  in = udp_address_to_sockaddr(bindto, &in_len);
2593  if (NULL == in)
2594  {
2596  "Failed to setup UDP socket address with path `%s'\n",
2597  bindto);
2598  GNUNET_free(bindto);
2599  return;
2600  }
2601  udp_sock =
2602  GNUNET_NETWORK_socket_create(in->sa_family, SOCK_DGRAM, IPPROTO_UDP);
2603  if (NULL == udp_sock)
2604  {
2606  GNUNET_free(in);
2607  GNUNET_free(bindto);
2608  return;
2609  }
2610  if (AF_INET6 == in->sa_family)
2612  if (GNUNET_OK != GNUNET_NETWORK_socket_bind(udp_sock, in, in_len))
2613  {
2615  GNUNET_NETWORK_socket_close(udp_sock);
2616  udp_sock = NULL;
2617  GNUNET_free(in);
2618  GNUNET_free(bindto);
2619  return;
2620  }
2621  /* We might have bound to port 0, allowing the OS to figure it out;
2622  thus, get the real IN-address from the socket */
2623  sto_len = sizeof(in_sto);
2624  if (0 != getsockname(GNUNET_NETWORK_get_fd(udp_sock),
2625  (struct sockaddr *)&in_sto,
2626  &sto_len))
2627  {
2628  memcpy(&in_sto, in, in_len);
2629  sto_len = in_len;
2630  }
2631  GNUNET_free(in);
2632  GNUNET_free(bindto);
2633  in = (struct sockaddr *)&in_sto;
2634  in_len = sto_len;
2636  "Bound to `%s'\n",
2637  GNUNET_a2s((const struct sockaddr *)&in_sto, sto_len));
2638  switch (in->sa_family)
2639  {
2640  case AF_INET:
2641  my_port = ntohs(((struct sockaddr_in *)in)->sin_port);
2642  break;
2643 
2644  case AF_INET6:
2645  my_port = ntohs(((struct sockaddr_in6 *)in)->sin6_port);
2646  break;
2647 
2648  default:
2649  GNUNET_break(0);
2650  my_port = 0;
2651  }
2652  stats = GNUNET_STATISTICS_create("C-UDP", cfg);
2656  receivers_heap =
2660  is = GNUNET_NT_scanner_init();
2662  if (NULL == my_private_key)
2663  {
2664  GNUNET_log(
2666  _(
2667  "Transport service is lacking key configuration settings. Exiting.\n"));
2669  return;
2670  }
2672  /* start reading */
2674  udp_sock,
2675  &sock_read,
2676  NULL);
2681  &mq_init,
2682  NULL,
2683  &enc_notify_cb,
2684  NULL);
2685  if (NULL == ch)
2686  {
2687  GNUNET_break(0);
2689  return;
2690  }
2692  if (NULL == ah)
2693  {
2694  GNUNET_break(0);
2696  return;
2697  }
2698  /* start broadcasting */
2699  if (GNUNET_YES !=
2702  "DISABLE_BROADCAST"))
2703  {
2704  broadcast_task = GNUNET_SCHEDULER_add_now(&do_broadcast, NULL);
2705  }
2706  nat = GNUNET_NAT_register(cfg,
2708  IPPROTO_UDP,
2709  1 /* one address */,
2710  (const struct sockaddr **)&in,
2711  &in_len,
2712  &nat_address_cb,
2713  NULL /* FIXME: support reversal: #5529 */,
2714  NULL /* closure */);
2715 }
2716 
2717 
2725 int
2726 main(int argc, char *const *argv)
2727 {
2728  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2730  };
2731  int ret;
2732 
2733  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
2734  return 2;
2735 
2736  ret = (GNUNET_OK == GNUNET_PROGRAM_run(argc,
2737  argv,
2738  "gnunet-communicator-udp",
2739  _("GNUnet UDP communicator"),
2740  options,
2741  &run,
2742  NULL))
2743  ? 0
2744  : 1;
2745  GNUNET_free((void *)argv);
2746  return ret;
2747 }
2748 
2749 
2750 /* end of gnunet-communicator-udp.c */
int GNUNET_CRYPTO_ecdhe_key_create2(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
Create a new private key.
Definition: crypto_ecc.c:632
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct KeyCacheEntry * next
Kept in a DLL.
Handle to the interface scanner.
Definition: nt.c:108
int GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, int level, int option_name, const void *option_value, socklen_t option_len)
Set socket option.
Definition: network.c:871
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_PAD
UDP communicator padding.
int GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
Return file descriptor for this network handle.
Definition: network.c:1068
size_t mtu
MTU we allowed transport for this receiver right now.
#define COMMUNICATOR_CONFIG_SECTION
Configuration section used by the communicator.
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:306
void GNUNET_CONTAINER_heap_update_cost(struct GNUNET_CONTAINER_HeapNode *node, GNUNET_CONTAINER_HeapCostType new_cost)
Updates the cost of any node in the tree.
static struct BroadcastInterface * bi_tail
Broadcast interface tasks.
static void consider_ss_ack(struct SharedSecret *ss)
We established a shared secret with a sender.
struct SharedSecret * ss_tail
Shared secrets we used with target, last used is tail.
Handle for active NAT registrations.
Definition: nat_api.c:70
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK.
#define BROADCAST_FREQUENCY
How often do we broadcast our presence on the LAN?
struct GNUNET_PeerIdentity sender
Identity of the inititor of the UDP connection (UDP client).
int main(int argc, char *const *argv)
The main function for the UNIX communicator.
struct KeyCacheEntry * prev
Kept in a DLL.
int found
Was this interface found in the last iface_proc() scan?
static void do_broadcast(void *cls)
Scan interfaces to broadcast our presence on the LAN.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static struct GNUNET_TRANSPORT_ApplicationHandle * ah
Our handle to report addresses for validation to TRANSPORT.
GNUNET_NAT_AddressClass
Some addresses contain sensitive information or are not suitable for global distribution.
uint64_t rel_value_us
The actual value.
struct GNUNET_PeerIdentity sender
Sender&#39;s peer identity.
struct KeyCacheEntry * kce_head
Kept in a DLL, sorted by sequence number.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
static uint16_t my_port
Port number to which we are actually bound.
Broadcast by peer in LAN announcing its presence.
unsigned int GNUNET_CONTAINER_multishortmap_size(const struct GNUNET_CONTAINER_MultiShortmap *map)
Get the number of key-value pairs in the map.
static struct SenderAddress * setup_sender(const struct GNUNET_PeerIdentity *target, const struct sockaddr *address, socklen_t address_len)
Create sender address for target.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK
UDP KX acknowledgement.
struct GNUNET_PeerIdentity receiver
Presumed identity of the target of the UDP connection (UDP server)
static struct GNUNET_NAT_Handle * nat
Connection to NAT service.
struct GNUNET_CONTAINER_HeapNode * GNUNET_CONTAINER_heap_insert(struct GNUNET_CONTAINER_Heap *heap, void *element, GNUNET_CONTAINER_HeapCostType cost)
Inserts a new element into the heap.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Setup communicator and launch network interactions.
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
char gcm_tag[(128/8)]
HMAC for the following encrypted message, using GCM.
#define KCN_TARGET
How many KCNs do we keep around after we hit the KCN_THRESHOLD? Should be larger than KCN_THRESHOLD s...
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 struct GNUNET_PeerIdentity my_identity
Our public key.
struct GNUNET_HashCode cmac
CMAC is used to identify master in ACKs.
static struct SharedSecret * setup_shared_secret_dec(const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral)
Setup shared secret for decryption.
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.
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:1284
char gcm_tag[(128/8)]
128-bit authentication tag for the following encrypted message, from GCM.
socklen_t address_len
Number of bytes in address.
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
struct GNUNET_CONTAINER_HeapNode * hn
Entry in sender expiration heap.
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1439
void GNUNET_TRANSPORT_application_validate(struct GNUNET_TRANSPORT_ApplicationHandle *ch, const struct GNUNET_PeerIdentity *peer, enum GNUNET_NetworkType nt, const char *addr)
An application (or a communicator) has received a HELLO (or other address data of another peer) and w...
static struct sockaddr * udp_address_to_sockaddr(const char *bindto, socklen_t *sock_len)
Convert UDP bind specification to a struct sockaddr *
struct GNUNET_CRYPTO_EcdhePublicKey ephemeral
Ephemeral key for KX.
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
static void secret_destroy(struct SharedSecret *ss)
Destroy ss and associated key cache entries.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Bandwidth allocation API for applications to interact with.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Purpose must be GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST.
static struct GNUNET_CONTAINER_MultiShortmap * key_cache
Cache of pre-generated key IDs.
static struct BroadcastInterface * bi_head
Broadcast interface tasks.
uint32_t sequence_allowed
Up to which sequence number did the other peer allow us to use this key, or up to which number did we...
socklen_t address_len
Length of the address.
struct sockaddr * address
Address of the other peer.
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:474
int GNUNET_CRYPTO_eddsa_sign(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EddsaSignature *sig)
EdDSA sign a given block.
Definition: crypto_ecc.c:989
static struct GNUNET_FS_UnindexContext * uc
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
unsigned int num_secrets
Length of the DLL at ss_head.
int GNUNET_CONTAINER_multishortmap_remove(struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key, const void *value)
Remove the given key-value pair from the map.
static struct GNUNET_NT_InterfaceScanner * is
Network scanner to determine network types.
struct SharedSecret * ss
Corresponding shared secret.
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.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
int GNUNET_CRYPTO_hkdf(void *result, size_t out_len, int xtr_algo, int prf_algo, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
Derive key.
Definition: crypto_hkdf.c:289
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
Standard IPv4 header.
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
static void kce_destroy(struct KeyCacheEntry *kce)
Free memory used by key cache entry.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Definition of a command line option.
struct SenderAddress * sender
Sender we use this shared secret with, or NULL.
void GNUNET_OS_network_interfaces_list(GNUNET_OS_NetworkInterfaceProcessor proc, void *proc_cls)
Enumerate all network interfaces.
Definition: os_network.c:388
static int iface_proc(void *cls, const char *name, int isDefault, const struct sockaddr *addr, const struct sockaddr *broadcast_addr, const struct sockaddr *netmask, socklen_t addrlen)
Callback function invoked for each interface found.
static int have_v6_socket
GNUNET_YES if udp_sock supports IPv6.
#define GCM_TAG_SIZE
Size of the GCM tag.
struct GNUNET_TIME_Absolute timeout
Timeout for this sender.
static int handle_ack(void *cls, const struct GNUNET_PeerIdentity *pid, void *value)
We received an ACK for pid.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
Encrypted continuation of UDP initial handshake, followed by message header with payload.
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.
#define GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE
Signature used by UDP communicator handshake.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:517
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).
static int ret
Final status code.
Definition: gnunet-arm.c:89
Handle for the service.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
void GNUNET_TRANSPORT_application_done(struct GNUNET_TRANSPORT_ApplicationHandle *ch)
Shutdown TRANSPORT application client.
static char * sockaddr_to_udpaddr_string(const struct sockaddr *address, socklen_t address_len)
Converts address to the address string format used by this communicator in HELLOs.
uint64_t abs_value_us
The actual value.
static void reschedule_sender_timeout(struct SenderAddress *sender)
Increment sender timeout due to activity.
static void bi_destroy(struct BroadcastInterface *bi)
An interface went away, stop broadcasting on it.
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
Definition: gnunet_nt_lib.h:35
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
uint32_t sequence_number
Sequence number used to derive this entry from master key.
"Plaintext" header at beginning of KX message.
static void setup_cipher(const struct GNUNET_HashCode *msec, uint32_t serial, gcry_cipher_hd_t *cipher)
Setup cipher based on shared secret msec and serial number serial.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won&#39;t work on W32.
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
A 256-bit hashcode.
Opaque handle to the transport service for communicators.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
void GNUNET_TRANSPORT_communicator_disconnect(struct GNUNET_TRANSPORT_CommunicatorHandle *ch)
Disconnect from the transport service.
struct GNUNET_ShortHashCode kid
Key and IV identification code.
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender&#39;s signature of type GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE.
struct sockaddr * ba
Broadcast address to use on the interface.
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
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 a backchannel message for this communicator (...
struct GNUNET_ShortHashCode kid
Key and IV identification code.
static void ifc_broadcast(void *cls)
Broadcast our presence on one of our interfaces.
struct SharedSecret * ss_head
Shared secrets we used with target, first used is head.
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.
struct GNUNET_PeerIdentity target
To whom are we talking to.
#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...
char * foreign_addr
Address of the receiver in the human-readable format with the COMMUNICATOR_ADDRESS_PREFIX.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1237
static struct GNUNET_CONTAINER_MultiPeerMap * receivers
Receivers (map from peer identity to struct ReceiverAddress)
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
Pre-generated "kid" code (key and IV identification code) to quickly derive master key for a struct U...
static struct GNUNET_SCHEDULER_Task * broadcast_task
ID of master broadcast task.
uint32_t sequence_used
Up to which sequence number did we use this master already? (for encrypting only) ...
static struct GNUNET_TIME_Relative timeout
User defined timestamp for completing operations.
Definition: gnunet-arm.c:114
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:550
int GNUNET_CRYPTO_eddsa_verify(uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const struct GNUNET_CRYPTO_EddsaSignature *sig, const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Verify EdDSA signature.
Definition: crypto_ecc.c:1116
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:1505
static char * value
Value of the record to add/remove.
static int get_sender_delete_it(void *cls, const struct GNUNET_PeerIdentity *target, void *value)
Iterator over all senders to clean up.
static int verify_confirmation(const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral, const struct UDPConfirmation *uc)
Check signature from uc against ephemeral.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
#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:44
struct GNUNET_HashCode master
Master shared secret.
enum GNUNET_NetworkType nt
Which network type does this queue use?
#define GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST
Signature used by UDP broadcasts.
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:1264
struct KeyCacheEntry * kce_tail
Kept in a DLL, sorted by sequence number.
#define MAX_SECRETS
How many shared master secrets do we keep around at most per sender? Should be large enough so that w...
#define AES_IV_SIZE
AES (GCM) IV size.
void * GNUNET_CONTAINER_heap_peek(const struct GNUNET_CONTAINER_Heap *heap)
Get element stored at the root of heap.
static void pass_plaintext_to_core(struct SenderAddress *sender, const void *plaintext, size_t plaintext_len)
We received plaintext_len bytes of plaintext from a sender.
struct SharedSecret * ss_tail
Shared secrets we received with target, last used is tail.
static void check_timeouts(void *cls)
Task run to check #receiver_heap and #sender_heap for timeouts.
ssize_t GNUNET_NETWORK_socket_sendto(const struct GNUNET_NETWORK_Handle *desc, const void *message, size_t length, const struct sockaddr *dest_addr, socklen_t dest_len)
Send data to a particular destination (always non-blocking).
Definition: network.c:838
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Purpose must be GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE.
static char buf[2048]
#define COMMUNICATOR_ADDRESS_PREFIX
Address prefix used by the communicator.
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.
int GNUNET_CONTAINER_multishortmap_put(struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
Handle to a node in a heap.
struct GNUNET_PeerIdentity sender
Identity of the inititor of the UDP broadcast.
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)...
static struct GNUNET_CRYPTO_EddsaPrivateKey * my_private_key
Our private key.
struct sockaddr * address
Address of the other peer.
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_CONTAINER_MultiShortmap * GNUNET_CONTAINER_multishortmap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
static void mq_destroy(struct GNUNET_MQ_Handle *mq, void *impl_state)
Signature of functions implementing the destruction of a message queue.
Heap with the minimum cost at the root.
void GNUNET_STATISTICS_set(struct GNUNET_STATISTICS_Handle *handle, const char *name, uint64_t value, int make_persistent)
Set statistic value for the peer.
UDP message box.
void GNUNET_CONTAINER_heap_destroy(struct GNUNET_CONTAINER_Heap *heap)
Destroys the heap.
struct sockaddr * sa
Sender&#39;s address of the interface.
A 512-bit hashcode.
socklen_t salen
Number of bytes in sa.
static void sock_read(void *cls)
Socket read task.
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:1615
static void mq_cancel(struct GNUNET_MQ_Handle *mq, void *impl_state)
Implementation function that cancels the currently sent message.
static void setup_receiver_mq(struct ReceiverAddress *receiver)
Setup the MQ for the receiver.
static int res
struct SharedSecret * next
Kept in a DLL.
Information we track per receiving address we have recently been in contact with (encryption to recei...
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:419
struct GNUNET_HashCode cmac
CMAC of the base key being acknowledged.
Node in the heap.
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:690
static void try_handle_plaintext(struct SenderAddress *sender, const void *buf, size_t buf_size)
Test if we have received a valid message in plaintext.
Shared secret we generated for a particular sender or receiver.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
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...
unsigned int num_secrets
Length of the DLL at ss_head.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_min(struct GNUNET_TIME_Relative t1, struct GNUNET_TIME_Relative t2)
Return the minimum of two relative time values.
Definition: time.c:272
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender&#39;s signature of type GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
struct GNUNET_HashCode key
The key used in the DHT.
Internal representation of the hash map.
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...
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static void mq_send(struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *msg, void *impl_state)
Signature of functions implementing the sending functionality of a message queue. ...
static struct GNUNET_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:303
static int get_receiver_delete_it(void *cls, const struct GNUNET_PeerIdentity *target, void *value)
Iterator over all receivers to clean up.
static struct GNUNET_CONTAINER_Heap * senders_heap
Expiration heap for senders (contains struct SenderAddress)
const char * name
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:376
static struct GNUNET_MQ_Envelope * ac
Handle to current GNUNET_PEERINFO_add_peer() operation.
struct GNUNET_NT_InterfaceScanner * GNUNET_NT_scanner_init(void)
Initialize the address characterization client handle.
Definition: nt.c:399
struct GNUNET_HashCode h_address
Hash of the sender&#39;s UDP address.
static void get_kid(const struct GNUNET_HashCode *msec, uint32_t serial, struct GNUNET_ShortHashCode *kid)
Compute kid.
static struct GNUNET_FS_SearchContext * sc
Definition: gnunet-search.c:37
Loopback (same host).
Definition: gnunet_nt_lib.h:44
static struct GNUNET_NETWORK_Handle * udp_sock
Our socket.
static void do_pad(gcry_cipher_hd_t out_cipher, char *dgram, size_t pad_size)
Pad dgram by pad_size using out_cipher.
unsigned int active_kce_count
Number of active KCN entries.
struct SharedSecret * prev
Kept in a DLL.
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:805
static struct GNUNET_CONTAINER_MultiPeerMap * senders
Senders (map from peer identity to struct SenderAddress)
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.
struct UDPBroadcast bcm
Message we broadcast on this interface.
static void decrypt_box(const struct UDPBox *box, size_t box_len, struct KeyCacheEntry *kce)
We received a box with matching kce.
void GNUNET_CONTAINER_multishortmap_destroy(struct GNUNET_CONTAINER_MultiShortmap *map)
Destroy a hash map.
struct GNUNET_CONTAINER_Heap * GNUNET_CONTAINER_heap_create(enum GNUNET_CONTAINER_HeapOrder order)
Create a new heap.
an ECC signature using EdDSA.
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:272
Standard IPv6 header.
static struct GNUNET_CONTAINER_Heap * receivers_heap
Expiration heap for receivers (contains struct ReceiverAddress)
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
#define KCN_THRESHOLD
If we fall below this number of available KCNs, we generate additional ACKs until we reach KCN_TARGET...
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:84
unsigned int acks_available
Number of BOX keys from ACKs we have currently available for this receiver.
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.
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won&#39;t work on W32;.
struct GNUNET_CONTAINER_HeapNode * hn
Entry in sender expiration heap.
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.
struct GNUNET_PeerIdentity sender
Sender&#39;s identity.
uint32_t sequence_max
Sequence acknowledgement limit.
struct ipv6_mreq mcreq
If this is an IPv6 interface, this is the request we use to join/leave the group. ...
The identity of the host (wraps the signing key of the peer).
#define AES_KEY_SIZE
AES key size.
static void reschedule_receiver_timeout(struct ReceiverAddress *receiver)
Increment receiver timeout due to activity.
struct ReceiverAddress * receiver
Receiver we use this shared secret with, or NULL.
Context information to be used while searching for operation contexts.
Definition: testbed_api.c:220
static void get_iv_key(const struct GNUNET_HashCode *msec, uint32_t serial, char key[(256/8)], char iv[(96/8)])
Compute key and iv.
struct GNUNET_TRANSPORT_ApplicationHandle * GNUNET_TRANSPORT_application_init(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the TRANSPORT application client handle.
#define GNUNET_PACKED
gcc-ism to get packed structs.
static uint16_t port
Port number.
Definition: gnunet-bcd.c:81
configuration data
Definition: configuration.c:83
void * GNUNET_CONTAINER_multishortmap_get(const struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key)
Given a key find a value in the map matching the key.
socklen_t address_len
Length of the address.
this is an outbound connection (transport initiated)
#define ADDRESS_VALIDITY_PERIOD
How long do we believe our addresses to remain up (before the other peer should revalidate).
Information we track per sender address we have recently been in contact with (we decrypt messages fr...
Signature we use to verify that the broadcast was really made by the peer that claims to have made it...
#define INTERFACE_SCAN_FREQUENCY
How often do we scan for changes to our network interfaces?
void GNUNET_TRANSPORT_communicator_notify(struct GNUNET_TRANSPORT_CommunicatorHandle *ch, const struct GNUNET_PeerIdentity *pid, const char *comm, const struct GNUNET_MessageHeader *header)
The communicator asks the transport service to route a message via a different path to another commun...
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
static void receiver_destroy(struct ReceiverAddress *receiver)
Destroys a receiving state due to timeout or shutdown.
#define GNUNET_log(kind,...)
UDP key acknowledgement.
Entry in list of pending tasks.
Definition: scheduler.c:131
static struct GNUNET_STATISTICS_Handle * stats
For logging statistics.
int GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
Signature we use to verify that the ephemeral key was really chosen by the specified sender...
static struct GNUNET_SCHEDULER_Task * read_task
ID of read task.
handle to a socket
Definition: network.c:46
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 ...
void GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
Fill block with a random values.
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 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...
static struct SharedSecret * setup_shared_secret_enc(const struct GNUNET_CRYPTO_EcdhePrivateKey *ephemeral, struct ReceiverAddress *receiver)
Setup shared secret for encryption.
struct GNUNET_SCHEDULER_Task * broadcast_task
Task for this broadcast interface.
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
static void calculate_cmac(struct SharedSecret *ss)
Calcualte cmac from master in ss.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Header for all communications.
static int find_sender_by_address(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
Find existing struct SenderAddress by matching addresses.
Time for absolute times used by GNUnet, in microseconds.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
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...
#define GNUNET_YES
Definition: gnunet_common.h:77
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:1467
struct SenderAddress * sender
Return value to set if we found a match.
struct GNUNET_TRANSPORT_QueueHandle * qh
handle for this queue with the ch.
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, 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...
unsigned int GNUNET_CONTAINER_multipeermap_size(const struct GNUNET_CONTAINER_MultiPeerMap *map)
Get the number of key-value pairs in the map.
#define MAX_SQN_DELTA
What is the maximum delta between KCN sequence numbers that we allow.
struct GNUNET_CRYPTO_EcdhePublicKey ephemeral
Ephemeral key used by the sender.
static struct GNUNET_TRANSPORT_CommunicatorHandle * ch
Our environment.
struct GNUNET_PeerIdentity target
To whom are we talking to.
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
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".
ssize_t GNUNET_NETWORK_socket_recvfrom(const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length, struct sockaddr *src_addr, socklen_t *addrlen)
Read data from a socket (always non-blocking).
Definition: network.c:742
struct GNUNET_TIME_Absolute timeout
Timeout for this receiver address.
struct GNUNET_MQ_Handle * mq
Message queue we are providing for the ch.
static void kce_generate(struct SharedSecret *ss, uint32_t seq)
Setup key cache entry for sequence number seq and shared secret ss.
void * GNUNET_CONTAINER_heap_remove_node(struct GNUNET_CONTAINER_HeapNode *node)
Removes a node from the heap.
static void do_shutdown(void *cls)
Shutdown the UNIX communicator.
Interface we broadcast our presence on.
Handle to the TRANSPORT subsystem for application management.
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:655
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:484
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
struct BroadcastInterface * prev
Kept in a DLL.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static void sender_destroy(struct SenderAddress *sender)
Functions with this signature are called whenever we need to close a sender&#39;s state due to timeout...
Handle returned to identify the internal data structure the transport API has created to manage a mes...
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:548
int GNUNET_CONTAINER_multipeermap_get_multiple(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, GNUNET_CONTAINER_PeerMapIterator it, void *it_cls)
Iterate over all entries in the map that match a particular key.
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
#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:78
struct BroadcastInterface * next
Kept in a DLL.
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:900
struct SharedSecret * ss_head
Shared secrets we received from target, first used is head.
static struct GNUNET_SCHEDULER_Task * timeout_task
ID of timeout task.
const struct sockaddr * address
Address we are looking for.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
static int try_decrypt(const struct SharedSecret *ss, const char tag[(128/8)], uint32_t serial, const char *in_buf, size_t in_buf_size, char *out_buf)
Try to decrypt buf using shared secret ss and key/iv derived using serial.
enum GNUNET_NetworkType nt
Which network type does this queue use?