ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn_tcp.C
(Generate patch)

Comparing gvpe/src/vpn_tcp.C (file contents):
Revision 1.1 by pcg, Sat Apr 5 02:32:40 2003 UTC vs.
Revision 1.8 by pcg, Tue Oct 14 15:48:15 2003 UTC

29 29
30#include <sys/types.h> 30#include <sys/types.h>
31#include <sys/socket.h> 31#include <sys/socket.h>
32#include <sys/poll.h> 32#include <sys/poll.h>
33#include <sys/wait.h> 33#include <sys/wait.h>
34#include <netinet/in.h>
35#include <sys/uio.h> 34#include <sys/uio.h>
36#include <arpa/inet.h>
37#include <errno.h> 35#include <errno.h>
38#include <time.h> 36#include <time.h>
39#include <unistd.h> 37#include <unistd.h>
40 38
41#include <map> 39#include <map>
42#include <unistd.h> 40#include <unistd.h>
43#include <fcntl.h> 41#include <fcntl.h>
44#include <sys/poll.h> 42#include <sys/poll.h>
45 43
44#include "netcompat.h"
45
46#include "vpn.h" 46#include "vpn.h"
47
48#if ENABLE_HTTP_PROXY
49# include "conf.h"
50#endif
47 51
48struct tcp_connection; 52struct tcp_connection;
49 53
50struct lt_sockinfo 54struct lt_sockinfo
51{ 55{
68struct tcp_connection : io_watcher { 72struct tcp_connection : io_watcher {
69 tstamp last_activity; 73 tstamp last_activity;
70 const sockinfo si; 74 const sockinfo si;
71 vpn &v; 75 vpn &v;
72 bool active; // this connection has been actively established 76 bool active; // this connection has been actively established
73 enum { ERROR, IDLE, CONNECTING, ESTABLISHED } state; 77 enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state;
74 78
75 vpn_packet *r_pkt; 79 vpn_packet *r_pkt;
76 u32 r_len, r_ofs; 80 u32 r_len, r_ofs;
77 81
82 vpn_packet *w_pkt;
83 u32 w_len, w_ofs;
84
85#if ENABLE_HTTP_PROXY
86 char *proxy_req;
87 int proxy_req_len;
88#endif
89
78 void tcpv4_ev (io_watcher &w, short revents); 90 void tcpv4_ev (io_watcher &w, short revents);
79 91
80 void send_packet (vpn_packet *pkt, int tos); 92 bool send_packet (vpn_packet *pkt, int tos);
93 bool write_packet ();
81 94
82 void error (); // abort conenction && cleanup 95 void error (); // abort conenction && cleanup
83 96
84 operator tcp_si_map::value_type() 97 operator tcp_si_map::value_type()
85 { 98 {
128 tcp_si.insert (*i); 141 tcp_si.insert (*i);
129 } 142 }
130 } 143 }
131} 144}
132 145
133void 146bool
134vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 147vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
135{ 148{
136 tcp_si_map::iterator info = tcp_si.find (&si); 149 tcp_si_map::iterator info = tcp_si.find (&si);
137 150
138 tcp_connection *i; 151 tcp_connection *i;
143 tcp_si.insert (*i); 156 tcp_si.insert (*i);
144 } 157 }
145 else 158 else
146 i = info->second; 159 i = info->second;
147 160
148 i->send_packet (pkt, tos); 161 return i->send_packet (pkt, tos);
162}
163
164bool
165tcp_connection::write_packet ()
166{
167 ssize_t len;
168
169 if (w_ofs < 2)
170 {
171 u16 plen = htons (w_pkt->len);
172
173 iovec vec[2];
174 //TODO: char* is the right type? hardly...
175 vec[0].iov_base = (char *)((u8 *)&plen) + w_ofs;
176 vec[0].iov_len = 2 - w_ofs;
177 vec[1].iov_base = (char *)&((*w_pkt)[0]);
178 vec[1].iov_len = w_len - 2;
179
180 len = writev (fd, vec, 2);
181 }
182 else
183 len = write (fd, &((*w_pkt)[w_ofs - 2]), w_len);
184
185 if (len > 0)
186 {
187 w_ofs += len;
188 w_len -= len;
189
190 return w_len == 0;
191 }
192 else if (len < 0 && (errno == EAGAIN || errno == EINTR))
193 return false;
194 else
195 {
196 error ();
197 return false;
198 }
199}
200
201void
202tcp_connection::tcpv4_ev (io_watcher &w, short revents)
203{
204 last_activity = NOW;
205
206 if (revents & (POLLERR | POLLHUP))
207 {
208 error ();
209 return;
210 }
211
212 if (revents & POLLOUT)
213 {
214 if (state == CONNECTING)
215 {
216 state = ESTABLISHED;
217 set (POLLIN);
218#if ENABLE_HTTP_PROXY
219 if (::conf.proxy_host && ::conf.proxy_port)
220 {
221 state = CONNECTING_PROXY;
222 write (fd, proxy_req, proxy_req_len);
223 free (proxy_req); proxy_req = 0;
224 }
225#endif
226 }
227 else if (state == ESTABLISHED)
228 {
229 if (w_pkt)
230 {
231 if (write_packet ())
232 {
233 delete w_pkt; w_pkt = 0;
234
235 set (POLLIN);
236 }
237 }
238 else
239 set (POLLIN);
240 }
241 else
242 set (POLLIN);
243 }
244
245 if (revents & POLLIN)
246 {
247 if (state == ESTABLISHED)
248 for (;;)
249 {
250 if (!r_pkt)
251 {
252 r_pkt = new vpn_packet;
253 r_ofs = 0;
254 r_len = 2; // header
255 }
256
257 ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len);
258
259 if (len > 0)
260 {
261 r_len -= len;
262 r_ofs += len;
263
264 if (r_len == 0)
265 {
266 if (r_ofs == 2)
267 {
268 r_len = ntohs (*(u16 *)&((*r_pkt)[0]));
269 r_pkt->len = r_len;
270
271 if (r_len > 0 && r_len < MAXSIZE)
272 continue;
273 }
274 else
275 {
276 v.recv_vpn_packet (r_pkt, si);
277 delete r_pkt;
278 r_pkt = 0;
279
280 continue;
281 }
282 }
283 else
284 break;
285 }
286 else if (len < 0 && (errno == EINTR || errno == EAGAIN))
287 break;
288
289 error ();
290 break;
291 }
292#if ENABLE_HTTP_PROXY
293 else if (state == CONNECTING_PROXY)
294 {
295 fcntl (fd, F_SETFL, 0);
296 char r[1024];
297 int i;
298 bool emptyline = false;
299
300 // we do a blocking read of the response, to hell with it
301 for (i = 0; i < 1023; i++)
302 {
303 int l = read (fd, &r[i], 1);
304
305 if (l <= 0)
306 {
307 error ();
308 return;
309 }
310
311 if (r[i] == '\012')
312 {
313 if (emptyline)
314 break;
315 else
316 emptyline = true;
317 }
318 else if (r[i] != '\015')
319 emptyline = false;
320 }
321
322 fcntl (fd, F_SETFL, O_NONBLOCK);
323
324 if (i < 12)
325 {
326 slog (L_ERR, _("(%s): unable to do proxy-forwarding, short response"),
327 (const char *)si);
328 error ();
329 }
330 else if (r[0] != 'H' || r[1] != 'T' || r[2] != 'T' || r[3] != 'P' || r[4] != '/'
331 || r[5] != '1' // http-major
332 || r[9] != '2') // response
333 {
334 slog (L_ERR, _("(%s): malformed or unexpected proxy response (%.12s)"),
335 (const char *)si, r);
336 error ();
337 }
338 else
339 state = ESTABLISHED;
340 }
341#endif
342 }
343}
344
345bool
346tcp_connection::send_packet (vpn_packet *pkt, int tos)
347{
348 last_activity = NOW;
349
350 if (state == IDLE)
351 {
352 // woaw, the first lost packet ;)
353 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
354
355 if (fd >= 0)
356 {
357 const sockinfo *csi = &si;
358
359#if ENABLE_HTTP_PROXY
360 sockinfo psi;
361
362 if (::conf.proxy_host && ::conf.proxy_port)
363 {
364 psi.set (::conf.proxy_host, ::conf.proxy_port, PROT_TCPv4);
365
366 if (psi.valid ())
367 {
368 csi = &psi;
369
370 proxy_req_len = asprintf (&proxy_req,
371 "CONNECT %s:%d HTTP/1.0\015\012"
372 "%s%s%s" // optional proxy-auth
373 "\015\012",
374 si.ntoa (),
375 ntohs (si.port),
376 ::conf.proxy_auth ? "Proxy-Authorization: Basic " : "",
377 ::conf.proxy_auth ? ::conf.proxy_auth : "",
378 ::conf.proxy_auth ? "\015\012" : "");
379
380 }
381 else
382 slog (L_ERR, _("unable to resolve http proxy hostname '%s', trying direct"),
383 ::conf.proxy_host);
384 }
385#endif
386
387 fcntl (fd, F_SETFL, O_NONBLOCK);
388
389 if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0
390 || errno == EINPROGRESS)
391 {
392 state = CONNECTING;
393 start (fd, POLLOUT);
394 }
395 else
396 close (fd);
397 }
398 }
399 else if (state == ESTABLISHED)
400 {
401 // drop packet if the tcp write buffer is full. this *is* the
402 // right thing to do, not using tcp *is* the right thing to do.
403 if (!w_pkt)
404 {
405 // how this maps to the underlying tcp packets we don't know
406 // and we don't care. at least we tried ;)
407#if defined(SOL_IP) && defined(IP_TOS)
408 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos);
409#endif
410
411 w_pkt = pkt;
412 w_ofs = 0;
413 w_len = pkt->len + 2; // length + size header
414
415 if (write_packet ())
416 w_pkt = 0;
417 else
418 {
419 w_pkt = new vpn_packet;
420 w_pkt->set (*pkt);
421
422 set (POLLIN | POLLOUT);
423 }
424 }
425 }
426
427 return state != ERROR;
149} 428}
150 429
151void tcp_connection::error () 430void tcp_connection::error ()
152{ 431{
153 if (fd >= 0) 432 if (fd >= 0)
154 { 433 {
155 close (fd); 434 close (fd);
156 fd = -1; 435 fd = -1;
157 } 436 }
158 437
159 delete r_pkt; 438 delete r_pkt; r_pkt = 0;
160 r_pkt = 0; 439 delete w_pkt; w_pkt = 0;
440#if ENABLE_HTTP_PROXY
441 free (proxy_req); proxy_req = 0;
442#endif
161 443
162 stop (); 444 stop ();
163 state = active ? IDLE : ERROR; 445 state = active ? IDLE : ERROR;
164} 446}
165 447
166void
167tcp_connection::tcpv4_ev (io_watcher &w, short revents)
168{
169 last_activity = NOW;
170
171 if (revents & (POLLERR | POLLHUP))
172 error ();
173 else if (revents & POLLOUT && state == CONNECTING)
174 {
175 state = ESTABLISHED;
176 set (POLLIN);
177 }
178
179 else if (revents & POLLIN)
180 {
181 for (;;)
182 {
183 if (!r_pkt)
184 {
185 r_pkt = new vpn_packet;
186 r_ofs = 0;
187 r_len = 2; // header
188 }
189
190 ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len);
191
192 if (len > 0)
193 {
194 r_len -= len;
195 r_ofs += len;
196
197 if (r_len == 0)
198 {
199 if (r_ofs == 2)
200 {
201 r_len = ntohs (*(u16 *)&((*r_pkt)[0]));
202 r_pkt->len = r_len;
203
204 if (r_len > 0 && r_len < MAXSIZE)
205 continue;
206 }
207 else
208 {
209 v.recv_vpn_packet (r_pkt, si);
210 delete r_pkt;
211 r_pkt = 0;
212
213 continue;
214 }
215 }
216
217 }
218 else if (len < 0 && (errno == EINTR || errno == EAGAIN))
219 return;
220
221 error ();
222 return;
223 }
224 }
225}
226
227void
228tcp_connection::send_packet (vpn_packet *pkt, int tos)
229{
230 last_activity = NOW;
231
232 if (state == IDLE)
233 {
234 // woaw, the first lost packet ;)
235 fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
236
237 if (fd >= 0)
238 {
239 fcntl (fd, F_SETFL, O_NONBLOCK);
240
241 if (connect (fd, si.sav4 (), si.salenv4 ()) >= 0
242 || errno == EINPROGRESS)
243 {
244 state = CONNECTING;
245 start (fd, POLLOUT);
246 }
247 else
248 close (fd);
249 }
250 }
251 else if (state == ESTABLISHED)
252 {
253 // how this maps to the underlying tcp packet we don't know
254 // and we don't care. at least we tried ;)
255 setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos);
256
257 // we use none of the advantages of tcp; if an error occurs, just drop
258 // (this happens when a tcp connection gets stuck, too, which might not be
259 // the wisest thing to do.. either drop packet (too late) or make sure
260 // it gets delivered)
261 u16 len = htons (pkt->len);
262
263 iovec vec[2];
264 vec[0].iov_base = &len;
265 vec[0].iov_len = sizeof len;
266 vec[1].iov_base = &((*pkt)[0]);
267 vec[1].iov_len = pkt->len;
268
269 if (sizeof (u16) + pkt->len != writev (fd, vec, 2))
270 error ();
271 }
272}
273
274tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) 448tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_)
275: v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev) 449: v(v_), si(si_), io_watcher(this, &tcp_connection::tcpv4_ev)
276{ 450{
277 last_activity = NOW; 451 last_activity = NOW;
278 r_pkt = 0; 452 r_pkt = 0;
453 w_pkt = 0;
279 fd = fd_; 454 fd = fd_;
455#if ENABLE_HTTP_PROXY
456 proxy_req = 0;
457#endif
280 458
281 if (fd < 0) 459 if (fd < 0)
282 { 460 {
283 active = true; 461 active = true;
284 state = IDLE; 462 state = IDLE;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines