diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-06-24 15:15:15 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-06-24 15:15:15 +0000 |
commit | a5b71afba9ee175223fc90e53359adfe6157c33a (patch) | |
tree | 5648b65e1a40efddba75693744ff01596a4f06ba | |
parent | 4c1969ee4afbb673085e1850f4bf6d5c3950344b (diff) | |
download | gnunet-a5b71afba9ee175223fc90e53359adfe6157c33a.tar.gz gnunet-a5b71afba9ee175223fc90e53359adfe6157c33a.zip |
migrate nat_test to MQ
-rw-r--r-- | src/nat/gnunet-nat-server.c | 108 | ||||
-rw-r--r-- | src/nat/nat_test.c | 204 |
2 files changed, 219 insertions, 93 deletions
diff --git a/src/nat/gnunet-nat-server.c b/src/nat/gnunet-nat-server.c index db51ef89a..6722deefb 100644 --- a/src/nat/gnunet-nat-server.c +++ b/src/nat/gnunet-nat-server.c | |||
@@ -47,18 +47,25 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg; | |||
47 | * | 47 | * |
48 | * @param dst_ipv4 IPv4 address to send the fake ICMP message | 48 | * @param dst_ipv4 IPv4 address to send the fake ICMP message |
49 | * @param dport destination port to include in ICMP message | 49 | * @param dport destination port to include in ICMP message |
50 | * @param is_tcp mark for TCP (GNUNET_YES) or UDP (GNUNET_NO) | 50 | * @param is_tcp mark for TCP (#GNUNET_YES) or UDP (#GNUNET_NO) |
51 | */ | 51 | */ |
52 | static void | 52 | static void |
53 | try_anat (uint32_t dst_ipv4, uint16_t dport, int is_tcp) | 53 | try_anat (uint32_t dst_ipv4, |
54 | uint16_t dport, | ||
55 | int is_tcp) | ||
54 | { | 56 | { |
55 | struct GNUNET_NAT_Handle *h; | 57 | struct GNUNET_NAT_Handle *h; |
56 | struct sockaddr_in sa; | 58 | struct sockaddr_in sa; |
57 | 59 | ||
58 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 60 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
59 | "Asking for connection reversal with %x and code %u\n", | 61 | "Asking for connection reversal with %x and code %u\n", |
60 | (unsigned int) dst_ipv4, (unsigned int) dport); | 62 | (unsigned int) dst_ipv4, |
61 | h = GNUNET_NAT_register (cfg, is_tcp, dport, 0, NULL, NULL, NULL, NULL, NULL, NULL); | 63 | (unsigned int) dport); |
64 | h = GNUNET_NAT_register (cfg, | ||
65 | is_tcp, | ||
66 | dport, | ||
67 | 0, | ||
68 | NULL, NULL, NULL, NULL, NULL, NULL); | ||
62 | memset (&sa, 0, sizeof (sa)); | 69 | memset (&sa, 0, sizeof (sa)); |
63 | sa.sin_family = AF_INET; | 70 | sa.sin_family = AF_INET; |
64 | #if HAVE_SOCKADDR_IN_SIN_LEN | 71 | #if HAVE_SOCKADDR_IN_SIN_LEN |
@@ -116,24 +123,29 @@ tcp_send (void *cls) | |||
116 | 123 | ||
117 | 124 | ||
118 | /** | 125 | /** |
119 | * Try to send 'data' to the | 126 | * Try to send @a data to the |
120 | * IP 'dst_ipv4' at port 'dport' via TCP. | 127 | * IP @a dst_ipv4' at port @a dport via TCP. |
121 | * | 128 | * |
122 | * @param dst_ipv4 target IP | 129 | * @param dst_ipv4 target IP |
123 | * @param dport target port | 130 | * @param dport target port |
124 | * @param data data to send | 131 | * @param data data to send |
125 | */ | 132 | */ |
126 | static void | 133 | static void |
127 | try_send_tcp (uint32_t dst_ipv4, uint16_t dport, uint16_t data) | 134 | try_send_tcp (uint32_t dst_ipv4, |
135 | uint16_t dport, | ||
136 | uint16_t data) | ||
128 | { | 137 | { |
129 | struct GNUNET_NETWORK_Handle *s; | 138 | struct GNUNET_NETWORK_Handle *s; |
130 | struct sockaddr_in sa; | 139 | struct sockaddr_in sa; |
131 | struct TcpContext *ctx; | 140 | struct TcpContext *ctx; |
132 | 141 | ||
133 | s = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0); | 142 | s = GNUNET_NETWORK_socket_create (AF_INET, |
143 | SOCK_STREAM, | ||
144 | 0); | ||
134 | if (NULL == s) | 145 | if (NULL == s) |
135 | { | 146 | { |
136 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket"); | 147 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, |
148 | "socket"); | ||
137 | return; | 149 | return; |
138 | } | 150 | } |
139 | memset (&sa, 0, sizeof (sa)); | 151 | memset (&sa, 0, sizeof (sa)); |
@@ -143,41 +155,54 @@ try_send_tcp (uint32_t dst_ipv4, uint16_t dport, uint16_t data) | |||
143 | #endif | 155 | #endif |
144 | sa.sin_addr.s_addr = dst_ipv4; | 156 | sa.sin_addr.s_addr = dst_ipv4; |
145 | sa.sin_port = htons (dport); | 157 | sa.sin_port = htons (dport); |
146 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending TCP message to `%s'\n", | 158 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
147 | GNUNET_a2s ((struct sockaddr *) &sa, sizeof (sa))); | 159 | "Sending TCP message to `%s'\n", |
148 | if ((GNUNET_OK != | 160 | GNUNET_a2s ((struct sockaddr *) &sa, |
149 | GNUNET_NETWORK_socket_connect (s, (const struct sockaddr *) &sa, | 161 | sizeof (sa))); |
150 | sizeof (sa))) && (errno != EINPROGRESS)) | 162 | if ( (GNUNET_OK != |
163 | GNUNET_NETWORK_socket_connect (s, | ||
164 | (const struct sockaddr *) &sa, | ||
165 | sizeof (sa))) && | ||
166 | (errno != EINPROGRESS) ) | ||
151 | { | 167 | { |
152 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect"); | 168 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, |
169 | "connect"); | ||
153 | GNUNET_NETWORK_socket_close (s); | 170 | GNUNET_NETWORK_socket_close (s); |
154 | return; | 171 | return; |
155 | } | 172 | } |
156 | ctx = GNUNET_new (struct TcpContext); | 173 | ctx = GNUNET_new (struct TcpContext); |
157 | ctx->s = s; | 174 | ctx->s = s; |
158 | ctx->data = data; | 175 | ctx->data = data; |
159 | GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_SECONDS, s, &tcp_send, ctx); | 176 | GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_SECONDS, |
177 | s, | ||
178 | &tcp_send, | ||
179 | ctx); | ||
160 | } | 180 | } |
161 | 181 | ||
162 | 182 | ||
163 | /** | 183 | /** |
164 | * Try to send 'data' to the | 184 | * Try to send @a data to the |
165 | * IP 'dst_ipv4' at port 'dport' via UDP. | 185 | * IP @a dst_ipv4' at port @a dport via UDP. |
166 | * | 186 | * |
167 | * @param dst_ipv4 target IP | 187 | * @param dst_ipv4 target IP |
168 | * @param dport target port | 188 | * @param dport target port |
169 | * @param data data to send | 189 | * @param data data to send |
170 | */ | 190 | */ |
171 | static void | 191 | static void |
172 | try_send_udp (uint32_t dst_ipv4, uint16_t dport, uint16_t data) | 192 | try_send_udp (uint32_t dst_ipv4, |
193 | uint16_t dport, | ||
194 | uint16_t data) | ||
173 | { | 195 | { |
174 | struct GNUNET_NETWORK_Handle *s; | 196 | struct GNUNET_NETWORK_Handle *s; |
175 | struct sockaddr_in sa; | 197 | struct sockaddr_in sa; |
176 | 198 | ||
177 | s = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0); | 199 | s = GNUNET_NETWORK_socket_create (AF_INET, |
200 | SOCK_DGRAM, | ||
201 | 0); | ||
178 | if (NULL == s) | 202 | if (NULL == s) |
179 | { | 203 | { |
180 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket"); | 204 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, |
205 | "socket"); | ||
181 | return; | 206 | return; |
182 | } | 207 | } |
183 | memset (&sa, 0, sizeof (sa)); | 208 | memset (&sa, 0, sizeof (sa)); |
@@ -187,12 +212,18 @@ try_send_udp (uint32_t dst_ipv4, uint16_t dport, uint16_t data) | |||
187 | #endif | 212 | #endif |
188 | sa.sin_addr.s_addr = dst_ipv4; | 213 | sa.sin_addr.s_addr = dst_ipv4; |
189 | sa.sin_port = htons (dport); | 214 | sa.sin_port = htons (dport); |
190 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending UDP packet to `%s'\n", | 215 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
191 | GNUNET_a2s ((struct sockaddr *) &sa, sizeof (sa))); | 216 | "Sending UDP packet to `%s'\n", |
217 | GNUNET_a2s ((struct sockaddr *) &sa, | ||
218 | sizeof (sa))); | ||
192 | if (-1 == | 219 | if (-1 == |
193 | GNUNET_NETWORK_socket_sendto (s, &data, sizeof (data), | 220 | GNUNET_NETWORK_socket_sendto (s, |
194 | (const struct sockaddr *) &sa, sizeof (sa))) | 221 | &data, |
195 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto"); | 222 | sizeof (data), |
223 | (const struct sockaddr *) &sa, | ||
224 | sizeof (sa))) | ||
225 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, | ||
226 | "sendto"); | ||
196 | GNUNET_NETWORK_socket_close (s); | 227 | GNUNET_NETWORK_socket_close (s); |
197 | } | 228 | } |
198 | 229 | ||
@@ -206,22 +237,31 @@ try_send_udp (uint32_t dst_ipv4, uint16_t dport, uint16_t data) | |||
206 | * @param msg message with details about what to test | 237 | * @param msg message with details about what to test |
207 | */ | 238 | */ |
208 | static void | 239 | static void |
209 | test (void *cls, struct GNUNET_SERVER_Client *client, | 240 | test (void *cls, |
241 | struct GNUNET_SERVER_Client *client, | ||
210 | const struct GNUNET_MessageHeader *msg) | 242 | const struct GNUNET_MessageHeader *msg) |
211 | { | 243 | { |
212 | const struct GNUNET_NAT_TestMessage *tm; | 244 | const struct GNUNET_NAT_TestMessage *tm; |
213 | uint16_t dport; | 245 | uint16_t dport; |
214 | 246 | ||
215 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received test request\n"); | 247 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
248 | "Received test request\n"); | ||
216 | tm = (const struct GNUNET_NAT_TestMessage *) msg; | 249 | tm = (const struct GNUNET_NAT_TestMessage *) msg; |
217 | dport = ntohs (tm->dport); | 250 | dport = ntohs (tm->dport); |
218 | if (0 == dport) | 251 | if (0 == dport) |
219 | try_anat (tm->dst_ipv4, ntohs (tm->data), (int) ntohl (tm->is_tcp)); | 252 | try_anat (tm->dst_ipv4, |
253 | ntohs (tm->data), | ||
254 | (int) ntohl (tm->is_tcp)); | ||
220 | else if (GNUNET_YES == ntohl (tm->is_tcp)) | 255 | else if (GNUNET_YES == ntohl (tm->is_tcp)) |
221 | try_send_tcp (tm->dst_ipv4, dport, tm->data); | 256 | try_send_tcp (tm->dst_ipv4, |
257 | dport, | ||
258 | tm->data); | ||
222 | else | 259 | else |
223 | try_send_udp (tm->dst_ipv4, dport, tm->data); | 260 | try_send_udp (tm->dst_ipv4, |
224 | GNUNET_SERVER_receive_done (client, GNUNET_NO); | 261 | dport, |
262 | tm->data); | ||
263 | GNUNET_SERVER_receive_done (client, | ||
264 | GNUNET_NO); | ||
225 | } | 265 | } |
226 | 266 | ||
227 | 267 | ||
@@ -247,7 +287,9 @@ shutdown_task (void *cls) | |||
247 | * @param c configuration | 287 | * @param c configuration |
248 | */ | 288 | */ |
249 | static void | 289 | static void |
250 | run (void *cls, char *const *args, const char *cfgfile, | 290 | run (void *cls, |
291 | char *const *args, | ||
292 | const char *cfgfile, | ||
251 | const struct GNUNET_CONFIGURATION_Handle *c) | 293 | const struct GNUNET_CONFIGURATION_Handle *c) |
252 | { | 294 | { |
253 | static const struct GNUNET_SERVER_MessageHandler handlers[] = { | 295 | static const struct GNUNET_SERVER_MessageHandler handlers[] = { |
diff --git a/src/nat/nat_test.c b/src/nat/nat_test.c index b2f22781e..803ff23e3 100644 --- a/src/nat/nat_test.c +++ b/src/nat/nat_test.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2011 GNUnet e.V. | 3 | Copyright (C) 2011, 2016 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 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 | 6 | it under the terms of the GNU General Public License as published |
@@ -60,7 +60,7 @@ struct NatActivity | |||
60 | /** | 60 | /** |
61 | * Task reading from the incoming connection. | 61 | * Task reading from the incoming connection. |
62 | */ | 62 | */ |
63 | struct GNUNET_SCHEDULER_Task * rtask; | 63 | struct GNUNET_SCHEDULER_Task *rtask; |
64 | }; | 64 | }; |
65 | 65 | ||
66 | 66 | ||
@@ -82,7 +82,12 @@ struct ClientActivity | |||
82 | /** | 82 | /** |
83 | * Socket of the incoming connection. | 83 | * Socket of the incoming connection. |
84 | */ | 84 | */ |
85 | struct GNUNET_CLIENT_Connection *client; | 85 | struct GNUNET_MQ_Handle *mq; |
86 | |||
87 | /** | ||
88 | * Handle to overall NAT test. | ||
89 | */ | ||
90 | struct GNUNET_NAT_Test *h; | ||
86 | 91 | ||
87 | }; | 92 | }; |
88 | 93 | ||
@@ -141,15 +146,15 @@ struct GNUNET_NAT_Test | |||
141 | /** | 146 | /** |
142 | * Identity of task for the listen socket (if any) | 147 | * Identity of task for the listen socket (if any) |
143 | */ | 148 | */ |
144 | struct GNUNET_SCHEDULER_Task * ltask; | 149 | struct GNUNET_SCHEDULER_Task *ltask; |
145 | 150 | ||
146 | /** | 151 | /** |
147 | * Task identifier for the timeout (if any) | 152 | * Task identifier for the timeout (if any) |
148 | */ | 153 | */ |
149 | struct GNUNET_SCHEDULER_Task * ttask; | 154 | struct GNUNET_SCHEDULER_Task *ttask; |
150 | 155 | ||
151 | /** | 156 | /** |
152 | * GNUNET_YES if we're testing TCP | 157 | * #GNUNET_YES if we're testing TCP |
153 | */ | 158 | */ |
154 | int is_tcp; | 159 | int is_tcp; |
155 | 160 | ||
@@ -176,7 +181,7 @@ struct GNUNET_NAT_Test | |||
176 | * | 181 | * |
177 | * @param cls closure, our `struct GNUNET_NAT_Handle` | 182 | * @param cls closure, our `struct GNUNET_NAT_Handle` |
178 | * @param addr public IP address of the other peer | 183 | * @param addr public IP address of the other peer |
179 | * @param addrlen actual lenght of the address | 184 | * @param addrlen actual lenght of the @a addr |
180 | */ | 185 | */ |
181 | static void | 186 | static void |
182 | reversal_cb (void *cls, | 187 | reversal_cb (void *cls, |
@@ -196,7 +201,8 @@ reversal_cb (void *cls, | |||
196 | return; /* wrong port */ | 201 | return; /* wrong port */ |
197 | } | 202 | } |
198 | /* report success */ | 203 | /* report success */ |
199 | h->report (h->report_cls, GNUNET_NAT_ERROR_SUCCESS); | 204 | h->report (h->report_cls, |
205 | GNUNET_NAT_ERROR_SUCCESS); | ||
200 | } | 206 | } |
201 | 207 | ||
202 | 208 | ||
@@ -217,14 +223,19 @@ do_udp_read (void *cls) | |||
217 | tst->ltask = | 223 | tst->ltask = |
218 | GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | 224 | GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, |
219 | tst->lsock, | 225 | tst->lsock, |
220 | &do_udp_read, tst); | 226 | &do_udp_read, |
227 | tst); | ||
221 | if ((NULL != tc->write_ready) && | 228 | if ((NULL != tc->write_ready) && |
222 | (GNUNET_NETWORK_fdset_isset (tc->read_ready, tst->lsock)) && | 229 | (GNUNET_NETWORK_fdset_isset (tc->read_ready, |
230 | tst->lsock)) && | ||
223 | (sizeof (data) == | 231 | (sizeof (data) == |
224 | GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data)))) | 232 | GNUNET_NETWORK_socket_recv (tst->lsock, |
233 | &data, | ||
234 | sizeof (data)))) | ||
225 | { | 235 | { |
226 | if (data == tst->data) | 236 | if (data == tst->data) |
227 | tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); | 237 | tst->report (tst->report_cls, |
238 | GNUNET_NAT_ERROR_SUCCESS); | ||
228 | else | 239 | else |
229 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 240 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
230 | "Received data mismatches expected value\n"); | 241 | "Received data mismatches expected value\n"); |
@@ -256,12 +267,16 @@ do_read (void *cls) | |||
256 | tst->na_tail, | 267 | tst->na_tail, |
257 | na); | 268 | na); |
258 | if ((NULL != tc->write_ready) && | 269 | if ((NULL != tc->write_ready) && |
259 | (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) && | 270 | (GNUNET_NETWORK_fdset_isset (tc->read_ready, |
271 | na->sock)) && | ||
260 | (sizeof (data) == | 272 | (sizeof (data) == |
261 | GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data)))) | 273 | GNUNET_NETWORK_socket_recv (na->sock, |
274 | &data, | ||
275 | sizeof (data)))) | ||
262 | { | 276 | { |
263 | if (data == tst->data) | 277 | if (data == tst->data) |
264 | tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS); | 278 | tst->report (tst->report_cls, |
279 | GNUNET_NAT_ERROR_SUCCESS); | ||
265 | else | 280 | else |
266 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 281 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
267 | "Received data does not match expected value\n"); | 282 | "Received data does not match expected value\n"); |
@@ -287,14 +302,17 @@ do_accept (void *cls) | |||
287 | struct GNUNET_NETWORK_Handle *s; | 302 | struct GNUNET_NETWORK_Handle *s; |
288 | struct NatActivity *wl; | 303 | struct NatActivity *wl; |
289 | 304 | ||
290 | tst->ltask = | 305 | tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, |
291 | GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | 306 | tst->lsock, |
292 | tst->lsock, | 307 | &do_accept, |
293 | &do_accept, tst); | 308 | tst); |
294 | s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL); | 309 | s = GNUNET_NETWORK_socket_accept (tst->lsock, |
310 | NULL, | ||
311 | NULL); | ||
295 | if (NULL == s) | 312 | if (NULL == s) |
296 | { | 313 | { |
297 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept"); | 314 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, |
315 | "accept"); | ||
298 | return; /* odd error */ | 316 | return; /* odd error */ |
299 | } | 317 | } |
300 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 318 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
@@ -305,7 +323,8 @@ do_accept (void *cls) | |||
305 | wl->rtask = | 323 | wl->rtask = |
306 | GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | 324 | GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, |
307 | wl->sock, | 325 | wl->sock, |
308 | &do_read, wl); | 326 | &do_read, |
327 | wl); | ||
309 | GNUNET_CONTAINER_DLL_insert (tst->na_head, | 328 | GNUNET_CONTAINER_DLL_insert (tst->na_head, |
310 | tst->na_tail, | 329 | tst->na_tail, |
311 | wl); | 330 | wl); |
@@ -313,6 +332,28 @@ do_accept (void *cls) | |||
313 | 332 | ||
314 | 333 | ||
315 | /** | 334 | /** |
335 | * We got disconnected from the NAT server. Stop | ||
336 | * waiting for a reply. | ||
337 | * | ||
338 | * @param cls the `struct ClientActivity` | ||
339 | * @param error error code | ||
340 | */ | ||
341 | static void | ||
342 | mq_error_handler (void *cls, | ||
343 | enum GNUNET_MQ_Error error) | ||
344 | { | ||
345 | struct ClientActivity *ca = cls; | ||
346 | struct GNUNET_NAT_Test *tst = ca->h; | ||
347 | |||
348 | GNUNET_CONTAINER_DLL_remove (tst->ca_head, | ||
349 | tst->ca_tail, | ||
350 | ca); | ||
351 | GNUNET_MQ_destroy (ca->mq); | ||
352 | GNUNET_free (ca); | ||
353 | } | ||
354 | |||
355 | |||
356 | /** | ||
316 | * Address-callback, used to send message to gnunet-nat-server. | 357 | * Address-callback, used to send message to gnunet-nat-server. |
317 | * | 358 | * |
318 | * @param cls closure | 359 | * @param cls closure |
@@ -329,8 +370,8 @@ addr_cb (void *cls, | |||
329 | { | 370 | { |
330 | struct GNUNET_NAT_Test *h = cls; | 371 | struct GNUNET_NAT_Test *h = cls; |
331 | struct ClientActivity *ca; | 372 | struct ClientActivity *ca; |
332 | struct GNUNET_CLIENT_Connection *client; | 373 | struct GNUNET_MQ_Envelope *env; |
333 | struct GNUNET_NAT_TestMessage msg; | 374 | struct GNUNET_NAT_TestMessage *msg; |
334 | const struct sockaddr_in *sa; | 375 | const struct sockaddr_in *sa; |
335 | 376 | ||
336 | if (GNUNET_YES != add_remove) | 377 | if (GNUNET_YES != add_remove) |
@@ -339,35 +380,41 @@ addr_cb (void *cls, | |||
339 | { | 380 | { |
340 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 381 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
341 | "NAT test ignores IPv6 address `%s' returned from NAT library\n", | 382 | "NAT test ignores IPv6 address `%s' returned from NAT library\n", |
342 | GNUNET_a2s (addr, addrlen)); | 383 | GNUNET_a2s (addr, |
384 | addrlen)); | ||
343 | return; /* ignore IPv6 here */ | 385 | return; /* ignore IPv6 here */ |
344 | } | 386 | } |
345 | LOG (GNUNET_ERROR_TYPE_INFO, | 387 | LOG (GNUNET_ERROR_TYPE_INFO, |
346 | "Asking gnunet-nat-server to connect to `%s'\n", | 388 | "Asking gnunet-nat-server to connect to `%s'\n", |
347 | GNUNET_a2s (addr, addrlen)); | 389 | GNUNET_a2s (addr, |
348 | sa = (const struct sockaddr_in *) addr; | 390 | addrlen)); |
349 | msg.header.size = htons (sizeof (struct GNUNET_NAT_TestMessage)); | 391 | |
350 | msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST); | 392 | ca = GNUNET_new (struct ClientActivity); |
351 | msg.dst_ipv4 = sa->sin_addr.s_addr; | 393 | ca->h = h; |
352 | msg.dport = sa->sin_port; | 394 | ca->mq = GNUNET_CLIENT_connecT (h->cfg, |
353 | msg.data = h->data; | 395 | "gnunet-nat-server", |
354 | msg.is_tcp = htonl ((uint32_t) h->is_tcp); | 396 | NULL, |
355 | 397 | &mq_error_handler, | |
356 | client = GNUNET_CLIENT_connect ("gnunet-nat-server", h->cfg); | 398 | ca); |
357 | if (NULL == client) | 399 | if (NULL == ca->mq) |
358 | { | 400 | { |
401 | GNUNET_free (ca); | ||
359 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 402 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
360 | _("Failed to connect to `gnunet-nat-server'\n")); | 403 | _("Failed to connect to `gnunet-nat-server'\n")); |
361 | return; | 404 | return; |
362 | } | 405 | } |
363 | ca = GNUNET_new (struct ClientActivity); | 406 | GNUNET_CONTAINER_DLL_insert (h->ca_head, |
364 | ca->client = client; | 407 | h->ca_tail, |
365 | GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca); | 408 | ca); |
366 | GNUNET_break (GNUNET_OK == | 409 | sa = (const struct sockaddr_in *) addr; |
367 | GNUNET_CLIENT_transmit_and_get_response (client, &msg.header, | 410 | env = GNUNET_MQ_msg (msg, |
368 | NAT_SERVER_TIMEOUT, | 411 | GNUNET_MESSAGE_TYPE_NAT_TEST); |
369 | GNUNET_YES, NULL, | 412 | msg->dst_ipv4 = sa->sin_addr.s_addr; |
370 | NULL)); | 413 | msg->dport = sa->sin_port; |
414 | msg->data = h->data; | ||
415 | msg->is_tcp = htonl ((uint32_t) h->is_tcp); | ||
416 | GNUNET_MQ_send (ca->mq, | ||
417 | env); | ||
371 | } | 418 | } |
372 | 419 | ||
373 | 420 | ||
@@ -418,8 +465,12 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
418 | { | 465 | { |
419 | struct GNUNET_NAT_Test *nh; | 466 | struct GNUNET_NAT_Test *nh; |
420 | struct sockaddr_in sa; | 467 | struct sockaddr_in sa; |
421 | const struct sockaddr *addrs[] = { (const struct sockaddr *) &sa }; | 468 | const struct sockaddr *addrs[] = { |
422 | const socklen_t addrlens[] = { sizeof (sa) }; | 469 | (const struct sockaddr *) &sa |
470 | }; | ||
471 | const socklen_t addrlens[] = { | ||
472 | sizeof (sa) | ||
473 | }; | ||
423 | 474 | ||
424 | memset (&sa, 0, sizeof (sa)); | 475 | memset (&sa, 0, sizeof (sa)); |
425 | sa.sin_family = AF_INET; | 476 | sa.sin_family = AF_INET; |
@@ -439,10 +490,16 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
439 | if (0 == bnd_port) | 490 | if (0 == bnd_port) |
440 | { | 491 | { |
441 | nh->nat | 492 | nh->nat |
442 | = GNUNET_NAT_register (cfg, is_tcp, 0, 0, | 493 | = GNUNET_NAT_register (cfg, |
443 | NULL, NULL, | 494 | is_tcp, |
495 | 0, | ||
496 | 0, | ||
497 | NULL, | ||
498 | NULL, | ||
444 | &addr_cb, | 499 | &addr_cb, |
445 | &reversal_cb, nh, NULL); | 500 | &reversal_cb, |
501 | nh, | ||
502 | NULL); | ||
446 | } | 503 | } |
447 | else | 504 | else |
448 | { | 505 | { |
@@ -453,12 +510,14 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
453 | 0); | 510 | 0); |
454 | if ((nh->lsock == NULL) || | 511 | if ((nh->lsock == NULL) || |
455 | (GNUNET_OK != | 512 | (GNUNET_OK != |
456 | GNUNET_NETWORK_socket_bind (nh->lsock, (const struct sockaddr *) &sa, | 513 | GNUNET_NETWORK_socket_bind (nh->lsock, |
514 | (const struct sockaddr *) &sa, | ||
457 | sizeof (sa)))) | 515 | sizeof (sa)))) |
458 | { | 516 | { |
459 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 517 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
460 | _("Failed to create listen socket bound to `%s' for NAT test: %s\n"), | 518 | _("Failed to create listen socket bound to `%s' for NAT test: %s\n"), |
461 | GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)), | 519 | GNUNET_a2s ((const struct sockaddr *) &sa, |
520 | sizeof (sa)), | ||
462 | STRERROR (errno)); | 521 | STRERROR (errno)); |
463 | if (NULL != nh->lsock) | 522 | if (NULL != nh->lsock) |
464 | { | 523 | { |
@@ -466,31 +525,43 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
466 | nh->lsock = NULL; | 525 | nh->lsock = NULL; |
467 | } | 526 | } |
468 | nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR; | 527 | nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR; |
469 | nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, nh); | 528 | nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, |
529 | nh); | ||
470 | return nh; | 530 | return nh; |
471 | } | 531 | } |
472 | if (GNUNET_YES == is_tcp) | 532 | if (GNUNET_YES == is_tcp) |
473 | { | 533 | { |
474 | GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (nh->lsock, 5)); | 534 | GNUNET_break (GNUNET_OK == |
535 | GNUNET_NETWORK_socket_listen (nh->lsock, | ||
536 | 5)); | ||
475 | nh->ltask = | 537 | nh->ltask = |
476 | GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | 538 | GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, |
477 | nh->lsock, | 539 | nh->lsock, |
478 | &do_accept, nh); | 540 | &do_accept, |
541 | nh); | ||
479 | } | 542 | } |
480 | else | 543 | else |
481 | { | 544 | { |
482 | nh->ltask = | 545 | nh->ltask = |
483 | GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | 546 | GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, |
484 | nh->lsock, | 547 | nh->lsock, |
485 | &do_udp_read, nh); | 548 | &do_udp_read, |
549 | nh); | ||
486 | } | 550 | } |
487 | LOG (GNUNET_ERROR_TYPE_INFO, | 551 | LOG (GNUNET_ERROR_TYPE_INFO, |
488 | "NAT test listens on port %u (%s)\n", | 552 | "NAT test listens on port %u (%s)\n", |
489 | bnd_port, | 553 | bnd_port, |
490 | (GNUNET_YES == is_tcp) ? "tcp" : "udp"); | 554 | (GNUNET_YES == is_tcp) ? "tcp" : "udp"); |
491 | nh->nat = GNUNET_NAT_register (cfg, is_tcp, adv_port, 1, | 555 | nh->nat = GNUNET_NAT_register (cfg, |
492 | addrs, addrlens, | 556 | is_tcp, |
493 | &addr_cb, NULL, nh, NULL); | 557 | adv_port, |
558 | 1, | ||
559 | addrs, | ||
560 | addrlens, | ||
561 | &addr_cb, | ||
562 | NULL, | ||
563 | nh, | ||
564 | NULL); | ||
494 | if (NULL == nh->nat) | 565 | if (NULL == nh->nat) |
495 | { | 566 | { |
496 | LOG (GNUNET_ERROR_TYPE_INFO, | 567 | LOG (GNUNET_ERROR_TYPE_INFO, |
@@ -506,7 +577,8 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
506 | nh->lsock = NULL; | 577 | nh->lsock = NULL; |
507 | } | 578 | } |
508 | nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED; | 579 | nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED; |
509 | nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, nh); | 580 | nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, |
581 | nh); | ||
510 | return nh; | 582 | return nh; |
511 | } | 583 | } |
512 | } | 584 | } |
@@ -535,7 +607,7 @@ GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst) | |||
535 | GNUNET_CONTAINER_DLL_remove (tst->ca_head, | 607 | GNUNET_CONTAINER_DLL_remove (tst->ca_head, |
536 | tst->ca_tail, | 608 | tst->ca_tail, |
537 | cpos); | 609 | cpos); |
538 | GNUNET_CLIENT_disconnect (cpos->client); | 610 | GNUNET_MQ_destroy (cpos->mq); |
539 | GNUNET_free (cpos); | 611 | GNUNET_free (cpos); |
540 | } | 612 | } |
541 | while (NULL != (pos = tst->na_head)) | 613 | while (NULL != (pos = tst->na_head)) |
@@ -548,13 +620,25 @@ GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst) | |||
548 | GNUNET_free (pos); | 620 | GNUNET_free (pos); |
549 | } | 621 | } |
550 | if (NULL != tst->ttask) | 622 | if (NULL != tst->ttask) |
623 | { | ||
551 | GNUNET_SCHEDULER_cancel (tst->ttask); | 624 | GNUNET_SCHEDULER_cancel (tst->ttask); |
625 | tst->ttask = NULL; | ||
626 | } | ||
552 | if (NULL != tst->ltask) | 627 | if (NULL != tst->ltask) |
628 | { | ||
553 | GNUNET_SCHEDULER_cancel (tst->ltask); | 629 | GNUNET_SCHEDULER_cancel (tst->ltask); |
630 | tst->ltask = NULL; | ||
631 | } | ||
554 | if (NULL != tst->lsock) | 632 | if (NULL != tst->lsock) |
633 | { | ||
555 | GNUNET_NETWORK_socket_close (tst->lsock); | 634 | GNUNET_NETWORK_socket_close (tst->lsock); |
635 | tst->lsock = NULL; | ||
636 | } | ||
556 | if (NULL != tst->nat) | 637 | if (NULL != tst->nat) |
638 | { | ||
557 | GNUNET_NAT_unregister (tst->nat); | 639 | GNUNET_NAT_unregister (tst->nat); |
640 | tst->nat = NULL; | ||
641 | } | ||
558 | GNUNET_free (tst); | 642 | GNUNET_free (tst); |
559 | } | 643 | } |
560 | 644 | ||