aboutsummaryrefslogtreecommitdiff
path: root/src/nat-auto/gnunet-nat-server.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nat-auto/gnunet-nat-server.c')
-rw-r--r--src/nat-auto/gnunet-nat-server.c270
1 files changed, 136 insertions, 134 deletions
diff --git a/src/nat-auto/gnunet-nat-server.c b/src/nat-auto/gnunet-nat-server.c
index db44d7629..5186cb8c0 100644
--- a/src/nat-auto/gnunet-nat-server.c
+++ b/src/nat-auto/gnunet-nat-server.c
@@ -33,7 +33,8 @@
33/** 33/**
34 * Information we track per client. 34 * Information we track per client.
35 */ 35 */
36struct ClientData { 36struct ClientData
37{
37 /** 38 /**
38 * Timeout task. 39 * Timeout task.
39 */ 40 */
@@ -60,9 +61,9 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
60 * @param is_tcp mark for TCP (#GNUNET_YES) or UDP (#GNUNET_NO) 61 * @param is_tcp mark for TCP (#GNUNET_YES) or UDP (#GNUNET_NO)
61 */ 62 */
62static void 63static void
63try_anat(uint32_t dst_ipv4, 64try_anat (uint32_t dst_ipv4,
64 uint16_t dport, 65 uint16_t dport,
65 int is_tcp) 66 int is_tcp)
66{ 67{
67 struct GNUNET_NAT_Handle *h; 68 struct GNUNET_NAT_Handle *h;
68 struct sockaddr_in lsa; 69 struct sockaddr_in lsa;
@@ -70,44 +71,45 @@ try_anat(uint32_t dst_ipv4,
70 const struct sockaddr *sa; 71 const struct sockaddr *sa;
71 socklen_t sa_len; 72 socklen_t sa_len;
72 73
73 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 74 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
74 "Asking for connection reversal with %x and code %u\n", 75 "Asking for connection reversal with %x and code %u\n",
75 (unsigned int)dst_ipv4, 76 (unsigned int) dst_ipv4,
76 (unsigned int)dport); 77 (unsigned int) dport);
77 memset(&lsa, 0, sizeof(lsa)); 78 memset (&lsa, 0, sizeof(lsa));
78 lsa.sin_family = AF_INET; 79 lsa.sin_family = AF_INET;
79#if HAVE_SOCKADDR_IN_SIN_LEN 80#if HAVE_SOCKADDR_IN_SIN_LEN
80 lsa.sin_len = sizeof(sa); 81 lsa.sin_len = sizeof(sa);
81#endif 82#endif
82 lsa.sin_addr.s_addr = 0; 83 lsa.sin_addr.s_addr = 0;
83 lsa.sin_port = htons(dport); 84 lsa.sin_port = htons (dport);
84 memset(&rsa, 0, sizeof(rsa)); 85 memset (&rsa, 0, sizeof(rsa));
85 rsa.sin_family = AF_INET; 86 rsa.sin_family = AF_INET;
86#if HAVE_SOCKADDR_IN_SIN_LEN 87#if HAVE_SOCKADDR_IN_SIN_LEN
87 rsa.sin_len = sizeof(sa); 88 rsa.sin_len = sizeof(sa);
88#endif 89#endif
89 rsa.sin_addr.s_addr = dst_ipv4; 90 rsa.sin_addr.s_addr = dst_ipv4;
90 rsa.sin_port = htons(dport); 91 rsa.sin_port = htons (dport);
91 sa_len = sizeof(lsa); 92 sa_len = sizeof(lsa);
92 sa = (const struct sockaddr *)&lsa; 93 sa = (const struct sockaddr *) &lsa;
93 h = GNUNET_NAT_register(cfg, 94 h = GNUNET_NAT_register (cfg,
94 "none", 95 "none",
95 is_tcp ? IPPROTO_TCP : IPPROTO_UDP, 96 is_tcp ? IPPROTO_TCP : IPPROTO_UDP,
96 1, 97 1,
97 &sa, 98 &sa,
98 &sa_len, 99 &sa_len,
99 NULL, NULL, NULL); 100 NULL, NULL, NULL);
100 GNUNET_NAT_request_reversal(h, 101 GNUNET_NAT_request_reversal (h,
101 &lsa, 102 &lsa,
102 &rsa); 103 &rsa);
103 GNUNET_NAT_unregister(h); 104 GNUNET_NAT_unregister (h);
104} 105}
105 106
106 107
107/** 108/**
108 * Closure for #tcp_send. 109 * Closure for #tcp_send.
109 */ 110 */
110struct TcpContext { 111struct TcpContext
112{
111 /** 113 /**
112 * TCP socket. 114 * TCP socket.
113 */ 115 */
@@ -127,24 +129,24 @@ struct TcpContext {
127 * @param cls the `struct TcpContext` 129 * @param cls the `struct TcpContext`
128 */ 130 */
129static void 131static void
130tcp_send(void *cls) 132tcp_send (void *cls)
131{ 133{
132 struct TcpContext *ctx = cls; 134 struct TcpContext *ctx = cls;
133 const struct GNUNET_SCHEDULER_TaskContext *tc; 135 const struct GNUNET_SCHEDULER_TaskContext *tc;
134 136
135 tc = GNUNET_SCHEDULER_get_task_context(); 137 tc = GNUNET_SCHEDULER_get_task_context ();
136 if ((NULL != tc->write_ready) && 138 if ((NULL != tc->write_ready) &&
137 (GNUNET_NETWORK_fdset_isset(tc->write_ready, ctx->s))) 139 (GNUNET_NETWORK_fdset_isset (tc->write_ready, ctx->s)))
140 {
141 if (-1 ==
142 GNUNET_NETWORK_socket_send (ctx->s, &ctx->data, sizeof(ctx->data)))
138 { 143 {
139 if (-1 == 144 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
140 GNUNET_NETWORK_socket_send(ctx->s, &ctx->data, sizeof(ctx->data)))
141 {
142 GNUNET_log_strerror(GNUNET_ERROR_TYPE_DEBUG, "send");
143 }
144 GNUNET_NETWORK_socket_shutdown(ctx->s, SHUT_RDWR);
145 } 145 }
146 GNUNET_NETWORK_socket_close(ctx->s); 146 GNUNET_NETWORK_socket_shutdown (ctx->s, SHUT_RDWR);
147 GNUNET_free(ctx); 147 }
148 GNUNET_NETWORK_socket_close (ctx->s);
149 GNUNET_free (ctx);
148} 150}
149 151
150 152
@@ -157,52 +159,52 @@ tcp_send(void *cls)
157 * @param data data to send 159 * @param data data to send
158 */ 160 */
159static void 161static void
160try_send_tcp(uint32_t dst_ipv4, 162try_send_tcp (uint32_t dst_ipv4,
161 uint16_t dport, 163 uint16_t dport,
162 uint16_t data) 164 uint16_t data)
163{ 165{
164 struct GNUNET_NETWORK_Handle *s; 166 struct GNUNET_NETWORK_Handle *s;
165 struct sockaddr_in sa; 167 struct sockaddr_in sa;
166 struct TcpContext *ctx; 168 struct TcpContext *ctx;
167 169
168 s = GNUNET_NETWORK_socket_create(AF_INET, 170 s = GNUNET_NETWORK_socket_create (AF_INET,
169 SOCK_STREAM, 171 SOCK_STREAM,
170 0); 172 0);
171 if (NULL == s) 173 if (NULL == s)
172 { 174 {
173 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, 175 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
174 "socket"); 176 "socket");
175 return; 177 return;
176 } 178 }
177 memset(&sa, 0, sizeof(sa)); 179 memset (&sa, 0, sizeof(sa));
178 sa.sin_family = AF_INET; 180 sa.sin_family = AF_INET;
179#if HAVE_SOCKADDR_IN_SIN_LEN 181#if HAVE_SOCKADDR_IN_SIN_LEN
180 sa.sin_len = sizeof(sa); 182 sa.sin_len = sizeof(sa);
181#endif 183#endif
182 sa.sin_addr.s_addr = dst_ipv4; 184 sa.sin_addr.s_addr = dst_ipv4;
183 sa.sin_port = htons(dport); 185 sa.sin_port = htons (dport);
184 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185 "Sending TCP message to `%s'\n", 187 "Sending TCP message to `%s'\n",
186 GNUNET_a2s((struct sockaddr *)&sa, 188 GNUNET_a2s ((struct sockaddr *) &sa,
187 sizeof(sa))); 189 sizeof(sa)));
188 if ((GNUNET_OK != 190 if ((GNUNET_OK !=
189 GNUNET_NETWORK_socket_connect(s, 191 GNUNET_NETWORK_socket_connect (s,
190 (const struct sockaddr *)&sa, 192 (const struct sockaddr *) &sa,
191 sizeof(sa))) && 193 sizeof(sa))) &&
192 (errno != EINPROGRESS)) 194 (errno != EINPROGRESS))
193 { 195 {
194 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, 196 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
195 "connect"); 197 "connect");
196 GNUNET_NETWORK_socket_close(s); 198 GNUNET_NETWORK_socket_close (s);
197 return; 199 return;
198 } 200 }
199 ctx = GNUNET_new(struct TcpContext); 201 ctx = GNUNET_new (struct TcpContext);
200 ctx->s = s; 202 ctx->s = s;
201 ctx->data = data; 203 ctx->data = data;
202 GNUNET_SCHEDULER_add_write_net(GNUNET_TIME_UNIT_SECONDS, 204 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_SECONDS,
203 s, 205 s,
204 &tcp_send, 206 &tcp_send,
205 ctx); 207 ctx);
206} 208}
207 209
208 210
@@ -215,42 +217,42 @@ try_send_tcp(uint32_t dst_ipv4,
215 * @param data data to send 217 * @param data data to send
216 */ 218 */
217static void 219static void
218try_send_udp(uint32_t dst_ipv4, 220try_send_udp (uint32_t dst_ipv4,
219 uint16_t dport, 221 uint16_t dport,
220 uint16_t data) 222 uint16_t data)
221{ 223{
222 struct GNUNET_NETWORK_Handle *s; 224 struct GNUNET_NETWORK_Handle *s;
223 struct sockaddr_in sa; 225 struct sockaddr_in sa;
224 226
225 s = GNUNET_NETWORK_socket_create(AF_INET, 227 s = GNUNET_NETWORK_socket_create (AF_INET,
226 SOCK_DGRAM, 228 SOCK_DGRAM,
227 0); 229 0);
228 if (NULL == s) 230 if (NULL == s)
229 { 231 {
230 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, 232 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
231 "socket"); 233 "socket");
232 return; 234 return;
233 } 235 }
234 memset(&sa, 0, sizeof(sa)); 236 memset (&sa, 0, sizeof(sa));
235 sa.sin_family = AF_INET; 237 sa.sin_family = AF_INET;
236#if HAVE_SOCKADDR_IN_SIN_LEN 238#if HAVE_SOCKADDR_IN_SIN_LEN
237 sa.sin_len = sizeof(sa); 239 sa.sin_len = sizeof(sa);
238#endif 240#endif
239 sa.sin_addr.s_addr = dst_ipv4; 241 sa.sin_addr.s_addr = dst_ipv4;
240 sa.sin_port = htons(dport); 242 sa.sin_port = htons (dport);
241 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242 "Sending UDP packet to `%s'\n", 244 "Sending UDP packet to `%s'\n",
243 GNUNET_a2s((struct sockaddr *)&sa, 245 GNUNET_a2s ((struct sockaddr *) &sa,
244 sizeof(sa))); 246 sizeof(sa)));
245 if (-1 == 247 if (-1 ==
246 GNUNET_NETWORK_socket_sendto(s, 248 GNUNET_NETWORK_socket_sendto (s,
247 &data, 249 &data,
248 sizeof(data), 250 sizeof(data),
249 (const struct sockaddr *)&sa, 251 (const struct sockaddr *) &sa,
250 sizeof(sa))) 252 sizeof(sa)))
251 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, 253 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
252 "sendto"); 254 "sendto");
253 GNUNET_NETWORK_socket_close(s); 255 GNUNET_NETWORK_socket_close (s);
254} 256}
255 257
256 258
@@ -262,28 +264,28 @@ try_send_udp(uint32_t dst_ipv4,
262 * @param msg message with details about what to test 264 * @param msg message with details about what to test
263 */ 265 */
264static void 266static void
265handle_test(void *cls, 267handle_test (void *cls,
266 const struct GNUNET_NAT_AUTO_TestMessage *tm) 268 const struct GNUNET_NAT_AUTO_TestMessage *tm)
267{ 269{
268 struct ClientData *cd = cls; 270 struct ClientData *cd = cls;
269 uint16_t dport; 271 uint16_t dport;
270 272
271 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272 "Received test request\n"); 274 "Received test request\n");
273 dport = ntohs(tm->dport); 275 dport = ntohs (tm->dport);
274 if (0 == dport) 276 if (0 == dport)
275 try_anat(tm->dst_ipv4, 277 try_anat (tm->dst_ipv4,
276 ntohs(tm->data), 278 ntohs (tm->data),
277 (int)ntohl(tm->is_tcp)); 279 (int) ntohl (tm->is_tcp));
278 else if (GNUNET_YES == ntohl(tm->is_tcp)) 280 else if (GNUNET_YES == ntohl (tm->is_tcp))
279 try_send_tcp(tm->dst_ipv4, 281 try_send_tcp (tm->dst_ipv4,
280 dport, 282 dport,
281 tm->data); 283 tm->data);
282 else 284 else
283 try_send_udp(tm->dst_ipv4, 285 try_send_udp (tm->dst_ipv4,
284 dport, 286 dport,
285 tm->data); 287 tm->data);
286 GNUNET_SERVICE_client_drop(cd->client); 288 GNUNET_SERVICE_client_drop (cd->client);
287} 289}
288 290
289 291
@@ -295,9 +297,9 @@ handle_test(void *cls,
295 * @param srv service handle 297 * @param srv service handle
296 */ 298 */
297static void 299static void
298run(void *cls, 300run (void *cls,
299 const struct GNUNET_CONFIGURATION_Handle *c, 301 const struct GNUNET_CONFIGURATION_Handle *c,
300 struct GNUNET_SERVICE_Handle *srv) 302 struct GNUNET_SERVICE_Handle *srv)
301{ 303{
302 cfg = c; 304 cfg = c;
303} 305}
@@ -309,12 +311,12 @@ run(void *cls,
309 * @param cls our `struct ClientData` of a client to drop 311 * @param cls our `struct ClientData` of a client to drop
310 */ 312 */
311static void 313static void
312force_timeout(void *cls) 314force_timeout (void *cls)
313{ 315{
314 struct ClientData *cd = cls; 316 struct ClientData *cd = cls;
315 317
316 cd->tt = NULL; 318 cd->tt = NULL;
317 GNUNET_SERVICE_client_drop(cd->client); 319 GNUNET_SERVICE_client_drop (cd->client);
318} 320}
319 321
320 322
@@ -328,17 +330,17 @@ force_timeout(void *cls)
328 * @return our `struct ClientData` 330 * @return our `struct ClientData`
329 */ 331 */
330static void * 332static void *
331client_connect_cb(void *cls, 333client_connect_cb (void *cls,
332 struct GNUNET_SERVICE_Client *c, 334 struct GNUNET_SERVICE_Client *c,
333 struct GNUNET_MQ_Handle *mq) 335 struct GNUNET_MQ_Handle *mq)
334{ 336{
335 struct ClientData *cd; 337 struct ClientData *cd;
336 338
337 cd = GNUNET_new(struct ClientData); 339 cd = GNUNET_new (struct ClientData);
338 cd->client = c; 340 cd->client = c;
339 cd->tt = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, 341 cd->tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
340 &force_timeout, 342 &force_timeout,
341 cd); 343 cd);
342 return cd; 344 return cd;
343} 345}
344 346
@@ -351,15 +353,15 @@ client_connect_cb(void *cls,
351 * @param internal_cls our `struct ClientData` 353 * @param internal_cls our `struct ClientData`
352 */ 354 */
353static void 355static void
354client_disconnect_cb(void *cls, 356client_disconnect_cb (void *cls,
355 struct GNUNET_SERVICE_Client *c, 357 struct GNUNET_SERVICE_Client *c,
356 void *internal_cls) 358 void *internal_cls)
357{ 359{
358 struct ClientData *cd = internal_cls; 360 struct ClientData *cd = internal_cls;
359 361
360 if (NULL != cd->tt) 362 if (NULL != cd->tt)
361 GNUNET_SCHEDULER_cancel(cd->tt); 363 GNUNET_SCHEDULER_cancel (cd->tt);
362 GNUNET_free(cd); 364 GNUNET_free (cd);
363} 365}
364 366
365 367
@@ -373,11 +375,11 @@ GNUNET_SERVICE_MAIN
373 &client_connect_cb, 375 &client_connect_cb,
374 &client_disconnect_cb, 376 &client_disconnect_cb,
375 NULL, 377 NULL,
376 GNUNET_MQ_hd_fixed_size(test, 378 GNUNET_MQ_hd_fixed_size (test,
377 GNUNET_MESSAGE_TYPE_NAT_TEST, 379 GNUNET_MESSAGE_TYPE_NAT_TEST,
378 struct GNUNET_NAT_AUTO_TestMessage, 380 struct GNUNET_NAT_AUTO_TestMessage,
379 NULL), 381 NULL),
380 GNUNET_MQ_handler_end()); 382 GNUNET_MQ_handler_end ());
381 383
382 384
383#if defined(LINUX) && defined(__GLIBC__) 385#if defined(LINUX) && defined(__GLIBC__)
@@ -387,11 +389,11 @@ GNUNET_SERVICE_MAIN
387 * MINIMIZE heap size (way below 128k) since this process doesn't need much. 389 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
388 */ 390 */
389void __attribute__ ((constructor)) 391void __attribute__ ((constructor))
390GNUNET_ARM_memory_init() 392GNUNET_ARM_memory_init ()
391{ 393{
392 mallopt(M_TRIM_THRESHOLD, 4 * 1024); 394 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
393 mallopt(M_TOP_PAD, 1 * 1024); 395 mallopt (M_TOP_PAD, 1 * 1024);
394 malloc_trim(0); 396 malloc_trim (0);
395} 397}
396#endif 398#endif
397 399