diff options
Diffstat (limited to 'src/transport/transport_api_cmd_connecting_peers_v3.c')
-rw-r--r-- | src/transport/transport_api_cmd_connecting_peers_v3.c | 496 |
1 files changed, 0 insertions, 496 deletions
diff --git a/src/transport/transport_api_cmd_connecting_peers_v3.c b/src/transport/transport_api_cmd_connecting_peers_v3.c deleted file mode 100644 index ae32a087a..000000000 --- a/src/transport/transport_api_cmd_connecting_peers_v3.c +++ /dev/null | |||
@@ -1,496 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2021 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 testing_api_cmd_start_peer.c | ||
23 | * @brief cmd to start a peer. | ||
24 | * @author t3sserakt | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_testing_ng_lib.h" | ||
29 | #include "gnunet_transport_application_service.h" | ||
30 | #include "gnunet_hello_lib.h" | ||
31 | #include "gnunet_transport_service.h" | ||
32 | #include "transport-testing-cmds.h" | ||
33 | |||
34 | /** | ||
35 | * Generic logging shortcut | ||
36 | */ | ||
37 | #define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__) | ||
38 | |||
39 | #define CONNECT_ADDRESS_TEMPLATE_TCP "tcp-192.168.15.%u:60002" | ||
40 | |||
41 | #define CONNECT_ADDRESS_TEMPLATE_UDP "udp-192.168.15.%u:60002" | ||
42 | |||
43 | #define ROUTER_CONNECT_ADDRESS_TEMPLATE_TCP "tcp-92.68.150.%u:60002" | ||
44 | |||
45 | #define ROUTER_CONNECT_ADDRESS_TEMPLATE_UDP "udp-92.68.150.%u:60002" | ||
46 | |||
47 | #define GLOBAL_CONNECT_ADDRESS_TEMPLATE_TCP "tcp-92.68.151.%u:60002" | ||
48 | |||
49 | #define GLOBAL_CONNECT_ADDRESS_TEMPLATE_UDP "udp-92.68.151.%u:60002" | ||
50 | |||
51 | #define PREFIX_TCP "tcp" | ||
52 | |||
53 | #define PREFIX_UDP "udp" | ||
54 | |||
55 | /** | ||
56 | * Struct to store information needed in callbacks. | ||
57 | * | ||
58 | */ | ||
59 | struct ConnectPeersState | ||
60 | { | ||
61 | /** | ||
62 | * Context for our asynchronous completion. | ||
63 | */ | ||
64 | struct GNUNET_TESTING_AsyncContext ac; | ||
65 | |||
66 | /** | ||
67 | * The testing system of this node. | ||
68 | */ | ||
69 | struct GNUNET_TESTING_System *tl_system; | ||
70 | |||
71 | // Label of the cmd which started the test system. | ||
72 | const char *create_label; | ||
73 | |||
74 | /** | ||
75 | * Number globally identifying the node. | ||
76 | * | ||
77 | */ | ||
78 | uint32_t num; | ||
79 | |||
80 | /** | ||
81 | * Label of the cmd to start a peer. | ||
82 | * | ||
83 | */ | ||
84 | const char *start_peer_label; | ||
85 | |||
86 | /** | ||
87 | * The peer identity of this peer. | ||
88 | * | ||
89 | */ | ||
90 | struct GNUNET_PeerIdentity *id; | ||
91 | |||
92 | /** | ||
93 | * The topology of the test setup. | ||
94 | */ | ||
95 | struct GNUNET_TESTING_NetjailTopology *topology; | ||
96 | |||
97 | /** | ||
98 | * Connections to other peers. | ||
99 | */ | ||
100 | struct GNUNET_TESTING_NodeConnection *node_connections_head; | ||
101 | |||
102 | struct GNUNET_TESTING_Interpreter *is; | ||
103 | |||
104 | /** | ||
105 | * Number of connections. | ||
106 | */ | ||
107 | unsigned int con_num; | ||
108 | }; | ||
109 | |||
110 | |||
111 | static struct GNUNET_PeerIdentity * | ||
112 | get_pub_key (unsigned int num, struct GNUNET_TESTING_System *tl_system) | ||
113 | { | ||
114 | struct GNUNET_PeerIdentity *peer = GNUNET_new (struct GNUNET_PeerIdentity); | ||
115 | struct GNUNET_CRYPTO_EddsaPublicKey *pub_key = GNUNET_new (struct | ||
116 | GNUNET_CRYPTO_EddsaPublicKey); | ||
117 | struct GNUNET_CRYPTO_EddsaPrivateKey *priv_key = GNUNET_new (struct | ||
118 | GNUNET_CRYPTO_EddsaPrivateKey); | ||
119 | |||
120 | priv_key = GNUNET_TESTING_hostkey_get (tl_system, | ||
121 | num, | ||
122 | peer); | ||
123 | |||
124 | GNUNET_CRYPTO_eddsa_key_get_public (priv_key, | ||
125 | pub_key); | ||
126 | peer->public_key = *pub_key; | ||
127 | return peer; | ||
128 | } | ||
129 | |||
130 | |||
131 | static int | ||
132 | log_nodes (void *cls, const struct GNUNET_ShortHashCode *id, void *value) | ||
133 | { | ||
134 | struct GNUNET_TESTING_NetjailNode *node = value; | ||
135 | struct GNUNET_TESTING_NodeConnection *pos_connection; | ||
136 | struct GNUNET_TESTING_ADDRESS_PREFIX *pos_prefix; | ||
137 | |||
138 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
139 | "plugin: %s space: %u node: %u global: %u\n", | ||
140 | node->plugin, | ||
141 | node->namespace_n, | ||
142 | node->node_n, | ||
143 | node->is_global); | ||
144 | |||
145 | for (pos_connection = node->node_connections_head; NULL != pos_connection; | ||
146 | pos_connection = pos_connection->next) | ||
147 | { | ||
148 | |||
149 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
150 | "namespace_n: %u node_n: %u node_type: %u\n", | ||
151 | pos_connection->namespace_n, | ||
152 | pos_connection->node_n, | ||
153 | pos_connection->node_type); | ||
154 | |||
155 | for (pos_prefix = pos_connection->address_prefixes_head; NULL != pos_prefix; | ||
156 | pos_prefix = | ||
157 | pos_prefix->next) | ||
158 | { | ||
159 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
160 | "prefix: %s\n", | ||
161 | pos_prefix->address_prefix); | ||
162 | } | ||
163 | } | ||
164 | return GNUNET_YES; | ||
165 | } | ||
166 | |||
167 | |||
168 | static int | ||
169 | log_namespaces (void *cls, const struct GNUNET_ShortHashCode *id, void *value) | ||
170 | { | ||
171 | struct GNUNET_TESTING_NetjailNamespace *namespace = value; | ||
172 | struct GNUNET_TESTING_NetjailRouter *router = namespace->router; | ||
173 | |||
174 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
175 | "router_tcp: %u router_udp: %u spaces: %u\n", | ||
176 | router->tcp_port, | ||
177 | router->udp_port, | ||
178 | namespace->namespace_n); | ||
179 | GNUNET_CONTAINER_multishortmap_iterate (namespace->nodes, &log_nodes, NULL); | ||
180 | return GNUNET_YES; | ||
181 | } | ||
182 | |||
183 | |||
184 | static int | ||
185 | log_topo (struct GNUNET_TESTING_NetjailTopology *topology) | ||
186 | { | ||
187 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
188 | "plugin: %s spaces: %u nodes: %u known: %u\n", | ||
189 | topology->plugin, | ||
190 | topology->namespaces_n, | ||
191 | topology->nodes_m, | ||
192 | topology->nodes_x); | ||
193 | |||
194 | GNUNET_CONTAINER_multishortmap_iterate (topology->map_namespaces, | ||
195 | log_namespaces, NULL); | ||
196 | GNUNET_CONTAINER_multishortmap_iterate (topology->map_globals, &log_nodes, | ||
197 | NULL); | ||
198 | return GNUNET_YES; | ||
199 | } | ||
200 | |||
201 | |||
202 | static struct GNUNET_TESTING_NodeConnection * | ||
203 | get_connections (unsigned int num, struct | ||
204 | GNUNET_TESTING_NetjailTopology *topology) | ||
205 | { | ||
206 | struct GNUNET_TESTING_NetjailNode *node; | ||
207 | struct GNUNET_ShortHashCode *hkey; | ||
208 | struct GNUNET_HashCode hc; | ||
209 | struct GNUNET_TESTING_NetjailNamespace *namespace; | ||
210 | unsigned int namespace_n, node_m; | ||
211 | |||
212 | log_topo (topology); | ||
213 | |||
214 | hkey = GNUNET_new (struct GNUNET_ShortHashCode); | ||
215 | if (topology->nodes_x >= num) | ||
216 | { | ||
217 | |||
218 | GNUNET_CRYPTO_hash (&num, sizeof(num), &hc); | ||
219 | memcpy (hkey, | ||
220 | &hc, | ||
221 | sizeof (*hkey)); | ||
222 | node = GNUNET_CONTAINER_multishortmap_get (topology->map_globals, | ||
223 | hkey); | ||
224 | } | ||
225 | else | ||
226 | { | ||
227 | namespace_n = (unsigned int) floor ((num - topology->nodes_x) | ||
228 | / topology->nodes_m); | ||
229 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
230 | "num: %u nodes_x: %u nodes_m: %u namespace_n: %u\n", | ||
231 | num, | ||
232 | topology->nodes_x, | ||
233 | topology->nodes_m, | ||
234 | namespace_n); | ||
235 | hkey = GNUNET_new (struct GNUNET_ShortHashCode); | ||
236 | GNUNET_CRYPTO_hash (&namespace_n, sizeof(namespace_n), &hc); | ||
237 | memcpy (hkey, | ||
238 | &hc, | ||
239 | sizeof (*hkey)); | ||
240 | namespace = GNUNET_CONTAINER_multishortmap_get (topology->map_namespaces, | ||
241 | hkey); | ||
242 | node_m = num - topology->nodes_x - topology->nodes_m * (namespace_n - 1); | ||
243 | hkey = GNUNET_new (struct GNUNET_ShortHashCode); | ||
244 | GNUNET_CRYPTO_hash (&node_m, sizeof(node_m), &hc); | ||
245 | memcpy (hkey, | ||
246 | &hc, | ||
247 | sizeof (*hkey)); | ||
248 | node = GNUNET_CONTAINER_multishortmap_get (namespace->nodes, | ||
249 | hkey); | ||
250 | } | ||
251 | |||
252 | |||
253 | return node->node_connections_head; | ||
254 | } | ||
255 | |||
256 | |||
257 | static unsigned int | ||
258 | calculate_num (struct GNUNET_TESTING_NodeConnection *node_connection, | ||
259 | struct GNUNET_TESTING_NetjailTopology *topology) | ||
260 | { | ||
261 | unsigned int n, m, num; | ||
262 | |||
263 | n = node_connection->namespace_n; | ||
264 | m = node_connection->node_n; | ||
265 | |||
266 | if (0 == n) | ||
267 | num = m; | ||
268 | else | ||
269 | num = (n - 1) * topology->nodes_m + m + topology->nodes_x; | ||
270 | |||
271 | return num; | ||
272 | } | ||
273 | |||
274 | |||
275 | static char * | ||
276 | get_address (struct GNUNET_TESTING_NodeConnection *connection, | ||
277 | char *prefix) | ||
278 | { | ||
279 | struct GNUNET_TESTING_NetjailNode *node; | ||
280 | char *addr; | ||
281 | |||
282 | node = connection->node; | ||
283 | if (connection->namespace_n == node->namespace_n) | ||
284 | { | ||
285 | if (0 == strcmp (PREFIX_TCP, prefix)) | ||
286 | { | ||
287 | |||
288 | GNUNET_asprintf (&addr, | ||
289 | CONNECT_ADDRESS_TEMPLATE_TCP, | ||
290 | connection->node_n); | ||
291 | } | ||
292 | else if (0 == strcmp (PREFIX_UDP, prefix)) | ||
293 | { | ||
294 | GNUNET_asprintf (&addr, | ||
295 | CONNECT_ADDRESS_TEMPLATE_UDP, | ||
296 | connection->node_n); | ||
297 | } | ||
298 | else | ||
299 | { | ||
300 | GNUNET_break (0); | ||
301 | } | ||
302 | } | ||
303 | else | ||
304 | { | ||
305 | if (0 == strcmp (PREFIX_TCP, prefix)) | ||
306 | { | ||
307 | |||
308 | GNUNET_asprintf (&addr, | ||
309 | ROUTER_CONNECT_ADDRESS_TEMPLATE_TCP, | ||
310 | connection->namespace_n); | ||
311 | } | ||
312 | else if (0 == strcmp (PREFIX_UDP, prefix)) | ||
313 | { | ||
314 | GNUNET_asprintf (&addr, | ||
315 | ROUTER_CONNECT_ADDRESS_TEMPLATE_UDP, | ||
316 | connection->namespace_n); | ||
317 | } | ||
318 | else | ||
319 | { | ||
320 | GNUNET_break (0); | ||
321 | } | ||
322 | } | ||
323 | |||
324 | return addr; | ||
325 | } | ||
326 | |||
327 | |||
328 | /** | ||
329 | * The run method of this cmd will connect to peers. | ||
330 | * | ||
331 | */ | ||
332 | static void | ||
333 | connect_peers_run (void *cls, | ||
334 | struct GNUNET_TESTING_Interpreter *is) | ||
335 | { | ||
336 | struct ConnectPeersState *cps = cls; | ||
337 | const struct GNUNET_TESTING_Command *system_cmd; | ||
338 | struct GNUNET_TESTING_System *tl_system; | ||
339 | |||
340 | |||
341 | const struct GNUNET_TESTING_Command *peer1_cmd; | ||
342 | struct GNUNET_TRANSPORT_ApplicationHandle *ah; | ||
343 | struct GNUNET_PeerIdentity *peer; | ||
344 | char *addr; | ||
345 | enum GNUNET_NetworkType nt = 0; | ||
346 | uint32_t num; | ||
347 | struct GNUNET_TESTING_NodeConnection *pos_connection; | ||
348 | struct GNUNET_TESTING_ADDRESS_PREFIX *pos_prefix; | ||
349 | unsigned int con_num = 0; | ||
350 | |||
351 | cps->is = is; | ||
352 | peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (is, | ||
353 | cps->start_peer_label); | ||
354 | GNUNET_TRANSPORT_get_trait_application_handle_v2 (peer1_cmd, | ||
355 | &ah); | ||
356 | |||
357 | system_cmd = GNUNET_TESTING_interpreter_lookup_command (is, | ||
358 | cps->create_label); | ||
359 | GNUNET_TESTING_get_trait_test_system (system_cmd, | ||
360 | &tl_system); | ||
361 | |||
362 | cps->tl_system = tl_system; | ||
363 | |||
364 | cps->node_connections_head = get_connections (cps->num, cps->topology); | ||
365 | |||
366 | for (pos_connection = cps->node_connections_head; NULL != pos_connection; | ||
367 | pos_connection = pos_connection->next) | ||
368 | { | ||
369 | con_num++; | ||
370 | num = calculate_num (pos_connection, cps->topology); | ||
371 | for (pos_prefix = pos_connection->address_prefixes_head; NULL != pos_prefix; | ||
372 | pos_prefix = | ||
373 | pos_prefix->next) | ||
374 | { | ||
375 | |||
376 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
377 | "prefix: %s\n", | ||
378 | pos_prefix->address_prefix); | ||
379 | |||
380 | addr = get_address (pos_connection, pos_prefix->address_prefix); | ||
381 | |||
382 | peer = get_pub_key (num, tl_system); | ||
383 | |||
384 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
385 | "num: %u pub_key %s addr: %s\n", | ||
386 | num, | ||
387 | GNUNET_CRYPTO_eddsa_public_key_to_string (&(peer->public_key)), | ||
388 | addr); | ||
389 | |||
390 | cps->id = peer; | ||
391 | |||
392 | GNUNET_TRANSPORT_application_validate (ah, | ||
393 | peer, | ||
394 | nt, | ||
395 | addr); | ||
396 | } | ||
397 | } | ||
398 | cps->con_num = con_num; | ||
399 | } | ||
400 | |||
401 | |||
402 | /** | ||
403 | * The finish function of this cmd will check if the peers we are trying to | ||
404 | * connect to are in the connected peers map of the start peer cmd for this peer. | ||
405 | * | ||
406 | */ | ||
407 | static int | ||
408 | connect_peers_finish (void *cls, | ||
409 | GNUNET_SCHEDULER_TaskCallback cont, | ||
410 | void *cont_cls) | ||
411 | { | ||
412 | struct ConnectPeersState *cps = cls; | ||
413 | const struct GNUNET_TESTING_Command *peer1_cmd; | ||
414 | struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map; | ||
415 | unsigned int ret; | ||
416 | struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode); | ||
417 | struct GNUNET_HashCode hc; | ||
418 | struct GNUNET_PeerIdentity *peer; | ||
419 | unsigned int con_num = 0; | ||
420 | struct GNUNET_TESTING_NodeConnection *pos_connection; | ||
421 | unsigned int num; | ||
422 | |||
423 | peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (cps->is, | ||
424 | cps->start_peer_label); | ||
425 | GNUNET_TRANSPORT_get_trait_connected_peers_map_v2 (peer1_cmd, | ||
426 | &connected_peers_map); | ||
427 | |||
428 | for (pos_connection = cps->node_connections_head; NULL != pos_connection; | ||
429 | pos_connection = pos_connection->next) | ||
430 | { | ||
431 | num = calculate_num (pos_connection, cps->topology); | ||
432 | peer = get_pub_key (num, cps->tl_system); | ||
433 | GNUNET_CRYPTO_hash (&(peer->public_key), sizeof(peer->public_key), &hc); | ||
434 | memcpy (key, | ||
435 | &hc, | ||
436 | sizeof (*key)); | ||
437 | if (GNUNET_YES == GNUNET_CONTAINER_multishortmap_contains ( | ||
438 | connected_peers_map, | ||
439 | key)) | ||
440 | con_num++; | ||
441 | } | ||
442 | |||
443 | |||
444 | if (cps->con_num == con_num) | ||
445 | { | ||
446 | cont (cont_cls); | ||
447 | ret = GNUNET_YES; | ||
448 | } | ||
449 | |||
450 | GNUNET_free (key); | ||
451 | return ret; | ||
452 | } | ||
453 | |||
454 | |||
455 | /** | ||
456 | * The cleanup function of this cmd frees resources the cmd allocated. | ||
457 | * | ||
458 | */ | ||
459 | static void | ||
460 | connect_peers_cleanup (void *cls) | ||
461 | { | ||
462 | struct ConnectPeersState *cps = cls; | ||
463 | |||
464 | GNUNET_free (cps->id); | ||
465 | GNUNET_free (cps); | ||
466 | } | ||
467 | |||
468 | |||
469 | struct GNUNET_TESTING_Command | ||
470 | GNUNET_TRANSPORT_cmd_connect_peers_v3 (const char *label, | ||
471 | const char *start_peer_label, | ||
472 | const char *create_label, | ||
473 | uint32_t num, | ||
474 | struct GNUNET_TESTING_NetjailTopology * | ||
475 | topology) | ||
476 | { | ||
477 | struct ConnectPeersState *cps; | ||
478 | |||
479 | cps = GNUNET_new (struct ConnectPeersState); | ||
480 | cps->start_peer_label = start_peer_label; | ||
481 | cps->num = num; | ||
482 | cps->create_label = create_label; | ||
483 | cps->topology = topology; | ||
484 | |||
485 | { | ||
486 | struct GNUNET_TESTING_Command cmd = { | ||
487 | .cls = cps, | ||
488 | .label = label, | ||
489 | .run = &connect_peers_run, | ||
490 | .ac = &cps->ac, | ||
491 | .cleanup = &connect_peers_cleanup | ||
492 | }; | ||
493 | |||
494 | return cmd; | ||
495 | } | ||
496 | } | ||