aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport_api_cmd_start_peer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/transport_api_cmd_start_peer.c')
-rw-r--r--src/transport/transport_api_cmd_start_peer.c491
1 files changed, 0 insertions, 491 deletions
diff --git a/src/transport/transport_api_cmd_start_peer.c b/src/transport/transport_api_cmd_start_peer.c
deleted file mode 100644
index 7448eff5a..000000000
--- a/src/transport/transport_api_cmd_start_peer.c
+++ /dev/null
@@ -1,491 +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_testing_netjail_lib.h"
30#include "gnunet_peerstore_service.h"
31#include "gnunet_transport_core_service.h"
32#include "gnunet_transport_application_service.h"
33#include "transport-testing-cmds.h"
34
35/**
36 * Generic logging shortcut
37 */
38#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
39
40
41static void
42retrieve_hello (void *cls);
43
44
45/**
46 * Callback delivering the hello of this peer from peerstore.
47 *
48 */
49static void
50hello_iter_cb (void *cb_cls,
51 const struct GNUNET_PEERSTORE_Record *record,
52 const char *emsg)
53{
54 struct StartPeerState *sps = cb_cls;
55 if (NULL == record)
56 {
57 sps->pic = NULL;
58 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
59 return;
60 }
61 // Check record type et al?
62 sps->hello_size = record->value_size;
63 sps->hello = GNUNET_malloc (sps->hello_size);
64 memcpy (sps->hello, record->value, sps->hello_size);
65 sps->hello[sps->hello_size - 1] = '\0';
66
67 GNUNET_PEERSTORE_iterate_cancel (sps->pic);
68 sps->pic = NULL;
69 GNUNET_TESTING_async_finish (&sps->ac);
70}
71
72
73/**
74 * Function to start the retrieval task to retrieve the hello of this peer
75 * from the peerstore.
76 *
77 */
78static void
79retrieve_hello (void *cls)
80{
81 struct StartPeerState *sps = cls;
82 sps->rh_task = NULL;
83 sps->pic = GNUNET_PEERSTORE_iterate (sps->ph,
84 "transport",
85 &sps->id,
86 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY,
87 hello_iter_cb,
88 sps);
89
90}
91
92
93/**
94 * Disconnect callback for the connection to the core service.
95 *
96 */
97static void
98notify_disconnect (void *cls,
99 const struct GNUNET_PeerIdentity *peer,
100 void *handler_cls)
101{
102 struct StartPeerState *sps = cls;
103
104 LOG (GNUNET_ERROR_TYPE_DEBUG,
105 "Peer %s disconnected from peer %u (`%s')\n",
106 GNUNET_i2s (peer),
107 sps->no,
108 GNUNET_i2s (&sps->id));
109
110}
111
112
113/**
114 * Connect callback for the connection to the core service.
115 *
116 */
117static void *
118notify_connect (void *cls,
119 const struct GNUNET_PeerIdentity *peer,
120 struct GNUNET_MQ_Handle *mq)
121{
122 struct StartPeerState *sps = cls;
123 struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode);
124 struct GNUNET_HashCode hc;
125 struct GNUNET_CRYPTO_EddsaPublicKey public_key = peer->public_key;
126
127 void *ret = (struct GNUNET_PeerIdentity *) peer;
128
129 LOG (GNUNET_ERROR_TYPE_DEBUG,
130 "This Peer %s \n",
131 GNUNET_i2s (&sps->id));
132 LOG (GNUNET_ERROR_TYPE_DEBUG,
133 "Peer %s connected to peer number %u\n",
134 GNUNET_i2s (peer),
135 sps->no);
136
137 GNUNET_CRYPTO_hash (&public_key, sizeof(public_key), &hc);
138
139
140 memcpy (key,
141 &hc,
142 sizeof (*key));
143 GNUNET_CONTAINER_multishortmap_put (sps->connected_peers_map,
144 key,
145 mq,
146 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
147
148 GNUNET_free (key);
149
150 sps->notify_connect (sps->ac.is,
151 peer);
152
153 return ret;
154}
155
156
157/**
158 * The run method of this cmd will start all services of a peer to test the transport service.
159 *
160 */
161static void
162start_peer_run (void *cls,
163 struct GNUNET_TESTING_Interpreter *is)
164{
165 struct StartPeerState *sps = cls;
166 char *emsg = NULL;
167 struct GNUNET_PeerIdentity dummy;
168 const struct GNUNET_TESTING_Command *system_cmd;
169 const struct GNUNET_TESTING_System *tl_system;
170 char *home;
171 char *transport_unix_path;
172 char *tcp_communicator_unix_path;
173 char *udp_communicator_unix_path;
174 char *bindto;
175 char *bindto_udp;
176
177 if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname))
178 {
179 LOG (GNUNET_ERROR_TYPE_ERROR,
180 "File not found: `%s'\n",
181 sps->cfgname);
182 GNUNET_TESTING_interpreter_fail (is);
183 return;
184 }
185
186
187 sps->cfg = GNUNET_CONFIGURATION_create ();
188 GNUNET_assert (GNUNET_OK ==
189 GNUNET_CONFIGURATION_load (sps->cfg, sps->cfgname));
190
191 GNUNET_asprintf (&home,
192 "$GNUNET_TMP/test-transport/api-tcp-p%u",
193 sps->no);
194
195 GNUNET_asprintf (&transport_unix_path,
196 "$GNUNET_RUNTIME_DIR/tng-p%u.sock",
197 sps->no);
198
199 GNUNET_asprintf (&tcp_communicator_unix_path,
200 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
201 sps->no);
202
203 GNUNET_asprintf (&udp_communicator_unix_path,
204 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
205 sps->no);
206
207 GNUNET_asprintf (&bindto,
208 "%s:60002",
209 sps->node_ip);
210
211 GNUNET_asprintf (&bindto_udp,
212 "2086");
213
214 LOG (GNUNET_ERROR_TYPE_DEBUG,
215 "node_ip %s\n",
216 bindto);
217
218 LOG (GNUNET_ERROR_TYPE_DEBUG,
219 "bind_udp %s\n",
220 GNUNET_YES == sps->broadcast ?
221 bindto_udp : bindto);
222
223 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "PATHS", "GNUNET_TEST_HOME",
224 home);
225 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "transport", "UNIXPATH",
226 transport_unix_path);
227 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
228 "BINDTO",
229 bindto);
230 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp",
231 "BINDTO",
232 GNUNET_YES == sps->broadcast ?
233 bindto_udp : bindto);
234 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
235 "UNIXPATH",
236 tcp_communicator_unix_path);
237 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp",
238 "UNIXPATH",
239 udp_communicator_unix_path);
240
241 system_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
242 sps->system_label);
243 GNUNET_TESTING_get_trait_test_system (system_cmd,
244 &tl_system);
245
246 sps->tl_system = tl_system;
247
248 LOG (GNUNET_ERROR_TYPE_DEBUG,
249 "Creating testing library with key number %u\n",
250 sps->no);
251
252 if (GNUNET_SYSERR ==
253 GNUNET_TESTING_configuration_create ((struct
254 GNUNET_TESTING_System *) tl_system,
255 sps->cfg))
256 {
257 LOG (GNUNET_ERROR_TYPE_DEBUG,
258 "Testing library failed to create unique configuration based on `%s'\n",
259 sps->cfgname);
260 GNUNET_CONFIGURATION_destroy (sps->cfg);
261 GNUNET_TESTING_interpreter_fail (is);
262 return;
263 }
264
265 sps->peer = GNUNET_TESTING_peer_configure ((struct
266 GNUNET_TESTING_System *) sps->
267 tl_system,
268 sps->cfg,
269 sps->no,
270 NULL,
271 &emsg);
272 if (NULL == sps->peer)
273 {
274 LOG (GNUNET_ERROR_TYPE_ERROR,
275 "Testing library failed to create unique configuration based on `%s': `%s' with key number %u\n",
276 sps->cfgname,
277 emsg,
278 sps->no);
279 GNUNET_free (emsg);
280 GNUNET_TESTING_interpreter_fail (is);
281 return;
282 }
283
284 if (GNUNET_OK != GNUNET_TESTING_peer_start (sps->peer))
285 {
286 LOG (GNUNET_ERROR_TYPE_ERROR,
287 "Testing library failed to create unique configuration based on `%s'\n",
288 sps->cfgname);
289 GNUNET_free (emsg);
290 GNUNET_TESTING_interpreter_fail (is);
291 return;
292 }
293
294 memset (&dummy,
295 '\0',
296 sizeof(dummy));
297
298 GNUNET_TESTING_peer_get_identity (sps->peer,
299 &sps->id);
300
301 if (0 == memcmp (&dummy,
302 &sps->id,
303 sizeof(struct GNUNET_PeerIdentity)))
304 {
305 LOG (GNUNET_ERROR_TYPE_ERROR,
306 "Testing library failed to obtain peer identity for peer %u\n",
307 sps->no);
308 GNUNET_free (emsg);
309 GNUNET_TESTING_interpreter_fail (is);
310 return;
311 }
312 LOG (GNUNET_ERROR_TYPE_DEBUG,
313 "Peer %u configured with identity `%s'\n",
314 sps->no,
315 GNUNET_i2s_full (&sps->id));
316
317 sps->th = GNUNET_TRANSPORT_core_connect (sps->cfg,
318 NULL,
319 sps->handlers,
320 sps,
321 &notify_connect,
322 &notify_disconnect);
323 if (NULL == sps->th)
324 {
325 LOG (GNUNET_ERROR_TYPE_ERROR,
326 "Failed to connect to transport service for peer `%s': `%s'\n",
327 sps->cfgname,
328 emsg);
329 GNUNET_free (emsg);
330 GNUNET_TESTING_interpreter_fail (is);
331 return;
332 }
333
334 sps->ph = GNUNET_PEERSTORE_connect (sps->cfg);
335 if (NULL == sps->th)
336 {
337 LOG (GNUNET_ERROR_TYPE_ERROR,
338 "Failed to connect to peerstore service for peer `%s': `%s'\n",
339 sps->cfgname,
340 emsg);
341 GNUNET_free (emsg);
342 GNUNET_TESTING_interpreter_fail (is);
343 return;
344 }
345
346 sps->ah = GNUNET_TRANSPORT_application_init (sps->cfg);
347 if (NULL == sps->ah)
348 {
349 LOG (GNUNET_ERROR_TYPE_ERROR,
350 "Failed to initialize the TRANSPORT application suggestion client handle for peer `%s': `%s'\n",
351 sps->cfgname,
352 emsg);
353 GNUNET_free (emsg);
354 GNUNET_TESTING_interpreter_fail (is);
355 return;
356 }
357 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
358 GNUNET_free (home);
359 GNUNET_free (transport_unix_path);
360 GNUNET_free (tcp_communicator_unix_path);
361 GNUNET_free (udp_communicator_unix_path);
362 GNUNET_free (bindto);
363 GNUNET_free (bindto_udp);
364}
365
366
367/**
368 * The cleanup function of this cmd frees resources the cmd allocated.
369 *
370 */
371static void
372start_peer_cleanup (void *cls)
373{
374 struct StartPeerState *sps = cls;
375
376 if (NULL != sps->handlers)
377 {
378 GNUNET_free (sps->handlers);
379 sps->handlers = NULL;
380 }
381 if (NULL != sps->cfg)
382 {
383 GNUNET_CONFIGURATION_destroy (sps->cfg);
384 sps->cfg = NULL;
385 }
386 GNUNET_free (sps->hello);
387 GNUNET_free (sps->connected_peers_map);
388 GNUNET_free (sps);
389}
390
391
392/**
393 * This function prepares an array with traits.
394 *
395 */
396static int
397start_peer_traits (void *cls,
398 const void **ret,
399 const char *trait,
400 unsigned int index)
401{
402 struct StartPeerState *sps = cls;
403 struct GNUNET_TRANSPORT_ApplicationHandle *ah = sps->ah;
404 struct GNUNET_PeerIdentity *id = &sps->id;
405 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
406 sps->connected_peers_map;
407 char *hello = sps->hello;
408 size_t hello_size = sps->hello_size;
409
410
411 struct GNUNET_TESTING_Trait traits[] = {
412 GNUNET_TRANSPORT_make_trait_application_handle ((const void *) ah),
413 GNUNET_TRANSPORT_make_trait_peer_id ((const void *) id),
414 GNUNET_TRANSPORT_make_trait_connected_peers_map ((const
415 void *)
416 connected_peers_map),
417 GNUNET_TRANSPORT_make_trait_hello ((const void *) hello),
418 GNUNET_TRANSPORT_make_trait_hello_size ((const void *) hello_size),
419 GNUNET_TRANSPORT_make_trait_state ((const void *) sps),
420 GNUNET_TRANSPORT_make_trait_broadcast ((const void *) &sps->broadcast),
421 GNUNET_TESTING_trait_end ()
422 };
423
424 return GNUNET_TESTING_get_trait (traits,
425 ret,
426 trait,
427 index);
428}
429
430
431/**
432 * Create command.
433 *
434 * @param label name for command.
435 * @param system_label Label of the cmd to setup a test environment.
436 * @param m The number of the local node of the actual network namespace.
437 * @param n The number of the actual namespace.
438 * @param local_m Number of local nodes in each namespace.
439 * @param handlers Handler for messages received by this peer.
440 * @param cfgname Configuration file name for this peer.
441 * @param notify_connect Method which will be called, when a peer connects.
442 * @param broadcast Flag indicating, if broadcast should be switched on.
443 * @return command.
444 */
445struct GNUNET_TESTING_Command
446GNUNET_TRANSPORT_cmd_start_peer (const char *label,
447 const char *system_label,
448 uint32_t no,
449 char *node_ip,
450 struct GNUNET_MQ_MessageHandler *handlers,
451 const char *cfgname,
452 GNUNET_TRANSPORT_notify_connect_cb
453 notify_connect,
454 unsigned int broadcast)
455{
456 struct StartPeerState *sps;
457 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
458 GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO);
459 unsigned int i;
460
461 sps = GNUNET_new (struct StartPeerState);
462 sps->no = no;
463 sps->system_label = system_label;
464 sps->connected_peers_map = connected_peers_map;
465 sps->cfgname = cfgname;
466 sps->node_ip = node_ip;
467 sps->notify_connect = notify_connect;
468 sps->broadcast = broadcast;
469
470 if (NULL != handlers)
471 {
472 for (i = 0; NULL != handlers[i].cb; i++)
473 ;
474 sps->handlers = GNUNET_new_array (i + 1,
475 struct GNUNET_MQ_MessageHandler);
476 GNUNET_memcpy (sps->handlers,
477 handlers,
478 i * sizeof(struct GNUNET_MQ_MessageHandler));
479 }
480
481 struct GNUNET_TESTING_Command cmd = {
482 .cls = sps,
483 .label = label,
484 .run = &start_peer_run,
485 .ac = &sps->ac,
486 .cleanup = &start_peer_cleanup,
487 .traits = &start_peer_traits
488 };
489
490 return cmd;
491}