diff options
author | Christian Grothoff <christian@grothoff.org> | 2020-12-01 08:39:13 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2020-12-01 08:39:13 +0100 |
commit | 4a085eddc5b52b472cc1320944975fb11c6ebe27 (patch) | |
tree | bff97df59ef2ab8e3a810e596ab636a9a02d3e78 | |
parent | 795ba5243bfcb595cd3b3066ae22f59c686c48bb (diff) | |
parent | ddbb4c740614d988431700bdcbb358de00be6313 (diff) | |
download | gnunet-4a085eddc5b52b472cc1320944975fb11c6ebe27.tar.gz gnunet-4a085eddc5b52b472cc1320944975fb11c6ebe27.zip |
Merge branch 'master' of git+ssh://gnunet.org/gnunet
-rw-r--r-- | po/POTFILES.in | 4 | ||||
-rw-r--r-- | src/transport/Makefile.am | 8 | ||||
-rw-r--r-- | src/transport/gnunet-communicator-udp.c | 829 | ||||
-rw-r--r-- | src/transport/test_communicator_basic.c | 33 | ||||
-rw-r--r-- | src/transport/test_communicator_udp_rekey_peer1.conf | 8 | ||||
-rw-r--r-- | src/transport/test_communicator_udp_rekey_peer2.conf | 8 | ||||
-rw-r--r-- | src/transport/transport-testing-communicator.c | 1164 | ||||
-rw-r--r-- | src/transport/transport-testing-communicator.h | 355 | ||||
-rw-r--r-- | src/transport/transport-testing2.c | 1697 | ||||
-rw-r--r-- | src/transport/transport-testing2.h | 971 |
10 files changed, 3749 insertions, 1328 deletions
diff --git a/po/POTFILES.in b/po/POTFILES.in index 00bdd7cbc..b0371ba21 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in | |||
@@ -205,8 +205,8 @@ src/json/json_generator.c | |||
205 | src/json/json_helper.c | 205 | src/json/json_helper.c |
206 | src/json/json_mhd.c | 206 | src/json/json_mhd.c |
207 | src/messenger/gnunet-messenger.c | 207 | src/messenger/gnunet-messenger.c |
208 | src/messenger/gnunet-service-messenger.c | ||
209 | src/messenger/gnunet-service-messenger_basement.c | 208 | src/messenger/gnunet-service-messenger_basement.c |
209 | src/messenger/gnunet-service-messenger.c | ||
210 | src/messenger/gnunet-service-messenger_contact.c | 210 | src/messenger/gnunet-service-messenger_contact.c |
211 | src/messenger/gnunet-service-messenger_handle.c | 211 | src/messenger/gnunet-service-messenger_handle.c |
212 | src/messenger/gnunet-service-messenger_list_handles.c | 212 | src/messenger/gnunet-service-messenger_list_handles.c |
@@ -545,7 +545,5 @@ src/vpn/vpn_api.c | |||
545 | src/zonemaster/gnunet-service-zonemaster.c | 545 | src/zonemaster/gnunet-service-zonemaster.c |
546 | src/zonemaster/gnunet-service-zonemaster-monitor.c | 546 | src/zonemaster/gnunet-service-zonemaster-monitor.c |
547 | src/fs/fs_api.h | 547 | src/fs/fs_api.h |
548 | src/include/gnunet_identity_service.h | ||
549 | src/include/gnunet_messenger_service.h | ||
550 | src/testbed/testbed_api.h | 548 | src/testbed/testbed_api.h |
551 | src/testbed/testbed_api_operations.h | 549 | src/testbed/testbed_api_operations.h |
diff --git a/src/transport/Makefile.am b/src/transport/Makefile.am index e0369059d..800a27aa2 100644 --- a/src/transport/Makefile.am +++ b/src/transport/Makefile.am | |||
@@ -168,10 +168,16 @@ libgnunettransporttesting_la_LDFLAGS = \ | |||
168 | $(GN_LIB_LDFLAGS) | 168 | $(GN_LIB_LDFLAGS) |
169 | 169 | ||
170 | libgnunettransporttesting2_la_SOURCES = \ | 170 | libgnunettransporttesting2_la_SOURCES = \ |
171 | transport-testing2.c transport-testing2.h | 171 | transport-testing2.c transport-testing2.h \ |
172 | transport-testing-communicator.c transport-testing-communicator.h | ||
172 | libgnunettransporttesting2_la_LIBADD = \ | 173 | libgnunettransporttesting2_la_LIBADD = \ |
173 | libgnunettransport.la \ | 174 | libgnunettransport.la \ |
175 | libgnunettransportapplication.la \ | ||
176 | libgnunettransportcore.la \ | ||
177 | $(top_builddir)/src/testing/libgnunettesting.la \ | ||
178 | $(top_builddir)/src/ats/libgnunetats.la \ | ||
174 | $(top_builddir)/src/hello/libgnunethello.la \ | 179 | $(top_builddir)/src/hello/libgnunethello.la \ |
180 | $(top_builddir)/src/peerstore/libgnunetpeerstore.la \ | ||
175 | $(top_builddir)/src/util/libgnunetutil.la | 181 | $(top_builddir)/src/util/libgnunetutil.la |
176 | libgnunettransporttesting2_la_LDFLAGS = \ | 182 | libgnunettransporttesting2_la_LDFLAGS = \ |
177 | $(GN_LIBINTL) \ | 183 | $(GN_LIBINTL) \ |
diff --git a/src/transport/gnunet-communicator-udp.c b/src/transport/gnunet-communicator-udp.c index e967e8e9a..073b1ea02 100644 --- a/src/transport/gnunet-communicator-udp.c +++ b/src/transport/gnunet-communicator-udp.c | |||
@@ -51,7 +51,7 @@ | |||
51 | /** | 51 | /** |
52 | * How often do we rekey based on time (at least) | 52 | * How often do we rekey based on time (at least) |
53 | */ | 53 | */ |
54 | #define REKEY_TIME_INTERVAL GNUNET_TIME_UNIT_DAYS | 54 | #define DEFAULT_REKEY_TIME_INTERVAL GNUNET_TIME_UNIT_DAYS |
55 | 55 | ||
56 | /** | 56 | /** |
57 | * How long do we wait until we must have received the initial KX? | 57 | * How long do we wait until we must have received the initial KX? |
@@ -207,6 +207,11 @@ struct InitialKX | |||
207 | * key derived from the handshake with sequence number zero. | 207 | * key derived from the handshake with sequence number zero. |
208 | */ | 208 | */ |
209 | char gcm_tag[GCM_TAG_SIZE]; | 209 | char gcm_tag[GCM_TAG_SIZE]; |
210 | |||
211 | /** | ||
212 | * A flag indicating, if the sender is doing rekeying. | ||
213 | */ | ||
214 | int rekeying; | ||
210 | }; | 215 | }; |
211 | 216 | ||
212 | 217 | ||
@@ -339,8 +344,42 @@ struct UDPBox | |||
339 | * `struct UdpHandshakeSignature`. | 344 | * `struct UdpHandshakeSignature`. |
340 | */ | 345 | */ |
341 | char gcm_tag[GCM_TAG_SIZE]; | 346 | char gcm_tag[GCM_TAG_SIZE]; |
347 | |||
348 | /** | ||
349 | * A flag indicating, if the sender is doing rekeying. | ||
350 | */ | ||
351 | int rekeying; | ||
342 | }; | 352 | }; |
343 | 353 | ||
354 | /** | ||
355 | * UDP message box. Always sent encrypted, only allowed after | ||
356 | * the receiver sent a `struct UDPAck` for the base key! | ||
357 | */ | ||
358 | struct UDPRekey | ||
359 | { | ||
360 | /** | ||
361 | * Key and IV identification code. KDF applied to an acknowledged | ||
362 | * base key and a sequence number. Sequence numbers must be used | ||
363 | * monotonically increasing up to the maximum specified in | ||
364 | * `struct UDPAck`. Without further `struct UDPAck`s, the sender | ||
365 | * must fall back to sending handshakes! | ||
366 | */ | ||
367 | struct GNUNET_ShortHashCode kid; | ||
368 | |||
369 | /** | ||
370 | * 128-bit authentication tag for the following encrypted message, | ||
371 | * from GCM. MAC starts at the @e body_start that follows and | ||
372 | * extends until the end of the UDP payload. If the @e hmac is | ||
373 | * wrong, the receiver should check if the message might be a | ||
374 | * `struct UdpHandshakeSignature`. | ||
375 | */ | ||
376 | char gcm_tag[GCM_TAG_SIZE]; | ||
377 | |||
378 | /** | ||
379 | * Sender's identity | ||
380 | */ | ||
381 | struct GNUNET_PeerIdentity sender; | ||
382 | }; | ||
344 | 383 | ||
345 | GNUNET_NETWORK_STRUCT_END | 384 | GNUNET_NETWORK_STRUCT_END |
346 | 385 | ||
@@ -471,6 +510,11 @@ struct SharedSecret | |||
471 | struct SenderAddress | 510 | struct SenderAddress |
472 | { | 511 | { |
473 | /** | 512 | /** |
513 | * Flag indicating sender is initiated rekeying for this receiver. | ||
514 | */ | ||
515 | int rekeying; | ||
516 | |||
517 | /** | ||
474 | * To whom are we talking to. | 518 | * To whom are we talking to. |
475 | */ | 519 | */ |
476 | struct GNUNET_PeerIdentity target; | 520 | struct GNUNET_PeerIdentity target; |
@@ -520,6 +564,11 @@ struct SenderAddress | |||
520 | * Which network type does this queue use? | 564 | * Which network type does this queue use? |
521 | */ | 565 | */ |
522 | enum GNUNET_NetworkType nt; | 566 | enum GNUNET_NetworkType nt; |
567 | |||
568 | /** | ||
569 | * sender_destroy already called on sender. | ||
570 | */ | ||
571 | int sender_destroy_called; | ||
523 | }; | 572 | }; |
524 | 573 | ||
525 | 574 | ||
@@ -529,6 +578,37 @@ struct SenderAddress | |||
529 | */ | 578 | */ |
530 | struct ReceiverAddress | 579 | struct ReceiverAddress |
531 | { | 580 | { |
581 | |||
582 | /** | ||
583 | * Shared secret we use with @e target for rekeying. | ||
584 | */ | ||
585 | struct SharedSecret *ss_rekey; | ||
586 | |||
587 | /** | ||
588 | * Acks available when we started rekeying. | ||
589 | */ | ||
590 | unsigned int rekey_acks_available; | ||
591 | |||
592 | /** | ||
593 | * Send bytes for this receiver address. | ||
594 | */ | ||
595 | uint64_t rekey_send_bytes; | ||
596 | |||
597 | /** | ||
598 | * Timeout for this receiver address. | ||
599 | */ | ||
600 | struct GNUNET_TIME_Absolute rekey_timeout; | ||
601 | |||
602 | /** | ||
603 | * Flag indicating sender is initiated rekeying for this receiver. | ||
604 | */ | ||
605 | int rekeying; | ||
606 | |||
607 | /** | ||
608 | * Numer of kce we retain for sending the rekeying shared secret. | ||
609 | */ | ||
610 | int number_rekeying_kce; | ||
611 | |||
532 | /** | 612 | /** |
533 | * To whom are we talking to. | 613 | * To whom are we talking to. |
534 | */ | 614 | */ |
@@ -615,8 +695,12 @@ struct ReceiverAddress | |||
615 | * Which network type does this queue use? | 695 | * Which network type does this queue use? |
616 | */ | 696 | */ |
617 | enum GNUNET_NetworkType nt; | 697 | enum GNUNET_NetworkType nt; |
618 | }; | ||
619 | 698 | ||
699 | /** | ||
700 | * receiver_destroy already called on receiver. | ||
701 | */ | ||
702 | int receiver_destroy_called; | ||
703 | }; | ||
620 | 704 | ||
621 | /** | 705 | /** |
622 | * Interface we broadcast our presence on. | 706 | * Interface we broadcast our presence on. |
@@ -671,9 +755,9 @@ struct BroadcastInterface | |||
671 | }; | 755 | }; |
672 | 756 | ||
673 | /** | 757 | /** |
674 | * Timeout for this receiver address. | 758 | * The rekey interval |
675 | */ | 759 | */ |
676 | struct GNUNET_TIME_Absolute *rekey_timeout; | 760 | static struct GNUNET_TIME_Relative rekey_interval; |
677 | 761 | ||
678 | /** | 762 | /** |
679 | * Shared secret we finished the last kce working queue for. | 763 | * Shared secret we finished the last kce working queue for. |
@@ -701,6 +785,11 @@ static struct GNUNET_SCHEDULER_Task *timeout_task; | |||
701 | static struct GNUNET_SCHEDULER_Task *kce_task; | 785 | static struct GNUNET_SCHEDULER_Task *kce_task; |
702 | 786 | ||
703 | /** | 787 | /** |
788 | * ID of kce rekey working queue task | ||
789 | */ | ||
790 | static struct GNUNET_SCHEDULER_Task *kce_task_rekey; | ||
791 | |||
792 | /** | ||
704 | * Is the kce_task finished? | 793 | * Is the kce_task finished? |
705 | */ | 794 | */ |
706 | static int kce_task_finished = GNUNET_NO; | 795 | static int kce_task_finished = GNUNET_NO; |
@@ -832,26 +921,28 @@ bi_destroy (struct BroadcastInterface *bi) | |||
832 | static void | 921 | static void |
833 | receiver_destroy (struct ReceiverAddress *receiver) | 922 | receiver_destroy (struct ReceiverAddress *receiver) |
834 | { | 923 | { |
835 | struct GNUNET_MQ_Handle *mq; | 924 | |
925 | receiver->receiver_destroy_called = GNUNET_YES; | ||
836 | 926 | ||
837 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 927 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
838 | "Disconnecting receiver for peer `%s'\n", | 928 | "Disconnecting receiver for peer `%s'\n", |
839 | GNUNET_i2s (&receiver->target)); | 929 | GNUNET_i2s (&receiver->target)); |
840 | if (NULL != (mq = receiver->kx_mq)) | 930 | /*if (NULL != (mq = receiver->kx_mq)) |
841 | { | 931 | { |
842 | receiver->kx_mq = NULL; | 932 | receiver->kx_mq = NULL; |
843 | GNUNET_MQ_destroy (mq); | 933 | GNUNET_MQ_destroy (mq); |
844 | } | 934 | }*/ |
845 | if (NULL != receiver->kx_qh) | 935 | if (NULL != receiver->kx_qh) |
846 | { | 936 | { |
847 | GNUNET_TRANSPORT_communicator_mq_del (receiver->kx_qh); | 937 | GNUNET_TRANSPORT_communicator_mq_del (receiver->kx_qh); |
848 | receiver->kx_qh = NULL; | 938 | receiver->kx_qh = NULL; |
939 | receiver->kx_mq = NULL; | ||
849 | } | 940 | } |
850 | if (NULL != (mq = receiver->d_mq)) | 941 | /*if (NULL != (mq = receiver->d_mq)) |
851 | { | 942 | { |
852 | receiver->d_mq = NULL; | 943 | receiver->d_mq = NULL; |
853 | GNUNET_MQ_destroy (mq); | 944 | GNUNET_MQ_destroy (mq); |
854 | } | 945 | }*/ |
855 | if (NULL != receiver->d_qh) | 946 | if (NULL != receiver->d_qh) |
856 | { | 947 | { |
857 | GNUNET_TRANSPORT_communicator_mq_del (receiver->d_qh); | 948 | GNUNET_TRANSPORT_communicator_mq_del (receiver->d_qh); |
@@ -956,8 +1047,9 @@ kce_generate (struct SharedSecret *ss, uint32_t seq) | |||
956 | * Destroy @a ss and associated key cache entries. | 1047 | * Destroy @a ss and associated key cache entries. |
957 | * | 1048 | * |
958 | * @param ss shared secret to destroy | 1049 | * @param ss shared secret to destroy |
1050 | * @param withoutKce If GNUNET_YES shared secrets with kce will not be destroyed. | ||
959 | */ | 1051 | */ |
960 | static void | 1052 | static int |
961 | secret_destroy (struct SharedSecret *ss, int withoutKce) | 1053 | secret_destroy (struct SharedSecret *ss, int withoutKce) |
962 | { | 1054 | { |
963 | struct SenderAddress *sender; | 1055 | struct SenderAddress *sender; |
@@ -970,7 +1062,7 @@ secret_destroy (struct SharedSecret *ss, int withoutKce) | |||
970 | ss->sequence_allowed); | 1062 | ss->sequence_allowed); |
971 | 1063 | ||
972 | if (withoutKce && (ss->sequence_allowed > 0)) | 1064 | if (withoutKce && (ss->sequence_allowed > 0)) |
973 | return; | 1065 | return GNUNET_NO; |
974 | 1066 | ||
975 | if (NULL != (sender = ss->sender)) | 1067 | if (NULL != (sender = ss->sender)) |
976 | { | 1068 | { |
@@ -993,6 +1085,7 @@ secret_destroy (struct SharedSecret *ss, int withoutKce) | |||
993 | GNUNET_CONTAINER_multishortmap_size (key_cache), | 1085 | GNUNET_CONTAINER_multishortmap_size (key_cache), |
994 | GNUNET_NO); | 1086 | GNUNET_NO); |
995 | GNUNET_free (ss); | 1087 | GNUNET_free (ss); |
1088 | return GNUNET_YES; | ||
996 | } | 1089 | } |
997 | 1090 | ||
998 | 1091 | ||
@@ -1005,6 +1098,7 @@ secret_destroy (struct SharedSecret *ss, int withoutKce) | |||
1005 | static void | 1098 | static void |
1006 | sender_destroy (struct SenderAddress *sender) | 1099 | sender_destroy (struct SenderAddress *sender) |
1007 | { | 1100 | { |
1101 | sender->sender_destroy_called = GNUNET_YES; | ||
1008 | GNUNET_assert ( | 1102 | GNUNET_assert ( |
1009 | GNUNET_YES == | 1103 | GNUNET_YES == |
1010 | GNUNET_CONTAINER_multipeermap_remove (senders, &sender->target, sender)); | 1104 | GNUNET_CONTAINER_multipeermap_remove (senders, &sender->target, sender)); |
@@ -1100,20 +1194,26 @@ check_timeouts (void *cls) | |||
1100 | rt = GNUNET_TIME_UNIT_FOREVER_REL; | 1194 | rt = GNUNET_TIME_UNIT_FOREVER_REL; |
1101 | while (NULL != (receiver = GNUNET_CONTAINER_heap_peek (receivers_heap))) | 1195 | while (NULL != (receiver = GNUNET_CONTAINER_heap_peek (receivers_heap))) |
1102 | { | 1196 | { |
1103 | rt = GNUNET_TIME_absolute_get_remaining (receiver->timeout); | 1197 | if (GNUNET_YES != receiver->receiver_destroy_called) |
1104 | if (0 != rt.rel_value_us) | 1198 | { |
1105 | break; | 1199 | rt = GNUNET_TIME_absolute_get_remaining (receiver->timeout); |
1106 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1200 | if (0 != rt.rel_value_us) |
1107 | "Receiver timed out\n"); | 1201 | break; |
1108 | receiver_destroy (receiver); | 1202 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1203 | "Receiver timed out\n"); | ||
1204 | receiver_destroy (receiver); | ||
1205 | } | ||
1109 | } | 1206 | } |
1110 | st = GNUNET_TIME_UNIT_FOREVER_REL; | 1207 | st = GNUNET_TIME_UNIT_FOREVER_REL; |
1111 | while (NULL != (sender = GNUNET_CONTAINER_heap_peek (senders_heap))) | 1208 | while (NULL != (sender = GNUNET_CONTAINER_heap_peek (senders_heap))) |
1112 | { | 1209 | { |
1113 | st = GNUNET_TIME_absolute_get_remaining (sender->timeout); | 1210 | if (GNUNET_YES != sender->sender_destroy_called) |
1114 | if (0 != st.rel_value_us) | 1211 | { |
1115 | break; | 1212 | st = GNUNET_TIME_absolute_get_remaining (sender->timeout); |
1116 | sender_destroy (sender); | 1213 | if (0 != st.rel_value_us) |
1214 | break; | ||
1215 | sender_destroy (sender); | ||
1216 | } | ||
1117 | } | 1217 | } |
1118 | delay = GNUNET_TIME_relative_min (rt, st); | 1218 | delay = GNUNET_TIME_relative_min (rt, st); |
1119 | if (delay.rel_value_us < GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) | 1219 | if (delay.rel_value_us < GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) |
@@ -1289,7 +1389,7 @@ setup_shared_secret_dec (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral) | |||
1289 | */ | 1389 | */ |
1290 | static struct SharedSecret * | 1390 | static struct SharedSecret * |
1291 | setup_shared_secret_enc (const struct GNUNET_CRYPTO_EcdhePrivateKey *ephemeral, | 1391 | setup_shared_secret_enc (const struct GNUNET_CRYPTO_EcdhePrivateKey *ephemeral, |
1292 | struct ReceiverAddress *receiver) | 1392 | struct ReceiverAddress *receiver, int add_to_receiver) |
1293 | { | 1393 | { |
1294 | struct SharedSecret *ss; | 1394 | struct SharedSecret *ss; |
1295 | 1395 | ||
@@ -1316,6 +1416,114 @@ setup_shared_secret_enc (const struct GNUNET_CRYPTO_EcdhePrivateKey *ephemeral, | |||
1316 | static void | 1416 | static void |
1317 | setup_receiver_mq (struct ReceiverAddress *receiver); | 1417 | setup_receiver_mq (struct ReceiverAddress *receiver); |
1318 | 1418 | ||
1419 | /** | ||
1420 | * Destroying all secrets. Depending on parameter we keep those secrets having a kce. | ||
1421 | * | ||
1422 | * @param ss The secret we will not destroy. | ||
1423 | * @param withoutKce If GNUNET_YES shared secrets with kce will not be destroyed. | ||
1424 | */ | ||
1425 | static void | ||
1426 | destroy_all_secrets (struct SharedSecret *ss, int withoutKce) | ||
1427 | { | ||
1428 | struct SenderAddress *sender; | ||
1429 | struct ReceiverAddress *receiver; | ||
1430 | struct SharedSecret *ss_to_destroy; | ||
1431 | struct SharedSecret *ss_start; | ||
1432 | struct SharedSecret *pos; | ||
1433 | int at_least_one_destroyed = GNUNET_NO; | ||
1434 | |||
1435 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1436 | "Starting destroy all.\n"); | ||
1437 | |||
1438 | if (NULL != (sender = ss->sender)) | ||
1439 | { | ||
1440 | ss_start = sender->ss_head; | ||
1441 | } | ||
1442 | else if (NULL != (receiver = ss->receiver)) | ||
1443 | { | ||
1444 | ss_start = receiver->ss_head; | ||
1445 | } | ||
1446 | else | ||
1447 | { | ||
1448 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1449 | "Shared secret has no sender or receiver!\n"); | ||
1450 | return; | ||
1451 | } | ||
1452 | |||
1453 | pos = ss_start; | ||
1454 | while ( NULL != pos) | ||
1455 | { | ||
1456 | ss_to_destroy = pos; | ||
1457 | pos = pos->next; | ||
1458 | |||
1459 | if (ss != ss_to_destroy) | ||
1460 | at_least_one_destroyed = secret_destroy (ss_to_destroy, withoutKce); | ||
1461 | } | ||
1462 | |||
1463 | if ((ss != ss_start) && ! at_least_one_destroyed) | ||
1464 | { | ||
1465 | destroy_all_secrets (ss_start, GNUNET_NO); | ||
1466 | } | ||
1467 | |||
1468 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1469 | "Finished destroy all.\n"); | ||
1470 | } | ||
1471 | |||
1472 | static void | ||
1473 | add_acks (struct SharedSecret *ss, int acks_to_add, int remove_from_receiver) | ||
1474 | { | ||
1475 | |||
1476 | struct ReceiverAddress *receiver = ss->receiver; | ||
1477 | |||
1478 | if (NULL == ss) | ||
1479 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1480 | "secret NULL!\n"); | ||
1481 | |||
1482 | if (NULL == receiver) | ||
1483 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1484 | "Receiver NULL!\n"); | ||
1485 | if (NULL == receiver->d_qh) | ||
1486 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1487 | "Queue NULL!\n"); | ||
1488 | |||
1489 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1490 | "Tell transport we have %u more acks!\n", | ||
1491 | acks_to_add); | ||
1492 | GNUNET_TRANSPORT_communicator_mq_update (ch, | ||
1493 | receiver->d_qh, | ||
1494 | acks_to_add, | ||
1495 | 1); | ||
1496 | // Until here for alternativ 1 | ||
1497 | |||
1498 | /* move ss to head to avoid discarding it anytime soon! */ | ||
1499 | if (remove_from_receiver) | ||
1500 | GNUNET_CONTAINER_DLL_remove (receiver->ss_head, receiver->ss_tail, ss); | ||
1501 | GNUNET_CONTAINER_DLL_insert (receiver->ss_head, receiver->ss_tail, ss); | ||
1502 | destroy_all_secrets (ss, GNUNET_YES); | ||
1503 | |||
1504 | |||
1505 | // Uncomment this for alternativ 2 of backchannel functionality | ||
1506 | /*if (receiver->acks_available != ack->acks_available) | ||
1507 | { | ||
1508 | receiver->acks_available = ack->acks_available; | ||
1509 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1510 | "Tell transport we have different number of acks!\n"); | ||
1511 | GNUNET_TRANSPORT_communicator_mq_update (ch, | ||
1512 | receiver->d_qh, | ||
1513 | receiver->acks_available, | ||
1514 | 1); | ||
1515 | }*/ | ||
1516 | // Until here for alternativ 2 | ||
1517 | } | ||
1518 | |||
1519 | static void | ||
1520 | add_acks_rekey (struct ReceiverAddress *receiver) | ||
1521 | { | ||
1522 | add_acks (receiver->ss_rekey, receiver->ss_rekey->sequence_allowed - 3, | ||
1523 | GNUNET_NO); | ||
1524 | receiver->acks_available = receiver->ss_rekey->sequence_allowed; | ||
1525 | receiver->ss_rekey = NULL; | ||
1526 | } | ||
1319 | 1527 | ||
1320 | /** | 1528 | /** |
1321 | * We received an ACK for @a pid. Check if it is for | 1529 | * We received an ACK for @a pid. Check if it is for |
@@ -1332,18 +1540,48 @@ handle_ack (void *cls, const struct GNUNET_PeerIdentity *pid, void *value) | |||
1332 | { | 1540 | { |
1333 | const struct UDPAck *ack = cls; | 1541 | const struct UDPAck *ack = cls; |
1334 | struct ReceiverAddress *receiver = value; | 1542 | struct ReceiverAddress *receiver = value; |
1335 | struct SharedSecret *pos; | 1543 | int acks_to_add; |
1544 | uint32_t allowed; | ||
1336 | 1545 | ||
1337 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1546 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1338 | "in handle ack\n"); | 1547 | "in handle ack\n"); |
1339 | struct SharedSecret *ss_to_destroy; | 1548 | |
1549 | if (NULL != receiver->ss_rekey) | ||
1550 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1551 | "Received secret with cmac %s \n", | ||
1552 | GNUNET_h2s (&receiver->ss_rekey->cmac)); | ||
1553 | |||
1554 | if ((NULL != receiver->ss_rekey) && (0 == memcmp (&ack->cmac, | ||
1555 | &receiver->ss_rekey->cmac, | ||
1556 | sizeof(struct | ||
1557 | GNUNET_HashCode))) ) | ||
1558 | { | ||
1559 | allowed = ntohl (ack->sequence_max); | ||
1560 | |||
1561 | if (allowed > receiver->ss_rekey->sequence_allowed) | ||
1562 | { | ||
1563 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1564 | "%u > %u (%u %u) for rekey secrect %s\n", allowed, | ||
1565 | receiver->ss_rekey->sequence_allowed, | ||
1566 | receiver->acks_available, | ||
1567 | ack->acks_available, | ||
1568 | GNUNET_h2s (&receiver->ss_rekey->master)); | ||
1569 | |||
1570 | receiver->ss_rekey->sequence_allowed = allowed; | ||
1571 | |||
1572 | if (GNUNET_NO == receiver->rekeying) | ||
1573 | add_acks_rekey (receiver); | ||
1574 | |||
1575 | return GNUNET_NO; | ||
1576 | } | ||
1577 | } | ||
1340 | 1578 | ||
1341 | (void) pid; | 1579 | (void) pid; |
1342 | for (struct SharedSecret *ss = receiver->ss_head; NULL != ss; ss = ss->next) | 1580 | for (struct SharedSecret *ss = receiver->ss_head; NULL != ss; ss = ss->next) |
1343 | { | 1581 | { |
1344 | if (0 == memcmp (&ack->cmac, &ss->cmac, sizeof(struct GNUNET_HashCode))) | 1582 | if (0 == memcmp (&ack->cmac, &ss->cmac, sizeof(struct GNUNET_HashCode))) |
1345 | { | 1583 | { |
1346 | uint32_t allowed; | 1584 | |
1347 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1585 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1348 | "Found matching mac\n"); | 1586 | "Found matching mac\n"); |
1349 | 1587 | ||
@@ -1358,41 +1596,19 @@ handle_ack (void *cls, const struct GNUNET_PeerIdentity *pid, void *value) | |||
1358 | ack->acks_available, | 1596 | ack->acks_available, |
1359 | GNUNET_h2s (&ss->master)); | 1597 | GNUNET_h2s (&ss->master)); |
1360 | // Uncomment this for alternativ 1 of backchannel functionality | 1598 | // Uncomment this for alternativ 1 of backchannel functionality |
1599 | acks_to_add = (allowed - ss->sequence_allowed); | ||
1600 | if ((GNUNET_NO == receiver->rekeying) && | ||
1601 | (receiver->number_rekeying_kce < | ||
1602 | 3) ) | ||
1603 | { | ||
1604 | acks_to_add -= (3 - receiver->number_rekeying_kce); | ||
1605 | receiver->number_rekeying_kce = 3; | ||
1606 | } | ||
1361 | receiver->acks_available += (allowed - ss->sequence_allowed); | 1607 | receiver->acks_available += (allowed - ss->sequence_allowed); |
1362 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1363 | "Tell transport we have more acks!\n"); | ||
1364 | GNUNET_TRANSPORT_communicator_mq_update (ch, | ||
1365 | receiver->d_qh, | ||
1366 | (allowed | ||
1367 | - ss->sequence_allowed), | ||
1368 | 1); | ||
1369 | // Until here for alternativ 1 | ||
1370 | ss->sequence_allowed = allowed; | 1608 | ss->sequence_allowed = allowed; |
1371 | /* move ss to head to avoid discarding it anytime soon! */ | ||
1372 | GNUNET_CONTAINER_DLL_remove (receiver->ss_head, receiver->ss_tail, ss); | ||
1373 | GNUNET_CONTAINER_DLL_insert (receiver->ss_head, receiver->ss_tail, ss); | ||
1374 | pos = receiver->ss_head; | ||
1375 | while ( NULL != pos) | ||
1376 | { | ||
1377 | ss_to_destroy = pos; | ||
1378 | pos = pos->next; | ||
1379 | 1609 | ||
1380 | secret_destroy (ss_to_destroy, GNUNET_YES); | 1610 | add_acks (ss, acks_to_add, GNUNET_YES); |
1381 | } | ||
1382 | } | 1611 | } |
1383 | |||
1384 | // Uncomment this for alternativ 2 of backchannel functionality | ||
1385 | /*if (receiver->acks_available != ack->acks_available) | ||
1386 | { | ||
1387 | receiver->acks_available = ack->acks_available; | ||
1388 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1389 | "Tell transport we have different number of acks!\n"); | ||
1390 | GNUNET_TRANSPORT_communicator_mq_update (ch, | ||
1391 | receiver->d_qh, | ||
1392 | receiver->acks_available, | ||
1393 | 1); | ||
1394 | }*/ | ||
1395 | // Until here for alternativ 2 | ||
1396 | return GNUNET_NO; | 1612 | return GNUNET_NO; |
1397 | } | 1613 | } |
1398 | } | 1614 | } |
@@ -1446,12 +1662,13 @@ try_handle_plaintext (struct SenderAddress *sender, | |||
1446 | } | 1662 | } |
1447 | } | 1663 | } |
1448 | 1664 | ||
1665 | |||
1666 | |||
1449 | static void | 1667 | static void |
1450 | kce_generate_cb (void *cls) | 1668 | kce_generate_cb (void *cls) |
1451 | { | 1669 | { |
1452 | struct SharedSecret *ss = cls; | 1670 | struct SharedSecret *ss = cls; |
1453 | 1671 | kce_task = NULL; | |
1454 | |||
1455 | 1672 | ||
1456 | if (ss->sender->acks_available < KCN_TARGET) | 1673 | if (ss->sender->acks_available < KCN_TARGET) |
1457 | { | 1674 | { |
@@ -1477,6 +1694,31 @@ kce_generate_cb (void *cls) | |||
1477 | 1694 | ||
1478 | } | 1695 | } |
1479 | 1696 | ||
1697 | static void | ||
1698 | kce_generate_rekey_cb (void *cls) | ||
1699 | { | ||
1700 | struct SharedSecret *ss = cls; | ||
1701 | kce_task_rekey = NULL; | ||
1702 | |||
1703 | if (NULL == kce_task) | ||
1704 | { | ||
1705 | for (int i = 0; i < GENERATE_AT_ONCE; i++) | ||
1706 | kce_generate (ss, ++ss->sequence_allowed); | ||
1707 | |||
1708 | kce_task = GNUNET_SCHEDULER_add_delayed (WORKING_QUEUE_INTERVALL, | ||
1709 | kce_generate_cb, | ||
1710 | ss); | ||
1711 | kce_task_rekey = NULL; | ||
1712 | } | ||
1713 | else | ||
1714 | { | ||
1715 | kce_task_rekey = GNUNET_SCHEDULER_add_delayed (WORKING_QUEUE_INTERVALL, | ||
1716 | kce_generate_rekey_cb, | ||
1717 | ss); | ||
1718 | } | ||
1719 | } | ||
1720 | |||
1721 | |||
1480 | /** | 1722 | /** |
1481 | * We established a shared secret with a sender. We should try to send | 1723 | * We established a shared secret with a sender. We should try to send |
1482 | * the sender an `struct UDPAck` at the next opportunity to allow the | 1724 | * the sender an `struct UDPAck` at the next opportunity to allow the |
@@ -1489,9 +1731,6 @@ kce_generate_cb (void *cls) | |||
1489 | static void | 1731 | static void |
1490 | consider_ss_ack (struct SharedSecret *ss, int initial) | 1732 | consider_ss_ack (struct SharedSecret *ss, int initial) |
1491 | { | 1733 | { |
1492 | struct SharedSecret *ss_to_destroy; | ||
1493 | struct SharedSecret *pos; | ||
1494 | |||
1495 | GNUNET_assert (NULL != ss->sender); | 1734 | GNUNET_assert (NULL != ss->sender); |
1496 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1735 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1497 | "Considering SS UDPAck %s\n", | 1736 | "Considering SS UDPAck %s\n", |
@@ -1535,18 +1774,13 @@ consider_ss_ack (struct SharedSecret *ss, int initial) | |||
1535 | &ss_finished->sender->target, | 1774 | &ss_finished->sender->target, |
1536 | COMMUNICATOR_ADDRESS_PREFIX, | 1775 | COMMUNICATOR_ADDRESS_PREFIX, |
1537 | &ack.header); | 1776 | &ack.header); |
1538 | pos = ss->sender->ss_head; | 1777 | destroy_all_secrets (ss, GNUNET_YES); |
1539 | while ( NULL != pos) | ||
1540 | { | ||
1541 | ss_to_destroy = pos; | ||
1542 | pos = pos->next; | ||
1543 | secret_destroy (ss_to_destroy, GNUNET_YES); | ||
1544 | } | ||
1545 | kce_task = NULL; | 1778 | kce_task = NULL; |
1779 | kce_task_finished = GNUNET_NO; | ||
1546 | } | 1780 | } |
1547 | else if (((NULL == kce_task) && (KCN_THRESHOLD > | 1781 | else if ((NULL == kce_task) && ((KCN_THRESHOLD > ss->sender->acks_available)|| |
1548 | ss->sender->acks_available)) || | 1782 | (GNUNET_YES == ss->sender->rekeying) || |
1549 | (ss->sender->num_secrets > MAX_SECRETS) ) | 1783 | (ss->sender->num_secrets > MAX_SECRETS) )) |
1550 | { | 1784 | { |
1551 | 1785 | ||
1552 | // kce_generate (ss, ++ss->sequence_allowed); | 1786 | // kce_generate (ss, ++ss->sequence_allowed); |
@@ -1556,39 +1790,13 @@ consider_ss_ack (struct SharedSecret *ss, int initial) | |||
1556 | ss); | 1790 | ss); |
1557 | 1791 | ||
1558 | } | 1792 | } |
1559 | 1793 | else if ((NULL == kce_task_rekey) && (GNUNET_YES == | |
1560 | 1794 | ss->sender->rekeying) ) | |
1561 | /*if (ss->active_kce_count < KCN_THRESHOLD) | ||
1562 | { | 1795 | { |
1563 | struct UDPAck ack; | 1796 | kce_task_rekey = GNUNET_SCHEDULER_add_delayed (WORKING_QUEUE_INTERVALL, |
1564 | 1797 | kce_generate_rekey_cb, | |
1565 | /** | 1798 | ss); |
1566 | * If we previously have seen this ss | 1799 | } |
1567 | * we now generate KCN_TARGET KCEs. | ||
1568 | * For the initial KX (active_kce_count==0), | ||
1569 | * we only generate a single KCE to prevent | ||
1570 | * unnecessary overhead. | ||
1571 | |||
1572 | if (0 < ss->active_kce_count) | ||
1573 | { | ||
1574 | while (ss->active_kce_count < KCN_TARGET) | ||
1575 | kce_generate (ss, ++ss->sequence_allowed); | ||
1576 | } | ||
1577 | else { | ||
1578 | kce_generate (ss, ++ss->sequence_allowed); | ||
1579 | } | ||
1580 | ack.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK); | ||
1581 | ack.header.size = htons (sizeof(ack)); | ||
1582 | ack.sequence_max = htonl (ss->sequence_allowed); | ||
1583 | ack.cmac = ss->cmac; | ||
1584 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1585 | "Notifying transport of UDPAck %s\n", | ||
1586 | GNUNET_i2s_full (&ss->sender->target)); | ||
1587 | GNUNET_TRANSPORT_communicator_notify (ch, | ||
1588 | &ss->sender->target, | ||
1589 | COMMUNICATOR_ADDRESS_PREFIX, | ||
1590 | &ack.header); | ||
1591 | }*/ | ||
1592 | } | 1800 | } |
1593 | 1801 | ||
1594 | 1802 | ||
@@ -1631,9 +1839,107 @@ decrypt_box (const struct UDPBox *box, | |||
1631 | "decrypted UDPBox with kid %s\n", | 1839 | "decrypted UDPBox with kid %s\n", |
1632 | GNUNET_sh2s (&box->kid)); | 1840 | GNUNET_sh2s (&box->kid)); |
1633 | try_handle_plaintext (ss->sender, out_buf, sizeof(out_buf)); | 1841 | try_handle_plaintext (ss->sender, out_buf, sizeof(out_buf)); |
1634 | consider_ss_ack (ss, GNUNET_NO); | 1842 | if ((GNUNET_NO == box->rekeying) && (GNUNET_YES == ss->sender->rekeying)) |
1843 | { | ||
1844 | ss->sender->rekeying = GNUNET_NO; | ||
1845 | // destroy_all_secrets (ss, GNUNET_NO); | ||
1846 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1847 | "Receiver stopped rekeying.\n"); | ||
1848 | } | ||
1849 | else if (GNUNET_NO == box->rekeying) | ||
1850 | consider_ss_ack (ss, GNUNET_YES); | ||
1851 | else{ | ||
1852 | ss->sender->rekeying = GNUNET_YES; | ||
1853 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1854 | "Receiver started rekeying.\n"); | ||
1855 | } | ||
1635 | } | 1856 | } |
1636 | 1857 | ||
1858 | /** | ||
1859 | * We received a @a rekey with matching @a kce. Decrypt and process it. | ||
1860 | * | ||
1861 | * @param rekey the data we received | ||
1862 | * @param rekey_len number of bytes in @a rekey | ||
1863 | * @param kce key index to decrypt @a rekey | ||
1864 | */ | ||
1865 | static void | ||
1866 | decrypt_rekey (const struct UDPRekey *rekey, | ||
1867 | size_t rekey_len, | ||
1868 | struct KeyCacheEntry *kce, | ||
1869 | struct SenderAddress *sender) | ||
1870 | { | ||
1871 | struct SharedSecret *ss = kce->ss; | ||
1872 | struct SharedSecret *ss_rekey; | ||
1873 | char out_buf[rekey_len - sizeof(*rekey)]; | ||
1874 | struct GNUNET_HashCode *master; | ||
1875 | |||
1876 | |||
1877 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1878 | "decrypt_rekey.\n"); | ||
1879 | |||
1880 | GNUNET_assert (NULL != ss->sender); | ||
1881 | if (GNUNET_OK != try_decrypt (ss, | ||
1882 | rekey->gcm_tag, | ||
1883 | kce->sequence_number, | ||
1884 | (const char *) &rekey[1], | ||
1885 | sizeof(out_buf), | ||
1886 | out_buf)) | ||
1887 | { | ||
1888 | GNUNET_STATISTICS_update (stats, | ||
1889 | "# Decryption failures with valid KCE", | ||
1890 | 1, | ||
1891 | GNUNET_NO); | ||
1892 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1893 | "Decryption with kid %s failed\n", | ||
1894 | GNUNET_sh2s (&rekey->kid)); | ||
1895 | kce_destroy (kce); | ||
1896 | return; | ||
1897 | } | ||
1898 | kce_destroy (kce); | ||
1899 | GNUNET_STATISTICS_update (stats, | ||
1900 | "# bytes decrypted with Rekey", | ||
1901 | sizeof(out_buf), | ||
1902 | GNUNET_NO); | ||
1903 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1904 | "decrypted UDPRekey with kid %s\n", | ||
1905 | GNUNET_sh2s (&rekey->kid)); | ||
1906 | /*cmac = (struct GNUNET_HashCode *) out_buf; | ||
1907 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1908 | "Received secret with cmac %s \n", | ||
1909 | GNUNET_h2s (&cmac));*/ | ||
1910 | // ss_rekey = (struct SharedSecret *) out_buf; | ||
1911 | master = (struct GNUNET_HashCode *) out_buf; | ||
1912 | ss_rekey = GNUNET_new (struct SharedSecret); | ||
1913 | ss_rekey->master = *master; | ||
1914 | calculate_cmac (ss_rekey); | ||
1915 | ss_rekey->sender = sender; | ||
1916 | // ss_rekey->sequence_used = 0; | ||
1917 | // ss_rekey->sequence_allowed = 0; | ||
1918 | /* ss_rekey->active_kce_count = 0; */ | ||
1919 | /* ss_rekey->prev = NULL; */ | ||
1920 | /* ss_rekey->next = NULL; */ | ||
1921 | /* GNUNET_assert (ss_rekey->prev == NULL && sender->ss_head != ss_rekey); */ | ||
1922 | /* GNUNET_assert (ss_rekey->next == NULL && sender->ss_tail != ss_rekey); */ | ||
1923 | GNUNET_CONTAINER_DLL_insert (sender->ss_head, sender->ss_tail, ss_rekey); | ||
1924 | sender->num_secrets++; | ||
1925 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1926 | "Received secret with cmac %s\n", | ||
1927 | GNUNET_h2s (&(ss_rekey->cmac))); | ||
1928 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1929 | "Received secret with master %s.\n", | ||
1930 | GNUNET_h2s (&(ss_rekey->master))); | ||
1931 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1932 | "We have %u sequence_allowed.\n", | ||
1933 | ss_rekey->sequence_allowed); | ||
1934 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1935 | "We have a sender %p\n", | ||
1936 | ss_rekey->sender); | ||
1937 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1938 | "We have %u acks available.\n", | ||
1939 | ss_rekey->sender->acks_available); | ||
1940 | consider_ss_ack (ss_rekey, GNUNET_YES); | ||
1941 | |||
1942 | } | ||
1637 | 1943 | ||
1638 | /** | 1944 | /** |
1639 | * Closure for #find_sender_by_address() | 1945 | * Closure for #find_sender_by_address() |
@@ -1832,6 +2138,38 @@ sock_read (void *cls) | |||
1832 | } | 2138 | } |
1833 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 2139 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1834 | "Read %lu bytes\n", rcvd); | 2140 | "Read %lu bytes\n", rcvd); |
2141 | |||
2142 | if (rcvd > sizeof(struct UDPRekey)) | ||
2143 | { | ||
2144 | const struct UDPRekey *rekey; | ||
2145 | const struct UDPBox *box; | ||
2146 | struct KeyCacheEntry *kce; | ||
2147 | struct SenderAddress *sender; | ||
2148 | int do_decrypt = GNUNET_NO; | ||
2149 | |||
2150 | rekey = (const struct UDPRekey *) buf; | ||
2151 | box = (const struct UDPBox *) buf; | ||
2152 | kce = GNUNET_CONTAINER_multishortmap_get (key_cache, &rekey->kid); | ||
2153 | |||
2154 | if ((GNUNET_YES == box->rekeying)||(GNUNET_NO == box->rekeying)) | ||
2155 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2156 | "UDPRekey has rekeying %u\n", | ||
2157 | box->rekeying); | ||
2158 | else | ||
2159 | do_decrypt = GNUNET_YES; | ||
2160 | if ((GNUNET_YES == do_decrypt)&& (NULL != kce) && (GNUNET_YES == | ||
2161 | kce->ss->sender->rekeying)) | ||
2162 | { | ||
2163 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2164 | "UDPRekey with kid %s\n", | ||
2165 | GNUNET_sh2s (&rekey->kid)); | ||
2166 | sender = setup_sender (&rekey->sender, (const struct sockaddr *) &sa, | ||
2167 | salen); | ||
2168 | decrypt_rekey (rekey, (size_t) rcvd, kce, sender); | ||
2169 | return; | ||
2170 | } | ||
2171 | } | ||
2172 | |||
1835 | /* first, see if it is a UDPBox */ | 2173 | /* first, see if it is a UDPBox */ |
1836 | if (rcvd > sizeof(struct UDPBox)) | 2174 | if (rcvd > sizeof(struct UDPBox)) |
1837 | { | 2175 | { |
@@ -1949,7 +2287,20 @@ sock_read (void *cls) | |||
1949 | 1, | 2287 | 1, |
1950 | GNUNET_NO); | 2288 | GNUNET_NO); |
1951 | try_handle_plaintext (sender, &uc[1], sizeof(pbuf) - sizeof(*uc)); | 2289 | try_handle_plaintext (sender, &uc[1], sizeof(pbuf) - sizeof(*uc)); |
1952 | consider_ss_ack (ss, GNUNET_YES); | 2290 | if ((GNUNET_NO == kx->rekeying) && (GNUNET_YES == ss->sender->rekeying)) |
2291 | { | ||
2292 | ss->sender->rekeying = GNUNET_NO; | ||
2293 | // destroy_all_secrets (ss, GNUNET_NO); | ||
2294 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2295 | "Receiver stopped rekeying.\n"); | ||
2296 | } | ||
2297 | else if (GNUNET_NO == kx->rekeying) | ||
2298 | consider_ss_ack (ss, GNUNET_YES); | ||
2299 | else{ | ||
2300 | ss->sender->rekeying = GNUNET_YES; | ||
2301 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2302 | "Receiver started rekeying.\n"); | ||
2303 | } | ||
1953 | /*if (sender->num_secrets > MAX_SECRETS) | 2304 | /*if (sender->num_secrets > MAX_SECRETS) |
1954 | secret_destroy (sender->ss_tail);*/ | 2305 | secret_destroy (sender->ss_tail);*/ |
1955 | } | 2306 | } |
@@ -2138,26 +2489,13 @@ mq_send_kx (struct GNUNET_MQ_Handle *mq, | |||
2138 | size_t dpos; | 2489 | size_t dpos; |
2139 | gcry_cipher_hd_t out_cipher; | 2490 | gcry_cipher_hd_t out_cipher; |
2140 | struct SharedSecret *ss; | 2491 | struct SharedSecret *ss; |
2141 | struct SharedSecret *ss_to_destroy; | ||
2142 | struct SharedSecret *pos; | ||
2143 | |||
2144 | if (receiver->num_secrets > MAX_SECRETS) | ||
2145 | { | ||
2146 | pos = receiver->ss_head; | ||
2147 | while ( NULL != pos) | ||
2148 | { | ||
2149 | ss_to_destroy = pos; | ||
2150 | pos = pos->next; | ||
2151 | secret_destroy (ss_to_destroy, GNUNET_YES); | ||
2152 | } | ||
2153 | } | ||
2154 | |||
2155 | 2492 | ||
2156 | GNUNET_assert (mq == receiver->kx_mq); | 2493 | GNUNET_assert (mq == receiver->kx_mq); |
2157 | if (msize > receiver->kx_mtu) | 2494 | if (msize > receiver->kx_mtu) |
2158 | { | 2495 | { |
2159 | GNUNET_break (0); | 2496 | GNUNET_break (0); |
2160 | receiver_destroy (receiver); | 2497 | if (GNUNET_YES != receiver->receiver_destroy_called) |
2498 | receiver_destroy (receiver); | ||
2161 | return; | 2499 | return; |
2162 | } | 2500 | } |
2163 | reschedule_receiver_timeout (receiver); | 2501 | reschedule_receiver_timeout (receiver); |
@@ -2165,7 +2503,13 @@ mq_send_kx (struct GNUNET_MQ_Handle *mq, | |||
2165 | /* setup key material */ | 2503 | /* setup key material */ |
2166 | GNUNET_CRYPTO_ecdhe_key_create (&epriv); | 2504 | GNUNET_CRYPTO_ecdhe_key_create (&epriv); |
2167 | 2505 | ||
2168 | ss = setup_shared_secret_enc (&epriv, receiver); | 2506 | ss = setup_shared_secret_enc (&epriv, receiver, GNUNET_YES); |
2507 | |||
2508 | if (receiver->num_secrets > MAX_SECRETS) | ||
2509 | { | ||
2510 | destroy_all_secrets (ss, GNUNET_YES); | ||
2511 | } | ||
2512 | |||
2169 | setup_cipher (&ss->master, 0, &out_cipher); | 2513 | setup_cipher (&ss->master, 0, &out_cipher); |
2170 | /* compute 'uc' */ | 2514 | /* compute 'uc' */ |
2171 | uc.sender = my_identity; | 2515 | uc.sender = my_identity; |
@@ -2199,6 +2543,10 @@ mq_send_kx (struct GNUNET_MQ_Handle *mq, | |||
2199 | GNUNET_assert ( | 2543 | GNUNET_assert ( |
2200 | 0 == gcry_cipher_gettag (out_cipher, kx.gcm_tag, sizeof(kx.gcm_tag))); | 2544 | 0 == gcry_cipher_gettag (out_cipher, kx.gcm_tag, sizeof(kx.gcm_tag))); |
2201 | gcry_cipher_close (out_cipher); | 2545 | gcry_cipher_close (out_cipher); |
2546 | if (GNUNET_NO == receiver->rekeying) | ||
2547 | kx.rekeying = GNUNET_NO; | ||
2548 | else | ||
2549 | kx.rekeying = GNUNET_YES; | ||
2202 | memcpy (dgram, &kx, sizeof(kx)); | 2550 | memcpy (dgram, &kx, sizeof(kx)); |
2203 | if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock, | 2551 | if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock, |
2204 | dgram, | 2552 | dgram, |
@@ -2212,6 +2560,162 @@ mq_send_kx (struct GNUNET_MQ_Handle *mq, | |||
2212 | GNUNET_MQ_impl_send_continue (mq); | 2560 | GNUNET_MQ_impl_send_continue (mq); |
2213 | } | 2561 | } |
2214 | 2562 | ||
2563 | static void | ||
2564 | check_for_rekeying (struct ReceiverAddress *receiver, struct UDPBox *box) | ||
2565 | { | ||
2566 | |||
2567 | struct GNUNET_TIME_Relative rt; | ||
2568 | |||
2569 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2570 | "Timeout is %lu\n.", | ||
2571 | receiver->rekey_timeout.abs_value_us); | ||
2572 | |||
2573 | if (0 == receiver->rekey_timeout.abs_value_us) | ||
2574 | { | ||
2575 | receiver->rekey_timeout = GNUNET_TIME_relative_to_absolute ( | ||
2576 | rekey_interval); | ||
2577 | } | ||
2578 | else | ||
2579 | { | ||
2580 | rt = GNUNET_TIME_absolute_get_remaining (receiver->rekey_timeout); | ||
2581 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2582 | "Relative time is %lu and timeout is %lu\n.", | ||
2583 | rt.rel_value_us, | ||
2584 | receiver->rekey_timeout.abs_value_us); | ||
2585 | |||
2586 | if ((0 == rt.rel_value_us)||(receiver->rekey_send_bytes > | ||
2587 | REKEY_MAX_BYTES) ) | ||
2588 | { | ||
2589 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2590 | "Bytes send %lu greater than %llu max bytes\n.", | ||
2591 | receiver->rekey_send_bytes, | ||
2592 | REKEY_MAX_BYTES); | ||
2593 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2594 | "Relative time is %lu and timeout is %lu\n.", | ||
2595 | rt.rel_value_us, | ||
2596 | receiver->rekey_timeout.abs_value_us); | ||
2597 | |||
2598 | receiver->rekey_timeout.abs_value_us = 0; | ||
2599 | receiver->rekey_send_bytes = 0; | ||
2600 | receiver->ss_rekey = NULL; | ||
2601 | // destroy_all_secrets (ss, GNUNET_NO); | ||
2602 | receiver->rekeying = GNUNET_YES; | ||
2603 | receiver->rekey_acks_available = receiver->acks_available; | ||
2604 | box->rekeying = GNUNET_YES; | ||
2605 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2606 | "Sender started rekeying.\n"); | ||
2607 | if (GNUNET_YES == box->rekeying) | ||
2608 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2609 | "Sending rekeying with kid %s\n", | ||
2610 | GNUNET_sh2s (&box->kid)); | ||
2611 | } | ||
2612 | } | ||
2613 | } | ||
2614 | |||
2615 | static void | ||
2616 | send_UDPRekey (struct ReceiverAddress *receiver, struct SharedSecret *ss) | ||
2617 | { | ||
2618 | uint8_t is_ss_rekey_sequence_allowed_zero = GNUNET_NO; | ||
2619 | uint8_t is_acks_available_below = GNUNET_NO; | ||
2620 | uint8_t send_rekey = GNUNET_NO; | ||
2621 | uint16_t not_below; | ||
2622 | struct GNUNET_CRYPTO_EcdhePrivateKey epriv; | ||
2623 | struct UDPRekey *rekey; | ||
2624 | size_t dpos; | ||
2625 | |||
2626 | char rekey_dgram[sizeof(struct UDPRekey) + receiver->d_mtu]; | ||
2627 | |||
2628 | if (NULL != receiver->ss_rekey) | ||
2629 | { | ||
2630 | not_below = (receiver->rekey_acks_available | ||
2631 | - (receiver->rekey_acks_available % 3)) / 3; | ||
2632 | is_ss_rekey_sequence_allowed_zero = (0 == | ||
2633 | receiver->ss_rekey->sequence_allowed); | ||
2634 | is_acks_available_below = (receiver->acks_available > not_below); | ||
2635 | send_rekey = (0 == (receiver->acks_available - not_below) % not_below) && | ||
2636 | is_acks_available_below && is_ss_rekey_sequence_allowed_zero; | ||
2637 | } | ||
2638 | else if (NULL == receiver->ss_rekey) | ||
2639 | { | ||
2640 | /* setup key material */ | ||
2641 | GNUNET_CRYPTO_ecdhe_key_create (&epriv); | ||
2642 | receiver->ss_rekey = setup_shared_secret_enc (&epriv, receiver, | ||
2643 | GNUNET_NO); | ||
2644 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2645 | "Setup secret with cmac %s\n", | ||
2646 | GNUNET_h2s (&(receiver->ss_rekey->cmac))); | ||
2647 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2648 | "Setup secret with master %s.\n", | ||
2649 | GNUNET_h2s (&(receiver->ss_rekey->master))); | ||
2650 | } | ||
2651 | |||
2652 | if (send_rekey) | ||
2653 | { | ||
2654 | GNUNET_assert (0 != receiver->number_rekeying_kce); | ||
2655 | gcry_cipher_hd_t rekey_out_cipher; | ||
2656 | |||
2657 | while (NULL != ss && ss->sequence_used >= ss->sequence_allowed) | ||
2658 | { | ||
2659 | ss = ss->prev; | ||
2660 | } | ||
2661 | |||
2662 | if (NULL != ss) | ||
2663 | { | ||
2664 | rekey = (struct UDPRekey *) rekey_dgram; | ||
2665 | rekey->sender = my_identity; | ||
2666 | ss->sequence_used++; | ||
2667 | get_kid (&ss->master, ss->sequence_used, &rekey->kid); | ||
2668 | receiver->number_rekeying_kce--; | ||
2669 | setup_cipher (&ss->master, ss->sequence_used, &rekey_out_cipher); | ||
2670 | /* Append encrypted payload to dgram */ | ||
2671 | dpos = sizeof(struct UDPRekey); | ||
2672 | |||
2673 | GNUNET_assert ( | ||
2674 | 0 == gcry_cipher_encrypt (rekey_out_cipher, &rekey_dgram[dpos], | ||
2675 | sizeof(receiver->ss_rekey->master), | ||
2676 | &(receiver->ss_rekey->master), | ||
2677 | sizeof(receiver->ss_rekey->master))); | ||
2678 | dpos += sizeof(receiver->ss_rekey->master); | ||
2679 | /* GNUNET_assert ( */ | ||
2680 | /* 0 == gcry_cipher_encrypt (rekey_out_cipher, &rekey_dgram[dpos], */ | ||
2681 | /* /\*sizeof(receiver->ss_rekey->cmac), */ | ||
2682 | /* &(receiver->ss_rekey->cmac), */ | ||
2683 | /* sizeof(receiver->ss_rekey->cmac))); */ | ||
2684 | /* dpos += sizeof(receiver->ss_rekey->cmac);*\/ */ | ||
2685 | /* sizeof(receiver->ss_rekey), */ | ||
2686 | /* receiver->ss_rekey, */ | ||
2687 | /* sizeof(receiver->ss_rekey))); */ | ||
2688 | /* dpos += sizeof(receiver->ss_rekey); */ | ||
2689 | do_pad (rekey_out_cipher, &rekey_dgram[dpos], sizeof(rekey_dgram) | ||
2690 | - dpos); | ||
2691 | GNUNET_assert (0 == gcry_cipher_gettag (rekey_out_cipher, | ||
2692 | rekey->gcm_tag, | ||
2693 | sizeof(rekey->gcm_tag))); | ||
2694 | gcry_cipher_close (rekey_out_cipher); | ||
2695 | |||
2696 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2697 | "Sending rekey with kid %s and master %s\n", | ||
2698 | GNUNET_sh2s (&rekey->kid), | ||
2699 | GNUNET_h2s (&(receiver->ss_rekey->master))); | ||
2700 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2701 | "Sending rekey with cmac %s\n", | ||
2702 | GNUNET_h2s (&(receiver->ss_rekey->cmac))); | ||
2703 | |||
2704 | if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock, | ||
2705 | rekey_dgram, | ||
2706 | sizeof(rekey_dgram), | ||
2707 | receiver->address, | ||
2708 | receiver->address_len)) | ||
2709 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send"); | ||
2710 | |||
2711 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2712 | "Sending UDPRekey to %s\n", GNUNET_a2s (receiver->address, | ||
2713 | receiver-> | ||
2714 | address_len)); | ||
2715 | } | ||
2716 | } | ||
2717 | } | ||
2718 | |||
2215 | 2719 | ||
2216 | /** | 2720 | /** |
2217 | * Signature of functions implementing the sending functionality of a | 2721 | * Signature of functions implementing the sending functionality of a |
@@ -2228,15 +2732,14 @@ mq_send_d (struct GNUNET_MQ_Handle *mq, | |||
2228 | { | 2732 | { |
2229 | struct ReceiverAddress *receiver = impl_state; | 2733 | struct ReceiverAddress *receiver = impl_state; |
2230 | uint16_t msize = ntohs (msg->size); | 2734 | uint16_t msize = ntohs (msg->size); |
2231 | struct GNUNET_TIME_Relative rt; | ||
2232 | struct SharedSecret *pos; | ||
2233 | 2735 | ||
2234 | GNUNET_assert (mq == receiver->d_mq); | 2736 | GNUNET_assert (mq == receiver->d_mq); |
2235 | if ((msize > receiver->d_mtu) || | 2737 | if ((msize > receiver->d_mtu) || |
2236 | (0 == receiver->acks_available)) | 2738 | (0 == receiver->acks_available)) |
2237 | { | 2739 | { |
2238 | GNUNET_break (0); | 2740 | GNUNET_break (0); |
2239 | receiver_destroy (receiver); | 2741 | if (GNUNET_YES != receiver->receiver_destroy_called) |
2742 | receiver_destroy (receiver); | ||
2240 | return; | 2743 | return; |
2241 | } | 2744 | } |
2242 | reschedule_receiver_timeout (receiver); | 2745 | reschedule_receiver_timeout (receiver); |
@@ -2278,6 +2781,16 @@ mq_send_d (struct GNUNET_MQ_Handle *mq, | |||
2278 | box->gcm_tag, | 2781 | box->gcm_tag, |
2279 | sizeof(box->gcm_tag))); | 2782 | sizeof(box->gcm_tag))); |
2280 | gcry_cipher_close (out_cipher); | 2783 | gcry_cipher_close (out_cipher); |
2784 | |||
2785 | receiver->rekey_send_bytes += sizeof(struct UDPBox) + receiver->d_mtu; | ||
2786 | |||
2787 | if (GNUNET_NO == receiver->rekeying) | ||
2788 | box->rekeying = GNUNET_NO; | ||
2789 | else | ||
2790 | box->rekeying = GNUNET_YES; | ||
2791 | |||
2792 | check_for_rekeying (receiver, box); | ||
2793 | |||
2281 | if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock, | 2794 | if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock, |
2282 | dgram, | 2795 | dgram, |
2283 | sizeof(dgram), | 2796 | sizeof(dgram), |
@@ -2288,38 +2801,26 @@ mq_send_d (struct GNUNET_MQ_Handle *mq, | |||
2288 | "Sending UDPBox to %s\n", GNUNET_a2s (receiver->address, | 2801 | "Sending UDPBox to %s\n", GNUNET_a2s (receiver->address, |
2289 | receiver->address_len)); | 2802 | receiver->address_len)); |
2290 | GNUNET_MQ_impl_send_continue (mq); | 2803 | GNUNET_MQ_impl_send_continue (mq); |
2291 | // receiver->acks_available--; | 2804 | receiver->acks_available--; |
2292 | if (0 == receiver->acks_available) | 2805 | if (0 == receiver->acks_available) |
2293 | { | 2806 | { |
2294 | /* We have no more ACKs */ | 2807 | /* We have no more ACKs */ |
2295 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 2808 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
2296 | "No more acks\n"); | 2809 | "No more acks\n"); |
2297 | } | 2810 | if (GNUNET_YES == receiver->rekeying) |
2298 | |||
2299 | /* (NULL == rekey_timeout) | ||
2300 | rekey_timeout = GNUNET_TIME_relative_to_absolute (REKEY_TIME_INTERVAL); | ||
2301 | else | ||
2302 | { | ||
2303 | rt = GNUNET_TIME_absolute_get_remaining (rekey_timeout); | ||
2304 | if (0 == rt.rel_value_us) | ||
2305 | { | 2811 | { |
2306 | rekey_timeout = NULL; | 2812 | receiver->rekeying = GNUNET_NO; |
2307 | pos = receiver->ss_head; | ||
2308 | while ( NULL != pos) | ||
2309 | { | ||
2310 | ss_to_destroy = pos; | ||
2311 | pos = pos->next; | ||
2312 | secret_destroy (ss_to_destroy, GNUNET_NO); | ||
2313 | } | ||
2314 | if (0 != receiver->acks_available) | ||
2315 | GNUNET_TRANSPORT_communicator_mq_update (ch, | ||
2316 | receiver->d_qh, | ||
2317 | // TODO We can not do this. But how can we signal this queue is not able to handle a message. Test code interprets q-len as additional length. | ||
2318 | -receiver->acks_available, | ||
2319 | 1); | ||
2320 | } | ||
2321 | }*/ | ||
2322 | 2813 | ||
2814 | if ((NULL != receiver->ss_rekey) && (0 < | ||
2815 | receiver->ss_rekey-> | ||
2816 | sequence_allowed) ) | ||
2817 | add_acks_rekey (receiver); | ||
2818 | } | ||
2819 | } | ||
2820 | else if ((GNUNET_YES == receiver->rekeying) ) | ||
2821 | { | ||
2822 | send_UDPRekey (receiver, ss); | ||
2823 | } | ||
2323 | 2824 | ||
2324 | return; | 2825 | return; |
2325 | } | 2826 | } |
@@ -2343,7 +2844,8 @@ mq_destroy_d (struct GNUNET_MQ_Handle *mq, void *impl_state) | |||
2343 | if (mq == receiver->d_mq) | 2844 | if (mq == receiver->d_mq) |
2344 | { | 2845 | { |
2345 | receiver->d_mq = NULL; | 2846 | receiver->d_mq = NULL; |
2346 | receiver_destroy (receiver); | 2847 | if (GNUNET_YES != receiver->receiver_destroy_called) |
2848 | receiver_destroy (receiver); | ||
2347 | } | 2849 | } |
2348 | } | 2850 | } |
2349 | 2851 | ||
@@ -2365,7 +2867,8 @@ mq_destroy_kx (struct GNUNET_MQ_Handle *mq, void *impl_state) | |||
2365 | if (mq == receiver->kx_mq) | 2867 | if (mq == receiver->kx_mq) |
2366 | { | 2868 | { |
2367 | receiver->kx_mq = NULL; | 2869 | receiver->kx_mq = NULL; |
2368 | receiver_destroy (receiver); | 2870 | if (GNUNET_YES != receiver->receiver_destroy_called) |
2871 | receiver_destroy (receiver); | ||
2369 | } | 2872 | } |
2370 | } | 2873 | } |
2371 | 2874 | ||
@@ -2629,6 +3132,21 @@ do_shutdown (void *cls) | |||
2629 | GNUNET_SCHEDULER_cancel (broadcast_task); | 3132 | GNUNET_SCHEDULER_cancel (broadcast_task); |
2630 | broadcast_task = NULL; | 3133 | broadcast_task = NULL; |
2631 | } | 3134 | } |
3135 | if (NULL != kce_task_rekey) | ||
3136 | { | ||
3137 | GNUNET_SCHEDULER_cancel (kce_task_rekey); | ||
3138 | kce_task_rekey = NULL; | ||
3139 | } | ||
3140 | if (NULL != kce_task) | ||
3141 | { | ||
3142 | GNUNET_SCHEDULER_cancel (kce_task); | ||
3143 | kce_task = NULL; | ||
3144 | } | ||
3145 | if (NULL != timeout_task) | ||
3146 | { | ||
3147 | GNUNET_SCHEDULER_cancel (timeout_task); | ||
3148 | timeout_task = NULL; | ||
3149 | } | ||
2632 | if (NULL != read_task) | 3150 | if (NULL != read_task) |
2633 | { | 3151 | { |
2634 | GNUNET_SCHEDULER_cancel (read_task); | 3152 | GNUNET_SCHEDULER_cancel (read_task); |
@@ -2997,6 +3515,13 @@ run (void *cls, | |||
2997 | return; | 3515 | return; |
2998 | } | 3516 | } |
2999 | 3517 | ||
3518 | if (GNUNET_OK != | ||
3519 | GNUNET_CONFIGURATION_get_value_time (cfg, | ||
3520 | COMMUNICATOR_CONFIG_SECTION, | ||
3521 | "REKEY_INTERVAL", | ||
3522 | &rekey_interval)) | ||
3523 | rekey_interval = DEFAULT_REKEY_TIME_INTERVAL; | ||
3524 | |||
3000 | in = udp_address_to_sockaddr (bindto, &in_len); | 3525 | in = udp_address_to_sockaddr (bindto, &in_len); |
3001 | if (NULL == in) | 3526 | if (NULL == in) |
3002 | { | 3527 | { |
diff --git a/src/transport/test_communicator_basic.c b/src/transport/test_communicator_basic.c index 8251a5169..7ea04afdb 100644 --- a/src/transport/test_communicator_basic.c +++ b/src/transport/test_communicator_basic.c | |||
@@ -26,11 +26,12 @@ | |||
26 | */ | 26 | */ |
27 | #include "platform.h" | 27 | #include "platform.h" |
28 | #include "gnunet_util_lib.h" | 28 | #include "gnunet_util_lib.h" |
29 | #include "transport-testing2.h" | 29 | #include "transport-testing-communicator.h" |
30 | #include "gnunet_ats_transport_service.h" | 30 | #include "gnunet_ats_transport_service.h" |
31 | #include "gnunet_signatures.h" | 31 | #include "gnunet_signatures.h" |
32 | #include "gnunet_testing_lib.h" | 32 | #include "gnunet_testing_lib.h" |
33 | #include "transport.h" | 33 | #include "transport.h" |
34 | #include "gnunet_statistics_service.h" | ||
34 | 35 | ||
35 | #include <inttypes.h> | 36 | #include <inttypes.h> |
36 | 37 | ||
@@ -54,6 +55,8 @@ GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS]; | |||
54 | 55 | ||
55 | static struct GNUNET_CONFIGURATION_Handle *cfg_peers[NUM_PEERS]; | 56 | static struct GNUNET_CONFIGURATION_Handle *cfg_peers[NUM_PEERS]; |
56 | 57 | ||
58 | static struct GNUNET_STATISTICS_Handle *stats[NUM_PEERS]; | ||
59 | |||
57 | static char *cfg_peers_name[NUM_PEERS]; | 60 | static char *cfg_peers_name[NUM_PEERS]; |
58 | 61 | ||
59 | static int ret; | 62 | static int ret; |
@@ -68,6 +71,8 @@ static struct GNUNET_TIME_Absolute timeout; | |||
68 | 71 | ||
69 | static struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *my_tc; | 72 | static struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *my_tc; |
70 | 73 | ||
74 | static char *test_name; | ||
75 | |||
71 | #define SHORT_MESSAGE_SIZE 128 | 76 | #define SHORT_MESSAGE_SIZE 128 |
72 | 77 | ||
73 | #define LONG_MESSAGE_SIZE 32000 /* FIXME */ | 78 | #define LONG_MESSAGE_SIZE 32000 /* FIXME */ |
@@ -509,6 +514,16 @@ update_avg_latency (const char*payload) | |||
509 | 514 | ||
510 | } | 515 | } |
511 | 516 | ||
517 | static int | ||
518 | process_statistics (void *cls, | ||
519 | const char *subsystem, | ||
520 | const char *name, | ||
521 | uint64_t value, | ||
522 | int is_persistent) | ||
523 | { | ||
524 | return GNUNET_OK; | ||
525 | } | ||
526 | |||
512 | /** | 527 | /** |
513 | * @brief Handle an incoming message | 528 | * @brief Handle an incoming message |
514 | * | 529 | * |
@@ -645,6 +660,15 @@ incoming_message_cb (void *cls, | |||
645 | short_test (NULL); | 660 | short_test (NULL); |
646 | break; | 661 | break; |
647 | } | 662 | } |
663 | /* if (("rekey" == test_name) || ("backchannel" == test_name)) */ | ||
664 | /* { */ | ||
665 | /* GNUNET_STATISTICS_get (stats[1], */ | ||
666 | /* "C-UDP", */ | ||
667 | /* "# bytes decrypted with Rekey", */ | ||
668 | /* NULL, */ | ||
669 | /* &process_statistics, */ | ||
670 | /* NULL); */ | ||
671 | /* } */ | ||
648 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 672 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
649 | "Finished\n"); | 673 | "Finished\n"); |
650 | GNUNET_SCHEDULER_shutdown (); | 674 | GNUNET_SCHEDULER_shutdown (); |
@@ -695,6 +719,12 @@ run (void *cls) | |||
695 | &incoming_message_cb, | 719 | &incoming_message_cb, |
696 | &handle_backchannel_cb, | 720 | &handle_backchannel_cb, |
697 | cfg_peers_name[i]); /* cls */ | 721 | cfg_peers_name[i]); /* cls */ |
722 | |||
723 | /* if (("rekey" == test_name) || ("backchannel" == test_name)) */ | ||
724 | /* { */ | ||
725 | /* stats[i] = GNUNET_STATISTICS_create ("C-UDP", */ | ||
726 | /* cfg_peers[i]); */ | ||
727 | /* } */ | ||
698 | } | 728 | } |
699 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, | 729 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, |
700 | NULL); | 730 | NULL); |
@@ -708,7 +738,6 @@ main (int argc, | |||
708 | struct GNUNET_CRYPTO_EddsaPrivateKey *private_key; | 738 | struct GNUNET_CRYPTO_EddsaPrivateKey *private_key; |
709 | char *communicator_name; | 739 | char *communicator_name; |
710 | char *test_mode; | 740 | char *test_mode; |
711 | char *test_name; | ||
712 | char *cfg_peer; | 741 | char *cfg_peer; |
713 | 742 | ||
714 | phase = TP_INIT; | 743 | phase = TP_INIT; |
diff --git a/src/transport/test_communicator_udp_rekey_peer1.conf b/src/transport/test_communicator_udp_rekey_peer1.conf index e7161e488..8fede6f8c 100644 --- a/src/transport/test_communicator_udp_rekey_peer1.conf +++ b/src/transport/test_communicator_udp_rekey_peer1.conf | |||
@@ -36,4 +36,12 @@ DISABLE_V6 = YES | |||
36 | REKEY_INTERVAL = 100ms | 36 | REKEY_INTERVAL = 100ms |
37 | 37 | ||
38 | [communicator-udp] | 38 | [communicator-udp] |
39 | #PREFIX = xterm -geometry 100x85 -T peer1 -e gdb --args | ||
40 | #PREFIX = valgrind --leak-check=full --track-origins=yes | ||
39 | BINDTO = 60002 | 41 | BINDTO = 60002 |
42 | DISABLE_V6 = YES | ||
43 | MAX_QUEUE_LENGTH=5000 | ||
44 | REKEY_INTERVAL = 1s | ||
45 | |||
46 | [communicator-test] | ||
47 | BACKCHANNEL_ENABLED = YES | ||
diff --git a/src/transport/test_communicator_udp_rekey_peer2.conf b/src/transport/test_communicator_udp_rekey_peer2.conf index 8f175a405..f545ba07a 100644 --- a/src/transport/test_communicator_udp_rekey_peer2.conf +++ b/src/transport/test_communicator_udp_rekey_peer2.conf | |||
@@ -36,4 +36,12 @@ DISABLE_V6 = YES | |||
36 | REKEY_INTERVAL = 100ms | 36 | REKEY_INTERVAL = 100ms |
37 | 37 | ||
38 | [communicator-udp] | 38 | [communicator-udp] |
39 | #PREFIX = xterm -geometry 100x85 -T peer1 -e gdb --args | ||
40 | #PREFIX = valgrind --leak-check=full --track-origins=yes | ||
39 | BINDTO = 60003 | 41 | BINDTO = 60003 |
42 | DISABLE_V6 = YES | ||
43 | MAX_QUEUE_LENGTH=5000 | ||
44 | REKEY_INTERVAL = 1s | ||
45 | |||
46 | [communicator-test] | ||
47 | BACKCHANNEL_ENABLED = YES | ||
diff --git a/src/transport/transport-testing-communicator.c b/src/transport/transport-testing-communicator.c new file mode 100644 index 000000000..6d74b12e8 --- /dev/null +++ b/src/transport/transport-testing-communicator.c | |||
@@ -0,0 +1,1164 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 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 | |||
21 | /** | ||
22 | * @file transport/transport-testing-communicator.c | ||
23 | * @brief functions related to testing-tng | ||
24 | * @author Christian Grothoff | ||
25 | * @author Julius Bünger | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_protocols.h" | ||
30 | #include "gnunet_constants.h" | ||
31 | #include "transport-testing-communicator.h" | ||
32 | #include "gnunet_ats_transport_service.h" | ||
33 | #include "gnunet_hello_lib.h" | ||
34 | #include "gnunet_signatures.h" | ||
35 | #include "transport.h" | ||
36 | #include <inttypes.h> | ||
37 | |||
38 | #define LOG(kind, ...) GNUNET_log_from (kind, "transport-testing2", __VA_ARGS__) | ||
39 | |||
40 | struct MyClient | ||
41 | { | ||
42 | struct MyClient *prev; | ||
43 | struct MyClient *next; | ||
44 | /** | ||
45 | * @brief Handle to the client | ||
46 | */ | ||
47 | struct GNUNET_SERVICE_Client *client; | ||
48 | |||
49 | /** | ||
50 | * @brief Handle to the client | ||
51 | */ | ||
52 | struct GNUNET_MQ_Handle *c_mq; | ||
53 | |||
54 | /** | ||
55 | * The TCH | ||
56 | */ | ||
57 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc; | ||
58 | |||
59 | }; | ||
60 | |||
61 | /** | ||
62 | * @brief Queue of a communicator and some context | ||
63 | */ | ||
64 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue | ||
65 | { | ||
66 | /** | ||
67 | * @brief Handle to the TransportCommunicator | ||
68 | */ | ||
69 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h; | ||
70 | |||
71 | /** | ||
72 | * @brief Envelope to a message that requests the opening of the queue. | ||
73 | * | ||
74 | * If the client already requests queue(s), but the communicator is not yet | ||
75 | * connected, we cannot send the request to open the queue. Save it until the | ||
76 | * communicator becomes available and send it then. | ||
77 | */ | ||
78 | struct GNUNET_MQ_Envelope *open_queue_env; | ||
79 | |||
80 | /** | ||
81 | * @brief Peer ID of the peer on the other side of the queue | ||
82 | */ | ||
83 | struct GNUNET_PeerIdentity peer_id; | ||
84 | |||
85 | /** | ||
86 | * @brief Queue ID | ||
87 | */ | ||
88 | uint32_t qid; | ||
89 | |||
90 | /** | ||
91 | * @brief Current message id | ||
92 | */ | ||
93 | uint64_t mid; | ||
94 | |||
95 | /** | ||
96 | * An `enum GNUNET_NetworkType` in NBO. | ||
97 | */ | ||
98 | uint32_t nt; | ||
99 | |||
100 | /** | ||
101 | * Maximum transmission unit. UINT32_MAX for unlimited. | ||
102 | */ | ||
103 | uint32_t mtu; | ||
104 | |||
105 | /** | ||
106 | * Queue length. UINT64_MAX for unlimited. | ||
107 | */ | ||
108 | uint64_t q_len; | ||
109 | |||
110 | /** | ||
111 | * Queue prio | ||
112 | */ | ||
113 | uint32_t priority; | ||
114 | |||
115 | /** | ||
116 | * An `enum GNUNET_TRANSPORT_ConnectionStatus` in NBO. | ||
117 | */ | ||
118 | uint32_t cs; | ||
119 | |||
120 | /** | ||
121 | * @brief Next element inside a DLL | ||
122 | */ | ||
123 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *next; | ||
124 | |||
125 | /** | ||
126 | * @brief Previous element inside a DLL | ||
127 | */ | ||
128 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *prev; | ||
129 | }; | ||
130 | |||
131 | |||
132 | /** | ||
133 | * @brief Handle/Context to a single transmission | ||
134 | */ | ||
135 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission | ||
136 | { | ||
137 | }; | ||
138 | |||
139 | |||
140 | /** | ||
141 | * @brief Check whether incoming msg indicating available communicator is | ||
142 | * correct | ||
143 | * | ||
144 | * @param cls Closure | ||
145 | * @param msg Message struct | ||
146 | * | ||
147 | * @return GNUNET_YES in case message is correct | ||
148 | */ | ||
149 | static int | ||
150 | check_communicator_available ( | ||
151 | void *cls, | ||
152 | const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg) | ||
153 | { | ||
154 | uint16_t size; | ||
155 | |||
156 | size = ntohs (msg->header.size) - sizeof(*msg); | ||
157 | if (0 == size) | ||
158 | return GNUNET_OK; /* receive-only communicator */ | ||
159 | GNUNET_MQ_check_zero_termination (msg); | ||
160 | return GNUNET_OK; | ||
161 | } | ||
162 | |||
163 | |||
164 | /** | ||
165 | * @brief Handle new communicator | ||
166 | * | ||
167 | * Store characteristics of communicator, call respective client callback. | ||
168 | * | ||
169 | * @param cls Closure - communicator handle | ||
170 | * @param msg Message struct | ||
171 | */ | ||
172 | static void | ||
173 | handle_communicator_available ( | ||
174 | void *cls, | ||
175 | const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg) | ||
176 | { | ||
177 | struct MyClient *client = cls; | ||
178 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
179 | client->tc; | ||
180 | uint16_t size; | ||
181 | tc_h->c_mq = client->c_mq; | ||
182 | |||
183 | size = ntohs (msg->header.size) - sizeof(*msg); | ||
184 | if (0 == size) | ||
185 | { | ||
186 | GNUNET_SERVICE_client_continue (client->client); | ||
187 | return; /* receive-only communicator */ | ||
188 | } | ||
189 | tc_h->c_characteristics = ntohl (msg->cc); | ||
190 | tc_h->c_addr_prefix = GNUNET_strdup ((const char *) &msg[1]); | ||
191 | if (NULL != tc_h->communicator_available_cb) | ||
192 | { | ||
193 | LOG (GNUNET_ERROR_TYPE_DEBUG, "calling communicator_available_cb()\n"); | ||
194 | tc_h->communicator_available_cb (tc_h->cb_cls, | ||
195 | tc_h, | ||
196 | tc_h->c_characteristics, | ||
197 | tc_h->c_addr_prefix); | ||
198 | } | ||
199 | GNUNET_SERVICE_client_continue (client->client); | ||
200 | LOG (GNUNET_ERROR_TYPE_DEBUG, "finished communicator_available_cb()\n"); | ||
201 | |||
202 | } | ||
203 | |||
204 | |||
205 | /** | ||
206 | * Incoming message. Test message is well-formed. | ||
207 | * | ||
208 | * @param cls the client | ||
209 | * @param msg the send message that was sent | ||
210 | * @return #GNUNET_OK if message is well-formed | ||
211 | */ | ||
212 | static int | ||
213 | check_communicator_backchannel (void *cls, | ||
214 | const struct | ||
215 | GNUNET_TRANSPORT_CommunicatorBackchannel *msg) | ||
216 | { | ||
217 | // struct TransportClient *tc = cls; | ||
218 | |||
219 | // if (CT_COMMUNICATOR != tc->type) | ||
220 | // { | ||
221 | // GNUNET_break (0); | ||
222 | // return GNUNET_SYSERR; | ||
223 | // } | ||
224 | // GNUNET_MQ_check_boxed_message (msg); | ||
225 | return GNUNET_OK; | ||
226 | } | ||
227 | |||
228 | |||
229 | /** | ||
230 | * @brief Receive an incoming message. | ||
231 | * | ||
232 | * Pass the message to the client. | ||
233 | * | ||
234 | * @param cls Closure - communicator handle | ||
235 | * @param msg Message | ||
236 | */ | ||
237 | static void | ||
238 | handle_communicator_backchannel (void *cls, | ||
239 | const struct | ||
240 | GNUNET_TRANSPORT_CommunicatorBackchannel * | ||
241 | bc_msg) | ||
242 | { | ||
243 | struct MyClient *client = cls; | ||
244 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
245 | client->tc; | ||
246 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *other_tc_h; | ||
247 | struct GNUNET_MessageHeader *msg; | ||
248 | msg = (struct GNUNET_MessageHeader *) &bc_msg[1]; | ||
249 | uint16_t isize = ntohs (msg->size); | ||
250 | const char *target_communicator = ((const char *) msg) + isize; | ||
251 | struct GNUNET_TRANSPORT_CommunicatorBackchannelIncoming *cbi; | ||
252 | struct GNUNET_MQ_Envelope *env; | ||
253 | |||
254 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
255 | "Received backchannel message\n"); | ||
256 | if (tc_h->bc_enabled != GNUNET_YES) | ||
257 | { | ||
258 | GNUNET_SERVICE_client_continue (client->client); | ||
259 | return; | ||
260 | } | ||
261 | /* Find client providing this communicator */ | ||
262 | /* Finally, deliver backchannel message to communicator */ | ||
263 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
264 | "Delivering backchannel message of type %u to %s\n", | ||
265 | ntohs (msg->type), | ||
266 | target_communicator); | ||
267 | other_tc_h = tc_h->bc_cb (tc_h, msg, (struct | ||
268 | GNUNET_PeerIdentity*) &bc_msg->pid); | ||
269 | env = GNUNET_MQ_msg_extra ( | ||
270 | cbi, | ||
271 | isize, | ||
272 | GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL_INCOMING); | ||
273 | cbi->pid = tc_h->peer_id; | ||
274 | memcpy (&cbi[1], msg, isize); | ||
275 | |||
276 | |||
277 | GNUNET_MQ_send (other_tc_h->c_mq, env); | ||
278 | GNUNET_SERVICE_client_continue (client->client); | ||
279 | } | ||
280 | |||
281 | |||
282 | /** | ||
283 | * Address of our peer added. Test message is well-formed. | ||
284 | * | ||
285 | * @param cls the client | ||
286 | * @param aam the send message that was sent | ||
287 | * @return #GNUNET_OK if message is well-formed | ||
288 | */ | ||
289 | static int | ||
290 | check_add_address (void *cls, | ||
291 | const struct GNUNET_TRANSPORT_AddAddressMessage *msg) | ||
292 | { | ||
293 | // if (CT_COMMUNICATOR != tc->type) | ||
294 | // { | ||
295 | // GNUNET_break (0); | ||
296 | // return GNUNET_SYSERR; | ||
297 | // } | ||
298 | GNUNET_MQ_check_zero_termination (msg); | ||
299 | return GNUNET_OK; | ||
300 | } | ||
301 | |||
302 | |||
303 | /** | ||
304 | * @brief The communicator informs about an address. | ||
305 | * | ||
306 | * Store address and call client callback. | ||
307 | * | ||
308 | * @param cls Closure - communicator handle | ||
309 | * @param msg Message | ||
310 | */ | ||
311 | static void | ||
312 | handle_add_address (void *cls, | ||
313 | const struct GNUNET_TRANSPORT_AddAddressMessage *msg) | ||
314 | { | ||
315 | struct MyClient *client = cls; | ||
316 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
317 | client->tc; | ||
318 | uint16_t size; | ||
319 | size = ntohs (msg->header.size) - sizeof(*msg); | ||
320 | if (0 == size) | ||
321 | return; /* receive-only communicator */ | ||
322 | LOG (GNUNET_ERROR_TYPE_DEBUG, "received add address cb %u\n", size); | ||
323 | tc_h->c_address = GNUNET_strdup ((const char *) &msg[1]); | ||
324 | if (NULL != tc_h->add_address_cb) | ||
325 | { | ||
326 | LOG (GNUNET_ERROR_TYPE_DEBUG, "calling add_address_cb()\n"); | ||
327 | tc_h->add_address_cb (tc_h->cb_cls, | ||
328 | tc_h, | ||
329 | tc_h->c_address, | ||
330 | GNUNET_TIME_relative_ntoh (msg->expiration), | ||
331 | msg->aid, | ||
332 | ntohl (msg->nt)); | ||
333 | } | ||
334 | GNUNET_SERVICE_client_continue (client->client); | ||
335 | } | ||
336 | |||
337 | |||
338 | /** | ||
339 | * Incoming message. Test message is well-formed. | ||
340 | * | ||
341 | * @param cls the client | ||
342 | * @param msg the send message that was sent | ||
343 | * @return #GNUNET_OK if message is well-formed | ||
344 | */ | ||
345 | static int | ||
346 | check_incoming_msg (void *cls, | ||
347 | const struct GNUNET_TRANSPORT_IncomingMessage *msg) | ||
348 | { | ||
349 | // struct TransportClient *tc = cls; | ||
350 | |||
351 | // if (CT_COMMUNICATOR != tc->type) | ||
352 | // { | ||
353 | // GNUNET_break (0); | ||
354 | // return GNUNET_SYSERR; | ||
355 | // } | ||
356 | GNUNET_MQ_check_boxed_message (msg); | ||
357 | return GNUNET_OK; | ||
358 | } | ||
359 | |||
360 | |||
361 | /** | ||
362 | * @brief Receive an incoming message. | ||
363 | * | ||
364 | * Pass the message to the client. | ||
365 | * | ||
366 | * @param cls Closure - communicator handle | ||
367 | * @param msg Message | ||
368 | */ | ||
369 | static void | ||
370 | handle_incoming_msg (void *cls, | ||
371 | const struct GNUNET_TRANSPORT_IncomingMessage *inc_msg) | ||
372 | { | ||
373 | struct MyClient *client = cls; | ||
374 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
375 | client->tc; | ||
376 | struct GNUNET_MessageHeader *msg; | ||
377 | msg = (struct GNUNET_MessageHeader *) &inc_msg[1]; | ||
378 | size_t payload_len = ntohs (msg->size) - sizeof (struct | ||
379 | GNUNET_MessageHeader); | ||
380 | if (NULL != tc_h->incoming_msg_cb) | ||
381 | { | ||
382 | tc_h->incoming_msg_cb (tc_h->cb_cls, | ||
383 | tc_h, | ||
384 | (char*) &msg[1], | ||
385 | payload_len); | ||
386 | } | ||
387 | else | ||
388 | { | ||
389 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
390 | "Incoming message from communicator but no handler!\n"); | ||
391 | } | ||
392 | if (GNUNET_YES == ntohl (inc_msg->fc_on)) | ||
393 | { | ||
394 | /* send ACK when done to communicator for flow control! */ | ||
395 | struct GNUNET_MQ_Envelope *env; | ||
396 | struct GNUNET_TRANSPORT_IncomingMessageAck *ack; | ||
397 | |||
398 | env = GNUNET_MQ_msg (ack, GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG_ACK); | ||
399 | GNUNET_assert (NULL != env); | ||
400 | ack->reserved = htonl (0); | ||
401 | ack->fc_id = inc_msg->fc_id; | ||
402 | ack->sender = inc_msg->sender; | ||
403 | GNUNET_MQ_send (tc_h->c_mq, env); | ||
404 | } | ||
405 | |||
406 | GNUNET_SERVICE_client_continue (client->client); | ||
407 | } | ||
408 | |||
409 | |||
410 | /** | ||
411 | * @brief Communicator informs that it tries to establish requested queue | ||
412 | * | ||
413 | * @param cls Closure - communicator handle | ||
414 | * @param msg Message | ||
415 | */ | ||
416 | static void | ||
417 | handle_queue_create_ok (void *cls, | ||
418 | const struct GNUNET_TRANSPORT_CreateQueueResponse *msg) | ||
419 | { | ||
420 | struct MyClient *client = cls; | ||
421 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
422 | client->tc; | ||
423 | |||
424 | if (NULL != tc_h->queue_create_reply_cb) | ||
425 | { | ||
426 | tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_YES); | ||
427 | } | ||
428 | GNUNET_SERVICE_client_continue (client->client); | ||
429 | } | ||
430 | |||
431 | |||
432 | /** | ||
433 | * @brief Communicator informs that it wont try establishing requested queue. | ||
434 | * | ||
435 | * It will not do so probably because the address is bougus (see comment to | ||
436 | * #GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL) | ||
437 | * | ||
438 | * @param cls Closure - communicator handle | ||
439 | * @param msg Message | ||
440 | */ | ||
441 | static void | ||
442 | handle_queue_create_fail ( | ||
443 | void *cls, | ||
444 | const struct GNUNET_TRANSPORT_CreateQueueResponse *msg) | ||
445 | { | ||
446 | struct MyClient *client = cls; | ||
447 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
448 | client->tc; | ||
449 | |||
450 | if (NULL != tc_h->queue_create_reply_cb) | ||
451 | { | ||
452 | tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_NO); | ||
453 | } | ||
454 | GNUNET_SERVICE_client_continue (client->client); | ||
455 | } | ||
456 | |||
457 | |||
458 | /** | ||
459 | * New queue became available. Check message. | ||
460 | * | ||
461 | * @param cls the client | ||
462 | * @param aqm the send message that was sent | ||
463 | */ | ||
464 | static int | ||
465 | check_add_queue_message (void *cls, | ||
466 | const struct GNUNET_TRANSPORT_AddQueueMessage *aqm) | ||
467 | { | ||
468 | GNUNET_MQ_check_zero_termination (aqm); | ||
469 | return GNUNET_OK; | ||
470 | } | ||
471 | |||
472 | |||
473 | /** | ||
474 | * @brief Handle new queue | ||
475 | * | ||
476 | * Store context and call client callback. | ||
477 | * | ||
478 | * @param cls Closure - communicator handle | ||
479 | * @param msg Message struct | ||
480 | */ | ||
481 | static void | ||
482 | handle_add_queue_message (void *cls, | ||
483 | const struct GNUNET_TRANSPORT_AddQueueMessage *msg) | ||
484 | { | ||
485 | struct MyClient *client = cls; | ||
486 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
487 | client->tc; | ||
488 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | ||
489 | |||
490 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
491 | "Got queue with ID %u\n", msg->qid); | ||
492 | for (tc_queue = tc_h->queue_head; NULL != tc_queue; tc_queue = tc_queue->next) | ||
493 | { | ||
494 | if (tc_queue->qid == msg->qid) | ||
495 | break; | ||
496 | } | ||
497 | if (NULL == tc_queue) | ||
498 | { | ||
499 | tc_queue = | ||
500 | GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue); | ||
501 | tc_queue->tc_h = tc_h; | ||
502 | tc_queue->qid = msg->qid; | ||
503 | tc_queue->peer_id = msg->receiver; | ||
504 | GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue); | ||
505 | } | ||
506 | GNUNET_assert (tc_queue->qid == msg->qid); | ||
507 | GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver)); | ||
508 | tc_queue->nt = msg->nt; | ||
509 | tc_queue->mtu = ntohl (msg->mtu); | ||
510 | tc_queue->cs = msg->cs; | ||
511 | tc_queue->priority = ntohl (msg->priority); | ||
512 | tc_queue->q_len = GNUNET_ntohll (msg->q_len); | ||
513 | if (NULL != tc_h->add_queue_cb) | ||
514 | { | ||
515 | tc_h->add_queue_cb (tc_h->cb_cls, tc_h, tc_queue, tc_queue->mtu); | ||
516 | } | ||
517 | GNUNET_SERVICE_client_continue (client->client); | ||
518 | } | ||
519 | |||
520 | |||
521 | /** | ||
522 | * @brief Handle new queue | ||
523 | * | ||
524 | * Store context and call client callback. | ||
525 | * | ||
526 | * @param cls Closure - communicator handle | ||
527 | * @param msg Message struct | ||
528 | */ | ||
529 | static void | ||
530 | handle_update_queue_message (void *cls, | ||
531 | const struct | ||
532 | GNUNET_TRANSPORT_UpdateQueueMessage *msg) | ||
533 | { | ||
534 | struct MyClient *client = cls; | ||
535 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
536 | client->tc; | ||
537 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | ||
538 | |||
539 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
540 | "Received queue update message for %u with q_len %" PRIu64 "\n", | ||
541 | msg->qid, GNUNET_ntohll (msg->q_len)); | ||
542 | tc_queue = tc_h->queue_head; | ||
543 | if (NULL != tc_queue) | ||
544 | { | ||
545 | while (tc_queue->qid != msg->qid) | ||
546 | { | ||
547 | tc_queue = tc_queue->next; | ||
548 | } | ||
549 | } | ||
550 | GNUNET_assert (tc_queue->qid == msg->qid); | ||
551 | GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver)); | ||
552 | tc_queue->nt = msg->nt; | ||
553 | tc_queue->mtu = ntohl (msg->mtu); | ||
554 | tc_queue->cs = msg->cs; | ||
555 | tc_queue->priority = ntohl (msg->priority); | ||
556 | // Uncomment this for alternativ 1 of backchannel functionality | ||
557 | tc_queue->q_len += GNUNET_ntohll (msg->q_len); | ||
558 | // Until here for alternativ 1 | ||
559 | // Uncomment this for alternativ 2 of backchannel functionality | ||
560 | // tc_queue->q_len = GNUNET_ntohll (msg->q_len); | ||
561 | // Until here for alternativ 2 | ||
562 | GNUNET_SERVICE_client_continue (client->client); | ||
563 | } | ||
564 | |||
565 | |||
566 | /** | ||
567 | * @brief Shut down the service | ||
568 | * | ||
569 | * @param cls Closure - Handle to the service | ||
570 | */ | ||
571 | static void | ||
572 | shutdown_service (void *cls) | ||
573 | { | ||
574 | struct GNUNET_SERVICE_Handle *h = cls; | ||
575 | |||
576 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
577 | "Shutting down service!\n"); | ||
578 | |||
579 | GNUNET_SERVICE_stop (h); | ||
580 | } | ||
581 | |||
582 | |||
583 | /** | ||
584 | * @brief Callback called when new Client (Communicator) connects | ||
585 | * | ||
586 | * @param cls Closure - TransporCommmunicator Handle | ||
587 | * @param client Client | ||
588 | * @param mq Messagequeue | ||
589 | * | ||
590 | * @return TransportCommunicator Handle | ||
591 | */ | ||
592 | static void * | ||
593 | connect_cb (void *cls, | ||
594 | struct GNUNET_SERVICE_Client *client, | ||
595 | struct GNUNET_MQ_Handle *mq) | ||
596 | { | ||
597 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls; | ||
598 | struct MyClient *new_c; | ||
599 | |||
600 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected to %p.\n", | ||
601 | client, tc_h); | ||
602 | new_c = GNUNET_new (struct MyClient); | ||
603 | new_c->client = client; | ||
604 | new_c->c_mq = mq; | ||
605 | new_c->tc = tc_h; | ||
606 | GNUNET_CONTAINER_DLL_insert (tc_h->client_head, | ||
607 | tc_h->client_tail, | ||
608 | new_c); | ||
609 | |||
610 | if (NULL == tc_h->queue_head) | ||
611 | return new_c; | ||
612 | /* Iterate over queues. They are yet to be opened. Request opening. */ | ||
613 | for (struct | ||
614 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_iter = | ||
615 | tc_h->queue_head; | ||
616 | NULL != tc_queue_iter; | ||
617 | tc_queue_iter = tc_queue_iter->next) | ||
618 | { | ||
619 | if (NULL == tc_queue_iter->open_queue_env) | ||
620 | continue; | ||
621 | /* Send the previously created mq envelope to request the creation of the | ||
622 | * queue. */ | ||
623 | GNUNET_MQ_send (tc_h->c_mq, | ||
624 | tc_queue_iter->open_queue_env); | ||
625 | tc_queue_iter->open_queue_env = NULL; | ||
626 | } | ||
627 | return new_c; | ||
628 | } | ||
629 | |||
630 | |||
631 | /** | ||
632 | * @brief Callback called when Client disconnects | ||
633 | * | ||
634 | * @param cls Closure - TransportCommunicator Handle | ||
635 | * @param client Client | ||
636 | * @param internal_cls TransporCommmunicator Handle | ||
637 | */ | ||
638 | static void | ||
639 | disconnect_cb (void *cls, | ||
640 | struct GNUNET_SERVICE_Client *client, | ||
641 | void *internal_cls) | ||
642 | { | ||
643 | struct MyClient *cl = cls; | ||
644 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls; | ||
645 | |||
646 | for (cl = tc_h->client_head; NULL != cl; cl = cl->next) | ||
647 | { | ||
648 | if (cl->client != client) | ||
649 | continue; | ||
650 | GNUNET_CONTAINER_DLL_remove (tc_h->client_head, | ||
651 | tc_h->client_tail, | ||
652 | cl); | ||
653 | if (cl->c_mq == tc_h->c_mq) | ||
654 | tc_h->c_mq = NULL; | ||
655 | GNUNET_free (cl); | ||
656 | break; | ||
657 | } | ||
658 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected.\n"); | ||
659 | } | ||
660 | |||
661 | |||
662 | /** | ||
663 | * Message was transmitted. Process the request. | ||
664 | * | ||
665 | * @param cls the client | ||
666 | * @param sma the send message that was sent | ||
667 | */ | ||
668 | static void | ||
669 | handle_send_message_ack (void *cls, | ||
670 | const struct GNUNET_TRANSPORT_SendMessageToAck *sma) | ||
671 | { | ||
672 | struct MyClient *client = cls; | ||
673 | GNUNET_SERVICE_client_continue (client->client); | ||
674 | // NOP | ||
675 | } | ||
676 | |||
677 | |||
678 | /** | ||
679 | * @brief Start the communicator part of the transport service | ||
680 | * | ||
681 | * @param communicator_available Callback to be called when a new communicator | ||
682 | * becomes available | ||
683 | * @param cfg Configuration | ||
684 | */ | ||
685 | static void | ||
686 | transport_communicator_start ( | ||
687 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
688 | { | ||
689 | struct GNUNET_MQ_MessageHandler mh[] = { | ||
690 | GNUNET_MQ_hd_var_size (communicator_available, | ||
691 | GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR, | ||
692 | struct GNUNET_TRANSPORT_CommunicatorAvailableMessage, | ||
693 | tc_h), | ||
694 | GNUNET_MQ_hd_var_size (communicator_backchannel, | ||
695 | GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL, | ||
696 | struct GNUNET_TRANSPORT_CommunicatorBackchannel, | ||
697 | tc_h), | ||
698 | GNUNET_MQ_hd_var_size (add_address, | ||
699 | GNUNET_MESSAGE_TYPE_TRANSPORT_ADD_ADDRESS, | ||
700 | struct GNUNET_TRANSPORT_AddAddressMessage, | ||
701 | tc_h), | ||
702 | // GNUNET_MQ_hd_fixed_size (del_address, | ||
703 | // GNUNET_MESSAGE_TYPE_TRANSPORT_DEL_ADDRESS, | ||
704 | // struct GNUNET_TRANSPORT_DelAddressMessage, | ||
705 | // NULL), | ||
706 | GNUNET_MQ_hd_var_size (incoming_msg, | ||
707 | GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG, | ||
708 | struct GNUNET_TRANSPORT_IncomingMessage, | ||
709 | tc_h), | ||
710 | GNUNET_MQ_hd_fixed_size (queue_create_ok, | ||
711 | GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_OK, | ||
712 | struct GNUNET_TRANSPORT_CreateQueueResponse, | ||
713 | tc_h), | ||
714 | GNUNET_MQ_hd_fixed_size (queue_create_fail, | ||
715 | GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL, | ||
716 | struct GNUNET_TRANSPORT_CreateQueueResponse, | ||
717 | tc_h), | ||
718 | GNUNET_MQ_hd_var_size (add_queue_message, | ||
719 | GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_SETUP, | ||
720 | struct GNUNET_TRANSPORT_AddQueueMessage, | ||
721 | tc_h), | ||
722 | GNUNET_MQ_hd_fixed_size (update_queue_message, | ||
723 | GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_UPDATE, | ||
724 | struct GNUNET_TRANSPORT_UpdateQueueMessage, | ||
725 | tc_h), | ||
726 | // GNUNET_MQ_hd_fixed_size (del_queue_message, | ||
727 | // GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_TEARDOWN, | ||
728 | // struct GNUNET_TRANSPORT_DelQueueMessage, | ||
729 | // NULL), | ||
730 | GNUNET_MQ_hd_fixed_size (send_message_ack, | ||
731 | GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG_ACK, | ||
732 | struct GNUNET_TRANSPORT_SendMessageToAck, | ||
733 | tc_h), | ||
734 | GNUNET_MQ_handler_end () | ||
735 | }; | ||
736 | |||
737 | |||
738 | tc_h->sh = GNUNET_SERVICE_start ("transport", | ||
739 | tc_h->cfg, | ||
740 | &connect_cb, | ||
741 | &disconnect_cb, | ||
742 | tc_h, | ||
743 | mh); | ||
744 | GNUNET_assert (NULL != tc_h->sh); | ||
745 | } | ||
746 | |||
747 | |||
748 | /** | ||
749 | * @brief Task run at shutdown to kill communicator and clean up | ||
750 | * | ||
751 | * @param cls Closure - Process of communicator | ||
752 | */ | ||
753 | static void | ||
754 | shutdown_process (struct GNUNET_OS_Process *proc) | ||
755 | { | ||
756 | if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) | ||
757 | { | ||
758 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
759 | "Error shutting down process with SIGERM, trying SIGKILL\n"); | ||
760 | if (0 != GNUNET_OS_process_kill (proc, SIGKILL)) | ||
761 | { | ||
762 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
763 | "Error shutting down process with SIGERM and SIGKILL\n"); | ||
764 | } | ||
765 | } | ||
766 | GNUNET_OS_process_destroy (proc); | ||
767 | } | ||
768 | |||
769 | static void | ||
770 | shutdown_peerstore (void *cls) | ||
771 | { | ||
772 | struct GNUNET_OS_Process *proc = cls; | ||
773 | shutdown_process (proc); | ||
774 | } | ||
775 | |||
776 | static void | ||
777 | shutdown_communicator (void *cls) | ||
778 | { | ||
779 | struct GNUNET_OS_Process *proc = cls; | ||
780 | shutdown_process (proc); | ||
781 | } | ||
782 | |||
783 | |||
784 | /** | ||
785 | * @brief Start the communicator | ||
786 | * | ||
787 | * @param cfgname Name of the communicator | ||
788 | */ | ||
789 | static void | ||
790 | communicator_start ( | ||
791 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h, | ||
792 | const char *binary_name) | ||
793 | { | ||
794 | char *binary; | ||
795 | char *loprefix; | ||
796 | char *section_name; | ||
797 | |||
798 | LOG (GNUNET_ERROR_TYPE_DEBUG, "communicator_start\n"); | ||
799 | |||
800 | section_name = strchr (binary_name, '-'); | ||
801 | section_name++; | ||
802 | |||
803 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (tc_h->cfg, | ||
804 | section_name, | ||
805 | "PREFIX", | ||
806 | &loprefix)) | ||
807 | loprefix = GNUNET_strdup (""); | ||
808 | |||
809 | |||
810 | binary = GNUNET_OS_get_libexec_binary_path (binary_name); | ||
811 | tc_h->c_proc = GNUNET_OS_start_process_s (GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | ||
812 | NULL, | ||
813 | loprefix, | ||
814 | binary, | ||
815 | binary_name, | ||
816 | "-c", | ||
817 | tc_h->cfg_filename, | ||
818 | NULL); | ||
819 | if (NULL == tc_h->c_proc) | ||
820 | { | ||
821 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start communicator!"); | ||
822 | return; | ||
823 | } | ||
824 | LOG (GNUNET_ERROR_TYPE_INFO, "started communicator\n"); | ||
825 | GNUNET_free (binary); | ||
826 | } | ||
827 | |||
828 | |||
829 | /** | ||
830 | * @brief Task run at shutdown to kill communicator and clean up | ||
831 | * | ||
832 | * @param cls Closure - Process of communicator | ||
833 | */ | ||
834 | static void | ||
835 | shutdown_nat (void *cls) | ||
836 | { | ||
837 | struct GNUNET_OS_Process *proc = cls; | ||
838 | shutdown_process (proc); | ||
839 | } | ||
840 | |||
841 | |||
842 | /** | ||
843 | * @brief Task run at shutdown to kill the resolver process | ||
844 | * | ||
845 | * @param cls Closure - Process of communicator | ||
846 | */ | ||
847 | static void | ||
848 | shutdown_resolver (void *cls) | ||
849 | { | ||
850 | struct GNUNET_OS_Process *proc = cls; | ||
851 | shutdown_process (proc); | ||
852 | } | ||
853 | |||
854 | |||
855 | static void | ||
856 | resolver_start (struct | ||
857 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
858 | { | ||
859 | char *binary; | ||
860 | |||
861 | LOG (GNUNET_ERROR_TYPE_DEBUG, "resolver_start\n"); | ||
862 | binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver"); | ||
863 | tc_h->resolver_proc = GNUNET_OS_start_process ( | ||
864 | GNUNET_OS_INHERIT_STD_OUT_AND_ERR | ||
865 | | GNUNET_OS_USE_PIPE_CONTROL, | ||
866 | NULL, | ||
867 | NULL, | ||
868 | NULL, | ||
869 | binary, | ||
870 | "gnunet-service-resolver", | ||
871 | "-c", | ||
872 | tc_h->cfg_filename, | ||
873 | NULL); | ||
874 | if (NULL == tc_h->resolver_proc) | ||
875 | { | ||
876 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start resolver service!"); | ||
877 | return; | ||
878 | } | ||
879 | LOG (GNUNET_ERROR_TYPE_INFO, "started resolver service\n"); | ||
880 | GNUNET_free (binary); | ||
881 | |||
882 | } | ||
883 | |||
884 | |||
885 | /** | ||
886 | * @brief Start Peerstore | ||
887 | * | ||
888 | */ | ||
889 | static void | ||
890 | peerstore_start ( | ||
891 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
892 | { | ||
893 | char *binary; | ||
894 | |||
895 | binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-peerstore"); | ||
896 | tc_h->ps_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | ||
897 | NULL, | ||
898 | NULL, | ||
899 | NULL, | ||
900 | binary, | ||
901 | "gnunet-service-peerstore", | ||
902 | "-c", | ||
903 | tc_h->cfg_filename, | ||
904 | NULL); | ||
905 | if (NULL == tc_h->ps_proc) | ||
906 | { | ||
907 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start Peerstore!"); | ||
908 | return; | ||
909 | } | ||
910 | LOG (GNUNET_ERROR_TYPE_INFO, "started Peerstore\n"); | ||
911 | GNUNET_free (binary); | ||
912 | } | ||
913 | |||
914 | /** | ||
915 | * @brief Start NAT | ||
916 | * | ||
917 | */ | ||
918 | static void | ||
919 | nat_start ( | ||
920 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
921 | { | ||
922 | char *binary; | ||
923 | |||
924 | LOG (GNUNET_ERROR_TYPE_DEBUG, "nat_start\n"); | ||
925 | binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-nat"); | ||
926 | tc_h->nat_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR | ||
927 | | GNUNET_OS_USE_PIPE_CONTROL, | ||
928 | NULL, | ||
929 | NULL, | ||
930 | NULL, | ||
931 | binary, | ||
932 | "gnunet-service-nat", | ||
933 | "-c", | ||
934 | tc_h->cfg_filename, | ||
935 | NULL); | ||
936 | if (NULL == tc_h->nat_proc) | ||
937 | { | ||
938 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start NAT!"); | ||
939 | return; | ||
940 | } | ||
941 | LOG (GNUNET_ERROR_TYPE_INFO, "started NAT\n"); | ||
942 | GNUNET_free (binary); | ||
943 | } | ||
944 | |||
945 | |||
946 | /** | ||
947 | * @brief Start communicator part of transport service and communicator | ||
948 | * | ||
949 | * @param service_name Name of the service | ||
950 | * @param cfg Configuration handle | ||
951 | * @param communicator_available_cb Callback that is called when a new | ||
952 | * @param add_address_cb Callback that is called when a new | ||
953 | * communicator becomes available | ||
954 | * @param cb_cls Closure to @a communicator_available_cb and @a | ||
955 | * | ||
956 | * @return Handle to the communicator duo | ||
957 | */ | ||
958 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle * | ||
959 | GNUNET_TRANSPORT_TESTING_transport_communicator_service_start ( | ||
960 | const char *service_name, | ||
961 | const char *binary_name, | ||
962 | const char *cfg_filename, | ||
963 | const struct GNUNET_PeerIdentity *peer_id, | ||
964 | GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback | ||
965 | communicator_available_cb, | ||
966 | GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb, | ||
967 | GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb, | ||
968 | GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb, | ||
969 | GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_message_cb, | ||
970 | GNUNET_TRANSPORT_TESTING_BackchannelCallback bc_cb, | ||
971 | void *cb_cls) | ||
972 | { | ||
973 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h; | ||
974 | |||
975 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
976 | "Starting new transport/communicator combo with config %s\n", | ||
977 | cfg_filename); | ||
978 | tc_h = | ||
979 | GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle); | ||
980 | tc_h->cfg_filename = GNUNET_strdup (cfg_filename); | ||
981 | tc_h->cfg = GNUNET_CONFIGURATION_create (); | ||
982 | if ((GNUNET_SYSERR == GNUNET_CONFIGURATION_load (tc_h->cfg, cfg_filename))) | ||
983 | { | ||
984 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
985 | _ ("Malformed configuration file `%s', exit ...\n"), | ||
986 | cfg_filename); | ||
987 | GNUNET_free (tc_h->cfg_filename); | ||
988 | GNUNET_CONFIGURATION_destroy (tc_h->cfg); | ||
989 | GNUNET_free (tc_h); | ||
990 | return NULL; | ||
991 | } | ||
992 | tc_h->bc_enabled = GNUNET_CONFIGURATION_get_value_yesno (tc_h->cfg, | ||
993 | "communicator-test", | ||
994 | "BACKCHANNEL_ENABLED"); | ||
995 | tc_h->communicator_available_cb = communicator_available_cb; | ||
996 | tc_h->add_address_cb = add_address_cb; | ||
997 | tc_h->queue_create_reply_cb = queue_create_reply_cb; | ||
998 | tc_h->add_queue_cb = add_queue_cb; | ||
999 | tc_h->incoming_msg_cb = incoming_message_cb; | ||
1000 | tc_h->bc_cb = bc_cb; | ||
1001 | tc_h->peer_id = *peer_id; | ||
1002 | tc_h->cb_cls = cb_cls; | ||
1003 | |||
1004 | /* Start communicator part of service */ | ||
1005 | transport_communicator_start (tc_h); | ||
1006 | /* Start NAT */ | ||
1007 | nat_start (tc_h); | ||
1008 | /* Start resolver service */ | ||
1009 | resolver_start (tc_h); | ||
1010 | /* Start peerstore service */ | ||
1011 | peerstore_start (tc_h); | ||
1012 | /* Schedule start communicator */ | ||
1013 | communicator_start (tc_h, | ||
1014 | binary_name); | ||
1015 | return tc_h; | ||
1016 | } | ||
1017 | |||
1018 | |||
1019 | void | ||
1020 | GNUNET_TRANSPORT_TESTING_transport_communicator_service_stop ( | ||
1021 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
1022 | { | ||
1023 | shutdown_communicator (tc_h->c_proc); | ||
1024 | shutdown_service (tc_h->sh); | ||
1025 | shutdown_nat (tc_h->nat_proc); | ||
1026 | shutdown_resolver (tc_h->resolver_proc); | ||
1027 | shutdown_peerstore (tc_h->ps_proc); | ||
1028 | GNUNET_CONFIGURATION_destroy (tc_h->cfg); | ||
1029 | GNUNET_free (tc_h); | ||
1030 | } | ||
1031 | |||
1032 | |||
1033 | /** | ||
1034 | * @brief Instruct communicator to open a queue | ||
1035 | * | ||
1036 | * @param tc_h Handle to communicator which shall open queue | ||
1037 | * @param peer_id Towards which peer | ||
1038 | * @param address For which address | ||
1039 | */ | ||
1040 | void | ||
1041 | GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue ( | ||
1042 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h, | ||
1043 | const struct GNUNET_PeerIdentity *peer_id, | ||
1044 | const char *address) | ||
1045 | { | ||
1046 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | ||
1047 | static uint32_t idgen; | ||
1048 | char *prefix; | ||
1049 | struct GNUNET_TRANSPORT_CreateQueue *msg; | ||
1050 | struct GNUNET_MQ_Envelope *env; | ||
1051 | size_t alen; | ||
1052 | |||
1053 | tc_queue = | ||
1054 | GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue); | ||
1055 | tc_queue->tc_h = tc_h; | ||
1056 | prefix = GNUNET_HELLO_address_to_prefix (address); | ||
1057 | if (NULL == prefix) | ||
1058 | { | ||
1059 | GNUNET_break (0); /* We got an invalid address!? */ | ||
1060 | GNUNET_free (tc_queue); | ||
1061 | return; | ||
1062 | } | ||
1063 | GNUNET_free (prefix); | ||
1064 | alen = strlen (address) + 1; | ||
1065 | env = | ||
1066 | GNUNET_MQ_msg_extra (msg, alen, GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE); | ||
1067 | msg->request_id = htonl (idgen++); | ||
1068 | tc_queue->qid = msg->request_id; | ||
1069 | msg->receiver = *peer_id; | ||
1070 | tc_queue->peer_id = *peer_id; | ||
1071 | memcpy (&msg[1], address, alen); | ||
1072 | if (NULL != tc_h->c_mq) | ||
1073 | { | ||
1074 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1075 | "Sending queue create immediately\n"); | ||
1076 | GNUNET_MQ_send (tc_h->c_mq, env); | ||
1077 | } | ||
1078 | else | ||
1079 | { | ||
1080 | tc_queue->open_queue_env = env; | ||
1081 | } | ||
1082 | GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue); | ||
1083 | } | ||
1084 | |||
1085 | |||
1086 | /** | ||
1087 | * @brief Instruct communicator to send data | ||
1088 | * | ||
1089 | * @param tc_queue The queue to use for sending | ||
1090 | * @param cont function to call when done sending | ||
1091 | * @param cont_cls closure for @a cont | ||
1092 | * @param payload Data to send | ||
1093 | * @param payload_size Size of the @a payload | ||
1094 | */ | ||
1095 | void | ||
1096 | GNUNET_TRANSPORT_TESTING_transport_communicator_send | ||
1097 | (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h, | ||
1098 | GNUNET_SCHEDULER_TaskCallback cont, | ||
1099 | void *cont_cls, | ||
1100 | const void *payload, | ||
1101 | size_t payload_size) | ||
1102 | { | ||
1103 | struct GNUNET_MessageHeader *mh; | ||
1104 | struct GNUNET_TRANSPORT_SendMessageTo *msg; | ||
1105 | struct GNUNET_MQ_Envelope *env; | ||
1106 | size_t inbox_size; | ||
1107 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | ||
1108 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_tmp; | ||
1109 | |||
1110 | tc_queue = NULL; | ||
1111 | for (tc_queue_tmp = tc_h->queue_head; | ||
1112 | NULL != tc_queue_tmp; | ||
1113 | tc_queue_tmp = tc_queue_tmp->next) | ||
1114 | { | ||
1115 | if (tc_queue_tmp->q_len <= 0) | ||
1116 | continue; | ||
1117 | if (NULL == tc_queue) | ||
1118 | { | ||
1119 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1120 | "Selecting queue with prio %u, len %" PRIu64 " and MTU %u\n", | ||
1121 | tc_queue_tmp->priority, | ||
1122 | tc_queue_tmp->q_len, | ||
1123 | tc_queue_tmp->mtu); | ||
1124 | tc_queue = tc_queue_tmp; | ||
1125 | continue; | ||
1126 | } | ||
1127 | if (tc_queue->priority < tc_queue_tmp->priority) | ||
1128 | { | ||
1129 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1130 | "Selecting queue with prio %u, len %" PRIu64 " and MTU %u\n", | ||
1131 | tc_queue_tmp->priority, | ||
1132 | tc_queue_tmp->q_len, | ||
1133 | tc_queue_tmp->mtu); | ||
1134 | tc_queue = tc_queue_tmp; | ||
1135 | } | ||
1136 | } | ||
1137 | GNUNET_assert (NULL != tc_queue); | ||
1138 | // Uncomment this for alternativ 1 of backchannel functionality | ||
1139 | if (tc_queue->q_len != GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED) | ||
1140 | tc_queue->q_len--; | ||
1141 | // Until here for alternativ 1 | ||
1142 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1143 | "Sending message\n"); | ||
1144 | inbox_size = sizeof (struct GNUNET_MessageHeader) + payload_size; | ||
1145 | env = GNUNET_MQ_msg_extra (msg, | ||
1146 | inbox_size, | ||
1147 | GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG); | ||
1148 | GNUNET_assert (NULL != env); | ||
1149 | msg->qid = htonl (tc_queue->qid); | ||
1150 | msg->mid = tc_queue->mid++; | ||
1151 | msg->receiver = tc_queue->peer_id; | ||
1152 | mh = (struct GNUNET_MessageHeader *) &msg[1]; | ||
1153 | mh->size = htons (inbox_size); | ||
1154 | mh->type = GNUNET_MESSAGE_TYPE_DUMMY; | ||
1155 | memcpy (&mh[1], | ||
1156 | payload, | ||
1157 | payload_size); | ||
1158 | if (NULL != cont) | ||
1159 | GNUNET_MQ_notify_sent (env, | ||
1160 | cont, | ||
1161 | cont_cls); | ||
1162 | GNUNET_MQ_send (tc_queue->tc_h->c_mq, | ||
1163 | env); | ||
1164 | } | ||
diff --git a/src/transport/transport-testing-communicator.h b/src/transport/transport-testing-communicator.h new file mode 100644 index 000000000..693e61515 --- /dev/null +++ b/src/transport/transport-testing-communicator.h | |||
@@ -0,0 +1,355 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 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 | |||
21 | /** | ||
22 | * @file transport/transport-testing-communicator.h | ||
23 | * @brief functions and structures related to testing-tng | ||
24 | * @author Christian Grothoff | ||
25 | * @author Julius Bünger | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_ats_transport_service.h" | ||
30 | #include "transport.h" | ||
31 | |||
32 | /** | ||
33 | * @brief Queue of a communicator and some context | ||
34 | */ | ||
35 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue; | ||
36 | |||
37 | |||
38 | /** | ||
39 | * @brief Handle/Context to a single transmission | ||
40 | */ | ||
41 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission; | ||
42 | |||
43 | /** | ||
44 | * @brief Function signature for callbacks that are called when new | ||
45 | * backchannel message arrived | ||
46 | * | ||
47 | * @param cls Closure | ||
48 | * @param msg Backchannel message | ||
49 | * @param pid Target peer | ||
50 | */ | ||
51 | typedef struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle * | ||
52 | (*GNUNET_TRANSPORT_TESTING_BackchannelCallback)(void *cls, | ||
53 | struct GNUNET_MessageHeader *msg, | ||
54 | struct GNUNET_PeerIdentity *pid); | ||
55 | |||
56 | |||
57 | /** | ||
58 | * @brief Function signature for callbacks that are called when new | ||
59 | * communicators become available | ||
60 | * | ||
61 | * @param cls Closure | ||
62 | * @param tc_h Communicator handle | ||
63 | * @param cc Characteristics of communicator | ||
64 | * @param address_prefix Prefix of the address | ||
65 | */ | ||
66 | typedef void | ||
67 | (*GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback)(void *cls, | ||
68 | struct | ||
69 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | ||
70 | *tc_h, | ||
71 | enum | ||
72 | GNUNET_TRANSPORT_CommunicatorCharacteristics | ||
73 | cc, | ||
74 | char *address_prefix); | ||
75 | |||
76 | |||
77 | /** | ||
78 | * @brief Receive information about the address of a communicator. | ||
79 | * | ||
80 | * @param cls Closure | ||
81 | * @param tc_h Communicator handle | ||
82 | * @param address Address represented as string | ||
83 | * @param expiration Expiration | ||
84 | * @param aid Aid | ||
85 | * @param nt Network Type | ||
86 | */ | ||
87 | typedef void | ||
88 | (*GNUNET_TRANSPORT_TESTING_AddAddressCallback)(void *cls, | ||
89 | struct | ||
90 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | ||
91 | *tc_h, | ||
92 | const char *address, | ||
93 | struct GNUNET_TIME_Relative | ||
94 | expiration, | ||
95 | uint32_t aid, | ||
96 | enum GNUNET_NetworkType nt); | ||
97 | |||
98 | |||
99 | /** | ||
100 | * @brief Get informed about the success of a queue request. | ||
101 | * | ||
102 | * @param cls Closure | ||
103 | * @param tc_h Communicator handle | ||
104 | * @param will_try #GNUNET_YES if communicator will try to create queue | ||
105 | */ | ||
106 | typedef void | ||
107 | (*GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback)(void *cls, | ||
108 | struct | ||
109 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | ||
110 | *tc_h, | ||
111 | int will_try); | ||
112 | |||
113 | |||
114 | /** | ||
115 | * @brief Handle opening of queue | ||
116 | * | ||
117 | * @param cls Closure | ||
118 | * @param tc_h Communicator handle | ||
119 | * @param tc_queue Handle to newly opened queue | ||
120 | */ | ||
121 | typedef void | ||
122 | (*GNUNET_TRANSPORT_TESTING_AddQueueCallback)(void *cls, | ||
123 | struct | ||
124 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | ||
125 | *tc_h, | ||
126 | struct | ||
127 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue | ||
128 | *tc_queue, | ||
129 | size_t mtu); | ||
130 | |||
131 | |||
132 | /** | ||
133 | * @brief Handle an incoming message | ||
134 | * | ||
135 | * @param cls Closure | ||
136 | * @param tc_h Handle to the receiving communicator | ||
137 | * @param msg Received message | ||
138 | */ | ||
139 | typedef void | ||
140 | (*GNUNET_TRANSPORT_TESTING_IncomingMessageCallback)(void *cls, | ||
141 | struct | ||
142 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | ||
143 | *tc_h, | ||
144 | const char*payload, | ||
145 | size_t payload_len); | ||
146 | |||
147 | /** | ||
148 | * @brief Handle to a transport communicator | ||
149 | */ | ||
150 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | ||
151 | { | ||
152 | /** | ||
153 | * Clients | ||
154 | */ | ||
155 | struct MyClient *client_head; | ||
156 | struct MyClient *client_tail; | ||
157 | |||
158 | /** | ||
159 | * @brief Handle to the client | ||
160 | */ | ||
161 | struct GNUNET_MQ_Handle *c_mq; | ||
162 | |||
163 | /** | ||
164 | * @brief Handle to the configuration | ||
165 | */ | ||
166 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
167 | |||
168 | /** | ||
169 | * @brief File name of configuration file | ||
170 | */ | ||
171 | char *cfg_filename; | ||
172 | |||
173 | struct GNUNET_PeerIdentity peer_id; | ||
174 | |||
175 | /** | ||
176 | * @brief Handle to the transport service | ||
177 | */ | ||
178 | struct GNUNET_SERVICE_Handle *tsh; | ||
179 | |||
180 | /** | ||
181 | * @brief Task that will be run on shutdown to stop and clean transport | ||
182 | * service | ||
183 | */ | ||
184 | struct GNUNET_SCHEDULER_Task *ts_shutdown_task; | ||
185 | |||
186 | |||
187 | /** | ||
188 | * @brief Process of the communicator | ||
189 | */ | ||
190 | struct GNUNET_OS_Process *c_proc; | ||
191 | |||
192 | /** | ||
193 | * NAT process | ||
194 | */ | ||
195 | struct GNUNET_OS_Process *nat_proc; | ||
196 | |||
197 | /** | ||
198 | * resolver service process | ||
199 | */ | ||
200 | struct GNUNET_OS_Process *resolver_proc; | ||
201 | |||
202 | /** | ||
203 | * peerstore service process | ||
204 | */ | ||
205 | struct GNUNET_OS_Process *ps_proc; | ||
206 | |||
207 | /** | ||
208 | * @brief Task that will be run on shutdown to stop and clean communicator | ||
209 | */ | ||
210 | struct GNUNET_SCHEDULER_Task *c_shutdown_task; | ||
211 | |||
212 | /** | ||
213 | * @brief Characteristics of the communicator | ||
214 | */ | ||
215 | enum GNUNET_TRANSPORT_CommunicatorCharacteristics c_characteristics; | ||
216 | |||
217 | /** | ||
218 | * @brief Specifies supported addresses | ||
219 | */ | ||
220 | char *c_addr_prefix; | ||
221 | |||
222 | /** | ||
223 | * @brief Specifies supported addresses | ||
224 | */ | ||
225 | char *c_address; | ||
226 | |||
227 | /** | ||
228 | * @brief Head of the DLL of queues associated with this communicator | ||
229 | */ | ||
230 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *queue_head; | ||
231 | |||
232 | /** | ||
233 | * @brief Tail of the DLL of queues associated with this communicator | ||
234 | */ | ||
235 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *queue_tail; | ||
236 | |||
237 | /* Callbacks + Closures */ | ||
238 | /** | ||
239 | * @brief Callback called when a new communicator connects | ||
240 | */ | ||
241 | GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback | ||
242 | communicator_available_cb; | ||
243 | |||
244 | /** | ||
245 | * @brief Callback called when a new communicator connects | ||
246 | */ | ||
247 | GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb; | ||
248 | |||
249 | /** | ||
250 | * @brief Callback called when a new communicator connects | ||
251 | */ | ||
252 | GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb; | ||
253 | |||
254 | /** | ||
255 | * @brief Callback called when a new communicator connects | ||
256 | */ | ||
257 | GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb; | ||
258 | |||
259 | /** | ||
260 | * @brief Callback called when a new communicator connects | ||
261 | */ | ||
262 | GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_msg_cb; | ||
263 | |||
264 | /** | ||
265 | * @brief Backchannel callback | ||
266 | */ | ||
267 | GNUNET_TRANSPORT_TESTING_BackchannelCallback bc_cb; | ||
268 | |||
269 | /** | ||
270 | * Our service handle | ||
271 | */ | ||
272 | struct GNUNET_SERVICE_Handle *sh; | ||
273 | |||
274 | /** | ||
275 | * @brief Closure to the callback | ||
276 | */ | ||
277 | void *cb_cls; | ||
278 | |||
279 | /** | ||
280 | * Backchannel supported | ||
281 | */ | ||
282 | int bc_enabled; | ||
283 | }; | ||
284 | |||
285 | /** | ||
286 | * @brief Start communicator part of transport service and communicator | ||
287 | * | ||
288 | * @param service_name Name of the service | ||
289 | * @param cfg Configuration handle | ||
290 | * @param communicator_available Callback that is called when a new | ||
291 | * communicator becomes available | ||
292 | * @param add_address_cb Callback handling new addresses | ||
293 | * @param queue_create_reply_cb Callback handling success of queue requests | ||
294 | * @param add_queue_cb Callback handling freshly created queues | ||
295 | * @param incoming_message_cb Callback handling incoming messages | ||
296 | * @param cb_cls Closure to @p communicator_available | ||
297 | * | ||
298 | * @return Handle to the communicator duo | ||
299 | */ | ||
300 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle * | ||
301 | GNUNET_TRANSPORT_TESTING_transport_communicator_service_start ( | ||
302 | const char *service_name, | ||
303 | const char *binary_name, | ||
304 | const char *cfg_filename, | ||
305 | const struct GNUNET_PeerIdentity *peer_id, | ||
306 | GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback | ||
307 | communicator_available_cb, | ||
308 | GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb, | ||
309 | GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb, | ||
310 | GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb, | ||
311 | GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_message_cb, | ||
312 | GNUNET_TRANSPORT_TESTING_BackchannelCallback bc_cb, | ||
313 | void *cb_cls); | ||
314 | |||
315 | |||
316 | void | ||
317 | GNUNET_TRANSPORT_TESTING_transport_communicator_service_stop ( | ||
318 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h); | ||
319 | |||
320 | |||
321 | /** | ||
322 | * @brief Instruct communicator to open a queue | ||
323 | * | ||
324 | * @param tc_h Handle to communicator which shall open queue | ||
325 | * @param peer_id Towards which peer | ||
326 | * @param address For which address | ||
327 | */ | ||
328 | void | ||
329 | GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (struct | ||
330 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | ||
331 | *tc_h, | ||
332 | const struct | ||
333 | GNUNET_PeerIdentity | ||
334 | *peer_id, | ||
335 | const char *address); | ||
336 | |||
337 | |||
338 | /** | ||
339 | * @brief Instruct communicator to send data | ||
340 | * | ||
341 | * @param tc_queue The queue to use for sending | ||
342 | * @param cont function to call when done sending | ||
343 | * @param cont_cls closure for @a cont | ||
344 | * @param payload Data to send | ||
345 | * @param payload_size Size of the @a payload | ||
346 | */ | ||
347 | void | ||
348 | GNUNET_TRANSPORT_TESTING_transport_communicator_send (struct | ||
349 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | ||
350 | *tc_h, | ||
351 | GNUNET_SCHEDULER_TaskCallback | ||
352 | cont, | ||
353 | void *cont_cls, | ||
354 | const void *payload, | ||
355 | size_t payload_size); | ||
diff --git a/src/transport/transport-testing2.c b/src/transport/transport-testing2.c index 076fbf331..ca338c52f 100644 --- a/src/transport/transport-testing2.c +++ b/src/transport/transport-testing2.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2019 GNUnet e.V. | 3 | Copyright (C) 2006, 2009, 2015, 2016 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 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 | 6 | under the terms of the GNU Affero General Public License as published |
@@ -17,1148 +17,907 @@ | |||
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | |||
21 | /** | 20 | /** |
22 | * @file transport/transport-testing2.c | 21 | * @file transport-testing.c |
23 | * @brief functions related to testing-tng | 22 | * @brief testing lib for transport service |
23 | * @author Matthias Wachs | ||
24 | * @author Christian Grothoff | 24 | * @author Christian Grothoff |
25 | * @author Julius Bünger | ||
26 | */ | 25 | */ |
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_protocols.h" | ||
30 | #include "gnunet_constants.h" | ||
31 | #include "transport-testing2.h" | 26 | #include "transport-testing2.h" |
32 | #include "gnunet_ats_transport_service.h" | ||
33 | #include "gnunet_hello_lib.h" | ||
34 | #include "gnunet_signatures.h" | ||
35 | #include "transport.h" | ||
36 | #include <inttypes.h> | ||
37 | |||
38 | #define LOG(kind, ...) GNUNET_log_from (kind, "transport-testing2", __VA_ARGS__) | ||
39 | |||
40 | struct MyClient | ||
41 | { | ||
42 | struct MyClient *prev; | ||
43 | struct MyClient *next; | ||
44 | /** | ||
45 | * @brief Handle to the client | ||
46 | */ | ||
47 | struct GNUNET_SERVICE_Client *client; | ||
48 | |||
49 | /** | ||
50 | * @brief Handle to the client | ||
51 | */ | ||
52 | struct GNUNET_MQ_Handle *c_mq; | ||
53 | |||
54 | /** | ||
55 | * The TCH | ||
56 | */ | ||
57 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc; | ||
58 | |||
59 | }; | ||
60 | |||
61 | /** | ||
62 | * @brief Queue of a communicator and some context | ||
63 | */ | ||
64 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue | ||
65 | { | ||
66 | /** | ||
67 | * @brief Handle to the TransportCommunicator | ||
68 | */ | ||
69 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h; | ||
70 | |||
71 | /** | ||
72 | * @brief Envelope to a message that requests the opening of the queue. | ||
73 | * | ||
74 | * If the client already requests queue(s), but the communicator is not yet | ||
75 | * connected, we cannot send the request to open the queue. Save it until the | ||
76 | * communicator becomes available and send it then. | ||
77 | */ | ||
78 | struct GNUNET_MQ_Envelope *open_queue_env; | ||
79 | |||
80 | /** | ||
81 | * @brief Peer ID of the peer on the other side of the queue | ||
82 | */ | ||
83 | struct GNUNET_PeerIdentity peer_id; | ||
84 | |||
85 | /** | ||
86 | * @brief Queue ID | ||
87 | */ | ||
88 | uint32_t qid; | ||
89 | |||
90 | /** | ||
91 | * @brief Current message id | ||
92 | */ | ||
93 | uint64_t mid; | ||
94 | |||
95 | /** | ||
96 | * An `enum GNUNET_NetworkType` in NBO. | ||
97 | */ | ||
98 | uint32_t nt; | ||
99 | |||
100 | /** | ||
101 | * Maximum transmission unit. UINT32_MAX for unlimited. | ||
102 | */ | ||
103 | uint32_t mtu; | ||
104 | |||
105 | /** | ||
106 | * Queue length. UINT64_MAX for unlimited. | ||
107 | */ | ||
108 | uint64_t q_len; | ||
109 | |||
110 | /** | ||
111 | * Queue prio | ||
112 | */ | ||
113 | uint32_t priority; | ||
114 | |||
115 | /** | ||
116 | * An `enum GNUNET_TRANSPORT_ConnectionStatus` in NBO. | ||
117 | */ | ||
118 | uint32_t cs; | ||
119 | |||
120 | /** | ||
121 | * @brief Next element inside a DLL | ||
122 | */ | ||
123 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *next; | ||
124 | |||
125 | /** | ||
126 | * @brief Previous element inside a DLL | ||
127 | */ | ||
128 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *prev; | ||
129 | }; | ||
130 | 27 | ||
131 | 28 | ||
132 | /** | 29 | #define LOG(kind, ...) GNUNET_log_from (kind, "transport-testing", __VA_ARGS__) |
133 | * @brief Handle/Context to a single transmission | ||
134 | */ | ||
135 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission | ||
136 | { | ||
137 | }; | ||
138 | 30 | ||
139 | 31 | ||
140 | /** | 32 | static struct GNUNET_TRANSPORT_TESTING_PeerContext * |
141 | * @brief Check whether incoming msg indicating available communicator is | 33 | find_peer_context (struct GNUNET_TRANSPORT_TESTING_Handle *tth, |
142 | * correct | 34 | const struct GNUNET_PeerIdentity *peer) |
143 | * | ||
144 | * @param cls Closure | ||
145 | * @param msg Message struct | ||
146 | * | ||
147 | * @return GNUNET_YES in case message is correct | ||
148 | */ | ||
149 | static int | ||
150 | check_communicator_available ( | ||
151 | void *cls, | ||
152 | const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg) | ||
153 | { | 35 | { |
154 | uint16_t size; | 36 | struct GNUNET_TRANSPORT_TESTING_PeerContext *t; |
155 | 37 | ||
156 | size = ntohs (msg->header.size) - sizeof(*msg); | 38 | for (t = tth->p_head; NULL != t; t = t->next) |
157 | if (0 == size) | 39 | if (0 == memcmp (&t->id, |
158 | return GNUNET_OK; /* receive-only communicator */ | 40 | peer, |
159 | GNUNET_MQ_check_zero_termination (msg); | 41 | sizeof(struct GNUNET_PeerIdentity))) |
160 | return GNUNET_OK; | 42 | return t; |
43 | return NULL; | ||
161 | } | 44 | } |
162 | 45 | ||
163 | 46 | ||
164 | /** | 47 | /** |
165 | * @brief Handle new communicator | 48 | * Find any connecting context matching the given pair of peers. |
166 | * | ||
167 | * Store characteristics of communicator, call respective client callback. | ||
168 | * | 49 | * |
169 | * @param cls Closure - communicator handle | 50 | * @param p1 first peer |
170 | * @param msg Message struct | 51 | * @param p2 second peer |
52 | * @param cb function to call | ||
53 | * @param cb_cls closure for @a cb | ||
171 | */ | 54 | */ |
172 | static void | 55 | void |
173 | handle_communicator_available ( | 56 | GNUNET_TRANSPORT_TESTING_find_connecting_context (struct |
174 | void *cls, | 57 | GNUNET_TRANSPORT_TESTING_PeerContext |
175 | const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg) | 58 | *p1, |
176 | { | 59 | struct |
177 | struct MyClient *client = cls; | 60 | GNUNET_TRANSPORT_TESTING_PeerContext |
178 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | 61 | *p2, |
179 | client->tc; | 62 | GNUNET_TRANSPORT_TESTING_ConnectContextCallback |
180 | uint16_t size; | 63 | cb, |
181 | tc_h->c_mq = client->c_mq; | 64 | void *cb_cls) |
182 | 65 | { | |
183 | size = ntohs (msg->header.size) - sizeof(*msg); | 66 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth; |
184 | if (0 == size) | 67 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; |
185 | { | 68 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; |
186 | GNUNET_SERVICE_client_continue (client->client); | 69 | |
187 | return; /* receive-only communicator */ | 70 | for (cc = tth->cc_head; NULL != cc; cc = ccn) |
188 | } | ||
189 | tc_h->c_characteristics = ntohl (msg->cc); | ||
190 | tc_h->c_addr_prefix = GNUNET_strdup ((const char *) &msg[1]); | ||
191 | if (NULL != tc_h->communicator_available_cb) | ||
192 | { | 71 | { |
193 | LOG (GNUNET_ERROR_TYPE_DEBUG, "calling communicator_available_cb()\n"); | 72 | ccn = cc->next; |
194 | tc_h->communicator_available_cb (tc_h->cb_cls, | 73 | if ((cc->p1 == p1) && |
195 | tc_h, | 74 | (cc->p2 == p2)) |
196 | tc_h->c_characteristics, | 75 | cb (cb_cls, |
197 | tc_h->c_addr_prefix); | 76 | cc); |
198 | } | 77 | } |
199 | GNUNET_SERVICE_client_continue (client->client); | ||
200 | LOG (GNUNET_ERROR_TYPE_DEBUG, "finished communicator_available_cb()\n"); | ||
201 | |||
202 | } | 78 | } |
203 | 79 | ||
204 | 80 | ||
205 | /** | 81 | static void |
206 | * Incoming message. Test message is well-formed. | 82 | set_p1c (void *cls, |
207 | * | 83 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) |
208 | * @param cls the client | ||
209 | * @param msg the send message that was sent | ||
210 | * @return #GNUNET_OK if message is well-formed | ||
211 | */ | ||
212 | static int | ||
213 | check_communicator_backchannel (void *cls, | ||
214 | const struct | ||
215 | GNUNET_TRANSPORT_CommunicatorBackchannel *msg) | ||
216 | { | 84 | { |
217 | // struct TransportClient *tc = cls; | 85 | int *found = cls; |
218 | 86 | ||
219 | // if (CT_COMMUNICATOR != tc->type) | 87 | if (NULL != found) |
220 | // { | 88 | *found = GNUNET_YES; |
221 | // GNUNET_break (0); | 89 | cx->p1_c = GNUNET_YES; |
222 | // return GNUNET_SYSERR; | ||
223 | // } | ||
224 | // GNUNET_MQ_check_boxed_message (msg); | ||
225 | return GNUNET_OK; | ||
226 | } | 90 | } |
227 | 91 | ||
228 | 92 | ||
229 | /** | ||
230 | * @brief Receive an incoming message. | ||
231 | * | ||
232 | * Pass the message to the client. | ||
233 | * | ||
234 | * @param cls Closure - communicator handle | ||
235 | * @param msg Message | ||
236 | */ | ||
237 | static void | 93 | static void |
238 | handle_communicator_backchannel (void *cls, | 94 | set_mq (void *cls, |
239 | const struct | 95 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) |
240 | GNUNET_TRANSPORT_CommunicatorBackchannel * | ||
241 | bc_msg) | ||
242 | { | 96 | { |
243 | struct MyClient *client = cls; | 97 | struct GNUNET_MQ_Handle *mq = cls; |
244 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
245 | client->tc; | ||
246 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *other_tc_h; | ||
247 | struct GNUNET_MessageHeader *msg; | ||
248 | msg = (struct GNUNET_MessageHeader *) &bc_msg[1]; | ||
249 | uint16_t isize = ntohs (msg->size); | ||
250 | const char *target_communicator = ((const char *) msg) + isize; | ||
251 | struct GNUNET_TRANSPORT_CommunicatorBackchannelIncoming *cbi; | ||
252 | struct GNUNET_MQ_Envelope *env; | ||
253 | 98 | ||
254 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 99 | cx->mq = mq; |
255 | "Received backchannel message\n"); | ||
256 | if (tc_h->bc_enabled != GNUNET_YES) | ||
257 | { | ||
258 | GNUNET_SERVICE_client_continue (client->client); | ||
259 | return; | ||
260 | } | ||
261 | /* Find client providing this communicator */ | ||
262 | /* Finally, deliver backchannel message to communicator */ | ||
263 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
264 | "Delivering backchannel message of type %u to %s\n", | ||
265 | ntohs (msg->type), | ||
266 | target_communicator); | ||
267 | other_tc_h = tc_h->bc_cb (tc_h, msg, (struct | ||
268 | GNUNET_PeerIdentity*) &bc_msg->pid); | ||
269 | env = GNUNET_MQ_msg_extra ( | ||
270 | cbi, | ||
271 | isize, | ||
272 | GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL_INCOMING); | ||
273 | cbi->pid = tc_h->peer_id; | ||
274 | memcpy (&cbi[1], msg, isize); | ||
275 | |||
276 | |||
277 | GNUNET_MQ_send (other_tc_h->c_mq, env); | ||
278 | GNUNET_SERVICE_client_continue (client->client); | ||
279 | } | 100 | } |
280 | 101 | ||
281 | 102 | ||
282 | /** | 103 | static void |
283 | * Address of our peer added. Test message is well-formed. | 104 | set_p2c (void *cls, |
284 | * | 105 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) |
285 | * @param cls the client | ||
286 | * @param aam the send message that was sent | ||
287 | * @return #GNUNET_OK if message is well-formed | ||
288 | */ | ||
289 | static int | ||
290 | check_add_address (void *cls, | ||
291 | const struct GNUNET_TRANSPORT_AddAddressMessage *msg) | ||
292 | { | 106 | { |
293 | // if (CT_COMMUNICATOR != tc->type) | 107 | int *found = cls; |
294 | // { | 108 | |
295 | // GNUNET_break (0); | 109 | if (NULL != found) |
296 | // return GNUNET_SYSERR; | 110 | *found = GNUNET_YES; |
297 | // } | 111 | cx->p2_c = GNUNET_YES; |
298 | GNUNET_MQ_check_zero_termination (msg); | ||
299 | return GNUNET_OK; | ||
300 | } | 112 | } |
301 | 113 | ||
302 | 114 | ||
303 | /** | ||
304 | * @brief The communicator informs about an address. | ||
305 | * | ||
306 | * Store address and call client callback. | ||
307 | * | ||
308 | * @param cls Closure - communicator handle | ||
309 | * @param msg Message | ||
310 | */ | ||
311 | static void | 115 | static void |
312 | handle_add_address (void *cls, | 116 | clear_p1c (void *cls, |
313 | const struct GNUNET_TRANSPORT_AddAddressMessage *msg) | 117 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) |
314 | { | 118 | { |
315 | struct MyClient *client = cls; | 119 | int *found = cls; |
316 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | 120 | |
317 | client->tc; | 121 | if (NULL != found) |
318 | uint16_t size; | 122 | *found = GNUNET_YES; |
319 | size = ntohs (msg->header.size) - sizeof(*msg); | 123 | cx->p1_c = GNUNET_NO; |
320 | if (0 == size) | ||
321 | return; /* receive-only communicator */ | ||
322 | LOG (GNUNET_ERROR_TYPE_DEBUG, "received add address cb %u\n", size); | ||
323 | tc_h->c_address = GNUNET_strdup ((const char *) &msg[1]); | ||
324 | if (NULL != tc_h->add_address_cb) | ||
325 | { | ||
326 | LOG (GNUNET_ERROR_TYPE_DEBUG, "calling add_address_cb()\n"); | ||
327 | tc_h->add_address_cb (tc_h->cb_cls, | ||
328 | tc_h, | ||
329 | tc_h->c_address, | ||
330 | GNUNET_TIME_relative_ntoh (msg->expiration), | ||
331 | msg->aid, | ||
332 | ntohl (msg->nt)); | ||
333 | } | ||
334 | GNUNET_SERVICE_client_continue (client->client); | ||
335 | } | 124 | } |
336 | 125 | ||
337 | 126 | ||
338 | /** | 127 | static void |
339 | * Incoming message. Test message is well-formed. | 128 | clear_p2c (void *cls, |
340 | * | 129 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) |
341 | * @param cls the client | ||
342 | * @param msg the send message that was sent | ||
343 | * @return #GNUNET_OK if message is well-formed | ||
344 | */ | ||
345 | static int | ||
346 | check_incoming_msg (void *cls, | ||
347 | const struct GNUNET_TRANSPORT_IncomingMessage *msg) | ||
348 | { | 130 | { |
349 | // struct TransportClient *tc = cls; | 131 | int *found = cls; |
350 | 132 | ||
351 | // if (CT_COMMUNICATOR != tc->type) | 133 | if (NULL != found) |
352 | // { | 134 | *found = GNUNET_YES; |
353 | // GNUNET_break (0); | 135 | cx->p2_c = GNUNET_NO; |
354 | // return GNUNET_SYSERR; | ||
355 | // } | ||
356 | GNUNET_MQ_check_boxed_message (msg); | ||
357 | return GNUNET_OK; | ||
358 | } | 136 | } |
359 | 137 | ||
360 | 138 | ||
361 | /** | 139 | static void * |
362 | * @brief Receive an incoming message. | 140 | notify_connect (void *cls, |
363 | * | 141 | const struct GNUNET_PeerIdentity *peer, |
364 | * Pass the message to the client. | 142 | struct GNUNET_MQ_Handle *mq) |
365 | * | 143 | { |
366 | * @param cls Closure - communicator handle | 144 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; |
367 | * @param msg Message | 145 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth; |
368 | */ | 146 | char *p2_s; |
369 | static void | 147 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2; |
370 | handle_incoming_msg (void *cls, | 148 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; |
371 | const struct GNUNET_TRANSPORT_IncomingMessage *inc_msg) | 149 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; |
372 | { | 150 | int found; |
373 | struct MyClient *client = cls; | 151 | void *ret; |
374 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | 152 | |
375 | client->tc; | 153 | p2 = find_peer_context (p->tth, |
376 | struct GNUNET_MessageHeader *msg; | 154 | peer); |
377 | msg = (struct GNUNET_MessageHeader *) &inc_msg[1]; | 155 | if (NULL != p->nc) |
378 | size_t payload_len = ntohs (msg->size) - sizeof (struct | 156 | ret = p->nc (p->cb_cls, |
379 | GNUNET_MessageHeader); | 157 | peer, |
380 | if (NULL != tc_h->incoming_msg_cb) | 158 | mq); |
159 | else | ||
160 | ret = NULL; | ||
161 | |||
162 | if (NULL != p2) | ||
163 | GNUNET_asprintf (&p2_s, | ||
164 | "%u (`%s')", | ||
165 | p2->no, | ||
166 | GNUNET_i2s (&p2->id)); | ||
167 | else | ||
168 | GNUNET_asprintf (&p2_s, | ||
169 | "`%s'", | ||
170 | GNUNET_i2s (peer)); | ||
171 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
172 | "Peers %s connected to peer %u (`%s')\n", | ||
173 | p2_s, | ||
174 | p->no, | ||
175 | GNUNET_i2s (&p->id)); | ||
176 | GNUNET_free (p2_s); | ||
177 | /* update flags in connecting contexts */ | ||
178 | found = GNUNET_NO; | ||
179 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p, | ||
180 | p2, | ||
181 | &set_p1c, | ||
182 | &found); | ||
183 | if (GNUNET_NO == found) | ||
381 | { | 184 | { |
382 | tc_h->incoming_msg_cb (tc_h->cb_cls, | 185 | cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest); |
383 | tc_h, | 186 | cc->p1 = p; |
384 | (char*) &msg[1], | 187 | cc->p2 = p2; |
385 | payload_len); | 188 | cc->p1_c = GNUNET_YES; |
189 | GNUNET_CONTAINER_DLL_insert (tth->cc_head, | ||
190 | tth->cc_tail, | ||
191 | cc); | ||
386 | } | 192 | } |
387 | else | 193 | found = GNUNET_NO; |
194 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p2, | ||
195 | p, | ||
196 | &set_p2c, | ||
197 | &found); | ||
198 | if (GNUNET_NO == found) | ||
388 | { | 199 | { |
389 | LOG (GNUNET_ERROR_TYPE_WARNING, | 200 | cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest); |
390 | "Incoming message from communicator but no handler!\n"); | 201 | cc->p1 = p2; |
202 | cc->p2 = p; | ||
203 | cc->p1_c = GNUNET_YES; | ||
204 | GNUNET_CONTAINER_DLL_insert (tth->cc_head, | ||
205 | tth->cc_tail, | ||
206 | cc); | ||
391 | } | 207 | } |
392 | if (GNUNET_YES == ntohl (inc_msg->fc_on)) | 208 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p, |
209 | p2, | ||
210 | &set_mq, | ||
211 | mq); | ||
212 | /* update set connected flag for all requests */ | ||
213 | for (cc = tth->cc_head; NULL != cc; cc = cc->next) | ||
393 | { | 214 | { |
394 | /* send ACK when done to communicator for flow control! */ | 215 | if (GNUNET_YES == cc->connected) |
395 | struct GNUNET_MQ_Envelope *env; | 216 | continue; |
396 | struct GNUNET_TRANSPORT_IncomingMessageAck *ack; | 217 | if ((GNUNET_YES == cc->p1_c) && |
397 | 218 | (GNUNET_YES == cc->p2_c)) | |
398 | env = GNUNET_MQ_msg (ack, GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG_ACK); | 219 | { |
399 | GNUNET_assert (NULL != env); | 220 | cc->connected = GNUNET_YES; |
400 | ack->reserved = htonl (0); | 221 | /* stop trying to connect */ |
401 | ack->fc_id = inc_msg->fc_id; | 222 | if (NULL != cc->tct) |
402 | ack->sender = inc_msg->sender; | 223 | { |
403 | GNUNET_MQ_send (tc_h->c_mq, env); | 224 | GNUNET_SCHEDULER_cancel (cc->tct); |
225 | cc->tct = NULL; | ||
226 | } | ||
227 | if (NULL != cc->ats_sh) | ||
228 | { | ||
229 | GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh); | ||
230 | cc->ats_sh = NULL; | ||
231 | } | ||
232 | } | ||
404 | } | 233 | } |
405 | 234 | /* then notify application */ | |
406 | GNUNET_SERVICE_client_continue (client->client); | 235 | for (cc = tth->cc_head; NULL != cc; cc = ccn) |
236 | { | ||
237 | ccn = cc->next; | ||
238 | if ((GNUNET_YES == cc->connected) && | ||
239 | (NULL != cc->cb)) | ||
240 | { | ||
241 | cc->cb (cc->cb_cls); | ||
242 | cc->cb = NULL; /* only notify once! */ | ||
243 | } | ||
244 | } | ||
245 | return ret; | ||
407 | } | 246 | } |
408 | 247 | ||
409 | 248 | ||
410 | /** | 249 | /** |
411 | * @brief Communicator informs that it tries to establish requested queue | 250 | * Offer the current HELLO of P2 to P1. |
412 | * | 251 | * |
413 | * @param cls Closure - communicator handle | 252 | * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest` |
414 | * @param msg Message | ||
415 | */ | 253 | */ |
416 | static void | 254 | static void |
417 | handle_queue_create_ok (void *cls, | 255 | offer_hello (void *cls); |
418 | const struct GNUNET_TRANSPORT_CreateQueueResponse *msg) | 256 | |
419 | { | ||
420 | struct MyClient *client = cls; | ||
421 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
422 | client->tc; | ||
423 | 257 | ||
424 | if (NULL != tc_h->queue_create_reply_cb) | 258 | static void |
259 | notify_disconnect (void *cls, | ||
260 | const struct GNUNET_PeerIdentity *peer, | ||
261 | void *handler_cls) | ||
262 | { | ||
263 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; | ||
264 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth; | ||
265 | char *p2_s; | ||
266 | /* Find PeerContext */ | ||
267 | int no = 0; | ||
268 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = NULL; | ||
269 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
270 | |||
271 | p2 = find_peer_context (p->tth, | ||
272 | peer); | ||
273 | no = p->no; | ||
274 | if (NULL != p2) | ||
275 | GNUNET_asprintf (&p2_s, | ||
276 | "%u (`%s')", | ||
277 | p2->no, | ||
278 | GNUNET_i2s (&p2->id)); | ||
279 | else | ||
280 | GNUNET_asprintf (&p2_s, | ||
281 | "`%s'", | ||
282 | GNUNET_i2s (peer)); | ||
283 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
284 | "Peers %s disconnected from peer %u (`%s')\n", | ||
285 | p2_s, | ||
286 | no, | ||
287 | GNUNET_i2s (&p->id)); | ||
288 | GNUNET_free (p2_s); | ||
289 | /* notify about disconnect */ | ||
290 | if (NULL != p->nd) | ||
291 | p->nd (p->cb_cls, | ||
292 | peer, | ||
293 | handler_cls); | ||
294 | if (NULL == p2) | ||
295 | return; | ||
296 | /* clear MQ, it is now invalid */ | ||
297 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p, | ||
298 | p2, | ||
299 | &set_mq, | ||
300 | NULL); | ||
301 | /* update set connected flags for all requests */ | ||
302 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p, | ||
303 | p2, | ||
304 | &clear_p1c, | ||
305 | NULL); | ||
306 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p2, | ||
307 | p, | ||
308 | &clear_p2c, | ||
309 | NULL); | ||
310 | /* resume connectivity requests as necessary */ | ||
311 | for (cc = tth->cc_head; NULL != cc; cc = cc->next) | ||
425 | { | 312 | { |
426 | tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_YES); | 313 | if (GNUNET_NO == cc->connected) |
314 | continue; | ||
315 | if ((GNUNET_YES != cc->p1_c) || | ||
316 | (GNUNET_YES != cc->p2_c)) | ||
317 | { | ||
318 | cc->connected = GNUNET_NO; | ||
319 | /* start trying to connect */ | ||
320 | if (NULL == cc->tct) | ||
321 | cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello, | ||
322 | cc); | ||
323 | if (NULL == cc->ats_sh) | ||
324 | cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats, | ||
325 | &p2->id, | ||
326 | 1); | ||
327 | } | ||
427 | } | 328 | } |
428 | GNUNET_SERVICE_client_continue (client->client); | ||
429 | } | 329 | } |
430 | 330 | ||
431 | 331 | ||
432 | /** | ||
433 | * @brief Communicator informs that it wont try establishing requested queue. | ||
434 | * | ||
435 | * It will not do so probably because the address is bougus (see comment to | ||
436 | * #GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL) | ||
437 | * | ||
438 | * @param cls Closure - communicator handle | ||
439 | * @param msg Message | ||
440 | */ | ||
441 | static void | 332 | static void |
442 | handle_queue_create_fail ( | 333 | hello_iter_cb (void *cb_cls, |
443 | void *cls, | 334 | const struct GNUNET_PEERSTORE_Record *record, |
444 | const struct GNUNET_TRANSPORT_CreateQueueResponse *msg) | 335 | const char *emsg) |
445 | { | 336 | { |
446 | struct MyClient *client = cls; | 337 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cb_cls; |
447 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | 338 | //Check record type et al? |
448 | client->tc; | 339 | p->hello = GNUNET_malloc (record->value_size); |
340 | memcpy (p->hello, record->value, record->value_size); | ||
449 | 341 | ||
450 | if (NULL != tc_h->queue_create_reply_cb) | 342 | GNUNET_PEERSTORE_iterate_cancel (p->pic); |
343 | if (NULL != p->start_cb) | ||
451 | { | 344 | { |
452 | tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_NO); | 345 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
346 | "Peer %u (`%s') successfully started\n", | ||
347 | p->no, | ||
348 | GNUNET_i2s (&p->id)); | ||
349 | p->start_cb (p->start_cb_cls); | ||
350 | p->start_cb = NULL; | ||
453 | } | 351 | } |
454 | GNUNET_SERVICE_client_continue (client->client); | ||
455 | } | 352 | } |
456 | 353 | ||
457 | 354 | ||
458 | /** | 355 | /** |
459 | * New queue became available. Check message. | 356 | * Start a peer with the given configuration |
460 | * | 357 | * @param tth the testing handle |
461 | * @param cls the client | 358 | * @param cfgname configuration file |
462 | * @param aqm the send message that was sent | 359 | * @param peer_id a unique number to identify the peer |
360 | * @param handlers functions for receiving messages | ||
361 | * @param nc connect callback | ||
362 | * @param nd disconnect callback | ||
363 | * @param cb_cls closure for callback | ||
364 | * @param start_cb start callback | ||
365 | * @param start_cb_cls closure for callback | ||
366 | * @return the peer context | ||
463 | */ | 367 | */ |
464 | static int | 368 | struct GNUNET_TRANSPORT_TESTING_PeerContext * |
465 | check_add_queue_message (void *cls, | 369 | GNUNET_TRANSPORT_TESTING_start_peer (struct |
466 | const struct GNUNET_TRANSPORT_AddQueueMessage *aqm) | 370 | GNUNET_TRANSPORT_TESTING_Handle *tth, |
467 | { | 371 | const char *cfgname, |
468 | GNUNET_MQ_check_zero_termination (aqm); | 372 | int peer_id, |
469 | return GNUNET_OK; | 373 | const struct |
470 | } | 374 | GNUNET_MQ_MessageHandler *handlers, |
471 | 375 | GNUNET_TRANSPORT_NotifyConnect nc, | |
472 | 376 | GNUNET_TRANSPORT_NotifyDisconnect nd, | |
473 | /** | 377 | void *cb_cls, |
474 | * @brief Handle new queue | 378 | GNUNET_SCHEDULER_TaskCallback start_cb, |
475 | * | 379 | void *start_cb_cls) |
476 | * Store context and call client callback. | 380 | { |
477 | * | 381 | char *emsg = NULL; |
478 | * @param cls Closure - communicator handle | 382 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p; |
479 | * @param msg Message struct | 383 | struct GNUNET_PeerIdentity dummy; |
480 | */ | 384 | unsigned int i; |
481 | static void | 385 | |
482 | handle_add_queue_message (void *cls, | 386 | if (GNUNET_NO == GNUNET_DISK_file_test (cfgname)) |
483 | const struct GNUNET_TRANSPORT_AddQueueMessage *msg) | ||
484 | { | ||
485 | struct MyClient *client = cls; | ||
486 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
487 | client->tc; | ||
488 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | ||
489 | |||
490 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
491 | "Got queue with ID %u\n", msg->qid); | ||
492 | for (tc_queue = tc_h->queue_head; NULL != tc_queue; tc_queue = tc_queue->next) | ||
493 | { | 387 | { |
494 | if (tc_queue->qid == msg->qid) | 388 | LOG (GNUNET_ERROR_TYPE_ERROR, |
495 | break; | 389 | "File not found: `%s'\n", |
390 | cfgname); | ||
391 | return NULL; | ||
496 | } | 392 | } |
497 | if (NULL == tc_queue) | 393 | |
394 | p = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_PeerContext); | ||
395 | p->tth = tth; | ||
396 | p->nc = nc; | ||
397 | p->nd = nd; | ||
398 | if (NULL != handlers) | ||
498 | { | 399 | { |
499 | tc_queue = | 400 | for (i = 0; NULL != handlers[i].cb; i++) |
500 | GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue); | 401 | ; |
501 | tc_queue->tc_h = tc_h; | 402 | p->handlers = GNUNET_new_array (i + 1, |
502 | tc_queue->qid = msg->qid; | 403 | struct GNUNET_MQ_MessageHandler); |
503 | tc_queue->peer_id = msg->receiver; | 404 | GNUNET_memcpy (p->handlers, |
504 | GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue); | 405 | handlers, |
406 | i * sizeof(struct GNUNET_MQ_MessageHandler)); | ||
505 | } | 407 | } |
506 | GNUNET_assert (tc_queue->qid == msg->qid); | 408 | if (NULL != cb_cls) |
507 | GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver)); | 409 | p->cb_cls = cb_cls; |
508 | tc_queue->nt = msg->nt; | 410 | else |
509 | tc_queue->mtu = ntohl (msg->mtu); | 411 | p->cb_cls = p; |
510 | tc_queue->cs = msg->cs; | 412 | p->start_cb = start_cb; |
511 | tc_queue->priority = ntohl (msg->priority); | 413 | if (NULL != start_cb_cls) |
512 | tc_queue->q_len = GNUNET_ntohll (msg->q_len); | 414 | p->start_cb_cls = start_cb_cls; |
513 | if (NULL != tc_h->add_queue_cb) | 415 | else |
416 | p->start_cb_cls = p; | ||
417 | GNUNET_CONTAINER_DLL_insert (tth->p_head, | ||
418 | tth->p_tail, | ||
419 | p); | ||
420 | |||
421 | /* Create configuration and call testing lib to modify it */ | ||
422 | p->cfg = GNUNET_CONFIGURATION_create (); | ||
423 | GNUNET_assert (GNUNET_OK == | ||
424 | GNUNET_CONFIGURATION_load (p->cfg, cfgname)); | ||
425 | if (GNUNET_SYSERR == | ||
426 | GNUNET_TESTING_configuration_create (tth->tl_system, | ||
427 | p->cfg)) | ||
514 | { | 428 | { |
515 | tc_h->add_queue_cb (tc_h->cb_cls, tc_h, tc_queue, tc_queue->mtu); | 429 | LOG (GNUNET_ERROR_TYPE_ERROR, |
430 | "Testing library failed to create unique configuration based on `%s'\n", | ||
431 | cfgname); | ||
432 | GNUNET_CONFIGURATION_destroy (p->cfg); | ||
433 | GNUNET_free (p); | ||
434 | return NULL; | ||
516 | } | 435 | } |
517 | GNUNET_SERVICE_client_continue (client->client); | ||
518 | } | ||
519 | 436 | ||
437 | p->no = peer_id; | ||
438 | /* Configure peer with configuration */ | ||
439 | p->peer = GNUNET_TESTING_peer_configure (tth->tl_system, | ||
440 | p->cfg, | ||
441 | p->no, | ||
442 | NULL, | ||
443 | &emsg); | ||
444 | if (NULL == p->peer) | ||
445 | { | ||
446 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
447 | "Testing library failed to create unique configuration based on `%s': `%s'\n", | ||
448 | cfgname, | ||
449 | emsg); | ||
450 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
451 | GNUNET_free (emsg); | ||
452 | return NULL; | ||
453 | } | ||
454 | GNUNET_free (emsg); | ||
455 | if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer)) | ||
456 | { | ||
457 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
458 | "Testing library failed to create unique configuration based on `%s'\n", | ||
459 | cfgname); | ||
460 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
461 | return NULL; | ||
462 | } | ||
520 | 463 | ||
521 | /** | 464 | memset (&dummy, |
522 | * @brief Handle new queue | 465 | '\0', |
523 | * | 466 | sizeof(dummy)); |
524 | * Store context and call client callback. | 467 | GNUNET_TESTING_peer_get_identity (p->peer, |
525 | * | 468 | &p->id); |
526 | * @param cls Closure - communicator handle | 469 | if (0 == memcmp (&dummy, |
527 | * @param msg Message struct | 470 | &p->id, |
528 | */ | 471 | sizeof(struct GNUNET_PeerIdentity))) |
529 | static void | 472 | { |
530 | handle_update_queue_message (void *cls, | 473 | LOG (GNUNET_ERROR_TYPE_ERROR, |
531 | const struct | 474 | "Testing library failed to obtain peer identity for peer %u\n", |
532 | GNUNET_TRANSPORT_UpdateQueueMessage *msg) | 475 | p->no); |
533 | { | 476 | GNUNET_TRANSPORT_TESTING_stop_peer (p); |
534 | struct MyClient *client = cls; | 477 | return NULL; |
535 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | 478 | } |
536 | client->tc; | ||
537 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | ||
538 | |||
539 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 479 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
540 | "Received queue update message for %u with q_len %" PRIu64 "\n", | 480 | "Peer %u configured with identity `%s'\n", |
541 | msg->qid, GNUNET_ntohll (msg->q_len)); | 481 | p->no, |
542 | tc_queue = tc_h->queue_head; | 482 | GNUNET_i2s_full (&p->id)); |
543 | if (NULL != tc_queue) | 483 | p->tmh = GNUNET_TRANSPORT_manipulation_connect (p->cfg); |
484 | p->th = GNUNET_TRANSPORT_core_connect (p->cfg, | ||
485 | NULL, | ||
486 | handlers, | ||
487 | p, | ||
488 | ¬ify_connect, | ||
489 | ¬ify_disconnect); | ||
490 | if ((NULL == p->th) || | ||
491 | (NULL == p->tmh)) | ||
544 | { | 492 | { |
545 | while (tc_queue->qid != msg->qid) | 493 | LOG (GNUNET_ERROR_TYPE_ERROR, |
546 | { | 494 | "Failed to connect to transport service for peer `%s': `%s'\n", |
547 | tc_queue = tc_queue->next; | 495 | cfgname, |
548 | } | 496 | emsg); |
497 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
498 | return NULL; | ||
499 | } | ||
500 | p->ats = GNUNET_ATS_connectivity_init (p->cfg); | ||
501 | if (NULL == p->ats) | ||
502 | { | ||
503 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
504 | "Failed to connect to ATS service for peer `%s': `%s'\n", | ||
505 | cfgname, | ||
506 | emsg); | ||
507 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
508 | return NULL; | ||
549 | } | 509 | } |
550 | GNUNET_assert (tc_queue->qid == msg->qid); | 510 | p->pic = GNUNET_PEERSTORE_iterate (p->ph, |
551 | GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver)); | 511 | "transport", |
552 | tc_queue->nt = msg->nt; | 512 | &p->id, |
553 | tc_queue->mtu = ntohl (msg->mtu); | 513 | GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY, |
554 | tc_queue->cs = msg->cs; | 514 | hello_iter_cb, |
555 | tc_queue->priority = ntohl (msg->priority); | 515 | p); |
556 | // Uncomment this for alternativ 1 of backchannel functionality | 516 | GNUNET_assert (NULL != p->pic); |
557 | tc_queue->q_len += GNUNET_ntohll (msg->q_len); | 517 | |
558 | // Until here for alternativ 1 | 518 | return p; |
559 | // Uncomment this for alternativ 2 of backchannel functionality | ||
560 | // tc_queue->q_len = GNUNET_ntohll (msg->q_len); | ||
561 | // Until here for alternativ 2 | ||
562 | GNUNET_SERVICE_client_continue (client->client); | ||
563 | } | 519 | } |
564 | 520 | ||
565 | 521 | ||
566 | /** | 522 | /** |
567 | * @brief Shut down the service | 523 | * Stops and restarts the given peer, sleeping (!) for 5s in between. |
568 | * | 524 | * |
569 | * @param cls Closure - Handle to the service | 525 | * @param p the peer |
526 | * @param restart_cb callback to call when restarted | ||
527 | * @param restart_cb_cls callback closure | ||
528 | * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR | ||
570 | */ | 529 | */ |
571 | static void | 530 | int |
572 | shutdown_service (void *cls) | 531 | GNUNET_TRANSPORT_TESTING_restart_peer (struct |
532 | GNUNET_TRANSPORT_TESTING_PeerContext *p, | ||
533 | GNUNET_SCHEDULER_TaskCallback restart_cb, | ||
534 | void *restart_cb_cls) | ||
573 | { | 535 | { |
574 | struct GNUNET_SERVICE_Handle *h = cls; | 536 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; |
537 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | ||
575 | 538 | ||
539 | /* shutdown */ | ||
576 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 540 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
577 | "Shutting down service!\n"); | 541 | "Stopping peer %u (`%s')\n", |
578 | 542 | p->no, | |
579 | GNUNET_SERVICE_stop (h); | 543 | GNUNET_i2s (&p->id)); |
580 | } | 544 | if (NULL != p->pic) |
581 | |||
582 | |||
583 | /** | ||
584 | * @brief Callback called when new Client (Communicator) connects | ||
585 | * | ||
586 | * @param cls Closure - TransporCommmunicator Handle | ||
587 | * @param client Client | ||
588 | * @param mq Messagequeue | ||
589 | * | ||
590 | * @return TransportCommunicator Handle | ||
591 | */ | ||
592 | static void * | ||
593 | connect_cb (void *cls, | ||
594 | struct GNUNET_SERVICE_Client *client, | ||
595 | struct GNUNET_MQ_Handle *mq) | ||
596 | { | ||
597 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls; | ||
598 | struct MyClient *new_c; | ||
599 | |||
600 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected to %p.\n", | ||
601 | client, tc_h); | ||
602 | new_c = GNUNET_new (struct MyClient); | ||
603 | new_c->client = client; | ||
604 | new_c->c_mq = mq; | ||
605 | new_c->tc = tc_h; | ||
606 | GNUNET_CONTAINER_DLL_insert (tc_h->client_head, | ||
607 | tc_h->client_tail, | ||
608 | new_c); | ||
609 | |||
610 | if (NULL == tc_h->queue_head) | ||
611 | return new_c; | ||
612 | /* Iterate over queues. They are yet to be opened. Request opening. */ | ||
613 | for (struct | ||
614 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_iter = | ||
615 | tc_h->queue_head; | ||
616 | NULL != tc_queue_iter; | ||
617 | tc_queue_iter = tc_queue_iter->next) | ||
618 | { | 545 | { |
619 | if (NULL == tc_queue_iter->open_queue_env) | 546 | GNUNET_PEERSTORE_iterate_cancel (p->pic); |
620 | continue; | 547 | p->pic = NULL; |
621 | /* Send the previously created mq envelope to request the creation of the | 548 | } |
622 | * queue. */ | 549 | if (NULL != p->th) |
623 | GNUNET_MQ_send (tc_h->c_mq, | 550 | { |
624 | tc_queue_iter->open_queue_env); | 551 | GNUNET_TRANSPORT_core_disconnect (p->th); |
625 | tc_queue_iter->open_queue_env = NULL; | 552 | p->th = NULL; |
553 | } | ||
554 | if (NULL != p->tmh) | ||
555 | { | ||
556 | GNUNET_TRANSPORT_manipulation_disconnect (p->tmh); | ||
557 | p->tmh = NULL; | ||
558 | } | ||
559 | for (cc = p->tth->cc_head; NULL != cc; cc = ccn) | ||
560 | { | ||
561 | ccn = cc->next; | ||
562 | if ((cc->p1 == p) || | ||
563 | (cc->p2 == p)) | ||
564 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); | ||
565 | } | ||
566 | if (NULL != p->ats) | ||
567 | { | ||
568 | GNUNET_ATS_connectivity_done (p->ats); | ||
569 | p->ats = NULL; | ||
570 | } | ||
571 | if (GNUNET_SYSERR == | ||
572 | GNUNET_TESTING_peer_stop (p->peer)) | ||
573 | { | ||
574 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
575 | "Failed to stop peer %u (`%s')\n", | ||
576 | p->no, | ||
577 | GNUNET_i2s (&p->id)); | ||
578 | return GNUNET_SYSERR; | ||
626 | } | 579 | } |
627 | return new_c; | ||
628 | } | ||
629 | |||
630 | 580 | ||
631 | /** | 581 | sleep (5); // YUCK! |
632 | * @brief Callback called when Client disconnects | ||
633 | * | ||
634 | * @param cls Closure - TransportCommunicator Handle | ||
635 | * @param client Client | ||
636 | * @param internal_cls TransporCommmunicator Handle | ||
637 | */ | ||
638 | static void | ||
639 | disconnect_cb (void *cls, | ||
640 | struct GNUNET_SERVICE_Client *client, | ||
641 | void *internal_cls) | ||
642 | { | ||
643 | struct MyClient *cl = cls; | ||
644 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls; | ||
645 | 582 | ||
646 | for (cl = tc_h->client_head; NULL != cl; cl = cl->next) | 583 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
584 | "Restarting peer %u (`%s')\n", | ||
585 | p->no, | ||
586 | GNUNET_i2s (&p->id)); | ||
587 | /* restart */ | ||
588 | if (GNUNET_SYSERR == GNUNET_TESTING_peer_start (p->peer)) | ||
647 | { | 589 | { |
648 | if (cl->client != client) | 590 | LOG (GNUNET_ERROR_TYPE_ERROR, |
649 | continue; | 591 | "Failed to restart peer %u (`%s')\n", |
650 | GNUNET_CONTAINER_DLL_remove (tc_h->client_head, | 592 | p->no, |
651 | tc_h->client_tail, | 593 | GNUNET_i2s (&p->id)); |
652 | cl); | 594 | return GNUNET_SYSERR; |
653 | if (cl->c_mq == tc_h->c_mq) | ||
654 | tc_h->c_mq = NULL; | ||
655 | GNUNET_free (cl); | ||
656 | break; | ||
657 | } | 595 | } |
658 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected.\n"); | ||
659 | } | ||
660 | 596 | ||
661 | 597 | GNUNET_assert (NULL == p->start_cb); | |
662 | /** | 598 | p->start_cb = restart_cb; |
663 | * Message was transmitted. Process the request. | 599 | p->start_cb_cls = restart_cb_cls; |
664 | * | 600 | |
665 | * @param cls the client | 601 | p->th = GNUNET_TRANSPORT_core_connect (p->cfg, |
666 | * @param sma the send message that was sent | 602 | NULL, |
667 | */ | 603 | p->handlers, |
668 | static void | 604 | p, |
669 | handle_send_message_ack (void *cls, | 605 | ¬ify_connect, |
670 | const struct GNUNET_TRANSPORT_SendMessageToAck *sma) | 606 | ¬ify_disconnect); |
671 | { | 607 | GNUNET_assert (NULL != p->th); |
672 | struct MyClient *client = cls; | 608 | p->ats = GNUNET_ATS_connectivity_init (p->cfg); |
673 | GNUNET_SERVICE_client_continue (client->client); | 609 | p->pic = GNUNET_PEERSTORE_iterate (p->ph, |
674 | // NOP | 610 | "transport", |
611 | &p->id, | ||
612 | GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY, | ||
613 | hello_iter_cb, | ||
614 | p); | ||
615 | GNUNET_assert (NULL != p->pic); | ||
616 | return GNUNET_OK; | ||
675 | } | 617 | } |
676 | 618 | ||
677 | 619 | ||
678 | /** | 620 | /** |
679 | * @brief Start the communicator part of the transport service | 621 | * Shutdown the given peer |
680 | * | 622 | * |
681 | * @param communicator_available Callback to be called when a new communicator | 623 | * @param p the peer |
682 | * becomes available | ||
683 | * @param cfg Configuration | ||
684 | */ | 624 | */ |
685 | static void | 625 | void |
686 | transport_communicator_start ( | 626 | GNUNET_TRANSPORT_TESTING_stop_peer (struct |
687 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | 627 | GNUNET_TRANSPORT_TESTING_PeerContext *p) |
688 | { | 628 | { |
689 | struct GNUNET_MQ_MessageHandler mh[] = { | 629 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth; |
690 | GNUNET_MQ_hd_var_size (communicator_available, | 630 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; |
691 | GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR, | 631 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; |
692 | struct GNUNET_TRANSPORT_CommunicatorAvailableMessage, | ||
693 | tc_h), | ||
694 | GNUNET_MQ_hd_var_size (communicator_backchannel, | ||
695 | GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL, | ||
696 | struct GNUNET_TRANSPORT_CommunicatorBackchannel, | ||
697 | tc_h), | ||
698 | GNUNET_MQ_hd_var_size (add_address, | ||
699 | GNUNET_MESSAGE_TYPE_TRANSPORT_ADD_ADDRESS, | ||
700 | struct GNUNET_TRANSPORT_AddAddressMessage, | ||
701 | tc_h), | ||
702 | // GNUNET_MQ_hd_fixed_size (del_address, | ||
703 | // GNUNET_MESSAGE_TYPE_TRANSPORT_DEL_ADDRESS, | ||
704 | // struct GNUNET_TRANSPORT_DelAddressMessage, | ||
705 | // NULL), | ||
706 | GNUNET_MQ_hd_var_size (incoming_msg, | ||
707 | GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG, | ||
708 | struct GNUNET_TRANSPORT_IncomingMessage, | ||
709 | tc_h), | ||
710 | GNUNET_MQ_hd_fixed_size (queue_create_ok, | ||
711 | GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_OK, | ||
712 | struct GNUNET_TRANSPORT_CreateQueueResponse, | ||
713 | tc_h), | ||
714 | GNUNET_MQ_hd_fixed_size (queue_create_fail, | ||
715 | GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL, | ||
716 | struct GNUNET_TRANSPORT_CreateQueueResponse, | ||
717 | tc_h), | ||
718 | GNUNET_MQ_hd_var_size (add_queue_message, | ||
719 | GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_SETUP, | ||
720 | struct GNUNET_TRANSPORT_AddQueueMessage, | ||
721 | tc_h), | ||
722 | GNUNET_MQ_hd_fixed_size (update_queue_message, | ||
723 | GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_UPDATE, | ||
724 | struct GNUNET_TRANSPORT_UpdateQueueMessage, | ||
725 | tc_h), | ||
726 | // GNUNET_MQ_hd_fixed_size (del_queue_message, | ||
727 | // GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_TEARDOWN, | ||
728 | // struct GNUNET_TRANSPORT_DelQueueMessage, | ||
729 | // NULL), | ||
730 | GNUNET_MQ_hd_fixed_size (send_message_ack, | ||
731 | GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG_ACK, | ||
732 | struct GNUNET_TRANSPORT_SendMessageToAck, | ||
733 | tc_h), | ||
734 | GNUNET_MQ_handler_end () | ||
735 | }; | ||
736 | |||
737 | |||
738 | tc_h->sh = GNUNET_SERVICE_start ("transport", | ||
739 | tc_h->cfg, | ||
740 | &connect_cb, | ||
741 | &disconnect_cb, | ||
742 | tc_h, | ||
743 | mh); | ||
744 | GNUNET_assert (NULL != tc_h->sh); | ||
745 | } | ||
746 | |||
747 | 632 | ||
748 | /** | 633 | for (cc = tth->cc_head; NULL != cc; cc = ccn) |
749 | * @brief Task run at shutdown to kill communicator and clean up | 634 | { |
750 | * | 635 | ccn = cc->next; |
751 | * @param cls Closure - Process of communicator | 636 | if ((cc->p1 == p) || |
752 | */ | 637 | (cc->p2 == p)) |
753 | static void | 638 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); |
754 | shutdown_process (struct GNUNET_OS_Process *proc) | 639 | } |
755 | { | 640 | if (NULL != p->pic) |
756 | if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) | 641 | { |
642 | GNUNET_PEERSTORE_iterate_cancel (p->pic); | ||
643 | p->pic = NULL; | ||
644 | } | ||
645 | if (NULL != p->tmh) | ||
646 | { | ||
647 | GNUNET_TRANSPORT_manipulation_disconnect (p->tmh); | ||
648 | p->tmh = NULL; | ||
649 | } | ||
650 | if (NULL != p->th) | ||
651 | { | ||
652 | GNUNET_TRANSPORT_core_disconnect (p->th); | ||
653 | p->th = NULL; | ||
654 | } | ||
655 | if (NULL != p->peer) | ||
757 | { | 656 | { |
758 | LOG (GNUNET_ERROR_TYPE_WARNING, | 657 | if (GNUNET_OK != |
759 | "Error shutting down process with SIGERM, trying SIGKILL\n"); | 658 | GNUNET_TESTING_peer_stop (p->peer)) |
760 | if (0 != GNUNET_OS_process_kill (proc, SIGKILL)) | ||
761 | { | 659 | { |
762 | LOG (GNUNET_ERROR_TYPE_ERROR, | 660 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
763 | "Error shutting down process with SIGERM and SIGKILL\n"); | 661 | "Testing lib failed to stop peer %u (`%s')\n", |
662 | p->no, | ||
663 | GNUNET_i2s (&p->id)); | ||
764 | } | 664 | } |
665 | GNUNET_TESTING_peer_destroy (p->peer); | ||
666 | p->peer = NULL; | ||
765 | } | 667 | } |
766 | GNUNET_OS_process_destroy (proc); | 668 | if (NULL != p->ats) |
767 | } | ||
768 | |||
769 | static void | ||
770 | shutdown_peerstore (void *cls) | ||
771 | { | ||
772 | struct GNUNET_OS_Process *proc = cls; | ||
773 | shutdown_process (proc); | ||
774 | } | ||
775 | |||
776 | static void | ||
777 | shutdown_communicator (void *cls) | ||
778 | { | ||
779 | struct GNUNET_OS_Process *proc = cls; | ||
780 | shutdown_process (proc); | ||
781 | } | ||
782 | |||
783 | |||
784 | /** | ||
785 | * @brief Start the communicator | ||
786 | * | ||
787 | * @param cfgname Name of the communicator | ||
788 | */ | ||
789 | static void | ||
790 | communicator_start ( | ||
791 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h, | ||
792 | const char *binary_name) | ||
793 | { | ||
794 | char *binary; | ||
795 | char *loprefix; | ||
796 | char *section_name; | ||
797 | |||
798 | LOG (GNUNET_ERROR_TYPE_DEBUG, "communicator_start\n"); | ||
799 | |||
800 | section_name = strchr (binary_name, '-'); | ||
801 | section_name++; | ||
802 | |||
803 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (tc_h->cfg, | ||
804 | section_name, | ||
805 | "PREFIX", | ||
806 | &loprefix)) | ||
807 | loprefix = GNUNET_strdup (""); | ||
808 | |||
809 | |||
810 | binary = GNUNET_OS_get_libexec_binary_path (binary_name); | ||
811 | tc_h->c_proc = GNUNET_OS_start_process_s (GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | ||
812 | NULL, | ||
813 | loprefix, | ||
814 | binary, | ||
815 | binary_name, | ||
816 | "-c", | ||
817 | tc_h->cfg_filename, | ||
818 | NULL); | ||
819 | if (NULL == tc_h->c_proc) | ||
820 | { | 669 | { |
821 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start communicator!"); | 670 | GNUNET_ATS_connectivity_done (p->ats); |
822 | return; | 671 | p->ats = NULL; |
672 | } | ||
673 | if (NULL != p->hello) | ||
674 | { | ||
675 | GNUNET_free (p->hello); | ||
676 | p->hello = NULL; | ||
677 | } | ||
678 | if (NULL != p->cfg) | ||
679 | { | ||
680 | GNUNET_CONFIGURATION_destroy (p->cfg); | ||
681 | p->cfg = NULL; | ||
823 | } | 682 | } |
824 | LOG (GNUNET_ERROR_TYPE_INFO, "started communicator\n"); | 683 | if (NULL != p->handlers) |
825 | GNUNET_free (binary); | 684 | { |
685 | GNUNET_free (p->handlers); | ||
686 | p->handlers = NULL; | ||
687 | } | ||
688 | GNUNET_CONTAINER_DLL_remove (tth->p_head, | ||
689 | tth->p_tail, | ||
690 | p); | ||
691 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
692 | "Peer %u (`%s') stopped\n", | ||
693 | p->no, | ||
694 | GNUNET_i2s (&p->id)); | ||
695 | GNUNET_free (p); | ||
826 | } | 696 | } |
827 | 697 | ||
828 | 698 | ||
829 | /** | 699 | /** |
830 | * @brief Task run at shutdown to kill communicator and clean up | 700 | * Function called after the HELLO was passed to the |
831 | * | 701 | * transport service. |
832 | * @param cls Closure - Process of communicator | 702 | * FIXME maybe schedule the application_validate somehow |
833 | */ | 703 | */ |
704 | /* | ||
834 | static void | 705 | static void |
835 | shutdown_nat (void *cls) | 706 | hello_offered (void *cls) |
836 | { | 707 | { |
837 | struct GNUNET_OS_Process *proc = cls; | 708 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls; |
838 | shutdown_process (proc); | 709 | |
839 | } | 710 | cc->oh = NULL; |
711 | cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
712 | &offer_hello, | ||
713 | cc); | ||
714 | }*/ | ||
840 | 715 | ||
841 | 716 | ||
842 | /** | 717 | /** |
843 | * @brief Task run at shutdown to kill the resolver process | 718 | * Offer the current HELLO of P2 to P1. |
844 | * | 719 | * |
845 | * @param cls Closure - Process of communicator | 720 | * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest` |
846 | */ | 721 | */ |
847 | static void | 722 | static void |
848 | shutdown_resolver (void *cls) | 723 | offer_hello (void *cls) |
849 | { | 724 | { |
850 | struct GNUNET_OS_Process *proc = cls; | 725 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls; |
851 | shutdown_process (proc); | 726 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p1 = cc->p1; |
852 | } | 727 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = cc->p2; |
853 | 728 | struct GNUNET_TIME_Absolute t; | |
729 | enum GNUNET_NetworkType nt; | ||
730 | char *addr; | ||
854 | 731 | ||
855 | static void | 732 | cc->tct = NULL; |
856 | resolver_start (struct | ||
857 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
858 | { | ||
859 | char *binary; | ||
860 | |||
861 | LOG (GNUNET_ERROR_TYPE_DEBUG, "resolver_start\n"); | ||
862 | binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver"); | ||
863 | tc_h->resolver_proc = GNUNET_OS_start_process ( | ||
864 | GNUNET_OS_INHERIT_STD_OUT_AND_ERR | ||
865 | | GNUNET_OS_USE_PIPE_CONTROL, | ||
866 | NULL, | ||
867 | NULL, | ||
868 | NULL, | ||
869 | binary, | ||
870 | "gnunet-service-resolver", | ||
871 | "-c", | ||
872 | tc_h->cfg_filename, | ||
873 | NULL); | ||
874 | if (NULL == tc_h->resolver_proc) | ||
875 | { | 733 | { |
876 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start resolver service!"); | 734 | char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id)); |
877 | return; | 735 | |
736 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
737 | "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %s\n", | ||
738 | p1->no, | ||
739 | GNUNET_i2s (&p1->id), | ||
740 | p2->no, | ||
741 | p2_s, | ||
742 | cc->p2->hello); | ||
743 | GNUNET_free (p2_s); | ||
878 | } | 744 | } |
879 | LOG (GNUNET_ERROR_TYPE_INFO, "started resolver service\n"); | ||
880 | GNUNET_free (binary); | ||
881 | 745 | ||
746 | addr = GNUNET_HELLO_extract_address (cc->p2->hello, | ||
747 | cc->p2->hello_size, | ||
748 | &cc->p2->id, | ||
749 | &nt, | ||
750 | &t); | ||
751 | GNUNET_assert (NULL == addr); | ||
752 | GNUNET_TRANSPORT_application_validate (cc->p1->ah, | ||
753 | &cc->p2->id, | ||
754 | nt, | ||
755 | addr); | ||
756 | GNUNET_free (addr); | ||
882 | } | 757 | } |
883 | 758 | ||
884 | 759 | ||
885 | /** | 760 | /** |
886 | * @brief Start Peerstore | 761 | * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message |
762 | * | ||
763 | * Remarks: start_peer's notify_connect callback can be called before. | ||
887 | * | 764 | * |
765 | * @param tth transport testing handle | ||
766 | * @param p1 peer 1 | ||
767 | * @param p2 peer 2 | ||
768 | * @param cb the callback to call when both peers notified that they are connected | ||
769 | * @param cls callback cls | ||
770 | * @return a connect request handle | ||
888 | */ | 771 | */ |
889 | static void | 772 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest * |
890 | peerstore_start ( | 773 | GNUNET_TRANSPORT_TESTING_connect_peers (struct |
891 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | 774 | GNUNET_TRANSPORT_TESTING_PeerContext *p1, |
892 | { | 775 | struct |
893 | char *binary; | 776 | GNUNET_TRANSPORT_TESTING_PeerContext *p2, |
894 | 777 | GNUNET_SCHEDULER_TaskCallback cb, | |
895 | binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-peerstore"); | 778 | void *cls) |
896 | tc_h->ps_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | 779 | { |
897 | NULL, | 780 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth; |
898 | NULL, | 781 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; |
899 | NULL, | 782 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; |
900 | binary, | 783 | |
901 | "gnunet-service-peerstore", | 784 | ccn = NULL; |
902 | "-c", | 785 | for (cc = tth->cc_head; NULL != cc; cc = cc->next) |
903 | tc_h->cfg_filename, | ||
904 | NULL); | ||
905 | if (NULL == tc_h->ps_proc) | ||
906 | { | 786 | { |
907 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start Peerstore!"); | 787 | if ((cc->p1 == p1) && |
908 | return; | 788 | (cc->p2 == p2)) |
789 | { | ||
790 | ccn = cc; | ||
791 | break; | ||
792 | } | ||
909 | } | 793 | } |
910 | LOG (GNUNET_ERROR_TYPE_INFO, "started Peerstore\n"); | ||
911 | GNUNET_free (binary); | ||
912 | } | ||
913 | 794 | ||
914 | /** | 795 | cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest); |
915 | * @brief Start NAT | 796 | cc->p1 = p1; |
916 | * | 797 | cc->p2 = p2; |
917 | */ | 798 | cc->cb = cb; |
918 | static void | 799 | if (NULL != cls) |
919 | nat_start ( | 800 | cc->cb_cls = cls; |
920 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | 801 | else |
921 | { | 802 | cc->cb_cls = cc; |
922 | char *binary; | 803 | if (NULL != ccn) |
923 | |||
924 | LOG (GNUNET_ERROR_TYPE_DEBUG, "nat_start\n"); | ||
925 | binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-nat"); | ||
926 | tc_h->nat_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR | ||
927 | | GNUNET_OS_USE_PIPE_CONTROL, | ||
928 | NULL, | ||
929 | NULL, | ||
930 | NULL, | ||
931 | binary, | ||
932 | "gnunet-service-nat", | ||
933 | "-c", | ||
934 | tc_h->cfg_filename, | ||
935 | NULL); | ||
936 | if (NULL == tc_h->nat_proc) | ||
937 | { | 804 | { |
938 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start NAT!"); | 805 | cc->p1_c = ccn->p1_c; |
939 | return; | 806 | cc->p2_c = ccn->p2_c; |
807 | cc->connected = ccn->connected; | ||
940 | } | 808 | } |
941 | LOG (GNUNET_ERROR_TYPE_INFO, "started NAT\n"); | 809 | GNUNET_CONTAINER_DLL_insert (tth->cc_head, |
942 | GNUNET_free (binary); | 810 | tth->cc_tail, |
811 | cc); | ||
812 | cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello, | ||
813 | cc); | ||
814 | cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats, | ||
815 | &p2->id, | ||
816 | 1); | ||
817 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
818 | "New connect request %p\n", | ||
819 | cc); | ||
820 | return cc; | ||
943 | } | 821 | } |
944 | 822 | ||
945 | 823 | ||
946 | /** | 824 | /** |
947 | * @brief Start communicator part of transport service and communicator | 825 | * Cancel the request to connect two peers |
948 | * | 826 | * Tou MUST cancel the request if you stop the peers before the peers connected succesfully |
949 | * @param service_name Name of the service | ||
950 | * @param cfg Configuration handle | ||
951 | * @param communicator_available_cb Callback that is called when a new | ||
952 | * @param add_address_cb Callback that is called when a new | ||
953 | * communicator becomes available | ||
954 | * @param cb_cls Closure to @a communicator_available_cb and @a | ||
955 | * | 827 | * |
956 | * @return Handle to the communicator duo | 828 | * @param tth transport testing handle |
829 | * @param cc a connect request handle | ||
957 | */ | 830 | */ |
958 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle * | 831 | void |
959 | GNUNET_TRANSPORT_TESTING_transport_communicator_service_start ( | 832 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct |
960 | const char *service_name, | 833 | GNUNET_TRANSPORT_TESTING_ConnectRequest |
961 | const char *binary_name, | 834 | *cc) |
962 | const char *cfg_filename, | ||
963 | const struct GNUNET_PeerIdentity *peer_id, | ||
964 | GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback | ||
965 | communicator_available_cb, | ||
966 | GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb, | ||
967 | GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb, | ||
968 | GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb, | ||
969 | GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_message_cb, | ||
970 | GNUNET_TRANSPORT_TESTING_BackchannelCallback bc_cb, | ||
971 | void *cb_cls) | ||
972 | { | 835 | { |
973 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h; | 836 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = cc->p1->tth; |
974 | 837 | ||
975 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 838 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
976 | "Starting new transport/communicator combo with config %s\n", | 839 | "Canceling connect request!\n"); |
977 | cfg_filename); | 840 | if (NULL != cc->tct) |
978 | tc_h = | ||
979 | GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle); | ||
980 | tc_h->cfg_filename = GNUNET_strdup (cfg_filename); | ||
981 | tc_h->cfg = GNUNET_CONFIGURATION_create (); | ||
982 | if ((GNUNET_SYSERR == GNUNET_CONFIGURATION_load (tc_h->cfg, cfg_filename))) | ||
983 | { | 841 | { |
984 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 842 | GNUNET_SCHEDULER_cancel (cc->tct); |
985 | _ ("Malformed configuration file `%s', exit ...\n"), | 843 | cc->tct = NULL; |
986 | cfg_filename); | ||
987 | GNUNET_free (tc_h->cfg_filename); | ||
988 | GNUNET_CONFIGURATION_destroy (tc_h->cfg); | ||
989 | GNUNET_free (tc_h); | ||
990 | return NULL; | ||
991 | } | 844 | } |
992 | tc_h->bc_enabled = GNUNET_CONFIGURATION_get_value_yesno (tc_h->cfg, | 845 | if (NULL != cc->ats_sh) |
993 | "communicator-test", | 846 | { |
994 | "BACKCHANNEL_ENABLED"); | 847 | GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh); |
995 | tc_h->communicator_available_cb = communicator_available_cb; | 848 | cc->ats_sh = NULL; |
996 | tc_h->add_address_cb = add_address_cb; | 849 | } |
997 | tc_h->queue_create_reply_cb = queue_create_reply_cb; | 850 | GNUNET_CONTAINER_DLL_remove (tth->cc_head, |
998 | tc_h->add_queue_cb = add_queue_cb; | 851 | tth->cc_tail, |
999 | tc_h->incoming_msg_cb = incoming_message_cb; | 852 | cc); |
1000 | tc_h->bc_cb = bc_cb; | 853 | GNUNET_free (cc); |
1001 | tc_h->peer_id = *peer_id; | ||
1002 | tc_h->cb_cls = cb_cls; | ||
1003 | |||
1004 | /* Start communicator part of service */ | ||
1005 | transport_communicator_start (tc_h); | ||
1006 | /* Start NAT */ | ||
1007 | nat_start (tc_h); | ||
1008 | /* Start resolver service */ | ||
1009 | resolver_start (tc_h); | ||
1010 | /* Start peerstore service */ | ||
1011 | peerstore_start (tc_h); | ||
1012 | /* Schedule start communicator */ | ||
1013 | communicator_start (tc_h, | ||
1014 | binary_name); | ||
1015 | return tc_h; | ||
1016 | } | ||
1017 | |||
1018 | |||
1019 | void | ||
1020 | GNUNET_TRANSPORT_TESTING_transport_communicator_service_stop ( | ||
1021 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
1022 | { | ||
1023 | shutdown_communicator (tc_h->c_proc); | ||
1024 | shutdown_service (tc_h->sh); | ||
1025 | shutdown_nat (tc_h->nat_proc); | ||
1026 | shutdown_resolver (tc_h->resolver_proc); | ||
1027 | shutdown_peerstore (tc_h->ps_proc); | ||
1028 | GNUNET_CONFIGURATION_destroy (tc_h->cfg); | ||
1029 | GNUNET_free (tc_h); | ||
1030 | } | 854 | } |
1031 | 855 | ||
1032 | 856 | ||
1033 | /** | 857 | /** |
1034 | * @brief Instruct communicator to open a queue | 858 | * Clean up the transport testing |
1035 | * | 859 | * |
1036 | * @param tc_h Handle to communicator which shall open queue | 860 | * @param tth transport testing handle |
1037 | * @param peer_id Towards which peer | ||
1038 | * @param address For which address | ||
1039 | */ | 861 | */ |
1040 | void | 862 | void |
1041 | GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue ( | 863 | GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth) |
1042 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h, | ||
1043 | const struct GNUNET_PeerIdentity *peer_id, | ||
1044 | const char *address) | ||
1045 | { | 864 | { |
1046 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | 865 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; |
1047 | static uint32_t idgen; | 866 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct; |
1048 | char *prefix; | 867 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p; |
1049 | struct GNUNET_TRANSPORT_CreateQueue *msg; | 868 | struct GNUNET_TRANSPORT_TESTING_PeerContext *t; |
1050 | struct GNUNET_MQ_Envelope *env; | 869 | |
1051 | size_t alen; | 870 | if (NULL == tth) |
1052 | |||
1053 | tc_queue = | ||
1054 | GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue); | ||
1055 | tc_queue->tc_h = tc_h; | ||
1056 | prefix = GNUNET_HELLO_address_to_prefix (address); | ||
1057 | if (NULL == prefix) | ||
1058 | { | ||
1059 | GNUNET_break (0); /* We got an invalid address!? */ | ||
1060 | GNUNET_free (tc_queue); | ||
1061 | return; | 871 | return; |
1062 | } | 872 | cc = tth->cc_head; |
1063 | GNUNET_free (prefix); | 873 | while (NULL != cc) |
1064 | alen = strlen (address) + 1; | ||
1065 | env = | ||
1066 | GNUNET_MQ_msg_extra (msg, alen, GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE); | ||
1067 | msg->request_id = htonl (idgen++); | ||
1068 | tc_queue->qid = msg->request_id; | ||
1069 | msg->receiver = *peer_id; | ||
1070 | tc_queue->peer_id = *peer_id; | ||
1071 | memcpy (&msg[1], address, alen); | ||
1072 | if (NULL != tc_h->c_mq) | ||
1073 | { | 874 | { |
1074 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 875 | ct = cc->next; |
1075 | "Sending queue create immediately\n"); | 876 | LOG (GNUNET_ERROR_TYPE_ERROR, |
1076 | GNUNET_MQ_send (tc_h->c_mq, env); | 877 | "Developer forgot to cancel connect request!\n"); |
878 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); | ||
879 | cc = ct; | ||
1077 | } | 880 | } |
1078 | else | 881 | p = tth->p_head; |
882 | while (NULL != p) | ||
1079 | { | 883 | { |
1080 | tc_queue->open_queue_env = env; | 884 | t = p->next; |
885 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
886 | "Developer forgot to stop peer!\n"); | ||
887 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
888 | p = t; | ||
1081 | } | 889 | } |
1082 | GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue); | 890 | GNUNET_TESTING_system_destroy (tth->tl_system, |
891 | GNUNET_YES); | ||
892 | |||
893 | GNUNET_free (tth); | ||
1083 | } | 894 | } |
1084 | 895 | ||
1085 | 896 | ||
1086 | /** | 897 | /** |
1087 | * @brief Instruct communicator to send data | 898 | * Initialize the transport testing |
1088 | * | 899 | * |
1089 | * @param tc_queue The queue to use for sending | 900 | * @return transport testing handle |
1090 | * @param cont function to call when done sending | ||
1091 | * @param cont_cls closure for @a cont | ||
1092 | * @param payload Data to send | ||
1093 | * @param payload_size Size of the @a payload | ||
1094 | */ | 901 | */ |
1095 | void | 902 | struct GNUNET_TRANSPORT_TESTING_Handle * |
1096 | GNUNET_TRANSPORT_TESTING_transport_communicator_send | 903 | GNUNET_TRANSPORT_TESTING_init () |
1097 | (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h, | ||
1098 | GNUNET_SCHEDULER_TaskCallback cont, | ||
1099 | void *cont_cls, | ||
1100 | const void *payload, | ||
1101 | size_t payload_size) | ||
1102 | { | 904 | { |
1103 | struct GNUNET_MessageHeader *mh; | 905 | struct GNUNET_TRANSPORT_TESTING_Handle *tth; |
1104 | struct GNUNET_TRANSPORT_SendMessageTo *msg; | 906 | |
1105 | struct GNUNET_MQ_Envelope *env; | 907 | tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_Handle); |
1106 | size_t inbox_size; | 908 | tth->tl_system = GNUNET_TESTING_system_create ("transport-testing", |
1107 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | 909 | NULL, |
1108 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_tmp; | 910 | NULL, |
1109 | 911 | NULL); | |
1110 | tc_queue = NULL; | 912 | if (NULL == tth->tl_system) |
1111 | for (tc_queue_tmp = tc_h->queue_head; | ||
1112 | NULL != tc_queue_tmp; | ||
1113 | tc_queue_tmp = tc_queue_tmp->next) | ||
1114 | { | 913 | { |
1115 | if (tc_queue_tmp->q_len <= 0) | 914 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
1116 | continue; | 915 | "Failed to initialize testing library!\n"); |
1117 | if (NULL == tc_queue) | 916 | GNUNET_free (tth); |
1118 | { | 917 | return NULL; |
1119 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1120 | "Selecting queue with prio %u, len %" PRIu64 " and MTU %u\n", | ||
1121 | tc_queue_tmp->priority, | ||
1122 | tc_queue_tmp->q_len, | ||
1123 | tc_queue_tmp->mtu); | ||
1124 | tc_queue = tc_queue_tmp; | ||
1125 | continue; | ||
1126 | } | ||
1127 | if (tc_queue->priority < tc_queue_tmp->priority) | ||
1128 | { | ||
1129 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1130 | "Selecting queue with prio %u, len %" PRIu64 " and MTU %u\n", | ||
1131 | tc_queue_tmp->priority, | ||
1132 | tc_queue_tmp->q_len, | ||
1133 | tc_queue_tmp->mtu); | ||
1134 | tc_queue = tc_queue_tmp; | ||
1135 | } | ||
1136 | } | 918 | } |
1137 | GNUNET_assert (NULL != tc_queue); | 919 | return tth; |
1138 | // Uncomment this for alternativ 1 of backchannel functionality | ||
1139 | if (tc_queue->q_len != GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED) | ||
1140 | tc_queue->q_len--; | ||
1141 | // Until here for alternativ 1 | ||
1142 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1143 | "Sending message\n"); | ||
1144 | inbox_size = sizeof (struct GNUNET_MessageHeader) + payload_size; | ||
1145 | env = GNUNET_MQ_msg_extra (msg, | ||
1146 | inbox_size, | ||
1147 | GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG); | ||
1148 | GNUNET_assert (NULL != env); | ||
1149 | msg->qid = htonl (tc_queue->qid); | ||
1150 | msg->mid = tc_queue->mid++; | ||
1151 | msg->receiver = tc_queue->peer_id; | ||
1152 | mh = (struct GNUNET_MessageHeader *) &msg[1]; | ||
1153 | mh->size = htons (inbox_size); | ||
1154 | mh->type = GNUNET_MESSAGE_TYPE_DUMMY; | ||
1155 | memcpy (&mh[1], | ||
1156 | payload, | ||
1157 | payload_size); | ||
1158 | if (NULL != cont) | ||
1159 | GNUNET_MQ_notify_sent (env, | ||
1160 | cont, | ||
1161 | cont_cls); | ||
1162 | GNUNET_MQ_send (tc_queue->tc_h->c_mq, | ||
1163 | env); | ||
1164 | } | 920 | } |
921 | |||
922 | |||
923 | /* end of transport-testing.c */ | ||
diff --git a/src/transport/transport-testing2.h b/src/transport/transport-testing2.h index 04f75fc88..3554436d8 100644 --- a/src/transport/transport-testing2.h +++ b/src/transport/transport-testing2.h | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2019 GNUnet e.V. | 3 | Copyright (C) 2006, 2009, 2015, 2016 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 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 | 6 | under the terms of the GNU Affero General Public License as published |
@@ -19,337 +19,906 @@ | |||
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file transport/transport-testing2.h | 22 | * @file transport-testing.h |
23 | * @brief functions and structures related to testing-tng | 23 | * @brief testing lib for transport service |
24 | * @author Matthias Wachs | ||
24 | * @author Christian Grothoff | 25 | * @author Christian Grothoff |
25 | * @author Julius Bünger | ||
26 | */ | 26 | */ |
27 | #ifndef TRANSPORT_TESTING_H | ||
28 | #define TRANSPORT_TESTING_H | ||
27 | #include "platform.h" | 29 | #include "platform.h" |
28 | #include "gnunet_util_lib.h" | 30 | #include "gnunet_util_lib.h" |
29 | #include "gnunet_ats_transport_service.h" | 31 | #include "gnunet_hello_lib.h" |
30 | #include "transport.h" | 32 | #include "gnunet_peerstore_service.h" |
33 | #include "gnunet_transport_core_service.h" | ||
34 | #include "gnunet_transport_application_service.h" | ||
35 | #include "gnunet_transport_manipulation_service.h" | ||
36 | #include "gnunet_testing_lib.h" | ||
37 | |||
38 | |||
39 | /* ************* Basic functions for starting/stopping/connecting *********** */ | ||
40 | |||
41 | /** | ||
42 | * Context for a single peer | ||
43 | */ | ||
44 | struct GNUNET_TRANSPORT_TESTING_PeerContext; | ||
45 | |||
46 | /** | ||
47 | * Definition for a transport testing handle | ||
48 | */ | ||
49 | struct GNUNET_TRANSPORT_TESTING_Handle; | ||
50 | |||
51 | |||
52 | /** | ||
53 | * Context for a single peer | ||
54 | */ | ||
55 | struct GNUNET_TRANSPORT_TESTING_PeerContext | ||
56 | { | ||
57 | /** | ||
58 | * Next element in the DLL | ||
59 | */ | ||
60 | struct GNUNET_TRANSPORT_TESTING_PeerContext *next; | ||
61 | |||
62 | /** | ||
63 | * Previous element in the DLL | ||
64 | */ | ||
65 | struct GNUNET_TRANSPORT_TESTING_PeerContext *prev; | ||
66 | |||
67 | /** | ||
68 | * Transport testing handle this peer belongs to | ||
69 | */ | ||
70 | struct GNUNET_TRANSPORT_TESTING_Handle *tth; | ||
71 | |||
72 | /** | ||
73 | * Application handle | ||
74 | */ | ||
75 | struct GNUNET_TRANSPORT_ApplicationHandle *ah; | ||
76 | |||
77 | /** | ||
78 | * Peer's configuration | ||
79 | */ | ||
80 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
81 | |||
82 | /** | ||
83 | * Peer's transport service handle | ||
84 | */ | ||
85 | struct GNUNET_TRANSPORT_CoreHandle *th; | ||
86 | |||
87 | /** | ||
88 | * Peer's transport service manipulation handle | ||
89 | */ | ||
90 | struct GNUNET_TRANSPORT_ManipulationHandle *tmh; | ||
91 | |||
92 | /** | ||
93 | * Peer's ATS handle. | ||
94 | */ | ||
95 | struct GNUNET_ATS_ConnectivityHandle *ats; | ||
96 | |||
97 | /** | ||
98 | * Peer's PEERSTORE Handle | ||
99 | */ | ||
100 | struct GNUNET_PEERSTORE_Handle *ph; | ||
101 | |||
102 | /** | ||
103 | * Peer's transport get hello handle to retrieve peer's HELLO message | ||
104 | */ | ||
105 | struct GNUNET_PEERSTORE_IterateContext *pic; | ||
106 | |||
107 | /** | ||
108 | * Hello | ||
109 | */ | ||
110 | char *hello; | ||
111 | |||
112 | /** | ||
113 | * Hello size | ||
114 | */ | ||
115 | size_t hello_size; | ||
116 | |||
117 | /** | ||
118 | * Peer's testing handle | ||
119 | */ | ||
120 | struct GNUNET_TESTING_Peer *peer; | ||
121 | |||
122 | /** | ||
123 | * Peer identity | ||
124 | */ | ||
125 | struct GNUNET_PeerIdentity id; | ||
126 | |||
127 | /** | ||
128 | * Handle for the peer's ARM process | ||
129 | */ | ||
130 | struct GNUNET_OS_Process *arm_proc; | ||
131 | |||
132 | /** | ||
133 | * Receive callback | ||
134 | */ | ||
135 | struct GNUNET_MQ_MessageHandler *handlers; | ||
136 | |||
137 | /** | ||
138 | * Notify connect callback | ||
139 | */ | ||
140 | GNUNET_TRANSPORT_NotifyConnect nc; | ||
141 | |||
142 | /** | ||
143 | * Notify disconnect callback | ||
144 | */ | ||
145 | GNUNET_TRANSPORT_NotifyDisconnect nd; | ||
146 | |||
147 | /** | ||
148 | * Startup completed callback | ||
149 | */ | ||
150 | GNUNET_SCHEDULER_TaskCallback start_cb; | ||
151 | |||
152 | /** | ||
153 | * Closure for the @a nc and @a nd callbacks | ||
154 | */ | ||
155 | void *cb_cls; | ||
156 | |||
157 | /** | ||
158 | * Closure for @e start_cb. | ||
159 | */ | ||
160 | void *start_cb_cls; | ||
161 | |||
162 | /** | ||
163 | * An unique number to identify the peer | ||
164 | */ | ||
165 | unsigned int no; | ||
166 | }; | ||
167 | |||
168 | |||
169 | /** | ||
170 | * Handle for a request to connect two peers. | ||
171 | */ | ||
172 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest | ||
173 | { | ||
174 | /** | ||
175 | * Kept in a DLL. | ||
176 | */ | ||
177 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *next; | ||
178 | |||
179 | /** | ||
180 | * Kept in a DLL. | ||
181 | */ | ||
182 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *prev; | ||
183 | |||
184 | /** | ||
185 | * Peer we want to connect. | ||
186 | */ | ||
187 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p1; | ||
188 | |||
189 | /** | ||
190 | * Peer we want to connect. | ||
191 | */ | ||
192 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2; | ||
193 | |||
194 | /** | ||
195 | * Task by which we accomplish the connection. | ||
196 | */ | ||
197 | struct GNUNET_SCHEDULER_Task *tct; | ||
198 | |||
199 | /** | ||
200 | * Handle by which we ask ATS to faciliate the connection. | ||
201 | */ | ||
202 | struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh; | ||
203 | |||
204 | /** | ||
205 | * Function to call upon completion. | ||
206 | */ | ||
207 | GNUNET_SCHEDULER_TaskCallback cb; | ||
208 | |||
209 | /** | ||
210 | * Closure for @e cb. | ||
211 | */ | ||
212 | void *cb_cls; | ||
213 | |||
214 | /** | ||
215 | * Message queue for sending from @a p1 to @a p2. | ||
216 | */ | ||
217 | struct GNUNET_MQ_Handle *mq; | ||
218 | |||
219 | /** | ||
220 | * Set if peer1 says the connection is up to peer2. | ||
221 | */ | ||
222 | int p1_c; | ||
223 | |||
224 | /** | ||
225 | * Set if peer2 says the connection is up to peer1. | ||
226 | */ | ||
227 | int p2_c; | ||
228 | |||
229 | /** | ||
230 | * #GNUNET_YES if both @e p1_c and @e p2_c are #GNUNET_YES. | ||
231 | */ | ||
232 | int connected; | ||
233 | }; | ||
234 | |||
235 | |||
236 | /** | ||
237 | * Handle for a test run. | ||
238 | */ | ||
239 | struct GNUNET_TRANSPORT_TESTING_Handle | ||
240 | { | ||
241 | /** | ||
242 | * Testing library system handle | ||
243 | */ | ||
244 | struct GNUNET_TESTING_System *tl_system; | ||
245 | |||
246 | /** | ||
247 | * head DLL of connect contexts | ||
248 | */ | ||
249 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_head; | ||
250 | |||
251 | /** | ||
252 | * head DLL of connect contexts | ||
253 | */ | ||
254 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_tail; | ||
255 | |||
256 | /** | ||
257 | * head DLL of peers | ||
258 | */ | ||
259 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p_head; | ||
260 | |||
261 | /** | ||
262 | * tail DLL of peers | ||
263 | */ | ||
264 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p_tail; | ||
265 | }; | ||
266 | |||
267 | |||
268 | /** | ||
269 | * Initialize the transport testing | ||
270 | * | ||
271 | * @return transport testing handle | ||
272 | */ | ||
273 | struct GNUNET_TRANSPORT_TESTING_Handle * | ||
274 | GNUNET_TRANSPORT_TESTING_init (void); | ||
275 | |||
276 | |||
277 | /** | ||
278 | * Clean up the transport testing | ||
279 | * | ||
280 | * @param tth transport testing handle | ||
281 | */ | ||
282 | void | ||
283 | GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth); | ||
284 | |||
285 | |||
286 | /** | ||
287 | * Start a peer with the given configuration | ||
288 | * | ||
289 | * @param tth the testing handle | ||
290 | * @param cfgname configuration file | ||
291 | * @param peer_id the peer_id | ||
292 | * @param handlers functions for receiving messages | ||
293 | * @param nc connect callback | ||
294 | * @param nd disconnect callback | ||
295 | * @param cb_cls closure for @a nc and @a nd callback | ||
296 | * @param start_cb start callback | ||
297 | * @param start_cb_cls closure for @a start_cb | ||
298 | * @return the peer context | ||
299 | */ | ||
300 | struct GNUNET_TRANSPORT_TESTING_PeerContext * | ||
301 | GNUNET_TRANSPORT_TESTING_start_peer ( | ||
302 | struct GNUNET_TRANSPORT_TESTING_Handle *tth, | ||
303 | const char *cfgname, | ||
304 | int peer_id, | ||
305 | const struct GNUNET_MQ_MessageHandler *handlers, | ||
306 | GNUNET_TRANSPORT_NotifyConnect nc, | ||
307 | GNUNET_TRANSPORT_NotifyDisconnect nd, | ||
308 | void *cb_cls, | ||
309 | GNUNET_SCHEDULER_TaskCallback start_cb, | ||
310 | void *start_cb_cls); | ||
311 | |||
312 | |||
313 | /** | ||
314 | * Shutdown the given peer | ||
315 | * | ||
316 | * @param p the peer | ||
317 | */ | ||
318 | void | ||
319 | GNUNET_TRANSPORT_TESTING_stop_peer ( | ||
320 | struct GNUNET_TRANSPORT_TESTING_PeerContext *pc); | ||
321 | |||
322 | |||
323 | /** | ||
324 | * Stops and restarts the given peer, sleeping (!) for 5s in between. | ||
325 | * | ||
326 | * @param p the peer | ||
327 | * @param restart_cb restart callback | ||
328 | * @param restart_cb_cls callback closure | ||
329 | * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR | ||
330 | */ | ||
331 | int | ||
332 | GNUNET_TRANSPORT_TESTING_restart_peer ( | ||
333 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p, | ||
334 | GNUNET_SCHEDULER_TaskCallback restart_cb, | ||
335 | void *restart_cb_cls); | ||
336 | |||
31 | 337 | ||
32 | /** | 338 | /** |
33 | * @brief Queue of a communicator and some context | 339 | * Connect the given peers and call the callback when both peers |
340 | * report the inbound connection. Remarks: start_peer's notify_connect | ||
341 | * callback can be called before. | ||
342 | * | ||
343 | * @param p1 peer 1 | ||
344 | * @param p2 peer 2 | ||
345 | * @param cb the callback to call when both peers notified that they are | ||
346 | * connected | ||
347 | * @param cls callback cls | ||
348 | * @return a connect request handle | ||
34 | */ | 349 | */ |
35 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue; | 350 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest * |
351 | GNUNET_TRANSPORT_TESTING_connect_peers ( | ||
352 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p1, | ||
353 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2, | ||
354 | GNUNET_SCHEDULER_TaskCallback cb, | ||
355 | void *cls); | ||
36 | 356 | ||
37 | 357 | ||
38 | /** | 358 | /** |
39 | * @brief Handle/Context to a single transmission | 359 | * Cancel the request to connect two peers. You MUST cancel the |
360 | * request if you stop the peers before the peers connected | ||
361 | * succesfully. | ||
362 | * | ||
363 | * @param cc a connect request handle | ||
40 | */ | 364 | */ |
41 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission; | 365 | void |
366 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel ( | ||
367 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc); | ||
368 | |||
42 | 369 | ||
43 | /** | 370 | /** |
44 | * @brief Function signature for callbacks that are called when new | 371 | * Function called on matching connect requests. |
45 | * backchannel message arrived | ||
46 | * | 372 | * |
47 | * @param cls Closure | 373 | * @param cls closure |
48 | * @param msg Backchannel message | 374 | * @param cc request matching the query |
49 | * @param pid Target peer | ||
50 | */ | 375 | */ |
51 | typedef struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle * | 376 | typedef void (*GNUNET_TRANSPORT_TESTING_ConnectContextCallback) ( |
52 | (*GNUNET_TRANSPORT_TESTING_BackchannelCallback)(void *cls, | 377 | void *cls, |
53 | struct GNUNET_MessageHeader *msg, | 378 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc); |
54 | struct GNUNET_PeerIdentity *pid); | ||
55 | 379 | ||
56 | 380 | ||
57 | /** | 381 | /** |
58 | * @brief Function signature for callbacks that are called when new | 382 | * Find any connecting context matching the given pair of peers. |
59 | * communicators become available | ||
60 | * | 383 | * |
61 | * @param cls Closure | 384 | * @param p1 first peer |
62 | * @param tc_h Communicator handle | 385 | * @param p2 second peer |
63 | * @param cc Characteristics of communicator | 386 | * @param cb function to call |
64 | * @param address_prefix Prefix of the address | 387 | * @param cb_cls closure for @a cb |
65 | */ | 388 | */ |
66 | typedef void | 389 | void |
67 | (*GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback)(void *cls, | 390 | GNUNET_TRANSPORT_TESTING_find_connecting_context ( |
68 | struct | 391 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p1, |
69 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | 392 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2, |
70 | *tc_h, | 393 | GNUNET_TRANSPORT_TESTING_ConnectContextCallback cb, |
71 | enum | 394 | void *cb_cls); |
72 | GNUNET_TRANSPORT_CommunicatorCharacteristics | 395 | |
73 | cc, | 396 | |
74 | char *address_prefix); | 397 | /* ********************** high-level process functions *************** */ |
75 | 398 | ||
76 | 399 | ||
77 | /** | 400 | /** |
78 | * @brief Receive information about the address of a communicator. | 401 | * Function called once the peers have been launched and |
402 | * connected by #GNUNET_TRANSPORT_TESTING_connect_check(). | ||
79 | * | 403 | * |
80 | * @param cls Closure | 404 | * @param cls closure |
81 | * @param tc_h Communicator handle | 405 | * @param num_peers size of the @a p array |
82 | * @param address Address represented as string | 406 | * @param p the peers that were launched |
83 | * @param expiration Expiration | 407 | */ |
84 | * @param aid Aid | 408 | typedef void (*GNUNET_TRANSPORT_TESTING_ConnectContinuation) ( |
85 | * @param nt Network Type | 409 | void *cls, |
86 | */ | 410 | unsigned int num_peers, |
87 | typedef void | 411 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p[]); |
88 | (*GNUNET_TRANSPORT_TESTING_AddAddressCallback)(void *cls, | 412 | |
89 | struct | 413 | |
90 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | 414 | /** |
91 | *tc_h, | 415 | * Internal data structure. |
92 | const char *address, | 416 | */ |
93 | struct GNUNET_TIME_Relative | 417 | struct GNUNET_TRANSPORT_TESTING_ConnectRequestList; |
94 | expiration, | 418 | |
95 | uint32_t aid, | 419 | /** |
96 | enum GNUNET_NetworkType nt); | 420 | * Internal data structure. |
97 | 421 | */ | |
98 | 422 | struct GNUNET_TRANSPORT_TESTING_InternalPeerContext; | |
99 | /** | 423 | |
100 | * @brief Get informed about the success of a queue request. | 424 | |
425 | GNUNET_NETWORK_STRUCT_BEGIN | ||
426 | struct GNUNET_TRANSPORT_TESTING_TestMessage | ||
427 | { | ||
428 | /** | ||
429 | * Type is (usually) #GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE. | ||
430 | */ | ||
431 | struct GNUNET_MessageHeader header; | ||
432 | |||
433 | /** | ||
434 | * Monotonically increasing counter throughout the test. | ||
435 | */ | ||
436 | uint32_t num GNUNET_PACKED; | ||
437 | }; | ||
438 | GNUNET_NETWORK_STRUCT_END | ||
439 | |||
440 | |||
441 | /** | ||
442 | * Function called by the transport for each received message. | ||
101 | * | 443 | * |
102 | * @param cls Closure | 444 | * @param cls closure |
103 | * @param tc_h Communicator handle | 445 | * @param receiver receiver of the message |
104 | * @param will_try #GNUNET_YES if communicator will try to create queue | 446 | * @param sender sender of the message |
447 | * @param message the message | ||
105 | */ | 448 | */ |
106 | typedef void | 449 | typedef void (*GNUNET_TRANSPORT_TESTING_ReceiveCallback) ( |
107 | (*GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback)(void *cls, | 450 | void *cls, |
108 | struct | 451 | struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver, |
109 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | 452 | const struct GNUNET_PeerIdentity *sender, |
110 | *tc_h, | 453 | const struct GNUNET_TRANSPORT_TESTING_TestMessage *message); |
111 | int will_try); | ||
112 | 454 | ||
113 | 455 | ||
114 | /** | 456 | /** |
115 | * @brief Handle opening of queue | 457 | * Function called to notify transport users that another |
458 | * peer connected to us. | ||
116 | * | 459 | * |
117 | * @param cls Closure | 460 | * @param cls closure |
118 | * @param tc_h Communicator handle | 461 | * @param me peer experiencing the event |
119 | * @param tc_queue Handle to newly opened queue | 462 | * @param other peer that connected to @a me |
120 | */ | 463 | */ |
121 | typedef void | 464 | typedef void (*GNUNET_TRANSPORT_TESTING_NotifyConnect) ( |
122 | (*GNUNET_TRANSPORT_TESTING_AddQueueCallback)(void *cls, | 465 | void *cls, |
123 | struct | 466 | struct GNUNET_TRANSPORT_TESTING_PeerContext *me, |
124 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | 467 | const struct GNUNET_PeerIdentity *other); |
125 | *tc_h, | ||
126 | struct | ||
127 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue | ||
128 | *tc_queue, | ||
129 | size_t mtu); | ||
130 | 468 | ||
131 | 469 | ||
132 | /** | 470 | /** |
133 | * @brief Handle an incoming message | 471 | * Function called to notify transport users that another |
472 | * peer disconnected from us. | ||
134 | * | 473 | * |
135 | * @param cls Closure | 474 | * @param cls closure |
136 | * @param tc_h Handle to the receiving communicator | 475 | * @param me peer experiencing the event |
137 | * @param msg Received message | 476 | * @param other peer that disconnected from @a me |
138 | */ | 477 | */ |
139 | typedef void | 478 | typedef void (*GNUNET_TRANSPORT_TESTING_NotifyDisconnect) ( |
140 | (*GNUNET_TRANSPORT_TESTING_IncomingMessageCallback)(void *cls, | 479 | void *cls, |
141 | struct | 480 | struct GNUNET_TRANSPORT_TESTING_PeerContext *me, |
142 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | 481 | const struct GNUNET_PeerIdentity *other); |
143 | *tc_h, | 482 | |
144 | const char*payload, | ||
145 | size_t payload_len); | ||
146 | 483 | ||
147 | /** | 484 | /** |
148 | * @brief Handle to a transport communicator | 485 | * Closure that must be passed to |
486 | * #GNUNET_TRANSPORT_TESTING_connect_check. | ||
149 | */ | 487 | */ |
150 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | 488 | struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext |
151 | { | 489 | { |
152 | /** | 490 | /** |
153 | * Clients | 491 | * How should we continue after the connect? |
154 | */ | 492 | */ |
155 | struct MyClient *client_head; | 493 | GNUNET_SCHEDULER_TaskCallback connect_continuation; |
156 | struct MyClient *client_tail; | ||
157 | 494 | ||
158 | /** | 495 | /** |
159 | * @brief Handle to the client | 496 | * Closure for @e connect_continuation. |
160 | */ | 497 | */ |
161 | struct GNUNET_MQ_Handle *c_mq; | 498 | void *connect_continuation_cls; |
162 | 499 | ||
163 | /** | 500 | /** |
164 | * @brief Handle to the configuration | 501 | * Which configuration file should we pass to the |
165 | */ | 502 | * #GNUNET_PROGRAM_run() of the testcase? |
166 | struct GNUNET_CONFIGURATION_Handle *cfg; | 503 | */ |
504 | const char *config_file; | ||
167 | 505 | ||
168 | /** | 506 | /** |
169 | * @brief File name of configuration file | 507 | * Receiver argument to give for peers we start. |
170 | */ | 508 | */ |
171 | char *cfg_filename; | 509 | GNUNET_TRANSPORT_TESTING_ReceiveCallback rec; |
172 | 510 | ||
173 | struct GNUNET_PeerIdentity peer_id; | 511 | /** |
512 | * Notify connect argument to give for peers we start. | ||
513 | */ | ||
514 | GNUNET_TRANSPORT_TESTING_NotifyConnect nc; | ||
174 | 515 | ||
175 | /** | 516 | /** |
176 | * @brief Handle to the transport service | 517 | * Notify disconnect argument to give for peers we start. |
177 | */ | 518 | */ |
178 | struct GNUNET_SERVICE_Handle *tsh; | 519 | GNUNET_TRANSPORT_TESTING_NotifyDisconnect nd; |
179 | 520 | ||
180 | /** | 521 | /** |
181 | * @brief Task that will be run on shutdown to stop and clean transport | 522 | * Closure for @e rec, @e nc and @e nd. |
182 | * service | ||
183 | */ | 523 | */ |
184 | struct GNUNET_SCHEDULER_Task *ts_shutdown_task; | 524 | void *cls; |
185 | 525 | ||
526 | /** | ||
527 | * Custom task to run on shutdown. | ||
528 | */ | ||
529 | GNUNET_SCHEDULER_TaskCallback shutdown_task; | ||
530 | |||
531 | /** | ||
532 | * Closure for @e shutdown_task. | ||
533 | */ | ||
534 | void *shutdown_task_cls; | ||
186 | 535 | ||
187 | /** | 536 | /** |
188 | * @brief Process of the communicator | 537 | * Custom task to run after peers were started but before we try to |
538 | * connect them. If this function is set, we wait ONE second after | ||
539 | * running this function until we continue with connecting the | ||
540 | * peers. | ||
189 | */ | 541 | */ |
190 | struct GNUNET_OS_Process *c_proc; | 542 | GNUNET_SCHEDULER_TaskCallback pre_connect_task; |
191 | 543 | ||
192 | /** | 544 | /** |
193 | * NAT process | 545 | * Closure for @e shutdown_task. |
194 | */ | 546 | */ |
195 | struct GNUNET_OS_Process *nat_proc; | 547 | void *pre_connect_task_cls; |
196 | 548 | ||
197 | /** | 549 | /** |
198 | * resolver service process | 550 | * When should the testcase time out? |
199 | */ | 551 | */ |
200 | struct GNUNET_OS_Process *resolver_proc; | 552 | struct GNUNET_TIME_Relative timeout; |
201 | 553 | ||
202 | /** | 554 | /** |
203 | * peerstore service process | 555 | * Should we try to create connections in both directions? |
204 | */ | 556 | */ |
205 | struct GNUNET_OS_Process *ps_proc; | 557 | int bi_directional; |
558 | |||
559 | /* ******* fields set by #GNUNET_TRANSPORT_TESTING_connect_check **** */ | ||
206 | 560 | ||
207 | /** | 561 | /** |
208 | * @brief Task that will be run on shutdown to stop and clean communicator | 562 | * Number of peers involved in the test. |
209 | */ | 563 | */ |
210 | struct GNUNET_SCHEDULER_Task *c_shutdown_task; | 564 | unsigned int num_peers; |
211 | 565 | ||
212 | /** | 566 | /** |
213 | * @brief Characteristics of the communicator | 567 | * Configuration files we have, array with @e num_peers entries. |
214 | */ | 568 | */ |
215 | enum GNUNET_TRANSPORT_CommunicatorCharacteristics c_characteristics; | 569 | char **cfg_files; |
216 | 570 | ||
217 | /** | 571 | /** |
218 | * @brief Specifies supported addresses | 572 | * Array with @e num_peers entries. |
219 | */ | 573 | */ |
220 | char *c_addr_prefix; | 574 | struct GNUNET_TRANSPORT_TESTING_PeerContext **p; |
221 | 575 | ||
222 | /** | 576 | /** |
223 | * @brief Specifies supported addresses | 577 | * Name of the plugin. |
224 | */ | 578 | */ |
225 | char *c_address; | 579 | const char *test_plugin; |
226 | 580 | ||
227 | /** | 581 | /** |
228 | * @brief Head of the DLL of queues associated with this communicator | 582 | * Name of the testcase. |
229 | */ | 583 | */ |
230 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *queue_head; | 584 | const char *test_name; |
231 | 585 | ||
232 | /** | 586 | /** |
233 | * @brief Tail of the DLL of queues associated with this communicator | 587 | * Configuration object for the testcase. |
234 | */ | 588 | */ |
235 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *queue_tail; | 589 | const struct GNUNET_CONFIGURATION_Handle *cfg; |
236 | 590 | ||
237 | /* Callbacks + Closures */ | ||
238 | /** | 591 | /** |
239 | * @brief Callback called when a new communicator connects | 592 | * Main testing handle. |
240 | */ | 593 | */ |
241 | GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback | 594 | struct GNUNET_TRANSPORT_TESTING_Handle *tth; |
242 | communicator_available_cb; | ||
243 | 595 | ||
244 | /** | 596 | /** |
245 | * @brief Callback called when a new communicator connects | 597 | * Result from the main function, set to #GNUNET_OK on success. |
598 | * Clients should set to #GNUNET_SYSERR to indicate test failure. | ||
246 | */ | 599 | */ |
247 | GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb; | 600 | int global_ret; |
248 | 601 | ||
249 | /** | 602 | /** |
250 | * @brief Callback called when a new communicator connects | 603 | * Generator for the `num` field in test messages. Incremented each |
604 | * time #GNUNET_TRANSPORT_TESTING_simple_send or | ||
605 | * #GNUNET_TRANSPORT_TESTING_large_send are used to transmit a | ||
606 | * message. | ||
251 | */ | 607 | */ |
252 | GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb; | 608 | uint32_t send_num_gen; |
609 | |||
610 | /* ******* internal state, clients should not mess with this **** */ | ||
253 | 611 | ||
254 | /** | 612 | /** |
255 | * @brief Callback called when a new communicator connects | 613 | * Task run on timeout. |
256 | */ | 614 | */ |
257 | GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb; | 615 | struct GNUNET_SCHEDULER_Task *timeout_task; |
258 | 616 | ||
259 | /** | 617 | /** |
260 | * @brief Callback called when a new communicator connects | 618 | * Task run to connect peers. |
261 | */ | 619 | */ |
262 | GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_msg_cb; | 620 | struct GNUNET_SCHEDULER_Task *connect_task; |
263 | 621 | ||
264 | /** | 622 | /** |
265 | * @brief Backchannel callback | 623 | * Number of peers that have been started. |
266 | */ | 624 | */ |
267 | GNUNET_TRANSPORT_TESTING_BackchannelCallback bc_cb; | 625 | unsigned int started; |
268 | 626 | ||
269 | /** | 627 | /** |
270 | * Our service handle | 628 | * DLL of active connect requests. |
271 | */ | 629 | */ |
272 | struct GNUNET_SERVICE_Handle *sh; | 630 | struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_head; |
273 | 631 | ||
274 | /** | 632 | /** |
275 | * @brief Closure to the callback | 633 | * DLL of active connect requests. |
276 | */ | 634 | */ |
277 | void *cb_cls; | 635 | struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_tail; |
278 | 636 | ||
279 | /** | 637 | /** |
280 | * Backchannel supported | 638 | * Array with @e num_peers entries. |
281 | */ | 639 | */ |
282 | int bc_enabled; | 640 | struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ip; |
283 | }; | 641 | }; |
284 | 642 | ||
643 | |||
285 | /** | 644 | /** |
286 | * @brief Start communicator part of transport service and communicator | 645 | * Find peer by peer ID. |
287 | * | 646 | * |
288 | * @param service_name Name of the service | 647 | * @param ccc context to search |
289 | * @param cfg Configuration handle | 648 | * @param peer peer to look for |
290 | * @param communicator_available Callback that is called when a new | 649 | * @return NULL if @a peer was not found |
291 | * communicator becomes available | 650 | */ |
292 | * @param add_address_cb Callback handling new addresses | 651 | struct GNUNET_TRANSPORT_TESTING_PeerContext * |
293 | * @param queue_create_reply_cb Callback handling success of queue requests | 652 | GNUNET_TRANSPORT_TESTING_find_peer ( |
294 | * @param add_queue_cb Callback handling freshly created queues | 653 | struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc, |
295 | * @param incoming_message_cb Callback handling incoming messages | 654 | const struct GNUNET_PeerIdentity *peer); |
296 | * @param cb_cls Closure to @p communicator_available | 655 | |
656 | |||
657 | /** | ||
658 | * Common implementation of the #GNUNET_TRANSPORT_TESTING_CheckCallback. | ||
659 | * Starts and connects the two peers, then invokes the | ||
660 | * `connect_continuation` from @a cls. Sets up a timeout to | ||
661 | * abort the test, and a shutdown handler to clean up properly | ||
662 | * on exit. | ||
297 | * | 663 | * |
298 | * @return Handle to the communicator duo | 664 | * @param cls closure of type `struct |
299 | */ | 665 | * GNUNET_TRANSPORT_TESTING_ConnectCheckContext` |
300 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle * | 666 | * @param tth_ initialized testing handle |
301 | GNUNET_TRANSPORT_TESTING_transport_communicator_service_start ( | 667 | * @param test_plugin_ name of the plugin |
302 | const char *service_name, | 668 | * @param test_name_ name of the test |
303 | const char *binary_name, | 669 | * @param num_peers number of entries in the @a cfg_file array |
304 | const char *cfg_filename, | 670 | * @param cfg_files array of names of configuration files for the peers |
305 | const struct GNUNET_PeerIdentity *peer_id, | 671 | * @return #GNUNET_SYSERR on error |
306 | GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback | 672 | */ |
307 | communicator_available_cb, | 673 | int |
308 | GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb, | 674 | GNUNET_TRANSPORT_TESTING_connect_check ( |
309 | GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb, | 675 | void *cls, |
310 | GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb, | 676 | struct GNUNET_TRANSPORT_TESTING_Handle *tth_, |
311 | GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_message_cb, | 677 | const char *test_plugin_, |
312 | GNUNET_TRANSPORT_TESTING_BackchannelCallback bc_cb, | 678 | const char *test_name_, |
313 | void *cb_cls); | 679 | unsigned int num_peers, |
680 | char *cfg_files[]); | ||
681 | |||
682 | |||
683 | /** | ||
684 | * Main function of a testcase. Called with the initial setup data | ||
685 | * for the test as derived from the source name and the binary name. | ||
686 | * | ||
687 | * @param cls closure | ||
688 | * @param tth_ initialized testing handle | ||
689 | * @param test_plugin_ name of the plugin | ||
690 | * @param test_name_ name of the test | ||
691 | * @param num_peers number of entries in the @a cfg_file array | ||
692 | * @param cfg_files array of names of configuration files for the peers | ||
693 | * @return #GNUNET_SYSERR on error | ||
694 | */ | ||
695 | typedef int (*GNUNET_TRANSPORT_TESTING_CheckCallback) ( | ||
696 | void *cls, | ||
697 | struct GNUNET_TRANSPORT_TESTING_Handle *tth_, | ||
698 | const char *test_plugin_, | ||
699 | const char *test_name_, | ||
700 | unsigned int num_peers, | ||
701 | char *cfg_files[]); | ||
702 | |||
703 | |||
704 | /** | ||
705 | * Setup testcase. Calls @a check with the data the test needs. | ||
706 | * | ||
707 | * @param argv0 binary name (argv[0]) | ||
708 | * @param filename source file name (__FILE__) | ||
709 | * @param num_peers number of peers to start | ||
710 | * @param check main function to run | ||
711 | * @param check_cls closure for @a check | ||
712 | * @return #GNUNET_OK on success | ||
713 | */ | ||
714 | int | ||
715 | GNUNET_TRANSPORT_TESTING_main_ (const char *argv0, | ||
716 | const char *filename, | ||
717 | unsigned int num_peers, | ||
718 | GNUNET_TRANSPORT_TESTING_CheckCallback check, | ||
719 | void *check_cls); | ||
720 | |||
721 | |||
722 | /** | ||
723 | * Setup testcase. Calls @a check with the data the test needs. | ||
724 | * | ||
725 | * @param num_peers number of peers to start | ||
726 | * @param check main function to run | ||
727 | * @param check_cls closure for @a check | ||
728 | * @return #GNUNET_OK on success | ||
729 | */ | ||
730 | #define GNUNET_TRANSPORT_TESTING_main(num_peers, check, check_cls) \ | ||
731 | GNUNET_TRANSPORT_TESTING_main_ (argv[0], \ | ||
732 | __FILE__, \ | ||
733 | num_peers, \ | ||
734 | check, \ | ||
735 | check_cls) | ||
736 | |||
737 | /* ***************** Convenience functions for sending ********* */ | ||
738 | |||
739 | /** | ||
740 | * Send a test message of type @a mtype and size @a msize from | ||
741 | * peer @a sender to peer @a receiver. The peers should be | ||
742 | * connected when this function is called. | ||
743 | * | ||
744 | * @param sender the sending peer | ||
745 | * @param receiver the receiving peer | ||
746 | * @param mtype message type to use | ||
747 | * @param msize size of the message, at least `sizeof (struct | ||
748 | * GNUNET_TRANSPORT_TESTING_TestMessage)` | ||
749 | * @param num unique message number | ||
750 | * @param cont continuation to call after transmission | ||
751 | * @param cont_cls closure for @a cont | ||
752 | * @return #GNUNET_OK if message was queued, | ||
753 | * #GNUNET_NO if peers are not connected | ||
754 | * #GNUNET_SYSERR if @a msize is illegal | ||
755 | */ | ||
756 | int | ||
757 | GNUNET_TRANSPORT_TESTING_send ( | ||
758 | struct GNUNET_TRANSPORT_TESTING_PeerContext *sender, | ||
759 | struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver, | ||
760 | uint16_t mtype, | ||
761 | uint16_t msize, | ||
762 | uint32_t num, | ||
763 | GNUNET_SCHEDULER_TaskCallback cont, | ||
764 | void *cont_cls); | ||
765 | |||
766 | |||
767 | /** | ||
768 | * Message type used by #GNUNET_TRANSPORT_TESTING_simple_send(). | ||
769 | */ | ||
770 | #define GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE 12345 | ||
771 | |||
772 | /** | ||
773 | * Alternative message type for tests. | ||
774 | */ | ||
775 | #define GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2 12346 | ||
776 | |||
777 | |||
778 | /** | ||
779 | * Type of the closure argument to pass to | ||
780 | * #GNUNET_TRANSPORT_TESTING_simple_send() and | ||
781 | * #GNUNET_TRANSPORT_TESTING_large_send(). | ||
782 | */ | ||
783 | struct GNUNET_TRANSPORT_TESTING_SendClosure | ||
784 | { | ||
785 | /** | ||
786 | * Context for the transmission. | ||
787 | */ | ||
788 | struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc; | ||
789 | |||
790 | /** | ||
791 | * Function that returns the desired message size. Overrides | ||
792 | * the message size, can be NULL in which case the message | ||
793 | * size is the default. | ||
794 | */ | ||
795 | size_t (*get_size_cb) (unsigned int n); | ||
796 | |||
797 | /** | ||
798 | * Number of messages to be transmitted in a loop. | ||
799 | * Use zero for "forever" (until external shutdown). | ||
800 | */ | ||
801 | unsigned int num_messages; | ||
802 | |||
803 | /** | ||
804 | * Function to call after all transmissions, can be NULL. | ||
805 | */ | ||
806 | GNUNET_SCHEDULER_TaskCallback cont; | ||
807 | |||
808 | /** | ||
809 | * Closure for @e cont. | ||
810 | */ | ||
811 | void *cont_cls; | ||
812 | }; | ||
314 | 813 | ||
315 | 814 | ||
815 | /** | ||
816 | * Task that sends a minimalistic test message from the | ||
817 | * first peer to the second peer. | ||
818 | * | ||
819 | * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure` | ||
820 | * which should contain at least two peers, the first two | ||
821 | * of which should be currently connected | ||
822 | */ | ||
316 | void | 823 | void |
317 | GNUNET_TRANSPORT_TESTING_transport_communicator_service_stop ( | 824 | GNUNET_TRANSPORT_TESTING_simple_send (void *cls); |
318 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h); | ||
319 | 825 | ||
826 | /** | ||
827 | * Size of a message sent with | ||
828 | * #GNUNET_TRANSPORT_TESTING_large_send(). Big enough | ||
829 | * to usually force defragmentation. | ||
830 | */ | ||
831 | #define GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE 2600 | ||
320 | 832 | ||
321 | /** | 833 | /** |
322 | * @brief Instruct communicator to open a queue | 834 | * Task that sends a large test message from the |
835 | * first peer to the second peer. | ||
323 | * | 836 | * |
324 | * @param tc_h Handle to communicator which shall open queue | 837 | * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure` |
325 | * @param peer_id Towards which peer | 838 | * which should contain at least two peers, the first two |
326 | * @param address For which address | 839 | * of which should be currently connected |
327 | */ | 840 | */ |
328 | void | 841 | void |
329 | GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (struct | 842 | GNUNET_TRANSPORT_TESTING_large_send (void *cls); |
330 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | 843 | |
331 | *tc_h, | 844 | |
332 | const struct | 845 | /* ********************** log-only convenience functions ************* */ |
333 | GNUNET_PeerIdentity | ||
334 | *peer_id, | ||
335 | const char *address); | ||
336 | 846 | ||
337 | 847 | ||
338 | /** | 848 | /** |
339 | * @brief Instruct communicator to send data | 849 | * Log a connect event. |
340 | * | 850 | * |
341 | * @param tc_queue The queue to use for sending | 851 | * @param cls NULL |
342 | * @param cont function to call when done sending | 852 | * @param me peer that had the event |
343 | * @param cont_cls closure for @a cont | 853 | * @param other peer that connected. |
344 | * @param payload Data to send | ||
345 | * @param payload_size Size of the @a payload | ||
346 | */ | 854 | */ |
347 | void | 855 | void |
348 | GNUNET_TRANSPORT_TESTING_transport_communicator_send (struct | 856 | GNUNET_TRANSPORT_TESTING_log_connect ( |
349 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | 857 | void *cls, |
350 | *tc_h, | 858 | struct GNUNET_TRANSPORT_TESTING_PeerContext *me, |
351 | GNUNET_SCHEDULER_TaskCallback | 859 | const struct GNUNET_PeerIdentity *other); |
352 | cont, | 860 | |
353 | void *cont_cls, | 861 | |
354 | const void *payload, | 862 | /** |
355 | size_t payload_size); | 863 | * Log a disconnect event. |
864 | * | ||
865 | * @param cls NULL | ||
866 | * @param me peer that had the event | ||
867 | * @param other peer that disconnected. | ||
868 | */ | ||
869 | void | ||
870 | GNUNET_TRANSPORT_TESTING_log_disconnect ( | ||
871 | void *cls, | ||
872 | struct GNUNET_TRANSPORT_TESTING_PeerContext *me, | ||
873 | const struct GNUNET_PeerIdentity *other); | ||
874 | |||
875 | |||
876 | /* ********************** low-level filename functions *************** */ | ||
877 | |||
878 | |||
879 | /** | ||
880 | * Extracts the test filename from an absolute file name and removes | ||
881 | * the extension. | ||
882 | * | ||
883 | * @param file absolute file name | ||
884 | * @return resulting test name | ||
885 | */ | ||
886 | char * | ||
887 | GNUNET_TRANSPORT_TESTING_get_test_name (const char *file); | ||
888 | |||
889 | |||
890 | /** | ||
891 | * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and | ||
892 | * if existing ".exe"-prefix and adds the peer-number | ||
893 | * | ||
894 | * @param file filename of the test, e.g. argv[0] | ||
895 | * @param count peer number | ||
896 | * @return configuration name to use | ||
897 | */ | ||
898 | char * | ||
899 | GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, int count); | ||
900 | |||
901 | |||
902 | /** | ||
903 | * Extracts the plugin anme from an absolute file name and the test name | ||
904 | * @param file absolute file name | ||
905 | * @param test test name | ||
906 | * @return the plugin name | ||
907 | */ | ||
908 | char * | ||
909 | GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *executable, | ||
910 | const char *testname); | ||
911 | |||
912 | |||
913 | /** | ||
914 | * Extracts the filename from an absolute file name and removes the | ||
915 | * extenstion | ||
916 | * | ||
917 | * @param file absolute file name | ||
918 | * @return the source name | ||
919 | */ | ||
920 | char * | ||
921 | GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file); | ||
922 | |||
923 | #endif | ||
924 | /* end of transport_testing.h */ | ||