2 This file is part of GNUnet.
3 Copyright (C) 2019 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file transport/transport-testing2.c
23 * @brief functions related to testing-tng
24 * @author Christian Grothoff
25 * @author Julius Bünger
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"
38 #define LOG(kind, ...) GNUNET_log_from (kind, "transport-testing2", __VA_ARGS__)
42 * @brief Handle to a transport communicator
44 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
47 * @brief Handle to the configuration
49 struct GNUNET_CONFIGURATION_Handle *cfg;
52 * @brief File name of configuration file
57 * @brief Handle to the transport service
59 struct GNUNET_SERVICE_Handle *tsh;
62 * @brief Task that will be run on shutdown to stop and clean transport
65 struct GNUNET_SCHEDULER_Task *ts_shutdown_task;
68 * @brief Handle to the client
70 struct GNUNET_SERVICE_Client *client;
73 * @brief Handle to the client
75 struct GNUNET_MQ_Handle *c_mq;
78 * @brief Process of the communicator
80 struct GNUNET_OS_Process *c_proc;
83 * @brief Task that will be run on shutdown to stop and clean communicator
85 struct GNUNET_SCHEDULER_Task *c_shutdown_task;
88 * @brief Characteristics of the communicator
90 enum GNUNET_TRANSPORT_CommunicatorCharacteristics c_characteristics;
93 * @brief Specifies supported addresses
98 * @brief Specifies supported addresses
103 * @brief Head of the DLL of queues associated with this communicator
105 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *queue_head;
108 * @brief Tail of the DLL of queues associated with this communicator
110 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *queue_tail;
112 /* Callbacks + Closures */
114 * @brief Callback called when a new communicator connects
116 GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback
117 communicator_available_cb;
120 * @brief Callback called when a new communicator connects
122 GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb;
125 * @brief Callback called when a new communicator connects
127 GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb;
130 * @brief Callback called when a new communicator connects
132 GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb;
135 * @brief Callback called when a new communicator connects
137 GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_msg_cb;
140 * @brief Closure to the callback
147 * @brief Queue of a communicator and some context
149 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue
152 * @brief Handle to the TransportCommunicator
154 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h;
157 * @brief Envelope to a message that requests the opening of the queue.
159 * If the client already requests queue(s), but the communicator is not yet
160 * connected, we cannot send the request to open the queue. Save it until the
161 * communicator becomes available and send it then.
163 struct GNUNET_MQ_Envelope *open_queue_env;
166 * @brief Peer ID of the peer on the other side of the queue
168 struct GNUNET_PeerIdentity peer_id;
176 * @brief Current message id
181 * An `enum GNUNET_NetworkType` in NBO.
186 * Maximum transmission unit, in NBO. UINT32_MAX for unlimited.
191 * An `enum GNUNET_TRANSPORT_ConnectionStatus` in NBO.
196 * @brief Next element inside a DLL
198 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *next;
201 * @brief Previous element inside a DLL
203 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *prev;
208 * @brief Handle/Context to a single transmission
210 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission
216 * @brief Check whether incoming msg indicating available communicator is
220 * @param msg Message struct
222 * @return GNUNET_YES in case message is correct
225 check_communicator_available (
227 const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
231 size = ntohs (msg->header.size) - sizeof(*msg);
233 return GNUNET_OK; /* receive-only communicator */
234 GNUNET_MQ_check_zero_termination (msg);
240 * @brief Handle new communicator
242 * Store characteristics of communicator, call respective client callback.
244 * @param cls Closure - communicator handle
245 * @param msg Message struct
248 handle_communicator_available (
250 const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
252 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
255 size = ntohs (msg->header.size) - sizeof(*msg);
257 return; /* receive-only communicator */
258 tc_h->c_characteristics = ntohl (msg->cc);
259 tc_h->c_addr_prefix = GNUNET_strdup ((const char *) &msg[1]);
260 if (NULL != tc_h->communicator_available_cb)
262 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling communicator_available_cb()\n");
263 tc_h->communicator_available_cb (tc_h->cb_cls,
265 tc_h->c_characteristics,
266 tc_h->c_addr_prefix);
268 GNUNET_SERVICE_client_continue (tc_h->client);
273 * Address of our peer added. Test message is well-formed.
275 * @param cls the client
276 * @param aam the send message that was sent
277 * @return #GNUNET_OK if message is well-formed
280 check_add_address (void *cls,
281 const struct GNUNET_TRANSPORT_AddAddressMessage *msg)
283 struct TransportClient *tc = cls;
285 // if (CT_COMMUNICATOR != tc->type)
288 // return GNUNET_SYSERR;
290 GNUNET_MQ_check_zero_termination (msg);
296 * @brief The communicator informs about an address.
298 * Store address and call client callback.
300 * @param cls Closure - communicator handle
304 handle_add_address (void *cls,
305 const struct GNUNET_TRANSPORT_AddAddressMessage *msg)
307 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
309 size = ntohs (msg->header.size) - sizeof(*msg);
311 return; /* receive-only communicator */
312 LOG (GNUNET_ERROR_TYPE_DEBUG, "received add address cb %u\n", size);
313 tc_h->c_address = GNUNET_strdup ((const char *) &msg[1]);
314 if (NULL != tc_h->add_address_cb)
316 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling add_address_cb()\n");
317 tc_h->add_address_cb (tc_h->cb_cls,
320 GNUNET_TIME_relative_ntoh (msg->expiration),
324 GNUNET_SERVICE_client_continue (tc_h->client);
329 * Incoming message. Test message is well-formed.
331 * @param cls the client
332 * @param msg the send message that was sent
333 * @return #GNUNET_OK if message is well-formed
336 check_incoming_msg (void *cls,
337 const struct GNUNET_TRANSPORT_IncomingMessage *msg)
339 // struct TransportClient *tc = cls;
341 // if (CT_COMMUNICATOR != tc->type)
344 // return GNUNET_SYSERR;
346 GNUNET_MQ_check_boxed_message (msg);
352 * @brief Receive an incoming message.
354 * Pass the message to the client.
356 * @param cls Closure - communicator handle
360 handle_incoming_msg (void *cls,
361 const struct GNUNET_TRANSPORT_IncomingMessage *msg)
363 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
365 if (NULL != tc_h->incoming_msg_cb)
367 tc_h->incoming_msg_cb (tc_h->cb_cls,
373 LOG (GNUNET_ERROR_TYPE_WARNING,
374 "Incoming message from communicator but no handler!\n");
376 GNUNET_SERVICE_client_continue (tc_h->client);
381 * @brief Communicator informs that it tries to establish requested queue
383 * @param cls Closure - communicator handle
387 handle_queue_create_ok (void *cls,
388 const struct GNUNET_TRANSPORT_CreateQueueResponse *msg)
390 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
392 if (NULL != tc_h->queue_create_reply_cb)
394 tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_YES);
396 GNUNET_SERVICE_client_continue (tc_h->client);
401 * @brief Communicator informs that it wont try establishing requested queue.
403 * It will not do so probably because the address is bougus (see comment to
404 * #GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL)
406 * @param cls Closure - communicator handle
410 handle_queue_create_fail (
412 const struct GNUNET_TRANSPORT_CreateQueueResponse *msg)
414 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
416 if (NULL != tc_h->queue_create_reply_cb)
418 tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_NO);
420 GNUNET_SERVICE_client_continue (tc_h->client);
425 * New queue became available. Check message.
427 * @param cls the client
428 * @param aqm the send message that was sent
431 check_add_queue_message (void *cls,
432 const struct GNUNET_TRANSPORT_AddQueueMessage *aqm)
434 GNUNET_MQ_check_zero_termination (aqm);
440 * @brief Handle new queue
442 * Store context and call client callback.
444 * @param cls Closure - communicator handle
445 * @param msg Message struct
448 handle_add_queue_message (void *cls,
449 const struct GNUNET_TRANSPORT_AddQueueMessage *msg)
451 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
452 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue;
454 tc_queue = tc_h->queue_head;
455 if (NULL != tc_queue)
457 while (tc_queue->qid != msg->qid)
459 tc_queue = tc_queue->next;
463 GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue);
464 tc_queue->tc_h = tc_h;
465 tc_queue->qid = msg->qid;
466 tc_queue->peer_id = msg->receiver;
467 GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue);
469 GNUNET_assert (tc_queue->qid == msg->qid);
470 GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver));
471 tc_queue->nt = msg->nt;
472 tc_queue->mtu = msg->mtu;
473 tc_queue->cs = msg->cs;
474 if (NULL != tc_h->add_queue_cb)
476 tc_h->add_queue_cb (tc_h->cb_cls, tc_h, tc_queue);
478 GNUNET_SERVICE_client_continue (tc_h->client);
483 * @brief Shut down the service
485 * @param cls Closure - Handle to the service
488 shutdown_service (void *cls)
490 struct GNUNET_SERVICE_Handle *h = cls;
492 GNUNET_SERVICE_stop (h);
497 * @brief Callback called when new Client (Communicator) connects
499 * @param cls Closure - TransporCommmunicator Handle
500 * @param client Client
501 * @param mq Messagequeue
503 * @return TransportCommunicator Handle
506 connect_cb (void *cls,
507 struct GNUNET_SERVICE_Client *client,
508 struct GNUNET_MQ_Handle *mq)
510 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
511 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_iter;
513 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client connected.\n");
514 tc_h->client = client;
517 if (NULL == tc_h->queue_head)
519 /* Iterate over queues. They are yet to be opened. Request opening. */
520 while (NULL != (tc_queue_iter = tc_h->queue_head))
522 if (NULL == tc_queue_iter->open_queue_env)
524 /* Send the previously created mq envelope to request the creation of the
526 GNUNET_MQ_send (tc_h->c_mq, tc_queue_iter->open_queue_env);
527 tc_queue_iter->open_queue_env = NULL;
534 * @brief Callback called when Client disconnects
536 * @param cls Closure - TransportCommunicator Handle
537 * @param client Client
538 * @param internal_cls TransporCommmunicator Handle
541 disconnect_cb (void *cls,
542 struct GNUNET_SERVICE_Client *client,
545 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
547 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected.\n");
553 * @brief Start the communicator part of the transport service
555 * @param communicator_available Callback to be called when a new communicator
557 * @param cfg Configuration
560 transport_communicator_start (
561 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
563 struct GNUNET_MQ_MessageHandler mh[] = {
564 GNUNET_MQ_hd_var_size (communicator_available,
565 GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR,
566 struct GNUNET_TRANSPORT_CommunicatorAvailableMessage,
568 // GNUNET_MQ_hd_var_size (communicator_backchannel,
569 // GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL,
570 // struct GNUNET_TRANSPORT_CommunicatorBackchannel,
572 GNUNET_MQ_hd_var_size (add_address,
573 GNUNET_MESSAGE_TYPE_TRANSPORT_ADD_ADDRESS,
574 struct GNUNET_TRANSPORT_AddAddressMessage,
576 // GNUNET_MQ_hd_fixed_size (del_address,
577 // GNUNET_MESSAGE_TYPE_TRANSPORT_DEL_ADDRESS,
578 // struct GNUNET_TRANSPORT_DelAddressMessage,
580 GNUNET_MQ_hd_var_size (incoming_msg,
581 GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG,
582 struct GNUNET_TRANSPORT_IncomingMessage,
584 GNUNET_MQ_hd_fixed_size (queue_create_ok,
585 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_OK,
586 struct GNUNET_TRANSPORT_CreateQueueResponse,
588 GNUNET_MQ_hd_fixed_size (queue_create_fail,
589 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL,
590 struct GNUNET_TRANSPORT_CreateQueueResponse,
592 GNUNET_MQ_hd_var_size (add_queue_message,
593 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_SETUP,
594 struct GNUNET_TRANSPORT_AddQueueMessage,
596 // GNUNET_MQ_hd_fixed_size (del_queue_message,
597 // GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_TEARDOWN,
598 // struct GNUNET_TRANSPORT_DelQueueMessage,
600 // GNUNET_MQ_hd_fixed_size (send_message_ack,
601 // GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG_ACK,
602 // struct GNUNET_TRANSPORT_SendMessageToAck,
605 struct GNUNET_SERVICE_Handle *h;
607 h = GNUNET_SERVICE_start ("transport",
614 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed starting service!\n");
617 LOG (GNUNET_ERROR_TYPE_DEBUG, "Started service\n");
618 /* TODO */ GNUNET_SCHEDULER_add_shutdown (&shutdown_service, h);
624 * @brief Task run at shutdown to kill communicator and clean up
626 * @param cls Closure - Process of communicator
629 shutdown_communicator (void *cls)
631 struct GNUNET_OS_Process *proc = cls;
633 if (GNUNET_OK != GNUNET_OS_process_kill (proc, SIGTERM))
635 LOG (GNUNET_ERROR_TYPE_WARNING,
636 "Error shutting down communicator with SIGERM, trying SIGKILL\n");
637 if (GNUNET_OK != GNUNET_OS_process_kill (proc, SIGKILL))
639 LOG (GNUNET_ERROR_TYPE_ERROR,
640 "Error shutting down communicator with SIGERM and SIGKILL\n");
643 GNUNET_OS_process_destroy (proc);
648 * @brief Start the communicator
650 * @param cfgname Name of the communicator
654 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
655 const char *binary_name)
659 LOG (GNUNET_ERROR_TYPE_DEBUG, "communicator_start\n");
660 binary = GNUNET_OS_get_libexec_binary_path (binary_name);
661 tc_h->c_proc = GNUNET_OS_start_process (GNUNET_YES,
662 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
671 if (NULL == tc_h->c_proc)
673 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start communicator!");
676 LOG (GNUNET_ERROR_TYPE_INFO, "started communicator\n");
677 GNUNET_free (binary);
678 /* TODO */ GNUNET_SCHEDULER_add_shutdown (&shutdown_communicator,
684 * @brief Start communicator part of transport service and communicator
686 * @param service_name Name of the service
687 * @param cfg Configuration handle
688 * @param communicator_available_cb Callback that is called when a new
689 * @param add_address_cb Callback that is called when a new
690 * communicator becomes available
691 * @param cb_cls Closure to @a communicator_available_cb and @a
693 * @return Handle to the communicator duo
695 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
696 GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
697 const char *service_name,
698 const char *binary_name,
699 const char *cfg_filename,
700 GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback
701 communicator_available_cb,
702 GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb,
703 GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb,
704 GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb,
705 GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_message_cb,
708 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h;
711 GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle);
712 tc_h->cfg_filename = GNUNET_strdup (cfg_filename);
713 tc_h->cfg = GNUNET_CONFIGURATION_create ();
714 if ((GNUNET_SYSERR == GNUNET_CONFIGURATION_load (tc_h->cfg, cfg_filename)))
716 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
717 _ ("Malformed configuration file `%s', exit ...\n"),
719 GNUNET_free (tc_h->cfg_filename);
720 GNUNET_CONFIGURATION_destroy (tc_h->cfg);
724 tc_h->communicator_available_cb = communicator_available_cb;
725 tc_h->add_address_cb = add_address_cb;
726 tc_h->queue_create_reply_cb = queue_create_reply_cb;
727 tc_h->add_queue_cb = add_queue_cb;
728 tc_h->incoming_msg_cb = incoming_message_cb;
729 tc_h->cb_cls = cb_cls;
731 /* Start communicator part of service */
732 transport_communicator_start (tc_h);
734 /* Schedule start communicator */
735 communicator_start (tc_h,
742 * @brief Instruct communicator to open a queue
744 * @param tc_h Handle to communicator which shall open queue
745 * @param peer_id Towards which peer
746 * @param address For which address
749 GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (
750 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
751 const struct GNUNET_PeerIdentity *peer_id,
754 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue;
755 static uint32_t idgen;
757 struct GNUNET_TRANSPORT_CreateQueue *msg;
758 struct GNUNET_MQ_Envelope *env;
762 GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue);
763 tc_queue->tc_h = tc_h;
764 prefix = GNUNET_HELLO_address_to_prefix (address);
767 GNUNET_break (0); /* We got an invalid address!? */
768 GNUNET_free (tc_queue);
771 GNUNET_free (prefix);
772 alen = strlen (address) + 1;
774 GNUNET_MQ_msg_extra (msg, alen, GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE);
775 msg->request_id = htonl (idgen++);
776 tc_queue->qid = msg->request_id;
777 msg->receiver = *peer_id;
778 tc_queue->peer_id = *peer_id;
779 memcpy (&msg[1], address, alen);
780 if (NULL != tc_h->c_mq)
782 GNUNET_MQ_send (tc_h->c_mq, env);
786 tc_queue->open_queue_env = env;
788 GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue);
793 * @brief Instruct communicator to send data
795 * @param tc_queue The queue to use for sending
796 * @param payload Data to send
797 * @param payload_size Size of the payload
799 * @return Handle to the transmission
801 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission *
802 GNUNET_TRANSPORT_TESTING_transport_communicator_send
803 (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue,
805 size_t payload_size /*,
806 GNUNET_TRANSPORT_TESTING_SuccessStatus cb,
809 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission *tc_t;
810 struct GNUNET_MessageHeader *mh;
811 struct GNUNET_TRANSPORT_SendMessageTo *msg;
812 struct GNUNET_MQ_Envelope *env;
815 inbox_size = sizeof(struct GNUNET_MessageHeader) + payload_size;
816 mh = GNUNET_malloc (inbox_size);
817 mh->size = htons (inbox_size);
818 mh->type = GNUNET_MESSAGE_TYPE_DUMMY;
822 env = GNUNET_MQ_msg_extra (msg,
824 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG);
825 msg->qid = htonl (tc_queue->qid);
826 msg->mid = tc_queue->mid++;
827 msg->receiver = tc_queue->peer_id;
828 memcpy (&msg[1], mh, inbox_size);
830 GNUNET_MQ_send (tc_queue->tc_h->c_mq, env);