Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / util / test_socks.c
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 /**
19  * @file util/test_socks.c
20  * @brief tests for socks.c
21  */
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24
25
26 #define PORT 35124
27
28 #define MYNAME "test_sockst"
29
30 static struct GNUNET_MQ_Handle *mq;
31
32 static struct GNUNET_SERVER_Handle *server;
33
34 static struct GNUNET_CONFIGURATION_Handle *cfg;
35
36 #define MY_TYPE 130
37
38 struct CopyContext
39 {
40   struct GNUNET_SERVER_Client *client;
41   struct GNUNET_MessageHeader *cpy;
42 };
43
44 static size_t
45 copy_msg (void *cls, size_t size, void *buf)
46 {
47   struct CopyContext *ctx = cls;
48   struct GNUNET_MessageHeader *cpy = ctx->cpy;
49
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);
54   GNUNET_free (cpy);
55   GNUNET_free (ctx);
56   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bounced back to client\n");
57   return sizeof (struct GNUNET_MessageHeader);
58 }
59
60
61 /**
62  * Callback that just bounces the message back to the sender.
63  */
64 static void
65 echo_cb (void *cls, struct GNUNET_SERVER_Client *client,
66          const struct GNUNET_MessageHeader *message)
67 {
68   struct CopyContext *cc;
69   struct GNUNET_MessageHeader *cpy;
70
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);
75   cc->client = client;
76   cpy = GNUNET_malloc (ntohs (message->size));
77   GNUNET_memcpy (cpy, message, ntohs (message->size));
78   cc->cpy = cpy;
79   GNUNET_assert (NULL !=
80                  GNUNET_SERVER_notify_transmit_ready (client,
81                                                       ntohs (message->size),
82                                                       GNUNET_TIME_UNIT_SECONDS,
83                                                       &copy_msg, cc));
84 }
85
86
87 static struct GNUNET_SERVER_MessageHandler handlers[] = {
88   {&echo_cb, NULL, MY_TYPE, sizeof (struct GNUNET_MessageHeader)},
89   {NULL, NULL, 0, 0}
90 };
91
92
93 static void
94 handle_bounce (void *cls,
95                const struct GNUNET_MessageHeader *got)
96 {
97   int *ok = cls;
98
99   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
100               "Receiving bounce, checking content\n");
101   GNUNET_assert (NULL != got);
102   GNUNET_MQ_destroy (mq);
103   mq = NULL;
104   GNUNET_SERVER_destroy (server);
105   server = NULL;
106   *ok = 0;
107 }
108
109
110 /**
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.
114  *
115  * @param cls closure with the `struct GNUNET_STATISTICS_Handle *`
116  * @param error error code
117  */
118 static void
119 mq_error_handler (void *cls,
120                   enum GNUNET_MQ_Error error)
121 {
122   GNUNET_assert (0); /* should never happen */
123 }
124
125
126 static void
127 task (void *cls)
128 {
129   struct sockaddr_in sa;
130   struct sockaddr *sap[2];
131   socklen_t slens[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,
136                              MY_TYPE,
137                              struct GNUNET_MessageHeader,
138                              cls),
139     GNUNET_MQ_handler_end ()
140   };
141
142   /* test IPC between client and server */
143   sap[0] = (struct sockaddr *) &sa;
144   slens[0] = sizeof (sa);
145   sap[1] = NULL;
146   slens[1] = 0;
147   memset (&sa, 0, sizeof (sa));
148 #if HAVE_SOCKADDR_IN_SIN_LEN
149   sa.sin_len = sizeof (sa);
150 #endif
151   sa.sin_family = AF_INET;
152   sa.sin_port = htons (PORT);
153   server =
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,
162                               MYNAME,
163                               chandlers,
164                               &mq_error_handler,
165                               NULL);
166   GNUNET_assert (NULL != mq);
167   env = GNUNET_MQ_msg (msg,
168                        MY_TYPE);
169   GNUNET_MQ_send (mq,
170                   env);
171 }
172
173
174 int
175 main (int argc, char *argv[])
176 {
177   int ok;
178   int status;
179   const char *socksport = "1081";
180
181   GNUNET_log_setup ("test_client",
182                     "WARNING",
183                     NULL);
184
185   pid_t pid = fork();
186   GNUNET_assert (pid >= 0);
187   if (pid == 0)
188   {
189     execlp ("ssh",
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\") ");
196     printf (""
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"
204 "\t  Protocol 2\n"
205 "\t  ProxyCommand nc 127.0.0.1 22\n");
206     kill (getppid(), SIGALRM);
207     return 1;
208   }
209   if (0 != sleep (1))
210   {
211     /* sleep interrupted, likely SIGALRM, failure to
212        launch child, terminate */
213     printf (""
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"
221 "\t  Protocol 2\n"
222 "\t  ProxyCommand nc 127.0.0.1 22\n");
223     return 77;
224   }
225   /* check if child exec()ed but died */
226   if (0 != waitpid (pid, &status, WNOHANG))
227   {
228     printf (""
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"
234 "\t  Protocol 2\n"
235 "\t  ProxyCommand nc 127.0.0.1 22\n");
236     return 77;
237   }
238
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");
244   ok = 1;
245   GNUNET_SCHEDULER_run (&task, &ok);
246   GNUNET_CONFIGURATION_destroy (cfg);
247
248   GNUNET_break (0 == kill (pid, SIGTERM));
249   GNUNET_break (pid == waitpid (pid, &status, 0));
250   return ok;
251 }
252
253 /* end of test_socks.c */