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