diff options
Diffstat (limited to 'src/transport/transport-testing2.c')
-rw-r--r-- | src/transport/transport-testing2.c | 961 |
1 files changed, 0 insertions, 961 deletions
diff --git a/src/transport/transport-testing2.c b/src/transport/transport-testing2.c deleted file mode 100644 index 6d41ec098..000000000 --- a/src/transport/transport-testing2.c +++ /dev/null | |||
@@ -1,961 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2006, 2009, 2015, 2016 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 | * @file transport-testing.c | ||
22 | * @brief testing lib for transport service | ||
23 | * @author Matthias Wachs | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "transport-testing2.h" | ||
27 | |||
28 | |||
29 | #define LOG(kind, ...) GNUNET_log_from (kind, "transport-testing", __VA_ARGS__) | ||
30 | |||
31 | |||
32 | static struct GNUNET_TRANSPORT_TESTING_PeerContext * | ||
33 | find_peer_context (struct GNUNET_TRANSPORT_TESTING_Handle *tth, | ||
34 | const struct GNUNET_PeerIdentity *peer) | ||
35 | { | ||
36 | struct GNUNET_TRANSPORT_TESTING_PeerContext *t; | ||
37 | |||
38 | for (t = tth->p_head; NULL != t; t = t->next) | ||
39 | if (0 == memcmp (&t->id, | ||
40 | peer, | ||
41 | sizeof(struct GNUNET_PeerIdentity))) | ||
42 | return t; | ||
43 | return NULL; | ||
44 | } | ||
45 | |||
46 | |||
47 | /** | ||
48 | * Find any connecting context matching the given pair of peers. | ||
49 | * | ||
50 | * @param p1 first peer | ||
51 | * @param p2 second peer | ||
52 | * @param cb function to call | ||
53 | * @param cb_cls closure for @a cb | ||
54 | */ | ||
55 | void | ||
56 | GNUNET_TRANSPORT_TESTING_find_connecting_context (struct | ||
57 | GNUNET_TRANSPORT_TESTING_PeerContext | ||
58 | *p1, | ||
59 | struct | ||
60 | GNUNET_TRANSPORT_TESTING_PeerContext | ||
61 | *p2, | ||
62 | GNUNET_TRANSPORT_TESTING_ConnectContextCallback | ||
63 | cb, | ||
64 | void *cb_cls) | ||
65 | { | ||
66 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth; | ||
67 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
68 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | ||
69 | |||
70 | for (cc = tth->cc_head; NULL != cc; cc = ccn) | ||
71 | { | ||
72 | ccn = cc->next; | ||
73 | if ((cc->p1 == p1) && | ||
74 | (cc->p2 == p2)) | ||
75 | cb (cb_cls, | ||
76 | cc); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | |||
81 | static void | ||
82 | set_p1c (void *cls, | ||
83 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
84 | { | ||
85 | int *found = cls; | ||
86 | |||
87 | if (NULL != found) | ||
88 | *found = GNUNET_YES; | ||
89 | cx->p1_c = GNUNET_YES; | ||
90 | } | ||
91 | |||
92 | |||
93 | static void | ||
94 | set_mq (void *cls, | ||
95 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
96 | { | ||
97 | struct GNUNET_MQ_Handle *mq = cls; | ||
98 | |||
99 | cx->mq = mq; | ||
100 | } | ||
101 | |||
102 | |||
103 | static void | ||
104 | set_p2c (void *cls, | ||
105 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
106 | { | ||
107 | int *found = cls; | ||
108 | |||
109 | if (NULL != found) | ||
110 | *found = GNUNET_YES; | ||
111 | cx->p2_c = GNUNET_YES; | ||
112 | } | ||
113 | |||
114 | |||
115 | static void | ||
116 | clear_p1c (void *cls, | ||
117 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
118 | { | ||
119 | int *found = cls; | ||
120 | |||
121 | if (NULL != found) | ||
122 | *found = GNUNET_YES; | ||
123 | cx->p1_c = GNUNET_NO; | ||
124 | } | ||
125 | |||
126 | |||
127 | static void | ||
128 | clear_p2c (void *cls, | ||
129 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
130 | { | ||
131 | int *found = cls; | ||
132 | |||
133 | if (NULL != found) | ||
134 | *found = GNUNET_YES; | ||
135 | cx->p2_c = GNUNET_NO; | ||
136 | } | ||
137 | |||
138 | |||
139 | static void * | ||
140 | notify_connect (void *cls, | ||
141 | const struct GNUNET_PeerIdentity *peer, | ||
142 | struct GNUNET_MQ_Handle *mq) | ||
143 | { | ||
144 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; | ||
145 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth; | ||
146 | char *p2_s; | ||
147 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2; | ||
148 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
149 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | ||
150 | int found; | ||
151 | void *ret; | ||
152 | |||
153 | p2 = find_peer_context (p->tth, | ||
154 | peer); | ||
155 | if (NULL != p->nc) | ||
156 | ret = p->nc (p->cb_cls, | ||
157 | peer, | ||
158 | mq); | ||
159 | else | ||
160 | ret = NULL; | ||
161 | |||
162 | if (NULL != p2) | ||
163 | GNUNET_asprintf (&p2_s, | ||
164 | "%u (`%s')", | ||
165 | p2->no, | ||
166 | GNUNET_i2s (&p2->id)); | ||
167 | else | ||
168 | GNUNET_asprintf (&p2_s, | ||
169 | "`%s'", | ||
170 | GNUNET_i2s (peer)); | ||
171 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
172 | "Peers %s connected to peer %u (`%s')\n", | ||
173 | p2_s, | ||
174 | p->no, | ||
175 | GNUNET_i2s (&p->id)); | ||
176 | GNUNET_free (p2_s); | ||
177 | /* update flags in connecting contexts */ | ||
178 | found = GNUNET_NO; | ||
179 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p, | ||
180 | p2, | ||
181 | &set_p1c, | ||
182 | &found); | ||
183 | if (GNUNET_NO == found) | ||
184 | { | ||
185 | cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest); | ||
186 | cc->p1 = p; | ||
187 | cc->p2 = p2; | ||
188 | cc->p1_c = GNUNET_YES; | ||
189 | GNUNET_CONTAINER_DLL_insert (tth->cc_head, | ||
190 | tth->cc_tail, | ||
191 | cc); | ||
192 | } | ||
193 | found = GNUNET_NO; | ||
194 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p2, | ||
195 | p, | ||
196 | &set_p2c, | ||
197 | &found); | ||
198 | if (GNUNET_NO == found) | ||
199 | { | ||
200 | cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest); | ||
201 | cc->p1 = p2; | ||
202 | cc->p2 = p; | ||
203 | cc->p1_c = GNUNET_YES; | ||
204 | GNUNET_CONTAINER_DLL_insert (tth->cc_head, | ||
205 | tth->cc_tail, | ||
206 | cc); | ||
207 | } | ||
208 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p, | ||
209 | p2, | ||
210 | &set_mq, | ||
211 | mq); | ||
212 | /* update set connected flag for all requests */ | ||
213 | for (cc = tth->cc_head; NULL != cc; cc = cc->next) | ||
214 | { | ||
215 | if (GNUNET_YES == cc->connected) | ||
216 | continue; | ||
217 | if ((GNUNET_YES == cc->p1_c) && | ||
218 | (GNUNET_YES == cc->p2_c)) | ||
219 | { | ||
220 | cc->connected = GNUNET_YES; | ||
221 | /* stop trying to connect */ | ||
222 | if (NULL != cc->tct) | ||
223 | { | ||
224 | GNUNET_SCHEDULER_cancel (cc->tct); | ||
225 | cc->tct = NULL; | ||
226 | } | ||
227 | if (NULL != cc->ats_sh) | ||
228 | { | ||
229 | GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh); | ||
230 | cc->ats_sh = NULL; | ||
231 | } | ||
232 | } | ||
233 | } | ||
234 | /* then notify application */ | ||
235 | for (cc = tth->cc_head; NULL != cc; cc = ccn) | ||
236 | { | ||
237 | ccn = cc->next; | ||
238 | if ((GNUNET_YES == cc->connected) && | ||
239 | (NULL != cc->cb)) | ||
240 | { | ||
241 | cc->cb (cc->cb_cls); | ||
242 | cc->cb = NULL; /* only notify once! */ | ||
243 | } | ||
244 | } | ||
245 | return ret; | ||
246 | } | ||
247 | |||
248 | |||
249 | /** | ||
250 | * Offer the current HELLO of P2 to P1. | ||
251 | * | ||
252 | * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest` | ||
253 | */ | ||
254 | static void | ||
255 | offer_hello (void *cls); | ||
256 | |||
257 | |||
258 | static void | ||
259 | notify_disconnect (void *cls, | ||
260 | const struct GNUNET_PeerIdentity *peer, | ||
261 | void *handler_cls) | ||
262 | { | ||
263 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; | ||
264 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth; | ||
265 | char *p2_s; | ||
266 | /* Find PeerContext */ | ||
267 | int no = 0; | ||
268 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = NULL; | ||
269 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
270 | |||
271 | p2 = find_peer_context (p->tth, | ||
272 | peer); | ||
273 | no = p->no; | ||
274 | if (NULL != p2) | ||
275 | GNUNET_asprintf (&p2_s, | ||
276 | "%u (`%s')", | ||
277 | p2->no, | ||
278 | GNUNET_i2s (&p2->id)); | ||
279 | else | ||
280 | GNUNET_asprintf (&p2_s, | ||
281 | "`%s'", | ||
282 | GNUNET_i2s (peer)); | ||
283 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
284 | "Peers %s disconnected from peer %u (`%s')\n", | ||
285 | p2_s, | ||
286 | no, | ||
287 | GNUNET_i2s (&p->id)); | ||
288 | GNUNET_free (p2_s); | ||
289 | /* notify about disconnect */ | ||
290 | if (NULL != p->nd) | ||
291 | p->nd (p->cb_cls, | ||
292 | peer, | ||
293 | handler_cls); | ||
294 | if (NULL == p2) | ||
295 | return; | ||
296 | /* clear MQ, it is now invalid */ | ||
297 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p, | ||
298 | p2, | ||
299 | &set_mq, | ||
300 | NULL); | ||
301 | /* update set connected flags for all requests */ | ||
302 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p, | ||
303 | p2, | ||
304 | &clear_p1c, | ||
305 | NULL); | ||
306 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p2, | ||
307 | p, | ||
308 | &clear_p2c, | ||
309 | NULL); | ||
310 | /* resume connectivity requests as necessary */ | ||
311 | for (cc = tth->cc_head; NULL != cc; cc = cc->next) | ||
312 | { | ||
313 | if (GNUNET_NO == cc->connected) | ||
314 | continue; | ||
315 | if ((GNUNET_YES != cc->p1_c) || | ||
316 | (GNUNET_YES != cc->p2_c)) | ||
317 | { | ||
318 | cc->connected = GNUNET_NO; | ||
319 | /* start trying to connect */ | ||
320 | if (NULL == cc->tct) | ||
321 | cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello, | ||
322 | cc); | ||
323 | if (NULL == cc->ats_sh) | ||
324 | cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats, | ||
325 | &p2->id, | ||
326 | 1); | ||
327 | } | ||
328 | } | ||
329 | } | ||
330 | |||
331 | static void | ||
332 | retrieve_hello (void *cls); | ||
333 | |||
334 | static void | ||
335 | hello_iter_cb (void *cb_cls, | ||
336 | const struct GNUNET_PEERSTORE_Record *record, | ||
337 | const char *emsg) | ||
338 | { | ||
339 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cb_cls; | ||
340 | if (NULL == record) | ||
341 | { | ||
342 | p->pic = NULL; | ||
343 | if (NULL != p->start_cb) | ||
344 | p->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, p); | ||
345 | return; | ||
346 | } | ||
347 | // Check record type et al? | ||
348 | p->hello_size = record->value_size; | ||
349 | p->hello = GNUNET_malloc (p->hello_size); | ||
350 | memcpy (p->hello, record->value, p->hello_size); | ||
351 | p->hello[p->hello_size - 1] = '\0'; | ||
352 | |||
353 | GNUNET_PEERSTORE_iterate_cancel (p->pic); | ||
354 | p->pic = NULL; | ||
355 | if (NULL != p->start_cb) | ||
356 | { | ||
357 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
358 | "Peer %u (`%s') successfully started\n", | ||
359 | p->no, | ||
360 | GNUNET_i2s (&p->id)); | ||
361 | p->start_cb (p->start_cb_cls); | ||
362 | p->start_cb = NULL; | ||
363 | } | ||
364 | } | ||
365 | |||
366 | |||
367 | static void | ||
368 | retrieve_hello (void *cls) | ||
369 | { | ||
370 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; | ||
371 | p->rh_task = NULL; | ||
372 | p->pic = GNUNET_PEERSTORE_iterate (p->ph, | ||
373 | "transport", | ||
374 | &p->id, | ||
375 | GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY, | ||
376 | hello_iter_cb, | ||
377 | p); | ||
378 | |||
379 | } | ||
380 | |||
381 | |||
382 | /** | ||
383 | * Start a peer with the given configuration | ||
384 | * @param tth the testing handle | ||
385 | * @param cfgname configuration file | ||
386 | * @param peer_id a unique number to identify the peer | ||
387 | * @param handlers functions for receiving messages | ||
388 | * @param nc connect callback | ||
389 | * @param nd disconnect callback | ||
390 | * @param cb_cls closure for callback | ||
391 | * @param start_cb start callback | ||
392 | * @param start_cb_cls closure for callback | ||
393 | * @return the peer context | ||
394 | */ | ||
395 | struct GNUNET_TRANSPORT_TESTING_PeerContext * | ||
396 | GNUNET_TRANSPORT_TESTING_start_peer (struct | ||
397 | GNUNET_TRANSPORT_TESTING_Handle *tth, | ||
398 | const char *cfgname, | ||
399 | int peer_id, | ||
400 | const struct | ||
401 | GNUNET_MQ_MessageHandler *handlers, | ||
402 | GNUNET_TRANSPORT_NotifyConnect nc, | ||
403 | GNUNET_TRANSPORT_NotifyDisconnect nd, | ||
404 | void *cb_cls, | ||
405 | GNUNET_SCHEDULER_TaskCallback start_cb, | ||
406 | void *start_cb_cls) | ||
407 | { | ||
408 | char *emsg = NULL; | ||
409 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p; | ||
410 | struct GNUNET_PeerIdentity dummy; | ||
411 | unsigned int i; | ||
412 | |||
413 | if (GNUNET_NO == GNUNET_DISK_file_test (cfgname)) | ||
414 | { | ||
415 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
416 | "File not found: `%s'\n", | ||
417 | cfgname); | ||
418 | return NULL; | ||
419 | } | ||
420 | |||
421 | p = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_PeerContext); | ||
422 | p->tth = tth; | ||
423 | p->nc = nc; | ||
424 | p->nd = nd; | ||
425 | if (NULL != handlers) | ||
426 | { | ||
427 | for (i = 0; NULL != handlers[i].cb; i++) | ||
428 | ; | ||
429 | p->handlers = GNUNET_new_array (i + 1, | ||
430 | struct GNUNET_MQ_MessageHandler); | ||
431 | GNUNET_memcpy (p->handlers, | ||
432 | handlers, | ||
433 | i * sizeof(struct GNUNET_MQ_MessageHandler)); | ||
434 | } | ||
435 | if (NULL != cb_cls) | ||
436 | p->cb_cls = cb_cls; | ||
437 | else | ||
438 | p->cb_cls = p; | ||
439 | p->start_cb = start_cb; | ||
440 | if (NULL != start_cb_cls) | ||
441 | p->start_cb_cls = start_cb_cls; | ||
442 | else | ||
443 | p->start_cb_cls = p; | ||
444 | GNUNET_CONTAINER_DLL_insert (tth->p_head, | ||
445 | tth->p_tail, | ||
446 | p); | ||
447 | |||
448 | /* Create configuration and call testing lib to modify it */ | ||
449 | p->cfg = GNUNET_CONFIGURATION_create (); | ||
450 | GNUNET_assert (GNUNET_OK == | ||
451 | GNUNET_CONFIGURATION_load (p->cfg, cfgname)); | ||
452 | if (GNUNET_SYSERR == | ||
453 | GNUNET_TESTING_configuration_create (tth->tl_system, | ||
454 | p->cfg)) | ||
455 | { | ||
456 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
457 | "Testing library failed to create unique configuration based on `%s'\n", | ||
458 | cfgname); | ||
459 | GNUNET_CONFIGURATION_destroy (p->cfg); | ||
460 | GNUNET_free (p); | ||
461 | return NULL; | ||
462 | } | ||
463 | |||
464 | p->no = peer_id; | ||
465 | /* Configure peer with configuration */ | ||
466 | p->peer = GNUNET_TESTING_peer_configure (tth->tl_system, | ||
467 | p->cfg, | ||
468 | p->no, | ||
469 | NULL, | ||
470 | &emsg); | ||
471 | if (NULL == p->peer) | ||
472 | { | ||
473 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
474 | "Testing library failed to create unique configuration based on `%s': `%s'\n", | ||
475 | cfgname, | ||
476 | emsg); | ||
477 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
478 | GNUNET_free (emsg); | ||
479 | return NULL; | ||
480 | } | ||
481 | |||
482 | if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer)) | ||
483 | { | ||
484 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
485 | "Testing library failed to create unique configuration based on `%s'\n", | ||
486 | cfgname); | ||
487 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
488 | return NULL; | ||
489 | } | ||
490 | |||
491 | memset (&dummy, | ||
492 | '\0', | ||
493 | sizeof(dummy)); | ||
494 | GNUNET_TESTING_peer_get_identity (p->peer, | ||
495 | &p->id); | ||
496 | if (0 == memcmp (&dummy, | ||
497 | &p->id, | ||
498 | sizeof(struct GNUNET_PeerIdentity))) | ||
499 | { | ||
500 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
501 | "Testing library failed to obtain peer identity for peer %u\n", | ||
502 | p->no); | ||
503 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
504 | return NULL; | ||
505 | } | ||
506 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
507 | "Peer %u configured with identity `%s'\n", | ||
508 | p->no, | ||
509 | GNUNET_i2s_full (&p->id)); | ||
510 | p->th = GNUNET_TRANSPORT_core_connect (p->cfg, | ||
511 | NULL, | ||
512 | handlers, | ||
513 | p, | ||
514 | ¬ify_connect, | ||
515 | ¬ify_disconnect); | ||
516 | if (NULL == p->th) | ||
517 | { | ||
518 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
519 | "Failed to connect to transport service for peer `%s': `%s'\n", | ||
520 | cfgname, | ||
521 | emsg); | ||
522 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
523 | GNUNET_free (emsg); | ||
524 | return NULL; | ||
525 | } | ||
526 | p->ats = GNUNET_ATS_connectivity_init (p->cfg); | ||
527 | if (NULL == p->ats) | ||
528 | { | ||
529 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
530 | "Failed to connect to ATS service for peer `%s': `%s'\n", | ||
531 | cfgname, | ||
532 | emsg); | ||
533 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
534 | GNUNET_free (emsg); | ||
535 | return NULL; | ||
536 | } | ||
537 | p->ph = GNUNET_PEERSTORE_connect (p->cfg); | ||
538 | // FIXME Error handling | ||
539 | p->ah = GNUNET_TRANSPORT_application_init (p->cfg); | ||
540 | GNUNET_assert (NULL != p->ah); | ||
541 | // FIXME Error handling | ||
542 | p->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, p); | ||
543 | |||
544 | return p; | ||
545 | } | ||
546 | |||
547 | |||
548 | /** | ||
549 | * Stops and restarts the given peer, sleeping (!) for 5s in between. | ||
550 | * | ||
551 | * @param p the peer | ||
552 | * @param restart_cb callback to call when restarted | ||
553 | * @param restart_cb_cls callback closure | ||
554 | * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR | ||
555 | */ | ||
556 | int | ||
557 | GNUNET_TRANSPORT_TESTING_restart_peer (struct | ||
558 | GNUNET_TRANSPORT_TESTING_PeerContext *p, | ||
559 | GNUNET_SCHEDULER_TaskCallback restart_cb, | ||
560 | void *restart_cb_cls) | ||
561 | { | ||
562 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
563 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | ||
564 | |||
565 | /* shutdown */ | ||
566 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
567 | "Stopping peer %u (`%s')\n", | ||
568 | p->no, | ||
569 | GNUNET_i2s (&p->id)); | ||
570 | if (NULL != p->pic) | ||
571 | { | ||
572 | GNUNET_PEERSTORE_iterate_cancel (p->pic); | ||
573 | p->pic = NULL; | ||
574 | } | ||
575 | if (NULL != p->th) | ||
576 | { | ||
577 | GNUNET_TRANSPORT_core_disconnect (p->th); | ||
578 | p->th = NULL; | ||
579 | } | ||
580 | for (cc = p->tth->cc_head; NULL != cc; cc = ccn) | ||
581 | { | ||
582 | ccn = cc->next; | ||
583 | if ((cc->p1 == p) || | ||
584 | (cc->p2 == p)) | ||
585 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); | ||
586 | } | ||
587 | if (NULL != p->ats) | ||
588 | { | ||
589 | GNUNET_ATS_connectivity_done (p->ats); | ||
590 | p->ats = NULL; | ||
591 | } | ||
592 | if (GNUNET_SYSERR == | ||
593 | GNUNET_TESTING_peer_stop (p->peer)) | ||
594 | { | ||
595 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
596 | "Failed to stop peer %u (`%s')\n", | ||
597 | p->no, | ||
598 | GNUNET_i2s (&p->id)); | ||
599 | return GNUNET_SYSERR; | ||
600 | } | ||
601 | |||
602 | sleep (5); // YUCK! | ||
603 | |||
604 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
605 | "Restarting peer %u (`%s')\n", | ||
606 | p->no, | ||
607 | GNUNET_i2s (&p->id)); | ||
608 | /* restart */ | ||
609 | if (GNUNET_SYSERR == GNUNET_TESTING_peer_start (p->peer)) | ||
610 | { | ||
611 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
612 | "Failed to restart peer %u (`%s')\n", | ||
613 | p->no, | ||
614 | GNUNET_i2s (&p->id)); | ||
615 | return GNUNET_SYSERR; | ||
616 | } | ||
617 | |||
618 | GNUNET_assert (NULL == p->start_cb); | ||
619 | p->start_cb = restart_cb; | ||
620 | p->start_cb_cls = restart_cb_cls; | ||
621 | |||
622 | p->th = GNUNET_TRANSPORT_core_connect (p->cfg, | ||
623 | NULL, | ||
624 | p->handlers, | ||
625 | p, | ||
626 | ¬ify_connect, | ||
627 | ¬ify_disconnect); | ||
628 | GNUNET_assert (NULL != p->th); | ||
629 | p->ats = GNUNET_ATS_connectivity_init (p->cfg); | ||
630 | p->pic = GNUNET_PEERSTORE_iterate (p->ph, | ||
631 | "transport", | ||
632 | &p->id, | ||
633 | GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY, | ||
634 | hello_iter_cb, | ||
635 | p); | ||
636 | GNUNET_assert (NULL != p->pic); | ||
637 | return GNUNET_OK; | ||
638 | } | ||
639 | |||
640 | |||
641 | /** | ||
642 | * Shutdown the given peer | ||
643 | * | ||
644 | * @param p the peer | ||
645 | */ | ||
646 | void | ||
647 | GNUNET_TRANSPORT_TESTING_stop_peer (struct | ||
648 | GNUNET_TRANSPORT_TESTING_PeerContext *p) | ||
649 | { | ||
650 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth; | ||
651 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
652 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | ||
653 | /* shutdown */ | ||
654 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
655 | "Stopping peer %u (`%s')\n", | ||
656 | p->no, | ||
657 | GNUNET_i2s (&p->id)); | ||
658 | |||
659 | for (cc = tth->cc_head; NULL != cc; cc = ccn) | ||
660 | { | ||
661 | ccn = cc->next; | ||
662 | if ((cc->p1 == p) || | ||
663 | (cc->p2 == p)) | ||
664 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); | ||
665 | } | ||
666 | if (NULL != p->pic) | ||
667 | { | ||
668 | GNUNET_PEERSTORE_iterate_cancel (p->pic); | ||
669 | p->pic = NULL; | ||
670 | } | ||
671 | if (NULL != p->th) | ||
672 | { | ||
673 | GNUNET_TRANSPORT_core_disconnect (p->th); | ||
674 | p->th = NULL; | ||
675 | } | ||
676 | if (NULL != p->ats) | ||
677 | { | ||
678 | GNUNET_ATS_connectivity_done (p->ats); | ||
679 | p->ats = NULL; | ||
680 | } | ||
681 | if (NULL != p->ah) | ||
682 | { | ||
683 | GNUNET_TRANSPORT_application_done (p->ah); | ||
684 | p->ah = NULL; | ||
685 | } | ||
686 | if (NULL != p->ph) | ||
687 | { | ||
688 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
689 | "Disconnecting from PEERSTORE service\n"); | ||
690 | GNUNET_PEERSTORE_disconnect (p->ph, GNUNET_NO); | ||
691 | p->ph = NULL; | ||
692 | } | ||
693 | |||
694 | if (NULL != p->peer) | ||
695 | { | ||
696 | if (GNUNET_OK != | ||
697 | GNUNET_TESTING_peer_stop (p->peer)) | ||
698 | { | ||
699 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
700 | "Testing lib failed to stop peer %u (`%s')\n", | ||
701 | p->no, | ||
702 | GNUNET_i2s (&p->id)); | ||
703 | } | ||
704 | GNUNET_TESTING_peer_destroy (p->peer); | ||
705 | p->peer = NULL; | ||
706 | } | ||
707 | if (NULL != p->hello) | ||
708 | { | ||
709 | GNUNET_free (p->hello); | ||
710 | p->hello = NULL; | ||
711 | } | ||
712 | if (NULL != p->cfg) | ||
713 | { | ||
714 | GNUNET_CONFIGURATION_destroy (p->cfg); | ||
715 | p->cfg = NULL; | ||
716 | } | ||
717 | if (NULL != p->handlers) | ||
718 | { | ||
719 | GNUNET_free (p->handlers); | ||
720 | p->handlers = NULL; | ||
721 | } | ||
722 | GNUNET_CONTAINER_DLL_remove (tth->p_head, | ||
723 | tth->p_tail, | ||
724 | p); | ||
725 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
726 | "Peer %u (`%s') stopped\n", | ||
727 | p->no, | ||
728 | GNUNET_i2s (&p->id)); | ||
729 | if (NULL != p->rh_task) | ||
730 | GNUNET_SCHEDULER_cancel (p->rh_task); | ||
731 | p->rh_task = NULL; | ||
732 | GNUNET_free (p); | ||
733 | } | ||
734 | |||
735 | |||
736 | /** | ||
737 | * Function called after the HELLO was passed to the | ||
738 | * transport service. | ||
739 | * FIXME maybe schedule the application_validate somehow | ||
740 | */ | ||
741 | /* | ||
742 | static void | ||
743 | hello_offered (void *cls) | ||
744 | { | ||
745 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls; | ||
746 | |||
747 | cc->oh = NULL; | ||
748 | cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
749 | &offer_hello, | ||
750 | cc); | ||
751 | }*/ | ||
752 | |||
753 | |||
754 | /** | ||
755 | * Offer the current HELLO of P2 to P1. | ||
756 | * | ||
757 | * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest` | ||
758 | */ | ||
759 | static void | ||
760 | offer_hello (void *cls) | ||
761 | { | ||
762 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls; | ||
763 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p1 = cc->p1; | ||
764 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = cc->p2; | ||
765 | struct GNUNET_TIME_Absolute t; | ||
766 | enum GNUNET_NetworkType nt = 0; | ||
767 | char *addr; | ||
768 | |||
769 | cc->tct = NULL; | ||
770 | { | ||
771 | char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id)); | ||
772 | |||
773 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
774 | "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %s\n", | ||
775 | p1->no, | ||
776 | GNUNET_i2s (&p1->id), | ||
777 | p2->no, | ||
778 | p2_s, | ||
779 | p2->hello); | ||
780 | GNUNET_free (p2_s); | ||
781 | } | ||
782 | |||
783 | addr = GNUNET_HELLO_extract_address (p2->hello, | ||
784 | p2->hello_size, | ||
785 | &p2->id, | ||
786 | &nt, | ||
787 | &t); | ||
788 | GNUNET_assert (NULL != addr); | ||
789 | GNUNET_assert (NULL != p1->hello); | ||
790 | GNUNET_TRANSPORT_application_validate (p1->ah, | ||
791 | &p2->id, | ||
792 | nt, | ||
793 | addr); | ||
794 | GNUNET_free (addr); | ||
795 | } | ||
796 | |||
797 | |||
798 | /** | ||
799 | * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message | ||
800 | * | ||
801 | * Remarks: start_peer's notify_connect callback can be called before. | ||
802 | * | ||
803 | * @param tth transport testing handle | ||
804 | * @param p1 peer 1 | ||
805 | * @param p2 peer 2 | ||
806 | * @param cb the callback to call when both peers notified that they are connected | ||
807 | * @param cls callback cls | ||
808 | * @return a connect request handle | ||
809 | */ | ||
810 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest * | ||
811 | GNUNET_TRANSPORT_TESTING_connect_peers (struct | ||
812 | GNUNET_TRANSPORT_TESTING_PeerContext *p1, | ||
813 | struct | ||
814 | GNUNET_TRANSPORT_TESTING_PeerContext *p2, | ||
815 | GNUNET_SCHEDULER_TaskCallback cb, | ||
816 | void *cls) | ||
817 | { | ||
818 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth; | ||
819 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
820 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | ||
821 | |||
822 | ccn = NULL; | ||
823 | for (cc = tth->cc_head; NULL != cc; cc = cc->next) | ||
824 | { | ||
825 | if ((cc->p1 == p1) && | ||
826 | (cc->p2 == p2)) | ||
827 | { | ||
828 | ccn = cc; | ||
829 | break; | ||
830 | } | ||
831 | } | ||
832 | |||
833 | cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest); | ||
834 | cc->p1 = p1; | ||
835 | cc->p2 = p2; | ||
836 | cc->cb = cb; | ||
837 | if (NULL != cls) | ||
838 | cc->cb_cls = cls; | ||
839 | else | ||
840 | cc->cb_cls = cc; | ||
841 | if (NULL != ccn) | ||
842 | { | ||
843 | cc->p1_c = ccn->p1_c; | ||
844 | cc->p2_c = ccn->p2_c; | ||
845 | cc->connected = ccn->connected; | ||
846 | } | ||
847 | GNUNET_CONTAINER_DLL_insert (tth->cc_head, | ||
848 | tth->cc_tail, | ||
849 | cc); | ||
850 | cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello, | ||
851 | cc); | ||
852 | cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats, | ||
853 | &p2->id, | ||
854 | 1); | ||
855 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
856 | "New connect request %p\n", | ||
857 | cc); | ||
858 | return cc; | ||
859 | } | ||
860 | |||
861 | |||
862 | /** | ||
863 | * Cancel the request to connect two peers | ||
864 | * Tou MUST cancel the request if you stop the peers before the peers connected successfully | ||
865 | * | ||
866 | * @param tth transport testing handle | ||
867 | * @param cc a connect request handle | ||
868 | */ | ||
869 | void | ||
870 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct | ||
871 | GNUNET_TRANSPORT_TESTING_ConnectRequest | ||
872 | *cc) | ||
873 | { | ||
874 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = cc->p1->tth; | ||
875 | |||
876 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
877 | "Canceling connect request!\n"); | ||
878 | if (NULL != cc->tct) | ||
879 | { | ||
880 | GNUNET_SCHEDULER_cancel (cc->tct); | ||
881 | cc->tct = NULL; | ||
882 | } | ||
883 | if (NULL != cc->ats_sh) | ||
884 | { | ||
885 | GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh); | ||
886 | cc->ats_sh = NULL; | ||
887 | } | ||
888 | GNUNET_CONTAINER_DLL_remove (tth->cc_head, | ||
889 | tth->cc_tail, | ||
890 | cc); | ||
891 | GNUNET_free (cc); | ||
892 | } | ||
893 | |||
894 | |||
895 | /** | ||
896 | * Clean up the transport testing | ||
897 | * | ||
898 | * @param tth transport testing handle | ||
899 | */ | ||
900 | void | ||
901 | GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth) | ||
902 | { | ||
903 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
904 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct; | ||
905 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p; | ||
906 | struct GNUNET_TRANSPORT_TESTING_PeerContext *t; | ||
907 | |||
908 | if (NULL == tth) | ||
909 | return; | ||
910 | cc = tth->cc_head; | ||
911 | while (NULL != cc) | ||
912 | { | ||
913 | ct = cc->next; | ||
914 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
915 | "Developer forgot to cancel connect request!\n"); | ||
916 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); | ||
917 | cc = ct; | ||
918 | } | ||
919 | p = tth->p_head; | ||
920 | while (NULL != p) | ||
921 | { | ||
922 | t = p->next; | ||
923 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
924 | "Developer forgot to stop peer!\n"); | ||
925 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
926 | p = t; | ||
927 | } | ||
928 | GNUNET_TESTING_system_destroy (tth->tl_system, | ||
929 | GNUNET_YES); | ||
930 | |||
931 | GNUNET_free (tth); | ||
932 | } | ||
933 | |||
934 | |||
935 | /** | ||
936 | * Initialize the transport testing | ||
937 | * | ||
938 | * @return transport testing handle | ||
939 | */ | ||
940 | struct GNUNET_TRANSPORT_TESTING_Handle * | ||
941 | GNUNET_TRANSPORT_TESTING_init () | ||
942 | { | ||
943 | struct GNUNET_TRANSPORT_TESTING_Handle *tth; | ||
944 | |||
945 | tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_Handle); | ||
946 | tth->tl_system = GNUNET_TESTING_system_create ("transport-testing", | ||
947 | NULL, | ||
948 | NULL, | ||
949 | NULL); | ||
950 | if (NULL == tth->tl_system) | ||
951 | { | ||
952 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
953 | "Failed to initialize testing library!\n"); | ||
954 | GNUNET_free (tth); | ||
955 | return NULL; | ||
956 | } | ||
957 | return tth; | ||
958 | } | ||
959 | |||
960 | |||
961 | /* end of transport-testing.c */ | ||