windoze suckssss
[oweals/gnunet.git] / src / transport / gnunet-nat-client-windows.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file src/transport/gnunet-nat-client-windows.c
23  * @brief Tool to help bypass NATs using ICMP method; must run as
24  *        root (SUID will do) or administrator on W32
25  *        This code will work under GNU/Linux or W32.
26  * @author Nathan Evans
27  *
28  * This program will send ONE ICMP message using RAW sockets
29  * to the IP address specified as the second argument.  Since
30  * it uses RAW sockets, it must be installed SUID or run as 'root'.
31  * In order to keep the security risk of the resulting SUID binary
32  * minimal, the program ONLY opens the RAW socket with root
33  * privileges, then drops them and only then starts to process
34  * command line arguments.  The code also does not link against
35  * any shared libraries (except libc) and is strictly minimal
36  * (except for checking for errors).  The following list of people
37  * have reviewed this code and considered it safe since the last
38  * modification (if you reviewed it, please have your name added
39  * to the list):
40  *
41  * - Christian Grothoff
42  * - Nathan Evans
43  */
44 #define _GNU_SOURCE
45 #ifdef WIN32
46 #include <winsock2.h>
47 #else
48 #include <sys/types.h> 
49 #include <sys/socket.h>
50 #include <arpa/inet.h>
51 #include <sys/select.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip_icmp.h>
54 #include <netinet/in.h>
55 #endif
56 #include <sys/time.h>
57 #include <sys/types.h>
58 #include <unistd.h>
59 #include <stdio.h>
60 #include <string.h>
61 #include <errno.h>
62 #include <stdlib.h>
63 #include <stdint.h>
64 #include <time.h>
65
66
67 #ifdef WIN32
68 typedef unsigned int uid_t;
69 typedef SOCKET Socket;
70 typedef unsigned short ushort;
71 #define ICMP_ECHO 8
72 #define IPDEFTTL        64              /* default ttl, from RFC 1340 */
73 #define ICMP_TIME_EXCEEDED      11      /* Time Exceeded                */
74 #define IP_HDRINCL      3       /* int; Header is included with data.  */
75 #else
76 typedef int Socket;
77 #endif
78
79 /**
80  * Must match IP given in the server.
81  */
82 #define DUMMY_IP "192.0.2.86"
83
84 #define NAT_TRAV_PORT 22225
85
86 struct ip_packet 
87 {
88   uint8_t vers_ihl;
89   uint8_t tos;
90   uint16_t pkt_len;
91   uint16_t id;
92   uint16_t flags_frag_offset;
93   uint8_t ttl;
94   uint8_t proto;
95   uint16_t checksum;
96   uint32_t src_ip;
97   uint32_t dst_ip;
98 };
99
100 struct icmp_packet 
101 {
102   uint8_t type;
103   uint8_t code;
104   uint16_t checksum;
105   uint32_t reserved;
106
107 };
108
109 struct icmp_echo_packet
110 {
111   uint8_t type;
112   uint8_t code;
113   uint16_t checksum;
114   uint32_t reserved;
115   uint32_t data;
116 };
117
118 struct udp_packet
119 {
120   uint16_t src_port;
121
122   uint16_t dst_port;
123
124   uint32_t length;
125 };
126
127 static Socket rawsock;
128
129 static struct in_addr dummy;
130  
131 static uint32_t port;
132
133 #if WIN32
134 /**
135  * @param af address family
136  * @param cp the address to print
137  * @param buf where to write the address result
138  */
139 static int inet_pton (int af, char *cp, struct in_addr *buf)
140 {
141   //ret = WSAStringToAddress (cp, af, NULL, (LPSOCKADDR)buf, &ssize);
142   buf->s_addr = inet_addr(cp);
143   if (buf->s_addr == INADDR_NONE)
144     {
145       fprintf(stderr, "Error %d handling address %s", WSAGetLastError(), cp);
146       return 0;
147     }
148   else
149     return 1;
150 }
151 #endif
152
153 static uint16_t 
154 calc_checksum(const uint16_t *data, 
155               unsigned int bytes)
156 {
157   uint32_t sum;
158   unsigned int i;
159
160   sum = 0;
161   for (i=0;i<bytes/2;i++) 
162     sum += data[i];        
163   sum = (sum & 0xffff) + (sum >> 16);
164   sum = htons(0xffff - sum);
165   return sum;
166 }
167
168
169 static void
170 make_echo (const struct in_addr *src_ip,
171            struct icmp_echo_packet *echo, uint32_t num)
172 {
173   memset(echo, 0, sizeof(struct icmp_echo_packet));
174   echo->type = ICMP_ECHO;
175   echo->code = 0;
176   echo->reserved = 0;
177   echo->checksum = 0;
178   echo->data = htons(num);
179   echo->checksum = htons(calc_checksum((uint16_t*)echo, 
180                                        sizeof (struct icmp_echo_packet)));
181 }
182
183 /**
184  * Send an ICMP message to the dummy IP.
185  *
186  * @param my_ip source address (our ip address)
187  */
188 static void
189 send_icmp_echo (const struct in_addr *my_ip)
190 {
191   struct icmp_packet icmp_echo;
192   struct sockaddr_in dst;
193   size_t off;
194   int err;
195   struct ip_packet ip_pkt;
196   struct icmp_packet icmp_pkt;
197   char packet[sizeof (ip_pkt) + sizeof (icmp_pkt)];
198
199   off = 0;
200   memset(&ip_pkt, 0, sizeof(ip_pkt));
201   ip_pkt.vers_ihl = 0x45;
202   ip_pkt.tos = 0;
203   ip_pkt.pkt_len = sizeof (packet);
204   ip_pkt.id = 1;
205   ip_pkt.flags_frag_offset = 0;
206   ip_pkt.ttl = IPDEFTTL;
207   ip_pkt.proto = IPPROTO_ICMP;
208   ip_pkt.checksum = 0;
209   ip_pkt.src_ip = my_ip->s_addr;
210   ip_pkt.dst_ip = dummy.s_addr;
211   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
212   memcpy (packet, &ip_pkt, sizeof (ip_pkt));
213   off += sizeof (ip_pkt);
214   make_echo (my_ip, &icmp_echo);
215   memcpy (&packet[off], &icmp_echo, sizeof (icmp_echo));
216   off += sizeof (icmp_echo);
217
218   memset (&dst, 0, sizeof (dst));
219   dst.sin_family = AF_INET;
220   dst.sin_addr = dummy;
221   err = sendto(rawsock,
222                packet, off, 0,
223                (struct sockaddr*)&dst,
224                sizeof(dst));
225   if (err < 0)
226     {
227 #if VERBOSE
228       fprintf(stderr,
229               "sendto failed: %s\n", strerror(errno));
230 #endif
231     }
232   else if (err != off)
233     {
234       fprintf(stderr,
235               "Error: partial send of ICMP message\n");
236     }
237 }
238
239 /**
240  * Send an ICMP message to the target.
241  *
242  * @param my_ip source address
243  * @param other target address
244  */
245 static void
246 send_icmp_udp (const struct in_addr *my_ip,
247                const struct in_addr *other)
248 {
249   struct ip_packet ip_pkt;
250   struct icmp_packet icmp_pkt;
251   struct udp_packet udp_pkt;
252
253   struct sockaddr_in dst;
254   char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
255
256   size_t off;
257   int err;
258
259   /* ip header: send to (known) ip address */
260   off = 0;
261   memset(&ip_pkt, 0, sizeof(ip_pkt));
262   ip_pkt.vers_ihl = 0x45;
263   ip_pkt.tos = 0;
264   ip_pkt.pkt_len = htons(sizeof (packet));
265   ip_pkt.id = htons(256);
266   ip_pkt.flags_frag_offset = 0;
267   ip_pkt.ttl = 128;
268   ip_pkt.proto = IPPROTO_ICMP;
269   ip_pkt.checksum = 0;
270   ip_pkt.src_ip = my_ip->s_addr;
271   ip_pkt.dst_ip = other->s_addr;
272   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
273   memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
274   off += sizeof(ip_pkt);
275
276   /* ip header of the presumably 'lost' udp packet */
277   ip_pkt.vers_ihl = 0x45;
278   ip_pkt.tos = 0;
279   ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
280
281   icmp_pkt.type = 11; /* TTL exceeded */
282   icmp_pkt.code = 0;
283   icmp_pkt.checksum = 0;
284   icmp_pkt.reserved = 0;
285   memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
286   off += sizeof(icmp_pkt);
287
288   /* build inner IP header */
289   memset(&ip_pkt, 0, sizeof(ip_pkt));
290   ip_pkt.vers_ihl = 0x45;
291   ip_pkt.tos = 0;
292   ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
293   ip_pkt.id = htons(0);
294   ip_pkt.flags_frag_offset = 0;
295   ip_pkt.ttl = 128;
296   ip_pkt.proto = IPPROTO_UDP;
297   ip_pkt.checksum = 0;
298   ip_pkt.src_ip = other->s_addr;
299   ip_pkt.dst_ip = dummy.s_addr;
300   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
301   memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
302   off += sizeof(ip_pkt);
303
304   /* build UDP header */
305   udp_pkt.src_port = htons(NAT_TRAV_PORT); /* FIXME: does this port matter? */
306   udp_pkt.dst_port = htons(NAT_TRAV_PORT);
307
308   memset(&udp_pkt.length, 0, sizeof(uint32_t));
309   udp_pkt.length = htonl(port);
310   memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
311   off += sizeof(udp_pkt);
312
313   /* set ICMP checksum */
314   icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
315                             sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
316   memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
317
318
319   memset (&dst, 0, sizeof (dst));
320   dst.sin_family = AF_INET;
321   dst.sin_addr = *other;
322   err = sendto(rawsock,
323                packet,
324                off, 0,
325                (struct sockaddr*)&dst,
326                sizeof(dst));
327
328   if (err < 0)
329     {
330       fprintf(stderr,
331               "sendto failed: %s\n", strerror(errno));
332     }
333   else if (err != off)
334     {
335       fprintf(stderr,
336               "Error: partial send of ICMP message\n");
337     }
338 }
339
340
341 /**
342  * Send an ICMP message to the target.
343  *
344  * @param my_ip source address
345  * @param other target address
346  */
347 static void
348 send_icmp (const struct in_addr *my_ip,
349            const struct in_addr *other)
350 {
351   struct ip_packet ip_pkt;
352   struct icmp_packet *icmp_pkt;
353   struct icmp_echo_packet icmp_echo;
354   struct sockaddr_in dst;
355   char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
356
357   size_t off;
358   int err;
359
360   /* ip header: send to (known) ip address */
361   off = 0;
362   memset(&ip_pkt, 0, sizeof(ip_pkt));
363   ip_pkt.vers_ihl = 0x45;
364   ip_pkt.tos = 0;
365   ip_pkt.pkt_len = sizeof (packet); /* huh? */
366   ip_pkt.id = 1; 
367   ip_pkt.flags_frag_offset = 0;
368   ip_pkt.ttl = IPDEFTTL;
369   ip_pkt.proto = IPPROTO_ICMP;
370   ip_pkt.checksum = 0; 
371   ip_pkt.src_ip = my_ip->s_addr;
372   ip_pkt.dst_ip = other->s_addr;
373   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
374   memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
375   off += sizeof (ip_pkt);
376   /* icmp reply: time exceeded */
377   icmp_pkt = (struct icmp_packet*) &packet[off];
378   memset(icmp_pkt, 0, sizeof(struct icmp_packet));
379   icmp_pkt->type = ICMP_TIME_EXCEEDED;
380   icmp_pkt->code = 0; 
381   icmp_pkt->reserved = 0;
382   icmp_pkt->checksum = 0;
383
384   off += sizeof (struct icmp_packet);
385
386   /* ip header of the presumably 'lost' udp packet */
387   ip_pkt.vers_ihl = 0x45;
388   ip_pkt.tos = 0;
389   ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
390
391   ip_pkt.id = 1; 
392   ip_pkt.flags_frag_offset = 0;
393   ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
394   ip_pkt.proto = IPPROTO_ICMP;
395   ip_pkt.src_ip = other->s_addr;
396   ip_pkt.dst_ip = dummy.s_addr;
397   ip_pkt.checksum = 0;
398   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));  
399   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
400   off += sizeof (struct ip_packet);
401
402   make_echo (other, &icmp_echo, port);
403   memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet));
404   off += sizeof (struct icmp_echo_packet);
405
406   icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
407                                              sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
408
409   memset (&dst, 0, sizeof (dst));
410   dst.sin_family = AF_INET;
411   dst.sin_addr = *other;
412   err = sendto(rawsock, 
413                packet, 
414                off, 0, 
415                (struct sockaddr*)&dst, 
416                sizeof(dst)); /* or sizeof 'struct sockaddr'? */
417
418   fprintf(stderr, "Sent %d bytes\n", err);
419   if (err < 0) 
420     {
421       fprintf(stderr,
422               "sendto failed: %s\n", strerror(errno));
423     }
424   else if (err != off) 
425     {
426       fprintf(stderr,
427               "Error: partial send of ICMP message\n");
428     }
429 }
430
431
432 static Socket
433 make_raw_socket ()
434 {
435   const int one = 1;
436   int ret;
437
438   ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
439   if (-1 == ret)
440     {
441       fprintf (stderr,
442                "Error opening RAW socket: %s\n",
443                strerror (errno));
444       return -1;
445     }  
446   if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
447                  (char *)&one, sizeof(one)) == -1)
448     fprintf(stderr,
449             "setsockopt failed: %s\n",
450             strerror (errno));
451   if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
452                  (char *)&one, sizeof(one)) == -1)
453     fprintf(stderr,
454             "setsockopt failed: %s\n",
455             strerror (errno));
456   return ret;
457 }
458
459
460 int
461 main (int argc, char *const *argv)
462 {
463   struct in_addr external;
464   struct in_addr target;
465 #ifndef WIN32
466   uid_t uid;
467 #endif
468
469 #ifdef WIN32
470   // WSA startup
471   WSADATA wsaData;
472   if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
473   {
474       fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
475       return 4;                       // ERROR
476   }
477 #endif
478
479   if (-1 == (rawsock = make_raw_socket()))
480     return 1;
481
482 #ifndef WIN32
483   uid = getuid ();
484   if (0 != setresuid (uid, uid, uid))
485     fprintf (stderr,
486              "Failed to setresuid: %s\n",
487              strerror (errno));
488 #endif
489   if (argc != 4)
490     {
491       fprintf (stderr,
492                "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
493       return 1;
494     }
495   port = atoi(argv[3]);
496
497   if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
498        (1 != inet_pton (AF_INET, argv[2], &target)) )
499     {
500       fprintf (stderr,
501                "Error parsing IPv4 address: %s\n",
502                strerror (errno));
503       return 1;
504     }
505   if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
506     {
507       fprintf (stderr,
508                "Error parsing IPv4 address: %s\n",
509                strerror (errno));
510       abort ();
511     }
512   send_icmp_echo(&target);
513   fprintf(stderr, "Sending icmp message.\n");
514   send_icmp (&external,
515              &target);
516   fprintf(stderr, "Sending icmp udp message.\n");
517   send_icmp_udp (&external,
518              &target);
519
520   close (rawsock);
521 #ifdef WIN32
522   WSACleanup ();
523 #endif
524   return 0;
525 }
526
527 /* end of gnunet-nat-client.c */