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[7];
  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 || (!param->nolongdatfilter && (param->ndatfilterscli > 0 || param->ndatfilterssrv))) 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. if(param->monitorsock)fds[2].fd = *param->monitorsock;
  149. fds[2].events = POLLIN;
  150. so._poll(fds, param->monitorsock? 3:2, sleeptime);
  151. if(fds[2].revents){
  152. RETURN (93);
  153. }
  154. sleeptime = 0;
  155. }
  156. if((param->srv->logdumpsrv && (param->statssrv64 > param->srv->logdumpsrv)) ||
  157. (param->srv->logdumpcli && (param->statscli64 > param->srv->logdumpcli)))
  158. dolog(param, NULL);
  159. if(param->version < conf.version){
  160. if(!param->srv->noforce && (res = (*param->srv->authfunc)(param)) && res != 2) {RETURN(res);}
  161. param->paused = conf.paused;
  162. param->version = conf.version;
  163. }
  164. if((param->maxtrafin64 && param->statssrv64 >= param->maxtrafin64) || (param->maxtrafout64 && param->statscli64 >= param->maxtrafout64)){
  165. RETURN (10);
  166. }
  167. if(inclientbuf && TOSERVER){
  168. #ifdef WITHLOG
  169. log("send to server from buf");
  170. #endif
  171. if(!param->nolongdatfilter){
  172. action = handledatfltcli(param, &param->clibuf, (int *)&param->clibufsize, param->cliinbuf - res, (int *)&param->cliinbuf);
  173. if(action == HANDLED){
  174. RETURN(0);
  175. }
  176. if(action != PASS) RETURN(19);
  177. inclientbuf=param->cliinbuf - param->clioffset;
  178. }
  179. if(!inclientbuf){
  180. param->clioffset = param->cliinbuf = 0;
  181. if(fromclient) TOCLIENTBUF = 1;
  182. }
  183. sasize = sizeof(param->sinsr);
  184. res = so._sendto(param->remsock, (char *)param->clibuf + param->clioffset, (int)MIN(inclientbuf, fromclient), 0, (struct sockaddr*)&param->sinsr, sasize);
  185. if(res <= 0) TOSERVER = 0;
  186. else {
  187. #ifdef WITHLOG
  188. log("done send to server from buf");
  189. #endif
  190. param->nwrites++;
  191. param->statscli64 += res;
  192. inclientbuf -= res;
  193. fromclient -= res;
  194. param->clioffset += res;
  195. if(param->clioffset == param->cliinbuf)param->clioffset = param->cliinbuf = 0;
  196. if(param->cliinbuf < param->clibufsize) TOCLIENTBUF = 1;
  197. if(param->bandlimfunc) {
  198. int sl1;
  199. sl1 = (*param->bandlimfunc)(param, 0, res);
  200. if(sl1 > sleeptime) sleeptime = sl1;
  201. }
  202. needaction = 0;
  203. continue;
  204. }
  205. }
  206. if(inserverbuf && TOCLIENT){
  207. #ifdef WITHLOG
  208. log("send to client from buf");
  209. #endif
  210. if(!param->nolongdatfilter){
  211. action = handledatfltsrv(param, &param->srvbuf, (int *)&param->srvbufsize, param->srvinbuf - res, (int *)&param->srvinbuf);
  212. if(action == HANDLED){
  213. RETURN(0);
  214. }
  215. if(action != PASS) RETURN(19);
  216. inserverbuf = param->srvinbuf - param->srvoffset;
  217. }
  218. if(!inserverbuf){
  219. param->srvinbuf = param->srvoffset = 0;
  220. continue;
  221. }
  222. sasize = sizeof(param->sincr);
  223. res = so._sendto(param->clisock, (char *)param->srvbuf + param->srvoffset, (int)MIN(inserverbuf,fromserver), 0, (struct sockaddr*)&param->sincr, sasize);
  224. if(res <= 0) TOCLIENT = 0;
  225. else {
  226. #ifdef WITHLOG
  227. log("done send to client from buf");
  228. #endif
  229. inserverbuf -= res;
  230. fromserver -= res;
  231. param->srvoffset += res;
  232. if(param->srvoffset == param->srvinbuf)param->srvoffset = param->srvinbuf =0;
  233. if(param->srvinbuf < param->srvbufsize) TOSERVERBUF = 1;
  234. needaction = 0;
  235. continue;
  236. }
  237. }
  238. #ifdef WITHSPLICE
  239. if(usesplice){
  240. if(inclientpipe && !inclientbuf && FROMCLIENTPIPE && TOSERVER){
  241. #ifdef WITHLOG
  242. log("send to server from pipe");
  243. #endif
  244. res = splice(pipecli[0], NULL, param->remsock, NULL, MIN(MAXSPLICE, inclientpipe), SPLICE_F_NONBLOCK|SPLICE_F_MOVE);
  245. if(res >0) {
  246. #ifdef WITHLOG
  247. log("done send to server from pipe");
  248. #endif
  249. param->nwrites++;
  250. param->statscli64 += res;
  251. inclientpipe -= res;
  252. fromclient -= res;
  253. if(param->bandlimfunc) {
  254. int sl1;
  255. sl1 = (*param->bandlimfunc)(param, 0, res);
  256. if(sl1 > sleeptime) sleeptime = sl1;
  257. }
  258. needaction = 0;
  259. continue;
  260. }
  261. else {
  262. FROMCLIENTPIPE = TOSERVER = 0;
  263. }
  264. }
  265. if(inserverpipe && !inserverbuf && FROMSERVERPIPE && TOCLIENT){
  266. #ifdef WITHLOG
  267. log("send to client from pipe");
  268. #endif
  269. res = splice(pipesrv[0], NULL, param->clisock, NULL, MIN(MAXSPLICE, inserverpipe), SPLICE_F_NONBLOCK|SPLICE_F_MOVE);
  270. if(res > 0) {
  271. #ifdef WITHLOG
  272. log("done send to client from pipe");
  273. #endif
  274. inserverpipe -= res;
  275. fromserver -= res;
  276. if(fromserver)TOSERVERPIPE = 1;
  277. needaction = 0;
  278. continue;
  279. }
  280. else {
  281. FROMSERVERPIPE = TOCLIENT = 0;
  282. }
  283. }
  284. if(fromclient>inclientpipe && FROMCLIENT && TOCLIENTPIPE){
  285. int error;
  286. socklen_t len=sizeof(error);
  287. #ifdef WITHLOG
  288. log("read from client to pipe");
  289. #endif
  290. res = splice(param->clisock, NULL, pipecli[1], NULL, (int)MIN((uint64_t)MAXSPLICE - inclientpipe, (uint64_t)fromclient-inclientpipe), SPLICE_F_NONBLOCK|SPLICE_F_MOVE);
  291. if(res <= 0) {
  292. #ifdef WITHLOG
  293. log("read failed");
  294. #endif
  295. FROMCLIENT = TOCLIENTPIPE = 0;
  296. if(res == 0) {
  297. CLIENTTERM = 1;
  298. continue;
  299. }
  300. }
  301. else {
  302. #ifdef WITHLOG
  303. log("done read from client to pipe");
  304. #endif
  305. inclientpipe += res;
  306. if(inclientpipe >= MAXSPLICE) TOCLIENTPIPE = 0;
  307. needaction = 0;
  308. continue;
  309. }
  310. }
  311. if(fromserver > inserverpipe && FROMSERVER && TOSERVERPIPE){
  312. int error;
  313. socklen_t len=sizeof(error);
  314. #ifdef WITHLOG
  315. log("read from server to pipe\n");
  316. #endif
  317. res = splice(param->remsock, NULL, pipesrv[1], NULL, MIN(MAXSPLICE - inclientpipe, fromserver - inserverpipe), SPLICE_F_NONBLOCK|SPLICE_F_MOVE);
  318. #ifdef WITHLOG
  319. log("splice finished\n");
  320. #endif
  321. if(res <= 0) {
  322. FROMSERVER = TOSERVERPIPE = 0;
  323. if(res == 0 || !errno) {
  324. SERVERTERM = 1;
  325. continue;
  326. }
  327. }
  328. else {
  329. #ifdef WITHLOG
  330. log("done read from server to pipe\n");
  331. #endif
  332. param->nreads++;
  333. param->statssrv64 += res;
  334. inserverpipe += res;
  335. if(inserverpipe >= MAXSPLICE) TOSERVERPIPE = 0;
  336. if(param->bandlimfunc) {
  337. int sl1;
  338. sl1 = (*param->bandlimfunc)(param, res, 0);
  339. if(sl1 > sleeptime) sleeptime = sl1;
  340. }
  341. if(param->operation == UDPASSOC && param->srv->singlepacket){
  342. fromserver = inserverpipe;
  343. FROMSERVER = 0;
  344. }
  345. needaction = 0;
  346. continue;
  347. }
  348. }
  349. }
  350. else
  351. #endif
  352. {
  353. if(fromclient > inclientbuf && FROMCLIENT && TOCLIENTBUF){
  354. #ifdef WITHLOG
  355. log("read from client to buf");
  356. #endif
  357. sasize = sizeof(param->sincr);
  358. 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);
  359. if(res <= 0) {
  360. FROMCLIENT = 0;
  361. if(res == 0){
  362. CLIENTTERM = 1;
  363. continue;
  364. }
  365. }
  366. else {
  367. #ifdef WITHLOG
  368. log("done read from client to buf");
  369. #endif
  370. inclientbuf += res;
  371. param->cliinbuf += res;
  372. if(param->clibufsize == param->cliinbuf) TOCLIENTBUF = 0;
  373. needaction = 0;
  374. continue;
  375. }
  376. }
  377. if(fromserver > inserverbuf && FROMSERVER && TOSERVERBUF){
  378. #ifdef WITHLOG
  379. log("read from server to buf");
  380. #endif
  381. sasize = sizeof(param->sinsr);
  382. 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);
  383. if(res <= 0) {
  384. FROMSERVER = 0;
  385. if(res == 0) {
  386. SERVERTERM = 1;
  387. continue;
  388. }
  389. }
  390. else {
  391. #ifdef WITHLOG
  392. log("done read from server to buf");
  393. #endif
  394. param->nreads++;
  395. param->statssrv64 += res;
  396. inserverbuf += res;
  397. param->srvinbuf += res;
  398. if(param->bandlimfunc) {
  399. int sl1;
  400. sl1 = (*param->bandlimfunc)(param, res, 0);
  401. if(sl1 > sleeptime) sleeptime = sl1;
  402. }
  403. if(param->srvbufsize == param->srvinbuf) TOSERVERBUF = 0;
  404. if(param->operation == UDPASSOC && param->srv->singlepacket){
  405. fromserver = inserverbuf;
  406. FROMSERVER = 0;
  407. }
  408. needaction = 0;
  409. continue;
  410. }
  411. }
  412. }
  413. for(after = 0; after <= 1; after ++){
  414. fdsc = 0;
  415. if(!after){
  416. memset(fds, 0, sizeof(fds));
  417. }
  418. if(param->monitorsock){
  419. if(!after){
  420. fds[fdsc].fd = *param->monitorsock;
  421. fds[fdsc].events = POLLIN;
  422. fdsc++;
  423. }
  424. else if(fds[fdsc].revents) {
  425. if(param->monaction == INVALID_SOCKET){
  426. so._closesocket(*param->monitorsock);
  427. *param->monitorsock = INVALID_SOCKET;
  428. param->monitorsock = NULL;
  429. }
  430. else RETURN(param->monaction);
  431. }
  432. }
  433. if(!CLIENTTERM){
  434. if(!after){
  435. fds[fdsc].fd = param->clisock;
  436. if(fromclient && !FROMCLIENT && ((
  437. #ifdef WITHSPLICE
  438. !usesplice &&
  439. #endif
  440. TOCLIENTBUF)
  441. #ifdef WITHSPLICE
  442. || (usesplice)
  443. #endif
  444. )){
  445. #ifdef WITHLOG
  446. log("wait reading from client");
  447. #endif
  448. fds[fdsc].events |= (POLLIN);
  449. }
  450. if(!TOCLIENT && (inserverbuf
  451. #ifdef WITHSPLICE
  452. || inserverpipe
  453. #endif
  454. )){
  455. #ifdef WITHLOG
  456. log("wait writing to client");
  457. #endif
  458. fds[fdsc].events |= POLLOUT;
  459. }
  460. }
  461. else{
  462. if(fds[fdsc].revents & (POLLERR|POLLNVAL)) {
  463. CLIENTTERM = 1;
  464. HASERROR |= 1;
  465. }
  466. else if(fds[fdsc].revents & (POLLHUP)) {
  467. CLIENTTERM = 1;
  468. }
  469. else {
  470. if(fds[fdsc].revents & POLLIN) {
  471. #ifdef WITHLOG
  472. log("ready to read from client");
  473. #endif
  474. FROMCLIENT = 1;
  475. }
  476. if(fds[fdsc].revents & POLLOUT) {
  477. #ifdef WITHLOG
  478. log("ready to write to client");
  479. #endif
  480. TOCLIENT = 1;
  481. }
  482. }
  483. }
  484. fdsc++;
  485. }
  486. if(!SERVERTERM){
  487. if(!after){
  488. fds[fdsc].fd = param->remsock;
  489. if(fromserver && !FROMSERVER && ((
  490. #ifdef WITHSPLICE
  491. !usesplice &&
  492. #endif
  493. TOSERVERBUF)
  494. #ifdef WITHSPLICE
  495. || (usesplice)
  496. #endif
  497. )){
  498. #ifdef WITHLOG
  499. log("wait reading from server");
  500. #endif
  501. fds[fdsc].events |= (POLLIN);
  502. }
  503. if(!TOSERVER && (inclientbuf
  504. #ifdef WITHSPLICE
  505. || inclientpipe
  506. #endif
  507. )){
  508. #ifdef WITHLOG
  509. log("wait writing from server");
  510. #endif
  511. fds[fdsc].events |= POLLOUT;
  512. }
  513. }
  514. else{
  515. if(fds[fdsc].revents & (POLLERR|POLLNVAL)) {
  516. #ifdef WITHLOG
  517. log("poll from server failed");
  518. #endif
  519. SERVERTERM = 1;
  520. HASERROR |=2;
  521. }
  522. if(fds[fdsc].revents & (POLLHUP)) {
  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. }