1 /*
2    3APA3A simpliest proxy server
3    (c) 2002-2020 by Vladimir Dubrovin <3proxy@3proxy.ru>
4 
5    please read License Agreement
6 
7 */
8 
9 #include "proxy.h"
10 
11 #define MAXFAILATTEMPT 10
12 
13 #ifdef WITHLOG
14 #if WITHLOG > 1
15 char logbuf[1024];
16 #endif
17 #define log(X) dolog(param,X)
18 #else
19 #define log(X)
20 #endif
21 
22 #ifdef WITHSPLICE
23 
24 #include <fcntl.h>
25 ssize_t splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags);
26 #ifndef SPLICE_F_MOVE
27 #define SPLICE_F_MOVE           0x01
28 #endif
29 #ifndef SPLICE_F_NONBLOCK
30 #define SPLICE_F_NONBLOCK       0x02
31 #endif
32 #ifndef SPLICE_F_MORE
33 #define SPLICE_F_MORE           0x04
34 #endif
35 #ifndef SPLICE_F_GIFT
36 #define SPLICE_F_GIFT           0x08
37 #endif
38 
39 
40 #define MAXSPLICE 65536
41 
42 #endif
43 
44 #define MIN(a,b) ((a>b)?b:a)
45 #define RETURN(xxx) { res = xxx; goto CLEANRET; }
46 
sockmap(struct clientparam * param,int timeo,int usesplice)47 int sockmap(struct clientparam * param, int timeo, int usesplice){
48  uint64_t fromclient=0x7fffffffffffffff, fromserver =0x7fffffffffffffff;
49  uint64_t inclientbuf = 0, inserverbuf = 0;
50  int FROMCLIENT = 1, TOCLIENTBUF = 1, FROMCLIENTBUF = 1, TOSERVER = 1,
51 	FROMSERVER = 1, TOSERVERBUF = 1, FROMSERVERBUF = 1, TOCLIENT = 1;
52  int HASERROR=0;
53  int CLIENTTERM = 0, SERVERTERM = 0;
54  int after = 0;
55  struct pollfd fds[6];
56  struct pollfd *fdsp = fds;
57  int fdsc = 0;
58  int sleeptime = 0;
59  FILTER_ACTION action;
60  int res;
61  SASIZETYPE sasize;
62  int needaction = 0;
63 
64 #ifdef WITHSPLICE
65  uint64_t inclientpipe = 0, inserverpipe = 0;
66  int TOCLIENTPIPE = 0, FROMCLIENTPIPE = 0, TOSERVERPIPE = 0, FROMSERVERPIPE = 0;
67  int pipesrv[2] = {-1,-1};
68  int pipecli[2] = {-1,-1};
69 
70  if(param->operation == UDPASSOC || (!param->nolongdatfilter && (param->ndatfilterscli > 0 || param->ndatfilterssrv))) usesplice = 0;
71  if(usesplice){
72 	TOCLIENTPIPE = FROMCLIENTPIPE = TOSERVERPIPE = FROMSERVERPIPE = 1;
73 	TOCLIENTBUF = TOSERVERBUF = 0;
74 	if(pipe2(pipecli, O_NONBLOCK) < 0) RETURN (21);
75 	if(pipe2(pipesrv, O_NONBLOCK) < 0) RETURN (21);
76  }
77 #endif
78 
79  inserverbuf = param->srvinbuf - param->srvoffset;
80  inclientbuf = param->cliinbuf - param->clioffset;
81 
82  if(param->waitclient64) {
83 	fromclient = param->waitclient64;
84 	fromserver = 0;
85 	inserverbuf = 0;
86 	TOCLIENT = 0;
87 	FROMSERVER = 0;
88  }
89  if(param->waitserver64) {
90 	fromserver = param->waitserver64;
91 	fromclient = 0;
92 	inclientbuf = 0;
93 	TOSERVER = 0;
94 	FROMCLIENT = 0;
95  }
96  if(param->operation == UDPASSOC && param->srv->singlepacket){
97 	fromclient = inclientbuf;
98 	FROMCLIENT = 0;
99  }
100  if(inserverbuf >= fromserver) FROMSERVER = 0;
101  if(inclientbuf >= fromclient) FROMCLIENT = 0;
102 #ifdef WITHSPLICE
103  if(!usesplice)
104 #endif
105  {
106 	if(fromserver && !param->srvbuf && (!(param->srvbuf=myalloc(SRVBUFSIZE)) || !(param->srvbufsize = SRVBUFSIZE))){
107 		RETURN (21);
108 	}
109 	if(fromclient && !param->clibuf && (!(param->clibuf=myalloc(SRVBUFSIZE)) || !(param->clibufsize = SRVBUFSIZE))){
110 		RETURN (21);
111 	}
112 
113  }
114  if(param->srvinbuf == param->srvoffset) param->srvinbuf =param->srvoffset = 0;
115  if(param->cliinbuf == param->clioffset) param->cliinbuf =param->clioffset = 0;
116  if(param->clibufsize == param->cliinbuf) TOCLIENTBUF = 0;
117  if(param->srvbufsize == param->srvinbuf) TOSERVERBUF = 0;
118 
119  action = handlepredatflt(param);
120  if(action == HANDLED){
121 	RETURN(0);
122  }
123  if(action != PASS) RETURN(19);
124 
125  while(
126 	((!CLIENTTERM) && fromserver && (inserverbuf
127 #ifdef WITHSPLICE
128 		|| inserverpipe
129 #endif
130 		|| (!SERVERTERM )))
131 	||
132 	((!SERVERTERM) && fromclient && (inclientbuf
133 #ifdef WITHSPLICE
134 		|| inclientpipe
135 #endif
136 		|| (!CLIENTTERM )))
137  ){
138 
139 
140 #if WITHLOG > 1
141 sprintf(logbuf, "int FROMCLIENT = %d, TOCLIENTBUF = %d, FROMCLIENTBUF = %d, TOSERVER = %d, "
142 	"FROMSERVER = %d, TOSERVERBUF = %d, FROMSERVERBUF = %d, TOCLIENT = %d; inclientbuf=%d; "
143 	"inserverbuf=%d, CLIENTTERM = %d, SERVERTERM =%d, fromserver=%u, fromclient=%u"
144 #ifdef WITHSPLICE
145 	 ", inserverpipe=%d, inclentpipe=%d "
146 	"TOCLIENTPIPE=%d FROMCLIENTPIPE==%d TOSERVERPIPE==%d FROMSERVERPIPE=%d"
147 #endif
148 	,
149  FROMCLIENT, TOCLIENTBUF, FROMCLIENTBUF, TOSERVER,
150 	FROMSERVER, TOSERVERBUF, FROMSERVERBUF, TOCLIENT,
151 	(int)inclientbuf, (int)inserverbuf, CLIENTTERM, SERVERTERM,
152 	(unsigned)fromserver, (unsigned)fromclient
153 #ifdef WITHSPLICE
154 	,(int)inserverpipe, (int)inclientpipe,
155 	TOCLIENTPIPE, FROMCLIENTPIPE, TOSERVERPIPE, FROMSERVERPIPE
156 #endif
157 	);
158 log(logbuf);
159 #endif
160 
161 	if(needaction > 2 && !sleeptime){
162 		if(needaction > (MAXFAILATTEMPT+1)){RETURN (93);}
163 		sleeptime = (1<<(needaction-2));
164 	}
165 	if(sleeptime > 0) {
166 		if(sleeptime > (timeo * 1000)){RETURN (93);}
167 		memset(fds, 0, sizeof(fds));
168 		fds[0].fd = param->clisock;
169 		fds[1].fd = param->remsock;
170 		so._poll(fds, 2, sleeptime);
171 		sleeptime = 0;
172 	}
173 	if((param->srv->logdumpsrv && (param->statssrv64 > param->srv->logdumpsrv)) ||
174 		(param->srv->logdumpcli && (param->statscli64 > param->srv->logdumpcli)))
175 			dolog(param, NULL);
176 
177 	if(param->version < conf.version){
178 		if(!param->srv->noforce && (res = (*param->srv->authfunc)(param)) && res != 2) {RETURN(res);}
179 		param->paused = conf.paused;
180 		param->version = conf.version;
181 	}
182 
183 	if((param->maxtrafin64 && param->statssrv64 >= param->maxtrafin64) || (param->maxtrafout64 && param->statscli64 >= param->maxtrafout64)){
184 		RETURN (10);
185 	}
186 
187 	if(inclientbuf && TOSERVER){
188 #ifdef WITHLOG
189 log("send to server from buf");
190 #endif
191 		if(!param->nolongdatfilter){
192 			action = handledatfltcli(param,  &param->clibuf, (int *)&param->clibufsize, param->cliinbuf - res, (int *)&param->cliinbuf);
193 			if(action == HANDLED){
194 				RETURN(0);
195 			}
196 			if(action != PASS) RETURN(19);
197 			inclientbuf=param->cliinbuf - param->clioffset;
198 		}
199 		if(!inclientbuf){
200 			param->clioffset = param->cliinbuf = 0;
201 			if(fromclient) TOCLIENTBUF = 1;
202 		}
203 		sasize = sizeof(param->sinsr);
204 		res = so._sendto(param->remsock, (char *)param->clibuf + param->clioffset, (int)MIN(inclientbuf, fromclient), 0, (struct sockaddr*)&param->sinsr, sasize);
205 		if(res <= 0) {
206 			TOSERVER = 0;
207 			if(errno && errno != EAGAIN && errno != EINTR){
208 				SERVERTERM = 1;
209 				HASERROR |= 2;
210 			}
211 		}
212 		else {
213 #ifdef WITHLOG
214 log("done send to server from buf");
215 #endif
216 		    	param->nwrites++;
217 			param->statscli64 += res;
218 			inclientbuf -= res;
219 			fromclient -= res;
220 			param->clioffset += res;
221 			if(param->clioffset == param->cliinbuf)param->clioffset = param->cliinbuf = 0;
222 			if(param->cliinbuf < param->clibufsize) TOCLIENTBUF = 1;
223 			if(param->bandlimfunc) {
224 				int sl1;
225 				sl1 = (*param->bandlimfunc)(param, 0, res);
226 				if(sl1 > sleeptime) sleeptime = sl1;
227 		    	}
228 			needaction = 0;
229 			continue;
230 		}
231 	}
232 	if(inserverbuf && TOCLIENT){
233 #ifdef WITHLOG
234 log("send to client from buf");
235 #endif
236 		if(!param->nolongdatfilter){
237 			action = handledatfltsrv(param,  &param->srvbuf, (int *)&param->srvbufsize, param->srvinbuf - res, (int *)&param->srvinbuf);
238 			if(action == HANDLED){
239 				RETURN(0);
240 			}
241 			if(action != PASS) RETURN(19);
242 			inserverbuf = param->srvinbuf - param->srvoffset;
243 		}
244 		if(!inserverbuf){
245 			param->srvinbuf = param->srvoffset = 0;
246 			continue;
247 		}
248 		sasize = sizeof(param->sincr);
249 		res = so._sendto(param->clisock, (char *)param->srvbuf + param->srvoffset, (int)MIN(inserverbuf,fromserver), 0, (struct sockaddr*)&param->sincr, sasize);
250 		if(res <= 0) {
251 			TOCLIENT = 0;
252 			if(errno && errno != EAGAIN && errno != EINTR){
253 				CLIENTTERM = 1;
254 				HASERROR |= 1;
255 			}
256 
257 		}
258 		else {
259 #ifdef WITHLOG
260 log("done send to client from buf");
261 #endif
262 			inserverbuf -= res;
263 			fromserver -= res;
264 			param->srvoffset += res;
265 			if(param->srvoffset == param->srvinbuf)param->srvoffset = param->srvinbuf =0;
266 			if(param->srvinbuf < param->srvbufsize) TOSERVERBUF = 1;
267 			needaction = 0;
268 			continue;
269 		}
270 	}
271 #ifdef WITHSPLICE
272 	if(usesplice){
273 		if(inclientpipe && !inclientbuf && FROMCLIENTPIPE && TOSERVER){
274 #ifdef WITHLOG
275 log("send to server from pipe");
276 #endif
277 			res = splice(pipecli[0], NULL, param->remsock, NULL, MIN(MAXSPLICE, inclientpipe), SPLICE_F_NONBLOCK|SPLICE_F_MOVE);
278 #ifdef WITHLOG
279 log("server from pipe splice finished\n");
280 #if WITHLOG > 1
281 #ifdef WITHSPLICE
282 sprintf(logbuf, "res: %d, errno: %d", (int)res, (int)errno);
283 log(logbuf);
284 #endif
285 #endif
286 #endif
287 			if(res >0) {
288 			    	param->nwrites++;
289 				param->statscli64 += res;
290 				inclientpipe -= res;
291 				fromclient -= res;
292 				if(param->bandlimfunc) {
293 					int sl1;
294 					sl1 = (*param->bandlimfunc)(param, 0, res);
295 					if(sl1 > sleeptime) sleeptime = sl1;
296 		    		}
297 				needaction = 0;
298 				continue;
299 			}
300 			else {
301 				FROMCLIENTPIPE = TOSERVER = 0;
302 			}
303 		}
304 		if(inserverpipe && !inserverbuf && FROMSERVERPIPE && TOCLIENT){
305 #ifdef WITHLOG
306 log("send to client from pipe");
307 #endif
308 			res = splice(pipesrv[0], NULL, param->clisock, NULL, MIN(MAXSPLICE, inserverpipe), SPLICE_F_NONBLOCK|SPLICE_F_MOVE);
309 #ifdef WITHLOG
310 log("client from pipe splice finished\n");
311 #if WITHLOG > 1
312 #ifdef WITHSPLICE
313 sprintf(logbuf, "res: %d, errno: %d", (int)res, (int)errno);
314 log(logbuf);
315 #endif
316 #endif
317 #endif
318 			if(res > 0) {
319 				inserverpipe -= res;
320 				fromserver -= res;
321 				if(fromserver)TOSERVERPIPE = 1;
322 				needaction = 0;
323 				continue;
324 			}
325 			else {
326 				FROMSERVERPIPE = TOCLIENT = 0;
327 			}
328 		}
329 		if(fromclient>inclientpipe && FROMCLIENT && TOCLIENTPIPE){
330 			int error;
331 			socklen_t len=sizeof(error);
332 #ifdef WITHLOG
333 log("read from client to pipe");
334 #endif
335 			errno = 0;
336 			res = splice(param->clisock, NULL, pipecli[1], NULL, (int)MIN((uint64_t)MAXSPLICE - inclientpipe, (uint64_t)fromclient-inclientpipe), SPLICE_F_NONBLOCK|SPLICE_F_MOVE);
337 #ifdef WITHLOG
338 log("client to pipe splice finished\n");
339 #if WITHLOG > 1
340 #ifdef WITHSPLICE
341 sprintf(logbuf, "res: %d, errno: %d", (int)res, (int)errno);
342 log(logbuf);
343 #endif
344 #endif
345 #endif
346 			if(res <= 0) {
347 				FROMCLIENT = TOCLIENTPIPE = 0;
348 				if(res == 0 && !errno) {
349 					CLIENTTERM = 1;
350 					continue;
351 				}
352 			}
353 			else {
354 #ifdef WITHLOG
355 log("done read from client to pipe");
356 #endif
357 				inclientpipe += res;
358 				if(inclientpipe >= MAXSPLICE) TOCLIENTPIPE = 0;
359 				needaction = 0;
360 				continue;
361 			}
362 		}
363 		if(fromserver > inserverpipe && FROMSERVER && TOSERVERPIPE){
364 			int error;
365 			socklen_t len=sizeof(error);
366 			errno = 0;
367 #ifdef WITHLOG
368 log("read from server to pipe\n");
369 #endif
370 			res = splice(param->remsock, NULL, pipesrv[1], NULL, MIN(MAXSPLICE - inclientpipe, fromserver - inserverpipe), SPLICE_F_NONBLOCK|SPLICE_F_MOVE);
371 #ifdef WITHLOG
372 log("server to pipe splice finished\n");
373 #if WITHLOG > 1
374 #ifdef WITHSPLICE
375 sprintf(logbuf, "res: %d, errno: %d", (int)res, (int)errno);
376 log(logbuf);
377 #endif
378 #endif
379 #endif
380 			if(res <= 0) {
381 				FROMSERVER = TOSERVERPIPE = 0;
382 				if(res == 0 && !errno) {
383 					SERVERTERM = 1;
384 					continue;
385 				}
386 			}
387 			else {
388 #ifdef WITHLOG
389 log("done read from server to pipe\n");
390 #endif
391 			    	param->nreads++;
392 				param->statssrv64 += res;
393 				inserverpipe += res;
394 				if(inserverpipe >= MAXSPLICE) TOSERVERPIPE = 0;
395 				if(param->bandlimfunc) {
396 					int sl1;
397 					sl1 = (*param->bandlimfunc)(param, res, 0);
398 					if(sl1 > sleeptime) sleeptime = sl1;
399 		    		}
400  				if(param->operation == UDPASSOC && param->srv->singlepacket){
401 					fromserver = inserverpipe;
402 					FROMSERVER = 0;
403 				}
404 				needaction = 0;
405 				continue;
406 			}
407 		}
408 	}
409 	else
410 #endif
411 	{
412 		if(fromclient > inclientbuf && FROMCLIENT && TOCLIENTBUF){
413 #ifdef WITHLOG
414 log("read from client to buf");
415 #endif
416 			sasize = sizeof(param->sincr);
417 			res = so._recvfrom(param->clisock, (char *)param->clibuf + param->cliinbuf, (int)MIN((uint64_t)param->clibufsize - param->cliinbuf, fromclient-inclientbuf), 0, (struct sockaddr *)&param->sincr, &sasize);
418 			if(res <= 0) {
419 				FROMCLIENT = 0;
420 				if(res == 0 || (errno && errno != EINTR && errno !=EAGAIN)){
421 					CLIENTTERM = 1;
422 					continue;
423 				}
424 			}
425 			else {
426 #ifdef WITHLOG
427 log("done read from client to buf");
428 #endif
429 				inclientbuf += res;
430 				param->cliinbuf += res;
431 				if(param->clibufsize == param->cliinbuf) TOCLIENTBUF = 0;
432 				needaction = 0;
433 				continue;
434 			}
435 		}
436 
437 		if(fromserver > inserverbuf && FROMSERVER && TOSERVERBUF){
438 #ifdef WITHLOG
439 log("read from server to buf");
440 #endif
441 			sasize = sizeof(param->sinsr);
442 			res = so._recvfrom(param->remsock, (char *)param->srvbuf + param->srvinbuf, (int)MIN((uint64_t)param->srvbufsize - param->srvinbuf, fromserver-inserverbuf), 0, (struct sockaddr *)&param->sinsr, &sasize);
443 			if(res <= 0) {
444 				FROMSERVER = 0;
445 				if(res == 0 || (errno && errno != EINTR && errno !=EAGAIN)) {
446 					SERVERTERM = 1;
447 					continue;
448 				}
449 			}
450 			else {
451 #ifdef WITHLOG
452 log("done read from server to buf");
453 #endif
454 			    	param->nreads++;
455 				param->statssrv64 += res;
456 				inserverbuf += res;
457 				param->srvinbuf += res;
458 				if(param->bandlimfunc) {
459 					int sl1;
460 					sl1 = (*param->bandlimfunc)(param, res, 0);
461 					if(sl1 > sleeptime) sleeptime = sl1;
462 		    		}
463 				if(param->srvbufsize == param->srvinbuf) TOSERVERBUF = 0;
464  				if(param->operation == UDPASSOC && param->srv->singlepacket){
465 					fromserver = inserverbuf;
466 					FROMSERVER = 0;
467 				}
468 				needaction = 0;
469 				continue;
470 			}
471 		}
472 	}
473 	for(after = 0; after < 2; after ++){
474 		fdsc = 0;
475 		if(!after){
476 			memset(fds, 0, sizeof(fds));
477 		}
478 		if(!CLIENTTERM){
479 			if(!after){
480 				fds[fdsc].fd = param->clisock;
481 				if(fromclient && !FROMCLIENT && ((
482 #ifdef WITHSPLICE
483 					!usesplice &&
484 #endif
485 					TOCLIENTBUF)
486 #ifdef WITHSPLICE
487 					|| (usesplice)
488 #endif
489 						)){
490 #ifdef WITHLOG
491 log("wait reading from client");
492 #endif
493 							fds[fdsc].events |= (POLLIN);
494 						}
495 				if(!TOCLIENT && (inserverbuf
496 #ifdef WITHSPLICE
497 					|| inserverpipe
498 #endif
499 						)){
500 #ifdef WITHLOG
501 log("wait writing to client");
502 #endif
503 							fds[fdsc].events |= POLLOUT;
504 						}
505 			}
506 			else{
507 				if(fds[fdsc].revents &  (POLLERR|POLLNVAL)) {
508 					CLIENTTERM = 1;
509 					HASERROR |= 1;
510 				}
511 				else {
512 					if(fds[fdsc].revents & POLLIN) {
513 #ifdef WITHLOG
514 log("ready to read from client");
515 #endif
516 						FROMCLIENT = 1;
517 					}
518 					if(fds[fdsc].revents & POLLOUT) {
519 #ifdef WITHLOG
520 log("ready to write to client");
521 #endif
522 						TOCLIENT = 1;
523 					}
524 					if(fds[fdsc].revents &  (POLLHUP)) {
525 						if(fds[fdsc].events & POLLIN) FROMCLIENT = 1;
526 						if(fds[fdsc].events & POLLOUT) CLIENTTERM = 1;
527 					}
528 				}
529 			}
530 			fdsc++;
531 		}
532 		if(!SERVERTERM){
533 			if(!after){
534 				fds[fdsc].fd = param->remsock;
535 				if(fromserver && !FROMSERVER && ((
536 #ifdef WITHSPLICE
537 					!usesplice &&
538 #endif
539 					TOSERVERBUF)
540 #ifdef WITHSPLICE
541 					|| (usesplice)
542 #endif
543 						)){
544 #ifdef WITHLOG
545 log("wait reading from server");
546 #endif
547 							fds[fdsc].events |= (POLLIN);
548 						}
549 				if(!TOSERVER && (inclientbuf
550 #ifdef WITHSPLICE
551 					|| inclientpipe
552 #endif
553 						)){
554 #ifdef WITHLOG
555 log("wait writing from server");
556 #endif
557 							fds[fdsc].events |= POLLOUT;
558 						}
559 			}
560 			else{
561 				if(fds[fdsc].revents &  (POLLERR|POLLNVAL)) {
562 #ifdef WITHLOG
563 log("poll from server failed");
564 #endif
565 
566 					SERVERTERM = 1;
567 					HASERROR |=2;
568 				}
569 				else {
570 					if(fds[fdsc].revents & POLLIN) {
571 #ifdef WITHLOG
572 log("ready to read from server");
573 #endif
574 						FROMSERVER = 1;
575 					}
576 					if(fds[fdsc].revents & POLLOUT) {
577 #ifdef WITHLOG
578 log("ready to write to server");
579 #endif
580 						TOSERVER = 1;
581 					}
582 					if(fds[fdsc].revents &  (POLLHUP)) {
583 #ifdef WITHLOG
584 log("server terminated connection");
585 #endif
586 						if(fds[fdsc].events & POLLIN) FROMSERVER = 1;
587 						if(fds[fdsc].events & POLLOUT) SERVERTERM = 1;
588 					}
589 				}
590 			}
591 			fdsc++;
592 		}
593 #ifdef WITHSPLICE
594 		if(usesplice){
595 			if(fromclient>inclientpipe && !TOCLIENTPIPE && inclientpipe < MAXSPLICE){
596 				if(!after){
597 #ifdef WITHLOG
598 log("wait writing to client pipe");
599 #endif
600 					fds[fdsc].fd = pipecli[1];
601 					fds[fdsc].events |= POLLOUT;
602 				}
603 				else {
604 					if(fds[fdsc].revents &  (POLLHUP|POLLERR|POLLNVAL)){
605 						RETURN(90);
606 					}
607 					if(fds[fdsc].revents & POLLOUT) {
608 #ifdef WITHLOG
609 log("ready to write to client pipe");
610 #endif
611 						TOCLIENTPIPE = 1;
612 					}
613 				}
614 				fdsc++;
615 			}
616 			if(inclientpipe && !FROMCLIENTPIPE){
617 				if(!after){
618 #ifdef WITHLOG
619 log("wait reading from client pipe");
620 #endif
621 					fds[fdsc].fd = pipecli[0];
622 					fds[fdsc].events |= (POLLIN);
623 				}
624 				else {
625 					if(fds[fdsc].revents &  (POLLHUP|POLLERR|POLLNVAL)){
626 						RETURN(90);
627 					}
628 #ifdef WITHLOG
629 log("ready reading from client pipe");
630 #endif
631 					if(fds[fdsc].revents & (POLLIN)) FROMCLIENTPIPE = 1;
632 				}
633 				fdsc++;
634 			}
635 			if(fromserver>inserverpipe && !TOSERVERPIPE && inserverpipe < MAXSPLICE){
636 				if(!after){
637 #ifdef WITHLOG
638 log("wait writing to server pipe");
639 #endif
640 					fds[fdsc].fd = pipesrv[1];
641 					fds[fdsc].events |= POLLOUT;
642 				}
643 				else {
644 					if(fds[fdsc].revents &  (POLLHUP|POLLERR|POLLNVAL)){
645 						RETURN(90);
646 					}
647 #ifdef WITHLOG
648 log("ready writing to server pipe");
649 #endif
650 					if(fds[fdsc].revents & POLLOUT) TOSERVERPIPE = 1;
651 				}
652 				fdsc++;
653 			}
654 			if(inserverpipe && !FROMSERVERPIPE){
655 				if(!after){
656 #ifdef WITHLOG
657 log("wait reading from server pipe");
658 #endif
659 					fds[fdsc].fd = pipesrv[0];
660 					fds[fdsc].events |= (POLLIN);
661 				}
662 				else {
663 					if(fds[fdsc].revents &  (POLLHUP|POLLERR|POLLNVAL)){
664 						RETURN(90);
665 					}
666 #ifdef WITHLOG
667 log("ready reading from server pipe");
668 #endif
669 					if(fds[fdsc].revents & (POLLIN)) FROMSERVERPIPE = 1;
670 				}
671 				fdsc++;
672 			}
673 		}
674 #endif
675 		if(!after){
676 			if(!fdsc) RETURN(90);
677 
678 
679 
680 
681 #ifdef WITHLOG
682 log("entering poll");
683 #endif
684 			res = so._poll(fds, fdsc, timeo*1000);
685 #ifdef WITHLOG
686 log("leaving poll");
687 #endif
688 			if(res < 0){
689 #ifdef WITHLOG
690 log("poll error");
691 #endif
692 				if(errno != EINTR) RETURN(91);
693 				break;
694 			}
695 			if(res < 1){
696 #ifdef WITHLOG
697 log("timeout");
698 #endif
699 				RETURN (92);
700 			}
701 		}
702 	}
703 	needaction++;
704 
705  }
706  res = 0;
707  if(!fromserver && param->waitserver64) res = 98;
708  else if(!fromclient && param->waitclient64) res = 99;
709  else if(HASERROR) res = 94+HASERROR;
710  else if((inclientbuf || inserverbuf)) res = 94;
711 #ifdef WITHSPLICE
712  else if(inclientpipe || inserverpipe) res = 94;
713 #endif
714 
715 CLEANRET:
716 
717 #ifdef WITHSPLICE
718  if(pipecli[0] >= 0) close(pipecli[0]);
719  if(pipecli[1] >= 0) close(pipecli[1]);
720  if(pipesrv[0] >= 0) close(pipesrv[0]);
721  if(pipesrv[1] >= 0) close(pipesrv[1]);
722 #endif
723 
724  return res;
725 }
726