sockmap.c 16 KB

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