GNUnet  0.11.x
gnunet-communicator-tcp.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2010-2014, 2018, 2019 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
35 #include "platform.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_protocols.h"
38 #include "gnunet_signatures.h"
39 #include "gnunet_constants.h"
40 #include "gnunet_nt_lib.h"
41 #include "gnunet_nat_service.h"
44 #include "transport.h"
51 #define ADDRESS_VALIDITY_PERIOD \
52  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
53 
62 #define DEFAULT_MAX_QUEUE_LENGTH 8
63 
68 #define BUF_SIZE (2 * 64 * 1024 + sizeof(struct TCPBox))
69 
73 #define DEFAULT_REKEY_INTERVAL GNUNET_TIME_UNIT_DAYS
74 
78 #define PROTO_QUEUE_TIMEOUT GNUNET_TIME_UNIT_MINUTES
79 
84 #define REKEY_MAX_BYTES (1024LLU * 1024 * 1024 * 4LLU)
85 
90 #define INITIAL_KX_SIZE \
91  (sizeof(struct GNUNET_CRYPTO_EcdhePublicKey) \
92  + sizeof(struct TCPConfirmation))
93 
94 
98 #define COMMUNICATOR_ADDRESS_PREFIX "tcp"
99 
103 #define COMMUNICATOR_CONFIG_SECTION "communicator-tcp"
104 
106 
107 
113 {
118 
123 
128 
133 
139 };
140 
141 
146 {
151 
155  struct GNUNET_CRYPTO_EddsaSignature sender_sig;
156 
161  struct GNUNET_TIME_AbsoluteNBO monotonic_time;
162 };
163 
164 
168 struct TCPBox
169 {
176  struct GNUNET_MessageHeader header;
177 
186  struct GNUNET_ShortHashCode hmac;
187 
188  /* followed by as may bytes of payload as indicated in @e header,
189  excluding the TCPBox itself! */
190 };
191 
192 
197 struct TCPRekey
198 {
202  struct GNUNET_MessageHeader header;
203 
212  struct GNUNET_ShortHashCode hmac;
213 
218 
222  struct GNUNET_CRYPTO_EddsaSignature sender_sig;
223 
228  struct GNUNET_TIME_AbsoluteNBO monotonic_time;
229 };
230 
231 
237 struct TCPFinish
238 {
242  struct GNUNET_MessageHeader header;
243 
252  struct GNUNET_ShortHashCode hmac;
253 };
254 
255 
257 
262 {
267 
272 };
273 
277 struct Queue
278 {
282  struct GNUNET_PeerIdentity target;
283 
288 
293 
298 
302  gcry_cipher_hd_t in_cipher;
303 
307  gcry_cipher_hd_t out_cipher;
308 
312  struct GNUNET_HashCode in_hmac;
313 
318  struct GNUNET_HashCode out_hmac;
319 
325 
330 
335 
339  struct sockaddr *address;
340 
346 
351  struct GNUNET_TIME_Absolute rekey_time;
352 
356  socklen_t address_len;
357 
362 
367 
371  unsigned long long bytes_in_queue;
372 
376  char cread_buf[BUF_SIZE];
377 
381  char cwrite_buf[BUF_SIZE];
382 
386  char pread_buf[UINT16_MAX + 1 + sizeof(struct TCPBox)];
387 
391  char pwrite_buf[UINT16_MAX + 1 + sizeof(struct TCPBox)];
392 
397  size_t cread_off;
398 
403  size_t cwrite_off;
404 
409  size_t pread_off;
410 
415  size_t pwrite_off;
416 
421 
429  unsigned int backpressure;
430 
435 
440 
445 
453 
458  int rekeyed;
459 };
460 
461 
467 {
471  struct ProtoQueue *next;
472 
476  struct ProtoQueue *prev;
477 
482 
487 
492 
497 
501  struct sockaddr *address;
502 
506  socklen_t address_len;
507 
512 
517  char ibuf[INITIAL_KX_SIZE];
518 
522  size_t ibuf_off;
523 };
524 
529 {
533  struct sockaddr *addr_ipv4;
534 
538  socklen_t *addr_len_ipv4;
539 
543  struct sockaddr *addr_ipv6;
544 
548  socklen_t *addr_len_ipv6;
549 
550 };
551 
555 struct Addresses
556 {
560  struct Addresses *next;
561 
565  struct Addresses *prev;
566 
570  struct sockaddr *addr;
571 
575  socklen_t addr_len;
576 
577 };
578 
579 
580 
584 static unsigned long long max_queue_length;
585 
590 
595 
600 
605 
610 
615 
619 static const struct GNUNET_CONFIGURATION_Handle *cfg;
620 
625 
629 static struct GNUNET_NAT_Handle *nat;
630 
634 static struct ProtoQueue *proto_head;
635 
639 static struct ProtoQueue *proto_tail;
640 
645 
650 
655 
660 
668 static void
669 listen_cb (void *cls);
670 
671 
679 static void
681 {
682  struct GNUNET_MQ_Handle *mq;
683  struct ListenTask *lt;
684  lt = GNUNET_new (struct ListenTask);
685  lt->listen_sock = queue->listen_sock;
686  lt->listen_task = queue->listen_task;
687 
689  "Disconnecting queue for peer `%s'\n",
690  GNUNET_i2s (&queue->target));
691  if (NULL != (mq = queue->mq))
692  {
693  queue->mq = NULL;
694  GNUNET_MQ_destroy (mq);
695  }
696  if (NULL != queue->qh)
697  {
699  queue->qh = NULL;
700  }
701  GNUNET_assert (
702  GNUNET_YES ==
703  GNUNET_CONTAINER_multipeermap_remove (queue_map, &queue->target, queue));
704  GNUNET_STATISTICS_set (stats,
705  "# queues active",
707  GNUNET_NO);
708  if (NULL != queue->read_task)
709  {
711  queue->read_task = NULL;
712  }
713  if (NULL != queue->write_task)
714  {
716  queue->write_task = NULL;
717  }
719  gcry_cipher_close (queue->in_cipher);
720  gcry_cipher_close (queue->out_cipher);
721  GNUNET_free (queue->address);
722  if (0 != queue->backpressure)
723  queue->destroyed = GNUNET_YES;
724  else
725  GNUNET_free (queue);
726 
727  if (NULL == lt->listen_task)
730  lt->listen_sock,
731  &listen_cb,
732  lt);
733 }
734 
735 
744 static void
745 calculate_hmac (struct GNUNET_HashCode *hmac_secret,
746  const void *buf,
747  size_t buf_size,
748  struct GNUNET_ShortHashCode *smac)
749 {
750  struct GNUNET_HashCode mac;
751 
752  GNUNET_CRYPTO_hmac_raw (hmac_secret,
753  sizeof(struct GNUNET_HashCode),
754  buf,
755  buf_size,
756  &mac);
757  /* truncate to `struct GNUNET_ShortHashCode` */
758  memcpy (smac, &mac, sizeof(struct GNUNET_ShortHashCode));
759  /* ratchet hmac key */
760  GNUNET_CRYPTO_hash (hmac_secret,
761  sizeof(struct GNUNET_HashCode),
762  hmac_secret);
763 }
764 
765 
772 static void
774 {
775  struct TCPFinish fin;
776 
777  memset (&fin, 0, sizeof(fin));
778  fin.header.size = htons (sizeof(fin));
780  calculate_hmac (&queue->out_hmac, &fin, sizeof(fin), &fin.hmac);
781  /* if there is any message left in pwrite_buf, we
782  overwrite it (possibly dropping the last message
783  from CORE hard here) */
784  memcpy (queue->pwrite_buf, &fin, sizeof(fin));
785  queue->pwrite_off = sizeof(fin);
786  /* This flag will ensure that #queue_write() no longer
787  notifies CORE about the possibility of sending
788  more data, and that #queue_write() will call
789  #queue_destroy() once the @c fin was fully written. */
790  queue->finishing = GNUNET_YES;
791 }
792 
793 
801 static void
803 {
804  queue->timeout =
806 }
807 
808 
814 static void
815 queue_read (void *cls);
816 
817 
825 static void
826 core_read_finished_cb (void *cls, int success)
827 {
828  struct Queue *queue = cls;
829  if (GNUNET_OK != success)
831  "# messages lost in communicator API towards CORE",
832  1,
833  GNUNET_NO);
834  queue->backpressure--;
835  /* handle deferred queue destruction */
836  if ((queue->destroyed) && (0 == queue->backpressure))
837  {
838  GNUNET_free (queue);
839  return;
840  }
841  reschedule_queue_timeout (queue);
842  /* possibly unchoke reading, now that CORE made progress */
843  if (NULL == queue->read_task)
844  queue->read_task =
846  queue->timeout),
847  queue->sock,
848  &queue_read,
849  queue);
850 }
851 
852 
862 static void
864  const void *plaintext,
865  size_t plaintext_len)
866 {
867  const struct GNUNET_MessageHeader *hdr = plaintext;
868  int ret;
869 
870  if (ntohs (hdr->size) != plaintext_len)
871  {
872  /* NOTE: If we ever allow multiple CORE messages in one
873  BOX, this will have to change! */
874  GNUNET_break (0);
875  return;
876  }
878  &queue->target,
879  hdr,
882  queue);
883  if (GNUNET_OK == ret)
884  queue->backpressure++;
885  GNUNET_break (GNUNET_NO != ret); /* backpressure not working!? */
886  if (GNUNET_SYSERR == ret)
888  "# bytes lost due to CORE not running",
889  plaintext_len,
890  GNUNET_NO);
891 }
892 
893 
903 static void
904 setup_cipher (const struct GNUNET_HashCode *dh,
905  const struct GNUNET_PeerIdentity *pid,
906  gcry_cipher_hd_t *cipher,
907  struct GNUNET_HashCode *hmac_key)
908 {
909  char key[256 / 8];
910  char ctr[128 / 8];
911 
912  gcry_cipher_open (cipher,
913  GCRY_CIPHER_AES256 /* low level: go for speed */,
914  GCRY_CIPHER_MODE_CTR,
915  0 /* flags */);
917  sizeof(key),
918  "TCP-key",
919  strlen ("TCP-key"),
920  dh,
921  sizeof(*dh),
922  pid,
923  sizeof(*pid),
924  NULL,
925  0));
926  gcry_cipher_setkey (*cipher, key, sizeof(key));
928  sizeof(ctr),
929  "TCP-ctr",
930  strlen ("TCP-ctr"),
931  dh,
932  sizeof(*dh),
933  pid,
934  sizeof(*pid),
935  NULL,
936  0));
937  gcry_cipher_setctr (*cipher, ctr, sizeof(ctr));
939  GNUNET_CRYPTO_kdf (hmac_key,
940  sizeof(struct GNUNET_HashCode),
941  "TCP-hmac",
942  strlen ("TCP-hmac"),
943  dh,
944  sizeof(*dh),
945  pid,
946  sizeof(*pid),
947  NULL,
948  0));
949 }
950 
951 
958 static void
960  struct Queue *queue)
961 {
962  struct GNUNET_HashCode dh;
963 
964  GNUNET_CRYPTO_eddsa_ecdh (my_private_key, ephemeral, &dh);
965  setup_cipher (&dh, &my_identity, &queue->in_cipher, &queue->in_hmac);
966 }
967 
968 
977 static void
978 do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
979 {
980  struct TcpHandshakeSignature thp;
982  thp.purpose.size = htonl (sizeof(thp));
983  thp.sender = queue->target;
984  thp.receiver = my_identity;
985  thp.ephemeral = rekey->ephemeral;
986  thp.monotonic_time = rekey->monotonic_time;
987  /* FIXME: check monotonic time is monotonic... */
988  if (GNUNET_OK !=
990  &thp,
991  &rekey->sender_sig,
992  &queue->target.public_key))
993  {
994  GNUNET_break (0);
995  queue_finish (queue);
996  return;
997  }
998  gcry_cipher_close (queue->in_cipher);
999  queue->rekeyed = GNUNET_YES;
1000  setup_in_cipher (&rekey->ephemeral, queue);
1001 }
1002 
1003 
1011 static size_t
1013 {
1014  const struct GNUNET_MessageHeader *hdr =
1015  (const struct GNUNET_MessageHeader *) queue->pread_buf;
1016  const struct TCPBox *box = (const struct TCPBox *) queue->pread_buf;
1017  const struct TCPRekey *rekey = (const struct TCPRekey *) queue->pread_buf;
1018  const struct TCPFinish *fin = (const struct TCPFinish *) queue->pread_buf;
1019  struct TCPRekey rekeyz;
1020  struct TCPFinish finz;
1021  struct GNUNET_ShortHashCode tmac;
1022  uint16_t type;
1023  size_t size = 0; /* make compiler happy */
1024 
1025  if (sizeof(*hdr) > queue->pread_off)
1026  return 0; /* not even a header */
1027  type = ntohs (hdr->type);
1028  switch (type)
1029  {
1031  /* Special case: header size excludes box itself! */
1032  if (ntohs (hdr->size) + sizeof(struct TCPBox) > queue->pread_off)
1033  return 0;
1034  calculate_hmac (&queue->in_hmac, &box[1], ntohs (hdr->size), &tmac);
1035  if (0 != memcmp (&tmac, &box->hmac, sizeof(tmac)))
1036  {
1037  GNUNET_break_op (0);
1038  queue_finish (queue);
1039  return 0;
1040  }
1041  pass_plaintext_to_core (queue, (const void *) &box[1], ntohs (hdr->size));
1042  size = ntohs (hdr->size) + sizeof(*box);
1043  break;
1044 
1046  if (sizeof(*rekey) > queue->pread_off)
1047  return 0;
1048  if (ntohs (hdr->size) != sizeof(*rekey))
1049  {
1050  GNUNET_break_op (0);
1051  queue_finish (queue);
1052  return 0;
1053  }
1054  rekeyz = *rekey;
1055  memset (&rekeyz.hmac, 0, sizeof(rekeyz.hmac));
1056  calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
1057  if (0 != memcmp (&tmac, &rekey->hmac, sizeof(tmac)))
1058  {
1059  GNUNET_break_op (0);
1060  queue_finish (queue);
1061  return 0;
1062  }
1063  do_rekey (queue, rekey);
1064  size = ntohs (hdr->size);
1065  break;
1066 
1068  if (sizeof(*fin) > queue->pread_off)
1069  return 0;
1070  if (ntohs (hdr->size) != sizeof(*fin))
1071  {
1072  GNUNET_break_op (0);
1073  queue_finish (queue);
1074  return 0;
1075  }
1076  finz = *fin;
1077  memset (&finz.hmac, 0, sizeof(finz.hmac));
1078  calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
1079  if (0 != memcmp (&tmac, &fin->hmac, sizeof(tmac)))
1080  {
1081  GNUNET_break_op (0);
1082  queue_finish (queue);
1083  return 0;
1084  }
1085  /* handle FINISH by destroying queue */
1086  queue_destroy (queue);
1087  break;
1088 
1089  default:
1090  GNUNET_break_op (0);
1091  queue_finish (queue);
1092  return 0;
1093  }
1094  GNUNET_assert (0 != size);
1095  return size;
1096 }
1097 
1098 
1104 static void
1105 queue_read (void *cls)
1106 {
1107  struct Queue *queue = cls;
1108  struct GNUNET_TIME_Relative left;
1109  ssize_t rcvd;
1110 
1111  queue->read_task = NULL;
1112  rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
1113  &queue->cread_buf[queue->cread_off],
1114  BUF_SIZE - queue->cread_off);
1116  "Received %lu bytes from TCP queue\n", rcvd);
1117  if (-1 == rcvd)
1118  {
1119  if ((EAGAIN != errno) && (EINTR != errno))
1120  {
1122  queue_finish (queue);
1123  return;
1124  }
1125  /* try again */
1127  queue->read_task =
1128  GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read, queue);
1129  return;
1130  }
1131  if (0 != rcvd)
1132  reschedule_queue_timeout (queue);
1133  queue->cread_off += rcvd;
1134  while ((queue->pread_off < sizeof(queue->pread_buf)) &&
1135  (queue->cread_off > 0))
1136  {
1137  size_t max = GNUNET_MIN (sizeof(queue->pread_buf) - queue->pread_off,
1138  queue->cread_off);
1139  size_t done;
1140  size_t total;
1141  size_t old_pread_off = queue->pread_off;
1142 
1143  GNUNET_assert (0 ==
1144  gcry_cipher_decrypt (queue->in_cipher,
1145  &queue->pread_buf[queue->pread_off],
1146  max,
1147  queue->cread_buf,
1148  max));
1149  queue->pread_off += max;
1150  total = 0;
1151  while (0 != (done = try_handle_plaintext (queue)))
1152  {
1153  /* 'done' bytes of plaintext were used, shift buffer */
1154  GNUNET_assert (done <= queue->pread_off);
1155  /* NOTE: this memmove() could possibly sometimes be
1156  avoided if we pass 'total' into try_handle_plaintext()
1157  and use it at an offset into the buffer there! */
1158  memmove (queue->pread_buf,
1159  &queue->pread_buf[done],
1160  queue->pread_off - done);
1161  queue->pread_off -= done;
1162  total += done;
1163  /* The last plaintext was a rekey, abort for now */
1164  if (GNUNET_YES == queue->rekeyed)
1165  break;
1166  }
1167  /* when we encounter a rekey message, the decryption above uses the
1168  wrong key for everything after the rekey; in that case, we have
1169  to re-do the decryption at 'total' instead of at 'max'.
1170  However, we have to take into account that the plaintext buffer may have
1171  already contained data and not jumpt too far ahead in the ciphertext.
1172  If there is no rekey and the last message is incomplete (max > total),
1173  it is safe to keep the decryption so we shift by 'max' */
1174  if (GNUNET_YES == queue->rekeyed)
1175  {
1176  max = total - old_pread_off;
1177  queue->rekeyed = GNUNET_NO;
1178  queue->pread_off = 0;
1179  }
1180  memmove (queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
1181  queue->cread_off -= max;
1182  }
1183  if (BUF_SIZE == queue->cread_off)
1184  return; /* buffer full, suspend reading */
1186  if (0 != left.rel_value_us)
1187  {
1188  if (max_queue_length > queue->backpressure)
1189  {
1190  /* continue reading */
1192  queue->read_task =
1193  GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read, queue);
1194  }
1195  return;
1196  }
1198  "Queue %p was idle for %s, disconnecting\n",
1199  queue,
1202  GNUNET_YES));
1203  queue_finish (queue);
1204 }
1205 
1213 static struct sockaddr *
1214 tcp_address_to_sockaddr_numeric_v6 (socklen_t *sock_len, struct sockaddr_in6 v6,
1215  unsigned int port)
1216 {
1217  struct sockaddr *in;
1218 
1220  "1 address %s\n",
1221  GNUNET_a2s (in, *sock_len));
1222 
1223  v6.sin6_family = AF_INET6;
1224  v6.sin6_port = htons ((uint16_t) port);
1225 #if HAVE_SOCKADDR_IN_SIN_LEN
1226  v6.sin6_len = sizeof(sizeof(struct sockaddr_in6));
1227 #endif
1228  in = GNUNET_memdup (&v6, sizeof(v6));
1229  *sock_len = sizeof(struct sockaddr_in6);
1231  "address %s\n",
1232  GNUNET_a2s (in, *sock_len));
1233 
1234  return in;
1235 }
1236 
1244 static struct sockaddr *
1245 tcp_address_to_sockaddr_numeric_v4 (socklen_t *sock_len, struct sockaddr_in v4,
1246  unsigned int port)
1247 {
1248  struct sockaddr *in;
1249 
1250  v4.sin_family = AF_INET;
1251  v4.sin_port = htons ((uint16_t) port);
1252 #if HAVE_SOCKADDR_IN_SIN_LEN
1253  v4.sin_len = sizeof(struct sockaddr_in);
1254 #endif
1255  in = GNUNET_memdup (&v4, sizeof(v4));
1256  *sock_len = sizeof(struct sockaddr_in);
1257  return in;
1258 }
1259 
1266 static struct PortOnlyIpv4Ipv6 *
1267 tcp_address_to_sockaddr_port_only (const char *bindto, unsigned int *port)
1268 {
1269  struct PortOnlyIpv4Ipv6 *po;
1270  struct sockaddr_in *i4;
1271  struct sockaddr_in6 *i6;
1272  socklen_t sock_len_ipv4;
1273  socklen_t sock_len_ipv6;
1274 
1275  /* interpreting value as just a PORT number */
1276  if (*port > UINT16_MAX)
1277  {
1279  "BINDTO specification `%s' invalid: value too large for port\n",
1280  bindto);
1281  return NULL;
1282  }
1283 
1284  po = GNUNET_new (struct PortOnlyIpv4Ipv6);
1285 
1286  if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
1287  (GNUNET_YES ==
1290  "DISABLE_V6")))
1291  {
1292  i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
1293  po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
1294  *port);
1295  po->addr_len_ipv4 = &sock_len_ipv4;
1296  }
1297  else
1298  {
1299 
1300  i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
1301  po->addr_ipv4 = tcp_address_to_sockaddr_numeric_v4 (&sock_len_ipv4, *i4,
1302  *port);
1303  po->addr_len_ipv4 = &sock_len_ipv4;
1304 
1306  "3.5 address %s\n",
1307  GNUNET_a2s (po->addr_ipv4, sock_len_ipv4));
1308 
1309  i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
1310  po->addr_ipv6 = tcp_address_to_sockaddr_numeric_v6 (&sock_len_ipv6, *i6,
1311  *port);
1313  "3 address %s\n",
1314  GNUNET_a2s (po->addr_ipv6, sock_len_ipv6));
1315 
1316  po->addr_len_ipv6 = &sock_len_ipv6;
1317  }
1318  return po;
1319 }
1320 
1327 static char *
1328 extract_address (const char *bindto)
1329 {
1330 
1331  char *start;
1332  char *token;
1333  char *cp;
1334  char *rest = NULL;
1335 
1336  if (NULL == bindto)
1338  "bindto is NULL\n");
1339 
1340  cp = GNUNET_strdup (bindto);
1341  start = cp;
1342  if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
1343  {
1344  start++; /* skip over '['*/
1345  cp[strlen (cp) - 1] = '\0'; /* eat ']'*/
1346  }
1347  else {
1348  token = strtok_r (cp, "]", &rest);
1349  if (strlen (bindto) == strlen (token))
1350  {
1351  token = strtok_r (cp, ":", &rest);
1352  }
1353  else
1354  {
1355  token++;
1356  return token;
1357  }
1358  }
1359 
1360  // GNUNET_free(cp);
1361 
1362  return start;
1363 }
1364 
1371 static unsigned int
1372 extract_port (const char *addr_and_port)
1373 {
1374  unsigned int port;
1375  char dummy[2];
1376  char *token;
1377  char *addr;
1378  char *colon;
1379  char *cp;
1380  char *rest = NULL;
1381 
1382  if (NULL != addr_and_port)
1383  {
1384  cp = GNUNET_strdup (addr_and_port);
1385  token = strtok_r (cp, "]", &rest);
1386  if (strlen (addr_and_port) == strlen (token))
1387  {
1388  colon = strrchr (cp, ':');
1389  if (NULL == colon)
1390  {
1391  return 0;
1392  }
1393  addr = colon;
1394  addr++;
1395  }
1396  else
1397  {
1398  token = strtok_r (NULL, "]", &rest);
1399  if (NULL == token)
1400  {
1401  return 0;
1402  }
1403  else
1404  {
1405  addr = token;
1406  addr++;
1407  }
1408  }
1409 
1410 
1411  if (1 == sscanf (addr, "%u%1s", &port, dummy))
1412  {
1413  /* interpreting value as just a PORT number */
1414  if (port > UINT16_MAX)
1415  {
1417  "Port `%u' invalid: value too large for port\n",
1418  port);
1419  // GNUNET_free (cp);
1420  return 0;
1421  }
1422  }
1423  else
1424  {
1426  "BINDTO specification invalid: last ':' not followed by number\n");
1427  // GNUNET_free (cp);
1428  return 0;
1429  }
1430  }
1431  else
1432  {
1434  "return 0\n");
1435  /* interpret missing port as 0, aka pick any free one */
1436  port = 0;
1437  }
1438 
1439 
1440  return port;
1441 }
1442 
1450 static struct sockaddr *
1451 tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
1452 {
1453  struct sockaddr *in;
1454  unsigned int port;
1455  struct sockaddr_in v4;
1456  struct sockaddr_in6 v6;
1457  const char *start;
1458 
1459  // cp = GNUNET_strdup (bindto);
1460  start = extract_address (bindto);
1461 
1462  if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
1463  {
1464  // colon = strrchr (cp, ':');
1465  port = extract_port (bindto);
1466  in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port);
1467  }
1468  else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
1469  {
1470  // colon = strrchr (cp, ':');
1471  port = extract_port (bindto);
1472  in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port);
1473  }
1474 
1475  return in;
1476 }
1477 
1478 
1485 static void
1487 {
1488  struct GNUNET_HashCode dh;
1489 
1490  GNUNET_CRYPTO_ecdh_eddsa (&queue->ephemeral, &queue->target.public_key, &dh);
1491  /* we don't need the private key anymore, drop it! */
1492  memset (&queue->ephemeral, 0, sizeof(queue->ephemeral));
1493  setup_cipher (&dh, &queue->target, &queue->out_cipher, &queue->out_hmac);
1495  queue->rekey_left_bytes =
1497 }
1498 
1499 
1506 static void
1508 {
1509  struct TCPRekey rekey;
1510  struct TcpHandshakeSignature thp;
1511 
1512  GNUNET_assert (0 == queue->pwrite_off);
1513  memset (&rekey, 0, sizeof(rekey));
1516  rekey.header.size = ntohs (sizeof(rekey));
1518  rekey.monotonic_time =
1521  thp.purpose.size = htonl (sizeof(thp));
1522  thp.sender = my_identity;
1523  thp.receiver = queue->target;
1524  thp.ephemeral = rekey.ephemeral;
1525  thp.monotonic_time = rekey.monotonic_time;
1526  GNUNET_CRYPTO_eddsa_sign (my_private_key,
1527  &thp,
1528  &rekey.sender_sig);
1529  calculate_hmac (&queue->out_hmac, &rekey, sizeof(rekey), &rekey.hmac);
1530  /* Encrypt rekey message with 'old' cipher */
1531  GNUNET_assert (0 ==
1532  gcry_cipher_encrypt (queue->out_cipher,
1533  &queue->cwrite_buf[queue->cwrite_off],
1534  sizeof(rekey),
1535  &rekey,
1536  sizeof(rekey)));
1537  queue->cwrite_off += sizeof(rekey);
1538  /* Setup new cipher for successive messages */
1539  gcry_cipher_close (queue->out_cipher);
1540  setup_out_cipher (queue);
1541 }
1542 
1543 
1550 static void
1551 queue_write (void *cls)
1552 {
1553  struct Queue *queue = cls;
1554  ssize_t sent;
1555  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In queue write\n");
1556  queue->write_task = NULL;
1557  if (0 != queue->cwrite_off)
1558  {
1559  sent = GNUNET_NETWORK_socket_send (queue->sock,
1560  queue->cwrite_buf,
1561  queue->cwrite_off);
1563  "Sent %lu bytes to TCP queue\n", sent);
1564  if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1565  {
1567  queue_destroy (queue);
1568  return;
1569  }
1570  if (sent > 0)
1571  {
1572  size_t usent = (size_t) sent;
1573  queue->cwrite_off -= usent;
1574  memmove (queue->cwrite_buf,
1575  &queue->cwrite_buf[usent],
1576  queue->cwrite_off);
1577  reschedule_queue_timeout (queue);
1578  }
1579  }
1580  /* can we encrypt more? (always encrypt full messages, needed
1581  such that #mq_cancel() can work!) */
1582  if ((0 < queue->rekey_left_bytes) &&
1583  (queue->pwrite_off > 0) &&
1584  (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE))
1585  {
1587  "Encrypting %lu bytes\n", queue->pwrite_off);
1588  GNUNET_assert (0 ==
1589  gcry_cipher_encrypt (queue->out_cipher,
1590  &queue->cwrite_buf[queue->cwrite_off],
1591  queue->pwrite_off,
1592  queue->pwrite_buf,
1593  queue->pwrite_off));
1594  if (queue->rekey_left_bytes > queue->pwrite_off)
1595  queue->rekey_left_bytes -= queue->pwrite_off;
1596  else
1597  queue->rekey_left_bytes = 0;
1598  queue->cwrite_off += queue->pwrite_off;
1599  queue->pwrite_off = 0;
1600  }
1601  if ((0 == queue->pwrite_off) &&
1602  ((0 == queue->rekey_left_bytes) ||
1603  (0 ==
1605  {
1606  inject_rekey (queue);
1607  }
1608  if ((0 == queue->pwrite_off) && (! queue->finishing) &&
1609  (GNUNET_YES == queue->mq_awaits_continue))
1610  {
1611  queue->mq_awaits_continue = GNUNET_NO;
1613  }
1614  /* did we just finish writing 'finish'? */
1615  if ((0 == queue->cwrite_off) && (GNUNET_YES == queue->finishing))
1616  {
1618  "Finishing queue\n");
1619  queue_destroy (queue);
1620  return;
1621  }
1622  /* do we care to write more? */
1623  if ((0 < queue->cwrite_off) || (0 < queue->pwrite_off))
1624  queue->write_task =
1626  queue->sock,
1627  &queue_write,
1628  queue);
1629 }
1630 
1631 
1640 static void
1642  const struct GNUNET_MessageHeader *msg,
1643  void *impl_state)
1644 {
1645  struct Queue *queue = impl_state;
1646  uint16_t msize = ntohs (msg->size);
1647  struct TCPBox box;
1649  "In MQ send. Queue finishing: %s; write task running: %s\n",
1650  (GNUNET_YES == queue->finishing) ? "yes" : "no",
1651  (NULL == queue->write_task) ? "yes" : "no");
1652  GNUNET_assert (mq == queue->mq);
1653  queue->mq_awaits_continue = GNUNET_YES;
1654  if (GNUNET_YES == queue->finishing)
1655  return; /* this queue is dying, drop msg */
1656  GNUNET_assert (0 == queue->pwrite_off);
1658  box.header.size = htons (msize);
1659  calculate_hmac (&queue->out_hmac, msg, msize, &box.hmac);
1660  memcpy (&queue->pwrite_buf[queue->pwrite_off], &box, sizeof(box));
1661  queue->pwrite_off += sizeof(box);
1662  memcpy (&queue->pwrite_buf[queue->pwrite_off], msg, msize);
1663  queue->pwrite_off += msize;
1665  "%lu bytes of plaintext to send\n", queue->pwrite_off);
1666  GNUNET_assert (NULL != queue->sock);
1667  if (NULL == queue->write_task)
1668  queue->write_task =
1670  queue->sock,
1671  &queue_write,
1672  queue);
1673 }
1674 
1675 
1684 static void
1685 mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
1686 {
1687  struct Queue *queue = impl_state;
1688 
1689  if (mq == queue->mq)
1690  {
1691  queue->mq = NULL;
1692  queue_finish (queue);
1693  }
1694 }
1695 
1696 
1703 static void
1704 mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
1705 {
1706  struct Queue *queue = impl_state;
1707 
1708  GNUNET_assert (0 != queue->pwrite_off);
1709  queue->pwrite_off = 0;
1710 }
1711 
1712 
1722 static void
1723 mq_error (void *cls, enum GNUNET_MQ_Error error)
1724 {
1725  struct Queue *queue = cls;
1726 
1728  "MQ error in queue to %s: %d\n",
1729  GNUNET_i2s (&queue->target),
1730  (int) error);
1731  queue_finish (queue);
1732 }
1733 
1734 
1742 static void
1744 {
1745  queue->nt =
1746  GNUNET_NT_scanner_get_type (is, queue->address, queue->address_len);
1748  queue_map,
1749  &queue->target,
1750  queue,
1752  GNUNET_STATISTICS_set (stats,
1753  "# queues active",
1755  GNUNET_NO);
1756  queue->timeout =
1759  &mq_destroy,
1760  &mq_cancel,
1761  queue,
1762  NULL,
1763  &mq_error,
1764  queue);
1765  {
1766  char *foreign_addr;
1767 
1768  switch (queue->address->sa_family)
1769  {
1770  case AF_INET:
1771  GNUNET_asprintf (&foreign_addr,
1772  "%s-%s",
1774  GNUNET_a2s (queue->address, queue->address_len));
1775  break;
1776 
1777  case AF_INET6:
1778  GNUNET_asprintf (&foreign_addr,
1779  "%s-%s",
1781  GNUNET_a2s (queue->address, queue->address_len));
1782  break;
1783 
1784  default:
1785  GNUNET_assert (0);
1786  }
1788  &queue->target,
1789  foreign_addr,
1790  0 /* no MTU */,
1792  0, /* Priority */
1793  queue->nt,
1794  cs,
1795  queue->mq);
1796  GNUNET_free (foreign_addr);
1797  }
1798 }
1799 
1800 
1811 static void
1813  const struct GNUNET_CRYPTO_EcdhePublicKey *epub)
1814 {
1815  struct TcpHandshakeSignature ths;
1816  struct TCPConfirmation tc;
1817 
1818  memcpy (queue->cwrite_buf, epub, sizeof(*epub));
1819  queue->cwrite_off = sizeof(*epub);
1820  /* compute 'tc' and append in encrypted format to cwrite_buf */
1821  tc.sender = my_identity;
1822  tc.monotonic_time =
1825  ths.purpose.size = htonl (sizeof(ths));
1826  ths.sender = my_identity;
1827  ths.receiver = queue->target;
1828  ths.ephemeral = *epub;
1829  ths.monotonic_time = tc.monotonic_time;
1830  GNUNET_CRYPTO_eddsa_sign (my_private_key,
1831  &ths,
1832  &tc.sender_sig);
1833  GNUNET_assert (0 ==
1834  gcry_cipher_encrypt (queue->out_cipher,
1835  &queue->cwrite_buf[queue->cwrite_off],
1836  sizeof(tc),
1837  &tc,
1838  sizeof(tc)));
1839  queue->cwrite_off += sizeof(tc);
1840 }
1841 
1842 
1850 static void
1852 {
1853  struct GNUNET_CRYPTO_EcdhePublicKey epub;
1854 
1857  setup_out_cipher (queue);
1858  transmit_kx (queue, &epub);
1859 }
1860 
1861 
1873 static int
1875  struct TCPConfirmation *tc,
1876  char *ibuf)
1877 {
1878  struct TcpHandshakeSignature ths;
1879 
1880  GNUNET_assert (
1881  0 ==
1882  gcry_cipher_decrypt (queue->in_cipher,
1883  tc,
1884  sizeof(*tc),
1885  &ibuf[sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)],
1886  sizeof(*tc)));
1888  ths.purpose.size = htonl (sizeof(ths));
1889  ths.sender = tc->sender;
1890  ths.receiver = my_identity;
1891  memcpy (&ths.ephemeral, ibuf, sizeof(struct GNUNET_CRYPTO_EcdhePublicKey));
1892  ths.monotonic_time = tc->monotonic_time;
1893  /* FIXME: check monotonic time against previous mono times
1894  from this sender! */
1897  &ths,
1898  &tc->sender_sig,
1899  &tc->sender.public_key);
1900 }
1901 
1902 
1908 static void
1910 {
1911  if (NULL != pq->listen_task)
1912  {
1914  pq->listen_task = NULL;
1915  }
1916  if (NULL != pq->listen_sock)
1917  {
1919  pq->listen_sock = NULL;
1920  }
1922  GNUNET_free (pq->address);
1923  GNUNET_CONTAINER_DLL_remove (proto_head, proto_tail, pq);
1924  GNUNET_free (pq);
1925 }
1926 
1927 
1934 static void
1935 proto_read_kx (void *cls)
1936 {
1937  struct ProtoQueue *pq = cls;
1938  ssize_t rcvd;
1939  struct GNUNET_TIME_Relative left;
1940  struct Queue *queue;
1941  struct TCPConfirmation tc;
1942 
1943  pq->read_task = NULL;
1945  if (0 == left.rel_value_us)
1946  {
1947  free_proto_queue (pq);
1948  return;
1949  }
1950  rcvd = GNUNET_NETWORK_socket_recv (pq->sock,
1951  &pq->ibuf[pq->ibuf_off],
1952  sizeof(pq->ibuf) - pq->ibuf_off);
1954  "Received %lu bytes for KX\n", rcvd);
1955  if (-1 == rcvd)
1956  {
1957  if ((EAGAIN != errno) && (EINTR != errno))
1958  {
1960  free_proto_queue (pq);
1961  return;
1962  }
1963  /* try again */
1964  pq->read_task =
1966  return;
1967  }
1968  pq->ibuf_off += rcvd;
1969  if (pq->ibuf_off > sizeof(pq->ibuf))
1970  {
1971  /* read more */
1972  pq->read_task =
1974  return;
1975  }
1976  /* we got all the data, let's find out who we are talking to! */
1977  queue = GNUNET_new (struct Queue);
1978  setup_in_cipher ((const struct GNUNET_CRYPTO_EcdhePublicKey *) pq->ibuf,
1979  queue);
1980  if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, pq->ibuf))
1981  {
1983  "Invalid TCP KX received from %s\n",
1984  GNUNET_a2s (queue->address, queue->address_len));
1985  gcry_cipher_close (queue->in_cipher);
1986  GNUNET_free (queue);
1987  free_proto_queue (pq);
1988  return;
1989  }
1990  queue->address = pq->address; /* steals reference */
1991  queue->address_len = pq->address_len;
1992  queue->target = tc.sender;
1993  queue->listen_task = pq->listen_task;
1994  queue->listen_sock = pq->listen_sock;
1995  queue->sock = pq->sock;
1996  start_initial_kx_out (queue);
1998  queue->read_task =
2000  queue->sock,
2001  &queue_read,
2002  queue);
2003  queue->write_task =
2005  queue->sock,
2006  &queue_write,
2007  queue);
2008  GNUNET_CONTAINER_DLL_remove (proto_head, proto_tail, pq);
2009  GNUNET_free (pq);
2010 }
2011 
2012 
2020 static void
2021 listen_cb (void *cls)
2022 {
2023  struct sockaddr_storage in;
2024  socklen_t addrlen;
2025  struct GNUNET_NETWORK_Handle *sock;
2026  struct ProtoQueue *pq;
2027  struct ListenTask *lt;
2028 
2029  lt = cls;
2030 
2031  lt->listen_task = NULL;
2032  GNUNET_assert (NULL != lt->listen_sock);
2033  addrlen = sizeof(in);
2034  memset (&in, 0, sizeof(in));
2036  (struct sockaddr*) &in,
2037  &addrlen);
2038  if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno)))
2039  return; /* system limit reached, wait until connection goes down */
2041  lt->listen_sock,
2042  &listen_cb,
2043  lt);
2044  if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno)))
2045  return;
2046  if (NULL == sock)
2047  {
2049  return;
2050  }
2051  pq = GNUNET_new (struct ProtoQueue);
2052  pq->address_len = addrlen;
2053  pq->address = GNUNET_memdup (&in, addrlen);
2055  pq->sock = sock;
2057  pq->sock,
2058  &proto_read_kx,
2059  pq);
2060  GNUNET_CONTAINER_DLL_insert (proto_head, proto_tail, pq);
2061 }
2062 
2063 
2071 static void
2072 queue_read_kx (void *cls)
2073 {
2074  struct Queue *queue = cls;
2075  ssize_t rcvd;
2076  struct GNUNET_TIME_Relative left;
2077  struct TCPConfirmation tc;
2078 
2079  queue->read_task = NULL;
2081  if (0 == left.rel_value_us)
2082  {
2083  queue_destroy (queue);
2084  return;
2085  }
2086  rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
2087  &queue->cread_buf[queue->cread_off],
2088  BUF_SIZE - queue->cread_off);
2089  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %lu bytes for KX\n", rcvd);
2090  if (-1 == rcvd)
2091  {
2092  if ((EAGAIN != errno) && (EINTR != errno))
2093  {
2095  queue_destroy (queue);
2096  return;
2097  }
2098  queue->read_task =
2099  GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read_kx, queue);
2100  return;
2101  }
2102  queue->cread_off += rcvd;
2103  if (queue->cread_off < INITIAL_KX_SIZE)
2104  {
2105  /* read more */
2106  queue->read_task =
2107  GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read_kx, queue);
2108  return;
2109  }
2110  /* we got all the data, let's find out who we are talking to! */
2112  queue->cread_buf,
2113  queue);
2114  if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, queue->cread_buf))
2115  {
2117  "Invalid TCP KX received from %s\n",
2118  GNUNET_a2s (queue->address, queue->address_len));
2119  queue_destroy (queue);
2120  return;
2121  }
2122  if (0 !=
2123  memcmp (&tc.sender, &queue->target, sizeof(struct GNUNET_PeerIdentity)))
2124  {
2126  "Invalid sender in TCP KX received from %s\n",
2127  GNUNET_a2s (queue->address, queue->address_len));
2128  queue_destroy (queue);
2129  return;
2130  }
2131 
2132  /* update queue timeout */
2133  reschedule_queue_timeout (queue);
2134  /* prepare to continue with regular read task immediately */
2135  memmove (queue->cread_buf,
2136  &queue->cread_buf[INITIAL_KX_SIZE],
2137  queue->cread_off - (INITIAL_KX_SIZE));
2138  queue->cread_off -= INITIAL_KX_SIZE;
2139  if (0 < queue->cread_off)
2140  queue->read_task = GNUNET_SCHEDULER_add_now (&queue_read, queue);
2141 }
2142 
2161 static int
2162 mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
2163 {
2164  struct Queue *queue;
2165  const char *path;
2166  struct sockaddr *in;
2167  socklen_t in_len;
2168  struct GNUNET_NETWORK_Handle *sock;
2169 
2171  "Connecting to %s\n", address);
2172  if (0 != strncmp (address,
2174  strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
2175  {
2176  GNUNET_break_op (0);
2177  return GNUNET_SYSERR;
2178  }
2179  path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
2180  in = tcp_address_to_sockaddr (path, &in_len);
2181 
2182  if (NULL == in)
2183  {
2185  "Failed to setup TCP socket address\n");
2186  return GNUNET_SYSERR;
2187  }
2188 
2189  sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP);
2190  if (NULL == sock)
2191  {
2193  "socket(%d) failed: %s",
2194  in->sa_family,
2195  strerror (errno));
2196  GNUNET_free (in);
2197  return GNUNET_SYSERR;
2198  }
2199  if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, in_len)) &&
2200  (errno != EINPROGRESS))
2201  {
2203  "connect to `%s' failed: %s",
2204  address,
2205  strerror (errno));
2207  GNUNET_free (in);
2208  return GNUNET_SYSERR;
2209  }
2210 
2211  queue = GNUNET_new (struct Queue);
2212  queue->target = *peer;
2213  queue->address = in;
2214  queue->address_len = in_len;
2215  queue->sock = sock;
2217  // queue->mq_awaits_continue = GNUNET_YES;
2218  queue->read_task =
2220  queue->sock,
2221  &queue_read_kx,
2222  queue);
2223  start_initial_kx_out (queue);
2224  queue->write_task =
2226  queue->sock,
2227  &queue_write,
2228  queue);
2229  return GNUNET_OK;
2230 }
2231 
2240 static int
2242  const struct GNUNET_PeerIdentity *target,
2243  void *value)
2244 {
2245  struct Queue *queue = value;
2246 
2247  (void) cls;
2248  (void) target;
2249  queue_destroy (queue);
2250  return GNUNET_OK;
2251 }
2252 
2253 
2259 static void
2260 do_shutdown (void *cls)
2261 {
2262  while (NULL != proto_head)
2263  free_proto_queue (proto_head);
2264  if (NULL != nat)
2265  {
2266  GNUNET_NAT_unregister (nat);
2267  nat = NULL;
2268  }
2271  if (NULL != ch)
2272  {
2274  ch = NULL;
2275  }
2276  if (NULL != stats)
2277  {
2279  stats = NULL;
2280  }
2281  if (NULL != my_private_key)
2282  {
2283  GNUNET_free (my_private_key);
2284  my_private_key = NULL;
2285  }
2286  if (NULL != is)
2287  {
2289  is = NULL;
2290  }
2291 }
2292 
2293 
2305 static void
2306 enc_notify_cb (void *cls,
2307  const struct GNUNET_PeerIdentity *sender,
2308  const struct GNUNET_MessageHeader *msg)
2309 {
2310  (void) cls;
2311  (void) sender;
2312  (void) msg;
2313  GNUNET_break_op (0);
2314 }
2315 
2316 
2330 static void
2331 nat_address_cb (void *cls,
2332  void **app_ctx,
2333  int add_remove,
2335  const struct sockaddr *addr,
2336  socklen_t addrlen)
2337 {
2338  char *my_addr;
2340 
2342  "1 nat_address %s\n",
2343  GNUNET_a2s (addr, addrlen));
2344 
2345  if (GNUNET_YES == add_remove)
2346  {
2347  enum GNUNET_NetworkType nt;
2348 
2349  GNUNET_asprintf (&my_addr,
2350  "%s-%s",
2352  GNUNET_a2s (addr, addrlen));
2353  nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
2354  ai =
2356  my_addr,
2357  nt,
2359  GNUNET_free (my_addr);
2360  *app_ctx = ai;
2361  }
2362  else
2363  {
2364  ai = *app_ctx;
2366  *app_ctx = NULL;
2367  }
2368 }
2369 
2377 static int
2378 init_socket (const struct sockaddr *addr,
2379  socklen_t in_len)
2380 {
2381  struct sockaddr_storage in_sto;
2382  socklen_t sto_len;
2383  struct GNUNET_NETWORK_Handle *listen_sock;
2384  struct ListenTask *lt;
2385 
2386  if (NULL == addr)
2387  {
2389  "Address is NULL.\n");
2390  return GNUNET_SYSERR;
2391  }
2392 
2394  "4 address %s\n",
2395  GNUNET_a2s (addr, in_len));
2396 
2398  "address %s\n",
2399  GNUNET_a2s (addr, in_len));
2400 
2401  listen_sock =
2402  GNUNET_NETWORK_socket_create (addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
2403  if (NULL == listen_sock)
2404  {
2406  return GNUNET_SYSERR;
2407  }
2408 
2409  if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, addr, in_len))
2410  {
2412  GNUNET_NETWORK_socket_close (listen_sock);
2413  listen_sock = NULL;
2414  return GNUNET_SYSERR;
2415  }
2416 
2417  if (GNUNET_OK !=
2418  GNUNET_NETWORK_socket_listen (listen_sock,
2419  5))
2420  {
2422  "listen");
2423  GNUNET_NETWORK_socket_close (listen_sock);
2424  listen_sock = NULL;
2425  return GNUNET_SYSERR;
2426  }
2427 
2428  /* We might have bound to port 0, allowing the OS to figure it out;
2429  thus, get the real IN-address from the socket */
2430  sto_len = sizeof(in_sto);
2431 
2432  if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock),
2433  (struct sockaddr *) &in_sto,
2434  &sto_len))
2435  {
2436  memcpy (&in_sto, addr, in_len);
2437  sto_len = in_len;
2438  }
2439 
2440  addr = (struct sockaddr *) &in_sto;
2441  in_len = sto_len;
2443  "Bound to `%s'\n",
2444  GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
2445  stats = GNUNET_STATISTICS_create ("C-TCP", cfg);
2447 
2448  if (NULL == is)
2449  is = GNUNET_NT_scanner_init ();
2450 
2451  if (NULL == my_private_key)
2453  if (NULL == my_private_key)
2454  {
2455  GNUNET_log (
2457  _ (
2458  "Transport service is lacking key configuration settings. Exiting.\n"));
2459  GNUNET_RESOLVER_request_cancel (resolve_request_handle);
2461  return GNUNET_SYSERR;
2462  }
2464  /* start listening */
2465 
2466  lt = GNUNET_new (struct ListenTask);
2467  lt->listen_sock = listen_sock;
2468 
2470  listen_sock,
2471  &listen_cb,
2472  lt);
2473 
2474  if (NULL == queue_map)
2476 
2477  if (NULL == ch )
2482  &mq_init,
2483  NULL,
2484  &enc_notify_cb,
2485  NULL);
2486 
2487  if (NULL == ch)
2488  {
2489  GNUNET_break (0);
2490  GNUNET_RESOLVER_request_cancel (resolve_request_handle);
2492  return GNUNET_SYSERR;
2493  }
2494 
2495  return GNUNET_OK;
2496 
2497 }
2498 
2502 static void
2504 {
2505 
2506  struct sockaddr **saddrs;
2507  socklen_t *saddr_lens;
2508  int i;
2509  struct Addresses *pos;
2510 
2512  "nat here\n");
2513 
2514  i = 0;
2515  saddrs = GNUNET_malloc ((addrs_lens + 1) * sizeof(struct sockaddr *));
2516 
2518  "2 nat here\n");
2519 
2520  saddr_lens = GNUNET_malloc ((addrs_lens + 1) * sizeof(socklen_t));
2521 
2523  "3 nat here\n");
2524 
2525  for (pos = addrs_head; NULL != pos; pos = pos->next)
2526  {
2527 
2529  "5 nat here\n");
2530 
2531  saddr_lens[i] = addrs_head->addr_len;
2532  saddrs[i] = GNUNET_malloc (saddr_lens[i]);
2533  saddrs[i] = addrs_head->addr;
2534 
2535  i++;
2536 
2538  "6 nat here\n");
2539 
2540  }
2541 
2542  nat = GNUNET_NAT_register (cfg,
2544  IPPROTO_TCP,
2545  addrs_lens,
2546  (const struct sockaddr **) saddrs,
2547  saddr_lens,
2548  &nat_address_cb,
2549  NULL /* FIXME: support reversal: #5529 */,
2550  NULL /* closure */);
2551 
2552  if (NULL == nat)
2553  {
2554  GNUNET_break (0);
2555  GNUNET_RESOLVER_request_cancel (resolve_request_handle);
2557  }
2558 }
2559 
2563 static void
2564 add_addr (struct sockaddr *in, socklen_t in_len)
2565 {
2566 
2567  struct Addresses *saddrs;
2568 
2569  saddrs = GNUNET_new (struct Addresses);
2570  saddrs->addr = in;
2571  saddrs->addr_len = in_len;
2572  GNUNET_CONTAINER_DLL_insert (addrs_head, addrs_tail, saddrs);
2573  addrs_lens++;
2574 }
2575 
2583 static void
2585  const struct sockaddr *addr,
2586  socklen_t in_len)
2587 {
2588  struct sockaddr_in *v4;
2589  struct sockaddr_in6 *v6;
2590  struct sockaddr *in;
2591  unsigned int *port;
2592 
2593  port = cls;
2594  if (NULL != addr)
2595  {
2596  if (AF_INET == addr->sa_family)
2597  {
2598  v4 = (struct sockaddr_in *) addr;
2599  in = tcp_address_to_sockaddr_numeric_v4 (&in_len, *v4, *port);// _global);
2600  add_addr (in, in_len);
2601  }
2602  else if (AF_INET6 == addr->sa_family)
2603  {
2604  v6 = (struct sockaddr_in6 *) addr;
2605  in = tcp_address_to_sockaddr_numeric_v6 (&in_len, *v6, *port);// _global);
2606  add_addr (in, in_len);
2607  }
2608  else
2609  {
2611  "Address family %u not suitable (not AF_INET %u nor AF_INET6 %u \n",
2612  addr->sa_family,
2613  AF_INET,
2614  AF_INET6);
2615  return;
2616  }
2617  init_socket (in,
2618  in_len);
2619  }
2620  else
2621  {
2623  "Address is NULL. This might be an error or the resolver finished resolving.\n");
2624  nat_register ();
2625  }
2626 }
2627 
2636 static void
2637 run (void *cls,
2638  char *const *args,
2639  const char *cfgfile,
2640  const struct GNUNET_CONFIGURATION_Handle *c)
2641 {
2642  char *bindto;
2643  struct sockaddr *in;
2644  socklen_t in_len;
2645  struct sockaddr_in v4;
2646  struct sockaddr_in6 v6;
2647  char *start;
2648  unsigned int port;
2649  char dummy[2];
2650  char *rest = NULL;
2651  struct PortOnlyIpv4Ipv6 *po;
2652 
2653  (void) cls;
2654  cfg = c;
2655  if (GNUNET_OK !=
2658  "BINDTO",
2659  &bindto))
2660  {
2663  "BINDTO");
2664  return;
2665  }
2666  if (GNUNET_OK !=
2669  "MAX_QUEUE_LENGTH",
2670  &max_queue_length))
2672  if (GNUNET_OK !=
2675  "REKEY_INTERVAL",
2676  &rekey_interval))
2678 
2679 
2680  // cp = GNUNET_strdup (bindto);
2681  start = extract_address (bindto);
2682 
2683  if (1 == sscanf (bindto, "%u%1s", &port, dummy))
2684  {
2685  po = tcp_address_to_sockaddr_port_only (bindto, &port);
2686 
2687  if (NULL != &po->addr_ipv4)
2688  {
2689  init_socket (po->addr_ipv4, *po->addr_len_ipv4);
2690  add_addr (po->addr_ipv4, *po->addr_len_ipv4);
2691  }
2692 
2693  if (NULL != &po->addr_ipv6)
2694  {
2695  init_socket (po->addr_ipv6, *po->addr_len_ipv6);
2696  add_addr (po->addr_ipv6, *po->addr_len_ipv6);
2697  }
2698 
2699  nat_register ();
2700  }
2701  else if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
2702  {
2703  port = extract_port (bindto);
2704 
2705  in = tcp_address_to_sockaddr_numeric_v4 (&in_len, v4, port);
2706  init_socket (in, in_len);
2707  add_addr (in, in_len);
2708  nat_register ();
2709  }
2710  else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
2711  {
2712  port = extract_port (bindto);
2713  in = tcp_address_to_sockaddr_numeric_v6 (&in_len, v6, port);
2714  init_socket (in, in_len);
2715  add_addr (in, in_len);
2716  nat_register ();
2717  }
2718  else
2719  {
2720  port = extract_port (bindto);
2721 
2722  resolve_request_handle = GNUNET_RESOLVER_ip_get (strtok_r (bindto, ":",
2723  &rest),
2724  AF_UNSPEC,
2727  &port);
2728  }
2729  GNUNET_free (bindto);
2730 }
2731 
2732 
2740 int
2741 main (int argc, char *const *argv)
2742 {
2743  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2745  };
2746  int ret;
2747 
2748  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2749  return 2;
2750 
2751  ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc,
2752  argv,
2753  "gnunet-communicator-tcp",
2754  _ ("GNUnet TCP communicator"),
2755  options,
2756  &run,
2757  NULL))
2758  ? 0
2759  : 1;
2760  GNUNET_free_nz ((void *) argv);
2761  return ret;
2762 }
2763 
2764 
2765 /* end of gnunet-communicator-tcp.c */
#define GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE
Signature used by TCP communicator handshake,.
In case of port only configuration we like to bind to ipv4 and ipv6 addresses.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
int GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, int backlog)
Listen on a socket.
Definition: network.c:708
#define COMMUNICATOR_CONFIG_SECTION
Configuration section used by the communicator.
static void queue(const char *hostname)
Add hostname to the list of requests to be made.
Handle to the interface scanner.
Definition: nt.c:110
int rekeyed
GNUNET_YES if we just rekeyed and must thus possibly re-decrypt ciphertext.
char ibuf[(sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)+sizeof(struct TCPConfirmation))]
Buffer for reading all the information we need to upgrade from protoqueue to queue.
static void listen_cb(void *cls)
We have been notified that our listen socket has something to read.
Transmission is reliabile (with ACKs), i.e.
int GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
Return file descriptor for this network handle.
Definition: network.c:1080
static void setup_in_cipher(const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral, struct Queue *queue)
Setup cipher of queue for decryption.
static void queue_read_kx(void *cls)
Read from the socket of the queue until we have enough data to initialize the decryption logic and ca...
char cread_buf[(2 *64 *1024+sizeof(struct TCPBox))]
Buffer for reading ciphertext from network into.
static void init_socket_resolv(void *cls, const struct sockaddr *addr, socklen_t in_len)
This method is the callback called by the resolver API, and wraps method init_socket.
enum GNUNET_NetworkType GNUNET_NT_scanner_get_type(struct GNUNET_NT_InterfaceScanner *is, const struct sockaddr *addr, socklen_t addrlen)
Returns where the address is located: loopback, LAN or WAN.
Definition: nt.c:314
static void done()
struct sockaddr * addr_ipv4
Ipv4 address we like to bind to.
Handle for active NAT registrations.
Definition: nat_api.c:71
static void boot_queue(struct Queue *queue, enum GNUNET_TRANSPORT_ConnectionStatus cs)
Add the given queue to our internal data structure.
int GNUNET_CONFIGURATION_get_value_time(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, struct GNUNET_TIME_Relative *time)
Get a configuration value that should be a relative time.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_PeerIdentity target
To whom are we talking to.
void GNUNET_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh)
Cancel a request that is still pending with the resolver.
GNUNET_NAT_AddressClass
Some addresses contain sensitive information or are not suitable for global distribution.
int GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
static void core_read_finished_cb(void *cls, int success)
Core tells us it is done processing a message that transport received on a queue with status success...
uint64_t rel_value_us
The actual value.
int main(int argc, char *const *argv)
The main function for the UNIX communicator.
struct GNUNET_NETWORK_Handle * listen_sock
Listen socket.
#define GNUNET_CRYPTO_eddsa_sign(priv, ps, sig)
EdDSA sign a given block.
static struct GNUNET_NAT_Handle * nat
Connection to NAT service.
int finishing
Did we enqueue a finish message and are closing down the queue?
struct GNUNET_HashCode in_hmac
Shared secret for HMAC verification on incoming data.
struct GNUNET_PeerIdentity sender
Identity of the inititor of the TCP connection (TCP client).
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
socklen_t address_len
Length of the address.
ssize_t GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc, const void *buffer, size_t length)
Send data (always non-blocking).
Definition: network.c:816
common internal definitions for transport service
static struct GNUNET_PeerIdentity my_identity
Our public key.
static int init_socket(const struct sockaddr *addr, socklen_t in_len)
This method launch network interactions for each address we like to bind to.
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
size_t cread_off
At which offset in the ciphertext read buffer should we append more ciphertext for transmission next...
struct GNUNET_TRANSPORT_AddressIdentifier * GNUNET_TRANSPORT_communicator_address_add(struct GNUNET_TRANSPORT_CommunicatorHandle *ch, const char *address, enum GNUNET_NetworkType nt, struct GNUNET_TIME_Relative expiration)
Notify transport service about an address that this communicator provides for this peer...
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Setup communicator and launch network interactions.
struct GNUNET_CRYPTO_EcdhePublicKey ephemeral
Ephemeral key used by the sender.
void GNUNET_CRYPTO_hmac_raw(const void *key, size_t key_len, const void *plaintext, size_t plaintext_len, struct GNUNET_HashCode *hmac)
Calculate HMAC of a message (RFC 2104) TODO: Shouldn&#39; this be the standard hmac function and the abov...
Definition: crypto_hash.c:420
struct GNUNET_PeerIdentity sender
Sender&#39;s identity.
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:1331
DLL to store the addresses we like to register at NAT service.
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender&#39;s signature of type GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE.
#define GNUNET_TIME_UNIT_MINUTES
One minute.
unsigned int backpressure
How may messages did we pass from this queue to CORE for which we have yet to receive an acknoweldgem...
ssize_t GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length)
Read data from a connected socket (always non-blocking).
Definition: network.c:787
struct GNUNET_SCHEDULER_Task * read_task
ID of read task for this connection.
#define GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY
Signature used by TCP communicator rekey.
int destroyed
Did we technically destroy this queue, but kept the allocation around because of backpressure not bei...
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1457
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
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY
TCP communicator rekey message.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void proto_read_kx(void *cls)
Read from the socket of the proto queue until we have enough data to upgrade to full queue...
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition: scheduler.c:423
uint64_t rekey_left_bytes
How many more bytes may we sent with the current out_cipher before we should rekey?
#define BUF_SIZE
Size of our IO buffers for ciphertext data.
int GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Bind a socket to a particular address.
Definition: network.c:485
#define PROTO_QUEUE_TIMEOUT
How long do we wait until we must have received the initial KX?
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_write_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *wfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1588
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static void pass_plaintext_to_core(struct Queue *queue, const void *plaintext, size_t plaintext_len)
We received plaintext_len bytes of plaintext on queue.
socklen_t * addr_len_ipv6
Length of ipv6 address.
static struct PortOnlyIpv4Ipv6 * tcp_address_to_sockaddr_port_only(const char *bindto, unsigned int *port)
Convert TCP bind specification to a struct PortOnlyIpv4Ipv6 *
int GNUNET_CONTAINER_multipeermap_remove(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, const void *value)
Remove the given key-value pair from the map.
struct GNUNET_TIME_Absolute rekey_time
Until what time may we sent with the current out_cipher before we should rekey?
int addrs_lens
Number of addresses in the DLL for register at NAT service.
struct GNUNET_RESOLVER_RequestHandle * GNUNET_RESOLVER_ip_get(const char *hostname, int af, struct GNUNET_TIME_Relative timeout, GNUNET_RESOLVER_AddressCallback callback, void *callback_cls)
Convert a string to one or more IP addresses.
Definition: resolver_api.c:939
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
static void transmit_kx(struct Queue *queue, const struct GNUNET_CRYPTO_EcdhePublicKey *epub)
Generate and transmit our ephemeral key and the signature for the initial KX with the other peer...
static struct GNUNET_NT_InterfaceScanner * is
Network scanner to determine network types.
struct GNUNET_CRYPTO_EcdhePrivateKey ephemeral
Our ephemeral key.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Definition of a command line option.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX
TCP communicator payload box.
struct GNUNET_RESOLVER_RequestHandle * resolve_request_handle
Handle for DNS lookup of bindto address.
static void calculate_hmac(struct GNUNET_HashCode *hmac_secret, const void *buf, size_t buf_size, struct GNUNET_ShortHashCode *smac)
Compute mac over buf, and ratched the hmac_secret.
struct GNUNET_SCHEDULER_Task * listen_task
ID of listen task.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
struct GNUNET_CONTAINER_MultiPeerMap * GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:531
Handle for the service.
static void queue_read(void *cls)
Queue read task.
#define GNUNET_CRYPTO_eddsa_verify(purp, ps, sig, pub)
Verify EdDSA signature.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
struct GNUNET_MQ_Handle * mq
Message queue we are providing for the ch.
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
Definition: gnunet_nt_lib.h:35
Handle for an incoming connection where we do not yet have enough information to setup a full queue...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct Addresses * prev
Kept in a DLL.
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won&#39;t work on W32.
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
static void inject_rekey(struct Queue *queue)
Inject a struct TCPRekey message into the queue&#39;s plaintext buffer.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
A 256-bit hashcode.
Opaque handle to the transport service for communicators.
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
void GNUNET_TRANSPORT_communicator_disconnect(struct GNUNET_TRANSPORT_CommunicatorHandle *ch)
Disconnect from the transport service.
static int decrypt_and_check_tc(struct Queue *queue, struct TCPConfirmation *tc, char *ibuf)
We have received the first bytes from the other side on a queue.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc, struct sockaddr *address, socklen_t *address_len)
Accept a new connection on a socket.
Definition: network.c:430
Signature we use to verify that the ephemeral key was really chosen by the specified sender...
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
static struct GNUNET_TIME_Relative rekey_interval
The rekey interval.
static unsigned long long max_queue_length
Maximum queue length before we stop reading towards the transport service.
void GNUNET_TRANSPORT_communicator_mq_del(struct GNUNET_TRANSPORT_QueueHandle *qh)
Notify transport service that an MQ became unavailable due to a disconnect or timeout.
#define INITIAL_KX_SIZE
Size of the initial key exchange message sent first in both directions.
struct GNUNET_HashCode out_hmac
Shared secret for HMAC generation on outgoing data, ratcheted after each operation.
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
void GNUNET_CRYPTO_ecdhe_key_create(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
Create a new private key.
Definition: crypto_ecc.c:508
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
static void enc_notify_cb(void *cls, const struct GNUNET_PeerIdentity *sender, const struct GNUNET_MessageHeader *msg)
Function called when the transport service has received an acknowledgement for this communicator (!) ...
int GNUNET_NETWORK_socket_connect(const struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Connect a socket to some remote address.
Definition: network.c:658
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
unsigned long long bytes_in_queue
Number of bytes we currently have in our write queue.
static struct GNUNET_CONTAINER_MultiPeerMap * queue_map
Queues (map from peer identity to struct Queue)
static struct sockaddr * tcp_address_to_sockaddr_numeric_v4(socklen_t *sock_len, struct sockaddr_in v4, unsigned int port)
Convert a struct sockaddr_in4 to astruct sockaddr *`.
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
struct GNUNET_MQ_Handle * GNUNET_MQ_queue_for_callbacks(GNUNET_MQ_SendImpl send, GNUNET_MQ_DestroyImpl destroy, GNUNET_MQ_CancelImpl cancel, void *impl_state, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *cls)
Create a message queue for the specified handlers.
Definition: mq.c:561
static void queue_destroy(struct Queue *queue)
Functions with this signature are called whenever we need to close a queue due to a disconnect or fai...
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
int GNUNET_CRYPTO_eddsa_ecdh(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a ECDH public key and a private EdDSA key.
Definition: crypto_ecc.c:1024
static char * value
Value of the record to add/remove.
Handle for a queue.
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:48
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1296
size_t cwrite_off
At which offset in the ciphertext write buffer should we append more ciphertext from reading next...
#define COMMUNICATOR_ADDRESS_PREFIX
Address prefix used by the communicator.
#define GNUNET_MIN(a, b)
Definition: gnunet_common.h:93
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:701
static char buf[2048]
static struct GNUNET_CRYPTO_EddsaPrivateKey * my_private_key
Our private key.
struct GNUNET_TRANSPORT_CommunicatorHandle * GNUNET_TRANSPORT_communicator_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *config_section_name, const char *addr_prefix, enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc, GNUNET_TRANSPORT_CommunicatorMqInit mq_init, void *mq_init_cls, GNUNET_TRANSPORT_CommunicatorNotify notify_cb, void *notify_cb_cls)
Connect to the transport service.
static void setup_cipher(const struct GNUNET_HashCode *dh, const struct GNUNET_PeerIdentity *pid, gcry_cipher_hd_t *cipher, struct GNUNET_HashCode *hmac_key)
Setup cipher based on shared secret dh and decrypting peer pid.
this is an inbound connection (communicator initiated)
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
static void mq_destroy(struct GNUNET_MQ_Handle *mq, void *impl_state)
Signature of functions implementing the destruction of a message queue.
static void reschedule_queue_timeout(struct Queue *queue)
Increment queue timeout due to activity.
static void queue_finish(struct Queue *queue)
Append a &#39;finish&#39; message to the outgoing transmission.
const char * GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string (for printing debug messages)...
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH.
struct GNUNET_NETWORK_Handle * sock
socket that we transmit all data with on this queue
struct GNUNET_TIME_Absolute timeout
Timeout for this protoqueue.
struct sockaddr * addr_ipv6
Ipv6 address we like to bind to.
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
Internal representation of the hash map.
struct GNUNET_SCHEDULER_Task * write_task
ID of write task for this connection.
struct sockaddr * address
Address of the other peer.
struct Addresses * addrs_head
Head of DLL with addresses we like to register at NAT servcie.
void GNUNET_STATISTICS_set(struct GNUNET_STATISTICS_Handle *handle, const char *name, uint64_t value, int make_persistent)
Set statistic value for the peer.
A 512-bit hashcode.
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender&#39;s signature of type GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY.
#define GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH
TCP communicator end of stream.
int GNUNET_CRYPTO_ecdh_eddsa(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EddsaPublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a EdDSA public key and a private ECDH key.
Definition: crypto_ecc.c:1084
char cwrite_buf[(2 *64 *1024+sizeof(struct TCPBox))]
buffer for writing ciphertext to network.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
static void do_rekey(struct Queue *queue, const struct TCPRekey *rekey)
Handle rekey message on queue.
char pwrite_buf[UINT16_MAX+1+sizeof(struct TCPBox)]
Plaintext buffer for messages to be encrypted.
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
struct GNUNET_SCHEDULER_Task * listen_task
ID of listen task.
void GNUNET_NT_scanner_done(struct GNUNET_NT_InterfaceScanner *is)
Terminate interface scanner.
Definition: nt.c:433
static struct sockaddr * tcp_address_to_sockaddr(const char *bindto, socklen_t *sock_len)
Convert TCP bind specification to a struct sockaddr *
int GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
static void mq_cancel(struct GNUNET_MQ_Handle *mq, void *impl_state)
Implementation function that cancels the currently sent message.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
Private ECC key encoded for transmission.
void GNUNET_NAT_unregister(struct GNUNET_NAT_Handle *nh)
Stop port redirection and public IP address detection for the given handle.
Definition: nat_api.c:692
#define GNUNET_free_nz(ptr)
Wrapper around free.
struct GNUNET_CRYPTO_EcdhePublicKey ephemeral
New ephemeral key.
size_t pread_off
At which offset in the plaintext input buffer should we append more plaintext from decryption next...
struct Addresses * next
Kept in a DLL.
int mq_awaits_continue
Is MQ awaiting a GNUNET_MQ_impl_send_continue() call?
struct GNUNET_NETWORK_Handle * sock
socket that we transmit all data with on this queue
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...
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
struct GNUNET_HashCode key
The key used in the DHT.
static unsigned int size
Size of the "table".
Definition: peer.c:67
static struct GNUNET_TRANSPORT_AddressIdentifier * ai
Handle to the operation that publishes our address.
void GNUNET_CRYPTO_ecdhe_key_get_public(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, struct GNUNET_CRYPTO_EcdhePublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:215
static int get_queue_delete_it(void *cls, const struct GNUNET_PeerIdentity *target, void *value)
Iterator over all message queues to clean up.
Handle to a request given to the resolver.
Definition: resolver_api.c:102
static int mq_init(void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
Function called by the transport service to initialize a message queue given address information abou...
static char * extract_address(const char *bindto)
This Method extracts the address part of the BINDTO string.
struct GNUNET_NAT_Handle * GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *config_section, uint8_t proto, unsigned int num_addrs, const struct sockaddr **addrs, const socklen_t *addrlens, GNUNET_NAT_AddressCallback address_callback, GNUNET_NAT_ReversalCallback reversal_callback, void *callback_cls)
Attempt to enable port redirection and detect public IP address contacting UPnP or NAT-PMP routers on...
Definition: nat_api.c:378
static struct GNUNET_MQ_Envelope * ac
Handle to current GNUNET_PEERINFO_add_peer() operation.
static void mq_send(struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *msg, void *impl_state)
Signature of functions implementing the sending functionality of a message queue. ...
struct GNUNET_NT_InterfaceScanner * GNUNET_NT_scanner_init(void)
Initialize the address characterization client handle.
Definition: nt.c:412
struct GNUNET_TRANSPORT_QueueHandle * GNUNET_TRANSPORT_communicator_mq_add(struct GNUNET_TRANSPORT_CommunicatorHandle *ch, const struct GNUNET_PeerIdentity *peer, const char *address, uint32_t mtu, uint64_t q_len, uint32_t priority, enum GNUNET_NetworkType nt, enum GNUNET_TRANSPORT_ConnectionStatus cs, struct GNUNET_MQ_Handle *mq)
Notify transport service that a MQ became available due to an "inbound" connection or because the com...
static struct ProtoQueue * proto_tail
Protoqueues DLL tail.
struct GNUNET_NETWORK_Handle * listen_sock
Listen socket.
#define DEFAULT_MAX_QUEUE_LENGTH
How many messages do we keep at most in the queue to the transport service before we start to drop (d...
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
struct Addresses * addrs_tail
Head of DLL with addresses we like to register at NAT servcie.
int GNUNET_TRANSPORT_communicator_receive(struct GNUNET_TRANSPORT_CommunicatorHandle *handle, const struct GNUNET_PeerIdentity *sender, const struct GNUNET_MessageHeader *msg, struct GNUNET_TIME_Relative expected_addr_validity, GNUNET_TRANSPORT_MessageCompletedCallback cb, void *cb_cls)
Notify transport service that the communicator has received a message.
TCP message box.
static void start_initial_kx_out(struct Queue *queue)
Initialize our key material for outgoing transmissions and inform the other peer about it...
an ECC signature using EdDSA.
struct GNUNET_PeerIdentity receiver
Presumed identity of the target of the TCP connection (TCP server)
void GNUNET_CRYPTO_eddsa_key_get_public(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:193
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
#define REKEY_MAX_BYTES
How often do we rekey based on number of bytes transmitted? (additionally randomized).
Allow multiple values with the same key.
#define GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
After how long do we consider a connection to a peer dead if we don&#39;t receive messages from the peer...
Handle to a message queue.
Definition: mq.c:85
int GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
socklen_t address_len
Length of the address.
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won&#39;t work on W32;.
Private ECC key encoded for transmission.
int GNUNET_CONTAINER_multipeermap_iterate(struct GNUNET_CONTAINER_MultiPeerMap *map, GNUNET_CONTAINER_PeerMapIterator it, void *it_cls)
Iterate over all entries in the map.
static void nat_register()
This method reads from the DLL addrs_head to register them at the NAT service.
The identity of the host (wraps the signing key of the peer).
static void free_proto_queue(struct ProtoQueue *pq)
Closes socket and frees memory associated with pq.
struct sockaddr * address
Address of the other peer.
size_t ibuf_off
Current offset for reading into ibuf.
static void setup_out_cipher(struct Queue *queue)
Setup cipher for outgoing data stream based on target and our ephemeral private key.
#define GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED
Queue length.
TCP rekey message box.
static uint16_t port
Port number.
Definition: gnunet-bcd.c:82
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
struct sockaddr * addr
Address we like to register at NAT service.
configuration data
Definition: configuration.c:84
this is an outbound connection (transport initiated)
static struct GNUNET_STATISTICS_Handle * stats
For logging statistics.
enum GNUNET_TRANSPORT_ConnectionStatus cs
Connection status for this queue.
struct GNUNET_SCHEDULER_Task * read_task
ID of read task for this connection.
Encrypted continuation of TCP initial handshake.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
#define ADDRESS_VALIDITY_PERIOD
How long do we believe our addresses to remain up (before the other peer should revalidate).
size_t pwrite_off
At which offset in the plaintext output buffer should we append more plaintext for encryption next...
#define GNUNET_log(kind,...)
static void nat_address_cb(void *cls, void **app_ctx, int add_remove, enum GNUNET_NAT_AddressClass ac, const struct sockaddr *addr, socklen_t addrlen)
Signature of the callback passed to GNUNET_NAT_register() for a function to call whenever our set of ...
Entry in list of pending tasks.
Definition: scheduler.c:134
struct ProtoQueue * next
Kept in a DLL.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY.
static unsigned int extract_port(const char *addr_and_port)
This Method extracts the port part of the BINDTO string.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX.
handle to a socket
Definition: network.c:52
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:367
struct ProtoQueue * prev
Kept in a DLL.
static void mq_error(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
static size_t try_handle_plaintext(struct Queue *queue)
Test if we have received a full message in plaintext.
struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_CRYPTO_eddsa_key_create_from_configuration(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a new private key by reading our peer&#39;s key from the file specified in the configuration...
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:331
socklen_t addr_len
Length of address we like to register at NAT service.
static struct sockaddr * tcp_address_to_sockaddr_numeric_v6(socklen_t *sock_len, struct sockaddr_in6 v6, unsigned int port)
Convert a struct sockaddr_in6 to astruct sockaddr *`.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
static void add_addr(struct sockaddr *in, socklen_t in_len)
This method adds addresses to the DLL, that are later register at the NAT service.
gcry_cipher_hd_t in_cipher
cipher for decryption of incoming data.
Header for all communications.
Time for absolute times used by GNUnet, in microseconds.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:833
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1517
socklen_t * addr_len_ipv4
Length of ipv4 address.
GNUNET_TRANSPORT_ConnectionStatus
Possible states of a connection.
struct GNUNET_NETWORK_Handle * listen_sock
Listen socket.
enum GNUNET_NetworkType nt
Which network type does this queue use?
unsigned int GNUNET_CONTAINER_multipeermap_size(const struct GNUNET_CONTAINER_MultiPeerMap *map)
Get the number of key-value pairs in the map.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Purpose must be GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE.
struct GNUNET_TIME_Absolute timeout
Timeout for this queue.
struct GNUNET_SCHEDULER_Task * listen_task
ID of listen task.
static void do_shutdown(void *cls)
Shutdown the UNIX communicator.
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
#define DEFAULT_REKEY_INTERVAL
How often do we rekey based on time (at least)
static struct ProtoQueue * proto_head
Protoqueues DLL head.
int GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
static struct GNUNET_TRANSPORT_CommunicatorHandle * ch
Our environment.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
struct GNUNET_TRANSPORT_QueueHandle * qh
handle for this queue with the ch.
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:657
static char * address
GNS address for this phone.
void GNUNET_MQ_impl_send_continue(struct GNUNET_MQ_Handle *mq)
Call the send implementation for the next queued message, if any.
Definition: mq.c:495
gcry_cipher_hd_t out_cipher
cipher for encryption of outgoing data.
int GNUNET_CRYPTO_kdf(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
Derive key.
Definition: crypto_kdf.c:89
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
Handle returned to identify the internal data structure the transport API has created to manage a mes...
char pread_buf[UINT16_MAX+1+sizeof(struct TCPBox)]
Plaintext buffer for decrypted plaintext.
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:560
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
Struct to use as closure.
#define GNUNET_malloc(size)
Wrapper around malloc.
int GNUNET_NETWORK_test_pf(int pf)
Test if the given protocol family is supported by this system.
Definition: network.c:85
Internal representation of an address a communicator is currently providing for the transport service...
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
struct GNUNET_CRYPTO_EddsaPublicKey public_key
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:912
static void queue_write(void *cls)
We have been notified that our socket is ready to write.
Bandwidth allocation API for the transport service.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972