aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport-testing2.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2020-12-01 08:39:13 +0100
committerChristian Grothoff <christian@grothoff.org>2020-12-01 08:39:13 +0100
commit4a085eddc5b52b472cc1320944975fb11c6ebe27 (patch)
treebff97df59ef2ab8e3a810e596ab636a9a02d3e78 /src/transport/transport-testing2.c
parent795ba5243bfcb595cd3b3066ae22f59c686c48bb (diff)
parentddbb4c740614d988431700bdcbb358de00be6313 (diff)
downloadgnunet-4a085eddc5b52b472cc1320944975fb11c6ebe27.tar.gz
gnunet-4a085eddc5b52b472cc1320944975fb11c6ebe27.zip
Merge branch 'master' of git+ssh://gnunet.org/gnunet
Diffstat (limited to 'src/transport/transport-testing2.c')
-rw-r--r--src/transport/transport-testing2.c1697
1 files changed, 728 insertions, 969 deletions
diff --git a/src/transport/transport-testing2.c b/src/transport/transport-testing2.c
index 076fbf331..ca338c52f 100644
--- a/src/transport/transport-testing2.c
+++ b/src/transport/transport-testing2.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2019 GNUnet e.V. 3 Copyright (C) 2006, 2009, 2015, 2016 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 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 6 under the terms of the GNU Affero General Public License as published
@@ -17,1148 +17,907 @@
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
20
21/** 20/**
22 * @file transport/transport-testing2.c 21 * @file transport-testing.c
23 * @brief functions related to testing-tng 22 * @brief testing lib for transport service
23 * @author Matthias Wachs
24 * @author Christian Grothoff 24 * @author Christian Grothoff
25 * @author Julius Bünger
26 */ 25 */
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_protocols.h"
30#include "gnunet_constants.h"
31#include "transport-testing2.h" 26#include "transport-testing2.h"
32#include "gnunet_ats_transport_service.h"
33#include "gnunet_hello_lib.h"
34#include "gnunet_signatures.h"
35#include "transport.h"
36#include <inttypes.h>
37
38#define LOG(kind, ...) GNUNET_log_from (kind, "transport-testing2", __VA_ARGS__)
39
40struct MyClient
41{
42 struct MyClient *prev;
43 struct MyClient *next;
44 /**
45 * @brief Handle to the client
46 */
47 struct GNUNET_SERVICE_Client *client;
48
49 /**
50 * @brief Handle to the client
51 */
52 struct GNUNET_MQ_Handle *c_mq;
53
54 /**
55 * The TCH
56 */
57 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc;
58
59};
60
61/**
62 * @brief Queue of a communicator and some context
63 */
64struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue
65{
66 /**
67 * @brief Handle to the TransportCommunicator
68 */
69 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h;
70
71 /**
72 * @brief Envelope to a message that requests the opening of the queue.
73 *
74 * If the client already requests queue(s), but the communicator is not yet
75 * connected, we cannot send the request to open the queue. Save it until the
76 * communicator becomes available and send it then.
77 */
78 struct GNUNET_MQ_Envelope *open_queue_env;
79
80 /**
81 * @brief Peer ID of the peer on the other side of the queue
82 */
83 struct GNUNET_PeerIdentity peer_id;
84
85 /**
86 * @brief Queue ID
87 */
88 uint32_t qid;
89
90 /**
91 * @brief Current message id
92 */
93 uint64_t mid;
94
95 /**
96 * An `enum GNUNET_NetworkType` in NBO.
97 */
98 uint32_t nt;
99
100 /**
101 * Maximum transmission unit. UINT32_MAX for unlimited.
102 */
103 uint32_t mtu;
104
105 /**
106 * Queue length. UINT64_MAX for unlimited.
107 */
108 uint64_t q_len;
109
110 /**
111 * Queue prio
112 */
113 uint32_t priority;
114
115 /**
116 * An `enum GNUNET_TRANSPORT_ConnectionStatus` in NBO.
117 */
118 uint32_t cs;
119
120 /**
121 * @brief Next element inside a DLL
122 */
123 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *next;
124
125 /**
126 * @brief Previous element inside a DLL
127 */
128 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *prev;
129};
130 27
131 28
132/** 29#define LOG(kind, ...) GNUNET_log_from (kind, "transport-testing", __VA_ARGS__)
133 * @brief Handle/Context to a single transmission
134 */
135struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission
136{
137};
138 30
139 31
140/** 32static struct GNUNET_TRANSPORT_TESTING_PeerContext *
141 * @brief Check whether incoming msg indicating available communicator is 33find_peer_context (struct GNUNET_TRANSPORT_TESTING_Handle *tth,
142 * correct 34 const struct GNUNET_PeerIdentity *peer)
143 *
144 * @param cls Closure
145 * @param msg Message struct
146 *
147 * @return GNUNET_YES in case message is correct
148 */
149static int
150check_communicator_available (
151 void *cls,
152 const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
153{ 35{
154 uint16_t size; 36 struct GNUNET_TRANSPORT_TESTING_PeerContext *t;
155 37
156 size = ntohs (msg->header.size) - sizeof(*msg); 38 for (t = tth->p_head; NULL != t; t = t->next)
157 if (0 == size) 39 if (0 == memcmp (&t->id,
158 return GNUNET_OK; /* receive-only communicator */ 40 peer,
159 GNUNET_MQ_check_zero_termination (msg); 41 sizeof(struct GNUNET_PeerIdentity)))
160 return GNUNET_OK; 42 return t;
43 return NULL;
161} 44}
162 45
163 46
164/** 47/**
165 * @brief Handle new communicator 48 * Find any connecting context matching the given pair of peers.
166 *
167 * Store characteristics of communicator, call respective client callback.
168 * 49 *
169 * @param cls Closure - communicator handle 50 * @param p1 first peer
170 * @param msg Message struct 51 * @param p2 second peer
52 * @param cb function to call
53 * @param cb_cls closure for @a cb
171 */ 54 */
172static void 55void
173handle_communicator_available ( 56GNUNET_TRANSPORT_TESTING_find_connecting_context (struct
174 void *cls, 57 GNUNET_TRANSPORT_TESTING_PeerContext
175 const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg) 58 *p1,
176{ 59 struct
177 struct MyClient *client = cls; 60 GNUNET_TRANSPORT_TESTING_PeerContext
178 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = 61 *p2,
179 client->tc; 62 GNUNET_TRANSPORT_TESTING_ConnectContextCallback
180 uint16_t size; 63 cb,
181 tc_h->c_mq = client->c_mq; 64 void *cb_cls)
182 65{
183 size = ntohs (msg->header.size) - sizeof(*msg); 66 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth;
184 if (0 == size) 67 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
185 { 68 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
186 GNUNET_SERVICE_client_continue (client->client); 69
187 return; /* receive-only communicator */ 70 for (cc = tth->cc_head; NULL != cc; cc = ccn)
188 }
189 tc_h->c_characteristics = ntohl (msg->cc);
190 tc_h->c_addr_prefix = GNUNET_strdup ((const char *) &msg[1]);
191 if (NULL != tc_h->communicator_available_cb)
192 { 71 {
193 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling communicator_available_cb()\n"); 72 ccn = cc->next;
194 tc_h->communicator_available_cb (tc_h->cb_cls, 73 if ((cc->p1 == p1) &&
195 tc_h, 74 (cc->p2 == p2))
196 tc_h->c_characteristics, 75 cb (cb_cls,
197 tc_h->c_addr_prefix); 76 cc);
198 } 77 }
199 GNUNET_SERVICE_client_continue (client->client);
200 LOG (GNUNET_ERROR_TYPE_DEBUG, "finished communicator_available_cb()\n");
201
202} 78}
203 79
204 80
205/** 81static void
206 * Incoming message. Test message is well-formed. 82set_p1c (void *cls,
207 * 83 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
208 * @param cls the client
209 * @param msg the send message that was sent
210 * @return #GNUNET_OK if message is well-formed
211 */
212static int
213check_communicator_backchannel (void *cls,
214 const struct
215 GNUNET_TRANSPORT_CommunicatorBackchannel *msg)
216{ 84{
217 // struct TransportClient *tc = cls; 85 int *found = cls;
218 86
219 // if (CT_COMMUNICATOR != tc->type) 87 if (NULL != found)
220 // { 88 *found = GNUNET_YES;
221 // GNUNET_break (0); 89 cx->p1_c = GNUNET_YES;
222 // return GNUNET_SYSERR;
223 // }
224 // GNUNET_MQ_check_boxed_message (msg);
225 return GNUNET_OK;
226} 90}
227 91
228 92
229/**
230 * @brief Receive an incoming message.
231 *
232 * Pass the message to the client.
233 *
234 * @param cls Closure - communicator handle
235 * @param msg Message
236 */
237static void 93static void
238handle_communicator_backchannel (void *cls, 94set_mq (void *cls,
239 const struct 95 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
240 GNUNET_TRANSPORT_CommunicatorBackchannel *
241 bc_msg)
242{ 96{
243 struct MyClient *client = cls; 97 struct GNUNET_MQ_Handle *mq = cls;
244 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h =
245 client->tc;
246 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *other_tc_h;
247 struct GNUNET_MessageHeader *msg;
248 msg = (struct GNUNET_MessageHeader *) &bc_msg[1];
249 uint16_t isize = ntohs (msg->size);
250 const char *target_communicator = ((const char *) msg) + isize;
251 struct GNUNET_TRANSPORT_CommunicatorBackchannelIncoming *cbi;
252 struct GNUNET_MQ_Envelope *env;
253 98
254 LOG (GNUNET_ERROR_TYPE_DEBUG, 99 cx->mq = mq;
255 "Received backchannel message\n");
256 if (tc_h->bc_enabled != GNUNET_YES)
257 {
258 GNUNET_SERVICE_client_continue (client->client);
259 return;
260 }
261 /* Find client providing this communicator */
262 /* Finally, deliver backchannel message to communicator */
263 LOG (GNUNET_ERROR_TYPE_DEBUG,
264 "Delivering backchannel message of type %u to %s\n",
265 ntohs (msg->type),
266 target_communicator);
267 other_tc_h = tc_h->bc_cb (tc_h, msg, (struct
268 GNUNET_PeerIdentity*) &bc_msg->pid);
269 env = GNUNET_MQ_msg_extra (
270 cbi,
271 isize,
272 GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL_INCOMING);
273 cbi->pid = tc_h->peer_id;
274 memcpy (&cbi[1], msg, isize);
275
276
277 GNUNET_MQ_send (other_tc_h->c_mq, env);
278 GNUNET_SERVICE_client_continue (client->client);
279} 100}
280 101
281 102
282/** 103static void
283 * Address of our peer added. Test message is well-formed. 104set_p2c (void *cls,
284 * 105 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
285 * @param cls the client
286 * @param aam the send message that was sent
287 * @return #GNUNET_OK if message is well-formed
288 */
289static int
290check_add_address (void *cls,
291 const struct GNUNET_TRANSPORT_AddAddressMessage *msg)
292{ 106{
293 // if (CT_COMMUNICATOR != tc->type) 107 int *found = cls;
294 // { 108
295 // GNUNET_break (0); 109 if (NULL != found)
296 // return GNUNET_SYSERR; 110 *found = GNUNET_YES;
297 // } 111 cx->p2_c = GNUNET_YES;
298 GNUNET_MQ_check_zero_termination (msg);
299 return GNUNET_OK;
300} 112}
301 113
302 114
303/**
304 * @brief The communicator informs about an address.
305 *
306 * Store address and call client callback.
307 *
308 * @param cls Closure - communicator handle
309 * @param msg Message
310 */
311static void 115static void
312handle_add_address (void *cls, 116clear_p1c (void *cls,
313 const struct GNUNET_TRANSPORT_AddAddressMessage *msg) 117 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
314{ 118{
315 struct MyClient *client = cls; 119 int *found = cls;
316 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = 120
317 client->tc; 121 if (NULL != found)
318 uint16_t size; 122 *found = GNUNET_YES;
319 size = ntohs (msg->header.size) - sizeof(*msg); 123 cx->p1_c = GNUNET_NO;
320 if (0 == size)
321 return; /* receive-only communicator */
322 LOG (GNUNET_ERROR_TYPE_DEBUG, "received add address cb %u\n", size);
323 tc_h->c_address = GNUNET_strdup ((const char *) &msg[1]);
324 if (NULL != tc_h->add_address_cb)
325 {
326 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling add_address_cb()\n");
327 tc_h->add_address_cb (tc_h->cb_cls,
328 tc_h,
329 tc_h->c_address,
330 GNUNET_TIME_relative_ntoh (msg->expiration),
331 msg->aid,
332 ntohl (msg->nt));
333 }
334 GNUNET_SERVICE_client_continue (client->client);
335} 124}
336 125
337 126
338/** 127static void
339 * Incoming message. Test message is well-formed. 128clear_p2c (void *cls,
340 * 129 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
341 * @param cls the client
342 * @param msg the send message that was sent
343 * @return #GNUNET_OK if message is well-formed
344 */
345static int
346check_incoming_msg (void *cls,
347 const struct GNUNET_TRANSPORT_IncomingMessage *msg)
348{ 130{
349 // struct TransportClient *tc = cls; 131 int *found = cls;
350 132
351 // if (CT_COMMUNICATOR != tc->type) 133 if (NULL != found)
352 // { 134 *found = GNUNET_YES;
353 // GNUNET_break (0); 135 cx->p2_c = GNUNET_NO;
354 // return GNUNET_SYSERR;
355 // }
356 GNUNET_MQ_check_boxed_message (msg);
357 return GNUNET_OK;
358} 136}
359 137
360 138
361/** 139static void *
362 * @brief Receive an incoming message. 140notify_connect (void *cls,
363 * 141 const struct GNUNET_PeerIdentity *peer,
364 * Pass the message to the client. 142 struct GNUNET_MQ_Handle *mq)
365 * 143{
366 * @param cls Closure - communicator handle 144 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
367 * @param msg Message 145 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
368 */ 146 char *p2_s;
369static void 147 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
370handle_incoming_msg (void *cls, 148 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
371 const struct GNUNET_TRANSPORT_IncomingMessage *inc_msg) 149 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
372{ 150 int found;
373 struct MyClient *client = cls; 151 void *ret;
374 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = 152
375 client->tc; 153 p2 = find_peer_context (p->tth,
376 struct GNUNET_MessageHeader *msg; 154 peer);
377 msg = (struct GNUNET_MessageHeader *) &inc_msg[1]; 155 if (NULL != p->nc)
378 size_t payload_len = ntohs (msg->size) - sizeof (struct 156 ret = p->nc (p->cb_cls,
379 GNUNET_MessageHeader); 157 peer,
380 if (NULL != tc_h->incoming_msg_cb) 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)
381 { 184 {
382 tc_h->incoming_msg_cb (tc_h->cb_cls, 185 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
383 tc_h, 186 cc->p1 = p;
384 (char*) &msg[1], 187 cc->p2 = p2;
385 payload_len); 188 cc->p1_c = GNUNET_YES;
189 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
190 tth->cc_tail,
191 cc);
386 } 192 }
387 else 193 found = GNUNET_NO;
194 GNUNET_TRANSPORT_TESTING_find_connecting_context (p2,
195 p,
196 &set_p2c,
197 &found);
198 if (GNUNET_NO == found)
388 { 199 {
389 LOG (GNUNET_ERROR_TYPE_WARNING, 200 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
390 "Incoming message from communicator but no handler!\n"); 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);
391 } 207 }
392 if (GNUNET_YES == ntohl (inc_msg->fc_on)) 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)
393 { 214 {
394 /* send ACK when done to communicator for flow control! */ 215 if (GNUNET_YES == cc->connected)
395 struct GNUNET_MQ_Envelope *env; 216 continue;
396 struct GNUNET_TRANSPORT_IncomingMessageAck *ack; 217 if ((GNUNET_YES == cc->p1_c) &&
397 218 (GNUNET_YES == cc->p2_c))
398 env = GNUNET_MQ_msg (ack, GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG_ACK); 219 {
399 GNUNET_assert (NULL != env); 220 cc->connected = GNUNET_YES;
400 ack->reserved = htonl (0); 221 /* stop trying to connect */
401 ack->fc_id = inc_msg->fc_id; 222 if (NULL != cc->tct)
402 ack->sender = inc_msg->sender; 223 {
403 GNUNET_MQ_send (tc_h->c_mq, env); 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 }
404 } 233 }
405 234 /* then notify application */
406 GNUNET_SERVICE_client_continue (client->client); 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;
407} 246}
408 247
409 248
410/** 249/**
411 * @brief Communicator informs that it tries to establish requested queue 250 * Offer the current HELLO of P2 to P1.
412 * 251 *
413 * @param cls Closure - communicator handle 252 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
414 * @param msg Message
415 */ 253 */
416static void 254static void
417handle_queue_create_ok (void *cls, 255offer_hello (void *cls);
418 const struct GNUNET_TRANSPORT_CreateQueueResponse *msg) 256
419{
420 struct MyClient *client = cls;
421 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h =
422 client->tc;
423 257
424 if (NULL != tc_h->queue_create_reply_cb) 258static void
259notify_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)
425 { 312 {
426 tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_YES); 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 }
427 } 328 }
428 GNUNET_SERVICE_client_continue (client->client);
429} 329}
430 330
431 331
432/**
433 * @brief Communicator informs that it wont try establishing requested queue.
434 *
435 * It will not do so probably because the address is bougus (see comment to
436 * #GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL)
437 *
438 * @param cls Closure - communicator handle
439 * @param msg Message
440 */
441static void 332static void
442handle_queue_create_fail ( 333hello_iter_cb (void *cb_cls,
443 void *cls, 334 const struct GNUNET_PEERSTORE_Record *record,
444 const struct GNUNET_TRANSPORT_CreateQueueResponse *msg) 335 const char *emsg)
445{ 336{
446 struct MyClient *client = cls; 337 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cb_cls;
447 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = 338 //Check record type et al?
448 client->tc; 339 p->hello = GNUNET_malloc (record->value_size);
340 memcpy (p->hello, record->value, record->value_size);
449 341
450 if (NULL != tc_h->queue_create_reply_cb) 342 GNUNET_PEERSTORE_iterate_cancel (p->pic);
343 if (NULL != p->start_cb)
451 { 344 {
452 tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_NO); 345 LOG (GNUNET_ERROR_TYPE_DEBUG,
346 "Peer %u (`%s') successfully started\n",
347 p->no,
348 GNUNET_i2s (&p->id));
349 p->start_cb (p->start_cb_cls);
350 p->start_cb = NULL;
453 } 351 }
454 GNUNET_SERVICE_client_continue (client->client);
455} 352}
456 353
457 354
458/** 355/**
459 * New queue became available. Check message. 356 * Start a peer with the given configuration
460 * 357 * @param tth the testing handle
461 * @param cls the client 358 * @param cfgname configuration file
462 * @param aqm the send message that was sent 359 * @param peer_id a unique number to identify the peer
360 * @param handlers functions for receiving messages
361 * @param nc connect callback
362 * @param nd disconnect callback
363 * @param cb_cls closure for callback
364 * @param start_cb start callback
365 * @param start_cb_cls closure for callback
366 * @return the peer context
463 */ 367 */
464static int 368struct GNUNET_TRANSPORT_TESTING_PeerContext *
465check_add_queue_message (void *cls, 369GNUNET_TRANSPORT_TESTING_start_peer (struct
466 const struct GNUNET_TRANSPORT_AddQueueMessage *aqm) 370 GNUNET_TRANSPORT_TESTING_Handle *tth,
467{ 371 const char *cfgname,
468 GNUNET_MQ_check_zero_termination (aqm); 372 int peer_id,
469 return GNUNET_OK; 373 const struct
470} 374 GNUNET_MQ_MessageHandler *handlers,
471 375 GNUNET_TRANSPORT_NotifyConnect nc,
472 376 GNUNET_TRANSPORT_NotifyDisconnect nd,
473/** 377 void *cb_cls,
474 * @brief Handle new queue 378 GNUNET_SCHEDULER_TaskCallback start_cb,
475 * 379 void *start_cb_cls)
476 * Store context and call client callback. 380{
477 * 381 char *emsg = NULL;
478 * @param cls Closure - communicator handle 382 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
479 * @param msg Message struct 383 struct GNUNET_PeerIdentity dummy;
480 */ 384 unsigned int i;
481static void 385
482handle_add_queue_message (void *cls, 386 if (GNUNET_NO == GNUNET_DISK_file_test (cfgname))
483 const struct GNUNET_TRANSPORT_AddQueueMessage *msg)
484{
485 struct MyClient *client = cls;
486 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h =
487 client->tc;
488 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue;
489
490 LOG (GNUNET_ERROR_TYPE_DEBUG,
491 "Got queue with ID %u\n", msg->qid);
492 for (tc_queue = tc_h->queue_head; NULL != tc_queue; tc_queue = tc_queue->next)
493 { 387 {
494 if (tc_queue->qid == msg->qid) 388 LOG (GNUNET_ERROR_TYPE_ERROR,
495 break; 389 "File not found: `%s'\n",
390 cfgname);
391 return NULL;
496 } 392 }
497 if (NULL == tc_queue) 393
394 p = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_PeerContext);
395 p->tth = tth;
396 p->nc = nc;
397 p->nd = nd;
398 if (NULL != handlers)
498 { 399 {
499 tc_queue = 400 for (i = 0; NULL != handlers[i].cb; i++)
500 GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue); 401 ;
501 tc_queue->tc_h = tc_h; 402 p->handlers = GNUNET_new_array (i + 1,
502 tc_queue->qid = msg->qid; 403 struct GNUNET_MQ_MessageHandler);
503 tc_queue->peer_id = msg->receiver; 404 GNUNET_memcpy (p->handlers,
504 GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue); 405 handlers,
406 i * sizeof(struct GNUNET_MQ_MessageHandler));
505 } 407 }
506 GNUNET_assert (tc_queue->qid == msg->qid); 408 if (NULL != cb_cls)
507 GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver)); 409 p->cb_cls = cb_cls;
508 tc_queue->nt = msg->nt; 410 else
509 tc_queue->mtu = ntohl (msg->mtu); 411 p->cb_cls = p;
510 tc_queue->cs = msg->cs; 412 p->start_cb = start_cb;
511 tc_queue->priority = ntohl (msg->priority); 413 if (NULL != start_cb_cls)
512 tc_queue->q_len = GNUNET_ntohll (msg->q_len); 414 p->start_cb_cls = start_cb_cls;
513 if (NULL != tc_h->add_queue_cb) 415 else
416 p->start_cb_cls = p;
417 GNUNET_CONTAINER_DLL_insert (tth->p_head,
418 tth->p_tail,
419 p);
420
421 /* Create configuration and call testing lib to modify it */
422 p->cfg = GNUNET_CONFIGURATION_create ();
423 GNUNET_assert (GNUNET_OK ==
424 GNUNET_CONFIGURATION_load (p->cfg, cfgname));
425 if (GNUNET_SYSERR ==
426 GNUNET_TESTING_configuration_create (tth->tl_system,
427 p->cfg))
514 { 428 {
515 tc_h->add_queue_cb (tc_h->cb_cls, tc_h, tc_queue, tc_queue->mtu); 429 LOG (GNUNET_ERROR_TYPE_ERROR,
430 "Testing library failed to create unique configuration based on `%s'\n",
431 cfgname);
432 GNUNET_CONFIGURATION_destroy (p->cfg);
433 GNUNET_free (p);
434 return NULL;
516 } 435 }
517 GNUNET_SERVICE_client_continue (client->client);
518}
519 436
437 p->no = peer_id;
438 /* Configure peer with configuration */
439 p->peer = GNUNET_TESTING_peer_configure (tth->tl_system,
440 p->cfg,
441 p->no,
442 NULL,
443 &emsg);
444 if (NULL == p->peer)
445 {
446 LOG (GNUNET_ERROR_TYPE_ERROR,
447 "Testing library failed to create unique configuration based on `%s': `%s'\n",
448 cfgname,
449 emsg);
450 GNUNET_TRANSPORT_TESTING_stop_peer (p);
451 GNUNET_free (emsg);
452 return NULL;
453 }
454 GNUNET_free (emsg);
455 if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
456 {
457 LOG (GNUNET_ERROR_TYPE_ERROR,
458 "Testing library failed to create unique configuration based on `%s'\n",
459 cfgname);
460 GNUNET_TRANSPORT_TESTING_stop_peer (p);
461 return NULL;
462 }
520 463
521/** 464 memset (&dummy,
522 * @brief Handle new queue 465 '\0',
523 * 466 sizeof(dummy));
524 * Store context and call client callback. 467 GNUNET_TESTING_peer_get_identity (p->peer,
525 * 468 &p->id);
526 * @param cls Closure - communicator handle 469 if (0 == memcmp (&dummy,
527 * @param msg Message struct 470 &p->id,
528 */ 471 sizeof(struct GNUNET_PeerIdentity)))
529static void 472 {
530handle_update_queue_message (void *cls, 473 LOG (GNUNET_ERROR_TYPE_ERROR,
531 const struct 474 "Testing library failed to obtain peer identity for peer %u\n",
532 GNUNET_TRANSPORT_UpdateQueueMessage *msg) 475 p->no);
533{ 476 GNUNET_TRANSPORT_TESTING_stop_peer (p);
534 struct MyClient *client = cls; 477 return NULL;
535 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = 478 }
536 client->tc;
537 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue;
538
539 LOG (GNUNET_ERROR_TYPE_DEBUG, 479 LOG (GNUNET_ERROR_TYPE_DEBUG,
540 "Received queue update message for %u with q_len %" PRIu64 "\n", 480 "Peer %u configured with identity `%s'\n",
541 msg->qid, GNUNET_ntohll (msg->q_len)); 481 p->no,
542 tc_queue = tc_h->queue_head; 482 GNUNET_i2s_full (&p->id));
543 if (NULL != tc_queue) 483 p->tmh = GNUNET_TRANSPORT_manipulation_connect (p->cfg);
484 p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
485 NULL,
486 handlers,
487 p,
488 &notify_connect,
489 &notify_disconnect);
490 if ((NULL == p->th) ||
491 (NULL == p->tmh))
544 { 492 {
545 while (tc_queue->qid != msg->qid) 493 LOG (GNUNET_ERROR_TYPE_ERROR,
546 { 494 "Failed to connect to transport service for peer `%s': `%s'\n",
547 tc_queue = tc_queue->next; 495 cfgname,
548 } 496 emsg);
497 GNUNET_TRANSPORT_TESTING_stop_peer (p);
498 return NULL;
499 }
500 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
501 if (NULL == p->ats)
502 {
503 LOG (GNUNET_ERROR_TYPE_ERROR,
504 "Failed to connect to ATS service for peer `%s': `%s'\n",
505 cfgname,
506 emsg);
507 GNUNET_TRANSPORT_TESTING_stop_peer (p);
508 return NULL;
549 } 509 }
550 GNUNET_assert (tc_queue->qid == msg->qid); 510 p->pic = GNUNET_PEERSTORE_iterate (p->ph,
551 GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver)); 511 "transport",
552 tc_queue->nt = msg->nt; 512 &p->id,
553 tc_queue->mtu = ntohl (msg->mtu); 513 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY,
554 tc_queue->cs = msg->cs; 514 hello_iter_cb,
555 tc_queue->priority = ntohl (msg->priority); 515 p);
556 // Uncomment this for alternativ 1 of backchannel functionality 516 GNUNET_assert (NULL != p->pic);
557 tc_queue->q_len += GNUNET_ntohll (msg->q_len); 517
558 // Until here for alternativ 1 518 return p;
559 // Uncomment this for alternativ 2 of backchannel functionality
560 // tc_queue->q_len = GNUNET_ntohll (msg->q_len);
561 // Until here for alternativ 2
562 GNUNET_SERVICE_client_continue (client->client);
563} 519}
564 520
565 521
566/** 522/**
567 * @brief Shut down the service 523 * Stops and restarts the given peer, sleeping (!) for 5s in between.
568 * 524 *
569 * @param cls Closure - Handle to the service 525 * @param p the peer
526 * @param restart_cb callback to call when restarted
527 * @param restart_cb_cls callback closure
528 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
570 */ 529 */
571static void 530int
572shutdown_service (void *cls) 531GNUNET_TRANSPORT_TESTING_restart_peer (struct
532 GNUNET_TRANSPORT_TESTING_PeerContext *p,
533 GNUNET_SCHEDULER_TaskCallback restart_cb,
534 void *restart_cb_cls)
573{ 535{
574 struct GNUNET_SERVICE_Handle *h = cls; 536 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
537 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
575 538
539 /* shutdown */
576 LOG (GNUNET_ERROR_TYPE_DEBUG, 540 LOG (GNUNET_ERROR_TYPE_DEBUG,
577 "Shutting down service!\n"); 541 "Stopping peer %u (`%s')\n",
578 542 p->no,
579 GNUNET_SERVICE_stop (h); 543 GNUNET_i2s (&p->id));
580} 544 if (NULL != p->pic)
581
582
583/**
584 * @brief Callback called when new Client (Communicator) connects
585 *
586 * @param cls Closure - TransporCommmunicator Handle
587 * @param client Client
588 * @param mq Messagequeue
589 *
590 * @return TransportCommunicator Handle
591 */
592static void *
593connect_cb (void *cls,
594 struct GNUNET_SERVICE_Client *client,
595 struct GNUNET_MQ_Handle *mq)
596{
597 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
598 struct MyClient *new_c;
599
600 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected to %p.\n",
601 client, tc_h);
602 new_c = GNUNET_new (struct MyClient);
603 new_c->client = client;
604 new_c->c_mq = mq;
605 new_c->tc = tc_h;
606 GNUNET_CONTAINER_DLL_insert (tc_h->client_head,
607 tc_h->client_tail,
608 new_c);
609
610 if (NULL == tc_h->queue_head)
611 return new_c;
612 /* Iterate over queues. They are yet to be opened. Request opening. */
613 for (struct
614 GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_iter =
615 tc_h->queue_head;
616 NULL != tc_queue_iter;
617 tc_queue_iter = tc_queue_iter->next)
618 { 545 {
619 if (NULL == tc_queue_iter->open_queue_env) 546 GNUNET_PEERSTORE_iterate_cancel (p->pic);
620 continue; 547 p->pic = NULL;
621 /* Send the previously created mq envelope to request the creation of the 548 }
622 * queue. */ 549 if (NULL != p->th)
623 GNUNET_MQ_send (tc_h->c_mq, 550 {
624 tc_queue_iter->open_queue_env); 551 GNUNET_TRANSPORT_core_disconnect (p->th);
625 tc_queue_iter->open_queue_env = NULL; 552 p->th = NULL;
553 }
554 if (NULL != p->tmh)
555 {
556 GNUNET_TRANSPORT_manipulation_disconnect (p->tmh);
557 p->tmh = NULL;
558 }
559 for (cc = p->tth->cc_head; NULL != cc; cc = ccn)
560 {
561 ccn = cc->next;
562 if ((cc->p1 == p) ||
563 (cc->p2 == p))
564 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
565 }
566 if (NULL != p->ats)
567 {
568 GNUNET_ATS_connectivity_done (p->ats);
569 p->ats = NULL;
570 }
571 if (GNUNET_SYSERR ==
572 GNUNET_TESTING_peer_stop (p->peer))
573 {
574 LOG (GNUNET_ERROR_TYPE_ERROR,
575 "Failed to stop peer %u (`%s')\n",
576 p->no,
577 GNUNET_i2s (&p->id));
578 return GNUNET_SYSERR;
626 } 579 }
627 return new_c;
628}
629
630 580
631/** 581 sleep (5); // YUCK!
632 * @brief Callback called when Client disconnects
633 *
634 * @param cls Closure - TransportCommunicator Handle
635 * @param client Client
636 * @param internal_cls TransporCommmunicator Handle
637 */
638static void
639disconnect_cb (void *cls,
640 struct GNUNET_SERVICE_Client *client,
641 void *internal_cls)
642{
643 struct MyClient *cl = cls;
644 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
645 582
646 for (cl = tc_h->client_head; NULL != cl; cl = cl->next) 583 LOG (GNUNET_ERROR_TYPE_DEBUG,
584 "Restarting peer %u (`%s')\n",
585 p->no,
586 GNUNET_i2s (&p->id));
587 /* restart */
588 if (GNUNET_SYSERR == GNUNET_TESTING_peer_start (p->peer))
647 { 589 {
648 if (cl->client != client) 590 LOG (GNUNET_ERROR_TYPE_ERROR,
649 continue; 591 "Failed to restart peer %u (`%s')\n",
650 GNUNET_CONTAINER_DLL_remove (tc_h->client_head, 592 p->no,
651 tc_h->client_tail, 593 GNUNET_i2s (&p->id));
652 cl); 594 return GNUNET_SYSERR;
653 if (cl->c_mq == tc_h->c_mq)
654 tc_h->c_mq = NULL;
655 GNUNET_free (cl);
656 break;
657 } 595 }
658 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected.\n");
659}
660 596
661 597 GNUNET_assert (NULL == p->start_cb);
662/** 598 p->start_cb = restart_cb;
663 * Message was transmitted. Process the request. 599 p->start_cb_cls = restart_cb_cls;
664 * 600
665 * @param cls the client 601 p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
666 * @param sma the send message that was sent 602 NULL,
667 */ 603 p->handlers,
668static void 604 p,
669handle_send_message_ack (void *cls, 605 &notify_connect,
670 const struct GNUNET_TRANSPORT_SendMessageToAck *sma) 606 &notify_disconnect);
671{ 607 GNUNET_assert (NULL != p->th);
672 struct MyClient *client = cls; 608 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
673 GNUNET_SERVICE_client_continue (client->client); 609 p->pic = GNUNET_PEERSTORE_iterate (p->ph,
674 // NOP 610 "transport",
611 &p->id,
612 GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY,
613 hello_iter_cb,
614 p);
615 GNUNET_assert (NULL != p->pic);
616 return GNUNET_OK;
675} 617}
676 618
677 619
678/** 620/**
679 * @brief Start the communicator part of the transport service 621 * Shutdown the given peer
680 * 622 *
681 * @param communicator_available Callback to be called when a new communicator 623 * @param p the peer
682 * becomes available
683 * @param cfg Configuration
684 */ 624 */
685static void 625void
686transport_communicator_start ( 626GNUNET_TRANSPORT_TESTING_stop_peer (struct
687 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) 627 GNUNET_TRANSPORT_TESTING_PeerContext *p)
688{ 628{
689 struct GNUNET_MQ_MessageHandler mh[] = { 629 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
690 GNUNET_MQ_hd_var_size (communicator_available, 630 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
691 GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR, 631 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
692 struct GNUNET_TRANSPORT_CommunicatorAvailableMessage,
693 tc_h),
694 GNUNET_MQ_hd_var_size (communicator_backchannel,
695 GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL,
696 struct GNUNET_TRANSPORT_CommunicatorBackchannel,
697 tc_h),
698 GNUNET_MQ_hd_var_size (add_address,
699 GNUNET_MESSAGE_TYPE_TRANSPORT_ADD_ADDRESS,
700 struct GNUNET_TRANSPORT_AddAddressMessage,
701 tc_h),
702 // GNUNET_MQ_hd_fixed_size (del_address,
703 // GNUNET_MESSAGE_TYPE_TRANSPORT_DEL_ADDRESS,
704 // struct GNUNET_TRANSPORT_DelAddressMessage,
705 // NULL),
706 GNUNET_MQ_hd_var_size (incoming_msg,
707 GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG,
708 struct GNUNET_TRANSPORT_IncomingMessage,
709 tc_h),
710 GNUNET_MQ_hd_fixed_size (queue_create_ok,
711 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_OK,
712 struct GNUNET_TRANSPORT_CreateQueueResponse,
713 tc_h),
714 GNUNET_MQ_hd_fixed_size (queue_create_fail,
715 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL,
716 struct GNUNET_TRANSPORT_CreateQueueResponse,
717 tc_h),
718 GNUNET_MQ_hd_var_size (add_queue_message,
719 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_SETUP,
720 struct GNUNET_TRANSPORT_AddQueueMessage,
721 tc_h),
722 GNUNET_MQ_hd_fixed_size (update_queue_message,
723 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_UPDATE,
724 struct GNUNET_TRANSPORT_UpdateQueueMessage,
725 tc_h),
726 // GNUNET_MQ_hd_fixed_size (del_queue_message,
727 // GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_TEARDOWN,
728 // struct GNUNET_TRANSPORT_DelQueueMessage,
729 // NULL),
730 GNUNET_MQ_hd_fixed_size (send_message_ack,
731 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG_ACK,
732 struct GNUNET_TRANSPORT_SendMessageToAck,
733 tc_h),
734 GNUNET_MQ_handler_end ()
735 };
736
737
738 tc_h->sh = GNUNET_SERVICE_start ("transport",
739 tc_h->cfg,
740 &connect_cb,
741 &disconnect_cb,
742 tc_h,
743 mh);
744 GNUNET_assert (NULL != tc_h->sh);
745}
746
747 632
748/** 633 for (cc = tth->cc_head; NULL != cc; cc = ccn)
749 * @brief Task run at shutdown to kill communicator and clean up 634 {
750 * 635 ccn = cc->next;
751 * @param cls Closure - Process of communicator 636 if ((cc->p1 == p) ||
752 */ 637 (cc->p2 == p))
753static void 638 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
754shutdown_process (struct GNUNET_OS_Process *proc) 639 }
755{ 640 if (NULL != p->pic)
756 if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) 641 {
642 GNUNET_PEERSTORE_iterate_cancel (p->pic);
643 p->pic = NULL;
644 }
645 if (NULL != p->tmh)
646 {
647 GNUNET_TRANSPORT_manipulation_disconnect (p->tmh);
648 p->tmh = NULL;
649 }
650 if (NULL != p->th)
651 {
652 GNUNET_TRANSPORT_core_disconnect (p->th);
653 p->th = NULL;
654 }
655 if (NULL != p->peer)
757 { 656 {
758 LOG (GNUNET_ERROR_TYPE_WARNING, 657 if (GNUNET_OK !=
759 "Error shutting down process with SIGERM, trying SIGKILL\n"); 658 GNUNET_TESTING_peer_stop (p->peer))
760 if (0 != GNUNET_OS_process_kill (proc, SIGKILL))
761 { 659 {
762 LOG (GNUNET_ERROR_TYPE_ERROR, 660 LOG (GNUNET_ERROR_TYPE_DEBUG,
763 "Error shutting down process with SIGERM and SIGKILL\n"); 661 "Testing lib failed to stop peer %u (`%s')\n",
662 p->no,
663 GNUNET_i2s (&p->id));
764 } 664 }
665 GNUNET_TESTING_peer_destroy (p->peer);
666 p->peer = NULL;
765 } 667 }
766 GNUNET_OS_process_destroy (proc); 668 if (NULL != p->ats)
767}
768
769static void
770shutdown_peerstore (void *cls)
771{
772 struct GNUNET_OS_Process *proc = cls;
773 shutdown_process (proc);
774}
775
776static void
777shutdown_communicator (void *cls)
778{
779 struct GNUNET_OS_Process *proc = cls;
780 shutdown_process (proc);
781}
782
783
784/**
785 * @brief Start the communicator
786 *
787 * @param cfgname Name of the communicator
788 */
789static void
790communicator_start (
791 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
792 const char *binary_name)
793{
794 char *binary;
795 char *loprefix;
796 char *section_name;
797
798 LOG (GNUNET_ERROR_TYPE_DEBUG, "communicator_start\n");
799
800 section_name = strchr (binary_name, '-');
801 section_name++;
802
803 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (tc_h->cfg,
804 section_name,
805 "PREFIX",
806 &loprefix))
807 loprefix = GNUNET_strdup ("");
808
809
810 binary = GNUNET_OS_get_libexec_binary_path (binary_name);
811 tc_h->c_proc = GNUNET_OS_start_process_s (GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
812 NULL,
813 loprefix,
814 binary,
815 binary_name,
816 "-c",
817 tc_h->cfg_filename,
818 NULL);
819 if (NULL == tc_h->c_proc)
820 { 669 {
821 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start communicator!"); 670 GNUNET_ATS_connectivity_done (p->ats);
822 return; 671 p->ats = NULL;
672 }
673 if (NULL != p->hello)
674 {
675 GNUNET_free (p->hello);
676 p->hello = NULL;
677 }
678 if (NULL != p->cfg)
679 {
680 GNUNET_CONFIGURATION_destroy (p->cfg);
681 p->cfg = NULL;
823 } 682 }
824 LOG (GNUNET_ERROR_TYPE_INFO, "started communicator\n"); 683 if (NULL != p->handlers)
825 GNUNET_free (binary); 684 {
685 GNUNET_free (p->handlers);
686 p->handlers = NULL;
687 }
688 GNUNET_CONTAINER_DLL_remove (tth->p_head,
689 tth->p_tail,
690 p);
691 LOG (GNUNET_ERROR_TYPE_DEBUG,
692 "Peer %u (`%s') stopped\n",
693 p->no,
694 GNUNET_i2s (&p->id));
695 GNUNET_free (p);
826} 696}
827 697
828 698
829/** 699/**
830 * @brief Task run at shutdown to kill communicator and clean up 700 * Function called after the HELLO was passed to the
831 * 701 * transport service.
832 * @param cls Closure - Process of communicator 702 * FIXME maybe schedule the application_validate somehow
833 */ 703 */
704/*
834static void 705static void
835shutdown_nat (void *cls) 706hello_offered (void *cls)
836{ 707{
837 struct GNUNET_OS_Process *proc = cls; 708 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
838 shutdown_process (proc); 709
839} 710 cc->oh = NULL;
711 cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
712 &offer_hello,
713 cc);
714}*/
840 715
841 716
842/** 717/**
843 * @brief Task run at shutdown to kill the resolver process 718 * Offer the current HELLO of P2 to P1.
844 * 719 *
845 * @param cls Closure - Process of communicator 720 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
846 */ 721 */
847static void 722static void
848shutdown_resolver (void *cls) 723offer_hello (void *cls)
849{ 724{
850 struct GNUNET_OS_Process *proc = cls; 725 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
851 shutdown_process (proc); 726 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1 = cc->p1;
852} 727 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = cc->p2;
853 728 struct GNUNET_TIME_Absolute t;
729 enum GNUNET_NetworkType nt;
730 char *addr;
854 731
855static void 732 cc->tct = NULL;
856resolver_start (struct
857 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
858{
859 char *binary;
860
861 LOG (GNUNET_ERROR_TYPE_DEBUG, "resolver_start\n");
862 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
863 tc_h->resolver_proc = GNUNET_OS_start_process (
864 GNUNET_OS_INHERIT_STD_OUT_AND_ERR
865 | GNUNET_OS_USE_PIPE_CONTROL,
866 NULL,
867 NULL,
868 NULL,
869 binary,
870 "gnunet-service-resolver",
871 "-c",
872 tc_h->cfg_filename,
873 NULL);
874 if (NULL == tc_h->resolver_proc)
875 { 733 {
876 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start resolver service!"); 734 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
877 return; 735
736 LOG (GNUNET_ERROR_TYPE_DEBUG,
737 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %s\n",
738 p1->no,
739 GNUNET_i2s (&p1->id),
740 p2->no,
741 p2_s,
742 cc->p2->hello);
743 GNUNET_free (p2_s);
878 } 744 }
879 LOG (GNUNET_ERROR_TYPE_INFO, "started resolver service\n");
880 GNUNET_free (binary);
881 745
746 addr = GNUNET_HELLO_extract_address (cc->p2->hello,
747 cc->p2->hello_size,
748 &cc->p2->id,
749 &nt,
750 &t);
751 GNUNET_assert (NULL == addr);
752 GNUNET_TRANSPORT_application_validate (cc->p1->ah,
753 &cc->p2->id,
754 nt,
755 addr);
756 GNUNET_free (addr);
882} 757}
883 758
884 759
885/** 760/**
886 * @brief Start Peerstore 761 * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message
762 *
763 * Remarks: start_peer's notify_connect callback can be called before.
887 * 764 *
765 * @param tth transport testing handle
766 * @param p1 peer 1
767 * @param p2 peer 2
768 * @param cb the callback to call when both peers notified that they are connected
769 * @param cls callback cls
770 * @return a connect request handle
888 */ 771 */
889static void 772struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
890peerstore_start ( 773GNUNET_TRANSPORT_TESTING_connect_peers (struct
891 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) 774 GNUNET_TRANSPORT_TESTING_PeerContext *p1,
892{ 775 struct
893 char *binary; 776 GNUNET_TRANSPORT_TESTING_PeerContext *p2,
894 777 GNUNET_SCHEDULER_TaskCallback cb,
895 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-peerstore"); 778 void *cls)
896 tc_h->ps_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 779{
897 NULL, 780 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth;
898 NULL, 781 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
899 NULL, 782 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
900 binary, 783
901 "gnunet-service-peerstore", 784 ccn = NULL;
902 "-c", 785 for (cc = tth->cc_head; NULL != cc; cc = cc->next)
903 tc_h->cfg_filename,
904 NULL);
905 if (NULL == tc_h->ps_proc)
906 { 786 {
907 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start Peerstore!"); 787 if ((cc->p1 == p1) &&
908 return; 788 (cc->p2 == p2))
789 {
790 ccn = cc;
791 break;
792 }
909 } 793 }
910 LOG (GNUNET_ERROR_TYPE_INFO, "started Peerstore\n");
911 GNUNET_free (binary);
912}
913 794
914/** 795 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
915 * @brief Start NAT 796 cc->p1 = p1;
916 * 797 cc->p2 = p2;
917 */ 798 cc->cb = cb;
918static void 799 if (NULL != cls)
919nat_start ( 800 cc->cb_cls = cls;
920 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) 801 else
921{ 802 cc->cb_cls = cc;
922 char *binary; 803 if (NULL != ccn)
923
924 LOG (GNUNET_ERROR_TYPE_DEBUG, "nat_start\n");
925 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-nat");
926 tc_h->nat_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR
927 | GNUNET_OS_USE_PIPE_CONTROL,
928 NULL,
929 NULL,
930 NULL,
931 binary,
932 "gnunet-service-nat",
933 "-c",
934 tc_h->cfg_filename,
935 NULL);
936 if (NULL == tc_h->nat_proc)
937 { 804 {
938 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start NAT!"); 805 cc->p1_c = ccn->p1_c;
939 return; 806 cc->p2_c = ccn->p2_c;
807 cc->connected = ccn->connected;
940 } 808 }
941 LOG (GNUNET_ERROR_TYPE_INFO, "started NAT\n"); 809 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
942 GNUNET_free (binary); 810 tth->cc_tail,
811 cc);
812 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
813 cc);
814 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
815 &p2->id,
816 1);
817 LOG (GNUNET_ERROR_TYPE_DEBUG,
818 "New connect request %p\n",
819 cc);
820 return cc;
943} 821}
944 822
945 823
946/** 824/**
947 * @brief Start communicator part of transport service and communicator 825 * Cancel the request to connect two peers
948 * 826 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
949 * @param service_name Name of the service
950 * @param cfg Configuration handle
951 * @param communicator_available_cb Callback that is called when a new
952 * @param add_address_cb Callback that is called when a new
953 * communicator becomes available
954 * @param cb_cls Closure to @a communicator_available_cb and @a
955 * 827 *
956 * @return Handle to the communicator duo 828 * @param tth transport testing handle
829 * @param cc a connect request handle
957 */ 830 */
958struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle * 831void
959GNUNET_TRANSPORT_TESTING_transport_communicator_service_start ( 832GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
960 const char *service_name, 833 GNUNET_TRANSPORT_TESTING_ConnectRequest
961 const char *binary_name, 834 *cc)
962 const char *cfg_filename,
963 const struct GNUNET_PeerIdentity *peer_id,
964 GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback
965 communicator_available_cb,
966 GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb,
967 GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb,
968 GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb,
969 GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_message_cb,
970 GNUNET_TRANSPORT_TESTING_BackchannelCallback bc_cb,
971 void *cb_cls)
972{ 835{
973 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h; 836 struct GNUNET_TRANSPORT_TESTING_Handle *tth = cc->p1->tth;
974 837
975 LOG (GNUNET_ERROR_TYPE_DEBUG, 838 LOG (GNUNET_ERROR_TYPE_DEBUG,
976 "Starting new transport/communicator combo with config %s\n", 839 "Canceling connect request!\n");
977 cfg_filename); 840 if (NULL != cc->tct)
978 tc_h =
979 GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle);
980 tc_h->cfg_filename = GNUNET_strdup (cfg_filename);
981 tc_h->cfg = GNUNET_CONFIGURATION_create ();
982 if ((GNUNET_SYSERR == GNUNET_CONFIGURATION_load (tc_h->cfg, cfg_filename)))
983 { 841 {
984 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 842 GNUNET_SCHEDULER_cancel (cc->tct);
985 _ ("Malformed configuration file `%s', exit ...\n"), 843 cc->tct = NULL;
986 cfg_filename);
987 GNUNET_free (tc_h->cfg_filename);
988 GNUNET_CONFIGURATION_destroy (tc_h->cfg);
989 GNUNET_free (tc_h);
990 return NULL;
991 } 844 }
992 tc_h->bc_enabled = GNUNET_CONFIGURATION_get_value_yesno (tc_h->cfg, 845 if (NULL != cc->ats_sh)
993 "communicator-test", 846 {
994 "BACKCHANNEL_ENABLED"); 847 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
995 tc_h->communicator_available_cb = communicator_available_cb; 848 cc->ats_sh = NULL;
996 tc_h->add_address_cb = add_address_cb; 849 }
997 tc_h->queue_create_reply_cb = queue_create_reply_cb; 850 GNUNET_CONTAINER_DLL_remove (tth->cc_head,
998 tc_h->add_queue_cb = add_queue_cb; 851 tth->cc_tail,
999 tc_h->incoming_msg_cb = incoming_message_cb; 852 cc);
1000 tc_h->bc_cb = bc_cb; 853 GNUNET_free (cc);
1001 tc_h->peer_id = *peer_id;
1002 tc_h->cb_cls = cb_cls;
1003
1004 /* Start communicator part of service */
1005 transport_communicator_start (tc_h);
1006 /* Start NAT */
1007 nat_start (tc_h);
1008 /* Start resolver service */
1009 resolver_start (tc_h);
1010 /* Start peerstore service */
1011 peerstore_start (tc_h);
1012 /* Schedule start communicator */
1013 communicator_start (tc_h,
1014 binary_name);
1015 return tc_h;
1016}
1017
1018
1019void
1020GNUNET_TRANSPORT_TESTING_transport_communicator_service_stop (
1021 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
1022{
1023 shutdown_communicator (tc_h->c_proc);
1024 shutdown_service (tc_h->sh);
1025 shutdown_nat (tc_h->nat_proc);
1026 shutdown_resolver (tc_h->resolver_proc);
1027 shutdown_peerstore (tc_h->ps_proc);
1028 GNUNET_CONFIGURATION_destroy (tc_h->cfg);
1029 GNUNET_free (tc_h);
1030} 854}
1031 855
1032 856
1033/** 857/**
1034 * @brief Instruct communicator to open a queue 858 * Clean up the transport testing
1035 * 859 *
1036 * @param tc_h Handle to communicator which shall open queue 860 * @param tth transport testing handle
1037 * @param peer_id Towards which peer
1038 * @param address For which address
1039 */ 861 */
1040void 862void
1041GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue ( 863GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth)
1042 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
1043 const struct GNUNET_PeerIdentity *peer_id,
1044 const char *address)
1045{ 864{
1046 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; 865 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
1047 static uint32_t idgen; 866 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct;
1048 char *prefix; 867 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
1049 struct GNUNET_TRANSPORT_CreateQueue *msg; 868 struct GNUNET_TRANSPORT_TESTING_PeerContext *t;
1050 struct GNUNET_MQ_Envelope *env; 869
1051 size_t alen; 870 if (NULL == tth)
1052
1053 tc_queue =
1054 GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue);
1055 tc_queue->tc_h = tc_h;
1056 prefix = GNUNET_HELLO_address_to_prefix (address);
1057 if (NULL == prefix)
1058 {
1059 GNUNET_break (0); /* We got an invalid address!? */
1060 GNUNET_free (tc_queue);
1061 return; 871 return;
1062 } 872 cc = tth->cc_head;
1063 GNUNET_free (prefix); 873 while (NULL != cc)
1064 alen = strlen (address) + 1;
1065 env =
1066 GNUNET_MQ_msg_extra (msg, alen, GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE);
1067 msg->request_id = htonl (idgen++);
1068 tc_queue->qid = msg->request_id;
1069 msg->receiver = *peer_id;
1070 tc_queue->peer_id = *peer_id;
1071 memcpy (&msg[1], address, alen);
1072 if (NULL != tc_h->c_mq)
1073 { 874 {
1074 LOG (GNUNET_ERROR_TYPE_DEBUG, 875 ct = cc->next;
1075 "Sending queue create immediately\n"); 876 LOG (GNUNET_ERROR_TYPE_ERROR,
1076 GNUNET_MQ_send (tc_h->c_mq, env); 877 "Developer forgot to cancel connect request!\n");
878 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
879 cc = ct;
1077 } 880 }
1078 else 881 p = tth->p_head;
882 while (NULL != p)
1079 { 883 {
1080 tc_queue->open_queue_env = env; 884 t = p->next;
885 LOG (GNUNET_ERROR_TYPE_ERROR,
886 "Developer forgot to stop peer!\n");
887 GNUNET_TRANSPORT_TESTING_stop_peer (p);
888 p = t;
1081 } 889 }
1082 GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue); 890 GNUNET_TESTING_system_destroy (tth->tl_system,
891 GNUNET_YES);
892
893 GNUNET_free (tth);
1083} 894}
1084 895
1085 896
1086/** 897/**
1087 * @brief Instruct communicator to send data 898 * Initialize the transport testing
1088 * 899 *
1089 * @param tc_queue The queue to use for sending 900 * @return transport testing handle
1090 * @param cont function to call when done sending
1091 * @param cont_cls closure for @a cont
1092 * @param payload Data to send
1093 * @param payload_size Size of the @a payload
1094 */ 901 */
1095void 902struct GNUNET_TRANSPORT_TESTING_Handle *
1096GNUNET_TRANSPORT_TESTING_transport_communicator_send 903GNUNET_TRANSPORT_TESTING_init ()
1097 (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
1098 GNUNET_SCHEDULER_TaskCallback cont,
1099 void *cont_cls,
1100 const void *payload,
1101 size_t payload_size)
1102{ 904{
1103 struct GNUNET_MessageHeader *mh; 905 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
1104 struct GNUNET_TRANSPORT_SendMessageTo *msg; 906
1105 struct GNUNET_MQ_Envelope *env; 907 tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_Handle);
1106 size_t inbox_size; 908 tth->tl_system = GNUNET_TESTING_system_create ("transport-testing",
1107 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; 909 NULL,
1108 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_tmp; 910 NULL,
1109 911 NULL);
1110 tc_queue = NULL; 912 if (NULL == tth->tl_system)
1111 for (tc_queue_tmp = tc_h->queue_head;
1112 NULL != tc_queue_tmp;
1113 tc_queue_tmp = tc_queue_tmp->next)
1114 { 913 {
1115 if (tc_queue_tmp->q_len <= 0) 914 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1116 continue; 915 "Failed to initialize testing library!\n");
1117 if (NULL == tc_queue) 916 GNUNET_free (tth);
1118 { 917 return NULL;
1119 LOG (GNUNET_ERROR_TYPE_DEBUG,
1120 "Selecting queue with prio %u, len %" PRIu64 " and MTU %u\n",
1121 tc_queue_tmp->priority,
1122 tc_queue_tmp->q_len,
1123 tc_queue_tmp->mtu);
1124 tc_queue = tc_queue_tmp;
1125 continue;
1126 }
1127 if (tc_queue->priority < tc_queue_tmp->priority)
1128 {
1129 LOG (GNUNET_ERROR_TYPE_DEBUG,
1130 "Selecting queue with prio %u, len %" PRIu64 " and MTU %u\n",
1131 tc_queue_tmp->priority,
1132 tc_queue_tmp->q_len,
1133 tc_queue_tmp->mtu);
1134 tc_queue = tc_queue_tmp;
1135 }
1136 } 918 }
1137 GNUNET_assert (NULL != tc_queue); 919 return tth;
1138 // Uncomment this for alternativ 1 of backchannel functionality
1139 if (tc_queue->q_len != GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED)
1140 tc_queue->q_len--;
1141 // Until here for alternativ 1
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143 "Sending message\n");
1144 inbox_size = sizeof (struct GNUNET_MessageHeader) + payload_size;
1145 env = GNUNET_MQ_msg_extra (msg,
1146 inbox_size,
1147 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG);
1148 GNUNET_assert (NULL != env);
1149 msg->qid = htonl (tc_queue->qid);
1150 msg->mid = tc_queue->mid++;
1151 msg->receiver = tc_queue->peer_id;
1152 mh = (struct GNUNET_MessageHeader *) &msg[1];
1153 mh->size = htons (inbox_size);
1154 mh->type = GNUNET_MESSAGE_TYPE_DUMMY;
1155 memcpy (&mh[1],
1156 payload,
1157 payload_size);
1158 if (NULL != cont)
1159 GNUNET_MQ_notify_sent (env,
1160 cont,
1161 cont_cls);
1162 GNUNET_MQ_send (tc_queue->tc_h->c_mq,
1163 env);
1164} 920}
921
922
923/* end of transport-testing.c */