GNUnet  0.17.6
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"
28 #include "gnunet_testing_ng_lib.h"
33 #include "transport-testing-cmds.h"
34 
38 #define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
39 
40 
41 static void
42 retrieve_hello (void *cls);
43 
44 
49 static void
50 hello_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;
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  sps->pic = NULL;
70 }
71 
72 
78 static void
79 retrieve_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,
88  sps);
89 
90 }
91 
92 
97 static void
98 notify_disconnect (void *cls,
99  const struct GNUNET_PeerIdentity *peer,
100  void *handler_cls)
101 {
102  struct StartPeerState *sps = cls;
103 
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 
117 static void *
118 notify_connect (void *cls,
119  const struct GNUNET_PeerIdentity *peer,
120  struct GNUNET_MQ_Handle *mq)
121 {
122  struct StartPeerState *sps = cls;
124  struct GNUNET_HashCode hc;
125  struct GNUNET_CRYPTO_EddsaPublicKey public_key = peer->public_key;
126 
127  void *ret = (struct GNUNET_PeerIdentity *) peer;
128 
130  "This Peer %s \n",
131  GNUNET_i2s (&sps->id));
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));
144  key,
145  mq,
147 
148  GNUNET_free (key);
149 
150  sps->notify_connect (sps->ac.is,
151  peer);
152 
153  return ret;
154 }
155 
156 
161 static void
162 start_peer_run (void *cls,
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 
178  {
180  "File not found: `%s'\n",
181  sps->cfgname);
183  return;
184  }
185 
186 
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 
215  "node_ip %s\n",
216  bindto);
217 
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 
242  sps->system_label);
244  &tl_system);
245 
246  sps->tl_system = tl_system;
247 
249  "Creating testing library with key number %u\n",
250  sps->no);
251 
252  if (GNUNET_SYSERR ==
254  GNUNET_TESTING_System *) tl_system,
255  sps->cfg))
256  {
258  "Testing library failed to create unique configuration based on `%s'\n",
259  sps->cfgname);
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  {
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);
281  return;
282  }
283 
285  {
287  "Testing library failed to create unique configuration based on `%s'\n",
288  sps->cfgname);
289  GNUNET_free (emsg);
291  return;
292  }
293 
294  memset (&dummy,
295  '\0',
296  sizeof(dummy));
297 
299  &sps->id);
300 
301  if (0 == memcmp (&dummy,
302  &sps->id,
303  sizeof(struct GNUNET_PeerIdentity)))
304  {
306  "Testing library failed to obtain peer identity for peer %u\n",
307  sps->no);
308  GNUNET_free (emsg);
310  return;
311  }
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,
323  if (NULL == sps->th)
324  {
326  "Failed to connect to transport service for peer `%s': `%s'\n",
327  sps->cfgname,
328  emsg);
329  GNUNET_free (emsg);
331  return;
332  }
333 
334  sps->ph = GNUNET_PEERSTORE_connect (sps->cfg);
335  if (NULL == sps->th)
336  {
338  "Failed to connect to peerstore service for peer `%s': `%s'\n",
339  sps->cfgname,
340  emsg);
341  GNUNET_free (emsg);
343  return;
344  }
345 
347  if (NULL == sps->ah)
348  {
350  "Failed to initialize the TRANSPORT application suggestion client handle for peer `%s': `%s'\n",
351  sps->cfgname,
352  emsg);
353  GNUNET_free (emsg);
355  return;
356  }
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 
371 static void
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  {
384  sps->cfg = NULL;
385  }
386  GNUNET_free (sps->hello);
388  GNUNET_free (sps);
389 }
390 
391 
396 static int
397 start_peer_traits (void *cls,
398  const void **ret,
399  const char *trait,
400  unsigned int index)
401 {
402  struct StartPeerState *sps = cls;
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[] = {
413  GNUNET_TRANSPORT_make_trait_peer_id ((const void *) id),
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),
422  };
423 
424  return GNUNET_TESTING_get_trait (traits,
425  ret,
426  trait,
427  index);
428 }
429 
430 
447  const char *system_label,
448  uint32_t no,
449  char *node_ip,
451  const char *cfgname,
454  unsigned int broadcast)
455 {
456  struct StartPeerState *sps;
457  struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map =
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;
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 }
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
static struct GNUNET_NT_InterfaceScanner * is
Network scanner to determine network types.
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.
enum GNUNET_GenericReturnValue GNUNET_TESTING_get_trait_test_system(const struct GNUNET_TESTING_Command *cmd, const struct GNUNET_TESTING_System **ret)
void GNUNET_TESTING_async_finish(struct GNUNET_TESTING_AsyncContext *ac)
The asynchronous command of ac has finished.
struct GNUNET_TESTING_Trait GNUNET_TESTING_trait_end(void)
"end" trait.
const struct GNUNET_TESTING_Command * GNUNET_TESTING_interpreter_lookup_command(struct GNUNET_TESTING_Interpreter *is, const char *label)
Lookup command by label.
enum GNUNET_GenericReturnValue GNUNET_TESTING_get_trait(const struct GNUNET_TESTING_Trait *traits, const void **ret, const char *trait, unsigned int index)
Extract a trait.
void GNUNET_TESTING_interpreter_fail(struct GNUNET_TESTING_Interpreter *is)
Current command failed, clean up and fail the test case.
Bandwidth allocation API for applications to interact with.
API of the transport service towards the CORE service (TNG version)
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.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy 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
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.
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).
@ 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
Definition: gnunet_common.h:99
@ GNUNET_YES
@ GNUNET_NO
Definition: gnunet_common.h:98
@ GNUNET_SYSERR
Definition: gnunet_common.h:97
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_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_Handle * GNUNET_PEERSTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the PEERSTORE service.
void GNUNET_PEERSTORE_iterate_cancel(struct GNUNET_PEERSTORE_IterateContext *ic)
Cancel an iterate request Please do not call after the iterate request is done.
#define GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY
Key used for storing HELLOs in the peerstore.
struct GNUNET_PEERSTORE_IterateContext * GNUNET_PEERSTORE_iterate(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 records matching supplied key information.
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:1281
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:1170
int GNUNET_TESTING_peer_start(struct GNUNET_TESTING_Peer *peer)
Start the peer.
Definition: testing.c:1363
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:1149
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:1341
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.
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:86
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.
const char * label
Label for the command.
void * cls
Closure for all commands with command-specific context information.
Global state of the interpreter, used by a command to access information about other commands.
Handle for a system on which GNUnet peers are executed; a system is used for reserving unique paths a...
Definition: testing.c:103
unsigned int index
Index number associated with the trait.
Handle to the TRANSPORT subsystem for application management.
unsigned int no
An unique number to identify the peer.
struct GNUNET_PeerIdentity id
Peer identity.
char * node_ip
The ip of a node.
enum GNUNET_GenericReturnValue broadcast
Flag indicating, if udp broadcast should be switched on.
struct GNUNET_MQ_MessageHandler * handlers
Receive callback.
struct GNUNET_CONFIGURATION_Handle * cfg
Peer's configuration.
struct GNUNET_PEERSTORE_Handle * ph
Peer's PEERSTORE Handle.
struct GNUNET_SCHEDULER_Task * rh_task
Hello get task.
struct GNUNET_PEERSTORE_IterateContext * pic
Peer's transport get hello handle to retrieve peer's HELLO message.
struct GNUNET_CONTAINER_MultiShortmap * connected_peers_map
struct GNUNET_TESTING_Peer * peer
GNUNET_TRANSPORT_notify_connect_cb notify_connect
size_t hello_size
Hello size.
struct GNUNET_TRANSPORT_CoreHandle * th
Peer's transport service handle.
struct GNUNET_TRANSPORT_ApplicationHandle * ah
Application handle.
const struct GNUNET_TESTING_System * tl_system
struct GNUNET_TESTING_AsyncContext ac
Context for our asynchronous completion.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
struct GNUNET_TESTING_Trait GNUNET_TRANSPORT_make_trait_application_handle(const struct GNUNET_TRANSPORT_ApplicationHandle *value)
struct GNUNET_TESTING_Trait GNUNET_TRANSPORT_make_trait_hello(const char *value)
void *(* GNUNET_TRANSPORT_notify_connect_cb)(struct GNUNET_TESTING_Interpreter *is, const struct GNUNET_PeerIdentity *peer)
struct GNUNET_TESTING_Trait GNUNET_TRANSPORT_make_trait_peer_id(const struct GNUNET_PeerIdentity *value)
struct GNUNET_TESTING_Trait GNUNET_TRANSPORT_make_trait_hello_size(const size_t *value)
struct GNUNET_TESTING_Trait GNUNET_TRANSPORT_make_trait_broadcast(const enum GNUNET_GenericReturnValue *value)
struct GNUNET_TESTING_Trait GNUNET_TRANSPORT_make_trait_state(const struct StartPeerState *value)
struct GNUNET_TESTING_Trait GNUNET_TRANSPORT_make_trait_connected_peers_map(const struct GNUNET_CONTAINER_MultiShortmap *value)
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.
struct GNUNET_TESTING_Command GNUNET_TRANSPORT_cmd_start_peer(const char *label, const char *system_label, uint32_t no, char *node_ip, struct GNUNET_MQ_MessageHandler *handlers, const char *cfgname, GNUNET_TRANSPORT_notify_connect_cb notify_connect, unsigned int broadcast)
Create command.
static void retrieve_hello(void *cls)
Function to start the retrieval task to retrieve the hello of this peer from the 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 int start_peer_traits(void *cls, const void **ret, const char *trait, unsigned int index)
This function prepares an array with traits.
#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.