GNUnet  0.11.x
transport_api_cmd_connecting_peers_v2.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"
30 #include "gnunet_hello_lib.h"
32 #include "transport-testing-cmds.h"
33 
37 #define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
38 
39 #define CONNECT_ADDRESS_TEMPLATE "tcp-192.168.15.%u:60002"
40 
45 struct ConnectPeersState
46 {
47  // Label of the cmd which started the test system.
48  const char *create_label;
49 
54  uint32_t num;
55 
60  const char *start_peer_label;
61 
66  struct GNUNET_PeerIdentity *id;
67 };
68 
69 
74 static void
75 connect_peers_run (void *cls,
76  const struct GNUNET_TESTING_Command *cmd,
78 {
79  struct ConnectPeersState *cps = cls;
80  const struct GNUNET_TESTING_Command *system_cmd;
81  struct GNUNET_TESTING_System *tl_system;
82  struct GNUNET_CRYPTO_EddsaPrivateKey *priv_key = GNUNET_new (struct
84  struct GNUNET_CRYPTO_EddsaPublicKey *pub_key = GNUNET_new (struct
86  const struct GNUNET_TESTING_Command *peer1_cmd;
89  char *addr;
90  enum GNUNET_NetworkType nt = 0;
91  struct GNUNET_PeerIdentity *other = GNUNET_new (struct GNUNET_PeerIdentity);
92  uint32_t num;
93 
96  &ah);
97 
100  &tl_system);
101 
102  if (1 == cps->num)
103  {
104  num = 2;
105  // addr = "tcp-192.168.15.2:60002";
106  }
107  else
108  {
109  num = 1;
110  // addr = "tcp-192.168.15.1:60002";
111  }
112 
113  GNUNET_asprintf (&addr,
115  num);
116 
117  priv_key = GNUNET_TESTING_hostkey_get (tl_system,
118  num,
119  other);
120 
122  pub_key);
123 
124 
125  peer->public_key = *pub_key;
126 
128  "num: %u pub_key %s\n",
129  num,
131 
132  cps->id = peer;
133 
135  peer,
136  nt,
137  addr);
138 }
139 
140 
145 static int
148  void *cont_cls)
149 {
150  struct ConnectPeersState *cps = cls;
151  const struct GNUNET_TESTING_Command *peer1_cmd;
152  struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map;
153  unsigned int ret;
155  struct GNUNET_HashCode hc;
156  int node_number;
157 
160  &connected_peers_map);
161 
162  node_number = 1;
163  GNUNET_CRYPTO_hash (&node_number, sizeof(node_number), &hc);
164 
165  // TODO we need to store with a key identifying the netns node in the future. For now we have only one connecting node.
166  memcpy (key,
167  &hc,
168  sizeof (*key));
169  ret = GNUNET_CONTAINER_multishortmap_contains (connected_peers_map,
170  key);
171 
172  if (GNUNET_YES == ret)
173  {
174  cont (cont_cls);
175  }
176 
177  GNUNET_free (key);
178  return ret;
179 }
180 
181 
186 static int
188  const void **ret,
189  const char *trait,
190  unsigned int index)
191 {
192  return GNUNET_OK;
193 }
194 
195 
200 static void
202  const struct GNUNET_TESTING_Command *cmd)
203 {
204  struct ConnectPeersState *cps = cls;
205 
206  GNUNET_free (cps->id);
207  GNUNET_free (cps);
208 }
209 
210 
220  const char *start_peer_label,
221  const char *create_label,
222  uint32_t num)
223 {
224  struct ConnectPeersState *cps;
225 
226  cps = GNUNET_new (struct ConnectPeersState);
228  cps->num = num;
229  cps->create_label = create_label;
230 
231 
232  struct GNUNET_TESTING_Command cmd = {
233  .cls = cps,
234  .label = label,
235  .run = &connect_peers_run,
236  .finish = &connect_peers_finish,
237  .cleanup = &connect_peers_cleanup,
238  .traits = &connect_peers_traits
239  };
240 
241  return cmd;
242 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_NAT_AUTO_AutoHandle * ah
Handle to ongoing autoconfiguration.
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_YES
Definition: gnunet_common.h:97
char * GNUNET_CRYPTO_eddsa_public_key_to_string(const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Convert a public key to a string.
Definition: crypto_ecc.c:251
int GNUNET_TESTING_get_trait_test_system(const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_System **test_system)
Function to get the trait with struct GNUNET_TESTING_System.
const struct GNUNET_TESTING_Command * GNUNET_TESTING_interpreter_lookup_command(const char *label)
Lookup command by label.
Bandwidth allocation API for applications to interact with.
void GNUNET_TRANSPORT_application_validate(struct GNUNET_TRANSPORT_ApplicationHandle *ch, const struct GNUNET_PeerIdentity *peer, enum GNUNET_NetworkType nt, const char *addr)
An application (or a communicator) has received a HELLO (or other address data of another peer) and w...
void GNUNET_CRYPTO_eddsa_key_get_public(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:197
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
int GNUNET_CONTAINER_multishortmap_contains(const struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key)
Check if the map contains any value under the given key (including values that are NULL).
@ GNUNET_ERROR_TYPE_ERROR
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_free(ptr)
Wrapper around free.
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
Definition: gnunet_nt_lib.h:36
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_TESTING_hostkey_get(const struct GNUNET_TESTING_System *system, uint32_t key_number, struct GNUNET_PeerIdentity *id)
Testing includes a number of pre-created hostkeys for faster peer startup.
Definition: testing.c:704
Struct to store information needed in callbacks.
const char * start_peer_label
Label of the cmd to start a peer.
uint32_t num
Number globally identifying the node.
struct GNUNET_PeerIdentity * id
The peer identity of this peer.
Internal representation of the hash map.
Private ECC key encoded for transmission.
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...
A 512-bit hashcode.
The identity of the host (wraps the signing key of the peer).
A 256-bit hashcode.
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.
Definition: testing.h:35
Handle for a system on which GNUnet peers are executed; a system is used for reserving unique paths a...
Definition: testing.c:93
Handle to the TRANSPORT subsystem for application management.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
struct GNUNET_TESTING_Interpreter * is
int GNUNET_TRANSPORT_get_trait_application_handle_v2(const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TRANSPORT_ApplicationHandle **ah)
Function to get the trait with the transport application handle.
int GNUNET_TRANSPORT_get_trait_connected_peers_map_v2(const struct GNUNET_TESTING_Command *cmd, struct GNUNET_CONTAINER_MultiShortmap **connected_peers_map)
Function to get the trait with the map of connected peers.
static void connect_peers_cleanup(void *cls, const struct GNUNET_TESTING_Command *cmd)
The cleanup function of this cmd frees resources the cmd allocated.
static int connect_peers_traits(void *cls, const void **ret, const char *trait, unsigned int index)
Trait function of this cmd does nothing.
static int connect_peers_finish(void *cls, GNUNET_SCHEDULER_TaskCallback cont, void *cont_cls)
The finish function of this cmd will check if the peer we are trying to connect to is in the connecte...
#define LOG(kind,...)
Generic logging shortcut.
#define CONNECT_ADDRESS_TEMPLATE
static void connect_peers_run(void *cls, const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_Interpreter *is)
The run method of this cmd will connect to peers.
struct GNUNET_TESTING_Command GNUNET_TRANSPORT_cmd_connect_peers_v2(const char *label, const char *start_peer_label, const char *create_label, uint32_t num)
Create command.