tcp_read.c 30 KB

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