stuff
[oweals/gnunet.git] / src / transport / gnunet-nat-server.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-server.c
23  * @brief Tool to help bypass NATs using ICMP method; must run as root (SUID will do)
24  *        This code will work under GNU/Linux only (or maybe BSDs, but never W32)
25  * @author Christian Grothoff
26  */
27 #define _GNU_SOURCE
28 #include <sys/types.h> 
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
31 #include <sys/select.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <stdint.h>
40 #include <time.h>
41 #include <netinet/ip.h>
42 #include <netinet/ip_icmp.h>
43 #include <netinet/in.h> 
44
45 /**
46  * Must match IP given in the client.
47  */
48 #define DUMMY_IP "1.2.3.4"
49
50 /**
51  * How often do we send our ICMP messages to receive replies?
52  */
53 #define ICMP_SEND_FREQUENCY_MS 500
54
55 struct ip_packet 
56 {
57   uint8_t vers_ihl;
58   uint8_t tos;
59   uint16_t pkt_len;
60   uint16_t id;
61   uint16_t flags_frag_offset;
62   uint8_t ttl;
63   uint8_t proto;
64   uint16_t checksum;
65   uint32_t src_ip;
66   uint32_t dst_ip;
67 };
68
69 struct icmp_packet 
70 {
71   uint8_t type;
72   uint8_t code;
73   uint16_t checksum;
74   uint32_t reserved;
75 };
76
77
78 static int icmpsock;
79
80 static int rawsock;
81
82 static struct in_addr dummy;
83
84 static uint16_t 
85 calc_checksum(const uint16_t *data, 
86               unsigned int bytes)
87 {
88   uint32_t sum;
89   unsigned int i;
90
91   sum = 0;
92   for (i=0;i<bytes/2;i++) 
93     sum += data[i];        
94   sum = (sum & 0xffff) + (sum >> 16);
95   sum = htons(0xffff - sum);
96   return sum;
97 }
98
99
100 static void
101 make_echo (const struct in_addr *src_ip,
102            struct icmp_packet *echo)
103 {
104   memset(echo, 0, sizeof(struct icmp_packet));
105   echo->type = ICMP_ECHO;
106   echo->code = 0;
107   echo->reserved = 0;
108   echo->checksum = 0;
109   echo->checksum = htons(calc_checksum((uint16_t*)echo, sizeof (struct icmp_packet)));
110 }
111
112
113 /**
114  * Send an ICMP message to the dummy IP.
115  *
116  * @param my_ip source address (our ip address)
117  */
118 static void
119 send_icmp_echo (const struct in_addr *my_ip)
120 {
121   struct icmp_packet icmp_echo;
122   struct sockaddr_in dst;
123   size_t off;
124   int err;
125   struct ip_packet ip_pkt;
126   struct icmp_packet icmp_pkt;
127   char packet[sizeof (ip_pkt) + sizeof (icmp_pkt)];
128
129   off = 0;
130   memset(&ip_pkt, 0, sizeof(ip_pkt));
131   ip_pkt.vers_ihl = 0x45;
132   ip_pkt.tos = 0;
133   ip_pkt.pkt_len = sizeof (packet);
134   ip_pkt.id = 1;
135   ip_pkt.flags_frag_offset = 0;
136   ip_pkt.ttl = IPDEFTTL;
137   ip_pkt.proto = IPPROTO_ICMP;
138   ip_pkt.checksum = 0; 
139   ip_pkt.src_ip = my_ip->s_addr;
140   ip_pkt.dst_ip = dummy.s_addr;
141   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
142   memcpy (packet, &ip_pkt, sizeof (ip_pkt));
143   off += sizeof (ip_pkt);
144   make_echo (my_ip, &icmp_echo);
145   memcpy (&packet[off], &icmp_echo, sizeof (icmp_echo));
146   off += sizeof (icmp_echo);
147  
148   memset (&dst, 0, sizeof (dst));
149   dst.sin_family = AF_INET;
150   dst.sin_addr = dummy;
151   err = sendto(rawsock, 
152                packet, off, 0, 
153                (struct sockaddr*)&dst, 
154                sizeof(dst));
155   if (err < 0) 
156     {
157       fprintf(stderr,
158               "sendto failed: %s\n", strerror(errno));
159     }
160   else if (err != off) 
161     {
162       fprintf(stderr,
163               "Error: partial send of ICMP message\n");
164     }
165 }
166
167
168 static void
169 process_icmp_response ()
170 {
171   char buf[65536];
172   ssize_t have;
173   struct in_addr sip;
174   struct ip_packet ip_pkt;
175   struct icmp_packet icmp_pkt;
176   size_t off;
177   
178   have = read (icmpsock, buf, sizeof (buf));
179   if (have == -1)
180     {
181       fprintf (stderr,
182                "Error reading raw socket: %s\n",
183                strerror (errno));
184       return; 
185     }
186   if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2)
187     {
188       fprintf (stderr,
189                "Received ICMP message of unexpected size: %u bytes\n",
190                (unsigned int) have);
191       return;
192     }
193   off = 0;
194   memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
195   off += sizeof (ip_pkt);
196   memcpy (&icmp_pkt, &buf[off], sizeof (icmp_pkt));
197   off += sizeof (icmp_pkt);
198   if ( (ip_pkt.proto != IPPROTO_ICMP) ||
199        (icmp_pkt.type != ICMP_TIME_EXCEEDED) || 
200        (icmp_pkt.code != 0) )
201     {
202       /* maybe we got an actual reply back... */
203       return;    
204     }
205   memcpy(&sip, 
206          &ip_pkt.src_ip, 
207          sizeof (sip));
208   fprintf (stdout,
209            "%s\n",
210            inet_ntop (AF_INET,
211                       &sip,
212                       buf,
213                       sizeof (buf)));
214 }
215
216
217 static int
218 make_icmp_socket ()
219 {
220   int ret;
221
222   ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
223   if (-1 == ret)
224     {
225       fprintf (stderr,
226                "Error opening RAW socket: %s\n",
227                strerror (errno));
228       return -1;
229     }  
230   if (ret >= FD_SETSIZE) 
231     {
232       fprintf (stderr,
233                "Socket number too large (%d > %u)\n",
234                ret,
235                (unsigned int) FD_SETSIZE);
236       close (ret);
237       return -1;
238     }
239   return ret;
240 }
241
242
243 static int
244 make_raw_socket ()
245 {
246   const int one = 1;
247   int ret;
248
249   ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
250   if (-1 == ret)
251     {
252       fprintf (stderr,
253                "Error opening RAW socket: %s\n",
254                strerror (errno));
255       return -1;
256     }  
257   if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
258                  (char *)&one, sizeof(one)) == -1)
259     fprintf(stderr,
260             "setsockopt failed: %s\n",
261             strerror (errno));
262   if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
263                  (char *)&one, sizeof(one)) == -1)
264     fprintf(stderr,
265             "setsockopt failed: %s\n",
266             strerror (errno));
267   return ret;
268 }
269
270
271 int
272 main (int argc, char *const *argv)
273 {
274   struct in_addr external;
275   fd_set rs;
276   struct timeval tv;
277   uid_t uid;
278
279   if (-1 == (icmpsock = make_icmp_socket()))
280     return 1; 
281   if (-1 == (rawsock = make_raw_socket()))
282     {
283       close (icmpsock);
284       return 1; 
285     }
286   uid = getuid ();
287   if (0 != setresuid (uid, uid, uid))
288     fprintf (stderr,
289              "Failed to setresuid: %s\n",
290              strerror (errno));    
291   if (argc != 2)
292     {
293       fprintf (stderr,
294                "This program must be started with our external IP as the only argument.\n");
295       return 1;
296     }
297   if (1 != inet_pton (AF_INET, argv[1], &external))
298     {
299       fprintf (stderr,
300                "Error parsing IPv4 address: %s\n",
301                strerror (errno));
302       return 1;
303     }
304   inet_pton (AF_INET, DUMMY_IP, &dummy);
305   while (1)
306     {
307       FD_ZERO (&rs);
308       FD_SET (icmpsock, &rs);
309       tv.tv_sec = 0;
310       tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000; 
311       select (icmpsock + 1, &rs, NULL, NULL, &tv);
312       if (FD_ISSET (icmpsock, &rs))
313         process_icmp_response ();
314       send_icmp_echo (&external);
315     }  
316   return 0;
317 }
318
319
320 /* end of gnunet-nat-server.c */