Mercurial > hg > quicktun
annotate src/common.c @ 8:6d86596d8884
Fixed BSD support, improved randombytes/secret key generation
author | ivo <Ivo@UCIS.nl> |
---|---|
date | Thu, 14 Oct 2010 02:15:55 +0200 |
parents | fd7c60905b13 |
children | 640f620a55cf |
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> | |
7 | 34 #include <sys/socket.h> |
0 | 35 #include <poll.h> |
36 #include <netdb.h> | |
37 #include <stdlib.h> | |
8
6d86596d8884
Fixed BSD support, improved randombytes/secret key generation
ivo <Ivo@UCIS.nl>
parents:
7
diff
changeset
|
38 #include <net/if.h> |
7 | 39 #ifdef linux |
40 #include <linux/if_tun.h> | |
41 #include <linux/if_ether.h> | |
42 #else | |
43 #define ETH_FRAME_LEN 1514 | |
44 #include <net/if_tun.h> | |
45 #endif | |
0 | 46 |
47 #define MAX_PACKET_LEN (ETH_FRAME_LEN+4) //Some space for optional packet information | |
48 | |
49 struct qtsession; | |
50 struct qtproto { | |
51 int encrypted; | |
52 int buffersize_raw; | |
53 int buffersize_enc; | |
54 int offset_raw; | |
55 int offset_enc; | |
56 int (*encode)(struct qtsession* sess, char* raw, char* enc, int len); | |
57 int (*decode)(struct qtsession* sess, char* enc, char* raw, int len); | |
58 int (*init)(struct qtsession* sess); | |
59 int protocol_data_size; | |
60 }; | |
61 struct qtsession { | |
62 struct qtproto protocol; | |
63 void* protocol_data; | |
64 int fd_socket; | |
65 int fd_dev; | |
66 int remote_float; | |
67 struct sockaddr_in remote_addr; | |
68 }; | |
69 | |
70 #ifdef COMBINED_BINARY | |
71 extern char* (*getconf)(const char*); | |
72 extern int errorexit(const char*); | |
73 extern int errorexitp(const char*); | |
74 extern void print_header(); | |
75 extern void hex2bin(unsigned char*, unsigned char*, int); | |
76 #else | |
77 | |
78 char* (*getconf)(const char*) = getenv; | |
79 | |
80 int errorexit(const char* text) { | |
81 fprintf(stderr, "%s\n", text); | |
82 return -1; | |
83 } | |
84 int errorexitp(const char* text) { | |
85 perror(text); | |
86 return -1; | |
87 } | |
88 | |
89 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
|
90 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
|
91 fprintf(stderr, "More information: http://wiki.qontrol.nl/QuickTun\n"); |
0 | 92 } |
93 | |
94 int init_udp(struct qtsession* session) { | |
95 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
|
96 fprintf(stderr, "Initializing UDP socket...\n"); |
0 | 97 int sfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
98 if (sfd < 0) return errorexitp("Could not create UDP socket"); | |
99 struct sockaddr_in udpaddr; | |
100 struct hostent *he; | |
101 udpaddr.sin_family = AF_INET; | |
102 udpaddr.sin_addr.s_addr = INADDR_ANY; | |
103 udpaddr.sin_port = htons(2998); | |
104 if (envval = getconf("LOCAL_ADDRESS")) { | |
105 he = gethostbyname(envval); | |
106 if (!he) return errorexit("bind address lookup failed"); | |
107 else if (!he->h_addr_list[0]) return errorexit("no address to bind to"); | |
108 udpaddr.sin_addr.s_addr = *((unsigned long*)he->h_addr_list[0]); | |
109 udpaddr.sin_family = he->h_addrtype; | |
110 } | |
111 if (envval = getconf("LOCAL_PORT")) { | |
112 udpaddr.sin_port = htons(atoi(envval)); | |
113 } | |
114 if (bind(sfd, (struct sockaddr*)&udpaddr, sizeof(struct sockaddr_in))) return errorexitp("Could not bind socket"); | |
115 if (!(envval = getconf("REMOTE_ADDRESS"))) { | |
116 session->remote_float = 1; | |
117 //return errorexit("Missing REMOTE_ADDRESS"); | |
118 } else { | |
119 session->remote_float = 0; | |
120 he = gethostbyname(envval); | |
121 if (!he) return errorexit("remote address lookup failed"); | |
122 else if (!he->h_addr_list[0]) return errorexit("no address to connect to"); | |
123 udpaddr.sin_family = he->h_addrtype; | |
124 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
|
125 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
|
126 session->remote_float = 1; |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
127 } else { |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
128 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
|
129 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
|
130 } |
b2c7c83a1dda
Accept 0.0.0.0 remote address for float mode
ivo <ivo@UFO-Net.nl>
parents:
0
diff
changeset
|
131 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
|
132 session->remote_addr = udpaddr; |
0 | 133 } |
134 } | |
135 session->fd_socket = sfd; | |
136 return sfd; | |
137 } | |
138 | |
139 int init_tuntap() { | |
140 char* envval; | |
7 | 141 fprintf(stderr, "Initializing tun/tap device...\n"); |
0 | 142 int ttfd; //Tap device file descriptor |
7 | 143 #ifdef linux |
144 struct ifreq ifr; //required for tun/tap setup | |
145 memset(&ifr, 0, sizeof(ifr)); | |
146 if ((ttfd = open("/dev/net/tun", O_RDWR)) < 0) return errorexitp("Could not open tun/tap device file"); | |
147 if (envval = getconf("INTERFACE")) strcpy(ifr.ifr_name, envval); | |
148 ifr.ifr_flags = getconf("TUN_MODE") ? IFF_TUN : IFF_TAP; | |
149 ifr.ifr_flags |= getconf("USE_PI") ? 0 : IFF_NO_PI; | |
150 if (ioctl(ttfd, TUNSETIFF, (void *)&ifr) < 0) return errorexitp("TUNSETIFF ioctl failed"); | |
151 #else | |
152 if (!(envval = getconf("INTERFACE"))) envval = "/dev/tun0"; | |
153 if ((ttfd = open(envval, O_RDWR)) < 0) return errorexitp("Could not open tun device file"); | |
154 #endif | |
0 | 155 return ttfd; |
156 } | |
157 | |
158 void hex2bin(unsigned char* dest, unsigned char* src, int count) { | |
159 int i; | |
160 for (i = 0; i < count; i++) { | |
161 if (*src >= '0' && *src <= '9') *dest = *src - '0'; | |
162 else if (*src >= 'a' && * src <='f') *dest = *src - 'a' + 10; | |
163 else if (*src >= 'A' && * src <='F') *dest = *src - 'A' + 10; | |
164 src++; *dest = *dest << 4; | |
165 if (*src >= '0' && *src <= '9') *dest += *src - '0'; | |
166 else if (*src >= 'a' && *src <= 'f') *dest += *src - 'a' + 10; | |
167 else if (*src >= 'A' && *src <= 'F') *dest += *src - 'A' + 10; | |
168 src++; dest++; | |
169 } | |
170 } | |
171 | |
172 int qtrun(struct qtproto* p) { | |
173 struct qtsession session; | |
174 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
|
175 |
0 | 176 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
|
177 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
|
178 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
|
179 |
0 | 180 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
|
181 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
|
182 if (ttfd == -1) return -1; |
0 | 183 |
184 char protocol_data[p->protocol_data_size]; | |
185 session.protocol_data = &protocol_data; | |
186 if (p->init) p->init(&session); | |
187 | |
6
cf9b44b46be5
Use stderr for output instead of stdout, added debugging code to nacltai
root <root@Really.UFO-Net.nl>
parents:
4
diff
changeset
|
188 fprintf(stderr, "The tunnel is now operational!\n"); |
0 | 189 |
190 struct pollfd fds[2]; | |
191 fds[0].fd = ttfd; | |
192 fds[0].events = POLLIN; | |
193 fds[1].fd = sfd; | |
194 fds[1].events = POLLIN; | |
195 | |
196 struct sockaddr_in recvaddr; | |
197 | |
198 char buffer_raw_a[p->buffersize_raw]; | |
199 char buffer_enc_a[p->buffersize_enc]; | |
200 char* buffer_raw = buffer_raw_a; | |
201 char* buffer_enc = buffer_enc_a; | |
202 | |
203 while (1) { | |
204 int len = poll(fds, 2, -1); | |
205 if (len < 0) return errorexitp("poll error"); | |
206 else if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) return errorexit("poll error on tap device"); | |
207 else if (fds[1].revents & (POLLHUP | POLLNVAL)) return errorexit("poll error on udp socket"); | |
208 if (fds[0].revents & POLLIN) { | |
209 if (session.remote_float == 0 || session.remote_float == 2) { | |
210 len = read(ttfd, buffer_raw + p->offset_raw, p->buffersize_raw); | |
211 len = p->encode(&session, buffer_raw, buffer_enc, len); | |
212 if (len < 0) return len; | |
213 if (session.remote_float == 0) { | |
214 write(sfd, buffer_enc + p->offset_enc, len); | |
215 } else { | |
216 sendto(sfd, buffer_enc + p->offset_enc, len, 0, (struct sockaddr*)&session.remote_addr, sizeof(session.remote_addr)); | |
217 } | |
218 } | |
219 } | |
220 if (fds[1].revents & POLLIN) { | |
221 socklen_t recvaddr_len = sizeof(recvaddr); | |
222 if (session.remote_float == 0) { | |
223 len = read(sfd, buffer_enc + p->offset_enc, p->buffersize_enc); | |
224 } else { | |
225 len = recvfrom(sfd, buffer_enc + p->offset_enc, p->buffersize_enc, 0, (struct sockaddr*)&recvaddr, &recvaddr_len); | |
226 } | |
227 if (len < 0) { | |
228 int out; | |
229 len = 4; | |
230 getsockopt(sfd, SOL_SOCKET, SO_ERROR, &out, &len); | |
231 fprintf(stderr, "End of file on udp socket"); | |
232 } else { | |
233 len = p->decode(&session, buffer_enc, buffer_raw, len); | |
234 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 | 235 fprintf(stderr, "Remote endpoint has changed to %08X:%d\n", recvaddr.sin_addr, ntohs(recvaddr.sin_port)); |
0 | 236 session.remote_addr = recvaddr; |
237 session.remote_float = 2; | |
238 } | |
239 if (len < 0) return len; | |
240 write(ttfd, buffer_raw + p->offset_raw, len); | |
241 } | |
242 } | |
243 } | |
244 return 0; | |
245 } | |
246 #endif |