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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file util/test_socks.c
22 * @brief tests for socks.c
25 #include "gnunet_util_lib.h"
30 #define MYNAME "test_sockst"
32 static struct GNUNET_MQ_Handle *mq;
34 static struct GNUNET_SERVER_Handle *server;
36 static struct GNUNET_CONFIGURATION_Handle *cfg;
41 struct GNUNET_SERVER_Client *client;
42 struct GNUNET_MessageHeader *cpy;
46 copy_msg(void *cls, size_t size, void *buf)
48 struct CopyContext *ctx = cls;
49 struct GNUNET_MessageHeader *cpy = ctx->cpy;
51 GNUNET_assert(sizeof(struct GNUNET_MessageHeader) == ntohs(cpy->size));
52 GNUNET_assert(size >= ntohs(cpy->size));
53 GNUNET_memcpy(buf, cpy, ntohs(cpy->size));
54 GNUNET_SERVER_receive_done(ctx->client, GNUNET_OK);
57 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Message bounced back to client\n");
58 return sizeof(struct GNUNET_MessageHeader);
63 * Callback that just bounces the message back to the sender.
66 echo_cb(void *cls, struct GNUNET_SERVER_Client *client,
67 const struct GNUNET_MessageHeader *message)
69 struct CopyContext *cc;
70 struct GNUNET_MessageHeader *cpy;
72 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
73 "Receiving message from client, bouncing back\n");
74 GNUNET_assert(sizeof(struct GNUNET_MessageHeader) == ntohs(message->size));
75 cc = GNUNET_new(struct CopyContext);
77 cpy = GNUNET_malloc(ntohs(message->size));
78 GNUNET_memcpy(cpy, message, ntohs(message->size));
81 GNUNET_SERVER_notify_transmit_ready(client,
83 GNUNET_TIME_UNIT_SECONDS,
88 static struct GNUNET_SERVER_MessageHandler handlers[] = {
89 { &echo_cb, NULL, MY_TYPE, sizeof(struct GNUNET_MessageHeader) },
95 handle_bounce(void *cls,
96 const struct GNUNET_MessageHeader *got)
100 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
101 "Receiving bounce, checking content\n");
102 GNUNET_assert(NULL != got);
103 GNUNET_MQ_destroy(mq);
105 GNUNET_SERVER_destroy(server);
112 * Generic error handler, called with the appropriate error code and
113 * the same closure specified at the creation of the message queue.
114 * Not every message queue implementation supports an error handler.
116 * @param cls closure with the `struct GNUNET_STATISTICS_Handle *`
117 * @param error error code
120 mq_error_handler(void *cls,
121 enum GNUNET_MQ_Error error)
123 GNUNET_assert(0); /* should never happen */
130 struct sockaddr_in sa;
131 struct sockaddr *sap[2];
133 struct GNUNET_MQ_Envelope *env;
134 struct GNUNET_MessageHeader *msg;
135 struct GNUNET_MQ_MessageHandler chandlers[] = {
136 GNUNET_MQ_hd_fixed_size(bounce,
138 struct GNUNET_MessageHeader,
140 GNUNET_MQ_handler_end()
143 /* test IPC between client and server */
144 sap[0] = (struct sockaddr *)&sa;
145 slens[0] = sizeof(sa);
148 memset(&sa, 0, sizeof(sa));
149 #if HAVE_SOCKADDR_IN_SIN_LEN
150 sa.sin_len = sizeof(sa);
152 sa.sin_family = AF_INET;
153 sa.sin_port = htons(PORT);
155 GNUNET_SERVER_create(NULL, NULL, sap, slens,
156 GNUNET_TIME_relative_multiply
157 (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO);
158 GNUNET_assert(server != NULL);
159 handlers[0].callback_cls = cls;
160 handlers[1].callback_cls = cls;
161 GNUNET_SERVER_add_handlers(server, handlers);
162 mq = GNUNET_CLIENT_connect(cfg,
167 GNUNET_assert(NULL != mq);
168 env = GNUNET_MQ_msg(msg,
176 main(int argc, char *argv[])
180 const char *socksport = "1081";
182 GNUNET_log_setup("test_client",
187 GNUNET_assert(pid >= 0);
191 "ssh", "-D", socksport,
192 "-o", "BatchMode yes",
193 "-o", "UserKnownHostsFile /tmp/gnunet_test_socks_ssh_garbage",
194 "-o", "StrictHostKeyChecking no",
195 "127.0.0.1", "-N", (char*)NULL);
196 perror("execlp (\"ssh\",\"ssh\",...,\"-D\",\"1081\",\"127.0.0.1\",\"-N\") ");
198 "Please ensure you have ssh installed and have sshd installed and running :\n"
199 "\tsudo apt-get install openssh-client openssh-server\n"
200 "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n"
201 "to localhost. Please either run make check from an unproxied user, or else\n"
202 "add these lines to the beginning of your ~/.ssh/config file :"
203 "\tHost 127.0.0.1 localhost\n"
204 "\t CheckHostIP no\n"
206 "\t ProxyCommand nc 127.0.0.1 22\n");
207 kill(getppid(), SIGALRM);
212 /* sleep interrupted, likely SIGALRM, failure to
213 launch child, terminate */
215 "Please ensure you have ssh installed and have sshd installed and running :\n"
216 "\tsudo apt-get install openssh-client openssh-server\n"
217 "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n"
218 "to localhost. Please either run make check from an unproxied user, or else\n"
219 "add these lines to the beginning of your ~/.ssh/config file :"
220 "\tHost 127.0.0.1 localhost\n"
221 "\t CheckHostIP no\n"
223 "\t ProxyCommand nc 127.0.0.1 22\n");
226 /* check if child exec()ed but died */
227 if (0 != waitpid(pid, &status, WNOHANG))
230 "If you run Tor as a network proxy then Tor might prevent ssh from connecting\n"
231 "to localhost. Please either run make check from an unproxied user, or else\n"
232 "add these lines to the beginning of your ~/.ssh/config file :"
233 "\tHost 127.0.0.1 localhost\n"
234 "\t CheckHostIP no\n"
236 "\t ProxyCommand nc 127.0.0.1 22\n");
240 cfg = GNUNET_CONFIGURATION_create();
241 GNUNET_CONFIGURATION_set_value_string(cfg, MYNAME, "SOCKSHOST", "127.0.0.1");
242 GNUNET_CONFIGURATION_set_value_string(cfg, MYNAME, "SOCKSPORT", socksport);
243 GNUNET_CONFIGURATION_set_value_number(cfg, MYNAME, "PORT", PORT);
244 GNUNET_CONFIGURATION_set_value_string(cfg, MYNAME, "HOSTNAME", "127.0.0.1");
246 GNUNET_SCHEDULER_run(&task, &ok);
247 GNUNET_CONFIGURATION_destroy(cfg);
249 GNUNET_break(0 == kill(pid, SIGTERM));
250 GNUNET_break(pid == waitpid(pid, &status, 0));
254 /* end of test_socks.c */