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 static void
184 make_echo2 (const struct in_addr *src_ip,
185            struct icmp_packet *echo)
186 {
187   memset(echo, 0, sizeof(struct icmp_packet));
188   echo->type = ICMP_ECHO;
189   echo->code = 0;
190   echo->reserved = 0;
191   echo->checksum = 0;
192   echo->checksum = htons(calc_checksum((uint16_t*)echo, sizeof (struct icmp_packet)));
193 }
194
195 /**
196  * Send an ICMP message to the dummy IP.
197  *
198  * @param my_ip source address (our ip address)
199  */
200 static void
201 send_icmp_echo (const struct in_addr *my_ip)
202 {
203   struct icmp_packet icmp_echo;
204   struct icmp_echo_packet icmp_echo_pkt;
205   struct sockaddr_in dst;
206   size_t off;
207   int err;
208   struct ip_packet ip_pkt;
209   struct icmp_packet icmp_pkt;
210   char packet[sizeof (ip_pkt) + sizeof (icmp_pkt)];
211
212   off = 0;
213   memset(&ip_pkt, 0, sizeof(ip_pkt));
214   ip_pkt.vers_ihl = 0x45;
215   ip_pkt.tos = 0;
216   ip_pkt.pkt_len = sizeof (packet);
217   ip_pkt.id = 1;
218   ip_pkt.flags_frag_offset = 0;
219   ip_pkt.ttl = IPDEFTTL;
220   ip_pkt.proto = IPPROTO_ICMP;
221   ip_pkt.checksum = 0;
222   ip_pkt.src_ip = my_ip->s_addr;
223   ip_pkt.dst_ip = dummy.s_addr;
224   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
225   memcpy (packet, &ip_pkt, sizeof (ip_pkt));
226   off += sizeof (ip_pkt);
227   make_echo (my_ip, &icmp_echo_pkt, port);
228   memcpy (&packet[off], &icmp_echo_pkt, sizeof (icmp_echo_pkt));
229   off += sizeof (icmp_echo_pkt);
230
231   memset (&dst, 0, sizeof (dst));
232   dst.sin_family = AF_INET;
233   dst.sin_addr = dummy;
234   err = sendto(rawsock,
235                packet, off, 0,
236                (struct sockaddr*)&dst,
237                sizeof(dst));
238
239   fprintf(stderr, "Sent %d bytes\n", err);
240   if (err < 0)
241     {
242 #if VERBOSE
243       fprintf(stderr,
244               "sendto failed: %s\n", strerror(errno));
245 #endif
246     }
247   else if (err != off)
248     {
249       fprintf(stderr,
250               "Error: partial send of ICMP message\n");
251     }
252 }
253
254 /**
255  * Send an ICMP message to the target.
256  *
257  * @param my_ip source address
258  * @param other target address
259  */
260 static void
261 send_icmp_udp (const struct in_addr *my_ip,
262                const struct in_addr *other)
263 {
264   struct ip_packet ip_pkt;
265   struct icmp_packet icmp_pkt;
266   struct udp_packet udp_pkt;
267
268   struct sockaddr_in dst;
269   char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
270
271   size_t off;
272   int err;
273
274   /* ip header: send to (known) ip address */
275   off = 0;
276   memset(&ip_pkt, 0, sizeof(ip_pkt));
277   ip_pkt.vers_ihl = 0x45;
278   ip_pkt.tos = 0;
279   ip_pkt.pkt_len = htons(sizeof (packet));
280   ip_pkt.id = htons(256);
281   ip_pkt.flags_frag_offset = 0;
282   ip_pkt.ttl = 128;
283   ip_pkt.proto = IPPROTO_ICMP;
284   ip_pkt.checksum = 0;
285   ip_pkt.src_ip = my_ip->s_addr;
286   ip_pkt.dst_ip = other->s_addr;
287   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
288   memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
289   off += sizeof(ip_pkt);
290
291   /* ip header of the presumably 'lost' udp packet */
292   ip_pkt.vers_ihl = 0x45;
293   ip_pkt.tos = 0;
294   ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
295
296   icmp_pkt.type = 11; /* TTL exceeded */
297   icmp_pkt.code = 0;
298   icmp_pkt.checksum = 0;
299   icmp_pkt.reserved = 0;
300   memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
301   off += sizeof(icmp_pkt);
302
303   /* build inner IP header */
304   memset(&ip_pkt, 0, sizeof(ip_pkt));
305   ip_pkt.vers_ihl = 0x45;
306   ip_pkt.tos = 0;
307   ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
308   ip_pkt.id = htons(0);
309   ip_pkt.flags_frag_offset = 0;
310   ip_pkt.ttl = 128;
311   ip_pkt.proto = IPPROTO_UDP;
312   ip_pkt.checksum = 0;
313   ip_pkt.src_ip = other->s_addr;
314   ip_pkt.dst_ip = dummy.s_addr;
315   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
316   memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
317   off += sizeof(ip_pkt);
318
319   /* build UDP header */
320   udp_pkt.src_port = htons(NAT_TRAV_PORT); /* FIXME: does this port matter? */
321   udp_pkt.dst_port = htons(NAT_TRAV_PORT);
322
323   memset(&udp_pkt.length, 0, sizeof(uint32_t));
324   udp_pkt.length = htonl(port);
325   memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
326   off += sizeof(udp_pkt);
327
328   /* set ICMP checksum */
329   icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
330                             sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
331   memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
332
333
334   memset (&dst, 0, sizeof (dst));
335   dst.sin_family = AF_INET;
336   dst.sin_addr = *other;
337   err = sendto(rawsock,
338                packet,
339                off, 0,
340                (struct sockaddr*)&dst,
341                sizeof(dst));
342
343   fprintf(stderr, "Sent %d bytes\n", err);
344
345   if (err < 0)
346     {
347       fprintf(stderr,
348               "sendto failed: %s\n", strerror(errno));
349     }
350   else if (err != off)
351     {
352       fprintf(stderr,
353               "Error: partial send of ICMP message\n");
354     }
355 }
356
357
358 /**
359  * Send an ICMP message to the target.
360  *
361  * @param my_ip source address
362  * @param other target address
363  */
364 static void
365 send_icmp (const struct in_addr *my_ip,
366            const struct in_addr *other)
367 {
368   struct ip_packet ip_pkt;
369   struct icmp_packet *icmp_pkt;
370   struct icmp_echo_packet icmp_echo;
371   struct sockaddr_in dst;
372   char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
373
374   size_t off;
375   int err;
376
377   /* ip header: send to (known) ip address */
378   off = 0;
379   memset(&ip_pkt, 0, sizeof(ip_pkt));
380   ip_pkt.vers_ihl = 0x45;
381   ip_pkt.tos = 0;
382   ip_pkt.pkt_len = sizeof (packet); /* huh? */
383   ip_pkt.id = 1; 
384   ip_pkt.flags_frag_offset = 0;
385   ip_pkt.ttl = IPDEFTTL;
386   ip_pkt.proto = IPPROTO_ICMP;
387   ip_pkt.checksum = 0; 
388   ip_pkt.src_ip = my_ip->s_addr;
389   ip_pkt.dst_ip = other->s_addr;
390   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
391   memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
392   off += sizeof (ip_pkt);
393   /* icmp reply: time exceeded */
394   icmp_pkt = (struct icmp_packet*) &packet[off];
395   memset(icmp_pkt, 0, sizeof(struct icmp_packet));
396   icmp_pkt->type = ICMP_TIME_EXCEEDED;
397   icmp_pkt->code = 0; 
398   icmp_pkt->reserved = 0;
399   icmp_pkt->checksum = 0;
400
401   off += sizeof (struct icmp_packet);
402
403   /* ip header of the presumably 'lost' udp packet */
404   ip_pkt.vers_ihl = 0x45;
405   ip_pkt.tos = 0;
406   ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
407
408   ip_pkt.id = 1; 
409   ip_pkt.flags_frag_offset = 0;
410   ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
411   ip_pkt.proto = IPPROTO_ICMP;
412   ip_pkt.src_ip = other->s_addr;
413   ip_pkt.dst_ip = dummy.s_addr;
414   ip_pkt.checksum = 0;
415   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));  
416   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
417   off += sizeof (struct ip_packet);
418
419   make_echo (other, &icmp_echo, port);
420   memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet));
421   off += sizeof (struct icmp_echo_packet);
422
423   icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
424                                              sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
425
426   memset (&dst, 0, sizeof (dst));
427   dst.sin_family = AF_INET;
428   dst.sin_addr = *other;
429   err = sendto(rawsock, 
430                packet, 
431                off, 0, 
432                (struct sockaddr*)&dst, 
433                sizeof(dst)); /* or sizeof 'struct sockaddr'? */
434
435   fprintf(stderr, "Sent %d bytes\n", err);
436   if (err < 0) 
437     {
438       fprintf(stderr,
439               "sendto failed: %s\n", strerror(errno));
440     }
441   else if (err != off) 
442     {
443       fprintf(stderr,
444               "Error: partial send of ICMP message\n");
445     }
446 }
447
448
449 static Socket
450 make_raw_socket ()
451 {
452   const int one = 1;
453   int ret;
454
455   ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
456   if (-1 == ret)
457     {
458       fprintf (stderr,
459                "Error opening RAW socket: %s\n",
460                strerror (errno));
461       return -1;
462     }  
463   if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
464                  (char *)&one, sizeof(one)) == -1)
465     fprintf(stderr,
466             "setsockopt failed: %s\n",
467             strerror (errno));
468   if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
469                  (char *)&one, sizeof(one)) == -1)
470     fprintf(stderr,
471             "setsockopt failed: %s\n",
472             strerror (errno));
473   return ret;
474 }
475
476
477 int
478 main (int argc, char *const *argv)
479 {
480   struct in_addr external;
481   struct in_addr target;
482 #ifndef WIN32
483   uid_t uid;
484 #endif
485
486 #ifdef WIN32
487   // WSA startup
488   WSADATA wsaData;
489   if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
490   {
491       fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
492       return 4;                       // ERROR
493   }
494 #endif
495
496   if (-1 == (rawsock = make_raw_socket()))
497     return 1;
498
499 #ifndef WIN32
500   uid = getuid ();
501   if (0 != setresuid (uid, uid, uid))
502     fprintf (stderr,
503              "Failed to setresuid: %s\n",
504              strerror (errno));
505 #endif
506   if (argc != 4)
507     {
508       fprintf (stderr,
509                "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
510       return 1;
511     }
512   port = atoi(argv[3]);
513
514   if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
515        (1 != inet_pton (AF_INET, argv[2], &target)) )
516     {
517       fprintf (stderr,
518                "Error parsing IPv4 address: %s\n",
519                strerror (errno));
520       return 1;
521     }
522   if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
523     {
524       fprintf (stderr,
525                "Error parsing IPv4 address: %s\n",
526                strerror (errno));
527       abort ();
528     }
529   fprintf(stderr, "Sending icmp echo message.\n");
530   send_icmp_echo(&target);
531   fprintf(stderr, "Sending icmp message.\n");
532   send_icmp (&external,
533              &target);
534   fprintf(stderr, "Sending icmp udp message.\n");
535   send_icmp_udp (&external,
536              &target);
537
538 #ifdef WIN32
539   WSACleanup ();
540 #endif
541   close (rawsock);
542   return 0;
543 }
544
545 /* end of gnunet-nat-client.c */