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 * Socket we use to send our UDP requests.
177 static SOCKET udpsock;
180 * Target "dummy" address.
182 static struct in_addr dummy;
186 * CRC-16 for IP/ICMP headers.
188 * @param data what to calculate the CRC over
189 * @param bytes number of bytes in data (must be multiple of 2)
190 * @return the CRC 16.
193 calc_checksum(const uint16_t *data,
200 for (i=0;i<bytes/2;i++)
202 sum = (sum & 0xffff) + (sum >> 16);
203 sum = htons(0xffff - sum);
209 * Convert IPv4 address from text to binary form.
211 * @param af address family
212 * @param cp the address to print
213 * @param buf where to write the address result
214 * @return 1 on success
221 buf->s_addr = inet_addr(cp);
222 if (buf->s_addr == INADDR_NONE)
225 "Error %d handling address %s",
235 * Send an ICMP message to the dummy IP.
237 * @param my_ip source address (our ip address)
240 send_icmp_echo (const struct in_addr *my_ip)
242 struct icmp_packet icmp_echo;
243 struct sockaddr_in dst;
246 struct ip_packet ip_pkt;
247 struct icmp_packet icmp_pkt;
248 char packet[sizeof (ip_pkt) + sizeof (icmp_pkt)];
251 memset(&ip_pkt, 0, sizeof(ip_pkt));
252 ip_pkt.vers_ihl = 0x45;
254 ip_pkt.pkt_len = sizeof (packet);
255 ip_pkt.id = htons (256);
256 ip_pkt.flags_frag_offset = 0;
257 ip_pkt.ttl = IPDEFTTL;
258 ip_pkt.proto = IPPROTO_ICMP;
260 ip_pkt.src_ip = my_ip->s_addr;
261 ip_pkt.dst_ip = dummy.s_addr;
262 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
263 memcpy (&packet[off], &ip_pkt, sizeof (ip_pkt));
264 off += sizeof (ip_pkt);
266 icmp_echo.type = ICMP_ECHO;
268 icmp_echo.reserved = 0;
269 icmp_echo.checksum = 0;
270 icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
271 sizeof (struct icmp_packet)));
272 memcpy (&packet[off], &icmp_echo, sizeof (icmp_echo));
273 off += sizeof (icmp_echo);
275 memset (&dst, 0, sizeof (dst));
276 dst.sin_family = AF_INET;
277 dst.sin_addr = dummy;
278 err = sendto(rawsock,
280 (struct sockaddr*)&dst,
286 "sendto failed: %s\n", strerror(errno));
292 "Error: partial send of ICMP message\n");
298 * Send a UDP message to the dummy IP.
300 * @param my_ip source address (our ip address)
303 send_udp (const struct in_addr *my_ip)
305 struct sockaddr_in dst;
308 memset (&dst, 0, sizeof (dst));
309 dst.sin_family = AF_INET;
310 dst.sin_addr = dummy;
311 dst.sin_port = htons (NAT_TRAV_PORT);
312 err = sendto(udpsock,
314 (struct sockaddr*)&dst,
320 "sendto failed: %s\n", strerror(errno));
326 "Error: partial send of ICMP message\n");
332 * We've received an ICMP response. Process it.
335 process_icmp_response ()
340 struct ip_packet ip_pkt;
341 struct icmp_packet icmp_pkt;
342 struct udp_packet udp_pkt;
347 have = read (icmpsock, buf, sizeof (buf));
351 "Error reading raw socket: %s\n",
358 "Received message of %u bytes\n",
359 (unsigned int) have);
361 if (have == sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2 + sizeof(uint32_t))
365 else if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2)
369 "Received ICMP message of unexpected size: %u bytes\n",
370 (unsigned int) have);
375 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
376 off += sizeof (ip_pkt);
377 memcpy (&icmp_pkt, &buf[off], sizeof (icmp_pkt));
378 off += sizeof (icmp_pkt);
379 if ( ((ip_pkt.proto != IPPROTO_ICMP) && (ip_pkt.proto != IPPROTO_UDP)) ||
380 (icmp_pkt.type != ICMP_TIME_EXCEEDED) ||
381 (icmp_pkt.code != 0) )
383 /* maybe we got an actual reply back... */
389 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
390 off += sizeof (ip_pkt);
395 &buf[sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2],
398 DWORD ssize = sizeof(buf);
399 WSAAddressToString((LPSOCKADDR)&sip,
409 else if (ip_pkt.proto == IPPROTO_UDP)
414 DWORD ssize = sizeof(buf);
415 WSAAddressToString((LPSOCKADDR)&sip,
423 ntohs((uint16_t)udp_pkt.length));
427 DWORD ssize = sizeof(buf);
428 WSAAddressToString((LPSOCKADDR)&sip,
442 * Create an ICMP raw socket for reading.
444 * @return INVALID_SOCKET on error
451 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
452 if (INVALID_SOCKET == ret)
455 "Error opening RAW socket: %s\n",
457 return INVALID_SOCKET;
464 * Create an ICMP raw socket for writing.
466 * @return INVALID_SOCKET on error
471 DWORD bOptVal = TRUE;
472 int bOptLen = sizeof(bOptVal);
474 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
475 if (INVALID_SOCKET == rawsock)
478 "Error opening RAW socket: %s\n",
480 return INVALID_SOCKET;
483 if (setsockopt(rawsock,
486 (char*)&bOptVal, bOptLen) != 0)
489 "Error setting SO_BROADCAST to ON: %s\n",
491 closesocket(rawsock);
492 return INVALID_SOCKET;
494 if (setsockopt(rawsock,
497 (char*)&bOptVal, bOptLen) != 0)
500 "Error setting IP_HDRINCL to ON: %s\n",
502 closesocket(rawsock);
503 return INVALID_SOCKET;
510 * Create a UDP socket for writinging.
512 * @return -1 on error
518 struct sockaddr_in addr;
520 ret = socket (AF_INET, SOCK_DGRAM, 0);
521 if (INVALID_SOCKET == ret)
524 "Error opening UDP socket: %s\n",
526 return INVALID_SOCKET;
528 memset (&addr, 0, sizeof (addr));
529 addr.sin_family = AF_INET;
530 /* addr.sin_addr zero == ours (hopefully...) */
531 addr.sin_port = htons (NAT_TRAV_PORT);
538 "Error binding UDP socket to port %u: %s\n",
541 /* likely problematic, but not certain, try to continue */
551 struct in_addr external;
560 "This program must be started with our (internal NAT) IP as the only argument.\n");
563 if (1 != inet_pton (AF_INET, argv[1], &external))
566 "Error parsing IPv4 address: %s, error %s\n",
567 argv[1], strerror (errno));
570 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
573 "Internal error converting dummy IP to binary.\n");
576 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
578 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
581 if (INVALID_SOCKET == (icmpsock = make_icmp_socket()))
585 if (INVALID_SOCKET == (make_raw_socket()))
587 closesocket (icmpsock);
590 if (INVALID_SOCKET == (udpsock = make_udp_socket()))
592 closesocket (icmpsock);
593 closesocket (rawsock);
599 FD_SET (icmpsock, &rs);
601 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
602 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
607 "select failed: %s\n",
611 if (FD_ISSET (icmpsock, &rs))
612 process_icmp_response ();
613 if (0 == (++alt % 2))
614 send_icmp_echo (&external);
616 send_udp (&external);
618 /* select failed (internal error or OS out of resources) */
619 closesocket(icmpsock);
620 closesocket(rawsock);
621 closesocket(udpsock);
627 /* end of gnunet-nat-server-windows.c */