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__)
41 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
44 * @brief Handle to the configuration
46 struct GNUNET_CONFIGURATION_Handle *cfg;
49 * @brief File name of configuration file
54 * @brief Handle to the transport service
56 struct GNUNET_SERVICE_Handle *tsh;
59 * @brief Task that will be run on shutdown to stop and clean transport
62 struct GNUNET_SCHEDULER_Task *ts_shutdown_task;
65 * @brief Handle to the client
67 struct GNUNET_SERVICE_Client *client;
70 * @brief Handle to the client
72 struct GNUNET_MQ_Handle *c_mq;
75 * @brief Process of the communicator
77 struct GNUNET_OS_Process *c_proc;
80 * @brief Task that will be run on shutdown to stop and clean communicator
82 struct GNUNET_SCHEDULER_Task *c_shutdown_task;
85 * @brief Characteristics of the communicator
87 enum GNUNET_TRANSPORT_CommunicatorCharacteristics c_characteristics;
90 * @brief Specifies supported addresses
95 * @brief Specifies supported addresses
100 * @brief Task to request the opening of a view
102 struct GNUNET_MQ_Envelope *open_queue_env;
104 /* Callbacks + Closures */
106 * @brief Callback called when a new communicator connects
108 GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback communicator_available_cb;
111 * @brief Callback called when a new communicator connects
113 GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb;
116 * @brief Callback called when a new communicator connects
118 GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb;
121 * @brief Callback called when a new communicator connects
123 GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb;
126 * @brief Closure to the callback
133 * @brief Check whether incoming msg indicating available communicator is
137 * @param msg Message struct
139 * @return GNUNET_YES in case message is correct
142 check_communicator_available (void *cls,
143 const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
147 size = ntohs (msg->header.size) - sizeof (*msg);
149 return GNUNET_OK; /* receive-only communicator */
150 GNUNET_MQ_check_zero_termination (msg);
156 * @brief Handle new communicator
159 * @param msg Message struct
162 handle_communicator_available (void *cls,
163 const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
165 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
168 size = ntohs (msg->header.size) - sizeof (*msg);
170 return; /* receive-only communicator */
171 tc_h->c_characteristics = ntohl (msg->cc);
172 tc_h->c_addr_prefix = GNUNET_strdup ((const char *) &msg[1]);
173 if (NULL != tc_h->communicator_available_cb)
175 LOG (GNUNET_ERROR_TYPE_DEBUG,
176 "calling communicator_available_cb()\n");
177 tc_h->communicator_available_cb (tc_h->cb_cls,
179 tc_h->c_characteristics,
180 tc_h->c_addr_prefix);
182 GNUNET_SERVICE_client_continue (tc_h->client);
187 * Address of our peer added. Test message is well-formed.
189 * @param cls the client
190 * @param aam the send message that was sent
191 * @return #GNUNET_OK if message is well-formed
194 check_add_address (void *cls,
195 const struct GNUNET_TRANSPORT_AddAddressMessage *msg)
197 struct TransportClient *tc = cls;
199 //if (CT_COMMUNICATOR != tc->type)
202 // return GNUNET_SYSERR;
204 GNUNET_MQ_check_zero_termination (msg);
210 handle_add_address (void *cls,
211 const struct GNUNET_TRANSPORT_AddAddressMessage *msg)
213 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
216 size = ntohs (msg->header.size) - sizeof (*msg);
218 return; /* receive-only communicator */
219 tc_h->c_address = GNUNET_strdup ((const char *) &msg[1]);
220 if (NULL != tc_h->add_address_cb)
222 LOG (GNUNET_ERROR_TYPE_DEBUG,
223 "calling communicator_available()\n");
224 tc_h->add_address_cb (tc_h->cb_cls,
227 GNUNET_TIME_relative_ntoh (msg->expiration),
231 GNUNET_SERVICE_client_continue (tc_h->client);
236 handle_queue_create_ok (void *cls,
237 const struct GNUNET_TRANSPORT_CreateQueueResponse *msg)
239 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
241 if (NULL != tc_h->queue_create_reply_cb)
243 tc_h->queue_create_reply_cb (tc_h->cb_cls,
247 GNUNET_SERVICE_client_continue (tc_h->client);
252 handle_queue_create_fail (void *cls,
253 const struct GNUNET_TRANSPORT_CreateQueueResponse *msg)
255 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
257 if (NULL != tc_h->queue_create_reply_cb)
259 tc_h->queue_create_reply_cb (tc_h->cb_cls,
263 GNUNET_SERVICE_client_continue (tc_h->client);
268 * New queue became available. Check message.
270 * @param cls the client
271 * @param aqm the send message that was sent
274 check_add_queue_message (void *cls,
275 const struct GNUNET_TRANSPORT_AddQueueMessage *aqm)
277 GNUNET_MQ_check_zero_termination (aqm);
283 * @brief Handle new communicator
286 * @param msg Message struct
289 handle_add_queue_message (void *cls,
290 const struct GNUNET_TRANSPORT_AddQueueMessage *msg)
292 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
294 if (NULL != tc_h->add_queue_cb)
296 tc_h->add_queue_cb (tc_h->cb_cls,
299 GNUNET_SERVICE_client_continue (tc_h->client);
304 * @brief Shut down the service
306 * @param cls Closure - Handle to the service
309 shutdown_service (void *cls)
311 struct GNUNET_SERVICE_Handle *h = cls;
313 GNUNET_SERVICE_stop (h);
318 * @brief Callback called when new Client (Communicator) connects
320 * @param cls Closure - TransporCommmunicator Handle
321 * @param client Client
322 * @param mq Messagequeue
324 * @return TransportCommunicator Handle
327 connect_cb (void *cls,
328 struct GNUNET_SERVICE_Client *client,
329 struct GNUNET_MQ_Handle *mq)
331 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
333 LOG (GNUNET_ERROR_TYPE_DEBUG,
334 "Client connected.\n");
335 tc_h->client = client;
338 if (NULL != tc_h->open_queue_env)
340 GNUNET_MQ_send (tc_h->c_mq,
341 tc_h->open_queue_env);
342 tc_h->open_queue_env = NULL;
349 * @brief Callback called when Client disconnects
351 * @param cls Closure - TransportCommunicator Handle
352 * @param client Client
353 * @param internal_cls TransporCommmunicator Handle
356 disconnect_cb (void *cls,
357 struct GNUNET_SERVICE_Client *client,
360 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
362 LOG (GNUNET_ERROR_TYPE_DEBUG,
363 "Client disconnected.\n");
369 * @brief Start the communicator part of the transport service
371 * @param communicator_available Callback to be called when a new communicator
373 * @param cfg Configuration
376 transport_communicator_start (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
378 struct GNUNET_MQ_MessageHandler mh[] = {
379 GNUNET_MQ_hd_var_size (communicator_available,
380 GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR,
381 struct GNUNET_TRANSPORT_CommunicatorAvailableMessage,
383 //GNUNET_MQ_hd_var_size (communicator_backchannel,
384 // GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL,
385 // struct GNUNET_TRANSPORT_CommunicatorBackchannel,
387 GNUNET_MQ_hd_var_size (add_address,
388 GNUNET_MESSAGE_TYPE_TRANSPORT_ADD_ADDRESS,
389 struct GNUNET_TRANSPORT_AddAddressMessage,
391 //GNUNET_MQ_hd_fixed_size (del_address,
392 // GNUNET_MESSAGE_TYPE_TRANSPORT_DEL_ADDRESS,
393 // struct GNUNET_TRANSPORT_DelAddressMessage,
395 //GNUNET_MQ_hd_var_size (incoming_msg,
396 // GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG,
397 // struct GNUNET_TRANSPORT_IncomingMessage,
399 GNUNET_MQ_hd_fixed_size (queue_create_ok,
400 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_OK,
401 struct GNUNET_TRANSPORT_CreateQueueResponse,
403 GNUNET_MQ_hd_fixed_size (queue_create_fail,
404 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL,
405 struct GNUNET_TRANSPORT_CreateQueueResponse,
407 GNUNET_MQ_hd_var_size (add_queue_message,
408 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_SETUP,
409 struct GNUNET_TRANSPORT_AddQueueMessage,
411 //GNUNET_MQ_hd_fixed_size (del_queue_message,
412 // GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_TEARDOWN,
413 // struct GNUNET_TRANSPORT_DelQueueMessage,
415 //GNUNET_MQ_hd_fixed_size (send_message_ack,
416 // GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG_ACK,
417 // struct GNUNET_TRANSPORT_SendMessageToAck,
420 struct GNUNET_SERVICE_Handle *h;
422 h = GNUNET_SERVICE_start ("transport",
429 LOG (GNUNET_ERROR_TYPE_ERROR,
430 "Failed starting service!\n");
433 LOG (GNUNET_ERROR_TYPE_DEBUG,
434 "Started service\n");
435 /* TODO */ GNUNET_SCHEDULER_add_shutdown (&shutdown_service, h);
441 * @brief Task run at shutdown to kill communicator and clean up
443 * @param cls Closure - Process of communicator
446 shutdown_communicator (void *cls)
448 struct GNUNET_OS_Process *proc = cls;
450 if (GNUNET_OK != GNUNET_OS_process_kill (proc,
453 LOG (GNUNET_ERROR_TYPE_WARNING,
454 "Error shutting down communicator with SIGERM, trying SIGKILL\n");
455 if (GNUNET_OK != GNUNET_OS_process_kill (proc,
458 LOG (GNUNET_ERROR_TYPE_ERROR,
459 "Error shutting down communicator with SIGERM and SIGKILL\n");
462 GNUNET_OS_process_destroy (proc);
467 * @brief Start the communicator
469 * @param cfgname Name of the communicator
472 communicator_start (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
476 LOG (GNUNET_ERROR_TYPE_DEBUG,
477 "communicator_start\n");
478 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-communicator-unix");
480 GNUNET_OS_start_process (GNUNET_YES,
481 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
484 "./gnunet-communicator-unix",
488 if (NULL == tc_h->c_proc)
490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
491 "Failed to start communicator!");
494 LOG (GNUNET_ERROR_TYPE_DEBUG,
495 "started communicator\n");
496 GNUNET_free (binary);
497 /* TODO */ GNUNET_SCHEDULER_add_shutdown (&shutdown_communicator,
503 * @brief Start communicator part of transport service and communicator
505 * @param service_name Name of the service
506 * @param cfg Configuration handle
507 * @param communicator_available_cb Callback that is called when a new
508 * @param add_address_cb Callback that is called when a new
509 * communicator becomes available
510 * @param cb_cls Closure to @a communicator_available_cb and @a
512 * @return Handle to the communicator duo
514 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
515 GNUNET_TRANSPORT_TESTING_transport_communicator_service_start
516 (const char *service_name,
517 const char *cfg_filename,
518 GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback communicator_available_cb,
519 GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb,
520 GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb,
521 GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb,
524 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h;
526 tc_h = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle);
527 tc_h->cfg_filename = GNUNET_strdup (cfg_filename);
528 tc_h->cfg = GNUNET_CONFIGURATION_create ();
529 if ( (GNUNET_SYSERR ==
530 GNUNET_CONFIGURATION_load (tc_h->cfg,
533 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
534 _("Malformed configuration file `%s', exit ...\n"),
538 tc_h->communicator_available_cb = communicator_available_cb;
539 tc_h->add_address_cb = add_address_cb;
540 tc_h->queue_create_reply_cb = queue_create_reply_cb;
541 tc_h->add_queue_cb = add_queue_cb;
542 tc_h->cb_cls = cb_cls;
544 /* Start communicator part of service */
545 transport_communicator_start (tc_h);
547 /* Schedule start communicator */
548 communicator_start (tc_h);
554 GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue
555 (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
556 const struct GNUNET_PeerIdentity *peer_id,
559 static uint32_t idgen;
561 struct GNUNET_TRANSPORT_CreateQueue *msg;
562 struct GNUNET_MQ_Envelope *env;
565 if (NULL != tc_h->open_queue_env)
567 // FIXME: handle multiple queue requests
568 return; /* Already waiting for opening of queue */
570 prefix = GNUNET_HELLO_address_to_prefix (address);
573 GNUNET_break (0); /* We got an invalid address!? */
576 alen = strlen (address) + 1;
577 env = GNUNET_MQ_msg_extra (msg,
579 GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE);
580 msg->request_id = htonl (idgen++);
581 msg->receiver = *peer_id;
585 if (NULL != tc_h->c_mq)
587 GNUNET_MQ_send (tc_h->c_mq,
592 tc_h->open_queue_env = env;
596 //struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission *
597 //GNUNET_TRANSPORT_TESTING_transport_communicator_send
598 // (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tcq,
599 // const struct GNUNET_MessageHeader *hdr,
600 // GNUNET_TRANSPORT_TESTING_SuccessStatus cb, void *cb_cls);