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