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 
49 #define ADDRESS_VALIDITY_PERIOD \
50  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
51 
60 #define DEFAULT_MAX_QUEUE_LENGTH 8
61 
66 #define BUF_SIZE (2 * 64 * 1024 + sizeof(struct TCPBox))
67 
71 #define DEFAULT_REKEY_INTERVAL GNUNET_TIME_UNIT_DAYS
72 
76 #define PROTO_QUEUE_TIMEOUT GNUNET_TIME_UNIT_MINUTES
77 
82 #define REKEY_MAX_BYTES (1024LLU * 1024 * 1024 * 4LLU)
83 
88 #define INITIAL_KX_SIZE \
89  (sizeof(struct GNUNET_CRYPTO_EcdhePublicKey) \
90  + sizeof(struct TCPConfirmation))
91 
92 
96 #define COMMUNICATOR_ADDRESS_PREFIX "tcp"
97 
101 #define COMMUNICATOR_CONFIG_SECTION "communicator-tcp"
102 
104 
105 
111 {
116 
121 
126 
131 
137 };
138 
139 
144 {
149 
153  struct GNUNET_CRYPTO_EddsaSignature sender_sig;
154 
159  struct GNUNET_TIME_AbsoluteNBO monotonic_time;
160 };
161 
162 
166 struct TCPBox
167 {
174  struct GNUNET_MessageHeader header;
175 
184  struct GNUNET_ShortHashCode hmac;
185 
186  /* followed by as may bytes of payload as indicated in @e header,
187  excluding the TCPBox itself! */
188 };
189 
190 
195 struct TCPRekey
196 {
200  struct GNUNET_MessageHeader header;
201 
210  struct GNUNET_ShortHashCode hmac;
211 
216 
220  struct GNUNET_CRYPTO_EddsaSignature sender_sig;
221 
226  struct GNUNET_TIME_AbsoluteNBO monotonic_time;
227 };
228 
229 
235 struct TCPFinish
236 {
240  struct GNUNET_MessageHeader header;
241 
250  struct GNUNET_ShortHashCode hmac;
251 };
252 
253 
255 
256 
260 struct Queue
261 {
265  struct GNUNET_PeerIdentity target;
266 
271 
275  gcry_cipher_hd_t in_cipher;
276 
280  gcry_cipher_hd_t out_cipher;
281 
285  struct GNUNET_HashCode in_hmac;
286 
291  struct GNUNET_HashCode out_hmac;
292 
298 
303 
308 
312  struct sockaddr *address;
313 
319 
324  struct GNUNET_TIME_Absolute rekey_time;
325 
329  socklen_t address_len;
330 
335 
340 
344  unsigned long long bytes_in_queue;
345 
349  char cread_buf[BUF_SIZE];
350 
354  char cwrite_buf[BUF_SIZE];
355 
359  char pread_buf[UINT16_MAX + 1 + sizeof(struct TCPBox)];
360 
364  char pwrite_buf[UINT16_MAX + 1 + sizeof(struct TCPBox)];
365 
370  size_t cread_off;
371 
376  size_t cwrite_off;
377 
382  size_t pread_off;
383 
388  size_t pwrite_off;
389 
394 
402  unsigned int backpressure;
403 
408 
413 
418 
426 
431  int rekeyed;
432 };
433 
434 
440 {
444  struct ProtoQueue *next;
445 
449  struct ProtoQueue *prev;
450 
455 
460 
464  struct sockaddr *address;
465 
469  socklen_t address_len;
470 
475 
480  char ibuf[INITIAL_KX_SIZE];
481 
485  size_t ibuf_off;
486 };
487 
488 
493 
497 static unsigned long long max_queue_length;
498 
503 
508 
513 
518 
523 
528 
533 
537 static const struct GNUNET_CONFIGURATION_Handle *cfg;
538 
543 
547 static struct GNUNET_NAT_Handle *nat;
548 
552 static struct ProtoQueue *proto_head;
553 
557 static struct ProtoQueue *proto_tail;
558 
559 
567 static void
568 listen_cb (void *cls);
569 
570 
578 static void
580 {
581  struct GNUNET_MQ_Handle *mq;
582 
584  "Disconnecting queue for peer `%s'\n",
585  GNUNET_i2s (&queue->target));
586  if (NULL != (mq = queue->mq))
587  {
588  queue->mq = NULL;
589  GNUNET_MQ_destroy (mq);
590  }
591  if (NULL != queue->qh)
592  {
594  queue->qh = NULL;
595  }
596  GNUNET_assert (
597  GNUNET_YES ==
598  GNUNET_CONTAINER_multipeermap_remove (queue_map, &queue->target, queue));
599  GNUNET_STATISTICS_set (stats,
600  "# queues active",
602  GNUNET_NO);
603  if (NULL != queue->read_task)
604  {
606  queue->read_task = NULL;
607  }
608  if (NULL != queue->write_task)
609  {
611  queue->write_task = NULL;
612  }
614  gcry_cipher_close (queue->in_cipher);
615  gcry_cipher_close (queue->out_cipher);
616  GNUNET_free (queue->address);
617  if (0 != queue->backpressure)
618  queue->destroyed = GNUNET_YES;
619  else
620  GNUNET_free (queue);
621  if (NULL == listen_task)
623  listen_sock,
624  &listen_cb,
625  NULL);
626 }
627 
628 
637 static void
638 calculate_hmac (struct GNUNET_HashCode *hmac_secret,
639  const void *buf,
640  size_t buf_size,
641  struct GNUNET_ShortHashCode *smac)
642 {
643  struct GNUNET_HashCode mac;
644 
645  GNUNET_CRYPTO_hmac_raw (hmac_secret,
646  sizeof(struct GNUNET_HashCode),
647  buf,
648  buf_size,
649  &mac);
650  /* truncate to `struct GNUNET_ShortHashCode` */
651  memcpy (smac, &mac, sizeof(struct GNUNET_ShortHashCode));
652  /* ratchet hmac key */
653  GNUNET_CRYPTO_hash (hmac_secret,
654  sizeof(struct GNUNET_HashCode),
655  hmac_secret);
656 }
657 
658 
665 static void
667 {
668  struct TCPFinish fin;
669 
670  memset (&fin, 0, sizeof(fin));
671  fin.header.size = htons (sizeof(fin));
673  calculate_hmac (&queue->out_hmac, &fin, sizeof(fin), &fin.hmac);
674  /* if there is any message left in pwrite_buf, we
675  overwrite it (possibly dropping the last message
676  from CORE hard here) */
677  memcpy (queue->pwrite_buf, &fin, sizeof(fin));
678  queue->pwrite_off = sizeof(fin);
679  /* This flag will ensure that #queue_write() no longer
680  notifies CORE about the possibility of sending
681  more data, and that #queue_write() will call
682  #queue_destroy() once the @c fin was fully written. */
683  queue->finishing = GNUNET_YES;
684 }
685 
686 
694 static void
696 {
697  queue->timeout =
699 }
700 
701 
707 static void
708 queue_read (void *cls);
709 
710 
718 static void
719 core_read_finished_cb (void *cls, int success)
720 {
721  struct Queue *queue = cls;
722  if (GNUNET_OK != success)
724  "# messages lost in communicator API towards CORE",
725  1,
726  GNUNET_NO);
727  queue->backpressure--;
728  /* handle deferred queue destruction */
729  if ((queue->destroyed) && (0 == queue->backpressure))
730  {
731  GNUNET_free (queue);
732  return;
733  }
734  reschedule_queue_timeout (queue);
735  /* possibly unchoke reading, now that CORE made progress */
736  if (NULL == queue->read_task)
737  queue->read_task =
739  queue->timeout),
740  queue->sock,
741  &queue_read,
742  queue);
743 }
744 
745 
755 static void
757  const void *plaintext,
758  size_t plaintext_len)
759 {
760  const struct GNUNET_MessageHeader *hdr = plaintext;
761  int ret;
762 
763  if (ntohs (hdr->size) != plaintext_len)
764  {
765  /* NOTE: If we ever allow multiple CORE messages in one
766  BOX, this will have to change! */
767  GNUNET_break (0);
768  return;
769  }
771  &queue->target,
772  hdr,
775  queue);
776  if (GNUNET_OK == ret)
777  queue->backpressure++;
778  GNUNET_break (GNUNET_NO != ret); /* backpressure not working!? */
779  if (GNUNET_SYSERR == ret)
781  "# bytes lost due to CORE not running",
782  plaintext_len,
783  GNUNET_NO);
784 }
785 
786 
796 static void
797 setup_cipher (const struct GNUNET_HashCode *dh,
798  const struct GNUNET_PeerIdentity *pid,
799  gcry_cipher_hd_t *cipher,
800  struct GNUNET_HashCode *hmac_key)
801 {
802  char key[256 / 8];
803  char ctr[128 / 8];
804 
805  gcry_cipher_open (cipher,
806  GCRY_CIPHER_AES256 /* low level: go for speed */,
807  GCRY_CIPHER_MODE_CTR,
808  0 /* flags */);
810  sizeof(key),
811  "TCP-key",
812  strlen ("TCP-key"),
813  dh,
814  sizeof(*dh),
815  pid,
816  sizeof(*pid),
817  NULL,
818  0));
819  gcry_cipher_setkey (*cipher, key, sizeof(key));
821  sizeof(ctr),
822  "TCP-ctr",
823  strlen ("TCP-ctr"),
824  dh,
825  sizeof(*dh),
826  pid,
827  sizeof(*pid),
828  NULL,
829  0));
830  gcry_cipher_setctr (*cipher, ctr, sizeof(ctr));
832  GNUNET_CRYPTO_kdf (hmac_key,
833  sizeof(struct GNUNET_HashCode),
834  "TCP-hmac",
835  strlen ("TCP-hmac"),
836  dh,
837  sizeof(*dh),
838  pid,
839  sizeof(*pid),
840  NULL,
841  0));
842 }
843 
844 
851 static void
853  struct Queue *queue)
854 {
855  struct GNUNET_HashCode dh;
856 
857  GNUNET_CRYPTO_eddsa_ecdh (my_private_key, ephemeral, &dh);
858  setup_cipher (&dh, &my_identity, &queue->in_cipher, &queue->in_hmac);
859 }
860 
861 
870 static void
871 do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
872 {
873  struct TcpHandshakeSignature thp;
875  thp.purpose.size = htonl (sizeof(thp));
876  thp.sender = queue->target;
877  thp.receiver = my_identity;
878  thp.ephemeral = rekey->ephemeral;
879  thp.monotonic_time = rekey->monotonic_time;
880  /* FIXME: check monotonic time is monotonic... */
881  if (GNUNET_OK !=
883  &thp,
884  &rekey->sender_sig,
885  &queue->target.public_key))
886  {
887  GNUNET_break (0);
888  queue_finish (queue);
889  return;
890  }
891  gcry_cipher_close (queue->in_cipher);
892  queue->rekeyed = GNUNET_YES;
893  setup_in_cipher (&rekey->ephemeral, queue);
894 }
895 
896 
904 static size_t
906 {
907  const struct GNUNET_MessageHeader *hdr =
908  (const struct GNUNET_MessageHeader *) queue->pread_buf;
909  const struct TCPBox *box = (const struct TCPBox *) queue->pread_buf;
910  const struct TCPRekey *rekey = (const struct TCPRekey *) queue->pread_buf;
911  const struct TCPFinish *fin = (const struct TCPFinish *) queue->pread_buf;
912  struct TCPRekey rekeyz;
913  struct TCPFinish finz;
914  struct GNUNET_ShortHashCode tmac;
915  uint16_t type;
916  size_t size = 0; /* make compiler happy */
917 
918  if (sizeof(*hdr) > queue->pread_off)
919  return 0; /* not even a header */
920  type = ntohs (hdr->type);
921  switch (type)
922  {
924  /* Special case: header size excludes box itself! */
925  if (ntohs (hdr->size) + sizeof(struct TCPBox) > queue->pread_off)
926  return 0;
927  calculate_hmac (&queue->in_hmac, &box[1], ntohs (hdr->size), &tmac);
928  if (0 != memcmp (&tmac, &box->hmac, sizeof(tmac)))
929  {
930  GNUNET_break_op (0);
931  queue_finish (queue);
932  return 0;
933  }
934  pass_plaintext_to_core (queue, (const void *) &box[1], ntohs (hdr->size));
935  size = ntohs (hdr->size) + sizeof(*box);
936  break;
937 
939  if (sizeof(*rekey) > queue->pread_off)
940  return 0;
941  if (ntohs (hdr->size) != sizeof(*rekey))
942  {
943  GNUNET_break_op (0);
944  queue_finish (queue);
945  return 0;
946  }
947  rekeyz = *rekey;
948  memset (&rekeyz.hmac, 0, sizeof(rekeyz.hmac));
949  calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
950  if (0 != memcmp (&tmac, &rekey->hmac, sizeof(tmac)))
951  {
952  GNUNET_break_op (0);
953  queue_finish (queue);
954  return 0;
955  }
956  do_rekey (queue, rekey);
957  size = ntohs (hdr->size);
958  break;
959 
961  if (sizeof(*fin) > queue->pread_off)
962  return 0;
963  if (ntohs (hdr->size) != sizeof(*fin))
964  {
965  GNUNET_break_op (0);
966  queue_finish (queue);
967  return 0;
968  }
969  finz = *fin;
970  memset (&finz.hmac, 0, sizeof(finz.hmac));
971  calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
972  if (0 != memcmp (&tmac, &fin->hmac, sizeof(tmac)))
973  {
974  GNUNET_break_op (0);
975  queue_finish (queue);
976  return 0;
977  }
978  /* handle FINISH by destroying queue */
979  queue_destroy (queue);
980  break;
981 
982  default:
983  GNUNET_break_op (0);
984  queue_finish (queue);
985  return 0;
986  }
987  GNUNET_assert (0 != size);
988  return size;
989 }
990 
991 
997 static void
998 queue_read (void *cls)
999 {
1000  struct Queue *queue = cls;
1001  struct GNUNET_TIME_Relative left;
1002  ssize_t rcvd;
1003 
1004  queue->read_task = NULL;
1005  rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
1006  &queue->cread_buf[queue->cread_off],
1007  BUF_SIZE - queue->cread_off);
1009  "Received %lu bytes from TCP queue\n", rcvd);
1010  if (-1 == rcvd)
1011  {
1012  if ((EAGAIN != errno) && (EINTR != errno))
1013  {
1015  queue_finish (queue);
1016  return;
1017  }
1018  /* try again */
1020  queue->read_task =
1021  GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read, queue);
1022  return;
1023  }
1024  if (0 != rcvd)
1025  reschedule_queue_timeout (queue);
1026  queue->cread_off += rcvd;
1027  while ((queue->pread_off < sizeof(queue->pread_buf)) &&
1028  (queue->cread_off > 0))
1029  {
1030  size_t max = GNUNET_MIN (sizeof(queue->pread_buf) - queue->pread_off,
1031  queue->cread_off);
1032  size_t done;
1033  size_t total;
1034  size_t old_pread_off = queue->pread_off;
1035 
1036  GNUNET_assert (0 ==
1037  gcry_cipher_decrypt (queue->in_cipher,
1038  &queue->pread_buf[queue->pread_off],
1039  max,
1040  queue->cread_buf,
1041  max));
1042  queue->pread_off += max;
1043  total = 0;
1044  while (0 != (done = try_handle_plaintext (queue)))
1045  {
1046  /* 'done' bytes of plaintext were used, shift buffer */
1047  GNUNET_assert (done <= queue->pread_off);
1048  /* NOTE: this memmove() could possibly sometimes be
1049  avoided if we pass 'total' into try_handle_plaintext()
1050  and use it at an offset into the buffer there! */
1051  memmove (queue->pread_buf,
1052  &queue->pread_buf[done],
1053  queue->pread_off - done);
1054  queue->pread_off -= done;
1055  total += done;
1056  /* The last plaintext was a rekey, abort for now */
1057  if (GNUNET_YES == queue->rekeyed)
1058  break;
1059  }
1060  /* when we encounter a rekey message, the decryption above uses the
1061  wrong key for everything after the rekey; in that case, we have
1062  to re-do the decryption at 'total' instead of at 'max'.
1063  However, we have to take into account that the plaintext buffer may have
1064  already contained data and not jumpt too far ahead in the ciphertext.
1065  If there is no rekey and the last message is incomplete (max > total),
1066  it is safe to keep the decryption so we shift by 'max' */if (GNUNET_YES == queue->rekeyed)
1067  {
1068  max = total - old_pread_off;
1069  queue->rekeyed = GNUNET_NO;
1070  queue->pread_off = 0;
1071  }
1072  memmove (queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
1073  queue->cread_off -= max;
1074  }
1075  if (BUF_SIZE == queue->cread_off)
1076  return; /* buffer full, suspend reading */
1078  if (0 != left.rel_value_us)
1079  {
1080  if (max_queue_length > queue->backpressure)
1081  {
1082  /* continue reading */
1084  queue->read_task =
1085  GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read, queue);
1086  }
1087  return;
1088  }
1090  "Queue %p was idle for %s, disconnecting\n",
1091  queue,
1094  GNUNET_YES));
1095  queue_finish (queue);
1096 }
1097 
1098 
1106 static struct sockaddr *
1107 tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
1108 {
1109  struct sockaddr *in;
1110  unsigned int port;
1111  char dummy[2];
1112  char *colon;
1113  char *cp;
1114 
1115  if (1 == sscanf (bindto, "%u%1s", &port, dummy))
1116  {
1117  /* interpreting value as just a PORT number */
1118  if (port > UINT16_MAX)
1119  {
1121  "BINDTO specification `%s' invalid: value too large for port\n",
1122  bindto);
1123  return NULL;
1124  }
1125  if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
1126  (GNUNET_YES ==
1129  "DISABLE_V6")))
1130  {
1131  struct sockaddr_in *i4;
1132 
1133  i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
1134  i4->sin_family = AF_INET;
1135  i4->sin_port = htons ((uint16_t) port);
1136 #if HAVE_SOCKADDR_IN_SIN_LEN
1137  i4->sin_len = sizeof(sizeof(struct sockaddr_in));
1138 #endif
1139  *sock_len = sizeof(struct sockaddr_in);
1140  in = (struct sockaddr *) i4;
1141  }
1142  else
1143  {
1144  struct sockaddr_in6 *i6;
1145 
1146  i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
1147  i6->sin6_family = AF_INET6;
1148  i6->sin6_port = htons ((uint16_t) port);
1149 #if HAVE_SOCKADDR_IN_SIN_LEN
1150  i6->sin6_len = sizeof(sizeof(struct sockaddr_in6));
1151 #endif
1152  *sock_len = sizeof(struct sockaddr_in6);
1153  in = (struct sockaddr *) i6;
1154  }
1155  return in;
1156  }
1157  cp = GNUNET_strdup (bindto);
1158  colon = strrchr (cp, ':');
1159  if (NULL != colon)
1160  {
1161  /* interpet value after colon as port */
1162  *colon = '\0';
1163  colon++;
1164  if (1 == sscanf (colon, "%u%1s", &port, dummy))
1165  {
1166  /* interpreting value as just a PORT number */
1167  if (port > UINT16_MAX)
1168  {
1170  "BINDTO specification `%s' invalid: value too large for port\n",
1171  bindto);
1172  GNUNET_free (cp);
1173  return NULL;
1174  }
1175  }
1176  else
1177  {
1178  GNUNET_log (
1180  "BINDTO specification `%s' invalid: last ':' not followed by number\n",
1181  bindto);
1182  GNUNET_free (cp);
1183  return NULL;
1184  }
1185  }
1186  else
1187  {
1188  /* interpret missing port as 0, aka pick any free one */
1189  port = 0;
1190  }
1191  {
1192  /* try IPv4 */
1193  struct sockaddr_in v4;
1194 
1195  if (1 == inet_pton (AF_INET, cp, &v4.sin_addr))
1196  {
1197  v4.sin_family = AF_INET;
1198  v4.sin_port = htons ((uint16_t) port);
1199 #if HAVE_SOCKADDR_IN_SIN_LEN
1200  v4.sin_len = sizeof(struct sockaddr_in);
1201 #endif
1202  in = GNUNET_memdup (&v4, sizeof(v4));
1203  *sock_len = sizeof(v4);
1204  GNUNET_free (cp);
1205  return in;
1206  }
1207  }
1208  {
1209  /* try IPv6 */
1210  struct sockaddr_in6 v6;
1211  const char *start;
1212 
1213  start = cp;
1214  if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
1215  {
1216  start++; /* skip over '[' */
1217  cp[strlen (cp) - 1] = '\0'; /* eat ']' */
1218  }
1219  if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
1220  {
1221  v6.sin6_family = AF_INET6;
1222  v6.sin6_port = htons ((uint16_t) port);
1223 #if HAVE_SOCKADDR_IN_SIN_LEN
1224  v6.sin6_len = sizeof(sizeof(struct sockaddr_in6));
1225 #endif
1226  in = GNUNET_memdup (&v6, sizeof(v6));
1227  *sock_len = sizeof(v6);
1228  GNUNET_free (cp);
1229  return in;
1230  }
1231  }
1232  /* #5528 FIXME (feature!): maybe also try getnameinfo()? */
1233  GNUNET_free (cp);
1234  return NULL;
1235 }
1236 
1237 
1244 static void
1246 {
1247  struct GNUNET_HashCode dh;
1248 
1249  GNUNET_CRYPTO_ecdh_eddsa (&queue->ephemeral, &queue->target.public_key, &dh);
1250  /* we don't need the private key anymore, drop it! */
1251  memset (&queue->ephemeral, 0, sizeof(queue->ephemeral));
1252  setup_cipher (&dh, &queue->target, &queue->out_cipher, &queue->out_hmac);
1254  queue->rekey_left_bytes =
1256 }
1257 
1258 
1265 static void
1267 {
1268  struct TCPRekey rekey;
1269  struct TcpHandshakeSignature thp;
1270 
1271  GNUNET_assert (0 == queue->pwrite_off);
1272  memset (&rekey, 0, sizeof(rekey));
1276  rekey.header.size = ntohs (sizeof(rekey));
1278  rekey.monotonic_time =
1281  thp.purpose.size = htonl (sizeof(thp));
1282  thp.sender = my_identity;
1283  thp.receiver = queue->target;
1284  thp.ephemeral = rekey.ephemeral;
1285  thp.monotonic_time = rekey.monotonic_time;
1286  GNUNET_CRYPTO_eddsa_sign (my_private_key,
1287  &thp,
1288  &rekey.sender_sig);
1289  calculate_hmac (&queue->out_hmac, &rekey, sizeof(rekey), &rekey.hmac);
1290  /* Encrypt rekey message with 'old' cipher */
1291  GNUNET_assert (0 ==
1292  gcry_cipher_encrypt (queue->out_cipher,
1293  &queue->cwrite_buf[queue->cwrite_off],
1294  sizeof(rekey),
1295  &rekey,
1296  sizeof(rekey)));
1297  queue->cwrite_off += sizeof(rekey);
1298  /* Setup new cipher for successive messages */
1299  gcry_cipher_close (queue->out_cipher);
1300  setup_out_cipher (queue);
1301 }
1302 
1303 
1310 static void
1311 queue_write (void *cls)
1312 {
1313  struct Queue *queue = cls;
1314  ssize_t sent;
1315  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In queue write\n");
1316  queue->write_task = NULL;
1317  if (0 != queue->cwrite_off)
1318  {
1319  sent = GNUNET_NETWORK_socket_send (queue->sock,
1320  queue->cwrite_buf,
1321  queue->cwrite_off);
1323  "Sent %lu bytes to TCP queue\n", sent);
1324  if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1325  {
1327  queue_destroy (queue);
1328  return;
1329  }
1330  if (sent > 0)
1331  {
1332  size_t usent = (size_t) sent;
1333  queue->cwrite_off -= usent;
1334  memmove (queue->cwrite_buf,
1335  &queue->cwrite_buf[usent],
1336  queue->cwrite_off);
1337  reschedule_queue_timeout (queue);
1338  }
1339  }
1340  /* can we encrypt more? (always encrypt full messages, needed
1341  such that #mq_cancel() can work!) */
1342  if ((0 < queue->rekey_left_bytes) &&
1343  (queue->pwrite_off > 0) &&
1344  (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE))
1345  {
1347  "Encrypting %lu bytes\n", queue->pwrite_off);
1348  GNUNET_assert (0 ==
1349  gcry_cipher_encrypt (queue->out_cipher,
1350  &queue->cwrite_buf[queue->cwrite_off],
1351  queue->pwrite_off,
1352  queue->pwrite_buf,
1353  queue->pwrite_off));
1354  if (queue->rekey_left_bytes > queue->pwrite_off)
1355  queue->rekey_left_bytes -= queue->pwrite_off;
1356  else
1357  queue->rekey_left_bytes = 0;
1358  queue->cwrite_off += queue->pwrite_off;
1359  queue->pwrite_off = 0;
1360  }
1361  if ((0 == queue->pwrite_off) &&
1362  ((0 == queue->rekey_left_bytes) ||
1363  (0 ==
1365  {
1366  inject_rekey (queue);
1367  }
1368  if ((0 == queue->pwrite_off) && (! queue->finishing) &&
1369  (GNUNET_YES == queue->mq_awaits_continue))
1370  {
1371  queue->mq_awaits_continue = GNUNET_NO;
1373  }
1374  /* did we just finish writing 'finish'? */
1375  if ((0 == queue->cwrite_off) && (GNUNET_YES == queue->finishing))
1376  {
1378  "Finishing queue\n");
1379  queue_destroy (queue);
1380  return;
1381  }
1382  /* do we care to write more? */
1383  if ((0 < queue->cwrite_off) || (0 < queue->pwrite_off))
1384  queue->write_task =
1386  queue->sock,
1387  &queue_write,
1388  queue);
1389 }
1390 
1391 
1400 static void
1402  const struct GNUNET_MessageHeader *msg,
1403  void *impl_state)
1404 {
1405  struct Queue *queue = impl_state;
1406  uint16_t msize = ntohs (msg->size);
1407  struct TCPBox box;
1409  "In MQ send. Queue finishing: %s; write task running: %s\n",
1410  (GNUNET_YES == queue->finishing) ? "yes" : "no",
1411  (NULL == queue->write_task) ? "yes" : "no");
1412  GNUNET_assert (mq == queue->mq);
1413  queue->mq_awaits_continue = GNUNET_YES;
1414  if (GNUNET_YES == queue->finishing)
1415  return; /* this queue is dying, drop msg */
1416  GNUNET_assert (0 == queue->pwrite_off);
1418  box.header.size = htons (msize);
1419  calculate_hmac (&queue->out_hmac, msg, msize, &box.hmac);
1420  memcpy (&queue->pwrite_buf[queue->pwrite_off], &box, sizeof(box));
1421  queue->pwrite_off += sizeof(box);
1422  memcpy (&queue->pwrite_buf[queue->pwrite_off], msg, msize);
1423  queue->pwrite_off += msize;
1425  "%lu bytes of plaintext to send\n", queue->pwrite_off);
1426  GNUNET_assert (NULL != queue->sock);
1427  if (NULL == queue->write_task)
1428  queue->write_task =
1430  queue->sock,
1431  &queue_write,
1432  queue);
1433 }
1434 
1435 
1444 static void
1445 mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
1446 {
1447  struct Queue *queue = impl_state;
1448 
1449  if (mq == queue->mq)
1450  {
1451  queue->mq = NULL;
1452  queue_finish (queue);
1453  }
1454 }
1455 
1456 
1463 static void
1464 mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
1465 {
1466  struct Queue *queue = impl_state;
1467 
1468  GNUNET_assert (0 != queue->pwrite_off);
1469  queue->pwrite_off = 0;
1470 }
1471 
1472 
1482 static void
1483 mq_error (void *cls, enum GNUNET_MQ_Error error)
1484 {
1485  struct Queue *queue = cls;
1486 
1488  "MQ error in queue to %s: %d\n",
1489  GNUNET_i2s (&queue->target),
1490  (int) error);
1491  queue_finish (queue);
1492 }
1493 
1494 
1502 static void
1504 {
1505  queue->nt =
1506  GNUNET_NT_scanner_get_type (is, queue->address, queue->address_len);
1508  queue_map,
1509  &queue->target,
1510  queue,
1512  GNUNET_STATISTICS_set (stats,
1513  "# queues active",
1515  GNUNET_NO);
1516  queue->timeout =
1519  &mq_destroy,
1520  &mq_cancel,
1521  queue,
1522  NULL,
1523  &mq_error,
1524  queue);
1525  {
1526  char *foreign_addr;
1527 
1528  switch (queue->address->sa_family)
1529  {
1530  case AF_INET:
1531  GNUNET_asprintf (&foreign_addr,
1532  "%s-%s",
1534  GNUNET_a2s (queue->address, queue->address_len));
1535  break;
1536 
1537  case AF_INET6:
1538  GNUNET_asprintf (&foreign_addr,
1539  "%s-%s",
1541  GNUNET_a2s (queue->address, queue->address_len));
1542  break;
1543 
1544  default:
1545  GNUNET_assert (0);
1546  }
1548  &queue->target,
1549  foreign_addr,
1550  0 /* no MTU */,
1551  queue->nt,
1552  cs,
1553  queue->mq);
1554  GNUNET_free (foreign_addr);
1555  }
1556 }
1557 
1558 
1569 static void
1571  const struct GNUNET_CRYPTO_EcdhePublicKey *epub)
1572 {
1573  struct TcpHandshakeSignature ths;
1574  struct TCPConfirmation tc;
1575 
1576  memcpy (queue->cwrite_buf, epub, sizeof(*epub));
1577  queue->cwrite_off = sizeof(*epub);
1578  /* compute 'tc' and append in encrypted format to cwrite_buf */
1579  tc.sender = my_identity;
1580  tc.monotonic_time =
1583  ths.purpose.size = htonl (sizeof(ths));
1584  ths.sender = my_identity;
1585  ths.receiver = queue->target;
1586  ths.ephemeral = *epub;
1587  ths.monotonic_time = tc.monotonic_time;
1588  GNUNET_CRYPTO_eddsa_sign (my_private_key,
1589  &ths,
1590  &tc.sender_sig);
1591  GNUNET_assert (0 ==
1592  gcry_cipher_encrypt (queue->out_cipher,
1593  &queue->cwrite_buf[queue->cwrite_off],
1594  sizeof(tc),
1595  &tc,
1596  sizeof(tc)));
1597  queue->cwrite_off += sizeof(tc);
1598 }
1599 
1600 
1608 static void
1610 {
1611  struct GNUNET_CRYPTO_EcdhePublicKey epub;
1612 
1616  setup_out_cipher (queue);
1617  transmit_kx (queue, &epub);
1618 }
1619 
1620 
1632 static int
1634  struct TCPConfirmation *tc,
1635  char *ibuf)
1636 {
1637  struct TcpHandshakeSignature ths;
1638 
1639  GNUNET_assert (
1640  0 ==
1641  gcry_cipher_decrypt (queue->in_cipher,
1642  tc,
1643  sizeof(*tc),
1644  &ibuf[sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)],
1645  sizeof(*tc)));
1647  ths.purpose.size = htonl (sizeof(ths));
1648  ths.sender = tc->sender;
1649  ths.receiver = my_identity;
1650  memcpy (&ths.ephemeral, ibuf, sizeof(struct GNUNET_CRYPTO_EcdhePublicKey));
1651  ths.monotonic_time = tc->monotonic_time;
1652  /* FIXME: check monotonic time against previous mono times
1653  from this sender! */
1656  &ths,
1657  &tc->sender_sig,
1658  &tc->sender.public_key);
1659 }
1660 
1661 
1667 static void
1669 {
1671  GNUNET_free (pq->address);
1672  GNUNET_CONTAINER_DLL_remove (proto_head, proto_tail, pq);
1673  GNUNET_free (pq);
1674 }
1675 
1676 
1683 static void
1684 proto_read_kx (void *cls)
1685 {
1686  struct ProtoQueue *pq = cls;
1687  ssize_t rcvd;
1688  struct GNUNET_TIME_Relative left;
1689  struct Queue *queue;
1690  struct TCPConfirmation tc;
1691 
1692  pq->read_task = NULL;
1694  if (0 == left.rel_value_us)
1695  {
1696  free_proto_queue (pq);
1697  return;
1698  }
1699  rcvd = GNUNET_NETWORK_socket_recv (pq->sock,
1700  &pq->ibuf[pq->ibuf_off],
1701  sizeof(pq->ibuf) - pq->ibuf_off);
1703  "Received %lu bytes for KX\n", rcvd);
1704  if (-1 == rcvd)
1705  {
1706  if ((EAGAIN != errno) && (EINTR != errno))
1707  {
1709  free_proto_queue (pq);
1710  return;
1711  }
1712  /* try again */
1713  pq->read_task =
1715  return;
1716  }
1717  pq->ibuf_off += rcvd;
1718  if (pq->ibuf_off > sizeof(pq->ibuf))
1719  {
1720  /* read more */
1721  pq->read_task =
1723  return;
1724  }
1725  /* we got all the data, let's find out who we are talking to! */
1726  queue = GNUNET_new (struct Queue);
1727  setup_in_cipher ((const struct GNUNET_CRYPTO_EcdhePublicKey *) pq->ibuf,
1728  queue);
1729  if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, pq->ibuf))
1730  {
1732  "Invalid TCP KX received from %s\n",
1733  GNUNET_a2s (queue->address, queue->address_len));
1734  gcry_cipher_close (queue->in_cipher);
1735  GNUNET_free (queue);
1736  free_proto_queue (pq);
1737  return;
1738  }
1739  queue->address = pq->address; /* steals reference */
1740  queue->address_len = pq->address_len;
1741  queue->target = tc.sender;
1742  queue->sock = pq->sock;
1743  start_initial_kx_out (queue);
1745  queue->read_task =
1747  queue->sock,
1748  &queue_read,
1749  queue);
1750  queue->write_task =
1752  queue->sock,
1753  &queue_write,
1754  queue);
1755  GNUNET_CONTAINER_DLL_remove (proto_head, proto_tail, pq);
1756  GNUNET_free (pq);
1757 }
1758 
1759 
1767 static void
1768 listen_cb (void *cls)
1769 {
1770  struct sockaddr_storage in;
1771  socklen_t addrlen;
1772  struct GNUNET_NETWORK_Handle *sock;
1773  struct ProtoQueue *pq;
1774 
1775  listen_task = NULL;
1776  GNUNET_assert (NULL != listen_sock);
1777  addrlen = sizeof(in);
1778  memset (&in, 0, sizeof(in));
1779  sock = GNUNET_NETWORK_socket_accept (listen_sock,
1780  (struct sockaddr*) &in,
1781  &addrlen);
1782  if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno)))
1783  return; /* system limit reached, wait until connection goes down */
1785  listen_sock,
1786  &listen_cb,
1787  NULL);
1788  if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno)))
1789  return;
1790  if (NULL == sock)
1791  {
1793  return;
1794  }
1795  pq = GNUNET_new (struct ProtoQueue);
1796  pq->address_len = addrlen;
1797  pq->address = GNUNET_memdup (&in, addrlen);
1799  pq->sock = sock;
1801  pq->sock,
1802  &proto_read_kx,
1803  pq);
1804  GNUNET_CONTAINER_DLL_insert (proto_head, proto_tail, pq);
1805 }
1806 
1807 
1815 static void
1816 queue_read_kx (void *cls)
1817 {
1818  struct Queue *queue = cls;
1819  ssize_t rcvd;
1820  struct GNUNET_TIME_Relative left;
1821  struct TCPConfirmation tc;
1822 
1823  queue->read_task = NULL;
1825  if (0 == left.rel_value_us)
1826  {
1827  queue_destroy (queue);
1828  return;
1829  }
1830  rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
1831  &queue->cread_buf[queue->cread_off],
1832  BUF_SIZE - queue->cread_off);
1833  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %lu bytes for KX\n", rcvd);
1834  if (-1 == rcvd)
1835  {
1836  if ((EAGAIN != errno) && (EINTR != errno))
1837  {
1839  queue_destroy (queue);
1840  return;
1841  }
1842  queue->read_task =
1843  GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read_kx, queue);
1844  return;
1845  }
1846  queue->cread_off += rcvd;
1847  if (queue->cread_off < INITIAL_KX_SIZE)
1848  {
1849  /* read more */
1850  queue->read_task =
1851  GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read_kx, queue);
1852  return;
1853  }
1854  /* we got all the data, let's find out who we are talking to! */
1856  queue->cread_buf,
1857  queue);
1858  if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, queue->cread_buf))
1859  {
1861  "Invalid TCP KX received from %s\n",
1862  GNUNET_a2s (queue->address, queue->address_len));
1863  queue_destroy (queue);
1864  return;
1865  }
1866  if (0 !=
1867  memcmp (&tc.sender, &queue->target, sizeof(struct GNUNET_PeerIdentity)))
1868  {
1870  "Invalid sender in TCP KX received from %s\n",
1871  GNUNET_a2s (queue->address, queue->address_len));
1872  queue_destroy (queue);
1873  return;
1874  }
1875 
1876  /* update queue timeout */
1877  reschedule_queue_timeout (queue);
1878  /* prepare to continue with regular read task immediately */
1879  memmove (queue->cread_buf,
1880  &queue->cread_buf[INITIAL_KX_SIZE],
1881  queue->cread_off - (INITIAL_KX_SIZE));
1882  queue->cread_off -= INITIAL_KX_SIZE;
1883  if (0 < queue->cread_off)
1884  queue->read_task = GNUNET_SCHEDULER_add_now (&queue_read, queue);
1885 }
1886 
1887 
1906 static int
1907 mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
1908 {
1909  struct Queue *queue;
1910  const char *path;
1911  struct sockaddr *in;
1912  socklen_t in_len;
1913  struct GNUNET_NETWORK_Handle *sock;
1914 
1916  "Connecting to %s\n", address);
1917  if (0 != strncmp (address,
1919  strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
1920  {
1921  GNUNET_break_op (0);
1922  return GNUNET_SYSERR;
1923  }
1924  path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
1925  in = tcp_address_to_sockaddr (path, &in_len);
1926 
1927  sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP);
1928  if (NULL == sock)
1929  {
1931  "socket(%d) failed: %s",
1932  in->sa_family,
1933  strerror (errno));
1934  GNUNET_free (in);
1935  return GNUNET_SYSERR;
1936  }
1937  if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, in_len)) &&
1938  (errno != EINPROGRESS))
1939  {
1941  "connect to `%s' failed: %s",
1942  address,
1943  strerror (errno));
1945  GNUNET_free (in);
1946  return GNUNET_SYSERR;
1947  }
1948 
1949  queue = GNUNET_new (struct Queue);
1950  queue->target = *peer;
1951  queue->address = in;
1952  queue->address_len = in_len;
1953  queue->sock = sock;
1955  // queue->mq_awaits_continue = GNUNET_YES;
1956  queue->read_task =
1958  queue->sock,
1959  &queue_read_kx,
1960  queue);
1961  start_initial_kx_out (queue);
1962  queue->write_task =
1964  queue->sock,
1965  &queue_write,
1966  queue);
1967  return GNUNET_OK;
1968 }
1969 
1970 
1979 static int
1981  const struct GNUNET_PeerIdentity *target,
1982  void *value)
1983 {
1984  struct Queue *queue = value;
1985 
1986  (void) cls;
1987  (void) target;
1988  queue_destroy (queue);
1989  return GNUNET_OK;
1990 }
1991 
1992 
1998 static void
1999 do_shutdown (void *cls)
2000 {
2001  while (NULL != proto_head)
2002  free_proto_queue (proto_head);
2003  if (NULL != nat)
2004  {
2005  GNUNET_NAT_unregister (nat);
2006  nat = NULL;
2007  }
2008  if (NULL != listen_task)
2009  {
2010  GNUNET_SCHEDULER_cancel (listen_task);
2011  listen_task = NULL;
2012  }
2013  if (NULL != listen_sock)
2014  {
2016  listen_sock = NULL;
2017  }
2020  if (NULL != ch)
2021  {
2023  ch = NULL;
2024  }
2025  if (NULL != stats)
2026  {
2028  stats = NULL;
2029  }
2030  if (NULL != my_private_key)
2031  {
2032  GNUNET_free (my_private_key);
2033  my_private_key = NULL;
2034  }
2035  if (NULL != is)
2036  {
2038  is = NULL;
2039  }
2040 }
2041 
2042 
2054 static void
2055 enc_notify_cb (void *cls,
2056  const struct GNUNET_PeerIdentity *sender,
2057  const struct GNUNET_MessageHeader *msg)
2058 {
2059  (void) cls;
2060  (void) sender;
2061  (void) msg;
2062  GNUNET_break_op (0);
2063 }
2064 
2065 
2079 static void
2080 nat_address_cb (void *cls,
2081  void **app_ctx,
2082  int add_remove,
2084  const struct sockaddr *addr,
2085  socklen_t addrlen)
2086 {
2087  char *my_addr;
2089 
2090  if (GNUNET_YES == add_remove)
2091  {
2092  enum GNUNET_NetworkType nt;
2093 
2094  GNUNET_asprintf (&my_addr,
2095  "%s-%s",
2097  GNUNET_a2s (addr, addrlen));
2098  nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
2099  ai =
2101  my_addr,
2102  nt,
2104  GNUNET_free (my_addr);
2105  *app_ctx = ai;
2106  }
2107  else
2108  {
2109  ai = *app_ctx;
2111  *app_ctx = NULL;
2112  }
2113 }
2114 
2115 
2124 static void
2125 run (void *cls,
2126  char *const *args,
2127  const char *cfgfile,
2128  const struct GNUNET_CONFIGURATION_Handle *c)
2129 {
2130  char *bindto;
2131  struct sockaddr *in;
2132  socklen_t in_len;
2133  struct sockaddr_storage in_sto;
2134  socklen_t sto_len;
2135 
2136  (void) cls;
2137  cfg = c;
2138  if (GNUNET_OK !=
2141  "BINDTO",
2142  &bindto))
2143  {
2146  "BINDTO");
2147  return;
2148  }
2149  if (GNUNET_OK !=
2152  "MAX_QUEUE_LENGTH",
2153  &max_queue_length))
2155  if (GNUNET_OK !=
2158  "REKEY_INTERVAL",
2159  &rekey_interval))
2161 
2162  in = tcp_address_to_sockaddr (bindto, &in_len);
2163  if (NULL == in)
2164  {
2166  "Failed to setup TCP socket address with path `%s'\n",
2167  bindto);
2168  GNUNET_free (bindto);
2169  return;
2170  }
2171  listen_sock =
2172  GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP);
2173  if (NULL == listen_sock)
2174  {
2176  GNUNET_free (in);
2177  GNUNET_free (bindto);
2178  return;
2179  }
2180  if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, in, in_len))
2181  {
2183  GNUNET_NETWORK_socket_close (listen_sock);
2184  listen_sock = NULL;
2185  GNUNET_free (in);
2186  GNUNET_free (bindto);
2187  return;
2188  }
2189  if (GNUNET_OK !=
2190  GNUNET_NETWORK_socket_listen (listen_sock,
2191  5))
2192  {
2194  "listen");
2195  GNUNET_NETWORK_socket_close (listen_sock);
2196  listen_sock = NULL;
2197  GNUNET_free (in);
2198  GNUNET_free (bindto);
2199  }
2200  /* We might have bound to port 0, allowing the OS to figure it out;
2201  thus, get the real IN-address from the socket */
2202  sto_len = sizeof(in_sto);
2203  if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock),
2204  (struct sockaddr *) &in_sto,
2205  &sto_len))
2206  {
2207  memcpy (&in_sto, in, in_len);
2208  sto_len = in_len;
2209  }
2210  GNUNET_free (in);
2211  GNUNET_free (bindto);
2212  in = (struct sockaddr *) &in_sto;
2213  in_len = sto_len;
2215  "Bound to `%s'\n",
2216  GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
2217  stats = GNUNET_STATISTICS_create ("C-TCP", cfg);
2219  is = GNUNET_NT_scanner_init ();
2221  if (NULL == my_private_key)
2222  {
2223  GNUNET_log (
2225  _ (
2226  "Transport service is lacking key configuration settings. Exiting.\n"));
2228  return;
2229  }
2231  /* start listening */
2233  listen_sock,
2234  &listen_cb,
2235  NULL);
2241  &mq_init,
2242  NULL,
2243  &enc_notify_cb,
2244  NULL);
2245  if (NULL == ch)
2246  {
2247  GNUNET_break (0);
2249  return;
2250  }
2251  nat = GNUNET_NAT_register (cfg,
2253  IPPROTO_TCP,
2254  1 /* one address */,
2255  (const struct sockaddr **) &in,
2256  &in_len,
2257  &nat_address_cb,
2258  NULL /* FIXME: support reversal: #5529 */,
2259  NULL /* closure */);
2260 }
2261 
2262 
2270 int
2271 main (int argc, char *const *argv)
2272 {
2273  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2275  };
2276  int ret;
2277 
2278  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2279  return 2;
2280 
2281  ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc,
2282  argv,
2283  "gnunet-communicator-tcp",
2284  _ ("GNUnet TCP communicator"),
2285  options,
2286  &run,
2287  NULL))
2288  ? 0
2289  : 1;
2290  GNUNET_free ((void *) argv);
2291  return ret;
2292 }
2293 
2294 
2295 /* end of gnunet-communicator-tcp.c */
#define GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE
Signature used by TCP communicator handshake,.
int GNUNET_CRYPTO_ecdhe_key_create2(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
Create a new private key.
Definition: crypto_ecc.c:521
#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.
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()
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.
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.
#define GNUNET_CRYPTO_eddsa_sign(priv, ps, sig)
EdDSA sign a given block.
static struct GNUNET_NAT_Handle * nat
Connection to NAT service.
int finishing
Did we enqueue a finish message and are closing down the queue?
struct GNUNET_HashCode in_hmac
Shared secret for HMAC verification on incoming data.
struct GNUNET_PeerIdentity sender
Identity of the inititor of the TCP connection (TCP client).
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
socklen_t address_len
Length of the address.
ssize_t GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc, const void *buffer, size_t length)
Send data (always non-blocking).
Definition: network.c:816
static struct GNUNET_PeerIdentity my_identity
Our public key.
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
size_t cread_off
At which offset in the ciphertext read buffer should we append more ciphertext for transmission next...
struct GNUNET_TRANSPORT_AddressIdentifier * GNUNET_TRANSPORT_communicator_address_add(struct GNUNET_TRANSPORT_CommunicatorHandle *ch, const char *address, enum GNUNET_NetworkType nt, struct GNUNET_TIME_Relative expiration)
Notify transport service about an address that this communicator provides for this peer...
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Setup communicator and launch network interactions.
struct GNUNET_CRYPTO_EcdhePublicKey ephemeral
Ephemeral key used by the sender.
void GNUNET_CRYPTO_hmac_raw(const void *key, size_t key_len, const void *plaintext, size_t plaintext_len, struct GNUNET_HashCode *hmac)
Calculate HMAC of a message (RFC 2104) TODO: Shouldn&#39; this be the standard hmac function and the abov...
Definition: crypto_hash.c:403
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:1333
struct GNUNET_CRYPTO_EddsaSignature sender_sig
Sender&#39;s signature of type GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE.
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.
static struct GNUNET_SCHEDULER_Task * listen_task
ID of listen task.
#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:1590
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.
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?
#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.
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.
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
#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:180
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...
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 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:1059
static char * value
Value of the record to add/remove.
Handle for a queue.
#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: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:1298
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.
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.
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:1120
char cwrite_buf[(2 *64 *1024+sizeof(struct TCPBox))]
buffer for writing ciphertext to network.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
static void do_rekey(struct Queue *queue, const struct TCPRekey *rekey)
Handle rekey message on queue.
char pwrite_buf[UINT16_MAX+1+sizeof(struct TCPBox)]
Plaintext buffer for messages to be encrypted.
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
void GNUNET_NT_scanner_done(struct GNUNET_NT_InterfaceScanner *is)
Terminate interface scanner.
Definition: nt.c:433
static struct sockaddr * tcp_address_to_sockaddr(const char *bindto, socklen_t *sock_len)
Convert TCP bind specification to a struct sockaddr *
int GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
static void mq_cancel(struct GNUNET_MQ_Handle *mq, void *impl_state)
Implementation function that cancels the currently sent message.
struct GNUNET_TIME_AbsoluteNBO monotonic_time
Monotonic time of sender, to possibly help detect replay attacks (if receiver persists times by sende...
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
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...
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:206
static int get_queue_delete_it(void *cls, const struct GNUNET_PeerIdentity *target, void *value)
Iterator over all message queues to clean up.
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...
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
static struct ProtoQueue * proto_tail
Protoqueues DLL tail.
#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
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:189
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
static struct GNUNET_NETWORK_Handle * listen_sock
Listen socket.
#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.
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.
TCP rekey message box.
static uint16_t port
Port number.
Definition: gnunet-bcd.c:81
struct GNUNET_ShortHashCode hmac
HMAC for the following encrypted message.
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.
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
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
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:1519
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...
GNUNET_TRANSPORT_ConnectionStatus
Possible states of a connection.
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.
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).
#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.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:974