diff options
Diffstat (limited to 'src/transport/transport-testing-communicator.c')
-rw-r--r-- | src/transport/transport-testing-communicator.c | 1227 |
1 files changed, 0 insertions, 1227 deletions
diff --git a/src/transport/transport-testing-communicator.c b/src/transport/transport-testing-communicator.c deleted file mode 100644 index ce4af01f2..000000000 --- a/src/transport/transport-testing-communicator.c +++ /dev/null | |||
@@ -1,1227 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2019 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 transport/transport-testing-communicator.c | ||
23 | * @brief functions related to testing-tng | ||
24 | * @author Christian Grothoff | ||
25 | * @author Julius Bünger | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_protocols.h" | ||
30 | #include "gnunet_constants.h" | ||
31 | #include "transport-testing-communicator.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 | |||
40 | struct 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 | */ | ||
64 | struct 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 | |||
131 | |||
132 | /** | ||
133 | * @brief Handle/Context to a single transmission | ||
134 | */ | ||
135 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission | ||
136 | { | ||
137 | }; | ||
138 | |||
139 | |||
140 | /** | ||
141 | * @brief Check whether incoming msg indicating available communicator is | ||
142 | * correct | ||
143 | * | ||
144 | * @param cls Closure | ||
145 | * @param msg Message struct | ||
146 | * | ||
147 | * @return GNUNET_YES in case message is correct | ||
148 | */ | ||
149 | static int | ||
150 | check_communicator_available ( | ||
151 | void *cls, | ||
152 | const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg) | ||
153 | { | ||
154 | uint16_t size; | ||
155 | |||
156 | size = ntohs (msg->header.size) - sizeof(*msg); | ||
157 | if (0 == size) | ||
158 | return GNUNET_OK; /* receive-only communicator */ | ||
159 | GNUNET_MQ_check_zero_termination (msg); | ||
160 | return GNUNET_OK; | ||
161 | } | ||
162 | |||
163 | |||
164 | /** | ||
165 | * @brief Handle new communicator | ||
166 | * | ||
167 | * Store characteristics of communicator, call respective client callback. | ||
168 | * | ||
169 | * @param cls Closure - communicator handle | ||
170 | * @param msg Message struct | ||
171 | */ | ||
172 | static void | ||
173 | handle_communicator_available ( | ||
174 | void *cls, | ||
175 | const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg) | ||
176 | { | ||
177 | struct MyClient *client = cls; | ||
178 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
179 | client->tc; | ||
180 | uint16_t size; | ||
181 | tc_h->c_mq = client->c_mq; | ||
182 | |||
183 | size = ntohs (msg->header.size) - sizeof(*msg); | ||
184 | if (0 == size) | ||
185 | { | ||
186 | GNUNET_SERVICE_client_continue (client->client); | ||
187 | return; /* receive-only communicator */ | ||
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 | { | ||
193 | LOG (GNUNET_ERROR_TYPE_DEBUG, "calling communicator_available_cb()\n"); | ||
194 | tc_h->communicator_available_cb (tc_h->cb_cls, | ||
195 | tc_h, | ||
196 | tc_h->c_characteristics, | ||
197 | tc_h->c_addr_prefix); | ||
198 | } | ||
199 | GNUNET_SERVICE_client_continue (client->client); | ||
200 | LOG (GNUNET_ERROR_TYPE_DEBUG, "finished communicator_available_cb()\n"); | ||
201 | |||
202 | } | ||
203 | |||
204 | |||
205 | /** | ||
206 | * Incoming message. Test message is well-formed. | ||
207 | * | ||
208 | * @param cls the client | ||
209 | * @param msg the send message that was sent | ||
210 | * @return #GNUNET_OK if message is well-formed | ||
211 | */ | ||
212 | static int | ||
213 | check_communicator_backchannel (void *cls, | ||
214 | const struct | ||
215 | GNUNET_TRANSPORT_CommunicatorBackchannel *msg) | ||
216 | { | ||
217 | // struct TransportClient *tc = cls; | ||
218 | |||
219 | // if (CT_COMMUNICATOR != tc->type) | ||
220 | // { | ||
221 | // GNUNET_break (0); | ||
222 | // return GNUNET_SYSERR; | ||
223 | // } | ||
224 | // GNUNET_MQ_check_boxed_message (msg); | ||
225 | return GNUNET_OK; | ||
226 | } | ||
227 | |||
228 | |||
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 | */ | ||
237 | static void | ||
238 | handle_communicator_backchannel (void *cls, | ||
239 | const struct | ||
240 | GNUNET_TRANSPORT_CommunicatorBackchannel * | ||
241 | bc_msg) | ||
242 | { | ||
243 | struct MyClient *client = 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 | |||
254 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
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 | } | ||
280 | |||
281 | |||
282 | /** | ||
283 | * Address of our peer added. Test message is well-formed. | ||
284 | * | ||
285 | * @param cls the client | ||
286 | * @param aam the send message that was sent | ||
287 | * @return #GNUNET_OK if message is well-formed | ||
288 | */ | ||
289 | static int | ||
290 | check_add_address (void *cls, | ||
291 | const struct GNUNET_TRANSPORT_AddAddressMessage *msg) | ||
292 | { | ||
293 | // if (CT_COMMUNICATOR != tc->type) | ||
294 | // { | ||
295 | // GNUNET_break (0); | ||
296 | // return GNUNET_SYSERR; | ||
297 | // } | ||
298 | GNUNET_MQ_check_zero_termination (msg); | ||
299 | return GNUNET_OK; | ||
300 | } | ||
301 | |||
302 | |||
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 | */ | ||
311 | static void | ||
312 | handle_add_address (void *cls, | ||
313 | const struct GNUNET_TRANSPORT_AddAddressMessage *msg) | ||
314 | { | ||
315 | struct MyClient *client = cls; | ||
316 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
317 | client->tc; | ||
318 | uint16_t size; | ||
319 | size = ntohs (msg->header.size) - sizeof(*msg); | ||
320 | LOG (GNUNET_ERROR_TYPE_DEBUG, "received add address cb %u\n", size); | ||
321 | if (0 == size) | ||
322 | return; /* receive-only communicator */ | ||
323 | LOG (GNUNET_ERROR_TYPE_DEBUG, "received add address cb %u\n", size); | ||
324 | tc_h->c_address = GNUNET_strdup ((const char *) &msg[1]); | ||
325 | if (NULL != tc_h->add_address_cb) | ||
326 | { | ||
327 | LOG (GNUNET_ERROR_TYPE_DEBUG, "calling add_address_cb()\n"); | ||
328 | tc_h->add_address_cb (tc_h->cb_cls, | ||
329 | tc_h, | ||
330 | tc_h->c_address, | ||
331 | GNUNET_TIME_relative_ntoh (msg->expiration), | ||
332 | msg->aid, | ||
333 | ntohl (msg->nt)); | ||
334 | } | ||
335 | GNUNET_SERVICE_client_continue (client->client); | ||
336 | } | ||
337 | |||
338 | |||
339 | /** | ||
340 | * Incoming message. Test message is well-formed. | ||
341 | * | ||
342 | * @param cls the client | ||
343 | * @param msg the send message that was sent | ||
344 | * @return #GNUNET_OK if message is well-formed | ||
345 | */ | ||
346 | static int | ||
347 | check_incoming_msg (void *cls, | ||
348 | const struct GNUNET_TRANSPORT_IncomingMessage *msg) | ||
349 | { | ||
350 | // struct TransportClient *tc = cls; | ||
351 | |||
352 | // if (CT_COMMUNICATOR != tc->type) | ||
353 | // { | ||
354 | // GNUNET_break (0); | ||
355 | // return GNUNET_SYSERR; | ||
356 | // } | ||
357 | GNUNET_MQ_check_boxed_message (msg); | ||
358 | return GNUNET_OK; | ||
359 | } | ||
360 | |||
361 | |||
362 | /** | ||
363 | * @brief Receive an incoming message. | ||
364 | * | ||
365 | * Pass the message to the client. | ||
366 | * | ||
367 | * @param cls Closure - communicator handle | ||
368 | * @param msg Message | ||
369 | */ | ||
370 | static void | ||
371 | handle_incoming_msg (void *cls, | ||
372 | const struct GNUNET_TRANSPORT_IncomingMessage *inc_msg) | ||
373 | { | ||
374 | struct MyClient *client = cls; | ||
375 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
376 | client->tc; | ||
377 | struct GNUNET_MessageHeader *msg; | ||
378 | msg = (struct GNUNET_MessageHeader *) &inc_msg[1]; | ||
379 | size_t payload_len = ntohs (msg->size) - sizeof (struct | ||
380 | GNUNET_MessageHeader); | ||
381 | if (NULL != tc_h->incoming_msg_cb) | ||
382 | { | ||
383 | tc_h->incoming_msg_cb (tc_h->cb_cls, | ||
384 | tc_h, | ||
385 | (char*) &msg[1], | ||
386 | payload_len); | ||
387 | } | ||
388 | else | ||
389 | { | ||
390 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
391 | "Incoming message from communicator but no handler!\n"); | ||
392 | } | ||
393 | if (GNUNET_YES == ntohl (inc_msg->fc_on)) | ||
394 | { | ||
395 | /* send ACK when done to communicator for flow control! */ | ||
396 | struct GNUNET_MQ_Envelope *env; | ||
397 | struct GNUNET_TRANSPORT_IncomingMessageAck *ack; | ||
398 | |||
399 | env = GNUNET_MQ_msg (ack, GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG_ACK); | ||
400 | GNUNET_assert (NULL != env); | ||
401 | ack->reserved = htonl (0); | ||
402 | ack->fc_id = inc_msg->fc_id; | ||
403 | ack->sender = inc_msg->sender; | ||
404 | GNUNET_MQ_send (tc_h->c_mq, env); | ||
405 | } | ||
406 | |||
407 | GNUNET_SERVICE_client_continue (client->client); | ||
408 | } | ||
409 | |||
410 | |||
411 | /** | ||
412 | * @brief Communicator informs that it tries to establish requested queue | ||
413 | * | ||
414 | * @param cls Closure - communicator handle | ||
415 | * @param msg Message | ||
416 | */ | ||
417 | static void | ||
418 | handle_queue_create_ok (void *cls, | ||
419 | const struct GNUNET_TRANSPORT_CreateQueueResponse *msg) | ||
420 | { | ||
421 | struct MyClient *client = cls; | ||
422 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
423 | client->tc; | ||
424 | |||
425 | if (NULL != tc_h->queue_create_reply_cb) | ||
426 | { | ||
427 | tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_YES); | ||
428 | } | ||
429 | GNUNET_SERVICE_client_continue (client->client); | ||
430 | } | ||
431 | |||
432 | |||
433 | /** | ||
434 | * @brief Communicator informs that it won't try establishing requested queue. | ||
435 | * | ||
436 | * It will not do so probably because the address is bougus (see comment to | ||
437 | * #GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL) | ||
438 | * | ||
439 | * @param cls Closure - communicator handle | ||
440 | * @param msg Message | ||
441 | */ | ||
442 | static void | ||
443 | handle_queue_create_fail ( | ||
444 | void *cls, | ||
445 | const struct GNUNET_TRANSPORT_CreateQueueResponse *msg) | ||
446 | { | ||
447 | struct MyClient *client = cls; | ||
448 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
449 | client->tc; | ||
450 | |||
451 | if (NULL != tc_h->queue_create_reply_cb) | ||
452 | { | ||
453 | tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_NO); | ||
454 | } | ||
455 | GNUNET_SERVICE_client_continue (client->client); | ||
456 | } | ||
457 | |||
458 | |||
459 | /** | ||
460 | * New queue became available. Check message. | ||
461 | * | ||
462 | * @param cls the client | ||
463 | * @param aqm the send message that was sent | ||
464 | */ | ||
465 | static int | ||
466 | check_add_queue_message (void *cls, | ||
467 | const struct GNUNET_TRANSPORT_AddQueueMessage *aqm) | ||
468 | { | ||
469 | GNUNET_MQ_check_zero_termination (aqm); | ||
470 | return GNUNET_OK; | ||
471 | } | ||
472 | |||
473 | |||
474 | /** | ||
475 | * @brief Handle new queue | ||
476 | * | ||
477 | * Store context and call client callback. | ||
478 | * | ||
479 | * @param cls Closure - communicator handle | ||
480 | * @param msg Message struct | ||
481 | */ | ||
482 | static void | ||
483 | handle_add_queue_message (void *cls, | ||
484 | const struct GNUNET_TRANSPORT_AddQueueMessage *msg) | ||
485 | { | ||
486 | struct MyClient *client = cls; | ||
487 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
488 | client->tc; | ||
489 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | ||
490 | |||
491 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
492 | "Got queue with ID %u\n", msg->qid); | ||
493 | for (tc_queue = tc_h->queue_head; NULL != tc_queue; tc_queue = tc_queue->next) | ||
494 | { | ||
495 | if (tc_queue->qid == msg->qid) | ||
496 | break; | ||
497 | } | ||
498 | if (NULL == tc_queue) | ||
499 | { | ||
500 | tc_queue = | ||
501 | GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue); | ||
502 | tc_queue->tc_h = tc_h; | ||
503 | tc_queue->qid = msg->qid; | ||
504 | tc_queue->peer_id = msg->receiver; | ||
505 | GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue); | ||
506 | } | ||
507 | GNUNET_assert (tc_queue->qid == msg->qid); | ||
508 | GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver)); | ||
509 | tc_queue->nt = msg->nt; | ||
510 | tc_queue->mtu = ntohl (msg->mtu); | ||
511 | tc_queue->cs = msg->cs; | ||
512 | tc_queue->priority = ntohl (msg->priority); | ||
513 | tc_queue->q_len = GNUNET_ntohll (msg->q_len); | ||
514 | if (NULL != tc_h->add_queue_cb) | ||
515 | { | ||
516 | tc_h->add_queue_cb (tc_h->cb_cls, tc_h, tc_queue, tc_queue->mtu); | ||
517 | } | ||
518 | GNUNET_SERVICE_client_continue (client->client); | ||
519 | } | ||
520 | |||
521 | |||
522 | /** | ||
523 | * @brief Handle new queue | ||
524 | * | ||
525 | * Store context and call client callback. | ||
526 | * | ||
527 | * @param cls Closure - communicator handle | ||
528 | * @param msg Message struct | ||
529 | */ | ||
530 | static void | ||
531 | handle_update_queue_message (void *cls, | ||
532 | const struct | ||
533 | GNUNET_TRANSPORT_UpdateQueueMessage *msg) | ||
534 | { | ||
535 | struct MyClient *client = cls; | ||
536 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
537 | client->tc; | ||
538 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | ||
539 | |||
540 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
541 | "Received queue update message for %u with q_len %" PRIu64 "\n", | ||
542 | msg->qid, GNUNET_ntohll (msg->q_len)); | ||
543 | tc_queue = tc_h->queue_head; | ||
544 | if (NULL != tc_queue) | ||
545 | { | ||
546 | while (tc_queue->qid != msg->qid) | ||
547 | { | ||
548 | tc_queue = tc_queue->next; | ||
549 | } | ||
550 | } | ||
551 | if (NULL == tc_queue) | ||
552 | { | ||
553 | GNUNET_SERVICE_client_continue (client->client); | ||
554 | return; | ||
555 | } | ||
556 | GNUNET_assert (tc_queue->qid == msg->qid); | ||
557 | GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver)); | ||
558 | tc_queue->nt = msg->nt; | ||
559 | tc_queue->mtu = ntohl (msg->mtu); | ||
560 | tc_queue->cs = msg->cs; | ||
561 | tc_queue->priority = ntohl (msg->priority); | ||
562 | // Uncomment this for alternativ 1 of backchannel functionality | ||
563 | tc_queue->q_len += GNUNET_ntohll (msg->q_len); | ||
564 | // Until here for alternativ 1 | ||
565 | // Uncomment this for alternativ 2 of backchannel functionality | ||
566 | // tc_queue->q_len = GNUNET_ntohll (msg->q_len); | ||
567 | // Until here for alternativ 2 | ||
568 | GNUNET_SERVICE_client_continue (client->client); | ||
569 | } | ||
570 | |||
571 | |||
572 | /** | ||
573 | * @brief Shut down the service | ||
574 | * | ||
575 | * @param cls Closure - Handle to the service | ||
576 | */ | ||
577 | static void | ||
578 | shutdown_service (void *cls) | ||
579 | { | ||
580 | struct GNUNET_SERVICE_Handle *h = cls; | ||
581 | |||
582 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
583 | "Shutting down service!\n"); | ||
584 | |||
585 | GNUNET_SERVICE_stop (h); | ||
586 | } | ||
587 | |||
588 | |||
589 | /** | ||
590 | * @brief Callback called when new Client (Communicator) connects | ||
591 | * | ||
592 | * @param cls Closure - TransporCommmunicator Handle | ||
593 | * @param client Client | ||
594 | * @param mq Messagequeue | ||
595 | * | ||
596 | * @return TransportCommunicator Handle | ||
597 | */ | ||
598 | static void * | ||
599 | connect_cb (void *cls, | ||
600 | struct GNUNET_SERVICE_Client *client, | ||
601 | struct GNUNET_MQ_Handle *mq) | ||
602 | { | ||
603 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls; | ||
604 | struct MyClient *new_c; | ||
605 | |||
606 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected to %p.\n", | ||
607 | client, tc_h); | ||
608 | new_c = GNUNET_new (struct MyClient); | ||
609 | new_c->client = client; | ||
610 | new_c->c_mq = mq; | ||
611 | new_c->tc = tc_h; | ||
612 | GNUNET_CONTAINER_DLL_insert (tc_h->client_head, | ||
613 | tc_h->client_tail, | ||
614 | new_c); | ||
615 | |||
616 | if (NULL == tc_h->queue_head) | ||
617 | return new_c; | ||
618 | /* Iterate over queues. They are yet to be opened. Request opening. */ | ||
619 | for (struct | ||
620 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_iter = | ||
621 | tc_h->queue_head; | ||
622 | NULL != tc_queue_iter; | ||
623 | tc_queue_iter = tc_queue_iter->next) | ||
624 | { | ||
625 | if (NULL == tc_queue_iter->open_queue_env) | ||
626 | continue; | ||
627 | /* Send the previously created mq envelope to request the creation of the | ||
628 | * queue. */ | ||
629 | GNUNET_MQ_send (tc_h->c_mq, | ||
630 | tc_queue_iter->open_queue_env); | ||
631 | tc_queue_iter->open_queue_env = NULL; | ||
632 | } | ||
633 | return new_c; | ||
634 | } | ||
635 | |||
636 | |||
637 | /** | ||
638 | * @brief Callback called when Client disconnects | ||
639 | * | ||
640 | * @param cls Closure - TransportCommunicator Handle | ||
641 | * @param client Client | ||
642 | * @param internal_cls TransporCommmunicator Handle | ||
643 | */ | ||
644 | static void | ||
645 | disconnect_cb (void *cls, | ||
646 | struct GNUNET_SERVICE_Client *client, | ||
647 | void *internal_cls) | ||
648 | { | ||
649 | struct MyClient *cl = cls; | ||
650 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls; | ||
651 | |||
652 | for (cl = tc_h->client_head; NULL != cl; cl = cl->next) | ||
653 | { | ||
654 | if (cl->client != client) | ||
655 | continue; | ||
656 | GNUNET_CONTAINER_DLL_remove (tc_h->client_head, | ||
657 | tc_h->client_tail, | ||
658 | cl); | ||
659 | if (cl->c_mq == tc_h->c_mq) | ||
660 | tc_h->c_mq = NULL; | ||
661 | GNUNET_free (cl); | ||
662 | break; | ||
663 | } | ||
664 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected.\n"); | ||
665 | } | ||
666 | |||
667 | |||
668 | /** | ||
669 | * Message was transmitted. Process the request. | ||
670 | * | ||
671 | * @param cls the client | ||
672 | * @param sma the send message that was sent | ||
673 | */ | ||
674 | static void | ||
675 | handle_send_message_ack (void *cls, | ||
676 | const struct GNUNET_TRANSPORT_SendMessageToAck *sma) | ||
677 | { | ||
678 | struct MyClient *client = cls; | ||
679 | GNUNET_SERVICE_client_continue (client->client); | ||
680 | // NOP | ||
681 | } | ||
682 | |||
683 | |||
684 | /** | ||
685 | * @brief Start the communicator part of the transport service | ||
686 | * | ||
687 | * @param communicator_available Callback to be called when a new communicator | ||
688 | * becomes available | ||
689 | * @param cfg Configuration | ||
690 | */ | ||
691 | static void | ||
692 | transport_communicator_start ( | ||
693 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
694 | { | ||
695 | struct GNUNET_MQ_MessageHandler mh[] = { | ||
696 | GNUNET_MQ_hd_var_size (communicator_available, | ||
697 | GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR, | ||
698 | struct GNUNET_TRANSPORT_CommunicatorAvailableMessage, | ||
699 | tc_h), | ||
700 | GNUNET_MQ_hd_var_size (communicator_backchannel, | ||
701 | GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL, | ||
702 | struct GNUNET_TRANSPORT_CommunicatorBackchannel, | ||
703 | tc_h), | ||
704 | GNUNET_MQ_hd_var_size (add_address, | ||
705 | GNUNET_MESSAGE_TYPE_TRANSPORT_ADD_ADDRESS, | ||
706 | struct GNUNET_TRANSPORT_AddAddressMessage, | ||
707 | tc_h), | ||
708 | // GNUNET_MQ_hd_fixed_size (del_address, | ||
709 | // GNUNET_MESSAGE_TYPE_TRANSPORT_DEL_ADDRESS, | ||
710 | // struct GNUNET_TRANSPORT_DelAddressMessage, | ||
711 | // NULL), | ||
712 | GNUNET_MQ_hd_var_size (incoming_msg, | ||
713 | GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG, | ||
714 | struct GNUNET_TRANSPORT_IncomingMessage, | ||
715 | tc_h), | ||
716 | GNUNET_MQ_hd_fixed_size (queue_create_ok, | ||
717 | GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_OK, | ||
718 | struct GNUNET_TRANSPORT_CreateQueueResponse, | ||
719 | tc_h), | ||
720 | GNUNET_MQ_hd_fixed_size (queue_create_fail, | ||
721 | GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL, | ||
722 | struct GNUNET_TRANSPORT_CreateQueueResponse, | ||
723 | tc_h), | ||
724 | GNUNET_MQ_hd_var_size (add_queue_message, | ||
725 | GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_SETUP, | ||
726 | struct GNUNET_TRANSPORT_AddQueueMessage, | ||
727 | tc_h), | ||
728 | GNUNET_MQ_hd_fixed_size (update_queue_message, | ||
729 | GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_UPDATE, | ||
730 | struct GNUNET_TRANSPORT_UpdateQueueMessage, | ||
731 | tc_h), | ||
732 | // GNUNET_MQ_hd_fixed_size (del_queue_message, | ||
733 | // GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_TEARDOWN, | ||
734 | // struct GNUNET_TRANSPORT_DelQueueMessage, | ||
735 | // NULL), | ||
736 | GNUNET_MQ_hd_fixed_size (send_message_ack, | ||
737 | GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG_ACK, | ||
738 | struct GNUNET_TRANSPORT_SendMessageToAck, | ||
739 | tc_h), | ||
740 | GNUNET_MQ_handler_end () | ||
741 | }; | ||
742 | |||
743 | |||
744 | tc_h->sh = GNUNET_SERVICE_start ("transport", | ||
745 | tc_h->cfg, | ||
746 | &connect_cb, | ||
747 | &disconnect_cb, | ||
748 | tc_h, | ||
749 | mh); | ||
750 | GNUNET_assert (NULL != tc_h->sh); | ||
751 | } | ||
752 | |||
753 | |||
754 | /** | ||
755 | * @brief Task run at shutdown to kill communicator and clean up | ||
756 | * | ||
757 | * @param cls Closure - Process of communicator | ||
758 | */ | ||
759 | static void | ||
760 | shutdown_process (struct GNUNET_OS_Process *proc) | ||
761 | { | ||
762 | if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) | ||
763 | { | ||
764 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
765 | "Error shutting down process with SIGERM, trying SIGKILL\n"); | ||
766 | if (0 != GNUNET_OS_process_kill (proc, SIGKILL)) | ||
767 | { | ||
768 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
769 | "Error shutting down process with SIGERM and SIGKILL\n"); | ||
770 | } | ||
771 | } | ||
772 | GNUNET_OS_process_destroy (proc); | ||
773 | } | ||
774 | |||
775 | /** | ||
776 | * @brief Task run at shutdown to kill the statistics process | ||
777 | * | ||
778 | * @param cls Closure - Process of communicator | ||
779 | */ | ||
780 | static void | ||
781 | shutdown_statistics (void *cls) | ||
782 | { | ||
783 | struct GNUNET_OS_Process *proc = cls; | ||
784 | shutdown_process (proc); | ||
785 | } | ||
786 | |||
787 | /** | ||
788 | * @brief Task run at shutdown to kill the peerstore process | ||
789 | * | ||
790 | * @param cls Closure - Process of communicator | ||
791 | */ | ||
792 | static void | ||
793 | shutdown_peerstore (void *cls) | ||
794 | { | ||
795 | struct GNUNET_OS_Process *proc = cls; | ||
796 | shutdown_process (proc); | ||
797 | } | ||
798 | |||
799 | /** | ||
800 | * @brief Task run at shutdown to kill a communicator process | ||
801 | * | ||
802 | * @param cls Closure - Process of communicator | ||
803 | */ | ||
804 | static void | ||
805 | shutdown_communicator (void *cls) | ||
806 | { | ||
807 | struct GNUNET_OS_Process *proc = cls; | ||
808 | shutdown_process (proc); | ||
809 | } | ||
810 | |||
811 | |||
812 | /** | ||
813 | * @brief Start the communicator | ||
814 | * | ||
815 | * @param cfgname Name of the communicator | ||
816 | */ | ||
817 | static void | ||
818 | communicator_start ( | ||
819 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h, | ||
820 | const char *binary_name) | ||
821 | { | ||
822 | char *binary; | ||
823 | char *loprefix; | ||
824 | char *section_name; | ||
825 | |||
826 | LOG (GNUNET_ERROR_TYPE_DEBUG, "communicator_start\n"); | ||
827 | |||
828 | section_name = strchr (binary_name, '-'); | ||
829 | section_name++; | ||
830 | |||
831 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (tc_h->cfg, | ||
832 | section_name, | ||
833 | "PREFIX", | ||
834 | &loprefix)) | ||
835 | loprefix = GNUNET_strdup (""); | ||
836 | |||
837 | |||
838 | binary = GNUNET_OS_get_libexec_binary_path (binary_name); | ||
839 | tc_h->c_proc = GNUNET_OS_start_process_s (GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | ||
840 | NULL, | ||
841 | loprefix, | ||
842 | binary, | ||
843 | binary_name, | ||
844 | "-c", | ||
845 | tc_h->cfg_filename, | ||
846 | NULL); | ||
847 | if (NULL == tc_h->c_proc) | ||
848 | { | ||
849 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start communicator!"); | ||
850 | return; | ||
851 | } | ||
852 | LOG (GNUNET_ERROR_TYPE_INFO, "started communicator\n"); | ||
853 | GNUNET_free (binary); | ||
854 | } | ||
855 | |||
856 | |||
857 | /** | ||
858 | * @brief Task run at shutdown to kill communicator and clean up | ||
859 | * | ||
860 | * @param cls Closure - Process of communicator | ||
861 | */ | ||
862 | static void | ||
863 | shutdown_nat (void *cls) | ||
864 | { | ||
865 | struct GNUNET_OS_Process *proc = cls; | ||
866 | shutdown_process (proc); | ||
867 | } | ||
868 | |||
869 | |||
870 | /** | ||
871 | * @brief Task run at shutdown to kill the resolver process | ||
872 | * | ||
873 | * @param cls Closure - Process of communicator | ||
874 | */ | ||
875 | static void | ||
876 | shutdown_resolver (void *cls) | ||
877 | { | ||
878 | struct GNUNET_OS_Process *proc = cls; | ||
879 | shutdown_process (proc); | ||
880 | } | ||
881 | |||
882 | |||
883 | /** | ||
884 | * @brief Start Resolver | ||
885 | * | ||
886 | */ | ||
887 | static void | ||
888 | resolver_start (struct | ||
889 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
890 | { | ||
891 | char *binary; | ||
892 | |||
893 | LOG (GNUNET_ERROR_TYPE_DEBUG, "resolver_start\n"); | ||
894 | binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver"); | ||
895 | tc_h->resolver_proc = GNUNET_OS_start_process ( | ||
896 | GNUNET_OS_INHERIT_STD_OUT_AND_ERR | ||
897 | | GNUNET_OS_USE_PIPE_CONTROL, | ||
898 | NULL, | ||
899 | NULL, | ||
900 | NULL, | ||
901 | binary, | ||
902 | "gnunet-service-resolver", | ||
903 | "-c", | ||
904 | tc_h->cfg_filename, | ||
905 | NULL); | ||
906 | if (NULL == tc_h->resolver_proc) | ||
907 | { | ||
908 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start resolver service!"); | ||
909 | return; | ||
910 | } | ||
911 | LOG (GNUNET_ERROR_TYPE_INFO, "started resolver service\n"); | ||
912 | GNUNET_free (binary); | ||
913 | |||
914 | } | ||
915 | |||
916 | /** | ||
917 | * @brief Start Statistics | ||
918 | * | ||
919 | */ | ||
920 | static void | ||
921 | statistics_start ( | ||
922 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
923 | { | ||
924 | char *binary; | ||
925 | |||
926 | binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics"); | ||
927 | tc_h->stat_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | ||
928 | NULL, | ||
929 | NULL, | ||
930 | NULL, | ||
931 | binary, | ||
932 | "gnunet-service-statistics", | ||
933 | "-c", | ||
934 | tc_h->cfg_filename, | ||
935 | NULL); | ||
936 | if (NULL == tc_h->stat_proc) | ||
937 | { | ||
938 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start Statistics!"); | ||
939 | return; | ||
940 | } | ||
941 | LOG (GNUNET_ERROR_TYPE_INFO, "started Statistics\n"); | ||
942 | GNUNET_free (binary); | ||
943 | } | ||
944 | |||
945 | /** | ||
946 | * @brief Start Peerstore | ||
947 | * | ||
948 | */ | ||
949 | static void | ||
950 | peerstore_start ( | ||
951 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
952 | { | ||
953 | char *binary; | ||
954 | |||
955 | binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-peerstore"); | ||
956 | tc_h->ps_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | ||
957 | NULL, | ||
958 | NULL, | ||
959 | NULL, | ||
960 | binary, | ||
961 | "gnunet-service-peerstore", | ||
962 | "-c", | ||
963 | tc_h->cfg_filename, | ||
964 | NULL); | ||
965 | if (NULL == tc_h->ps_proc) | ||
966 | { | ||
967 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start Peerstore!"); | ||
968 | return; | ||
969 | } | ||
970 | LOG (GNUNET_ERROR_TYPE_INFO, "started Peerstore\n"); | ||
971 | GNUNET_free (binary); | ||
972 | } | ||
973 | |||
974 | /** | ||
975 | * @brief Start NAT | ||
976 | * | ||
977 | */ | ||
978 | static void | ||
979 | nat_start ( | ||
980 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
981 | { | ||
982 | char *binary; | ||
983 | |||
984 | LOG (GNUNET_ERROR_TYPE_DEBUG, "nat_start\n"); | ||
985 | binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-nat"); | ||
986 | tc_h->nat_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR | ||
987 | | GNUNET_OS_USE_PIPE_CONTROL, | ||
988 | NULL, | ||
989 | NULL, | ||
990 | NULL, | ||
991 | binary, | ||
992 | "gnunet-service-nat", | ||
993 | "-c", | ||
994 | tc_h->cfg_filename, | ||
995 | NULL); | ||
996 | if (NULL == tc_h->nat_proc) | ||
997 | { | ||
998 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start NAT!"); | ||
999 | return; | ||
1000 | } | ||
1001 | LOG (GNUNET_ERROR_TYPE_INFO, "started NAT\n"); | ||
1002 | GNUNET_free (binary); | ||
1003 | } | ||
1004 | |||
1005 | |||
1006 | /** | ||
1007 | * @brief Start communicator part of transport service and communicator | ||
1008 | * | ||
1009 | * @param service_name Name of the service | ||
1010 | * @param cfg Configuration handle | ||
1011 | * @param communicator_available_cb Callback that is called when a new | ||
1012 | * @param add_address_cb Callback that is called when a new | ||
1013 | * communicator becomes available | ||
1014 | * @param cb_cls Closure to @a communicator_available_cb and @a | ||
1015 | * | ||
1016 | * @return Handle to the communicator duo | ||
1017 | */ | ||
1018 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle * | ||
1019 | GNUNET_TRANSPORT_TESTING_transport_communicator_service_start ( | ||
1020 | const char *service_name, | ||
1021 | const char *binary_name, | ||
1022 | const char *cfg_filename, | ||
1023 | const struct GNUNET_PeerIdentity *peer_id, | ||
1024 | GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback | ||
1025 | communicator_available_cb, | ||
1026 | GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb, | ||
1027 | GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb, | ||
1028 | GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb, | ||
1029 | GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_message_cb, | ||
1030 | GNUNET_TRANSPORT_TESTING_BackchannelCallback bc_cb, | ||
1031 | void *cb_cls) | ||
1032 | { | ||
1033 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h; | ||
1034 | |||
1035 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1036 | "Starting new transport/communicator combo with config %s\n", | ||
1037 | cfg_filename); | ||
1038 | tc_h = | ||
1039 | GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle); | ||
1040 | tc_h->cfg_filename = GNUNET_strdup (cfg_filename); | ||
1041 | tc_h->cfg = GNUNET_CONFIGURATION_create (); | ||
1042 | if ((GNUNET_SYSERR == GNUNET_CONFIGURATION_load (tc_h->cfg, cfg_filename))) | ||
1043 | { | ||
1044 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1045 | _ ("Malformed configuration file `%s', exit ...\n"), | ||
1046 | cfg_filename); | ||
1047 | GNUNET_free (tc_h->cfg_filename); | ||
1048 | GNUNET_CONFIGURATION_destroy (tc_h->cfg); | ||
1049 | GNUNET_free (tc_h); | ||
1050 | return NULL; | ||
1051 | } | ||
1052 | tc_h->bc_enabled = GNUNET_CONFIGURATION_get_value_yesno (tc_h->cfg, | ||
1053 | "communicator-test", | ||
1054 | "BACKCHANNEL_ENABLED"); | ||
1055 | tc_h->communicator_available_cb = communicator_available_cb; | ||
1056 | tc_h->add_address_cb = add_address_cb; | ||
1057 | tc_h->queue_create_reply_cb = queue_create_reply_cb; | ||
1058 | tc_h->add_queue_cb = add_queue_cb; | ||
1059 | tc_h->incoming_msg_cb = incoming_message_cb; | ||
1060 | tc_h->bc_cb = bc_cb; | ||
1061 | tc_h->peer_id = *peer_id; | ||
1062 | tc_h->cb_cls = cb_cls; | ||
1063 | |||
1064 | /* Start communicator part of service */ | ||
1065 | transport_communicator_start (tc_h); | ||
1066 | /* Start NAT */ | ||
1067 | nat_start (tc_h); | ||
1068 | /* Start resolver service */ | ||
1069 | resolver_start (tc_h); | ||
1070 | /* Start peerstore service */ | ||
1071 | peerstore_start (tc_h); | ||
1072 | /* Start statistic service */ | ||
1073 | statistics_start (tc_h); | ||
1074 | /* Schedule start communicator */ | ||
1075 | communicator_start (tc_h, | ||
1076 | binary_name); | ||
1077 | return tc_h; | ||
1078 | } | ||
1079 | |||
1080 | |||
1081 | void | ||
1082 | GNUNET_TRANSPORT_TESTING_transport_communicator_service_stop ( | ||
1083 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
1084 | { | ||
1085 | shutdown_communicator (tc_h->c_proc); | ||
1086 | shutdown_service (tc_h->sh); | ||
1087 | shutdown_nat (tc_h->nat_proc); | ||
1088 | shutdown_resolver (tc_h->resolver_proc); | ||
1089 | shutdown_peerstore (tc_h->ps_proc); | ||
1090 | shutdown_statistics (tc_h->stat_proc); | ||
1091 | GNUNET_CONFIGURATION_destroy (tc_h->cfg); | ||
1092 | GNUNET_free (tc_h); | ||
1093 | } | ||
1094 | |||
1095 | |||
1096 | /** | ||
1097 | * @brief Instruct communicator to open a queue | ||
1098 | * | ||
1099 | * @param tc_h Handle to communicator which shall open queue | ||
1100 | * @param peer_id Towards which peer | ||
1101 | * @param address For which address | ||
1102 | */ | ||
1103 | void | ||
1104 | GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue ( | ||
1105 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h, | ||
1106 | const struct GNUNET_PeerIdentity *peer_id, | ||
1107 | const char *address) | ||
1108 | { | ||
1109 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | ||
1110 | static uint32_t idgen; | ||
1111 | char *prefix; | ||
1112 | struct GNUNET_TRANSPORT_CreateQueue *msg; | ||
1113 | struct GNUNET_MQ_Envelope *env; | ||
1114 | size_t alen; | ||
1115 | |||
1116 | tc_queue = | ||
1117 | GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue); | ||
1118 | tc_queue->tc_h = tc_h; | ||
1119 | prefix = GNUNET_HELLO_address_to_prefix (address); | ||
1120 | if (NULL == prefix) | ||
1121 | { | ||
1122 | GNUNET_break (0); /* We got an invalid address!? */ | ||
1123 | GNUNET_free (tc_queue); | ||
1124 | return; | ||
1125 | } | ||
1126 | GNUNET_free (prefix); | ||
1127 | alen = strlen (address) + 1; | ||
1128 | env = | ||
1129 | GNUNET_MQ_msg_extra (msg, alen, GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE); | ||
1130 | msg->request_id = htonl (idgen++); | ||
1131 | tc_queue->qid = msg->request_id; | ||
1132 | msg->receiver = *peer_id; | ||
1133 | tc_queue->peer_id = *peer_id; | ||
1134 | memcpy (&msg[1], address, alen); | ||
1135 | if (NULL != tc_h->c_mq) | ||
1136 | { | ||
1137 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1138 | "Sending queue create immediately\n"); | ||
1139 | GNUNET_MQ_send (tc_h->c_mq, env); | ||
1140 | } | ||
1141 | else | ||
1142 | { | ||
1143 | tc_queue->open_queue_env = env; | ||
1144 | } | ||
1145 | GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue); | ||
1146 | } | ||
1147 | |||
1148 | |||
1149 | /** | ||
1150 | * @brief Instruct communicator to send data | ||
1151 | * | ||
1152 | * @param tc_queue The queue to use for sending | ||
1153 | * @param cont function to call when done sending | ||
1154 | * @param cont_cls closure for @a cont | ||
1155 | * @param payload Data to send | ||
1156 | * @param payload_size Size of the @a payload | ||
1157 | */ | ||
1158 | void | ||
1159 | GNUNET_TRANSPORT_TESTING_transport_communicator_send | ||
1160 | (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h, | ||
1161 | GNUNET_SCHEDULER_TaskCallback cont, | ||
1162 | void *cont_cls, | ||
1163 | const void *payload, | ||
1164 | size_t payload_size) | ||
1165 | { | ||
1166 | struct GNUNET_MessageHeader *mh; | ||
1167 | struct GNUNET_TRANSPORT_SendMessageTo *msg; | ||
1168 | struct GNUNET_MQ_Envelope *env; | ||
1169 | size_t inbox_size; | ||
1170 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | ||
1171 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_tmp; | ||
1172 | |||
1173 | tc_queue = NULL; | ||
1174 | for (tc_queue_tmp = tc_h->queue_head; | ||
1175 | NULL != tc_queue_tmp; | ||
1176 | tc_queue_tmp = tc_queue_tmp->next) | ||
1177 | { | ||
1178 | if (tc_queue_tmp->q_len <= 0) | ||
1179 | continue; | ||
1180 | if (NULL == tc_queue) | ||
1181 | { | ||
1182 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1183 | "Selecting queue with prio %u, len %" PRIu64 " and MTU %u\n", | ||
1184 | tc_queue_tmp->priority, | ||
1185 | tc_queue_tmp->q_len, | ||
1186 | tc_queue_tmp->mtu); | ||
1187 | tc_queue = tc_queue_tmp; | ||
1188 | continue; | ||
1189 | } | ||
1190 | if (tc_queue->priority < tc_queue_tmp->priority) | ||
1191 | { | ||
1192 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1193 | "Selecting queue with prio %u, len %" PRIu64 " and MTU %u\n", | ||
1194 | tc_queue_tmp->priority, | ||
1195 | tc_queue_tmp->q_len, | ||
1196 | tc_queue_tmp->mtu); | ||
1197 | tc_queue = tc_queue_tmp; | ||
1198 | } | ||
1199 | } | ||
1200 | GNUNET_assert (NULL != tc_queue); | ||
1201 | // Uncomment this for alternativ 1 of backchannel functionality | ||
1202 | if (tc_queue->q_len != GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED) | ||
1203 | tc_queue->q_len--; | ||
1204 | // Until here for alternativ 1 | ||
1205 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1206 | "Sending message\n"); | ||
1207 | inbox_size = sizeof (struct GNUNET_MessageHeader) + payload_size; | ||
1208 | env = GNUNET_MQ_msg_extra (msg, | ||
1209 | inbox_size, | ||
1210 | GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG); | ||
1211 | GNUNET_assert (NULL != env); | ||
1212 | msg->qid = htonl (tc_queue->qid); | ||
1213 | msg->mid = tc_queue->mid++; | ||
1214 | msg->receiver = tc_queue->peer_id; | ||
1215 | mh = (struct GNUNET_MessageHeader *) &msg[1]; | ||
1216 | mh->size = htons (inbox_size); | ||
1217 | mh->type = GNUNET_MESSAGE_TYPE_DUMMY; | ||
1218 | memcpy (&mh[1], | ||
1219 | payload, | ||
1220 | payload_size); | ||
1221 | if (NULL != cont) | ||
1222 | GNUNET_MQ_notify_sent (env, | ||
1223 | cont, | ||
1224 | cont_cls); | ||
1225 | GNUNET_MQ_send (tc_queue->tc_h->c_mq, | ||
1226 | env); | ||
1227 | } | ||