diff options
Diffstat (limited to 'src/service/core/core_api_cmd_connecting_peers.c')
-rw-r--r-- | src/service/core/core_api_cmd_connecting_peers.c | 275 |
1 files changed, 275 insertions, 0 deletions
diff --git a/src/service/core/core_api_cmd_connecting_peers.c b/src/service/core/core_api_cmd_connecting_peers.c new file mode 100644 index 000000000..ce2184a3c --- /dev/null +++ b/src/service/core/core_api_cmd_connecting_peers.c | |||
@@ -0,0 +1,275 @@ | |||
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_testing_netjail_lib.h" | ||
30 | #include "gnunet_transport_application_service.h" | ||
31 | #include "gnunet_hello_lib.h" | ||
32 | #include "gnunet_transport_core_service.h" | ||
33 | |||
34 | /** | ||
35 | * Generic logging shortcut | ||
36 | */ | ||
37 | #define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__) | ||
38 | |||
39 | |||
40 | /** | ||
41 | * The run method of this cmd will connect to peers. | ||
42 | * | ||
43 | */ | ||
44 | static void | ||
45 | connect_peers_run (void *cls, | ||
46 | struct GNUNET_TESTING_Interpreter *is) | ||
47 | { | ||
48 | struct GNUNET_TESTING_ConnectPeersState *cps = cls; | ||
49 | const struct GNUNET_TESTING_StartPeerState *sps; | ||
50 | const struct GNUNET_TESTING_Command *system_cmd; | ||
51 | const struct GNUNET_TESTING_System *tl_system; | ||
52 | const struct GNUNET_TESTING_Command *peer1_cmd; | ||
53 | struct GNUNET_PeerIdentity *peer; | ||
54 | enum GNUNET_NetworkType nt = 0; | ||
55 | struct GNUNET_TESTING_NodeConnection *pos_connection; | ||
56 | struct GNUNET_TESTING_AddressPrefix *pos_prefix; | ||
57 | const enum GNUNET_GenericReturnValue *broadcast; | ||
58 | unsigned int con_num = 0; | ||
59 | uint32_t num; | ||
60 | char *addr; | ||
61 | char *addr_and_port; | ||
62 | char *emsg = NULL; | ||
63 | |||
64 | cps->is = is; | ||
65 | peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (is, | ||
66 | cps->start_peer_label); | ||
67 | GNUNET_TESTING_get_trait_broadcast (peer1_cmd, | ||
68 | &broadcast); | ||
69 | GNUNET_TESTING_get_trait_state (peer1_cmd, | ||
70 | &sps); | ||
71 | |||
72 | system_cmd = GNUNET_TESTING_interpreter_lookup_command (is, | ||
73 | cps->create_label); | ||
74 | GNUNET_TESTING_get_trait_test_system (system_cmd, | ||
75 | &tl_system); | ||
76 | |||
77 | cps->tl_system = tl_system; | ||
78 | |||
79 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
80 | "cps->num: %u \n", | ||
81 | cps->num); | ||
82 | |||
83 | |||
84 | cps->ah = GNUNET_TRANSPORT_application_init (sps->cfg); | ||
85 | if (NULL == cps->ah) | ||
86 | { | ||
87 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
88 | "Failed to initialize the TRANSPORT application suggestion client handle for peer `%s': `%s'\n", | ||
89 | sps->cfgname, | ||
90 | emsg); | ||
91 | GNUNET_free (emsg); | ||
92 | GNUNET_TESTING_interpreter_fail (is); | ||
93 | return; | ||
94 | } | ||
95 | |||
96 | cps->node_connections_head = GNUNET_TESTING_get_connections (cps->num, | ||
97 | cps->topology); | ||
98 | |||
99 | for (pos_connection = cps->node_connections_head; NULL != pos_connection; | ||
100 | pos_connection = pos_connection->next) | ||
101 | { | ||
102 | con_num++; | ||
103 | num = GNUNET_TESTING_calculate_num (pos_connection, cps->topology); | ||
104 | for (pos_prefix = pos_connection->address_prefixes_head; NULL != pos_prefix; | ||
105 | pos_prefix = | ||
106 | pos_prefix->next) | ||
107 | { | ||
108 | addr = GNUNET_TESTING_get_address (pos_connection, | ||
109 | pos_prefix->address_prefix); | ||
110 | if (NULL != addr) | ||
111 | { | ||
112 | char *natted_p = strstr (pos_prefix->address_prefix, "_"); | ||
113 | |||
114 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
115 | "0 validating peer number %s %s %s\n", | ||
116 | natted_p, | ||
117 | pos_prefix->address_prefix, | ||
118 | addr); | ||
119 | if (0 == GNUNET_memcmp (pos_prefix->address_prefix, "udp")) | ||
120 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
121 | "validating memcmp\n"); | ||
122 | if (GNUNET_YES == *broadcast) | ||
123 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
124 | "validating broadcast\n"); | ||
125 | if ((0 == GNUNET_memcmp (pos_prefix->address_prefix, "udp")) && | ||
126 | (GNUNET_YES == *broadcast) ) | ||
127 | GNUNET_asprintf (&addr_and_port, | ||
128 | "%s:2086", | ||
129 | addr); | ||
130 | else if (NULL == natted_p) | ||
131 | GNUNET_asprintf (&addr_and_port, | ||
132 | "%s:60002", | ||
133 | addr); | ||
134 | else if (NULL != natted_p) | ||
135 | { | ||
136 | char *prefix; | ||
137 | char *rest; | ||
138 | char *rest2; | ||
139 | char *address; | ||
140 | |||
141 | prefix = strtok (addr, "_"); | ||
142 | rest = strtok (NULL, "_"); | ||
143 | rest2 = strtok (rest, "-"); | ||
144 | address = strtok (NULL, "-"); | ||
145 | |||
146 | GNUNET_asprintf (&addr_and_port, | ||
147 | "%s-%s:0", | ||
148 | prefix, | ||
149 | address); | ||
150 | |||
151 | } | ||
152 | peer = GNUNET_TESTING_get_peer (num, tl_system); | ||
153 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
154 | "validating peer number %u with identity %s and address %s %u %s and handle %p\n", | ||
155 | num, | ||
156 | GNUNET_i2s (peer), | ||
157 | addr_and_port, | ||
158 | *broadcast, | ||
159 | pos_prefix->address_prefix, | ||
160 | cps->ah); | ||
161 | GNUNET_TRANSPORT_application_validate ((struct | ||
162 | GNUNET_TRANSPORT_ApplicationHandle | ||
163 | *) cps->ah, | ||
164 | peer, | ||
165 | nt, | ||
166 | addr_and_port); | ||
167 | GNUNET_free (peer); | ||
168 | GNUNET_free (addr); | ||
169 | GNUNET_free (addr_and_port); | ||
170 | } | ||
171 | } | ||
172 | } | ||
173 | cps->con_num = con_num; | ||
174 | } | ||
175 | |||
176 | |||
177 | /** | ||
178 | * The cleanup function of this cmd frees resources the cmd allocated. | ||
179 | * | ||
180 | */ | ||
181 | static void | ||
182 | connect_peers_cleanup (void *cls) | ||
183 | { | ||
184 | struct GNUNET_TESTING_ConnectPeersState *cps = cls; | ||
185 | |||
186 | GNUNET_free (cps->connected_peers_map); | ||
187 | GNUNET_free (cps); | ||
188 | } | ||
189 | |||
190 | |||
191 | /** | ||
192 | * This function prepares an array with traits. | ||
193 | * | ||
194 | */ | ||
195 | enum GNUNET_GenericReturnValue | ||
196 | connect_peers_traits (void *cls, | ||
197 | const void **ret, | ||
198 | const char *trait, | ||
199 | unsigned int index) | ||
200 | { | ||
201 | struct GNUNET_TESTING_ConnectPeersState *cps = cls; | ||
202 | struct GNUNET_TESTING_Trait traits[] = { | ||
203 | GNUNET_TESTING_make_trait_connect_peer_state ((const void *) cps), | ||
204 | GNUNET_TESTING_trait_end () | ||
205 | }; | ||
206 | return GNUNET_TESTING_get_trait (traits, | ||
207 | ret, | ||
208 | trait, | ||
209 | index); | ||
210 | } | ||
211 | |||
212 | |||
213 | struct GNUNET_TESTING_Command | ||
214 | GNUNET_CORE_cmd_connect_peers (const char *label, | ||
215 | const char *start_peer_label, | ||
216 | const char *create_label, | ||
217 | uint32_t num, | ||
218 | struct GNUNET_TESTING_NetjailTopology * | ||
219 | topology, | ||
220 | unsigned int additional_connects, | ||
221 | unsigned int wait_for_connect, | ||
222 | struct GNUNET_MQ_MessageHandler *handlers) | ||
223 | { | ||
224 | struct GNUNET_TESTING_ConnectPeersState *cps; | ||
225 | unsigned int node_additional_connects; | ||
226 | struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map = | ||
227 | GNUNET_CONTAINER_multishortmap_create (1,GNUNET_NO); | ||
228 | unsigned int i; | ||
229 | |||
230 | node_additional_connects = GNUNET_TESTING_get_additional_connects (num, | ||
231 | topology); | ||
232 | |||
233 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
234 | "global: %u and local: %u additional_connects\n", | ||
235 | additional_connects, | ||
236 | node_additional_connects); | ||
237 | |||
238 | if (0 != node_additional_connects) | ||
239 | additional_connects = node_additional_connects; | ||
240 | |||
241 | cps = GNUNET_new (struct GNUNET_TESTING_ConnectPeersState); | ||
242 | cps->start_peer_label = start_peer_label; | ||
243 | cps->num = num; | ||
244 | cps->create_label = create_label; | ||
245 | cps->topology = topology; | ||
246 | cps->additional_connects = additional_connects; | ||
247 | cps->wait_for_connect = wait_for_connect; | ||
248 | cps->connected_peers_map = connected_peers_map; | ||
249 | |||
250 | if (NULL != handlers) | ||
251 | { | ||
252 | for (i = 0; NULL != handlers[i].cb; i++) | ||
253 | ; | ||
254 | cps->handlers = GNUNET_new_array (i + 1, | ||
255 | struct GNUNET_MQ_MessageHandler); | ||
256 | GNUNET_memcpy (cps->handlers, | ||
257 | handlers, | ||
258 | i * sizeof(struct GNUNET_MQ_MessageHandler)); | ||
259 | } | ||
260 | |||
261 | if (GNUNET_YES == wait_for_connect) | ||
262 | return GNUNET_TESTING_command_new (cps, | ||
263 | label, | ||
264 | &connect_peers_run, | ||
265 | &connect_peers_cleanup, | ||
266 | &connect_peers_traits, | ||
267 | &cps->ac); | ||
268 | else | ||
269 | return GNUNET_TESTING_command_new (cps, | ||
270 | label, | ||
271 | &connect_peers_run, | ||
272 | &connect_peers_cleanup, | ||
273 | &connect_peers_traits, | ||
274 | NULL); | ||
275 | } | ||