diff options
Diffstat (limited to 'src/nat/gnunet-helper-nat-client.c')
-rw-r--r-- | src/nat/gnunet-helper-nat-client.c | 54 |
1 files changed, 42 insertions, 12 deletions
diff --git a/src/nat/gnunet-helper-nat-client.c b/src/nat/gnunet-helper-nat-client.c index d9129afb0..359e529b0 100644 --- a/src/nat/gnunet-helper-nat-client.c +++ b/src/nat/gnunet-helper-nat-client.c | |||
@@ -67,6 +67,17 @@ | |||
67 | #endif | 67 | #endif |
68 | 68 | ||
69 | /** | 69 | /** |
70 | * Call memcpy() but check for @a n being 0 first. In the latter | ||
71 | * case, it is now safe to pass NULL for @a src or @a dst. | ||
72 | * Unlike traditional memcpy(), returns nothing. | ||
73 | * | ||
74 | * @param dst destination 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 | ||
77 | */ | ||
78 | #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0) | ||
79 | |||
80 | /** | ||
70 | * Must match IP given in the server. | 81 | * Must match IP given in the server. |
71 | */ | 82 | */ |
72 | #define DUMMY_IP "192.0.2.86" | 83 | #define DUMMY_IP "192.0.2.86" |
@@ -251,14 +262,18 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) | |||
251 | ip_pkt.dst_ip = other->s_addr; | 262 | ip_pkt.dst_ip = other->s_addr; |
252 | ip_pkt.checksum = | 263 | ip_pkt.checksum = |
253 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); | 264 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); |
254 | memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); | 265 | GNUNET_memcpy (&packet[off], |
266 | &ip_pkt, | ||
267 | sizeof (struct ip_header)); | ||
255 | off += sizeof (struct ip_header); | 268 | off += sizeof (struct ip_header); |
256 | 269 | ||
257 | icmp_pkt.type = ICMP_TIME_EXCEEDED; | 270 | icmp_pkt.type = ICMP_TIME_EXCEEDED; |
258 | icmp_pkt.code = 0; | 271 | icmp_pkt.code = 0; |
259 | icmp_pkt.checksum = 0; | 272 | icmp_pkt.checksum = 0; |
260 | icmp_pkt.unused = 0; | 273 | icmp_pkt.unused = 0; |
261 | memcpy (&packet[off], &icmp_pkt, sizeof (struct icmp_ttl_exceeded_header)); | 274 | GNUNET_memcpy (&packet[off], |
275 | &icmp_pkt, | ||
276 | sizeof (struct icmp_ttl_exceeded_header)); | ||
262 | off += sizeof (struct icmp_ttl_exceeded_header); | 277 | off += sizeof (struct icmp_ttl_exceeded_header); |
263 | 278 | ||
264 | /* ip header of the presumably 'lost' udp packet */ | 279 | /* ip header of the presumably 'lost' udp packet */ |
@@ -275,7 +290,9 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) | |||
275 | ip_pkt.dst_ip = dummy.s_addr; | 290 | ip_pkt.dst_ip = dummy.s_addr; |
276 | ip_pkt.checksum = | 291 | ip_pkt.checksum = |
277 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); | 292 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); |
278 | memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); | 293 | GNUNET_memcpy (&packet[off], |
294 | &ip_pkt, | ||
295 | sizeof (struct ip_header)); | ||
279 | off += sizeof (struct ip_header); | 296 | off += sizeof (struct ip_header); |
280 | 297 | ||
281 | /* build UDP header */ | 298 | /* build UDP header */ |
@@ -283,7 +300,9 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) | |||
283 | udp_pkt.dst_port = htons (NAT_TRAV_PORT); | 300 | udp_pkt.dst_port = htons (NAT_TRAV_PORT); |
284 | udp_pkt.length = htons (port); | 301 | udp_pkt.length = htons (port); |
285 | udp_pkt.crc = 0; | 302 | udp_pkt.crc = 0; |
286 | memcpy (&packet[off], &udp_pkt, sizeof (struct udp_header)); | 303 | GNUNET_memcpy (&packet[off], |
304 | &udp_pkt, | ||
305 | sizeof (struct udp_header)); | ||
287 | off += sizeof (struct udp_header); | 306 | off += sizeof (struct udp_header); |
288 | 307 | ||
289 | /* set ICMP checksum */ | 308 | /* set ICMP checksum */ |
@@ -292,8 +311,9 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) | |||
292 | ((uint16_t *) & packet[sizeof (struct ip_header)], | 311 | ((uint16_t *) & packet[sizeof (struct ip_header)], |
293 | sizeof (struct icmp_ttl_exceeded_header) + | 312 | sizeof (struct icmp_ttl_exceeded_header) + |
294 | sizeof (struct ip_header) + sizeof (struct udp_header))); | 313 | sizeof (struct ip_header) + sizeof (struct udp_header))); |
295 | memcpy (&packet[sizeof (struct ip_header)], &icmp_pkt, | 314 | GNUNET_memcpy (&packet[sizeof (struct ip_header)], |
296 | sizeof (struct icmp_ttl_exceeded_header)); | 315 | &icmp_pkt, |
316 | sizeof (struct icmp_ttl_exceeded_header)); | ||
297 | 317 | ||
298 | memset (&dst, 0, sizeof (dst)); | 318 | memset (&dst, 0, sizeof (dst)); |
299 | dst.sin_family = AF_INET; | 319 | dst.sin_family = AF_INET; |
@@ -352,7 +372,9 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | |||
352 | ip_pkt.dst_ip = other->s_addr; | 372 | ip_pkt.dst_ip = other->s_addr; |
353 | ip_pkt.checksum = | 373 | ip_pkt.checksum = |
354 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); | 374 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); |
355 | memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); | 375 | GNUNET_memcpy (&packet[off], |
376 | &ip_pkt, | ||
377 | sizeof (struct ip_header)); | ||
356 | off = sizeof (ip_pkt); | 378 | off = sizeof (ip_pkt); |
357 | 379 | ||
358 | /* icmp reply: time exceeded */ | 380 | /* icmp reply: time exceeded */ |
@@ -360,7 +382,9 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | |||
360 | icmp_ttl.code = 0; | 382 | icmp_ttl.code = 0; |
361 | icmp_ttl.checksum = 0; | 383 | icmp_ttl.checksum = 0; |
362 | icmp_ttl.unused = 0; | 384 | icmp_ttl.unused = 0; |
363 | memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header)); | 385 | GNUNET_memcpy (&packet[off], |
386 | &icmp_ttl, | ||
387 | sizeof (struct icmp_ttl_exceeded_header)); | ||
364 | off += sizeof (struct icmp_ttl_exceeded_header); | 388 | off += sizeof (struct icmp_ttl_exceeded_header); |
365 | 389 | ||
366 | /* ip header of the presumably 'lost' udp packet */ | 390 | /* ip header of the presumably 'lost' udp packet */ |
@@ -377,7 +401,9 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | |||
377 | ip_pkt.checksum = 0; | 401 | ip_pkt.checksum = 0; |
378 | ip_pkt.checksum = | 402 | ip_pkt.checksum = |
379 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); | 403 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); |
380 | memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); | 404 | GNUNET_memcpy (&packet[off], |
405 | &ip_pkt, | ||
406 | sizeof (struct ip_header)); | ||
381 | off += sizeof (struct ip_header); | 407 | off += sizeof (struct ip_header); |
382 | 408 | ||
383 | icmp_echo.type = ICMP_ECHO; | 409 | icmp_echo.type = ICMP_ECHO; |
@@ -386,8 +412,10 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | |||
386 | icmp_echo.checksum = 0; | 412 | icmp_echo.checksum = 0; |
387 | icmp_echo.checksum = | 413 | icmp_echo.checksum = |
388 | htons (calc_checksum | 414 | htons (calc_checksum |
389 | ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header))); | 415 | ((uint16_t *) &icmp_echo, sizeof (struct icmp_echo_header))); |
390 | memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header)); | 416 | GNUNET_memcpy (&packet[off], |
417 | &icmp_echo, | ||
418 | sizeof (struct icmp_echo_header)); | ||
391 | 419 | ||
392 | /* no go back to calculate ICMP packet checksum */ | 420 | /* no go back to calculate ICMP packet checksum */ |
393 | off = sizeof (struct ip_header); | 421 | off = sizeof (struct ip_header); |
@@ -396,7 +424,9 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | |||
396 | ((uint16_t *) & packet[off], | 424 | ((uint16_t *) & packet[off], |
397 | sizeof (struct icmp_ttl_exceeded_header) + | 425 | sizeof (struct icmp_ttl_exceeded_header) + |
398 | sizeof (struct ip_header) + sizeof (struct icmp_echo_header))); | 426 | sizeof (struct ip_header) + sizeof (struct icmp_echo_header))); |
399 | memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header)); | 427 | GNUNET_memcpy (&packet[off], |
428 | &icmp_ttl, | ||
429 | sizeof (struct icmp_ttl_exceeded_header)); | ||
400 | 430 | ||
401 | /* prepare for transmission */ | 431 | /* prepare for transmission */ |
402 | memset (&dst, 0, sizeof (dst)); | 432 | memset (&dst, 0, sizeof (dst)); |