2 This file is part of GNUnet.
3 Copyright (C) 2010 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file src/nat/gnunet-helper-nat-server-windows.c
21 * @brief Windows tool to help bypass NATs using ICMP method
22 * This code will work under W32 only
23 * @author Christian Grothoff
25 * This program will send ONE ICMP message every 500 ms RAW sockets
26 * to a DUMMY IP address and also listens for ICMP replies. Since
27 * it uses RAW sockets, it must be run as an administrative user.
28 * In order to keep the security risk of the resulting binary
29 * minimal, the program ONLY opens the two RAW sockets with administrative
30 * privileges, then drops them and only then starts to process
31 * command line arguments. The code also does not link against
32 * any shared libraries (except libc) and is strictly minimal
33 * (except for checking for errors). The following list of people
34 * have reviewed this code and considered it safe since the last
35 * modification (if you reviewed it, please have your name added
39 * - Christian Grothoff
42 /* Instead of including gnunet_common.h */
43 #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
45 #define FD_SETSIZE 1024
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 * Must match packet ID used by gnunet-helper-nat-client.c
79 * TTL to use for our outgoing messages.
85 #define ICMP_TIME_EXCEEDED 11
88 * How often do we send our ICMP messages to receive replies?
90 #define ICMP_SEND_FREQUENCY_MS 500
99 * Version (4 bits) + Internet header length (4 bits)
119 * Flags (3 bits) + Fragment offset (13 bits)
121 uint16_t flags_frag_offset;
144 * Destination address
150 * Format of ICMP packet.
152 struct icmp_ttl_exceeded_header
162 /* followed by original payload */
165 struct icmp_echo_header
177 * Beginning of UDP packet.
191 * Will this binary be run in permissions testing mode?
193 static boolean privilege_testing = FALSE;
196 * Socket we use to receive "fake" ICMP replies.
198 static SOCKET icmpsock;
201 * Socket we use to send our ICMP requests.
203 static SOCKET rawsock;
206 * Socket we use to send our UDP requests.
208 static SOCKET udpsock;
211 * Target "dummy" address.
213 static struct in_addr dummy;
217 * CRC-16 for IP/ICMP headers.
219 * @param data what to calculate the CRC over
220 * @param bytes number of bytes in data (must be multiple of 2)
221 * @return the CRC 16.
224 calc_checksum (const uint16_t * data, unsigned int bytes)
230 for (i = 0; i < bytes / 2; i++)
232 sum = (sum & 0xffff) + (sum >> 16);
233 sum = htons (0xffff - sum);
239 * Convert IPv4 address from text to binary form.
241 * @param af address family
242 * @param cp the address to print
243 * @param buf where to write the address result
244 * @return 1 on success
247 inet_pton (int af, const char *cp, struct in_addr *buf)
249 buf->s_addr = inet_addr (cp);
250 if (buf->s_addr == INADDR_NONE)
252 fprintf (stderr, "Error %d handling address %s", WSAGetLastError (), cp);
260 * Send an ICMP message to the dummy IP.
262 * @param my_ip source address (our ip address)
265 send_icmp_echo (const struct in_addr *my_ip)
267 char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)];
268 struct icmp_echo_header icmp_echo;
269 struct ip_header ip_pkt;
270 struct sockaddr_in dst;
275 ip_pkt.vers_ihl = 0x45;
277 ip_pkt.pkt_len = htons (sizeof (packet));
278 ip_pkt.id = htons (PACKET_ID);
279 ip_pkt.flags_frag_offset = 0;
280 ip_pkt.ttl = IPDEFTTL;
281 ip_pkt.proto = IPPROTO_ICMP;
283 ip_pkt.src_ip = my_ip->s_addr;
284 ip_pkt.dst_ip = dummy.s_addr;
286 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
287 GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
288 off += sizeof (struct ip_header);
290 icmp_echo.type = ICMP_ECHO;
292 icmp_echo.reserved = 0;
293 icmp_echo.checksum = 0;
296 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
297 GNUNET_memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
298 off += sizeof (struct icmp_echo_header);
300 memset (&dst, 0, sizeof (dst));
301 dst.sin_family = AF_INET;
302 dst.sin_addr = dummy;
304 sendto (rawsock, packet, off, 0, (struct sockaddr *) &dst, sizeof (dst));
308 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
313 fprintf (stderr, "Error: partial send of ICMP message\n");
319 * Send a UDP message to the dummy IP.
324 struct sockaddr_in dst;
327 memset (&dst, 0, sizeof (dst));
328 dst.sin_family = AF_INET;
329 dst.sin_addr = dummy;
330 dst.sin_port = htons (NAT_TRAV_PORT);
331 err = sendto (udpsock, NULL, 0, 0, (struct sockaddr *) &dst, sizeof (dst));
335 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
340 fprintf (stderr, "Error: partial send of ICMP message\n");
346 * We've received an ICMP response. Process it.
349 process_icmp_response ()
353 struct in_addr source_ip;
354 struct ip_header ip_pkt;
355 struct icmp_ttl_exceeded_header icmp_ttl;
356 struct icmp_echo_header icmp_echo;
357 struct udp_header udp_pkt;
362 have = read (icmpsock, buf, sizeof (buf));
365 fprintf (stderr, "Error reading raw socket: %s\n", strerror (errno));
369 fprintf (stderr, "Received message of %u bytes\n", (unsigned int) have);
372 (ssize_t) (sizeof (struct ip_header) +
373 sizeof (struct icmp_ttl_exceeded_header) +
374 sizeof (struct ip_header)))
380 GNUNET_memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header));
381 off += sizeof (struct ip_header);
382 GNUNET_memcpy (&source_ip, &ip_pkt.src_ip, sizeof (source_ip));
383 GNUNET_memcpy (&icmp_ttl, &buf[off], sizeof (struct icmp_ttl_exceeded_header));
384 off += sizeof (struct icmp_ttl_exceeded_header);
385 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code))
387 /* different type than what we want */
390 /* skip 2nd IP header */
391 GNUNET_memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header));
392 off += sizeof (struct ip_header);
394 switch (ip_pkt.proto)
398 (sizeof (struct ip_header) * 2 +
399 sizeof (struct icmp_ttl_exceeded_header) +
400 sizeof (struct icmp_echo_header)))
405 /* grab ICMP ECHO content */
406 GNUNET_memcpy (&icmp_echo, &buf[off], sizeof (struct icmp_echo_header));
407 port = (uint16_t) ntohl (icmp_echo.reserved);
411 (sizeof (struct ip_header) * 2 +
412 sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct udp_header)))
417 /* grab UDP content */
418 GNUNET_memcpy (&udp_pkt, &buf[off], sizeof (struct udp_header));
419 port = ntohs (udp_pkt.length);
422 /* different type than what we want */
426 ssize = sizeof (buf);
427 WSAAddressToString ((LPSOCKADDR) & source_ip, sizeof (source_ip), NULL, buf,
430 fprintf (stdout, "%s\n", buf);
432 fprintf (stdout, "%s:%u\n", buf, (unsigned int) port);
438 * Create an ICMP raw socket for reading.
440 * @return INVALID_SOCKET on error
447 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
448 if (INVALID_SOCKET == ret)
450 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
451 return INVALID_SOCKET;
458 * Create an ICMP raw socket for writing.
460 * @return INVALID_SOCKET on error
465 DWORD bOptVal = TRUE;
466 int bOptLen = sizeof (bOptVal);
468 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
469 if (INVALID_SOCKET == rawsock)
471 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
472 return INVALID_SOCKET;
476 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal,
479 fprintf (stderr, "Error setting SO_BROADCAST to ON: %s\n",
481 closesocket (rawsock);
482 return INVALID_SOCKET;
485 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &bOptVal, bOptLen))
487 fprintf (stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror (errno));
488 closesocket (rawsock);
489 return INVALID_SOCKET;
496 * Create a UDP socket for writing.
498 * @param my_ip source address (our ip address)
499 * @return INVALID_SOCKET on error
502 make_udp_socket (const struct in_addr *my_ip)
505 struct sockaddr_in addr;
507 ret = socket (AF_INET, SOCK_DGRAM, 0);
508 if (INVALID_SOCKET == ret)
510 fprintf (stderr, "Error opening UDP socket: %s\n", strerror (errno));
511 return INVALID_SOCKET;
513 memset (&addr, 0, sizeof (addr));
514 addr.sin_family = AF_INET;
515 addr.sin_addr = *my_ip;
516 addr.sin_port = htons (NAT_TRAV_PORT);
517 if (0 != bind (ret, (struct sockaddr *) &addr, sizeof (addr)))
519 fprintf (stderr, "Error binding UDP socket to port %u: %s\n", NAT_TRAV_PORT,
521 /* likely problematic, but not certain, try to continue */
528 main (int argc, char *const *argv)
530 struct in_addr external;
534 unsigned int alt = 0;
536 if ( (argc > 1) && (0 != strcmp (argv[1], "-d")))
538 privilege_testing = TRUE;
541 "DEBUG: Running binary in privilege testing mode.");
549 "This program must be started with our (internal NAT) IP as the only argument.\n");
552 if (1 != inet_pton (AF_INET, argv[1], &external))
554 fprintf (stderr, "Error parsing IPv4 address: %s, error %s\n", argv[1],
558 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
560 fprintf (stderr, "Internal error converting dummy IP to binary.\n");
563 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
565 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
568 if (INVALID_SOCKET == (icmpsock = make_icmp_socket ()))
572 if (INVALID_SOCKET == (make_raw_socket ()))
574 closesocket (icmpsock);
577 if (INVALID_SOCKET == (udpsock = make_udp_socket (&external)))
579 closesocket (icmpsock);
580 closesocket (rawsock);
584 while ( ! privilege_testing)
587 FD_SET (icmpsock, &rs);
589 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
590 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
594 fprintf (stderr, "select failed: %s\n", strerror (errno));
597 if (FD_ISSET (icmpsock, &rs))
598 process_icmp_response ();
599 if (0 == (++alt % 2))
600 send_icmp_echo (&external);
604 /* select failed (internal error or OS out of resources) */
605 closesocket (icmpsock);
606 closesocket (rawsock);
607 closesocket (udpsock);
609 if (privilege_testing)
615 /* end of gnunet-helper-nat-server-windows.c */