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/nat/gnunet-helper-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, unsigned int bytes)
220 for (i = 0; i < bytes / 2; i++)
222 sum = (sum & 0xffff) + (sum >> 16);
223 sum = htons (0xffff - sum);
229 * Convert IPv4 address from text to binary form.
231 * @param af address family
232 * @param cp the address to print
233 * @param buf where to write the address result
234 * @return 1 on success
237 inet_pton (int af, const char *cp, struct in_addr *buf)
239 buf->s_addr = inet_addr (cp);
240 if (buf->s_addr == INADDR_NONE)
242 fprintf (stderr, "Error %d handling address %s", WSAGetLastError (), cp);
250 * Send an ICMP message to the dummy IP.
252 * @param my_ip source address (our ip address)
255 send_icmp_echo (const struct in_addr *my_ip)
257 char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)];
258 struct icmp_echo_header icmp_echo;
259 struct ip_header ip_pkt;
260 struct sockaddr_in dst;
265 ip_pkt.vers_ihl = 0x45;
267 ip_pkt.pkt_len = htons (sizeof (packet));
268 ip_pkt.id = htons (256);
269 ip_pkt.flags_frag_offset = 0;
270 ip_pkt.ttl = IPDEFTTL;
271 ip_pkt.proto = IPPROTO_ICMP;
273 ip_pkt.src_ip = my_ip->s_addr;
274 ip_pkt.dst_ip = dummy.s_addr;
276 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
277 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
278 off += sizeof (struct ip_header);
280 icmp_echo.type = ICMP_ECHO;
282 icmp_echo.reserved = 0;
283 icmp_echo.checksum = 0;
286 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
287 memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
288 off += sizeof (struct icmp_echo_header);
290 memset (&dst, 0, sizeof (dst));
291 dst.sin_family = AF_INET;
292 dst.sin_addr = dummy;
294 sendto (rawsock, packet, off, 0, (struct sockaddr *) &dst, sizeof (dst));
298 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
303 fprintf (stderr, "Error: partial send of ICMP message\n");
309 * Send a UDP message to the dummy IP.
314 struct sockaddr_in dst;
317 memset (&dst, 0, sizeof (dst));
318 dst.sin_family = AF_INET;
319 dst.sin_addr = dummy;
320 dst.sin_port = htons (NAT_TRAV_PORT);
321 err = sendto (udpsock, NULL, 0, 0, (struct sockaddr *) &dst, sizeof (dst));
325 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
330 fprintf (stderr, "Error: partial send of ICMP message\n");
336 * We've received an ICMP response. Process it.
339 process_icmp_response ()
343 struct in_addr source_ip;
344 struct ip_header ip_pkt;
345 struct icmp_ttl_exceeded_header icmp_ttl;
346 struct icmp_echo_header icmp_echo;
347 struct udp_header udp_pkt;
352 have = read (icmpsock, buf, sizeof (buf));
355 fprintf (stderr, "Error reading raw socket: %s\n", strerror (errno));
359 fprintf (stderr, "Received message of %u bytes\n", (unsigned int) have);
362 (ssize_t) (sizeof (struct ip_header) +
363 sizeof (struct icmp_ttl_exceeded_header) +
364 sizeof (struct ip_header)))
370 memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header));
371 off += sizeof (struct ip_header);
372 memcpy (&source_ip, &ip_pkt.src_ip, sizeof (source_ip));
373 memcpy (&icmp_ttl, &buf[off], sizeof (struct icmp_ttl_exceeded_header));
374 off += sizeof (struct icmp_ttl_exceeded_header);
375 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code))
377 /* different type than what we want */
380 /* skip 2nd IP header */
381 memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header));
382 off += sizeof (struct ip_header);
384 switch (ip_pkt.proto)
388 (sizeof (struct ip_header) * 2 +
389 sizeof (struct icmp_ttl_exceeded_header) +
390 sizeof (struct icmp_echo_header)))
395 /* grab ICMP ECHO content */
396 memcpy (&icmp_echo, &buf[off], sizeof (struct icmp_echo_header));
397 port = (uint16_t) ntohl (icmp_echo.reserved);
401 (sizeof (struct ip_header) * 2 +
402 sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct udp_header)))
407 /* grab UDP content */
408 memcpy (&udp_pkt, &buf[off], sizeof (struct udp_header));
409 port = ntohs (udp_pkt.length);
412 /* different type than what we want */
416 ssize = sizeof (buf);
417 WSAAddressToString ((LPSOCKADDR) & source_ip, sizeof (source_ip), NULL, buf,
420 fprintf (stdout, "%s\n", buf);
422 fprintf (stdout, "%s:%u\n", buf, (unsigned int) port);
428 * Create an ICMP raw socket for reading.
430 * @return INVALID_SOCKET on error
437 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
438 if (INVALID_SOCKET == ret)
440 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
441 return INVALID_SOCKET;
448 * Create an ICMP raw socket for writing.
450 * @return INVALID_SOCKET on error
455 DWORD bOptVal = TRUE;
456 int bOptLen = sizeof (bOptVal);
458 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
459 if (INVALID_SOCKET == rawsock)
461 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
462 return INVALID_SOCKET;
466 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal,
469 fprintf (stderr, "Error setting SO_BROADCAST to ON: %s\n",
471 closesocket (rawsock);
472 return INVALID_SOCKET;
475 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &bOptVal, bOptLen))
477 fprintf (stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror (errno));
478 closesocket (rawsock);
479 return INVALID_SOCKET;
486 * Create a UDP socket for writing.
488 * @param my_ip source address (our ip address)
489 * @return INVALID_SOCKET on error
492 make_udp_socket (const struct in_addr *my_ip)
495 struct sockaddr_in addr;
497 ret = socket (AF_INET, SOCK_DGRAM, 0);
498 if (INVALID_SOCKET == ret)
500 fprintf (stderr, "Error opening UDP socket: %s\n", strerror (errno));
501 return INVALID_SOCKET;
503 memset (&addr, 0, sizeof (addr));
504 addr.sin_family = AF_INET;
505 addr.sin_addr = *my_ip;
506 addr.sin_port = htons (NAT_TRAV_PORT);
507 if (0 != bind (ret, (struct sockaddr *) &addr, sizeof (addr)))
509 fprintf (stderr, "Error binding UDP socket to port %u: %s\n", NAT_TRAV_PORT,
511 /* likely problematic, but not certain, try to continue */
518 main (int argc, char *const *argv)
520 struct in_addr external;
530 "This program must be started with our (internal NAT) IP as the only argument.\n");
533 if (1 != inet_pton (AF_INET, argv[1], &external))
535 fprintf (stderr, "Error parsing IPv4 address: %s, error %s\n", argv[1],
539 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
541 fprintf (stderr, "Internal error converting dummy IP to binary.\n");
544 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
546 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
549 if (INVALID_SOCKET == (icmpsock = make_icmp_socket ()))
553 if (INVALID_SOCKET == (make_raw_socket ()))
555 closesocket (icmpsock);
558 if (INVALID_SOCKET == (udpsock = make_udp_socket (&external)))
560 closesocket (icmpsock);
561 closesocket (rawsock);
567 FD_SET (icmpsock, &rs);
569 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
570 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
574 fprintf (stderr, "select failed: %s\n", strerror (errno));
577 if (FD_ISSET (icmpsock, &rs))
578 process_icmp_response ();
579 if (0 == (++alt % 2))
580 send_icmp_echo (&external);
584 /* select failed (internal error or OS out of resources) */
585 closesocket (icmpsock);
586 closesocket (rawsock);
587 closesocket (udpsock);
593 /* end of gnunet-helper-nat-server-windows.c */