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 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.
17 * @file src/nat/gnunet-helper-nat-server-windows.c
18 * @brief Windows tool to help bypass NATs using ICMP method
19 * This code will work under W32 only
20 * @author Christian Grothoff
22 * This program will send ONE ICMP message every 500 ms RAW sockets
23 * to a DUMMY IP address and also listens for ICMP replies. Since
24 * it uses RAW sockets, it must be run as an administrative user.
25 * In order to keep the security risk of the resulting binary
26 * minimal, the program ONLY opens the two RAW sockets with administrative
27 * privileges, then drops them and only then starts to process
28 * command line arguments. The code also does not link against
29 * any shared libraries (except libc) and is strictly minimal
30 * (except for checking for errors). The following list of people
31 * have reviewed this code and considered it safe since the last
32 * modification (if you reviewed it, please have your name added
36 * - Christian Grothoff
39 /* Instead of including gnunet_common.h */
40 #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
42 #define FD_SETSIZE 1024
46 #include <sys/types.h>
56 * Should we print some debug output?
61 * Must match IP given in the client.
63 #define DUMMY_IP "192.0.2.86"
68 #define NAT_TRAV_PORT 22225
71 * Must match packet ID used by gnunet-helper-nat-client.c
76 * TTL to use for our outgoing messages.
82 #define ICMP_TIME_EXCEEDED 11
85 * How often do we send our ICMP messages to receive replies?
87 #define ICMP_SEND_FREQUENCY_MS 500
96 * Version (4 bits) + Internet header length (4 bits)
116 * Flags (3 bits) + Fragment offset (13 bits)
118 uint16_t flags_frag_offset;
141 * Destination address
147 * Format of ICMP packet.
149 struct icmp_ttl_exceeded_header
159 /* followed by original payload */
162 struct icmp_echo_header
174 * Beginning of UDP packet.
188 * Will this binary be run in permissions testing mode?
190 static boolean privilege_testing = FALSE;
193 * Socket we use to receive "fake" ICMP replies.
195 static SOCKET icmpsock;
198 * Socket we use to send our ICMP requests.
200 static SOCKET rawsock;
203 * Socket we use to send our UDP requests.
205 static SOCKET udpsock;
208 * Target "dummy" address.
210 static struct in_addr dummy;
214 * CRC-16 for IP/ICMP headers.
216 * @param data what to calculate the CRC over
217 * @param bytes number of bytes in data (must be multiple of 2)
218 * @return the CRC 16.
221 calc_checksum (const uint16_t * data, unsigned int bytes)
227 for (i = 0; i < bytes / 2; i++)
229 sum = (sum & 0xffff) + (sum >> 16);
230 sum = htons (0xffff - sum);
236 * Convert IPv4 address from text to binary form.
238 * @param af address family
239 * @param cp the address to print
240 * @param buf where to write the address result
241 * @return 1 on success
244 inet_pton (int af, const char *cp, struct in_addr *buf)
246 buf->s_addr = inet_addr (cp);
247 if (buf->s_addr == INADDR_NONE)
249 fprintf (stderr, "Error %d handling address %s", WSAGetLastError (), cp);
257 * Send an ICMP message to the dummy IP.
259 * @param my_ip source address (our ip address)
262 send_icmp_echo (const struct in_addr *my_ip)
264 char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)];
265 struct icmp_echo_header icmp_echo;
266 struct ip_header ip_pkt;
267 struct sockaddr_in dst;
272 ip_pkt.vers_ihl = 0x45;
274 ip_pkt.pkt_len = htons (sizeof (packet));
275 ip_pkt.id = htons (PACKET_ID);
276 ip_pkt.flags_frag_offset = 0;
277 ip_pkt.ttl = IPDEFTTL;
278 ip_pkt.proto = IPPROTO_ICMP;
280 ip_pkt.src_ip = my_ip->s_addr;
281 ip_pkt.dst_ip = dummy.s_addr;
283 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
284 GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
285 off += sizeof (struct ip_header);
287 icmp_echo.type = ICMP_ECHO;
289 icmp_echo.reserved = 0;
290 icmp_echo.checksum = 0;
293 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
294 GNUNET_memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
295 off += sizeof (struct icmp_echo_header);
297 memset (&dst, 0, sizeof (dst));
298 dst.sin_family = AF_INET;
299 dst.sin_addr = dummy;
301 sendto (rawsock, packet, off, 0, (struct sockaddr *) &dst, sizeof (dst));
305 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
310 fprintf (stderr, "Error: partial send of ICMP message\n");
316 * Send a UDP message to the dummy IP.
321 struct sockaddr_in dst;
324 memset (&dst, 0, sizeof (dst));
325 dst.sin_family = AF_INET;
326 dst.sin_addr = dummy;
327 dst.sin_port = htons (NAT_TRAV_PORT);
328 err = sendto (udpsock, NULL, 0, 0, (struct sockaddr *) &dst, sizeof (dst));
332 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
337 fprintf (stderr, "Error: partial send of ICMP message\n");
343 * We've received an ICMP response. Process it.
346 process_icmp_response ()
350 struct in_addr source_ip;
351 struct ip_header ip_pkt;
352 struct icmp_ttl_exceeded_header icmp_ttl;
353 struct icmp_echo_header icmp_echo;
354 struct udp_header udp_pkt;
359 have = read (icmpsock, buf, sizeof (buf));
362 fprintf (stderr, "Error reading raw socket: %s\n", strerror (errno));
366 fprintf (stderr, "Received message of %u bytes\n", (unsigned int) have);
369 (ssize_t) (sizeof (struct ip_header) +
370 sizeof (struct icmp_ttl_exceeded_header) +
371 sizeof (struct ip_header)))
377 GNUNET_memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header));
378 off += sizeof (struct ip_header);
379 GNUNET_memcpy (&source_ip, &ip_pkt.src_ip, sizeof (source_ip));
380 GNUNET_memcpy (&icmp_ttl, &buf[off], sizeof (struct icmp_ttl_exceeded_header));
381 off += sizeof (struct icmp_ttl_exceeded_header);
382 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code))
384 /* different type than what we want */
387 /* skip 2nd IP header */
388 GNUNET_memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header));
389 off += sizeof (struct ip_header);
391 switch (ip_pkt.proto)
395 (sizeof (struct ip_header) * 2 +
396 sizeof (struct icmp_ttl_exceeded_header) +
397 sizeof (struct icmp_echo_header)))
402 /* grab ICMP ECHO content */
403 GNUNET_memcpy (&icmp_echo, &buf[off], sizeof (struct icmp_echo_header));
404 port = (uint16_t) ntohl (icmp_echo.reserved);
408 (sizeof (struct ip_header) * 2 +
409 sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct udp_header)))
414 /* grab UDP content */
415 GNUNET_memcpy (&udp_pkt, &buf[off], sizeof (struct udp_header));
416 port = ntohs (udp_pkt.length);
419 /* different type than what we want */
423 ssize = sizeof (buf);
424 WSAAddressToString ((LPSOCKADDR) & source_ip, sizeof (source_ip), NULL, buf,
427 fprintf (stdout, "%s\n", buf);
429 fprintf (stdout, "%s:%u\n", buf, (unsigned int) port);
435 * Create an ICMP raw socket for reading.
437 * @return INVALID_SOCKET on error
444 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
445 if (INVALID_SOCKET == ret)
447 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
448 return INVALID_SOCKET;
455 * Create an ICMP raw socket for writing.
457 * @return INVALID_SOCKET on error
462 DWORD bOptVal = TRUE;
463 int bOptLen = sizeof (bOptVal);
465 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
466 if (INVALID_SOCKET == rawsock)
468 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
469 return INVALID_SOCKET;
473 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal,
476 fprintf (stderr, "Error setting SO_BROADCAST to ON: %s\n",
478 closesocket (rawsock);
479 return INVALID_SOCKET;
482 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &bOptVal, bOptLen))
484 fprintf (stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror (errno));
485 closesocket (rawsock);
486 return INVALID_SOCKET;
493 * Create a UDP socket for writing.
495 * @param my_ip source address (our ip address)
496 * @return INVALID_SOCKET on error
499 make_udp_socket (const struct in_addr *my_ip)
502 struct sockaddr_in addr;
504 ret = socket (AF_INET, SOCK_DGRAM, 0);
505 if (INVALID_SOCKET == ret)
507 fprintf (stderr, "Error opening UDP socket: %s\n", strerror (errno));
508 return INVALID_SOCKET;
510 memset (&addr, 0, sizeof (addr));
511 addr.sin_family = AF_INET;
512 addr.sin_addr = *my_ip;
513 addr.sin_port = htons (NAT_TRAV_PORT);
514 if (0 != bind (ret, (struct sockaddr *) &addr, sizeof (addr)))
516 fprintf (stderr, "Error binding UDP socket to port %u: %s\n", NAT_TRAV_PORT,
518 /* likely problematic, but not certain, try to continue */
525 main (int argc, char *const *argv)
527 struct in_addr external;
531 unsigned int alt = 0;
533 if ( (argc > 1) && (0 != strcmp (argv[1], "-d")))
535 privilege_testing = TRUE;
538 "DEBUG: Running binary in privilege testing mode.");
546 "This program must be started with our (internal NAT) IP as the only argument.\n");
549 if (1 != inet_pton (AF_INET, argv[1], &external))
551 fprintf (stderr, "Error parsing IPv4 address: %s, error %s\n", argv[1],
555 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
557 fprintf (stderr, "Internal error converting dummy IP to binary.\n");
560 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
562 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
565 if (INVALID_SOCKET == (icmpsock = make_icmp_socket ()))
569 if (INVALID_SOCKET == (make_raw_socket ()))
571 closesocket (icmpsock);
574 if (INVALID_SOCKET == (udpsock = make_udp_socket (&external)))
576 closesocket (icmpsock);
577 closesocket (rawsock);
581 while ( ! privilege_testing)
584 FD_SET (icmpsock, &rs);
586 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
587 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
591 fprintf (stderr, "select failed: %s\n", strerror (errno));
594 if (FD_ISSET (icmpsock, &rs))
595 process_icmp_response ();
596 if (0 == (++alt % 2))
597 send_icmp_echo (&external);
601 /* select failed (internal error or OS out of resources) */
602 closesocket (icmpsock);
603 closesocket (rawsock);
604 closesocket (udpsock);
606 if (privilege_testing)
612 /* end of gnunet-helper-nat-server-windows.c */