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