GNUnet 0.21.0
transport_api_cmd_start_peer.c
Go to the documentation of this file.
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
26#include "platform.h"
27#include "gnunet_util_lib.h"
34
38#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
39
40
41static void
42retrieve_hello (void *cls);
43
44
49static void
50hello_iter_cb (void *cb_cls,
51 const struct GNUNET_PEERSTORE_Record *record,
52 const char *emsg)
53{
54 struct GNUNET_TESTING_StartPeerState *sps = cb_cls;
55 if (NULL == record)
56 {
57 sps->pic = NULL;
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
68 "Our hello %s\n",
69 sps->hello);
71 sps->pic = NULL;
74}
75
76
82static void
83retrieve_hello (void *cls)
84{
85 struct GNUNET_TESTING_StartPeerState *sps = cls;
86 sps->rh_task = NULL;
88 "transport",
89 &sps->id,
92 sps);
93
94}
95
96
101static void
103 const struct GNUNET_PeerIdentity *peer,
104 void *handler_cls)
105{
106 struct GNUNET_TESTING_StartPeerState *sps = cls;
107
109 "Peer %s disconnected from peer %u (`%s')\n",
111 sps->no,
112 GNUNET_i2s (&sps->id));
113
114}
115
116
121static void *
122notify_connect (void *cls,
123 const struct GNUNET_PeerIdentity *peer,
124 struct GNUNET_MQ_Handle *mq)
125{
126 struct GNUNET_TESTING_StartPeerState *sps = cls;
128 struct GNUNET_HashCode hc;
129 struct GNUNET_CRYPTO_EddsaPublicKey public_key = peer->public_key;
130
131 void *ret = (struct GNUNET_PeerIdentity *) peer;
132
134 "This Peer %s \n",
135 GNUNET_i2s (&sps->id));
136
137
139
141 "Peer %s connected to peer number %u with mq %p\n",
142 GNUNET_i2s (peer),
143 sps->no,
144 mq);
145
146
147 memcpy (key,
148 &hc,
149 sizeof (*key));
151 key,
152 mq,
154
156
157 sps->notify_connect (sps->ac.is,
158 peer);
159
160 return ret;
161}
162
163
168static void
169start_peer_run (void *cls,
171{
172 struct GNUNET_TESTING_StartPeerState *sps = cls;
173 char *emsg = NULL;
175 const struct GNUNET_TESTING_Command *system_cmd;
176 const struct GNUNET_TESTING_System *tl_system;
177 char *home;
178 char *transport_unix_path;
179 char *tcp_communicator_unix_path;
180 char *udp_communicator_unix_path;
181 char *bindto;
182 char *bindto_udp;
183
185 {
187 "File not found: `%s'\n",
188 sps->cfgname);
190 return;
191 }
192
193
197
198 GNUNET_asprintf (&home,
199 "$GNUNET_TMP/test-transport/api-tcp-p%u",
200 sps->no);
201
202 GNUNET_asprintf (&transport_unix_path,
203 "$GNUNET_RUNTIME_DIR/tng-p%u.sock",
204 sps->no);
205
206 GNUNET_asprintf (&tcp_communicator_unix_path,
207 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
208 sps->no);
209
210 GNUNET_asprintf (&udp_communicator_unix_path,
211 "$GNUNET_RUNTIME_DIR/tcp-comm-p%u.sock",
212 sps->no);
213
214 GNUNET_asprintf (&bindto,
215 "%s:60002",
216 sps->node_ip);
217
218 GNUNET_asprintf (&bindto_udp,
219 "2086");
220
222 "node_ip %s\n",
223 bindto);
224
226 "bind_udp %s\n",
227 GNUNET_YES == sps->broadcast ?
228 bindto_udp : bindto);
229
230 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "PATHS", "GNUNET_TEST_HOME",
231 home);
232 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "transport", "UNIXPATH",
233 transport_unix_path);
234 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
235 "BINDTO",
236 bindto);
237 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp",
238 "BINDTO",
239 GNUNET_YES == sps->broadcast ?
240 bindto_udp : bindto);
241 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-tcp",
242 "UNIXPATH",
243 tcp_communicator_unix_path);
244 GNUNET_CONFIGURATION_set_value_string (sps->cfg, "communicator-udp",
245 "UNIXPATH",
246 udp_communicator_unix_path);
247
248
250 sps->system_label);
252 &tl_system);
253
254 sps->tl_system = tl_system;
255
257 "Creating testing library with key number %u\n",
258 sps->no);
259
260 if (GNUNET_SYSERR ==
262 GNUNET_TESTING_System *) tl_system,
263 sps->cfg))
264 {
266 "Testing library failed to create unique configuration based on `%s'\n",
267 sps->cfgname);
270 return;
271 }
272
273 sps->peer = GNUNET_TESTING_peer_configure ((struct
274 GNUNET_TESTING_System *) sps->
275 tl_system,
276 sps->cfg,
277 sps->no,
278 NULL,
279 &emsg);
280 if (NULL == sps->peer)
281 {
283 "Testing library failed to create unique configuration based on `%s': `%s' with key number %u\n",
284 sps->cfgname,
285 emsg,
286 sps->no);
287 GNUNET_free (emsg);
289 return;
290 }
291
293 {
295 "Testing library failed to create unique configuration based on `%s'\n",
296 sps->cfgname);
297 GNUNET_free (emsg);
299 return;
300 }
301
302 memset (&dummy,
303 '\0',
304 sizeof(dummy));
305
307 &sps->id);
308
309 if (0 == memcmp (&dummy,
310 &sps->id,
311 sizeof(struct GNUNET_PeerIdentity)))
312 {
314 "Testing library failed to obtain peer identity for peer %u\n",
315 sps->no);
316 GNUNET_free (emsg);
318 return;
319 }
321 "Peer %u configured with identity `%s'\n",
322 sps->no,
323 GNUNET_i2s_full (&sps->id));
324
326 NULL,
327 sps->handlers,
328 sps,
331 if (NULL == sps->th)
332 {
334 "Failed to connect to transport service for peer `%s': `%s'\n",
335 sps->cfgname,
336 emsg);
337 GNUNET_free (emsg);
339 return;
340 }
341
342 sps->ph = GNUNET_PEERSTORE_connect (sps->cfg);
343 if (NULL == sps->ph)
344 {
346 "Failed to connect to peerstore service for peer `%s': `%s'\n",
347 sps->cfgname,
348 emsg);
349 GNUNET_free (emsg);
351 return;
352 }
353
355 if (NULL == sps->ah)
356 {
358 "Failed to initialize the TRANSPORT application suggestion client handle for peer `%s': `%s'\n",
359 sps->cfgname,
360 emsg);
361 GNUNET_free (emsg);
363 return;
364 }
366 GNUNET_free (home);
367 GNUNET_free (transport_unix_path);
368 GNUNET_free (tcp_communicator_unix_path);
369 GNUNET_free (udp_communicator_unix_path);
370 GNUNET_free (bindto);
371 GNUNET_free (bindto_udp);
372}
373
374
379static void
381{
382 struct GNUNET_TESTING_StartPeerState *sps = cls;
383
384 if (NULL != sps->handlers)
385 {
386 GNUNET_free (sps->handlers);
387 sps->handlers = NULL;
388 }
389 // TODO Investigate why this caused problems during shutdown.
390 /*if (NULL != sps->cfg)
391 {
392 GNUNET_CONFIGURATION_destroy (sps->cfg);
393 sps->cfg = NULL;
394 }*/
395 GNUNET_free (sps->cfgname);
396 GNUNET_free (sps->node_ip);
398 GNUNET_free (sps->hello);
400 GNUNET_free (sps);
401}
402
403
408static int
410 const void **ret,
411 const char *trait,
412 unsigned int index)
413{
414 struct GNUNET_TESTING_StartPeerState *sps = cls;
416 struct GNUNET_PeerIdentity *id = &sps->id;
417 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
419 char *hello = sps->hello;
420 size_t hello_size = sps->hello_size;
421
422
423 struct GNUNET_TESTING_Trait traits[] = {
427 void *)
428 connected_peers_map),
429 GNUNET_TRANSPORT_TESTING_make_trait_hello ((const void *) hello),
430 GNUNET_TRANSPORT_TESTING_make_trait_hello_size ((const void *) hello_size),
433 void *) &sps->broadcast),
435 };
436
437 return GNUNET_TESTING_get_trait (traits,
438 ret,
439 trait,
440 index);
441}
442
443
446 const char *system_label,
447 uint32_t no,
448 const char *node_ip,
450 const char *cfgname,
453 unsigned int broadcast)
454{
456 struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
458 unsigned int i;
459
461 sps->no = no;
462 sps->system_label = GNUNET_strdup (system_label);
463 sps->connected_peers_map = connected_peers_map;
464 sps->cfgname = GNUNET_strdup (cfgname);
465 sps->node_ip = GNUNET_strdup (node_ip);
467 sps->broadcast = broadcast;
468
469 if (NULL != handlers)
470 {
471 for (i = 0; NULL != handlers[i].cb; i++)
472 ;
473 sps->handlers = GNUNET_new_array (i + 1,
476 handlers,
477 i * sizeof(struct GNUNET_MQ_MessageHandler));
478 }
479 return GNUNET_TESTING_command_new (sps,
480 label,
484 &sps->ac);
485}
struct GNUNET_MQ_MessageHandlers handlers[]
Definition: 003.c:1
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
static int ret
Final status code.
Definition: gnunet-arm.c:94
struct GNUNET_TESTING_Interpreter * is
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
struct GNUNET_HashCode key
The key used in the DHT.
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
static struct GNUNET_NAT_AUTO_AutoHandle * ah
Handle to ongoing autoconfiguration.
API to the peerstore service.
void GNUNET_TESTING_async_finish(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has finished.
const struct GNUNET_TESTING_Command * GNUNET_TESTING_interpreter_lookup_command(struct GNUNET_TESTING_Interpreter *is, const char *label)
Lookup command by label.
struct GNUNET_TESTING_Command GNUNET_TESTING_command_new(void *cls, const char *label, GNUNET_TESTING_CommandRunRoutine run, GNUNET_TESTING_CommandCleanupRoutine cleanup, GNUNET_TESTING_CommandGetTraits traits, struct GNUNET_TESTING_AsyncContext *ac)
Create a new command.
struct GNUNET_TESTING_Trait GNUNET_TESTING_trait_end(void)
"end" of traits array.
enum GNUNET_GenericReturnValue GNUNET_TESTING_get_trait(const struct GNUNET_TESTING_Trait *traits, const void **ret, const char *trait, unsigned int index)
Obtain value of a trait from a command.
void GNUNET_TESTING_interpreter_fail(struct GNUNET_TESTING_Interpreter *is)
Current command failed, clean up and fail the test case.
enum GNUNET_GenericReturnValue GNUNET_TESTING_get_trait_test_system(const struct GNUNET_TESTING_Command *cmd, const struct GNUNET_TESTING_System **ret)
Bandwidth allocation API for applications to interact with.
API of the transport service towards the CORE service (TNG version)
struct GNUNET_TESTING_Trait GNUNET_TRANSPORT_TESTING_make_trait_broadcast(const enum GNUNET_GenericReturnValue *value)
struct GNUNET_TESTING_Trait GNUNET_TRANSPORT_TESTING_make_trait_state(const struct GNUNET_TESTING_StartPeerState *value)
struct GNUNET_TESTING_Trait GNUNET_TRANSPORT_TESTING_make_trait_hello(const char *value)
struct GNUNET_TESTING_Trait GNUNET_TRANSPORT_TESTING_make_trait_application_handle(const struct GNUNET_TRANSPORT_ApplicationHandle *value)
struct GNUNET_TESTING_Trait GNUNET_TRANSPORT_TESTING_make_trait_connected_peers_map(const struct GNUNET_CONTAINER_MultiShortmap *value)
struct GNUNET_TESTING_Trait GNUNET_TRANSPORT_TESTING_make_trait_peer_id(const struct GNUNET_PeerIdentity *value)
struct GNUNET_TESTING_Trait GNUNET_TRANSPORT_TESTING_make_trait_hello_size(const size_t *value)
struct GNUNET_TRANSPORT_CoreHandle * GNUNET_TRANSPORT_core_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_PeerIdentity *self, const struct GNUNET_MQ_MessageHandler *handlers, void *cls, GNUNET_TRANSPORT_NotifyConnect nc, GNUNET_TRANSPORT_NotifyDisconnect nd)
Connect to the transport service.
struct GNUNET_TRANSPORT_ApplicationHandle * GNUNET_TRANSPORT_application_init(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the TRANSPORT application client handle.
void GNUNET_CONFIGURATION_set_value_string(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value)
Set a configuration value that should be a string.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory).
Definition: disk.c:482
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
struct GNUNET_CONTAINER_MultiShortmap * GNUNET_CONTAINER_multishortmap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multishortmap_put(struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE
Allow multiple values with the same key.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_PEERSTORE_IterateContext * GNUNET_PEERSTORE_iteration_start(struct GNUNET_PEERSTORE_Handle *h, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, GNUNET_PEERSTORE_Processor callback, void *callback_cls)
Iterate over peerstore entries.
void GNUNET_PEERSTORE_iteration_next(struct GNUNET_PEERSTORE_IterateContext *ic, uint64_t limit)
Continue an iteration.
struct GNUNET_PEERSTORE_Handle * GNUNET_PEERSTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the PEERSTORE service.
#define GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY
Key used for storing HELLOs in the peerstore.
void GNUNET_PEERSTORE_iteration_stop(struct GNUNET_PEERSTORE_IterateContext *ic)
Cancel an iteration.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1299
int GNUNET_TESTING_peer_start(struct GNUNET_TESTING_Peer *peer)
Start the peer.
Definition: testing.c:1381
struct GNUNET_TESTING_Peer * GNUNET_TESTING_peer_configure(struct GNUNET_TESTING_System *system, struct GNUNET_CONFIGURATION_Handle *cfg, uint32_t key_number, struct GNUNET_PeerIdentity *id, char **emsg)
Configure a GNUnet peer.
Definition: testing.c:1188
int GNUNET_TESTING_configuration_create(struct GNUNET_TESTING_System *system, struct GNUNET_CONFIGURATION_Handle *cfg)
Create a new configuration using the given configuration as a template; ports and paths will be modif...
Definition: testing.c:1167
void GNUNET_TESTING_peer_get_identity(struct GNUNET_TESTING_Peer *peer, struct GNUNET_PeerIdentity *id)
Obtain the peer identity from a peer handle.
Definition: testing.c:1359
Internal representation of the hash map.
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...
A 512-bit hashcode.
Handle to a message queue.
Definition: mq.c:87
Message handler for a specific message type.
Single PEERSTORE record.
The identity of the host (wraps the signing key of the peer).
struct GNUNET_CRYPTO_EddsaPublicKey public_key
A 256-bit hashcode.
struct GNUNET_TESTING_Interpreter * is
Interpreter we are part of.
A command to be run by the interpreter.
struct GNUNET_TESTING_CommandLabel label
Label for the command.
Global state of the interpreter, used by a command to access information about other commands.
struct GNUNET_TESTING_AsyncContext ac
Context for our asynchronous completion.
struct GNUNET_PeerIdentity id
Peer identity.
struct GNUNET_PEERSTORE_IterateContext * pic
Peer's transport get hello handle to retrieve peer's HELLO message.
const struct GNUNET_TESTING_System * tl_system
Test setup for this peer.
struct GNUNET_TRANSPORT_ApplicationHandle * ah
Application handle.
struct GNUNET_CONFIGURATION_Handle * cfg
Peer's configuration.
struct GNUNET_MQ_MessageHandler * handlers
Receive callback.
char * system_label
The label of the command which was started by calling GNUNET_TESTING_cmd_system_create.
unsigned int no
An unique number to identify the peer.
struct GNUNET_CONTAINER_MultiShortmap * connected_peers_map
A map with struct GNUNET_MQ_Handle values for each peer this peer is connected to.
struct GNUNET_PEERSTORE_Handle * ph
Peer's PEERSTORE Handle.
struct GNUNET_TESTING_Peer * peer
struct GNUNET_TESTING_Peer returned by GNUNET_TESTING_peer_configure.
GNUNET_TESTING_notify_connect_cb notify_connect
Callback which is called on neighbour connect events.
enum GNUNET_GenericReturnValue broadcast
Flag indicating, if udp broadcast should be switched on.
struct GNUNET_TRANSPORT_CoreHandle * th
Peer's transport service handle.
struct GNUNET_SCHEDULER_Task * rh_task
Hello get task.
char * cfgname
GNUnet configuration file used to start a peer.
Handle for a system on which GNUnet peers are executed; a system is used for reserving unique paths a...
Definition: testing.c:122
A struct GNUNET_TESTING_Trait can be used to exchange data between cmds.
unsigned int index
Index number associated with the trait.
Handle to the TRANSPORT subsystem for application management.
void *(* GNUNET_TRANSPORT_notify_connect_cb)(struct GNUNET_TESTING_Interpreter *is, const struct GNUNET_PeerIdentity *peer)
static void start_peer_cleanup(void *cls)
The cleanup function of this cmd frees resources the cmd allocated.
static void hello_iter_cb(void *cb_cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Callback delivering the hello of this peer from peerstore.
static void * notify_connect(void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_MQ_Handle *mq)
Connect callback for the connection to the core service.
static void retrieve_hello(void *cls)
Function to start the retrieval task to retrieve the hello of this peer from the peerstore.
static int start_peer_traits(void *cls, const void **ret, const char *trait, unsigned int index)
This function prepares an array with traits.
struct GNUNET_TESTING_Command GNUNET_TRANSPORT_cmd_start_peer(const char *label, const char *system_label, uint32_t no, const char *node_ip, struct GNUNET_MQ_MessageHandler *handlers, const char *cfgname, GNUNET_TRANSPORT_notify_connect_cb notify_connect, unsigned int broadcast)
Create command.
#define LOG(kind,...)
Generic logging shortcut.
static void start_peer_run(void *cls, struct GNUNET_TESTING_Interpreter *is)
The run method of this cmd will start all services of a peer to test the transport service.
static void notify_disconnect(void *cls, const struct GNUNET_PeerIdentity *peer, void *handler_cls)
Disconnect callback for the connection to the core service.