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 be2ea25b4..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 = NULL;
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 // TODO what does the handler function need?
154 return ret;
155}
156
157
158/**
159 * The run method of this cmd will start all services of a peer to test the transport service.
160 *
161 */
162static void
163start_peer_run (void *cls,
164 struct GNUNET_TESTING_Interpreter *is)
165{
166 struct StartPeerState *sps = cls;
167 char *emsg = NULL;
168 struct GNUNET_PeerIdentity dummy;
169 const struct GNUNET_TESTING_Command *system_cmd;
170 const struct GNUNET_TESTING_System *tl_system;
171 char *home;
172 char *transport_unix_path;
173 char *tcp_communicator_unix_path;
174 char *udp_communicator_unix_path;
175 char *bindto;
176 char *bindto_udp;
177
178 if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname))
179 {
180 LOG (GNUNET_ERROR_TYPE_ERROR,
181 "File not found: `%s'\n",
182 sps->cfgname);
183 GNUNET_TESTING_interpreter_fail (is);
184 return;
185 }
186
187
188 sps->cfg = GNUNET_CONFIGURATION_create ();
189 GNUNET_assert (GNUNET_OK ==
190 GNUNET_CONFIGURATION_load (sps->cfg, sps->cfgname));
191
192 GNUNET_asprintf (&home,
193 "$GNUNET_TMP/test-transport/api-tcp-p%u",
194 sps->no);
195
196 GNUNET_asprintf (&transport_unix_path,
197 "$GNUNET_RUNTIME_DIR/tng-p%u.sock",
198 sps->no);
199
200 GNUNET_asprintf (&tcp_communicator_unix_path,
201 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
202 sps->no);
203
204 GNUNET_asprintf (&udp_communicator_unix_path,
205 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
206 sps->no);
207
208 GNUNET_asprintf (&bindto,
209 "%s:60002",
210 sps->node_ip);
211
212 GNUNET_asprintf (&bindto_udp,
213 "2086");
214
215 LOG (GNUNET_ERROR_TYPE_DEBUG,
216 "node_ip %s\n",
217 bindto);
218
219 LOG (GNUNET_ERROR_TYPE_DEBUG,
220 "bind_udp %s\n",
221 GNUNET_YES == sps->broadcast ?
222 bindto_udp : bindto);
223
224 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "PATHS", "GNUNET_TEST_HOME",
225 home);
226 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "transport", "UNIXPATH",
227 transport_unix_path);
228 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
229 "BINDTO",
230 bindto);
231 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp",
232 "BINDTO",
233 GNUNET_YES == sps->broadcast ?
234 bindto_udp : bindto);
235 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
236 "UNIXPATH",
237 tcp_communicator_unix_path);
238 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp",
239 "UNIXPATH",
240 udp_communicator_unix_path);
241
242 system_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
243 sps->system_label);
244 GNUNET_TESTING_get_trait_test_system (system_cmd,
245 &tl_system);
246
247 sps->tl_system = tl_system;
248
249 LOG (GNUNET_ERROR_TYPE_DEBUG,
250 "Creating testing library with key number %u\n",
251 sps->no);
252
253 if (GNUNET_SYSERR ==
254 GNUNET_TESTING_configuration_create ((struct
255 GNUNET_TESTING_System *) tl_system,
256 sps->cfg))
257 {
258 LOG (GNUNET_ERROR_TYPE_DEBUG,
259 "Testing library failed to create unique configuration based on `%s'\n",
260 sps->cfgname);
261 GNUNET_CONFIGURATION_destroy (sps->cfg);
262 GNUNET_TESTING_interpreter_fail (is);
263 return;
264 }
265
266 sps->peer = GNUNET_TESTING_peer_configure ((struct
267 GNUNET_TESTING_System *) sps->
268 tl_system,
269 sps->cfg,
270 sps->no,
271 NULL,
272 &emsg);
273 if (NULL == sps->peer)
274 {
275 LOG (GNUNET_ERROR_TYPE_ERROR,
276 "Testing library failed to create unique configuration based on `%s': `%s' with key number %u\n",
277 sps->cfgname,
278 emsg,
279 sps->no);
280 GNUNET_free (emsg);
281 GNUNET_TESTING_interpreter_fail (is);
282 return;
283 }
284
285 if (GNUNET_OK != GNUNET_TESTING_peer_start (sps->peer))
286 {
287 LOG (GNUNET_ERROR_TYPE_ERROR,
288 "Testing library failed to create unique configuration based on `%s'\n",
289 sps->cfgname);
290 GNUNET_free (emsg);
291 GNUNET_TESTING_interpreter_fail (is);
292 return;
293 }
294
295 memset (&dummy,
296 '\0',
297 sizeof(dummy));
298
299 GNUNET_TESTING_peer_get_identity (sps->peer,
300 &sps->id);
301
302 if (0 == memcmp (&dummy,
303 &sps->id,
304 sizeof(struct GNUNET_PeerIdentity)))
305 {
306 LOG (GNUNET_ERROR_TYPE_ERROR,
307 "Testing library failed to obtain peer identity for peer %u\n",
308 sps->no);
309 GNUNET_free (emsg);
310 GNUNET_TESTING_interpreter_fail (is);
311 return;
312 }
313 LOG (GNUNET_ERROR_TYPE_DEBUG,
314 "Peer %u configured with identity `%s'\n",
315 sps->no,
316 GNUNET_i2s_full (&sps->id));
317
318 sps->th = GNUNET_TRANSPORT_core_connect (sps->cfg,
319 NULL,
320 sps->handlers,
321 sps,
322 &notify_connect,
323 &notify_disconnect);
324 if (NULL == sps->th)
325 {
326 LOG (GNUNET_ERROR_TYPE_ERROR,
327 "Failed to connect to transport service for peer `%s': `%s'\n",
328 sps->cfgname,
329 emsg);
330 GNUNET_free (emsg);
331 GNUNET_TESTING_interpreter_fail (is);
332 return;
333 }
334
335 sps->ph = GNUNET_PEERSTORE_connect (sps->cfg);
336 if (NULL == sps->th)
337 {
338 LOG (GNUNET_ERROR_TYPE_ERROR,
339 "Failed to connect to peerstore service for peer `%s': `%s'\n",
340 sps->cfgname,
341 emsg);
342 GNUNET_free (emsg);
343 GNUNET_TESTING_interpreter_fail (is);
344 return;
345 }
346
347 sps->ah = GNUNET_TRANSPORT_application_init (sps->cfg);
348 if (NULL == sps->ah)
349 {
350 LOG (GNUNET_ERROR_TYPE_ERROR,
351 "Failed to initialize the TRANSPORT application suggestion client handle for peer `%s': `%s'\n",
352 sps->cfgname,
353 emsg);
354 GNUNET_free (emsg);
355 GNUNET_TESTING_interpreter_fail (is);
356 return;
357 }
358 sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, sps);
359 GNUNET_free (home);
360 GNUNET_free (transport_unix_path);
361 GNUNET_free (tcp_communicator_unix_path);
362 GNUNET_free (udp_communicator_unix_path);
363 GNUNET_free (bindto);
364 GNUNET_free (bindto_udp);
365}
366
367
368/**
369 * The cleanup function of this cmd frees resources the cmd allocated.
370 *
371 */
372static void
373start_peer_cleanup (void *cls)
374{
375 struct StartPeerState *sps = cls;
376
377 if (NULL != sps->handlers)
378 {
379 GNUNET_free (sps->handlers);
380 sps->handlers = NULL;
381 }
382 if (NULL != sps->cfg)
383 {
384 GNUNET_CONFIGURATION_destroy (sps->cfg);
385 sps->cfg = NULL;
386 }
387 GNUNET_free (sps->hello);
388 GNUNET_free (sps->connected_peers_map);
389 GNUNET_free (sps);
390}
391
392
393/**
394 * This function prepares an array with traits.
395 *
396 */
397static int
398start_peer_traits (void *cls,
399 const void **ret,
400 const char *trait,
401 unsigned int index)
402{
403 struct StartPeerState *sps = cls;
404 struct GNUNET_TRANSPORT_ApplicationHandle *ah = sps->ah;
405 struct GNUNET_PeerIdentity *id = &sps->id;
406 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
407 sps->connected_peers_map;
408 char *hello = sps->hello;
409 size_t hello_size = sps->hello_size;
410
411
412 struct GNUNET_TESTING_Trait traits[] = {
413 GNUNET_TRANSPORT_make_trait_application_handle ((const void *) ah),
414 GNUNET_TRANSPORT_make_trait_peer_id ((const void *) id),
415 GNUNET_TRANSPORT_make_trait_connected_peers_map ((const
416 void *)
417 connected_peers_map),
418 GNUNET_TRANSPORT_make_trait_hello ((const void *) hello),
419 GNUNET_TRANSPORT_make_trait_hello_size ((const void *) hello_size),
420 GNUNET_TRANSPORT_make_trait_state ((const void *) sps),
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}