Debian Codegraph @

src/FDTunnel.cpp

raw
1 // For all support, instructions and copyright go to:
2
3 // http://e2guardian.org/
4 // Released under the GPL v2, with the OpenSSL exception described in the README file.
5
6 // This class is a generic multiplexing tunnel
7 // that uses blocking poll() to be as efficient as possible. It tunnels
8 // between the two supplied FDs.
9
10 // INCLUDES
11
12 #ifdef HAVE_CONFIG_H
13 #include "e2config.h"
14 #endif
15
16 #include <sys/time.h>
17 #include <unistd.h>
18 #include <stdexcept>
19 #include <cerrno>
20 #include <sys/socket.h>
21 #include <string.h>
22 #include <algorithm>
23 #include <sys/select.h>
24
25 #include "FDTunnel.hpp"
26 #include "Logger.hpp"
27
28 // IMPLEMENTATION
29
30 FDTunnel::FDTunnel()
31 : throughput(0) {
32 }
33
34 void FDTunnel::reset() {
35 throughput = 0;
36 }
37
38 // tunnel data from fdfrom to fdto (unfiltered)
39 // return false if throughput larger than target throughput
40 bool
41 FDTunnel::tunnel(Socket &sockfrom, Socket &sockto, bool twoway, off_t targetthroughput, bool ignore, bool chunked) {
42 if (chunked) {
43 DEBUG_network("tunnelling chunked data.");
44 int maxlen = 32000;
45 timeout = sockfrom.getTimeout();
46 int rd = 0;
47 int total_rd = 0;
48 while ((rd = sockfrom.readChunk(sfbuff, maxlen, timeout)) > 0) {
49 sockto.writeChunk(sfbuff, rd, timeout);
50 total_rd += rd;
51 }
52 sockto.writeChunkTrailer(sockfrom.chunked_trailer);
53 DEBUG_network("tunnelled chunked data.", total_rd, " bytes");
54 throughput = total_rd;
55 return true;
56 }
57
58 throughput = 0;
59
60 bool st_isSsl = sockto.isSsl();
61 bool sf_isSsl = sockfrom.isSsl();
62
63 if (targetthroughput == 0) {
64 DEBUG_network("No data expected, tunnelling aborted.");
65 return true;
66 }
67
68 if (targetthroughput < 0) {
69 DEBUG_network("Tunnelling without known content-length");
70 } else {
71 DEBUG_network("Tunnelling with content length ", targetthroughput);
72 }
73
74
75 int fdfrom, fdto;
76
77 fdfrom = sockfrom.getFD();
78 fdto = sockto.getFD();
79 twayfds[0].fd = fdfrom;
80 twayfds[0].events = 0;
81 twayfds[1].events = 0;
82 twayfds[0].revents = 0; // simplifies 1st loop logic
83 twayfds[1].revents = 0;
84 //if (ignore && !twoway) { // this porb will not work with non-block https
85 // twayfds[1].fd = -1;
86 // twayfds[1].revents = 0;
87 //}
88 //else
89 twayfds[1].fd = fdto;
90
91 char stbuff[32768]; // buffer for the return
92
93 timeout = 120000; // should be made setable in conf files
94 if (twoway) {
95 ignore = false;
96 }
97
98 int sfbuff_cnt = 0;
99 int stbuff_cnt = 0;
100
101 // Functionb now split into different functions for different cases as ssl sockets need treating differently to
102 // normal sockets with different order of operations for each case
103 // Yes, it is untidy, but makes bug-fixing much easier. PP.
104 if (!st_isSsl && !sf_isSsl) { // both sockets not ssl
105 return nots2nots_tunnel(sockfrom, sockto, fdto, fdfrom, twoway, targetthroughput, ignore);
106 }
107 if (sf_isSsl && !st_isSsl) { // from socket is ssl - to is not ssl
108 return iss2nots_tunnel(sockfrom, sockto, fdto, fdfrom, twoway, targetthroughput, ignore);
109 }
110 if (sf_isSsl && st_isSsl) { // from socket is ssl - to is ssl
111 return iss2iss_tunnel(sockfrom, sockto, fdto, fdfrom, twoway, targetthroughput, ignore);
112 }
113
114
115 bool sf_read_wait = true;
116 bool st_read_wait = false;
117 //if (twoway) st_read_wait = true;
118 bool sf_write_wait = false;
119 bool st_write_wait = false;
120
121 short int sf_read_wait_flags = 0;
122 short int st_read_wait_flags = 0;
123 short int sf_write_wait_flags = 0;
124 short int st_write_wait_flags = 0;
125
126 // Note: poll has to be after read (or write) for ssl sockets, but before read (or write) for non-ssl!
127
128 //if (!sf_isSsl) {
129 sf_read_wait = true; // wait for poll before reading
130 sf_read_wait_flags = POLLIN;
131 sf_write_wait_flags = POLLOUT;
132 //}
133 if (twoway) {
134 st_read_wait = true; // wait for poll before reading
135 st_read_wait_flags = POLLIN;
136 st_write_wait_flags = POLLOUT;
137 }
138
139 bool done = false; // so we get past the first while
140
141 // disable poll before read/write = only affects non SSL i/o
142 sockfrom.doCheck = false;
143 sockto.doCheck = false;
144
145 // v5.5 changed socket logic to psuedio non-blocking so that poll is used in MITM
146 // after read/write - PP
147 if ((sockfrom.bufflen - sockfrom.buffstart) > 0) {
148 int to_write = (sockfrom.bufflen - sockfrom.buffstart);
149 if ((targetthroughput > 0) && (targetthroughput < to_write))
150 to_write = targetthroughput;
151 sfbuff_cnt = sockfrom.readFromSocket(sfbuff, to_write, 0, 0);
152 if (sfbuff_cnt < 1) { //should never happen!
153 return true;
154 }
155 DEBUG_network("Data in fdfrom's buffer now in sfbuffer ", to_write, " bytes");
156
157 //throughput += sfbuff_cnt;
158 st_write_wait = true;
159 st_write_wait_flags = POLLOUT;
160 }
161
162
163 while (!done && (targetthroughput > -1 ? throughput < targetthroughput : true)) {
164 done = true; // if we don't make a successful read and write this
165 // flag will stay true and so the while() will exit
166 //DEBUG_network("Start of tunnel loop: throughput:", throughput, " target:", targetthroughput);
167
168 // 1st Try 'from' socket for input if not waiting for write on socket
169 //
170 if ((sfbuff_cnt == 0) && !sf_write_wait) {
171 DEBUG_network("stage 1 - read from");
172 //std::cout <<thread_id << "tunnel got past 131: " << std::endl;
173 if (sf_isSsl && sockfrom.checkForInput(0))
174 sf_read_wait = false;
175 if ((!sf_read_wait)
176 || ((twayfds[0].revents & sf_read_wait_flags) == sf_read_wait_flags)
177 // || (sf_isSsl && sockfrom.checkForInput(0))
178 ) {
179 if (targetthroughput > -1)
180 // we have a target throughput - only read in the exact amount of data we've been told to
181 sfbuff_cnt = sockfrom.readFromSocket(sfbuff,
182 (((int) sizeof(sfbuff) < ((targetthroughput - throughput)))
183 ? sizeof(sfbuff) : (targetthroughput - throughput)), 0, 0,
184 true);
185 else
186 sfbuff_cnt = sockfrom.readFromSocket(sfbuff, sizeof(sfbuff), 0, 0, true);
187
188 DEBUG_network("tunnel got return rom sockfrom:read ", sfbuff_cnt, " bytes");
189
190 if (sfbuff_cnt < 0) {
191 sfbuff_cnt = 0;
192 if (sockfrom.timedout && sf_isSsl) { //do data yet
193 sf_read_wait = true;
194 sf_read_wait_flags = sockfrom.get_wait_flag(false);
195 done = false;
196 DEBUG_network(" sfread_flags ", sf_read_wait_flags);
197 } else if (sockfrom.sockError()) {
198 break; // an error occurred so end the while()
199 }
200 } else if (sfbuff_cnt == 0) {
201 done = true; // none received so pipe is closed so flag it
202 break;
203 } else { // some data read
204 DEBUG_network("tunnel got data from sockfrom: ", sfbuff_cnt, " bytes");
205 // throughput += sfbuff_cnt; // increment our counter used to log
206 DEBUG_network("throughput is ", throughput, " of ", targetthroughput);
207 // if(sf_isSsl) {
208 sf_read_wait = true;
209 // } else {
210 // if(!st_isSsl) {
211 st_write_wait_flags = POLLOUT;
212 st_write_wait = true;
213 // }
214 // }
215 done = false;
216 }
217 }
218 }
219
220 // 2nd try 'to' socket for input
221 // IF twoway get input from 'to' socket if no write waiting on socket
222 // else if ignore not set if any pending input in buffer stop tunneling
223
224 if (twoway) {
225 if ((stbuff_cnt == 0) && !st_write_wait) {
226 DEBUG_network("stage 2 - read to");
227 if (st_isSsl && sockto.checkForInput(0))
228 st_read_wait = false;
229 if ((!st_read_wait) || ((twayfds[1].revents & st_read_wait_flags) == st_read_wait_flags)
230 //|| (st_isSsl && sockto.checkForInput(0))
231 ) {
232
233 DEBUG_network("stage 2 about to read");
234 stbuff_cnt = sockto.readFromSocket(stbuff, sizeof(stbuff), 0, 0, true);
235 DEBUG_network("tunnel got return rom sockto:read ", stbuff_cnt, " bytes");
236
237 if (stbuff_cnt < 0) {
238 stbuff_cnt = 0;
239 if (sockto.timedout && st_isSsl) { //do data yet
240 DEBUG_network(" got timeout");
241 st_read_wait = true;
242 st_read_wait_flags = sockto.get_wait_flag(false);
243 done = false;
244 DEBUG_network(" stread_flags ", st_read_wait_flags);
245 } else if (sockto.sockError()) {
246 break; // an error occurred so end the while()
247 }
248 } else if (stbuff_cnt == 0) {
249 done = true; // none received so pipe is closed so flag it
250 break;
251 } else { // some data read
252
253 DEBUG_network("tunnel got data from sockto: ", stbuff_cnt, " bytes");
254 throughput += stbuff_cnt;
255 // if(st_isSsl) {
256 st_read_wait = false;
257 // } else {
258 // if(!sf_isSsl) {
259 sf_write_wait = true;
260 sf_write_wait_flags = POLLOUT;
261 // }
262 // }
263 done = false;
264 }
265 }
266 }
267 } else { // !twoway = one way
268 if (!ignore) {
269 if (st_isSsl) {
270 if (sockto.s_checkPending() > 0)
271 break;
272 } else { // not ssl
273 if (twayfds[1].revents & POLLIN) // can't use this for ssl as POLLIN may be for a write
274 break;
275
276 }
277 }
278 }
279
280 // 3rd try and write to 'to' socket if any data in buffer
281
282 DEBUG_network("stage 3 - write to");
283 if ((sfbuff_cnt > 0) &&
284 ((!st_write_wait)
285 || ((twayfds[1].revents & st_write_wait_flags) == st_write_wait_flags))) {
286
287 DEBUG_network("stage 3 about to write ");
288 if (!sockto.writeToSocket(sfbuff, sfbuff_cnt, 0, 0)) {
289 if (sockto.timedout && st_isSsl) { //do data yet
290 st_write_wait = true;
291 st_write_wait_flags = sockto.get_wait_flag(true);
292 done = false;
293 } else if (sockto.sockError()) {
294 break; // an error occurred so end the while()
295 }
296 } else { // data written
297 DEBUG_network("tunnel wrote data out: ", sfbuff_cnt, " bytes");
298 st_write_wait = false;
299 throughput += sfbuff_cnt; // increment our counter used to log
300 sfbuff_cnt = 0;
301 if (!sf_isSsl) {
302 sf_read_wait = true;
303 sf_read_wait_flags = POLLIN;
304 }
305 done = false;
306 }
307 }
308
309 // 4th try and write to 'from' socket if any data in buffer
310
311 DEBUG_network("stage 4 - write from");
312 if ((stbuff_cnt > 0) &&
313 ((!sf_write_wait) || ((twayfds[0].revents & sf_write_wait_flags) == sf_write_wait_flags))) {
314
315 DEBUG_network("stage 4 about to write ");
316 if (!sockfrom.writeToSocket(stbuff, stbuff_cnt, 0, 0)) {
317 if (sockfrom.timedout && sf_isSsl) { //do data yet
318 sf_write_wait = true;
319 sf_write_wait_flags = sockfrom.get_wait_flag(true);
320 done = false;
321 } else if (sockfrom.sockError()) {
322 break; // an error occurred so end the while()
323 }
324 } else { // data written
325 DEBUG_network("tunnel wrote data out: ", stbuff_cnt, " bytes");
326 sf_write_wait = false;
327 stbuff_cnt = 0;
328 if (!st_isSsl) {
329 st_read_wait = true;
330 st_read_wait_flags = POLLIN;
331 }
332 done = false;
333 }
334 }
335
336 // 4th Break if either socket is hung up - has to be done after read as data can be pending when hung up
337
338 if ((twayfds[0].revents & POLLHUP) || (twayfds[1].revents & POLLHUP)) {
339 break;
340 }
341
342 DEBUG_network("sf_ww is ", sf_write_wait, " st_ww is ", st_write_wait, " sf_rw is ", sf_read_wait, " st_rw is ",
343 st_read_wait);
344 DEBUG_network("sf_ww_f is ", sf_write_wait_flags, " st_ww_f is ", st_write_wait_flags, " sf_rw_f is ",
345 sf_read_wait_flags, " st_rw_f is ", st_read_wait_flags);
346
347 if (sf_write_wait || st_write_wait || sf_read_wait || st_read_wait) {
348
349 if ((targetthroughput == -1) || (throughput < targetthroughput)) {
350 // 5th set up and do poll
351
352 twayfds[0].events = 0;
353 if (sf_write_wait)
354 twayfds[0].events = sf_write_wait_flags;
355 else if (sf_read_wait)
356 twayfds[0].events = sf_read_wait_flags;
357
358 twayfds[1].events = 0;
359 if (st_write_wait)
360 twayfds[1].events = st_write_wait_flags;
361 else if (st_read_wait)
362 twayfds[1].events = st_read_wait_flags;
363
364 if (twayfds[0].events == 0)
365 twayfds[0].events = POLLIN; // set for read to avoid deadlock
366
367 if (!ignore && (twayfds[1].events == 0))
368 twayfds[1].events = POLLIN; // set for read to avoid deadlock
369
370 int rc = poll(twayfds, 2, timeout);
371 if (rc < 1) {
372 DEBUG_network("tunnel tw poll returned error or timeout::", rc);
373 break; // an error occurred or it timed out so end while()
374 }
375
376 DEBUG_network("tunnel tw poll returned ok:", rc);
377 DEBUG_network("tunnel tw poll returned revents:", twayfds[0].revents, " ", twayfds[1].revents);
378
379 if ((twayfds[0].revents & POLLIN)) {
380 done = false;
381 continue;
382 }
383
384 if ((twayfds[1].revents & POLLIN)) {
385 done = false;
386 continue;
387 }
388
389 if ((twayfds[0].revents & POLLERR) || (twayfds[1].revents & POLLERR)) {
390 break;
391 }
392 done = false;
393 }
394 }
395 }
396
397 // re-enable poll before read/write = only affects non SSL i/o
398 sockfrom.doCheck = true;
399 sockto.doCheck = true;
400
401 if ((throughput >= targetthroughput) && (targetthroughput > -1)) {
402 DEBUG_network("All expected data tunnelled. (expected ", targetthroughput, "; tunnelled ", throughput, ")");
403 } else {
404 DEBUG_network("Tunnel closed.");
405 }
406
407 return (targetthroughput > -1) ? (throughput >= targetthroughput) : true;
408 }
409
410
411 bool FDTunnel::nots2nots_tunnel(Socket &sockfrom, Socket &sockto, int &fdto, int &fdfrom, bool twoway,
412 off_t targetthroughput, bool ignore) {
413 // it is a twoway with both sockets not ssl
414
415 bool done = false; // so we get past the first while
416
417 ssize_t rc = 0;
418
419 tooutfds[0].fd = fdto;
420 tooutfds[0].events = POLLOUT;
421 tooutfds[0].revents = 0;
422 fromoutfds[0].fd = fdfrom;
423 fromoutfds[0].events = POLLOUT;
424 fromoutfds[0].revents = 0;
425 twayfds[0].events = POLLIN;
426 twayfds[0].revents = 0;
427 twayfds[1].revents = 0;
428 if (twoway)
429 twayfds[1].events = POLLIN;
430
431 if ((sockfrom.bufflen - sockfrom.buffstart) > 0) {
432 int to_write = (sockfrom.bufflen - sockfrom.buffstart);
433 if ((targetthroughput > 0) && (targetthroughput < to_write))
434 to_write = targetthroughput;
435 DEBUG_network("Data in fdfrom's buffer; sending ", to_write, " bytes");
436 if (!sockto.writeToSocket(sockfrom.buffer + sockfrom.buffstart, to_write, 0, 120000))
437 return false;
438 DEBUG_network("Data in fdfrom's buffer; sent ", to_write, " bytes");
439
440 throughput += to_write;
441 sockfrom.buffstart += to_write;
442 if (sockfrom.buffstart == sockfrom.bufflen) {
443 sockfrom.bufflen = 0;
444 sockfrom.buffstart = 0;
445 }
446 }
447
448
449 while (!done && (targetthroughput > -1 ? throughput < targetthroughput : true)) {
450 done = true; // if we don't make a sucessful read and write this
451 // flag will stay true and so the while() will exit
452 DEBUG_network("Start of tunnel loop: throughput:", throughput, " target:", targetthroughput);
453 //FD_CLR(fdto, &inset);
454
455 {
456 int rc = poll(twayfds, 2, timeout);
457 if (rc < 1) {
458 DEBUG_network("tunnel tw poll returned error or timeout::", rc);
459 break; // an error occurred or it timed out so end while()
460 }
461 DEBUG_network("tunnel tw poll returned ok:", rc, " twayfds.revents:", twayfds[0].revents, " ",
462 twayfds[1].revents);
463 DEBUG_network("POLLIN is ", POLLIN);
464 }
465
466 if (twayfds[0].revents & (POLLIN | POLLHUP)) {
467 DEBUG_network("Reading from");
468 if (targetthroughput > -1) {
469
470 // we have a target throughput - only read in the exact amount of data we've been told to
471
472 rc = recv(fdfrom, sfbuff,
473 (((int) sizeof(sfbuff) < ((targetthroughput - throughput) /*+2*/)) ? sizeof(sfbuff) : (
474 targetthroughput - throughput)), 0);
475 } else
476
477 rc = recv(fdfrom, sfbuff, sizeof(sfbuff), 0);
478
479 DEBUG_network("read from returned ", rc);
480
481 // read as much as is available
482 if (rc < 0) {
483 break; // an error occurred so end the while()
484 } else if (!rc) {
485 done = true; // none received so pipe is closed so flag it
486 } else { // some data read
487 DEBUG_network("tunnel got data from sockfrom: ", rc, " bytes");
488 throughput += rc; // increment our counter used to log
489 if (poll(tooutfds, 1, timeout) < 1) {
490 break; // an error occurred or timed out so end while()
491 }
492
493 if (tooutfds[0].revents & POLLOUT) {
494
495 rc = send(fdto, sfbuff, rc, 0);
496 if (rc < 1) { // write data
497 break; // was an error writing
498 }
499 DEBUG_network("tunnel wrote data out: ", rc, " bytes");
500 done = false; // flag to say data still to be handled
501 } else {
502 break; // should never get here
503 }
504 }
505 }
506 if (twoway && twayfds[1].revents & (POLLIN | POLLHUP)) {
507 DEBUG_network("Reading to");
508
509
510 // read as much as is available
511
512 rc = recv(fdto, sfbuff, sizeof(sfbuff), 0);
513 DEBUG_network("read to returned", rc);
514
515 if (rc < 0) {
516 DEBUG_network("read to error is", errno);
517 break; // an error occurred so end the while()
518 } else if (!rc) {
519 done = true; // none received so pipe is closed so flag it
520 break;
521 } else { // some data read
522 if (poll(fromoutfds, 1, timeout) < 1) {
523 break; // an error occurred or timed out so end while()
524 }
525
526 if (fromoutfds[0].revents & POLLOUT) {
527 rc = send(fdfrom, sfbuff, rc, 0);
528
529 if (rc < 1) { // write data
530 break; // was an error writing
531 }
532 done = false; // flag to say data still to be handled
533 } else {
534 break; // should never get here
535 }
536 }
537 }
538 }
539 if ((throughput >= targetthroughput) && (targetthroughput > -1)) {
540 DEBUG_network("All expected data tunnelled. (expected ", targetthroughput, "; tunnelled ", throughput, ")");
541 } else {
542 DEBUG_network("Tunnel closed.");
543 }
544 return (targetthroughput > -1) ? (throughput >= targetthroughput) : true;
545 }
546
547
548 bool FDTunnel::iss2nots_tunnel(Socket &sockfrom, Socket &sockto, int &fdto, int &fdfrom, bool twoway,
549 off_t targetthroughput, bool ignore) {
550 // it is a twoway with from socket ssl and to socket not ssl
551
552 bool done = false; // so we get past the first while
553 int l_timeout = timeout;
554
555
556 ssize_t rc = 0;
557
558 tooutfds[0].fd = fdto;
559 tooutfds[0].events = POLLOUT;
560 tooutfds[0].revents = 0;
561 fromoutfds[0].fd = fdfrom;
562 fromoutfds[0].events = POLLOUT;
563 fromoutfds[0].revents = 0;
564 twayfds[0].events = POLLIN;
565 twayfds[0].revents = 0;
566 twayfds[1].revents = 0;
567 if (twoway)
568 twayfds[1].events = POLLIN;
569
570 if ((sockfrom.bufflen - sockfrom.buffstart) > 0) {
571 int to_write = (sockfrom.bufflen - sockfrom.buffstart);
572 if ((targetthroughput > 0) && (targetthroughput < to_write))
573 to_write = targetthroughput;
574 DEBUG_network("Data in fdfrom's buffer; sending ", to_write, " bytes");
575 if (!sockto.writeToSocket(sockfrom.buffer + sockfrom.buffstart, to_write, 0, 120000))
576 return false;
577 DEBUG_network("Data in fdfrom's buffer; sent ", to_write, " bytes");
578
579 throughput += to_write;
580 sockfrom.buffstart += to_write;
581 if (sockfrom.buffstart == sockfrom.bufflen) {
582 sockfrom.bufflen = 0;
583 sockfrom.buffstart = 0;
584 }
585 }
586
587 bool do_poll = true;
588 while (!done && (targetthroughput > -1 ? throughput < targetthroughput : true)) {
589 done = true; // if we don't make a sucessful read and write this
590 // flag will stay true and so the while() will exit
591 DEBUG_network("Start of fromSsl to toNotSsl tunnel loop: throughput:", throughput, " target:",
592 targetthroughput);
593 //FD_CLR(fdto, &inset);
594
595 // 1st check pending before doing poll
596
597
598 {
599 // 1st check pending before doing poll
600 if ((sockfrom.s_checkPending() || (twayfds[0].revents & POLLIN)) && sockfrom.s_checkShutdown() == 0) {
601 DEBUG_network("Reading from");
602 if (targetthroughput > -1) {
603 // we have a target throughput - only read in the exact amount of data we've been told to
604 rc = SSL_read(sockfrom.ssl, sfbuff,
605 (((int) sizeof(sfbuff) < ((targetthroughput - throughput) /*+2*/)) ? sizeof(sfbuff)
606 : (
607 targetthroughput - throughput)));
608 } else {
609 rc = SSL_read(sockfrom.ssl, sfbuff, sizeof(sfbuff));
610 }
611 if (rc < 0) {
612 break; // an error occurred so end the while()
613 } else if (!rc) {
614 done = true; // none received so pipe is closed so flag it
615 break;
616 } else { // some data read
617 DEBUG_network("tunnel got data from sockfrom: ", rc, " bytes");
618 throughput += rc; // increment our counter used to log
619 if (poll(tooutfds, 1, timeout) < 1) {
620 break; // an error occurred or timed out so end while()
621 }
622
623 if (tooutfds[0].revents & POLLOUT) {
624
625 // rc = send(fdto, sfbuff, rc, 0);
626 rc = sockto.writeToSocket(sfbuff, rc, 0, 0);
627 if (rc < 1) { // write data
628 break; // was an error writing
629 }
630 DEBUG_network("tunnel wrote data out: ", rc, " bytes");
631 // if (sockfrom.s_checkPending()))
632 //do_poll = false;
633 //else
634 //do_poll = true;
635 done = false; // flag to say data still to be handled
636 } else {
637 break; // should never get here
638 }
639 }
640 }
641 }
642 bool pending = sockfrom.s_checkPending();
643 if (pending)
644 do_poll = false;
645 DEBUG_network("has_pending is ", pending, " do_poll is ", do_poll);
646 if (do_poll) {
647 rc = poll(twayfds, 2, l_timeout);
648 //int rc = poll(twayfds, 2, 10000); // reduced timeout for testing
649 if (rc < 1) {
650 DEBUG_network("tunnel tw poll returned error or timeout::", rc);
651 break; // an error occurred or it timed out so end while()
652 }
653 done = false;
654 DEBUG_network("tunnel tw poll returned ok:", rc, " twayfds.revents:", twayfds[0].revents, " ",
655 twayfds[1].revents);
656 DEBUG_network("POLLIN is ", POLLIN);
657 } else {
658 do_poll = true;
659 }
660
661 if (twoway && twayfds[1].revents & (POLLIN | POLLHUP)) {
662 DEBUG_network("Reading to");
663
664 // read as much as is available
665
666 rc = recv(fdto, sfbuff, sizeof(sfbuff), 0);
667 DEBUG_network("read to returned", rc);
668
669 if (rc < 0) {
670 DEBUG_network("read to error is", errno);
671 break; // an error occurred so end the while()
672 } else if (!rc) {
673 done = true; // none received so pipe is closed so flag it
674 break;
675 } else { // some data read
676 if (poll(fromoutfds, 1, timeout) < 1) {
677 break; // an error occurred or timed out so end while()
678 }
679
680 if ((fromoutfds[0].revents & POLLOUT) && sockfrom.s_checkShutdown() == 0) {
681 rc = sockfrom.writeToSocket(sfbuff, rc, 0, timeout);
682
683 if (rc < 1) { // write data
684 break; // was an error writing
685 }
686 //if (sockfrom.s_checkPending())
687 //do_poll = false;
688 done = false; // flag to say data still to be handled
689 } else {
690 break; // should never get here
691 }
692 }
693 }
694 }
695 if ((throughput >= targetthroughput) && (targetthroughput > -1)) {
696 DEBUG_network("All expected data tunnelled. (expected ", targetthroughput, "; tunnelled ", throughput, ")");
697 } else {
698 DEBUG_network("Tunnel closed.");
699 }
700 return (targetthroughput > -1) ? (throughput >= targetthroughput) : true;
701 }
702
703
704 bool FDTunnel::iss2iss_tunnel(Socket &sockfrom, Socket &sockto, int &fdto, int &fdfrom, bool twoway,
705 off_t targetthroughput, bool ignore) {
706 // it is a tunnel with from socket ssl and to socket ssl
707
708 bool done = false; // so we get past the first while
709
710 ssize_t rc = 0;
711
712 tooutfds[0].fd = fdto;
713 tooutfds[0].events = POLLOUT;
714 tooutfds[0].revents = 0;
715 fromoutfds[0].fd = fdfrom;
716 fromoutfds[0].events = POLLOUT;
717 fromoutfds[0].revents = 0;
718 twayfds[0].events = POLLIN;
719 twayfds[0].revents = 0;
720 twayfds[1].revents = 0;
721 if (twoway)
722 twayfds[1].events = POLLIN;
723
724 if ((sockfrom.bufflen - sockfrom.buffstart) > 0) {
725 int to_write = (sockfrom.bufflen - sockfrom.buffstart);
726 if ((targetthroughput > 0) && (targetthroughput < to_write))
727 to_write = targetthroughput;
728 DEBUG_network("Data in fdfrom's buffer; sending ", to_write, " bytes");
729 if (!sockto.writeToSocket(sockfrom.buffer + sockfrom.buffstart, to_write, 0, 120000))
730 return false;
731 DEBUG_network("Data in fdfrom's buffer; sent ", to_write, " bytes");
732
733 throughput += to_write;
734 sockfrom.buffstart += to_write;
735 if (sockfrom.buffstart == sockfrom.bufflen) {
736 sockfrom.bufflen = 0;
737 sockfrom.buffstart = 0;
738 }
739 }
740
741
742 while (!done && (targetthroughput > -1 ? throughput < targetthroughput : true)) {
743 done = true; // if we don't make a sucessful read and write this
744 // flag will stay true and so the while() will exit
745 DEBUG_network("Start of fromSsl to toSsl tunnel loop: throughput:", throughput, " target:", targetthroughput);
746 //FD_CLR(fdto, &inset);
747
748 // 1st check pending before doing poll
749 bool do_poll = true;
750
751 {
752 // 1st check pending before doing poll
753 if ((sockfrom.s_checkPending()) || twayfds[0].revents & POLLIN) {
754 DEBUG_network("Reading from");
755 if (targetthroughput > -1) {
756 // we have a target throughput - only read in the exact amount of data we've been told to
757 //rc = SSL_read(sockfrom.ssl, sfbuff,
758 rc = sockfrom.readFromSocket(sfbuff,
759 (((int) sizeof(sfbuff) < ((targetthroughput - throughput) /*+2*/))
760 ? sizeof(sfbuff)
761 : (
762 targetthroughput - throughput)), 0, 0, true);
763 } else {
764 rc = sockfrom.readFromSocket(sfbuff, 4096, 0, 0, true);
765 //rc = sockfrom.readFromSocket(sfbuff, sizeof(sfbuff),0,0,true);
766 //rc = SSL_read(sockfrom.ssl, sfbuff, sizeof(sfbuff));
767 }
768 if (rc < 0) {
769 break; // an error occurred so end the while()
770 } else if (!rc) {
771 done = true; // none received so pipe is closed so flag it
772 break;
773 } else { // some data read
774 if (sockfrom.s_checkPending())
775 do_poll = false;
776 DEBUG_network("tunnel got data from sockfrom: ", rc, " bytes");
777 throughput += rc; // increment our counter used to log
778 if (poll(tooutfds, 1, timeout) < 1) {
779 break; // an error occurred or timed out so end while()
780 }
781
782 if (tooutfds[0].revents & POLLOUT) {
783
784 rc = sockto.writeToSocket(sfbuff, rc, 0, timeout);
785 if (rc < 1) { // write data
786 break; // was an error writing
787 }
788 DEBUG_network("tunnel wrote data out: ", rc, " bytes");
789 done = false; // flag to say data still to be handled
790 if (twoway && (sockto.s_checkPending()))
791 do_poll = false;
792 } else {
793 break; // should never get here
794 }
795 }
796 }
797 }
798
799 if (twoway && ((sockto.s_checkPending()) || twayfds[1].revents & (POLLIN | POLLHUP))) {
800 DEBUG_network("Reading to");
801
802 // read as much as is available
803
804 rc = sockto.readFromSocket(sfbuff, sizeof(sfbuff), 0, 0, true);
805 DEBUG_network("read to returned", rc);
806
807 if (rc < 0) {
808 DEBUG_network("read to error is", errno);
809 break; // an error occurred so end the while()
810 } else if (!rc) {
811 done = true; // none received so pipe is closed so flag it
812 break;
813 } else { // some data read
814 if (poll(fromoutfds, 1, timeout) < 1) {
815 break; // an error occurred or timed out so end while()
816 }
817 if (sockto.s_checkPending())
818 do_poll = false;
819
820 if (fromoutfds[0].revents & POLLOUT) {
821 rc = sockfrom.writeToSocket(sfbuff, rc, 0, timeout);
822
823 if (rc < 1) { // write data
824 break; // was an error writing
825 }
826 done = false; // flag to say data still to be handled
827 if (sockfrom.s_checkPending())
828 do_poll = false;
829 } else {
830 break; // should never get here
831 }
832 }
833 }
834 if ((throughput >= targetthroughput) && (targetthroughput > -1)) {
835 do_poll = false;
836 }
837
838 if (do_poll) {
839
840 rc = poll(twayfds, 2, timeout);
841 if (rc < 1) {
842 DEBUG_network("tunnel tw poll returned error or timeout::", rc);
843 break; // an error occurred or it timed out so end while()
844 }
845 done = false;
846 DEBUG_network("tunnel tw poll returned ok:", rc, " twayfds.revents:", twayfds[0].revents, " ",
847 twayfds[1].revents);
848 DEBUG_network("POLLIN is ", POLLIN);
849
850 } else {
851 do_poll = true;
852 }
853 }
854 if ((throughput >= targetthroughput) && (targetthroughput > -1)) {
855 DEBUG_network("All expected data tunnelled. (expected ", targetthroughput, "; tunnelled ", throughput, ")");
856 } else {
857 DEBUG_network("Tunnel closed.");
858 }
859 return (targetthroughput > -1) ? (throughput >= targetthroughput) : true;
860 }