aboutsummaryrefslogtreecommitdiff
path: root/src/cadet/cadet_api.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-01-26 20:49:59 +0100
committerChristian Grothoff <christian@grothoff.org>2019-01-26 20:49:59 +0100
commit7fc78674a6d3f2edb41e32cea59200908d0b344b (patch)
treeb99608184fa08dcc6cd3e297369cb2761a03524f /src/cadet/cadet_api.c
parenta5a54c51011a9498e72b2a9341b35333c6beef1a (diff)
downloadgnunet-7fc78674a6d3f2edb41e32cea59200908d0b344b.tar.gz
gnunet-7fc78674a6d3f2edb41e32cea59200908d0b344b.zip
starting with #5385 in earnest
Diffstat (limited to 'src/cadet/cadet_api.c')
-rw-r--r--src/cadet/cadet_api.c571
1 files changed, 0 insertions, 571 deletions
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 */
38union 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 */
71struct GNUNET_CADET_Handle 38struct 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 */
864static int
865check_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 */
886static void
887handle_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 */
919static int
920check_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 */
948static void
949handle_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 */
1009static int
1010check_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 */
1031static void
1032handle_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 */
1066static int
1067check_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 */
1109static void
1110handle_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 */
1378static void
1379send_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 */
1399void
1400GNUNET_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 */
1419int
1420GNUNET_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 */
1445void *
1446GNUNET_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 */
1469int
1470GNUNET_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 */
1506int
1507GNUNET_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 */
1530void *
1531GNUNET_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 */
1554int
1555GNUNET_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 */
1588const struct GNUNET_HashCode *
1589GC_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.