tcp_read.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. #ifdef USE_TCP
  28. #include <stdio.h>
  29. #include <errno.h>
  30. #include <string.h>
  31. #include <sys/select.h>
  32. #include <sys/time.h>
  33. #include <sys/types.h>
  34. #include <sys/socket.h>
  35. #include <unistd.h>
  36. #include "dprint.h"
  37. #include "tcp_conn.h"
  38. #include "pass_fd.h"
  39. #include "globals.h"
  40. #include "receive.h"
  41. #include "timer.h"
  42. #define q_memchr memchr
  43. /* reads next available bytes
  44. * return number of bytes read, 0 on EOF or -1 on error,
  45. * sets also r->error */
  46. int tcp_read(struct tcp_req *r, int fd)
  47. {
  48. int bytes_free, bytes_read;
  49. bytes_free=TCP_BUF_SIZE- (int)(r->pos - r->buf);
  50. if (bytes_free==0){
  51. LOG(L_ERR, "ERROR: tcp_read: buffer overrun, dropping\n");
  52. r->error=TCP_REQ_OVERRUN;
  53. return -1;
  54. }
  55. again:
  56. bytes_read=read(fd, r->pos, bytes_free);
  57. if(bytes_read==-1){
  58. if (errno == EWOULDBLOCK || errno == EAGAIN){
  59. return 0; /* nothing has been read */
  60. }else if (errno == EINTR) goto again;
  61. else{
  62. LOG(L_ERR, "ERROR: tcp_read: error reading: %s\n",strerror(errno));
  63. r->error=TCP_READ_ERROR;
  64. return -1;
  65. }
  66. }
  67. r->pos+=bytes_read;
  68. return bytes_read;
  69. }
  70. /* reads all headers (until double crlf), & parses the content-length header
  71. * (WARNING: highly ineficient, tries to reuse receive_msg but will parse
  72. * all the header names twice [once here & once in receive_msg]; a more
  73. * speed eficient version will result in either major code duplication or
  74. * major changes to the receive code - TODO)
  75. * returns number of bytes read & sets r->state & r->body
  76. * when either r->body!=0 or r->state==H_BODY =>
  77. * all headers have been read. It should be called in a while loop.
  78. * returns < 0 if error or 0 if EOF */
  79. int tcp_read_headers(struct tcp_req *r, int fd)
  80. {
  81. int bytes, remaining;
  82. char *p;
  83. #define crlf_default_skip_case \
  84. case '\n': \
  85. r->state=H_LF; \
  86. break; \
  87. default: \
  88. r->state=H_SKIP
  89. #define content_len_beg_case \
  90. case ' ': \
  91. case '\t': \
  92. if (!r->has_content_len) r->state=H_STARTWS; \
  93. else r->state=H_SKIP; \
  94. /* not interested if we already found one */ \
  95. break; \
  96. case 'C': \
  97. case 'c': \
  98. if(!r->has_content_len) r->state=H_CONT_LEN1; \
  99. else r->state=H_SKIP; \
  100. break
  101. #define change_state(upper, lower, newstate)\
  102. switch(*p){ \
  103. case upper: \
  104. case lower: \
  105. r->state=(newstate); break; \
  106. crlf_default_skip_case; \
  107. }
  108. #define change_state_case(state0, upper, lower, newstate)\
  109. case state0: \
  110. change_state(upper, lower, newstate); \
  111. p++; \
  112. break
  113. bytes=tcp_read(r, fd);
  114. if (bytes<=0) return bytes;
  115. p=r->parsed;
  116. while(p<r->pos && r->error==TCP_REQ_OK){
  117. switch((unsigned char)r->state){
  118. case H_BODY: /* read the body*/
  119. remaining=r->pos-p;
  120. if (remaining>r->bytes_to_go) remaining=r->bytes_to_go;
  121. r->bytes_to_go-=remaining;
  122. p+=remaining;
  123. if (r->bytes_to_go==0){
  124. r->complete=1;
  125. goto skip;
  126. }
  127. break;
  128. case H_SKIP:
  129. /* find lf, we are in this state if we are not interested
  130. * in anything till end of line*/
  131. p=q_memchr(p, '\n', r->pos-r->parsed);
  132. if (p){
  133. p++;
  134. r->state=H_LF;
  135. }else{
  136. p=r->pos;
  137. }
  138. break;
  139. case H_LF:
  140. /* terminate on LF CR LF or LF LF */
  141. switch (*p){
  142. case '\r':
  143. r->state=H_LFCR;
  144. break;
  145. case '\n':
  146. /* found LF LF */
  147. r->state=H_BODY;
  148. if (r->has_content_len){
  149. r->body=p+1;
  150. r->bytes_to_go=r->content_len;
  151. if (r->bytes_to_go==0){
  152. r->complete=1;
  153. goto skip;
  154. }
  155. }else{
  156. r->error=TCP_REQ_BAD_LEN;
  157. }
  158. break;
  159. content_len_beg_case;
  160. default:
  161. r->state=H_SKIP;
  162. }
  163. p++;
  164. break;
  165. case H_LFCR:
  166. if (*p=='\n'){
  167. /* found LF CR LF */
  168. r->state=H_BODY;
  169. if (r->has_content_len){
  170. r->body=p+1;
  171. r->bytes_to_go=r->content_len;
  172. if (r->bytes_to_go==0){
  173. r->complete=1;
  174. goto skip;
  175. }
  176. }else{
  177. r->error=TCP_REQ_BAD_LEN;
  178. }
  179. }else r->state=H_SKIP;
  180. p++;
  181. break;
  182. case H_STARTWS:
  183. switch (*p){
  184. content_len_beg_case;
  185. crlf_default_skip_case;
  186. }
  187. p++;
  188. break;
  189. case H_SKIP_EMPTY:
  190. switch (*p){
  191. case '\n':
  192. case '\r':
  193. case ' ':
  194. case '\t':
  195. /* skip empty lines */
  196. break;
  197. case 'C':
  198. case 'c':
  199. r->state=H_CONT_LEN1;
  200. r->start=p;
  201. break;
  202. default:
  203. r->state=H_SKIP;
  204. r->start=p;
  205. };
  206. p++;
  207. break;
  208. change_state_case(H_CONT_LEN1, 'O', 'o', H_CONT_LEN2);
  209. change_state_case(H_CONT_LEN2, 'N', 'n', H_CONT_LEN3);
  210. change_state_case(H_CONT_LEN3, 'T', 't', H_CONT_LEN4);
  211. change_state_case(H_CONT_LEN4, 'E', 'e', H_CONT_LEN5);
  212. change_state_case(H_CONT_LEN5, 'N', 'n', H_CONT_LEN6);
  213. change_state_case(H_CONT_LEN6, 'T', 't', H_CONT_LEN7);
  214. change_state_case(H_CONT_LEN7, '-', '_', H_CONT_LEN8);
  215. change_state_case(H_CONT_LEN8, 'L', 'l', H_CONT_LEN9);
  216. change_state_case(H_CONT_LEN9, 'E', 'e', H_CONT_LEN10);
  217. change_state_case(H_CONT_LEN10, 'N', 'n', H_CONT_LEN11);
  218. change_state_case(H_CONT_LEN11, 'G', 'g', H_CONT_LEN12);
  219. change_state_case(H_CONT_LEN12, 'T', 't', H_CONT_LEN13);
  220. change_state_case(H_CONT_LEN13, 'H', 'h', H_L_COLON);
  221. case H_L_COLON:
  222. switch(*p){
  223. case ' ':
  224. case '\t':
  225. break; /* skip space */
  226. case ':':
  227. r->state=H_CONT_LEN_BODY;
  228. break;
  229. crlf_default_skip_case;
  230. };
  231. p++;
  232. break;
  233. case H_CONT_LEN_BODY:
  234. switch(*p){
  235. case ' ':
  236. case '\t':
  237. break; /* eat space */
  238. case '0':
  239. case '1':
  240. case '2':
  241. case '3':
  242. case '4':
  243. case '5':
  244. case '6':
  245. case '7':
  246. case '8':
  247. case '9':
  248. r->state=H_CONT_LEN_BODY_PARSE;
  249. r->content_len=(*p-'0');
  250. break;
  251. /*FIXME: content lenght on different lines ! */
  252. crlf_default_skip_case;
  253. }
  254. p++;
  255. break;
  256. case H_CONT_LEN_BODY_PARSE:
  257. switch(*p){
  258. case '0':
  259. case '1':
  260. case '2':
  261. case '3':
  262. case '4':
  263. case '5':
  264. case '6':
  265. case '7':
  266. case '8':
  267. case '9':
  268. r->content_len=r->content_len*10+(*p-'0');
  269. break;
  270. case '\r':
  271. case ' ':
  272. case '\t': /* FIXME: check if line contains only WS */
  273. r->state=H_SKIP;
  274. r->has_content_len=1;
  275. break;
  276. case '\n':
  277. /* end of line, parse succesfull */
  278. r->state=H_LF;
  279. r->has_content_len=1;
  280. break;
  281. default:
  282. LOG(L_ERR, "ERROR: tcp_read_headers: bad "
  283. "Content-Length header value, unexpected "
  284. "char %c in state %d\n", *p, r->state);
  285. r->state=H_SKIP; /* try to find another?*/
  286. }
  287. p++;
  288. break;
  289. default:
  290. LOG(L_CRIT, "BUG: tcp_read_headers: unexpected state %d\n",
  291. r->state);
  292. abort();
  293. }
  294. }
  295. skip:
  296. r->parsed=p;
  297. return bytes;
  298. }
  299. int tcp_read_req(struct tcp_connection* con)
  300. {
  301. int bytes;
  302. int resp;
  303. long size;
  304. struct tcp_req* req;
  305. int s;
  306. resp=CONN_RELEASE;
  307. s=con->fd;
  308. req=&con->req;
  309. if(req->complete==0 && req->error==TCP_REQ_OK){
  310. bytes=tcp_read_headers(req, s);
  311. /* if timeout state=0; goto end__req; */
  312. DBG("read= %d bytes, parsed=%d, state=%d, error=%d\n",
  313. bytes, req->parsed-req->buf, req->state, req->error );
  314. if (bytes==-1){
  315. LOG(L_ERR, "ERROR: tcp_read_req: error reading \n");
  316. resp=CONN_ERROR;
  317. goto end_req;
  318. }
  319. if (bytes==0){
  320. DBG( "tcp_read_req: EOF\n");
  321. resp=CONN_EOF;
  322. goto end_req;
  323. }
  324. }
  325. if (req->error!=TCP_REQ_OK){
  326. LOG(L_ERR,"ERROR: tcp_read_req: bad request, state=%d, error=%d\n",
  327. req->state, req->error);
  328. resp=CONN_ERROR;
  329. goto end_req;
  330. }
  331. if (req->complete){
  332. DBG("tcp_read_req: end of header part\n");
  333. DBG("tcp_read_req: headers:\n%.*s.\n",
  334. req->body-req->buf, req->buf);
  335. if (req->has_content_len){
  336. DBG("tcp_read_req: content-length= %d\n", req->content_len);
  337. DBG("tcp_read_req: body:\n%.*s\n", req->content_len,req->body);
  338. }else{
  339. req->error=TCP_REQ_BAD_LEN;
  340. LOG(L_ERR, "ERROR: tcp_read_req: content length not present or"
  341. " unparsable\n");
  342. resp=CONN_ERROR;
  343. goto end_req;
  344. }
  345. /* if we are here everything is nice and ok*/
  346. resp=CONN_RELEASE;
  347. /* just for debugging use sendipv4 as receiving socket */
  348. DBG("calling receive_msg(%p, %d, )\n",
  349. req->buf, (int)(req->parsed-req->start));
  350. bind_address=sendipv4; /*&tcp_info[con->sock_idx];*/
  351. con->rcv.proto_reserved1=con->id; /* copy the id */
  352. if (receive_msg(req->start, req->parsed-req->start, &con->rcv)<0){
  353. resp=CONN_ERROR;
  354. goto end_req;
  355. }
  356. /* prepare for next request */
  357. size=req->pos-req->parsed;
  358. if (size) memmove(req->buf, req->parsed, size);
  359. DBG("tcp_read_req: preparing for new request, kept %ld bytes\n",
  360. size);
  361. req->pos=req->buf+size;
  362. req->parsed=req->buf;
  363. req->start=req->buf;
  364. req->body=0;
  365. req->error=TCP_REQ_OK;
  366. req->state=H_SKIP_EMPTY;
  367. req->complete=req->content_len=req->has_content_len=0;
  368. req->bytes_to_go=0;
  369. }
  370. end_req:
  371. return resp;
  372. }
  373. void release_tcpconn(struct tcp_connection* c, long state, int unix_sock)
  374. {
  375. long response[2];
  376. DBG( "releasing con %p, state %ld\n", c, state );
  377. /* release req & signal the parent */
  378. if (c->fd!=-1) close(c->fd);
  379. /* errno==EINTR, EWOULDBLOCK a.s.o todo */
  380. response[0]=(long)c;
  381. response[1]=state;
  382. write(unix_sock, response, sizeof(response));
  383. }
  384. void tcp_receive_loop(int unix_sock)
  385. {
  386. struct tcp_connection* list; /* list with connections in use */
  387. struct tcp_connection* con;
  388. struct tcp_connection* c_next;
  389. int n;
  390. int nfds;
  391. int s;
  392. long resp;
  393. fd_set master_set;
  394. fd_set sel_set;
  395. int maxfd;
  396. struct timeval timeout;
  397. int ticks;
  398. /* init */
  399. list=con=0;
  400. FD_ZERO(&master_set);
  401. FD_SET(unix_sock, &master_set);
  402. maxfd=unix_sock;
  403. /* listen on the unix socket for the fd */
  404. for(;;){
  405. timeout.tv_sec=TCP_CHILD_SELECT_TIMEOUT;
  406. timeout.tv_usec=0;
  407. sel_set=master_set;
  408. nfds=select(maxfd+1, &sel_set, 0 , 0 , &timeout);
  409. if (nfds<0){
  410. if (errno==EINTR) continue; /* just a signal */
  411. /* errors */
  412. LOG(L_ERR, "ERROR: tcp_receive_loop: select:(%d) %s\n", errno,
  413. strerror(errno));
  414. continue;
  415. }
  416. if (FD_ISSET(unix_sock, &sel_set)){
  417. nfds--;
  418. /* a new conn from "main" */
  419. n=receive_fd(unix_sock, &con, sizeof(con), &s);
  420. if (n<0){
  421. if (errno == EWOULDBLOCK || errno == EAGAIN ||
  422. errno == EINTR){
  423. continue;
  424. }else{
  425. LOG(L_CRIT,"BUG: tcp_receive_loop: read_fd: %s\n",
  426. strerror(errno));
  427. abort(); /* big error*/
  428. }
  429. }
  430. if (n==0){
  431. LOG(L_ERR, "WARNING: tcp_receive_loop: 0 bytes read\n");
  432. continue;
  433. }
  434. con->fd=s;
  435. DBG("received n=%d con=%p, fd=%d\n", n, con, s);
  436. if (s==-1) {
  437. LOG(L_ERR, "ERROR: tcp_receive_loop: read_fd:"
  438. "no fd read\n");
  439. resp=CONN_ERROR;
  440. release_tcpconn(con, resp, unix_sock);
  441. }
  442. if (con==0){
  443. LOG(L_ERR, "ERROR: tcp_receive_loop: null pointer\n");
  444. resp=CONN_ERROR;
  445. release_tcpconn(con, resp, unix_sock);
  446. }
  447. con->timeout=get_ticks()+TCP_CHILD_TIMEOUT;
  448. FD_SET(s, &master_set);
  449. if (maxfd<s) maxfd=s;
  450. tcpconn_listadd(list, con, c_next, c_prev);
  451. }
  452. ticks=get_ticks();
  453. for (con=list; con ; con=c_next){
  454. c_next=con->c_next; /* safe for removing*/
  455. if (nfds && FD_ISSET(con->fd, &sel_set)){
  456. nfds--;
  457. resp=tcp_read_req(con);
  458. if (resp<0){
  459. FD_CLR(con->fd, &master_set);
  460. tcpconn_listrm(list, con, c_next, c_prev);
  461. release_tcpconn(con, resp, unix_sock);
  462. }else{
  463. /* update timeout */
  464. con->timeout=ticks+TCP_CHILD_TIMEOUT;
  465. }
  466. }else{
  467. /* timeout */
  468. if (con->timeout<=ticks){
  469. /* expired, return to "tcp main" */
  470. DBG("tcp_receive_loop: %p expired (%d, %d)\n",
  471. con, con->timeout, ticks);
  472. resp=CONN_RELEASE;
  473. FD_CLR(con->fd, &master_set);
  474. tcpconn_listrm(list, con, c_next, c_prev);
  475. release_tcpconn(con, resp, unix_sock);
  476. }
  477. }
  478. }
  479. }
  480. }
  481. #if 0
  482. int main(int argv, char** argc )
  483. {
  484. printf("starting tests\n");
  485. tcp_receive_loop();
  486. }
  487. #endif
  488. #endif