tcp_read.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. /*
  28. * History:
  29. * --------
  30. * 2002-12-?? created by andrei.
  31. * 2003-02-10 zero term before calling receive_msg & undo afterward (andrei)
  32. * 2003-05-13 l: (short form of Content-Length) is now recognized (andrei)
  33. * 2003-07-01 tcp_read & friends take no a single tcp_connection
  34. * parameter & they set c->state to S_CONN_EOF on eof (andrei)
  35. * 2003-07-04 fixed tcp EOF handling (possible infinite loop) (andrei)
  36. * 2005-07-05 migrated to the new io_wait code (andrei)
  37. * 2006-02-03 use tsend_stream instead of send_all (andrei)
  38. * 2006-10-13 added STUN support - state machine for TCP (vlada)
  39. * 2007-02-20 fixed timeout calc. bug (andrei)
  40. * 2007-11-26 improved tcp timers: switched to local_timer (andrei)
  41. * 2008-02-04 optimizations: handle POLLRDHUP (if supported), detect short
  42. * reads (sock. buffer empty) (andrei)
  43. * 2009-02-26 direct blacklist support (andrei)
  44. */
  45. #ifdef USE_TCP
  46. #include <stdio.h>
  47. #include <errno.h>
  48. #include <string.h>
  49. #include <sys/time.h>
  50. #include <sys/types.h>
  51. #include <sys/select.h>
  52. #include <sys/socket.h>
  53. #include <unistd.h>
  54. #include <stdlib.h> /* for abort() */
  55. #include "dprint.h"
  56. #include "tcp_conn.h"
  57. #include "pass_fd.h"
  58. #include "globals.h"
  59. #include "receive.h"
  60. #include "timer.h"
  61. #include "local_timer.h"
  62. #include "ut.h"
  63. #include "pt.h"
  64. #include "cfg/cfg_struct.h"
  65. #ifdef CORE_TLS
  66. #include "tls/tls_server.h"
  67. #else
  68. #include "tls_hooks.h"
  69. #endif /* CORE_TLS */
  70. #ifdef USE_DST_BLACKLIST
  71. #include "dst_blacklist.h"
  72. #endif /* USE_DST_BLACKLIST */
  73. #define HANDLE_IO_INLINE
  74. #include "io_wait.h"
  75. #include <fcntl.h> /* must be included after io_wait.h if SIGIO_RT is used */
  76. #include "tsend.h"
  77. #include "forward.h"
  78. #ifdef USE_STUN
  79. #include "ser_stun.h"
  80. int is_msg_complete(struct tcp_req* r);
  81. #endif /* USE_STUN */
  82. #define TCPCONN_TIMEOUT_MIN_RUN 1 /* run the timers each new tick */
  83. #define RD_CONN_SHORT_READ 1
  84. #define RD_CONN_EOF 2
  85. #define RD_CONN_FORCE_EOF 65536
  86. /* types used in io_wait* */
  87. enum fd_types { F_NONE, F_TCPMAIN, F_TCPCONN };
  88. /* list of tcp connections handled by this process */
  89. static struct tcp_connection* tcp_conn_lst=0;
  90. static io_wait_h io_w; /* io_wait handler*/
  91. static int tcpmain_sock=-1;
  92. static struct local_timer tcp_reader_ltimer;
  93. static ticks_t tcp_reader_prev_ticks;
  94. /* reads next available bytes
  95. * c- tcp connection used for reading, tcp_read changes also c->state on
  96. * EOF and c->req.error on read error
  97. * * flags - value/result - used to signal a seen or "forced" EOF on the
  98. * connection (when it is known that no more data will come after the
  99. * current socket buffer is emptied )=> return/signal EOF on the first
  100. * short read (=> don't use it on POLLPRI, as OOB data will cause short
  101. * reads even if there are still remaining bytes in the socket buffer)
  102. * return number of bytes read, 0 on EOF or -1 on error,
  103. * on EOF it also sets c->state to S_CONN_EOF.
  104. * (to distinguish from reads that would block which could return 0)
  105. * RD_CONN_SHORT_READ is also set in *flags for short reads.
  106. * sets also r->error */
  107. int tcp_read(struct tcp_connection *c, int* flags)
  108. {
  109. int bytes_free, bytes_read;
  110. struct tcp_req *r;
  111. int fd;
  112. r=&c->req;
  113. fd=c->fd;
  114. bytes_free=TCP_BUF_SIZE- (int)(r->pos - r->buf);
  115. if (bytes_free==0){
  116. LOG(L_ERR, "ERROR: tcp_read: buffer overrun, dropping\n");
  117. r->error=TCP_REQ_OVERRUN;
  118. return -1;
  119. }
  120. again:
  121. bytes_read=read(fd, r->pos, bytes_free);
  122. if (likely(bytes_read!=bytes_free)){
  123. if(unlikely(bytes_read==-1)){
  124. if (errno == EWOULDBLOCK || errno == EAGAIN){
  125. bytes_read=0; /* nothing has been read */
  126. }else if (errno == EINTR) goto again;
  127. else{
  128. #ifdef USE_DST_BLACKLIST
  129. if (cfg_get(core, core_cfg, use_dst_blacklist))
  130. switch(errno){
  131. case ECONNRESET:
  132. case ETIMEDOUT:
  133. dst_blacklist_su((c->state==S_CONN_CONNECT)?
  134. BLST_ERR_CONNECT:
  135. BLST_ERR_SEND,
  136. c->rcv.proto,
  137. &c->rcv.src_su, 0);
  138. break;
  139. }
  140. #endif /* USE_DST_BLACKLIST */
  141. LOG(L_ERR, "ERROR: tcp_read: error reading: %s (%d)\n",
  142. strerror(errno), errno);
  143. r->error=TCP_READ_ERROR;
  144. return -1;
  145. }
  146. }else if (unlikely((bytes_read==0) ||
  147. (*flags & RD_CONN_FORCE_EOF))){
  148. c->state=S_CONN_EOF;
  149. *flags|=RD_CONN_EOF;
  150. DBG("tcp_read: EOF on %p, FD %d\n", c, fd);
  151. }else{
  152. if (unlikely(c->state==S_CONN_CONNECT))
  153. c->state=S_CONN_OK;
  154. }
  155. /* short read */
  156. *flags|=RD_CONN_SHORT_READ;
  157. }else{ /* else normal full read */
  158. if (unlikely(c->state==S_CONN_CONNECT))
  159. c->state=S_CONN_OK;
  160. }
  161. #ifdef EXTRA_DEBUG
  162. DBG("tcp_read: read %d bytes:\n%.*s\n", bytes_read, bytes_read, r->pos);
  163. #endif
  164. r->pos+=bytes_read;
  165. return bytes_read;
  166. }
  167. /* reads all headers (until double crlf), & parses the content-length header
  168. * (WARNING: inefficient, tries to reuse receive_msg but will go through
  169. * the headers twice [once here looking for Content-Length and for the end
  170. * of the headers and once in receive_msg]; a more speed efficient version will
  171. * result in either major code duplication or major changes to the receive code)
  172. * returns number of bytes read & sets r->state & r->body
  173. * when either r->body!=0 or r->state==H_BODY =>
  174. * all headers have been read. It should be called in a while loop.
  175. * returns < 0 if error or 0 if EOF */
  176. int tcp_read_headers(struct tcp_connection *c, int* read_flags)
  177. {
  178. int bytes, remaining;
  179. char *p;
  180. struct tcp_req* r;
  181. #ifdef USE_STUN
  182. unsigned int mc; /* magic cookie */
  183. unsigned short body_len;
  184. #endif
  185. #define crlf_default_skip_case \
  186. case '\n': \
  187. r->state=H_LF; \
  188. break; \
  189. default: \
  190. r->state=H_SKIP
  191. #define content_len_beg_case \
  192. case ' ': \
  193. case '\t': \
  194. if (!r->has_content_len) r->state=H_STARTWS; \
  195. else r->state=H_SKIP; \
  196. /* not interested if we already found one */ \
  197. break; \
  198. case 'C': \
  199. case 'c': \
  200. if(!r->has_content_len) r->state=H_CONT_LEN1; \
  201. else r->state=H_SKIP; \
  202. break; \
  203. case 'l': \
  204. case 'L': \
  205. /* short form for Content-Length */ \
  206. if (!r->has_content_len) r->state=H_L_COLON; \
  207. else r->state=H_SKIP; \
  208. break
  209. #define change_state(upper, lower, newstate)\
  210. switch(*p){ \
  211. case upper: \
  212. case lower: \
  213. r->state=(newstate); break; \
  214. crlf_default_skip_case; \
  215. }
  216. #define change_state_case(state0, upper, lower, newstate)\
  217. case state0: \
  218. change_state(upper, lower, newstate); \
  219. p++; \
  220. break
  221. r=&c->req;
  222. /* if we still have some unparsed part, parse it first, don't do the read*/
  223. if (unlikely(r->parsed<r->pos)){
  224. bytes=0;
  225. }else{
  226. #ifdef USE_TLS
  227. if (unlikely(c->type==PROTO_TLS))
  228. bytes=tls_read(c); /* FIXME: read_flags support */
  229. else
  230. #endif
  231. bytes=tcp_read(c, read_flags);
  232. if (bytes<=0) return bytes;
  233. }
  234. p=r->parsed;
  235. while(p<r->pos && r->error==TCP_REQ_OK){
  236. switch((unsigned char)r->state){
  237. case H_BODY: /* read the body*/
  238. remaining=r->pos-p;
  239. if (remaining>r->bytes_to_go) remaining=r->bytes_to_go;
  240. r->bytes_to_go-=remaining;
  241. p+=remaining;
  242. if (r->bytes_to_go==0){
  243. r->complete=1;
  244. goto skip;
  245. }
  246. break;
  247. case H_SKIP:
  248. /* find lf, we are in this state if we are not interested
  249. * in anything till end of line*/
  250. p=q_memchr(p, '\n', r->pos-p);
  251. if (p){
  252. p++;
  253. r->state=H_LF;
  254. }else{
  255. p=r->pos;
  256. }
  257. break;
  258. case H_LF:
  259. /* terminate on LF CR LF or LF LF */
  260. switch (*p){
  261. case '\r':
  262. r->state=H_LFCR;
  263. break;
  264. case '\n':
  265. /* found LF LF */
  266. r->state=H_BODY;
  267. if (r->has_content_len){
  268. r->body=p+1;
  269. r->bytes_to_go=r->content_len;
  270. if (r->bytes_to_go==0){
  271. r->complete=1;
  272. p++;
  273. goto skip;
  274. }
  275. }else{
  276. DBG("tcp_read_headers: ERROR: no clen, p=%X\n",
  277. *p);
  278. r->error=TCP_REQ_BAD_LEN;
  279. }
  280. break;
  281. content_len_beg_case;
  282. default:
  283. r->state=H_SKIP;
  284. }
  285. p++;
  286. break;
  287. case H_LFCR:
  288. if (*p=='\n'){
  289. /* found LF CR LF */
  290. r->state=H_BODY;
  291. if (r->has_content_len){
  292. r->body=p+1;
  293. r->bytes_to_go=r->content_len;
  294. if (r->bytes_to_go==0){
  295. r->complete=1;
  296. p++;
  297. goto skip;
  298. }
  299. }else{
  300. DBG("tcp_read_headers: ERROR: no clen, p=%X\n",
  301. *p);
  302. r->error=TCP_REQ_BAD_LEN;
  303. }
  304. }else r->state=H_SKIP;
  305. p++;
  306. break;
  307. case H_STARTWS:
  308. switch (*p){
  309. content_len_beg_case;
  310. crlf_default_skip_case;
  311. }
  312. p++;
  313. break;
  314. case H_SKIP_EMPTY:
  315. switch (*p){
  316. case '\n':
  317. break;
  318. case '\r':
  319. if (tcp_options.crlf_ping) {
  320. r->state=H_SKIP_EMPTY_CR_FOUND;
  321. r->start=p;
  322. }
  323. break;
  324. case ' ':
  325. case '\t':
  326. /* skip empty lines */
  327. break;
  328. case 'C':
  329. case 'c':
  330. r->state=H_CONT_LEN1;
  331. r->start=p;
  332. break;
  333. case 'l':
  334. case 'L':
  335. /* short form for Content-Length */
  336. r->state=H_L_COLON;
  337. r->start=p;
  338. break;
  339. default:
  340. #ifdef USE_STUN
  341. /* STUN support can be switched off even if it's compiled */
  342. /* stun test */
  343. if (stun_allow_stun && (unsigned char)*p == 0x00) {
  344. r->state=H_STUN_MSG;
  345. /* body will used as pointer to the last used byte */
  346. r->body=p;
  347. body_len = 0;
  348. DBG("stun msg detected\n");
  349. }else
  350. #endif
  351. r->state=H_SKIP;
  352. r->start=p;
  353. };
  354. p++;
  355. break;
  356. case H_SKIP_EMPTY_CR_FOUND:
  357. if (*p=='\n'){
  358. r->state=H_SKIP_EMPTY_CRLF_FOUND;
  359. p++;
  360. }else{
  361. r->state=H_SKIP_EMPTY;
  362. }
  363. break;
  364. case H_SKIP_EMPTY_CRLF_FOUND:
  365. if (*p=='\r'){
  366. r->state = H_SKIP_EMPTY_CRLFCR_FOUND;
  367. p++;
  368. }else{
  369. r->state = H_SKIP_EMPTY;
  370. }
  371. break;
  372. case H_SKIP_EMPTY_CRLFCR_FOUND:
  373. if (*p=='\n'){
  374. r->state = H_PING_CRLF;
  375. r->complete = 1;
  376. r->has_content_len = 1; /* hack to avoid error check */
  377. p++;
  378. goto skip;
  379. }else{
  380. r->state = H_SKIP_EMPTY;
  381. }
  382. break;
  383. #ifdef USE_STUN
  384. case H_STUN_MSG:
  385. if ((r->pos - r->body) >= sizeof(struct stun_hdr)) {
  386. r->content_len = 0;
  387. /* copy second short from buffer where should be body
  388. * length
  389. */
  390. memcpy(&body_len, &r->start[sizeof(unsigned short)],
  391. sizeof(unsigned short));
  392. body_len = ntohs(r->content_len);
  393. /* check if there is valid magic cookie */
  394. memcpy(&mc, &r->start[sizeof(unsigned int)],
  395. sizeof(unsigned int));
  396. mc = ntohl(mc);
  397. /* using has_content_len as a flag if there should be
  398. * fingerprint or no
  399. */
  400. r->has_content_len = (mc == MAGIC_COOKIE) ? 1 : 0;
  401. r->body += sizeof(struct stun_hdr);
  402. p = r->body;
  403. if (body_len > 0) {
  404. r->state = H_STUN_READ_BODY;
  405. }
  406. else {
  407. if (is_msg_complete(r) != 0) {
  408. goto skip;
  409. }
  410. else {
  411. /* set content_len to length of fingerprint */
  412. body_len = sizeof(struct stun_attr) +
  413. SHA_DIGEST_LENGTH;
  414. }
  415. }
  416. }
  417. else {
  418. p = r->pos;
  419. }
  420. break;
  421. case H_STUN_READ_BODY:
  422. /* check if the whole body was read */
  423. if ((r->pos - r->body) >= body_len) {
  424. r->body += body_len;
  425. p = r->body;
  426. if (is_msg_complete(r) != 0) {
  427. goto skip;
  428. }
  429. else {
  430. /* set content_len to length of fingerprint */
  431. body_len = sizeof(struct stun_attr)+SHA_DIGEST_LENGTH;
  432. }
  433. }
  434. else {
  435. p = r->pos;
  436. }
  437. break;
  438. case H_STUN_FP:
  439. /* content_len contains length of fingerprint in this place! */
  440. if ((r->pos - r->body) >= body_len) {
  441. r->body += body_len;
  442. p = r->body;
  443. r->state = H_STUN_END;
  444. r->complete = 1;
  445. r->has_content_len = 1; /* hack to avoid error check */
  446. goto skip;
  447. }
  448. else {
  449. p = r->pos;
  450. }
  451. break;
  452. #endif /* USE_STUN */
  453. change_state_case(H_CONT_LEN1, 'O', 'o', H_CONT_LEN2);
  454. change_state_case(H_CONT_LEN2, 'N', 'n', H_CONT_LEN3);
  455. change_state_case(H_CONT_LEN3, 'T', 't', H_CONT_LEN4);
  456. change_state_case(H_CONT_LEN4, 'E', 'e', H_CONT_LEN5);
  457. change_state_case(H_CONT_LEN5, 'N', 'n', H_CONT_LEN6);
  458. change_state_case(H_CONT_LEN6, 'T', 't', H_CONT_LEN7);
  459. change_state_case(H_CONT_LEN7, '-', '_', H_CONT_LEN8);
  460. change_state_case(H_CONT_LEN8, 'L', 'l', H_CONT_LEN9);
  461. change_state_case(H_CONT_LEN9, 'E', 'e', H_CONT_LEN10);
  462. change_state_case(H_CONT_LEN10, 'N', 'n', H_CONT_LEN11);
  463. change_state_case(H_CONT_LEN11, 'G', 'g', H_CONT_LEN12);
  464. change_state_case(H_CONT_LEN12, 'T', 't', H_CONT_LEN13);
  465. change_state_case(H_CONT_LEN13, 'H', 'h', H_L_COLON);
  466. case H_L_COLON:
  467. switch(*p){
  468. case ' ':
  469. case '\t':
  470. break; /* skip space */
  471. case ':':
  472. r->state=H_CONT_LEN_BODY;
  473. break;
  474. crlf_default_skip_case;
  475. };
  476. p++;
  477. break;
  478. case H_CONT_LEN_BODY:
  479. switch(*p){
  480. case ' ':
  481. case '\t':
  482. break; /* eat space */
  483. case '0':
  484. case '1':
  485. case '2':
  486. case '3':
  487. case '4':
  488. case '5':
  489. case '6':
  490. case '7':
  491. case '8':
  492. case '9':
  493. r->state=H_CONT_LEN_BODY_PARSE;
  494. r->content_len=(*p-'0');
  495. break;
  496. /*FIXME: content length on different lines ! */
  497. crlf_default_skip_case;
  498. }
  499. p++;
  500. break;
  501. case H_CONT_LEN_BODY_PARSE:
  502. switch(*p){
  503. case '0':
  504. case '1':
  505. case '2':
  506. case '3':
  507. case '4':
  508. case '5':
  509. case '6':
  510. case '7':
  511. case '8':
  512. case '9':
  513. r->content_len=r->content_len*10+(*p-'0');
  514. break;
  515. case '\r':
  516. case ' ':
  517. case '\t': /* FIXME: check if line contains only WS */
  518. r->state=H_SKIP;
  519. r->has_content_len=1;
  520. break;
  521. case '\n':
  522. /* end of line, parse successful */
  523. r->state=H_LF;
  524. r->has_content_len=1;
  525. break;
  526. default:
  527. LOG(L_ERR, "ERROR: tcp_read_headers: bad "
  528. "Content-Length header value, unexpected "
  529. "char %c in state %d\n", *p, r->state);
  530. r->state=H_SKIP; /* try to find another?*/
  531. }
  532. p++;
  533. break;
  534. default:
  535. LOG(L_CRIT, "BUG: tcp_read_headers: unexpected state %d\n",
  536. r->state);
  537. abort();
  538. }
  539. }
  540. skip:
  541. r->parsed=p;
  542. return bytes;
  543. }
  544. int tcp_read_req(struct tcp_connection* con, int* bytes_read, int* read_flags)
  545. {
  546. int bytes;
  547. int total_bytes;
  548. int resp;
  549. long size;
  550. struct tcp_req* req;
  551. struct dest_info dst;
  552. int s;
  553. char c;
  554. int ret;
  555. bytes=-1;
  556. total_bytes=0;
  557. resp=CONN_RELEASE;
  558. s=con->fd;
  559. req=&con->req;
  560. #ifdef USE_TLS
  561. if (con->type==PROTO_TLS){
  562. if (tls_fix_read_conn(con)!=0){
  563. resp=CONN_ERROR;
  564. goto end_req;
  565. }
  566. if(con->state!=S_CONN_OK) goto end_req; /* not enough data */
  567. }
  568. #endif
  569. again:
  570. if (likely(req->error==TCP_REQ_OK)){
  571. bytes=tcp_read_headers(con, read_flags);
  572. #ifdef EXTRA_DEBUG
  573. /* if timeout state=0; goto end__req; */
  574. DBG("read= %d bytes, parsed=%d, state=%d, error=%d\n",
  575. bytes, (int)(req->parsed-req->start), req->state,
  576. req->error );
  577. DBG("tcp_read_req: last char=0x%02X, parsed msg=\n%.*s\n",
  578. *(req->parsed-1), (int)(req->parsed-req->start),
  579. req->start);
  580. #endif
  581. if (unlikely(bytes==-1)){
  582. LOG(L_ERR, "ERROR: tcp_read_req: error reading \n");
  583. resp=CONN_ERROR;
  584. goto end_req;
  585. }
  586. total_bytes+=bytes;
  587. /* eof check:
  588. * is EOF if eof on fd and req. not complete yet,
  589. * if req. is complete we might have a second unparsed
  590. * request after it, so postpone release_with_eof
  591. */
  592. if (unlikely((con->state==S_CONN_EOF) && (req->complete==0))) {
  593. DBG( "tcp_read_req: EOF\n");
  594. resp=CONN_EOF;
  595. goto end_req;
  596. }
  597. }
  598. if (unlikely(req->error!=TCP_REQ_OK)){
  599. LOG(L_ERR,"ERROR: tcp_read_req: bad request, state=%d, error=%d "
  600. "buf:\n%.*s\nparsed:\n%.*s\n", req->state, req->error,
  601. (int)(req->pos-req->buf), req->buf,
  602. (int)(req->parsed-req->start), req->start);
  603. DBG("- received from: port %d\n", con->rcv.src_port);
  604. print_ip("- received from: ip ",&con->rcv.src_ip, "\n");
  605. resp=CONN_ERROR;
  606. goto end_req;
  607. }
  608. if (likely(req->complete)){
  609. #ifdef EXTRA_DEBUG
  610. DBG("tcp_read_req: end of header part\n");
  611. DBG("- received from: port %d\n", con->rcv.src_port);
  612. print_ip("- received from: ip ", &con->rcv.src_ip, "\n");
  613. DBG("tcp_read_req: headers:\n%.*s.\n",
  614. (int)(req->body-req->start), req->start);
  615. #endif
  616. if (likely(req->has_content_len)){
  617. DBG("tcp_read_req: content-length= %d\n", req->content_len);
  618. #ifdef EXTRA_DEBUG
  619. DBG("tcp_read_req: body:\n%.*s\n", req->content_len,req->body);
  620. #endif
  621. }else{
  622. req->error=TCP_REQ_BAD_LEN;
  623. LOG(L_ERR, "ERROR: tcp_read_req: content length not present or"
  624. " unparsable\n");
  625. resp=CONN_ERROR;
  626. goto end_req;
  627. }
  628. /* if we are here everything is nice and ok*/
  629. resp=CONN_RELEASE;
  630. #ifdef EXTRA_DEBUG
  631. DBG("calling receive_msg(%p, %d, )\n",
  632. req->start, (int)(req->parsed-req->start));
  633. #endif
  634. /* rcv.bind_address should always be !=0 */
  635. bind_address=con->rcv.bind_address;
  636. /* just for debugging use sendipv4 as receiving socket FIXME*/
  637. /*
  638. if (con->rcv.dst_ip.af==AF_INET6){
  639. bind_address=sendipv6_tcp;
  640. }else{
  641. bind_address=sendipv4_tcp;
  642. }
  643. */
  644. con->rcv.proto_reserved1=con->id; /* copy the id */
  645. c=*req->parsed; /* ugly hack: zero term the msg & save the
  646. previous char, req->parsed should be ok
  647. because we always alloc BUF_SIZE+1 */
  648. *req->parsed=0;
  649. if (req->state==H_PING_CRLF) {
  650. init_dst_from_rcv(&dst, &con->rcv);
  651. if (tcp_send(&dst, 0, CRLF, CRLF_LEN) < 0) {
  652. LOG(L_ERR, "CRLF ping: tcp_send() failed\n");
  653. }
  654. ret = 0;
  655. }else
  656. #ifdef USE_STUN
  657. if (unlikely(req->state==H_STUN_END)){
  658. /* stun request */
  659. ret = stun_process_msg(req->start, req->parsed-req->start,
  660. &con->rcv);
  661. }else
  662. #endif
  663. ret = receive_msg(req->start, req->parsed-req->start,
  664. &con->rcv);
  665. if (unlikely(ret < 0)) {
  666. *req->parsed=c;
  667. resp=CONN_ERROR;
  668. goto end_req;
  669. }
  670. *req->parsed=c;
  671. /* prepare for next request */
  672. size=req->pos-req->parsed;
  673. req->start=req->buf;
  674. req->body=0;
  675. req->error=TCP_REQ_OK;
  676. req->state=H_SKIP_EMPTY;
  677. req->complete=req->content_len=req->has_content_len=0;
  678. req->bytes_to_go=0;
  679. req->pos=req->buf+size;
  680. if (unlikely(size)){
  681. memmove(req->buf, req->parsed, size);
  682. req->parsed=req->buf; /* fix req->parsed after using it */
  683. #ifdef EXTRA_DEBUG
  684. DBG("tcp_read_req: preparing for new request, kept %ld"
  685. " bytes\n", size);
  686. #endif
  687. /*if we still have some unparsed bytes, try to parse them too*/
  688. goto again;
  689. } else if (unlikely(con->state==S_CONN_EOF)){
  690. DBG( "tcp_read_req: EOF after reading complete request\n");
  691. resp=CONN_EOF;
  692. }
  693. req->parsed=req->buf; /* fix req->parsed */
  694. }
  695. end_req:
  696. if (likely(bytes_read)) *bytes_read=total_bytes;
  697. return resp;
  698. }
  699. void release_tcpconn(struct tcp_connection* c, long state, int unix_sock)
  700. {
  701. long response[2];
  702. DBG( "releasing con %p, state %ld, fd=%d, id=%d\n",
  703. c, state, c->fd, c->id);
  704. DBG(" extra_data %p\n", c->extra_data);
  705. /* release req & signal the parent */
  706. c->reader_pid=0; /* reset it */
  707. if (c->fd!=-1){
  708. close(c->fd);
  709. c->fd=-1;
  710. }
  711. /* errno==EINTR, EWOULDBLOCK a.s.o todo */
  712. response[0]=(long)c;
  713. response[1]=state;
  714. if (tsend_stream(unix_sock, (char*)response, sizeof(response), -1)<=0)
  715. LOG(L_ERR, "ERROR: release_tcpconn: tsend_stream failed\n");
  716. }
  717. static ticks_t tcpconn_read_timeout(ticks_t t, struct timer_ln* tl, void* data)
  718. {
  719. struct tcp_connection *c;
  720. c=(struct tcp_connection*)data;
  721. /* or (struct tcp...*)(tl-offset(c->timer)) */
  722. if (likely(!(c->state<0) && TICKS_LT(t, c->timeout))){
  723. /* timeout extended, exit */
  724. return (ticks_t)(c->timeout - t);
  725. }
  726. /* if conn->state is ERROR or BAD => force timeout too */
  727. if (unlikely(io_watch_del(&io_w, c->fd, -1, IO_FD_CLOSING)<0)){
  728. LOG(L_ERR, "ERROR: tcpconn_read_timeout: io_watch_del failed for %p"
  729. " id %d fd %d, state %d, flags %x, main fd %d\n",
  730. c, c->id, c->fd, c->state, c->flags, c->s);
  731. }
  732. tcpconn_listrm(tcp_conn_lst, c, c_next, c_prev);
  733. release_tcpconn(c, (c->state<0)?CONN_ERROR:CONN_RELEASE, tcpmain_sock);
  734. return 0;
  735. }
  736. /* handle io routine, based on the fd_map type
  737. * (it will be called from io_wait_loop* )
  738. * params: fm - pointer to a fd hash entry
  739. * idx - index in the fd_array (or -1 if not known)
  740. * return: -1 on error, or when we are not interested any more on reads
  741. * from this fd (e.g.: we are closing it )
  742. * 0 on EAGAIN or when by some other way it is known that no more
  743. * io events are queued on the fd (the receive buffer is empty).
  744. * Usefull to detect when there are no more io events queued for
  745. * sigio_rt, epoll_et, kqueue.
  746. * >0 on successfull read from the fd (when there might be more io
  747. * queued -- the receive buffer might still be non-empty)
  748. */
  749. inline static int handle_io(struct fd_map* fm, short events, int idx)
  750. {
  751. int ret;
  752. int n;
  753. int read_flags;
  754. struct tcp_connection* con;
  755. int s;
  756. long resp;
  757. ticks_t t;
  758. /* update the local config */
  759. cfg_update();
  760. switch(fm->type){
  761. case F_TCPMAIN:
  762. again:
  763. ret=n=receive_fd(fm->fd, &con, sizeof(con), &s, 0);
  764. DBG("received n=%d con=%p, fd=%d\n", n, con, s);
  765. if (unlikely(n<0)){
  766. if (errno == EWOULDBLOCK || errno == EAGAIN){
  767. ret=0;
  768. break;
  769. }else if (errno == EINTR) goto again;
  770. else{
  771. LOG(L_CRIT,"BUG: tcp_receive: handle_io: read_fd: %s \n",
  772. strerror(errno));
  773. abort(); /* big error*/
  774. }
  775. }
  776. if (unlikely(n==0)){
  777. LOG(L_ERR, "WARNING: tcp_receive: handle_io: 0 bytes read\n");
  778. goto error;
  779. }
  780. if (unlikely(con==0)){
  781. LOG(L_CRIT, "BUG: tcp_receive: handle_io null pointer\n");
  782. goto error;
  783. }
  784. con->fd=s;
  785. if (unlikely(s==-1)) {
  786. LOG(L_ERR, "ERROR: tcp_receive: handle_io: read_fd:"
  787. "no fd read\n");
  788. goto con_error;
  789. }
  790. con->reader_pid=my_pid();
  791. if (unlikely(con==tcp_conn_lst)){
  792. LOG(L_CRIT, "BUG: tcp_receive: handle_io: duplicate"
  793. " connection received: %p, id %d, fd %d, refcnt %d"
  794. " state %d (n=%d)\n", con, con->id, con->fd,
  795. atomic_get(&con->refcnt), con->state, n);
  796. goto con_error;
  797. break; /* try to recover */
  798. }
  799. if (unlikely(con->state==S_CONN_BAD)){
  800. LOG(L_WARN, "WARNING: tcp_receive: handle_io: received an"
  801. " already bad connection: %p id %d refcnt %d\n",
  802. con, con->id, atomic_get(&con->refcnt));
  803. goto con_error;
  804. }
  805. /* if we received the fd there is most likely data waiting to
  806. * be read => process it first to avoid extra sys calls */
  807. read_flags=((con->flags & (F_CONN_EOF_SEEN|F_CONN_FORCE_EOF)) &&
  808. !(con->flags & F_CONN_OOB_DATA))? RD_CONN_FORCE_EOF
  809. :0;
  810. resp=tcp_read_req(con, &n, &read_flags);
  811. if (unlikely(resp<0)){
  812. /* some error occured, but on the new fd, not on the tcp
  813. * main fd, so keep the ret value */
  814. if (unlikely(resp!=CONN_EOF))
  815. con->state=S_CONN_BAD;
  816. release_tcpconn(con, resp, tcpmain_sock);
  817. break;
  818. }
  819. /* must be before io_watch_add, io_watch_add might catch some
  820. * already existing events => might call handle_io and
  821. * handle_io might decide to del. the new connection =>
  822. * must be in the list */
  823. tcpconn_listadd(tcp_conn_lst, con, c_next, c_prev);
  824. t=get_ticks_raw();
  825. con->timeout=t+S_TO_TICKS(TCP_CHILD_TIMEOUT);
  826. /* re-activate the timer */
  827. con->timer.f=tcpconn_read_timeout;
  828. local_timer_reinit(&con->timer);
  829. local_timer_add(&tcp_reader_ltimer, &con->timer,
  830. S_TO_TICKS(TCP_CHILD_TIMEOUT), t);
  831. if (unlikely(io_watch_add(&io_w, s, POLLIN, F_TCPCONN, con)<0)){
  832. LOG(L_CRIT, "ERROR: tcpconn_receive: handle_io: io_watch_add "
  833. "failed for %p id %d fd %d, state %d, flags %x,"
  834. " main fd %d, refcnt %d\n",
  835. con, con->id, con->fd, con->state, con->flags,
  836. con->s, atomic_get(&con->refcnt));
  837. tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev);
  838. local_timer_del(&tcp_reader_ltimer, &con->timer);
  839. goto con_error;
  840. }
  841. break;
  842. case F_TCPCONN:
  843. con=(struct tcp_connection*)fm->data;
  844. if (unlikely(con->state==S_CONN_BAD)){
  845. resp=CONN_ERROR;
  846. LOG(L_WARN, "WARNING: tcp_receive: handle_io: F_TCPCONN"
  847. " connection marked as bad: %p id %d refcnt %d\n",
  848. con, con->id, atomic_get(&con->refcnt));
  849. goto read_error;
  850. }
  851. #ifdef POLLRDHUP
  852. read_flags=(((events & POLLRDHUP) |
  853. (con->flags & (F_CONN_EOF_SEEN|F_CONN_FORCE_EOF)))
  854. && !(events & POLLPRI))? RD_CONN_FORCE_EOF: 0;
  855. #else /* POLLRDHUP */
  856. read_flags=0;
  857. #endif /* POLLRDHUP */
  858. resp=tcp_read_req(con, &ret, &read_flags);
  859. if (unlikely(resp<0)){
  860. read_error:
  861. ret=-1; /* some error occured */
  862. if (unlikely(io_watch_del(&io_w, con->fd, idx,
  863. IO_FD_CLOSING) < 0)){
  864. LOG(L_CRIT, "ERROR: tcpconn_receive: handle_io: "
  865. "io_watch_del failed for %p id %d fd %d,"
  866. " state %d, flags %x, main fd %d, refcnt %d\n",
  867. con, con->id, con->fd, con->state,
  868. con->flags, con->s, atomic_get(&con->refcnt));
  869. }
  870. tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev);
  871. local_timer_del(&tcp_reader_ltimer, &con->timer);
  872. if (unlikely(resp!=CONN_EOF))
  873. con->state=S_CONN_BAD;
  874. release_tcpconn(con, resp, tcpmain_sock);
  875. }else{
  876. /* update timeout */
  877. con->timeout=get_ticks_raw()+S_TO_TICKS(TCP_CHILD_TIMEOUT);
  878. /* ret= 0 (read the whole socket buffer) if short read &
  879. * !POLLPRI, bytes read otherwise */
  880. ret&=(((read_flags & RD_CONN_SHORT_READ) &&
  881. !(events & POLLPRI)) - 1);
  882. }
  883. break;
  884. case F_NONE:
  885. LOG(L_CRIT, "BUG: handle_io: empty fd map %p (%d): "
  886. "{%d, %d, %p}\n", fm, (int)(fm-io_w.fd_hash),
  887. fm->fd, fm->type, fm->data);
  888. goto error;
  889. default:
  890. LOG(L_CRIT, "BUG: handle_io: uknown fd type %d\n", fm->type);
  891. goto error;
  892. }
  893. return ret;
  894. con_error:
  895. con->state=S_CONN_BAD;
  896. release_tcpconn(con, CONN_ERROR, tcpmain_sock);
  897. return ret;
  898. error:
  899. return -1;
  900. }
  901. inline static void tcp_reader_timer_run()
  902. {
  903. ticks_t ticks;
  904. ticks=get_ticks_raw();
  905. if (unlikely((ticks-tcp_reader_prev_ticks)<TCPCONN_TIMEOUT_MIN_RUN))
  906. return;
  907. tcp_reader_prev_ticks=ticks;
  908. local_timer_run(&tcp_reader_ltimer, ticks);
  909. }
  910. void tcp_receive_loop(int unix_sock)
  911. {
  912. /* init */
  913. tcpmain_sock=unix_sock; /* init com. socket */
  914. if (init_io_wait(&io_w, get_max_open_fds(), tcp_poll_method)<0)
  915. goto error;
  916. tcp_reader_prev_ticks=get_ticks_raw();
  917. if (init_local_timer(&tcp_reader_ltimer, get_ticks_raw())!=0)
  918. goto error;
  919. /* add the unix socket */
  920. if (io_watch_add(&io_w, tcpmain_sock, POLLIN, F_TCPMAIN, 0)<0){
  921. LOG(L_CRIT, "ERROR: tcp_receive_loop: init: failed to add socket "
  922. " to the fd list\n");
  923. goto error;
  924. }
  925. /* initialize the config framework */
  926. if (cfg_child_init()) goto error;
  927. /* main loop */
  928. switch(io_w.poll_method){
  929. case POLL_POLL:
  930. while(1){
  931. io_wait_loop_poll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  932. tcp_reader_timer_run();
  933. }
  934. break;
  935. #ifdef HAVE_SELECT
  936. case POLL_SELECT:
  937. while(1){
  938. io_wait_loop_select(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  939. tcp_reader_timer_run();
  940. }
  941. break;
  942. #endif
  943. #ifdef HAVE_SIGIO_RT
  944. case POLL_SIGIO_RT:
  945. while(1){
  946. io_wait_loop_sigio_rt(&io_w, TCP_CHILD_SELECT_TIMEOUT);
  947. tcp_reader_timer_run();
  948. }
  949. break;
  950. #endif
  951. #ifdef HAVE_EPOLL
  952. case POLL_EPOLL_LT:
  953. while(1){
  954. io_wait_loop_epoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  955. tcp_reader_timer_run();
  956. }
  957. break;
  958. case POLL_EPOLL_ET:
  959. while(1){
  960. io_wait_loop_epoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 1);
  961. tcp_reader_timer_run();
  962. }
  963. break;
  964. #endif
  965. #ifdef HAVE_KQUEUE
  966. case POLL_KQUEUE:
  967. while(1){
  968. io_wait_loop_kqueue(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  969. tcp_reader_timer_run();
  970. }
  971. break;
  972. #endif
  973. #ifdef HAVE_DEVPOLL
  974. case POLL_DEVPOLL:
  975. while(1){
  976. io_wait_loop_devpoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  977. tcp_reader_timer_run();
  978. }
  979. break;
  980. #endif
  981. default:
  982. LOG(L_CRIT, "BUG: tcp_receive_loop: no support for poll method "
  983. " %s (%d)\n",
  984. poll_method_name(io_w.poll_method), io_w.poll_method);
  985. goto error;
  986. }
  987. error:
  988. destroy_io_wait(&io_w);
  989. LOG(L_CRIT, "ERROR: tcp_receive_loop: exiting...");
  990. exit(-1);
  991. }
  992. #ifdef USE_STUN
  993. int is_msg_complete(struct tcp_req* r)
  994. {
  995. if (r->has_content_len == 1) {
  996. r->state = H_STUN_FP;
  997. return 0;
  998. }
  999. else {
  1000. /* STUN message is complete */
  1001. r->state = H_STUN_END;
  1002. r->complete = 1;
  1003. r->has_content_len = 1; /* hack to avoid error check */
  1004. return 1;
  1005. }
  1006. }
  1007. #endif
  1008. #endif /* USE_TCP */