test_socks.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2015, 2016 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 util/test_socks.c
  18. * @brief tests for socks.c
  19. */
  20. #include "platform.h"
  21. #include "gnunet_util_lib.h"
  22. #define PORT 35124
  23. #define MYNAME "test_sockst"
  24. static struct GNUNET_MQ_Handle *mq;
  25. static struct GNUNET_SERVER_Handle *server;
  26. static struct GNUNET_CONFIGURATION_Handle *cfg;
  27. #define MY_TYPE 130
  28. struct CopyContext
  29. {
  30. struct GNUNET_SERVER_Client *client;
  31. struct GNUNET_MessageHeader *cpy;
  32. };
  33. static size_t
  34. copy_msg (void *cls, size_t size, void *buf)
  35. {
  36. struct CopyContext *ctx = cls;
  37. struct GNUNET_MessageHeader *cpy = ctx->cpy;
  38. GNUNET_assert (sizeof(struct GNUNET_MessageHeader) == ntohs (cpy->size));
  39. GNUNET_assert (size >= ntohs (cpy->size));
  40. GNUNET_memcpy (buf, cpy, ntohs (cpy->size));
  41. GNUNET_SERVER_receive_done (ctx->client, GNUNET_OK);
  42. GNUNET_free (cpy);
  43. GNUNET_free (ctx);
  44. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bounced back to client\n");
  45. return sizeof(struct GNUNET_MessageHeader);
  46. }
  47. /**
  48. * Callback that just bounces the message back to the sender.
  49. */
  50. static void
  51. echo_cb (void *cls, struct GNUNET_SERVER_Client *client,
  52. const struct GNUNET_MessageHeader *message)
  53. {
  54. struct CopyContext *cc;
  55. struct GNUNET_MessageHeader *cpy;
  56. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  57. "Receiving message from client, bouncing back\n");
  58. GNUNET_assert (sizeof(struct GNUNET_MessageHeader) == ntohs (message->size));
  59. cc = GNUNET_new (struct CopyContext);
  60. cc->client = client;
  61. cpy = GNUNET_malloc (ntohs (message->size));
  62. GNUNET_memcpy (cpy, message, ntohs (message->size));
  63. cc->cpy = cpy;
  64. GNUNET_assert (NULL !=
  65. GNUNET_SERVER_notify_transmit_ready (client,
  66. ntohs (message->size),
  67. GNUNET_TIME_UNIT_SECONDS,
  68. &copy_msg, cc));
  69. }
  70. static struct GNUNET_SERVER_MessageHandler handlers[] = {
  71. { &echo_cb, NULL, MY_TYPE, sizeof(struct GNUNET_MessageHeader) },
  72. { NULL, NULL, 0, 0 }
  73. };
  74. static void
  75. handle_bounce (void *cls,
  76. const struct GNUNET_MessageHeader *got)
  77. {
  78. int *ok = cls;
  79. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  80. "Receiving bounce, checking content\n");
  81. GNUNET_assert (NULL != got);
  82. GNUNET_MQ_destroy (mq);
  83. mq = NULL;
  84. GNUNET_SERVER_destroy (server);
  85. server = NULL;
  86. *ok = 0;
  87. }
  88. /**
  89. * Generic error handler, called with the appropriate error code and
  90. * the same closure specified at the creation of the message queue.
  91. * Not every message queue implementation supports an error handler.
  92. *
  93. * @param cls closure with the `struct GNUNET_STATISTICS_Handle *`
  94. * @param error error code
  95. */
  96. static void
  97. mq_error_handler (void *cls,
  98. enum GNUNET_MQ_Error error)
  99. {
  100. GNUNET_assert (0); /* should never happen */
  101. }
  102. static void
  103. task (void *cls)
  104. {
  105. struct sockaddr_in sa;
  106. struct sockaddr *sap[2];
  107. socklen_t slens[2];
  108. struct GNUNET_MQ_Envelope *env;
  109. struct GNUNET_MessageHeader *msg;
  110. struct GNUNET_MQ_MessageHandler chandlers[] = {
  111. GNUNET_MQ_hd_fixed_size (bounce,
  112. MY_TYPE,
  113. struct GNUNET_MessageHeader,
  114. cls),
  115. GNUNET_MQ_handler_end ()
  116. };
  117. /* test IPC between client and server */
  118. sap[0] = (struct sockaddr *) &sa;
  119. slens[0] = sizeof(sa);
  120. sap[1] = NULL;
  121. slens[1] = 0;
  122. memset (&sa, 0, sizeof(sa));
  123. #if HAVE_SOCKADDR_IN_SIN_LEN
  124. sa.sin_len = sizeof(sa);
  125. #endif
  126. sa.sin_family = AF_INET;
  127. sa.sin_port = htons (PORT);
  128. server =
  129. GNUNET_SERVER_create (NULL, NULL, sap, slens,
  130. GNUNET_TIME_relative_multiply
  131. (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO);
  132. GNUNET_assert (server != NULL);
  133. handlers[0].callback_cls = cls;
  134. handlers[1].callback_cls = cls;
  135. GNUNET_SERVER_add_handlers (server, handlers);
  136. mq = GNUNET_CLIENT_connect (cfg,
  137. MYNAME,
  138. chandlers,
  139. &mq_error_handler,
  140. NULL);
  141. GNUNET_assert (NULL != mq);
  142. env = GNUNET_MQ_msg (msg,
  143. MY_TYPE);
  144. GNUNET_MQ_send (mq,
  145. env);
  146. }
  147. int
  148. main (int argc, char *argv[])
  149. {
  150. int ok;
  151. int status;
  152. const char *socksport = "1081";
  153. GNUNET_log_setup ("test_client",
  154. "WARNING",
  155. NULL);
  156. pid_t pid = fork ();
  157. GNUNET_assert (pid >= 0);
  158. if (pid == 0)
  159. {
  160. execlp ("ssh",
  161. "ssh", "-D", socksport,
  162. "-o", "BatchMode yes",
  163. "-o", "UserKnownHostsFile /tmp/gnunet_test_socks_ssh_garbage",
  164. "-o", "StrictHostKeyChecking no",
  165. "127.0.0.1", "-N", (char *) NULL);
  166. perror (
  167. "execlp (\"ssh\",\"ssh\",...,\"-D\",\"1081\",\"127.0.0.1\",\"-N\") ");
  168. printf (""
  169. "Please ensure you have ssh installed and have sshd installed and running :\n"
  170. "\tsudo apt-get install openssh-client openssh-server\n"
  171. "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n"
  172. "to localhost. Please either run make check from an unproxied user, or else\n"
  173. "add these lines to the beginning of your ~/.ssh/config file :"
  174. "\tHost 127.0.0.1 localhost\n"
  175. "\t CheckHostIP no\n"
  176. "\t Protocol 2\n"
  177. "\t ProxyCommand nc 127.0.0.1 22\n");
  178. kill (getppid (), SIGALRM);
  179. return 1;
  180. }
  181. if (0 != sleep (1))
  182. {
  183. /* sleep interrupted, likely SIGALRM, failure to
  184. launch child, terminate */
  185. printf (""
  186. "Please ensure you have ssh installed and have sshd installed and running :\n"
  187. "\tsudo apt-get install openssh-client openssh-server\n"
  188. "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n"
  189. "to localhost. Please either run make check from an unproxied user, or else\n"
  190. "add these lines to the beginning of your ~/.ssh/config file :"
  191. "\tHost 127.0.0.1 localhost\n"
  192. "\t CheckHostIP no\n"
  193. "\t Protocol 2\n"
  194. "\t ProxyCommand nc 127.0.0.1 22\n");
  195. return 77;
  196. }
  197. /* check if child exec()ed but died */
  198. if (0 != waitpid (pid, &status, WNOHANG))
  199. {
  200. printf (""
  201. "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n"
  202. "to localhost. Please either run make check from an unproxied user, or else\n"
  203. "add these lines to the beginning of your ~/.ssh/config file :"
  204. "\tHost 127.0.0.1 localhost\n"
  205. "\t CheckHostIP no\n"
  206. "\t Protocol 2\n"
  207. "\t ProxyCommand nc 127.0.0.1 22\n");
  208. return 77;
  209. }
  210. cfg = GNUNET_CONFIGURATION_create ();
  211. GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "SOCKSHOST", "127.0.0.1");
  212. GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "SOCKSPORT", socksport);
  213. GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
  214. GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", "127.0.0.1");
  215. ok = 1;
  216. GNUNET_SCHEDULER_run (&task, &ok);
  217. GNUNET_CONFIGURATION_destroy (cfg);
  218. GNUNET_break (0 == kill (pid, SIGTERM));
  219. GNUNET_break (pid == waitpid (pid, &status, 0));
  220. return ok;
  221. }
  222. /* end of test_socks.c */