230c35b4f8125a5d906f0ccad5011bcd3b684e58
[oweals/gnunet.git] / src / transport / transport-testing2.c
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
37
38 #define LOG(kind, ...) GNUNET_log_from (kind, "transport-testing2", __VA_ARGS__)
39
40
41 /**
42  * @brief Handle to a transport communicator
43  */
44 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
45 {
46   /**
47    * @brief Handle to the configuration
48    */
49   struct GNUNET_CONFIGURATION_Handle *cfg;
50
51   /**
52    * @brief File name of configuration file
53    */
54   char *cfg_filename;
55
56   /**
57    * @brief Handle to the transport service
58    */
59   struct GNUNET_SERVICE_Handle *tsh;
60
61   /**
62    * @brief Task that will be run on shutdown to stop and clean transport
63    * service
64    */
65   struct GNUNET_SCHEDULER_Task *ts_shutdown_task;
66
67   /**
68    * @brief Handle to the client
69    */
70   struct GNUNET_SERVICE_Client *client;
71
72   /**
73    * @brief Handle to the client
74    */
75   struct GNUNET_MQ_Handle *c_mq;
76
77   /**
78    * @brief Process of the communicator
79    */
80   struct GNUNET_OS_Process *c_proc;
81
82   /**
83    * @brief Task that will be run on shutdown to stop and clean communicator
84    */
85   struct GNUNET_SCHEDULER_Task *c_shutdown_task;
86
87   /**
88    * @brief Characteristics of the communicator
89    */
90   enum GNUNET_TRANSPORT_CommunicatorCharacteristics c_characteristics;
91
92   /**
93    * @brief Specifies supported addresses
94    */
95   char *c_addr_prefix;
96
97   /**
98    * @brief Specifies supported addresses
99    */
100   char *c_address;
101
102   /**
103    * @brief Head of the DLL of queues associated with this communicator
104    */
105   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *queue_head;
106
107   /**
108    * @brief Tail of the DLL of queues associated with this communicator
109    */
110   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *queue_tail;
111
112   /* Callbacks + Closures */
113   /**
114    * @brief Callback called when a new communicator connects
115    */
116   GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback
117     communicator_available_cb;
118
119   /**
120    * @brief Callback called when a new communicator connects
121    */
122   GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb;
123
124   /**
125    * @brief Callback called when a new communicator connects
126    */
127   GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb;
128
129   /**
130    * @brief Callback called when a new communicator connects
131    */
132   GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb;
133
134   /**
135    * @brief Callback called when a new communicator connects
136    */
137   GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_msg_cb;
138
139   /**
140    * @brief Closure to the callback
141    */
142   void *cb_cls;
143 };
144
145
146 /**
147  * @brief Queue of a communicator and some context
148  */
149 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue
150 {
151   /**
152    * @brief Handle to the TransportCommunicator
153    */
154   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h;
155
156   /**
157    * @brief Envelope to a message that requests the opening of the queue.
158    *
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.
162    */
163   struct GNUNET_MQ_Envelope *open_queue_env;
164
165   /**
166    * @brief Peer ID of the peer on the other side of the queue
167    */
168   struct GNUNET_PeerIdentity peer_id;
169
170   /**
171    * @brief Queue ID
172    */
173   uint32_t qid;
174
175   /**
176    * @brief Current message id
177    */
178   uint64_t mid;
179
180   /**
181    * An `enum GNUNET_NetworkType` in NBO.
182    */
183   uint32_t nt;
184
185   /**
186    * Maximum transmission unit, in NBO.  UINT32_MAX for unlimited.
187    */
188   uint32_t mtu;
189
190   /**
191    * An `enum GNUNET_TRANSPORT_ConnectionStatus` in NBO.
192    */
193   uint32_t cs;
194
195   /**
196    * @brief Next element inside a DLL
197    */
198   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *next;
199
200   /**
201    * @brief Previous element inside a DLL
202    */
203   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *prev;
204 };
205
206
207 /**
208  * @brief Handle/Context to a single transmission
209  */
210 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission
211 {
212 };
213
214
215 /**
216  * @brief Check whether incoming msg indicating available communicator is
217  * correct
218  *
219  * @param cls Closure
220  * @param msg Message struct
221  *
222  * @return GNUNET_YES in case message is correct
223  */
224 static int
225 check_communicator_available (
226   void *cls,
227   const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
228 {
229   uint16_t size;
230
231   size = ntohs (msg->header.size) - sizeof(*msg);
232   if (0 == size)
233     return GNUNET_OK; /* receive-only communicator */
234   GNUNET_MQ_check_zero_termination (msg);
235   return GNUNET_OK;
236 }
237
238
239 /**
240  * @brief Handle new communicator
241  *
242  * Store characteristics of communicator, call respective client callback.
243  *
244  * @param cls Closure - communicator handle
245  * @param msg Message struct
246  */
247 static void
248 handle_communicator_available (
249   void *cls,
250   const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
251 {
252   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
253   uint16_t size;
254
255   size = ntohs (msg->header.size) - sizeof(*msg);
256   if (0 == size)
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)
261   {
262     LOG (GNUNET_ERROR_TYPE_DEBUG, "calling communicator_available_cb()\n");
263     tc_h->communicator_available_cb (tc_h->cb_cls,
264                                      tc_h,
265                                      tc_h->c_characteristics,
266                                      tc_h->c_addr_prefix);
267   }
268   GNUNET_SERVICE_client_continue (tc_h->client);
269 }
270
271
272 /**
273  * Address of our peer added.  Test message is well-formed.
274  *
275  * @param cls the client
276  * @param aam the send message that was sent
277  * @return #GNUNET_OK if message is well-formed
278  */
279 static int
280 check_add_address (void *cls,
281                    const struct GNUNET_TRANSPORT_AddAddressMessage *msg)
282 {
283   struct TransportClient *tc = cls;
284
285   // if (CT_COMMUNICATOR != tc->type)
286   // {
287   //  GNUNET_break (0);
288   //  return GNUNET_SYSERR;
289   // }
290   GNUNET_MQ_check_zero_termination (msg);
291   return GNUNET_OK;
292 }
293
294
295 /**
296  * @brief The communicator informs about an address.
297  *
298  * Store address and call client callback.
299  *
300  * @param cls Closure - communicator handle
301  * @param msg Message
302  */
303 static void
304 handle_add_address (void *cls,
305                     const struct GNUNET_TRANSPORT_AddAddressMessage *msg)
306 {
307   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
308   uint16_t size;
309   size = ntohs (msg->header.size) - sizeof(*msg);
310   if (0 == size)
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)
315   {
316     LOG (GNUNET_ERROR_TYPE_DEBUG, "calling add_address_cb()\n");
317     tc_h->add_address_cb (tc_h->cb_cls,
318                           tc_h,
319                           tc_h->c_address,
320                           GNUNET_TIME_relative_ntoh (msg->expiration),
321                           msg->aid,
322                           ntohl (msg->nt));
323   }
324   GNUNET_SERVICE_client_continue (tc_h->client);
325 }
326
327
328 /**
329  * Incoming message.  Test message is well-formed.
330  *
331  * @param cls the client
332  * @param msg the send message that was sent
333  * @return #GNUNET_OK if message is well-formed
334  */
335 static int
336 check_incoming_msg (void *cls,
337                     const struct GNUNET_TRANSPORT_IncomingMessage *msg)
338 {
339   // struct TransportClient *tc = cls;
340
341   // if (CT_COMMUNICATOR != tc->type)
342   // {
343   //  GNUNET_break (0);
344   //  return GNUNET_SYSERR;
345   // }
346   GNUNET_MQ_check_boxed_message (msg);
347   return GNUNET_OK;
348 }
349
350
351 /**
352  * @brief Receive an incoming message.
353  *
354  * Pass the message to the client.
355  *
356  * @param cls Closure - communicator handle
357  * @param msg Message
358  */
359 static void
360 handle_incoming_msg (void *cls,
361                      const struct GNUNET_TRANSPORT_IncomingMessage *inc_msg)
362 {
363   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
364   struct GNUNET_MessageHeader *msg;
365   msg = (struct GNUNET_MessageHeader *)&inc_msg[1];
366   size_t payload_len = ntohs (msg->size) - sizeof (struct
367                                                           GNUNET_MessageHeader);
368
369   if (NULL != tc_h->incoming_msg_cb)
370   {
371     tc_h->incoming_msg_cb (tc_h->cb_cls,
372                            tc_h,
373                            (char*) &msg[1],
374                            payload_len);
375   }
376   else
377   {
378     LOG (GNUNET_ERROR_TYPE_WARNING,
379          "Incoming message from communicator but no handler!\n");
380   }
381   if (0 != ntohl (inc_msg->fc_on))
382   {
383     /* send ACK when done to communicator for flow control! */
384     struct GNUNET_MQ_Envelope *env;
385     struct GNUNET_TRANSPORT_IncomingMessageAck *ack;
386
387     env = GNUNET_MQ_msg (ack, GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG_ACK);
388     ack->reserved = htonl (0);
389     ack->fc_id = inc_msg->fc_id;
390     ack->sender = inc_msg->sender;
391     GNUNET_MQ_send (tc_h->c_mq, env);
392   }
393
394   GNUNET_SERVICE_client_continue (tc_h->client);
395 }
396
397
398 /**
399  * @brief Communicator informs that it tries to establish requested queue
400  *
401  * @param cls Closure - communicator handle
402  * @param msg Message
403  */
404 static void
405 handle_queue_create_ok (void *cls,
406                         const struct GNUNET_TRANSPORT_CreateQueueResponse *msg)
407 {
408   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
409
410   if (NULL != tc_h->queue_create_reply_cb)
411   {
412     tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_YES);
413   }
414   GNUNET_SERVICE_client_continue (tc_h->client);
415 }
416
417
418 /**
419  * @brief Communicator informs that it wont try establishing requested queue.
420  *
421  * It will not do so probably because the address is bougus (see comment to
422  * #GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL)
423  *
424  * @param cls Closure - communicator handle
425  * @param msg Message
426  */
427 static void
428 handle_queue_create_fail (
429   void *cls,
430   const struct GNUNET_TRANSPORT_CreateQueueResponse *msg)
431 {
432   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
433
434   if (NULL != tc_h->queue_create_reply_cb)
435   {
436     tc_h->queue_create_reply_cb (tc_h->cb_cls, tc_h, GNUNET_NO);
437   }
438   GNUNET_SERVICE_client_continue (tc_h->client);
439 }
440
441
442 /**
443  * New queue became available.  Check message.
444  *
445  * @param cls the client
446  * @param aqm the send message that was sent
447  */
448 static int
449 check_add_queue_message (void *cls,
450                          const struct GNUNET_TRANSPORT_AddQueueMessage *aqm)
451 {
452   GNUNET_MQ_check_zero_termination (aqm);
453   return GNUNET_OK;
454 }
455
456
457 /**
458  * @brief Handle new queue
459  *
460  * Store context and call client callback.
461  *
462  * @param cls Closure - communicator handle
463  * @param msg Message struct
464  */
465 static void
466 handle_add_queue_message (void *cls,
467                           const struct GNUNET_TRANSPORT_AddQueueMessage *msg)
468 {
469   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
470   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue;
471
472   tc_queue = tc_h->queue_head;
473   if (NULL != tc_queue)
474   {
475     while (tc_queue->qid != msg->qid)
476     {
477       tc_queue = tc_queue->next;
478     }
479   }
480   else
481   {
482     tc_queue =
483       GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue);
484     tc_queue->tc_h = tc_h;
485     tc_queue->qid = msg->qid;
486     tc_queue->peer_id = msg->receiver;
487     GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue);
488   }
489   GNUNET_assert (tc_queue->qid == msg->qid);
490   GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver));
491   tc_queue->nt = msg->nt;
492   tc_queue->mtu = msg->mtu;
493   tc_queue->cs = msg->cs;
494   if (NULL != tc_h->add_queue_cb)
495   {
496     tc_h->add_queue_cb (tc_h->cb_cls, tc_h, tc_queue);
497   }
498   GNUNET_SERVICE_client_continue (tc_h->client);
499 }
500
501
502 /**
503  * @brief Shut down the service
504  *
505  * @param cls Closure - Handle to the service
506  */
507 static void
508 shutdown_service (void *cls)
509 {
510   struct GNUNET_SERVICE_Handle *h = cls;
511
512   GNUNET_SERVICE_stop (h);
513 }
514
515
516 /**
517  * @brief Callback called when new Client (Communicator) connects
518  *
519  * @param cls Closure - TransporCommmunicator Handle
520  * @param client Client
521  * @param mq Messagequeue
522  *
523  * @return TransportCommunicator Handle
524  */
525 static void *
526 connect_cb (void *cls,
527             struct GNUNET_SERVICE_Client *client,
528             struct GNUNET_MQ_Handle *mq)
529 {
530   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
531   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue_iter;
532
533   LOG (GNUNET_ERROR_TYPE_DEBUG, "Client connected.\n");
534   tc_h->client = client;
535   tc_h->c_mq = mq;
536
537   if (NULL == tc_h->queue_head)
538     return tc_h;
539   /* Iterate over queues. They are yet to be opened. Request opening. */
540   while (NULL != (tc_queue_iter = tc_h->queue_head))
541   {
542     if (NULL == tc_queue_iter->open_queue_env)
543       continue;
544     /* Send the previously created mq envelope to request the creation of the
545      * queue. */
546     GNUNET_MQ_send (tc_h->c_mq, tc_queue_iter->open_queue_env);
547     tc_queue_iter->open_queue_env = NULL;
548   }
549   return tc_h;
550 }
551
552
553 /**
554  * @brief Callback called when Client disconnects
555  *
556  * @param cls Closure - TransportCommunicator Handle
557  * @param client Client
558  * @param internal_cls TransporCommmunicator Handle
559  */
560 static void
561 disconnect_cb (void *cls,
562                struct GNUNET_SERVICE_Client *client,
563                void *internal_cls)
564 {
565   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
566
567   LOG (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected.\n");
568   tc_h->client = NULL;
569 }
570
571
572 /**
573  * @brief Start the communicator part of the transport service
574  *
575  * @param communicator_available Callback to be called when a new communicator
576  * becomes available
577  * @param cfg Configuration
578  */
579 static void
580 transport_communicator_start (
581   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
582 {
583   struct GNUNET_MQ_MessageHandler mh[] = {
584     GNUNET_MQ_hd_var_size (communicator_available,
585                            GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR,
586                            struct GNUNET_TRANSPORT_CommunicatorAvailableMessage,
587                            &tc_h),
588     // GNUNET_MQ_hd_var_size (communicator_backchannel,
589     //    GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL,
590     //    struct GNUNET_TRANSPORT_CommunicatorBackchannel,
591     //    NULL),
592     GNUNET_MQ_hd_var_size (add_address,
593                            GNUNET_MESSAGE_TYPE_TRANSPORT_ADD_ADDRESS,
594                            struct GNUNET_TRANSPORT_AddAddressMessage,
595                            &tc_h),
596     // GNUNET_MQ_hd_fixed_size (del_address,
597     //                         GNUNET_MESSAGE_TYPE_TRANSPORT_DEL_ADDRESS,
598     //                         struct GNUNET_TRANSPORT_DelAddressMessage,
599     //                         NULL),
600     GNUNET_MQ_hd_var_size (incoming_msg,
601                            GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG,
602                            struct GNUNET_TRANSPORT_IncomingMessage,
603                            NULL),
604     GNUNET_MQ_hd_fixed_size (queue_create_ok,
605                              GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_OK,
606                              struct GNUNET_TRANSPORT_CreateQueueResponse,
607                              tc_h),
608     GNUNET_MQ_hd_fixed_size (queue_create_fail,
609                              GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL,
610                              struct GNUNET_TRANSPORT_CreateQueueResponse,
611                              tc_h),
612     GNUNET_MQ_hd_var_size (add_queue_message,
613                            GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_SETUP,
614                            struct GNUNET_TRANSPORT_AddQueueMessage,
615                            NULL),
616     // GNUNET_MQ_hd_fixed_size (del_queue_message,
617     //                         GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_TEARDOWN,
618     //                         struct GNUNET_TRANSPORT_DelQueueMessage,
619     //                         NULL),
620     // GNUNET_MQ_hd_fixed_size (send_message_ack,
621     //                         GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG_ACK,
622     //                         struct GNUNET_TRANSPORT_SendMessageToAck,
623     //                         NULL),
624   };
625   struct GNUNET_SERVICE_Handle *h;
626
627   h = GNUNET_SERVICE_start ("transport",
628                             tc_h->cfg,
629                             &connect_cb,
630                             &disconnect_cb,
631                             tc_h,
632                             mh);
633   if (NULL == h)
634     LOG (GNUNET_ERROR_TYPE_ERROR, "Failed starting service!\n");
635   else
636   {
637     LOG (GNUNET_ERROR_TYPE_DEBUG, "Started service\n");
638     /* TODO */ GNUNET_SCHEDULER_add_shutdown (&shutdown_service, h);
639   }
640 }
641
642
643 /**
644  * @brief Task run at shutdown to kill communicator and clean up
645  *
646  * @param cls Closure - Process of communicator
647  */
648 static void
649 shutdown_communicator (void *cls)
650 {
651   struct GNUNET_OS_Process *proc = cls;
652
653   if (GNUNET_OK != GNUNET_OS_process_kill (proc, SIGTERM))
654   {
655     LOG (GNUNET_ERROR_TYPE_WARNING,
656          "Error shutting down communicator with SIGERM, trying SIGKILL\n");
657     if (GNUNET_OK != GNUNET_OS_process_kill (proc, SIGKILL))
658     {
659       LOG (GNUNET_ERROR_TYPE_ERROR,
660            "Error shutting down communicator with SIGERM and SIGKILL\n");
661     }
662   }
663   GNUNET_OS_process_destroy (proc);
664 }
665
666
667 /**
668  * @brief Start the communicator
669  *
670  * @param cfgname Name of the communicator
671  */
672 static void
673 communicator_start (
674   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
675   const char *binary_name)
676 {
677   char *binary;
678
679   LOG (GNUNET_ERROR_TYPE_DEBUG, "communicator_start\n");
680   binary = GNUNET_OS_get_libexec_binary_path (binary_name);
681   tc_h->c_proc = GNUNET_OS_start_process (GNUNET_YES,
682                                           GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
683                                           NULL,
684                                           NULL,
685                                           NULL,
686                                           binary,
687                                           binary_name,
688                                           "-c",
689                                           tc_h->cfg_filename,
690                                           NULL);
691   if (NULL == tc_h->c_proc)
692   {
693     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start communicator!");
694     return;
695   }
696   LOG (GNUNET_ERROR_TYPE_INFO, "started communicator\n");
697   GNUNET_free (binary);
698   /* TODO */ GNUNET_SCHEDULER_add_shutdown (&shutdown_communicator,
699                                             tc_h->c_proc);
700 }
701
702
703 /**
704  * @brief Start communicator part of transport service and communicator
705  *
706  * @param service_name Name of the service
707  * @param cfg Configuration handle
708  * @param communicator_available_cb Callback that is called when a new
709  * @param add_address_cb Callback that is called when a new
710  * communicator becomes available
711  * @param cb_cls Closure to @a communicator_available_cb and @a
712  *
713  * @return Handle to the communicator duo
714  */
715 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
716 GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
717   const char *service_name,
718   const char *binary_name,
719   const char *cfg_filename,
720   GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback
721   communicator_available_cb,
722   GNUNET_TRANSPORT_TESTING_AddAddressCallback add_address_cb,
723   GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback queue_create_reply_cb,
724   GNUNET_TRANSPORT_TESTING_AddQueueCallback add_queue_cb,
725   GNUNET_TRANSPORT_TESTING_IncomingMessageCallback incoming_message_cb,
726   void *cb_cls)
727 {
728   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h;
729
730   tc_h =
731     GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle);
732   tc_h->cfg_filename = GNUNET_strdup (cfg_filename);
733   tc_h->cfg = GNUNET_CONFIGURATION_create ();
734   if ((GNUNET_SYSERR == GNUNET_CONFIGURATION_load (tc_h->cfg, cfg_filename)))
735   {
736     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
737                 _ ("Malformed configuration file `%s', exit ...\n"),
738                 cfg_filename);
739     GNUNET_free (tc_h->cfg_filename);
740     GNUNET_CONFIGURATION_destroy (tc_h->cfg);
741     GNUNET_free (tc_h);
742     return NULL;
743   }
744   tc_h->communicator_available_cb = communicator_available_cb;
745   tc_h->add_address_cb = add_address_cb;
746   tc_h->queue_create_reply_cb = queue_create_reply_cb;
747   tc_h->add_queue_cb = add_queue_cb;
748   tc_h->incoming_msg_cb = incoming_message_cb;
749   tc_h->cb_cls = cb_cls;
750
751   /* Start communicator part of service */
752   transport_communicator_start (tc_h);
753
754   /* Schedule start communicator */
755   communicator_start (tc_h,
756                       binary_name);
757   return tc_h;
758 }
759
760
761 /**
762  * @brief Instruct communicator to open a queue
763  *
764  * @param tc_h Handle to communicator which shall open queue
765  * @param peer_id Towards which peer
766  * @param address For which address
767  */
768 void
769 GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (
770   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
771   const struct GNUNET_PeerIdentity *peer_id,
772   const char *address)
773 {
774   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue;
775   static uint32_t idgen;
776   char *prefix;
777   struct GNUNET_TRANSPORT_CreateQueue *msg;
778   struct GNUNET_MQ_Envelope *env;
779   size_t alen;
780
781   tc_queue =
782     GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue);
783   tc_queue->tc_h = tc_h;
784   prefix = GNUNET_HELLO_address_to_prefix (address);
785   if (NULL == prefix)
786   {
787     GNUNET_break (0);  /* We got an invalid address!? */
788     GNUNET_free (tc_queue);
789     return;
790   }
791   GNUNET_free (prefix);
792   alen = strlen (address) + 1;
793   env =
794     GNUNET_MQ_msg_extra (msg, alen, GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE);
795   msg->request_id = htonl (idgen++);
796   tc_queue->qid = msg->request_id;
797   msg->receiver = *peer_id;
798   tc_queue->peer_id = *peer_id;
799   memcpy (&msg[1], address, alen);
800   if (NULL != tc_h->c_mq)
801   {
802     GNUNET_MQ_send (tc_h->c_mq, env);
803   }
804   else
805   {
806     tc_queue->open_queue_env = env;
807   }
808   GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue);
809 }
810
811
812 /**
813  * @brief Instruct communicator to send data
814  *
815  * @param tc_queue The queue to use for sending
816  * @param payload Data to send
817  * @param payload_size Size of the payload
818  *
819  * @return Handle to the transmission
820  */
821 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission *
822 GNUNET_TRANSPORT_TESTING_transport_communicator_send
823   (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue,
824   const void *payload,
825   size_t payload_size)
826 {
827   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission *tc_t;
828   struct GNUNET_MessageHeader *mh;
829   struct GNUNET_TRANSPORT_SendMessageTo *msg;
830   struct GNUNET_MQ_Envelope *env;
831   size_t inbox_size;
832
833   inbox_size = sizeof(struct GNUNET_MessageHeader) + payload_size;
834   mh = GNUNET_malloc (inbox_size);
835   mh->size = htons (inbox_size);
836   mh->type = GNUNET_MESSAGE_TYPE_DUMMY;
837   memcpy (&mh[1],
838           payload,
839           payload_size);
840   env = GNUNET_MQ_msg_extra (msg,
841                              inbox_size,
842                              GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG);
843   msg->qid = htonl (tc_queue->qid);
844   msg->mid = tc_queue->mid++;
845   msg->receiver = tc_queue->peer_id;
846   memcpy (&msg[1], mh, inbox_size);
847   GNUNET_free (mh);
848   GNUNET_MQ_send (tc_queue->tc_h->c_mq, env);
849   return tc_t;
850 }