diff options
Diffstat (limited to 'src/nat/gnunet-helper-nat-client.c')
-rw-r--r-- | src/nat/gnunet-helper-nat-client.c | 72 |
1 files changed, 50 insertions, 22 deletions
diff --git a/src/nat/gnunet-helper-nat-client.c b/src/nat/gnunet-helper-nat-client.c index d9129afb0..d4fc54b24 100644 --- a/src/nat/gnunet-helper-nat-client.c +++ b/src/nat/gnunet-helper-nat-client.c | |||
@@ -2,20 +2,18 @@ | |||
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2010 GNUnet e.V. | 3 | Copyright (C) 2010 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | it under the terms of the GNU 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, or (at your | 7 | by the Free Software Foundation, either version 3 of the License, |
8 | 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 | 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 General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with GNUnet; see the file COPYING. If not, write to the | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | 17 | */ |
20 | 18 | ||
21 | /** | 19 | /** |
@@ -67,6 +65,17 @@ | |||
67 | #endif | 65 | #endif |
68 | 66 | ||
69 | /** | 67 | /** |
68 | * Call memcpy() but check for @a n being 0 first. In the latter | ||
69 | * case, it is now safe to pass NULL for @a src or @a dst. | ||
70 | * Unlike traditional memcpy(), returns nothing. | ||
71 | * | ||
72 | * @param dst destination of the copy, may be NULL if @a n is zero | ||
73 | * @param src source of the copy, may be NULL if @a n is zero | ||
74 | * @param n number of bytes to copy | ||
75 | */ | ||
76 | #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0) | ||
77 | |||
78 | /** | ||
70 | * Must match IP given in the server. | 79 | * Must match IP given in the server. |
71 | */ | 80 | */ |
72 | #define DUMMY_IP "192.0.2.86" | 81 | #define DUMMY_IP "192.0.2.86" |
@@ -251,14 +260,18 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) | |||
251 | ip_pkt.dst_ip = other->s_addr; | 260 | ip_pkt.dst_ip = other->s_addr; |
252 | ip_pkt.checksum = | 261 | ip_pkt.checksum = |
253 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); | 262 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); |
254 | memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); | 263 | GNUNET_memcpy (&packet[off], |
264 | &ip_pkt, | ||
265 | sizeof (struct ip_header)); | ||
255 | off += sizeof (struct ip_header); | 266 | off += sizeof (struct ip_header); |
256 | 267 | ||
257 | icmp_pkt.type = ICMP_TIME_EXCEEDED; | 268 | icmp_pkt.type = ICMP_TIME_EXCEEDED; |
258 | icmp_pkt.code = 0; | 269 | icmp_pkt.code = 0; |
259 | icmp_pkt.checksum = 0; | 270 | icmp_pkt.checksum = 0; |
260 | icmp_pkt.unused = 0; | 271 | icmp_pkt.unused = 0; |
261 | memcpy (&packet[off], &icmp_pkt, sizeof (struct icmp_ttl_exceeded_header)); | 272 | GNUNET_memcpy (&packet[off], |
273 | &icmp_pkt, | ||
274 | sizeof (struct icmp_ttl_exceeded_header)); | ||
262 | off += sizeof (struct icmp_ttl_exceeded_header); | 275 | off += sizeof (struct icmp_ttl_exceeded_header); |
263 | 276 | ||
264 | /* ip header of the presumably 'lost' udp packet */ | 277 | /* ip header of the presumably 'lost' udp packet */ |
@@ -275,7 +288,9 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) | |||
275 | ip_pkt.dst_ip = dummy.s_addr; | 288 | ip_pkt.dst_ip = dummy.s_addr; |
276 | ip_pkt.checksum = | 289 | ip_pkt.checksum = |
277 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); | 290 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); |
278 | memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); | 291 | GNUNET_memcpy (&packet[off], |
292 | &ip_pkt, | ||
293 | sizeof (struct ip_header)); | ||
279 | off += sizeof (struct ip_header); | 294 | off += sizeof (struct ip_header); |
280 | 295 | ||
281 | /* build UDP header */ | 296 | /* build UDP header */ |
@@ -283,7 +298,9 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) | |||
283 | udp_pkt.dst_port = htons (NAT_TRAV_PORT); | 298 | udp_pkt.dst_port = htons (NAT_TRAV_PORT); |
284 | udp_pkt.length = htons (port); | 299 | udp_pkt.length = htons (port); |
285 | udp_pkt.crc = 0; | 300 | udp_pkt.crc = 0; |
286 | memcpy (&packet[off], &udp_pkt, sizeof (struct udp_header)); | 301 | GNUNET_memcpy (&packet[off], |
302 | &udp_pkt, | ||
303 | sizeof (struct udp_header)); | ||
287 | off += sizeof (struct udp_header); | 304 | off += sizeof (struct udp_header); |
288 | 305 | ||
289 | /* set ICMP checksum */ | 306 | /* set ICMP checksum */ |
@@ -292,8 +309,9 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) | |||
292 | ((uint16_t *) & packet[sizeof (struct ip_header)], | 309 | ((uint16_t *) & packet[sizeof (struct ip_header)], |
293 | sizeof (struct icmp_ttl_exceeded_header) + | 310 | sizeof (struct icmp_ttl_exceeded_header) + |
294 | sizeof (struct ip_header) + sizeof (struct udp_header))); | 311 | sizeof (struct ip_header) + sizeof (struct udp_header))); |
295 | memcpy (&packet[sizeof (struct ip_header)], &icmp_pkt, | 312 | GNUNET_memcpy (&packet[sizeof (struct ip_header)], |
296 | sizeof (struct icmp_ttl_exceeded_header)); | 313 | &icmp_pkt, |
314 | sizeof (struct icmp_ttl_exceeded_header)); | ||
297 | 315 | ||
298 | memset (&dst, 0, sizeof (dst)); | 316 | memset (&dst, 0, sizeof (dst)); |
299 | dst.sin_family = AF_INET; | 317 | dst.sin_family = AF_INET; |
@@ -352,7 +370,9 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | |||
352 | ip_pkt.dst_ip = other->s_addr; | 370 | ip_pkt.dst_ip = other->s_addr; |
353 | ip_pkt.checksum = | 371 | ip_pkt.checksum = |
354 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); | 372 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); |
355 | memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); | 373 | GNUNET_memcpy (&packet[off], |
374 | &ip_pkt, | ||
375 | sizeof (struct ip_header)); | ||
356 | off = sizeof (ip_pkt); | 376 | off = sizeof (ip_pkt); |
357 | 377 | ||
358 | /* icmp reply: time exceeded */ | 378 | /* icmp reply: time exceeded */ |
@@ -360,7 +380,9 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | |||
360 | icmp_ttl.code = 0; | 380 | icmp_ttl.code = 0; |
361 | icmp_ttl.checksum = 0; | 381 | icmp_ttl.checksum = 0; |
362 | icmp_ttl.unused = 0; | 382 | icmp_ttl.unused = 0; |
363 | memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header)); | 383 | GNUNET_memcpy (&packet[off], |
384 | &icmp_ttl, | ||
385 | sizeof (struct icmp_ttl_exceeded_header)); | ||
364 | off += sizeof (struct icmp_ttl_exceeded_header); | 386 | off += sizeof (struct icmp_ttl_exceeded_header); |
365 | 387 | ||
366 | /* ip header of the presumably 'lost' udp packet */ | 388 | /* ip header of the presumably 'lost' udp packet */ |
@@ -377,7 +399,9 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | |||
377 | ip_pkt.checksum = 0; | 399 | ip_pkt.checksum = 0; |
378 | ip_pkt.checksum = | 400 | ip_pkt.checksum = |
379 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); | 401 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); |
380 | memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); | 402 | GNUNET_memcpy (&packet[off], |
403 | &ip_pkt, | ||
404 | sizeof (struct ip_header)); | ||
381 | off += sizeof (struct ip_header); | 405 | off += sizeof (struct ip_header); |
382 | 406 | ||
383 | icmp_echo.type = ICMP_ECHO; | 407 | icmp_echo.type = ICMP_ECHO; |
@@ -386,8 +410,10 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | |||
386 | icmp_echo.checksum = 0; | 410 | icmp_echo.checksum = 0; |
387 | icmp_echo.checksum = | 411 | icmp_echo.checksum = |
388 | htons (calc_checksum | 412 | htons (calc_checksum |
389 | ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header))); | 413 | ((uint16_t *) &icmp_echo, sizeof (struct icmp_echo_header))); |
390 | memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header)); | 414 | GNUNET_memcpy (&packet[off], |
415 | &icmp_echo, | ||
416 | sizeof (struct icmp_echo_header)); | ||
391 | 417 | ||
392 | /* no go back to calculate ICMP packet checksum */ | 418 | /* no go back to calculate ICMP packet checksum */ |
393 | off = sizeof (struct ip_header); | 419 | off = sizeof (struct ip_header); |
@@ -396,7 +422,9 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | |||
396 | ((uint16_t *) & packet[off], | 422 | ((uint16_t *) & packet[off], |
397 | sizeof (struct icmp_ttl_exceeded_header) + | 423 | sizeof (struct icmp_ttl_exceeded_header) + |
398 | sizeof (struct ip_header) + sizeof (struct icmp_echo_header))); | 424 | sizeof (struct ip_header) + sizeof (struct icmp_echo_header))); |
399 | memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header)); | 425 | GNUNET_memcpy (&packet[off], |
426 | &icmp_ttl, | ||
427 | sizeof (struct icmp_ttl_exceeded_header)); | ||
400 | 428 | ||
401 | /* prepare for transmission */ | 429 | /* prepare for transmission */ |
402 | memset (&dst, 0, sizeof (dst)); | 430 | memset (&dst, 0, sizeof (dst)); |