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 SOCKET Socket;
68 typedef unsigned short ushort;
70 #define IPDEFTTL 64 /* default ttl, from RFC 1340 */
71 #define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
72 #define IP_HDRINCL 3 /* int; Header is included with data. */
78 * Must match IP given in the client.
80 #define DUMMY_IP "192.0.2.86"
85 * How often do we send our ICMP messages to receive replies?
87 #define ICMP_SEND_FREQUENCY_MS 500
95 uint16_t flags_frag_offset;
120 static Socket icmpsock;
122 static Socket rawsock;
124 static struct in_addr dummy;
127 calc_checksum(const uint16_t *data,
134 for (i=0;i<bytes/2;i++)
136 sum = (sum & 0xffff) + (sum >> 16);
137 sum = htons(0xffff - sum);
143 make_echo (const struct in_addr *src_ip,
144 struct icmp_packet *echo)
146 memset(echo, 0, sizeof(struct icmp_packet));
147 echo->type = ICMP_ECHO;
151 echo->checksum = htons(calc_checksum((uint16_t*)echo, sizeof (struct icmp_packet)));
156 * Send an ICMP message to the dummy IP.
158 * @param my_ip source address (our ip address)
161 send_icmp_echo (const struct in_addr *my_ip)
163 struct icmp_packet icmp_echo;
164 struct sockaddr_in dst;
167 struct ip_packet ip_pkt;
168 struct icmp_packet icmp_pkt;
169 char packet[sizeof (ip_pkt) + sizeof (icmp_pkt)];
172 memset(&ip_pkt, 0, sizeof(ip_pkt));
173 ip_pkt.vers_ihl = 0x45;
175 ip_pkt.pkt_len = sizeof (packet);
177 ip_pkt.flags_frag_offset = 0;
178 ip_pkt.ttl = IPDEFTTL;
179 ip_pkt.proto = IPPROTO_ICMP;
181 ip_pkt.src_ip = my_ip->s_addr;
182 ip_pkt.dst_ip = dummy.s_addr;
183 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
184 memcpy (packet, &ip_pkt, sizeof (ip_pkt));
185 off += sizeof (ip_pkt);
186 make_echo (my_ip, &icmp_echo);
187 memcpy (&packet[off], &icmp_echo, sizeof (icmp_echo));
188 off += sizeof (icmp_echo);
190 memset (&dst, 0, sizeof (dst));
191 dst.sin_family = AF_INET;
192 dst.sin_addr = dummy;
193 err = sendto(rawsock,
195 (struct sockaddr*)&dst,
201 "sendto failed: %s\n", strerror(errno));
207 "Error: partial send of ICMP message\n");
213 process_icmp_response ()
218 struct ip_packet ip_pkt;
219 struct icmp_packet icmp_pkt;
220 struct udp_packet udp_pkt;
226 have = read (icmpsock, buf, sizeof (buf));
230 "Error reading raw socket: %s\n",
236 if (have == sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2 + sizeof(uint32_t))
240 else if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2)
244 "Received ICMP message of unexpected size: %u bytes\n",
245 (unsigned int) have);
250 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
251 off += sizeof (ip_pkt);
252 memcpy (&icmp_pkt, &buf[off], sizeof (icmp_pkt));
253 off += sizeof (icmp_pkt);
254 if ( ((ip_pkt.proto != IPPROTO_ICMP) && (ip_pkt.proto != IPPROTO_UDP)) ||
255 (icmp_pkt.type != ICMP_TIME_EXCEEDED) ||
256 (icmp_pkt.code != 0) )
258 /* maybe we got an actual reply back... */
265 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
266 off += sizeof (ip_pkt);
269 if (ip_pkt.proto == IPPROTO_UDP)
276 memcpy(&port, &buf[sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2], sizeof(uint32_t));
283 sizeof (buf)), port);
287 memcpy(&udp_pkt, &buf[off], sizeof(udp_pkt));
293 sizeof (buf)), ntohl(udp_pkt.length));
313 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
317 "Error opening RAW socket: %s\n",
321 if (ret >= FD_SETSIZE)
324 "Socket number too large (%d > %u)\n",
326 (unsigned int) FD_SETSIZE);
344 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
348 "Error opening RAW socket: %s\n",
352 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
353 (char *)&one, sizeof(one)) == -1)
355 "setsockopt failed: %s\n",
357 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
358 (char *)&one, sizeof(one)) == -1)
360 "setsockopt failed: %s\n",
367 main (int argc, char *const *argv)
369 struct in_addr external;
377 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
379 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
384 if (-1 == (icmpsock = make_icmp_socket()))
386 if (-1 == (rawsock = make_raw_socket()))
392 if (0 != setresuid (uid, uid, uid))
394 "Failed to setresuid: %s\n",
399 "This program must be started with our (internal NAT) IP as the only argument.\n");
402 if (1 != inet_pton (AF_INET, argv[1], &external))
405 "Error parsing IPv4 address: %s\n",
409 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) abort ();
413 FD_SET (icmpsock, &rs);
415 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
416 select (icmpsock + 1, &rs, NULL, NULL, &tv);
417 if (FD_ISSET (icmpsock, &rs))
418 process_icmp_response ();
419 send_icmp_echo (&external);
423 closesocket(icmpsock);
424 closesocket(rawsock);
431 /* end of gnunet-nat-server-windows.c */