GNUnet  0.11.x
transport_api_cmd_connecting_peers.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 
44 {
45  // Label of the cmd which started the test system.
46  const char *create_label;
47 
52  uint32_t num;
53 
58  const char *start_peer_label;
59 
65 };
66 
67 
72 static void
73 connect_peers_run (void *cls,
74  const struct GNUNET_TESTING_Command *cmd,
76 {
77  struct ConnectPeersState *cps = cls;
78  const struct GNUNET_TESTING_Command *system_cmd;
79  struct GNUNET_TESTING_System *tl_system;
80  struct GNUNET_CRYPTO_EddsaPrivateKey *priv_key = GNUNET_new (struct
82  struct GNUNET_CRYPTO_EddsaPublicKey *pub_key = GNUNET_new (struct
84  ;
85  const struct GNUNET_TESTING_Command *peer1_cmd;
86  // const struct GNUNET_TESTING_Command *peer2_cmd;
89  char *addr;
90  // struct GNUNET_TIME_Absolute t;
91  char *hello;
92  // size_t *hello_size;
93  enum GNUNET_NetworkType nt = 0;
94  char *peer_id;
95  struct GNUNET_PeerIdentity *id;
96  struct GNUNET_PeerIdentity *other = GNUNET_new (struct GNUNET_PeerIdentity);
97  uint32_t num;
98 
101  &ah);
102 
104  &hello);
105 
107  &id);
108 
111  &tl_system);
112 
113  if (2 == cps->num)
114  num = 1;
115  else
116  num = 2;
117 
118 
119 
120 
121  // if (strstr (hello, "60002") != NULL)
122  if (2 == num)
123  {
124  addr = "tcp-192.168.15.2:60002";
125  peer_id = "F2F3X9G1YNCTXKK7A4J6M4ZM4BBSKC9DEXZVHCWQ475M0C7PNWCG";
126  }
127  else
128  {
129  addr = "tcp-192.168.15.1:60002";
130  peer_id = "4TTC9WBSVP9RJT6DVEZ7E0TDW7TQXC11NR1EMR2F8ARS87WZ2730";
131  }
132 
133  priv_key = GNUNET_TESTING_hostkey_get (tl_system,
134  num,
135  other);
136 
138  pub_key);
139 
141  strlen (peer_id),
142  &peer->public_key);
143 
144  peer->public_key = *pub_key;
145 
147  "\nnum: %u\n peer_id: %s\n pub_key %s\n",
148  num,
149  peer_id,
151 
152  cps->id = peer;
153 
154  // TODO This does not work, because the other peer is running in another local loop. We need to message between different local loops. For now we will create the hello manually with the known information about the other local peers.
155  // ---------------------------------------------
156  /*peer2_cmd = GNUNET_TESTING_interpreter_lookup_command (cps->peer2_label);
157  GNUNET_TRANSPORT_get_trait_peer_id (peer2_cmd,
158  &id);
159  GNUNET_TRANSPORT_get_trait_hello (peer2_cmd,
160  &hello);
161  GNUNET_TRANSPORT_get_trait_hello_size (peer2_cmd,
162  &hello_size);
163 
164  addr = GNUNET_HELLO_extract_address (hello,
165  *hello_size,
166  id,
167  &nt,
168  &t);*/
169 
170  // ----------------------------------------------
171 
172 
174  peer,
175  nt,
176  addr);
177 }
178 
179 
184 static int
187  void *cont_cls)
188 {
189  struct ConnectPeersState *cps = cls;
190  const struct GNUNET_TESTING_Command *peer1_cmd;
191  struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map;
192  unsigned int ret;
194  struct GNUNET_HashCode hc;
195  int node_number;
196 
199  &connected_peers_map);
200 
201  node_number = 1;
202  GNUNET_CRYPTO_hash (&node_number, sizeof(node_number), &hc);
203 
204  // TODO we need to store with a key identifying the netns node in the future. For now we have only one connecting node.
205  memcpy (key,
206  &hc,
207  sizeof (*key));
208  ret = GNUNET_CONTAINER_multishortmap_contains (connected_peers_map,
209  key);
210 
211  if (GNUNET_YES == ret)
212  {
213  cont (cont_cls);
214  }
215 
216  GNUNET_free (key);
217  return ret;
218 }
219 
220 
225 static int
227  const void **ret,
228  const char *trait,
229  unsigned int index)
230 {
231  return GNUNET_OK;
232 }
233 
234 
239 static void
241  const struct GNUNET_TESTING_Command *cmd)
242 {
243  struct ConnectPeersState *cps = cls;
244 
245  GNUNET_free (cps->id);
246  GNUNET_free (cps);
247 }
248 
249 
259  const char *start_peer_label,
260  const char *create_label,
261  uint32_t num)
262 {
263  struct ConnectPeersState *cps;
264 
265  cps = GNUNET_new (struct ConnectPeersState);
267  cps->num = num;
268  cps->create_label = create_label;
269 
270 
271  struct GNUNET_TESTING_Command cmd = {
272  .cls = cps,
273  .label = label,
274  .run = &connect_peers_run,
275  .finish = &connect_peers_finish,
276  .cleanup = &connect_peers_cleanup,
277  .traits = &connect_peers_traits
278  };
279 
280  return cmd;
281 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static char * peer_id
Option –peer.
Definition: gnunet-cadet.c:42
static struct GNUNET_IDENTITY_Handle * id
Handle to identity service.
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
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_public_key_from_string(const char *enc, size_t enclen, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Convert a string representing a public key to a public key.
Definition: crypto_ecc.c:357
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
#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_connected_peers_map(const struct GNUNET_TESTING_Command *cmd, struct GNUNET_CONTAINER_MultiShortmap **connected_peers_map)
Function to get the trait with the map of connected peers.
int GNUNET_TRANSPORT_get_trait_hello(const struct GNUNET_TESTING_Command *cmd, char **hello)
Function to get the trait with the hello.
int GNUNET_TRANSPORT_get_trait_peer_id(const struct GNUNET_TESTING_Command *cmd, struct GNUNET_PeerIdentity **id)
Function to get the trait with the peer id.
int GNUNET_TRANSPORT_get_trait_application_handle(const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TRANSPORT_ApplicationHandle **ah)
Function to get the trait with the transport application handle.
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...
struct GNUNET_TESTING_Command GNUNET_TRANSPORT_cmd_connect_peers(const char *label, const char *start_peer_label, const char *create_label, uint32_t num)
Create command.
#define LOG(kind,...)
Generic logging shortcut.
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.