Mercurial > hg > quicktun
annotate src/common.c @ 6:cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
author | root <root@Really.UFO-Net.nl> |
---|---|
date | Fri, 08 Oct 2010 23:29:56 +0000 |
parents | a989ecbd5f53 |
children | fd7c60905b13 |
rev | line source |
---|---|
0 | 1 /* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved. |
2 Redistribution and use in source and binary forms, with or without modification, are | |
3 permitted provided that the following conditions are met: | |
4 | |
5 1. Redistributions of source code must retain the above copyright notice, this list of | |
6 conditions and the following disclaimer. | |
7 | |
8 2. Redistributions in binary form must reproduce the above copyright notice, this list | |
9 of conditions and the following disclaimer in the documentation and/or other materials | |
10 provided with the distribution. | |
11 | |
12 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
13 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
14 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR | |
15 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
16 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
17 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
18 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
19 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
20 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
21 | |
22 The views and conclusions contained in the software and documentation are those of the | |
23 authors and should not be interpreted as representing official policies, either expressed | |
24 or implied, of Ivo Smits.*/ | |
25 | |
26 #include <stdio.h> | |
27 #include <stdlib.h> | |
28 #include <string.h> | |
29 #include <fcntl.h> | |
30 #ifndef HAVE_NETINET_IN_H | |
31 #include <netinet/in.h> | |
32 #endif | |
33 #include <sys/ioctl.h> | |
34 #include <linux/if.h> | |
35 #include <linux/if_tun.h> | |
36 #include <linux/if_ether.h> | |
37 #include <poll.h> | |
38 #include <netdb.h> | |
39 #include <stdlib.h> | |
40 | |
41 #define MAX_PACKET_LEN (ETH_FRAME_LEN+4) //Some space for optional packet information | |
42 | |
43 struct qtsession; | |
44 struct qtproto { | |
45 int encrypted; | |
46 int buffersize_raw; | |
47 int buffersize_enc; | |
48 int offset_raw; | |
49 int offset_enc; | |
50 int (*encode)(struct qtsession* sess, char* raw, char* enc, int len); | |
51 int (*decode)(struct qtsession* sess, char* enc, char* raw, int len); | |
52 int (*init)(struct qtsession* sess); | |
53 int protocol_data_size; | |
54 }; | |
55 struct qtsession { | |
56 struct qtproto protocol; | |
57 void* protocol_data; | |
58 int fd_socket; | |
59 int fd_dev; | |
60 int remote_float; | |
61 struct sockaddr_in remote_addr; | |
62 }; | |
63 | |
64 #ifdef COMBINED_BINARY | |
65 extern char* (*getconf)(const char*); | |
66 extern int errorexit(const char*); | |
67 extern int errorexitp(const char*); | |
68 extern void print_header(); | |
69 extern void hex2bin(unsigned char*, unsigned char*, int); | |
70 #else | |
71 | |
72 char* (*getconf)(const char*) = getenv; | |
73 | |
74 int errorexit(const char* text) { | |
75 fprintf(stderr, "%s\n", text); | |
76 return -1; | |
77 } | |
78 int errorexitp(const char* text) { | |
79 perror(text); | |
80 return -1; | |
81 } | |
82 | |
83 void print_header() { | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
84 fprintf(stderr, "UCIS QuickTun (c) 2010 Ivo Smits <Ivo@UCIS.nl>\n"); |
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
85 fprintf(stderr, "More information: http://wiki.qontrol.nl/QuickTun\n"); |
0 | 86 } |
87 | |
88 int init_udp(struct qtsession* session) { | |
89 char* envval; | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
90 fprintf(stderr, "Initializing UDP socket...\n"); |
0 | 91 int sfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
92 if (sfd < 0) return errorexitp("Could not create UDP socket"); | |
93 struct sockaddr_in udpaddr; | |
94 struct hostent *he; | |
95 udpaddr.sin_family = AF_INET; | |
96 udpaddr.sin_addr.s_addr = INADDR_ANY; | |
97 udpaddr.sin_port = htons(2998); | |
98 if (envval = getconf("LOCAL_ADDRESS")) { | |
99 he = gethostbyname(envval); | |
100 if (!he) return errorexit("bind address lookup failed"); | |
101 else if (!he->h_addr_list[0]) return errorexit("no address to bind to"); | |
102 udpaddr.sin_addr.s_addr = *((unsigned long*)he->h_addr_list[0]); | |
103 udpaddr.sin_family = he->h_addrtype; | |
104 } | |
105 if (envval = getconf("LOCAL_PORT")) { | |
106 udpaddr.sin_port = htons(atoi(envval)); | |
107 } | |
108 if (bind(sfd, (struct sockaddr*)&udpaddr, sizeof(struct sockaddr_in))) return errorexitp("Could not bind socket"); | |
109 if (!(envval = getconf("REMOTE_ADDRESS"))) { | |
110 session->remote_float = 1; | |
111 //return errorexit("Missing REMOTE_ADDRESS"); | |
112 } else { | |
113 session->remote_float = 0; | |
114 he = gethostbyname(envval); | |
115 if (!he) return errorexit("remote address lookup failed"); | |
116 else if (!he->h_addr_list[0]) return errorexit("no address to connect to"); | |
117 udpaddr.sin_family = he->h_addrtype; | |
118 udpaddr.sin_addr.s_addr = *((unsigned long*)he->h_addr_list[0]); | |
2
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
119 if (udpaddr.sin_addr.s_addr == 0) { |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
120 session->remote_float = 1; |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
121 } else { |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
122 if (envval = getconf("REMOTE_PORT")) { |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
123 udpaddr.sin_port = htons(atoi(envval)); |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
124 } |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
125 if (connect(sfd, (struct sockaddr*)&udpaddr, sizeof(struct sockaddr_in))) return errorexitp("Could not connect socket"); |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
126 session->remote_addr = udpaddr; |
0 | 127 } |
128 } | |
129 session->fd_socket = sfd; | |
130 return sfd; | |
131 } | |
132 | |
133 int init_tuntap() { | |
134 char* envval; | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
135 fprintf(stderr, "Initializing tap device...\n"); |
0 | 136 int ttfd; //Tap device file descriptor |
137 struct ifreq ifr; //required for tun/tap setup | |
138 memset(&ifr, 0, sizeof(ifr)); | |
139 if ((ttfd = open("/dev/net/tun", O_RDWR)) < 0) return errorexitp("Could not open tap device file"); | |
140 if (envval = getconf("INTERFACE")) strcpy(ifr.ifr_name, envval); | |
141 ifr.ifr_flags = getconf("TUN_MODE") ? IFF_TUN : IFF_TAP; | |
142 ifr.ifr_flags |= getconf("USE_PI") ? 0 : IFF_NO_PI; | |
143 if (ioctl(ttfd, TUNSETIFF, (void *)&ifr) < 0) return errorexitp("TUNSETIFF ioctl failed"); | |
144 return ttfd; | |
145 } | |
146 | |
147 void hex2bin(unsigned char* dest, unsigned char* src, int count) { | |
148 int i; | |
149 for (i = 0; i < count; i++) { | |
150 if (*src >= '0' && *src <= '9') *dest = *src - '0'; | |
151 else if (*src >= 'a' && * src <='f') *dest = *src - 'a' + 10; | |
152 else if (*src >= 'A' && * src <='F') *dest = *src - 'A' + 10; | |
153 src++; *dest = *dest << 4; | |
154 if (*src >= '0' && *src <= '9') *dest += *src - '0'; | |
155 else if (*src >= 'a' && *src <= 'f') *dest += *src - 'a' + 10; | |
156 else if (*src >= 'A' && *src <= 'F') *dest += *src - 'A' + 10; | |
157 src++; dest++; | |
158 } | |
159 } | |
160 | |
161 int qtrun(struct qtproto* p) { | |
162 struct qtsession session; | |
163 session.protocol = *p; | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
164 |
0 | 165 init_udp(&session); |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
166 int sfd = session.fd_socket; |
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
167 if (sfd == -1) return -1; |
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
168 |
0 | 169 session.fd_dev = init_tuntap(); |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
170 int ttfd = session.fd_dev; |
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
171 if (ttfd == -1) return -1; |
0 | 172 |
173 char protocol_data[p->protocol_data_size]; | |
174 session.protocol_data = &protocol_data; | |
175 if (p->init) p->init(&session); | |
176 | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
177 fprintf(stderr, "The tunnel is now operational!\n"); |
0 | 178 |
179 struct pollfd fds[2]; | |
180 fds[0].fd = ttfd; | |
181 fds[0].events = POLLIN; | |
182 fds[1].fd = sfd; | |
183 fds[1].events = POLLIN; | |
184 | |
185 struct sockaddr_in recvaddr; | |
186 | |
187 char buffer_raw_a[p->buffersize_raw]; | |
188 char buffer_enc_a[p->buffersize_enc]; | |
189 char* buffer_raw = buffer_raw_a; | |
190 char* buffer_enc = buffer_enc_a; | |
191 | |
192 while (1) { | |
193 int len = poll(fds, 2, -1); | |
194 if (len < 0) return errorexitp("poll error"); | |
195 else if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) return errorexit("poll error on tap device"); | |
196 else if (fds[1].revents & (POLLHUP | POLLNVAL)) return errorexit("poll error on udp socket"); | |
197 if (fds[0].revents & POLLIN) { | |
198 if (session.remote_float == 0 || session.remote_float == 2) { | |
199 len = read(ttfd, buffer_raw + p->offset_raw, p->buffersize_raw); | |
200 len = p->encode(&session, buffer_raw, buffer_enc, len); | |
201 if (len < 0) return len; | |
202 if (session.remote_float == 0) { | |
203 write(sfd, buffer_enc + p->offset_enc, len); | |
204 } else { | |
205 sendto(sfd, buffer_enc + p->offset_enc, len, 0, (struct sockaddr*)&session.remote_addr, sizeof(session.remote_addr)); | |
206 } | |
207 } | |
208 } | |
209 if (fds[1].revents & POLLIN) { | |
210 socklen_t recvaddr_len = sizeof(recvaddr); | |
211 if (session.remote_float == 0) { | |
212 len = read(sfd, buffer_enc + p->offset_enc, p->buffersize_enc); | |
213 } else { | |
214 len = recvfrom(sfd, buffer_enc + p->offset_enc, p->buffersize_enc, 0, (struct sockaddr*)&recvaddr, &recvaddr_len); | |
215 } | |
216 if (len < 0) { | |
217 int out; | |
218 len = 4; | |
219 getsockopt(sfd, SOL_SOCKET, SO_ERROR, &out, &len); | |
220 fprintf(stderr, "End of file on udp socket"); | |
221 } else { | |
222 len = p->decode(&session, buffer_enc, buffer_raw, len); | |
223 if (len != 0 && session.remote_float != 0 && (session.remote_addr.sin_addr.s_addr != recvaddr.sin_addr.s_addr || session.remote_addr.sin_port != recvaddr.sin_port)) { | |
4 | 224 fprintf(stderr, "Remote endpoint has changed to %08X:%d\n", recvaddr.sin_addr, ntohs(recvaddr.sin_port)); |
0 | 225 session.remote_addr = recvaddr; |
226 session.remote_float = 2; | |
227 } | |
228 if (len < 0) return len; | |
229 write(ttfd, buffer_raw + p->offset_raw, len); | |
230 } | |
231 } | |
232 } | |
233 return 0; | |
234 } | |
235 #endif |