test_communicator_unix.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2019 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file transport/test_communicator_unix.c
  18. * @brief test the unix communicator
  19. * @author Julius Bünger
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "transport-testing2.h"
  24. #include "gnunet_ats_transport_service.h"
  25. #include "gnunet_signatures.h"
  26. #include "transport.h"
  27. #include <inttypes.h>
  28. #define LOG(kind,...) GNUNET_log_from (kind, "test_transport_communicator_unix", __VA_ARGS__)
  29. #define NUM_PEERS 2
  30. static struct GNUNET_PeerIdentity peer_id[NUM_PEERS];
  31. static struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS];
  32. //static char *addresses[NUM_PEERS];
  33. #define PAYLOAD_SIZE 256
  34. //static char payload[PAYLOAD_SIZE] = "TEST PAYLOAD";
  35. //static char payload[] = "TEST PAYLOAD";
  36. static uint32_t payload = 42;
  37. static void
  38. communicator_available_cb (void *cls,
  39. struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
  40. enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc,
  41. char *address_prefix)
  42. {
  43. LOG (GNUNET_ERROR_TYPE_DEBUG,
  44. "Communicator available. (cc: %u, prefix: %s)\n",
  45. cc,
  46. address_prefix);
  47. }
  48. static void
  49. add_address_cb (void *cls,
  50. struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
  51. const char *address,
  52. struct GNUNET_TIME_Relative expiration,
  53. uint32_t aid,
  54. enum GNUNET_NetworkType nt)
  55. {
  56. LOG (GNUNET_ERROR_TYPE_DEBUG,
  57. "New address. (addr: %s, expir: %" PRIu32 ", ID: %" PRIu32 ", nt: %u\n",
  58. address,
  59. expiration.rel_value_us,
  60. aid,
  61. nt);
  62. //addresses[1] = GNUNET_strdup (address);
  63. GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (tc_hs[0],
  64. &peer_id[1],
  65. address);
  66. }
  67. /**
  68. * @brief Callback that informs whether the requested queue will be
  69. * established
  70. *
  71. * Implements #GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback.
  72. *
  73. * @param cls Closure - unused
  74. * @param tc_h Communicator handle - unused
  75. * @param will_try #GNUNET_YES if queue will be established
  76. * #GNUNET_NO if queue will not be established (bogous address)
  77. */
  78. static void
  79. queue_create_reply_cb (void *cls,
  80. struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
  81. int will_try)
  82. {
  83. if (GNUNET_YES == will_try)
  84. LOG (GNUNET_ERROR_TYPE_DEBUG,
  85. "Queue will be established!\n");
  86. else
  87. LOG (GNUNET_ERROR_TYPE_WARNING,
  88. "Queue won't be established (bougus address?)!\n");
  89. }
  90. /**
  91. * @brief Handle opening of queue
  92. *
  93. * Issues sending of test data
  94. *
  95. * Implements #GNUNET_TRANSPORT_TESTING_AddQueueCallback
  96. *
  97. * @param cls Closure
  98. * @param tc_h Communicator handle
  99. * @param tc_queue Handle to newly opened queue
  100. */
  101. static void
  102. add_queue_cb (void *cls,
  103. struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
  104. struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue)
  105. {
  106. LOG (GNUNET_ERROR_TYPE_DEBUG,
  107. "Got Queue!\n");
  108. GNUNET_TRANSPORT_TESTING_transport_communicator_send (tc_queue,
  109. &payload,
  110. sizeof (payload));
  111. }
  112. /**
  113. * @brief Handle an incoming message
  114. *
  115. * Implements #GNUNET_TRANSPORT_TESTING_IncomingMessageCallback
  116. * @param cls Closure
  117. * @param tc_h Handle to the receiving communicator
  118. * @param msg Received message
  119. */
  120. void
  121. incoming_message_cb (void *cls,
  122. struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
  123. const struct GNUNET_MessageHeader *msg)
  124. {
  125. }
  126. /**
  127. * @brief Main function called by the scheduler
  128. *
  129. * @param cls Closure - Handle to configuration
  130. */
  131. static void
  132. run (void *cls)
  133. {
  134. struct GNUNET_CONFIGURATION_Handle *cfg = cls;
  135. tc_hs[0] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
  136. "transport",
  137. "gnunet-communicator-unix",
  138. "test_communicator_1.conf",
  139. &communicator_available_cb,
  140. NULL,
  141. &queue_create_reply_cb,
  142. &add_queue_cb,
  143. NULL,
  144. NULL); /* cls */
  145. tc_hs[1] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
  146. "transport",
  147. "gnunet-communicator-unix",
  148. "test_communicator_2.conf",
  149. &communicator_available_cb,
  150. &add_address_cb,
  151. NULL,
  152. &add_queue_cb,
  153. NULL,
  154. NULL); /* cls */
  155. }
  156. int
  157. main (int argc,
  158. char *const *argv)
  159. {
  160. char *cfg_filename;
  161. char *opt_cfg_filename;
  162. const char *xdg;
  163. char *loglev;
  164. char *logfile;
  165. struct GNUNET_CONFIGURATION_Handle *cfg;
  166. struct GNUNET_GETOPT_CommandLineOption service_options[] = {
  167. GNUNET_GETOPT_option_cfgfile (&opt_cfg_filename),
  168. GNUNET_GETOPT_option_help (NULL),
  169. GNUNET_GETOPT_option_loglevel (&loglev),
  170. GNUNET_GETOPT_option_logfile (&logfile),
  171. GNUNET_GETOPT_OPTION_END
  172. };
  173. if (GNUNET_OK != GNUNET_log_setup ("test_communicator_unix",
  174. loglev,
  175. logfile))
  176. {
  177. GNUNET_break (0);
  178. return GNUNET_SYSERR;
  179. }
  180. xdg = getenv ("XDG_CONFIG_HOME");
  181. if (NULL != xdg)
  182. GNUNET_asprintf (&cfg_filename,
  183. "%s%s%s",
  184. xdg,
  185. DIR_SEPARATOR_STR,
  186. GNUNET_OS_project_data_get ()->config_file);
  187. else
  188. cfg_filename = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
  189. cfg = GNUNET_CONFIGURATION_create ();
  190. if (NULL != opt_cfg_filename)
  191. {
  192. if ( (GNUNET_YES !=
  193. GNUNET_DISK_file_test (opt_cfg_filename)) ||
  194. (GNUNET_SYSERR ==
  195. GNUNET_CONFIGURATION_load (cfg,
  196. opt_cfg_filename)) )
  197. {
  198. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  199. _("Malformed configuration file `%s', exit ...\n"),
  200. opt_cfg_filename);
  201. return GNUNET_SYSERR;
  202. }
  203. }
  204. else
  205. {
  206. if (GNUNET_YES ==
  207. GNUNET_DISK_file_test (cfg_filename))
  208. {
  209. if (GNUNET_SYSERR ==
  210. GNUNET_CONFIGURATION_load (cfg,
  211. cfg_filename))
  212. {
  213. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  214. _("Malformed configuration file `%s', exit ...\n"),
  215. cfg_filename);
  216. return GNUNET_SYSERR;
  217. }
  218. }
  219. else
  220. {
  221. if (GNUNET_SYSERR ==
  222. GNUNET_CONFIGURATION_load (cfg,
  223. NULL))
  224. {
  225. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  226. _("Malformed configuration, exit ...\n"));
  227. return GNUNET_SYSERR;
  228. }
  229. }
  230. }
  231. GNUNET_SCHEDULER_run (&run,
  232. cfg);
  233. }