diff options
Diffstat (limited to 'src/transport/transport-testing2.c')
-rw-r--r-- | src/transport/transport-testing2.c | 1164 |
1 files changed, 0 insertions, 1164 deletions
diff --git a/src/transport/transport-testing2.c b/src/transport/transport-testing2.c deleted file mode 100644 index 076fbf331..000000000 --- a/src/transport/transport-testing2.c +++ /dev/null | |||
@@ -1,1164 +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-testing2.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-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 | |||
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 | 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 | } | ||
336 | |||
337 | |||
338 | /** | ||
339 | * Incoming message. Test message is well-formed. | ||
340 | * | ||
341 | * @param cls the client | ||
342 | * @param msg the send message that was sent | ||
343 | * @return #GNUNET_OK if message is well-formed | ||
344 | */ | ||
345 | static int | ||
346 | check_incoming_msg (void *cls, | ||
347 | const struct GNUNET_TRANSPORT_IncomingMessage *msg) | ||
348 | { | ||
349 | // struct TransportClient *tc = cls; | ||
350 | |||
351 | // if (CT_COMMUNICATOR != tc->type) | ||
352 | // { | ||
353 | // GNUNET_break (0); | ||
354 | // return GNUNET_SYSERR; | ||
355 | // } | ||
356 | GNUNET_MQ_check_boxed_message (msg); | ||
357 | return GNUNET_OK; | ||
358 | } | ||
359 | |||
360 | |||
361 | /** | ||
362 | * @brief Receive an incoming message. | ||
363 | * | ||
364 | * Pass the message to the client. | ||
365 | * | ||
366 | * @param cls Closure - communicator handle | ||
367 | * @param msg Message | ||
368 | */ | ||
369 | static void | ||
370 | handle_incoming_msg (void *cls, | ||
371 | const struct GNUNET_TRANSPORT_IncomingMessage *inc_msg) | ||
372 | { | ||
373 | struct MyClient *client = cls; | ||
374 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
375 | client->tc; | ||
376 | struct GNUNET_MessageHeader *msg; | ||
377 | msg = (struct GNUNET_MessageHeader *) &inc_msg[1]; | ||
378 | size_t payload_len = ntohs (msg->size) - sizeof (struct | ||
379 | GNUNET_MessageHeader); | ||
380 | if (NULL != tc_h->incoming_msg_cb) | ||
381 | { | ||
382 | tc_h->incoming_msg_cb (tc_h->cb_cls, | ||
383 | tc_h, | ||
384 | (char*) &msg[1], | ||
385 | payload_len); | ||
386 | } | ||
387 | else | ||
388 | { | ||
389 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
390 | "Incoming message from communicator but no handler!\n"); | ||
391 | } | ||
392 | if (GNUNET_YES == ntohl (inc_msg->fc_on)) | ||
393 | { | ||
394 | /* send ACK when done to communicator for flow control! */ | ||
395 | struct GNUNET_MQ_Envelope *env; | ||
396 | struct GNUNET_TRANSPORT_IncomingMessageAck *ack; | ||
397 | |||
398 | env = GNUNET_MQ_msg (ack, GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG_ACK); | ||
399 | GNUNET_assert (NULL != env); | ||
400 | ack->reserved = htonl (0); | ||
401 | ack->fc_id = inc_msg->fc_id; | ||
402 | ack->sender = inc_msg->sender; | ||
403 | GNUNET_MQ_send (tc_h->c_mq, env); | ||
404 | } | ||
405 | |||
406 | GNUNET_SERVICE_client_continue (client->client); | ||
407 | } | ||
408 | |||
409 | |||
410 | /** | ||
411 | * @brief Communicator informs that it tries to establish requested queue | ||
412 | * | ||
413 | * @param cls Closure - communicator handle | ||
414 | * @param msg Message | ||
415 | */ | ||
416 | static void | ||
417 | handle_queue_create_ok (void *cls, | ||
418 | const struct GNUNET_TRANSPORT_CreateQueueResponse *msg) | ||
419 | { | ||
420 | struct MyClient *client = cls; | ||
421 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
422 | client->tc; | ||
423 | |||
424 | if (NULL != tc_h->queue_create_reply_cb) | ||
425 | { | ||
426 | tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_YES); | ||
427 | } | ||
428 | GNUNET_SERVICE_client_continue (client->client); | ||
429 | } | ||
430 | |||
431 | |||
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 | */ | ||
441 | static void | ||
442 | handle_queue_create_fail ( | ||
443 | void *cls, | ||
444 | const struct GNUNET_TRANSPORT_CreateQueueResponse *msg) | ||
445 | { | ||
446 | struct MyClient *client = cls; | ||
447 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
448 | client->tc; | ||
449 | |||
450 | if (NULL != tc_h->queue_create_reply_cb) | ||
451 | { | ||
452 | tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_NO); | ||
453 | } | ||
454 | GNUNET_SERVICE_client_continue (client->client); | ||
455 | } | ||
456 | |||
457 | |||
458 | /** | ||
459 | * New queue became available. Check message. | ||
460 | * | ||
461 | * @param cls the client | ||
462 | * @param aqm the send message that was sent | ||
463 | */ | ||
464 | static int | ||
465 | check_add_queue_message (void *cls, | ||
466 | const struct GNUNET_TRANSPORT_AddQueueMessage *aqm) | ||
467 | { | ||
468 | GNUNET_MQ_check_zero_termination (aqm); | ||
469 | return GNUNET_OK; | ||
470 | } | ||
471 | |||
472 | |||
473 | /** | ||
474 | * @brief Handle new queue | ||
475 | * | ||
476 | * Store context and call client callback. | ||
477 | * | ||
478 | * @param cls Closure - communicator handle | ||
479 | * @param msg Message struct | ||
480 | */ | ||
481 | static void | ||
482 | handle_add_queue_message (void *cls, | ||
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 | { | ||
494 | if (tc_queue->qid == msg->qid) | ||
495 | break; | ||
496 | } | ||
497 | if (NULL == tc_queue) | ||
498 | { | ||
499 | tc_queue = | ||
500 | GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue); | ||
501 | tc_queue->tc_h = tc_h; | ||
502 | tc_queue->qid = msg->qid; | ||
503 | tc_queue->peer_id = msg->receiver; | ||
504 | GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue); | ||
505 | } | ||
506 | GNUNET_assert (tc_queue->qid == msg->qid); | ||
507 | GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver)); | ||
508 | tc_queue->nt = msg->nt; | ||
509 | tc_queue->mtu = ntohl (msg->mtu); | ||
510 | tc_queue->cs = msg->cs; | ||
511 | tc_queue->priority = ntohl (msg->priority); | ||
512 | tc_queue->q_len = GNUNET_ntohll (msg->q_len); | ||
513 | if (NULL != tc_h->add_queue_cb) | ||
514 | { | ||
515 | tc_h->add_queue_cb (tc_h->cb_cls, tc_h, tc_queue, tc_queue->mtu); | ||
516 | } | ||
517 | GNUNET_SERVICE_client_continue (client->client); | ||
518 | } | ||
519 | |||
520 | |||
521 | /** | ||
522 | * @brief Handle new queue | ||
523 | * | ||
524 | * Store context and call client callback. | ||
525 | * | ||
526 | * @param cls Closure - communicator handle | ||
527 | * @param msg Message struct | ||
528 | */ | ||
529 | static void | ||
530 | handle_update_queue_message (void *cls, | ||
531 | const struct | ||
532 | GNUNET_TRANSPORT_UpdateQueueMessage *msg) | ||
533 | { | ||
534 | struct MyClient *client = cls; | ||
535 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = | ||
536 | client->tc; | ||
537 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | ||
538 | |||
539 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
540 | "Received queue update message for %u with q_len %" PRIu64 "\n", | ||
541 | msg->qid, GNUNET_ntohll (msg->q_len)); | ||
542 | tc_queue = tc_h->queue_head; | ||
543 | if (NULL != tc_queue) | ||
544 | { | ||
545 | while (tc_queue->qid != msg->qid) | ||
546 | { | ||
547 | tc_queue = tc_queue->next; | ||
548 | } | ||
549 | } | ||
550 | GNUNET_assert (tc_queue->qid == msg->qid); | ||
551 | GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver)); | ||
552 | tc_queue->nt = msg->nt; | ||
553 | tc_queue->mtu = ntohl (msg->mtu); | ||
554 | tc_queue->cs = msg->cs; | ||
555 | tc_queue->priority = ntohl (msg->priority); | ||
556 | // Uncomment this for alternativ 1 of backchannel functionality | ||
557 | tc_queue->q_len += GNUNET_ntohll (msg->q_len); | ||
558 | // Until here for alternativ 1 | ||
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 | } | ||
564 | |||
565 | |||
566 | /** | ||
567 | * @brief Shut down the service | ||
568 | * | ||
569 | * @param cls Closure - Handle to the service | ||
570 | */ | ||
571 | static void | ||
572 | shutdown_service (void *cls) | ||
573 | { | ||
574 | struct GNUNET_SERVICE_Handle *h = cls; | ||
575 | |||
576 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
577 | "Shutting down service!\n"); | ||
578 | |||
579 | GNUNET_SERVICE_stop (h); | ||
580 | } | ||
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 | */ | ||
592 | static void * | ||
593 | connect_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 | { | ||
619 | if (NULL == tc_queue_iter->open_queue_env) | ||
620 | continue; | ||
621 | /* Send the previously created mq envelope to request the creation of the | ||
622 | * queue. */ | ||
623 | GNUNET_MQ_send (tc_h->c_mq, | ||
624 | tc_queue_iter->open_queue_env); | ||
625 | tc_queue_iter->open_queue_env = NULL; | ||
626 | } | ||
627 | return new_c; | ||
628 | } | ||
629 | |||
630 | |||
631 | /** | ||
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 | */ | ||
638 | static void | ||
639 | disconnect_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 | |||
646 | for (cl = tc_h->client_head; NULL != cl; cl = cl->next) | ||
647 | { | ||
648 | if (cl->client != client) | ||
649 | continue; | ||
650 | GNUNET_CONTAINER_DLL_remove (tc_h->client_head, | ||
651 | tc_h->client_tail, | ||
652 | cl); | ||
653 | if (cl->c_mq == tc_h->c_mq) | ||
654 | tc_h->c_mq = NULL; | ||
655 | GNUNET_free (cl); | ||
656 | break; | ||
657 | } | ||
658 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected.\n"); | ||
659 | } | ||
660 | |||
661 | |||
662 | /** | ||
663 | * Message was transmitted. Process the request. | ||
664 | * | ||
665 | * @param cls the client | ||
666 | * @param sma the send message that was sent | ||
667 | */ | ||
668 | static void | ||
669 | handle_send_message_ack (void *cls, | ||
670 | const struct GNUNET_TRANSPORT_SendMessageToAck *sma) | ||
671 | { | ||
672 | struct MyClient *client = cls; | ||
673 | GNUNET_SERVICE_client_continue (client->client); | ||
674 | // NOP | ||
675 | } | ||
676 | |||
677 | |||
678 | /** | ||
679 | * @brief Start the communicator part of the transport service | ||
680 | * | ||
681 | * @param communicator_available Callback to be called when a new communicator | ||
682 | * becomes available | ||
683 | * @param cfg Configuration | ||
684 | */ | ||
685 | static void | ||
686 | transport_communicator_start ( | ||
687 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
688 | { | ||
689 | struct GNUNET_MQ_MessageHandler mh[] = { | ||
690 | GNUNET_MQ_hd_var_size (communicator_available, | ||
691 | GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR, | ||
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 | |||
748 | /** | ||
749 | * @brief Task run at shutdown to kill communicator and clean up | ||
750 | * | ||
751 | * @param cls Closure - Process of communicator | ||
752 | */ | ||
753 | static void | ||
754 | shutdown_process (struct GNUNET_OS_Process *proc) | ||
755 | { | ||
756 | if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) | ||
757 | { | ||
758 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
759 | "Error shutting down process with SIGERM, trying SIGKILL\n"); | ||
760 | if (0 != GNUNET_OS_process_kill (proc, SIGKILL)) | ||
761 | { | ||
762 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
763 | "Error shutting down process with SIGERM and SIGKILL\n"); | ||
764 | } | ||
765 | } | ||
766 | GNUNET_OS_process_destroy (proc); | ||
767 | } | ||
768 | |||
769 | static void | ||
770 | shutdown_peerstore (void *cls) | ||
771 | { | ||
772 | struct GNUNET_OS_Process *proc = cls; | ||
773 | shutdown_process (proc); | ||
774 | } | ||
775 | |||
776 | static void | ||
777 | shutdown_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 | */ | ||
789 | static void | ||
790 | communicator_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 | { | ||
821 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start communicator!"); | ||
822 | return; | ||
823 | } | ||
824 | LOG (GNUNET_ERROR_TYPE_INFO, "started communicator\n"); | ||
825 | GNUNET_free (binary); | ||
826 | } | ||
827 | |||
828 | |||
829 | /** | ||
830 | * @brief Task run at shutdown to kill communicator and clean up | ||
831 | * | ||
832 | * @param cls Closure - Process of communicator | ||
833 | */ | ||
834 | static void | ||
835 | shutdown_nat (void *cls) | ||
836 | { | ||
837 | struct GNUNET_OS_Process *proc = cls; | ||
838 | shutdown_process (proc); | ||
839 | } | ||
840 | |||
841 | |||
842 | /** | ||
843 | * @brief Task run at shutdown to kill the resolver process | ||
844 | * | ||
845 | * @param cls Closure - Process of communicator | ||
846 | */ | ||
847 | static void | ||
848 | shutdown_resolver (void *cls) | ||
849 | { | ||
850 | struct GNUNET_OS_Process *proc = cls; | ||
851 | shutdown_process (proc); | ||
852 | } | ||
853 | |||
854 | |||
855 | static void | ||
856 | resolver_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 | { | ||
876 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start resolver service!"); | ||
877 | return; | ||
878 | } | ||
879 | LOG (GNUNET_ERROR_TYPE_INFO, "started resolver service\n"); | ||
880 | GNUNET_free (binary); | ||
881 | |||
882 | } | ||
883 | |||
884 | |||
885 | /** | ||
886 | * @brief Start Peerstore | ||
887 | * | ||
888 | */ | ||
889 | static void | ||
890 | peerstore_start ( | ||
891 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
892 | { | ||
893 | char *binary; | ||
894 | |||
895 | binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-peerstore"); | ||
896 | tc_h->ps_proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | ||
897 | NULL, | ||
898 | NULL, | ||
899 | NULL, | ||
900 | binary, | ||
901 | "gnunet-service-peerstore", | ||
902 | "-c", | ||
903 | tc_h->cfg_filename, | ||
904 | NULL); | ||
905 | if (NULL == tc_h->ps_proc) | ||
906 | { | ||
907 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start Peerstore!"); | ||
908 | return; | ||
909 | } | ||
910 | LOG (GNUNET_ERROR_TYPE_INFO, "started Peerstore\n"); | ||
911 | GNUNET_free (binary); | ||
912 | } | ||
913 | |||
914 | /** | ||
915 | * @brief Start NAT | ||
916 | * | ||
917 | */ | ||
918 | static void | ||
919 | nat_start ( | ||
920 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) | ||
921 | { | ||
922 | char *binary; | ||
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 | { | ||
938 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start NAT!"); | ||
939 | return; | ||
940 | } | ||
941 | LOG (GNUNET_ERROR_TYPE_INFO, "started NAT\n"); | ||
942 | GNUNET_free (binary); | ||
943 | } | ||
944 | |||
945 | |||
946 | /** | ||
947 | * @brief Start communicator part of transport service and communicator | ||
948 | * | ||
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 | * | ||
956 | * @return Handle to the communicator duo | ||
957 | */ | ||
958 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle * | ||
959 | GNUNET_TRANSPORT_TESTING_transport_communicator_service_start ( | ||
960 | const char *service_name, | ||
961 | const char *binary_name, | ||
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 | { | ||
973 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h; | ||
974 | |||
975 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
976 | "Starting new transport/communicator combo with config %s\n", | ||
977 | cfg_filename); | ||
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 | { | ||
984 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
985 | _ ("Malformed configuration file `%s', exit ...\n"), | ||
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 | } | ||
992 | tc_h->bc_enabled = GNUNET_CONFIGURATION_get_value_yesno (tc_h->cfg, | ||
993 | "communicator-test", | ||
994 | "BACKCHANNEL_ENABLED"); | ||
995 | tc_h->communicator_available_cb = communicator_available_cb; | ||
996 | tc_h->add_address_cb = add_address_cb; | ||
997 | tc_h->queue_create_reply_cb = queue_create_reply_cb; | ||
998 | tc_h->add_queue_cb = add_queue_cb; | ||
999 | tc_h->incoming_msg_cb = incoming_message_cb; | ||
1000 | tc_h->bc_cb = bc_cb; | ||
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 | |||
1019 | void | ||
1020 | GNUNET_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 | } | ||
1031 | |||
1032 | |||
1033 | /** | ||
1034 | * @brief Instruct communicator to open a queue | ||
1035 | * | ||
1036 | * @param tc_h Handle to communicator which shall open queue | ||
1037 | * @param peer_id Towards which peer | ||
1038 | * @param address For which address | ||
1039 | */ | ||
1040 | void | ||
1041 | GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue ( | ||
1042 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h, | ||
1043 | const struct GNUNET_PeerIdentity *peer_id, | ||
1044 | const char *address) | ||
1045 | { | ||
1046 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | ||
1047 | static uint32_t idgen; | ||
1048 | char *prefix; | ||
1049 | struct GNUNET_TRANSPORT_CreateQueue *msg; | ||
1050 | struct GNUNET_MQ_Envelope *env; | ||
1051 | size_t alen; | ||
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; | ||
1062 | } | ||
1063 | GNUNET_free (prefix); | ||
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 | { | ||
1074 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1075 | "Sending queue create immediately\n"); | ||
1076 | GNUNET_MQ_send (tc_h->c_mq, env); | ||
1077 | } | ||
1078 | else | ||
1079 | { | ||
1080 | tc_queue->open_queue_env = env; | ||
1081 | } | ||
1082 | GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue); | ||
1083 | } | ||
1084 | |||
1085 | |||
1086 | /** | ||
1087 | * @brief Instruct communicator to send data | ||
1088 | * | ||
1089 | * @param tc_queue The queue to use for sending | ||
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 | */ | ||
1095 | void | ||
1096 | GNUNET_TRANSPORT_TESTING_transport_communicator_send | ||
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 | { | ||
1103 | struct GNUNET_MessageHeader *mh; | ||
1104 | struct GNUNET_TRANSPORT_SendMessageTo *msg; | ||
1105 | struct GNUNET_MQ_Envelope *env; | ||
1106 | size_t inbox_size; | ||
1107 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue; | ||
1108 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_tmp; | ||
1109 | |||
1110 | tc_queue = NULL; | ||
1111 | for (tc_queue_tmp = tc_h->queue_head; | ||
1112 | NULL != tc_queue_tmp; | ||
1113 | tc_queue_tmp = tc_queue_tmp->next) | ||
1114 | { | ||
1115 | if (tc_queue_tmp->q_len <= 0) | ||
1116 | continue; | ||
1117 | if (NULL == tc_queue) | ||
1118 | { | ||
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 | } | ||
1137 | GNUNET_assert (NULL != tc_queue); | ||
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 | } | ||