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.2 by pcg, Sun Apr 6 18:12:18 2003 UTC vs.
Revision 1.9 by pcg, Wed Oct 15 00:41:59 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines