Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / transport / test_communicator_unix.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/test_communicator_unix.c
23  * @brief test the unix communicator
24  * @author Julius Bünger
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "transport-testing2.h"
29 #include "gnunet_ats_transport_service.h"
30 #include "gnunet_signatures.h"
31 #include "transport.h"
32
33 #include <inttypes.h>
34
35
36 #define LOG(kind, ...) GNUNET_log_from (kind, \
37                                         "test_transport_communicator_unix", \
38                                         __VA_ARGS__)
39
40 #define NUM_PEERS 2
41
42 static struct GNUNET_PeerIdentity peer_id[NUM_PEERS];
43
44 static struct
45 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS];
46
47 // static char *addresses[NUM_PEERS];
48
49
50 #define PAYLOAD_SIZE 256
51
52 // static char payload[PAYLOAD_SIZE] = "TEST PAYLOAD";
53 // static char payload[] = "TEST PAYLOAD";
54 static uint32_t payload = 42;
55
56
57 static void
58 communicator_available_cb (void *cls,
59                            struct
60                            GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
61                            *tc_h,
62                            enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc,
63                            char *address_prefix)
64 {
65   LOG (GNUNET_ERROR_TYPE_DEBUG,
66        "Communicator available. (cc: %u, prefix: %s)\n",
67        cc,
68        address_prefix);
69 }
70
71
72 static void
73 add_address_cb (void *cls,
74                 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
75                 tc_h,
76                 const char *address,
77                 struct GNUNET_TIME_Relative expiration,
78                 uint32_t aid,
79                 enum GNUNET_NetworkType nt)
80 {
81   LOG (GNUNET_ERROR_TYPE_DEBUG,
82        "New address. (addr: %s, expir: %" PRIu32 ", ID: %" PRIu32 ", nt: %u\n",
83        address,
84        expiration.rel_value_us,
85        aid,
86        nt);
87   // addresses[1] = GNUNET_strdup (address);
88   GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (tc_hs[0],
89                                                               &peer_id[1],
90                                                               address);
91 }
92
93
94 /**
95  * @brief Callback that informs whether the requested queue will be
96  * established
97  *
98  * Implements #GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback.
99  *
100  * @param cls Closure - unused
101  * @param tc_h Communicator handle - unused
102  * @param will_try #GNUNET_YES if queue will be established
103  *                #GNUNET_NO if queue will not be established (bogous address)
104  */
105 static void
106 queue_create_reply_cb (void *cls,
107                        struct
108                        GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
109                        tc_h,
110                        int will_try)
111 {
112   if (GNUNET_YES == will_try)
113     LOG (GNUNET_ERROR_TYPE_DEBUG,
114          "Queue will be established!\n");
115   else
116     LOG (GNUNET_ERROR_TYPE_WARNING,
117          "Queue won't be established (bougus address?)!\n");
118 }
119
120
121 /**
122  * @brief Handle opening of queue
123  *
124  * Issues sending of test data
125  *
126  * Implements #GNUNET_TRANSPORT_TESTING_AddQueueCallback
127  *
128  * @param cls Closure
129  * @param tc_h Communicator handle
130  * @param tc_queue Handle to newly opened queue
131  */
132 static void
133 add_queue_cb (void *cls,
134               struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
135               struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *
136               tc_queue)
137 {
138   LOG (GNUNET_ERROR_TYPE_DEBUG,
139        "Got Queue!\n");
140   GNUNET_TRANSPORT_TESTING_transport_communicator_send (tc_queue,
141                                                         &payload,
142                                                         sizeof(payload));
143 }
144
145
146 /**
147  * @brief Handle an incoming message
148  *
149  * Implements #GNUNET_TRANSPORT_TESTING_IncomingMessageCallback
150
151  * @param cls Closure
152  * @param tc_h Handle to the receiving communicator
153  * @param msg Received message
154  */
155 void
156 incoming_message_cb (void *cls,
157                      struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
158                      *tc_h,
159                      const struct GNUNET_MessageHeader *msg)
160 {
161 }
162
163
164 /**
165  * @brief Main function called by the scheduler
166  *
167  * @param cls Closure - Handle to configuration
168  */
169 static void
170 run (void *cls)
171 {
172   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
173
174   tc_hs[0] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
175     "transport",
176     "gnunet-communicator-unix",
177     "test_communicator_1.conf",
178     &communicator_available_cb,
179     NULL,
180     &queue_create_reply_cb,
181     &add_queue_cb,
182     NULL,
183     NULL);   /* cls */
184   tc_hs[1] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
185     "transport",
186     "gnunet-communicator-unix",
187     "test_communicator_2.conf",
188     &communicator_available_cb,
189     &add_address_cb,
190     NULL,
191     &add_queue_cb,
192     NULL,
193     NULL);   /* cls */
194 }
195
196 int
197 main (int argc,
198       char *const *argv)
199 {
200   char *cfg_filename;
201   char *opt_cfg_filename;
202   const char *xdg;
203   char *loglev;
204   char *logfile;
205   struct GNUNET_CONFIGURATION_Handle *cfg;
206
207   struct GNUNET_GETOPT_CommandLineOption service_options[] = {
208     GNUNET_GETOPT_option_cfgfile (&opt_cfg_filename),
209     GNUNET_GETOPT_option_help (NULL),
210     GNUNET_GETOPT_option_loglevel (&loglev),
211     GNUNET_GETOPT_option_logfile (&logfile),
212     GNUNET_GETOPT_OPTION_END
213   };
214
215   if (GNUNET_OK != GNUNET_log_setup ("test_communicator_unix",
216                                      loglev,
217                                      logfile))
218   {
219     GNUNET_break (0);
220     return GNUNET_SYSERR;
221   }
222
223   xdg = getenv ("XDG_CONFIG_HOME");
224   if (NULL != xdg)
225     GNUNET_asprintf (&cfg_filename,
226                      "%s%s%s",
227                      xdg,
228                      DIR_SEPARATOR_STR,
229                      GNUNET_OS_project_data_get ()->config_file);
230   else
231     cfg_filename = GNUNET_strdup (
232       GNUNET_OS_project_data_get ()->user_config_file);
233   cfg = GNUNET_CONFIGURATION_create ();
234   if (NULL != opt_cfg_filename)
235   {
236     if ((GNUNET_YES !=
237          GNUNET_DISK_file_test (opt_cfg_filename)) ||
238         (GNUNET_SYSERR ==
239          GNUNET_CONFIGURATION_load (cfg,
240                                     opt_cfg_filename)))
241     {
242       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
243                   _ ("Malformed configuration file `%s', exit ...\n"),
244                   opt_cfg_filename);
245       return GNUNET_SYSERR;
246     }
247   }
248   else
249   {
250     if (GNUNET_YES ==
251         GNUNET_DISK_file_test (cfg_filename))
252     {
253       if (GNUNET_SYSERR ==
254           GNUNET_CONFIGURATION_load (cfg,
255                                      cfg_filename))
256       {
257         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
258                     _ ("Malformed configuration file `%s', exit ...\n"),
259                     cfg_filename);
260         return GNUNET_SYSERR;
261       }
262     }
263     else
264     {
265       if (GNUNET_SYSERR ==
266           GNUNET_CONFIGURATION_load (cfg,
267                                      NULL))
268       {
269         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
270                     _ ("Malformed configuration, exit ...\n"));
271         return GNUNET_SYSERR;
272       }
273     }
274   }
275   GNUNET_SCHEDULER_run (&run,
276                         cfg);
277 }