2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file src/transport/gnunet-nat-server-windows.c
23 * @brief Windows tool to help bypass NATs using ICMP method
24 * This code will work under W32 only
25 * @author Christian Grothoff
27 * This program will send ONE ICMP message every 500 ms RAW sockets
28 * to a DUMMY IP address and also listens for ICMP replies. Since
29 * it uses RAW sockets, it must be run as an administrative user.
30 * In order to keep the security risk of the resulting binary
31 * minimal, the program ONLY opens the two RAW sockets with administrative
32 * privileges, then drops them and only then starts to process
33 * command line arguments. The code also does not link against
34 * any shared libraries (except libc) and is strictly minimal
35 * (except for checking for errors). The following list of people
36 * have reviewed this code and considered it safe since the last
37 * modification (if you reviewed it, please have your name added
41 * - Christian Grothoff
49 #include <sys/types.h>
59 * Should we print some debug output?
64 * Must match IP given in the client.
66 #define DUMMY_IP "192.0.2.86"
71 #define NAT_TRAV_PORT 22225
74 * TTL to use for our outgoing messages.
80 #define ICMP_TIME_EXCEEDED 11
83 * How often do we send our ICMP messages to receive replies?
85 #define ICMP_SEND_FREQUENCY_MS 500
94 * Version (4 bits) + Internet header length (4 bits)
114 * Flags (3 bits) + Fragment offset (13 bits)
116 uint16_t flags_frag_offset;
139 * Destination address
145 * Format of ICMP packet.
147 struct icmp_ttl_exceeded_header
157 /* followed by original payload */
160 struct icmp_echo_header
172 * Beginning of UDP packet.
186 * Socket we use to receive "fake" ICMP replies.
188 static SOCKET icmpsock;
191 * Socket we use to send our ICMP requests.
193 static SOCKET rawsock;
196 * Socket we use to send our UDP requests.
198 static SOCKET udpsock;
201 * Target "dummy" address.
203 static struct in_addr dummy;
207 * CRC-16 for IP/ICMP headers.
209 * @param data what to calculate the CRC over
210 * @param bytes number of bytes in data (must be multiple of 2)
211 * @return the CRC 16.
214 calc_checksum(const uint16_t *data,
221 for (i=0;i<bytes/2;i++)
223 sum = (sum & 0xffff) + (sum >> 16);
224 sum = htons(0xffff - sum);
230 * Convert IPv4 address from text to binary form.
232 * @param af address family
233 * @param cp the address to print
234 * @param buf where to write the address result
235 * @return 1 on success
242 buf->s_addr = inet_addr(cp);
243 if (buf->s_addr == INADDR_NONE)
246 "Error %d handling address %s",
256 * Send an ICMP message to the dummy IP.
258 * @param my_ip source address (our ip address)
261 send_icmp_echo (const struct in_addr *my_ip)
263 char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)];
264 struct icmp_echo_header icmp_echo;
265 struct ip_header ip_pkt;
266 struct sockaddr_in dst;
271 ip_pkt.vers_ihl = 0x45;
273 ip_pkt.pkt_len = htons (sizeof (packet));
274 ip_pkt.id = htons (256);
275 ip_pkt.flags_frag_offset = 0;
276 ip_pkt.ttl = IPDEFTTL;
277 ip_pkt.proto = IPPROTO_ICMP;
279 ip_pkt.src_ip = my_ip->s_addr;
280 ip_pkt.dst_ip = dummy.s_addr;
281 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
282 sizeof (struct ip_header)));
283 memcpy (&packet[off],
285 sizeof (struct ip_header));
286 off += sizeof (struct ip_header);
288 icmp_echo.type = ICMP_ECHO;
290 icmp_echo.reserved = 0;
291 icmp_echo.checksum = 0;
292 icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
293 sizeof (struct icmp_echo_header)));
294 memcpy (&packet[off],
296 sizeof (struct icmp_echo_header));
297 off += sizeof (struct icmp_echo_header);
299 memset (&dst, 0, sizeof (dst));
300 dst.sin_family = AF_INET;
301 dst.sin_addr = dummy;
302 err = sendto(rawsock,
304 (struct sockaddr*)&dst,
310 "sendto failed: %s\n", strerror(errno));
316 "Error: partial send of ICMP message\n");
322 * Send a UDP message to the dummy IP.
327 struct sockaddr_in dst;
330 memset (&dst, 0, sizeof (dst));
331 dst.sin_family = AF_INET;
332 dst.sin_addr = dummy;
333 dst.sin_port = htons (NAT_TRAV_PORT);
334 err = sendto(udpsock,
336 (struct sockaddr*)&dst,
342 "sendto failed: %s\n", strerror(errno));
348 "Error: partial send of ICMP message\n");
354 * We've received an ICMP response. Process it.
357 process_icmp_response ()
361 struct in_addr source_ip;
362 struct ip_header ip_pkt;
363 struct icmp_ttl_exceeded_header icmp_ttl;
364 struct icmp_echo_header icmp_echo;
365 struct udp_header udp_pkt;
370 have = read (icmpsock, buf, sizeof (buf));
374 "Error reading raw socket: %s\n",
380 "Received message of %u bytes\n",
381 (unsigned int) have);
383 if (have < (ssize_t) (sizeof (struct ip_header) + sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct ip_header)))
391 sizeof (struct ip_header));
392 off += sizeof (struct ip_header);
398 sizeof (struct icmp_ttl_exceeded_header));
399 off += sizeof (struct icmp_ttl_exceeded_header);
400 if ( (ICMP_TIME_EXCEEDED != icmp_ttl.type) ||
401 (0 != icmp_ttl.code) )
403 /* different type than what we want */
406 /* skip 2nd IP header */
409 sizeof (struct ip_header));
410 off += sizeof (struct ip_header);
412 switch (ip_pkt.proto)
415 if (have != (sizeof (struct ip_header) * 2 +
416 sizeof (struct icmp_ttl_exceeded_header) +
417 sizeof (struct icmp_echo_header)) )
422 /* grab ICMP ECHO content */
425 sizeof (struct icmp_echo_header));
426 port = (uint16_t) ntohl (icmp_echo.reserved);
429 if (have != (sizeof (struct ip_header) * 2 +
430 sizeof (struct icmp_ttl_exceeded_header) +
431 sizeof (struct udp_header)) )
436 /* grab UDP content */
439 sizeof (struct udp_header));
440 port = ntohs (udp_pkt.length);
443 /* different type than what we want */
448 WSAAddressToString((LPSOCKADDR)&source_ip,
461 (unsigned int) port);
467 * Create an ICMP raw socket for reading.
469 * @return INVALID_SOCKET on error
476 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
477 if (INVALID_SOCKET == ret)
480 "Error opening RAW socket: %s\n",
482 return INVALID_SOCKET;
489 * Create an ICMP raw socket for writing.
491 * @return INVALID_SOCKET on error
496 DWORD bOptVal = TRUE;
497 int bOptLen = sizeof(bOptVal);
499 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
500 if (INVALID_SOCKET == rawsock)
503 "Error opening RAW socket: %s\n",
505 return INVALID_SOCKET;
508 if (0 != setsockopt(rawsock,
511 (char*)&bOptVal, bOptLen))
514 "Error setting SO_BROADCAST to ON: %s\n",
516 closesocket(rawsock);
517 return INVALID_SOCKET;
519 if (0 != setsockopt(rawsock,
522 (char*)&bOptVal, bOptLen))
525 "Error setting IP_HDRINCL to ON: %s\n",
527 closesocket(rawsock);
528 return INVALID_SOCKET;
535 * Create a UDP socket for writing.
537 * @param my_ip source address (our ip address)
538 * @return INVALID_SOCKET on error
541 make_udp_socket (const struct in_addr *my_ip)
544 struct sockaddr_in addr;
546 ret = socket (AF_INET, SOCK_DGRAM, 0);
547 if (INVALID_SOCKET == ret)
550 "Error opening UDP socket: %s\n",
552 return INVALID_SOCKET;
554 memset (&addr, 0, sizeof (addr));
555 addr.sin_family = AF_INET;
556 addr.sin_addr = *my_ip;
557 addr.sin_port = htons (NAT_TRAV_PORT);
559 (struct sockaddr *)&addr,
563 "Error binding UDP socket to port %u: %s\n",
566 /* likely problematic, but not certain, try to continue */
576 struct in_addr external;
586 "This program must be started with our (internal NAT) IP as the only argument.\n");
589 if (1 != inet_pton (AF_INET, argv[1], &external))
592 "Error parsing IPv4 address: %s, error %s\n",
593 argv[1], strerror (errno));
596 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
599 "Internal error converting dummy IP to binary.\n");
602 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
604 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
607 if (INVALID_SOCKET == (icmpsock = make_icmp_socket()))
611 if (INVALID_SOCKET == (make_raw_socket()))
613 closesocket (icmpsock);
616 if (INVALID_SOCKET == (udpsock = make_udp_socket(&external)))
618 closesocket (icmpsock);
619 closesocket (rawsock);
625 FD_SET (icmpsock, &rs);
627 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
628 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
633 "select failed: %s\n",
637 if (FD_ISSET (icmpsock, &rs))
638 process_icmp_response ();
639 if (0 == (++alt % 2))
640 send_icmp_echo (&external);
644 /* select failed (internal error or OS out of resources) */
645 closesocket(icmpsock);
646 closesocket(rawsock);
647 closesocket(udpsock);
653 /* end of gnunet-nat-server-windows.c */