2 This file is part of GNUnet.
3 Copyright (C) 2015, 2016 GNUnet e.V.
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.
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.
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/>.
19 * @file util/test_socks.c
20 * @brief tests for socks.c
23 #include "gnunet_util_lib.h"
28 #define MYNAME "test_sockst"
30 static struct GNUNET_MQ_Handle *mq;
32 static struct GNUNET_SERVER_Handle *server;
34 static struct GNUNET_CONFIGURATION_Handle *cfg;
40 struct GNUNET_SERVER_Client *client;
41 struct GNUNET_MessageHeader *cpy;
45 copy_msg (void *cls, size_t size, void *buf)
47 struct CopyContext *ctx = cls;
48 struct GNUNET_MessageHeader *cpy = ctx->cpy;
50 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (cpy->size));
51 GNUNET_assert (size >= ntohs (cpy->size));
52 GNUNET_memcpy (buf, cpy, ntohs (cpy->size));
53 GNUNET_SERVER_receive_done (ctx->client, GNUNET_OK);
56 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bounced back to client\n");
57 return sizeof (struct GNUNET_MessageHeader);
62 * Callback that just bounces the message back to the sender.
65 echo_cb (void *cls, struct GNUNET_SERVER_Client *client,
66 const struct GNUNET_MessageHeader *message)
68 struct CopyContext *cc;
69 struct GNUNET_MessageHeader *cpy;
71 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
72 "Receiving message from client, bouncing back\n");
73 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size));
74 cc = GNUNET_new (struct CopyContext);
76 cpy = GNUNET_malloc (ntohs (message->size));
77 GNUNET_memcpy (cpy, message, ntohs (message->size));
79 GNUNET_assert (NULL !=
80 GNUNET_SERVER_notify_transmit_ready (client,
81 ntohs (message->size),
82 GNUNET_TIME_UNIT_SECONDS,
87 static struct GNUNET_SERVER_MessageHandler handlers[] = {
88 {&echo_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
94 handle_bounce (void *cls,
95 const struct GNUNET_MessageHeader *got)
99 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
100 "Receiving bounce, checking content\n");
101 GNUNET_assert (NULL != got);
102 GNUNET_MQ_destroy (mq);
104 GNUNET_SERVER_destroy (server);
111 * Generic error handler, called with the appropriate error code and
112 * the same closure specified at the creation of the message queue.
113 * Not every message queue implementation supports an error handler.
115 * @param cls closure with the `struct GNUNET_STATISTICS_Handle *`
116 * @param error error code
119 mq_error_handler (void *cls,
120 enum GNUNET_MQ_Error error)
122 GNUNET_assert (0); /* should never happen */
129 struct sockaddr_in sa;
130 struct sockaddr *sap[2];
132 struct GNUNET_MQ_Envelope *env;
133 struct GNUNET_MessageHeader *msg;
134 struct GNUNET_MQ_MessageHandler chandlers[] = {
135 GNUNET_MQ_hd_fixed_size (bounce,
137 struct GNUNET_MessageHeader,
139 GNUNET_MQ_handler_end ()
142 /* test IPC between client and server */
143 sap[0] = (struct sockaddr *) &sa;
144 slens[0] = sizeof (sa);
147 memset (&sa, 0, sizeof (sa));
148 #if HAVE_SOCKADDR_IN_SIN_LEN
149 sa.sin_len = sizeof (sa);
151 sa.sin_family = AF_INET;
152 sa.sin_port = htons (PORT);
154 GNUNET_SERVER_create (NULL, NULL, sap, slens,
155 GNUNET_TIME_relative_multiply
156 (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO);
157 GNUNET_assert (server != NULL);
158 handlers[0].callback_cls = cls;
159 handlers[1].callback_cls = cls;
160 GNUNET_SERVER_add_handlers (server, handlers);
161 mq = GNUNET_CLIENT_connect (cfg,
166 GNUNET_assert (NULL != mq);
167 env = GNUNET_MQ_msg (msg,
175 main (int argc, char *argv[])
179 const char *socksport = "1081";
181 GNUNET_log_setup ("test_client",
186 GNUNET_assert (pid >= 0);
190 "ssh","-D",socksport,
191 "-o","BatchMode yes",
192 "-o","UserKnownHostsFile /tmp/gnunet_test_socks_ssh_garbage",
193 "-o","StrictHostKeyChecking no",
194 "127.0.0.1","-N",(char*)NULL);
195 perror ("execlp (\"ssh\",\"ssh\",...,\"-D\",\"1081\",\"127.0.0.1\",\"-N\") ");
197 "Please ensure you have ssh installed and have sshd installed and running :\n"
198 "\tsudo apt-get install openssh-client openssh-server\n"
199 "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n"
200 "to localhost. Please either run make check from an unproxied user, or else\n"
201 "add these lines to the beginning of your ~/.ssh/config file :"
202 "\tHost 127.0.0.1 localhost\n"
203 "\t CheckHostIP no\n"
205 "\t ProxyCommand nc 127.0.0.1 22\n");
206 kill (getppid(), SIGALRM);
211 /* sleep interrupted, likely SIGALRM, failure to
212 launch child, terminate */
214 "Please ensure you have ssh installed and have sshd installed and running :\n"
215 "\tsudo apt-get install openssh-client openssh-server\n"
216 "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n"
217 "to localhost. Please either run make check from an unproxied user, or else\n"
218 "add these lines to the beginning of your ~/.ssh/config file :"
219 "\tHost 127.0.0.1 localhost\n"
220 "\t CheckHostIP no\n"
222 "\t ProxyCommand nc 127.0.0.1 22\n");
225 /* check if child exec()ed but died */
226 if (0 != waitpid (pid, &status, WNOHANG))
229 "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n"
230 "to localhost. Please either run make check from an unproxied user, or else\n"
231 "add these lines to the beginning of your ~/.ssh/config file :"
232 "\tHost 127.0.0.1 localhost\n"
233 "\t CheckHostIP no\n"
235 "\t ProxyCommand nc 127.0.0.1 22\n");
239 cfg = GNUNET_CONFIGURATION_create ();
240 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "SOCKSHOST", "127.0.0.1");
241 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "SOCKSPORT", socksport);
242 GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
243 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", "127.0.0.1");
245 GNUNET_SCHEDULER_run (&task, &ok);
246 GNUNET_CONFIGURATION_destroy (cfg);
248 GNUNET_break (0 == kill (pid, SIGTERM));
249 GNUNET_break (pid == waitpid (pid, &status, 0));
253 /* end of test_socks.c */