aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport_api_cmd_connecting_peers_v2.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/transport_api_cmd_connecting_peers_v2.c')
-rw-r--r--src/transport/transport_api_cmd_connecting_peers_v2.c242
1 files changed, 242 insertions, 0 deletions
diff --git a/src/transport/transport_api_cmd_connecting_peers_v2.c b/src/transport/transport_api_cmd_connecting_peers_v2.c
new file mode 100644
index 000000000..0d286b714
--- /dev/null
+++ b/src/transport/transport_api_cmd_connecting_peers_v2.c
@@ -0,0 +1,242 @@
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_transport_application_service.h"
30#include "gnunet_hello_lib.h"
31#include "gnunet_transport_service.h"
32#include "transport-testing-cmds.h"
33
34/**
35 * Generic logging shortcut
36 */
37#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
38
39#define CONNECT_ADDRESS_TEMPLATE "tcp-192.168.15.%u:60002"
40
41/**
42 * Struct to store information needed in callbacks.
43 *
44 */
45struct ConnectPeersState
46{
47 // Label of the cmd which started the test system.
48 const char *create_label;
49
50 /**
51 * Number globally identifying the node.
52 *
53 */
54 uint32_t num;
55
56 /**
57 * Label of the cmd to start a peer.
58 *
59 */
60 const char *start_peer_label;
61
62 /**
63 * The peer identity of this peer.
64 *
65 */
66 struct GNUNET_PeerIdentity *id;
67};
68
69
70/**
71 * The run method of this cmd will connect to peers.
72 *
73 */
74static void
75connect_peers_run (void *cls,
76 const struct GNUNET_TESTING_Command *cmd,
77 struct GNUNET_TESTING_Interpreter *is)
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
83 GNUNET_CRYPTO_EddsaPrivateKey);
84 struct GNUNET_CRYPTO_EddsaPublicKey *pub_key = GNUNET_new (struct
85 GNUNET_CRYPTO_EddsaPublicKey);
86 const struct GNUNET_TESTING_Command *peer1_cmd;
87 struct GNUNET_TRANSPORT_ApplicationHandle *ah;
88 struct GNUNET_PeerIdentity *peer = GNUNET_new (struct GNUNET_PeerIdentity);
89 char *addr;
90 enum GNUNET_NetworkType nt = 0;
91 struct GNUNET_PeerIdentity *other = GNUNET_new (struct GNUNET_PeerIdentity);
92 uint32_t num;
93
94 peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (cps->start_peer_label);
95 GNUNET_TRANSPORT_get_trait_application_handle_v2 (peer1_cmd,
96 &ah);
97
98 system_cmd = GNUNET_TESTING_interpreter_lookup_command (cps->create_label);
99 GNUNET_TESTING_get_trait_test_system (system_cmd,
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,
114 CONNECT_ADDRESS_TEMPLATE,
115 num);
116
117 priv_key = GNUNET_TESTING_hostkey_get (tl_system,
118 num,
119 other);
120
121 GNUNET_CRYPTO_eddsa_key_get_public (priv_key,
122 pub_key);
123
124
125 peer->public_key = *pub_key;
126
127 LOG (GNUNET_ERROR_TYPE_ERROR,
128 "num: %u pub_key %s\n",
129 num,
130 GNUNET_CRYPTO_eddsa_public_key_to_string (pub_key));
131
132 cps->id = peer;
133
134 GNUNET_TRANSPORT_application_validate (ah,
135 peer,
136 nt,
137 addr);
138}
139
140
141/**
142 * The finish function of this cmd will check if the peer we are trying to connect to is in the connected peers map of the start peer cmd for this peer.
143 *
144 */
145static int
146connect_peers_finish (void *cls,
147 GNUNET_SCHEDULER_TaskCallback cont,
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;
154 struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode);
155 struct GNUNET_HashCode hc;
156 int node_number;
157
158 peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (cps->start_peer_label);
159 GNUNET_TRANSPORT_get_trait_connected_peers_map_v2 (peer1_cmd,
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
182/**
183 * Trait function of this cmd does nothing.
184 *
185 */
186static int
187connect_peers_traits (void *cls,
188 const void **ret,
189 const char *trait,
190 unsigned int index)
191{
192 return GNUNET_OK;
193}
194
195
196/**
197 * The cleanup function of this cmd frees resources the cmd allocated.
198 *
199 */
200static void
201connect_peers_cleanup (void *cls,
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
211/**
212 * Create command.
213 *
214 * @param label name for command.
215 * @param start_peer_label Label of the cmd to start a peer.
216 * @return command.
217 */
218struct GNUNET_TESTING_Command
219GNUNET_TRANSPORT_cmd_connect_peers_v2 (const char *label,
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);
227 cps->start_peer_label = start_peer_label;
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}