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