aboutsummaryrefslogtreecommitdiff
path: root/src/nat
diff options
context:
space:
mode:
Diffstat (limited to 'src/nat')
-rw-r--r--src/nat/gnunet-helper-nat-client-windows.c325
-rw-r--r--src/nat/gnunet-helper-nat-client.c341
-rw-r--r--src/nat/gnunet-helper-nat-server-windows.c433
-rw-r--r--src/nat/gnunet-helper-nat-server.c583
-rw-r--r--src/nat/gnunet-nat.c432
-rw-r--r--src/nat/gnunet-service-nat.c2190
-rw-r--r--src/nat/gnunet-service-nat.h26
-rw-r--r--src/nat/gnunet-service-nat_externalip.c181
-rw-r--r--src/nat/gnunet-service-nat_externalip.h18
-rw-r--r--src/nat/gnunet-service-nat_helper.c368
-rw-r--r--src/nat/gnunet-service-nat_helper.h28
-rw-r--r--src/nat/gnunet-service-nat_mini.c594
-rw-r--r--src/nat/gnunet-service-nat_mini.h20
-rw-r--r--src/nat/gnunet-service-nat_stun.c178
-rw-r--r--src/nat/gnunet-service-nat_stun.h10
-rw-r--r--src/nat/nat.h34
-rw-r--r--src/nat/nat_api.c548
-rw-r--r--src/nat/nat_api_stun.c155
-rw-r--r--src/nat/nat_stun.h67
-rw-r--r--src/nat/test_nat.c124
-rw-r--r--src/nat/test_nat_mini.c84
-rw-r--r--src/nat/test_nat_test.c107
-rw-r--r--src/nat/test_stun.c278
23 files changed, 3549 insertions, 3575 deletions
diff --git a/src/nat/gnunet-helper-nat-client-windows.c b/src/nat/gnunet-helper-nat-client-windows.c
index dda6552d8..2dac5d48e 100644
--- a/src/nat/gnunet-helper-nat-client-windows.c
+++ b/src/nat/gnunet-helper-nat-client-windows.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/** 21/**
22 * @file src/nat/gnunet-helper-nat-client-windows.c 22 * @file src/nat/gnunet-helper-nat-client-windows.c
@@ -43,7 +43,7 @@
43 */ 43 */
44#define _GNU_SOURCE 44#define _GNU_SOURCE
45/* Instead of including gnunet_common.h */ 45/* Instead of including gnunet_common.h */
46#define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0) 46#define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void)memcpy(dst, src, n); } } while (0)
47 47
48#define FD_SETSIZE 1024 48#define FD_SETSIZE 1024
49#include <winsock2.h> 49#include <winsock2.h>
@@ -73,9 +73,7 @@
73/** 73/**
74 * IPv4 header. 74 * IPv4 header.
75 */ 75 */
76struct ip_header 76struct ip_header {
77{
78
79 /** 77 /**
80 * Version (4 bits) + Internet header length (4 bits) 78 * Version (4 bits) + Internet header length (4 bits)
81 */ 79 */
@@ -131,8 +129,7 @@ struct ip_header
131/** 129/**
132 * Format of ICMP packet. 130 * Format of ICMP packet.
133 */ 131 */
134struct icmp_ttl_exceeded_header 132struct icmp_ttl_exceeded_header {
135{
136 uint8_t type; 133 uint8_t type;
137 134
138 uint8_t code; 135 uint8_t code;
@@ -144,8 +141,7 @@ struct icmp_ttl_exceeded_header
144 /* followed by original payload */ 141 /* followed by original payload */
145}; 142};
146 143
147struct icmp_echo_header 144struct icmp_echo_header {
148{
149 uint8_t type; 145 uint8_t type;
150 146
151 uint8_t code; 147 uint8_t code;
@@ -158,8 +154,7 @@ struct icmp_echo_header
158/** 154/**
159 * Beginning of UDP packet. 155 * Beginning of UDP packet.
160 */ 156 */
161struct udp_header 157struct udp_header {
162{
163 uint16_t src_port; 158 uint16_t src_port;
164 159
165 uint16_t dst_port; 160 uint16_t dst_port;
@@ -200,14 +195,14 @@ static uint16_t port;
200 * @return 1 on success 195 * @return 1 on success
201 */ 196 */
202static int 197static int
203inet_pton (int af, const char *cp, struct in_addr *buf) 198inet_pton(int af, const char *cp, struct in_addr *buf)
204{ 199{
205 buf->s_addr = inet_addr (cp); 200 buf->s_addr = inet_addr(cp);
206 if (buf->s_addr == INADDR_NONE) 201 if (buf->s_addr == INADDR_NONE)
207 { 202 {
208 fprintf (stderr, "Error %d handling address %s", WSAGetLastError (), cp); 203 fprintf(stderr, "Error %d handling address %s", WSAGetLastError(), cp);
209 return 0; 204 return 0;
210 } 205 }
211 return 1; 206 return 1;
212} 207}
213 208
@@ -220,7 +215,7 @@ inet_pton (int af, const char *cp, struct in_addr *buf)
220 * @return the CRC 16. 215 * @return the CRC 16.
221 */ 216 */
222static uint16_t 217static uint16_t
223calc_checksum (const uint16_t * data, unsigned int bytes) 218calc_checksum(const uint16_t * data, unsigned int bytes)
224{ 219{
225 uint32_t sum; 220 uint32_t sum;
226 unsigned int i; 221 unsigned int i;
@@ -229,7 +224,7 @@ calc_checksum (const uint16_t * data, unsigned int bytes)
229 for (i = 0; i < bytes / 2; i++) 224 for (i = 0; i < bytes / 2; i++)
230 sum += data[i]; 225 sum += data[i];
231 sum = (sum & 0xffff) + (sum >> 16); 226 sum = (sum & 0xffff) + (sum >> 16);
232 sum = htons (0xffff - sum); 227 sum = htons(0xffff - sum);
233 return sum; 228 return sum;
234} 229}
235 230
@@ -241,11 +236,11 @@ calc_checksum (const uint16_t * data, unsigned int bytes)
241 * @param other target address 236 * @param other target address
242 */ 237 */
243static void 238static void
244send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) 239send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other)
245{ 240{
246 char packet[sizeof (struct ip_header) * 2 + 241 char packet[sizeof(struct ip_header) * 2 +
247 sizeof (struct icmp_ttl_exceeded_header) + 242 sizeof(struct icmp_ttl_exceeded_header) +
248 sizeof (struct udp_header)]; 243 sizeof(struct udp_header)];
249 struct ip_header ip_pkt; 244 struct ip_header ip_pkt;
250 struct icmp_ttl_exceeded_header icmp_pkt; 245 struct icmp_ttl_exceeded_header icmp_pkt;
251 struct udp_header udp_pkt; 246 struct udp_header udp_pkt;
@@ -257,8 +252,8 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
257 off = 0; 252 off = 0;
258 ip_pkt.vers_ihl = 0x45; 253 ip_pkt.vers_ihl = 0x45;
259 ip_pkt.tos = 0; 254 ip_pkt.tos = 0;
260 ip_pkt.pkt_len = htons (sizeof (packet)); 255 ip_pkt.pkt_len = htons(sizeof(packet));
261 ip_pkt.id = htons (256); 256 ip_pkt.id = htons(256);
262 ip_pkt.flags_frag_offset = 0; 257 ip_pkt.flags_frag_offset = 0;
263 ip_pkt.ttl = 128; 258 ip_pkt.ttl = 128;
264 ip_pkt.proto = IPPROTO_ICMP; 259 ip_pkt.proto = IPPROTO_ICMP;
@@ -266,23 +261,23 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
266 ip_pkt.src_ip = my_ip->s_addr; 261 ip_pkt.src_ip = my_ip->s_addr;
267 ip_pkt.dst_ip = other->s_addr; 262 ip_pkt.dst_ip = other->s_addr;
268 ip_pkt.checksum = 263 ip_pkt.checksum =
269 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); 264 htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header)));
270 GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 265 GNUNET_memcpy(&packet[off], &ip_pkt, sizeof(struct ip_header));
271 off += sizeof (struct ip_header); 266 off += sizeof(struct ip_header);
272 267
273 icmp_pkt.type = ICMP_TIME_EXCEEDED; 268 icmp_pkt.type = ICMP_TIME_EXCEEDED;
274 icmp_pkt.code = 0; 269 icmp_pkt.code = 0;
275 icmp_pkt.checksum = 0; 270 icmp_pkt.checksum = 0;
276 icmp_pkt.unused = 0; 271 icmp_pkt.unused = 0;
277 GNUNET_memcpy (&packet[off], &icmp_pkt, sizeof (struct icmp_ttl_exceeded_header)); 272 GNUNET_memcpy(&packet[off], &icmp_pkt, sizeof(struct icmp_ttl_exceeded_header));
278 off += sizeof (struct icmp_ttl_exceeded_header); 273 off += sizeof(struct icmp_ttl_exceeded_header);
279 274
280 /* ip header of the presumably 'lost' udp packet */ 275 /* ip header of the presumably 'lost' udp packet */
281 ip_pkt.vers_ihl = 0x45; 276 ip_pkt.vers_ihl = 0x45;
282 ip_pkt.tos = 0; 277 ip_pkt.tos = 0;
283 ip_pkt.pkt_len = 278 ip_pkt.pkt_len =
284 htons (sizeof (struct ip_header) + sizeof (struct udp_header)); 279 htons(sizeof(struct ip_header) + sizeof(struct udp_header));
285 ip_pkt.id = htons (0); 280 ip_pkt.id = htons(0);
286 ip_pkt.flags_frag_offset = 0; 281 ip_pkt.flags_frag_offset = 0;
287 ip_pkt.ttl = 128; 282 ip_pkt.ttl = 128;
288 ip_pkt.proto = IPPROTO_UDP; 283 ip_pkt.proto = IPPROTO_UDP;
@@ -290,41 +285,41 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
290 ip_pkt.src_ip = other->s_addr; 285 ip_pkt.src_ip = other->s_addr;
291 ip_pkt.dst_ip = dummy.s_addr; 286 ip_pkt.dst_ip = dummy.s_addr;
292 ip_pkt.checksum = 287 ip_pkt.checksum =
293 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); 288 htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header)));
294 GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 289 GNUNET_memcpy(&packet[off], &ip_pkt, sizeof(struct ip_header));
295 off += sizeof (struct ip_header); 290 off += sizeof(struct ip_header);
296 291
297 /* build UDP header */ 292 /* build UDP header */
298 udp_pkt.src_port = htons (NAT_TRAV_PORT); 293 udp_pkt.src_port = htons(NAT_TRAV_PORT);
299 udp_pkt.dst_port = htons (NAT_TRAV_PORT); 294 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
300 udp_pkt.length = htons (port); 295 udp_pkt.length = htons(port);
301 udp_pkt.crc = 0; 296 udp_pkt.crc = 0;
302 GNUNET_memcpy (&packet[off], &udp_pkt, sizeof (struct udp_header)); 297 GNUNET_memcpy(&packet[off], &udp_pkt, sizeof(struct udp_header));
303 off += sizeof (struct udp_header); 298 off += sizeof(struct udp_header);
304 299
305 /* no go back to calculate ICMP packet checksum */ 300 /* no go back to calculate ICMP packet checksum */
306 icmp_pkt.checksum = 301 icmp_pkt.checksum =
307 htons (calc_checksum 302 htons(calc_checksum
308 ((uint16_t *) & packet[off], 303 ((uint16_t *)&packet[off],
309 sizeof (struct icmp_ttl_exceeded_header) + 304 sizeof(struct icmp_ttl_exceeded_header) +
310 sizeof (struct ip_header) + sizeof (struct udp_header))); 305 sizeof(struct ip_header) + sizeof(struct udp_header)));
311 GNUNET_memcpy (&packet[sizeof (struct ip_header)], &icmp_pkt, 306 GNUNET_memcpy(&packet[sizeof(struct ip_header)], &icmp_pkt,
312 sizeof (struct icmp_ttl_exceeded_header)); 307 sizeof(struct icmp_ttl_exceeded_header));
313 308
314 memset (&dst, 0, sizeof (dst)); 309 memset(&dst, 0, sizeof(dst));
315 dst.sin_family = AF_INET; 310 dst.sin_family = AF_INET;
316 dst.sin_addr = *other; 311 dst.sin_addr = *other;
317 err = 312 err =
318 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst, 313 sendto(rawsock, packet, sizeof(packet), 0, (struct sockaddr *)&dst,
319 sizeof (dst)); 314 sizeof(dst));
320 if (err < 0) 315 if (err < 0)
321 { 316 {
322 fprintf (stderr, "sendto failed: %s\n", strerror (errno)); 317 fprintf(stderr, "sendto failed: %s\n", strerror(errno));
323 } 318 }
324 else if (sizeof (packet) != (size_t) err) 319 else if (sizeof(packet) != (size_t)err)
325 { 320 {
326 fprintf (stderr, "Error: partial send of ICMP message\n"); 321 fprintf(stderr, "Error: partial send of ICMP message\n");
327 } 322 }
328} 323}
329 324
330 325
@@ -335,15 +330,15 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
335 * @param other target address 330 * @param other target address
336 */ 331 */
337static void 332static void
338send_icmp (const struct in_addr *my_ip, const struct in_addr *other) 333send_icmp(const struct in_addr *my_ip, const struct in_addr *other)
339{ 334{
340 struct ip_header ip_pkt; 335 struct ip_header ip_pkt;
341 struct icmp_ttl_exceeded_header icmp_ttl; 336 struct icmp_ttl_exceeded_header icmp_ttl;
342 struct icmp_echo_header icmp_echo; 337 struct icmp_echo_header icmp_echo;
343 struct sockaddr_in dst; 338 struct sockaddr_in dst;
344 char packet[sizeof (struct ip_header) * 2 + 339 char packet[sizeof(struct ip_header) * 2 +
345 sizeof (struct icmp_ttl_exceeded_header) + 340 sizeof(struct icmp_ttl_exceeded_header) +
346 sizeof (struct icmp_echo_header)]; 341 sizeof(struct icmp_echo_header)];
347 size_t off; 342 size_t off;
348 int err; 343 int err;
349 344
@@ -351,8 +346,8 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
351 off = 0; 346 off = 0;
352 ip_pkt.vers_ihl = 0x45; 347 ip_pkt.vers_ihl = 0x45;
353 ip_pkt.tos = 0; 348 ip_pkt.tos = 0;
354 ip_pkt.pkt_len = htons (sizeof (packet)); 349 ip_pkt.pkt_len = htons(sizeof(packet));
355 ip_pkt.id = htons (256); 350 ip_pkt.id = htons(256);
356 ip_pkt.flags_frag_offset = 0; 351 ip_pkt.flags_frag_offset = 0;
357 ip_pkt.ttl = IPDEFTTL; 352 ip_pkt.ttl = IPDEFTTL;
358 ip_pkt.proto = IPPROTO_ICMP; 353 ip_pkt.proto = IPPROTO_ICMP;
@@ -360,24 +355,24 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
360 ip_pkt.src_ip = my_ip->s_addr; 355 ip_pkt.src_ip = my_ip->s_addr;
361 ip_pkt.dst_ip = other->s_addr; 356 ip_pkt.dst_ip = other->s_addr;
362 ip_pkt.checksum = 357 ip_pkt.checksum =
363 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); 358 htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header)));
364 GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 359 GNUNET_memcpy(&packet[off], &ip_pkt, sizeof(struct ip_header));
365 off += sizeof (ip_pkt); 360 off += sizeof(ip_pkt);
366 361
367 /* icmp reply: time exceeded */ 362 /* icmp reply: time exceeded */
368 icmp_ttl.type = ICMP_TIME_EXCEEDED; 363 icmp_ttl.type = ICMP_TIME_EXCEEDED;
369 icmp_ttl.code = 0; 364 icmp_ttl.code = 0;
370 icmp_ttl.checksum = 0; 365 icmp_ttl.checksum = 0;
371 icmp_ttl.unused = 0; 366 icmp_ttl.unused = 0;
372 GNUNET_memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header)); 367 GNUNET_memcpy(&packet[off], &icmp_ttl, sizeof(struct icmp_ttl_exceeded_header));
373 off += sizeof (struct icmp_ttl_exceeded_header); 368 off += sizeof(struct icmp_ttl_exceeded_header);
374 369
375 /* ip header of the presumably 'lost' udp packet */ 370 /* ip header of the presumably 'lost' udp packet */
376 ip_pkt.vers_ihl = 0x45; 371 ip_pkt.vers_ihl = 0x45;
377 ip_pkt.tos = 0; 372 ip_pkt.tos = 0;
378 ip_pkt.pkt_len = 373 ip_pkt.pkt_len =
379 htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header)); 374 htons(sizeof(struct ip_header) + sizeof(struct icmp_echo_header));
380 ip_pkt.id = htons (256); 375 ip_pkt.id = htons(256);
381 ip_pkt.flags_frag_offset = 0; 376 ip_pkt.flags_frag_offset = 0;
382 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ 377 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
383 ip_pkt.proto = IPPROTO_ICMP; 378 ip_pkt.proto = IPPROTO_ICMP;
@@ -385,44 +380,44 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
385 ip_pkt.dst_ip = dummy.s_addr; 380 ip_pkt.dst_ip = dummy.s_addr;
386 ip_pkt.checksum = 0; 381 ip_pkt.checksum = 0;
387 ip_pkt.checksum = 382 ip_pkt.checksum =
388 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); 383 htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header)));
389 GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 384 GNUNET_memcpy(&packet[off], &ip_pkt, sizeof(struct ip_header));
390 off += sizeof (struct ip_header); 385 off += sizeof(struct ip_header);
391 386
392 icmp_echo.type = ICMP_ECHO; 387 icmp_echo.type = ICMP_ECHO;
393 icmp_echo.code = 0; 388 icmp_echo.code = 0;
394 icmp_echo.reserved = htonl (port); 389 icmp_echo.reserved = htonl(port);
395 icmp_echo.checksum = 0; 390 icmp_echo.checksum = 0;
396 icmp_echo.checksum = 391 icmp_echo.checksum =
397 htons (calc_checksum 392 htons(calc_checksum
398 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header))); 393 ((uint16_t *)&icmp_echo, sizeof(struct icmp_echo_header)));
399 GNUNET_memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header)); 394 GNUNET_memcpy(&packet[off], &icmp_echo, sizeof(struct icmp_echo_header));
400 395
401 /* no go back to calculate ICMP packet checksum */ 396 /* no go back to calculate ICMP packet checksum */
402 off = sizeof (struct ip_header); 397 off = sizeof(struct ip_header);
403 icmp_ttl.checksum = 398 icmp_ttl.checksum =
404 htons (calc_checksum 399 htons(calc_checksum
405 ((uint16_t *) & packet[off], 400 ((uint16_t *)&packet[off],
406 sizeof (struct icmp_ttl_exceeded_header) + 401 sizeof(struct icmp_ttl_exceeded_header) +
407 sizeof (struct ip_header) + sizeof (struct icmp_echo_header))); 402 sizeof(struct ip_header) + sizeof(struct icmp_echo_header)));
408 GNUNET_memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header)); 403 GNUNET_memcpy(&packet[off], &icmp_ttl, sizeof(struct icmp_ttl_exceeded_header));
409 404
410 memset (&dst, 0, sizeof (dst)); 405 memset(&dst, 0, sizeof(dst));
411 dst.sin_family = AF_INET; 406 dst.sin_family = AF_INET;
412 dst.sin_addr = *other; 407 dst.sin_addr = *other;
413 408
414 err = 409 err =
415 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst, 410 sendto(rawsock, packet, sizeof(packet), 0, (struct sockaddr *)&dst,
416 sizeof (dst)); 411 sizeof(dst));
417 412
418 if (err < 0) 413 if (err < 0)
419 { 414 {
420 fprintf (stderr, "sendto failed: %s\n", strerror (errno)); 415 fprintf(stderr, "sendto failed: %s\n", strerror(errno));
421 } 416 }
422 else if (sizeof (packet) != (size_t) err) 417 else if (sizeof(packet) != (size_t)err)
423 { 418 {
424 fprintf (stderr, "Error: partial send of ICMP message\n"); 419 fprintf(stderr, "Error: partial send of ICMP message\n");
425 } 420 }
426} 421}
427 422
428 423
@@ -432,100 +427,102 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
432 * @return INVALID_SOCKET on error 427 * @return INVALID_SOCKET on error
433 */ 428 */
434static SOCKET 429static SOCKET
435make_raw_socket () 430make_raw_socket()
436{ 431{
437 DWORD bOptVal = TRUE; 432 DWORD bOptVal = TRUE;
438 int bOptLen = sizeof (bOptVal); 433 int bOptLen = sizeof(bOptVal);
439 SOCKET ret; 434 SOCKET ret;
440 435
441 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW); 436 ret = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
442 if (INVALID_SOCKET == ret) 437 if (INVALID_SOCKET == ret)
443 { 438 {
444 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno)); 439 fprintf(stderr, "Error opening RAW socket: %s\n", strerror(errno));
445 return INVALID_SOCKET; 440 return INVALID_SOCKET;
446 } 441 }
447 if (0 != 442 if (0 !=
448 setsockopt (ret, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen)) 443 setsockopt(ret, SOL_SOCKET, SO_BROADCAST, (char *)&bOptVal, bOptLen))
449 { 444 {
450 fprintf (stderr, "Error setting SO_BROADCAST to ON: %s\n", 445 fprintf(stderr, "Error setting SO_BROADCAST to ON: %s\n",
451 strerror (errno)); 446 strerror(errno));
452 closesocket (rawsock); 447 closesocket(rawsock);
453 return INVALID_SOCKET; 448 return INVALID_SOCKET;
454 } 449 }
455 450
456 if (0 != setsockopt (ret, IPPROTO_IP, IP_HDRINCL, (char *) &bOptVal, bOptLen)) 451 if (0 != setsockopt(ret, IPPROTO_IP, IP_HDRINCL, (char *)&bOptVal, bOptLen))
457 { 452 {
458 fprintf (stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror (errno)); 453 fprintf(stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror(errno));
459 closesocket (rawsock); 454 closesocket(rawsock);
460 return INVALID_SOCKET; 455 return INVALID_SOCKET;
461 } 456 }
462 return ret; 457 return ret;
463} 458}
464 459
465 460
466int 461int
467main (int argc, char *const *argv) 462main(int argc, char *const *argv)
468{ 463{
469 struct in_addr external; 464 struct in_addr external;
470 struct in_addr target; 465 struct in_addr target;
471 WSADATA wsaData; 466 WSADATA wsaData;
472 unsigned int p; 467 unsigned int p;
473 468
474 if (argc > 1 && 0 != strcmp (argv[1], "-d")){ 469 if (argc > 1 && 0 != strcmp(argv[1], "-d"))
470 {
475 privilege_testing = TRUE; 471 privilege_testing = TRUE;
476 fprintf (stderr, 472 fprintf(stderr,
477 "%s", 473 "%s",
478 "DEBUG: Running binary in privilege testing mode."); 474 "DEBUG: Running binary in privilege testing mode.");
479 argv++; 475 argv++;
480 argc--; 476 argc--;
481 } 477 }
482 478
483 if (argc != 4) 479 if (argc != 4)
484 { 480 {
485 fprintf (stderr, 481 fprintf(stderr,
486 "%s", 482 "%s",
487 "This program must be started with our IP, the targets external IP, and our port as arguments.\n"); 483 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
488 return 1; 484 return 1;
489 } 485 }
490 if ((1 != inet_pton (AF_INET, argv[1], &external)) || 486 if ((1 != inet_pton(AF_INET, argv[1], &external)) ||
491 (1 != inet_pton (AF_INET, argv[2], &target))) 487 (1 != inet_pton(AF_INET, argv[2], &target)))
492 { 488 {
493 fprintf (stderr, 489 fprintf(stderr,
494 "Error parsing IPv4 address: %s\n", 490 "Error parsing IPv4 address: %s\n",
495 strerror (errno)); 491 strerror(errno));
496 return 1; 492 return 1;
497 } 493 }
498 if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p)) 494 if ((1 != sscanf(argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p))
499 { 495 {
500 fprintf (stderr, 496 fprintf(stderr,
501 "Error parsing port value `%s'\n", 497 "Error parsing port value `%s'\n",
502 argv[3]); 498 argv[3]);
503 return 1; 499 return 1;
504 } 500 }
505 port = (uint16_t) p; 501 port = (uint16_t)p;
506 502
507 if (0 != WSAStartup (MAKEWORD (2, 1), &wsaData)) 503 if (0 != WSAStartup(MAKEWORD(2, 1), &wsaData))
508 { 504 {
509 fprintf (stderr, 505 fprintf(stderr,
510 "%s", 506 "%s",
511 "Failed to find Winsock 2.1 or better.\n"); 507 "Failed to find Winsock 2.1 or better.\n");
512 return 2; 508 return 2;
513 } 509 }
514 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) 510 if (1 != inet_pton(AF_INET, DUMMY_IP, &dummy))
515 { 511 {
516 fprintf (stderr, 512 fprintf(stderr,
517 "%s", 513 "%s",
518 "Internal error converting dummy IP to binary.\n"); 514 "Internal error converting dummy IP to binary.\n");
519 return 2; 515 return 2;
520 } 516 }
521 if (-1 == (rawsock = make_raw_socket ())) 517 if (-1 == (rawsock = make_raw_socket()))
522 return 3; 518 return 3;
523 if (!privilege_testing){ 519 if (!privilege_testing)
524 send_icmp (&external, &target); 520 {
525 send_icmp_udp (&external, &target); 521 send_icmp(&external, &target);
526 } 522 send_icmp_udp(&external, &target);
527 closesocket (rawsock); 523 }
528 WSACleanup (); 524 closesocket(rawsock);
525 WSACleanup();
529 return 0; 526 return 0;
530} 527}
531 528
diff --git a/src/nat/gnunet-helper-nat-client.c b/src/nat/gnunet-helper-nat-client.c
index 54a993452..d5d774964 100644
--- a/src/nat/gnunet-helper-nat-client.c
+++ b/src/nat/gnunet-helper-nat-client.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/** 21/**
22 * @file src/nat/gnunet-helper-nat-client.c 22 * @file src/nat/gnunet-helper-nat-client.c
@@ -75,7 +75,7 @@
75 * @param src source of the copy, may be NULL if @a n is zero 75 * @param src source of the copy, may be NULL if @a n is zero
76 * @param n number of bytes to copy 76 * @param n number of bytes to copy
77 */ 77 */
78#define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0) 78#define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void)memcpy(dst, src, n); } } while (0)
79 79
80/** 80/**
81 * Must match IP given in the server. 81 * Must match IP given in the server.
@@ -92,9 +92,7 @@
92/** 92/**
93 * IPv4 header. 93 * IPv4 header.
94 */ 94 */
95struct ip_header 95struct ip_header {
96{
97
98 /** 96 /**
99 * Version (4 bits) + Internet header length (4 bits) 97 * Version (4 bits) + Internet header length (4 bits)
100 */ 98 */
@@ -149,8 +147,7 @@ struct ip_header
149/** 147/**
150 * Format of ICMP packet. 148 * Format of ICMP packet.
151 */ 149 */
152struct icmp_ttl_exceeded_header 150struct icmp_ttl_exceeded_header {
153{
154 uint8_t type; 151 uint8_t type;
155 152
156 uint8_t code; 153 uint8_t code;
@@ -162,8 +159,7 @@ struct icmp_ttl_exceeded_header
162 /* followed by original payload */ 159 /* followed by original payload */
163}; 160};
164 161
165struct icmp_echo_header 162struct icmp_echo_header {
166{
167 uint8_t type; 163 uint8_t type;
168 164
169 uint8_t code; 165 uint8_t code;
@@ -176,8 +172,7 @@ struct icmp_echo_header
176/** 172/**
177 * Beginning of UDP packet. 173 * Beginning of UDP packet.
178 */ 174 */
179struct udp_header 175struct udp_header {
180{
181 uint16_t src_port; 176 uint16_t src_port;
182 177
183 uint16_t dst_port; 178 uint16_t dst_port;
@@ -211,7 +206,7 @@ static uint16_t port;
211 * @return the CRC 16. 206 * @return the CRC 16.
212 */ 207 */
213static uint16_t 208static uint16_t
214calc_checksum (const uint16_t * data, unsigned int bytes) 209calc_checksum(const uint16_t * data, unsigned int bytes)
215{ 210{
216 uint32_t sum; 211 uint32_t sum;
217 unsigned int i; 212 unsigned int i;
@@ -220,7 +215,7 @@ calc_checksum (const uint16_t * data, unsigned int bytes)
220 for (i = 0; i < bytes / 2; i++) 215 for (i = 0; i < bytes / 2; i++)
221 sum += data[i]; 216 sum += data[i];
222 sum = (sum & 0xffff) + (sum >> 16); 217 sum = (sum & 0xffff) + (sum >> 16);
223 sum = htons (0xffff - sum); 218 sum = htons(0xffff - sum);
224 return sum; 219 return sum;
225} 220}
226 221
@@ -232,11 +227,11 @@ calc_checksum (const uint16_t * data, unsigned int bytes)
232 * @param other target address 227 * @param other target address
233 */ 228 */
234static void 229static void
235send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) 230send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other)
236{ 231{
237 char packet[sizeof (struct ip_header) * 2 + 232 char packet[sizeof(struct ip_header) * 2 +
238 sizeof (struct icmp_ttl_exceeded_header) + 233 sizeof(struct icmp_ttl_exceeded_header) +
239 sizeof (struct udp_header)]; 234 sizeof(struct udp_header)];
240 struct ip_header ip_pkt; 235 struct ip_header ip_pkt;
241 struct icmp_ttl_exceeded_header icmp_pkt; 236 struct icmp_ttl_exceeded_header icmp_pkt;
242 struct udp_header udp_pkt; 237 struct udp_header udp_pkt;
@@ -249,11 +244,11 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
249 ip_pkt.vers_ihl = 0x45; 244 ip_pkt.vers_ihl = 0x45;
250 ip_pkt.tos = 0; 245 ip_pkt.tos = 0;
251#ifdef FREEBSD 246#ifdef FREEBSD
252 ip_pkt.pkt_len = sizeof (packet); /* Workaround PR kern/21737 */ 247 ip_pkt.pkt_len = sizeof(packet); /* Workaround PR kern/21737 */
253#else 248#else
254 ip_pkt.pkt_len = htons (sizeof (packet)); 249 ip_pkt.pkt_len = htons(sizeof(packet));
255#endif 250#endif
256 ip_pkt.id = htons (PACKET_ID); 251 ip_pkt.id = htons(PACKET_ID);
257 ip_pkt.flags_frag_offset = 0; 252 ip_pkt.flags_frag_offset = 0;
258 ip_pkt.ttl = 128; 253 ip_pkt.ttl = 128;
259 ip_pkt.proto = IPPROTO_ICMP; 254 ip_pkt.proto = IPPROTO_ICMP;
@@ -261,27 +256,27 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
261 ip_pkt.src_ip = my_ip->s_addr; 256 ip_pkt.src_ip = my_ip->s_addr;
262 ip_pkt.dst_ip = other->s_addr; 257 ip_pkt.dst_ip = other->s_addr;
263 ip_pkt.checksum = 258 ip_pkt.checksum =
264 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); 259 htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header)));
265 GNUNET_memcpy (&packet[off], 260 GNUNET_memcpy(&packet[off],
266 &ip_pkt, 261 &ip_pkt,
267 sizeof (struct ip_header)); 262 sizeof(struct ip_header));
268 off += sizeof (struct ip_header); 263 off += sizeof(struct ip_header);
269 264
270 icmp_pkt.type = ICMP_TIME_EXCEEDED; 265 icmp_pkt.type = ICMP_TIME_EXCEEDED;
271 icmp_pkt.code = 0; 266 icmp_pkt.code = 0;
272 icmp_pkt.checksum = 0; 267 icmp_pkt.checksum = 0;
273 icmp_pkt.unused = 0; 268 icmp_pkt.unused = 0;
274 GNUNET_memcpy (&packet[off], 269 GNUNET_memcpy(&packet[off],
275 &icmp_pkt, 270 &icmp_pkt,
276 sizeof (struct icmp_ttl_exceeded_header)); 271 sizeof(struct icmp_ttl_exceeded_header));
277 off += sizeof (struct icmp_ttl_exceeded_header); 272 off += sizeof(struct icmp_ttl_exceeded_header);
278 273
279 /* ip header of the presumably 'lost' udp packet */ 274 /* ip header of the presumably 'lost' udp packet */
280 ip_pkt.vers_ihl = 0x45; 275 ip_pkt.vers_ihl = 0x45;
281 ip_pkt.tos = 0; 276 ip_pkt.tos = 0;
282 ip_pkt.pkt_len = 277 ip_pkt.pkt_len =
283 htons (sizeof (struct ip_header) + sizeof (struct udp_header)); 278 htons(sizeof(struct ip_header) + sizeof(struct udp_header));
284 ip_pkt.id = htons (0); 279 ip_pkt.id = htons(0);
285 ip_pkt.flags_frag_offset = 0; 280 ip_pkt.flags_frag_offset = 0;
286 ip_pkt.ttl = 128; 281 ip_pkt.ttl = 128;
287 ip_pkt.proto = IPPROTO_UDP; 282 ip_pkt.proto = IPPROTO_UDP;
@@ -289,49 +284,49 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
289 ip_pkt.src_ip = other->s_addr; 284 ip_pkt.src_ip = other->s_addr;
290 ip_pkt.dst_ip = dummy.s_addr; 285 ip_pkt.dst_ip = dummy.s_addr;
291 ip_pkt.checksum = 286 ip_pkt.checksum =
292 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); 287 htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header)));
293 GNUNET_memcpy (&packet[off], 288 GNUNET_memcpy(&packet[off],
294 &ip_pkt, 289 &ip_pkt,
295 sizeof (struct ip_header)); 290 sizeof(struct ip_header));
296 off += sizeof (struct ip_header); 291 off += sizeof(struct ip_header);
297 292
298 /* build UDP header */ 293 /* build UDP header */
299 udp_pkt.src_port = htons (NAT_TRAV_PORT); 294 udp_pkt.src_port = htons(NAT_TRAV_PORT);
300 udp_pkt.dst_port = htons (NAT_TRAV_PORT); 295 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
301 udp_pkt.length = htons (port); 296 udp_pkt.length = htons(port);
302 udp_pkt.crc = 0; 297 udp_pkt.crc = 0;
303 GNUNET_memcpy (&packet[off], 298 GNUNET_memcpy(&packet[off],
304 &udp_pkt, 299 &udp_pkt,
305 sizeof (struct udp_header)); 300 sizeof(struct udp_header));
306 off += sizeof (struct udp_header); 301 off += sizeof(struct udp_header);
307 302
308 /* set ICMP checksum */ 303 /* set ICMP checksum */
309 icmp_pkt.checksum = 304 icmp_pkt.checksum =
310 htons (calc_checksum 305 htons(calc_checksum
311 ((uint16_t *) & packet[sizeof (struct ip_header)], 306 ((uint16_t *)&packet[sizeof(struct ip_header)],
312 sizeof (struct icmp_ttl_exceeded_header) + 307 sizeof(struct icmp_ttl_exceeded_header) +
313 sizeof (struct ip_header) + sizeof (struct udp_header))); 308 sizeof(struct ip_header) + sizeof(struct udp_header)));
314 GNUNET_memcpy (&packet[sizeof (struct ip_header)], 309 GNUNET_memcpy(&packet[sizeof(struct ip_header)],
315 &icmp_pkt, 310 &icmp_pkt,
316 sizeof (struct icmp_ttl_exceeded_header)); 311 sizeof(struct icmp_ttl_exceeded_header));
317 312
318 memset (&dst, 0, sizeof (dst)); 313 memset(&dst, 0, sizeof(dst));
319 dst.sin_family = AF_INET; 314 dst.sin_family = AF_INET;
320#if HAVE_SOCKADDR_IN_SIN_LEN 315#if HAVE_SOCKADDR_IN_SIN_LEN
321 dst.sin_len = sizeof (struct sockaddr_in); 316 dst.sin_len = sizeof(struct sockaddr_in);
322#endif 317#endif
323 dst.sin_addr = *other; 318 dst.sin_addr = *other;
324 err = 319 err =
325 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst, 320 sendto(rawsock, packet, sizeof(packet), 0, (struct sockaddr *)&dst,
326 sizeof (dst)); 321 sizeof(dst));
327 if (err < 0) 322 if (err < 0)
328 { 323 {
329 fprintf (stderr, "sendto failed: %s\n", strerror (errno)); 324 fprintf(stderr, "sendto failed: %s\n", strerror(errno));
330 } 325 }
331 else if (sizeof (packet) != (size_t) err) 326 else if (sizeof(packet) != (size_t)err)
332 { 327 {
333 fprintf (stderr, "Error: partial send of ICMP message with size %lu\n", (unsigned long) off); 328 fprintf(stderr, "Error: partial send of ICMP message with size %lu\n", (unsigned long)off);
334 } 329 }
335} 330}
336 331
337 332
@@ -342,15 +337,15 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
342 * @param other target address 337 * @param other target address
343 */ 338 */
344static void 339static void
345send_icmp (const struct in_addr *my_ip, const struct in_addr *other) 340send_icmp(const struct in_addr *my_ip, const struct in_addr *other)
346{ 341{
347 struct ip_header ip_pkt; 342 struct ip_header ip_pkt;
348 struct icmp_ttl_exceeded_header icmp_ttl; 343 struct icmp_ttl_exceeded_header icmp_ttl;
349 struct icmp_echo_header icmp_echo; 344 struct icmp_echo_header icmp_echo;
350 struct sockaddr_in dst; 345 struct sockaddr_in dst;
351 char packet[sizeof (struct ip_header) * 2 + 346 char packet[sizeof(struct ip_header) * 2 +
352 sizeof (struct icmp_ttl_exceeded_header) + 347 sizeof(struct icmp_ttl_exceeded_header) +
353 sizeof (struct icmp_echo_header)]; 348 sizeof(struct icmp_echo_header)];
354 size_t off; 349 size_t off;
355 int err; 350 int err;
356 351
@@ -359,11 +354,11 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
359 ip_pkt.vers_ihl = 0x45; 354 ip_pkt.vers_ihl = 0x45;
360 ip_pkt.tos = 0; 355 ip_pkt.tos = 0;
361#ifdef FREEBSD 356#ifdef FREEBSD
362 ip_pkt.pkt_len = sizeof (packet); /* Workaround PR kern/21737 */ 357 ip_pkt.pkt_len = sizeof(packet); /* Workaround PR kern/21737 */
363#else 358#else
364 ip_pkt.pkt_len = htons (sizeof (packet)); 359 ip_pkt.pkt_len = htons(sizeof(packet));
365#endif 360#endif
366 ip_pkt.id = htons (PACKET_ID); 361 ip_pkt.id = htons(PACKET_ID);
367 ip_pkt.flags_frag_offset = 0; 362 ip_pkt.flags_frag_offset = 0;
368 ip_pkt.ttl = IPDEFTTL; 363 ip_pkt.ttl = IPDEFTTL;
369 ip_pkt.proto = IPPROTO_ICMP; 364 ip_pkt.proto = IPPROTO_ICMP;
@@ -371,28 +366,28 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
371 ip_pkt.src_ip = my_ip->s_addr; 366 ip_pkt.src_ip = my_ip->s_addr;
372 ip_pkt.dst_ip = other->s_addr; 367 ip_pkt.dst_ip = other->s_addr;
373 ip_pkt.checksum = 368 ip_pkt.checksum =
374 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); 369 htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header)));
375 GNUNET_memcpy (&packet[off], 370 GNUNET_memcpy(&packet[off],
376 &ip_pkt, 371 &ip_pkt,
377 sizeof (struct ip_header)); 372 sizeof(struct ip_header));
378 off = sizeof (ip_pkt); 373 off = sizeof(ip_pkt);
379 374
380 /* icmp reply: time exceeded */ 375 /* icmp reply: time exceeded */
381 icmp_ttl.type = ICMP_TIME_EXCEEDED; 376 icmp_ttl.type = ICMP_TIME_EXCEEDED;
382 icmp_ttl.code = 0; 377 icmp_ttl.code = 0;
383 icmp_ttl.checksum = 0; 378 icmp_ttl.checksum = 0;
384 icmp_ttl.unused = 0; 379 icmp_ttl.unused = 0;
385 GNUNET_memcpy (&packet[off], 380 GNUNET_memcpy(&packet[off],
386 &icmp_ttl, 381 &icmp_ttl,
387 sizeof (struct icmp_ttl_exceeded_header)); 382 sizeof(struct icmp_ttl_exceeded_header));
388 off += sizeof (struct icmp_ttl_exceeded_header); 383 off += sizeof(struct icmp_ttl_exceeded_header);
389 384
390 /* ip header of the presumably 'lost' udp packet */ 385 /* ip header of the presumably 'lost' udp packet */
391 ip_pkt.vers_ihl = 0x45; 386 ip_pkt.vers_ihl = 0x45;
392 ip_pkt.tos = 0; 387 ip_pkt.tos = 0;
393 ip_pkt.pkt_len = 388 ip_pkt.pkt_len =
394 htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header)); 389 htons(sizeof(struct ip_header) + sizeof(struct icmp_echo_header));
395 ip_pkt.id = htons (PACKET_ID); 390 ip_pkt.id = htons(PACKET_ID);
396 ip_pkt.flags_frag_offset = 0; 391 ip_pkt.flags_frag_offset = 0;
397 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ 392 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
398 ip_pkt.proto = IPPROTO_ICMP; 393 ip_pkt.proto = IPPROTO_ICMP;
@@ -400,57 +395,57 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
400 ip_pkt.dst_ip = dummy.s_addr; 395 ip_pkt.dst_ip = dummy.s_addr;
401 ip_pkt.checksum = 0; 396 ip_pkt.checksum = 0;
402 ip_pkt.checksum = 397 ip_pkt.checksum =
403 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); 398 htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header)));
404 GNUNET_memcpy (&packet[off], 399 GNUNET_memcpy(&packet[off],
405 &ip_pkt, 400 &ip_pkt,
406 sizeof (struct ip_header)); 401 sizeof(struct ip_header));
407 off += sizeof (struct ip_header); 402 off += sizeof(struct ip_header);
408 403
409 icmp_echo.type = ICMP_ECHO; 404 icmp_echo.type = ICMP_ECHO;
410 icmp_echo.code = 0; 405 icmp_echo.code = 0;
411 icmp_echo.reserved = htonl (port); 406 icmp_echo.reserved = htonl(port);
412 icmp_echo.checksum = 0; 407 icmp_echo.checksum = 0;
413 icmp_echo.checksum = 408 icmp_echo.checksum =
414 htons (calc_checksum 409 htons(calc_checksum
415 ((uint16_t *) &icmp_echo, sizeof (struct icmp_echo_header))); 410 ((uint16_t *)&icmp_echo, sizeof(struct icmp_echo_header)));
416 GNUNET_memcpy (&packet[off], 411 GNUNET_memcpy(&packet[off],
417 &icmp_echo, 412 &icmp_echo,
418 sizeof (struct icmp_echo_header)); 413 sizeof(struct icmp_echo_header));
419 414
420 /* no go back to calculate ICMP packet checksum */ 415 /* no go back to calculate ICMP packet checksum */
421 off = sizeof (struct ip_header); 416 off = sizeof(struct ip_header);
422 icmp_ttl.checksum = 417 icmp_ttl.checksum =
423 htons (calc_checksum 418 htons(calc_checksum
424 ((uint16_t *) & packet[off], 419 ((uint16_t *)&packet[off],
425 sizeof (struct icmp_ttl_exceeded_header) + 420 sizeof(struct icmp_ttl_exceeded_header) +
426 sizeof (struct ip_header) + sizeof (struct icmp_echo_header))); 421 sizeof(struct ip_header) + sizeof(struct icmp_echo_header)));
427 GNUNET_memcpy (&packet[off], 422 GNUNET_memcpy(&packet[off],
428 &icmp_ttl, 423 &icmp_ttl,
429 sizeof (struct icmp_ttl_exceeded_header)); 424 sizeof(struct icmp_ttl_exceeded_header));
430 425
431 /* prepare for transmission */ 426 /* prepare for transmission */
432 memset (&dst, 0, sizeof (dst)); 427 memset(&dst, 0, sizeof(dst));
433 dst.sin_family = AF_INET; 428 dst.sin_family = AF_INET;
434#if HAVE_SOCKADDR_IN_SIN_LEN 429#if HAVE_SOCKADDR_IN_SIN_LEN
435 dst.sin_len = sizeof (struct sockaddr_in); 430 dst.sin_len = sizeof(struct sockaddr_in);
436#endif 431#endif
437 dst.sin_addr = *other; 432 dst.sin_addr = *other;
438 err = 433 err =
439 sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst, 434 sendto(rawsock, packet, sizeof(packet), 0, (struct sockaddr *)&dst,
440 sizeof (dst)); 435 sizeof(dst));
441 if (err < 0) 436 if (err < 0)
442 { 437 {
443 fprintf (stderr, "sendto failed: %s\n", strerror (errno)); 438 fprintf(stderr, "sendto failed: %s\n", strerror(errno));
444 } 439 }
445 else if (sizeof (packet) != (size_t) err) 440 else if (sizeof(packet) != (size_t)err)
446 { 441 {
447 fprintf (stderr, "Error: partial send of ICMP message\n"); 442 fprintf(stderr, "Error: partial send of ICMP message\n");
448 } 443 }
449} 444}
450 445
451 446
452int 447int
453main (int argc, char *const *argv) 448main(int argc, char *const *argv)
454{ 449{
455 const int one = 1; 450 const int one = 1;
456 struct in_addr external; 451 struct in_addr external;
@@ -461,80 +456,80 @@ main (int argc, char *const *argv)
461 int global_ret; 456 int global_ret;
462 457
463 /* Create an ICMP raw socket for writing (only operation that requires root) */ 458 /* Create an ICMP raw socket for writing (only operation that requires root) */
464 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW); 459 rawsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
465 raw_eno = errno; /* for later error checking */ 460 raw_eno = errno; /* for later error checking */
466 461
467 /* now drop root privileges */ 462 /* now drop root privileges */
468 uid = getuid (); 463 uid = getuid();
469#ifdef HAVE_SETRESUID 464#ifdef HAVE_SETRESUID
470 if (0 != setresuid (uid, uid, uid)) 465 if (0 != setresuid(uid, uid, uid))
471 { 466 {
472 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno)); 467 fprintf(stderr, "Failed to setresuid: %s\n", strerror(errno));
473 global_ret = 1; 468 global_ret = 1;
474 goto cleanup; 469 goto cleanup;
475 } 470 }
476#else 471#else
477 if (0 != (setuid (uid) | seteuid (uid))) 472 if (0 != (setuid(uid) | seteuid(uid)))
478 { 473 {
479 fprintf (stderr, "Failed to setuid: %s\n", strerror (errno)); 474 fprintf(stderr, "Failed to setuid: %s\n", strerror(errno));
480 global_ret = 2; 475 global_ret = 2;
481 goto cleanup; 476 goto cleanup;
482 } 477 }
483#endif 478#endif
484 if (-1 == rawsock) 479 if (-1 == rawsock)
485 { 480 {
486 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (raw_eno)); 481 fprintf(stderr, "Error opening RAW socket: %s\n", strerror(raw_eno));
487 global_ret = 3; 482 global_ret = 3;
488 goto cleanup; 483 goto cleanup;
489 } 484 }
490 if (0 != 485 if (0 !=
491 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one))) 486 setsockopt(rawsock, SOL_SOCKET, SO_BROADCAST, (char *)&one, sizeof(one)))
492 { 487 {
493 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno)); 488 fprintf(stderr, "setsockopt failed: %s\n", strerror(errno));
494 global_ret = 4; 489 global_ret = 4;
495 goto cleanup; 490 goto cleanup;
496 } 491 }
497 if (0 != 492 if (0 !=
498 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one))) 493 setsockopt(rawsock, IPPROTO_IP, IP_HDRINCL, (char *)&one, sizeof(one)))
499 { 494 {
500 fprintf (stderr, "setsockopt failed: %s\n", strerror (errno)); 495 fprintf(stderr, "setsockopt failed: %s\n", strerror(errno));
501 global_ret = 5; 496 global_ret = 5;
502 goto cleanup; 497 goto cleanup;
503 } 498 }
504 499
505 if (4 != argc) 500 if (4 != argc)
506 { 501 {
507 fprintf (stderr, 502 fprintf(stderr,
508 "This program must be started with our IP, the targets external IP, and our port as arguments.\n"); 503 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
509 global_ret = 6; 504 global_ret = 6;
510 goto cleanup; 505 goto cleanup;
511 } 506 }
512 if ((1 != inet_pton (AF_INET, argv[1], &external)) || 507 if ((1 != inet_pton(AF_INET, argv[1], &external)) ||
513 (1 != inet_pton (AF_INET, argv[2], &target))) 508 (1 != inet_pton(AF_INET, argv[2], &target)))
514 { 509 {
515 fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno)); 510 fprintf(stderr, "Error parsing IPv4 address: %s\n", strerror(errno));
516 global_ret = 7; 511 global_ret = 7;
517 goto cleanup; 512 goto cleanup;
518 } 513 }
519 if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p)) 514 if ((1 != sscanf(argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p))
520 { 515 {
521 fprintf (stderr, "Error parsing port value `%s'\n", argv[3]); 516 fprintf(stderr, "Error parsing port value `%s'\n", argv[3]);
522 global_ret = 8; 517 global_ret = 8;
523 goto cleanup; 518 goto cleanup;
524 } 519 }
525 port = (uint16_t) p; 520 port = (uint16_t)p;
526 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) 521 if (1 != inet_pton(AF_INET, DUMMY_IP, &dummy))
527 { 522 {
528 fprintf (stderr, "Internal error converting dummy IP to binary.\n"); 523 fprintf(stderr, "Internal error converting dummy IP to binary.\n");
529 global_ret = 9; 524 global_ret = 9;
530 goto cleanup; 525 goto cleanup;
531 } 526 }
532 send_icmp (&external, &target); 527 send_icmp(&external, &target);
533 send_icmp_udp (&external, &target); 528 send_icmp_udp(&external, &target);
534 global_ret = 0; 529 global_ret = 0;
535 cleanup: 530cleanup:
536 if (-1 != rawsock) 531 if (-1 != rawsock)
537 (void) close (rawsock); 532 (void)close(rawsock);
538 return global_ret; 533 return global_ret;
539} 534}
540 535
diff --git a/src/nat/gnunet-helper-nat-server-windows.c b/src/nat/gnunet-helper-nat-server-windows.c
index f1b4896e8..13a4d8e6f 100644
--- a/src/nat/gnunet-helper-nat-server-windows.c
+++ b/src/nat/gnunet-helper-nat-server-windows.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/** 21/**
22 * @file src/nat/gnunet-helper-nat-server-windows.c 22 * @file src/nat/gnunet-helper-nat-server-windows.c
@@ -42,7 +42,7 @@
42 */ 42 */
43#define _GNU_SOURCE 43#define _GNU_SOURCE
44/* Instead of including gnunet_common.h */ 44/* Instead of including gnunet_common.h */
45#define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0) 45#define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void)memcpy(dst, src, n); } } while (0)
46 46
47#define FD_SETSIZE 1024 47#define FD_SETSIZE 1024
48#include <winsock2.h> 48#include <winsock2.h>
@@ -94,9 +94,7 @@
94/** 94/**
95 * IPv4 header. 95 * IPv4 header.
96 */ 96 */
97struct ip_header 97struct ip_header {
98{
99
100 /** 98 /**
101 * Version (4 bits) + Internet header length (4 bits) 99 * Version (4 bits) + Internet header length (4 bits)
102 */ 100 */
@@ -151,8 +149,7 @@ struct ip_header
151/** 149/**
152 * Format of ICMP packet. 150 * Format of ICMP packet.
153 */ 151 */
154struct icmp_ttl_exceeded_header 152struct icmp_ttl_exceeded_header {
155{
156 uint8_t type; 153 uint8_t type;
157 154
158 uint8_t code; 155 uint8_t code;
@@ -164,8 +161,7 @@ struct icmp_ttl_exceeded_header
164 /* followed by original payload */ 161 /* followed by original payload */
165}; 162};
166 163
167struct icmp_echo_header 164struct icmp_echo_header {
168{
169 uint8_t type; 165 uint8_t type;
170 166
171 uint8_t code; 167 uint8_t code;
@@ -178,8 +174,7 @@ struct icmp_echo_header
178/** 174/**
179 * Beginning of UDP packet. 175 * Beginning of UDP packet.
180 */ 176 */
181struct udp_header 177struct udp_header {
182{
183 uint16_t src_port; 178 uint16_t src_port;
184 179
185 uint16_t dst_port; 180 uint16_t dst_port;
@@ -223,7 +218,7 @@ static struct in_addr dummy;
223 * @return the CRC 16. 218 * @return the CRC 16.
224 */ 219 */
225static uint16_t 220static uint16_t
226calc_checksum (const uint16_t * data, unsigned int bytes) 221calc_checksum(const uint16_t * data, unsigned int bytes)
227{ 222{
228 uint32_t sum; 223 uint32_t sum;
229 unsigned int i; 224 unsigned int i;
@@ -232,7 +227,7 @@ calc_checksum (const uint16_t * data, unsigned int bytes)
232 for (i = 0; i < bytes / 2; i++) 227 for (i = 0; i < bytes / 2; i++)
233 sum += data[i]; 228 sum += data[i];
234 sum = (sum & 0xffff) + (sum >> 16); 229 sum = (sum & 0xffff) + (sum >> 16);
235 sum = htons (0xffff - sum); 230 sum = htons(0xffff - sum);
236 return sum; 231 return sum;
237} 232}
238 233
@@ -246,14 +241,14 @@ calc_checksum (const uint16_t * data, unsigned int bytes)
246 * @return 1 on success 241 * @return 1 on success
247 */ 242 */
248static int 243static int
249inet_pton (int af, const char *cp, struct in_addr *buf) 244inet_pton(int af, const char *cp, struct in_addr *buf)
250{ 245{
251 buf->s_addr = inet_addr (cp); 246 buf->s_addr = inet_addr(cp);
252 if (buf->s_addr == INADDR_NONE) 247 if (buf->s_addr == INADDR_NONE)
253 { 248 {
254 fprintf (stderr, "Error %d handling address %s", WSAGetLastError (), cp); 249 fprintf(stderr, "Error %d handling address %s", WSAGetLastError(), cp);
255 return 0; 250 return 0;
256 } 251 }
257 return 1; 252 return 1;
258} 253}
259 254
@@ -264,9 +259,9 @@ inet_pton (int af, const char *cp, struct in_addr *buf)
264 * @param my_ip source address (our ip address) 259 * @param my_ip source address (our ip address)
265 */ 260 */
266static void 261static void
267send_icmp_echo (const struct in_addr *my_ip) 262send_icmp_echo(const struct in_addr *my_ip)
268{ 263{
269 char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)]; 264 char packet[sizeof(struct ip_header) + sizeof(struct icmp_echo_header)];
270 struct icmp_echo_header icmp_echo; 265 struct icmp_echo_header icmp_echo;
271 struct ip_header ip_pkt; 266 struct ip_header ip_pkt;
272 struct sockaddr_in dst; 267 struct sockaddr_in dst;
@@ -276,8 +271,8 @@ send_icmp_echo (const struct in_addr *my_ip)
276 off = 0; 271 off = 0;
277 ip_pkt.vers_ihl = 0x45; 272 ip_pkt.vers_ihl = 0x45;
278 ip_pkt.tos = 0; 273 ip_pkt.tos = 0;
279 ip_pkt.pkt_len = htons (sizeof (packet)); 274 ip_pkt.pkt_len = htons(sizeof(packet));
280 ip_pkt.id = htons (PACKET_ID); 275 ip_pkt.id = htons(PACKET_ID);
281 ip_pkt.flags_frag_offset = 0; 276 ip_pkt.flags_frag_offset = 0;
282 ip_pkt.ttl = IPDEFTTL; 277 ip_pkt.ttl = IPDEFTTL;
283 ip_pkt.proto = IPPROTO_ICMP; 278 ip_pkt.proto = IPPROTO_ICMP;
@@ -285,35 +280,35 @@ send_icmp_echo (const struct in_addr *my_ip)
285 ip_pkt.src_ip = my_ip->s_addr; 280 ip_pkt.src_ip = my_ip->s_addr;
286 ip_pkt.dst_ip = dummy.s_addr; 281 ip_pkt.dst_ip = dummy.s_addr;
287 ip_pkt.checksum = 282 ip_pkt.checksum =
288 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); 283 htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header)));
289 GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); 284 GNUNET_memcpy(&packet[off], &ip_pkt, sizeof(struct ip_header));
290 off += sizeof (struct ip_header); 285 off += sizeof(struct ip_header);
291 286
292 icmp_echo.type = ICMP_ECHO; 287 icmp_echo.type = ICMP_ECHO;
293 icmp_echo.code = 0; 288 icmp_echo.code = 0;
294 icmp_echo.reserved = 0; 289 icmp_echo.reserved = 0;
295 icmp_echo.checksum = 0; 290 icmp_echo.checksum = 0;
296 icmp_echo.checksum = 291 icmp_echo.checksum =
297 htons (calc_checksum 292 htons(calc_checksum
298 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header))); 293 ((uint16_t *)&icmp_echo, sizeof(struct icmp_echo_header)));
299 GNUNET_memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header)); 294 GNUNET_memcpy(&packet[off], &icmp_echo, sizeof(struct icmp_echo_header));
300 off += sizeof (struct icmp_echo_header); 295 off += sizeof(struct icmp_echo_header);
301 296
302 memset (&dst, 0, sizeof (dst)); 297 memset(&dst, 0, sizeof(dst));
303 dst.sin_family = AF_INET; 298 dst.sin_family = AF_INET;
304 dst.sin_addr = dummy; 299 dst.sin_addr = dummy;
305 err = 300 err =
306 sendto (rawsock, packet, off, 0, (struct sockaddr *) &dst, sizeof (dst)); 301 sendto(rawsock, packet, off, 0, (struct sockaddr *)&dst, sizeof(dst));
307 if (err < 0) 302 if (err < 0)
308 { 303 {
309#if VERBOSE 304#if VERBOSE
310 fprintf (stderr, "sendto failed: %s\n", strerror (errno)); 305 fprintf(stderr, "sendto failed: %s\n", strerror(errno));
311#endif 306#endif
312 } 307 }
313 else if (err != off) 308 else if (err != off)
314 { 309 {
315 fprintf (stderr, "Error: partial send of ICMP message\n"); 310 fprintf(stderr, "Error: partial send of ICMP message\n");
316 } 311 }
317} 312}
318 313
319 314
@@ -321,26 +316,26 @@ send_icmp_echo (const struct in_addr *my_ip)
321 * Send a UDP message to the dummy IP. 316 * Send a UDP message to the dummy IP.
322 */ 317 */
323static void 318static void
324send_udp () 319send_udp()
325{ 320{
326 struct sockaddr_in dst; 321 struct sockaddr_in dst;
327 ssize_t err; 322 ssize_t err;
328 323
329 memset (&dst, 0, sizeof (dst)); 324 memset(&dst, 0, sizeof(dst));
330 dst.sin_family = AF_INET; 325 dst.sin_family = AF_INET;
331 dst.sin_addr = dummy; 326 dst.sin_addr = dummy;
332 dst.sin_port = htons (NAT_TRAV_PORT); 327 dst.sin_port = htons(NAT_TRAV_PORT);
333 err = sendto (udpsock, NULL, 0, 0, (struct sockaddr *) &dst, sizeof (dst)); 328 err = sendto(udpsock, NULL, 0, 0, (struct sockaddr *)&dst, sizeof(dst));
334 if (err < 0) 329 if (err < 0)
335 { 330 {
336#if VERBOSE 331#if VERBOSE
337 fprintf (stderr, "sendto failed: %s\n", strerror (errno)); 332 fprintf(stderr, "sendto failed: %s\n", strerror(errno));
338#endif 333#endif
339 } 334 }
340 else if (0 != err) 335 else if (0 != err)
341 { 336 {
342 fprintf (stderr, "Error: partial send of ICMP message\n"); 337 fprintf(stderr, "Error: partial send of ICMP message\n");
343 } 338 }
344} 339}
345 340
346 341
@@ -348,7 +343,7 @@ send_udp ()
348 * We've received an ICMP response. Process it. 343 * We've received an ICMP response. Process it.
349 */ 344 */
350static void 345static void
351process_icmp_response () 346process_icmp_response()
352{ 347{
353 char buf[65536]; 348 char buf[65536];
354 ssize_t have; 349 ssize_t have;
@@ -361,78 +356,80 @@ process_icmp_response ()
361 uint16_t port; 356 uint16_t port;
362 DWORD ssize; 357 DWORD ssize;
363 358
364 have = read (icmpsock, buf, sizeof (buf)); 359 have = read(icmpsock, buf, sizeof(buf));
365 if (have == -1) 360 if (have == -1)
366 { 361 {
367 fprintf (stderr, "Error reading raw socket: %s\n", strerror (errno)); 362 fprintf(stderr, "Error reading raw socket: %s\n", strerror(errno));
368 return; 363 return;
369 } 364 }
370#if VERBOSE 365#if VERBOSE
371 fprintf (stderr, "Received message of %u bytes\n", (unsigned int) have); 366 fprintf(stderr, "Received message of %u bytes\n", (unsigned int)have);
372#endif 367#endif
373 if (have < 368 if (have <
374 (ssize_t) (sizeof (struct ip_header) + 369 (ssize_t)(sizeof(struct ip_header) +
375 sizeof (struct icmp_ttl_exceeded_header) + 370 sizeof(struct icmp_ttl_exceeded_header) +
376 sizeof (struct ip_header))) 371 sizeof(struct ip_header)))
377 { 372 {
378 /* malformed */ 373 /* malformed */
379 return; 374 return;
380 } 375 }
381 off = 0; 376 off = 0;
382 GNUNET_memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header)); 377 GNUNET_memcpy(&ip_pkt, &buf[off], sizeof(struct ip_header));
383 off += sizeof (struct ip_header); 378 off += sizeof(struct ip_header);
384 GNUNET_memcpy (&source_ip, &ip_pkt.src_ip, sizeof (source_ip)); 379 GNUNET_memcpy(&source_ip, &ip_pkt.src_ip, sizeof(source_ip));
385 GNUNET_memcpy (&icmp_ttl, &buf[off], sizeof (struct icmp_ttl_exceeded_header)); 380 GNUNET_memcpy(&icmp_ttl, &buf[off], sizeof(struct icmp_ttl_exceeded_header));
386 off += sizeof (struct icmp_ttl_exceeded_header); 381 off += sizeof(struct icmp_ttl_exceeded_header);
387 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code)) 382 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code))
388 {
389 /* different type than what we want */
390 return;
391 }
392 /* skip 2nd IP header */
393 GNUNET_memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header));
394 off += sizeof (struct ip_header);
395
396 switch (ip_pkt.proto)
397 {
398 case IPPROTO_ICMP:
399 if (have !=
400 (sizeof (struct ip_header) * 2 +
401 sizeof (struct icmp_ttl_exceeded_header) +
402 sizeof (struct icmp_echo_header)))
403 { 383 {
404 /* malformed */ 384 /* different type than what we want */
405 return; 385 return;
406 } 386 }
407 /* grab ICMP ECHO content */ 387 /* skip 2nd IP header */
408 GNUNET_memcpy (&icmp_echo, &buf[off], sizeof (struct icmp_echo_header)); 388 GNUNET_memcpy(&ip_pkt, &buf[off], sizeof(struct ip_header));
409 port = (uint16_t) ntohl (icmp_echo.reserved); 389 off += sizeof(struct ip_header);
410 break; 390
411 case IPPROTO_UDP: 391 switch (ip_pkt.proto)
412 if (have !=
413 (sizeof (struct ip_header) * 2 +
414 sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct udp_header)))
415 { 392 {
416 /* malformed */ 393 case IPPROTO_ICMP:
394 if (have !=
395 (sizeof(struct ip_header) * 2 +
396 sizeof(struct icmp_ttl_exceeded_header) +
397 sizeof(struct icmp_echo_header)))
398 {
399 /* malformed */
400 return;
401 }
402 /* grab ICMP ECHO content */
403 GNUNET_memcpy(&icmp_echo, &buf[off], sizeof(struct icmp_echo_header));
404 port = (uint16_t)ntohl(icmp_echo.reserved);
405 break;
406
407 case IPPROTO_UDP:
408 if (have !=
409 (sizeof(struct ip_header) * 2 +
410 sizeof(struct icmp_ttl_exceeded_header) + sizeof(struct udp_header)))
411 {
412 /* malformed */
413 return;
414 }
415 /* grab UDP content */
416 GNUNET_memcpy(&udp_pkt, &buf[off], sizeof(struct udp_header));
417 port = ntohs(udp_pkt.length);
418 break;
419
420 default:
421 /* different type than what we want */
417 return; 422 return;
418 } 423 }
419 /* grab UDP content */ 424
420 GNUNET_memcpy (&udp_pkt, &buf[off], sizeof (struct udp_header)); 425 ssize = sizeof(buf);
421 port = ntohs (udp_pkt.length); 426 WSAAddressToString((LPSOCKADDR)&source_ip, sizeof(source_ip), NULL, buf,
422 break; 427 &ssize);
423 default:
424 /* different type than what we want */
425 return;
426 }
427
428 ssize = sizeof (buf);
429 WSAAddressToString ((LPSOCKADDR) & source_ip, sizeof (source_ip), NULL, buf,
430 &ssize);
431 if (port == 0) 428 if (port == 0)
432 fprintf (stdout, "%s\n", buf); 429 fprintf(stdout, "%s\n", buf);
433 else 430 else
434 fprintf (stdout, "%s:%u\n", buf, (unsigned int) port); 431 fprintf(stdout, "%s:%u\n", buf, (unsigned int)port);
435 fflush (stdout); 432 fflush(stdout);
436} 433}
437 434
438 435
@@ -442,16 +439,16 @@ process_icmp_response ()
442 * @return INVALID_SOCKET on error 439 * @return INVALID_SOCKET on error
443 */ 440 */
444static SOCKET 441static SOCKET
445make_icmp_socket () 442make_icmp_socket()
446{ 443{
447 SOCKET ret; 444 SOCKET ret;
448 445
449 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); 446 ret = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
450 if (INVALID_SOCKET == ret) 447 if (INVALID_SOCKET == ret)
451 { 448 {
452 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno)); 449 fprintf(stderr, "Error opening RAW socket: %s\n", strerror(errno));
453 return INVALID_SOCKET; 450 return INVALID_SOCKET;
454 } 451 }
455 return ret; 452 return ret;
456} 453}
457 454
@@ -462,34 +459,34 @@ make_icmp_socket ()
462 * @return INVALID_SOCKET on error 459 * @return INVALID_SOCKET on error
463 */ 460 */
464static SOCKET 461static SOCKET
465make_raw_socket () 462make_raw_socket()
466{ 463{
467 DWORD bOptVal = TRUE; 464 DWORD bOptVal = TRUE;
468 int bOptLen = sizeof (bOptVal); 465 int bOptLen = sizeof(bOptVal);
469 466
470 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); 467 rawsock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
471 if (INVALID_SOCKET == rawsock) 468 if (INVALID_SOCKET == rawsock)
472 { 469 {
473 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno)); 470 fprintf(stderr, "Error opening RAW socket: %s\n", strerror(errno));
474 return INVALID_SOCKET; 471 return INVALID_SOCKET;
475 } 472 }
476 473
477 if (0 != 474 if (0 !=
478 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, 475 setsockopt(rawsock, SOL_SOCKET, SO_BROADCAST, (char *)&bOptVal,
479 bOptLen)) 476 bOptLen))
480 { 477 {
481 fprintf (stderr, "Error setting SO_BROADCAST to ON: %s\n", 478 fprintf(stderr, "Error setting SO_BROADCAST to ON: %s\n",
482 strerror (errno)); 479 strerror(errno));
483 closesocket (rawsock); 480 closesocket(rawsock);
484 return INVALID_SOCKET; 481 return INVALID_SOCKET;
485 } 482 }
486 if (0 != 483 if (0 !=
487 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &bOptVal, bOptLen)) 484 setsockopt(rawsock, IPPROTO_IP, IP_HDRINCL, (char *)&bOptVal, bOptLen))
488 { 485 {
489 fprintf (stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror (errno)); 486 fprintf(stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror(errno));
490 closesocket (rawsock); 487 closesocket(rawsock);
491 return INVALID_SOCKET; 488 return INVALID_SOCKET;
492 } 489 }
493 return rawsock; 490 return rawsock;
494} 491}
495 492
@@ -501,33 +498,33 @@ make_raw_socket ()
501 * @return INVALID_SOCKET on error 498 * @return INVALID_SOCKET on error
502 */ 499 */
503static SOCKET 500static SOCKET
504make_udp_socket (const struct in_addr *my_ip) 501make_udp_socket(const struct in_addr *my_ip)
505{ 502{
506 SOCKET ret; 503 SOCKET ret;
507 struct sockaddr_in addr; 504 struct sockaddr_in addr;
508 505
509 ret = socket (AF_INET, SOCK_DGRAM, 0); 506 ret = socket(AF_INET, SOCK_DGRAM, 0);
510 if (INVALID_SOCKET == ret) 507 if (INVALID_SOCKET == ret)
511 { 508 {
512 fprintf (stderr, "Error opening UDP socket: %s\n", strerror (errno)); 509 fprintf(stderr, "Error opening UDP socket: %s\n", strerror(errno));
513 return INVALID_SOCKET; 510 return INVALID_SOCKET;
514 } 511 }
515 memset (&addr, 0, sizeof (addr)); 512 memset(&addr, 0, sizeof(addr));
516 addr.sin_family = AF_INET; 513 addr.sin_family = AF_INET;
517 addr.sin_addr = *my_ip; 514 addr.sin_addr = *my_ip;
518 addr.sin_port = htons (NAT_TRAV_PORT); 515 addr.sin_port = htons(NAT_TRAV_PORT);
519 if (0 != bind (ret, (struct sockaddr *) &addr, sizeof (addr))) 516 if (0 != bind(ret, (struct sockaddr *)&addr, sizeof(addr)))
520 { 517 {
521 fprintf (stderr, "Error binding UDP socket to port %u: %s\n", NAT_TRAV_PORT, 518 fprintf(stderr, "Error binding UDP socket to port %u: %s\n", NAT_TRAV_PORT,
522 strerror (errno)); 519 strerror(errno));
523 /* likely problematic, but not certain, try to continue */ 520 /* likely problematic, but not certain, try to continue */
524 } 521 }
525 return ret; 522 return ret;
526} 523}
527 524
528 525
529int 526int
530main (int argc, char *const *argv) 527main(int argc, char *const *argv)
531{ 528{
532 struct in_addr external; 529 struct in_addr external;
533 fd_set rs; 530 fd_set rs;
@@ -535,79 +532,79 @@ main (int argc, char *const *argv)
535 WSADATA wsaData; 532 WSADATA wsaData;
536 unsigned int alt = 0; 533 unsigned int alt = 0;
537 534
538 if ( (argc > 1) && (0 != strcmp (argv[1], "-d"))) 535 if ((argc > 1) && (0 != strcmp(argv[1], "-d")))
539 { 536 {
540 privilege_testing = TRUE; 537 privilege_testing = TRUE;
541 fprintf (stderr, 538 fprintf(stderr,
542 "%s", 539 "%s",
543 "DEBUG: Running binary in privilege testing mode."); 540 "DEBUG: Running binary in privilege testing mode.");
544 argv++; 541 argv++;
545 argc--; 542 argc--;
546 } 543 }
547 544
548 if (2 != argc) 545 if (2 != argc)
549 {
550 fprintf (stderr,
551 "This program must be started with our (internal NAT) IP as the only argument.\n");
552 return 1;
553 }
554 if (1 != inet_pton (AF_INET, argv[1], &external))
555 {
556 fprintf (stderr, "Error parsing IPv4 address: %s, error %s\n", argv[1],
557 strerror (errno));
558 return 1;
559 }
560 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
561 {
562 fprintf (stderr, "Internal error converting dummy IP to binary.\n");
563 return 2;
564 }
565 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
566 {
567 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
568 return 2;
569 }
570 if (INVALID_SOCKET == (icmpsock = make_icmp_socket ()))
571 {
572 return 3;
573 }
574 if (INVALID_SOCKET == (make_raw_socket ()))
575 {
576 closesocket (icmpsock);
577 return 3;
578 }
579 if (INVALID_SOCKET == (udpsock = make_udp_socket (&external)))
580 {
581 closesocket (icmpsock);
582 closesocket (rawsock);
583 return 3;
584 }
585
586 while ( ! privilege_testing)
587 {
588 FD_ZERO (&rs);
589 FD_SET (icmpsock, &rs);
590 tv.tv_sec = 0;
591 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
592 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
593 { 546 {
594 if (errno == EINTR) 547 fprintf(stderr,
595 continue; 548 "This program must be started with our (internal NAT) IP as the only argument.\n");
596 fprintf (stderr, "select failed: %s\n", strerror (errno)); 549 return 1;
597 break; 550 }
551 if (1 != inet_pton(AF_INET, argv[1], &external))
552 {
553 fprintf(stderr, "Error parsing IPv4 address: %s, error %s\n", argv[1],
554 strerror(errno));
555 return 1;
556 }
557 if (1 != inet_pton(AF_INET, DUMMY_IP, &dummy))
558 {
559 fprintf(stderr, "Internal error converting dummy IP to binary.\n");
560 return 2;
561 }
562 if (WSAStartup(MAKEWORD(2, 1), &wsaData) != 0)
563 {
564 fprintf(stderr, "Failed to find Winsock 2.1 or better.\n");
565 return 2;
566 }
567 if (INVALID_SOCKET == (icmpsock = make_icmp_socket()))
568 {
569 return 3;
570 }
571 if (INVALID_SOCKET == (make_raw_socket()))
572 {
573 closesocket(icmpsock);
574 return 3;
575 }
576 if (INVALID_SOCKET == (udpsock = make_udp_socket(&external)))
577 {
578 closesocket(icmpsock);
579 closesocket(rawsock);
580 return 3;
581 }
582
583 while (!privilege_testing)
584 {
585 FD_ZERO(&rs);
586 FD_SET(icmpsock, &rs);
587 tv.tv_sec = 0;
588 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
589 if (-1 == select(icmpsock + 1, &rs, NULL, NULL, &tv))
590 {
591 if (errno == EINTR)
592 continue;
593 fprintf(stderr, "select failed: %s\n", strerror(errno));
594 break;
595 }
596 if (FD_ISSET(icmpsock, &rs))
597 process_icmp_response();
598 if (0 == (++alt % 2))
599 send_icmp_echo(&external);
600 else
601 send_udp();
598 } 602 }
599 if (FD_ISSET (icmpsock, &rs))
600 process_icmp_response ();
601 if (0 == (++alt % 2))
602 send_icmp_echo (&external);
603 else
604 send_udp ();
605 }
606 /* select failed (internal error or OS out of resources) */ 603 /* select failed (internal error or OS out of resources) */
607 closesocket (icmpsock); 604 closesocket(icmpsock);
608 closesocket (rawsock); 605 closesocket(rawsock);
609 closesocket (udpsock); 606 closesocket(udpsock);
610 WSACleanup (); 607 WSACleanup();
611 if (privilege_testing) 608 if (privilege_testing)
612 return 0; 609 return 0;
613 return 4; 610 return 4;
diff --git a/src/nat/gnunet-helper-nat-server.c b/src/nat/gnunet-helper-nat-server.c
index 78b926037..f087590c8 100644
--- a/src/nat/gnunet-helper-nat-server.c
+++ b/src/nat/gnunet-helper-nat-server.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/** 21/**
22 * @file src/nat/gnunet-helper-nat-server.c 22 * @file src/nat/gnunet-helper-nat-server.c
@@ -79,7 +79,7 @@
79 * @param src source of the copy, may be NULL if @a n is zero 79 * @param src source of the copy, may be NULL if @a n is zero
80 * @param n number of bytes to copy 80 * @param n number of bytes to copy
81 */ 81 */
82#define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0) 82#define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void)memcpy(dst, src, n); } } while (0)
83 83
84/** 84/**
85 * Should we print some debug output? 85 * Should we print some debug output?
@@ -109,9 +109,7 @@
109/** 109/**
110 * IPv4 header. 110 * IPv4 header.
111 */ 111 */
112struct ip_header 112struct ip_header {
113{
114
115 /** 113 /**
116 * Version (4 bits) + Internet header length (4 bits) 114 * Version (4 bits) + Internet header length (4 bits)
117 */ 115 */
@@ -166,8 +164,7 @@ struct ip_header
166/** 164/**
167 * Format of ICMP packet. 165 * Format of ICMP packet.
168 */ 166 */
169struct icmp_ttl_exceeded_header 167struct icmp_ttl_exceeded_header {
170{
171 uint8_t type; 168 uint8_t type;
172 169
173 uint8_t code; 170 uint8_t code;
@@ -179,8 +176,7 @@ struct icmp_ttl_exceeded_header
179 /* followed by original payload */ 176 /* followed by original payload */
180}; 177};
181 178
182struct icmp_echo_header 179struct icmp_echo_header {
183{
184 uint8_t type; 180 uint8_t type;
185 181
186 uint8_t code; 182 uint8_t code;
@@ -194,8 +190,7 @@ struct icmp_echo_header
194/** 190/**
195 * Beginning of UDP packet. 191 * Beginning of UDP packet.
196 */ 192 */
197struct udp_header 193struct udp_header {
198{
199 uint16_t src_port; 194 uint16_t src_port;
200 195
201 uint16_t dst_port; 196 uint16_t dst_port;
@@ -234,7 +229,7 @@ static struct in_addr dummy;
234 * @return the CRC 16. 229 * @return the CRC 16.
235 */ 230 */
236static uint16_t 231static uint16_t
237calc_checksum (const uint16_t * data, unsigned int bytes) 232calc_checksum(const uint16_t * data, unsigned int bytes)
238{ 233{
239 uint32_t sum; 234 uint32_t sum;
240 unsigned int i; 235 unsigned int i;
@@ -243,7 +238,7 @@ calc_checksum (const uint16_t * data, unsigned int bytes)
243 for (i = 0; i < bytes / 2; i++) 238 for (i = 0; i < bytes / 2; i++)
244 sum += data[i]; 239 sum += data[i];
245 sum = (sum & 0xffff) + (sum >> 16); 240 sum = (sum & 0xffff) + (sum >> 16);
246 sum = htons (0xffff - sum); 241 sum = htons(0xffff - sum);
247 return sum; 242 return sum;
248} 243}
249 244
@@ -254,9 +249,9 @@ calc_checksum (const uint16_t * data, unsigned int bytes)
254 * @param my_ip source address (our ip address) 249 * @param my_ip source address (our ip address)
255 */ 250 */
256static void 251static void
257send_icmp_echo (const struct in_addr *my_ip) 252send_icmp_echo(const struct in_addr *my_ip)
258{ 253{
259 char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)]; 254 char packet[sizeof(struct ip_header) + sizeof(struct icmp_echo_header)];
260 struct icmp_echo_header icmp_echo; 255 struct icmp_echo_header icmp_echo;
261 struct ip_header ip_pkt; 256 struct ip_header ip_pkt;
262 struct sockaddr_in dst; 257 struct sockaddr_in dst;
@@ -266,8 +261,8 @@ send_icmp_echo (const struct in_addr *my_ip)
266 off = 0; 261 off = 0;
267 ip_pkt.vers_ihl = 0x45; 262 ip_pkt.vers_ihl = 0x45;
268 ip_pkt.tos = 0; 263 ip_pkt.tos = 0;
269 ip_pkt.pkt_len = htons (sizeof (packet)); 264 ip_pkt.pkt_len = htons(sizeof(packet));
270 ip_pkt.id = htons (PACKET_ID); 265 ip_pkt.id = htons(PACKET_ID);
271 ip_pkt.flags_frag_offset = 0; 266 ip_pkt.flags_frag_offset = 0;
272 ip_pkt.ttl = IPDEFTTL; 267 ip_pkt.ttl = IPDEFTTL;
273 ip_pkt.proto = IPPROTO_ICMP; 268 ip_pkt.proto = IPPROTO_ICMP;
@@ -275,51 +270,51 @@ send_icmp_echo (const struct in_addr *my_ip)
275 ip_pkt.src_ip = my_ip->s_addr; 270 ip_pkt.src_ip = my_ip->s_addr;
276 ip_pkt.dst_ip = dummy.s_addr; 271 ip_pkt.dst_ip = dummy.s_addr;
277 ip_pkt.checksum = 272 ip_pkt.checksum =
278 htons (calc_checksum ((uint16_t *) & ip_pkt, 273 htons(calc_checksum((uint16_t *)&ip_pkt,
279 sizeof (struct ip_header))); 274 sizeof(struct ip_header)));
280 GNUNET_memcpy (&packet[off], 275 GNUNET_memcpy(&packet[off],
281 &ip_pkt, 276 &ip_pkt,
282 sizeof (struct ip_header)); 277 sizeof(struct ip_header));
283 off += sizeof (struct ip_header); 278 off += sizeof(struct ip_header);
284 279
285 icmp_echo.type = ICMP_ECHO; 280 icmp_echo.type = ICMP_ECHO;
286 icmp_echo.code = 0; 281 icmp_echo.code = 0;
287 icmp_echo.checksum = 0; 282 icmp_echo.checksum = 0;
288 icmp_echo.reserved = 0; 283 icmp_echo.reserved = 0;
289 icmp_echo.checksum = 284 icmp_echo.checksum =
290 htons (calc_checksum 285 htons(calc_checksum
291 ((uint16_t *) & icmp_echo, 286 ((uint16_t *)&icmp_echo,
292 sizeof (struct icmp_echo_header))); 287 sizeof(struct icmp_echo_header)));
293 GNUNET_memcpy (&packet[off], 288 GNUNET_memcpy(&packet[off],
294 &icmp_echo, 289 &icmp_echo,
295 sizeof (struct icmp_echo_header)); 290 sizeof(struct icmp_echo_header));
296 off += sizeof (struct icmp_echo_header); 291 off += sizeof(struct icmp_echo_header);
297 292
298 memset (&dst, 0, sizeof (dst)); 293 memset(&dst, 0, sizeof(dst));
299 dst.sin_family = AF_INET; 294 dst.sin_family = AF_INET;
300#if HAVE_SOCKADDR_IN_SIN_LEN 295#if HAVE_SOCKADDR_IN_SIN_LEN
301 dst.sin_len = sizeof (struct sockaddr_in); 296 dst.sin_len = sizeof(struct sockaddr_in);
302#endif 297#endif
303 dst.sin_addr = dummy; 298 dst.sin_addr = dummy;
304 err = sendto (rawsock, 299 err = sendto(rawsock,
305 packet, 300 packet,
306 off, 301 off,
307 0, 302 0,
308 (struct sockaddr *) &dst, 303 (struct sockaddr *)&dst,
309 sizeof (dst)); 304 sizeof(dst));
310 if (err < 0) 305 if (err < 0)
311 { 306 {
312#if VERBOSE 307#if VERBOSE
313 fprintf (stderr, 308 fprintf(stderr,
314 "sendto failed: %s\n", 309 "sendto failed: %s\n",
315 strerror (errno)); 310 strerror(errno));
316#endif 311#endif
317 } 312 }
318 else if (sizeof (packet) != err) 313 else if (sizeof(packet) != err)
319 { 314 {
320 fprintf (stderr, 315 fprintf(stderr,
321 "Error: partial send of ICMP message\n"); 316 "Error: partial send of ICMP message\n");
322 } 317 }
323} 318}
324 319
325 320
@@ -327,37 +322,37 @@ send_icmp_echo (const struct in_addr *my_ip)
327 * Send a UDP message to the dummy IP. 322 * Send a UDP message to the dummy IP.
328 */ 323 */
329static void 324static void
330send_udp () 325send_udp()
331{ 326{
332 struct sockaddr_in dst; 327 struct sockaddr_in dst;
333 ssize_t err; 328 ssize_t err;
334 329
335 memset (&dst, 0, sizeof (dst)); 330 memset(&dst, 0, sizeof(dst));
336 dst.sin_family = AF_INET; 331 dst.sin_family = AF_INET;
337#if HAVE_SOCKADDR_IN_SIN_LEN 332#if HAVE_SOCKADDR_IN_SIN_LEN
338 dst.sin_len = sizeof (struct sockaddr_in); 333 dst.sin_len = sizeof(struct sockaddr_in);
339#endif 334#endif
340 dst.sin_addr = dummy; 335 dst.sin_addr = dummy;
341 dst.sin_port = htons (NAT_TRAV_PORT); 336 dst.sin_port = htons(NAT_TRAV_PORT);
342 err = sendto (udpsock, 337 err = sendto(udpsock,
343 NULL, 338 NULL,
344 0, 339 0,
345 0, 340 0,
346 (struct sockaddr *) &dst, 341 (struct sockaddr *)&dst,
347 sizeof (dst)); 342 sizeof(dst));
348 if (err < 0) 343 if (err < 0)
349 { 344 {
350#if VERBOSE 345#if VERBOSE
351 fprintf (stderr, 346 fprintf(stderr,
352 "sendto failed: %s\n", 347 "sendto failed: %s\n",
353 strerror (errno)); 348 strerror(errno));
354#endif 349#endif
355 } 350 }
356 else if (0 != err) 351 else if (0 != err)
357 { 352 {
358 fprintf (stderr, 353 fprintf(stderr,
359 "Error: partial send of ICMP message\n"); 354 "Error: partial send of ICMP message\n");
360 } 355 }
361} 356}
362 357
363 358
@@ -365,7 +360,7 @@ send_udp ()
365 * We've received an ICMP response. Process it. 360 * We've received an ICMP response. Process it.
366 */ 361 */
367static void 362static void
368process_icmp_response () 363process_icmp_response()
369{ 364{
370 char buf[65536]; 365 char buf[65536];
371 ssize_t have; 366 ssize_t have;
@@ -377,94 +372,96 @@ process_icmp_response ()
377 size_t off; 372 size_t off;
378 uint16_t port; 373 uint16_t port;
379 374
380 have = read (icmpsock, buf, sizeof (buf)); 375 have = read(icmpsock, buf, sizeof(buf));
381 if (-1 == have) 376 if (-1 == have)
382 { 377 {
383 fprintf (stderr, 378 fprintf(stderr,
384 "Error reading raw socket: %s\n", 379 "Error reading raw socket: %s\n",
385 strerror (errno)); 380 strerror(errno));
386 return; 381 return;
387 } 382 }
388#if VERBOSE 383#if VERBOSE
389 fprintf (stderr, 384 fprintf(stderr,
390 "Received message of %u bytes\n", 385 "Received message of %u bytes\n",
391 (unsigned int) have); 386 (unsigned int)have);
392#endif 387#endif
393 if (have < 388 if (have <
394 (ssize_t) (sizeof (struct ip_header) + 389 (ssize_t)(sizeof(struct ip_header) +
395 sizeof (struct icmp_ttl_exceeded_header) + 390 sizeof(struct icmp_ttl_exceeded_header) +
396 sizeof (struct ip_header))) 391 sizeof(struct ip_header)))
397 { 392 {
398 /* malformed */ 393 /* malformed */
399 return; 394 return;
400 } 395 }
401 off = 0; 396 off = 0;
402 GNUNET_memcpy (&ip_pkt, 397 GNUNET_memcpy(&ip_pkt,
403 &buf[off], 398 &buf[off],
404 sizeof (struct ip_header)); 399 sizeof(struct ip_header));
405 off += sizeof (struct ip_header); 400 off += sizeof(struct ip_header);
406 GNUNET_memcpy (&icmp_ttl, 401 GNUNET_memcpy(&icmp_ttl,
407 &buf[off], 402 &buf[off],
408 sizeof (struct icmp_ttl_exceeded_header)); 403 sizeof(struct icmp_ttl_exceeded_header));
409 off += sizeof (struct icmp_ttl_exceeded_header); 404 off += sizeof(struct icmp_ttl_exceeded_header);
410 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code)) 405 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code))
411 { 406 {
412 /* different type than what we want */ 407 /* different type than what we want */
413 return; 408 return;
414 } 409 }
415 /* grab source IP of 1st IP header */ 410 /* grab source IP of 1st IP header */
416 source_ip.s_addr = ip_pkt.src_ip; 411 source_ip.s_addr = ip_pkt.src_ip;
417 412
418 /* skip 2nd IP header */ 413 /* skip 2nd IP header */
419 GNUNET_memcpy (&ip_pkt, 414 GNUNET_memcpy(&ip_pkt,
420 &buf[off], 415 &buf[off],
421 sizeof (struct ip_header)); 416 sizeof(struct ip_header));
422 off += sizeof (struct ip_header); 417 off += sizeof(struct ip_header);
423 418
424 switch (ip_pkt.proto) 419 switch (ip_pkt.proto)
425 {
426 case IPPROTO_ICMP:
427 if (have !=
428 (sizeof (struct ip_header) * 2 +
429 sizeof (struct icmp_ttl_exceeded_header) +
430 sizeof (struct icmp_echo_header)))
431 { 420 {
432 /* malformed */ 421 case IPPROTO_ICMP:
433 return; 422 if (have !=
434 } 423 (sizeof(struct ip_header) * 2 +
435 /* grab ICMP ECHO content */ 424 sizeof(struct icmp_ttl_exceeded_header) +
436 GNUNET_memcpy (&icmp_echo, 425 sizeof(struct icmp_echo_header)))
437 &buf[off], 426 {
438 sizeof (struct icmp_echo_header)); 427 /* malformed */
439 port = (uint16_t) ntohl (icmp_echo.reserved); 428 return;
440 break; 429 }
441 case IPPROTO_UDP: 430 /* grab ICMP ECHO content */
442 if (have != 431 GNUNET_memcpy(&icmp_echo,
443 (sizeof (struct ip_header) * 2 + 432 &buf[off],
444 sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct udp_header))) 433 sizeof(struct icmp_echo_header));
445 { 434 port = (uint16_t)ntohl(icmp_echo.reserved);
446 /* malformed */ 435 break;
436
437 case IPPROTO_UDP:
438 if (have !=
439 (sizeof(struct ip_header) * 2 +
440 sizeof(struct icmp_ttl_exceeded_header) + sizeof(struct udp_header)))
441 {
442 /* malformed */
443 return;
444 }
445 /* grab UDP content */
446 GNUNET_memcpy(&udp_pkt,
447 &buf[off],
448 sizeof(struct udp_header));
449 port = ntohs(udp_pkt.length);
450 break;
451
452 default:
453 /* different type than what we want */
447 return; 454 return;
448 } 455 }
449 /* grab UDP content */
450 GNUNET_memcpy (&udp_pkt,
451 &buf[off],
452 sizeof (struct udp_header));
453 port = ntohs (udp_pkt.length);
454 break;
455 default:
456 /* different type than what we want */
457 return;
458 }
459 456
460 if (port == 0) 457 if (port == 0)
461 fprintf (stdout, "%s\n", 458 fprintf(stdout, "%s\n",
462 inet_ntop (AF_INET, &source_ip, buf, sizeof (buf))); 459 inet_ntop(AF_INET, &source_ip, buf, sizeof(buf)));
463 else 460 else
464 fprintf (stdout, "%s:%u\n", 461 fprintf(stdout, "%s:%u\n",
465 inet_ntop (AF_INET, &source_ip, buf, sizeof (buf)), 462 inet_ntop(AF_INET, &source_ip, buf, sizeof(buf)),
466 (unsigned int) port); 463 (unsigned int)port);
467 fflush (stdout); 464 fflush(stdout);
468} 465}
469 466
470 467
@@ -474,34 +471,34 @@ process_icmp_response ()
474 * @return -1 on error, 0 on success 471 * @return -1 on error, 0 on success
475 */ 472 */
476static int 473static int
477setup_raw_socket () 474setup_raw_socket()
478{ 475{
479 const int one = 1; 476 const int one = 1;
480 477
481 if (-1 == 478 if (-1 ==
482 setsockopt (rawsock, 479 setsockopt(rawsock,
483 SOL_SOCKET, 480 SOL_SOCKET,
484 SO_BROADCAST, 481 SO_BROADCAST,
485 (char *) &one, 482 (char *)&one,
486 sizeof (one))) 483 sizeof(one)))
487 { 484 {
488 fprintf (stderr, 485 fprintf(stderr,
489 "setsockopt failed: %s\n", 486 "setsockopt failed: %s\n",
490 strerror (errno)); 487 strerror(errno));
491 return -1; 488 return -1;
492 } 489 }
493 if (-1 == 490 if (-1 ==
494 setsockopt (rawsock, 491 setsockopt(rawsock,
495 IPPROTO_IP, 492 IPPROTO_IP,
496 IP_HDRINCL, 493 IP_HDRINCL,
497 (char *) &one, 494 (char *)&one,
498 sizeof (one))) 495 sizeof(one)))
499 { 496 {
500 fprintf (stderr, 497 fprintf(stderr,
501 "setsockopt failed: %s\n", 498 "setsockopt failed: %s\n",
502 strerror (errno)); 499 strerror(errno));
503 return -1; 500 return -1;
504 } 501 }
505 return 0; 502 return 0;
506} 503}
507 504
@@ -513,45 +510,45 @@ setup_raw_socket ()
513 * @return -1 on error 510 * @return -1 on error
514 */ 511 */
515static int 512static int
516make_udp_socket (const struct in_addr *my_ip) 513make_udp_socket(const struct in_addr *my_ip)
517{ 514{
518 int ret; 515 int ret;
519 struct sockaddr_in addr; 516 struct sockaddr_in addr;
520 517
521 ret = socket (AF_INET, SOCK_DGRAM, 0); 518 ret = socket(AF_INET, SOCK_DGRAM, 0);
522 if (-1 == ret) 519 if (-1 == ret)
523 { 520 {
524 fprintf (stderr, 521 fprintf(stderr,
525 "Error opening UDP socket: %s\n", 522 "Error opening UDP socket: %s\n",
526 strerror (errno)); 523 strerror(errno));
527 return -1; 524 return -1;
528 } 525 }
529 memset (&addr, 0, sizeof (addr)); 526 memset(&addr, 0, sizeof(addr));
530 addr.sin_family = AF_INET; 527 addr.sin_family = AF_INET;
531#if HAVE_SOCKADDR_IN_SIN_LEN 528#if HAVE_SOCKADDR_IN_SIN_LEN
532 addr.sin_len = sizeof (struct sockaddr_in); 529 addr.sin_len = sizeof(struct sockaddr_in);
533#endif 530#endif
534 addr.sin_addr = *my_ip; 531 addr.sin_addr = *my_ip;
535 addr.sin_port = htons (NAT_TRAV_PORT); 532 addr.sin_port = htons(NAT_TRAV_PORT);
536 533
537 if (0 != bind (ret, 534 if (0 != bind(ret,
538 (struct sockaddr *) &addr, 535 (struct sockaddr *)&addr,
539 sizeof (addr))) 536 sizeof(addr)))
540 { 537 {
541 fprintf (stderr, 538 fprintf(stderr,
542 "Error binding UDP socket to port %u: %s\n", 539 "Error binding UDP socket to port %u: %s\n",
543 NAT_TRAV_PORT, 540 NAT_TRAV_PORT,
544 strerror (errno)); 541 strerror(errno));
545 (void) close (ret); 542 (void)close(ret);
546 return -1; 543 return -1;
547 } 544 }
548 return ret; 545 return ret;
549} 546}
550 547
551 548
552int 549int
553main (int argc, 550main(int argc,
554 char *const *argv) 551 char *const *argv)
555{ 552{
556 struct in_addr external; 553 struct in_addr external;
557 fd_set rs; 554 fd_set rs;
@@ -563,147 +560,147 @@ main (int argc,
563 int global_ret; 560 int global_ret;
564 561
565 /* Create an ICMP raw socket for reading (we'll check errors later) */ 562 /* Create an ICMP raw socket for reading (we'll check errors later) */
566 icmpsock = socket (AF_INET, 563 icmpsock = socket(AF_INET,
567 SOCK_RAW, 564 SOCK_RAW,
568 IPPROTO_ICMP); 565 IPPROTO_ICMP);
569 icmp_eno = errno; 566 icmp_eno = errno;
570 567
571 /* Create an (ICMP) raw socket for writing (we'll check errors later) */ 568 /* Create an (ICMP) raw socket for writing (we'll check errors later) */
572 rawsock = socket (AF_INET, 569 rawsock = socket(AF_INET,
573 SOCK_RAW, 570 SOCK_RAW,
574 IPPROTO_RAW); 571 IPPROTO_RAW);
575 raw_eno = errno; 572 raw_eno = errno;
576 udpsock = -1; 573 udpsock = -1;
577 574
578 /* drop root rights */ 575 /* drop root rights */
579 uid = getuid (); 576 uid = getuid();
580#ifdef HAVE_SETRESUID 577#ifdef HAVE_SETRESUID
581 if (0 != setresuid (uid, uid, uid)) 578 if (0 != setresuid(uid, uid, uid))
582 { 579 {
583 fprintf (stderr, 580 fprintf(stderr,
584 "Failed to setresuid: %s\n", 581 "Failed to setresuid: %s\n",
585 strerror (errno)); 582 strerror(errno));
586 global_ret = 1; 583 global_ret = 1;
587 goto error_exit; 584 goto error_exit;
588 } 585 }
589#else 586#else
590 if (0 != (setuid (uid) | seteuid (uid))) 587 if (0 != (setuid(uid) | seteuid(uid)))
591 { 588 {
592 fprintf (stderr, 589 fprintf(stderr,
593 "Failed to setuid: %s\n", 590 "Failed to setuid: %s\n",
594 strerror (errno)); 591 strerror(errno));
595 global_ret = 2; 592 global_ret = 2;
596 goto error_exit; 593 goto error_exit;
597 } 594 }
598#endif 595#endif
599 596
600 /* Now that we run without root rights, we can do error checking... */ 597 /* Now that we run without root rights, we can do error checking... */
601 if (2 != argc) 598 if (2 != argc)
602 { 599 {
603 fprintf (stderr, 600 fprintf(stderr,
604 "This program must be started with our (internal NAT) IP as the only argument.\n"); 601 "This program must be started with our (internal NAT) IP as the only argument.\n");
605 global_ret = 3; 602 global_ret = 3;
606 goto error_exit; 603 goto error_exit;
607 } 604 }
608 if (1 != inet_pton (AF_INET, argv[1], &external)) 605 if (1 != inet_pton(AF_INET, argv[1], &external))
609 { 606 {
610 fprintf (stderr, 607 fprintf(stderr,
611 "Error parsing IPv4 address: %s\n", 608 "Error parsing IPv4 address: %s\n",
612 strerror (errno)); 609 strerror(errno));
613 global_ret = 4; 610 global_ret = 4;
614 goto error_exit; 611 goto error_exit;
615 } 612 }
616 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) 613 if (1 != inet_pton(AF_INET, DUMMY_IP, &dummy))
617 { 614 {
618 fprintf (stderr, 615 fprintf(stderr,
619 "Internal error converting dummy IP to binary.\n"); 616 "Internal error converting dummy IP to binary.\n");
620 global_ret = 5; 617 global_ret = 5;
621 goto error_exit; 618 goto error_exit;
622 } 619 }
623 620
624 /* error checking icmpsock */ 621 /* error checking icmpsock */
625 if (-1 == icmpsock) 622 if (-1 == icmpsock)
626 { 623 {
627 fprintf (stderr, 624 fprintf(stderr,
628 "Error opening RAW socket: %s\n", 625 "Error opening RAW socket: %s\n",
629 strerror (icmp_eno)); 626 strerror(icmp_eno));
630 global_ret = 6; 627 global_ret = 6;
631 goto error_exit; 628 goto error_exit;
632 } 629 }
633 if (icmpsock >= FD_SETSIZE) 630 if (icmpsock >= FD_SETSIZE)
634 { 631 {
635 /* this could happen if we were started with a large number of already-open 632 /* this could happen if we were started with a large number of already-open
636 file descriptors... */ 633 file descriptors... */
637 fprintf (stderr, 634 fprintf(stderr,
638 "Socket number too large (%d > %u)\n", 635 "Socket number too large (%d > %u)\n",
639 icmpsock, 636 icmpsock,
640 (unsigned int) FD_SETSIZE); 637 (unsigned int)FD_SETSIZE);
641 global_ret = 7; 638 global_ret = 7;
642 goto error_exit; 639 goto error_exit;
643 } 640 }
644 641
645 /* error checking rawsock */ 642 /* error checking rawsock */
646 if (-1 == rawsock) 643 if (-1 == rawsock)
647 { 644 {
648 fprintf (stderr, 645 fprintf(stderr,
649 "Error opening RAW socket: %s\n", 646 "Error opening RAW socket: %s\n",
650 strerror (raw_eno)); 647 strerror(raw_eno));
651 global_ret = 8; 648 global_ret = 8;
652 goto error_exit; 649 goto error_exit;
653 } 650 }
654 /* no need to check 'rawsock' against FD_SETSIZE as it is never used 651 /* no need to check 'rawsock' against FD_SETSIZE as it is never used
655 with 'select' */ 652 with 'select' */
656 653
657 if (0 != setup_raw_socket ()) 654 if (0 != setup_raw_socket())
658 { 655 {
659 global_ret = 9; 656 global_ret = 9;
660 goto error_exit; 657 goto error_exit;
661 } 658 }
662 659
663 if (-1 == (udpsock = make_udp_socket (&external))) 660 if (-1 == (udpsock = make_udp_socket(&external)))
664 { 661 {
665 global_ret = 10; 662 global_ret = 10;
666 goto error_exit; 663 goto error_exit;
667 } 664 }
668 665
669 alt = 0; 666 alt = 0;
670 while (1) 667 while (1)
671 {
672 FD_ZERO (&rs);
673 FD_SET (icmpsock, &rs);
674 tv.tv_sec = 0;
675 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
676 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
677 {
678 if (errno == EINTR)
679 continue;
680 fprintf (stderr,
681 "select failed: %s\n",
682 strerror (errno));
683 break;
684 }
685 if (1 == getppid ()) /* Check the parent process id, if 1 the parent has died, so we should die too */
686 break;
687 if (FD_ISSET (icmpsock, &rs))
688 { 668 {
689 process_icmp_response (); 669 FD_ZERO(&rs);
690 continue; 670 FD_SET(icmpsock, &rs);
671 tv.tv_sec = 0;
672 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
673 if (-1 == select(icmpsock + 1, &rs, NULL, NULL, &tv))
674 {
675 if (errno == EINTR)
676 continue;
677 fprintf(stderr,
678 "select failed: %s\n",
679 strerror(errno));
680 break;
681 }
682 if (1 == getppid()) /* Check the parent process id, if 1 the parent has died, so we should die too */
683 break;
684 if (FD_ISSET(icmpsock, &rs))
685 {
686 process_icmp_response();
687 continue;
688 }
689 if (0 == (++alt % 2))
690 send_icmp_echo(&external);
691 else
692 send_udp();
691 } 693 }
692 if (0 == (++alt % 2))
693 send_icmp_echo (&external);
694 else
695 send_udp ();
696 }
697 694
698 /* select failed (internal error or OS out of resources) */ 695 /* select failed (internal error or OS out of resources) */
699 global_ret = 11; 696 global_ret = 11;
700error_exit: 697error_exit:
701 if (-1 != icmpsock) 698 if (-1 != icmpsock)
702 (void) close (icmpsock); 699 (void)close(icmpsock);
703 if (-1 != rawsock) 700 if (-1 != rawsock)
704 (void) close (rawsock); 701 (void)close(rawsock);
705 if (-1 != udpsock) 702 if (-1 != udpsock)
706 (void) close (udpsock); 703 (void)close(udpsock);
707 return global_ret; 704 return global_ret;
708} 705}
709 706
diff --git a/src/nat/gnunet-nat.c b/src/nat/gnunet-nat.c
index 6c533374e..d82898f56 100644
--- a/src/nat/gnunet-nat.c
+++ b/src/nat/gnunet-nat.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/** 21/**
22 * @file src/nat/gnunet-nat.c 22 * @file src/nat/gnunet-nat.c
@@ -95,13 +95,13 @@ static struct GNUNET_SCHEDULER_Task *rtask;
95 * terminate. 95 * terminate.
96 */ 96 */
97static void 97static void
98test_finished () 98test_finished()
99{ 99{
100 if (NULL != nh) 100 if (NULL != nh)
101 return; 101 return;
102 if (NULL != rtask) 102 if (NULL != rtask)
103 return; 103 return;
104 GNUNET_SCHEDULER_shutdown (); 104 GNUNET_SCHEDULER_shutdown();
105} 105}
106 106
107 107
@@ -119,21 +119,21 @@ test_finished ()
119 * @param addrlen actual length of the @a addr 119 * @param addrlen actual length of the @a addr
120 */ 120 */
121static void 121static void
122address_cb (void *cls, 122address_cb(void *cls,
123 void **app_ctx, 123 void **app_ctx,
124 int add_remove, 124 int add_remove,
125 enum GNUNET_NAT_AddressClass ac, 125 enum GNUNET_NAT_AddressClass ac,
126 const struct sockaddr *addr, 126 const struct sockaddr *addr,
127 socklen_t addrlen) 127 socklen_t addrlen)
128{ 128{
129 (void) cls; 129 (void)cls;
130 (void) app_ctx; 130 (void)app_ctx;
131 131
132 fprintf (stdout, 132 fprintf(stdout,
133 "%s %s (%d)\n", 133 "%s %s (%d)\n",
134 add_remove ? "+" : "-", 134 add_remove ? "+" : "-",
135 GNUNET_a2s (addr, addrlen), 135 GNUNET_a2s(addr, addrlen),
136 (int) ac); 136 (int)ac);
137} 137}
138 138
139 139
@@ -147,13 +147,13 @@ address_cb (void *cls,
147 * @param remote_addrlen actual length of the @a remote_addr 147 * @param remote_addrlen actual length of the @a remote_addr
148 */ 148 */
149static void 149static void
150reversal_cb (void *cls, 150reversal_cb(void *cls,
151 const struct sockaddr *remote_addr, 151 const struct sockaddr *remote_addr,
152 socklen_t remote_addrlen) 152 socklen_t remote_addrlen)
153{ 153{
154 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 154 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
155 "Connection reversal requested by %s\n", 155 "Connection reversal requested by %s\n",
156 GNUNET_a2s (remote_addr, remote_addrlen)); 156 GNUNET_a2s(remote_addr, remote_addrlen));
157} 157}
158 158
159 159
@@ -163,23 +163,23 @@ reversal_cb (void *cls,
163 * @param cls NULL 163 * @param cls NULL
164 */ 164 */
165static void 165static void
166do_shutdown (void *cls) 166do_shutdown(void *cls)
167{ 167{
168 if (NULL != nh) 168 if (NULL != nh)
169 { 169 {
170 GNUNET_NAT_unregister (nh); 170 GNUNET_NAT_unregister(nh);
171 nh = NULL; 171 nh = NULL;
172 } 172 }
173 if (NULL != ls) 173 if (NULL != ls)
174 { 174 {
175 GNUNET_NETWORK_socket_close (ls); 175 GNUNET_NETWORK_socket_close(ls);
176 ls = NULL; 176 ls = NULL;
177 } 177 }
178 if (NULL != rtask) 178 if (NULL != rtask)
179 { 179 {
180 GNUNET_SCHEDULER_cancel (rtask); 180 GNUNET_SCHEDULER_cancel(rtask);
181 rtask = NULL; 181 rtask = NULL;
182 } 182 }
183} 183}
184 184
185 185
@@ -187,45 +187,45 @@ do_shutdown (void *cls)
187 * Task to receive incoming packets for STUN processing. 187 * Task to receive incoming packets for STUN processing.
188 */ 188 */
189static void 189static void
190stun_read_task (void *cls) 190stun_read_task(void *cls)
191{ 191{
192 ssize_t size; 192 ssize_t size;
193 193
194 rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 194 rtask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
195 ls, 195 ls,
196 &stun_read_task, 196 &stun_read_task,
197 NULL); 197 NULL);
198 size = GNUNET_NETWORK_socket_recvfrom_amount (ls); 198 size = GNUNET_NETWORK_socket_recvfrom_amount(ls);
199 if (size > 0) 199 if (size > 0)
200 { 200 {
201 GNUNET_break (0); 201 GNUNET_break(0);
202 GNUNET_SCHEDULER_shutdown (); 202 GNUNET_SCHEDULER_shutdown();
203 global_ret = 1; 203 global_ret = 1;
204 return; 204 return;
205 } 205 }
206 { 206 {
207 char buf[size + 1]; 207 char buf[size + 1];
208 struct sockaddr_storage sa; 208 struct sockaddr_storage sa;
209 socklen_t salen = sizeof (sa); 209 socklen_t salen = sizeof(sa);
210 ssize_t ret; 210 ssize_t ret;
211 211
212 ret = GNUNET_NETWORK_socket_recvfrom (ls, 212 ret = GNUNET_NETWORK_socket_recvfrom(ls,
213 buf, 213 buf,
214 size + 1, 214 size + 1,
215 (struct sockaddr *) &sa, 215 (struct sockaddr *)&sa,
216 &salen); 216 &salen);
217 if (ret != size) 217 if (ret != size)
218 { 218 {
219 GNUNET_break (0); 219 GNUNET_break(0);
220 GNUNET_SCHEDULER_shutdown (); 220 GNUNET_SCHEDULER_shutdown();
221 global_ret = 1; 221 global_ret = 1;
222 return; 222 return;
223 } 223 }
224 (void) GNUNET_NAT_stun_handle_packet (nh, 224 (void)GNUNET_NAT_stun_handle_packet(nh,
225 (const struct sockaddr *) &sa, 225 (const struct sockaddr *)&sa,
226 salen, 226 salen,
227 buf, 227 buf,
228 ret); 228 ret);
229 } 229 }
230} 230}
231 231
@@ -239,10 +239,10 @@ stun_read_task (void *cls)
239 * @param c configuration 239 * @param c configuration
240 */ 240 */
241static void 241static void
242run (void *cls, 242run(void *cls,
243 char *const *args, 243 char *const *args,
244 const char *cfgfile, 244 const char *cfgfile,
245 const struct GNUNET_CONFIGURATION_Handle *c) 245 const struct GNUNET_CONFIGURATION_Handle *c)
246{ 246{
247 uint8_t af; 247 uint8_t af;
248 struct sockaddr *local_sa; 248 struct sockaddr *local_sa;
@@ -251,154 +251,156 @@ run (void *cls,
251 size_t remote_len; 251 size_t remote_len;
252 252
253 if (use_tcp && use_udp) 253 if (use_tcp && use_udp)
254 { 254 {
255 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n"); 255 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n");
256 global_ret = 1; 256 global_ret = 1;
257 return; 257 return;
258 } 258 }
259 proto = 0; 259 proto = 0;
260 if (use_tcp) 260 if (use_tcp)
261 proto = IPPROTO_TCP; 261 proto = IPPROTO_TCP;
262 if (use_udp) 262 if (use_udp)
263 proto = IPPROTO_UDP; 263 proto = IPPROTO_UDP;
264 264
265 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); 265 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL);
266 266
267 if (0 == proto) 267 if (0 == proto)
268 { 268 {
269 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Must specify either TCP or UDP\n"); 269 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "Must specify either TCP or UDP\n");
270 global_ret = 1; 270 global_ret = 1;
271 return; 271 return;
272 } 272 }
273 local_len = 0; 273 local_len = 0;
274 local_sa = NULL; 274 local_sa = NULL;
275 remote_len = 0; 275 remote_len = 0;
276 remote_sa = NULL; 276 remote_sa = NULL;
277 if (NULL != local_addr) 277 if (NULL != local_addr)
278 {
279 local_len =
280 (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr, &af, &local_sa);
281 if (0 == local_len)
282 { 278 {
283 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 279 local_len =
284 "Invalid socket address `%s'\n", 280 (socklen_t)GNUNET_STRINGS_parse_socket_addr(local_addr, &af, &local_sa);
285 local_addr); 281 if (0 == local_len)
286 goto fail_and_shutdown; 282 {
283 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
284 "Invalid socket address `%s'\n",
285 local_addr);
286 goto fail_and_shutdown;
287 }
287 } 288 }
288 }
289 289
290 if (NULL != remote_addr) 290 if (NULL != remote_addr)
291 {
292 remote_len =
293 GNUNET_STRINGS_parse_socket_addr (remote_addr, &af, &remote_sa);
294 if (0 == remote_len)
295 { 291 {
296 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 292 remote_len =
297 "Invalid socket address `%s'\n", 293 GNUNET_STRINGS_parse_socket_addr(remote_addr, &af, &remote_sa);
298 remote_addr); 294 if (0 == remote_len)
299 goto fail_and_shutdown; 295 {
296 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
297 "Invalid socket address `%s'\n",
298 remote_addr);
299 goto fail_and_shutdown;
300 }
300 } 301 }
301 }
302 302
303 if (NULL != local_addr) 303 if (NULL != local_addr)
304 {
305 if (NULL == section_name)
306 section_name = GNUNET_strdup ("undefined");
307 nh = GNUNET_NAT_register (c,
308 section_name,
309 proto,
310 1,
311 (const struct sockaddr **) &local_sa,
312 &local_len,
313 &address_cb,
314 (listen_reversal) ? &reversal_cb : NULL,
315 NULL);
316 }
317 else if (listen_reversal)
318 {
319 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
320 "Use of `-W` only effective in combination with `-i`\n");
321 goto fail_and_shutdown;
322 }
323
324 if (NULL != remote_addr)
325 {
326 int ret;
327
328 if ((NULL == nh) || (sizeof (struct sockaddr_in) != local_len))
329 { 304 {
330 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 305 if (NULL == section_name)
331 "Require IPv4 local address to initiate connection reversal\n"); 306 section_name = GNUNET_strdup("undefined");
332 goto fail_and_shutdown; 307 nh = GNUNET_NAT_register(c,
308 section_name,
309 proto,
310 1,
311 (const struct sockaddr **)&local_sa,
312 &local_len,
313 &address_cb,
314 (listen_reversal) ? &reversal_cb : NULL,
315 NULL);
333 } 316 }
334 if (sizeof (struct sockaddr_in) != remote_len) 317 else if (listen_reversal)
335 { 318 {
336 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 319 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
337 "Require IPv4 reversal target address\n"); 320 "Use of `-W` only effective in combination with `-i`\n");
338 goto fail_and_shutdown; 321 goto fail_and_shutdown;
339 } 322 }
340 GNUNET_assert (AF_INET == local_sa->sa_family); 323
341 GNUNET_assert (AF_INET == remote_sa->sa_family); 324 if (NULL != remote_addr)
342 ret = GNUNET_NAT_request_reversal (nh,
343 (const struct sockaddr_in *) local_sa,
344 (const struct sockaddr_in *) remote_sa);
345 switch (ret)
346 { 325 {
347 case GNUNET_SYSERR: 326 int ret;
348 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 327
349 "Connection reversal internal error\n"); 328 if ((NULL == nh) || (sizeof(struct sockaddr_in) != local_len))
350 break; 329 {
351 case GNUNET_NO: 330 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
352 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 331 "Require IPv4 local address to initiate connection reversal\n");
353 "Connection reversal unavailable\n"); 332 goto fail_and_shutdown;
354 break; 333 }
355 case GNUNET_OK: 334 if (sizeof(struct sockaddr_in) != remote_len)
356 /* operation in progress */ 335 {
357 break; 336 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
337 "Require IPv4 reversal target address\n");
338 goto fail_and_shutdown;
339 }
340 GNUNET_assert(AF_INET == local_sa->sa_family);
341 GNUNET_assert(AF_INET == remote_sa->sa_family);
342 ret = GNUNET_NAT_request_reversal(nh,
343 (const struct sockaddr_in *)local_sa,
344 (const struct sockaddr_in *)remote_sa);
345 switch (ret)
346 {
347 case GNUNET_SYSERR:
348 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
349 "Connection reversal internal error\n");
350 break;
351
352 case GNUNET_NO:
353 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
354 "Connection reversal unavailable\n");
355 break;
356
357 case GNUNET_OK:
358 /* operation in progress */
359 break;
360 }
358 } 361 }
359 }
360 362
361 if (do_stun) 363 if (do_stun)
362 {
363 if (NULL == local_addr)
364 {
365 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
366 "Require local address to support STUN requests\n");
367 goto fail_and_shutdown;
368 }
369 if (IPPROTO_UDP != proto)
370 { 364 {
371 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "STUN only supported over UDP\n"); 365 if (NULL == local_addr)
372 goto fail_and_shutdown; 366 {
367 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
368 "Require local address to support STUN requests\n");
369 goto fail_and_shutdown;
370 }
371 if (IPPROTO_UDP != proto)
372 {
373 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "STUN only supported over UDP\n");
374 goto fail_and_shutdown;
375 }
376 ls = GNUNET_NETWORK_socket_create(af, SOCK_DGRAM, IPPROTO_UDP);
377 if (NULL == ls)
378 {
379 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "Failed to create socket\n");
380 goto fail_and_shutdown;
381 }
382 if (GNUNET_OK != GNUNET_NETWORK_socket_bind(ls, local_sa, local_len))
383 {
384 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
385 "Failed to bind to %s: %s\n",
386 GNUNET_a2s(local_sa, local_len),
387 strerror(errno));
388 goto fail_and_shutdown;
389 }
390 rtask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
391 ls,
392 &stun_read_task,
393 NULL);
373 } 394 }
374 ls = GNUNET_NETWORK_socket_create (af, SOCK_DGRAM, IPPROTO_UDP); 395 GNUNET_free_non_null(remote_sa);
375 if (NULL == ls) 396 GNUNET_free_non_null(local_sa);
376 { 397 test_finished();
377 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Failed to create socket\n");
378 goto fail_and_shutdown;
379 }
380 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ls, local_sa, local_len))
381 {
382 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
383 "Failed to bind to %s: %s\n",
384 GNUNET_a2s (local_sa, local_len),
385 strerror (errno));
386 goto fail_and_shutdown;
387 }
388 rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
389 ls,
390 &stun_read_task,
391 NULL);
392 }
393 GNUNET_free_non_null (remote_sa);
394 GNUNET_free_non_null (local_sa);
395 test_finished ();
396 return; 398 return;
397fail_and_shutdown: 399fail_and_shutdown:
398 global_ret = 1; 400 global_ret = 1;
399 GNUNET_SCHEDULER_shutdown (); 401 GNUNET_SCHEDULER_shutdown();
400 GNUNET_free_non_null (remote_sa); 402 GNUNET_free_non_null(remote_sa);
401 GNUNET_free_non_null (local_sa); 403 GNUNET_free_non_null(local_sa);
402} 404}
403 405
404 406
@@ -410,63 +412,63 @@ fail_and_shutdown:
410 * @return 0 on success, -1 on error 412 * @return 0 on success, -1 on error
411 */ 413 */
412int 414int
413main (int argc, char *const argv[]) 415main(int argc, char *const argv[])
414{ 416{
415 struct GNUNET_GETOPT_CommandLineOption options[] = { 417 struct GNUNET_GETOPT_CommandLineOption options[] = {
416 418 GNUNET_GETOPT_option_string(
417 GNUNET_GETOPT_option_string (
418 'i', 419 'i',
419 "in", 420 "in",
420 "ADDRESS", 421 "ADDRESS",
421 gettext_noop ("which IP and port are we locally using to bind/listen to"), 422 gettext_noop("which IP and port are we locally using to bind/listen to"),
422 &local_addr), 423 &local_addr),
423 424
424 GNUNET_GETOPT_option_string ( 425 GNUNET_GETOPT_option_string(
425 'r', 426 'r',
426 "remote", 427 "remote",
427 "ADDRESS", 428 "ADDRESS",
428 gettext_noop ( 429 gettext_noop(
429 "which remote IP and port should be asked for connection reversal"), 430 "which remote IP and port should be asked for connection reversal"),
430 &remote_addr), 431 &remote_addr),
431 432
432 GNUNET_GETOPT_option_string ( 433 GNUNET_GETOPT_option_string(
433 'S', 434 'S',
434 "section", 435 "section",
435 NULL, 436 NULL,
436 gettext_noop ( 437 gettext_noop(
437 "name of configuration section to find additional options, such as manual host punching data"), 438 "name of configuration section to find additional options, such as manual host punching data"),
438 &section_name), 439 &section_name),
439 440
440 GNUNET_GETOPT_option_flag ('s', 441 GNUNET_GETOPT_option_flag('s',
441 "stun", 442 "stun",
442 gettext_noop ("enable STUN processing"), 443 gettext_noop("enable STUN processing"),
443 &do_stun), 444 &do_stun),
444 445
445 GNUNET_GETOPT_option_flag ('t', "tcp", gettext_noop ("use TCP"), &use_tcp), 446 GNUNET_GETOPT_option_flag('t', "tcp", gettext_noop("use TCP"), &use_tcp),
446 447
447 GNUNET_GETOPT_option_flag ('u', "udp", gettext_noop ("use UDP"), &use_udp), 448 GNUNET_GETOPT_option_flag('u', "udp", gettext_noop("use UDP"), &use_udp),
448 449
449 GNUNET_GETOPT_option_flag ('W', 450 GNUNET_GETOPT_option_flag('W',
450 "watch", 451 "watch",
451 gettext_noop ( 452 gettext_noop(
452 "watch for connection reversal requests"), 453 "watch for connection reversal requests"),
453 &listen_reversal), 454 &listen_reversal),
454 GNUNET_GETOPT_OPTION_END}; 455 GNUNET_GETOPT_OPTION_END
456 };
455 457
456 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 458 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
457 return 2; 459 return 2;
458 if (GNUNET_OK != 460 if (GNUNET_OK !=
459 GNUNET_PROGRAM_run (argc, 461 GNUNET_PROGRAM_run(argc,
460 argv, 462 argv,
461 "gnunet-nat [options]", 463 "gnunet-nat [options]",
462 _ ("GNUnet NAT traversal autoconfigure daemon"), 464 _("GNUnet NAT traversal autoconfigure daemon"),
463 options, 465 options,
464 &run, 466 &run,
465 NULL)) 467 NULL))
466 { 468 {
467 global_ret = 1; 469 global_ret = 1;
468 } 470 }
469 GNUNET_free ((void *) argv); 471 GNUNET_free((void *)argv);
470 return global_ret; 472 return global_ret;
471} 473}
472 474
diff --git a/src/nat/gnunet-service-nat.c b/src/nat/gnunet-service-nat.c
index 2498a990a..6fd09a7d9 100644
--- a/src/nat/gnunet-service-nat.c
+++ b/src/nat/gnunet-service-nat.c
@@ -1,19 +1,19 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2016, 2017 GNUnet e.V. 3 Copyright (C) 2016, 2017 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 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 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, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
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 */
@@ -55,24 +55,23 @@
55 * How often should we ask the OS about a list of active 55 * How often should we ask the OS about a list of active
56 * network interfaces? 56 * network interfaces?
57 */ 57 */
58#define SCAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15) 58#define SCAN_FREQ GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15)
59 59
60/** 60/**
61 * How long do we wait until we forcefully terminate autoconfiguration? 61 * How long do we wait until we forcefully terminate autoconfiguration?
62 */ 62 */
63#define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) 63#define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
64 64
65/** 65/**
66 * How often do we scan for changes in how our external (dyndns) hostname resolves? 66 * How often do we scan for changes in how our external (dyndns) hostname resolves?
67 */ 67 */
68#define DYNDNS_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 7) 68#define DYNDNS_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 7)
69 69
70 70
71/** 71/**
72 * Information we track per client address. 72 * Information we track per client address.
73 */ 73 */
74struct ClientAddress 74struct ClientAddress {
75{
76 /** 75 /**
77 * Network address used by the client. 76 * Network address used by the client.
78 */ 77 */
@@ -84,15 +83,13 @@ struct ClientAddress
84 * pending. 83 * pending.
85 */ 84 */
86 struct GNUNET_NAT_MiniHandle *mh; 85 struct GNUNET_NAT_MiniHandle *mh;
87
88}; 86};
89 87
90 88
91/** 89/**
92 * List of local addresses this system has. 90 * List of local addresses this system has.
93 */ 91 */
94struct LocalAddressList 92struct LocalAddressList {
95{
96 /** 93 /**
97 * This is a linked list. 94 * This is a linked list.
98 */ 95 */
@@ -131,16 +128,13 @@ struct LocalAddressList
131 * What type of address is this? 128 * What type of address is this?
132 */ 129 */
133 enum GNUNET_NAT_AddressClass ac; 130 enum GNUNET_NAT_AddressClass ac;
134
135}; 131};
136 132
137 133
138/** 134/**
139 * Internal data structure we track for each of our clients. 135 * Internal data structure we track for each of our clients.
140 */ 136 */
141struct ClientHandle 137struct ClientHandle {
142{
143
144 /** 138 /**
145 * Kept in a DLL. 139 * Kept in a DLL.
146 */ 140 */
@@ -232,15 +226,13 @@ struct ClientHandle
232 * Client's IPPROTO, e.g. IPPROTO_UDP or IPPROTO_TCP. 226 * Client's IPPROTO, e.g. IPPROTO_UDP or IPPROTO_TCP.
233 */ 227 */
234 uint8_t proto; 228 uint8_t proto;
235
236}; 229};
237 230
238 231
239/** 232/**
240 * External IP address as given to us via some STUN server. 233 * External IP address as given to us via some STUN server.
241 */ 234 */
242struct StunExternalIP 235struct StunExternalIP {
243{
244 /** 236 /**
245 * Kept in a DLL. 237 * Kept in a DLL.
246 */ 238 */
@@ -344,22 +336,22 @@ int enable_upnp;
344 * @param lal entry to free 336 * @param lal entry to free
345 */ 337 */
346static void 338static void
347free_lal (struct LocalAddressList *lal) 339free_lal(struct LocalAddressList *lal)
348{ 340{
349 GNUNET_CONTAINER_DLL_remove (lal_head, 341 GNUNET_CONTAINER_DLL_remove(lal_head,
350 lal_tail, 342 lal_tail,
351 lal); 343 lal);
352 if (NULL != lal->hc) 344 if (NULL != lal->hc)
353 { 345 {
354 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 346 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
355 "Lost NATed local address %s, stopping NAT server\n", 347 "Lost NATed local address %s, stopping NAT server\n",
356 GNUNET_a2s ((const struct sockaddr *) &lal->addr, 348 GNUNET_a2s((const struct sockaddr *)&lal->addr,
357 sizeof (struct sockaddr_in))); 349 sizeof(struct sockaddr_in)));
358 350
359 GN_stop_gnunet_nat_server_ (lal->hc); 351 GN_stop_gnunet_nat_server_(lal->hc);
360 lal->hc = NULL; 352 lal->hc = NULL;
361 } 353 }
362 GNUNET_free (lal); 354 GNUNET_free(lal);
363} 355}
364 356
365 357
@@ -367,12 +359,12 @@ free_lal (struct LocalAddressList *lal)
367 * Free the DLL starting at #lal_head. 359 * Free the DLL starting at #lal_head.
368 */ 360 */
369static void 361static void
370destroy_lal () 362destroy_lal()
371{ 363{
372 struct LocalAddressList *lal; 364 struct LocalAddressList *lal;
373 365
374 while (NULL != (lal = lal_head)) 366 while (NULL != (lal = lal_head))
375 free_lal (lal); 367 free_lal(lal);
376} 368}
377 369
378 370
@@ -385,53 +377,55 @@ destroy_lal ()
385 * @return #GNUNET_OK if message is well-formed 377 * @return #GNUNET_OK if message is well-formed
386 */ 378 */
387static int 379static int
388check_register (void *cls, 380check_register(void *cls,
389 const struct GNUNET_NAT_RegisterMessage *message) 381 const struct GNUNET_NAT_RegisterMessage *message)
390{ 382{
391 uint16_t num_addrs = ntohs (message->num_addrs); 383 uint16_t num_addrs = ntohs(message->num_addrs);
392 const char *off = (const char *) &message[1]; 384 const char *off = (const char *)&message[1];
393 size_t left = ntohs (message->header.size) - sizeof (*message); 385 size_t left = ntohs(message->header.size) - sizeof(*message);
394
395 for (unsigned int i=0;i<num_addrs;i++)
396 {
397 size_t alen;
398 const struct sockaddr *sa = (const struct sockaddr *) off;
399 386
400 if (sizeof (sa_family_t) > left) 387 for (unsigned int i = 0; i < num_addrs; i++)
401 { 388 {
402 GNUNET_break (0); 389 size_t alen;
403 return GNUNET_SYSERR; 390 const struct sockaddr *sa = (const struct sockaddr *)off;
404 } 391
405 switch (sa->sa_family) 392 if (sizeof(sa_family_t) > left)
406 { 393 {
407 case AF_INET: 394 GNUNET_break(0);
408 alen = sizeof (struct sockaddr_in); 395 return GNUNET_SYSERR;
409 break; 396 }
410 case AF_INET6: 397 switch (sa->sa_family)
411 alen = sizeof (struct sockaddr_in6); 398 {
412 break; 399 case AF_INET:
400 alen = sizeof(struct sockaddr_in);
401 break;
402
403 case AF_INET6:
404 alen = sizeof(struct sockaddr_in6);
405 break;
406
413#if AF_UNIX 407#if AF_UNIX
414 case AF_UNIX: 408 case AF_UNIX:
415 alen = sizeof (struct sockaddr_un); 409 alen = sizeof(struct sockaddr_un);
416 break; 410 break;
417#endif 411#endif
418 default: 412 default:
419 GNUNET_break (0); 413 GNUNET_break(0);
420 return GNUNET_SYSERR; 414 return GNUNET_SYSERR;
415 }
416 if (alen > left)
417 {
418 GNUNET_break(0);
419 return GNUNET_SYSERR;
420 }
421 off += alen;
422 left -= alen;
421 } 423 }
422 if (alen > left) 424 if (left != ntohs(message->str_len))
423 { 425 {
424 GNUNET_break (0); 426 GNUNET_break(0);
425 return GNUNET_SYSERR; 427 return GNUNET_SYSERR;
426 } 428 }
427 off += alen;
428 left -= alen;
429 }
430 if (left != ntohs (message->str_len))
431 {
432 GNUNET_break (0);
433 return GNUNET_SYSERR;
434 }
435 return GNUNET_OK; 429 return GNUNET_OK;
436} 430}
437 431
@@ -445,9 +439,9 @@ check_register (void *cls,
445 * @return #GNUNET_YES if @a ip is in @a network 439 * @return #GNUNET_YES if @a ip is in @a network
446 */ 440 */
447static int 441static int
448match_ipv4 (const char *network, 442match_ipv4(const char *network,
449 const struct in_addr *ip, 443 const struct in_addr *ip,
450 uint8_t bits) 444 uint8_t bits)
451{ 445{
452 struct in_addr net; 446 struct in_addr net;
453 447
@@ -455,10 +449,10 @@ match_ipv4 (const char *network,
455 return GNUNET_YES; 449 return GNUNET_YES;
456 if (0 == bits) 450 if (0 == bits)
457 return GNUNET_YES; 451 return GNUNET_YES;
458 GNUNET_assert (1 == inet_pton (AF_INET, 452 GNUNET_assert(1 == inet_pton(AF_INET,
459 network, 453 network,
460 &net)); 454 &net));
461 return ! ((ip->s_addr ^ net.s_addr) & htonl (0xFFFFFFFFu << (32 - bits))); 455 return !((ip->s_addr ^ net.s_addr) & htonl(0xFFFFFFFFu << (32 - bits)));
462} 456}
463 457
464 458
@@ -471,9 +465,9 @@ match_ipv4 (const char *network,
471 * @return #GNUNET_YES if @a ip is in @a network 465 * @return #GNUNET_YES if @a ip is in @a network
472 */ 466 */
473static int 467static int
474match_ipv6 (const char *network, 468match_ipv6(const char *network,
475 const struct in6_addr *ip, 469 const struct in6_addr *ip,
476 uint8_t bits) 470 uint8_t bits)
477{ 471{
478 struct in6_addr net; 472 struct in6_addr net;
479 struct in6_addr mask; 473 struct in6_addr mask;
@@ -481,27 +475,27 @@ match_ipv6 (const char *network,
481 475
482 if (0 == bits) 476 if (0 == bits)
483 return GNUNET_YES; 477 return GNUNET_YES;
484 GNUNET_assert (1 == inet_pton (AF_INET6, 478 GNUNET_assert(1 == inet_pton(AF_INET6,
485 network, 479 network,
486 &net)); 480 &net));
487 memset (&mask, 0, sizeof (mask)); 481 memset(&mask, 0, sizeof(mask));
488 if (0 == GNUNET_memcmp (&mask, 482 if (0 == GNUNET_memcmp(&mask,
489 ip)) 483 ip))
490 return GNUNET_YES; 484 return GNUNET_YES;
491 off = 0; 485 off = 0;
492 while (bits > 8) 486 while (bits > 8)
493 { 487 {
494 mask.s6_addr[off++] = 0xFF; 488 mask.s6_addr[off++] = 0xFF;
495 bits -= 8; 489 bits -= 8;
496 } 490 }
497 while (bits > 0) 491 while (bits > 0)
498 { 492 {
499 mask.s6_addr[off] = (mask.s6_addr[off] >> 1) + 0x80; 493 mask.s6_addr[off] = (mask.s6_addr[off] >> 1) + 0x80;
500 bits--; 494 bits--;
501 } 495 }
502 for (unsigned j = 0; j < sizeof (struct in6_addr) / sizeof (uint32_t); j++) 496 for (unsigned j = 0; j < sizeof(struct in6_addr) / sizeof(uint32_t); j++)
503 if (((((uint32_t *) ip)[j] & ((uint32_t *) &mask)[j])) != 497 if (((((uint32_t *)ip)[j] & ((uint32_t *)&mask)[j])) !=
504 (((uint32_t *) &net)[j] & ((int *) &mask)[j])) 498 (((uint32_t *)&net)[j] & ((int *)&mask)[j]))
505 return GNUNET_NO; 499 return GNUNET_NO;
506 return GNUNET_YES; 500 return GNUNET_YES;
507} 501}
@@ -515,14 +509,14 @@ match_ipv6 (const char *network,
515 * @return #GNUNET_YES if @a ip is in a NAT range 509 * @return #GNUNET_YES if @a ip is in a NAT range
516 */ 510 */
517static int 511static int
518is_nat_v4 (const struct in_addr *ip) 512is_nat_v4(const struct in_addr *ip)
519{ 513{
520 return 514 return
521 match_ipv4 ("10.0.0.0", ip, 8) || /* RFC 1918 */ 515 match_ipv4("10.0.0.0", ip, 8) || /* RFC 1918 */
522 match_ipv4 ("100.64.0.0", ip, 10) || /* CG-NAT, RFC 6598 */ 516 match_ipv4("100.64.0.0", ip, 10) || /* CG-NAT, RFC 6598 */
523 match_ipv4 ("192.168.0.0", ip, 12) || /* RFC 1918 */ 517 match_ipv4("192.168.0.0", ip, 12) || /* RFC 1918 */
524 match_ipv4 ("169.254.0.0", ip, 16) || /* AUTO, RFC 3927 */ 518 match_ipv4("169.254.0.0", ip, 16) || /* AUTO, RFC 3927 */
525 match_ipv4 ("172.16.0.0", ip, 16); /* RFC 1918 */ 519 match_ipv4("172.16.0.0", ip, 16); /* RFC 1918 */
526} 520}
527 521
528 522
@@ -534,21 +528,19 @@ is_nat_v4 (const struct in_addr *ip)
534 * @return #GNUNET_YES if @a ip is in a NAT range 528 * @return #GNUNET_YES if @a ip is in a NAT range
535 */ 529 */
536static int 530static int
537is_nat_v6 (const struct in6_addr *ip) 531is_nat_v6(const struct in6_addr *ip)
538{ 532{
539 return 533 return
540 match_ipv6 ("fc00::", ip, 7) || /* RFC 4193 */ 534 match_ipv6("fc00::", ip, 7) || /* RFC 4193 */
541 match_ipv6 ("fec0::", ip, 10) || /* RFC 3879 */ 535 match_ipv6("fec0::", ip, 10) || /* RFC 3879 */
542 match_ipv6 ("fe80::", ip, 10); /* RFC 4291, link-local */ 536 match_ipv6("fe80::", ip, 10); /* RFC 4291, link-local */
543} 537}
544 538
545 539
546/** 540/**
547 * Closure for #ifc_proc. 541 * Closure for #ifc_proc.
548 */ 542 */
549struct IfcProcContext 543struct IfcProcContext {
550{
551
552 /** 544 /**
553 * Head of DLL of local addresses. 545 * Head of DLL of local addresses.
554 */ 546 */
@@ -558,7 +550,6 @@ struct IfcProcContext
558 * Tail of DLL of local addresses. 550 * Tail of DLL of local addresses.
559 */ 551 */
560 struct LocalAddressList *lal_tail; 552 struct LocalAddressList *lal_tail;
561
562}; 553};
563 554
564 555
@@ -576,13 +567,13 @@ struct IfcProcContext
576 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort 567 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
577 */ 568 */
578static int 569static int
579ifc_proc (void *cls, 570ifc_proc(void *cls,
580 const char *name, 571 const char *name,
581 int isDefault, 572 int isDefault,
582 const struct sockaddr *addr, 573 const struct sockaddr *addr,
583 const struct sockaddr *broadcast_addr, 574 const struct sockaddr *broadcast_addr,
584 const struct sockaddr *netmask, 575 const struct sockaddr *netmask,
585 socklen_t addrlen) 576 socklen_t addrlen)
586{ 577{
587 struct IfcProcContext *ifc_ctx = cls; 578 struct IfcProcContext *ifc_ctx = cls;
588 struct LocalAddressList *lal; 579 struct LocalAddressList *lal;
@@ -592,51 +583,53 @@ ifc_proc (void *cls,
592 enum GNUNET_NAT_AddressClass ac; 583 enum GNUNET_NAT_AddressClass ac;
593 584
594 switch (addr->sa_family) 585 switch (addr->sa_family)
595 {
596 case AF_INET:
597 alen = sizeof (struct sockaddr_in);
598 ip4 = &((const struct sockaddr_in *) addr)->sin_addr;
599 if (match_ipv4 ("127.0.0.0", ip4, 8))
600 ac = GNUNET_NAT_AC_LOOPBACK;
601 else if (is_nat_v4 (ip4))
602 ac = GNUNET_NAT_AC_LAN;
603 else
604 ac = GNUNET_NAT_AC_GLOBAL;
605 break;
606 case AF_INET6:
607 alen = sizeof (struct sockaddr_in6);
608 ip6 = &((const struct sockaddr_in6 *) addr)->sin6_addr;
609 if (match_ipv6 ("::1", ip6, 128))
610 ac = GNUNET_NAT_AC_LOOPBACK;
611 else if (is_nat_v6 (ip6))
612 ac = GNUNET_NAT_AC_LAN;
613 else
614 ac = GNUNET_NAT_AC_GLOBAL;
615 if ( (ip6->s6_addr[11] == 0xFF) &&
616 (ip6->s6_addr[12] == 0xFE) )
617 { 586 {
618 /* contains a MAC, be extra careful! */ 587 case AF_INET:
619 ac |= GNUNET_NAT_AC_PRIVATE; 588 alen = sizeof(struct sockaddr_in);
620 } 589 ip4 = &((const struct sockaddr_in *)addr)->sin_addr;
621 break; 590 if (match_ipv4("127.0.0.0", ip4, 8))
591 ac = GNUNET_NAT_AC_LOOPBACK;
592 else if (is_nat_v4(ip4))
593 ac = GNUNET_NAT_AC_LAN;
594 else
595 ac = GNUNET_NAT_AC_GLOBAL;
596 break;
597
598 case AF_INET6:
599 alen = sizeof(struct sockaddr_in6);
600 ip6 = &((const struct sockaddr_in6 *)addr)->sin6_addr;
601 if (match_ipv6("::1", ip6, 128))
602 ac = GNUNET_NAT_AC_LOOPBACK;
603 else if (is_nat_v6(ip6))
604 ac = GNUNET_NAT_AC_LAN;
605 else
606 ac = GNUNET_NAT_AC_GLOBAL;
607 if ((ip6->s6_addr[11] == 0xFF) &&
608 (ip6->s6_addr[12] == 0xFE))
609 {
610 /* contains a MAC, be extra careful! */
611 ac |= GNUNET_NAT_AC_PRIVATE;
612 }
613 break;
614
622#if AF_UNIX 615#if AF_UNIX
623 case AF_UNIX: 616 case AF_UNIX:
624 GNUNET_break (0); 617 GNUNET_break(0);
625 return GNUNET_OK; 618 return GNUNET_OK;
626#endif 619#endif
627 default: 620 default:
628 GNUNET_break (0); 621 GNUNET_break(0);
629 return GNUNET_OK; 622 return GNUNET_OK;
630 } 623 }
631 lal = GNUNET_malloc (sizeof (*lal)); 624 lal = GNUNET_malloc(sizeof(*lal));
632 lal->af = addr->sa_family; 625 lal->af = addr->sa_family;
633 lal->ac = ac; 626 lal->ac = ac;
634 GNUNET_memcpy (&lal->addr, 627 GNUNET_memcpy(&lal->addr,
635 addr, 628 addr,
636 alen); 629 alen);
637 GNUNET_CONTAINER_DLL_insert (ifc_ctx->lal_head, 630 GNUNET_CONTAINER_DLL_insert(ifc_ctx->lal_head,
638 ifc_ctx->lal_tail, 631 ifc_ctx->lal_tail,
639 lal); 632 lal);
640 return GNUNET_OK; 633 return GNUNET_OK;
641} 634}
642 635
@@ -652,30 +645,30 @@ ifc_proc (void *cls,
652 * @param addr_len number of bytes in @a addr 645 * @param addr_len number of bytes in @a addr
653 */ 646 */
654static void 647static void
655notify_client (enum GNUNET_NAT_AddressClass ac, 648notify_client(enum GNUNET_NAT_AddressClass ac,
656 struct ClientHandle *ch, 649 struct ClientHandle *ch,
657 int add, 650 int add,
658 const void *addr, 651 const void *addr,
659 size_t addr_len) 652 size_t addr_len)
660{ 653{
661 struct GNUNET_MQ_Envelope *env; 654 struct GNUNET_MQ_Envelope *env;
662 struct GNUNET_NAT_AddressChangeNotificationMessage *msg; 655 struct GNUNET_NAT_AddressChangeNotificationMessage *msg;
663 656
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 657 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
665 "Notifying client about %s of IP %s\n", 658 "Notifying client about %s of IP %s\n",
666 add ? "addition" : "removal", 659 add ? "addition" : "removal",
667 GNUNET_a2s (addr, 660 GNUNET_a2s(addr,
668 addr_len)); 661 addr_len));
669 env = GNUNET_MQ_msg_extra (msg, 662 env = GNUNET_MQ_msg_extra(msg,
670 addr_len, 663 addr_len,
671 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE); 664 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE);
672 msg->add_remove = htonl (add); 665 msg->add_remove = htonl(add);
673 msg->addr_class = htonl (ac); 666 msg->addr_class = htonl(ac);
674 GNUNET_memcpy (&msg[1], 667 GNUNET_memcpy(&msg[1],
675 addr, 668 addr,
676 addr_len); 669 addr_len);
677 GNUNET_MQ_send (ch->mq, 670 GNUNET_MQ_send(ch->mq,
678 env); 671 env);
679} 672}
680 673
681 674
@@ -688,121 +681,123 @@ notify_client (enum GNUNET_NAT_AddressClass ac,
688 * @param add #GNUNET_YES to add, #GNUNET_NO to remove 681 * @param add #GNUNET_YES to add, #GNUNET_NO to remove
689 */ 682 */
690static void 683static void
691check_notify_client (struct LocalAddressList *delta, 684check_notify_client(struct LocalAddressList *delta,
692 struct ClientHandle *ch, 685 struct ClientHandle *ch,
693 int add) 686 int add)
694{ 687{
695 size_t alen; 688 size_t alen;
696 struct sockaddr_in v4; 689 struct sockaddr_in v4;
697 struct sockaddr_in6 v6; 690 struct sockaddr_in6 v6;
698 691
699 if (0 == (ch->flags & GNUNET_NAT_RF_ADDRESSES)) 692 if (0 == (ch->flags & GNUNET_NAT_RF_ADDRESSES))
700 {
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "Not notifying client as it does not care about addresses\n");
703 return;
704 }
705 switch (delta->af)
706 {
707 case AF_INET:
708 alen = sizeof (struct sockaddr_in);
709 GNUNET_memcpy (&v4,
710 &delta->addr,
711 alen);
712
713 /* Check for client notifications */
714 for (unsigned int i=0;i<ch->num_caddrs;i++)
715 { 693 {
716 const struct sockaddr_in *c4; 694 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
717 695 "Not notifying client as it does not care about addresses\n");
718 if (AF_INET != ch->caddrs[i].ss.ss_family) 696 return;
719 continue; /* IPv4 not relevant */
720 c4 = (const struct sockaddr_in *) &ch->caddrs[i].ss;
721 if ( match_ipv4 ("127.0.0.1", &c4->sin_addr, 8) &&
722 (0 != c4->sin_addr.s_addr) &&
723 (! match_ipv4 ("127.0.0.1", &v4.sin_addr, 8)) )
724 continue; /* bound to loopback, but this is not loopback */
725 if ( (! match_ipv4 ("127.0.0.1", &c4->sin_addr, 8) ) &&
726 match_ipv4 ("127.0.0.1", &v4.sin_addr, 8) )
727 continue; /* bound to non-loopback, but this is loopback */
728 if ( (0 != (delta->ac & GNUNET_NAT_AC_EXTERN)) &&
729 (0 != c4->sin_addr.s_addr) &&
730 (! is_nat_v4 (&v4.sin_addr)) )
731 continue; /* based on external-IP, but this IP is not
732 from private address range. */
733 if ( (0 != GNUNET_memcmp (&v4.sin_addr,
734 &c4->sin_addr)) &&
735 (0 != c4->sin_addr.s_addr) &&
736 (! is_nat_v4 (&c4->sin_addr)) )
737 continue; /* this IP is not from private address range,
738 and IP does not match. */
739
740 /* OK, IP seems relevant, notify client */
741 if (0 == htons (v4.sin_port))
742 v4.sin_port = c4->sin_port;
743 notify_client (delta->ac,
744 ch,
745 add,
746 &v4,
747 alen);
748 } 697 }
749 break; 698 switch (delta->af)
750 case AF_INET6:
751 alen = sizeof (struct sockaddr_in6);
752 GNUNET_memcpy (&v6,
753 &delta->addr,
754 alen);
755 for (unsigned int i=0;i<ch->num_caddrs;i++)
756 { 699 {
757 const struct sockaddr_in6 *c6; 700 case AF_INET:
758 701 alen = sizeof(struct sockaddr_in);
759 if (AF_INET6 != ch->caddrs[i].ss.ss_family) 702 GNUNET_memcpy(&v4,
760 continue; /* IPv4 not relevant */ 703 &delta->addr,
761 c6 = (const struct sockaddr_in6 *) &ch->caddrs[i].ss; 704 alen);
762 if ( match_ipv6 ("::1", &c6->sin6_addr, 128) && 705
763 (0 != GNUNET_memcmp (&c6->sin6_addr, 706 /* Check for client notifications */
764 &in6addr_any)) && 707 for (unsigned int i = 0; i < ch->num_caddrs; i++)
765 (! match_ipv6 ("::1", &v6.sin6_addr, 128)) ) 708 {
766 continue; /* bound to loopback, but this is not loopback */ 709 const struct sockaddr_in *c4;
767 if ( (! match_ipv6 ("::1", &c6->sin6_addr, 128) ) && 710
768 match_ipv6 ("::1", &v6.sin6_addr, 128) ) 711 if (AF_INET != ch->caddrs[i].ss.ss_family)
769 continue; /* bound to non-loopback, but this is loopback */ 712 continue; /* IPv4 not relevant */
770 if ( (0 != (delta->ac & GNUNET_NAT_AC_EXTERN)) && 713 c4 = (const struct sockaddr_in *)&ch->caddrs[i].ss;
771 (0 != GNUNET_memcmp (&c6->sin6_addr, 714 if (match_ipv4("127.0.0.1", &c4->sin_addr, 8) &&
772 &in6addr_any)) && 715 (0 != c4->sin_addr.s_addr) &&
773 (! is_nat_v6 (&v6.sin6_addr)) ) 716 (!match_ipv4("127.0.0.1", &v4.sin_addr, 8)))
774 continue; /* based on external-IP, but this IP is not 717 continue; /* bound to loopback, but this is not loopback */
775 from private address range. */ 718 if ((!match_ipv4("127.0.0.1", &c4->sin_addr, 8)) &&
776 if ( (0 != GNUNET_memcmp (&v6.sin6_addr, 719 match_ipv4("127.0.0.1", &v4.sin_addr, 8))
777 &c6->sin6_addr)) && 720 continue; /* bound to non-loopback, but this is loopback */
778 (0 != GNUNET_memcmp (&c6->sin6_addr, 721 if ((0 != (delta->ac & GNUNET_NAT_AC_EXTERN)) &&
779 &in6addr_any)) && 722 (0 != c4->sin_addr.s_addr) &&
780 (! is_nat_v6 (&c6->sin6_addr)) ) 723 (!is_nat_v4(&v4.sin_addr)))
781 continue; /* this IP is not from private address range, 724 continue; /* based on external-IP, but this IP is not
782 and IP does not match. */ 725 from private address range. */
783 if ( (match_ipv6 ("fe80::", &c6->sin6_addr, 10)) && 726 if ((0 != GNUNET_memcmp(&v4.sin_addr,
784 (0 != GNUNET_memcmp (&c6->sin6_addr, 727 &c4->sin_addr)) &&
785 &in6addr_any)) && 728 (0 != c4->sin_addr.s_addr) &&
786 (0 != GNUNET_memcmp (&v6.sin6_addr, 729 (!is_nat_v4(&c4->sin_addr)))
787 &c6->sin6_addr)) && 730 continue; /* this IP is not from private address range,
788 (0 == (delta->ac & GNUNET_NAT_AC_EXTERN)) ) 731 and IP does not match. */
789 continue; /* client bound to link-local, and the other address 732
790 does not match and is not an external IP */ 733 /* OK, IP seems relevant, notify client */
791 734 if (0 == htons(v4.sin_port))
792 /* OK, IP seems relevant, notify client */ 735 v4.sin_port = c4->sin_port;
793 if (0 == htons (v6.sin6_port)) 736 notify_client(delta->ac,
794 v6.sin6_port = c6->sin6_port; 737 ch,
795 notify_client (delta->ac, 738 add,
796 ch, 739 &v4,
797 add, 740 alen);
798 &v6, 741 }
799 alen); 742 break;
743
744 case AF_INET6:
745 alen = sizeof(struct sockaddr_in6);
746 GNUNET_memcpy(&v6,
747 &delta->addr,
748 alen);
749 for (unsigned int i = 0; i < ch->num_caddrs; i++)
750 {
751 const struct sockaddr_in6 *c6;
752
753 if (AF_INET6 != ch->caddrs[i].ss.ss_family)
754 continue; /* IPv4 not relevant */
755 c6 = (const struct sockaddr_in6 *)&ch->caddrs[i].ss;
756 if (match_ipv6("::1", &c6->sin6_addr, 128) &&
757 (0 != GNUNET_memcmp(&c6->sin6_addr,
758 &in6addr_any)) &&
759 (!match_ipv6("::1", &v6.sin6_addr, 128)))
760 continue; /* bound to loopback, but this is not loopback */
761 if ((!match_ipv6("::1", &c6->sin6_addr, 128)) &&
762 match_ipv6("::1", &v6.sin6_addr, 128))
763 continue; /* bound to non-loopback, but this is loopback */
764 if ((0 != (delta->ac & GNUNET_NAT_AC_EXTERN)) &&
765 (0 != GNUNET_memcmp(&c6->sin6_addr,
766 &in6addr_any)) &&
767 (!is_nat_v6(&v6.sin6_addr)))
768 continue; /* based on external-IP, but this IP is not
769 from private address range. */
770 if ((0 != GNUNET_memcmp(&v6.sin6_addr,
771 &c6->sin6_addr)) &&
772 (0 != GNUNET_memcmp(&c6->sin6_addr,
773 &in6addr_any)) &&
774 (!is_nat_v6(&c6->sin6_addr)))
775 continue; /* this IP is not from private address range,
776 and IP does not match. */
777 if ((match_ipv6("fe80::", &c6->sin6_addr, 10)) &&
778 (0 != GNUNET_memcmp(&c6->sin6_addr,
779 &in6addr_any)) &&
780 (0 != GNUNET_memcmp(&v6.sin6_addr,
781 &c6->sin6_addr)) &&
782 (0 == (delta->ac & GNUNET_NAT_AC_EXTERN)))
783 continue; /* client bound to link-local, and the other address
784 does not match and is not an external IP */
785
786 /* OK, IP seems relevant, notify client */
787 if (0 == htons(v6.sin6_port))
788 v6.sin6_port = c6->sin6_port;
789 notify_client(delta->ac,
790 ch,
791 add,
792 &v6,
793 alen);
794 }
795 break;
796
797 default:
798 GNUNET_break(0);
799 return;
800 } 800 }
801 break;
802 default:
803 GNUNET_break (0);
804 return;
805 }
806} 801}
807 802
808 803
@@ -814,15 +809,15 @@ check_notify_client (struct LocalAddressList *delta,
814 * @param add #GNUNET_YES to add, #GNUNET_NO to remove 809 * @param add #GNUNET_YES to add, #GNUNET_NO to remove
815 */ 810 */
816static void 811static void
817notify_clients (struct LocalAddressList *delta, 812notify_clients(struct LocalAddressList *delta,
818 int add) 813 int add)
819{ 814{
820 for (struct ClientHandle *ch = ch_head; 815 for (struct ClientHandle *ch = ch_head;
821 NULL != ch; 816 NULL != ch;
822 ch = ch->next) 817 ch = ch->next)
823 check_notify_client (delta, 818 check_notify_client(delta,
824 ch, 819 ch,
825 add); 820 add);
826} 821}
827 822
828 823
@@ -835,74 +830,74 @@ notify_clients (struct LocalAddressList *delta,
835 * @param add #GNUNET_YES to add, #GNUNET_NO to remove 830 * @param add #GNUNET_YES to add, #GNUNET_NO to remove
836 */ 831 */
837static void 832static void
838notify_client_external_ipv4_change (void *cls, 833notify_client_external_ipv4_change(void *cls,
839 const struct in_addr *v4, 834 const struct in_addr *v4,
840 int add) 835 int add)
841{ 836{
842 struct ClientHandle *ch = cls; 837 struct ClientHandle *ch = cls;
843 struct sockaddr_in sa; 838 struct sockaddr_in sa;
844 int have_v4; 839 int have_v4;
845 840
846 /* (0) check if this impacts 'hole_external' */ 841 /* (0) check if this impacts 'hole_external' */
847 if ( (NULL != ch->hole_external) && 842 if ((NULL != ch->hole_external) &&
848 (0 == strcasecmp (ch->hole_external, 843 (0 == strcasecmp(ch->hole_external,
849 "AUTO")) ) 844 "AUTO")))
850 { 845 {
851 struct LocalAddressList lal; 846 struct LocalAddressList lal;
852 struct sockaddr_in *s4; 847 struct sockaddr_in *s4;
853 848
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 849 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
855 "Detected eternal IP, can now back-fill AUTO:%u in hole punching specification of `%s'\n", 850 "Detected eternal IP, can now back-fill AUTO:%u in hole punching specification of `%s'\n",
856 (unsigned int) ch->ext_dns_port, 851 (unsigned int)ch->ext_dns_port,
857 ch->section_name); 852 ch->section_name);
858 memset (&lal, 0, sizeof (lal)); 853 memset(&lal, 0, sizeof(lal));
859 s4 = (struct sockaddr_in *) &lal.addr; 854 s4 = (struct sockaddr_in *)&lal.addr;
860 s4->sin_family = AF_INET; 855 s4->sin_family = AF_INET;
861 s4->sin_port = htons (ch->ext_dns_port); 856 s4->sin_port = htons(ch->ext_dns_port);
862 s4->sin_addr = *v4; 857 s4->sin_addr = *v4;
863 lal.af = AF_INET; 858 lal.af = AF_INET;
864 lal.ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL; 859 lal.ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL;
865 check_notify_client (&lal, 860 check_notify_client(&lal,
866 ch, 861 ch,
867 add); 862 add);
868 } 863 }
869 864
870 /* (1) check if client cares. */ 865 /* (1) check if client cares. */
871 if (! ch->natted_address) 866 if (!ch->natted_address)
872 return; 867 return;
873 have_v4 = GNUNET_NO; 868 have_v4 = GNUNET_NO;
874 for (unsigned int i=0;i<ch->num_caddrs;i++) 869 for (unsigned int i = 0; i < ch->num_caddrs; i++)
875 { 870 {
876 const struct sockaddr_storage *ss = &ch->caddrs[i].ss; 871 const struct sockaddr_storage *ss = &ch->caddrs[i].ss;
877 872
878 if (AF_INET != ss->ss_family) 873 if (AF_INET != ss->ss_family)
879 continue; 874 continue;
880 have_v4 = GNUNET_YES; 875 have_v4 = GNUNET_YES;
881 break; 876 break;
882 } 877 }
883 if (GNUNET_NO == have_v4) 878 if (GNUNET_NO == have_v4)
884 return; /* IPv6-only */ 879 return; /* IPv6-only */
885 880
886 /* (2) build address info */ 881 /* (2) build address info */
887 memset (&sa, 882 memset(&sa,
888 0, 883 0,
889 sizeof (sa)); 884 sizeof(sa));
890 sa.sin_family = AF_INET; 885 sa.sin_family = AF_INET;
891 sa.sin_addr = *v4; 886 sa.sin_addr = *v4;
892 sa.sin_port = htons (0); 887 sa.sin_port = htons(0);
893 888
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 889 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
895 "Detected eternal IP %s, notifying client of external IP (without port)\n", 890 "Detected eternal IP %s, notifying client of external IP (without port)\n",
896 GNUNET_a2s ((const struct sockaddr *) &sa, 891 GNUNET_a2s((const struct sockaddr *)&sa,
897 sizeof (sa))); 892 sizeof(sa)));
898 /* (3) notify client of change */ 893 /* (3) notify client of change */
899 notify_client (is_nat_v4 (v4) 894 notify_client(is_nat_v4(v4)
900 ? GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_LAN 895 ? GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_LAN
901 : GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_GLOBAL, 896 : GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_GLOBAL,
902 ch, 897 ch,
903 add, 898 add,
904 &sa, 899 &sa,
905 sizeof (sa)); 900 sizeof(sa));
906} 901}
907 902
908 903
@@ -914,54 +909,54 @@ notify_client_external_ipv4_change (void *cls,
914 * @param ra IP address of the peer who wants us to connect to it 909 * @param ra IP address of the peer who wants us to connect to it
915 */ 910 */
916static void 911static void
917reversal_callback (void *cls, 912reversal_callback(void *cls,
918 const struct sockaddr_in *ra) 913 const struct sockaddr_in *ra)
919{ 914{
920 struct LocalAddressList *lal = cls; 915 struct LocalAddressList *lal = cls;
921 const struct sockaddr_in *l4; 916 const struct sockaddr_in *l4;
922 917
923 GNUNET_assert (AF_INET == lal->af); 918 GNUNET_assert(AF_INET == lal->af);
924 l4 = (const struct sockaddr_in *) &lal->addr; 919 l4 = (const struct sockaddr_in *)&lal->addr;
925 for (struct ClientHandle *ch = ch_head; 920 for (struct ClientHandle *ch = ch_head;
926 NULL != ch; 921 NULL != ch;
927 ch = ch->next) 922 ch = ch->next)
928 {
929 struct GNUNET_NAT_ConnectionReversalRequestedMessage *crrm;
930 struct GNUNET_MQ_Envelope *env;
931 int match;
932
933 /* Check if client is in applicable range for ICMP NAT traversal
934 for this local address */
935 if (! ch->natted_address)
936 continue;
937 match = GNUNET_NO;
938 for (unsigned int i=0;i<ch->num_caddrs;i++)
939 { 923 {
940 struct ClientAddress *ca = &ch->caddrs[i]; 924 struct GNUNET_NAT_ConnectionReversalRequestedMessage *crrm;
941 const struct sockaddr_in *c4; 925 struct GNUNET_MQ_Envelope *env;
942 926 int match;
943 if (AF_INET != ca->ss.ss_family) 927
944 continue; 928 /* Check if client is in applicable range for ICMP NAT traversal
945 c4 = (const struct sockaddr_in *) &ca->ss; 929 for this local address */
946 if ( (0 != c4->sin_addr.s_addr) && 930 if (!ch->natted_address)
947 (l4->sin_addr.s_addr != c4->sin_addr.s_addr) ) 931 continue;
948 continue; 932 match = GNUNET_NO;
949 match = GNUNET_YES; 933 for (unsigned int i = 0; i < ch->num_caddrs; i++)
950 break; 934 {
935 struct ClientAddress *ca = &ch->caddrs[i];
936 const struct sockaddr_in *c4;
937
938 if (AF_INET != ca->ss.ss_family)
939 continue;
940 c4 = (const struct sockaddr_in *)&ca->ss;
941 if ((0 != c4->sin_addr.s_addr) &&
942 (l4->sin_addr.s_addr != c4->sin_addr.s_addr))
943 continue;
944 match = GNUNET_YES;
945 break;
946 }
947 if (!match)
948 continue;
949
950 /* Notify applicable client about connection reversal request */
951 env = GNUNET_MQ_msg_extra(crrm,
952 sizeof(struct sockaddr_in),
953 GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED);
954 GNUNET_memcpy(&crrm[1],
955 ra,
956 sizeof(struct sockaddr_in));
957 GNUNET_MQ_send(ch->mq,
958 env);
951 } 959 }
952 if (! match)
953 continue;
954
955 /* Notify applicable client about connection reversal request */
956 env = GNUNET_MQ_msg_extra (crrm,
957 sizeof (struct sockaddr_in),
958 GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED);
959 GNUNET_memcpy (&crrm[1],
960 ra,
961 sizeof (struct sockaddr_in));
962 GNUNET_MQ_send (ch->mq,
963 env);
964 }
965} 960}
966 961
967 962
@@ -971,104 +966,104 @@ reversal_callback (void *cls,
971 * @param cls NULL 966 * @param cls NULL
972 */ 967 */
973static void 968static void
974run_scan (void *cls) 969run_scan(void *cls)
975{ 970{
976 struct IfcProcContext ifc_ctx; 971 struct IfcProcContext ifc_ctx;
977 int found; 972 int found;
978 int have_nat; 973 int have_nat;
979 struct LocalAddressList *lnext; 974 struct LocalAddressList *lnext;
980 975
981 scan_task = GNUNET_SCHEDULER_add_delayed (SCAN_FREQ, 976 scan_task = GNUNET_SCHEDULER_add_delayed(SCAN_FREQ,
982 &run_scan, 977 &run_scan,
983 NULL); 978 NULL);
984 memset (&ifc_ctx, 979 memset(&ifc_ctx,
985 0, 980 0,
986 sizeof (ifc_ctx)); 981 sizeof(ifc_ctx));
987 GNUNET_OS_network_interfaces_list (&ifc_proc, 982 GNUNET_OS_network_interfaces_list(&ifc_proc,
988 &ifc_ctx); 983 &ifc_ctx);
989 /* remove addresses that disappeared */ 984 /* remove addresses that disappeared */
990 for (struct LocalAddressList *lal = lal_head; 985 for (struct LocalAddressList *lal = lal_head;
991 NULL != lal; 986 NULL != lal;
992 lal = lnext) 987 lal = lnext)
993 {
994 lnext = lal->next;
995 found = GNUNET_NO;
996 for (struct LocalAddressList *pos = ifc_ctx.lal_head;
997 NULL != pos;
998 pos = pos->next)
999 { 988 {
1000 if ( (pos->af == lal->af) && 989 lnext = lal->next;
1001 (0 == memcmp (&lal->addr, 990 found = GNUNET_NO;
1002 &pos->addr, 991 for (struct LocalAddressList *pos = ifc_ctx.lal_head;
1003 (AF_INET == lal->af) 992 NULL != pos;
1004 ? sizeof (struct sockaddr_in) 993 pos = pos->next)
1005 : sizeof (struct sockaddr_in6))) ) 994 {
1006 { 995 if ((pos->af == lal->af) &&
1007 found = GNUNET_YES; 996 (0 == memcmp(&lal->addr,
1008 } 997 &pos->addr,
998 (AF_INET == lal->af)
999 ? sizeof(struct sockaddr_in)
1000 : sizeof(struct sockaddr_in6))))
1001 {
1002 found = GNUNET_YES;
1003 }
1004 }
1005 if (GNUNET_NO == found)
1006 {
1007 notify_clients(lal,
1008 GNUNET_NO);
1009 free_lal(lal);
1010 }
1009 } 1011 }
1010 if (GNUNET_NO == found)
1011 {
1012 notify_clients (lal,
1013 GNUNET_NO);
1014 free_lal (lal);
1015 }
1016 }
1017 1012
1018 /* add addresses that appeared */ 1013 /* add addresses that appeared */
1019 have_nat = GNUNET_NO; 1014 have_nat = GNUNET_NO;
1020 for (struct LocalAddressList *pos = ifc_ctx.lal_head; 1015 for (struct LocalAddressList *pos = ifc_ctx.lal_head;
1021 NULL != pos; 1016 NULL != pos;
1022 pos = ifc_ctx.lal_head) 1017 pos = ifc_ctx.lal_head)
1023 {
1024 found = GNUNET_NO;
1025 if (GNUNET_NAT_AC_LAN == (GNUNET_NAT_AC_LAN & pos->ac))
1026 have_nat = GNUNET_YES;
1027 for (struct LocalAddressList *lal = lal_head;
1028 NULL != lal;
1029 lal = lal->next)
1030 {
1031 if ( (pos->af == lal->af) &&
1032 (0 == memcmp (&lal->addr,
1033 &pos->addr,
1034 (AF_INET == lal->af)
1035 ? sizeof (struct sockaddr_in)
1036 : sizeof (struct sockaddr_in6))) )
1037 found = GNUNET_YES;
1038 }
1039 GNUNET_CONTAINER_DLL_remove (ifc_ctx.lal_head,
1040 ifc_ctx.lal_tail,
1041 pos);
1042 if (GNUNET_YES == found)
1043 {
1044 GNUNET_free (pos);
1045 }
1046 else
1047 { 1018 {
1048 notify_clients (pos, 1019 found = GNUNET_NO;
1049 GNUNET_YES); 1020 if (GNUNET_NAT_AC_LAN == (GNUNET_NAT_AC_LAN & pos->ac))
1050 GNUNET_CONTAINER_DLL_insert (lal_head, 1021 have_nat = GNUNET_YES;
1051 lal_tail, 1022 for (struct LocalAddressList *lal = lal_head;
1052 pos); 1023 NULL != lal;
1053 if ( (AF_INET == pos->af) && 1024 lal = lal->next)
1054 (NULL == pos->hc) && 1025 {
1055 (0 != (GNUNET_NAT_AC_LAN & pos->ac)) ) 1026 if ((pos->af == lal->af) &&
1056 { 1027 (0 == memcmp(&lal->addr,
1057 const struct sockaddr_in *s4 1028 &pos->addr,
1058 = (const struct sockaddr_in *) &pos->addr; 1029 (AF_INET == lal->af)
1059 1030 ? sizeof(struct sockaddr_in)
1060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1031 : sizeof(struct sockaddr_in6))))
1061 "Found NATed local address %s, starting NAT server\n", 1032 found = GNUNET_YES;
1062 GNUNET_a2s ((const struct sockaddr *) &pos->addr, 1033 }
1063 sizeof (*s4))); 1034 GNUNET_CONTAINER_DLL_remove(ifc_ctx.lal_head,
1064 pos->hc = GN_start_gnunet_nat_server_ (&s4->sin_addr, 1035 ifc_ctx.lal_tail,
1065 &reversal_callback, 1036 pos);
1066 pos, 1037 if (GNUNET_YES == found)
1067 cfg); 1038 {
1068 } 1039 GNUNET_free(pos);
1040 }
1041 else
1042 {
1043 notify_clients(pos,
1044 GNUNET_YES);
1045 GNUNET_CONTAINER_DLL_insert(lal_head,
1046 lal_tail,
1047 pos);
1048 if ((AF_INET == pos->af) &&
1049 (NULL == pos->hc) &&
1050 (0 != (GNUNET_NAT_AC_LAN & pos->ac)))
1051 {
1052 const struct sockaddr_in *s4
1053 = (const struct sockaddr_in *)&pos->addr;
1054
1055 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1056 "Found NATed local address %s, starting NAT server\n",
1057 GNUNET_a2s((const struct sockaddr *)&pos->addr,
1058 sizeof(*s4)));
1059 pos->hc = GN_start_gnunet_nat_server_(&s4->sin_addr,
1060 &reversal_callback,
1061 pos,
1062 cfg);
1063 }
1064 }
1069 } 1065 }
1070 } 1066 GN_nat_status_changed(have_nat);
1071 GN_nat_status_changed (have_nat);
1072} 1067}
1073 1068
1074 1069
@@ -1084,81 +1079,91 @@ run_scan (void *cls)
1084 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code 1079 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
1085 */ 1080 */
1086static void 1081static void
1087upnp_addr_change_cb (void *cls, 1082upnp_addr_change_cb(void *cls,
1088 int add_remove, 1083 int add_remove,
1089 const struct sockaddr *addr, 1084 const struct sockaddr *addr,
1090 socklen_t addrlen, 1085 socklen_t addrlen,
1091 enum GNUNET_NAT_StatusCode result) 1086 enum GNUNET_NAT_StatusCode result)
1092{ 1087{
1093 struct ClientHandle *ch = cls; 1088 struct ClientHandle *ch = cls;
1094 enum GNUNET_NAT_AddressClass ac; 1089 enum GNUNET_NAT_AddressClass ac;
1095 1090
1096 switch (result) 1091 switch (result)
1097 { 1092 {
1098 case GNUNET_NAT_ERROR_SUCCESS: 1093 case GNUNET_NAT_ERROR_SUCCESS:
1099 GNUNET_assert (NULL != addr); 1094 GNUNET_assert(NULL != addr);
1100 break; 1095 break;
1101 case GNUNET_NAT_ERROR_UPNPC_FAILED: 1096
1102 case GNUNET_NAT_ERROR_UPNPC_TIMEOUT: 1097 case GNUNET_NAT_ERROR_UPNPC_FAILED:
1103 case GNUNET_NAT_ERROR_IPC_FAILURE: 1098 case GNUNET_NAT_ERROR_UPNPC_TIMEOUT:
1104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1099 case GNUNET_NAT_ERROR_IPC_FAILURE:
1105 "Running upnpc failed: %d\n", 1100 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1106 result); 1101 "Running upnpc failed: %d\n",
1107 return; 1102 result);
1108 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND: 1103 return;
1109 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1104
1110 "external-ip binary not found\n"); 1105 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND:
1111 return; 1106 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1112 case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND: 1107 "external-ip binary not found\n");
1113 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1108 return;
1114 "upnpc binary not found\n"); 1109
1115 return; 1110 case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND:
1116 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED: 1111 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1117 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1112 "upnpc binary not found\n");
1118 "external-ip binary could not be run\n"); 1113 return;
1119 return; 1114
1120 case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED: 1115 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED:
1121 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1116 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1122 "upnpc failed to create port mapping\n"); 1117 "external-ip binary could not be run\n");
1123 return; 1118 return;
1124 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID: 1119
1125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1120 case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED:
1126 "Invalid output from upnpc\n"); 1121 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1127 return; 1122 "upnpc failed to create port mapping\n");
1128 case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID: 1123 return;
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1124
1130 "Invalid address returned by upnpc\n"); 1125 case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID:
1131 return; 1126 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1132 default: 1127 "Invalid output from upnpc\n");
1133 GNUNET_break (0); /* should not be possible */ 1128 return;
1134 return; 1129
1135 } 1130 case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID:
1131 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1132 "Invalid address returned by upnpc\n");
1133 return;
1134
1135 default:
1136 GNUNET_break(0); /* should not be possible */
1137 return;
1138 }
1136 switch (addr->sa_family) 1139 switch (addr->sa_family)
1137 { 1140 {
1138 case AF_INET: 1141 case AF_INET:
1139 ac = is_nat_v4 (&((const struct sockaddr_in *) addr)->sin_addr) 1142 ac = is_nat_v4(&((const struct sockaddr_in *)addr)->sin_addr)
1140 ? GNUNET_NAT_AC_LAN 1143 ? GNUNET_NAT_AC_LAN
1141 : GNUNET_NAT_AC_EXTERN; 1144 : GNUNET_NAT_AC_EXTERN;
1142 break; 1145 break;
1143 case AF_INET6: 1146
1144 ac = is_nat_v6 (&((const struct sockaddr_in6 *) addr)->sin6_addr) 1147 case AF_INET6:
1145 ? GNUNET_NAT_AC_LAN 1148 ac = is_nat_v6(&((const struct sockaddr_in6 *)addr)->sin6_addr)
1146 : GNUNET_NAT_AC_EXTERN; 1149 ? GNUNET_NAT_AC_LAN
1147 break; 1150 : GNUNET_NAT_AC_EXTERN;
1148 default: 1151 break;
1149 GNUNET_break (0); 1152
1150 return; 1153 default:
1151 } 1154 GNUNET_break(0);
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1155 return;
1153 "upnp external address %s: %s\n", 1156 }
1154 add_remove ? "added" : "removed", 1157 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1155 GNUNET_a2s (addr, 1158 "upnp external address %s: %s\n",
1156 addrlen)); 1159 add_remove ? "added" : "removed",
1157 notify_client (ac, 1160 GNUNET_a2s(addr,
1158 ch, 1161 addrlen));
1159 add_remove, 1162 notify_client(ac,
1160 addr, 1163 ch,
1161 addrlen); 1164 add_remove,
1165 addr,
1166 addrlen);
1162} 1167}
1163 1168
1164 1169
@@ -1171,7 +1176,7 @@ upnp_addr_change_cb (void *cls,
1171 * @param ch client handle to act upon 1176 * @param ch client handle to act upon
1172 */ 1177 */
1173static void 1178static void
1174dyndns_lookup (void *cls); 1179dyndns_lookup(void *cls);
1175 1180
1176 1181
1177/** 1182/**
@@ -1184,9 +1189,9 @@ dyndns_lookup (void *cls);
1184 * @param addrlen number of bytes in @a addr 1189 * @param addrlen number of bytes in @a addr
1185 */ 1190 */
1186static void 1191static void
1187process_external_ip (void *cls, 1192process_external_ip(void *cls,
1188 const struct sockaddr *addr, 1193 const struct sockaddr *addr,
1189 socklen_t addrlen) 1194 socklen_t addrlen)
1190{ 1195{
1191 struct ClientHandle *ch = cls; 1196 struct ClientHandle *ch = cls;
1192 struct LocalAddressList *lal; 1197 struct LocalAddressList *lal;
@@ -1195,84 +1200,86 @@ process_external_ip (void *cls,
1195 struct sockaddr_in6 *v6; 1200 struct sockaddr_in6 *v6;
1196 1201
1197 if (NULL == addr) 1202 if (NULL == addr)
1198 {
1199 struct LocalAddressList *laln;
1200
1201 ch->ext_dns = NULL;
1202 ch->ext_dns_task
1203 = GNUNET_SCHEDULER_add_delayed (dyndns_frequency,
1204 &dyndns_lookup,
1205 ch);
1206 /* Current iteration is over, remove 'old' IPs now */
1207 for (lal = ch->ext_addr_head; NULL != lal; lal = laln)
1208 { 1203 {
1209 laln = lal->next; 1204 struct LocalAddressList *laln;
1210 if (GNUNET_YES == lal->old) 1205
1211 { 1206 ch->ext_dns = NULL;
1212 GNUNET_CONTAINER_DLL_remove (ch->ext_addr_head, 1207 ch->ext_dns_task
1213 ch->ext_addr_tail, 1208 = GNUNET_SCHEDULER_add_delayed(dyndns_frequency,
1214 lal); 1209 &dyndns_lookup,
1215 check_notify_client (lal, 1210 ch);
1216 ch, 1211 /* Current iteration is over, remove 'old' IPs now */
1217 GNUNET_NO); 1212 for (lal = ch->ext_addr_head; NULL != lal; lal = laln)
1218 GNUNET_free (lal); 1213 {
1219 } 1214 laln = lal->next;
1215 if (GNUNET_YES == lal->old)
1216 {
1217 GNUNET_CONTAINER_DLL_remove(ch->ext_addr_head,
1218 ch->ext_addr_tail,
1219 lal);
1220 check_notify_client(lal,
1221 ch,
1222 GNUNET_NO);
1223 GNUNET_free(lal);
1224 }
1225 }
1226 return;
1220 } 1227 }
1221 return; 1228 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1222 } 1229 "Got IP `%s' for external address `%s'\n",
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1230 GNUNET_a2s(addr,
1224 "Got IP `%s' for external address `%s'\n", 1231 addrlen),
1225 GNUNET_a2s (addr, 1232 ch->hole_external);
1226 addrlen),
1227 ch->hole_external);
1228 1233
1229 /* build sockaddr storage with port number */ 1234 /* build sockaddr storage with port number */
1230 memset (&ss, 1235 memset(&ss,
1231 0, 1236 0,
1232 sizeof (ss)); 1237 sizeof(ss));
1233 GNUNET_memcpy (&ss, 1238 GNUNET_memcpy(&ss,
1234 addr, 1239 addr,
1235 addrlen); 1240 addrlen);
1236 switch (addr->sa_family) 1241 switch (addr->sa_family)
1237 { 1242 {
1238 case AF_INET: 1243 case AF_INET:
1239 v4 = (struct sockaddr_in *) &ss; 1244 v4 = (struct sockaddr_in *)&ss;
1240 v4->sin_port = htons (ch->ext_dns_port); 1245 v4->sin_port = htons(ch->ext_dns_port);
1241 break; 1246 break;
1242 case AF_INET6: 1247
1243 v6 = (struct sockaddr_in6 *) &ss; 1248 case AF_INET6:
1244 v6->sin6_port = htons (ch->ext_dns_port); 1249 v6 = (struct sockaddr_in6 *)&ss;
1245 break; 1250 v6->sin6_port = htons(ch->ext_dns_port);
1246 default: 1251 break;
1247 GNUNET_break (0); 1252
1248 return; 1253 default:
1249 } 1254 GNUNET_break(0);
1255 return;
1256 }
1250 /* See if 'ss' matches any of our known addresses */ 1257 /* See if 'ss' matches any of our known addresses */
1251 for (lal = ch->ext_addr_head; NULL != lal; lal = lal->next) 1258 for (lal = ch->ext_addr_head; NULL != lal; lal = lal->next)
1252 {
1253 if (GNUNET_NO == lal->old)
1254 continue; /* already processed, skip */
1255 if ( (addr->sa_family == lal->addr.ss_family) &&
1256 (0 == memcmp (&ss,
1257 &lal->addr,
1258 addrlen)) )
1259 { 1259 {
1260 /* Address unchanged, remember so we do not remove */ 1260 if (GNUNET_NO == lal->old)
1261 lal->old = GNUNET_NO; 1261 continue; /* already processed, skip */
1262 return; /* done here */ 1262 if ((addr->sa_family == lal->addr.ss_family) &&
1263 (0 == memcmp(&ss,
1264 &lal->addr,
1265 addrlen)))
1266 {
1267 /* Address unchanged, remember so we do not remove */
1268 lal->old = GNUNET_NO;
1269 return; /* done here */
1270 }
1263 } 1271 }
1264 }
1265 /* notify client, and remember IP for later removal! */ 1272 /* notify client, and remember IP for later removal! */
1266 lal = GNUNET_new (struct LocalAddressList); 1273 lal = GNUNET_new(struct LocalAddressList);
1267 lal->addr = ss; 1274 lal->addr = ss;
1268 lal->af = ss.ss_family; 1275 lal->af = ss.ss_family;
1269 lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL; 1276 lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL;
1270 GNUNET_CONTAINER_DLL_insert (ch->ext_addr_head, 1277 GNUNET_CONTAINER_DLL_insert(ch->ext_addr_head,
1271 ch->ext_addr_tail, 1278 ch->ext_addr_tail,
1272 lal); 1279 lal);
1273 check_notify_client (lal, 1280 check_notify_client(lal,
1274 ch, 1281 ch,
1275 GNUNET_YES); 1282 GNUNET_YES);
1276} 1283}
1277 1284
1278 1285
@@ -1285,24 +1292,24 @@ process_external_ip (void *cls,
1285 * @param ch client handle to act upon 1292 * @param ch client handle to act upon
1286 */ 1293 */
1287static void 1294static void
1288dyndns_lookup (void *cls) 1295dyndns_lookup(void *cls)
1289{ 1296{
1290 struct ClientHandle *ch = cls; 1297 struct ClientHandle *ch = cls;
1291 struct LocalAddressList *lal; 1298 struct LocalAddressList *lal;
1292 1299
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1300 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1294 "Performing DNS lookup for punched hole given for `%s' as `%s:%u'\n", 1301 "Performing DNS lookup for punched hole given for `%s' as `%s:%u'\n",
1295 ch->section_name, 1302 ch->section_name,
1296 ch->hole_external, 1303 ch->hole_external,
1297 (unsigned int) ch->ext_dns_port); 1304 (unsigned int)ch->ext_dns_port);
1298 for (lal = ch->ext_addr_head; NULL != lal; lal = lal->next) 1305 for (lal = ch->ext_addr_head; NULL != lal; lal = lal->next)
1299 lal->old = GNUNET_YES; 1306 lal->old = GNUNET_YES;
1300 ch->ext_dns_task = NULL; 1307 ch->ext_dns_task = NULL;
1301 ch->ext_dns = GNUNET_RESOLVER_ip_get (ch->hole_external, 1308 ch->ext_dns = GNUNET_RESOLVER_ip_get(ch->hole_external,
1302 AF_UNSPEC, 1309 AF_UNSPEC,
1303 GNUNET_TIME_UNIT_MINUTES, 1310 GNUNET_TIME_UNIT_MINUTES,
1304 &process_external_ip, 1311 &process_external_ip,
1305 ch); 1312 ch);
1306} 1313}
1307 1314
1308 1315
@@ -1318,105 +1325,105 @@ dyndns_lookup (void *cls)
1318 * @param ch client handle to act upon 1325 * @param ch client handle to act upon
1319 */ 1326 */
1320static void 1327static void
1321lookup_hole_external (struct ClientHandle *ch) 1328lookup_hole_external(struct ClientHandle *ch)
1322{ 1329{
1323 char *port; 1330 char *port;
1324 unsigned int pnum; 1331 unsigned int pnum;
1325 struct sockaddr_in *s4; 1332 struct sockaddr_in *s4;
1326 struct LocalAddressList *lal; 1333 struct LocalAddressList *lal;
1327 1334
1328 port = strrchr (ch->hole_external, ':'); 1335 port = strrchr(ch->hole_external, ':');
1329 if (NULL == port) 1336 if (NULL == port)
1330 { 1337 {
1331 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1338 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1332 _("Malformed punched hole specification `%s' (lacks port)\n"), 1339 _("Malformed punched hole specification `%s' (lacks port)\n"),
1333 ch->hole_external); 1340 ch->hole_external);
1334 return; 1341 return;
1335 } 1342 }
1336 if ( (1 != sscanf (port + 1, 1343 if ((1 != sscanf(port + 1,
1337 "%u", 1344 "%u",
1338 &pnum)) || 1345 &pnum)) ||
1339 (pnum > 65535) ) 1346 (pnum > 65535))
1340 { 1347 {
1341 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1348 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1342 _("Invalid port number in punched hole specification `%s' (lacks port)\n"), 1349 _("Invalid port number in punched hole specification `%s' (lacks port)\n"),
1343 port + 1); 1350 port + 1);
1344 return; 1351 return;
1345 } 1352 }
1346 ch->ext_dns_port = (uint16_t) pnum; 1353 ch->ext_dns_port = (uint16_t)pnum;
1347 *port = '\0'; 1354 *port = '\0';
1348 1355
1349 lal = GNUNET_new (struct LocalAddressList); 1356 lal = GNUNET_new(struct LocalAddressList);
1350 if ('[' == *ch->hole_external) 1357 if ('[' == *ch->hole_external)
1351 { 1358 {
1352 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) &lal->addr; 1359 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&lal->addr;
1360
1361 s6->sin6_family = AF_INET6;
1362 if (']' != (ch->hole_external[strlen(ch->hole_external) - 1]))
1363 {
1364 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1365 _("Malformed punched hole specification `%s' (lacks `]')\n"),
1366 ch->hole_external);
1367 GNUNET_free(lal);
1368 return;
1369 }
1370 ch->hole_external[strlen(ch->hole_external) - 1] = '\0';
1371 if (1 != inet_pton(AF_INET6,
1372 ch->hole_external + 1,
1373 &s6->sin6_addr))
1374 {
1375 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1376 _("Malformed punched hole specification `%s' (IPv6 address invalid)"),
1377 ch->hole_external + 1);
1378 GNUNET_free(lal);
1379 return;
1380 }
1381 s6->sin6_port = htons(ch->ext_dns_port);
1382 lal->af = AF_INET6;
1383 lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL;
1384 GNUNET_CONTAINER_DLL_insert(ch->ext_addr_head,
1385 ch->ext_addr_tail,
1386 lal);
1387 check_notify_client(lal,
1388 ch,
1389 GNUNET_YES);
1390 return;
1391 }
1353 1392
1354 s6->sin6_family = AF_INET6; 1393 s4 = (struct sockaddr_in *)&lal->addr;
1355 if (']' != (ch->hole_external[strlen(ch->hole_external)-1])) 1394 s4->sin_family = AF_INET;
1395 if (1 == inet_pton(AF_INET,
1396 ch->hole_external,
1397 &s4->sin_addr))
1356 { 1398 {
1357 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1358 _("Malformed punched hole specification `%s' (lacks `]')\n"), 1400 "IPv4 punched hole given for `%s' via `%s:%u'\n",
1359 ch->hole_external); 1401 ch->section_name,
1360 GNUNET_free (lal); 1402 ch->hole_external,
1403 (unsigned int)ch->ext_dns_port);
1404 s4->sin_port = htons(ch->ext_dns_port);
1405 lal->af = AF_INET;
1406 lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL;
1407 GNUNET_CONTAINER_DLL_insert(ch->ext_addr_head,
1408 ch->ext_addr_tail,
1409 lal);
1410 check_notify_client(lal,
1411 ch,
1412 GNUNET_YES);
1361 return; 1413 return;
1362 } 1414 }
1363 ch->hole_external[strlen(ch->hole_external)-1] = '\0'; 1415 if (0 == strcasecmp(ch->hole_external,
1364 if (1 != inet_pton (AF_INET6, 1416 "AUTO"))
1365 ch->hole_external + 1,
1366 &s6->sin6_addr))
1367 { 1417 {
1368 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1418 /* handled in #notify_client_external_ipv4_change() */
1369 _("Malformed punched hole specification `%s' (IPv6 address invalid)"), 1419 GNUNET_free(lal);
1370 ch->hole_external + 1);
1371 GNUNET_free (lal);
1372 return; 1420 return;
1373 } 1421 }
1374 s6->sin6_port = htons (ch->ext_dns_port);
1375 lal->af = AF_INET6;
1376 lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL;
1377 GNUNET_CONTAINER_DLL_insert (ch->ext_addr_head,
1378 ch->ext_addr_tail,
1379 lal);
1380 check_notify_client (lal,
1381 ch,
1382 GNUNET_YES);
1383 return;
1384 }
1385
1386 s4 = (struct sockaddr_in *) &lal->addr;
1387 s4->sin_family = AF_INET;
1388 if (1 == inet_pton (AF_INET,
1389 ch->hole_external,
1390 &s4->sin_addr))
1391 {
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393 "IPv4 punched hole given for `%s' via `%s:%u'\n",
1394 ch->section_name,
1395 ch->hole_external,
1396 (unsigned int) ch->ext_dns_port);
1397 s4->sin_port = htons (ch->ext_dns_port);
1398 lal->af = AF_INET;
1399 lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL;
1400 GNUNET_CONTAINER_DLL_insert (ch->ext_addr_head,
1401 ch->ext_addr_tail,
1402 lal);
1403 check_notify_client (lal,
1404 ch,
1405 GNUNET_YES);
1406 return;
1407 }
1408 if (0 == strcasecmp (ch->hole_external,
1409 "AUTO"))
1410 {
1411 /* handled in #notify_client_external_ipv4_change() */
1412 GNUNET_free (lal);
1413 return;
1414 }
1415 /* got a DNS name, trigger lookup! */ 1422 /* got a DNS name, trigger lookup! */
1416 GNUNET_free (lal); 1423 GNUNET_free(lal);
1417 ch->ext_dns_task 1424 ch->ext_dns_task
1418 = GNUNET_SCHEDULER_add_now (&dyndns_lookup, 1425 = GNUNET_SCHEDULER_add_now(&dyndns_lookup,
1419 ch); 1426 ch);
1420} 1427}
1421 1428
1422 1429
@@ -1428,132 +1435,134 @@ lookup_hole_external (struct ClientHandle *ch)
1428 * @param message the message received 1435 * @param message the message received
1429 */ 1436 */
1430static void 1437static void
1431handle_register (void *cls, 1438handle_register(void *cls,
1432 const struct GNUNET_NAT_RegisterMessage *message) 1439 const struct GNUNET_NAT_RegisterMessage *message)
1433{ 1440{
1434 struct ClientHandle *ch = cls; 1441 struct ClientHandle *ch = cls;
1435 const char *off; 1442 const char *off;
1436 size_t left; 1443 size_t left;
1437 1444
1438 if ( (0 != ch->proto) || 1445 if ((0 != ch->proto) ||
1439 (NULL != ch->caddrs) ) 1446 (NULL != ch->caddrs))
1440 {
1441 /* double registration not allowed */
1442 GNUNET_break (0);
1443 GNUNET_SERVICE_client_drop (ch->client);
1444 return;
1445 }
1446 ch->flags = message->flags;
1447 ch->proto = message->proto;
1448 ch->num_caddrs = ntohs (message->num_addrs);
1449 ch->caddrs = GNUNET_new_array (ch->num_caddrs,
1450 struct ClientAddress);
1451 left = ntohs (message->header.size) - sizeof (*message);
1452 off = (const char *) &message[1];
1453 for (unsigned int i=0;i<ch->num_caddrs;i++)
1454 {
1455 const struct sockaddr *sa = (const struct sockaddr *) off;
1456 size_t alen;
1457 uint16_t port;
1458 int is_nat;
1459
1460 if (sizeof (sa_family_t) > left)
1461 { 1447 {
1462 GNUNET_break (0); 1448 /* double registration not allowed */
1463 GNUNET_SERVICE_client_drop (ch->client); 1449 GNUNET_break(0);
1450 GNUNET_SERVICE_client_drop(ch->client);
1464 return; 1451 return;
1465 } 1452 }
1466 is_nat = GNUNET_NO; 1453 ch->flags = message->flags;
1467 switch (sa->sa_family) 1454 ch->proto = message->proto;
1455 ch->num_caddrs = ntohs(message->num_addrs);
1456 ch->caddrs = GNUNET_new_array(ch->num_caddrs,
1457 struct ClientAddress);
1458 left = ntohs(message->header.size) - sizeof(*message);
1459 off = (const char *)&message[1];
1460 for (unsigned int i = 0; i < ch->num_caddrs; i++)
1468 { 1461 {
1469 case AF_INET: 1462 const struct sockaddr *sa = (const struct sockaddr *)off;
1470 { 1463 size_t alen;
1471 struct sockaddr_in s4; 1464 uint16_t port;
1472 1465 int is_nat;
1473 GNUNET_memcpy (&s4, 1466
1474 off, 1467 if (sizeof(sa_family_t) > left)
1475 sizeof (struct sockaddr_in)); 1468 {
1476 alen = sizeof (struct sockaddr_in); 1469 GNUNET_break(0);
1477 if (is_nat_v4 (&s4.sin_addr)) 1470 GNUNET_SERVICE_client_drop(ch->client);
1478 is_nat = GNUNET_YES; 1471 return;
1479 port = ntohs (s4.sin_port); 1472 }
1480 } 1473 is_nat = GNUNET_NO;
1481 break; 1474 switch (sa->sa_family)
1482 case AF_INET6: 1475 {
1483 { 1476 case AF_INET:
1484 struct sockaddr_in6 s6; 1477 {
1485 1478 struct sockaddr_in s4;
1486 GNUNET_memcpy (&s6, 1479
1487 off, 1480 GNUNET_memcpy(&s4,
1488 sizeof (struct sockaddr_in6)); 1481 off,
1489 alen = sizeof (struct sockaddr_in6); 1482 sizeof(struct sockaddr_in));
1490 if (is_nat_v6 (&s6.sin6_addr)) 1483 alen = sizeof(struct sockaddr_in);
1491 is_nat = GNUNET_YES; 1484 if (is_nat_v4(&s4.sin_addr))
1492 port = ntohs (s6.sin6_port); 1485 is_nat = GNUNET_YES;
1493 } 1486 port = ntohs(s4.sin_port);
1494 break; 1487 }
1488 break;
1489
1490 case AF_INET6:
1491 {
1492 struct sockaddr_in6 s6;
1493
1494 GNUNET_memcpy(&s6,
1495 off,
1496 sizeof(struct sockaddr_in6));
1497 alen = sizeof(struct sockaddr_in6);
1498 if (is_nat_v6(&s6.sin6_addr))
1499 is_nat = GNUNET_YES;
1500 port = ntohs(s6.sin6_port);
1501 }
1502 break;
1503
1495#if AF_UNIX 1504#if AF_UNIX
1496 case AF_UNIX: 1505 case AF_UNIX:
1497 alen = sizeof (struct sockaddr_un); 1506 alen = sizeof(struct sockaddr_un);
1498 port = 0; 1507 port = 0;
1499 break; 1508 break;
1500#endif 1509#endif
1501 default: 1510 default:
1502 GNUNET_break (0); 1511 GNUNET_break(0);
1503 GNUNET_SERVICE_client_drop (ch->client); 1512 GNUNET_SERVICE_client_drop(ch->client);
1504 return; 1513 return;
1505 } 1514 }
1506 /* store address */ 1515 /* store address */
1507 GNUNET_assert (alen <= left); 1516 GNUNET_assert(alen <= left);
1508 GNUNET_assert (alen <= sizeof (struct sockaddr_storage)); 1517 GNUNET_assert(alen <= sizeof(struct sockaddr_storage));
1509 GNUNET_memcpy (&ch->caddrs[i].ss, 1518 GNUNET_memcpy(&ch->caddrs[i].ss,
1510 off, 1519 off,
1511 alen); 1520 alen);
1512 1521
1513 /* If applicable, try UPNPC NAT punching */ 1522 /* If applicable, try UPNPC NAT punching */
1514 if ( (is_nat) && 1523 if ((is_nat) &&
1515 (enable_upnp) && 1524 (enable_upnp) &&
1516 ( (IPPROTO_TCP == ch->proto) || 1525 ((IPPROTO_TCP == ch->proto) ||
1517 (IPPROTO_UDP == ch->proto) ) ) 1526 (IPPROTO_UDP == ch->proto)))
1518 { 1527 {
1519 ch->natted_address = GNUNET_YES; 1528 ch->natted_address = GNUNET_YES;
1520 ch->caddrs[i].mh 1529 ch->caddrs[i].mh
1521 = GNUNET_NAT_mini_map_start (port, 1530 = GNUNET_NAT_mini_map_start(port,
1522 IPPROTO_TCP == ch->proto, 1531 IPPROTO_TCP == ch->proto,
1523 &upnp_addr_change_cb, 1532 &upnp_addr_change_cb,
1524 ch); 1533 ch);
1534 }
1535
1536 off += alen;
1525 } 1537 }
1526 1538
1527 off += alen;
1528 }
1529
1530 ch->section_name 1539 ch->section_name
1531 = GNUNET_strndup (off, 1540 = GNUNET_strndup(off,
1532 ntohs (message->str_len)); 1541 ntohs(message->str_len));
1533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1542 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1534 "Received REGISTER message from client for subsystem `%s'\n", 1543 "Received REGISTER message from client for subsystem `%s'\n",
1535 ch->section_name); 1544 ch->section_name);
1536 if (GNUNET_OK == 1545 if (GNUNET_OK ==
1537 GNUNET_CONFIGURATION_get_value_string (cfg, 1546 GNUNET_CONFIGURATION_get_value_string(cfg,
1538 ch->section_name, 1547 ch->section_name,
1539 "HOLE_EXTERNAL", 1548 "HOLE_EXTERNAL",
1540 &ch->hole_external)) 1549 &ch->hole_external))
1541 lookup_hole_external (ch); 1550 lookup_hole_external(ch);
1542 1551
1543 /* Actually send IP address list to client */ 1552 /* Actually send IP address list to client */
1544 for (struct LocalAddressList *lal = lal_head; 1553 for (struct LocalAddressList *lal = lal_head;
1545 NULL != lal; 1554 NULL != lal;
1546 lal = lal->next) 1555 lal = lal->next)
1547 { 1556 {
1548 check_notify_client (lal, 1557 check_notify_client(lal,
1549 ch, 1558 ch,
1550 GNUNET_YES); 1559 GNUNET_YES);
1551 } 1560 }
1552 /* Also consider IPv4 determined by `external-ip` */ 1561 /* Also consider IPv4 determined by `external-ip` */
1553 ch->external_monitor 1562 ch->external_monitor
1554 = GN_external_ipv4_monitor_start (&notify_client_external_ipv4_change, 1563 = GN_external_ipv4_monitor_start(&notify_client_external_ipv4_change,
1555 ch); 1564 ch);
1556 GNUNET_SERVICE_client_continue (ch->client); 1565 GNUNET_SERVICE_client_continue(ch->client);
1557} 1566}
1558 1567
1559 1568
@@ -1566,22 +1575,22 @@ handle_register (void *cls,
1566 * @return #GNUNET_OK if message is well-formed 1575 * @return #GNUNET_OK if message is well-formed
1567 */ 1576 */
1568static int 1577static int
1569check_stun (void *cls, 1578check_stun(void *cls,
1570 const struct GNUNET_NAT_HandleStunMessage *message) 1579 const struct GNUNET_NAT_HandleStunMessage *message)
1571{ 1580{
1572 size_t sa_len = ntohs (message->sender_addr_size); 1581 size_t sa_len = ntohs(message->sender_addr_size);
1573 size_t expect = sa_len + ntohs (message->payload_size); 1582 size_t expect = sa_len + ntohs(message->payload_size);
1574 1583
1575 if (ntohs (message->header.size) - sizeof (*message) != expect) 1584 if (ntohs(message->header.size) - sizeof(*message) != expect)
1576 { 1585 {
1577 GNUNET_break (0); 1586 GNUNET_break(0);
1578 return GNUNET_SYSERR; 1587 return GNUNET_SYSERR;
1579 } 1588 }
1580 if (sa_len < sizeof (sa_family_t)) 1589 if (sa_len < sizeof(sa_family_t))
1581 { 1590 {
1582 GNUNET_break (0); 1591 GNUNET_break(0);
1583 return GNUNET_SYSERR; 1592 return GNUNET_SYSERR;
1584 } 1593 }
1585 return GNUNET_OK; 1594 return GNUNET_OK;
1586} 1595}
1587 1596
@@ -1594,33 +1603,33 @@ check_stun (void *cls,
1594 * @param add #GNUNET_YES to add, #GNUNET_NO to remove 1603 * @param add #GNUNET_YES to add, #GNUNET_NO to remove
1595 */ 1604 */
1596static void 1605static void
1597notify_clients_stun_change (const struct sockaddr_in *ip, 1606notify_clients_stun_change(const struct sockaddr_in *ip,
1598 int add) 1607 int add)
1599{ 1608{
1600 for (struct ClientHandle *ch = ch_head; 1609 for (struct ClientHandle *ch = ch_head;
1601 NULL != ch; 1610 NULL != ch;
1602 ch = ch->next) 1611 ch = ch->next)
1603 { 1612 {
1604 struct sockaddr_in v4; 1613 struct sockaddr_in v4;
1605 struct GNUNET_NAT_AddressChangeNotificationMessage *msg; 1614 struct GNUNET_NAT_AddressChangeNotificationMessage *msg;
1606 struct GNUNET_MQ_Envelope *env; 1615 struct GNUNET_MQ_Envelope *env;
1607 1616
1608 if (! ch->natted_address) 1617 if (!ch->natted_address)
1609 continue; 1618 continue;
1610 v4 = *ip; 1619 v4 = *ip;
1611 v4.sin_port = htons (0); 1620 v4.sin_port = htons(0);
1612 env = GNUNET_MQ_msg_extra (msg, 1621 env = GNUNET_MQ_msg_extra(msg,
1613 sizeof (v4), 1622 sizeof(v4),
1614 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE); 1623 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE);
1615 msg->add_remove = htonl ((int32_t) add); 1624 msg->add_remove = htonl((int32_t)add);
1616 msg->addr_class = htonl (GNUNET_NAT_AC_EXTERN | 1625 msg->addr_class = htonl(GNUNET_NAT_AC_EXTERN |
1617 GNUNET_NAT_AC_GLOBAL); 1626 GNUNET_NAT_AC_GLOBAL);
1618 GNUNET_memcpy (&msg[1], 1627 GNUNET_memcpy(&msg[1],
1619 &v4, 1628 &v4,
1620 sizeof (v4)); 1629 sizeof(v4));
1621 GNUNET_MQ_send (ch->mq, 1630 GNUNET_MQ_send(ch->mq,
1622 env); 1631 env);
1623 } 1632 }
1624} 1633}
1625 1634
1626 1635
@@ -1632,17 +1641,17 @@ notify_clients_stun_change (const struct sockaddr_in *ip,
1632 * @param cls the `struct StunExternalIP` to drop 1641 * @param cls the `struct StunExternalIP` to drop
1633 */ 1642 */
1634static void 1643static void
1635stun_ip_timeout (void *cls) 1644stun_ip_timeout(void *cls)
1636{ 1645{
1637 struct StunExternalIP *se = cls; 1646 struct StunExternalIP *se = cls;
1638 1647
1639 se->timeout_task = NULL; 1648 se->timeout_task = NULL;
1640 notify_clients_stun_change (&se->external_addr, 1649 notify_clients_stun_change(&se->external_addr,
1641 GNUNET_NO); 1650 GNUNET_NO);
1642 GNUNET_CONTAINER_DLL_remove (se_head, 1651 GNUNET_CONTAINER_DLL_remove(se_head,
1643 se_tail, 1652 se_tail,
1644 se); 1653 se);
1645 GNUNET_free (se); 1654 GNUNET_free(se);
1646} 1655}
1647 1656
1648 1657
@@ -1654,106 +1663,107 @@ stun_ip_timeout (void *cls)
1654 * @param message the message received 1663 * @param message the message received
1655 */ 1664 */
1656static void 1665static void
1657handle_stun (void *cls, 1666handle_stun(void *cls,
1658 const struct GNUNET_NAT_HandleStunMessage *message) 1667 const struct GNUNET_NAT_HandleStunMessage *message)
1659{ 1668{
1660 struct ClientHandle *ch = cls; 1669 struct ClientHandle *ch = cls;
1661 const char *buf = (const char *) &message[1]; 1670 const char *buf = (const char *)&message[1];
1662 const struct sockaddr *sa; 1671 const struct sockaddr *sa;
1663 const void *payload; 1672 const void *payload;
1664 size_t sa_len; 1673 size_t sa_len;
1665 size_t payload_size; 1674 size_t payload_size;
1666 struct sockaddr_in external_addr; 1675 struct sockaddr_in external_addr;
1667 1676
1668 sa_len = ntohs (message->sender_addr_size); 1677 sa_len = ntohs(message->sender_addr_size);
1669 payload_size = ntohs (message->payload_size); 1678 payload_size = ntohs(message->payload_size);
1670 sa = (const struct sockaddr *) &buf[0]; 1679 sa = (const struct sockaddr *)&buf[0];
1671 payload = (const struct sockaddr *) &buf[sa_len]; 1680 payload = (const struct sockaddr *)&buf[sa_len];
1672 switch (sa->sa_family) 1681 switch (sa->sa_family)
1673 {
1674 case AF_INET:
1675 if (sa_len != sizeof (struct sockaddr_in))
1676 {
1677 GNUNET_break (0);
1678 GNUNET_SERVICE_client_drop (ch->client);
1679 return;
1680 }
1681 break;
1682 case AF_INET6:
1683 if (sa_len != sizeof (struct sockaddr_in6))
1684 { 1682 {
1685 GNUNET_break (0); 1683 case AF_INET:
1686 GNUNET_SERVICE_client_drop (ch->client); 1684 if (sa_len != sizeof(struct sockaddr_in))
1687 return; 1685 {
1686 GNUNET_break(0);
1687 GNUNET_SERVICE_client_drop(ch->client);
1688 return;
1689 }
1690 break;
1691
1692 case AF_INET6:
1693 if (sa_len != sizeof(struct sockaddr_in6))
1694 {
1695 GNUNET_break(0);
1696 GNUNET_SERVICE_client_drop(ch->client);
1697 return;
1698 }
1699 break;
1688 } 1700 }
1689 break; 1701 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1690 } 1702 "Received HANDLE_STUN message from client\n");
1691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1692 "Received HANDLE_STUN message from client\n");
1693 if (GNUNET_OK == 1703 if (GNUNET_OK ==
1694 GNUNET_NAT_stun_handle_packet_ (payload, 1704 GNUNET_NAT_stun_handle_packet_(payload,
1695 payload_size, 1705 payload_size,
1696 &external_addr)) 1706 &external_addr))
1697 {
1698 /* We now know that a server at "sa" claims that
1699 we are visible at IP "external_addr".
1700
1701 We should (for some fixed period of time) tell
1702 all of our clients that listen to a NAT'ed address
1703 that they might want to consider the given 'external_ip'
1704 as their public IP address (this includes TCP and UDP
1705 clients, even if only UDP sends STUN requests).
1706
1707 If we do not get a renewal, the "external_addr" should be
1708 removed again. The timeout frequency should be configurable
1709 (with a sane default), so that the UDP plugin can tell how
1710 often to re-request STUN.
1711 */
1712 struct StunExternalIP *se;
1713
1714 /* Check if we had a prior response from this STUN server */
1715 for (se = se_head; NULL != se; se = se->next)
1716 { 1707 {
1717 if ( (se->stun_server_addr_len != sa_len) || 1708 /* We now know that a server at "sa" claims that
1718 (0 != memcmp (sa, 1709 we are visible at IP "external_addr".
1719 &se->stun_server_addr, 1710
1720 sa_len)) ) 1711 We should (for some fixed period of time) tell
1721 continue; /* different STUN server */ 1712 all of our clients that listen to a NAT'ed address
1722 if (0 != GNUNET_memcmp (&external_addr, 1713 that they might want to consider the given 'external_ip'
1723 &se->external_addr)) 1714 as their public IP address (this includes TCP and UDP
1724 { 1715 clients, even if only UDP sends STUN requests).
1725 /* external IP changed, update! */ 1716
1726 notify_clients_stun_change (&se->external_addr, 1717 If we do not get a renewal, the "external_addr" should be
1727 GNUNET_NO); 1718 removed again. The timeout frequency should be configurable
1728 se->external_addr = external_addr; 1719 (with a sane default), so that the UDP plugin can tell how
1729 notify_clients_stun_change (&se->external_addr, 1720 often to re-request STUN.
1730 GNUNET_YES); 1721 */
1731 } 1722 struct StunExternalIP *se;
1732 /* update timeout */ 1723
1733 GNUNET_SCHEDULER_cancel (se->timeout_task); 1724 /* Check if we had a prior response from this STUN server */
1734 se->timeout_task 1725 for (se = se_head; NULL != se; se = se->next)
1735 = GNUNET_SCHEDULER_add_delayed (stun_stale_timeout, 1726 {
1736 &stun_ip_timeout, 1727 if ((se->stun_server_addr_len != sa_len) ||
1737 se); 1728 (0 != memcmp(sa,
1738 return; 1729 &se->stun_server_addr,
1730 sa_len)))
1731 continue; /* different STUN server */
1732 if (0 != GNUNET_memcmp(&external_addr,
1733 &se->external_addr))
1734 {
1735 /* external IP changed, update! */
1736 notify_clients_stun_change(&se->external_addr,
1737 GNUNET_NO);
1738 se->external_addr = external_addr;
1739 notify_clients_stun_change(&se->external_addr,
1740 GNUNET_YES);
1741 }
1742 /* update timeout */
1743 GNUNET_SCHEDULER_cancel(se->timeout_task);
1744 se->timeout_task
1745 = GNUNET_SCHEDULER_add_delayed(stun_stale_timeout,
1746 &stun_ip_timeout,
1747 se);
1748 return;
1749 }
1750 /* STUN server is completely new, create fresh entry */
1751 se = GNUNET_new(struct StunExternalIP);
1752 se->external_addr = external_addr;
1753 GNUNET_memcpy(&se->stun_server_addr,
1754 sa,
1755 sa_len);
1756 se->stun_server_addr_len = sa_len;
1757 se->timeout_task = GNUNET_SCHEDULER_add_delayed(stun_stale_timeout,
1758 &stun_ip_timeout,
1759 se);
1760 GNUNET_CONTAINER_DLL_insert(se_head,
1761 se_tail,
1762 se);
1763 notify_clients_stun_change(&se->external_addr,
1764 GNUNET_NO);
1739 } 1765 }
1740 /* STUN server is completely new, create fresh entry */ 1766 GNUNET_SERVICE_client_continue(ch->client);
1741 se = GNUNET_new (struct StunExternalIP);
1742 se->external_addr = external_addr;
1743 GNUNET_memcpy (&se->stun_server_addr,
1744 sa,
1745 sa_len);
1746 se->stun_server_addr_len = sa_len;
1747 se->timeout_task = GNUNET_SCHEDULER_add_delayed (stun_stale_timeout,
1748 &stun_ip_timeout,
1749 se);
1750 GNUNET_CONTAINER_DLL_insert (se_head,
1751 se_tail,
1752 se);
1753 notify_clients_stun_change (&se->external_addr,
1754 GNUNET_NO);
1755 }
1756 GNUNET_SERVICE_client_continue (ch->client);
1757} 1767}
1758 1768
1759 1769
@@ -1767,18 +1777,18 @@ handle_stun (void *cls,
1767 * @return #GNUNET_OK if message is well-formed 1777 * @return #GNUNET_OK if message is well-formed
1768 */ 1778 */
1769static int 1779static int
1770check_request_connection_reversal (void *cls, 1780check_request_connection_reversal(void *cls,
1771 const struct GNUNET_NAT_RequestConnectionReversalMessage *message) 1781 const struct GNUNET_NAT_RequestConnectionReversalMessage *message)
1772{ 1782{
1773 size_t expect; 1783 size_t expect;
1774 1784
1775 expect = ntohs (message->local_addr_size) 1785 expect = ntohs(message->local_addr_size)
1776 + ntohs (message->remote_addr_size); 1786 + ntohs(message->remote_addr_size);
1777 if (ntohs (message->header.size) - sizeof (*message) != expect) 1787 if (ntohs(message->header.size) - sizeof(*message) != expect)
1778 { 1788 {
1779 GNUNET_break (0); 1789 GNUNET_break(0);
1780 return GNUNET_SYSERR; 1790 return GNUNET_SYSERR;
1781 } 1791 }
1782 return GNUNET_OK; 1792 return GNUNET_OK;
1783} 1793}
1784 1794
@@ -1791,48 +1801,48 @@ check_request_connection_reversal (void *cls,
1791 * @param message the message received 1801 * @param message the message received
1792 */ 1802 */
1793static void 1803static void
1794handle_request_connection_reversal (void *cls, 1804handle_request_connection_reversal(void *cls,
1795 const struct GNUNET_NAT_RequestConnectionReversalMessage *message) 1805 const struct GNUNET_NAT_RequestConnectionReversalMessage *message)
1796{ 1806{
1797 struct ClientHandle *ch = cls; 1807 struct ClientHandle *ch = cls;
1798 const char *buf = (const char *) &message[1]; 1808 const char *buf = (const char *)&message[1];
1799 size_t local_sa_len = ntohs (message->local_addr_size); 1809 size_t local_sa_len = ntohs(message->local_addr_size);
1800 size_t remote_sa_len = ntohs (message->remote_addr_size); 1810 size_t remote_sa_len = ntohs(message->remote_addr_size);
1801 struct sockaddr_in l4; 1811 struct sockaddr_in l4;
1802 struct sockaddr_in r4; 1812 struct sockaddr_in r4;
1803 int ret; 1813 int ret;
1804 1814
1805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1815 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1806 "Received REQUEST CONNECTION REVERSAL message from client\n"); 1816 "Received REQUEST CONNECTION REVERSAL message from client\n");
1807 if (local_sa_len != sizeof (struct sockaddr_in)) 1817 if (local_sa_len != sizeof(struct sockaddr_in))
1808 { 1818 {
1809 GNUNET_break_op (0); 1819 GNUNET_break_op(0);
1810 GNUNET_SERVICE_client_drop (ch->client); 1820 GNUNET_SERVICE_client_drop(ch->client);
1811 return; 1821 return;
1812 } 1822 }
1813 if (remote_sa_len != sizeof (struct sockaddr_in)) 1823 if (remote_sa_len != sizeof(struct sockaddr_in))
1814 { 1824 {
1815 GNUNET_break_op (0); 1825 GNUNET_break_op(0);
1816 GNUNET_SERVICE_client_drop (ch->client); 1826 GNUNET_SERVICE_client_drop(ch->client);
1817 return; 1827 return;
1818 } 1828 }
1819 GNUNET_memcpy (&l4, 1829 GNUNET_memcpy(&l4,
1820 buf, 1830 buf,
1821 sizeof (struct sockaddr_in)); 1831 sizeof(struct sockaddr_in));
1822 GNUNET_break_op (AF_INET == l4.sin_family); 1832 GNUNET_break_op(AF_INET == l4.sin_family);
1823 buf += sizeof (struct sockaddr_in); 1833 buf += sizeof(struct sockaddr_in);
1824 GNUNET_memcpy (&r4, 1834 GNUNET_memcpy(&r4,
1825 buf, 1835 buf,
1826 sizeof (struct sockaddr_in)); 1836 sizeof(struct sockaddr_in));
1827 GNUNET_break_op (AF_INET == r4.sin_family); 1837 GNUNET_break_op(AF_INET == r4.sin_family);
1828 ret = GN_request_connection_reversal (&l4.sin_addr, 1838 ret = GN_request_connection_reversal(&l4.sin_addr,
1829 ntohs (l4.sin_port), 1839 ntohs(l4.sin_port),
1830 &r4.sin_addr, 1840 &r4.sin_addr,
1831 cfg); 1841 cfg);
1832 if (GNUNET_OK != ret) 1842 if (GNUNET_OK != ret)
1833 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1843 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1834 _("Connection reversal request failed\n")); 1844 _("Connection reversal request failed\n"));
1835 GNUNET_SERVICE_client_continue (ch->client); 1845 GNUNET_SERVICE_client_continue(ch->client);
1836} 1846}
1837 1847
1838 1848
@@ -1842,31 +1852,31 @@ handle_request_connection_reversal (void *cls,
1842 * @param cls unused 1852 * @param cls unused
1843 */ 1853 */
1844static void 1854static void
1845shutdown_task (void *cls) 1855shutdown_task(void *cls)
1846{ 1856{
1847 struct StunExternalIP *se; 1857 struct StunExternalIP *se;
1848 1858
1849 while (NULL != (se = se_head)) 1859 while (NULL != (se = se_head))
1850 { 1860 {
1851 GNUNET_CONTAINER_DLL_remove (se_head, 1861 GNUNET_CONTAINER_DLL_remove(se_head,
1852 se_tail, 1862 se_tail,
1853 se); 1863 se);
1854 GNUNET_SCHEDULER_cancel (se->timeout_task); 1864 GNUNET_SCHEDULER_cancel(se->timeout_task);
1855 GNUNET_free (se); 1865 GNUNET_free(se);
1856 } 1866 }
1857 GN_nat_status_changed (GNUNET_NO); 1867 GN_nat_status_changed(GNUNET_NO);
1858 if (NULL != scan_task) 1868 if (NULL != scan_task)
1859 { 1869 {
1860 GNUNET_SCHEDULER_cancel (scan_task); 1870 GNUNET_SCHEDULER_cancel(scan_task);
1861 scan_task = NULL; 1871 scan_task = NULL;
1862 } 1872 }
1863 if (NULL != stats) 1873 if (NULL != stats)
1864 { 1874 {
1865 GNUNET_STATISTICS_destroy (stats, 1875 GNUNET_STATISTICS_destroy(stats,
1866 GNUNET_NO); 1876 GNUNET_NO);
1867 stats = NULL; 1877 stats = NULL;
1868 } 1878 }
1869 destroy_lal (); 1879 destroy_lal();
1870} 1880}
1871 1881
1872 1882
@@ -1878,49 +1888,49 @@ shutdown_task (void *cls)
1878 * @param service the initialized service 1888 * @param service the initialized service
1879 */ 1889 */
1880static void 1890static void
1881run (void *cls, 1891run(void *cls,
1882 const struct GNUNET_CONFIGURATION_Handle *c, 1892 const struct GNUNET_CONFIGURATION_Handle *c,
1883 struct GNUNET_SERVICE_Handle *service) 1893 struct GNUNET_SERVICE_Handle *service)
1884{ 1894{
1885 cfg = c; 1895 cfg = c;
1886 if (GNUNET_OK != 1896 if (GNUNET_OK !=
1887 GNUNET_CONFIGURATION_get_value_time (cfg, 1897 GNUNET_CONFIGURATION_get_value_time(cfg,
1888 "NAT", 1898 "NAT",
1889 "STUN_STALE", 1899 "STUN_STALE",
1890 &stun_stale_timeout)) 1900 &stun_stale_timeout))
1891 stun_stale_timeout = GNUNET_TIME_UNIT_HOURS; 1901 stun_stale_timeout = GNUNET_TIME_UNIT_HOURS;
1892 1902
1893 /* Check for UPnP */ 1903 /* Check for UPnP */
1894 enable_upnp 1904 enable_upnp
1895 = GNUNET_CONFIGURATION_get_value_yesno (cfg, 1905 = GNUNET_CONFIGURATION_get_value_yesno(cfg,
1896 "NAT", 1906 "NAT",
1897 "ENABLE_UPNP"); 1907 "ENABLE_UPNP");
1898 if (GNUNET_YES == enable_upnp) 1908 if (GNUNET_YES == enable_upnp)
1899 {
1900 /* check if it works */
1901 if (GNUNET_SYSERR ==
1902 GNUNET_OS_check_helper_binary ("upnpc",
1903 GNUNET_NO,
1904 NULL))
1905 { 1909 {
1906 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1910 /* check if it works */
1907 _("UPnP enabled in configuration, but UPnP client `upnpc` command not found, disabling UPnP\n")); 1911 if (GNUNET_SYSERR ==
1908 enable_upnp = GNUNET_SYSERR; 1912 GNUNET_OS_check_helper_binary("upnpc",
1913 GNUNET_NO,
1914 NULL))
1915 {
1916 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1917 _("UPnP enabled in configuration, but UPnP client `upnpc` command not found, disabling UPnP\n"));
1918 enable_upnp = GNUNET_SYSERR;
1919 }
1909 } 1920 }
1910 }
1911 if (GNUNET_OK != 1921 if (GNUNET_OK !=
1912 GNUNET_CONFIGURATION_get_value_time (cfg, 1922 GNUNET_CONFIGURATION_get_value_time(cfg,
1913 "nat", 1923 "nat",
1914 "DYNDNS_FREQUENCY", 1924 "DYNDNS_FREQUENCY",
1915 &dyndns_frequency)) 1925 &dyndns_frequency))
1916 dyndns_frequency = DYNDNS_FREQUENCY; 1926 dyndns_frequency = DYNDNS_FREQUENCY;
1917 1927
1918 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, 1928 GNUNET_SCHEDULER_add_shutdown(&shutdown_task,
1919 NULL); 1929 NULL);
1920 stats = GNUNET_STATISTICS_create ("nat", 1930 stats = GNUNET_STATISTICS_create("nat",
1921 cfg); 1931 cfg);
1922 scan_task = GNUNET_SCHEDULER_add_now (&run_scan, 1932 scan_task = GNUNET_SCHEDULER_add_now(&run_scan,
1923 NULL); 1933 NULL);
1924} 1934}
1925 1935
1926 1936
@@ -1933,18 +1943,18 @@ run (void *cls,
1933 * @return a `struct ClientHandle` 1943 * @return a `struct ClientHandle`
1934 */ 1944 */
1935static void * 1945static void *
1936client_connect_cb (void *cls, 1946client_connect_cb(void *cls,
1937 struct GNUNET_SERVICE_Client *c, 1947 struct GNUNET_SERVICE_Client *c,
1938 struct GNUNET_MQ_Handle *mq) 1948 struct GNUNET_MQ_Handle *mq)
1939{ 1949{
1940 struct ClientHandle *ch; 1950 struct ClientHandle *ch;
1941 1951
1942 ch = GNUNET_new (struct ClientHandle); 1952 ch = GNUNET_new(struct ClientHandle);
1943 ch->mq = mq; 1953 ch->mq = mq;
1944 ch->client = c; 1954 ch->client = c;
1945 GNUNET_CONTAINER_DLL_insert (ch_head, 1955 GNUNET_CONTAINER_DLL_insert(ch_head,
1946 ch_tail, 1956 ch_tail,
1947 ch); 1957 ch);
1948 return ch; 1958 return ch;
1949} 1959}
1950 1960
@@ -1957,50 +1967,50 @@ client_connect_cb (void *cls,
1957 * @param internal_cls a `struct ClientHandle *` 1967 * @param internal_cls a `struct ClientHandle *`
1958 */ 1968 */
1959static void 1969static void
1960client_disconnect_cb (void *cls, 1970client_disconnect_cb(void *cls,
1961 struct GNUNET_SERVICE_Client *c, 1971 struct GNUNET_SERVICE_Client *c,
1962 void *internal_cls) 1972 void *internal_cls)
1963{ 1973{
1964 struct ClientHandle *ch = internal_cls; 1974 struct ClientHandle *ch = internal_cls;
1965 struct LocalAddressList *lal; 1975 struct LocalAddressList *lal;
1966 1976
1967 GNUNET_CONTAINER_DLL_remove (ch_head, 1977 GNUNET_CONTAINER_DLL_remove(ch_head,
1968 ch_tail, 1978 ch_tail,
1969 ch); 1979 ch);
1970 for (unsigned int i=0;i<ch->num_caddrs;i++) 1980 for (unsigned int i = 0; i < ch->num_caddrs; i++)
1971 {
1972 if (NULL != ch->caddrs[i].mh)
1973 { 1981 {
1974 GNUNET_NAT_mini_map_stop (ch->caddrs[i].mh); 1982 if (NULL != ch->caddrs[i].mh)
1975 ch->caddrs[i].mh = NULL; 1983 {
1984 GNUNET_NAT_mini_map_stop(ch->caddrs[i].mh);
1985 ch->caddrs[i].mh = NULL;
1986 }
1976 } 1987 }
1977 } 1988 GNUNET_free_non_null(ch->caddrs);
1978 GNUNET_free_non_null (ch->caddrs);
1979 while (NULL != (lal = ch->ext_addr_head)) 1989 while (NULL != (lal = ch->ext_addr_head))
1980 { 1990 {
1981 GNUNET_CONTAINER_DLL_remove (ch->ext_addr_head, 1991 GNUNET_CONTAINER_DLL_remove(ch->ext_addr_head,
1982 ch->ext_addr_tail, 1992 ch->ext_addr_tail,
1983 lal); 1993 lal);
1984 GNUNET_free (lal); 1994 GNUNET_free(lal);
1985 } 1995 }
1986 if (NULL != ch->ext_dns_task) 1996 if (NULL != ch->ext_dns_task)
1987 { 1997 {
1988 GNUNET_SCHEDULER_cancel (ch->ext_dns_task); 1998 GNUNET_SCHEDULER_cancel(ch->ext_dns_task);
1989 ch->ext_dns_task = NULL; 1999 ch->ext_dns_task = NULL;
1990 } 2000 }
1991 if (NULL != ch->external_monitor) 2001 if (NULL != ch->external_monitor)
1992 { 2002 {
1993 GN_external_ipv4_monitor_stop (ch->external_monitor); 2003 GN_external_ipv4_monitor_stop(ch->external_monitor);
1994 ch->external_monitor = NULL; 2004 ch->external_monitor = NULL;
1995 } 2005 }
1996 if (NULL != ch->ext_dns) 2006 if (NULL != ch->ext_dns)
1997 { 2007 {
1998 GNUNET_RESOLVER_request_cancel (ch->ext_dns); 2008 GNUNET_RESOLVER_request_cancel(ch->ext_dns);
1999 ch->ext_dns = NULL; 2009 ch->ext_dns = NULL;
2000 } 2010 }
2001 GNUNET_free_non_null (ch->hole_external); 2011 GNUNET_free_non_null(ch->hole_external);
2002 GNUNET_free_non_null (ch->section_name); 2012 GNUNET_free_non_null(ch->section_name);
2003 GNUNET_free (ch); 2013 GNUNET_free(ch);
2004} 2014}
2005 2015
2006 2016
@@ -2008,25 +2018,25 @@ client_disconnect_cb (void *cls,
2008 * Define "main" method using service macro. 2018 * Define "main" method using service macro.
2009 */ 2019 */
2010GNUNET_SERVICE_MAIN 2020GNUNET_SERVICE_MAIN
2011("nat", 2021 ("nat",
2012 GNUNET_SERVICE_OPTION_NONE, 2022 GNUNET_SERVICE_OPTION_NONE,
2013 &run, 2023 &run,
2014 &client_connect_cb, 2024 &client_connect_cb,
2015 &client_disconnect_cb, 2025 &client_disconnect_cb,
2016 NULL, 2026 NULL,
2017 GNUNET_MQ_hd_var_size (register, 2027 GNUNET_MQ_hd_var_size(register,
2018 GNUNET_MESSAGE_TYPE_NAT_REGISTER, 2028 GNUNET_MESSAGE_TYPE_NAT_REGISTER,
2019 struct GNUNET_NAT_RegisterMessage, 2029 struct GNUNET_NAT_RegisterMessage,
2020 NULL), 2030 NULL),
2021 GNUNET_MQ_hd_var_size (stun, 2031 GNUNET_MQ_hd_var_size(stun,
2022 GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN, 2032 GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN,
2023 struct GNUNET_NAT_HandleStunMessage, 2033 struct GNUNET_NAT_HandleStunMessage,
2024 NULL), 2034 NULL),
2025 GNUNET_MQ_hd_var_size (request_connection_reversal, 2035 GNUNET_MQ_hd_var_size(request_connection_reversal,
2026 GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL, 2036 GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL,
2027 struct GNUNET_NAT_RequestConnectionReversalMessage, 2037 struct GNUNET_NAT_RequestConnectionReversalMessage,
2028 NULL), 2038 NULL),
2029 GNUNET_MQ_handler_end ()); 2039 GNUNET_MQ_handler_end());
2030 2040
2031 2041
2032#if defined(LINUX) && defined(__GLIBC__) 2042#if defined(LINUX) && defined(__GLIBC__)
@@ -2036,11 +2046,11 @@ GNUNET_SERVICE_MAIN
2036 * MINIMIZE heap size (way below 128k) since this process doesn't need much. 2046 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
2037 */ 2047 */
2038void __attribute__ ((constructor)) 2048void __attribute__ ((constructor))
2039GNUNET_ARM_memory_init () 2049GNUNET_ARM_memory_init()
2040{ 2050{
2041 mallopt (M_TRIM_THRESHOLD, 4 * 1024); 2051 mallopt(M_TRIM_THRESHOLD, 4 * 1024);
2042 mallopt (M_TOP_PAD, 1 * 1024); 2052 mallopt(M_TOP_PAD, 1 * 1024);
2043 malloc_trim (0); 2053 malloc_trim(0);
2044} 2054}
2045#endif 2055#endif
2046 2056
diff --git a/src/nat/gnunet-service-nat.h b/src/nat/gnunet-service-nat.h
index ec642f6e5..5717306bb 100644
--- a/src/nat/gnunet-service-nat.h
+++ b/src/nat/gnunet-service-nat.h
@@ -1,19 +1,19 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2016, 2017 GNUnet e.V. 3 Copyright (C) 2016, 2017 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 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 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, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
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 */
diff --git a/src/nat/gnunet-service-nat_externalip.c b/src/nat/gnunet-service-nat_externalip.c
index 282b070d3..c97f7f58b 100644
--- a/src/nat/gnunet-service-nat_externalip.c
+++ b/src/nat/gnunet-service-nat_externalip.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 * Code to figure out what our external IPv4 address(es) might 21 * Code to figure out what our external IPv4 address(es) might
22 * be (external IPv4s are what is seen on the rest of the Internet). 22 * be (external IPv4s are what is seen on the rest of the Internet).
@@ -54,26 +54,25 @@
54 * How long do we wait until we re-try running `external-ip` if the 54 * How long do we wait until we re-try running `external-ip` if the
55 * command failed to terminate nicely? 55 * command failed to terminate nicely?
56 */ 56 */
57#define EXTERN_IP_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15) 57#define EXTERN_IP_RETRY_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 15)
58 58
59/** 59/**
60 * How long do we wait until we re-try running `external-ip` if the 60 * How long do we wait until we re-try running `external-ip` if the
61 * command failed (but terminated)? 61 * command failed (but terminated)?
62 */ 62 */
63#define EXTERN_IP_RETRY_FAILURE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30) 63#define EXTERN_IP_RETRY_FAILURE GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 30)
64 64
65/** 65/**
66 * How long do we wait until we re-try running `external-ip` if the 66 * How long do we wait until we re-try running `external-ip` if the
67 * command succeeded? 67 * command succeeded?
68 */ 68 */
69#define EXTERN_IP_RETRY_SUCCESS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) 69#define EXTERN_IP_RETRY_SUCCESS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
70 70
71 71
72/** 72/**
73 * Handle to monitor for external IP changes. 73 * Handle to monitor for external IP changes.
74 */ 74 */
75struct GN_ExternalIPMonitor 75struct GN_ExternalIPMonitor {
76{
77 /** 76 /**
78 * Kept in DLL. 77 * Kept in DLL.
79 */ 78 */
@@ -93,7 +92,6 @@ struct GN_ExternalIPMonitor
93 * Closure for @e cb. 92 * Closure for @e cb.
94 */ 93 */
95 void *cb_cls; 94 void *cb_cls;
96
97}; 95};
98 96
99 97
@@ -133,15 +131,15 @@ static struct in_addr mini_external_ipv4;
133 * @param v4 the external address that changed 131 * @param v4 the external address that changed
134 */ 132 */
135static void 133static void
136notify_monitors_external_ipv4_change (int add, 134notify_monitors_external_ipv4_change(int add,
137 const struct in_addr *v4) 135 const struct in_addr *v4)
138{ 136{
139 for (struct GN_ExternalIPMonitor *mon = mon_head; 137 for (struct GN_ExternalIPMonitor *mon = mon_head;
140 NULL != mon; 138 NULL != mon;
141 mon = mon->next) 139 mon = mon->next)
142 mon->cb (mon->cb_cls, 140 mon->cb(mon->cb_cls,
143 v4, 141 v4,
144 add); 142 add);
145} 143}
146 144
147 145
@@ -152,7 +150,7 @@ notify_monitors_external_ipv4_change (int add,
152 * @param cls NULL 150 * @param cls NULL
153 */ 151 */
154static void 152static void
155run_external_ip (void *cls); 153run_external_ip(void *cls);
156 154
157 155
158/** 156/**
@@ -165,46 +163,47 @@ run_external_ip (void *cls);
165 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code 163 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
166 */ 164 */
167static void 165static void
168handle_external_ip (void *cls, 166handle_external_ip(void *cls,
169 const struct in_addr *addr, 167 const struct in_addr *addr,
170 enum GNUNET_NAT_StatusCode result) 168 enum GNUNET_NAT_StatusCode result)
171{ 169{
172 char buf[INET_ADDRSTRLEN]; 170 char buf[INET_ADDRSTRLEN];
173 171
174 probe_external_ip_op = NULL; 172 probe_external_ip_op = NULL;
175 GNUNET_SCHEDULER_cancel (probe_external_ip_task); 173 GNUNET_SCHEDULER_cancel(probe_external_ip_task);
176 probe_external_ip_task 174 probe_external_ip_task
177 = GNUNET_SCHEDULER_add_delayed ((NULL == addr) 175 = GNUNET_SCHEDULER_add_delayed((NULL == addr)
178 ? EXTERN_IP_RETRY_FAILURE 176 ? EXTERN_IP_RETRY_FAILURE
179 : EXTERN_IP_RETRY_SUCCESS, 177 : EXTERN_IP_RETRY_SUCCESS,
180 &run_external_ip, 178 &run_external_ip,
181 NULL); 179 NULL);
182 switch (result) 180 switch (result)
183 { 181 {
184 case GNUNET_NAT_ERROR_SUCCESS: 182 case GNUNET_NAT_ERROR_SUCCESS:
185 GNUNET_assert (NULL != addr); 183 GNUNET_assert(NULL != addr);
186 if (addr->s_addr == mini_external_ipv4.s_addr) 184 if (addr->s_addr == mini_external_ipv4.s_addr)
187 return; /* not change */ 185 return; /* not change */
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 186 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
189 "Our external IP is now %s\n", 187 "Our external IP is now %s\n",
190 inet_ntop (AF_INET, 188 inet_ntop(AF_INET,
191 addr, 189 addr,
192 buf, 190 buf,
193 sizeof (buf))); 191 sizeof(buf)));
194 if (0 != mini_external_ipv4.s_addr) 192 if (0 != mini_external_ipv4.s_addr)
195 notify_monitors_external_ipv4_change (GNUNET_NO, 193 notify_monitors_external_ipv4_change(GNUNET_NO,
196 &mini_external_ipv4); 194 &mini_external_ipv4);
197 mini_external_ipv4 = *addr; 195 mini_external_ipv4 = *addr;
198 notify_monitors_external_ipv4_change (GNUNET_YES, 196 notify_monitors_external_ipv4_change(GNUNET_YES,
199 &mini_external_ipv4); 197 &mini_external_ipv4);
200 break; 198 break;
201 default: 199
202 if (0 != mini_external_ipv4.s_addr) 200 default:
203 notify_monitors_external_ipv4_change (GNUNET_NO, 201 if (0 != mini_external_ipv4.s_addr)
204 &mini_external_ipv4); 202 notify_monitors_external_ipv4_change(GNUNET_NO,
205 mini_external_ipv4.s_addr = 0; 203 &mini_external_ipv4);
206 break; 204 mini_external_ipv4.s_addr = 0;
207 } 205 break;
206 }
208} 207}
209 208
210 209
@@ -215,20 +214,20 @@ handle_external_ip (void *cls,
215 * @param cls NULL 214 * @param cls NULL
216 */ 215 */
217static void 216static void
218run_external_ip (void *cls) 217run_external_ip(void *cls)
219{ 218{
220 probe_external_ip_task 219 probe_external_ip_task
221 = GNUNET_SCHEDULER_add_delayed (EXTERN_IP_RETRY_TIMEOUT, 220 = GNUNET_SCHEDULER_add_delayed(EXTERN_IP_RETRY_TIMEOUT,
222 &run_external_ip, 221 &run_external_ip,
223 NULL); 222 NULL);
224 if (NULL != probe_external_ip_op) 223 if (NULL != probe_external_ip_op)
225 { 224 {
226 GNUNET_NAT_mini_get_external_ipv4_cancel_ (probe_external_ip_op); 225 GNUNET_NAT_mini_get_external_ipv4_cancel_(probe_external_ip_op);
227 probe_external_ip_op = NULL; 226 probe_external_ip_op = NULL;
228 } 227 }
229 probe_external_ip_op 228 probe_external_ip_op
230 = GNUNET_NAT_mini_get_external_ipv4_ (&handle_external_ip, 229 = GNUNET_NAT_mini_get_external_ipv4_(&handle_external_ip,
231 NULL); 230 NULL);
232} 231}
233 232
234 233
@@ -239,32 +238,32 @@ run_external_ip (void *cls)
239 * @param have_nat #GNUNET_YES if we believe we are behind NAT 238 * @param have_nat #GNUNET_YES if we believe we are behind NAT
240 */ 239 */
241void 240void
242GN_nat_status_changed (int have_nat) 241GN_nat_status_changed(int have_nat)
243{ 242{
244 if (GNUNET_YES != enable_upnp) 243 if (GNUNET_YES != enable_upnp)
245 return; 244 return;
246 if ( (GNUNET_YES == have_nat) && 245 if ((GNUNET_YES == have_nat) &&
247 (NULL == probe_external_ip_task) && 246 (NULL == probe_external_ip_task) &&
248 (NULL == probe_external_ip_op) ) 247 (NULL == probe_external_ip_op))
249 {
250 probe_external_ip_task
251 = GNUNET_SCHEDULER_add_now (&run_external_ip,
252 NULL);
253 return;
254 }
255 if (GNUNET_NO == have_nat)
256 {
257 if (NULL != probe_external_ip_task)
258 { 248 {
259 GNUNET_SCHEDULER_cancel (probe_external_ip_task); 249 probe_external_ip_task
260 probe_external_ip_task = NULL; 250 = GNUNET_SCHEDULER_add_now(&run_external_ip,
251 NULL);
252 return;
261 } 253 }
262 if (NULL != probe_external_ip_op) 254 if (GNUNET_NO == have_nat)
263 { 255 {
264 GNUNET_NAT_mini_get_external_ipv4_cancel_ (probe_external_ip_op); 256 if (NULL != probe_external_ip_task)
265 probe_external_ip_op = NULL; 257 {
258 GNUNET_SCHEDULER_cancel(probe_external_ip_task);
259 probe_external_ip_task = NULL;
260 }
261 if (NULL != probe_external_ip_op)
262 {
263 GNUNET_NAT_mini_get_external_ipv4_cancel_(probe_external_ip_op);
264 probe_external_ip_op = NULL;
265 }
266 } 266 }
267 }
268} 267}
269 268
270 269
@@ -276,21 +275,21 @@ GN_nat_status_changed (int have_nat)
276 * @return handle to cancel 275 * @return handle to cancel
277 */ 276 */
278struct GN_ExternalIPMonitor * 277struct GN_ExternalIPMonitor *
279GN_external_ipv4_monitor_start (GN_NotifyExternalIPv4Change cb, 278GN_external_ipv4_monitor_start(GN_NotifyExternalIPv4Change cb,
280 void *cb_cls) 279 void *cb_cls)
281{ 280{
282 struct GN_ExternalIPMonitor *mon; 281 struct GN_ExternalIPMonitor *mon;
283 282
284 mon = GNUNET_new (struct GN_ExternalIPMonitor); 283 mon = GNUNET_new(struct GN_ExternalIPMonitor);
285 mon->cb = cb; 284 mon->cb = cb;
286 mon->cb_cls = cb_cls; 285 mon->cb_cls = cb_cls;
287 GNUNET_CONTAINER_DLL_insert (mon_head, 286 GNUNET_CONTAINER_DLL_insert(mon_head,
288 mon_tail, 287 mon_tail,
289 mon); 288 mon);
290 if (0 != mini_external_ipv4.s_addr) 289 if (0 != mini_external_ipv4.s_addr)
291 cb (cb_cls, 290 cb(cb_cls,
292 &mini_external_ipv4, 291 &mini_external_ipv4,
293 GNUNET_YES); 292 GNUNET_YES);
294 return mon; 293 return mon;
295} 294}
296 295
@@ -301,12 +300,12 @@ GN_external_ipv4_monitor_start (GN_NotifyExternalIPv4Change cb,
301 * @param mon monitor to call 300 * @param mon monitor to call
302 */ 301 */
303void 302void
304GN_external_ipv4_monitor_stop (struct GN_ExternalIPMonitor *mon) 303GN_external_ipv4_monitor_stop(struct GN_ExternalIPMonitor *mon)
305{ 304{
306 GNUNET_CONTAINER_DLL_remove (mon_head, 305 GNUNET_CONTAINER_DLL_remove(mon_head,
307 mon_tail, 306 mon_tail,
308 mon); 307 mon);
309 GNUNET_free (mon); 308 GNUNET_free(mon);
310} 309}
311 310
312/* end of gnunet-service-nat_externalip.c */ 311/* end of gnunet-service-nat_externalip.c */
diff --git a/src/nat/gnunet-service-nat_externalip.h b/src/nat/gnunet-service-nat_externalip.h
index f93b6793d..41ab49e2f 100644
--- a/src/nat/gnunet-service-nat_externalip.h
+++ b/src/nat/gnunet-service-nat_externalip.h
@@ -11,19 +11,19 @@
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 * Code to figure out what our external IPv4 address(es) might 21 * Code to figure out what our external IPv4 address(es) might
22 * be (external IPv4s are what is seen on the rest of the Internet). 22 * be (external IPv4s are what is seen on the rest of the Internet).
23 * 23 *
24 * This can be implemented using different methods, and we allow 24 * This can be implemented using different methods, and we allow
25 * the main service to be notified about changes to what we believe 25 * the main service to be notified about changes to what we believe
26 * is our external IPv4 address. 26 * is our external IPv4 address.
27 * 27 *
28 * Note that this is explicitly only about NATed systems; if one 28 * Note that this is explicitly only about NATed systems; if one
29 * of our network interfaces has a global IP address this does 29 * of our network interfaces has a global IP address this does
@@ -46,7 +46,7 @@
46 * @param have_nat #GNUNET_YES if we believe we are behind NAT 46 * @param have_nat #GNUNET_YES if we believe we are behind NAT
47 */ 47 */
48void 48void
49GN_nat_status_changed (int have_nat); 49GN_nat_status_changed(int have_nat);
50 50
51 51
52/** 52/**
@@ -58,8 +58,8 @@ GN_nat_status_changed (int have_nat);
58 */ 58 */
59typedef void 59typedef void
60(*GN_NotifyExternalIPv4Change)(void *cls, 60(*GN_NotifyExternalIPv4Change)(void *cls,
61 const struct in_addr *ip, 61 const struct in_addr *ip,
62 int add_remove); 62 int add_remove);
63 63
64 64
65/** 65/**
@@ -76,8 +76,8 @@ struct GN_ExternalIPMonitor;
76 * @return handle to cancel 76 * @return handle to cancel
77 */ 77 */
78struct GN_ExternalIPMonitor * 78struct GN_ExternalIPMonitor *
79GN_external_ipv4_monitor_start (GN_NotifyExternalIPv4Change cb, 79GN_external_ipv4_monitor_start(GN_NotifyExternalIPv4Change cb,
80 void *cb_cls); 80 void *cb_cls);
81 81
82 82
83/** 83/**
@@ -86,7 +86,7 @@ GN_external_ipv4_monitor_start (GN_NotifyExternalIPv4Change cb,
86 * @param mon monitor to call 86 * @param mon monitor to call
87 */ 87 */
88void 88void
89GN_external_ipv4_monitor_stop (struct GN_ExternalIPMonitor *mon); 89GN_external_ipv4_monitor_stop(struct GN_ExternalIPMonitor *mon);
90 90
91 91
92#endif 92#endif
diff --git a/src/nat/gnunet-service-nat_helper.c b/src/nat/gnunet-service-nat_helper.c
index cf5038dd7..42612751d 100644
--- a/src/nat/gnunet-service-nat_helper.c
+++ b/src/nat/gnunet-service-nat_helper.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/** 21/**
22 * @file nat/gnunet-service-nat_helper.c 22 * @file nat/gnunet-service-nat_helper.c
@@ -32,9 +32,7 @@
32/** 32/**
33 * Information we keep per NAT helper process. 33 * Information we keep per NAT helper process.
34 */ 34 */
35struct HelperContext 35struct HelperContext {
36{
37
38 /** 36 /**
39 * IP address we pass to the NAT helper. 37 * IP address we pass to the NAT helper.
40 */ 38 */
@@ -89,7 +87,7 @@ struct HelperContext
89 * @param cls a `struct HelperContext` 87 * @param cls a `struct HelperContext`
90 */ 88 */
91static void 89static void
92restart_nat_server (void *cls); 90restart_nat_server(void *cls);
93 91
94 92
95/** 93/**
@@ -98,13 +96,13 @@ restart_nat_server (void *cls);
98 * @param h context of the helper 96 * @param h context of the helper
99 */ 97 */
100static void 98static void
101try_again (struct HelperContext *h) 99try_again(struct HelperContext *h)
102{ 100{
103 GNUNET_assert (NULL == h->server_read_task); 101 GNUNET_assert(NULL == h->server_read_task);
104 h->server_retry_delay = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay); 102 h->server_retry_delay = GNUNET_TIME_STD_BACKOFF(h->server_retry_delay);
105 h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay, 103 h->server_read_task = GNUNET_SCHEDULER_add_delayed(h->server_retry_delay,
106 &restart_nat_server, 104 &restart_nat_server,
107 h); 105 h);
108} 106}
109 107
110 108
@@ -116,7 +114,7 @@ try_again (struct HelperContext *h)
116 * @param cls the `struct HelperContext` 114 * @param cls the `struct HelperContext`
117 */ 115 */
118static void 116static void
119nat_server_read (void *cls) 117nat_server_read(void *cls)
120{ 118{
121 struct HelperContext *h = cls; 119 struct HelperContext *h = cls;
122 char mybuf[40]; 120 char mybuf[40];
@@ -126,73 +124,73 @@ nat_server_read (void *cls)
126 struct sockaddr_in sin_addr; 124 struct sockaddr_in sin_addr;
127 125
128 h->server_read_task = NULL; 126 h->server_read_task = NULL;
129 memset (mybuf, 0, sizeof (mybuf)); 127 memset(mybuf, 0, sizeof(mybuf));
130 bytes = 128 bytes =
131 GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof (mybuf)); 129 GNUNET_DISK_file_read(h->server_stdout_handle, mybuf, sizeof(mybuf));
132 if (bytes < 1) 130 if (bytes < 1)
133 {
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Finished reading from server stdout with code: %d\n",
136 (int) bytes);
137 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
138 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "kill");
139 GNUNET_OS_process_wait (h->server_proc);
140 GNUNET_OS_process_destroy (h->server_proc);
141 h->server_proc = NULL;
142 GNUNET_DISK_pipe_close (h->server_stdout);
143 h->server_stdout = NULL;
144 h->server_stdout_handle = NULL;
145 try_again (h);
146 return;
147 }
148
149 port_start = NULL;
150 for (size_t i = 0; i < sizeof (mybuf); i++)
151 {
152 if (mybuf[i] == '\n')
153 { 131 {
154 mybuf[i] = '\0'; 132 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
155 break; 133 "Finished reading from server stdout with code: %d\n",
134 (int)bytes);
135 if (0 != GNUNET_OS_process_kill(h->server_proc, GNUNET_TERM_SIG))
136 GNUNET_log_from_strerror(GNUNET_ERROR_TYPE_WARNING, "nat", "kill");
137 GNUNET_OS_process_wait(h->server_proc);
138 GNUNET_OS_process_destroy(h->server_proc);
139 h->server_proc = NULL;
140 GNUNET_DISK_pipe_close(h->server_stdout);
141 h->server_stdout = NULL;
142 h->server_stdout_handle = NULL;
143 try_again(h);
144 return;
156 } 145 }
157 if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf))) 146
147 port_start = NULL;
148 for (size_t i = 0; i < sizeof(mybuf); i++)
158 { 149 {
159 mybuf[i] = '\0'; 150 if (mybuf[i] == '\n')
160 port_start = &mybuf[i + 1]; 151 {
152 mybuf[i] = '\0';
153 break;
154 }
155 if ((mybuf[i] == ':') && (i + 1 < sizeof(mybuf)))
156 {
157 mybuf[i] = '\0';
158 port_start = &mybuf[i + 1];
159 }
161 } 160 }
162 }
163 161
164 /* construct socket address of sender */ 162 /* construct socket address of sender */
165 memset (&sin_addr, 0, sizeof (sin_addr)); 163 memset(&sin_addr, 0, sizeof(sin_addr));
166 sin_addr.sin_family = AF_INET; 164 sin_addr.sin_family = AF_INET;
167#if HAVE_SOCKADDR_IN_SIN_LEN 165#if HAVE_SOCKADDR_IN_SIN_LEN
168 sin_addr.sin_len = sizeof (sin_addr); 166 sin_addr.sin_len = sizeof(sin_addr);
169#endif 167#endif
170 if ((NULL == port_start) || (1 != sscanf (port_start, "%d", &port)) || 168 if ((NULL == port_start) || (1 != sscanf(port_start, "%d", &port)) ||
171 (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr))) 169 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)))
172 { 170 {
173 /* should we restart gnunet-helper-nat-server? */ 171 /* should we restart gnunet-helper-nat-server? */
174 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 172 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
175 _ ( 173 _(
176 "gnunet-helper-nat-server generated malformed address `%s'\n"), 174 "gnunet-helper-nat-server generated malformed address `%s'\n"),
177 mybuf); 175 mybuf);
178 h->server_read_task = 176 h->server_read_task =
179 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 177 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL,
180 h->server_stdout_handle, 178 h->server_stdout_handle,
181 &nat_server_read, 179 &nat_server_read,
182 h); 180 h);
183 return; 181 return;
184 } 182 }
185 sin_addr.sin_port = htons ((uint16_t) port); 183 sin_addr.sin_port = htons((uint16_t)port);
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 184 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
187 "gnunet-helper-nat-server read: %s:%d\n", 185 "gnunet-helper-nat-server read: %s:%d\n",
188 mybuf, 186 mybuf,
189 port); 187 port);
190 h->cb (h->cb_cls, &sin_addr); 188 h->cb(h->cb_cls, &sin_addr);
191 h->server_read_task = 189 h->server_read_task =
192 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 190 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL,
193 h->server_stdout_handle, 191 h->server_stdout_handle,
194 &nat_server_read, 192 &nat_server_read,
195 h); 193 h);
196} 194}
197 195
198 196
@@ -203,67 +201,67 @@ nat_server_read (void *cls)
203 * @param cls a `struct HelperContext` 201 * @param cls a `struct HelperContext`
204 */ 202 */
205static void 203static void
206restart_nat_server (void *cls) 204restart_nat_server(void *cls)
207{ 205{
208 struct HelperContext *h = cls; 206 struct HelperContext *h = cls;
209 char *binary; 207 char *binary;
210 char ia[INET_ADDRSTRLEN]; 208 char ia[INET_ADDRSTRLEN];
211 209
212 h->server_read_task = NULL; 210 h->server_read_task = NULL;
213 GNUNET_assert (NULL != 211 GNUNET_assert(NULL !=
214 inet_ntop (AF_INET, &h->internal_address, ia, sizeof (ia))); 212 inet_ntop(AF_INET, &h->internal_address, ia, sizeof(ia)));
215 /* Start the server process */ 213 /* Start the server process */
216 binary = GNUNET_OS_get_suid_binary_path (h->cfg, "gnunet-helper-nat-server"); 214 binary = GNUNET_OS_get_suid_binary_path(h->cfg, "gnunet-helper-nat-server");
217 if (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, ia)) 215 if (GNUNET_YES != GNUNET_OS_check_helper_binary(binary, GNUNET_YES, ia))
218 { 216 {
219 /* move instantly to max delay, as this is unlikely to be fixed */ 217 /* move instantly to max delay, as this is unlikely to be fixed */
220 h->server_retry_delay = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD; 218 h->server_retry_delay = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD;
221 GNUNET_free (binary); 219 GNUNET_free(binary);
222 try_again (h); 220 try_again(h);
223 return; 221 return;
224 } 222 }
225 h->server_stdout = 223 h->server_stdout =
226 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 224 GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
227 if (NULL == h->server_stdout) 225 if (NULL == h->server_stdout)
228 { 226 {
229 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe"); 227 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "pipe");
230 GNUNET_free (binary); 228 GNUNET_free(binary);
231 try_again (h); 229 try_again(h);
232 return; 230 return;
233 } 231 }
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 232 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
235 "Starting `%s' at `%s'\n", 233 "Starting `%s' at `%s'\n",
236 "gnunet-helper-nat-server", 234 "gnunet-helper-nat-server",
237 ia); 235 ia);
238 h->server_proc = GNUNET_OS_start_process (GNUNET_NO, 236 h->server_proc = GNUNET_OS_start_process(GNUNET_NO,
239 0, 237 0,
240 NULL, 238 NULL,
241 h->server_stdout, 239 h->server_stdout,
242 NULL, 240 NULL,
243 binary, 241 binary,
244 "gnunet-helper-nat-server", 242 "gnunet-helper-nat-server",
245 ia, 243 ia,
246 NULL); 244 NULL);
247 GNUNET_free (binary); 245 GNUNET_free(binary);
248 if (NULL == h->server_proc) 246 if (NULL == h->server_proc)
249 { 247 {
250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 248 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
251 _ ("Failed to start %s\n"), 249 _("Failed to start %s\n"),
252 "gnunet-helper-nat-server"); 250 "gnunet-helper-nat-server");
253 GNUNET_DISK_pipe_close (h->server_stdout); 251 GNUNET_DISK_pipe_close(h->server_stdout);
254 h->server_stdout = NULL; 252 h->server_stdout = NULL;
255 try_again (h); 253 try_again(h);
256 return; 254 return;
257 } 255 }
258 /* Close the write end of the read pipe */ 256 /* Close the write end of the read pipe */
259 GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE); 257 GNUNET_DISK_pipe_close_end(h->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
260 h->server_stdout_handle = 258 h->server_stdout_handle =
261 GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ); 259 GNUNET_DISK_pipe_handle(h->server_stdout, GNUNET_DISK_PIPE_END_READ);
262 h->server_read_task = 260 h->server_read_task =
263 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 261 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL,
264 h->server_stdout_handle, 262 h->server_stdout_handle,
265 &nat_server_read, 263 &nat_server_read,
266 h); 264 h);
267} 265}
268 266
269 267
@@ -278,24 +276,24 @@ restart_nat_server (void *cls)
278 * @return NULL on error 276 * @return NULL on error
279 */ 277 */
280struct HelperContext * 278struct HelperContext *
281GN_start_gnunet_nat_server_ (const struct in_addr *internal_address, 279GN_start_gnunet_nat_server_(const struct in_addr *internal_address,
282 GN_ReversalCallback cb, 280 GN_ReversalCallback cb,
283 void *cb_cls, 281 void *cb_cls,
284 const struct GNUNET_CONFIGURATION_Handle *cfg) 282 const struct GNUNET_CONFIGURATION_Handle *cfg)
285{ 283{
286 struct HelperContext *h; 284 struct HelperContext *h;
287 285
288 h = GNUNET_new (struct HelperContext); 286 h = GNUNET_new(struct HelperContext);
289 h->cb = cb; 287 h->cb = cb;
290 h->cb_cls = cb_cls; 288 h->cb_cls = cb_cls;
291 h->internal_address = *internal_address; 289 h->internal_address = *internal_address;
292 h->cfg = cfg; 290 h->cfg = cfg;
293 restart_nat_server (h); 291 restart_nat_server(h);
294 if (NULL == h->server_stdout) 292 if (NULL == h->server_stdout)
295 { 293 {
296 GN_stop_gnunet_nat_server_ (h); 294 GN_stop_gnunet_nat_server_(h);
297 return NULL; 295 return NULL;
298 } 296 }
299 return h; 297 return h;
300} 298}
301 299
@@ -307,31 +305,31 @@ GN_start_gnunet_nat_server_ (const struct in_addr *internal_address,
307 * @param h helper context to stop 305 * @param h helper context to stop
308 */ 306 */
309void 307void
310GN_stop_gnunet_nat_server_ (struct HelperContext *h) 308GN_stop_gnunet_nat_server_(struct HelperContext *h)
311{ 309{
312 if (NULL != h->server_read_task) 310 if (NULL != h->server_read_task)
313 { 311 {
314 GNUNET_SCHEDULER_cancel (h->server_read_task); 312 GNUNET_SCHEDULER_cancel(h->server_read_task);
315 h->server_read_task = NULL; 313 h->server_read_task = NULL;
316 } 314 }
317 if (NULL != h->server_proc) 315 if (NULL != h->server_proc)
318 { 316 {
319 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG)) 317 if (0 != GNUNET_OS_process_kill(h->server_proc, GNUNET_TERM_SIG))
320 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 318 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
321 GNUNET_OS_process_wait (h->server_proc); 319 GNUNET_OS_process_wait(h->server_proc);
322 GNUNET_OS_process_destroy (h->server_proc); 320 GNUNET_OS_process_destroy(h->server_proc);
323 h->server_proc = NULL; 321 h->server_proc = NULL;
324 GNUNET_DISK_pipe_close (h->server_stdout); 322 GNUNET_DISK_pipe_close(h->server_stdout);
325 h->server_stdout = NULL; 323 h->server_stdout = NULL;
326 h->server_stdout_handle = NULL; 324 h->server_stdout_handle = NULL;
327 } 325 }
328 if (NULL != h->server_stdout) 326 if (NULL != h->server_stdout)
329 { 327 {
330 GNUNET_DISK_pipe_close (h->server_stdout); 328 GNUNET_DISK_pipe_close(h->server_stdout);
331 h->server_stdout = NULL; 329 h->server_stdout = NULL;
332 h->server_stdout_handle = NULL; 330 h->server_stdout_handle = NULL;
333 } 331 }
334 GNUNET_free (h); 332 GNUNET_free(h);
335} 333}
336 334
337 335
@@ -348,10 +346,10 @@ GN_stop_gnunet_nat_server_ (struct HelperContext *h)
348 * #GNUNET_OK otherwise 346 * #GNUNET_OK otherwise
349 */ 347 */
350int 348int
351GN_request_connection_reversal (const struct in_addr *internal_address, 349GN_request_connection_reversal(const struct in_addr *internal_address,
352 uint16_t internal_port, 350 uint16_t internal_port,
353 const struct in_addr *remote_v4, 351 const struct in_addr *remote_v4,
354 const struct GNUNET_CONFIGURATION_Handle *cfg) 352 const struct GNUNET_CONFIGURATION_Handle *cfg)
355{ 353{
356 char intv4[INET_ADDRSTRLEN]; 354 char intv4[INET_ADDRSTRLEN];
357 char remv4[INET_ADDRSTRLEN]; 355 char remv4[INET_ADDRSTRLEN];
@@ -359,44 +357,44 @@ GN_request_connection_reversal (const struct in_addr *internal_address,
359 struct GNUNET_OS_Process *proc; 357 struct GNUNET_OS_Process *proc;
360 char *binary; 358 char *binary;
361 359
362 if (NULL == inet_ntop (AF_INET, internal_address, intv4, INET_ADDRSTRLEN)) 360 if (NULL == inet_ntop(AF_INET, internal_address, intv4, INET_ADDRSTRLEN))
363 { 361 {
364 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); 362 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
365 return GNUNET_SYSERR; 363 return GNUNET_SYSERR;
366 } 364 }
367 if (NULL == inet_ntop (AF_INET, remote_v4, remv4, INET_ADDRSTRLEN)) 365 if (NULL == inet_ntop(AF_INET, remote_v4, remv4, INET_ADDRSTRLEN))
368 { 366 {
369 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); 367 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
370 return GNUNET_SYSERR; 368 return GNUNET_SYSERR;
371 } 369 }
372 GNUNET_snprintf (port_as_string, 370 GNUNET_snprintf(port_as_string,
373 sizeof (port_as_string), 371 sizeof(port_as_string),
374 "%d", 372 "%d",
375 internal_port); 373 internal_port);
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 374 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
377 "Running gnunet-helper-nat-client %s %s %u\n", 375 "Running gnunet-helper-nat-client %s %s %u\n",
378 intv4, 376 intv4,
379 remv4, 377 remv4,
380 internal_port); 378 internal_port);
381 binary = GNUNET_OS_get_suid_binary_path (cfg, "gnunet-helper-nat-client"); 379 binary = GNUNET_OS_get_suid_binary_path(cfg, "gnunet-helper-nat-client");
382 proc = GNUNET_OS_start_process (GNUNET_NO, 380 proc = GNUNET_OS_start_process(GNUNET_NO,
383 0, 381 0,
384 NULL, 382 NULL,
385 NULL, 383 NULL,
386 NULL, 384 NULL,
387 binary, 385 binary,
388 "gnunet-helper-nat-client", 386 "gnunet-helper-nat-client",
389 intv4, 387 intv4,
390 remv4, 388 remv4,
391 port_as_string, 389 port_as_string,
392 NULL); 390 NULL);
393 GNUNET_free (binary); 391 GNUNET_free(binary);
394 if (NULL == proc) 392 if (NULL == proc)
395 return GNUNET_SYSERR; 393 return GNUNET_SYSERR;
396 /* we know that the gnunet-helper-nat-client will terminate virtually 394 /* we know that the gnunet-helper-nat-client will terminate virtually
397 * instantly */ 395 * instantly */
398 GNUNET_OS_process_wait (proc); 396 GNUNET_OS_process_wait(proc);
399 GNUNET_OS_process_destroy (proc); 397 GNUNET_OS_process_destroy(proc);
400 return GNUNET_OK; 398 return GNUNET_OK;
401} 399}
402 400
diff --git a/src/nat/gnunet-service-nat_helper.h b/src/nat/gnunet-service-nat_helper.h
index 4c39a374c..e655149bc 100644
--- a/src/nat/gnunet-service-nat_helper.h
+++ b/src/nat/gnunet-service-nat_helper.h
@@ -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/** 21/**
22 * @file nat/gnunet-service-nat_helper.h 22 * @file nat/gnunet-service-nat_helper.h
@@ -39,11 +39,11 @@ struct HelperContext;
39 * request from another peer. 39 * request from another peer.
40 * 40 *
41 * @param cls closure 41 * @param cls closure
42 * @param ra IP address of the peer who wants us to connect to it 42 * @param ra IP address of the peer who wants us to connect to it
43 */ 43 */
44typedef void 44typedef void
45(*GN_ReversalCallback) (void *cls, 45(*GN_ReversalCallback) (void *cls,
46 const struct sockaddr_in *ra); 46 const struct sockaddr_in *ra);
47 47
48 48
49/** 49/**
@@ -57,12 +57,12 @@ typedef void
57 * @return NULL on error 57 * @return NULL on error
58 */ 58 */
59struct HelperContext * 59struct HelperContext *
60GN_start_gnunet_nat_server_ (const struct in_addr *internal_address, 60GN_start_gnunet_nat_server_(const struct in_addr *internal_address,
61 GN_ReversalCallback cb, 61 GN_ReversalCallback cb,
62 void *cb_cls, 62 void *cb_cls,
63 const struct GNUNET_CONFIGURATION_Handle *cfg); 63 const struct GNUNET_CONFIGURATION_Handle *cfg);
64
64 65
65
66/** 66/**
67 * Start the gnunet-helper-nat-server and process incoming 67 * Start the gnunet-helper-nat-server and process incoming
68 * requests. 68 * requests.
@@ -70,7 +70,7 @@ GN_start_gnunet_nat_server_ (const struct in_addr *internal_address,
70 * @param h helper context to stop 70 * @param h helper context to stop
71 */ 71 */
72void 72void
73GN_stop_gnunet_nat_server_ (struct HelperContext *h); 73GN_stop_gnunet_nat_server_(struct HelperContext *h);
74 74
75 75
76/** 76/**
@@ -86,10 +86,10 @@ GN_stop_gnunet_nat_server_ (struct HelperContext *h);
86 * #GNUNET_OK otherwise 86 * #GNUNET_OK otherwise
87 */ 87 */
88int 88int
89GN_request_connection_reversal (const struct in_addr *internal_address, 89GN_request_connection_reversal(const struct in_addr *internal_address,
90 uint16_t internal_port, 90 uint16_t internal_port,
91 const struct in_addr *remote_v4, 91 const struct in_addr *remote_v4,
92 const struct GNUNET_CONFIGURATION_Handle *cfg); 92 const struct GNUNET_CONFIGURATION_Handle *cfg);
93 93
94 94
95/* end of gnunet-service-nat_helper.h */ 95/* end of gnunet-service-nat_helper.h */
diff --git a/src/nat/gnunet-service-nat_mini.c b/src/nat/gnunet-service-nat_mini.c
index 375188ff2..94534e81c 100644
--- a/src/nat/gnunet-service-nat_mini.c
+++ b/src/nat/gnunet-service-nat_mini.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/** 21/**
22 * @file nat/gnunet-service-nat_mini.c 22 * @file nat/gnunet-service-nat_mini.c
@@ -29,24 +29,24 @@
29#include "gnunet-service-nat_mini.h" 29#include "gnunet-service-nat_mini.h"
30#include "nat.h" 30#include "nat.h"
31 31
32#define LOG(kind, ...) GNUNET_log_from (kind, "nat", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from(kind, "nat", __VA_ARGS__)
33 33
34/** 34/**
35 * How long do we give upnpc to create a mapping? 35 * How long do we give upnpc to create a mapping?
36 */ 36 */
37#define MAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15) 37#define MAP_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15)
38 38
39/** 39/**
40 * How long do we give upnpc to remove a mapping? 40 * How long do we give upnpc to remove a mapping?
41 */ 41 */
42#define UNMAP_TIMEOUT \ 42#define UNMAP_TIMEOUT \
43 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1) 43 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1)
44 44
45/** 45/**
46 * How often do we check for changes in the mapping? 46 * How often do we check for changes in the mapping?
47 */ 47 */
48#define MAP_REFRESH_FREQ \ 48#define MAP_REFRESH_FREQ \
49 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) 49 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
50 50
51 51
52/* ************************* external-ip calling ************************ */ 52/* ************************* external-ip calling ************************ */
@@ -54,9 +54,7 @@
54/** 54/**
55 * Opaque handle to cancel "GNUNET_NAT_mini_get_external_ipv4" operation. 55 * Opaque handle to cancel "GNUNET_NAT_mini_get_external_ipv4" operation.
56 */ 56 */
57struct GNUNET_NAT_ExternalHandle 57struct GNUNET_NAT_ExternalHandle {
58{
59
60 /** 58 /**
61 * Function to call with the result. 59 * Function to call with the result.
62 */ 60 */
@@ -111,43 +109,43 @@ struct GNUNET_NAT_ExternalHandle
111 * @param cls the `struct GNUNET_NAT_ExternalHandle` 109 * @param cls the `struct GNUNET_NAT_ExternalHandle`
112 */ 110 */
113static void 111static void
114read_external_ipv4 (void *cls) 112read_external_ipv4(void *cls)
115{ 113{
116 struct GNUNET_NAT_ExternalHandle *eh = cls; 114 struct GNUNET_NAT_ExternalHandle *eh = cls;
117 ssize_t ret; 115 ssize_t ret;
118 struct in_addr addr; 116 struct in_addr addr;
119 117
120 eh->task = NULL; 118 eh->task = NULL;
121 ret = GNUNET_DISK_file_read (eh->r, 119 ret = GNUNET_DISK_file_read(eh->r,
122 &eh->buf[eh->off], 120 &eh->buf[eh->off],
123 sizeof (eh->buf) - eh->off); 121 sizeof(eh->buf) - eh->off);
124 if (ret > 0) 122 if (ret > 0)
125 { 123 {
126 /* try to read more */ 124 /* try to read more */
127 eh->off += ret; 125 eh->off += ret;
128 eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 126 eh->task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL,
129 eh->r, 127 eh->r,
130 &read_external_ipv4, 128 &read_external_ipv4,
131 eh); 129 eh);
132 return; 130 return;
133 } 131 }
134 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID; 132 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID;
135 if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n')) 133 if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n'))
136 {
137 eh->buf[eh->off - 1] = '\0';
138 if (1 == inet_pton (AF_INET, eh->buf, &addr))
139 { 134 {
140 if (0 == addr.s_addr) 135 eh->buf[eh->off - 1] = '\0';
141 eh->ret = 136 if (1 == inet_pton(AF_INET, eh->buf, &addr))
142 GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */ 137 {
143 else 138 if (0 == addr.s_addr)
144 eh->ret = GNUNET_NAT_ERROR_SUCCESS; 139 eh->ret =
140 GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */
141 else
142 eh->ret = GNUNET_NAT_ERROR_SUCCESS;
143 }
145 } 144 }
146 } 145 eh->cb(eh->cb_cls,
147 eh->cb (eh->cb_cls, 146 (GNUNET_NAT_ERROR_SUCCESS == eh->ret) ? &addr : NULL,
148 (GNUNET_NAT_ERROR_SUCCESS == eh->ret) ? &addr : NULL, 147 eh->ret);
149 eh->ret); 148 GNUNET_NAT_mini_get_external_ipv4_cancel_(eh);
150 GNUNET_NAT_mini_get_external_ipv4_cancel_ (eh);
151} 149}
152 150
153 151
@@ -157,13 +155,13 @@ read_external_ipv4 (void *cls)
157 * @param cls the `struct GNUNET_NAT_ExternalHandle` (freed) 155 * @param cls the `struct GNUNET_NAT_ExternalHandle` (freed)
158 */ 156 */
159static void 157static void
160signal_external_ip_error (void *cls) 158signal_external_ip_error(void *cls)
161{ 159{
162 struct GNUNET_NAT_ExternalHandle *eh = cls; 160 struct GNUNET_NAT_ExternalHandle *eh = cls;
163 161
164 eh->task = NULL; 162 eh->task = NULL;
165 eh->cb (eh->cb_cls, NULL, eh->ret); 163 eh->cb(eh->cb_cls, NULL, eh->ret);
166 GNUNET_free (eh); 164 GNUNET_free(eh);
167} 165}
168 166
169 167
@@ -175,52 +173,52 @@ signal_external_ip_error (void *cls)
175 * @return handle for cancellation (can only be used until @a cb is called), never NULL 173 * @return handle for cancellation (can only be used until @a cb is called), never NULL
176 */ 174 */
177struct GNUNET_NAT_ExternalHandle * 175struct GNUNET_NAT_ExternalHandle *
178GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, void *cb_cls) 176GNUNET_NAT_mini_get_external_ipv4_(GNUNET_NAT_IPCallback cb, void *cb_cls)
179{ 177{
180 struct GNUNET_NAT_ExternalHandle *eh; 178 struct GNUNET_NAT_ExternalHandle *eh;
181 179
182 eh = GNUNET_new (struct GNUNET_NAT_ExternalHandle); 180 eh = GNUNET_new(struct GNUNET_NAT_ExternalHandle);
183 eh->cb = cb; 181 eh->cb = cb;
184 eh->cb_cls = cb_cls; 182 eh->cb_cls = cb_cls;
185 eh->ret = GNUNET_NAT_ERROR_SUCCESS; 183 eh->ret = GNUNET_NAT_ERROR_SUCCESS;
186 if (GNUNET_SYSERR == 184 if (GNUNET_SYSERR ==
187 GNUNET_OS_check_helper_binary ("external-ip", GNUNET_NO, NULL)) 185 GNUNET_OS_check_helper_binary("external-ip", GNUNET_NO, NULL))
188 { 186 {
189 LOG (GNUNET_ERROR_TYPE_INFO, _ ("`external-ip' command not found\n")); 187 LOG(GNUNET_ERROR_TYPE_INFO, _("`external-ip' command not found\n"));
190 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND; 188 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND;
191 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); 189 eh->task = GNUNET_SCHEDULER_add_now(&signal_external_ip_error, eh);
192 return eh; 190 return eh;
193 } 191 }
194 LOG (GNUNET_ERROR_TYPE_DEBUG, 192 LOG(GNUNET_ERROR_TYPE_DEBUG,
195 "Running `external-ip' to determine our external IP\n"); 193 "Running `external-ip' to determine our external IP\n");
196 eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 194 eh->opipe = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
197 if (NULL == eh->opipe) 195 if (NULL == eh->opipe)
198 { 196 {
199 eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE; 197 eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE;
200 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); 198 eh->task = GNUNET_SCHEDULER_add_now(&signal_external_ip_error, eh);
201 return eh; 199 return eh;
202 } 200 }
203 eh->eip = GNUNET_OS_start_process (GNUNET_NO, 201 eh->eip = GNUNET_OS_start_process(GNUNET_NO,
204 0, 202 0,
205 NULL, 203 NULL,
206 eh->opipe, 204 eh->opipe,
207 NULL, 205 NULL,
208 "external-ip", 206 "external-ip",
209 "external-ip", 207 "external-ip",
210 NULL); 208 NULL);
211 if (NULL == eh->eip) 209 if (NULL == eh->eip)
212 { 210 {
213 GNUNET_DISK_pipe_close (eh->opipe); 211 GNUNET_DISK_pipe_close(eh->opipe);
214 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED; 212 eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED;
215 eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); 213 eh->task = GNUNET_SCHEDULER_add_now(&signal_external_ip_error, eh);
216 return eh; 214 return eh;
217 } 215 }
218 GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE); 216 GNUNET_DISK_pipe_close_end(eh->opipe, GNUNET_DISK_PIPE_END_WRITE);
219 eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ); 217 eh->r = GNUNET_DISK_pipe_handle(eh->opipe, GNUNET_DISK_PIPE_END_READ);
220 eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 218 eh->task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL,
221 eh->r, 219 eh->r,
222 &read_external_ipv4, 220 &read_external_ipv4,
223 eh); 221 eh);
224 return eh; 222 return eh;
225} 223}
226 224
@@ -231,25 +229,25 @@ GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, void *cb_cls)
231 * @param eh operation to cancel 229 * @param eh operation to cancel
232 */ 230 */
233void 231void
234GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct GNUNET_NAT_ExternalHandle *eh) 232GNUNET_NAT_mini_get_external_ipv4_cancel_(struct GNUNET_NAT_ExternalHandle *eh)
235{ 233{
236 if (NULL != eh->eip) 234 if (NULL != eh->eip)
237 { 235 {
238 (void) GNUNET_OS_process_kill (eh->eip, SIGKILL); 236 (void)GNUNET_OS_process_kill(eh->eip, SIGKILL);
239 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (eh->eip)); 237 GNUNET_break(GNUNET_OK == GNUNET_OS_process_wait(eh->eip));
240 GNUNET_OS_process_destroy (eh->eip); 238 GNUNET_OS_process_destroy(eh->eip);
241 } 239 }
242 if (NULL != eh->opipe) 240 if (NULL != eh->opipe)
243 { 241 {
244 GNUNET_DISK_pipe_close (eh->opipe); 242 GNUNET_DISK_pipe_close(eh->opipe);
245 eh->opipe = NULL; 243 eh->opipe = NULL;
246 } 244 }
247 if (NULL != eh->task) 245 if (NULL != eh->task)
248 { 246 {
249 GNUNET_SCHEDULER_cancel (eh->task); 247 GNUNET_SCHEDULER_cancel(eh->task);
250 eh->task = NULL; 248 eh->task = NULL;
251 } 249 }
252 GNUNET_free (eh); 250 GNUNET_free(eh);
253} 251}
254 252
255 253
@@ -259,9 +257,7 @@ GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct GNUNET_NAT_ExternalHandle *eh)
259/** 257/**
260 * Handle to a mapping created with upnpc. 258 * Handle to a mapping created with upnpc.
261 */ 259 */
262struct GNUNET_NAT_MiniHandle 260struct GNUNET_NAT_MiniHandle {
263{
264
265 /** 261 /**
266 * Function to call on mapping changes. 262 * Function to call on mapping changes.
267 */ 263 */
@@ -326,7 +322,7 @@ struct GNUNET_NAT_MiniHandle
326 * @param cls the `struct GNUNET_NAT_MiniHandle` 322 * @param cls the `struct GNUNET_NAT_MiniHandle`
327 */ 323 */
328static void 324static void
329do_refresh (void *cls); 325do_refresh(void *cls);
330 326
331 327
332/** 328/**
@@ -336,7 +332,7 @@ do_refresh (void *cls);
336 * @param line line of output, NULL at the end 332 * @param line line of output, NULL at the end
337 */ 333 */
338static void 334static void
339process_map_output (void *cls, const char *line); 335process_map_output(void *cls, const char *line);
340 336
341 337
342/** 338/**
@@ -345,29 +341,29 @@ process_map_output (void *cls, const char *line);
345 * @param mini our handle 341 * @param mini our handle
346 */ 342 */
347static void 343static void
348run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini) 344run_upnpc_r(struct GNUNET_NAT_MiniHandle *mini)
349{ 345{
350 char pstr[6]; 346 char pstr[6];
351 347
352 GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) mini->port); 348 GNUNET_snprintf(pstr, sizeof(pstr), "%u", (unsigned int)mini->port);
353 mini->map_cmd = GNUNET_OS_command_run (&process_map_output, 349 mini->map_cmd = GNUNET_OS_command_run(&process_map_output,
354 mini, 350 mini,
355 MAP_TIMEOUT, 351 MAP_TIMEOUT,
356 "upnpc", 352 "upnpc",
357 "upnpc", 353 "upnpc",
358 "-r", 354 "-r",
359 pstr, 355 pstr,
360 mini->is_tcp ? "tcp" : "udp", 356 mini->is_tcp ? "tcp" : "udp",
361 NULL); 357 NULL);
362 if (NULL == mini->map_cmd) 358 if (NULL == mini->map_cmd)
363 { 359 {
364 mini->ac (mini->ac_cls, 360 mini->ac(mini->ac_cls,
365 GNUNET_SYSERR, 361 GNUNET_SYSERR,
366 NULL, 362 NULL,
367 0, 363 0,
368 GNUNET_NAT_ERROR_UPNPC_FAILED); 364 GNUNET_NAT_ERROR_UPNPC_FAILED);
369 return; 365 return;
370 } 366 }
371} 367}
372 368
373 369
@@ -379,7 +375,7 @@ run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini)
379 * @param line line of output, NULL at the end 375 * @param line line of output, NULL at the end
380 */ 376 */
381static void 377static void
382process_refresh_output (void *cls, const char *line) 378process_refresh_output(void *cls, const char *line)
383{ 379{
384 struct GNUNET_NAT_MiniHandle *mini = cls; 380 struct GNUNET_NAT_MiniHandle *mini = cls;
385 char pstr[9]; 381 char pstr[9];
@@ -388,53 +384,53 @@ process_refresh_output (void *cls, const char *line)
388 struct in_addr exip; 384 struct in_addr exip;
389 385
390 if (NULL == line) 386 if (NULL == line)
391 {
392 GNUNET_OS_command_stop (mini->refresh_cmd);
393 mini->refresh_cmd = NULL;
394 if (GNUNET_NO == mini->found)
395 { 387 {
396 /* mapping disappeared, try to re-create */ 388 GNUNET_OS_command_stop(mini->refresh_cmd);
397 if (GNUNET_YES == mini->did_map) 389 mini->refresh_cmd = NULL;
398 { 390 if (GNUNET_NO == mini->found)
399 mini->ac (mini->ac_cls, 391 {
400 GNUNET_NO, 392 /* mapping disappeared, try to re-create */
401 (const struct sockaddr *) &mini->current_addr, 393 if (GNUNET_YES == mini->did_map)
402 sizeof (mini->current_addr), 394 {
403 GNUNET_NAT_ERROR_SUCCESS); 395 mini->ac(mini->ac_cls,
404 mini->did_map = GNUNET_NO; 396 GNUNET_NO,
405 } 397 (const struct sockaddr *)&mini->current_addr,
406 run_upnpc_r (mini); 398 sizeof(mini->current_addr),
399 GNUNET_NAT_ERROR_SUCCESS);
400 mini->did_map = GNUNET_NO;
401 }
402 run_upnpc_r(mini);
403 }
404 return;
407 } 405 }
408 return; 406 if (!mini->did_map)
409 }
410 if (! mini->did_map)
411 return; /* never mapped, won't find our mapping anyway */ 407 return; /* never mapped, won't find our mapping anyway */
412 408
413 /* we're looking for output of the form: 409 /* we're looking for output of the form:
414 * "ExternalIPAddress = 12.134.41.124" */ 410 * "ExternalIPAddress = 12.134.41.124" */
415 411
416 s = strstr (line, "ExternalIPAddress = "); 412 s = strstr(line, "ExternalIPAddress = ");
417 if (NULL != s) 413 if (NULL != s)
418 { 414 {
419 s += strlen ("ExternalIPAddress = "); 415 s += strlen("ExternalIPAddress = ");
420 if (1 != inet_pton (AF_INET, s, &exip)) 416 if (1 != inet_pton(AF_INET, s, &exip))
421 return; /* skip */ 417 return; /* skip */
422 if (exip.s_addr == mini->current_addr.sin_addr.s_addr) 418 if (exip.s_addr == mini->current_addr.sin_addr.s_addr)
423 return; /* no change */ 419 return; /* no change */
424 /* update mapping */ 420 /* update mapping */
425 mini->ac (mini->ac_cls, 421 mini->ac(mini->ac_cls,
426 GNUNET_NO, 422 GNUNET_NO,
427 (const struct sockaddr *) &mini->current_addr, 423 (const struct sockaddr *)&mini->current_addr,
428 sizeof (mini->current_addr), 424 sizeof(mini->current_addr),
429 GNUNET_NAT_ERROR_SUCCESS); 425 GNUNET_NAT_ERROR_SUCCESS);
430 mini->current_addr.sin_addr = exip; 426 mini->current_addr.sin_addr = exip;
431 mini->ac (mini->ac_cls, 427 mini->ac(mini->ac_cls,
432 GNUNET_YES, 428 GNUNET_YES,
433 (const struct sockaddr *) &mini->current_addr, 429 (const struct sockaddr *)&mini->current_addr,
434 sizeof (mini->current_addr), 430 sizeof(mini->current_addr),
435 GNUNET_NAT_ERROR_SUCCESS); 431 GNUNET_NAT_ERROR_SUCCESS);
436 return; 432 return;
437 } 433 }
438 /* 434 /*
439 * we're looking for output of the form: 435 * we're looking for output of the form:
440 * 436 *
@@ -446,32 +442,32 @@ process_refresh_output (void *cls, const char *line)
446 * "%s TCP PORT->STRING:OURPORT *" or 442 * "%s TCP PORT->STRING:OURPORT *" or
447 * "%s UDP PORT->STRING:OURPORT *" 443 * "%s UDP PORT->STRING:OURPORT *"
448 */ 444 */
449 GNUNET_snprintf (pstr, sizeof (pstr), ":%u ", mini->port); 445 GNUNET_snprintf(pstr, sizeof(pstr), ":%u ", mini->port);
450 if (NULL == (s = strstr (line, "->"))) 446 if (NULL == (s = strstr(line, "->")))
451 return; /* skip */ 447 return; /* skip */
452 if (NULL == strstr (s, pstr)) 448 if (NULL == strstr(s, pstr))
453 return; /* skip */ 449 return; /* skip */
454 if (1 != sscanf (line, 450 if (1 != sscanf(line,
455 (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s" 451 (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s"
456 : "%*u UDP %u->%*s:%*u %*s", 452 : "%*u UDP %u->%*s:%*u %*s",
457 &nport)) 453 &nport))
458 return; /* skip */ 454 return; /* skip */
459 mini->found = GNUNET_YES; 455 mini->found = GNUNET_YES;
460 if (nport == ntohs (mini->current_addr.sin_port)) 456 if (nport == ntohs(mini->current_addr.sin_port))
461 return; /* no change */ 457 return; /* no change */
462 458
463 /* external port changed, update mapping */ 459 /* external port changed, update mapping */
464 mini->ac (mini->ac_cls, 460 mini->ac(mini->ac_cls,
465 GNUNET_NO, 461 GNUNET_NO,
466 (const struct sockaddr *) &mini->current_addr, 462 (const struct sockaddr *)&mini->current_addr,
467 sizeof (mini->current_addr), 463 sizeof(mini->current_addr),
468 GNUNET_NAT_ERROR_SUCCESS); 464 GNUNET_NAT_ERROR_SUCCESS);
469 mini->current_addr.sin_port = htons ((uint16_t) nport); 465 mini->current_addr.sin_port = htons((uint16_t)nport);
470 mini->ac (mini->ac_cls, 466 mini->ac(mini->ac_cls,
471 GNUNET_YES, 467 GNUNET_YES,
472 (const struct sockaddr *) &mini->current_addr, 468 (const struct sockaddr *)&mini->current_addr,
473 sizeof (mini->current_addr), 469 sizeof(mini->current_addr),
474 GNUNET_NAT_ERROR_SUCCESS); 470 GNUNET_NAT_ERROR_SUCCESS);
475} 471}
476 472
477 473
@@ -481,44 +477,44 @@ process_refresh_output (void *cls, const char *line)
481 * @param cls the 'struct GNUNET_NAT_MiniHandle' 477 * @param cls the 'struct GNUNET_NAT_MiniHandle'
482 */ 478 */
483static void 479static void
484do_refresh (void *cls) 480do_refresh(void *cls)
485{ 481{
486 struct GNUNET_NAT_MiniHandle *mini = cls; 482 struct GNUNET_NAT_MiniHandle *mini = cls;
487 int ac; 483 int ac;
488 484
489 mini->refresh_task = 485 mini->refresh_task =
490 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini); 486 GNUNET_SCHEDULER_add_delayed(MAP_REFRESH_FREQ, &do_refresh, mini);
491 LOG (GNUNET_ERROR_TYPE_DEBUG, 487 LOG(GNUNET_ERROR_TYPE_DEBUG,
492 "Running `upnpc' to check if our mapping still exists\n"); 488 "Running `upnpc' to check if our mapping still exists\n");
493 mini->found = GNUNET_NO; 489 mini->found = GNUNET_NO;
494 ac = GNUNET_NO; 490 ac = GNUNET_NO;
495 if (NULL != mini->map_cmd) 491 if (NULL != mini->map_cmd)
496 { 492 {
497 /* took way too long, abort it! */ 493 /* took way too long, abort it! */
498 GNUNET_OS_command_stop (mini->map_cmd); 494 GNUNET_OS_command_stop(mini->map_cmd);
499 mini->map_cmd = NULL; 495 mini->map_cmd = NULL;
500 ac = GNUNET_YES; 496 ac = GNUNET_YES;
501 } 497 }
502 if (NULL != mini->refresh_cmd) 498 if (NULL != mini->refresh_cmd)
503 { 499 {
504 /* took way too long, abort it! */ 500 /* took way too long, abort it! */
505 GNUNET_OS_command_stop (mini->refresh_cmd); 501 GNUNET_OS_command_stop(mini->refresh_cmd);
506 mini->refresh_cmd = NULL; 502 mini->refresh_cmd = NULL;
507 ac = GNUNET_YES; 503 ac = GNUNET_YES;
508 } 504 }
509 mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output, 505 mini->refresh_cmd = GNUNET_OS_command_run(&process_refresh_output,
510 mini, 506 mini,
511 MAP_TIMEOUT, 507 MAP_TIMEOUT,
512 "upnpc", 508 "upnpc",
513 "upnpc", 509 "upnpc",
514 "-l", 510 "-l",
515 NULL); 511 NULL);
516 if (GNUNET_YES == ac) 512 if (GNUNET_YES == ac)
517 mini->ac (mini->ac_cls, 513 mini->ac(mini->ac_cls,
518 GNUNET_SYSERR, 514 GNUNET_SYSERR,
519 NULL, 515 NULL,
520 0, 516 0,
521 GNUNET_NAT_ERROR_UPNPC_TIMEOUT); 517 GNUNET_NAT_ERROR_UPNPC_TIMEOUT);
522} 518}
523 519
524 520
@@ -529,7 +525,7 @@ do_refresh (void *cls)
529 * @param line line of output, NULL at the end 525 * @param line line of output, NULL at the end
530 */ 526 */
531static void 527static void
532process_map_output (void *cls, const char *line) 528process_map_output(void *cls, const char *line)
533{ 529{
534 struct GNUNET_NAT_MiniHandle *mini = cls; 530 struct GNUNET_NAT_MiniHandle *mini = cls;
535 const char *ipaddr; 531 const char *ipaddr;
@@ -538,51 +534,51 @@ process_map_output (void *cls, const char *line)
538 unsigned int port; 534 unsigned int port;
539 535
540 if (NULL == line) 536 if (NULL == line)
541 { 537 {
542 GNUNET_OS_command_stop (mini->map_cmd); 538 GNUNET_OS_command_stop(mini->map_cmd);
543 mini->map_cmd = NULL; 539 mini->map_cmd = NULL;
544 if (GNUNET_YES != mini->did_map) 540 if (GNUNET_YES != mini->did_map)
545 mini->ac (mini->ac_cls, 541 mini->ac(mini->ac_cls,
546 GNUNET_SYSERR, 542 GNUNET_SYSERR,
547 NULL, 543 NULL,
548 0, 544 0,
549 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED); 545 GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED);
550 if (NULL == mini->refresh_task) 546 if (NULL == mini->refresh_task)
551 mini->refresh_task = 547 mini->refresh_task =
552 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini); 548 GNUNET_SCHEDULER_add_delayed(MAP_REFRESH_FREQ, &do_refresh, mini);
553 return; 549 return;
554 } 550 }
555 /* 551 /*
556 * The upnpc output we're after looks like this: 552 * The upnpc output we're after looks like this:
557 * 553 *
558 * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000" 554 * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000"
559 */ 555 */
560 if ((NULL == (ipaddr = strstr (line, " "))) || 556 if ((NULL == (ipaddr = strstr(line, " "))) ||
561 (NULL == (pstr = strstr (ipaddr, ":"))) || 557 (NULL == (pstr = strstr(ipaddr, ":"))) ||
562 (1 != sscanf (pstr + 1, "%u", &port))) 558 (1 != sscanf(pstr + 1, "%u", &port)))
563 { 559 {
564 return; /* skip line */ 560 return; /* skip line */
565 } 561 }
566 ipa = GNUNET_strdup (ipaddr + 1); 562 ipa = GNUNET_strdup(ipaddr + 1);
567 strstr (ipa, ":")[0] = '\0'; 563 strstr(ipa, ":")[0] = '\0';
568 if (1 != inet_pton (AF_INET, ipa, &mini->current_addr.sin_addr)) 564 if (1 != inet_pton(AF_INET, ipa, &mini->current_addr.sin_addr))
569 { 565 {
570 GNUNET_free (ipa); 566 GNUNET_free(ipa);
571 return; /* skip line */ 567 return; /* skip line */
572 } 568 }
573 GNUNET_free (ipa); 569 GNUNET_free(ipa);
574 570
575 mini->current_addr.sin_port = htons (port); 571 mini->current_addr.sin_port = htons(port);
576 mini->current_addr.sin_family = AF_INET; 572 mini->current_addr.sin_family = AF_INET;
577#if HAVE_SOCKADDR_IN_SIN_LEN 573#if HAVE_SOCKADDR_IN_SIN_LEN
578 mini->current_addr.sin_len = sizeof (struct sockaddr_in); 574 mini->current_addr.sin_len = sizeof(struct sockaddr_in);
579#endif 575#endif
580 mini->did_map = GNUNET_YES; 576 mini->did_map = GNUNET_YES;
581 mini->ac (mini->ac_cls, 577 mini->ac(mini->ac_cls,
582 GNUNET_YES, 578 GNUNET_YES,
583 (const struct sockaddr *) &mini->current_addr, 579 (const struct sockaddr *)&mini->current_addr,
584 sizeof (mini->current_addr), 580 sizeof(mini->current_addr),
585 GNUNET_NAT_ERROR_SUCCESS); 581 GNUNET_NAT_ERROR_SUCCESS);
586} 582}
587 583
588 584
@@ -600,28 +596,28 @@ process_map_output (void *cls, const char *line)
600 * @return NULL on error (no 'upnpc' installed) 596 * @return NULL on error (no 'upnpc' installed)
601 */ 597 */
602struct GNUNET_NAT_MiniHandle * 598struct GNUNET_NAT_MiniHandle *
603GNUNET_NAT_mini_map_start (uint16_t port, 599GNUNET_NAT_mini_map_start(uint16_t port,
604 int is_tcp, 600 int is_tcp,
605 GNUNET_NAT_MiniAddressCallback ac, 601 GNUNET_NAT_MiniAddressCallback ac,
606 void *ac_cls) 602 void *ac_cls)
607{ 603{
608 struct GNUNET_NAT_MiniHandle *ret; 604 struct GNUNET_NAT_MiniHandle *ret;
609 605
610 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL)) 606 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary("upnpc", GNUNET_NO, NULL))
611 { 607 {
612 LOG (GNUNET_ERROR_TYPE_INFO, _ ("`upnpc' command not found\n")); 608 LOG(GNUNET_ERROR_TYPE_INFO, _("`upnpc' command not found\n"));
613 ac (ac_cls, GNUNET_SYSERR, NULL, 0, GNUNET_NAT_ERROR_UPNPC_NOT_FOUND); 609 ac(ac_cls, GNUNET_SYSERR, NULL, 0, GNUNET_NAT_ERROR_UPNPC_NOT_FOUND);
614 return NULL; 610 return NULL;
615 } 611 }
616 LOG (GNUNET_ERROR_TYPE_DEBUG, "Running `upnpc' to install mapping\n"); 612 LOG(GNUNET_ERROR_TYPE_DEBUG, "Running `upnpc' to install mapping\n");
617 ret = GNUNET_new (struct GNUNET_NAT_MiniHandle); 613 ret = GNUNET_new(struct GNUNET_NAT_MiniHandle);
618 ret->ac = ac; 614 ret->ac = ac;
619 ret->ac_cls = ac_cls; 615 ret->ac_cls = ac_cls;
620 ret->is_tcp = is_tcp; 616 ret->is_tcp = is_tcp;
621 ret->port = port; 617 ret->port = port;
622 ret->refresh_task = 618 ret->refresh_task =
623 GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, ret); 619 GNUNET_SCHEDULER_add_delayed(MAP_REFRESH_FREQ, &do_refresh, ret);
624 run_upnpc_r (ret); 620 run_upnpc_r(ret);
625 return ret; 621 return ret;
626} 622}
627 623
@@ -633,18 +629,18 @@ GNUNET_NAT_mini_map_start (uint16_t port,
633 * @param line line of output, NULL at the end 629 * @param line line of output, NULL at the end
634 */ 630 */
635static void 631static void
636process_unmap_output (void *cls, const char *line) 632process_unmap_output(void *cls, const char *line)
637{ 633{
638 struct GNUNET_NAT_MiniHandle *mini = cls; 634 struct GNUNET_NAT_MiniHandle *mini = cls;
639 635
640 if (NULL == line) 636 if (NULL == line)
641 { 637 {
642 LOG (GNUNET_ERROR_TYPE_DEBUG, "UPnP unmap done\n"); 638 LOG(GNUNET_ERROR_TYPE_DEBUG, "UPnP unmap done\n");
643 GNUNET_OS_command_stop (mini->unmap_cmd); 639 GNUNET_OS_command_stop(mini->unmap_cmd);
644 mini->unmap_cmd = NULL; 640 mini->unmap_cmd = NULL;
645 GNUNET_free (mini); 641 GNUNET_free(mini);
646 return; 642 return;
647 } 643 }
648 /* we don't really care about the output... */ 644 /* we don't really care about the output... */
649} 645}
650 646
@@ -658,54 +654,54 @@ process_unmap_output (void *cls, const char *line)
658 * @param mini the handle 654 * @param mini the handle
659 */ 655 */
660void 656void
661GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini) 657GNUNET_NAT_mini_map_stop(struct GNUNET_NAT_MiniHandle *mini)
662{ 658{
663 char pstr[6]; 659 char pstr[6];
664 660
665 if (NULL != mini->refresh_task) 661 if (NULL != mini->refresh_task)
666 { 662 {
667 GNUNET_SCHEDULER_cancel (mini->refresh_task); 663 GNUNET_SCHEDULER_cancel(mini->refresh_task);
668 mini->refresh_task = NULL; 664 mini->refresh_task = NULL;
669 } 665 }
670 if (NULL != mini->refresh_cmd) 666 if (NULL != mini->refresh_cmd)
671 { 667 {
672 GNUNET_OS_command_stop (mini->refresh_cmd); 668 GNUNET_OS_command_stop(mini->refresh_cmd);
673 mini->refresh_cmd = NULL; 669 mini->refresh_cmd = NULL;
674 } 670 }
675 if (NULL != mini->map_cmd) 671 if (NULL != mini->map_cmd)
676 { 672 {
677 GNUNET_OS_command_stop (mini->map_cmd); 673 GNUNET_OS_command_stop(mini->map_cmd);
678 mini->map_cmd = NULL; 674 mini->map_cmd = NULL;
679 } 675 }
680 if (GNUNET_NO == mini->did_map) 676 if (GNUNET_NO == mini->did_map)
681 { 677 {
682 GNUNET_free (mini); 678 GNUNET_free(mini);
683 return; 679 return;
684 } 680 }
685 mini->ac (mini->ac_cls, 681 mini->ac(mini->ac_cls,
686 GNUNET_NO, 682 GNUNET_NO,
687 (const struct sockaddr *) &mini->current_addr, 683 (const struct sockaddr *)&mini->current_addr,
688 sizeof (mini->current_addr), 684 sizeof(mini->current_addr),
689 GNUNET_NAT_ERROR_SUCCESS); 685 GNUNET_NAT_ERROR_SUCCESS);
690 /* Note: oddly enough, deletion uses the external port whereas 686 /* Note: oddly enough, deletion uses the external port whereas
691 * addition uses the internal port; this rarely matters since they 687 * addition uses the internal port; this rarely matters since they
692 * often are the same, but it might... */ 688 * often are the same, but it might... */
693 GNUNET_snprintf (pstr, 689 GNUNET_snprintf(pstr,
694 sizeof (pstr), 690 sizeof(pstr),
695 "%u", 691 "%u",
696 (unsigned int) ntohs (mini->current_addr.sin_port)); 692 (unsigned int)ntohs(mini->current_addr.sin_port));
697 LOG (GNUNET_ERROR_TYPE_DEBUG, 693 LOG(GNUNET_ERROR_TYPE_DEBUG,
698 "Unmapping port %u with UPnP\n", 694 "Unmapping port %u with UPnP\n",
699 ntohs (mini->current_addr.sin_port)); 695 ntohs(mini->current_addr.sin_port));
700 mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output, 696 mini->unmap_cmd = GNUNET_OS_command_run(&process_unmap_output,
701 mini, 697 mini,
702 UNMAP_TIMEOUT, 698 UNMAP_TIMEOUT,
703 "upnpc", 699 "upnpc",
704 "upnpc", 700 "upnpc",
705 "-d", 701 "-d",
706 pstr, 702 pstr,
707 mini->is_tcp ? "tcp" : "udp", 703 mini->is_tcp ? "tcp" : "udp",
708 NULL); 704 NULL);
709} 705}
710 706
711 707
diff --git a/src/nat/gnunet-service-nat_mini.h b/src/nat/gnunet-service-nat_mini.h
index 4b84fb425..031b8e935 100644
--- a/src/nat/gnunet-service-nat_mini.h
+++ b/src/nat/gnunet-service-nat_mini.h
@@ -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/** 21/**
22 * @file nat/gnunet-service-nat_mini.c 22 * @file nat/gnunet-service-nat_mini.c
@@ -54,8 +54,8 @@ struct GNUNET_NAT_ExternalHandle;
54 * @return handle for cancellation (can only be used until @a cb is called), NULL on error 54 * @return handle for cancellation (can only be used until @a cb is called), NULL on error
55 */ 55 */
56struct GNUNET_NAT_ExternalHandle * 56struct GNUNET_NAT_ExternalHandle *
57GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, 57GNUNET_NAT_mini_get_external_ipv4_(GNUNET_NAT_IPCallback cb,
58 void *cb_cls); 58 void *cb_cls);
59 59
60 60
61/** 61/**
@@ -64,7 +64,7 @@ GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb,
64 * @param eh operation to cancel 64 * @param eh operation to cancel
65 */ 65 */
66void 66void
67GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct GNUNET_NAT_ExternalHandle *eh); 67GNUNET_NAT_mini_get_external_ipv4_cancel_(struct GNUNET_NAT_ExternalHandle *eh);
68 68
69 69
70/** 70/**
@@ -106,10 +106,10 @@ typedef void
106 * @return NULL on error 106 * @return NULL on error
107 */ 107 */
108struct GNUNET_NAT_MiniHandle * 108struct GNUNET_NAT_MiniHandle *
109GNUNET_NAT_mini_map_start (uint16_t port, 109GNUNET_NAT_mini_map_start(uint16_t port,
110 int is_tcp, 110 int is_tcp,
111 GNUNET_NAT_MiniAddressCallback ac, 111 GNUNET_NAT_MiniAddressCallback ac,
112 void *ac_cls); 112 void *ac_cls);
113 113
114 114
115/** 115/**
@@ -121,7 +121,7 @@ GNUNET_NAT_mini_map_start (uint16_t port,
121 * @param mini the handle 121 * @param mini the handle
122 */ 122 */
123void 123void
124GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini); 124GNUNET_NAT_mini_map_stop(struct GNUNET_NAT_MiniHandle *mini);
125 125
126 126
127#endif 127#endif
diff --git a/src/nat/gnunet-service-nat_stun.c b/src/nat/gnunet-service-nat_stun.c
index 639289207..d8b791d0a 100644
--- a/src/nat/gnunet-service-nat_stun.c
+++ b/src/nat/gnunet-service-nat_stun.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 * This code provides some support for doing STUN transactions. We 21 * This code provides some support for doing STUN transactions. We
22 * receive the simplest possible packet as the STUN server and try 22 * receive the simplest possible packet as the STUN server and try
@@ -40,15 +40,14 @@
40#include "gnunet_util_lib.h" 40#include "gnunet_util_lib.h"
41#include "nat_stun.h" 41#include "nat_stun.h"
42 42
43#define LOG(kind,...) GNUNET_log_from (kind, "stun", __VA_ARGS__) 43#define LOG(kind, ...) GNUNET_log_from(kind, "stun", __VA_ARGS__)
44 44
45 45
46/** 46/**
47 * Context for #stun_get_mapped(). 47 * Context for #stun_get_mapped().
48 * Used to store state across processing attributes. 48 * Used to store state across processing attributes.
49 */ 49 */
50struct StunState 50struct StunState {
51{
52 uint16_t attr; 51 uint16_t attr;
53}; 52};
54 53
@@ -65,41 +64,44 @@ struct StunState
65 * @return #GNUNET_OK if @a arg was initialized 64 * @return #GNUNET_OK if @a arg was initialized
66 */ 65 */
67static int 66static int
68stun_get_mapped (struct StunState *st, 67stun_get_mapped(struct StunState *st,
69 const struct stun_attr *attr, 68 const struct stun_attr *attr,
70 uint32_t magic, 69 uint32_t magic,
71 struct sockaddr_in *arg) 70 struct sockaddr_in *arg)
72{ 71{
73 const struct stun_addr *returned_addr; 72 const struct stun_addr *returned_addr;
74 struct sockaddr_in *sa = (struct sockaddr_in *) arg; 73 struct sockaddr_in *sa = (struct sockaddr_in *)arg;
75 uint16_t type = ntohs (attr->attr); 74 uint16_t type = ntohs(attr->attr);
76 75
77 switch (type) 76 switch (type)
78 { 77 {
79 case STUN_MAPPED_ADDRESS: 78 case STUN_MAPPED_ADDRESS:
80 if ( (st->attr == STUN_XOR_MAPPED_ADDRESS) || 79 if ((st->attr == STUN_XOR_MAPPED_ADDRESS) ||
81 (st->attr == STUN_MS_XOR_MAPPED_ADDRESS) ) 80 (st->attr == STUN_MS_XOR_MAPPED_ADDRESS))
82 return GNUNET_NO; 81 return GNUNET_NO;
83 magic = 0; 82 magic = 0;
84 break; 83 break;
85 case STUN_MS_XOR_MAPPED_ADDRESS: 84
86 if (st->attr == STUN_XOR_MAPPED_ADDRESS) 85 case STUN_MS_XOR_MAPPED_ADDRESS:
86 if (st->attr == STUN_XOR_MAPPED_ADDRESS)
87 return GNUNET_NO;
88 break;
89
90 case STUN_XOR_MAPPED_ADDRESS:
91 break;
92
93 default:
87 return GNUNET_NO; 94 return GNUNET_NO;
88 break; 95 }
89 case STUN_XOR_MAPPED_ADDRESS: 96
90 break; 97 if (ntohs(attr->len) < sizeof(struct stun_addr))
91 default:
92 return GNUNET_NO;
93 }
94
95 if (ntohs (attr->len) < sizeof (struct stun_addr))
96 return GNUNET_NO; 98 return GNUNET_NO;
97 returned_addr = (const struct stun_addr *)(attr + 1); 99 returned_addr = (const struct stun_addr *)(attr + 1);
98 if (AF_INET != returned_addr->family) 100 if (AF_INET != returned_addr->family)
99 return GNUNET_NO; 101 return GNUNET_NO;
100 st->attr = type; 102 st->attr = type;
101 sa->sin_family = AF_INET; 103 sa->sin_family = AF_INET;
102 sa->sin_port = returned_addr->port ^ htons (ntohl(magic) >> 16); 104 sa->sin_port = returned_addr->port ^ htons(ntohl(magic) >> 16);
103 sa->sin_addr.s_addr = returned_addr->addr ^ magic; 105 sa->sin_addr.s_addr = returned_addr->addr ^ magic;
104 return GNUNET_OK; 106 return GNUNET_OK;
105} 107}
@@ -119,9 +121,9 @@ stun_get_mapped (struct StunState *st,
119 * #GNUNET_NO if the packet is invalid (not a stun packet) 121 * #GNUNET_NO if the packet is invalid (not a stun packet)
120 */ 122 */
121int 123int
122GNUNET_NAT_stun_handle_packet_ (const void *data, 124GNUNET_NAT_stun_handle_packet_(const void *data,
123 size_t len, 125 size_t len,
124 struct sockaddr_in *arg) 126 struct sockaddr_in *arg)
125{ 127{
126 const struct stun_header *hdr; 128 const struct stun_header *hdr;
127 const struct stun_attr *attr; 129 const struct stun_attr *attr;
@@ -135,76 +137,76 @@ GNUNET_NAT_stun_handle_packet_ (const void *data,
135 * while 'data' is advanced accordingly. 137 * while 'data' is advanced accordingly.
136 */ 138 */
137 if (len < sizeof(struct stun_header)) 139 if (len < sizeof(struct stun_header))
138 { 140 {
139 LOG (GNUNET_ERROR_TYPE_DEBUG, 141 LOG(GNUNET_ERROR_TYPE_DEBUG,
140 "Packet too short to be a STUN packet\n"); 142 "Packet too short to be a STUN packet\n");
141 return GNUNET_NO; 143 return GNUNET_NO;
142 } 144 }
143 hdr = data; 145 hdr = data;
144 /* Skip header as it is already in hdr */ 146 /* Skip header as it is already in hdr */
145 len -= sizeof(struct stun_header); 147 len -= sizeof(struct stun_header);
146 data += sizeof(struct stun_header); 148 data += sizeof(struct stun_header);
147 149
148 /* len as advertised in the message */ 150 /* len as advertised in the message */
149 advertised_message_size = ntohs (hdr->msglen); 151 advertised_message_size = ntohs(hdr->msglen);
150 message_magic_cookie = ntohl (hdr->magic); 152 message_magic_cookie = ntohl(hdr->magic);
151 /* Compare if the cookie match */ 153 /* Compare if the cookie match */
152 if (STUN_MAGIC_COOKIE != message_magic_cookie) 154 if (STUN_MAGIC_COOKIE != message_magic_cookie)
153 { 155 {
154 LOG (GNUNET_ERROR_TYPE_DEBUG, 156 LOG(GNUNET_ERROR_TYPE_DEBUG,
155 "Invalid magic cookie for STUN packet\n"); 157 "Invalid magic cookie for STUN packet\n");
156 return GNUNET_NO; 158 return GNUNET_NO;
157 } 159 }
158 160
159 LOG (GNUNET_ERROR_TYPE_INFO, 161 LOG(GNUNET_ERROR_TYPE_INFO,
160 "STUN Packet, msg %s (%04x), length: %d\n", 162 "STUN Packet, msg %s (%04x), length: %d\n",
161 stun_msg2str (ntohs (hdr->msgtype)), 163 stun_msg2str(ntohs(hdr->msgtype)),
162 ntohs (hdr->msgtype), 164 ntohs(hdr->msgtype),
163 advertised_message_size); 165 advertised_message_size);
164 if (advertised_message_size > len) 166 if (advertised_message_size > len)
165 {
166 LOG (GNUNET_ERROR_TYPE_INFO,
167 "Scrambled STUN packet length (got %d, expecting %d)\n",
168 advertised_message_size,
169 (int) len);
170 return GNUNET_NO;
171 }
172 len = advertised_message_size;
173 memset (&st, 0, sizeof(st));
174
175 while (len > 0)
176 {
177 if (len < sizeof (struct stun_attr))
178 { 167 {
179 LOG (GNUNET_ERROR_TYPE_INFO, 168 LOG(GNUNET_ERROR_TYPE_INFO,
180 "Attribute too short (got %d, expecting %d)\n", 169 "Scrambled STUN packet length (got %d, expecting %d)\n",
181 (int) len, 170 advertised_message_size,
182 (int) sizeof (struct stun_attr)); 171 (int)len);
183 break; 172 return GNUNET_NO;
184 } 173 }
185 attr = (const struct stun_attr *) data; 174 len = advertised_message_size;
186 175 memset(&st, 0, sizeof(st));
187 /* compute total attribute length */
188 advertised_message_size = ntohs (attr->len) + sizeof (struct stun_attr);
189 176
190 /* Check if we still have space in our buffer */ 177 while (len > 0)
191 if (advertised_message_size > len)
192 { 178 {
193 LOG (GNUNET_ERROR_TYPE_INFO, 179 if (len < sizeof(struct stun_attr))
194 "Inconsistent attribute (length %d exceeds remaining msg len %d)\n", 180 {
195 advertised_message_size, 181 LOG(GNUNET_ERROR_TYPE_INFO,
196 (int) len); 182 "Attribute too short (got %d, expecting %d)\n",
197 break; 183 (int)len,
184 (int)sizeof(struct stun_attr));
185 break;
186 }
187 attr = (const struct stun_attr *)data;
188
189 /* compute total attribute length */
190 advertised_message_size = ntohs(attr->len) + sizeof(struct stun_attr);
191
192 /* Check if we still have space in our buffer */
193 if (advertised_message_size > len)
194 {
195 LOG(GNUNET_ERROR_TYPE_INFO,
196 "Inconsistent attribute (length %d exceeds remaining msg len %d)\n",
197 advertised_message_size,
198 (int)len);
199 break;
200 }
201 if (GNUNET_OK ==
202 stun_get_mapped(&st,
203 attr,
204 hdr->magic,
205 arg))
206 ret = GNUNET_OK;
207 data += advertised_message_size;
208 len -= advertised_message_size;
198 } 209 }
199 if (GNUNET_OK ==
200 stun_get_mapped (&st,
201 attr,
202 hdr->magic,
203 arg))
204 ret = GNUNET_OK;
205 data += advertised_message_size;
206 len -= advertised_message_size;
207 }
208 return ret; 210 return ret;
209} 211}
210 212
diff --git a/src/nat/gnunet-service-nat_stun.h b/src/nat/gnunet-service-nat_stun.h
index 4a61c702f..af6520fbf 100644
--- a/src/nat/gnunet-service-nat_stun.h
+++ b/src/nat/gnunet-service-nat_stun.h
@@ -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 * This code provides some support for doing STUN transactions. We 21 * This code provides some support for doing STUN transactions. We
22 * receive the simplest possible packet as the STUN server and try 22 * receive the simplest possible packet as the STUN server and try
@@ -54,8 +54,8 @@
54 * #GNUNET_NO if the packet is invalid (not a stun packet) 54 * #GNUNET_NO if the packet is invalid (not a stun packet)
55 */ 55 */
56int 56int
57GNUNET_NAT_stun_handle_packet_ (const void *data, 57GNUNET_NAT_stun_handle_packet_(const void *data,
58 size_t len, 58 size_t len,
59 struct sockaddr_in *arg); 59 struct sockaddr_in *arg);
60 60
61#endif 61#endif
diff --git a/src/nat/nat.h b/src/nat/nat.h
index 86de3c2f6..62377435a 100644
--- a/src/nat/nat.h
+++ b/src/nat/nat.h
@@ -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/** 21/**
22 * @file src/nat/nat.h 22 * @file src/nat/nat.h
@@ -36,8 +36,7 @@ GNUNET_NETWORK_STRUCT_BEGIN
36 * Request to test NAT traversal, sent to the gnunet-nat-server 36 * Request to test NAT traversal, sent to the gnunet-nat-server
37 * (not the service!). 37 * (not the service!).
38 */ 38 */
39struct GNUNET_NAT_TestMessage 39struct GNUNET_NAT_TestMessage {
40{
41 /** 40 /**
42 * Header with type #GNUNET_MESSAGE_TYPE_NAT_TEST 41 * Header with type #GNUNET_MESSAGE_TYPE_NAT_TEST
43 */ 42 */
@@ -62,7 +61,6 @@ struct GNUNET_NAT_TestMessage
62 * #GNUNET_YES for TCP, #GNUNET_NO for UDP. 61 * #GNUNET_YES for TCP, #GNUNET_NO for UDP.
63 */ 62 */
64 int32_t is_tcp; 63 int32_t is_tcp;
65
66}; 64};
67 65
68 66
@@ -70,8 +68,7 @@ struct GNUNET_NAT_TestMessage
70 * Flags specifying the events this client would be 68 * Flags specifying the events this client would be
71 * interested in being told about. 69 * interested in being told about.
72 */ 70 */
73enum GNUNET_NAT_RegisterFlags 71enum GNUNET_NAT_RegisterFlags {
74{
75 /** 72 /**
76 * This client does not want any notifications. 73 * This client does not want any notifications.
77 */ 74 */
@@ -93,8 +90,7 @@ enum GNUNET_NAT_RegisterFlags
93/** 90/**
94 * Message sent by a client to register with its addresses. 91 * Message sent by a client to register with its addresses.
95 */ 92 */
96struct GNUNET_NAT_RegisterMessage 93struct GNUNET_NAT_RegisterMessage {
97{
98 /** 94 /**
99 * Header with type #GNUNET_MESSAGE_TYPE_NAT_REGISTER 95 * Header with type #GNUNET_MESSAGE_TYPE_NAT_REGISTER
100 */ 96 */
@@ -127,15 +123,13 @@ struct GNUNET_NAT_RegisterMessage
127 sockaddr' */ 123 sockaddr' */
128 124
129 /* Followed by @e str_len section name to use for options */ 125 /* Followed by @e str_len section name to use for options */
130
131}; 126};
132 127
133 128
134/** 129/**
135 * Client telling the service to (possibly) handle a STUN message. 130 * Client telling the service to (possibly) handle a STUN message.
136 */ 131 */
137struct GNUNET_NAT_HandleStunMessage 132struct GNUNET_NAT_HandleStunMessage {
138{
139 /** 133 /**
140 * Header with type #GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN 134 * Header with type #GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN
141 */ 135 */
@@ -160,8 +154,7 @@ struct GNUNET_NAT_HandleStunMessage
160/** 154/**
161 * Client asking the service to initiate connection reversal. 155 * Client asking the service to initiate connection reversal.
162 */ 156 */
163struct GNUNET_NAT_RequestConnectionReversalMessage 157struct GNUNET_NAT_RequestConnectionReversalMessage {
164{
165 /** 158 /**
166 * Header with type #GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL 159 * Header with type #GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL
167 */ 160 */
@@ -180,31 +173,27 @@ struct GNUNET_NAT_RequestConnectionReversalMessage
180 /* followed by a `struct sockaddr` of @e local_addr_size bytes */ 173 /* followed by a `struct sockaddr` of @e local_addr_size bytes */
181 174
182 /* followed by a `struct sockaddr` of @e remote_addr_size bytes */ 175 /* followed by a `struct sockaddr` of @e remote_addr_size bytes */
183
184}; 176};
185 177
186 178
187/** 179/**
188 * Service telling a client that connection reversal was requested. 180 * Service telling a client that connection reversal was requested.
189 */ 181 */
190struct GNUNET_NAT_ConnectionReversalRequestedMessage 182struct GNUNET_NAT_ConnectionReversalRequestedMessage {
191{
192 /** 183 /**
193 * Header with type #GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED 184 * Header with type #GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED
194 */ 185 */
195 struct GNUNET_MessageHeader header; 186 struct GNUNET_MessageHeader header;
196 187
197 /* followed by a `struct sockaddr_in` */ 188 /* followed by a `struct sockaddr_in` */
198
199}; 189};
200 190
201 191
202/** 192/**
203 * Service notifying the client about changes in the set of 193 * Service notifying the client about changes in the set of
204 * addresses it has. 194 * addresses it has.
205 */ 195 */
206struct GNUNET_NAT_AddressChangeNotificationMessage 196struct GNUNET_NAT_AddressChangeNotificationMessage {
207{
208 /** 197 /**
209 * Header with type #GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE 198 * Header with type #GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE
210 */ 199 */
@@ -212,7 +201,7 @@ struct GNUNET_NAT_AddressChangeNotificationMessage
212 201
213 /** 202 /**
214 * #GNUNET_YES to add, #GNUNET_NO to remove the address from the list. 203 * #GNUNET_YES to add, #GNUNET_NO to remove the address from the list.
215 */ 204 */
216 int32_t add_remove GNUNET_PACKED; 205 int32_t add_remove GNUNET_PACKED;
217 206
218 /** 207 /**
@@ -220,7 +209,6 @@ struct GNUNET_NAT_AddressChangeNotificationMessage
220 */ 209 */
221 uint32_t addr_class GNUNET_PACKED; 210 uint32_t addr_class GNUNET_PACKED;
222 /* followed by a `struct sockaddr` */ 211 /* followed by a `struct sockaddr` */
223
224}; 212};
225 213
226 214
diff --git a/src/nat/nat_api.c b/src/nat/nat_api.c
index 0dea501d3..877589d85 100644
--- a/src/nat/nat_api.c
+++ b/src/nat/nat_api.c
@@ -16,7 +16,7 @@
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/** 21/**
22 * @author Christian Grothoff 22 * @author Christian Grothoff
@@ -35,9 +35,7 @@
35/** 35/**
36 * Entry in DLL of addresses of this peer. 36 * Entry in DLL of addresses of this peer.
37 */ 37 */
38struct AddrEntry 38struct AddrEntry {
39{
40
41 /** 39 /**
42 * DLL. 40 * DLL.
43 */ 41 */
@@ -69,9 +67,7 @@ struct AddrEntry
69/** 67/**
70 * Handle for active NAT registrations. 68 * Handle for active NAT registrations.
71 */ 69 */
72struct GNUNET_NAT_Handle 70struct GNUNET_NAT_Handle {
73{
74
75 /** 71 /**
76 * Configuration we use. 72 * Configuration we use.
77 */ 73 */
@@ -130,7 +126,7 @@ struct GNUNET_NAT_Handle
130 * @param cls our `struct GNUNET_NAT_Handle *` 126 * @param cls our `struct GNUNET_NAT_Handle *`
131 */ 127 */
132static void 128static void
133do_connect (void *cls); 129do_connect(void *cls);
134 130
135 131
136/** 132/**
@@ -139,29 +135,29 @@ do_connect (void *cls);
139 * @param nh handle to reconnect 135 * @param nh handle to reconnect
140 */ 136 */
141static void 137static void
142reconnect (struct GNUNET_NAT_Handle *nh) 138reconnect(struct GNUNET_NAT_Handle *nh)
143{ 139{
144 struct AddrEntry *ae; 140 struct AddrEntry *ae;
145 141
146 if (NULL != nh->mq) 142 if (NULL != nh->mq)
147 { 143 {
148 GNUNET_MQ_destroy (nh->mq); 144 GNUNET_MQ_destroy(nh->mq);
149 nh->mq = NULL; 145 nh->mq = NULL;
150 } 146 }
151 while (NULL != (ae = nh->ae_head)) 147 while (NULL != (ae = nh->ae_head))
152 { 148 {
153 GNUNET_CONTAINER_DLL_remove (nh->ae_head, nh->ae_tail, ae); 149 GNUNET_CONTAINER_DLL_remove(nh->ae_head, nh->ae_tail, ae);
154 nh->address_callback (nh->callback_cls, 150 nh->address_callback(nh->callback_cls,
155 &ae->app_ctx, 151 &ae->app_ctx,
156 GNUNET_NO, 152 GNUNET_NO,
157 ae->ac, 153 ae->ac,
158 (const struct sockaddr *) &ae[1], 154 (const struct sockaddr *)&ae[1],
159 ae->addrlen); 155 ae->addrlen);
160 GNUNET_free (ae); 156 GNUNET_free(ae);
161 } 157 }
162 nh->reconnect_delay = GNUNET_TIME_STD_BACKOFF (nh->reconnect_delay); 158 nh->reconnect_delay = GNUNET_TIME_STD_BACKOFF(nh->reconnect_delay);
163 nh->reconnect_task = 159 nh->reconnect_task =
164 GNUNET_SCHEDULER_add_delayed (nh->reconnect_delay, &do_connect, nh); 160 GNUNET_SCHEDULER_add_delayed(nh->reconnect_delay, &do_connect, nh);
165} 161}
166 162
167 163
@@ -173,15 +169,15 @@ reconnect (struct GNUNET_NAT_Handle *nh)
173 * @return #GNUNET_OK if @a crm is well-formed 169 * @return #GNUNET_OK if @a crm is well-formed
174 */ 170 */
175static int 171static int
176check_connection_reversal_request ( 172check_connection_reversal_request(
177 void *cls, 173 void *cls,
178 const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm) 174 const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm)
179{ 175{
180 if (ntohs (crm->header.size) != sizeof (*crm) + sizeof (struct sockaddr_in)) 176 if (ntohs(crm->header.size) != sizeof(*crm) + sizeof(struct sockaddr_in))
181 { 177 {
182 GNUNET_break (0); 178 GNUNET_break(0);
183 return GNUNET_SYSERR; 179 return GNUNET_SYSERR;
184 } 180 }
185 return GNUNET_OK; 181 return GNUNET_OK;
186} 182}
187 183
@@ -193,15 +189,15 @@ check_connection_reversal_request (
193 * @param crm the message 189 * @param crm the message
194 */ 190 */
195static void 191static void
196handle_connection_reversal_request ( 192handle_connection_reversal_request(
197 void *cls, 193 void *cls,
198 const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm) 194 const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm)
199{ 195{
200 struct GNUNET_NAT_Handle *nh = cls; 196 struct GNUNET_NAT_Handle *nh = cls;
201 197
202 nh->reversal_callback (nh->callback_cls, 198 nh->reversal_callback(nh->callback_cls,
203 (const struct sockaddr *) &crm[1], 199 (const struct sockaddr *)&crm[1],
204 sizeof (struct sockaddr_in)); 200 sizeof(struct sockaddr_in));
205} 201}
206 202
207 203
@@ -213,36 +209,38 @@ handle_connection_reversal_request (
213 * @return #GNUNET_OK if @a crm is well-formed 209 * @return #GNUNET_OK if @a crm is well-formed
214 */ 210 */
215static int 211static int
216check_address_change_notification ( 212check_address_change_notification(
217 void *cls, 213 void *cls,
218 const struct GNUNET_NAT_AddressChangeNotificationMessage *acn) 214 const struct GNUNET_NAT_AddressChangeNotificationMessage *acn)
219{ 215{
220 size_t alen = ntohs (acn->header.size) - sizeof (*acn); 216 size_t alen = ntohs(acn->header.size) - sizeof(*acn);
221 217
222 switch (alen) 218 switch (alen)
223 {
224 case sizeof (struct sockaddr_in): {
225 const struct sockaddr_in *s4 = (const struct sockaddr_in *) &acn[1];
226 if (AF_INET != s4->sin_family)
227 { 219 {
228 GNUNET_break (0); 220 case sizeof(struct sockaddr_in): {
229 return GNUNET_SYSERR; 221 const struct sockaddr_in *s4 = (const struct sockaddr_in *)&acn[1];
222 if (AF_INET != s4->sin_family)
223 {
224 GNUNET_break(0);
225 return GNUNET_SYSERR;
226 }
230 } 227 }
231 } 228 break;
232 break; 229
233 case sizeof (struct sockaddr_in6): { 230 case sizeof(struct sockaddr_in6): {
234 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) &acn[1]; 231 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *)&acn[1];
235 if (AF_INET6 != s6->sin6_family) 232 if (AF_INET6 != s6->sin6_family)
236 { 233 {
237 GNUNET_break (0); 234 GNUNET_break(0);
235 return GNUNET_SYSERR;
236 }
237 }
238 break;
239
240 default:
241 GNUNET_break(0);
238 return GNUNET_SYSERR; 242 return GNUNET_SYSERR;
239 } 243 }
240 }
241 break;
242 default:
243 GNUNET_break (0);
244 return GNUNET_SYSERR;
245 }
246 return GNUNET_OK; 244 return GNUNET_OK;
247} 245}
248 246
@@ -254,53 +252,53 @@ check_address_change_notification (
254 * @param acn the message 252 * @param acn the message
255 */ 253 */
256static void 254static void
257handle_address_change_notification ( 255handle_address_change_notification(
258 void *cls, 256 void *cls,
259 const struct GNUNET_NAT_AddressChangeNotificationMessage *acn) 257 const struct GNUNET_NAT_AddressChangeNotificationMessage *acn)
260{ 258{
261 struct GNUNET_NAT_Handle *nh = cls; 259 struct GNUNET_NAT_Handle *nh = cls;
262 size_t alen = ntohs (acn->header.size) - sizeof (*acn); 260 size_t alen = ntohs(acn->header.size) - sizeof(*acn);
263 const struct sockaddr *sa = (const struct sockaddr *) &acn[1]; 261 const struct sockaddr *sa = (const struct sockaddr *)&acn[1];
264 enum GNUNET_NAT_AddressClass ac; 262 enum GNUNET_NAT_AddressClass ac;
265 struct AddrEntry *ae; 263 struct AddrEntry *ae;
266 264
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 265 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
268 "Received address change notification\n"); 266 "Received address change notification\n");
269 ac = (enum GNUNET_NAT_AddressClass) ntohl (acn->addr_class); 267 ac = (enum GNUNET_NAT_AddressClass)ntohl(acn->addr_class);
270 if (GNUNET_YES == ntohl (acn->add_remove)) 268 if (GNUNET_YES == ntohl(acn->add_remove))
271 { 269 {
272 ae = GNUNET_malloc (sizeof (*ae) + alen); 270 ae = GNUNET_malloc(sizeof(*ae) + alen);
273 ae->ac = ac; 271 ae->ac = ac;
274 ae->addrlen = alen; 272 ae->addrlen = alen;
275 GNUNET_memcpy (&ae[1], sa, alen); 273 GNUNET_memcpy(&ae[1], sa, alen);
276 GNUNET_CONTAINER_DLL_insert (nh->ae_head, nh->ae_tail, ae); 274 GNUNET_CONTAINER_DLL_insert(nh->ae_head, nh->ae_tail, ae);
277 nh->address_callback (nh->callback_cls, 275 nh->address_callback(nh->callback_cls,
278 &ae->app_ctx, 276 &ae->app_ctx,
279 ntohl (acn->add_remove), 277 ntohl(acn->add_remove),
280 ac, 278 ac,
281 sa, 279 sa,
282 alen); 280 alen);
283 } 281 }
284 else 282 else
285 {
286 for (ae = nh->ae_head; NULL != ae; ae = ae->next)
287 if ((ae->addrlen == alen) && (0 == memcmp (&ae[1], sa, alen)))
288 break;
289 if (NULL == ae)
290 { 283 {
291 GNUNET_break (0); 284 for (ae = nh->ae_head; NULL != ae; ae = ae->next)
292 reconnect (nh); 285 if ((ae->addrlen == alen) && (0 == memcmp(&ae[1], sa, alen)))
293 return; 286 break;
287 if (NULL == ae)
288 {
289 GNUNET_break(0);
290 reconnect(nh);
291 return;
292 }
293 GNUNET_CONTAINER_DLL_remove(nh->ae_head, nh->ae_tail, ae);
294 nh->address_callback(nh->callback_cls,
295 &ae->app_ctx,
296 ntohl(acn->add_remove),
297 ac,
298 sa,
299 alen);
300 GNUNET_free(ae);
294 } 301 }
295 GNUNET_CONTAINER_DLL_remove (nh->ae_head, nh->ae_tail, ae);
296 nh->address_callback (nh->callback_cls,
297 &ae->app_ctx,
298 ntohl (acn->add_remove),
299 ac,
300 sa,
301 alen);
302 GNUNET_free (ae);
303 }
304} 302}
305 303
306 304
@@ -311,11 +309,11 @@ handle_address_change_notification (
311 * @param error details about the error 309 * @param error details about the error
312 */ 310 */
313static void 311static void
314mq_error_handler (void *cls, enum GNUNET_MQ_Error error) 312mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
315{ 313{
316 struct GNUNET_NAT_Handle *nh = cls; 314 struct GNUNET_NAT_Handle *nh = cls;
317 315
318 reconnect (nh); 316 reconnect(nh);
319} 317}
320 318
321 319
@@ -325,32 +323,32 @@ mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
325 * @param cls our `struct GNUNET_NAT_Handle *` 323 * @param cls our `struct GNUNET_NAT_Handle *`
326 */ 324 */
327static void 325static void
328do_connect (void *cls) 326do_connect(void *cls)
329{ 327{
330 struct GNUNET_NAT_Handle *nh = cls; 328 struct GNUNET_NAT_Handle *nh = cls;
331 struct GNUNET_MQ_MessageHandler handlers[] = 329 struct GNUNET_MQ_MessageHandler handlers[] =
332 {GNUNET_MQ_hd_var_size (connection_reversal_request, 330 { GNUNET_MQ_hd_var_size(connection_reversal_request,
333 GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED, 331 GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED,
334 struct 332 struct
335 GNUNET_NAT_ConnectionReversalRequestedMessage, 333 GNUNET_NAT_ConnectionReversalRequestedMessage,
336 nh), 334 nh),
337 GNUNET_MQ_hd_var_size (address_change_notification, 335 GNUNET_MQ_hd_var_size(address_change_notification,
338 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE, 336 GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE,
339 struct GNUNET_NAT_AddressChangeNotificationMessage, 337 struct GNUNET_NAT_AddressChangeNotificationMessage,
340 nh), 338 nh),
341 GNUNET_MQ_handler_end ()}; 339 GNUNET_MQ_handler_end() };
342 struct GNUNET_MQ_Envelope *env; 340 struct GNUNET_MQ_Envelope *env;
343 341
344 nh->reconnect_task = NULL; 342 nh->reconnect_task = NULL;
345 nh->mq = 343 nh->mq =
346 GNUNET_CLIENT_connect (nh->cfg, "nat", handlers, &mq_error_handler, nh); 344 GNUNET_CLIENT_connect(nh->cfg, "nat", handlers, &mq_error_handler, nh);
347 if (NULL == nh->mq) 345 if (NULL == nh->mq)
348 { 346 {
349 reconnect (nh); 347 reconnect(nh);
350 return; 348 return;
351 } 349 }
352 env = GNUNET_MQ_msg_copy (nh->reg); 350 env = GNUNET_MQ_msg_copy(nh->reg);
353 GNUNET_MQ_send (nh->mq, env); 351 GNUNET_MQ_send(nh->mq, env);
354} 352}
355 353
356 354
@@ -375,15 +373,15 @@ do_connect (void *cls)
375 * @return NULL on error, otherwise handle that can be used to unregister 373 * @return NULL on error, otherwise handle that can be used to unregister
376 */ 374 */
377struct GNUNET_NAT_Handle * 375struct GNUNET_NAT_Handle *
378GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, 376GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg,
379 const char *config_section, 377 const char *config_section,
380 uint8_t proto, 378 uint8_t proto,
381 unsigned int num_addrs, 379 unsigned int num_addrs,
382 const struct sockaddr **addrs, 380 const struct sockaddr **addrs,
383 const socklen_t *addrlens, 381 const socklen_t *addrlens,
384 GNUNET_NAT_AddressCallback address_callback, 382 GNUNET_NAT_AddressCallback address_callback,
385 GNUNET_NAT_ReversalCallback reversal_callback, 383 GNUNET_NAT_ReversalCallback reversal_callback,
386 void *callback_cls) 384 void *callback_cls)
387{ 385{
388 struct GNUNET_NAT_Handle *nh; 386 struct GNUNET_NAT_Handle *nh;
389 struct GNUNET_NAT_RegisterMessage *rm; 387 struct GNUNET_NAT_RegisterMessage *rm;
@@ -394,73 +392,75 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
394 len = 0; 392 len = 0;
395 for (unsigned int i = 0; i < num_addrs; i++) 393 for (unsigned int i = 0; i < num_addrs; i++)
396 len += addrlens[i]; 394 len += addrlens[i];
397 str_len = strlen (config_section) + 1; 395 str_len = strlen(config_section) + 1;
398 len += str_len; 396 len += str_len;
399 if ((len > GNUNET_MAX_MESSAGE_SIZE - sizeof (*rm)) || 397 if ((len > GNUNET_MAX_MESSAGE_SIZE - sizeof(*rm)) ||
400 (num_addrs > UINT16_MAX)) 398 (num_addrs > UINT16_MAX))
401 { 399 {
402 GNUNET_break (0); 400 GNUNET_break(0);
403 return NULL; 401 return NULL;
404 } 402 }
405 rm = GNUNET_malloc (sizeof (*rm) + len); 403 rm = GNUNET_malloc(sizeof(*rm) + len);
406 rm->header.size = htons (sizeof (*rm) + len); 404 rm->header.size = htons(sizeof(*rm) + len);
407 rm->header.type = htons (GNUNET_MESSAGE_TYPE_NAT_REGISTER); 405 rm->header.type = htons(GNUNET_MESSAGE_TYPE_NAT_REGISTER);
408 rm->flags = GNUNET_NAT_RF_NONE; 406 rm->flags = GNUNET_NAT_RF_NONE;
409 if (NULL != address_callback) 407 if (NULL != address_callback)
410 rm->flags |= GNUNET_NAT_RF_ADDRESSES; 408 rm->flags |= GNUNET_NAT_RF_ADDRESSES;
411 if (NULL != reversal_callback) 409 if (NULL != reversal_callback)
412 rm->flags |= GNUNET_NAT_RF_REVERSAL; 410 rm->flags |= GNUNET_NAT_RF_REVERSAL;
413 rm->proto = proto; 411 rm->proto = proto;
414 rm->str_len = htons (str_len); 412 rm->str_len = htons(str_len);
415 rm->num_addrs = htons ((uint16_t) num_addrs); 413 rm->num_addrs = htons((uint16_t)num_addrs);
416 off = (char *) &rm[1]; 414 off = (char *)&rm[1];
417 for (unsigned int i = 0; i < num_addrs; i++) 415 for (unsigned int i = 0; i < num_addrs; i++)
418 {
419 switch (addrs[i]->sa_family)
420 { 416 {
421 case AF_INET: 417 switch (addrs[i]->sa_family)
422 if (sizeof (struct sockaddr_in) != addrlens[i]) 418 {
423 { 419 case AF_INET:
424 GNUNET_break (0); 420 if (sizeof(struct sockaddr_in) != addrlens[i])
425 GNUNET_free (rm); 421 {
426 return NULL; 422 GNUNET_break(0);
427 } 423 GNUNET_free(rm);
428 break; 424 return NULL;
429 case AF_INET6: 425 }
430 if (sizeof (struct sockaddr_in6) != addrlens[i]) 426 break;
431 { 427
432 GNUNET_break (0); 428 case AF_INET6:
433 GNUNET_free (rm); 429 if (sizeof(struct sockaddr_in6) != addrlens[i])
434 return NULL; 430 {
435 } 431 GNUNET_break(0);
436 break; 432 GNUNET_free(rm);
433 return NULL;
434 }
435 break;
436
437#if AF_UNIX 437#if AF_UNIX
438 case AF_UNIX: 438 case AF_UNIX:
439 if (sizeof (struct sockaddr_un) != addrlens[i]) 439 if (sizeof(struct sockaddr_un) != addrlens[i])
440 { 440 {
441 GNUNET_break (0); 441 GNUNET_break(0);
442 GNUNET_free (rm); 442 GNUNET_free(rm);
443 return NULL; 443 return NULL;
444 } 444 }
445 break; 445 break;
446#endif 446#endif
447 default: 447 default:
448 GNUNET_break (0); 448 GNUNET_break(0);
449 GNUNET_free (rm); 449 GNUNET_free(rm);
450 return NULL; 450 return NULL;
451 }
452 GNUNET_memcpy(off, addrs[i], addrlens[i]);
453 off += addrlens[i];
451 } 454 }
452 GNUNET_memcpy (off, addrs[i], addrlens[i]); 455 GNUNET_memcpy(off, config_section, str_len);
453 off += addrlens[i];
454 }
455 GNUNET_memcpy (off, config_section, str_len);
456 456
457 nh = GNUNET_new (struct GNUNET_NAT_Handle); 457 nh = GNUNET_new(struct GNUNET_NAT_Handle);
458 nh->reg = &rm->header; 458 nh->reg = &rm->header;
459 nh->cfg = cfg; 459 nh->cfg = cfg;
460 nh->address_callback = address_callback; 460 nh->address_callback = address_callback;
461 nh->reversal_callback = reversal_callback; 461 nh->reversal_callback = reversal_callback;
462 nh->callback_cls = callback_cls; 462 nh->callback_cls = callback_cls;
463 do_connect (nh); 463 do_connect(nh);
464 return nh; 464 return nh;
465} 465}
466 466
@@ -474,7 +474,7 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
474 * #GNUNET_NO if the packet is invalid (not a stun packet) 474 * #GNUNET_NO if the packet is invalid (not a stun packet)
475 */ 475 */
476static int 476static int
477test_stun_packet (const void *data, size_t len) 477test_stun_packet(const void *data, size_t len)
478{ 478{
479 const struct stun_header *hdr; 479 const struct stun_header *hdr;
480 const struct stun_attr *attr; 480 const struct stun_attr *attr;
@@ -485,71 +485,71 @@ test_stun_packet (const void *data, size_t len)
485 * initial checks it becomes the size of unprocessed options, 485 * initial checks it becomes the size of unprocessed options,
486 * while 'data' is advanced accordingly. 486 * while 'data' is advanced accordingly.
487 */ 487 */
488 if (len < sizeof (struct stun_header)) 488 if (len < sizeof(struct stun_header))
489 { 489 {
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 490 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
491 "STUN packet too short (only %d, wanting at least %d)\n", 491 "STUN packet too short (only %d, wanting at least %d)\n",
492 (int) len, 492 (int)len,
493 (int) sizeof (struct stun_header)); 493 (int)sizeof(struct stun_header));
494 return GNUNET_NO; 494 return GNUNET_NO;
495 } 495 }
496 hdr = (const struct stun_header *) data; 496 hdr = (const struct stun_header *)data;
497 /* Skip header as it is already in hdr */ 497 /* Skip header as it is already in hdr */
498 len -= sizeof (struct stun_header); 498 len -= sizeof(struct stun_header);
499 data += sizeof (struct stun_header); 499 data += sizeof(struct stun_header);
500 500
501 /* len as advertised in the message */ 501 /* len as advertised in the message */
502 advertised_message_size = ntohs (hdr->msglen); 502 advertised_message_size = ntohs(hdr->msglen);
503 503
504 message_magic_cookie = ntohl (hdr->magic); 504 message_magic_cookie = ntohl(hdr->magic);
505 /* Compare if the cookie match */ 505 /* Compare if the cookie match */
506 if (STUN_MAGIC_COOKIE != message_magic_cookie) 506 if (STUN_MAGIC_COOKIE != message_magic_cookie)
507 {
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Invalid magic cookie for STUN\n");
509 return GNUNET_NO;
510 }
511
512 if (advertised_message_size > len)
513 {
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515 "Scrambled STUN packet length (got %d, expecting %d)\n",
516 advertised_message_size,
517 (int) len);
518 return GNUNET_NO;
519 }
520 len = advertised_message_size;
521 while (len > 0)
522 {
523 if (len < sizeof (struct stun_attr))
524 { 507 {
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 508 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Invalid magic cookie for STUN\n");
526 "Attribute too short in STUN packet (got %d, expecting %d)\n",
527 (int) len,
528 (int) sizeof (struct stun_attr));
529 return GNUNET_NO; 509 return GNUNET_NO;
530 } 510 }
531 attr = (const struct stun_attr *) data;
532
533 /* compute total attribute length */
534 advertised_message_size = ntohs (attr->len) + sizeof (struct stun_attr);
535 511
536 /* Check if we still have space in our buffer */ 512 if (advertised_message_size > len)
537 if (advertised_message_size > len)
538 { 513 {
539 GNUNET_log ( 514 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
540 GNUNET_ERROR_TYPE_DEBUG, 515 "Scrambled STUN packet length (got %d, expecting %d)\n",
541 "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", 516 advertised_message_size,
542 advertised_message_size, 517 (int)len);
543 (int) len);
544 return GNUNET_NO; 518 return GNUNET_NO;
545 } 519 }
546 data += advertised_message_size; 520 len = advertised_message_size;
547 len -= advertised_message_size; 521 while (len > 0)
548 } 522 {
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 523 if (len < sizeof(struct stun_attr))
550 "STUN Packet, msg %04x, length: %d\n", 524 {
551 ntohs (hdr->msgtype), 525 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
552 advertised_message_size); 526 "Attribute too short in STUN packet (got %d, expecting %d)\n",
527 (int)len,
528 (int)sizeof(struct stun_attr));
529 return GNUNET_NO;
530 }
531 attr = (const struct stun_attr *)data;
532
533 /* compute total attribute length */
534 advertised_message_size = ntohs(attr->len) + sizeof(struct stun_attr);
535
536 /* Check if we still have space in our buffer */
537 if (advertised_message_size > len)
538 {
539 GNUNET_log(
540 GNUNET_ERROR_TYPE_DEBUG,
541 "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n",
542 advertised_message_size,
543 (int)len);
544 return GNUNET_NO;
545 }
546 data += advertised_message_size;
547 len -= advertised_message_size;
548 }
549 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
550 "STUN Packet, msg %04x, length: %d\n",
551 ntohs(hdr->msgtype),
552 advertised_message_size);
553 return GNUNET_OK; 553 return GNUNET_OK;
554} 554}
555 555
@@ -578,30 +578,30 @@ test_stun_packet (const void *data, size_t len)
578 * #GNUNET_SYSERR on internal error handling the packet 578 * #GNUNET_SYSERR on internal error handling the packet
579 */ 579 */
580int 580int
581GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh, 581GNUNET_NAT_stun_handle_packet(struct GNUNET_NAT_Handle *nh,
582 const struct sockaddr *sender_addr, 582 const struct sockaddr *sender_addr,
583 size_t sender_addr_len, 583 size_t sender_addr_len,
584 const void *data, 584 const void *data,
585 size_t data_size) 585 size_t data_size)
586{ 586{
587 struct GNUNET_MQ_Envelope *env; 587 struct GNUNET_MQ_Envelope *env;
588 struct GNUNET_NAT_HandleStunMessage *hsn; 588 struct GNUNET_NAT_HandleStunMessage *hsn;
589 char *buf; 589 char *buf;
590 590
591 if (GNUNET_YES != test_stun_packet (data, data_size)) 591 if (GNUNET_YES != test_stun_packet(data, data_size))
592 return GNUNET_NO; 592 return GNUNET_NO;
593 if (NULL == nh->mq) 593 if (NULL == nh->mq)
594 return GNUNET_SYSERR; 594 return GNUNET_SYSERR;
595 env = GNUNET_MQ_msg_extra (hsn, 595 env = GNUNET_MQ_msg_extra(hsn,
596 data_size + sender_addr_len, 596 data_size + sender_addr_len,
597 GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN); 597 GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN);
598 hsn->sender_addr_size = htons ((uint16_t) sender_addr_len); 598 hsn->sender_addr_size = htons((uint16_t)sender_addr_len);
599 hsn->payload_size = htons ((uint16_t) data_size); 599 hsn->payload_size = htons((uint16_t)data_size);
600 buf = (char *) &hsn[1]; 600 buf = (char *)&hsn[1];
601 GNUNET_memcpy (buf, sender_addr, sender_addr_len); 601 GNUNET_memcpy(buf, sender_addr, sender_addr_len);
602 buf += sender_addr_len; 602 buf += sender_addr_len;
603 GNUNET_memcpy (buf, data, data_size); 603 GNUNET_memcpy(buf, data, data_size);
604 GNUNET_MQ_send (nh->mq, env); 604 GNUNET_MQ_send(nh->mq, env);
605 return GNUNET_OK; 605 return GNUNET_OK;
606} 606}
607 607
@@ -620,20 +620,20 @@ GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh,
620 * #GNUNET_SYSERR if the address is malformed 620 * #GNUNET_SYSERR if the address is malformed
621 */ 621 */
622int 622int
623GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh, 623GNUNET_NAT_test_address(struct GNUNET_NAT_Handle *nh,
624 const void *addr, 624 const void *addr,
625 socklen_t addrlen) 625 socklen_t addrlen)
626{ 626{
627 struct AddrEntry *ae; 627 struct AddrEntry *ae;
628 628
629 if ((addrlen != sizeof (struct sockaddr_in)) && 629 if ((addrlen != sizeof(struct sockaddr_in)) &&
630 (addrlen != sizeof (struct sockaddr_in6))) 630 (addrlen != sizeof(struct sockaddr_in6)))
631 { 631 {
632 GNUNET_break (0); 632 GNUNET_break(0);
633 return GNUNET_SYSERR; 633 return GNUNET_SYSERR;
634 } 634 }
635 for (ae = nh->ae_head; NULL != ae; ae = ae->next) 635 for (ae = nh->ae_head; NULL != ae; ae = ae->next)
636 if ((addrlen == ae->addrlen) && (0 == memcmp (addr, &ae[1], addrlen))) 636 if ((addrlen == ae->addrlen) && (0 == memcmp(addr, &ae[1], addrlen)))
637 return GNUNET_YES; 637 return GNUNET_YES;
638 return GNUNET_NO; 638 return GNUNET_NO;
639} 639}
@@ -652,9 +652,9 @@ GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh,
652 * #GNUNET_OK otherwise (presumably in progress) 652 * #GNUNET_OK otherwise (presumably in progress)
653 */ 653 */
654int 654int
655GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh, 655GNUNET_NAT_request_reversal(struct GNUNET_NAT_Handle *nh,
656 const struct sockaddr_in *local_sa, 656 const struct sockaddr_in *local_sa,
657 const struct sockaddr_in *remote_sa) 657 const struct sockaddr_in *remote_sa)
658{ 658{
659 struct GNUNET_MQ_Envelope *env; 659 struct GNUNET_MQ_Envelope *env;
660 struct GNUNET_NAT_RequestConnectionReversalMessage *req; 660 struct GNUNET_NAT_RequestConnectionReversalMessage *req;
@@ -662,19 +662,19 @@ GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh,
662 662
663 if (NULL == nh->mq) 663 if (NULL == nh->mq)
664 return GNUNET_SYSERR; 664 return GNUNET_SYSERR;
665 GNUNET_break (AF_INET == local_sa->sin_family); 665 GNUNET_break(AF_INET == local_sa->sin_family);
666 GNUNET_break (AF_INET == remote_sa->sin_family); 666 GNUNET_break(AF_INET == remote_sa->sin_family);
667 env = 667 env =
668 GNUNET_MQ_msg_extra (req, 668 GNUNET_MQ_msg_extra(req,
669 2 * sizeof (struct sockaddr_in), 669 2 * sizeof(struct sockaddr_in),
670 GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL); 670 GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL);
671 req->local_addr_size = htons (sizeof (struct sockaddr_in)); 671 req->local_addr_size = htons(sizeof(struct sockaddr_in));
672 req->remote_addr_size = htons (sizeof (struct sockaddr_in)); 672 req->remote_addr_size = htons(sizeof(struct sockaddr_in));
673 buf = (char *) &req[1]; 673 buf = (char *)&req[1];
674 GNUNET_memcpy (buf, local_sa, sizeof (struct sockaddr_in)); 674 GNUNET_memcpy(buf, local_sa, sizeof(struct sockaddr_in));
675 buf += sizeof (struct sockaddr_in); 675 buf += sizeof(struct sockaddr_in);
676 GNUNET_memcpy (buf, remote_sa, sizeof (struct sockaddr_in)); 676 GNUNET_memcpy(buf, remote_sa, sizeof(struct sockaddr_in));
677 GNUNET_MQ_send (nh->mq, env); 677 GNUNET_MQ_send(nh->mq, env);
678 return GNUNET_OK; 678 return GNUNET_OK;
679} 679}
680 680
@@ -687,20 +687,20 @@ GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh,
687 * @param nh the handle to stop 687 * @param nh the handle to stop
688 */ 688 */
689void 689void
690GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh) 690GNUNET_NAT_unregister(struct GNUNET_NAT_Handle *nh)
691{ 691{
692 if (NULL != nh->mq) 692 if (NULL != nh->mq)
693 { 693 {
694 GNUNET_MQ_destroy (nh->mq); 694 GNUNET_MQ_destroy(nh->mq);
695 nh->mq = NULL; 695 nh->mq = NULL;
696 } 696 }
697 if (NULL != nh->reconnect_task) 697 if (NULL != nh->reconnect_task)
698 { 698 {
699 GNUNET_SCHEDULER_cancel (nh->reconnect_task); 699 GNUNET_SCHEDULER_cancel(nh->reconnect_task);
700 nh->reconnect_task = NULL; 700 nh->reconnect_task = NULL;
701 } 701 }
702 GNUNET_free (nh->reg); 702 GNUNET_free(nh->reg);
703 GNUNET_free (nh); 703 GNUNET_free(nh);
704} 704}
705 705
706 706
diff --git a/src/nat/nat_api_stun.c b/src/nat/nat_api_stun.c
index a0931a06f..0f8694867 100644
--- a/src/nat/nat_api_stun.c
+++ b/src/nat/nat_api_stun.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 * This code provides some support for doing STUN transactions. 21 * This code provides some support for doing STUN transactions.
22 * We send simplest possible packet ia REQUEST with BIND to a STUN server. 22 * We send simplest possible packet ia REQUEST with BIND to a STUN server.
@@ -43,9 +43,9 @@
43 43
44#include "nat_stun.h" 44#include "nat_stun.h"
45 45
46#define LOG(kind,...) GNUNET_log_from (kind, "stun", __VA_ARGS__) 46#define LOG(kind, ...) GNUNET_log_from(kind, "stun", __VA_ARGS__)
47 47
48#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15) 48#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15)
49 49
50 50
51/** 51/**
@@ -53,9 +53,7 @@
53 * the request prior to the timeout or successful execution. Also 53 * the request prior to the timeout or successful execution. Also
54 * used to track our internal state for the request. 54 * used to track our internal state for the request.
55 */ 55 */
56struct GNUNET_NAT_STUN_Handle 56struct GNUNET_NAT_STUN_Handle {
57{
58
59 /** 57 /**
60 * Handle to a pending DNS lookup request. 58 * Handle to a pending DNS lookup request.
61 */ 59 */
@@ -90,7 +88,6 @@ struct GNUNET_NAT_STUN_Handle
90 * STUN port 88 * STUN port
91 */ 89 */
92 uint16_t stun_port; 90 uint16_t stun_port;
93
94}; 91};
95 92
96 93
@@ -102,11 +99,11 @@ struct GNUNET_NAT_STUN_Handle
102 * @return message in a STUN compatible format 99 * @return message in a STUN compatible format
103 */ 100 */
104static int 101static int
105encode_message (enum StunClasses msg_class, 102encode_message(enum StunClasses msg_class,
106 enum StunMethods method) 103 enum StunMethods method)
107{ 104{
108 return ((msg_class & 1) << 4) | ((msg_class & 2) << 7) | 105 return ((msg_class & 1) << 4) | ((msg_class & 2) << 7) |
109 (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800) << 2); 106 (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800) << 2);
110} 107}
111 108
112 109
@@ -116,12 +113,12 @@ encode_message (enum StunClasses msg_class,
116 * @param req, stun header to be filled 113 * @param req, stun header to be filled
117 */ 114 */
118static void 115static void
119generate_request_id (struct stun_header *req) 116generate_request_id(struct stun_header *req)
120{ 117{
121 req->magic = htonl(STUN_MAGIC_COOKIE); 118 req->magic = htonl(STUN_MAGIC_COOKIE);
122 for (unsigned int x = 0; x < 3; x++) 119 for (unsigned int x = 0; x < 3; x++)
123 req->id.id[x] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 120 req->id.id[x] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE,
124 UINT32_MAX); 121 UINT32_MAX);
125} 122}
126 123
127 124
@@ -133,67 +130,67 @@ generate_request_id (struct stun_header *req)
133 * @param addrlen length of @a addr 130 * @param addrlen length of @a addr
134 */ 131 */
135static void 132static void
136stun_dns_callback (void *cls, 133stun_dns_callback(void *cls,
137 const struct sockaddr *addr, 134 const struct sockaddr *addr,
138 socklen_t addrlen) 135 socklen_t addrlen)
139{ 136{
140 struct GNUNET_NAT_STUN_Handle *rh = cls; 137 struct GNUNET_NAT_STUN_Handle *rh = cls;
141 struct stun_header req; 138 struct stun_header req;
142 struct sockaddr_in server; 139 struct sockaddr_in server;
143 140
144 if (NULL == addr) 141 if (NULL == addr)
145 {
146 rh->dns_active = NULL;
147 if (GNUNET_NO == rh->dns_success)
148 {
149 LOG (GNUNET_ERROR_TYPE_INFO,
150 "Error resolving host %s\n",
151 rh->stun_server);
152 rh->cb (rh->cb_cls,
153 GNUNET_NAT_ERROR_NOT_ONLINE);
154 }
155 else if (GNUNET_SYSERR == rh->dns_success)
156 { 142 {
157 rh->cb (rh->cb_cls, 143 rh->dns_active = NULL;
158 GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR); 144 if (GNUNET_NO == rh->dns_success)
145 {
146 LOG(GNUNET_ERROR_TYPE_INFO,
147 "Error resolving host %s\n",
148 rh->stun_server);
149 rh->cb(rh->cb_cls,
150 GNUNET_NAT_ERROR_NOT_ONLINE);
151 }
152 else if (GNUNET_SYSERR == rh->dns_success)
153 {
154 rh->cb(rh->cb_cls,
155 GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR);
156 }
157 else
158 {
159 rh->cb(rh->cb_cls,
160 GNUNET_NAT_ERROR_SUCCESS);
161 }
162 GNUNET_NAT_stun_make_request_cancel(rh);
163 return;
159 } 164 }
160 else
161 {
162 rh->cb (rh->cb_cls,
163 GNUNET_NAT_ERROR_SUCCESS);
164 }
165 GNUNET_NAT_stun_make_request_cancel (rh);
166 return;
167 }
168 165
169 rh->dns_success = GNUNET_YES; 166 rh->dns_success = GNUNET_YES;
170 memset (&server, 0, sizeof(server)); 167 memset(&server, 0, sizeof(server));
171 server.sin_family = AF_INET; 168 server.sin_family = AF_INET;
172 server.sin_addr = ((struct sockaddr_in *)addr)->sin_addr; 169 server.sin_addr = ((struct sockaddr_in *)addr)->sin_addr;
173 server.sin_port = htons (rh->stun_port); 170 server.sin_port = htons(rh->stun_port);
174#if HAVE_SOCKADDR_IN_SIN_LEN 171#if HAVE_SOCKADDR_IN_SIN_LEN
175 server.sin_len = (u_char) sizeof (struct sockaddr_in); 172 server.sin_len = (u_char)sizeof(struct sockaddr_in);
176#endif 173#endif
177 174
178 /* Craft the simplest possible STUN packet. A request binding */ 175 /* Craft the simplest possible STUN packet. A request binding */
179 generate_request_id (&req); 176 generate_request_id(&req);
180 req.msglen = htons (0); 177 req.msglen = htons(0);
181 req.msgtype = htons (encode_message (STUN_REQUEST, 178 req.msgtype = htons(encode_message(STUN_REQUEST,
182 STUN_BINDING)); 179 STUN_BINDING));
183 180
184 /* Send the packet */ 181 /* Send the packet */
185 if (-1 == 182 if (-1 ==
186 GNUNET_NETWORK_socket_sendto (rh->sock, 183 GNUNET_NETWORK_socket_sendto(rh->sock,
187 &req, 184 &req,
188 sizeof (req), 185 sizeof(req),
189 (const struct sockaddr *) &server, 186 (const struct sockaddr *)&server,
190 sizeof (server))) 187 sizeof(server)))
191 { 188 {
192 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, 189 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR,
193 "sendto"); 190 "sendto");
194 rh->dns_success = GNUNET_SYSERR; 191 rh->dns_success = GNUNET_SYSERR;
195 return; 192 return;
196 } 193 }
197} 194}
198 195
199 196
@@ -209,31 +206,31 @@ stun_dns_callback (void *cls,
209 * @return NULL on error 206 * @return NULL on error
210 */ 207 */
211struct GNUNET_NAT_STUN_Handle * 208struct GNUNET_NAT_STUN_Handle *
212GNUNET_NAT_stun_make_request (const char *server, 209GNUNET_NAT_stun_make_request(const char *server,
213 uint16_t port, 210 uint16_t port,
214 struct GNUNET_NETWORK_Handle *sock, 211 struct GNUNET_NETWORK_Handle *sock,
215 GNUNET_NAT_TestCallback cb, 212 GNUNET_NAT_TestCallback cb,
216 void *cb_cls) 213 void *cb_cls)
217{ 214{
218 struct GNUNET_NAT_STUN_Handle *rh; 215 struct GNUNET_NAT_STUN_Handle *rh;
219 216
220 rh = GNUNET_new (struct GNUNET_NAT_STUN_Handle); 217 rh = GNUNET_new(struct GNUNET_NAT_STUN_Handle);
221 rh->sock = sock; 218 rh->sock = sock;
222 rh->cb = cb; 219 rh->cb = cb;
223 rh->cb_cls = cb_cls; 220 rh->cb_cls = cb_cls;
224 rh->stun_server = GNUNET_strdup (server); 221 rh->stun_server = GNUNET_strdup(server);
225 rh->stun_port = port; 222 rh->stun_port = port;
226 rh->dns_success = GNUNET_NO; 223 rh->dns_success = GNUNET_NO;
227 rh->dns_active = GNUNET_RESOLVER_ip_get (rh->stun_server, 224 rh->dns_active = GNUNET_RESOLVER_ip_get(rh->stun_server,
228 AF_INET, 225 AF_INET,
229 TIMEOUT, 226 TIMEOUT,
230 &stun_dns_callback, 227 &stun_dns_callback,
231 rh); 228 rh);
232 if (NULL == rh->dns_active) 229 if (NULL == rh->dns_active)
233 { 230 {
234 GNUNET_NAT_stun_make_request_cancel (rh); 231 GNUNET_NAT_stun_make_request_cancel(rh);
235 return NULL; 232 return NULL;
236 } 233 }
237 return rh; 234 return rh;
238} 235}
239 236
@@ -245,15 +242,15 @@ GNUNET_NAT_stun_make_request (const char *server,
245 * @param rh request to cancel 242 * @param rh request to cancel
246 */ 243 */
247void 244void
248GNUNET_NAT_stun_make_request_cancel (struct GNUNET_NAT_STUN_Handle *rh) 245GNUNET_NAT_stun_make_request_cancel(struct GNUNET_NAT_STUN_Handle *rh)
249{ 246{
250 if (NULL != rh->dns_active) 247 if (NULL != rh->dns_active)
251 { 248 {
252 GNUNET_RESOLVER_request_cancel (rh->dns_active); 249 GNUNET_RESOLVER_request_cancel(rh->dns_active);
253 rh->dns_active = NULL; 250 rh->dns_active = NULL;
254 } 251 }
255 GNUNET_free (rh->stun_server); 252 GNUNET_free(rh->stun_server);
256 GNUNET_free (rh); 253 GNUNET_free(rh);
257} 254}
258 255
259 256
diff --git a/src/nat/nat_stun.h b/src/nat/nat_stun.h
index fb5262dfa..85b177271 100644
--- a/src/nat/nat_stun.h
+++ b/src/nat/nat_stun.h
@@ -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 * Message types for STUN server resolution 21 * Message types for STUN server resolution
22 * 22 *
@@ -28,18 +28,17 @@
28 */ 28 */
29 29
30 30
31#define STUN_IGNORE (0) 31#define STUN_IGNORE (0)
32#define STUN_ACCEPT (1) 32#define STUN_ACCEPT (1)
33 33
34#define STUN_MAGIC_COOKIE 0x2112A442 34#define STUN_MAGIC_COOKIE 0x2112A442
35 35
36typedef struct { 36typedef struct {
37 uint32_t id[3]; 37 uint32_t id[3];
38} GNUNET_PACKED stun_trans_id; 38} GNUNET_PACKED stun_trans_id;
39 39
40 40
41struct stun_header 41struct stun_header {
42{
43 uint16_t msgtype; 42 uint16_t msgtype;
44 uint16_t msglen; 43 uint16_t msglen;
45 uint32_t magic; 44 uint32_t magic;
@@ -47,8 +46,7 @@ struct stun_header
47} GNUNET_PACKED; 46} GNUNET_PACKED;
48 47
49 48
50struct stun_attr 49struct stun_attr {
51{
52 uint16_t attr; 50 uint16_t attr;
53 uint16_t len; 51 uint16_t len;
54} GNUNET_PACKED; 52} GNUNET_PACKED;
@@ -57,14 +55,13 @@ struct stun_attr
57/** 55/**
58 * The format normally used for addresses carried by STUN messages. 56 * The format normally used for addresses carried by STUN messages.
59 */ 57 */
60struct stun_addr 58struct stun_addr {
61{ 59 uint8_t unused;
62 uint8_t unused;
63 60
64 /** 61 /**
65 * Address family, we expect AF_INET. 62 * Address family, we expect AF_INET.
66 */ 63 */
67 uint8_t family; 64 uint8_t family;
68 65
69 /** 66 /**
70 * Port number. 67 * Port number.
@@ -74,7 +71,7 @@ struct stun_addr
74 /** 71 /**
75 * IPv4 address. Should this be "struct in_addr"? 72 * IPv4 address. Should this be "struct in_addr"?
76 */ 73 */
77 uint32_t addr; 74 uint32_t addr;
78} GNUNET_PACKED; 75} GNUNET_PACKED;
79 76
80 77
@@ -136,10 +133,10 @@ enum StunAttributes {
136 * @return the converted StunClass 133 * @return the converted StunClass
137 */ 134 */
138static enum StunClasses 135static enum StunClasses
139decode_class (int msg) 136decode_class(int msg)
140{ 137{
141 /* Sorry for the magic, but this maps the class according to rfc5245 */ 138 /* Sorry for the magic, but this maps the class according to rfc5245 */
142 return (enum StunClasses) ((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7); 139 return (enum StunClasses)((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7);
143} 140}
144 141
145 142
@@ -150,9 +147,9 @@ decode_class (int msg)
150 * @return the converted StunMethod 147 * @return the converted StunMethod
151 */ 148 */
152static enum StunMethods 149static enum StunMethods
153decode_method (int msg) 150decode_method(int msg)
154{ 151{
155 return (enum StunMethods) (msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg & 0x3e00) >> 2); 152 return (enum StunMethods)(msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg & 0x3e00) >> 2);
156} 153}
157 154
158 155
@@ -164,7 +161,7 @@ decode_method (int msg)
164 */ 161 */
165GNUNET_UNUSED 162GNUNET_UNUSED
166static const char * 163static const char *
167stun_msg2str (int msg) 164stun_msg2str(int msg)
168{ 165{
169 static const struct { 166 static const struct {
170 enum StunClasses value; 167 enum StunClasses value;
@@ -189,25 +186,25 @@ stun_msg2str (int msg)
189 enum StunClasses cvalue; 186 enum StunClasses cvalue;
190 enum StunMethods mvalue; 187 enum StunMethods mvalue;
191 188
192 cvalue = decode_class (msg); 189 cvalue = decode_class(msg);
193 for (unsigned int i = 0; classes[i].name; i++) 190 for (unsigned int i = 0; classes[i].name; i++)
194 if (classes[i].value == cvalue) 191 if (classes[i].value == cvalue)
195 { 192 {
196 msg_class = classes[i].name; 193 msg_class = classes[i].name;
197 break; 194 break;
198 } 195 }
199 mvalue = decode_method (msg); 196 mvalue = decode_method(msg);
200 for (unsigned int i = 0; methods[i].name; i++) 197 for (unsigned int i = 0; methods[i].name; i++)
201 if (methods[i].value == mvalue) 198 if (methods[i].value == mvalue)
202 { 199 {
203 method = methods[i].name; 200 method = methods[i].name;
204 break; 201 break;
205 } 202 }
206 GNUNET_snprintf (result, 203 GNUNET_snprintf(result,
207 sizeof(result), 204 sizeof(result),
208 "%s %s", 205 "%s %s",
209 method ? : "Unknown Method", 206 method ? : "Unknown Method",
210 msg_class ? : "Unknown Class Message"); 207 msg_class ? : "Unknown Class Message");
211 return result; 208 return result;
212} 209}
213 210
@@ -220,7 +217,7 @@ stun_msg2str (int msg)
220 */ 217 */
221GNUNET_UNUSED 218GNUNET_UNUSED
222static const char * 219static const char *
223stun_attr2str (enum StunAttributes msg) 220stun_attr2str(enum StunAttributes msg)
224{ 221{
225 static const struct { 222 static const struct {
226 enum StunAttributes value; 223 enum StunAttributes value;
diff --git a/src/nat/test_nat.c b/src/nat/test_nat.c
index 80d7cf792..5758f63b9 100644
--- a/src/nat/test_nat.c
+++ b/src/nat/test_nat.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/** 21/**
22 * Testcase for port redirection and public IP address retrieval. 22 * Testcase for port redirection and public IP address retrieval.
@@ -43,7 +43,7 @@
43/** 43/**
44 * Time to wait before stopping NAT, in seconds 44 * Time to wait before stopping NAT, in seconds
45 */ 45 */
46#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) 46#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
47 47
48 48
49/** 49/**
@@ -51,15 +51,15 @@
51 * believes to be valid for the transport. 51 * believes to be valid for the transport.
52 */ 52 */
53static void 53static void
54addr_callback (void *cls, int add_remove, const struct sockaddr *addr, 54addr_callback(void *cls, int add_remove, const struct sockaddr *addr,
55 socklen_t addrlen) 55 socklen_t addrlen)
56{ 56{
57 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 57 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
58 "Address changed: %s `%s' (%u bytes)\n", 58 "Address changed: %s `%s' (%u bytes)\n",
59 add_remove == GNUNET_YES ? "added" : "removed", 59 add_remove == GNUNET_YES ? "added" : "removed",
60 GNUNET_a2s (addr, 60 GNUNET_a2s(addr,
61 addrlen), 61 addrlen),
62 (unsigned int) addrlen); 62 (unsigned int)addrlen);
63} 63}
64 64
65 65
@@ -67,18 +67,17 @@ addr_callback (void *cls, int add_remove, const struct sockaddr *addr,
67 * Function that terminates the test. 67 * Function that terminates the test.
68 */ 68 */
69static void 69static void
70stop (void *cls) 70stop(void *cls)
71{ 71{
72 struct GNUNET_NAT_Handle *nat = cls; 72 struct GNUNET_NAT_Handle *nat = cls;
73 73
74 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 74 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
75 "Stopping NAT and quitting...\n"); 75 "Stopping NAT and quitting...\n");
76 GNUNET_NAT_unregister (nat); 76 GNUNET_NAT_unregister(nat);
77} 77}
78 78
79 79
80struct addr_cls 80struct addr_cls {
81{
82 struct sockaddr *addr; 81 struct sockaddr *addr;
83 socklen_t addrlen; 82 socklen_t addrlen;
84}; 83};
@@ -96,21 +95,21 @@ struct addr_cls
96 * @return #GNUNET_OK to continue iterating 95 * @return #GNUNET_OK to continue iterating
97 */ 96 */
98static int 97static int
99process_if (void *cls, 98process_if(void *cls,
100 const char *name, 99 const char *name,
101 int isDefault, 100 int isDefault,
102 const struct sockaddr *addr, 101 const struct sockaddr *addr,
103 const struct sockaddr *broadcast_addr, 102 const struct sockaddr *broadcast_addr,
104 const struct sockaddr *netmask, 103 const struct sockaddr *netmask,
105 socklen_t addrlen) 104 socklen_t addrlen)
106{ 105{
107 struct addr_cls *data = cls; 106 struct addr_cls *data = cls;
108 107
109 if (addr == NULL) 108 if (addr == NULL)
110 return GNUNET_OK; 109 return GNUNET_OK;
111 GNUNET_free_non_null (data->addr); 110 GNUNET_free_non_null(data->addr);
112 data->addr = GNUNET_malloc (addrlen); 111 data->addr = GNUNET_malloc(addrlen);
113 GNUNET_memcpy (data->addr, addr, addrlen); 112 GNUNET_memcpy(data->addr, addr, addrlen);
114 data->addrlen = addrlen; 113 data->addrlen = addrlen;
115 if (isDefault) 114 if (isDefault)
116 return GNUNET_SYSERR; 115 return GNUNET_SYSERR;
@@ -122,44 +121,44 @@ process_if (void *cls,
122 * Main function run with scheduler. 121 * Main function run with scheduler.
123 */ 122 */
124static void 123static void
125run (void *cls, 124run(void *cls,
126 char *const *args, 125 char *const *args,
127 const char *cfgfile, 126 const char *cfgfile,
128 const struct GNUNET_CONFIGURATION_Handle *cfg) 127 const struct GNUNET_CONFIGURATION_Handle *cfg)
129{ 128{
130 struct GNUNET_NAT_Handle *nat; 129 struct GNUNET_NAT_Handle *nat;
131 struct addr_cls data; 130 struct addr_cls data;
132 struct sockaddr *addr; 131 struct sockaddr *addr;
133 132
134 data.addr = NULL; 133 data.addr = NULL;
135 GNUNET_OS_network_interfaces_list (process_if, &data); 134 GNUNET_OS_network_interfaces_list(process_if, &data);
136 if (NULL == data.addr) 135 if (NULL == data.addr)
137 { 136 {
138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 137 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
139 "Could not find a valid interface address!\n"); 138 "Could not find a valid interface address!\n");
140 exit (77); /* marks test as skipped */ 139 exit(77); /* marks test as skipped */
141 } 140 }
142 addr = data.addr; 141 addr = data.addr;
143 GNUNET_assert (addr->sa_family == AF_INET || addr->sa_family == AF_INET6); 142 GNUNET_assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
144 if (addr->sa_family == AF_INET) 143 if (addr->sa_family == AF_INET)
145 ((struct sockaddr_in *) addr)->sin_port = htons (2086); 144 ((struct sockaddr_in *)addr)->sin_port = htons(2086);
146 else 145 else
147 ((struct sockaddr_in6 *) addr)->sin6_port = htons (2086); 146 ((struct sockaddr_in6 *)addr)->sin6_port = htons(2086);
148 147
149 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 148 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
150 "Requesting NAT redirection from address %s...\n", 149 "Requesting NAT redirection from address %s...\n",
151 GNUNET_a2s (addr, data.addrlen)); 150 GNUNET_a2s(addr, data.addrlen));
152 151
153 nat = GNUNET_NAT_register (cfg, GNUNET_YES /* tcp */ , 152 nat = GNUNET_NAT_register(cfg, GNUNET_YES /* tcp */,
154 2086, 1, (const struct sockaddr **) &addr, 153 2086, 1, (const struct sockaddr **)&addr,
155 &data.addrlen, &addr_callback, NULL, NULL, NULL); 154 &data.addrlen, &addr_callback, NULL, NULL, NULL);
156 GNUNET_free (addr); 155 GNUNET_free(addr);
157 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, nat); 156 GNUNET_SCHEDULER_add_delayed(TIMEOUT, &stop, nat);
158} 157}
159 158
160 159
161int 160int
162main (int argc, char *const argv[]) 161main(int argc, char *const argv[])
163{ 162{
164 struct GNUNET_GETOPT_CommandLineOption options[] = { 163 struct GNUNET_GETOPT_CommandLineOption options[] = {
165 GNUNET_GETOPT_OPTION_END 164 GNUNET_GETOPT_OPTION_END
@@ -171,19 +170,20 @@ main (int argc, char *const argv[])
171 "test_nat_data.conf", 170 "test_nat_data.conf",
172 NULL 171 NULL
173 }; 172 };
174 GNUNET_log_setup ("test-nat", 173
175 "WARNING", 174 GNUNET_log_setup("test-nat",
176 NULL); 175 "WARNING",
177 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 176 NULL);
178 "Testing NAT library, timeout set to %s\n", 177 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
179 GNUNET_STRINGS_relative_time_to_string (TIMEOUT, 178 "Testing NAT library, timeout set to %s\n",
180 GNUNET_YES)); 179 GNUNET_STRINGS_relative_time_to_string(TIMEOUT,
181 GNUNET_PROGRAM_run (3, 180 GNUNET_YES));
182 argv_prog, 181 GNUNET_PROGRAM_run(3,
183 "test-nat", 182 argv_prog,
184 "nohelp", 183 "test-nat",
185 options, 184 "nohelp",
186 &run, NULL); 185 options,
186 &run, NULL);
187 return 0; 187 return 0;
188} 188}
189 189
diff --git a/src/nat/test_nat_mini.c b/src/nat/test_nat_mini.c
index 8a7ff16f4..09231d8ec 100644
--- a/src/nat/test_nat_mini.c
+++ b/src/nat/test_nat_mini.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/** 21/**
22 * Testcase for port redirection and public IP address retrieval. 22 * Testcase for port redirection and public IP address retrieval.
@@ -35,31 +35,31 @@
35#include "gnunet_nat_lib.h" 35#include "gnunet_nat_lib.h"
36 36
37/* Time to wait before stopping NAT, in seconds */ 37/* Time to wait before stopping NAT, in seconds */
38#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) 38#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
39 39
40/** 40/**
41 * Function called on each address that the NAT service 41 * Function called on each address that the NAT service
42 * believes to be valid for the transport. 42 * believes to be valid for the transport.
43 */ 43 */
44static void 44static void
45addr_callback (void *cls, int add_remove, 45addr_callback(void *cls, int add_remove,
46 const struct sockaddr *addr, 46 const struct sockaddr *addr,
47 socklen_t addrlen, 47 socklen_t addrlen,
48 enum GNUNET_NAT_StatusCode ret) 48 enum GNUNET_NAT_StatusCode ret)
49{ 49{
50 if (GNUNET_NAT_ERROR_SUCCESS == ret) 50 if (GNUNET_NAT_ERROR_SUCCESS == ret)
51 { 51 {
52 fprintf (stderr, 52 fprintf(stderr,
53 "Address changed: %s `%s' (%u bytes)\n", 53 "Address changed: %s `%s' (%u bytes)\n",
54 add_remove == GNUNET_YES 54 add_remove == GNUNET_YES
55 ? "added" : "removed", 55 ? "added" : "removed",
56 GNUNET_a2s (addr, 56 GNUNET_a2s(addr,
57 addrlen), 57 addrlen),
58 (unsigned int) addrlen); 58 (unsigned int)addrlen);
59 } 59 }
60 else 60 else
61 ; 61 ;
62 //TODO: proper error handling! 62 //TODO: proper error handling!
63} 63}
64 64
65 65
@@ -67,12 +67,12 @@ addr_callback (void *cls, int add_remove,
67 * Function that terminates the test. 67 * Function that terminates the test.
68 */ 68 */
69static void 69static void
70stop (void *cls) 70stop(void *cls)
71{ 71{
72 struct GNUNET_NAT_MiniHandle *mini = cls; 72 struct GNUNET_NAT_MiniHandle *mini = cls;
73 73
74 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n"); 74 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n");
75 GNUNET_NAT_mini_map_stop (mini); 75 GNUNET_NAT_mini_map_stop(mini);
76} 76}
77 77
78#define PORT 10000 78#define PORT 10000
@@ -81,27 +81,27 @@ stop (void *cls)
81 * Main function run with scheduler. 81 * Main function run with scheduler.
82 */ 82 */
83static void 83static void
84run (void *cls, char *const *args, const char *cfgfile, 84run(void *cls, char *const *args, const char *cfgfile,
85 const struct GNUNET_CONFIGURATION_Handle *cfg) 85 const struct GNUNET_CONFIGURATION_Handle *cfg)
86{ 86{
87 struct GNUNET_NAT_MiniHandle *mini; 87 struct GNUNET_NAT_MiniHandle *mini;
88 88
89 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 89 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
90 "Requesting NAT redirection for port %u...\n", 90 "Requesting NAT redirection for port %u...\n",
91 PORT); 91 PORT);
92 mini = GNUNET_NAT_mini_map_start (PORT, GNUNET_YES /* tcp */ , 92 mini = GNUNET_NAT_mini_map_start(PORT, GNUNET_YES /* tcp */,
93 &addr_callback, NULL); 93 &addr_callback, NULL);
94 if (NULL == mini) 94 if (NULL == mini)
95 { 95 {
96 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Could not start UPnP interaction\n"); 96 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Could not start UPnP interaction\n");
97 return; 97 return;
98 } 98 }
99 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, mini); 99 GNUNET_SCHEDULER_add_delayed(TIMEOUT, &stop, mini);
100} 100}
101 101
102 102
103int 103int
104main (int argc, char *const argv[]) 104main(int argc, char *const argv[])
105{ 105{
106 struct GNUNET_GETOPT_CommandLineOption options[] = { 106 struct GNUNET_GETOPT_CommandLineOption options[] = {
107 GNUNET_GETOPT_OPTION_END 107 GNUNET_GETOPT_OPTION_END
@@ -116,16 +116,16 @@ main (int argc, char *const argv[])
116 NULL 116 NULL
117 }; 117 };
118 118
119 GNUNET_log_setup ("test-nat-mini", 119 GNUNET_log_setup("test-nat-mini",
120 "WARNING", 120 "WARNING",
121 NULL); 121 NULL);
122 122
123 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 123 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
124 "UPnP test for NAT library, timeout set to %s\n", 124 "UPnP test for NAT library, timeout set to %s\n",
125 GNUNET_STRINGS_relative_time_to_string (TIMEOUT, 125 GNUNET_STRINGS_relative_time_to_string(TIMEOUT,
126 GNUNET_YES)); 126 GNUNET_YES));
127 GNUNET_PROGRAM_run (5, argv_prog, "test-nat-mini", "nohelp", options, &run, 127 GNUNET_PROGRAM_run(5, argv_prog, "test-nat-mini", "nohelp", options, &run,
128 NULL); 128 NULL);
129 return 0; 129 return 0;
130} 130}
131 131
diff --git a/src/nat/test_nat_test.c b/src/nat/test_nat_test.c
index c9479a688..e217c1d29 100644
--- a/src/nat/test_nat_test.c
+++ b/src/nat/test_nat_test.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 nat/test_nat_test.c 21 * @file nat/test_nat_test.c
22 * @brief Testcase for NAT testing functions 22 * @brief Testcase for NAT testing functions
@@ -29,7 +29,7 @@
29/** 29/**
30 * Time to wait before stopping NAT test, in seconds 30 * Time to wait before stopping NAT test, in seconds
31 */ 31 */
32#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15) 32#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15)
33 33
34 34
35static int ret = 1; 35static int ret = 1;
@@ -40,32 +40,32 @@ static struct GNUNET_SCHEDULER_Task * tsk;
40 40
41 41
42static void 42static void
43report_result (void *cls, 43report_result(void *cls,
44 enum GNUNET_NAT_StatusCode aret) 44 enum GNUNET_NAT_StatusCode aret)
45{ 45{
46 if (GNUNET_NAT_ERROR_TIMEOUT == aret) 46 if (GNUNET_NAT_ERROR_TIMEOUT == aret)
47 fprintf (stderr, 47 fprintf(stderr,
48 "NAT test timed out\n"); 48 "NAT test timed out\n");
49 else if (GNUNET_NAT_ERROR_SUCCESS != aret) 49 else if (GNUNET_NAT_ERROR_SUCCESS != aret)
50 fprintf (stderr, 50 fprintf(stderr,
51 "NAT test reported error %d\n", aret); 51 "NAT test reported error %d\n", aret);
52 else 52 else
53 ret = 0; 53 ret = 0;
54 GNUNET_NAT_test_stop (tst); 54 GNUNET_NAT_test_stop(tst);
55 tst = NULL; 55 tst = NULL;
56 GNUNET_SCHEDULER_cancel (tsk); 56 GNUNET_SCHEDULER_cancel(tsk);
57 tsk = NULL; 57 tsk = NULL;
58} 58}
59 59
60 60
61static void 61static void
62failed_timeout (void *cls) 62failed_timeout(void *cls)
63{ 63{
64 tsk = NULL; 64 tsk = NULL;
65 fprintf (stderr, 65 fprintf(stderr,
66 "NAT test failed to terminate on timeout\n"); 66 "NAT test failed to terminate on timeout\n");
67 ret = 2; 67 ret = 2;
68 GNUNET_NAT_test_stop (tst); 68 GNUNET_NAT_test_stop(tst);
69 tst = NULL; 69 tst = NULL;
70} 70}
71 71
@@ -74,23 +74,22 @@ failed_timeout (void *cls)
74 * Main function run with scheduler. 74 * Main function run with scheduler.
75 */ 75 */
76static void 76static void
77run (void *cls, char *const *args, const char *cfgfile, 77run(void *cls, char *const *args, const char *cfgfile,
78 const struct GNUNET_CONFIGURATION_Handle *cfg) 78 const struct GNUNET_CONFIGURATION_Handle *cfg)
79{ 79{
80 tst = 80 tst =
81 GNUNET_NAT_test_start (cfg, GNUNET_YES, 1285, 1285, TIMEOUT, 81 GNUNET_NAT_test_start(cfg, GNUNET_YES, 1285, 1285, TIMEOUT,
82 &report_result, 82 &report_result,
83 NULL); 83 NULL);
84 tsk = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (TIMEOUT, 84 tsk = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(TIMEOUT,
85 2), 85 2),
86 &failed_timeout, 86 &failed_timeout,
87 NULL); 87 NULL);
88
89} 88}
90 89
91 90
92int 91int
93main (int argc, char *const argv[]) 92main(int argc, char *const argv[])
94{ 93{
95 struct GNUNET_GETOPT_CommandLineOption options[] = { 94 struct GNUNET_GETOPT_CommandLineOption options[] = {
96 GNUNET_GETOPT_OPTION_END 95 GNUNET_GETOPT_OPTION_END
@@ -104,37 +103,37 @@ main (int argc, char *const argv[])
104 NULL 103 NULL
105 }; 104 };
106 105
107 GNUNET_log_setup ("test-nat-test", 106 GNUNET_log_setup("test-nat-test",
108 "WARNING", 107 "WARNING",
109 NULL); 108 NULL);
110 109
111 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server", GNUNET_NO, NULL); 110 nat_res = GNUNET_OS_check_helper_binary("gnunet-nat-server", GNUNET_NO, NULL);
112 if (GNUNET_SYSERR == nat_res) 111 if (GNUNET_SYSERR == nat_res)
113 { 112 {
114 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 113 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
115 "Cannot run NAT test: `%s' file not found\n", 114 "Cannot run NAT test: `%s' file not found\n",
116 "gnunet-nat-server"); 115 "gnunet-nat-server");
117 return 0; 116 return 0;
118 } 117 }
119 118
120 gns = GNUNET_OS_start_process (GNUNET_YES, 119 gns = GNUNET_OS_start_process(GNUNET_YES,
121 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 120 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
122 NULL, NULL, NULL, 121 NULL, NULL, NULL,
123 "gnunet-nat-server", 122 "gnunet-nat-server",
124 "gnunet-nat-server", 123 "gnunet-nat-server",
125 "-c", "test_nat_test_data.conf", 124 "-c", "test_nat_test_data.conf",
126 "12345", NULL); 125 "12345", NULL);
127 GNUNET_assert (NULL != gns); 126 GNUNET_assert(NULL != gns);
128 GNUNET_PROGRAM_run (3, argv_prog, 127 GNUNET_PROGRAM_run(3, argv_prog,
129 "test-nat-test", "nohelp", 128 "test-nat-test", "nohelp",
130 options, &run, 129 options, &run,
131 NULL); 130 NULL);
132 GNUNET_break (0 == GNUNET_OS_process_kill (gns, GNUNET_TERM_SIG)); 131 GNUNET_break(0 == GNUNET_OS_process_kill(gns, GNUNET_TERM_SIG));
133 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (gns)); 132 GNUNET_break(GNUNET_OK == GNUNET_OS_process_wait(gns));
134 GNUNET_OS_process_destroy (gns); 133 GNUNET_OS_process_destroy(gns);
135 if (0 != ret) 134 if (0 != ret)
136 fprintf (stderr, 135 fprintf(stderr,
137 "NAT test failed to report success\n"); 136 "NAT test failed to report success\n");
138 return ret; 137 return ret;
139} 138}
140 139
diff --git a/src/nat/test_stun.c b/src/nat/test_stun.c
index ee2db35e5..ee755905e 100644
--- a/src/nat/test_stun.c
+++ b/src/nat/test_stun.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/** 21/**
22 * Testcase for STUN server resolution 22 * Testcase for STUN server resolution
@@ -36,12 +36,12 @@
36 36
37 37
38 38
39#define LOG(kind,...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__) 39#define LOG(kind, ...) GNUNET_log_from(kind, "test-stun", __VA_ARGS__)
40 40
41/** 41/**
42 * Time to wait before stopping NAT, in seconds 42 * Time to wait before stopping NAT, in seconds
43 */ 43 */
44#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) 44#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
45 45
46 46
47/** 47/**
@@ -74,10 +74,10 @@ static struct GNUNET_NAT_STUN_Handle *rh;
74static void 74static void
75print_answer(struct sockaddr_in* answer) 75print_answer(struct sockaddr_in* answer)
76{ 76{
77 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 77 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
78 "External IP is: %s , with port %d\n", 78 "External IP is: %s , with port %d\n",
79 inet_ntoa (answer->sin_addr), 79 inet_ntoa(answer->sin_addr),
80 ntohs (answer->sin_port)); 80 ntohs(answer->sin_port));
81} 81}
82 82
83 83
@@ -85,25 +85,25 @@ print_answer(struct sockaddr_in* answer)
85 * Function that terminates the test. 85 * Function that terminates the test.
86 */ 86 */
87static void 87static void
88stop () 88stop()
89{ 89{
90 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 90 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
91 "Stopping NAT and quitting...\n"); 91 "Stopping NAT and quitting...\n");
92 if (NULL != ltask4) 92 if (NULL != ltask4)
93 { 93 {
94 GNUNET_SCHEDULER_cancel (ltask4); 94 GNUNET_SCHEDULER_cancel(ltask4);
95 ltask4 = NULL; 95 ltask4 = NULL;
96 } 96 }
97 if(NULL != lsock4) 97 if (NULL != lsock4)
98 { 98 {
99 GNUNET_NETWORK_socket_close(lsock4); 99 GNUNET_NETWORK_socket_close(lsock4);
100 lsock4 = NULL; 100 lsock4 = NULL;
101 } 101 }
102 if (NULL != rh) 102 if (NULL != rh)
103 { 103 {
104 GNUNET_NAT_stun_make_request_cancel (rh); 104 GNUNET_NAT_stun_make_request_cancel(rh);
105 rh = NULL; 105 rh = NULL;
106 } 106 }
107} 107}
108 108
109 109
@@ -114,7 +114,7 @@ stop ()
114 * @param cls 114 * @param cls
115 */ 115 */
116static void 116static void
117do_udp_read (void *cls) 117do_udp_read(void *cls)
118{ 118{
119 //struct GNUNET_NAT_Test *tst = cls; 119 //struct GNUNET_NAT_Test *tst = cls;
120 unsigned char reply_buf[1024]; 120 unsigned char reply_buf[1024];
@@ -123,36 +123,36 @@ do_udp_read (void *cls)
123 const struct GNUNET_SCHEDULER_TaskContext *tc; 123 const struct GNUNET_SCHEDULER_TaskContext *tc;
124 124
125 ltask4 = NULL; 125 ltask4 = NULL;
126 tc = GNUNET_SCHEDULER_get_task_context (); 126 tc = GNUNET_SCHEDULER_get_task_context();
127 if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) || 127 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) ||
128 (! GNUNET_NETWORK_fdset_isset (tc->read_ready, 128 (!GNUNET_NETWORK_fdset_isset(tc->read_ready,
129 lsock4)) ) 129 lsock4)))
130 { 130 {
131 fprintf (stderr, 131 fprintf(stderr,
132 "Timeout waiting for STUN response\n"); 132 "Timeout waiting for STUN response\n");
133 stop(); 133 stop();
134 } 134 }
135 rlen = GNUNET_NETWORK_socket_recv (lsock4, 135 rlen = GNUNET_NETWORK_socket_recv(lsock4,
136 reply_buf, 136 reply_buf,
137 sizeof (reply_buf)); 137 sizeof(reply_buf));
138 memset (&answer, 138 memset(&answer,
139 0, 139 0,
140 sizeof(struct sockaddr_in)); 140 sizeof(struct sockaddr_in));
141 if (GNUNET_OK != 141 if (GNUNET_OK !=
142 GNUNET_NAT_stun_handle_packet (reply_buf, 142 GNUNET_NAT_stun_handle_packet(reply_buf,
143 rlen, 143 rlen,
144 &answer)) 144 &answer))
145 { 145 {
146 fprintf (stderr, 146 fprintf(stderr,
147 "Unexpected UDP packet, trying to read more\n"); 147 "Unexpected UDP packet, trying to read more\n");
148 ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT, 148 ltask4 = GNUNET_SCHEDULER_add_read_net(TIMEOUT,
149 lsock4, 149 lsock4,
150 &do_udp_read, NULL); 150 &do_udp_read, NULL);
151 return; 151 return;
152 } 152 }
153 ret = 0; 153 ret = 0;
154 print_answer (&answer); 154 print_answer(&answer);
155 stop (); 155 stop();
156} 156}
157 157
158 158
@@ -162,33 +162,33 @@ do_udp_read (void *cls)
162 * @return NULL on error 162 * @return NULL on error
163 */ 163 */
164static struct GNUNET_NETWORK_Handle * 164static struct GNUNET_NETWORK_Handle *
165bind_v4 () 165bind_v4()
166{ 166{
167 struct GNUNET_NETWORK_Handle *ls; 167 struct GNUNET_NETWORK_Handle *ls;
168 struct sockaddr_in sa4; 168 struct sockaddr_in sa4;
169 int eno; 169 int eno;
170 170
171 memset (&sa4, 0, sizeof (sa4)); 171 memset(&sa4, 0, sizeof(sa4));
172 sa4.sin_family = AF_INET; 172 sa4.sin_family = AF_INET;
173 sa4.sin_port = htons (port); 173 sa4.sin_port = htons(port);
174#if HAVE_SOCKADDR_IN_SIN_LEN 174#if HAVE_SOCKADDR_IN_SIN_LEN
175 sa4.sin_len = sizeof (sa4); 175 sa4.sin_len = sizeof(sa4);
176#endif 176#endif
177 ls = GNUNET_NETWORK_socket_create (AF_INET, 177 ls = GNUNET_NETWORK_socket_create(AF_INET,
178 SOCK_DGRAM, 178 SOCK_DGRAM,
179 0); 179 0);
180 if (NULL == ls) 180 if (NULL == ls)
181 return NULL; 181 return NULL;
182 if (GNUNET_OK != 182 if (GNUNET_OK !=
183 GNUNET_NETWORK_socket_bind (ls, 183 GNUNET_NETWORK_socket_bind(ls,
184 (const struct sockaddr *) &sa4, 184 (const struct sockaddr *)&sa4,
185 sizeof (sa4))) 185 sizeof(sa4)))
186 { 186 {
187 eno = errno; 187 eno = errno;
188 GNUNET_NETWORK_socket_close (ls); 188 GNUNET_NETWORK_socket_close(ls);
189 errno = eno; 189 errno = eno;
190 return NULL; 190 return NULL;
191 } 191 }
192 return ls; 192 return ls;
193} 193}
194 194
@@ -200,29 +200,29 @@ bind_v4 ()
200 * @param error status code from STUN 200 * @param error status code from STUN
201 */ 201 */
202static void 202static void
203request_callback (void *cls, 203request_callback(void *cls,
204 enum GNUNET_NAT_StatusCode error) 204 enum GNUNET_NAT_StatusCode error)
205{ 205{
206 rh = NULL; 206 rh = NULL;
207 if (GNUNET_NAT_ERROR_SUCCESS == error) 207 if (GNUNET_NAT_ERROR_SUCCESS == error)
208 { 208 {
209 /* all good, start to receive */ 209 /* all good, start to receive */
210 ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT, 210 ltask4 = GNUNET_SCHEDULER_add_read_net(TIMEOUT,
211 lsock4, 211 lsock4,
212 &do_udp_read, 212 &do_udp_read,
213 NULL); 213 NULL);
214 return; 214 return;
215 } 215 }
216 if (error == GNUNET_NAT_ERROR_NOT_ONLINE) 216 if (error == GNUNET_NAT_ERROR_NOT_ONLINE)
217 { 217 {
218 ret = 77; /* report 'skip' */ 218 ret = 77; /* report 'skip' */
219 fprintf (stderr, 219 fprintf(stderr,
220 "System is offline, cannot test STUN request.\n"); 220 "System is offline, cannot test STUN request.\n");
221 } 221 }
222 else 222 else
223 { 223 {
224 ret = error; 224 ret = error;
225 } 225 }
226 stop(); 226 stop();
227} 227}
228 228
@@ -231,81 +231,81 @@ request_callback (void *cls,
231 * Main function run with scheduler. 231 * Main function run with scheduler.
232 */ 232 */
233static void 233static void
234run (void *cls, 234run(void *cls,
235 char *const *args, 235 char *const *args,
236 const char *cfgfile, 236 const char *cfgfile,
237 const struct GNUNET_CONFIGURATION_Handle *cfg) 237 const struct GNUNET_CONFIGURATION_Handle *cfg)
238{ 238{
239 //Lets create the socket 239 //Lets create the socket
240 lsock4 = bind_v4 (); 240 lsock4 = bind_v4();
241 if (NULL == lsock4) 241 if (NULL == lsock4)
242 { 242 {
243 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, 243 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR,
244 "bind"); 244 "bind");
245 GNUNET_SCHEDULER_shutdown (); 245 GNUNET_SCHEDULER_shutdown();
246 return; 246 return;
247 } 247 }
248 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 248 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
249 "Service listens on port %u\n", 249 "Service listens on port %u\n",
250 (unsigned int) port); 250 (unsigned int)port);
251 rh = GNUNET_NAT_stun_make_request (stun_server, 251 rh = GNUNET_NAT_stun_make_request(stun_server,
252 stun_port, 252 stun_port,
253 lsock4, 253 lsock4,
254 &request_callback, NULL); 254 &request_callback, NULL);
255 GNUNET_SCHEDULER_add_delayed (TIMEOUT, 255 GNUNET_SCHEDULER_add_delayed(TIMEOUT,
256 &stop, NULL); 256 &stop, NULL);
257} 257}
258 258
259 259
260int 260int
261main (int argc, char *const argv[]) 261main(int argc, char *const argv[])
262{ 262{
263 struct GNUNET_GETOPT_CommandLineOption options[] = { 263 struct GNUNET_GETOPT_CommandLineOption options[] = {
264 GNUNET_GETOPT_OPTION_END 264 GNUNET_GETOPT_OPTION_END
265 }; 265 };
266 char *const argv_prog[] = { 266 char *const argv_prog[] = {
267 "test-stun", 267 "test-stun",
268 "-c", 268 "-c",
269 "test_stun.conf", 269 "test_stun.conf",
270 NULL 270 NULL
271 }; 271 };
272 char *fn; 272 char *fn;
273 struct GNUNET_OS_Process *proc; 273 struct GNUNET_OS_Process *proc;
274 274
275 GNUNET_log_setup ("test-stun", 275 GNUNET_log_setup("test-stun",
276 "WARNING", 276 "WARNING",
277 NULL); 277 NULL);
278 278
279 /* Lets start resolver */ 279 /* Lets start resolver */
280 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver"); 280 fn = GNUNET_OS_get_libexec_binary_path("gnunet-service-resolver");
281 proc = GNUNET_OS_start_process (GNUNET_YES, 281 proc = GNUNET_OS_start_process(GNUNET_YES,
282 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 282 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
283 NULL, NULL, NULL, 283 NULL, NULL, NULL,
284 fn, 284 fn,
285 "gnunet-service-resolver", 285 "gnunet-service-resolver",
286 "-c", "test_stun.conf", NULL); 286 "-c", "test_stun.conf", NULL);
287 287
288 if (NULL == proc) 288 if (NULL == proc)
289 { 289 {
290 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 290 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
291 "This test was unable to start gnunet-service-resolver, and it is required to run ...\n"); 291 "This test was unable to start gnunet-service-resolver, and it is required to run ...\n");
292 exit(1); 292 exit(1);
293 } 293 }
294 294
295 GNUNET_PROGRAM_run (3, argv_prog, 295 GNUNET_PROGRAM_run(3, argv_prog,
296 "test-stun", "nohelp", 296 "test-stun", "nohelp",
297 options, 297 options,
298 &run, NULL); 298 &run, NULL);
299 299
300 /* Now kill the resolver */ 300 /* Now kill the resolver */
301 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) 301 if (0 != GNUNET_OS_process_kill(proc, GNUNET_TERM_SIG))
302 { 302 {
303 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 303 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
304 } 304 }
305 GNUNET_OS_process_wait (proc); 305 GNUNET_OS_process_wait(proc);
306 GNUNET_OS_process_destroy (proc); 306 GNUNET_OS_process_destroy(proc);
307 proc = NULL; 307 proc = NULL;
308 GNUNET_free (fn); 308 GNUNET_free(fn);
309 309
310 return ret; 310 return ret;
311} 311}