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
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <arpa/inet.h>
50 #include <sys/select.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_icmp.h>
53 #include <netinet/in.h>
56 #include <sys/types.h>
67 typedef unsigned int uid_t;
68 typedef SOCKET Socket;
69 typedef unsigned short ushort;
71 #define IPDEFTTL 64 /* default ttl, from RFC 1340 */
72 #define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
73 #define IP_HDRINCL 3 /* int; Header is included with data. */
79 * Must match IP given in the client.
81 #define DUMMY_IP "192.0.2.86"
86 * How often do we send our ICMP messages to receive replies?
88 #define ICMP_SEND_FREQUENCY_MS 500
96 uint16_t flags_frag_offset;
103 u_char vers_ihl; // Version (4 bits) + Internet header length (4 bits)
104 u_char tos; // Type of service
105 u_short pkt_len; // Total length
106 u_short id; // Identification
107 u_short flags_frag_offset; // Flags (3 bits) + Fragment offset (13 bits)
108 u_char ttl; // Time to live
109 u_char proto; // Protocol
110 u_short checksum; // Header checksum
111 u_long src_ip; // Source address
112 u_long dst_ip; // Destination address
132 static Socket icmpsock;
134 static Socket rawsock;
136 static struct in_addr dummy;
139 calc_checksum(const uint16_t *data,
146 for (i=0;i<bytes/2;i++)
148 sum = (sum & 0xffff) + (sum >> 16);
149 sum = htons(0xffff - sum);
155 * @param af address family
156 * @param cp the address to print
157 * @param buf where to write the address result
159 static int inet_pton (int af, char *cp, struct in_addr *buf)
161 //ret = WSAStringToAddress (cp, af, NULL, (LPSOCKADDR)buf, &ssize);
162 buf->s_addr = inet_addr(cp);
163 if (buf->s_addr == INADDR_NONE)
165 fprintf(stderr, "Error %d handling address %s", WSAGetLastError(), cp);
174 make_echo (const struct in_addr *src_ip,
175 struct icmp_packet *echo)
177 memset(echo, 0, sizeof(struct icmp_packet));
178 echo->type = ICMP_ECHO;
182 echo->checksum = htons(calc_checksum((uint16_t*)echo, sizeof (struct icmp_packet)));
187 * Send an ICMP message to the dummy IP.
189 * @param my_ip source address (our ip address)
192 send_icmp_echo (const struct in_addr *my_ip)
194 struct icmp_packet icmp_echo;
195 struct sockaddr_in dst;
198 struct ip_packet ip_pkt;
199 struct icmp_packet icmp_pkt;
200 char packet[sizeof (ip_pkt) + sizeof (icmp_pkt)];
203 memset(&ip_pkt, 0, sizeof(ip_pkt));
204 ip_pkt.vers_ihl = 0x45;
206 ip_pkt.pkt_len = sizeof (packet);
208 ip_pkt.flags_frag_offset = 0;
209 ip_pkt.ttl = IPDEFTTL;
210 ip_pkt.proto = IPPROTO_ICMP;
212 ip_pkt.src_ip = my_ip->s_addr;
213 ip_pkt.dst_ip = dummy.s_addr;
214 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
215 memcpy (packet, &ip_pkt, sizeof (ip_pkt));
216 off += sizeof (ip_pkt);
218 memset (&dst, 0, sizeof (dst));
219 dst.sin_family = AF_INET;
220 dst.sin_addr = dummy;
221 err = sendto(rawsock,
223 (struct sockaddr*)&dst,
226 make_echo (my_ip, &icmp_echo);
227 memcpy (&packet[off], &icmp_echo, sizeof (icmp_echo));
228 off += sizeof (icmp_echo);
230 memset (&dst, 0, sizeof (dst));
231 dst.sin_family = AF_INET;
232 dst.sin_addr = dummy;
233 err = sendto(rawsock,
235 (struct sockaddr*)&dst,
241 "sendto failed: %s\n", strerror(errno));
247 "Error: partial send of ICMP message\n");
253 process_icmp_response ()
258 struct ip_packet ip_pkt;
259 struct icmp_packet icmp_pkt;
260 struct udp_packet udp_pkt;
265 have = read (icmpsock, buf, sizeof (buf));
269 "Error reading raw socket: %s\n",
276 "Received message of %u bytes\n",
277 (unsigned int) have);
279 if (have == sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2 + sizeof(uint32_t))
283 else if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2)
287 "Received ICMP message of unexpected size: %u bytes\n",
288 (unsigned int) have);
293 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
294 off += sizeof (ip_pkt);
295 memcpy (&icmp_pkt, &buf[off], sizeof (icmp_pkt));
296 off += sizeof (icmp_pkt);
297 if ( ((ip_pkt.proto != IPPROTO_ICMP) && (ip_pkt.proto != IPPROTO_UDP)) ||
298 (icmp_pkt.type != ICMP_TIME_EXCEEDED) ||
299 (icmp_pkt.code != 0) )
301 /* maybe we got an actual reply back... */
308 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
309 off += sizeof (ip_pkt);
312 if (ip_pkt.proto == IPPROTO_UDP)
319 memcpy(&port, &buf[sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2], sizeof(uint32_t));
322 DWORD ssize = sizeof(buf);
323 WSAAddressToString((LPSOCKADDR)&sip, sizeof(sip), NULL, buf, &ssize);
324 fprintf (stdout, "%s:%d\n", buf, port);
331 sizeof (buf)), port);
336 memcpy(&udp_pkt, &buf[off], sizeof(udp_pkt));
338 DWORD ssize = sizeof(buf);
339 WSAAddressToString((LPSOCKADDR)&sip, sizeof(sip), NULL, buf, &ssize);
340 fprintf (stdout, "%s:%d\n", buf, ntohs((int)udp_pkt.length));
347 sizeof (buf)), ntohl(udp_pkt.length));
353 DWORD ssize = sizeof(buf);
354 WSAAddressToString((LPSOCKADDR)&sip, sizeof(sip), NULL, buf, &ssize);
355 fprintf (stdout, "%s\n", buf);
374 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
378 "Error opening RAW socket: %s\n",
383 if (ret == INVALID_SOCKET)
385 fprintf (stderr, "Invalid socket %d!\n", ret);
389 if (ret >= FD_SETSIZE)
392 "Socket number too large (%d > %u)\n",
394 (unsigned int) FD_SETSIZE);
409 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
413 "Error opening RAW socket: %s\n",
417 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
418 (char *)&one, sizeof(one)) == -1)
420 "setsockopt failed: %s\n",
422 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
423 (char *)&one, sizeof(one)) == -1)
425 "setsockopt failed: %s\n",
432 main (int argc, char *const *argv)
434 struct in_addr external;
444 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
446 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
451 if (-1 == (icmpsock = make_icmp_socket()))
453 if (-1 == (rawsock = make_raw_socket()))
460 if (0 != setresuid (uid, uid, uid))
462 "Failed to setresuid: %s\n",
468 "This program must be started with our (internal NAT) IP as the only argument.\n");
472 if (1 != inet_pton (AF_INET, argv[1], &external))
475 "Error parsing IPv4 address: %s, error %s\n",
476 argv[1], strerror (errno));
480 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) abort ();
484 FD_SET (icmpsock, &rs);
486 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
487 select (icmpsock + 1, &rs, NULL, NULL, &tv);
488 if (FD_ISSET (icmpsock, &rs))
489 process_icmp_response ();
490 send_icmp_echo (&external);
494 closesocket(icmpsock);
495 closesocket(rawsock);
502 /* end of gnunet-nat-server-windows.c */