test_core_api_send_to_self.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2010 Christian Grothoff
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file core/test_core_api_send_to_self.c
  19. * @brief
  20. * @author Philipp Toelke
  21. */
  22. #include <platform.h>
  23. #include <gnunet_common.h>
  24. #include <gnunet_program_lib.h>
  25. #include <gnunet_protocols.h>
  26. #include <gnunet_core_service.h>
  27. #include <gnunet_constants.h>
  28. /**
  29. * Final status code.
  30. */
  31. static int ret;
  32. /**
  33. * Handle to the cleanup task.
  34. */
  35. GNUNET_SCHEDULER_TaskIdentifier die_task;
  36. static struct GNUNET_PeerIdentity myself;
  37. /**
  38. * Configuration to load for the new peer.
  39. */
  40. struct GNUNET_CONFIGURATION_Handle *core_cfg;
  41. /**
  42. * The handle to core
  43. */
  44. struct GNUNET_CORE_Handle *core;
  45. /**
  46. * Handle to gnunet-service-arm.
  47. */
  48. struct GNUNET_OS_Process *arm_proc;
  49. /**
  50. * Function scheduled as very last function, cleans up after us
  51. */
  52. static void
  53. cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tskctx)
  54. {
  55. die_task = GNUNET_SCHEDULER_NO_TASK;
  56. if (core != NULL)
  57. {
  58. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting core.\n");
  59. GNUNET_CORE_disconnect (core);
  60. core = NULL;
  61. }
  62. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  63. "Stopping peer\n");
  64. if (0 != GNUNET_OS_process_kill (arm_proc, SIGTERM))
  65. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  66. if (GNUNET_OS_process_wait(arm_proc) != GNUNET_OK)
  67. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
  68. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  69. "ARM process %u stopped\n", GNUNET_OS_process_get_pid (arm_proc));
  70. GNUNET_OS_process_close (arm_proc);
  71. arm_proc = NULL;
  72. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
  73. }
  74. static int
  75. receive(void* cls, const struct GNUNET_PeerIdentity* other, const struct GNUNET_MessageHeader* message, const struct GNUNET_TRANSPORT_ATS_Information* atsi)
  76. {
  77. if (die_task != GNUNET_SCHEDULER_NO_TASK)
  78. GNUNET_SCHEDULER_cancel(die_task);
  79. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received message from peer %s\n", GNUNET_i2s(other));
  80. GNUNET_SCHEDULER_add_now(&cleanup, NULL);
  81. ret = 0;
  82. return GNUNET_OK;
  83. }
  84. static size_t
  85. send_message (void* cls, size_t size, void* buf)
  86. {
  87. if (size == 0 || buf == NULL)
  88. {
  89. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Could not send; got 0 buffer\n");
  90. return 0;
  91. }
  92. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending!\n");
  93. struct GNUNET_MessageHeader *hdr = buf;
  94. hdr->size = htons(sizeof(struct GNUNET_MessageHeader));
  95. hdr->type = htons(GNUNET_MESSAGE_TYPE_SERVICE_UDP);
  96. return ntohs(hdr->size);
  97. }
  98. static void
  99. init (void *cls, struct GNUNET_CORE_Handle *core,
  100. const struct GNUNET_PeerIdentity *my_identity,
  101. const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pk)
  102. {
  103. if (core == NULL)
  104. {
  105. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could NOT connect to CORE;\n");
  106. return;
  107. }
  108. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  109. "Correctly connected to CORE; we are the peer %s.\n",
  110. GNUNET_i2s (my_identity));
  111. memcpy (&myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
  112. }
  113. static void
  114. connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
  115. const struct GNUNET_TRANSPORT_ATS_Information *atsi)
  116. {
  117. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s.\n",
  118. GNUNET_i2s (peer));
  119. if (0 == memcmp (peer, &myself, sizeof (struct GNUNET_PeerIdentity)))
  120. {
  121. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  122. "Connected to myself; sending message!\n");
  123. GNUNET_CORE_notify_transmit_ready (core,
  124. GNUNET_YES,
  125. 0, GNUNET_TIME_UNIT_FOREVER_REL,
  126. peer,
  127. sizeof (struct GNUNET_MessageHeader),
  128. send_message, NULL);
  129. }
  130. }
  131. /**
  132. * Main function that will be run by the scheduler.
  133. *
  134. * @param cls closure
  135. * @param args remaining command-line arguments
  136. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  137. * @param cfg configuration
  138. */
  139. static void
  140. run (void *cls,
  141. char *const *args,
  142. const char *cfgfile,
  143. const struct GNUNET_CONFIGURATION_Handle *cfg)
  144. {
  145. const static struct GNUNET_CORE_MessageHandler handlers[] = {
  146. {&receive, GNUNET_MESSAGE_TYPE_SERVICE_UDP, 0},
  147. {NULL, 0, 0}
  148. };
  149. core_cfg = GNUNET_CONFIGURATION_create ();
  150. arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
  151. "gnunet-service-arm",
  152. #if VERBOSE
  153. "-L", "DEBUG",
  154. #endif
  155. "-c", "test_core_api_peer1.conf", NULL);
  156. GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_load (core_cfg, "test_core_api_peer1.conf"));
  157. core = GNUNET_CORE_connect (core_cfg,
  158. 42,
  159. NULL,
  160. &init,
  161. &connect_cb,
  162. NULL, NULL, NULL, 0, NULL, 0, handlers);
  163. die_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60), &cleanup, cls);
  164. }
  165. static int
  166. check ()
  167. {
  168. char *const argv[] = { "test-core-api-send-to-self",
  169. "-c",
  170. "test_core_api_data.conf",
  171. #if VERBOSE
  172. "-L", "DEBUG",
  173. #endif
  174. NULL
  175. };
  176. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  177. GNUNET_GETOPT_OPTION_END
  178. };
  179. ret = 1;
  180. return (GNUNET_OK ==
  181. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
  182. argv,
  183. "test_core_api_send_to_self",
  184. gettext_noop ("help text"),
  185. options, &run, NULL)) ? ret : 1;
  186. }
  187. /**
  188. * The main function to obtain template from gnunetd.
  189. *
  190. * @param argc number of arguments from the command line
  191. * @param argv command line arguments
  192. * @return 0 ok, 1 on error
  193. */
  194. int
  195. main (int argc, char *argv[])
  196. {
  197. GNUNET_log_setup ("test-core-api-send-to-self",
  198. #if VERBOSE
  199. "DEBUG",
  200. #else
  201. "WARNING",
  202. #endif
  203. NULL);
  204. ret = check ();
  205. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
  206. return ret;
  207. }
  208. /* end of test_core_api_send_to_self.c */