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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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
44 /* Instead of including gnunet_common.h */
45 #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
47 #define FD_SETSIZE 1024
51 #include <sys/types.h>
61 * Should we print some debug output?
66 * Must match IP given in the client.
68 #define DUMMY_IP "192.0.2.86"
73 #define NAT_TRAV_PORT 22225
76 * Must match packet ID used by gnunet-helper-nat-client.c
81 * TTL to use for our outgoing messages.
87 #define ICMP_TIME_EXCEEDED 11
90 * How often do we send our ICMP messages to receive replies?
92 #define ICMP_SEND_FREQUENCY_MS 500
101 * Version (4 bits) + Internet header length (4 bits)
121 * Flags (3 bits) + Fragment offset (13 bits)
123 uint16_t flags_frag_offset;
146 * Destination address
152 * Format of ICMP packet.
154 struct icmp_ttl_exceeded_header
164 /* followed by original payload */
167 struct icmp_echo_header
179 * Beginning of UDP packet.
193 * Will this binary be run in permissions testing mode?
195 static boolean privilege_testing = FALSE;
198 * Socket we use to receive "fake" ICMP replies.
200 static SOCKET icmpsock;
203 * Socket we use to send our ICMP requests.
205 static SOCKET rawsock;
208 * Socket we use to send our UDP requests.
210 static SOCKET udpsock;
213 * Target "dummy" address.
215 static struct in_addr dummy;
219 * CRC-16 for IP/ICMP headers.
221 * @param data what to calculate the CRC over
222 * @param bytes number of bytes in data (must be multiple of 2)
223 * @return the CRC 16.
226 calc_checksum (const uint16_t * data, unsigned int bytes)
232 for (i = 0; i < bytes / 2; i++)
234 sum = (sum & 0xffff) + (sum >> 16);
235 sum = htons (0xffff - sum);
241 * Convert IPv4 address from text to binary form.
243 * @param af address family
244 * @param cp the address to print
245 * @param buf where to write the address result
246 * @return 1 on success
249 inet_pton (int af, const char *cp, struct in_addr *buf)
251 buf->s_addr = inet_addr (cp);
252 if (buf->s_addr == INADDR_NONE)
254 fprintf (stderr, "Error %d handling address %s", WSAGetLastError (), cp);
262 * Send an ICMP message to the dummy IP.
264 * @param my_ip source address (our ip address)
267 send_icmp_echo (const struct in_addr *my_ip)
269 char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)];
270 struct icmp_echo_header icmp_echo;
271 struct ip_header ip_pkt;
272 struct sockaddr_in dst;
277 ip_pkt.vers_ihl = 0x45;
279 ip_pkt.pkt_len = htons (sizeof (packet));
280 ip_pkt.id = htons (PACKET_ID);
281 ip_pkt.flags_frag_offset = 0;
282 ip_pkt.ttl = IPDEFTTL;
283 ip_pkt.proto = IPPROTO_ICMP;
285 ip_pkt.src_ip = my_ip->s_addr;
286 ip_pkt.dst_ip = dummy.s_addr;
288 htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
289 GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
290 off += sizeof (struct ip_header);
292 icmp_echo.type = ICMP_ECHO;
294 icmp_echo.reserved = 0;
295 icmp_echo.checksum = 0;
298 ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
299 GNUNET_memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
300 off += sizeof (struct icmp_echo_header);
302 memset (&dst, 0, sizeof (dst));
303 dst.sin_family = AF_INET;
304 dst.sin_addr = dummy;
306 sendto (rawsock, packet, off, 0, (struct sockaddr *) &dst, sizeof (dst));
310 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
315 fprintf (stderr, "Error: partial send of ICMP message\n");
321 * Send a UDP message to the dummy IP.
326 struct sockaddr_in dst;
329 memset (&dst, 0, sizeof (dst));
330 dst.sin_family = AF_INET;
331 dst.sin_addr = dummy;
332 dst.sin_port = htons (NAT_TRAV_PORT);
333 err = sendto (udpsock, NULL, 0, 0, (struct sockaddr *) &dst, sizeof (dst));
337 fprintf (stderr, "sendto failed: %s\n", strerror (errno));
342 fprintf (stderr, "Error: partial send of ICMP message\n");
348 * We've received an ICMP response. Process it.
351 process_icmp_response ()
355 struct in_addr source_ip;
356 struct ip_header ip_pkt;
357 struct icmp_ttl_exceeded_header icmp_ttl;
358 struct icmp_echo_header icmp_echo;
359 struct udp_header udp_pkt;
364 have = read (icmpsock, buf, sizeof (buf));
367 fprintf (stderr, "Error reading raw socket: %s\n", strerror (errno));
371 fprintf (stderr, "Received message of %u bytes\n", (unsigned int) have);
374 (ssize_t) (sizeof (struct ip_header) +
375 sizeof (struct icmp_ttl_exceeded_header) +
376 sizeof (struct ip_header)))
382 GNUNET_memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header));
383 off += sizeof (struct ip_header);
384 GNUNET_memcpy (&source_ip, &ip_pkt.src_ip, sizeof (source_ip));
385 GNUNET_memcpy (&icmp_ttl, &buf[off], sizeof (struct icmp_ttl_exceeded_header));
386 off += sizeof (struct icmp_ttl_exceeded_header);
387 if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code))
389 /* different type than what we want */
392 /* skip 2nd IP header */
393 GNUNET_memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header));
394 off += sizeof (struct ip_header);
396 switch (ip_pkt.proto)
400 (sizeof (struct ip_header) * 2 +
401 sizeof (struct icmp_ttl_exceeded_header) +
402 sizeof (struct icmp_echo_header)))
407 /* grab ICMP ECHO content */
408 GNUNET_memcpy (&icmp_echo, &buf[off], sizeof (struct icmp_echo_header));
409 port = (uint16_t) ntohl (icmp_echo.reserved);
413 (sizeof (struct ip_header) * 2 +
414 sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct udp_header)))
419 /* grab UDP content */
420 GNUNET_memcpy (&udp_pkt, &buf[off], sizeof (struct udp_header));
421 port = ntohs (udp_pkt.length);
424 /* different type than what we want */
428 ssize = sizeof (buf);
429 WSAAddressToString ((LPSOCKADDR) & source_ip, sizeof (source_ip), NULL, buf,
432 fprintf (stdout, "%s\n", buf);
434 fprintf (stdout, "%s:%u\n", buf, (unsigned int) port);
440 * Create an ICMP raw socket for reading.
442 * @return INVALID_SOCKET on error
449 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
450 if (INVALID_SOCKET == ret)
452 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
453 return INVALID_SOCKET;
460 * Create an ICMP raw socket for writing.
462 * @return INVALID_SOCKET on error
467 DWORD bOptVal = TRUE;
468 int bOptLen = sizeof (bOptVal);
470 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
471 if (INVALID_SOCKET == rawsock)
473 fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
474 return INVALID_SOCKET;
478 setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal,
481 fprintf (stderr, "Error setting SO_BROADCAST to ON: %s\n",
483 closesocket (rawsock);
484 return INVALID_SOCKET;
487 setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &bOptVal, bOptLen))
489 fprintf (stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror (errno));
490 closesocket (rawsock);
491 return INVALID_SOCKET;
498 * Create a UDP socket for writing.
500 * @param my_ip source address (our ip address)
501 * @return INVALID_SOCKET on error
504 make_udp_socket (const struct in_addr *my_ip)
507 struct sockaddr_in addr;
509 ret = socket (AF_INET, SOCK_DGRAM, 0);
510 if (INVALID_SOCKET == ret)
512 fprintf (stderr, "Error opening UDP socket: %s\n", strerror (errno));
513 return INVALID_SOCKET;
515 memset (&addr, 0, sizeof (addr));
516 addr.sin_family = AF_INET;
517 addr.sin_addr = *my_ip;
518 addr.sin_port = htons (NAT_TRAV_PORT);
519 if (0 != bind (ret, (struct sockaddr *) &addr, sizeof (addr)))
521 fprintf (stderr, "Error binding UDP socket to port %u: %s\n", NAT_TRAV_PORT,
523 /* likely problematic, but not certain, try to continue */
530 main (int argc, char *const *argv)
532 struct in_addr external;
536 unsigned int alt = 0;
538 if ( (argc > 1) && (0 != strcmp (argv[1], "-d")))
540 privilege_testing = TRUE;
543 "DEBUG: Running binary in privilege testing mode.");
551 "This program must be started with our (internal NAT) IP as the only argument.\n");
554 if (1 != inet_pton (AF_INET, argv[1], &external))
556 fprintf (stderr, "Error parsing IPv4 address: %s, error %s\n", argv[1],
560 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
562 fprintf (stderr, "Internal error converting dummy IP to binary.\n");
565 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
567 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
570 if (INVALID_SOCKET == (icmpsock = make_icmp_socket ()))
574 if (INVALID_SOCKET == (make_raw_socket ()))
576 closesocket (icmpsock);
579 if (INVALID_SOCKET == (udpsock = make_udp_socket (&external)))
581 closesocket (icmpsock);
582 closesocket (rawsock);
586 while ( ! privilege_testing)
589 FD_SET (icmpsock, &rs);
591 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
592 if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
596 fprintf (stderr, "select failed: %s\n", strerror (errno));
599 if (FD_ISSET (icmpsock, &rs))
600 process_icmp_response ();
601 if (0 == (++alt % 2))
602 send_icmp_echo (&external);
606 /* select failed (internal error or OS out of resources) */
607 closesocket (icmpsock);
608 closesocket (rawsock);
609 closesocket (udpsock);
611 if (privilege_testing)
617 /* end of gnunet-helper-nat-server-windows.c */