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 
160 {
165 
170 
175 
180 
186 };
187 
188 
193 struct InitialKX
194 {
195 
200 
205  char gcm_tag[GCM_TAG_SIZE];
206 };
207 
208 
214 {
219 
223  struct GNUNET_CRYPTO_EddsaSignature sender_sig;
224 
229  struct GNUNET_TIME_AbsoluteNBO monotonic_time;
230 
231  /* followed by messages */
232 
233  /* padding may follow actual messages */
234 };
235 
236 
241 struct UDPAck
242 {
243 
247  struct GNUNET_MessageHeader header;
248 
253  uint32_t sequence_max GNUNET_PACKED;
254 
258  struct GNUNET_HashCode cmac;
259 };
260 
261 
271 {
276 
281 
285  struct GNUNET_HashCode h_address;
286 };
287 
288 
296 {
297 
302 
307  struct GNUNET_CRYPTO_EddsaSignature sender_sig;
308 };
309 
310 
315 struct UDPBox
316 {
317 
326 
334  char gcm_tag[GCM_TAG_SIZE];
335 };
336 
337 
339 
343 struct SharedSecret;
344 
345 
351 {
352 
357 
362 
371 
375  struct SharedSecret *ss;
376 
380  uint32_t sequence_number;
381 };
382 
383 
388 struct SenderAddress;
389 
394 struct ReceiverAddress;
395 
400 {
405 
410 
415 
420 
425 
430 
434  struct GNUNET_HashCode master;
435 
439  struct GNUNET_HashCode cmac;
440 
445  uint32_t sequence_used;
446 
453 
457  unsigned int active_kce_count;
458 };
459 
460 
466 {
467 
471  struct GNUNET_PeerIdentity target;
472 
477 
482 
487 
491  struct sockaddr *address;
492 
496  socklen_t address_len;
497 
502 
506  unsigned int num_secrets;
507 
512 };
513 
514 
520 {
521 
525  struct GNUNET_PeerIdentity target;
526 
531 
536 
542 
546  struct sockaddr *address;
547 
551  socklen_t address_len;
552 
557 
562 
567 
572 
576  size_t mtu;
577 
581  unsigned int num_secrets;
582 
587  unsigned int acks_available;
588 
593 };
594 
595 
600 {
601 
606 
611 
616 
620  struct sockaddr *sa;
621 
625  struct sockaddr *ba;
626 
630  struct UDPBroadcast bcm;
631 
636  struct ipv6_mreq mcreq;
637 
641  socklen_t salen;
642 
646  int found;
647 };
648 
649 
654 
659 
664 
669 
674 
679 
684 
689 
694 
699 
704 
709 
714 
718 static int have_v6_socket;
719 
724 
729 
733 static const struct GNUNET_CONFIGURATION_Handle *cfg;
734 
739 
744 
748 static struct GNUNET_NAT_Handle *nat;
749 
753 static uint16_t my_port;
754 
755 
761 static void
763 {
764  if (AF_INET6 == bi->sa->sa_family)
765  {
766  /* Leave the multicast group */
768  IPPROTO_IPV6,
769  IPV6_LEAVE_GROUP,
770  &bi->mcreq,
771  sizeof (bi->mcreq)))
772  {
774  }
775  }
776  GNUNET_CONTAINER_DLL_remove (bi_head, bi_tail, bi);
778  GNUNET_free (bi->sa);
779  GNUNET_free_non_null (bi->ba);
780  GNUNET_free (bi);
781 }
782 
783 
789 static void
791 {
792  struct GNUNET_MQ_Handle *mq;
793 
795  "Disconnecting receiver for peer `%s'\n",
796  GNUNET_i2s (&receiver->target));
797  if (NULL != (mq = receiver->mq))
798  {
799  receiver->mq = NULL;
800  GNUNET_MQ_destroy (mq);
801  }
802  if (NULL != receiver->qh)
803  {
805  receiver->qh = NULL;
806  }
809  &receiver->target,
810  receiver));
811  GNUNET_assert (receiver == GNUNET_CONTAINER_heap_remove_node (receiver->hn));
812  GNUNET_STATISTICS_set (stats,
813  "# receivers active",
815  GNUNET_NO);
816  GNUNET_free (receiver->address);
817  GNUNET_free (receiver->foreign_addr);
818  GNUNET_free (receiver);
819 }
820 
821 
827 static void
829 {
830  struct SharedSecret *ss = kce->ss;
831 
832  ss->active_kce_count--;
835  &kce->kid,
836  kce));
837  GNUNET_free (kce);
838 }
839 
840 
848 static void
849 get_kid (const struct GNUNET_HashCode *msec,
850  uint32_t serial,
851  struct GNUNET_ShortHashCode *kid)
852 {
853  uint32_t sid = htonl (serial);
854 
855  GNUNET_CRYPTO_hkdf (kid,
856  sizeof (*kid),
857  GCRY_MD_SHA512,
858  GCRY_MD_SHA256,
859  &sid,
860  sizeof (sid),
861  msec,
862  sizeof (*msec),
863  "UDP-KID",
864  strlen ("UDP-KID"),
865  NULL,
866  0);
867 }
868 
869 
876 static void
877 kce_generate (struct SharedSecret *ss, uint32_t seq)
878 {
879  struct KeyCacheEntry *kce;
880 
881  GNUNET_assert (0 < seq);
882  kce = GNUNET_new (struct KeyCacheEntry);
883  kce->ss = ss;
884  kce->sequence_number = seq;
885  get_kid (&ss->master, seq, &kce->kid);
887  ss->active_kce_count++;
889  key_cache,
890  &kce->kid,
891  kce,
893  GNUNET_STATISTICS_set (stats,
894  "# KIDs active",
896  GNUNET_NO);
897 }
898 
899 
905 static void
907 {
908  struct SenderAddress *sender;
909  struct ReceiverAddress *receiver;
910  struct KeyCacheEntry *kce;
911 
912  if (NULL != (sender = ss->sender))
913  {
914  GNUNET_CONTAINER_DLL_remove (sender->ss_head, sender->ss_tail, ss);
915  sender->num_secrets--;
916  }
917  if (NULL != (receiver = ss->receiver))
918  {
919  GNUNET_CONTAINER_DLL_remove (receiver->ss_head, receiver->ss_tail, ss);
920  receiver->num_secrets--;
921  receiver->acks_available -= (ss->sequence_allowed - ss->sequence_used);
922  }
923  while (NULL != (kce = ss->kce_head))
924  kce_destroy (kce);
925  GNUNET_STATISTICS_update (stats, "# Secrets active", -1, GNUNET_NO);
926  GNUNET_STATISTICS_set (stats,
927  "# KIDs active",
929  GNUNET_NO);
930  GNUNET_free (ss);
931 }
932 
933 
940 static void
942 {
943  GNUNET_assert (
944  GNUNET_YES ==
945  GNUNET_CONTAINER_multipeermap_remove (senders, &sender->target, sender));
946  GNUNET_assert (sender == GNUNET_CONTAINER_heap_remove_node (sender->hn));
947  GNUNET_STATISTICS_set (stats,
948  "# senders active",
950  GNUNET_NO);
951  GNUNET_free (sender->address);
952  GNUNET_free (sender);
953 }
954 
955 
964 static void
965 get_iv_key (const struct GNUNET_HashCode *msec,
966  uint32_t serial,
967  char key[AES_KEY_SIZE],
968  char iv[AES_IV_SIZE])
969 {
970  uint32_t sid = htonl (serial);
971  char res[AES_KEY_SIZE + AES_IV_SIZE];
972 
973  GNUNET_CRYPTO_hkdf (res,
974  sizeof (res),
975  GCRY_MD_SHA512,
976  GCRY_MD_SHA256,
977  &sid,
978  sizeof (sid),
979  msec,
980  sizeof (*msec),
981  "UDP-IV-KEY",
982  strlen ("UDP-IV-KEY"),
983  NULL,
984  0);
985  memcpy (key, res, AES_KEY_SIZE);
986  memcpy (iv, &res[AES_KEY_SIZE], AES_IV_SIZE);
987 }
988 
989 
995 static void
997 {
998  sender->timeout =
1001 }
1002 
1003 
1009 static void
1011 {
1012  receiver->timeout =
1015  receiver->timeout.abs_value_us);
1016 }
1017 
1018 
1024 static void
1025 check_timeouts (void *cls)
1026 {
1027  struct GNUNET_TIME_Relative st;
1028  struct GNUNET_TIME_Relative rt;
1029  struct GNUNET_TIME_Relative delay;
1030  struct ReceiverAddress *receiver;
1031  struct SenderAddress *sender;
1032 
1033  (void) cls;
1034  timeout_task = NULL;
1036  while (NULL != (receiver = GNUNET_CONTAINER_heap_peek (receivers_heap)))
1037  {
1039  if (0 != rt.rel_value_us)
1040  break;
1041  receiver_destroy (receiver);
1042  }
1044  while (NULL != (sender = GNUNET_CONTAINER_heap_peek (senders_heap)))
1045  {
1047  if (0 != st.rel_value_us)
1048  break;
1049  sender_destroy (sender);
1050  }
1051  delay = GNUNET_TIME_relative_min (rt, st);
1052  if (delay.rel_value_us < GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1053  timeout_task = GNUNET_SCHEDULER_add_delayed (delay, &check_timeouts, NULL);
1054 }
1055 
1056 
1062 static void
1064 {
1065  GNUNET_CRYPTO_hkdf (&ss->cmac,
1066  sizeof (ss->cmac),
1067  GCRY_MD_SHA512,
1068  GCRY_MD_SHA256,
1069  "CMAC",
1070  strlen ("CMAC"),
1071  &ss->master,
1072  sizeof (ss->master),
1073  "UDP-CMAC",
1074  strlen ("UDP-CMAC"),
1075  NULL,
1076  0);
1077 }
1078 
1079 
1088 static void
1090  const void *plaintext,
1091  size_t plaintext_len)
1092 {
1093  const struct GNUNET_MessageHeader *hdr = plaintext;
1094 
1095  while (ntohs (hdr->size) < plaintext_len)
1096  {
1097  GNUNET_STATISTICS_update (stats,
1098  "# bytes given to core",
1099  ntohs (hdr->size),
1100  GNUNET_NO);
1101  (void)
1103  &sender->target,
1104  hdr,
1106  NULL /* no flow control possible */
1107  ,
1108  NULL);
1109  /* move on to next message, if any */
1110  plaintext_len -= ntohs (hdr->size);
1111  if (plaintext_len < sizeof (*hdr))
1112  break;
1113  hdr = plaintext + ntohs (hdr->size);
1114  }
1115  GNUNET_STATISTICS_update (stats,
1116  "# bytes padding discarded",
1117  plaintext_len,
1118  GNUNET_NO);
1119 }
1120 
1121 
1130 static void
1131 setup_cipher (const struct GNUNET_HashCode *msec,
1132  uint32_t serial,
1133  gcry_cipher_hd_t *cipher)
1134 {
1135  char key[AES_KEY_SIZE];
1136  char iv[AES_IV_SIZE];
1137 
1138  gcry_cipher_open (cipher,
1139  GCRY_CIPHER_AES256 /* low level: go for speed */,
1140  GCRY_CIPHER_MODE_GCM,
1141  0 /* flags */);
1142  get_iv_key (msec, serial, key, iv);
1143  gcry_cipher_setkey (*cipher, key, sizeof (key));
1144  gcry_cipher_setiv (*cipher, iv, sizeof (iv));
1145 }
1146 
1147 
1160 static int
1161 try_decrypt (const struct SharedSecret *ss,
1162  const char tag[GCM_TAG_SIZE],
1163  uint32_t serial,
1164  const char *in_buf,
1165  size_t in_buf_size,
1166  char *out_buf)
1167 {
1168  gcry_cipher_hd_t cipher;
1169 
1170  setup_cipher (&ss->master, serial, &cipher);
1171  GNUNET_assert (
1172  0 ==
1173  gcry_cipher_decrypt (cipher, out_buf, in_buf_size, in_buf, in_buf_size));
1174  if (0 != gcry_cipher_checktag (cipher, tag, GCM_TAG_SIZE))
1175  {
1176  gcry_cipher_close (cipher);
1177  GNUNET_STATISTICS_update (stats,
1178  "# AEAD authentication failures",
1179  1,
1180  GNUNET_NO);
1181  return GNUNET_SYSERR;
1182  }
1183  gcry_cipher_close (cipher);
1184  return GNUNET_OK;
1185 }
1186 
1187 
1194 static struct SharedSecret *
1196 {
1197  struct SharedSecret *ss;
1198 
1199  ss = GNUNET_new (struct SharedSecret);
1200  GNUNET_CRYPTO_eddsa_ecdh (my_private_key, ephemeral, &ss->master);
1201  return ss;
1202 }
1203 
1204 
1212 static struct SharedSecret *
1214  struct ReceiverAddress *receiver)
1215 {
1216  struct SharedSecret *ss;
1217 
1218  ss = GNUNET_new (struct SharedSecret);
1219  GNUNET_CRYPTO_ecdh_eddsa (ephemeral,
1220  &receiver->target.public_key,
1221  &ss->master);
1222  calculate_cmac (ss);
1223  ss->receiver = receiver;
1224  GNUNET_CONTAINER_DLL_insert (receiver->ss_head, receiver->ss_tail, ss);
1225  receiver->num_secrets++;
1226  GNUNET_STATISTICS_update (stats, "# Secrets active", 1, GNUNET_NO);
1227  return ss;
1228 }
1229 
1230 
1238 static void
1240 
1241 
1252 static int
1253 handle_ack (void *cls, const struct GNUNET_PeerIdentity *pid, void *value)
1254 {
1255  const struct UDPAck *ack = cls;
1256  struct ReceiverAddress *receiver = value;
1257 
1258  (void) pid;
1259  for (struct SharedSecret *ss = receiver->ss_head; NULL != ss; ss = ss->next)
1260  {
1261  if (0 == memcmp (&ack->cmac, &ss->cmac, sizeof (struct GNUNET_HashCode)))
1262  {
1263  uint32_t allowed;
1264 
1265  allowed = ntohl (ack->sequence_max);
1266 
1267  if (allowed > ss->sequence_allowed)
1268  {
1269  receiver->acks_available += (allowed - ss->sequence_allowed);
1270  if ((allowed - ss->sequence_allowed) == receiver->acks_available)
1271  {
1272  /* we just incremented from zero => MTU change! */
1273  setup_receiver_mq (receiver);
1274  }
1275  ss->sequence_allowed = allowed;
1276  /* move ss to head to avoid discarding it anytime soon! */
1277  GNUNET_CONTAINER_DLL_remove (receiver->ss_head, receiver->ss_tail, ss);
1278  GNUNET_CONTAINER_DLL_insert (receiver->ss_head, receiver->ss_tail, ss);
1279  }
1280  return GNUNET_NO;
1281  }
1282  }
1283  return GNUNET_YES;
1284 }
1285 
1286 
1295 static void
1297  const void *buf,
1298  size_t buf_size)
1299 {
1300  const struct GNUNET_MessageHeader *hdr =
1301  (const struct GNUNET_MessageHeader *) buf;
1302  const struct UDPAck *ack = (const struct UDPAck *) buf;
1303  uint16_t type;
1304 
1305  if (sizeof (*hdr) > buf_size)
1306  return; /* not even a header */
1307  if (ntohs (hdr->size) > buf_size)
1308  return; /* not even a header */
1309  type = ntohs (hdr->type);
1310  switch (type)
1311  {
1313  /* lookup master secret by 'cmac', then update sequence_max */
1315  &sender->target,
1316  &handle_ack,
1317  (void *) ack);
1318  /* There could be more messages after the ACK, handle those as well */
1319  buf += ntohs (hdr->size);
1320  buf_size -= ntohs (hdr->size);
1321  pass_plaintext_to_core (sender, buf, buf_size);
1322  break;
1324  /* skip padding */
1325  break;
1326  default:
1327  pass_plaintext_to_core (sender, buf, buf_size);
1328  }
1329 }
1330 
1331 
1340 static void
1342 {
1343  GNUNET_assert (NULL != ss->sender);
1344  /* drop ancient KeyCacheEntries */
1345  while ((NULL != ss->kce_head) &&
1346  (MAX_SQN_DELTA <
1348  kce_destroy (ss->kce_tail);
1349  if (ss->active_kce_count < KCN_THRESHOLD)
1350  {
1351  struct UDPAck ack;
1352 
1353  while (ss->active_kce_count < KCN_TARGET)
1354  kce_generate (ss, ++ss->sequence_allowed);
1356  ack.header.size = htons (sizeof (ack));
1357  ack.sequence_max = htonl (ss->sequence_allowed);
1358  ack.cmac = ss->cmac;
1360  &ss->sender->target,
1362  &ack.header);
1363  }
1364 }
1365 
1366 
1374 static void
1375 decrypt_box (const struct UDPBox *box,
1376  size_t box_len,
1377  struct KeyCacheEntry *kce)
1378 {
1379  struct SharedSecret *ss = kce->ss;
1380  char out_buf[box_len - sizeof (*box)];
1381 
1382  GNUNET_assert (NULL != ss->sender);
1383  if (GNUNET_OK != try_decrypt (ss,
1384  box->gcm_tag,
1385  kce->sequence_number,
1386  (const char *) &box[1],
1387  sizeof (out_buf),
1388  out_buf))
1389  {
1390  GNUNET_STATISTICS_update (stats,
1391  "# Decryption failures with valid KCE",
1392  1,
1393  GNUNET_NO);
1394  kce_destroy (kce);
1395  return;
1396  }
1397  kce_destroy (kce);
1398  GNUNET_STATISTICS_update (stats,
1399  "# bytes decrypted with BOX",
1400  sizeof (out_buf),
1401  GNUNET_NO);
1402  try_handle_plaintext (ss->sender, out_buf, sizeof (out_buf));
1403  consider_ss_ack (ss);
1404 }
1405 
1406 
1410 struct SearchContext
1411 {
1415  const struct sockaddr *address;
1416 
1420  socklen_t address_len;
1421 
1426 };
1427 
1428 
1437 static int
1439  const struct GNUNET_PeerIdentity *key,
1440  void *value)
1441 {
1442  struct SearchContext *sc = cls;
1443  struct SenderAddress *sender = value;
1444 
1445  if ((sender->address_len == sc->address_len) &&
1446  (0 == memcmp (sender->address, sc->address, sender->address_len)))
1447  {
1448  sc->sender = sender;
1449  return GNUNET_NO; /* stop iterating! */
1450  }
1451  return GNUNET_YES;
1452 }
1453 
1454 
1466 static struct SenderAddress *
1468  const struct sockaddr *address,
1469  socklen_t address_len)
1470 {
1471  struct SenderAddress *sender;
1472  struct SearchContext sc = {.address = address,
1473  .address_len = address_len,
1474  .sender = NULL};
1475 
1477  target,
1479  &sc);
1480  if (NULL != sc.sender)
1481  {
1483  return sc.sender;
1484  }
1485  sender = GNUNET_new (struct SenderAddress);
1486  sender->target = *target;
1487  sender->address = GNUNET_memdup (address, address_len);
1488  sender->address_len = address_len;
1490  senders,
1491  &sender->target,
1492  sender,
1494  GNUNET_STATISTICS_set (stats,
1495  "# senders active",
1497  GNUNET_NO);
1498  sender->timeout =
1500  sender->hn = GNUNET_CONTAINER_heap_insert (senders_heap,
1501  sender,
1502  sender->timeout.abs_value_us);
1503  sender->nt = GNUNET_NT_scanner_get_type (is, address, address_len);
1504  if (NULL == timeout_task)
1505  timeout_task = GNUNET_SCHEDULER_add_now (&check_timeouts, NULL);
1506  return sender;
1507 }
1508 
1509 
1517 static int
1519  const struct UDPConfirmation *uc)
1520 {
1521  struct UdpHandshakeSignature uhs;
1522 
1524  uhs.purpose.size = htonl (sizeof (uhs));
1525  uhs.sender = uc->sender;
1526  uhs.receiver = my_identity;
1527  uhs.ephemeral = *ephemeral;
1528  uhs.monotonic_time = uc->monotonic_time;
1530  &uhs.purpose,
1531  &uc->sender_sig,
1532  &uc->sender.public_key);
1533 }
1534 
1535 
1544 static char *
1545 sockaddr_to_udpaddr_string (const struct sockaddr *address,
1546  socklen_t address_len)
1547 {
1548  char *ret;
1549 
1550  switch (address->sa_family)
1551  {
1552  case AF_INET:
1553  GNUNET_asprintf (&ret,
1554  "%s-%s",
1556  GNUNET_a2s (address, address_len));
1557  break;
1558  case AF_INET6:
1559  GNUNET_asprintf (&ret,
1560  "%s-%s",
1562  GNUNET_a2s (address, address_len));
1563  break;
1564  default:
1565  GNUNET_assert (0);
1566  }
1567  return ret;
1568 }
1569 
1570 
1576 static void
1577 sock_read (void *cls)
1578 {
1579  struct sockaddr_storage sa;
1580  socklen_t salen = sizeof (sa);
1581  char buf[UINT16_MAX];
1582  ssize_t rcvd;
1583 
1584  (void) cls;
1586  udp_sock,
1587  &sock_read,
1588  NULL);
1589  rcvd = GNUNET_NETWORK_socket_recvfrom (udp_sock,
1590  buf,
1591  sizeof (buf),
1592  (struct sockaddr *) &sa,
1593  &salen);
1594  if (-1 == rcvd)
1595  {
1597  return;
1598  }
1599 
1600  /* first, see if it is a UDPBox */
1601  if (rcvd > sizeof (struct UDPBox))
1602  {
1603  const struct UDPBox *box;
1604  struct KeyCacheEntry *kce;
1605 
1606  box = (const struct UDPBox *) buf;
1607  kce = GNUNET_CONTAINER_multishortmap_get (key_cache, &box->kid);
1608  if (NULL != kce)
1609  {
1610  decrypt_box (box, (size_t) rcvd, kce);
1611  return;
1612  }
1613  }
1614 
1615  /* next, check if it is a broadcast */
1616  if (sizeof (struct UDPBroadcast) == rcvd)
1617  {
1618  const struct UDPBroadcast *ub;
1619  struct UdpBroadcastSignature uhs;
1620 
1621  ub = (const struct UDPBroadcast *) buf;
1623  uhs.purpose.size = htonl (sizeof (uhs));
1624  uhs.sender = ub->sender;
1625  GNUNET_CRYPTO_hash (&sa, salen, &uhs.h_address);
1626  if (GNUNET_OK ==
1628  &uhs.purpose,
1629  &ub->sender_sig,
1630  &ub->sender.public_key))
1631  {
1632  char *addr_s;
1633  enum GNUNET_NetworkType nt;
1634 
1635  addr_s =
1636  sockaddr_to_udpaddr_string ((const struct sockaddr *) &sa, salen);
1637  GNUNET_STATISTICS_update (stats, "# broadcasts received", 1, GNUNET_NO);
1638  /* use our own mechanism to determine network type */
1639  nt =
1640  GNUNET_NT_scanner_get_type (is, (const struct sockaddr *) &sa, salen);
1641  GNUNET_TRANSPORT_application_validate (ah, &ub->sender, nt, addr_s);
1642  GNUNET_free (addr_s);
1643  return;
1644  }
1645  /* continue with KX, mostly for statistics... */
1646  }
1647 
1648 
1649  /* finally, test if it is a KX */
1650  if (rcvd < sizeof (struct UDPConfirmation) + sizeof (struct InitialKX))
1651  {
1652  GNUNET_STATISTICS_update (stats,
1653  "# messages dropped (no kid, too small for KX)",
1654  1,
1655  GNUNET_NO);
1656  return;
1657  }
1658 
1659  {
1660  const struct InitialKX *kx;
1661  struct SharedSecret *ss;
1662  char pbuf[rcvd - sizeof (struct InitialKX)];
1663  const struct UDPConfirmation *uc;
1664  struct SenderAddress *sender;
1665 
1666  kx = (const struct InitialKX *) buf;
1667  ss = setup_shared_secret_dec (&kx->ephemeral);
1668  if (GNUNET_OK != try_decrypt (ss,
1669  kx->gcm_tag,
1670  0,
1671  &buf[sizeof (*kx)],
1672  sizeof (pbuf),
1673  pbuf))
1674  {
1675  GNUNET_free (ss);
1677  stats,
1678  "# messages dropped (no kid, AEAD decryption failed)",
1679  1,
1680  GNUNET_NO);
1681  return;
1682  }
1683  uc = (const struct UDPConfirmation *) pbuf;
1684  if (GNUNET_OK != verify_confirmation (&kx->ephemeral, uc))
1685  {
1686  GNUNET_break_op (0);
1687  GNUNET_free (ss);
1688  GNUNET_STATISTICS_update (stats,
1689  "# messages dropped (sender signature invalid)",
1690  1,
1691  GNUNET_NO);
1692  return;
1693  }
1694  calculate_cmac (ss);
1695  sender = setup_sender (&uc->sender, (const struct sockaddr *) &sa, salen);
1696  ss->sender = sender;
1697  GNUNET_CONTAINER_DLL_insert (sender->ss_head, sender->ss_tail, ss);
1698  sender->num_secrets++;
1699  GNUNET_STATISTICS_update (stats, "# Secrets active", 1, GNUNET_NO);
1700  GNUNET_STATISTICS_update (stats,
1701  "# messages decrypted without BOX",
1702  1,
1703  GNUNET_NO);
1704  try_handle_plaintext (sender, &uc[1], sizeof (pbuf) - sizeof (*uc));
1705  consider_ss_ack (ss);
1706  if (sender->num_secrets > MAX_SECRETS)
1707  secret_destroy (sender->ss_tail);
1708  }
1709 }
1710 
1711 
1719 static struct sockaddr *
1720 udp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
1721 {
1722  struct sockaddr *in;
1723  unsigned int port;
1724  char dummy[2];
1725  char *colon;
1726  char *cp;
1727 
1728  if (1 == SSCANF (bindto, "%u%1s", &port, dummy))
1729  {
1730  /* interpreting value as just a PORT number */
1731  if (port > UINT16_MAX)
1732  {
1734  "BINDTO specification `%s' invalid: value too large for port\n",
1735  bindto);
1736  return NULL;
1737  }
1738  if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
1739  (GNUNET_YES ==
1742  "DISABLE_V6")))
1743  {
1744  struct sockaddr_in *i4;
1745 
1746  i4 = GNUNET_malloc (sizeof (struct sockaddr_in));
1747  i4->sin_family = AF_INET;
1748  i4->sin_port = htons ((uint16_t) port);
1749  *sock_len = sizeof (struct sockaddr_in);
1750  in = (struct sockaddr *) i4;
1751  }
1752  else
1753  {
1754  struct sockaddr_in6 *i6;
1755 
1756  i6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
1757  i6->sin6_family = AF_INET6;
1758  i6->sin6_port = htons ((uint16_t) port);
1759  *sock_len = sizeof (struct sockaddr_in6);
1760  in = (struct sockaddr *) i6;
1761  }
1762  return in;
1763  }
1764  cp = GNUNET_strdup (bindto);
1765  colon = strrchr (cp, ':');
1766  if (NULL != colon)
1767  {
1768  /* interpet value after colon as port */
1769  *colon = '\0';
1770  colon++;
1771  if (1 == SSCANF (colon, "%u%1s", &port, dummy))
1772  {
1773  /* interpreting value as just a PORT number */
1774  if (port > UINT16_MAX)
1775  {
1777  "BINDTO specification `%s' invalid: value too large for port\n",
1778  bindto);
1779  GNUNET_free (cp);
1780  return NULL;
1781  }
1782  }
1783  else
1784  {
1785  GNUNET_log (
1787  "BINDTO specification `%s' invalid: last ':' not followed by number\n",
1788  bindto);
1789  GNUNET_free (cp);
1790  return NULL;
1791  }
1792  }
1793  else
1794  {
1795  /* interpret missing port as 0, aka pick any free one */
1796  port = 0;
1797  }
1798  {
1799  /* try IPv4 */
1800  struct sockaddr_in v4;
1801 
1802  if (1 == inet_pton (AF_INET, cp, &v4))
1803  {
1804  v4.sin_port = htons ((uint16_t) port);
1805  in = GNUNET_memdup (&v4, sizeof (v4));
1806  *sock_len = sizeof (v4);
1807  GNUNET_free (cp);
1808  return in;
1809  }
1810  }
1811  {
1812  /* try IPv6 */
1813  struct sockaddr_in6 v6;
1814  const char *start;
1815 
1816  start = cp;
1817  if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
1818  {
1819  start++; /* skip over '[' */
1820  cp[strlen (cp) - 1] = '\0'; /* eat ']' */
1821  }
1822  if (1 == inet_pton (AF_INET6, start, &v6))
1823  {
1824  v6.sin6_port = htons ((uint16_t) port);
1825  in = GNUNET_memdup (&v6, sizeof (v6));
1826  *sock_len = sizeof (v6);
1827  GNUNET_free (cp);
1828  return in;
1829  }
1830  }
1831  /* #5528 FIXME (feature!): maybe also try getnameinfo()? */
1832  GNUNET_free (cp);
1833  return NULL;
1834 }
1835 
1836 
1844 static void
1845 do_pad (gcry_cipher_hd_t out_cipher, char *dgram, size_t pad_size)
1846 {
1847  char pad[pad_size];
1848 
1850  if (sizeof (pad) > sizeof (struct GNUNET_MessageHeader))
1851  {
1852  struct GNUNET_MessageHeader hdr =
1853  {.size = htons (sizeof (pad)),
1855 
1856  memcpy (pad, &hdr, sizeof (hdr));
1857  }
1858  GNUNET_assert (
1859  0 ==
1860  gcry_cipher_encrypt (out_cipher, dgram, sizeof (pad), pad, sizeof (pad)));
1861 }
1862 
1863 
1872 static void
1874  const struct GNUNET_MessageHeader *msg,
1875  void *impl_state)
1876 {
1877  struct ReceiverAddress *receiver = impl_state;
1878  uint16_t msize = ntohs (msg->size);
1879 
1880  GNUNET_assert (mq == receiver->mq);
1881  if (msize > receiver->mtu)
1882  {
1883  GNUNET_break (0);
1884  receiver_destroy (receiver);
1885  return;
1886  }
1887  reschedule_receiver_timeout (receiver);
1888 
1889  if (0 == receiver->acks_available)
1890  {
1891  /* use KX encryption method */
1892  struct UdpHandshakeSignature uhs;
1893  struct UDPConfirmation uc;
1894  struct InitialKX kx;
1895  struct GNUNET_CRYPTO_EcdhePrivateKey epriv;
1896  char dgram[receiver->mtu + sizeof (uc) + sizeof (kx)];
1897  size_t dpos;
1898  gcry_cipher_hd_t out_cipher;
1899  struct SharedSecret *ss;
1900 
1901  /* setup key material */
1903 
1904  ss = setup_shared_secret_enc (&epriv, receiver);
1905  setup_cipher (&ss->master, 0, &out_cipher);
1906  /* compute 'uc' */
1907  uc.sender = my_identity;
1908  uc.monotonic_time =
1911  uhs.purpose.size = htonl (sizeof (uhs));
1912  uhs.sender = my_identity;
1913  uhs.receiver = receiver->target;
1915  uhs.monotonic_time = uc.monotonic_time;
1916  GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (my_private_key,
1917  &uhs.purpose,
1918  &uc.sender_sig));
1919  /* Leave space for kx */
1920  dpos = sizeof (struct GNUNET_CRYPTO_EcdhePublicKey);
1921  /* Append encrypted uc to dgram */
1922  GNUNET_assert (0 == gcry_cipher_encrypt (out_cipher,
1923  &dgram[dpos],
1924  sizeof (uc),
1925  &uc,
1926  sizeof (uc)));
1927  dpos += sizeof (uc);
1928  /* Append encrypted payload to dgram */
1929  GNUNET_assert (
1930  0 == gcry_cipher_encrypt (out_cipher, &dgram[dpos], msize, msg, msize));
1931  dpos += msize;
1932  do_pad (out_cipher, &dgram[dpos], sizeof (dgram) - dpos);
1933  /* Datagram starts with kx */
1934  kx.ephemeral = uhs.ephemeral;
1935  GNUNET_assert (
1936  0 == gcry_cipher_gettag (out_cipher, kx.gcm_tag, sizeof (kx.gcm_tag)));
1937  gcry_cipher_close (out_cipher);
1938  memcpy (dgram, &kx, sizeof (kx));
1939  if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock,
1940  dgram,
1941  sizeof (dgram),
1942  receiver->address,
1943  receiver->address_len))
1946  return;
1947  } /* End of KX encryption method */
1948 
1949  /* begin "BOX" encryption method, scan for ACKs from tail! */
1950  for (struct SharedSecret *ss = receiver->ss_tail; NULL != ss; ss = ss->prev)
1951  {
1952  if (ss->sequence_used < ss->sequence_allowed)
1953  {
1954  char dgram[sizeof (struct UDPBox) + receiver->mtu];
1955  struct UDPBox *box;
1956  gcry_cipher_hd_t out_cipher;
1957  size_t dpos;
1958 
1959  box = (struct UDPBox *) dgram;
1960  ss->sequence_used++;
1961  get_kid (&ss->master, ss->sequence_used, &box->kid);
1962  setup_cipher (&ss->master, ss->sequence_used, &out_cipher);
1963  /* Append encrypted payload to dgram */
1964  dpos = sizeof (struct UDPBox);
1965  GNUNET_assert (
1966  0 == gcry_cipher_encrypt (out_cipher, &dgram[dpos], msize, msg, msize));
1967  dpos += msize;
1968  do_pad (out_cipher, &dgram[dpos], sizeof (dgram) - dpos);
1969  GNUNET_assert (0 == gcry_cipher_gettag (out_cipher,
1970  box->gcm_tag,
1971  sizeof (box->gcm_tag)));
1972  gcry_cipher_close (out_cipher);
1973  if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock,
1974  dgram,
1975  sizeof (dgram),
1976  receiver->address,
1977  receiver->address_len))
1980  receiver->acks_available--;
1981  if (0 == receiver->acks_available)
1982  {
1983  /* We have no more ACKs => MTU change! */
1984  setup_receiver_mq (receiver);
1985  }
1986  return;
1987  }
1988  }
1989  GNUNET_assert (0);
1990 }
1991 
1992 
2001 static void
2002 mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
2003 {
2004  struct ReceiverAddress *receiver = impl_state;
2005 
2006  if (mq == receiver->mq)
2007  {
2008  receiver->mq = NULL;
2009  receiver_destroy (receiver);
2010  }
2011 }
2012 
2013 
2020 static void
2021 mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
2022 {
2023  /* Cancellation is impossible with UDP; bail */
2024  GNUNET_assert (0);
2025 }
2026 
2027 
2037 static void
2038 mq_error (void *cls, enum GNUNET_MQ_Error error)
2039 {
2040  struct ReceiverAddress *receiver = cls;
2041 
2043  "MQ error in queue to %s: %d\n",
2044  GNUNET_i2s (&receiver->target),
2045  (int) error);
2046  receiver_destroy (receiver);
2047 }
2048 
2049 
2057 static void
2059 {
2060  size_t base_mtu;
2061 
2062  if (NULL != receiver->qh)
2063  {
2065  receiver->qh = NULL;
2066  }
2067  GNUNET_assert (NULL == receiver->mq);
2068  switch (receiver->address->sa_family)
2069  {
2070  case AF_INET:
2071  base_mtu = 1480 /* Ethernet MTU, 1500 - Ethernet header - VLAN tag */
2072  - sizeof (struct GNUNET_TUN_IPv4Header) /* 20 */
2073  - sizeof (struct GNUNET_TUN_UdpHeader) /* 8 */;
2074  break;
2075  case AF_INET6:
2076  base_mtu = 1280 /* Minimum MTU required by IPv6 */
2077  - sizeof (struct GNUNET_TUN_IPv6Header) /* 40 */
2078  - sizeof (struct GNUNET_TUN_UdpHeader) /* 8 */;
2079  break;
2080  default:
2081  GNUNET_assert (0);
2082  break;
2083  }
2084  if (0 == receiver->acks_available)
2085  {
2086  /* MTU based on full KX messages */
2087  receiver->mtu = base_mtu - sizeof (struct InitialKX) /* 48 */
2088  - sizeof (struct UDPConfirmation); /* 104 */
2089  }
2090  else
2091  {
2092  /* MTU based on BOXed messages */
2093  receiver->mtu = base_mtu - sizeof (struct UDPBox);
2094  }
2095  /* => Effective MTU for CORE will range from 1080 (IPv6 + KX) to
2096  1404 (IPv4 + Box) bytes, depending on circumstances... */
2097  if (NULL == receiver->mq)
2099  &mq_destroy,
2100  &mq_cancel,
2101  receiver,
2102  NULL,
2103  &mq_error,
2104  receiver);
2105  receiver->qh =
2107  &receiver->target,
2108  receiver->foreign_addr,
2109  receiver->mtu,
2110  receiver->nt,
2112  receiver->mq);
2113 }
2114 
2115 
2134 static int
2135 mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
2136 {
2137  struct ReceiverAddress *receiver;
2138  const char *path;
2139  struct sockaddr *in;
2140  socklen_t in_len;
2141 
2142  if (0 != strncmp (address,
2144  strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
2145  {
2146  GNUNET_break_op (0);
2147  return GNUNET_SYSERR;
2148  }
2149  path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
2150  in = udp_address_to_sockaddr (path, &in_len);
2151 
2152  receiver = GNUNET_new (struct ReceiverAddress);
2153  receiver->address = in;
2154  receiver->address_len = in_len;
2155  receiver->target = *peer;
2156  receiver->nt = GNUNET_NT_scanner_get_type (is, in, in_len);
2158  receivers,
2159  &receiver->target,
2160  receiver,
2162  receiver->timeout =
2164  receiver->hn = GNUNET_CONTAINER_heap_insert (receivers_heap,
2165  receiver,
2166  receiver->timeout.abs_value_us);
2167  GNUNET_STATISTICS_set (stats,
2168  "# receivers active",
2170  GNUNET_NO);
2171  receiver->foreign_addr =
2172  sockaddr_to_udpaddr_string (receiver->address, receiver->address_len);
2173  setup_receiver_mq (receiver);
2174  if (NULL == timeout_task)
2175  timeout_task = GNUNET_SCHEDULER_add_now (&check_timeouts, NULL);
2176  return GNUNET_OK;
2177 }
2178 
2179 
2188 static int
2190  const struct GNUNET_PeerIdentity *target,
2191  void *value)
2192 {
2193  struct ReceiverAddress *receiver = value;
2194 
2195  (void) cls;
2196  (void) target;
2197  receiver_destroy (receiver);
2198  return GNUNET_OK;
2199 }
2200 
2201 
2210 static int
2212  const struct GNUNET_PeerIdentity *target,
2213  void *value)
2214 {
2215  struct SenderAddress *sender = value;
2216 
2217  (void) cls;
2218  (void) target;
2219  sender_destroy (sender);
2220  return GNUNET_OK;
2221 }
2222 
2223 
2229 static void
2230 do_shutdown (void *cls)
2231 {
2232  if (NULL != nat)
2233  {
2234  GNUNET_NAT_unregister (nat);
2235  nat = NULL;
2236  }
2237  while (NULL != bi_head)
2238  bi_destroy (bi_head);
2239  if (NULL != broadcast_task)
2240  {
2241  GNUNET_SCHEDULER_cancel (broadcast_task);
2242  broadcast_task = NULL;
2243  }
2244  if (NULL != read_task)
2245  {
2246  GNUNET_SCHEDULER_cancel (read_task);
2247  read_task = NULL;
2248  }
2249  if (NULL != udp_sock)
2250  {
2252  udp_sock = NULL;
2253  }
2256  NULL);
2261  GNUNET_CONTAINER_heap_destroy (senders_heap);
2262  GNUNET_CONTAINER_heap_destroy (receivers_heap);
2263  if (NULL != ch)
2264  {
2266  ch = NULL;
2267  }
2268  if (NULL != ah)
2269  {
2271  ah = NULL;
2272  }
2273  if (NULL != stats)
2274  {
2276  stats = NULL;
2277  }
2278  if (NULL != my_private_key)
2279  {
2280  GNUNET_free (my_private_key);
2281  my_private_key = NULL;
2282  }
2283  if (NULL != is)
2284  {
2286  is = NULL;
2287  }
2288 }
2289 
2290 
2300 static void
2301 enc_notify_cb (void *cls,
2302  const struct GNUNET_PeerIdentity *sender,
2303  const struct GNUNET_MessageHeader *msg)
2304 {
2305  const struct UDPAck *ack;
2306 
2307  (void) cls;
2308  if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK) ||
2309  (ntohs (msg->size) != sizeof (struct UDPAck)))
2310  {
2311  GNUNET_break_op (0);
2312  return;
2313  }
2314  ack = (const struct UDPAck *) msg;
2316  sender,
2317  &handle_ack,
2318  (void *) ack);
2319 }
2320 
2321 
2335 static void
2336 nat_address_cb (void *cls,
2337  void **app_ctx,
2338  int add_remove,
2340  const struct sockaddr *addr,
2341  socklen_t addrlen)
2342 {
2343  char *my_addr;
2345 
2346  if (GNUNET_YES == add_remove)
2347  {
2348  enum GNUNET_NetworkType nt;
2349 
2350  GNUNET_asprintf (&my_addr,
2351  "%s-%s",
2353  GNUNET_a2s (addr, addrlen));
2354  nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
2355  ai =
2357  my_addr,
2358  nt,
2360  GNUNET_free (my_addr);
2361  *app_ctx = ai;
2362  }
2363  else
2364  {
2365  ai = *app_ctx;
2367  *app_ctx = NULL;
2368  }
2369 }
2370 
2371 
2377 static void
2378 ifc_broadcast (void *cls)
2379 {
2380  struct BroadcastInterface *bi = cls;
2381  struct GNUNET_TIME_Relative delay;
2382 
2383  delay = BROADCAST_FREQUENCY;
2384  delay.rel_value_us =
2386  bi->broadcast_task =
2388 
2389  switch (bi->sa->sa_family)
2390  {
2391  case AF_INET: {
2392  static int yes = 1;
2393  static int no = 0;
2394  ssize_t sent;
2395 
2397  SOL_SOCKET,
2398  SO_BROADCAST,
2399  &yes,
2400  sizeof (int)))
2402  sent = GNUNET_NETWORK_socket_sendto (udp_sock,
2403  &bi->bcm,
2404  sizeof (bi->bcm),
2405  bi->ba,
2406  bi->salen);
2407  if (-1 == sent)
2410  SOL_SOCKET,
2411  SO_BROADCAST,
2412  &no,
2413  sizeof (int)))
2415  break;
2416  }
2417  case AF_INET6: {
2418  ssize_t sent;
2419  struct sockaddr_in6 dst;
2420 
2421  dst.sin6_family = AF_INET6;
2422  dst.sin6_port = htons (my_port);
2423  dst.sin6_addr = bi->mcreq.ipv6mr_multiaddr;
2424  dst.sin6_scope_id = ((struct sockaddr_in6 *) bi->ba)->sin6_scope_id;
2425 
2426  sent = GNUNET_NETWORK_socket_sendto (udp_sock,
2427  &bi->bcm,
2428  sizeof (bi->bcm),
2429  (const struct sockaddr *) &dst,
2430  sizeof (dst));
2431  if (-1 == sent)
2433  break;
2434  }
2435  default:
2436  GNUNET_break (0);
2437  break;
2438  }
2439 }
2440 
2441 
2456 static int
2457 iface_proc (void *cls,
2458  const char *name,
2459  int isDefault,
2460  const struct sockaddr *addr,
2461  const struct sockaddr *broadcast_addr,
2462  const struct sockaddr *netmask,
2463  socklen_t addrlen)
2464 {
2465  struct BroadcastInterface *bi;
2466  enum GNUNET_NetworkType network;
2467  struct UdpBroadcastSignature ubs;
2468 
2469  (void) cls;
2470  (void) netmask;
2471  if (NULL == addr)
2472  return GNUNET_YES; /* need to know our address! */
2473  network = GNUNET_NT_scanner_get_type (is, addr, addrlen);
2474  if (GNUNET_NT_LOOPBACK == network)
2475  {
2476  /* Broadcasting on loopback does not make sense */
2477  return GNUNET_YES;
2478  }
2479  for (bi = bi_head; NULL != bi; bi = bi->next)
2480  {
2481  if ((bi->salen == addrlen) && (0 == memcmp (addr, bi->sa, addrlen)))
2482  {
2483  bi->found = GNUNET_YES;
2484  return GNUNET_OK;
2485  }
2486  }
2487 
2488  if ((AF_INET6 == addr->sa_family) && (NULL == broadcast_addr))
2489  return GNUNET_OK; /* broadcast_addr is required for IPv6! */
2490  if ((AF_INET6 == addr->sa_family) && (GNUNET_YES != have_v6_socket))
2491  return GNUNET_OK; /* not using IPv6 */
2492 
2493  bi = GNUNET_new (struct BroadcastInterface);
2494  bi->sa = GNUNET_memdup (addr, addrlen);
2495  if (NULL != broadcast_addr)
2496  bi->ba = GNUNET_memdup (broadcast_addr, addrlen);
2497  bi->salen = addrlen;
2498  bi->found = GNUNET_YES;
2499  bi->bcm.sender = my_identity;
2501  ubs.purpose.size = htonl (sizeof (ubs));
2502  ubs.sender = my_identity;
2503  GNUNET_CRYPTO_hash (addr, addrlen, &ubs.h_address);
2504  GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (my_private_key,
2505  &ubs.purpose,
2506  &bi->bcm.sender_sig));
2508  GNUNET_CONTAINER_DLL_insert (bi_head, bi_tail, bi);
2509  if ((AF_INET6 == addr->sa_family) && (NULL != broadcast_addr))
2510  {
2511  /* Create IPv6 multicast request */
2512  const struct sockaddr_in6 *s6 =
2513  (const struct sockaddr_in6 *) broadcast_addr;
2514 
2515  GNUNET_assert (
2516  1 == inet_pton (AF_INET6, "FF05::13B", &bi->mcreq.ipv6mr_multiaddr));
2517 
2518  /* http://tools.ietf.org/html/rfc2553#section-5.2:
2519  *
2520  * IPV6_JOIN_GROUP
2521  *
2522  * Join a multicast group on a specified local interface. If the
2523  * interface index is specified as 0, the kernel chooses the local
2524  * interface. For example, some kernels look up the multicast
2525  * group in the normal IPv6 routing table and using the resulting
2526  * interface; we do this for each interface, so no need to use
2527  * zero (anymore...).
2528  */
2529  bi->mcreq.ipv6mr_interface = s6->sin6_scope_id;
2530 
2531  /* Join the multicast group */
2533  IPPROTO_IPV6,
2534  IPV6_JOIN_GROUP,
2535  &bi->mcreq,
2536  sizeof (bi->mcreq)))
2537  {
2539  }
2540  }
2541  return GNUNET_OK;
2542 }
2543 
2544 
2550 static void
2551 do_broadcast (void *cls)
2552 {
2553  struct BroadcastInterface *bin;
2554 
2555  (void) cls;
2556  for (struct BroadcastInterface *bi = bi_head; NULL != bi; bi = bi->next)
2557  bi->found = GNUNET_NO;
2559  for (struct BroadcastInterface *bi = bi_head; NULL != bi; bi = bin)
2560  {
2561  bin = bi->next;
2562  if (GNUNET_NO == bi->found)
2563  bi_destroy (bi);
2564  }
2566  &do_broadcast,
2567  NULL);
2568 }
2569 
2570 
2579 static void
2580 run (void *cls,
2581  char *const *args,
2582  const char *cfgfile,
2583  const struct GNUNET_CONFIGURATION_Handle *c)
2584 {
2585  char *bindto;
2586  struct sockaddr *in;
2587  socklen_t in_len;
2588  struct sockaddr_storage in_sto;
2589  socklen_t sto_len;
2590 
2591  (void) cls;
2592  cfg = c;
2593  if (GNUNET_OK !=
2596  "BINDTO",
2597  &bindto))
2598  {
2601  "BINDTO");
2602  return;
2603  }
2604 
2605  in = udp_address_to_sockaddr (bindto, &in_len);
2606  if (NULL == in)
2607  {
2609  "Failed to setup UDP socket address with path `%s'\n",
2610  bindto);
2611  GNUNET_free (bindto);
2612  return;
2613  }
2614  udp_sock =
2615  GNUNET_NETWORK_socket_create (in->sa_family, SOCK_DGRAM, IPPROTO_UDP);
2616  if (NULL == udp_sock)
2617  {
2619  GNUNET_free (in);
2620  GNUNET_free (bindto);
2621  return;
2622  }
2623  if (AF_INET6 == in->sa_family)
2625  if (GNUNET_OK != GNUNET_NETWORK_socket_bind (udp_sock, in, in_len))
2626  {
2628  GNUNET_NETWORK_socket_close (udp_sock);
2629  udp_sock = NULL;
2630  GNUNET_free (in);
2631  GNUNET_free (bindto);
2632  return;
2633  }
2634  /* We might have bound to port 0, allowing the OS to figure it out;
2635  thus, get the real IN-address from the socket */
2636  sto_len = sizeof (in_sto);
2637  if (0 != getsockname (GNUNET_NETWORK_get_fd (udp_sock),
2638  (struct sockaddr *) &in_sto,
2639  &sto_len))
2640  {
2641  memcpy (&in_sto, in, in_len);
2642  sto_len = in_len;
2643  }
2644  GNUNET_free (in);
2645  GNUNET_free (bindto);
2646  in = (struct sockaddr *) &in_sto;
2647  in_len = sto_len;
2649  "Bound to `%s'\n",
2650  GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
2651  switch (in->sa_family)
2652  {
2653  case AF_INET:
2654  my_port = ntohs (((struct sockaddr_in *) in)->sin_port);
2655  break;
2656  case AF_INET6:
2657  my_port = ntohs (((struct sockaddr_in6 *) in)->sin6_port);
2658  break;
2659  default:
2660  GNUNET_break (0);
2661  my_port = 0;
2662  }
2663  stats = GNUNET_STATISTICS_create ("C-UDP", cfg);
2667  receivers_heap =
2671  is = GNUNET_NT_scanner_init ();
2673  if (NULL == my_private_key)
2674  {
2675  GNUNET_log (
2677  _ (
2678  "Transport service is lacking key configuration settings. Exiting.\n"));
2680  return;
2681  }
2683  /* start reading */
2685  udp_sock,
2686  &sock_read,
2687  NULL);
2692  &mq_init,
2693  NULL,
2694  &enc_notify_cb,
2695  NULL);
2696  if (NULL == ch)
2697  {
2698  GNUNET_break (0);
2700  return;
2701  }
2703  if (NULL == ah)
2704  {
2705  GNUNET_break (0);
2707  return;
2708  }
2709  /* start broadcasting */
2710  if (GNUNET_YES !=
2713  "DISABLE_BROADCAST"))
2714  {
2715  broadcast_task = GNUNET_SCHEDULER_add_now (&do_broadcast, NULL);
2716  }
2717  nat = GNUNET_NAT_register (cfg,
2719  IPPROTO_UDP,
2720  1 /* one address */,
2721  (const struct sockaddr **) &in,
2722  &in_len,
2723  &nat_address_cb,
2724  NULL /* FIXME: support reversal: #5529 */,
2725  NULL /* closure */);
2726 }
2727 
2728 
2736 int
2737 main (int argc, char *const *argv)
2738 {
2739  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2741  int ret;
2742 
2743  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2744  return 2;
2745 
2746  ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc,
2747  argv,
2748  "gnunet-communicator-udp",
2749  _ ("GNUnet UDP communicator"),
2750  options,
2751  &run,
2752  NULL))
2753  ? 0
2754  : 1;
2755  GNUNET_free ((void *) argv);
2756  return ret;
2757 }
2758 
2759 
2760 /* 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:103
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:1005
#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:1268
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:303
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:72
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.
static struct in_addr dummy
Target "dummy" address.
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:1293
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:1521
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:245
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:522
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:81
#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:78
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:524
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:208
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:1246
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:552
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:1504
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:1273
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:968
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:1614
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:413
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:271
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:79
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:378
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:393
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:45
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:807
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)
#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:85
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:233
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.
#define SSCANF
Definition: plibc.h:691
static uint16_t port
Port number.
Definition: gnunet-bcd.c:79
configuration data
Definition: configuration.c:85
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.
static int inet_pton(int af, const char *cp, struct in_addr *buf)
Convert IPv4 address from text to binary form.
#define GNUNET_log(kind,...)
UDP key acknowledgement.
Entry in list of pending tasks.
Definition: scheduler.c:134
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:361
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:824
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:80
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:1478
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:856
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:654
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:486
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:604
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:84
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:1037
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:965
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?