diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-01-26 20:49:59 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-01-26 20:49:59 +0100 |
commit | 7fc78674a6d3f2edb41e32cea59200908d0b344b (patch) | |
tree | b99608184fa08dcc6cd3e297369cb2761a03524f /src/cadet | |
parent | a5a54c51011a9498e72b2a9341b35333c6beef1a (diff) | |
download | gnunet-7fc78674a6d3f2edb41e32cea59200908d0b344b.tar.gz gnunet-7fc78674a6d3f2edb41e32cea59200908d0b344b.zip |
starting with #5385 in earnest
Diffstat (limited to 'src/cadet')
-rw-r--r-- | src/cadet/Makefile.am | 3 | ||||
-rw-r--r-- | src/cadet/cadet_api.c | 571 | ||||
-rw-r--r-- | src/cadet/cadet_api_get_channel.c | 96 | ||||
-rw-r--r-- | src/cadet/cadet_api_get_peer.c | 219 | ||||
-rw-r--r-- | src/cadet/cadet_api_helper.c | 55 | ||||
-rw-r--r-- | src/cadet/cadet_api_list_peers.c | 179 | ||||
-rw-r--r-- | src/cadet/cadet_api_list_tunnels.c | 187 | ||||
-rw-r--r-- | src/cadet/gnunet-cadet.c | 91 |
8 files changed, 773 insertions, 628 deletions
diff --git a/src/cadet/Makefile.am b/src/cadet/Makefile.am index b2d436061..bccaf2cd9 100644 --- a/src/cadet/Makefile.am +++ b/src/cadet/Makefile.am | |||
@@ -33,7 +33,8 @@ lib_LTLIBRARIES = \ | |||
33 | $(EXP_LIB) | 33 | $(EXP_LIB) |
34 | 34 | ||
35 | libgnunetcadet_la_SOURCES = \ | 35 | libgnunetcadet_la_SOURCES = \ |
36 | cadet_api.c | 36 | cadet_api.c \ |
37 | cadet_api_helper.c | ||
37 | libgnunetcadet_la_LIBADD = \ | 38 | libgnunetcadet_la_LIBADD = \ |
38 | $(top_builddir)/src/util/libgnunetutil.la \ | 39 | $(top_builddir)/src/util/libgnunetutil.la \ |
39 | $(XLIB) \ | 40 | $(XLIB) \ |
diff --git a/src/cadet/cadet_api.c b/src/cadet/cadet_api.c index f9b81a82b..ce2356216 100644 --- a/src/cadet/cadet_api.c +++ b/src/cadet/cadet_api.c | |||
@@ -33,39 +33,6 @@ | |||
33 | #define LOG(kind,...) GNUNET_log_from (kind, "cadet-api",__VA_ARGS__) | 33 | #define LOG(kind,...) GNUNET_log_from (kind, "cadet-api",__VA_ARGS__) |
34 | 34 | ||
35 | /** | 35 | /** |
36 | * Ugly legacy hack. | ||
37 | */ | ||
38 | union CadetInfoCB | ||
39 | { | ||
40 | |||
41 | /** | ||
42 | * Channel callback. | ||
43 | */ | ||
44 | GNUNET_CADET_ChannelCB channel_cb; | ||
45 | |||
46 | /** | ||
47 | * Monitor callback | ||
48 | */ | ||
49 | GNUNET_CADET_PeersCB peers_cb; | ||
50 | |||
51 | /** | ||
52 | * Monitor callback | ||
53 | */ | ||
54 | GNUNET_CADET_PeerCB peer_cb; | ||
55 | |||
56 | /** | ||
57 | * Monitor callback | ||
58 | */ | ||
59 | GNUNET_CADET_TunnelsCB tunnels_cb; | ||
60 | |||
61 | /** | ||
62 | * Tunnel callback. | ||
63 | */ | ||
64 | GNUNET_CADET_TunnelCB tunnel_cb; | ||
65 | }; | ||
66 | |||
67 | |||
68 | /** | ||
69 | * Opaque handle to the service. | 36 | * Opaque handle to the service. |
70 | */ | 37 | */ |
71 | struct GNUNET_CADET_Handle | 38 | struct GNUNET_CADET_Handle |
@@ -101,16 +68,6 @@ struct GNUNET_CADET_Handle | |||
101 | struct GNUNET_SCHEDULER_Task *reconnect_task; | 68 | struct GNUNET_SCHEDULER_Task *reconnect_task; |
102 | 69 | ||
103 | /** | 70 | /** |
104 | * Callback for an info task (only one active at a time). | ||
105 | */ | ||
106 | union CadetInfoCB info_cb; | ||
107 | |||
108 | /** | ||
109 | * Info callback closure for @c info_cb. | ||
110 | */ | ||
111 | void *info_cls; | ||
112 | |||
113 | /** | ||
114 | * Time to the next reconnect in case one reconnect fails | 71 | * Time to the next reconnect in case one reconnect fails |
115 | */ | 72 | */ |
116 | struct GNUNET_TIME_Relative reconnect_time; | 73 | struct GNUNET_TIME_Relative reconnect_time; |
@@ -854,288 +811,6 @@ handle_mq_error (void *cls, | |||
854 | 811 | ||
855 | 812 | ||
856 | /** | 813 | /** |
857 | * Check that message received from CADET service is well-formed. | ||
858 | * | ||
859 | * @param cls the `struct GNUNET_CADET_Handle` | ||
860 | * @param message the message we got | ||
861 | * @return #GNUNET_OK if the message is well-formed, | ||
862 | * #GNUNET_SYSERR otherwise | ||
863 | */ | ||
864 | static int | ||
865 | check_get_peers (void *cls, | ||
866 | const struct GNUNET_MessageHeader *message) | ||
867 | { | ||
868 | size_t esize; | ||
869 | |||
870 | (void) cls; | ||
871 | esize = ntohs (message->size); | ||
872 | if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == esize) | ||
873 | return GNUNET_OK; | ||
874 | if (sizeof (struct GNUNET_MessageHeader) == esize) | ||
875 | return GNUNET_OK; | ||
876 | return GNUNET_SYSERR; | ||
877 | } | ||
878 | |||
879 | |||
880 | /** | ||
881 | * Process a local reply about info on all tunnels, pass info to the user. | ||
882 | * | ||
883 | * @param cls Closure (Cadet handle). | ||
884 | * @param msg Message itself. | ||
885 | */ | ||
886 | static void | ||
887 | handle_get_peers (void *cls, | ||
888 | const struct GNUNET_MessageHeader *msg) | ||
889 | { | ||
890 | struct GNUNET_CADET_Handle *h = cls; | ||
891 | const struct GNUNET_CADET_LocalInfoPeer *info = | ||
892 | (const struct GNUNET_CADET_LocalInfoPeer *) msg; | ||
893 | |||
894 | if (NULL == h->info_cb.peers_cb) | ||
895 | return; | ||
896 | if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == ntohs (msg->size)) | ||
897 | h->info_cb.peers_cb (h->info_cls, | ||
898 | &info->destination, | ||
899 | (int) ntohs (info->tunnel), | ||
900 | (unsigned int) ntohs (info->paths), | ||
901 | 0); | ||
902 | else | ||
903 | h->info_cb.peers_cb (h->info_cls, | ||
904 | NULL, | ||
905 | 0, | ||
906 | 0, | ||
907 | 0); | ||
908 | } | ||
909 | |||
910 | |||
911 | /** | ||
912 | * Check that message received from CADET service is well-formed. | ||
913 | * | ||
914 | * @param cls the `struct GNUNET_CADET_Handle` | ||
915 | * @param message the message we got | ||
916 | * @return #GNUNET_OK if the message is well-formed, | ||
917 | * #GNUNET_SYSERR otherwise | ||
918 | */ | ||
919 | static int | ||
920 | check_get_peer (void *cls, | ||
921 | const struct GNUNET_CADET_LocalInfoPeer *message) | ||
922 | { | ||
923 | size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer); | ||
924 | size_t esize; | ||
925 | |||
926 | (void) cls; | ||
927 | esize = ntohs (message->header.size); | ||
928 | if (esize < msize) | ||
929 | { | ||
930 | GNUNET_break (0); | ||
931 | return GNUNET_SYSERR; | ||
932 | } | ||
933 | if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity))) | ||
934 | { | ||
935 | GNUNET_break (0); | ||
936 | return GNUNET_SYSERR; | ||
937 | } | ||
938 | return GNUNET_OK; | ||
939 | } | ||
940 | |||
941 | |||
942 | /** | ||
943 | * Process a local peer info reply, pass info to the user. | ||
944 | * | ||
945 | * @param cls Closure (Cadet handle). | ||
946 | * @param message Message itself. | ||
947 | */ | ||
948 | static void | ||
949 | handle_get_peer (void *cls, | ||
950 | const struct GNUNET_CADET_LocalInfoPeer *message) | ||
951 | { | ||
952 | struct GNUNET_CADET_Handle *h = cls; | ||
953 | const struct GNUNET_PeerIdentity *paths_array; | ||
954 | unsigned int paths; | ||
955 | unsigned int path_length; | ||
956 | int neighbor; | ||
957 | unsigned int peers; | ||
958 | |||
959 | if (NULL == h->info_cb.peer_cb) | ||
960 | return; | ||
961 | |||
962 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
963 | "number of paths %u\n", | ||
964 | ntohs (message->paths)); | ||
965 | |||
966 | paths = ntohs (message->paths); | ||
967 | paths_array = (const struct GNUNET_PeerIdentity *) &message[1]; | ||
968 | peers = (ntohs (message->header.size) - sizeof (*message)) | ||
969 | / sizeof (struct GNUNET_PeerIdentity); | ||
970 | path_length = 0; | ||
971 | neighbor = GNUNET_NO; | ||
972 | |||
973 | for (unsigned int i = 0; i < peers; i++) | ||
974 | { | ||
975 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
976 | " %s\n", | ||
977 | GNUNET_i2s (&paths_array[i])); | ||
978 | path_length++; | ||
979 | if (0 == memcmp (&paths_array[i], &message->destination, | ||
980 | sizeof (struct GNUNET_PeerIdentity))) | ||
981 | { | ||
982 | if (1 == path_length) | ||
983 | neighbor = GNUNET_YES; | ||
984 | path_length = 0; | ||
985 | } | ||
986 | } | ||
987 | |||
988 | /* Call Callback with tunnel info. */ | ||
989 | paths_array = (const struct GNUNET_PeerIdentity *) &message[1]; | ||
990 | h->info_cb.peer_cb (h->info_cls, | ||
991 | &message->destination, | ||
992 | (int) ntohs (message->tunnel), | ||
993 | neighbor, | ||
994 | paths, | ||
995 | paths_array, | ||
996 | (int) ntohs (message->offset), | ||
997 | (int) ntohs (message->finished_with_paths)); | ||
998 | } | ||
999 | |||
1000 | |||
1001 | /** | ||
1002 | * Check that message received from CADET service is well-formed. | ||
1003 | * | ||
1004 | * @param cls the `struct GNUNET_CADET_Handle` | ||
1005 | * @param message the message we got | ||
1006 | * @return #GNUNET_OK if the message is well-formed, | ||
1007 | * #GNUNET_SYSERR otherwise | ||
1008 | */ | ||
1009 | static int | ||
1010 | check_get_tunnels (void *cls, | ||
1011 | const struct GNUNET_MessageHeader *message) | ||
1012 | { | ||
1013 | size_t esize; | ||
1014 | |||
1015 | (void) cls; | ||
1016 | esize = ntohs (message->size); | ||
1017 | if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) == esize) | ||
1018 | return GNUNET_OK; | ||
1019 | if (sizeof (struct GNUNET_MessageHeader) == esize) | ||
1020 | return GNUNET_OK; | ||
1021 | return GNUNET_SYSERR; | ||
1022 | } | ||
1023 | |||
1024 | |||
1025 | /** | ||
1026 | * Process a local reply about info on all tunnels, pass info to the user. | ||
1027 | * | ||
1028 | * @param cls Closure (Cadet handle). | ||
1029 | * @param message Message itself. | ||
1030 | */ | ||
1031 | static void | ||
1032 | handle_get_tunnels (void *cls, | ||
1033 | const struct GNUNET_MessageHeader *msg) | ||
1034 | { | ||
1035 | struct GNUNET_CADET_Handle *h = cls; | ||
1036 | const struct GNUNET_CADET_LocalInfoTunnel *info = | ||
1037 | (const struct GNUNET_CADET_LocalInfoTunnel *) msg; | ||
1038 | |||
1039 | if (NULL == h->info_cb.tunnels_cb) | ||
1040 | return; | ||
1041 | if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) == ntohs (msg->size)) | ||
1042 | h->info_cb.tunnels_cb (h->info_cls, | ||
1043 | &info->destination, | ||
1044 | ntohl (info->channels), | ||
1045 | ntohl (info->connections), | ||
1046 | ntohs (info->estate), | ||
1047 | ntohs (info->cstate)); | ||
1048 | else | ||
1049 | h->info_cb.tunnels_cb (h->info_cls, | ||
1050 | NULL, | ||
1051 | 0, | ||
1052 | 0, | ||
1053 | 0, | ||
1054 | 0); | ||
1055 | } | ||
1056 | |||
1057 | |||
1058 | /** | ||
1059 | * Check that message received from CADET service is well-formed. | ||
1060 | * | ||
1061 | * @param cls the `struct GNUNET_CADET_Handle` | ||
1062 | * @param msg the message we got | ||
1063 | * @return #GNUNET_OK if the message is well-formed, | ||
1064 | * #GNUNET_SYSERR otherwise | ||
1065 | */ | ||
1066 | static int | ||
1067 | check_get_tunnel (void *cls, | ||
1068 | const struct GNUNET_CADET_LocalInfoTunnel *msg) | ||
1069 | { | ||
1070 | unsigned int ch_n; | ||
1071 | unsigned int c_n; | ||
1072 | size_t esize; | ||
1073 | size_t msize; | ||
1074 | |||
1075 | (void) cls; | ||
1076 | /* Verify message sanity */ | ||
1077 | msize = ntohs (msg->header.size); | ||
1078 | esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel); | ||
1079 | if (esize > msize) | ||
1080 | { | ||
1081 | GNUNET_break (0); | ||
1082 | return GNUNET_SYSERR; | ||
1083 | } | ||
1084 | ch_n = ntohl (msg->channels); | ||
1085 | c_n = ntohl (msg->connections); | ||
1086 | esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber); | ||
1087 | esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier); | ||
1088 | if (msize != esize) | ||
1089 | { | ||
1090 | GNUNET_break_op (0); | ||
1091 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1092 | "m:%u, e: %u (%u ch, %u conn)\n", | ||
1093 | (unsigned int) msize, | ||
1094 | (unsigned int) esize, | ||
1095 | ch_n, | ||
1096 | c_n); | ||
1097 | return GNUNET_SYSERR; | ||
1098 | } | ||
1099 | return GNUNET_OK; | ||
1100 | } | ||
1101 | |||
1102 | |||
1103 | /** | ||
1104 | * Process a local tunnel info reply, pass info to the user. | ||
1105 | * | ||
1106 | * @param cls Closure (Cadet handle). | ||
1107 | * @param msg Message itself. | ||
1108 | */ | ||
1109 | static void | ||
1110 | handle_get_tunnel (void *cls, | ||
1111 | const struct GNUNET_CADET_LocalInfoTunnel *msg) | ||
1112 | { | ||
1113 | struct GNUNET_CADET_Handle *h = cls; | ||
1114 | unsigned int ch_n; | ||
1115 | unsigned int c_n; | ||
1116 | const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns; | ||
1117 | const struct GNUNET_CADET_ChannelTunnelNumber *chns; | ||
1118 | |||
1119 | if (NULL == h->info_cb.tunnel_cb) | ||
1120 | return; | ||
1121 | ch_n = ntohl (msg->channels); | ||
1122 | c_n = ntohl (msg->connections); | ||
1123 | |||
1124 | /* Call Callback with tunnel info. */ | ||
1125 | conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1]; | ||
1126 | chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n]; | ||
1127 | h->info_cb.tunnel_cb (h->info_cls, | ||
1128 | &msg->destination, | ||
1129 | ch_n, | ||
1130 | c_n, | ||
1131 | chns, | ||
1132 | conns, | ||
1133 | ntohs (msg->estate), | ||
1134 | ntohs (msg->cstate)); | ||
1135 | } | ||
1136 | |||
1137 | |||
1138 | /** | ||
1139 | * Reconnect to the service, retransmit all infomation to try to restore the | 814 | * Reconnect to the service, retransmit all infomation to try to restore the |
1140 | * original state. | 815 | * original state. |
1141 | * | 816 | * |
@@ -1161,22 +836,6 @@ reconnect (struct GNUNET_CADET_Handle *h) | |||
1161 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK, | 836 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK, |
1162 | struct GNUNET_CADET_LocalAck, | 837 | struct GNUNET_CADET_LocalAck, |
1163 | h), | 838 | h), |
1164 | GNUNET_MQ_hd_var_size (get_peers, | ||
1165 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS, | ||
1166 | struct GNUNET_MessageHeader, | ||
1167 | h), | ||
1168 | GNUNET_MQ_hd_var_size (get_peer, | ||
1169 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER, | ||
1170 | struct GNUNET_CADET_LocalInfoPeer, | ||
1171 | h), | ||
1172 | GNUNET_MQ_hd_var_size (get_tunnels, | ||
1173 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS, | ||
1174 | struct GNUNET_MessageHeader, | ||
1175 | h), | ||
1176 | GNUNET_MQ_hd_var_size (get_tunnel, | ||
1177 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL, | ||
1178 | struct GNUNET_CADET_LocalInfoTunnel, | ||
1179 | h), | ||
1180 | GNUNET_MQ_handler_end () | 839 | GNUNET_MQ_handler_end () |
1181 | }; | 840 | }; |
1182 | 841 | ||
@@ -1370,236 +1029,6 @@ GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel) | |||
1370 | 1029 | ||
1371 | 1030 | ||
1372 | /** | 1031 | /** |
1373 | * Send message of @a type to CADET service of @a h | ||
1374 | * | ||
1375 | * @param h handle to CADET service | ||
1376 | * @param type message type of trivial information request to send | ||
1377 | */ | ||
1378 | static void | ||
1379 | send_info_request (struct GNUNET_CADET_Handle *h, | ||
1380 | uint16_t type) | ||
1381 | { | ||
1382 | struct GNUNET_MessageHeader *msg; | ||
1383 | struct GNUNET_MQ_Envelope *env; | ||
1384 | |||
1385 | env = GNUNET_MQ_msg (msg, | ||
1386 | type); | ||
1387 | GNUNET_MQ_send (h->mq, | ||
1388 | env); | ||
1389 | } | ||
1390 | |||
1391 | |||
1392 | /** | ||
1393 | * Request a debug dump on the service's STDERR. | ||
1394 | * | ||
1395 | * WARNING: unstable API, likely to change in the future! | ||
1396 | * | ||
1397 | * @param h cadet handle | ||
1398 | */ | ||
1399 | void | ||
1400 | GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h) | ||
1401 | { | ||
1402 | send_info_request (h, | ||
1403 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP); | ||
1404 | } | ||
1405 | |||
1406 | |||
1407 | /** | ||
1408 | * Request information about peers known to the running cadet service. | ||
1409 | * The callback will be called for every peer known to the service. | ||
1410 | * Only one info request (of any kind) can be active at once. | ||
1411 | * | ||
1412 | * WARNING: unstable API, likely to change in the future! | ||
1413 | * | ||
1414 | * @param h Handle to the cadet peer. | ||
1415 | * @param callback Function to call with the requested data. | ||
1416 | * @param callback_cls Closure for @c callback. | ||
1417 | * @return #GNUNET_OK / #GNUNET_SYSERR | ||
1418 | */ | ||
1419 | int | ||
1420 | GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h, | ||
1421 | GNUNET_CADET_PeersCB callback, | ||
1422 | void *callback_cls) | ||
1423 | { | ||
1424 | if (NULL != h->info_cb.peers_cb) | ||
1425 | { | ||
1426 | GNUNET_break (0); | ||
1427 | return GNUNET_SYSERR; | ||
1428 | } | ||
1429 | send_info_request (h, | ||
1430 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS); | ||
1431 | h->info_cb.peers_cb = callback; | ||
1432 | h->info_cls = callback_cls; | ||
1433 | return GNUNET_OK; | ||
1434 | } | ||
1435 | |||
1436 | |||
1437 | /** | ||
1438 | * Cancel a peer info request. The callback will not be called (anymore). | ||
1439 | * | ||
1440 | * WARNING: unstable API, likely to change in the future! | ||
1441 | * | ||
1442 | * @param h Cadet handle. | ||
1443 | * @return Closure given to GNUNET_CADET_get_peers(). | ||
1444 | */ | ||
1445 | void * | ||
1446 | GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h) | ||
1447 | { | ||
1448 | void *cls = h->info_cls; | ||
1449 | |||
1450 | h->info_cb.peers_cb = NULL; | ||
1451 | h->info_cls = NULL; | ||
1452 | return cls; | ||
1453 | } | ||
1454 | |||
1455 | |||
1456 | /** | ||
1457 | * Request information about a peer known to the running cadet peer. | ||
1458 | * The callback will be called for the tunnel once. | ||
1459 | * Only one info request (of any kind) can be active at once. | ||
1460 | * | ||
1461 | * WARNING: unstable API, likely to change in the future! | ||
1462 | * | ||
1463 | * @param h Handle to the cadet peer. | ||
1464 | * @param id Peer whose tunnel to examine. | ||
1465 | * @param callback Function to call with the requested data. | ||
1466 | * @param callback_cls Closure for @c callback. | ||
1467 | * @return #GNUNET_OK / #GNUNET_SYSERR | ||
1468 | */ | ||
1469 | int | ||
1470 | GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h, | ||
1471 | const struct GNUNET_PeerIdentity *id, | ||
1472 | GNUNET_CADET_PeerCB callback, | ||
1473 | void *callback_cls) | ||
1474 | { | ||
1475 | struct GNUNET_CADET_LocalInfo *msg; | ||
1476 | struct GNUNET_MQ_Envelope *env; | ||
1477 | |||
1478 | if (NULL != h->info_cb.peer_cb) | ||
1479 | { | ||
1480 | GNUNET_break (0); | ||
1481 | return GNUNET_SYSERR; | ||
1482 | } | ||
1483 | env = GNUNET_MQ_msg (msg, | ||
1484 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER); | ||
1485 | msg->peer = *id; | ||
1486 | GNUNET_MQ_send (h->mq, | ||
1487 | env); | ||
1488 | h->info_cb.peer_cb = callback; | ||
1489 | h->info_cls = callback_cls; | ||
1490 | return GNUNET_OK; | ||
1491 | } | ||
1492 | |||
1493 | |||
1494 | /** | ||
1495 | * Request information about tunnels of the running cadet peer. | ||
1496 | * The callback will be called for every tunnel of the service. | ||
1497 | * Only one info request (of any kind) can be active at once. | ||
1498 | * | ||
1499 | * WARNING: unstable API, likely to change in the future! | ||
1500 | * | ||
1501 | * @param h Handle to the cadet peer. | ||
1502 | * @param callback Function to call with the requested data. | ||
1503 | * @param callback_cls Closure for @c callback. | ||
1504 | * @return #GNUNET_OK / #GNUNET_SYSERR | ||
1505 | */ | ||
1506 | int | ||
1507 | GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h, | ||
1508 | GNUNET_CADET_TunnelsCB callback, | ||
1509 | void *callback_cls) | ||
1510 | { | ||
1511 | if (NULL != h->info_cb.tunnels_cb) | ||
1512 | { | ||
1513 | GNUNET_break (0); | ||
1514 | return GNUNET_SYSERR; | ||
1515 | } | ||
1516 | send_info_request (h, | ||
1517 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS); | ||
1518 | h->info_cb.tunnels_cb = callback; | ||
1519 | h->info_cls = callback_cls; | ||
1520 | return GNUNET_OK; | ||
1521 | } | ||
1522 | |||
1523 | |||
1524 | /** | ||
1525 | * Cancel a monitor request. The monitor callback will not be called. | ||
1526 | * | ||
1527 | * @param h Cadet handle. | ||
1528 | * @return Closure given to GNUNET_CADET_get_tunnels(). | ||
1529 | */ | ||
1530 | void * | ||
1531 | GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h) | ||
1532 | { | ||
1533 | void *cls = h->info_cls; | ||
1534 | |||
1535 | h->info_cb.tunnels_cb = NULL; | ||
1536 | h->info_cls = NULL; | ||
1537 | return cls; | ||
1538 | } | ||
1539 | |||
1540 | |||
1541 | /** | ||
1542 | * Request information about a tunnel of the running cadet peer. | ||
1543 | * The callback will be called for the tunnel once. | ||
1544 | * Only one info request (of any kind) can be active at once. | ||
1545 | * | ||
1546 | * WARNING: unstable API, likely to change in the future! | ||
1547 | * | ||
1548 | * @param h Handle to the cadet peer. | ||
1549 | * @param id Peer whose tunnel to examine. | ||
1550 | * @param callback Function to call with the requested data. | ||
1551 | * @param callback_cls Closure for @c callback. | ||
1552 | * @return #GNUNET_OK / #GNUNET_SYSERR | ||
1553 | */ | ||
1554 | int | ||
1555 | GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h, | ||
1556 | const struct GNUNET_PeerIdentity *id, | ||
1557 | GNUNET_CADET_TunnelCB callback, | ||
1558 | void *callback_cls) | ||
1559 | { | ||
1560 | struct GNUNET_CADET_LocalInfo *msg; | ||
1561 | struct GNUNET_MQ_Envelope *env; | ||
1562 | |||
1563 | if (NULL != h->info_cb.tunnel_cb) | ||
1564 | { | ||
1565 | GNUNET_break (0); | ||
1566 | return GNUNET_SYSERR; | ||
1567 | } | ||
1568 | env = GNUNET_MQ_msg (msg, | ||
1569 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL); | ||
1570 | msg->peer = *id; | ||
1571 | GNUNET_MQ_send (h->mq, | ||
1572 | env); | ||
1573 | h->info_cb.tunnel_cb = callback; | ||
1574 | h->info_cls = callback_cls; | ||
1575 | return GNUNET_OK; | ||
1576 | } | ||
1577 | |||
1578 | |||
1579 | /** | ||
1580 | * Transitional function to convert an unsigned int port to a hash value. | ||
1581 | * WARNING: local static value returned, NOT reentrant! | ||
1582 | * WARNING: do not use this function for new code! | ||
1583 | * | ||
1584 | * @param port Numerical port (unsigned int format). | ||
1585 | * | ||
1586 | * @return A GNUNET_HashCode usable for the new CADET API. | ||
1587 | */ | ||
1588 | const struct GNUNET_HashCode * | ||
1589 | GC_u2h (uint32_t port) | ||
1590 | { | ||
1591 | static struct GNUNET_HashCode hash; | ||
1592 | |||
1593 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1594 | "This is a transitional function, use proper crypto hashes as CADET ports\n"); | ||
1595 | GNUNET_CRYPTO_hash (&port, | ||
1596 | sizeof (port), | ||
1597 | &hash); | ||
1598 | return &hash; | ||
1599 | } | ||
1600 | |||
1601 | |||
1602 | /** | ||
1603 | * Connect to the MQ-based cadet service. | 1032 | * Connect to the MQ-based cadet service. |
1604 | * | 1033 | * |
1605 | * @param cfg Configuration to use. | 1034 | * @param cfg Configuration to use. |
diff --git a/src/cadet/cadet_api_get_channel.c b/src/cadet/cadet_api_get_channel.c new file mode 100644 index 000000000..c36766a3f --- /dev/null +++ b/src/cadet/cadet_api_get_channel.c | |||
@@ -0,0 +1,96 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011, 2017 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 | * @file cadet/cadet_api_get_channel.c | ||
22 | * @brief cadet api: client implementation of cadet service | ||
23 | * @author Bartlomiej Polot | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_constants.h" | ||
29 | #include "gnunet_cadet_service.h" | ||
30 | #include "cadet.h" | ||
31 | #include "cadet_protocol.h" | ||
32 | |||
33 | |||
34 | |||
35 | /** | ||
36 | * Ugly legacy hack. | ||
37 | */ | ||
38 | struct GNUNET_CADET_ChannelMonitor | ||
39 | { | ||
40 | |||
41 | /** | ||
42 | * Channel callback. | ||
43 | */ | ||
44 | GNUNET_CADET_ChannelCB channel_cb; | ||
45 | |||
46 | /** | ||
47 | * Info callback closure for @c channel_cb. | ||
48 | */ | ||
49 | void *channel_cb_cls; | ||
50 | |||
51 | }; | ||
52 | |||
53 | |||
54 | /** | ||
55 | * Send message of @a type to CADET service of @a h | ||
56 | * | ||
57 | * @param h handle to CADET service | ||
58 | * @param type message type of trivial information request to send | ||
59 | */ | ||
60 | static void | ||
61 | send_info_request (struct GNUNET_CADET_Handle *h, | ||
62 | uint16_t type) | ||
63 | { | ||
64 | struct GNUNET_MessageHeader *msg; | ||
65 | struct GNUNET_MQ_Envelope *env; | ||
66 | |||
67 | env = GNUNET_MQ_msg (msg, | ||
68 | type); | ||
69 | GNUNET_MQ_send (h->mq, | ||
70 | env); | ||
71 | } | ||
72 | |||
73 | /** | ||
74 | * Request information about a specific channel of the running cadet peer. | ||
75 | * | ||
76 | * WARNING: unstable API, likely to change in the future! | ||
77 | * | ||
78 | * @param h Handle to the cadet peer. | ||
79 | * @param peer ID of the other end of the channel. | ||
80 | * @param channel_number Channel number. | ||
81 | * @param callback Function to call with the requested data. | ||
82 | * @param callback_cls Closure for @c callback. | ||
83 | */ | ||
84 | struct GNUNET_CADET_ChannelMonitor * | ||
85 | GNUNET_CADET_get_channel (struct GNUNET_CADET_Handle *h, | ||
86 | struct GNUNET_PeerIdentity *peer, | ||
87 | uint32_t /* UGH */ channel_number, | ||
88 | GNUNET_CADET_ChannelCB callback, | ||
89 | void *callback_cls) | ||
90 | { | ||
91 | } | ||
92 | |||
93 | |||
94 | void * | ||
95 | GNUNET_CADET_get_channel_cancel (struct GNUNET_CADET_ChannelMonitor *cm); | ||
96 | |||
diff --git a/src/cadet/cadet_api_get_peer.c b/src/cadet/cadet_api_get_peer.c new file mode 100644 index 000000000..b29b66e1c --- /dev/null +++ b/src/cadet/cadet_api_get_peer.c | |||
@@ -0,0 +1,219 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011, 2017 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 | * @file cadet/cadet_api_get_peer.c | ||
22 | * @brief cadet api: client implementation of cadet service | ||
23 | * @author Bartlomiej Polot | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_constants.h" | ||
29 | #include "gnunet_cadet_service.h" | ||
30 | #include "cadet.h" | ||
31 | #include "cadet_protocol.h" | ||
32 | |||
33 | |||
34 | |||
35 | /** | ||
36 | * Ugly legacy hack. | ||
37 | */ | ||
38 | struct GNUNET_CADET_GetPeer | ||
39 | { | ||
40 | |||
41 | /** | ||
42 | * Monitor callback | ||
43 | */ | ||
44 | GNUNET_CADET_PeerCB peer_cb; | ||
45 | |||
46 | /** | ||
47 | * Info callback closure for @c info_cb. | ||
48 | */ | ||
49 | void *peer_cb_cls; | ||
50 | |||
51 | |||
52 | }; | ||
53 | |||
54 | |||
55 | /** | ||
56 | * Send message of @a type to CADET service of @a h | ||
57 | * | ||
58 | * @param h handle to CADET service | ||
59 | * @param type message type of trivial information request to send | ||
60 | */ | ||
61 | static void | ||
62 | send_info_request (struct GNUNET_CADET_Handle *h, | ||
63 | uint16_t type) | ||
64 | { | ||
65 | struct GNUNET_MessageHeader *msg; | ||
66 | struct GNUNET_MQ_Envelope *env; | ||
67 | |||
68 | env = GNUNET_MQ_msg (msg, | ||
69 | type); | ||
70 | GNUNET_MQ_send (h->mq, | ||
71 | env); | ||
72 | } | ||
73 | |||
74 | |||
75 | /** | ||
76 | * Check that message received from CADET service is well-formed. | ||
77 | * | ||
78 | * @param cls the `struct GNUNET_CADET_Handle` | ||
79 | * @param message the message we got | ||
80 | * @return #GNUNET_OK if the message is well-formed, | ||
81 | * #GNUNET_SYSERR otherwise | ||
82 | */ | ||
83 | static int | ||
84 | check_get_peer (void *cls, | ||
85 | const struct GNUNET_CADET_LocalInfoPeer *message) | ||
86 | { | ||
87 | size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer); | ||
88 | size_t esize; | ||
89 | |||
90 | (void) cls; | ||
91 | esize = ntohs (message->header.size); | ||
92 | if (esize < msize) | ||
93 | { | ||
94 | GNUNET_break (0); | ||
95 | return GNUNET_SYSERR; | ||
96 | } | ||
97 | if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity))) | ||
98 | { | ||
99 | GNUNET_break (0); | ||
100 | return GNUNET_SYSERR; | ||
101 | } | ||
102 | return GNUNET_OK; | ||
103 | } | ||
104 | |||
105 | |||
106 | /** | ||
107 | * Process a local peer info reply, pass info to the user. | ||
108 | * | ||
109 | * @param cls Closure (Cadet handle). | ||
110 | * @param message Message itself. | ||
111 | */ | ||
112 | static void | ||
113 | handle_get_peer (void *cls, | ||
114 | const struct GNUNET_CADET_LocalInfoPeer *message) | ||
115 | { | ||
116 | struct GNUNET_CADET_Handle *h = cls; | ||
117 | const struct GNUNET_PeerIdentity *paths_array; | ||
118 | unsigned int paths; | ||
119 | unsigned int path_length; | ||
120 | int neighbor; | ||
121 | unsigned int peers; | ||
122 | |||
123 | if (NULL == h->info_cb.peer_cb) | ||
124 | return; | ||
125 | |||
126 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
127 | "number of paths %u\n", | ||
128 | ntohs (message->paths)); | ||
129 | |||
130 | paths = ntohs (message->paths); | ||
131 | paths_array = (const struct GNUNET_PeerIdentity *) &message[1]; | ||
132 | peers = (ntohs (message->header.size) - sizeof (*message)) | ||
133 | / sizeof (struct GNUNET_PeerIdentity); | ||
134 | path_length = 0; | ||
135 | neighbor = GNUNET_NO; | ||
136 | |||
137 | for (unsigned int i = 0; i < peers; i++) | ||
138 | { | ||
139 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
140 | " %s\n", | ||
141 | GNUNET_i2s (&paths_array[i])); | ||
142 | path_length++; | ||
143 | if (0 == memcmp (&paths_array[i], &message->destination, | ||
144 | sizeof (struct GNUNET_PeerIdentity))) | ||
145 | { | ||
146 | if (1 == path_length) | ||
147 | neighbor = GNUNET_YES; | ||
148 | path_length = 0; | ||
149 | } | ||
150 | } | ||
151 | |||
152 | /* Call Callback with tunnel info. */ | ||
153 | paths_array = (const struct GNUNET_PeerIdentity *) &message[1]; | ||
154 | h->info_cb.peer_cb (h->info_cls, | ||
155 | &message->destination, | ||
156 | (int) ntohs (message->tunnel), | ||
157 | neighbor, | ||
158 | paths, | ||
159 | paths_array, | ||
160 | (int) ntohs (message->offset), | ||
161 | (int) ntohs (message->finished_with_paths)); | ||
162 | } | ||
163 | |||
164 | |||
165 | |||
166 | |||
167 | /** | ||
168 | * Request information about a peer known to the running cadet peer. | ||
169 | * The callback will be called for the tunnel once. | ||
170 | * Only one info request (of any kind) can be active at once. | ||
171 | * | ||
172 | * WARNING: unstable API, likely to change in the future! | ||
173 | * | ||
174 | * @param h Handle to the cadet peer. | ||
175 | * @param id Peer whose tunnel to examine. | ||
176 | * @param callback Function to call with the requested data. | ||
177 | * @param callback_cls Closure for @c callback. | ||
178 | * @return #GNUNET_OK / #GNUNET_SYSERR | ||
179 | */ | ||
180 | int | ||
181 | GNUNET_CADET_get_peer (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
182 | const struct GNUNET_PeerIdentity *id, | ||
183 | GNUNET_CADET_PeerCB callback, | ||
184 | void *callback_cls) | ||
185 | { | ||
186 | struct GNUNET_CADET_LocalInfo *msg; | ||
187 | struct GNUNET_MQ_Envelope *env; | ||
188 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
189 | |||
190 | GNUNET_MQ_hd_var_size (get_peers, | ||
191 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS, | ||
192 | struct GNUNET_MessageHeader, | ||
193 | h), | ||
194 | GNUNET_MQ_hd_var_size (get_peer, | ||
195 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER, | ||
196 | struct GNUNET_CADET_LocalInfoPeer, | ||
197 | h), | ||
198 | GNUNET_MQ_handler_end () | ||
199 | |||
200 | |||
201 | if (NULL != h->info_cb.peer_cb) | ||
202 | { | ||
203 | GNUNET_break (0); | ||
204 | return GNUNET_SYSERR; | ||
205 | } | ||
206 | env = GNUNET_MQ_msg (msg, | ||
207 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER); | ||
208 | msg->peer = *id; | ||
209 | GNUNET_MQ_send (h->mq, | ||
210 | env); | ||
211 | h->info_cb.peer_cb = callback; | ||
212 | h->info_cls = callback_cls; | ||
213 | return GNUNET_OK; | ||
214 | } | ||
215 | |||
216 | |||
217 | |||
218 | |||
219 | |||
diff --git a/src/cadet/cadet_api_helper.c b/src/cadet/cadet_api_helper.c new file mode 100644 index 000000000..6f7634068 --- /dev/null +++ b/src/cadet/cadet_api_helper.c | |||
@@ -0,0 +1,55 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011, 2017 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 | * @file cadet/cadet_api_helper.c | ||
22 | * @brief cadet api: client implementation of cadet service | ||
23 | * @author Bartlomiej Polot | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_constants.h" | ||
29 | #include "gnunet_cadet_service.h" | ||
30 | #include "cadet.h" | ||
31 | #include "cadet_protocol.h" | ||
32 | |||
33 | |||
34 | /** | ||
35 | * Transitional function to convert an unsigned int port to a hash value. | ||
36 | * WARNING: local static value returned, NOT reentrant! | ||
37 | * WARNING: do not use this function for new code! | ||
38 | * | ||
39 | * @param port Numerical port (unsigned int format). | ||
40 | * | ||
41 | * @return A GNUNET_HashCode usable for the new CADET API. | ||
42 | */ | ||
43 | const struct GNUNET_HashCode * | ||
44 | GC_u2h (uint32_t port) | ||
45 | { | ||
46 | static struct GNUNET_HashCode hash; | ||
47 | |||
48 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
49 | "This is a transitional function, use proper crypto hashes as CADET ports\n"); | ||
50 | GNUNET_CRYPTO_hash (&port, | ||
51 | sizeof (port), | ||
52 | &hash); | ||
53 | return &hash; | ||
54 | } | ||
55 | |||
diff --git a/src/cadet/cadet_api_list_peers.c b/src/cadet/cadet_api_list_peers.c new file mode 100644 index 000000000..4bdc8b961 --- /dev/null +++ b/src/cadet/cadet_api_list_peers.c | |||
@@ -0,0 +1,179 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011, 2017 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 | * @file cadet/cadet_api.c | ||
22 | * @brief cadet api: client implementation of cadet service | ||
23 | * @author Bartlomiej Polot | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_constants.h" | ||
29 | #include "gnunet_cadet_service.h" | ||
30 | #include "cadet.h" | ||
31 | #include "cadet_protocol.h" | ||
32 | |||
33 | |||
34 | |||
35 | /** | ||
36 | * Ugly legacy hack. | ||
37 | */ | ||
38 | struct GNUNET_CADET_PeersLister | ||
39 | { | ||
40 | |||
41 | /** | ||
42 | * Monitor callback | ||
43 | */ | ||
44 | GNUNET_CADET_PeersCB peers_cb; | ||
45 | |||
46 | /** | ||
47 | * Info callback closure for @c info_cb. | ||
48 | */ | ||
49 | void *peers_cb_cls; | ||
50 | }; | ||
51 | |||
52 | |||
53 | /** | ||
54 | * Send message of @a type to CADET service of @a h | ||
55 | * | ||
56 | * @param h handle to CADET service | ||
57 | * @param type message type of trivial information request to send | ||
58 | */ | ||
59 | static void | ||
60 | send_info_request (struct GNUNET_CADET_Handle *h, | ||
61 | uint16_t type) | ||
62 | { | ||
63 | struct GNUNET_MessageHeader *msg; | ||
64 | struct GNUNET_MQ_Envelope *env; | ||
65 | |||
66 | env = GNUNET_MQ_msg (msg, | ||
67 | type); | ||
68 | GNUNET_MQ_send (h->mq, | ||
69 | env); | ||
70 | } | ||
71 | |||
72 | |||
73 | /** | ||
74 | * Request information about peers known to the running cadet service. | ||
75 | * The callback will be called for every peer known to the service. | ||
76 | * Only one info request (of any kind) can be active at once. | ||
77 | * | ||
78 | * WARNING: unstable API, likely to change in the future! | ||
79 | * | ||
80 | * @param h Handle to the cadet peer. | ||
81 | * @param callback Function to call with the requested data. | ||
82 | * @param callback_cls Closure for @c callback. | ||
83 | * @return #GNUNET_OK / #GNUNET_SYSERR | ||
84 | */ | ||
85 | int | ||
86 | GNUNET_CADET_list_peers (struct GNUNET_CADET_Handle *h, | ||
87 | GNUNET_CADET_PeersCB callback, | ||
88 | void *callback_cls) | ||
89 | { | ||
90 | if (NULL != h->info_cb.peers_cb) | ||
91 | { | ||
92 | GNUNET_break (0); | ||
93 | return GNUNET_SYSERR; | ||
94 | } | ||
95 | send_info_request (h, | ||
96 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS); | ||
97 | h->info_cb.peers_cb = callback; | ||
98 | h->info_cls = callback_cls; | ||
99 | return GNUNET_OK; | ||
100 | } | ||
101 | |||
102 | |||
103 | /** | ||
104 | * Cancel a peer info request. The callback will not be called (anymore). | ||
105 | * | ||
106 | * WARNING: unstable API, likely to change in the future! | ||
107 | * | ||
108 | * @param h Cadet handle. | ||
109 | * @return Closure given to GNUNET_CADET_get_peers(). | ||
110 | */ | ||
111 | void * | ||
112 | GNUNET_CADET_list_peers_cancel (struct GNUNET_CADET_Handle *h) | ||
113 | { | ||
114 | void *cls = h->info_cls; | ||
115 | |||
116 | h->info_cb.peers_cb = NULL; | ||
117 | h->info_cls = NULL; | ||
118 | return cls; | ||
119 | } | ||
120 | |||
121 | |||
122 | /** | ||
123 | * Check that message received from CADET service is well-formed. | ||
124 | * | ||
125 | * @param cls the `struct GNUNET_CADET_Handle` | ||
126 | * @param message the message we got | ||
127 | * @return #GNUNET_OK if the message is well-formed, | ||
128 | * #GNUNET_SYSERR otherwise | ||
129 | */ | ||
130 | static int | ||
131 | check_get_peers (void *cls, | ||
132 | const struct GNUNET_MessageHeader *message) | ||
133 | { | ||
134 | size_t esize; | ||
135 | |||
136 | (void) cls; | ||
137 | esize = ntohs (message->size); | ||
138 | if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == esize) | ||
139 | return GNUNET_OK; | ||
140 | if (sizeof (struct GNUNET_MessageHeader) == esize) | ||
141 | return GNUNET_OK; | ||
142 | return GNUNET_SYSERR; | ||
143 | } | ||
144 | |||
145 | |||
146 | /** | ||
147 | * Process a local reply about info on all tunnels, pass info to the user. | ||
148 | * | ||
149 | * @param cls Closure (Cadet handle). | ||
150 | * @param msg Message itself. | ||
151 | */ | ||
152 | static void | ||
153 | handle_get_peers (void *cls, | ||
154 | const struct GNUNET_MessageHeader *msg) | ||
155 | { | ||
156 | struct GNUNET_CADET_Handle *h = cls; | ||
157 | const struct GNUNET_CADET_LocalInfoPeer *info = | ||
158 | (const struct GNUNET_CADET_LocalInfoPeer *) msg; | ||
159 | |||
160 | if (NULL == h->info_cb.peers_cb) | ||
161 | return; | ||
162 | if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == ntohs (msg->size)) | ||
163 | h->info_cb.peers_cb (h->info_cls, | ||
164 | &info->destination, | ||
165 | (int) ntohs (info->tunnel), | ||
166 | (unsigned int) ntohs (info->paths), | ||
167 | 0); | ||
168 | else | ||
169 | h->info_cb.peers_cb (h->info_cls, | ||
170 | NULL, | ||
171 | 0, | ||
172 | 0, | ||
173 | 0); | ||
174 | } | ||
175 | |||
176 | |||
177 | |||
178 | |||
179 | |||
diff --git a/src/cadet/cadet_api_list_tunnels.c b/src/cadet/cadet_api_list_tunnels.c new file mode 100644 index 000000000..da5f85aa0 --- /dev/null +++ b/src/cadet/cadet_api_list_tunnels.c | |||
@@ -0,0 +1,187 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011, 2017 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 | * @file cadet/cadet_api.c | ||
22 | * @brief cadet api: client implementation of cadet service | ||
23 | * @author Bartlomiej Polot | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_constants.h" | ||
29 | #include "gnunet_cadet_service.h" | ||
30 | #include "cadet.h" | ||
31 | #include "cadet_protocol.h" | ||
32 | |||
33 | |||
34 | |||
35 | /** | ||
36 | * Ugly legacy hack. | ||
37 | */ | ||
38 | struct GNUNET_CADET_ListTunnels | ||
39 | { | ||
40 | |||
41 | /** | ||
42 | * Monitor callback | ||
43 | */ | ||
44 | GNUNET_CADET_TunnelsCB tunnels_cb; | ||
45 | |||
46 | /** | ||
47 | * Info callback closure for @c tunnels_cb. | ||
48 | */ | ||
49 | void *tunnels_cb_cls; | ||
50 | }; | ||
51 | |||
52 | |||
53 | /** | ||
54 | * Send message of @a type to CADET service of @a h | ||
55 | * | ||
56 | * @param h handle to CADET service | ||
57 | * @param type message type of trivial information request to send | ||
58 | */ | ||
59 | static void | ||
60 | send_info_request (struct GNUNET_CADET_Handle *h, | ||
61 | uint16_t type) | ||
62 | { | ||
63 | struct GNUNET_MessageHeader *msg; | ||
64 | struct GNUNET_MQ_Envelope *env; | ||
65 | |||
66 | env = GNUNET_MQ_msg (msg, | ||
67 | type); | ||
68 | GNUNET_MQ_send (h->mq, | ||
69 | env); | ||
70 | } | ||
71 | |||
72 | |||
73 | /** | ||
74 | * Check that message received from CADET service is well-formed. | ||
75 | * | ||
76 | * @param cls the `struct GNUNET_CADET_Handle` | ||
77 | * @param message the message we got | ||
78 | * @return #GNUNET_OK if the message is well-formed, | ||
79 | * #GNUNET_SYSERR otherwise | ||
80 | */ | ||
81 | static int | ||
82 | check_get_tunnels (void *cls, | ||
83 | const struct GNUNET_MessageHeader *message) | ||
84 | { | ||
85 | size_t esize; | ||
86 | |||
87 | (void) cls; | ||
88 | esize = ntohs (message->size); | ||
89 | if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) == esize) | ||
90 | return GNUNET_OK; | ||
91 | if (sizeof (struct GNUNET_MessageHeader) == esize) | ||
92 | return GNUNET_OK; | ||
93 | return GNUNET_SYSERR; | ||
94 | } | ||
95 | |||
96 | |||
97 | /** | ||
98 | * Process a local reply about info on all tunnels, pass info to the user. | ||
99 | * | ||
100 | * @param cls Closure (Cadet handle). | ||
101 | * @param message Message itself. | ||
102 | */ | ||
103 | static void | ||
104 | handle_get_tunnels (void *cls, | ||
105 | const struct GNUNET_MessageHeader *msg) | ||
106 | { | ||
107 | struct GNUNET_CADET_Handle *h = cls; | ||
108 | const struct GNUNET_CADET_LocalInfoTunnel *info = | ||
109 | (const struct GNUNET_CADET_LocalInfoTunnel *) msg; | ||
110 | |||
111 | if (NULL == h->info_cb.tunnels_cb) | ||
112 | return; | ||
113 | if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) == ntohs (msg->size)) | ||
114 | h->info_cb.tunnels_cb (h->info_cls, | ||
115 | &info->destination, | ||
116 | ntohl (info->channels), | ||
117 | ntohl (info->connections), | ||
118 | ntohs (info->estate), | ||
119 | ntohs (info->cstate)); | ||
120 | else | ||
121 | h->info_cb.tunnels_cb (h->info_cls, | ||
122 | NULL, | ||
123 | 0, | ||
124 | 0, | ||
125 | 0, | ||
126 | 0); | ||
127 | } | ||
128 | |||
129 | |||
130 | /** | ||
131 | * Request information about tunnels of the running cadet peer. | ||
132 | * The callback will be called for every tunnel of the service. | ||
133 | * Only one info request (of any kind) can be active at once. | ||
134 | * | ||
135 | * WARNING: unstable API, likely to change in the future! | ||
136 | * | ||
137 | * @param h Handle to the cadet peer. | ||
138 | * @param callback Function to call with the requested data. | ||
139 | * @param callback_cls Closure for @c callback. | ||
140 | * @return #GNUNET_OK / #GNUNET_SYSERR | ||
141 | */ | ||
142 | int | ||
143 | GNUNET_CADET_list_tunnels (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
144 | GNUNET_CADET_TunnelsCB callback, | ||
145 | void *callback_cls) | ||
146 | { | ||
147 | |||
148 | GNUNET_MQ_hd_var_size (get_tunnels, | ||
149 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS, | ||
150 | struct GNUNET_MessageHeader, | ||
151 | h), | ||
152 | GNUNET_MQ_hd_var_size (get_tunnel, | ||
153 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL, | ||
154 | struct GNUNET_CADET_LocalInfoTunnel, | ||
155 | h), | ||
156 | GNUNET_MQ_handler_end () | ||
157 | |||
158 | if (NULL != h->info_cb.tunnels_cb) | ||
159 | { | ||
160 | GNUNET_break (0); | ||
161 | return GNUNET_SYSERR; | ||
162 | } | ||
163 | send_info_request (h, | ||
164 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS); | ||
165 | h->info_cb.tunnels_cb = callback; | ||
166 | h->info_cls = callback_cls; | ||
167 | return GNUNET_OK; | ||
168 | } | ||
169 | |||
170 | |||
171 | /** | ||
172 | * Cancel a monitor request. The monitor callback will not be called. | ||
173 | * | ||
174 | * @param h Cadet handle. | ||
175 | * @return Closure given to GNUNET_CADET_list_tunnels(). | ||
176 | */ | ||
177 | void * | ||
178 | GNUNET_CADET_list_tunnels_cancel (struct GNUNET_CADET_ListTunnels *lt) | ||
179 | { | ||
180 | void *cls = h->info_cls; | ||
181 | |||
182 | h->info_cb.tunnels_cb = NULL; | ||
183 | h->info_cls = NULL; | ||
184 | return cls; | ||
185 | } | ||
186 | |||
187 | |||
diff --git a/src/cadet/gnunet-cadet.c b/src/cadet/gnunet-cadet.c index ccc757d7e..b36113318 100644 --- a/src/cadet/gnunet-cadet.c +++ b/src/cadet/gnunet-cadet.c | |||
@@ -72,11 +72,6 @@ static char *listen_port; | |||
72 | static int echo; | 72 | static int echo; |
73 | 73 | ||
74 | /** | 74 | /** |
75 | * Request a debug dump | ||
76 | */ | ||
77 | static int dump; | ||
78 | |||
79 | /** | ||
80 | * Time of last echo request. | 75 | * Time of last echo request. |
81 | */ | 76 | */ |
82 | static struct GNUNET_TIME_Absolute echo_time; | 77 | static struct GNUNET_TIME_Absolute echo_time; |
@@ -102,6 +97,11 @@ static char *target_port = "default"; | |||
102 | static struct GNUNET_CADET_Handle *mh; | 97 | static struct GNUNET_CADET_Handle *mh; |
103 | 98 | ||
104 | /** | 99 | /** |
100 | * Our configuration. | ||
101 | */ | ||
102 | static const struct GNUNET_CONFIGURATION_Handle *my_cfg; | ||
103 | |||
104 | /** | ||
105 | * Channel handle. | 105 | * Channel handle. |
106 | */ | 106 | */ |
107 | static struct GNUNET_CADET_Channel *ch; | 107 | static struct GNUNET_CADET_Channel *ch; |
@@ -396,20 +396,6 @@ send_echo (void *cls) | |||
396 | 396 | ||
397 | 397 | ||
398 | /** | 398 | /** |
399 | * Call CADET's monitor API, request debug dump on the service. | ||
400 | * | ||
401 | * @param cls Closure (unused). | ||
402 | */ | ||
403 | static void | ||
404 | request_dump (void *cls) | ||
405 | { | ||
406 | job = NULL; | ||
407 | GNUNET_CADET_request_dump (mh); | ||
408 | GNUNET_SCHEDULER_shutdown (); | ||
409 | } | ||
410 | |||
411 | |||
412 | /** | ||
413 | * Check data message sanity. Does nothing so far (all messages are OK). | 399 | * Check data message sanity. Does nothing so far (all messages are OK). |
414 | * | 400 | * |
415 | * @param cls Closure (unused). | 401 | * @param cls Closure (unused). |
@@ -683,7 +669,11 @@ static void | |||
683 | get_peers (void *cls) | 669 | get_peers (void *cls) |
684 | { | 670 | { |
685 | job = NULL; | 671 | job = NULL; |
686 | GNUNET_CADET_get_peers (mh, &peers_callback, NULL); | 672 | #if FIXME5385 |
673 | GNUNET_CADET_list_peers (my_cfg, | ||
674 | &peers_callback, | ||
675 | NULL); | ||
676 | #endif | ||
687 | } | 677 | } |
688 | 678 | ||
689 | 679 | ||
@@ -709,7 +699,12 @@ show_peer (void *cls) | |||
709 | GNUNET_SCHEDULER_shutdown(); | 699 | GNUNET_SCHEDULER_shutdown(); |
710 | return; | 700 | return; |
711 | } | 701 | } |
712 | GNUNET_CADET_get_peer (mh, &pid, peer_callback, NULL); | 702 | #if FIXME5385 |
703 | GNUNET_CADET_get_peer (my_cfg, | ||
704 | &pid, | ||
705 | &peer_callback, | ||
706 | NULL); | ||
707 | #endif | ||
713 | } | 708 | } |
714 | 709 | ||
715 | 710 | ||
@@ -722,7 +717,11 @@ static void | |||
722 | get_tunnels (void *cls) | 717 | get_tunnels (void *cls) |
723 | { | 718 | { |
724 | job = NULL; | 719 | job = NULL; |
725 | GNUNET_CADET_get_tunnels (mh, &tunnels_callback, NULL); | 720 | #if FIXME5385 |
721 | GNUNET_CADET_list_tunnels (my_cfg, | ||
722 | &tunnels_callback, | ||
723 | NULL); | ||
724 | #endif | ||
726 | } | 725 | } |
727 | 726 | ||
728 | 727 | ||
@@ -748,10 +747,12 @@ show_tunnel (void *cls) | |||
748 | GNUNET_SCHEDULER_shutdown (); | 747 | GNUNET_SCHEDULER_shutdown (); |
749 | return; | 748 | return; |
750 | } | 749 | } |
751 | GNUNET_CADET_get_tunnel (mh, | 750 | #if FIXME5385 |
751 | GNUNET_CADET_get_tunnel (my_cfg, | ||
752 | &pid, | 752 | &pid, |
753 | &tunnel_callback, | 753 | &tunnel_callback, |
754 | NULL); | 754 | NULL); |
755 | #endif | ||
755 | } | 756 | } |
756 | 757 | ||
757 | 758 | ||
@@ -804,7 +805,7 @@ run (void *cls, | |||
804 | }; | 805 | }; |
805 | 806 | ||
806 | /* FIXME add option to monitor apps */ | 807 | /* FIXME add option to monitor apps */ |
807 | 808 | my_cfg = cfg; | |
808 | target_id = args[0]; | 809 | target_id = args[0]; |
809 | if (target_id && args[1]) | 810 | if (target_id && args[1]) |
810 | target_port = args[1]; | 811 | target_port = args[1]; |
@@ -821,14 +822,7 @@ run (void *cls, | |||
821 | return; | 822 | return; |
822 | } | 823 | } |
823 | 824 | ||
824 | if (GNUNET_YES == dump) | 825 | if (NULL != peer_id) |
825 | { | ||
826 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
827 | "Requesting debug dump\n"); | ||
828 | job = GNUNET_SCHEDULER_add_now (&request_dump, | ||
829 | NULL); | ||
830 | } | ||
831 | else if (NULL != peer_id) | ||
832 | { | 826 | { |
833 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 827 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
834 | "Show peer\n"); | 828 | "Show peer\n"); |
@@ -971,48 +965,33 @@ main (int argc, | |||
971 | "CONNECTION_ID", | 965 | "CONNECTION_ID", |
972 | gettext_noop ("Provide information about a particular connection"), | 966 | gettext_noop ("Provide information about a particular connection"), |
973 | &conn_id), | 967 | &conn_id), |
974 | |||
975 | GNUNET_GETOPT_option_flag ('e', | 968 | GNUNET_GETOPT_option_flag ('e', |
976 | "echo", | 969 | "echo", |
977 | gettext_noop ("Activate echo mode"), | 970 | gettext_noop ("Activate echo mode"), |
978 | &echo), | 971 | &echo), |
979 | |||
980 | GNUNET_GETOPT_option_flag ('d', | ||
981 | "dump", | ||
982 | gettext_noop ("Dump debug information to STDERR"), | ||
983 | &dump), | ||
984 | |||
985 | GNUNET_GETOPT_option_string ('o', | 972 | GNUNET_GETOPT_option_string ('o', |
986 | "open-port", | 973 | "open-port", |
987 | "SHARED_SECRET", | 974 | "SHARED_SECRET", |
988 | gettext_noop ("Listen for connections using a shared secret among sender and recipient"), | 975 | gettext_noop ("Listen for connections using a shared secret among sender and recipient"), |
989 | &listen_port), | 976 | &listen_port), |
990 | |||
991 | |||
992 | GNUNET_GETOPT_option_string ('p', | 977 | GNUNET_GETOPT_option_string ('p', |
993 | "peer", | 978 | "peer", |
994 | "PEER_ID", | 979 | "PEER_ID", |
995 | gettext_noop ("Provide information about a patricular peer"), | 980 | gettext_noop ("Provide information about a patricular peer"), |
996 | &peer_id), | 981 | &peer_id), |
997 | |||
998 | |||
999 | GNUNET_GETOPT_option_flag ('P', | 982 | GNUNET_GETOPT_option_flag ('P', |
1000 | "peers", | 983 | "peers", |
1001 | gettext_noop ("Provide information about all peers"), | 984 | gettext_noop ("Provide information about all peers"), |
1002 | &request_peers), | 985 | &request_peers), |
1003 | |||
1004 | GNUNET_GETOPT_option_string ('t', | 986 | GNUNET_GETOPT_option_string ('t', |
1005 | "tunnel", | 987 | "tunnel", |
1006 | "TUNNEL_ID", | 988 | "TUNNEL_ID", |
1007 | gettext_noop ("Provide information about a particular tunnel"), | 989 | gettext_noop ("Provide information about a particular tunnel"), |
1008 | &tunnel_id), | 990 | &tunnel_id), |
1009 | |||
1010 | |||
1011 | GNUNET_GETOPT_option_flag ('T', | 991 | GNUNET_GETOPT_option_flag ('T', |
1012 | "tunnels", | 992 | "tunnels", |
1013 | gettext_noop ("Provide information about all tunnels"), | 993 | gettext_noop ("Provide information about all tunnels"), |
1014 | &request_tunnels), | 994 | &request_tunnels), |
1015 | |||
1016 | GNUNET_GETOPT_OPTION_END | 995 | GNUNET_GETOPT_OPTION_END |
1017 | }; | 996 | }; |
1018 | 997 | ||