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
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"
69 * TTL to use for our outgoing messages.
75 #define ICMP_TIME_EXCEEDED 11
78 * How often do we send our ICMP messages to receive replies?
80 #define ICMP_SEND_FREQUENCY_MS 500
89 * Version (4 bits) + Internet header length (4 bits)
109 * Flags (3 bits) + Fragment offset (13 bits)
111 uint16_t flags_frag_offset;
134 * Destination address
140 * Format of ICMP packet.
142 struct icmp_ttl_exceeded_header
152 /* followed by original payload */
155 struct icmp_echo_header
167 * Beginning of UDP packet.
181 * Socket we use to receive "fake" ICMP replies.
183 static SOCKET icmpsock;
186 * Socket we use to send our ICMP requests.
188 static SOCKET rawsock;
191 * Socket we use to send our UDP requests.
193 static SOCKET udpsock;
196 * Target "dummy" address.
198 static struct in_addr dummy;
202 * CRC-16 for IP/ICMP headers.
204 * @param data what to calculate the CRC over
205 * @param bytes number of bytes in data (must be multiple of 2)
206 * @return the CRC 16.
209 calc_checksum(const uint16_t *data,
216 for (i=0;i<bytes/2;i++)
218 sum = (sum & 0xffff) + (sum >> 16);
219 sum = htons(0xffff - sum);
225 * Convert IPv4 address from text to binary form.
227 * @param af address family
228 * @param cp the address to print
229 * @param buf where to write the address result
230 * @return 1 on success
237 buf->s_addr = inet_addr(cp);
238 if (buf->s_addr == INADDR_NONE)
241 "Error %d handling address %s",
251 * Send an ICMP message to the dummy IP.
253 * @param my_ip source address (our ip address)
256 send_icmp_echo (const struct in_addr *my_ip)
258 char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)];
259 struct icmp_ttl_exceeded_header icmp_echo;
260 struct ip_header ip_pkt;
261 struct sockaddr_in dst;
266 ip_pkt.vers_ihl = 0x45;
268 ip_pkt.pkt_len = htons (sizeof (packet));
269 ip_pkt.id = htons (256);
270 ip_pkt.flags_frag_offset = 0;
271 ip_pkt.ttl = IPDEFTTL;
272 ip_pkt.proto = IPPROTO_ICMP;
274 ip_pkt.src_ip = my_ip->s_addr;
275 ip_pkt.dst_ip = dummy.s_addr;
276 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
277 sizeof (struct ip_header)));
278 memcpy (&packet[off],
280 sizeof (struct ip_header));
281 off += sizeof (struct ip_header);
283 icmp_echo.type = ICMP_ECHO;
285 icmp_echo.reserved = 0;
286 icmp_echo.checksum = 0;
287 icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
288 sizeof (struct icmp_echo_header)));
289 memcpy (&packet[off],
291 sizeof (struct icmp_echo_header));
292 off += sizeof (struct icmp_echo_header);
294 memset (&dst, 0, sizeof (dst));
295 dst.sin_family = AF_INET;
296 dst.sin_addr = dummy;
297 err = sendto(rawsock,
299 (struct sockaddr*)&dst,
305 "sendto failed: %s\n", strerror(errno));
311 "Error: partial send of ICMP message\n");
317 * Send a UDP message to the dummy IP.
322 struct sockaddr_in dst;
325 memset (&dst, 0, sizeof (dst));
326 dst.sin_family = AF_INET;
327 dst.sin_addr = dummy;
328 dst.sin_port = htons (NAT_TRAV_PORT);
329 err = sendto(udpsock,
331 (struct sockaddr*)&dst,
337 "sendto failed: %s\n", strerror(errno));
343 "Error: partial send of ICMP message\n");
349 * We've received an ICMP response. Process it.
352 process_icmp_response ()
356 struct in_addr source_ip;
357 struct ip_header ip_pkt;
358 struct icmp_ttl_exceeded_header icmp_pkt;
359 struct icmp_echo_header icmp_echo;
360 struct udp_header udp_pkt;
365 have = read (icmpsock, buf, sizeof (buf));
369 "Error reading raw socket: %s\n",
376 "Received message of %u bytes\n",
377 (unsigned int) have);
379 if (have < (ssize_t) (sizeof (struct ip_header) + sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct ip_header)))
387 sizeof (struct ip_header));
388 off += sizeof (struct ip_header);
394 sizeof (struct icmp_ttl_exceeded_header));
395 off += sizeof (struct icmp_ttl_exceeded_header);
396 if ( (ICMP_TIME_EXCEEDED != icmp_ttl.type) ||
397 (0 != icmp_ttl.code) )
399 /* different type than what we want */
402 /* skip 2nd IP header */
405 sizeof (struct ip_header));
406 off += sizeof (struct ip_header);
408 switch (ip_pkt.proto)
411 if (have != (sizeof (struct ip_header) * 2 +
412 sizeof (struct icmp_ttl_exceeded_header) +
413 sizeof (struct icmp_echo_header)) )
418 /* grab ICMP ECHO content */
421 sizeof (struct icmp_echo_header));
422 port = (uint16_t) ntohl (icmp_echo.reserved);
425 if (have != (sizeof (struct ip_header) * 2 +
426 sizeof (struct icmp_ttl_exceeded_header) +
427 sizeof (struct udp_header)) )
432 /* grab UDP content */
435 sizeof (struct udp_header));
436 port = ntohs (udp_pkt.length);
439 /* different type than what we want */
444 WSAAddressToString((LPSOCKADDR)&source_ip,
457 (unsigned int) port);
463 * Create an ICMP raw socket for reading.
465 * @return INVALID_SOCKET on error
472 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
473 if (INVALID_SOCKET == ret)
476 "Error opening RAW socket: %s\n",
478 return INVALID_SOCKET;
485 * Create an ICMP raw socket for writing.
487 * @return INVALID_SOCKET on error
492 DWORD bOptVal = TRUE;
493 int bOptLen = sizeof(bOptVal);
495 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
496 if (INVALID_SOCKET == rawsock)
499 "Error opening RAW socket: %s\n",
501 return INVALID_SOCKET;
504 if (0 != setsockopt(rawsock,
507 (char*)&bOptVal, bOptLen))
510 "Error setting SO_BROADCAST to ON: %s\n",
512 closesocket(rawsock);
513 return INVALID_SOCKET;
515 if (0 != setsockopt(rawsock,
518 (char*)&bOptVal, bOptLen))
521 "Error setting IP_HDRINCL to ON: %s\n",
523 closesocket(rawsock);
524 return INVALID_SOCKET;
531 * Create a UDP socket for writinging.
533 * @param my_ip source address (our ip address)
534 * @return INVALID_SOCKET on error
537 make_udp_socket (const struct in_addr *my_ip)
540 struct sockaddr_in addr;
542 ret = socket (AF_INET, SOCK_DGRAM, 0);
543 if (INVALID_SOCKET == ret)
546 "Error opening UDP socket: %s\n",
548 return INVALID_SOCKET;
550 memset (&addr, 0, sizeof (addr));
551 addr.sin_family = AF_INET;
552 addr.sin_addr = *my_ip;
553 addr.sin_port = htons (NAT_TRAV_PORT);
559 "Error binding UDP socket to port %u: %s\n",
562 /* likely problematic, but not certain, try to continue */
572 struct in_addr external;
581 "This program must be started with our (internal NAT) IP as the only argument.\n");
584 if (1 != inet_pton (AF_INET, argv[1], &external))
587 "Error parsing IPv4 address: %s, error %s\n",
588 argv[1], strerror (errno));
591 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
594 "Internal error converting dummy IP to binary.\n");
597 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
599 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
602 if (INVALID_SOCKET == (icmpsock = make_icmp_socket()))
606 if (INVALID_SOCKET == (make_raw_socket()))
608 closesocket (icmpsock);
611 if (INVALID_SOCKET == (udpsock = make_udp_socket(&external)))
613 closesocket (icmpsock);
614 closesocket (rawsock);
620 FD_SET (icmpsock, &rs);
622 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
623 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
628 "select failed: %s\n",
632 if (FD_ISSET (icmpsock, &rs))
633 process_icmp_response ();
634 if (0 == (++alt % 2))
635 send_icmp_echo (&external);
639 /* select failed (internal error or OS out of resources) */
640 closesocket(icmpsock);
641 closesocket(rawsock);
642 closesocket(udpsock);
648 /* end of gnunet-nat-server-windows.c */