aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/util/socks.c8
-rw-r--r--src/util/test_socks.c219
2 files changed, 225 insertions, 2 deletions
diff --git a/src/util/socks.c b/src/util/socks.c
index d90fa26d1..d54a086f9 100644
--- a/src/util/socks.c
+++ b/src/util/socks.c
@@ -274,6 +274,7 @@ SOCKS5_handshake_step (struct GNUNET_SOCKS_Handshake *ih)
274 case SOCKS5_step_done: 274 case SOCKS5_step_done:
275 GNUNET_assert (0); 275 GNUNET_assert (0);
276 } 276 }
277 ++ih->step;
277 ih->instart = b; 278 ih->instart = b;
278 /* Do not reschedule the sender unless we're done reading. 279 /* Do not reschedule the sender unless we're done reading.
279 * I imagine this lets us avoid ever cancelling the transmit handle. */ 280 * I imagine this lets us avoid ever cancelling the transmit handle. */
@@ -294,6 +295,7 @@ reciever (void *cls,
294 const struct sockaddr * addr, 295 const struct sockaddr * addr,
295 socklen_t addrlen, int errCode) 296 socklen_t addrlen, int errCode)
296{ 297{
298printf("Meow(%d)",available);
297 struct GNUNET_SOCKS_Handshake * ih = cls; 299 struct GNUNET_SOCKS_Handshake * ih = cls;
298 GNUNET_assert (&ih->inend[available] < &ih->inbuf[1024]); 300 GNUNET_assert (&ih->inend[available] < &ih->inbuf[1024]);
299 memcpy(ih->inend, buf, available); 301 memcpy(ih->inend, buf, available);
@@ -312,6 +314,7 @@ reciever (void *cls,
312void 314void
313register_reciever (struct GNUNET_SOCKS_Handshake *ih, int want) 315register_reciever (struct GNUNET_SOCKS_Handshake *ih, int want)
314{ 316{
317 printf("register_reciever on step %u for %d bytes.\n", ih->step, want );
315 GNUNET_CONNECTION_receive (ih->socks5_connection, 318 GNUNET_CONNECTION_receive (ih->socks5_connection,
316 want, 319 want,
317 GNUNET_TIME_relative_get_minute_ (), 320 GNUNET_TIME_relative_get_minute_ (),
@@ -381,12 +384,13 @@ transmit_ready (void *cls,
381 GNUNET_assert (1024 >= size && size > 0); 384 GNUNET_assert (1024 >= size && size > 0);
382 GNUNET_assert (SOCKS5_step_done > ih->step && ih->step >= 0); 385 GNUNET_assert (SOCKS5_step_done > ih->step && ih->step >= 0);
383 unsigned char * b = ih->outstep[ih->step]; 386 unsigned char * b = ih->outstep[ih->step];
384 unsigned char * e = ih->outstep[ih->step++]; 387 unsigned char * e = ih->outstep[ih->step+1];
385 GNUNET_assert (e <= &ih->outbuf[1024]); 388 GNUNET_assert (e <= &ih->outbuf[1024]);
386 unsigned l = e - b; 389 unsigned l = e - b;
387 GNUNET_assert (size >= l && l >= 0); 390 GNUNET_assert (size >= l && l >= 0);
388 memcpy(b, buf, l); 391 memcpy(buf, b, l);
389 register_reciever (ih, register_reciever_wants(ih)); 392 register_reciever (ih, register_reciever_wants(ih));
393 printf("sent(%d)\n",l);
390 return l; 394 return l;
391} 395}
392 396
diff --git a/src/util/test_socks.c b/src/util/test_socks.c
new file mode 100644
index 000000000..129d340f2
--- /dev/null
+++ b/src/util/test_socks.c
@@ -0,0 +1,219 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009 Christian Grothoff Jeff Burdges, and other contributing authors
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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_CLIENT_Connection *client;
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 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 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
96recv_bounce (void *cls, const struct GNUNET_MessageHeader *got)
97{
98 int *ok = cls;
99 struct GNUNET_MessageHeader msg;
100
101 GNUNET_assert (got != NULL); /* timeout */
102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving bounce, checking content\n");
103 msg.type = htons (MY_TYPE);
104 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
105 GNUNET_assert (0 == memcmp (got, &msg, sizeof (struct GNUNET_MessageHeader)));
106 GNUNET_CLIENT_disconnect (client);
107 client = NULL;
108 GNUNET_SERVER_destroy (server);
109 server = NULL;
110 *ok = 0;
111}
112
113
114static size_t
115make_msg (void *cls, size_t size, void *buf)
116{
117 struct GNUNET_MessageHeader *msg = buf;
118
119 printf("Hmm(%u,%u)\n",NULL==buf,GNUNET_SCHEDULER_get_reason ());
120 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
121 msg->type = htons (MY_TYPE);
122 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating message for transmission\n");
124 return sizeof (struct GNUNET_MessageHeader);
125}
126
127
128static void
129task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
130{
131 struct sockaddr_in sa;
132 struct sockaddr *sap[2];
133 socklen_t slens[2];
134
135 /* test that ill-configured client fails instantly */
136 GNUNET_assert (NULL == GNUNET_CLIENT_connect ("invalid-service", cfg));
137
138 /* test IPC between client and server */
139 sap[0] = (struct sockaddr *) &sa;
140 slens[0] = sizeof (sa);
141 sap[1] = NULL;
142 slens[1] = 0;
143 memset (&sa, 0, sizeof (sa));
144#if HAVE_SOCKADDR_IN_SIN_LEN
145 sa.sin_len = sizeof (sa);
146#endif
147 sa.sin_family = AF_INET;
148 sa.sin_port = htons (PORT);
149 server =
150 GNUNET_SERVER_create (NULL, NULL, sap, slens,
151 GNUNET_TIME_relative_multiply
152 (GNUNET_TIME_UNIT_MILLISECONDS, 10000), GNUNET_NO);
153 GNUNET_assert (server != NULL);
154 handlers[0].callback_cls = cls;
155 handlers[1].callback_cls = cls;
156 GNUNET_SERVER_add_handlers (server, handlers);
157 client = GNUNET_CLIENT_connect (MYNAME, cfg);
158 GNUNET_assert (client != NULL);
159 GNUNET_assert (NULL !=
160 GNUNET_CLIENT_notify_transmit_ready (client,
161 sizeof (struct
162 GNUNET_MessageHeader),
163 GNUNET_TIME_relative_multiply
164 (GNUNET_TIME_UNIT_SECONDS,5),
165 GNUNET_NO, &make_msg,
166 NULL));
167 GNUNET_CLIENT_receive (client, &recv_bounce, cls,
168 GNUNET_TIME_relative_multiply
169 (GNUNET_TIME_UNIT_MILLISECONDS, 10000));
170}
171
172
173int
174main (int argc, char *argv[])
175{
176 int ok;
177 char * socksport = "1081";
178
179 GNUNET_log_setup ("test_client",
180 "WARNING",
181 NULL);
182
183 pid_t pid = fork();
184 if (pid < 0)
185 abort();
186 if (pid == 0) {
187 return 0;
188 execlp("ssh","ssh","-D",socksport,"127.0.0.1","-N",(char*)NULL);
189 perror("execlp(\"ssh\",\"ssh\",\"-D\",\"1081\",\"127.0.0.1\",\"-N\") ");
190 printf(""
191"Please ensure you have ssh installed and have sshd installed and running :\n"
192"\tsudo apt-get install openssh-client openssh-server\n"
193"If you run Tor as a network proxy then Tor might prevent ssh from connecting\n"
194"to localhost. Please either run make check from an unproxied user, or else\n"
195"add these lines to the beginning of your ~/.ssh/config file :"
196"\tHost 127.0.0.1 localhost\n"
197"\t CheckHostIP no\n"
198"\t Protocol 2\n"
199"\t ProxyCommand nc 127.0.0.1 22\n");
200 kill(getppid(), SIGTERM);
201 return 1;
202 }
203 sleep(1);
204
205 cfg = GNUNET_CONFIGURATION_create ();
206 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "SOCKSHOST", "127.0.0.1");
207 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "SOCKSPORT", socksport);
208 GNUNET_CONFIGURATION_set_value_number (cfg, MYNAME, "PORT", PORT);
209 GNUNET_CONFIGURATION_set_value_string (cfg, MYNAME, "HOSTNAME", "127.0.0.1");
210 ok = 1;
211 GNUNET_SCHEDULER_run (&task, &ok);
212 GNUNET_CONFIGURATION_destroy (cfg);
213
214 printf("killing %d\n",pid);
215 kill(pid,SIGTERM);
216 return ok;
217}
218
219/* end of test_client.c */