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