sockmap.c 17 KB

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