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