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
48 #include <sys/types.h>
58 * Should we print some debug output?
63 * Must match IP given in the client.
65 #define DUMMY_IP "192.0.2.86"
68 * TTL to use for our outgoing messages.
74 #define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
77 * How often do we send our ICMP messages to receive replies?
79 #define ICMP_SEND_FREQUENCY_MS 500
88 * Version (4 bits) + Internet header length (4 bits)
108 * Flags (3 bits) + Fragment offset (13 bits)
110 uint16_t flags_frag_offset;
133 * Destination address
139 * Format of ICMP packet.
153 * Beginning of UDP packet.
165 * Socket we use to receive "fake" ICMP replies.
167 static SOCKET icmpsock;
170 * Socket we use to send our ICMP requests.
172 static SOCKET rawsock;
175 * Target "dummy" address.
177 static struct in_addr dummy;
181 * CRC-16 for IP/ICMP headers.
183 * @param data what to calculate the CRC over
184 * @param bytes number of bytes in data (must be multiple of 2)
185 * @return the CRC 16.
188 calc_checksum(const uint16_t *data,
195 for (i=0;i<bytes/2;i++)
197 sum = (sum & 0xffff) + (sum >> 16);
198 sum = htons(0xffff - sum);
204 * Convert IPv4 address from text to binary form.
206 * @param af address family
207 * @param cp the address to print
208 * @param buf where to write the address result
209 * @return 1 on success
216 buf->s_addr = inet_addr(cp);
217 if (buf->s_addr == INADDR_NONE)
220 "Error %d handling address %s",
230 * Send an ICMP message to the dummy IP.
232 * @param my_ip source address (our ip address)
235 send_icmp_echo (const struct in_addr *my_ip)
237 struct icmp_packet icmp_echo;
238 struct sockaddr_in dst;
241 struct ip_packet ip_pkt;
242 struct icmp_packet icmp_pkt;
243 char packet[sizeof (ip_pkt) + sizeof (icmp_pkt)];
246 memset(&ip_pkt, 0, sizeof(ip_pkt));
247 ip_pkt.vers_ihl = 0x45;
249 ip_pkt.pkt_len = sizeof (packet);
250 ip_pkt.id = htons (256);
251 ip_pkt.flags_frag_offset = 0;
252 ip_pkt.ttl = IPDEFTTL;
253 ip_pkt.proto = IPPROTO_ICMP;
255 ip_pkt.src_ip = my_ip->s_addr;
256 ip_pkt.dst_ip = dummy.s_addr;
257 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
258 memcpy (packet, &ip_pkt, sizeof (ip_pkt));
259 off += sizeof (ip_pkt);
261 icmp_echo.type = ICMP_ECHO;
263 icmp_echo.reserved = 0;
264 icmp_echo.checksum = 0;
265 icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
266 sizeof (struct icmp_packet)));
267 memcpy (&packet[off], &icmp_echo, sizeof (icmp_echo));
268 off += sizeof (icmp_echo);
270 memset (&dst, 0, sizeof (dst));
271 dst.sin_family = AF_INET;
272 dst.sin_addr = dummy;
273 err = sendto(rawsock,
275 (struct sockaddr*)&dst,
281 "sendto failed: %s\n", strerror(errno));
287 "Error: partial send of ICMP message\n");
293 * We've received an ICMP response. Process it.
296 process_icmp_response ()
301 struct ip_packet ip_pkt;
302 struct icmp_packet icmp_pkt;
303 struct udp_packet udp_pkt;
308 have = read (icmpsock, buf, sizeof (buf));
312 "Error reading raw socket: %s\n",
319 "Received message of %u bytes\n",
320 (unsigned int) have);
322 if (have == sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2 + sizeof(uint32_t))
326 else if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2)
330 "Received ICMP message of unexpected size: %u bytes\n",
331 (unsigned int) have);
336 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
337 off += sizeof (ip_pkt);
338 memcpy (&icmp_pkt, &buf[off], sizeof (icmp_pkt));
339 off += sizeof (icmp_pkt);
340 if ( ((ip_pkt.proto != IPPROTO_ICMP) && (ip_pkt.proto != IPPROTO_UDP)) ||
341 (icmp_pkt.type != ICMP_TIME_EXCEEDED) ||
342 (icmp_pkt.code != 0) )
344 /* maybe we got an actual reply back... */
350 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
351 off += sizeof (ip_pkt);
356 &buf[sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2],
359 DWORD ssize = sizeof(buf);
360 WSAAddressToString((LPSOCKADDR)&sip,
370 else if (ip_pkt.proto == IPPROTO_UDP)
375 DWORD ssize = sizeof(buf);
376 WSAAddressToString((LPSOCKADDR)&sip,
384 ntohs((uint16_t)udp_pkt.length));
388 DWORD ssize = sizeof(buf);
389 WSAAddressToString((LPSOCKADDR)&sip,
403 * Create an ICMP raw socket for reading.
405 * @return INVALID_SOCKET on error
412 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
413 if (INVALID_SOCKET == ret)
416 "Error opening RAW socket: %s\n",
418 return INVALID_SOCKET;
425 * Create an ICMP raw socket for writing.
427 * @return INVALID_SOCKET on error
432 DWORD bOptVal = TRUE;
433 int bOptLen = sizeof(bOptVal);
435 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
436 if (INVALID_SOCKET == rawsock)
439 "Error opening RAW socket: %s\n",
441 return INVALID_SOCKET;
444 if (setsockopt(rawsock,
447 (char*)&bOptVal, bOptLen) != 0)
450 "Error setting SO_BROADCAST to ON: %s\n",
452 closesocket(rawsock);
453 return INVALID_SOCKET;
455 if (setsockopt(rawsock,
458 (char*)&bOptVal, bOptLen) != 0)
461 "Error setting IP_HDRINCL to ON: %s\n",
463 closesocket(rawsock);
464 return INVALID_SOCKET;
474 struct in_addr external;
482 "This program must be started with our (internal NAT) IP as the only argument.\n");
485 if (1 != inet_pton (AF_INET, argv[1], &external))
488 "Error parsing IPv4 address: %s, error %s\n",
489 argv[1], strerror (errno));
492 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
495 "Internal error converting dummy IP to binary.\n");
498 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
500 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
503 if (INVALID_SOCKET == (icmpsock = make_icmp_socket()))
507 if (INVALID_SOCKET == (make_raw_socket()))
509 closesocket (icmpsock);
515 FD_SET (icmpsock, &rs);
517 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
518 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
523 "select failed: %s\n",
527 if (FD_ISSET (icmpsock, &rs))
528 process_icmp_response ();
529 send_icmp_echo (&external);
531 /* select failed (internal error or OS out of resources) */
532 closesocket(icmpsock);
533 closesocket(rawsock);
539 /* end of gnunet-nat-server-windows.c */