tcp_read.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. * 2010-05-14 split tcp_read() into tcp_read() and tcp_read_data() (andrei)
  46. * 2010-05-17 new RD_CONN_REPEAT_READ flag, used by the tls hooks (andrei)
  47. */
  48. /** tcp readers processes, tcp read and pre-parse msg. functions.
  49. * @file tcp_read.c
  50. * @ingroup core
  51. * Module: @ref core
  52. */
  53. #ifdef USE_TCP
  54. #include <stdio.h>
  55. #include <errno.h>
  56. #include <string.h>
  57. #include <sys/time.h>
  58. #include <sys/types.h>
  59. #include <sys/select.h>
  60. #include <sys/socket.h>
  61. #include <unistd.h>
  62. #include <stdlib.h> /* for abort() */
  63. #include "dprint.h"
  64. #include "tcp_conn.h"
  65. #include "tcp_read.h"
  66. #include "tcp_stats.h"
  67. #include "tcp_ev.h"
  68. #include "pass_fd.h"
  69. #include "globals.h"
  70. #include "receive.h"
  71. #include "timer.h"
  72. #include "local_timer.h"
  73. #include "ut.h"
  74. #include "trim.h"
  75. #include "pt.h"
  76. #include "cfg/cfg_struct.h"
  77. #ifdef CORE_TLS
  78. #include "tls/tls_server.h"
  79. #else
  80. #include "tls_hooks.h"
  81. #endif /* CORE_TLS */
  82. #ifdef USE_DST_BLACKLIST
  83. #include "dst_blacklist.h"
  84. #endif /* USE_DST_BLACKLIST */
  85. #define HANDLE_IO_INLINE
  86. #include "io_wait.h"
  87. #include <fcntl.h> /* must be included after io_wait.h if SIGIO_RT is used */
  88. #include "tsend.h"
  89. #include "forward.h"
  90. #include "events.h"
  91. #include "stun.h"
  92. #ifdef READ_HTTP11
  93. #define HTTP11CONTINUE "HTTP/1.1 100 Continue\r\nContent-Length: 0\r\n\r\n"
  94. #define HTTP11CONTINUE_LEN (sizeof(HTTP11CONTINUE)-1)
  95. #endif
  96. #define TCPCONN_TIMEOUT_MIN_RUN 1 /* run the timers each new tick */
  97. /* types used in io_wait* */
  98. enum fd_types { F_NONE, F_TCPMAIN, F_TCPCONN };
  99. /* list of tcp connections handled by this process */
  100. static struct tcp_connection* tcp_conn_lst=0;
  101. static io_wait_h io_w; /* io_wait handler*/
  102. static int tcpmain_sock=-1;
  103. static struct local_timer tcp_reader_ltimer;
  104. static ticks_t tcp_reader_prev_ticks;
  105. int is_msg_complete(struct tcp_req* r);
  106. /**
  107. * control cloning of TCP receive buffer
  108. * - needed for operations working directly inside the buffer
  109. * (like msg_apply_changes())
  110. */
  111. #define TCP_CLONE_RCVBUF
  112. static int tcp_clone_rcvbuf = 0;
  113. int tcp_set_clone_rcvbuf(int v)
  114. {
  115. int r;
  116. r = tcp_clone_rcvbuf;
  117. tcp_clone_rcvbuf = v;
  118. return r;
  119. }
  120. #ifdef READ_HTTP11
  121. static inline char *strfindcasestrz(str *haystack, char *needlez)
  122. {
  123. int i,j;
  124. str needle;
  125. needle.s = needlez;
  126. needle.len = strlen(needlez);
  127. for(i=0;i<haystack->len-needle.len;i++) {
  128. for(j=0;j<needle.len;j++) {
  129. if ( !((haystack->s[i+j]==needle.s[j]) ||
  130. ( isalpha((int)haystack->s[i+j])
  131. && ((haystack->s[i+j])^(needle.s[j]))==0x20 )) )
  132. break;
  133. }
  134. if (j==needle.len)
  135. return haystack->s+i;
  136. }
  137. return 0;
  138. }
  139. int tcp_http11_continue(struct tcp_connection *c)
  140. {
  141. struct dest_info dst;
  142. char *p;
  143. struct msg_start fline;
  144. int ret;
  145. str msg;
  146. ret = 0;
  147. msg.s = c->req.start;
  148. msg.len = c->req.pos - c->req.start;
  149. #ifdef READ_MSRP
  150. /* skip if MSRP message */
  151. if(c->req.flags&F_TCP_REQ_MSRP_FRAME)
  152. return 0;
  153. #endif
  154. p = parse_first_line(msg.s, msg.len, &fline);
  155. if(p==NULL)
  156. return 0;
  157. if(fline.type!=SIP_REQUEST)
  158. return 0;
  159. /* check if http request */
  160. if(fline.u.request.version.len < HTTP_VERSION_LEN
  161. || strncasecmp(fline.u.request.version.s,
  162. HTTP_VERSION, HTTP_VERSION_LEN))
  163. return 0;
  164. /* check for Expect header */
  165. if(strfindcasestrz(&msg, "Expect: 100-continue")!=NULL)
  166. {
  167. init_dst_from_rcv(&dst, &c->rcv);
  168. if (tcp_send(&dst, 0, HTTP11CONTINUE, HTTP11CONTINUE_LEN) < 0) {
  169. LM_ERR("HTTP/1.1 continue failed\n");
  170. }
  171. }
  172. /* check for Transfer-Encoding header */
  173. if(strfindcasestrz(&msg, "Transfer-Encoding: chunked")!=NULL)
  174. {
  175. c->req.flags |= F_TCP_REQ_BCHUNKED;
  176. ret = 1;
  177. }
  178. /* check for HTTP Via header
  179. * - HTTP Via format is different that SIP Via
  180. * - workaround: replace with Hia to be ignored by SIP parser
  181. */
  182. if((p=strfindcasestrz(&msg, "\nVia:"))!=NULL)
  183. {
  184. p++;
  185. *p = 'H';
  186. LM_DBG("replaced HTTP Via with Hia [[\n%.*s]]\n", msg.len, msg.s);
  187. }
  188. return ret;
  189. }
  190. #endif /* HTTP11 */
  191. /** reads data from an existing tcp connection.
  192. * Side-effects: blacklisting, sets connection state to S_CONN_OK, tcp stats.
  193. * @param fd - connection file descriptor
  194. * @param c - tcp connection structure. c->state might be changed and
  195. * receive info might be used for blacklisting.
  196. * @param buf - buffer where the received data will be stored.
  197. * @param b_size - buffer size.
  198. * @param flags - value/result - used to signal a seen or "forced" EOF on the
  199. * connection (when it is known that no more data will come after the
  200. * current socket buffer is emptied )=> return/signal EOF on the first
  201. * short read (=> don't use it on POLLPRI, as OOB data will cause short
  202. * reads even if there are still remaining bytes in the socket buffer)
  203. * input: RD_CONN_FORCE_EOF - force EOF after the first successful read
  204. * (bytes_read >=0 )
  205. * output: RD_CONN_SHORT_READ - if the read exhausted all the bytes
  206. * in the socket read buffer.
  207. * RD_CONN_EOF - if EOF detected (0 bytes read) or forced via
  208. * RD_CONN_FORCE_EOF.
  209. * RD_CONN_REPEAT_READ - the read should be repeated immediately
  210. * (used only by the tls code for now).
  211. * Note: RD_CONN_SHORT_READ & RD_CONN_EOF _are_ not cleared internally,
  212. * so one should clear them before calling this function.
  213. * @return number of bytes read, 0 on EOF or -1 on error,
  214. * on EOF it also sets c->state to S_CONN_EOF.
  215. * (to distinguish from reads that would block which could return 0)
  216. * RD_CONN_SHORT_READ is also set in *flags for short reads.
  217. * EOF checking should be done by checking the RD_CONN_EOF flag.
  218. */
  219. int tcp_read_data(int fd, struct tcp_connection *c,
  220. char* buf, int b_size, int* flags)
  221. {
  222. int bytes_read;
  223. again:
  224. bytes_read=read(fd, buf, b_size);
  225. if (likely(bytes_read!=b_size)){
  226. if(unlikely(bytes_read==-1)){
  227. if (errno == EWOULDBLOCK || errno == EAGAIN){
  228. bytes_read=0; /* nothing has been read */
  229. }else if (errno == EINTR) goto again;
  230. else{
  231. if (unlikely(c->state==S_CONN_CONNECT)){
  232. switch(errno){
  233. case ECONNRESET:
  234. #ifdef USE_DST_BLACKLIST
  235. dst_blacklist_su(BLST_ERR_CONNECT, c->rcv.proto,
  236. &c->rcv.src_su,
  237. &c->send_flags, 0);
  238. #endif /* USE_DST_BLACKLIST */
  239. TCP_EV_CONNECT_RST(errno, TCP_LADDR(c),
  240. TCP_LPORT(c), TCP_PSU(c), TCP_PROTO(c));
  241. break;
  242. case ETIMEDOUT:
  243. #ifdef USE_DST_BLACKLIST
  244. dst_blacklist_su(BLST_ERR_CONNECT, c->rcv.proto,
  245. &c->rcv.src_su,
  246. &c->send_flags, 0);
  247. #endif /* USE_DST_BLACKLIST */
  248. TCP_EV_CONNECT_TIMEOUT(errno, TCP_LADDR(c),
  249. TCP_LPORT(c), TCP_PSU(c), TCP_PROTO(c));
  250. break;
  251. default:
  252. TCP_EV_CONNECT_ERR(errno, TCP_LADDR(c),
  253. TCP_LPORT(c), TCP_PSU(c), TCP_PROTO(c));
  254. }
  255. TCP_STATS_CONNECT_FAILED();
  256. }else{
  257. switch(errno){
  258. case ECONNRESET:
  259. TCP_STATS_CON_RESET();
  260. case ETIMEDOUT:
  261. #ifdef USE_DST_BLACKLIST
  262. dst_blacklist_su(BLST_ERR_SEND, c->rcv.proto,
  263. &c->rcv.src_su,
  264. &c->send_flags, 0);
  265. #endif /* USE_DST_BLACKLIST */
  266. break;
  267. }
  268. }
  269. LOG(cfg_get(core, core_cfg, corelog),
  270. "error reading: %s (%d)\n", strerror(errno), errno);
  271. return -1;
  272. }
  273. }else if (unlikely((bytes_read==0) ||
  274. (*flags & RD_CONN_FORCE_EOF))){
  275. c->state=S_CONN_EOF;
  276. *flags|=RD_CONN_EOF;
  277. DBG("EOF on %p, FD %d\n", c, fd);
  278. }else{
  279. if (unlikely(c->state==S_CONN_CONNECT || c->state==S_CONN_ACCEPT)){
  280. TCP_STATS_ESTABLISHED(c->state);
  281. c->state=S_CONN_OK;
  282. }
  283. }
  284. /* short read */
  285. *flags|=RD_CONN_SHORT_READ;
  286. }else{ /* else normal full read */
  287. if (unlikely(c->state==S_CONN_CONNECT || c->state==S_CONN_ACCEPT)){
  288. TCP_STATS_ESTABLISHED(c->state);
  289. c->state=S_CONN_OK;
  290. }
  291. }
  292. return bytes_read;
  293. }
  294. /* reads next available bytes
  295. * c- tcp connection used for reading, tcp_read changes also c->state on
  296. * EOF and c->req.error on read error
  297. * * flags - value/result - used to signal a seen or "forced" EOF on the
  298. * connection (when it is known that no more data will come after the
  299. * current socket buffer is emptied )=> return/signal EOF on the first
  300. * short read (=> don't use it on POLLPRI, as OOB data will cause short
  301. * reads even if there are still remaining bytes in the socket buffer)
  302. * return number of bytes read, 0 on EOF or -1 on error,
  303. * on EOF it also sets c->state to S_CONN_EOF.
  304. * (to distinguish from reads that would block which could return 0)
  305. * RD_CONN_SHORT_READ is also set in *flags for short reads.
  306. * sets also r->error */
  307. int tcp_read(struct tcp_connection *c, int* flags)
  308. {
  309. int bytes_free, bytes_read;
  310. struct tcp_req *r;
  311. int fd;
  312. r=&c->req;
  313. fd=c->fd;
  314. bytes_free=r->b_size- (int)(r->pos - r->buf);
  315. if (unlikely(bytes_free==0)){
  316. LM_ERR("buffer overrun, dropping\n");
  317. r->error=TCP_REQ_OVERRUN;
  318. return -1;
  319. }
  320. bytes_read = tcp_read_data(fd, c, r->pos, bytes_free, flags);
  321. if (unlikely(bytes_read < 0)){
  322. r->error=TCP_READ_ERROR;
  323. return -1;
  324. }
  325. #ifdef EXTRA_DEBUG
  326. DBG("tcp_read: read %d bytes:\n%.*s\n", bytes_read, bytes_read, r->pos);
  327. #endif
  328. r->pos+=bytes_read;
  329. return bytes_read;
  330. }
  331. /* reads all headers (until double crlf), & parses the content-length header
  332. * (WARNING: inefficient, tries to reuse receive_msg but will go through
  333. * the headers twice [once here looking for Content-Length and for the end
  334. * of the headers and once in receive_msg]; a more speed efficient version will
  335. * result in either major code duplication or major changes to the receive code)
  336. * returns number of bytes read & sets r->state & r->body
  337. * when either r->body!=0 or r->state==H_BODY =>
  338. * all headers have been read. It should be called in a while loop.
  339. * returns < 0 if error or 0 if EOF */
  340. int tcp_read_headers(struct tcp_connection *c, int* read_flags)
  341. {
  342. int bytes, remaining;
  343. char *p;
  344. struct tcp_req* r;
  345. unsigned int mc; /* magic cookie */
  346. unsigned short body_len;
  347. #ifdef READ_MSRP
  348. char *mfline;
  349. str mtransid;
  350. #endif
  351. #define crlf_default_skip_case \
  352. case '\n': \
  353. r->state=H_LF; \
  354. break; \
  355. default: \
  356. r->state=H_SKIP
  357. #define content_len_beg_case \
  358. case ' ': \
  359. case '\t': \
  360. if (!TCP_REQ_HAS_CLEN(r)) r->state=H_STARTWS; \
  361. else r->state=H_SKIP; \
  362. /* not interested if we already found one */ \
  363. break; \
  364. case 'C': \
  365. case 'c': \
  366. if(!TCP_REQ_HAS_CLEN(r)) r->state=H_CONT_LEN1; \
  367. else r->state=H_SKIP; \
  368. break; \
  369. case 'l': \
  370. case 'L': \
  371. /* short form for Content-Length */ \
  372. if (!TCP_REQ_HAS_CLEN(r)) r->state=H_L_COLON; \
  373. else r->state=H_SKIP; \
  374. break
  375. #define change_state(upper, lower, newstate)\
  376. switch(*p){ \
  377. case upper: \
  378. case lower: \
  379. r->state=(newstate); break; \
  380. crlf_default_skip_case; \
  381. }
  382. #define change_state_case(state0, upper, lower, newstate)\
  383. case state0: \
  384. change_state(upper, lower, newstate); \
  385. p++; \
  386. break
  387. r=&c->req;
  388. /* if we still have some unparsed part, parse it first, don't do the read*/
  389. if (unlikely(r->parsed<r->pos)){
  390. bytes=0;
  391. }else{
  392. #ifdef USE_TLS
  393. if (unlikely(c->type==PROTO_TLS))
  394. bytes=tls_read(c, read_flags);
  395. else
  396. #endif
  397. bytes=tcp_read(c, read_flags);
  398. if (bytes<=0) return bytes;
  399. }
  400. p=r->parsed;
  401. while(p<r->pos && r->error==TCP_REQ_OK){
  402. switch((unsigned char)r->state){
  403. case H_BODY: /* read the body*/
  404. remaining=r->pos-p;
  405. if (remaining>r->bytes_to_go) remaining=r->bytes_to_go;
  406. r->bytes_to_go-=remaining;
  407. p+=remaining;
  408. if (r->bytes_to_go==0){
  409. r->flags|=F_TCP_REQ_COMPLETE;
  410. goto skip;
  411. }
  412. break;
  413. case H_SKIP:
  414. /* find lf, we are in this state if we are not interested
  415. * in anything till end of line*/
  416. p=q_memchr(p, '\n', r->pos-p);
  417. if (p){
  418. #ifdef READ_MSRP
  419. /* catch if it is MSRP or not with first '\n' */
  420. if(!((r->flags&F_TCP_REQ_MSRP_NO)
  421. || (r->flags&F_TCP_REQ_MSRP_FRAME))) {
  422. if((r->pos - r->start)>5
  423. && strncmp(r->start, "MSRP ", 5)==0)
  424. {
  425. r->flags |= F_TCP_REQ_MSRP_FRAME;
  426. } else {
  427. r->flags |= F_TCP_REQ_MSRP_NO;
  428. }
  429. }
  430. #endif
  431. p++;
  432. r->state=H_LF;
  433. }else{
  434. p=r->pos;
  435. }
  436. break;
  437. case H_LF:
  438. /* terminate on LF CR LF or LF LF */
  439. switch (*p){
  440. case '\r':
  441. r->state=H_LFCR;
  442. break;
  443. case '\n':
  444. /* found LF LF */
  445. r->state=H_BODY;
  446. #ifdef READ_HTTP11
  447. if (cfg_get(tcp, tcp_cfg, accept_no_cl)!=0)
  448. tcp_http11_continue(c);
  449. #endif
  450. if (TCP_REQ_HAS_CLEN(r)){
  451. r->body=p+1;
  452. r->bytes_to_go=r->content_len;
  453. if (r->bytes_to_go==0){
  454. r->flags|=F_TCP_REQ_COMPLETE;
  455. p++;
  456. goto skip;
  457. }
  458. }else{
  459. if(cfg_get(tcp, tcp_cfg, accept_no_cl)!=0) {
  460. #ifdef READ_MSRP
  461. /* if MSRP message */
  462. if(c->req.flags&F_TCP_REQ_MSRP_FRAME)
  463. {
  464. r->body=p+1;
  465. /* at least 3 bytes: 0\r\n */
  466. r->bytes_to_go=3;
  467. p++;
  468. r->content_len = 0;
  469. r->state=H_MSRP_BODY;
  470. break;
  471. }
  472. #endif
  473. #ifdef READ_HTTP11
  474. if(TCP_REQ_BCHUNKED(r)) {
  475. r->body=p+1;
  476. /* at least 3 bytes: 0\r\n */
  477. r->bytes_to_go=3;
  478. p++;
  479. r->content_len = 0;
  480. r->state=H_HTTP11_CHUNK_START;
  481. break;
  482. }
  483. #endif
  484. r->body=p+1;
  485. r->bytes_to_go=0;
  486. r->flags|=F_TCP_REQ_COMPLETE;
  487. p++;
  488. goto skip;
  489. } else {
  490. DBG("tcp_read_headers: ERROR: no clen, p=%X\n",
  491. *p);
  492. r->error=TCP_REQ_BAD_LEN;
  493. }
  494. }
  495. break;
  496. case '-':
  497. r->state=H_SKIP;
  498. #ifdef READ_MSRP
  499. /* catch end of MSRP frame without body
  500. * '-------sessid$\r\n'
  501. * follows headers wihtout extra CRLF */
  502. if(r->flags&F_TCP_REQ_MSRP_FRAME) {
  503. p--;
  504. r->state=H_MSRP_BODY_END;
  505. }
  506. #endif
  507. break;
  508. content_len_beg_case;
  509. default:
  510. r->state=H_SKIP;
  511. }
  512. p++;
  513. break;
  514. case H_LFCR:
  515. if (*p=='\n'){
  516. /* found LF CR LF */
  517. r->state=H_BODY;
  518. #ifdef READ_HTTP11
  519. if (cfg_get(tcp, tcp_cfg, accept_no_cl)!=0)
  520. tcp_http11_continue(c);
  521. #endif
  522. if (TCP_REQ_HAS_CLEN(r)){
  523. r->body=p+1;
  524. r->bytes_to_go=r->content_len;
  525. if (r->bytes_to_go==0){
  526. r->flags|=F_TCP_REQ_COMPLETE;
  527. p++;
  528. goto skip;
  529. }
  530. }else{
  531. if (cfg_get(tcp, tcp_cfg, accept_no_cl)!=0) {
  532. #ifdef READ_MSRP
  533. /* if MSRP message */
  534. if(c->req.flags&F_TCP_REQ_MSRP_FRAME)
  535. {
  536. r->body=p+1;
  537. /* at least 3 bytes: 0\r\n */
  538. r->bytes_to_go=3;
  539. p++;
  540. r->content_len = 0;
  541. r->state=H_MSRP_BODY;
  542. break;
  543. }
  544. #endif
  545. #ifdef READ_HTTP11
  546. if(TCP_REQ_BCHUNKED(r)) {
  547. r->body=p+1;
  548. /* at least 3 bytes: 0\r\n */
  549. r->bytes_to_go=3;
  550. p++;
  551. r->content_len = 0;
  552. r->state=H_HTTP11_CHUNK_START;
  553. break;
  554. }
  555. #endif
  556. r->body=p+1;
  557. r->bytes_to_go=0;
  558. r->flags|=F_TCP_REQ_COMPLETE;
  559. p++;
  560. goto skip;
  561. } else {
  562. DBG("tcp_read_headers: ERROR: no clen, p=%X\n",
  563. *p);
  564. r->error=TCP_REQ_BAD_LEN;
  565. }
  566. }
  567. }else r->state=H_SKIP;
  568. p++;
  569. break;
  570. case H_STARTWS:
  571. switch (*p){
  572. content_len_beg_case;
  573. crlf_default_skip_case;
  574. }
  575. p++;
  576. break;
  577. case H_SKIP_EMPTY:
  578. switch (*p){
  579. case '\n':
  580. break;
  581. case '\r':
  582. if (cfg_get(tcp, tcp_cfg, crlf_ping)) {
  583. r->state=H_SKIP_EMPTY_CR_FOUND;
  584. r->start=p;
  585. }
  586. break;
  587. case ' ':
  588. case '\t':
  589. /* skip empty lines */
  590. break;
  591. case 'C':
  592. case 'c':
  593. r->state=H_CONT_LEN1;
  594. r->start=p;
  595. break;
  596. case 'l':
  597. case 'L':
  598. /* short form for Content-Length */
  599. r->state=H_L_COLON;
  600. r->start=p;
  601. break;
  602. default:
  603. /* stun test */
  604. if (unlikely(sr_event_enabled(SREV_STUN_IN)) && (unsigned char)*p == 0x00) {
  605. r->state=H_STUN_MSG;
  606. /* body will used as pointer to the last used byte */
  607. r->body=p;
  608. r->content_len = 0;
  609. DBG("stun msg detected\n");
  610. } else {
  611. r->state=H_SKIP;
  612. }
  613. r->start=p;
  614. };
  615. p++;
  616. break;
  617. case H_SKIP_EMPTY_CR_FOUND:
  618. if (*p=='\n'){
  619. r->state=H_SKIP_EMPTY_CRLF_FOUND;
  620. p++;
  621. }else{
  622. r->state=H_SKIP_EMPTY;
  623. }
  624. break;
  625. case H_SKIP_EMPTY_CRLF_FOUND:
  626. if (*p=='\r'){
  627. r->state = H_SKIP_EMPTY_CRLFCR_FOUND;
  628. p++;
  629. }else{
  630. r->state = H_SKIP_EMPTY;
  631. }
  632. break;
  633. case H_SKIP_EMPTY_CRLFCR_FOUND:
  634. if (*p=='\n'){
  635. r->state = H_PING_CRLF;
  636. r->flags |= F_TCP_REQ_HAS_CLEN |
  637. F_TCP_REQ_COMPLETE; /* hack to avoid error check */
  638. p++;
  639. goto skip;
  640. }else{
  641. r->state = H_SKIP_EMPTY;
  642. }
  643. break;
  644. case H_STUN_MSG:
  645. if ((r->pos - r->body) >= sizeof(struct stun_hdr)) {
  646. /* copy second short from buffer where should be body
  647. * length
  648. */
  649. memcpy(&body_len, &r->start[sizeof(unsigned short)],
  650. sizeof(unsigned short));
  651. body_len = ntohs(body_len);
  652. /* check if there is valid magic cookie */
  653. memcpy(&mc, &r->start[sizeof(unsigned int)],
  654. sizeof(unsigned int));
  655. mc = ntohl(mc);
  656. /* using has_content_len as a flag if there should be
  657. * fingerprint or no
  658. */
  659. r->flags |= (mc == MAGIC_COOKIE) ? F_TCP_REQ_HAS_CLEN : 0;
  660. r->body += sizeof(struct stun_hdr);
  661. p = r->body;
  662. if (body_len > 0) {
  663. r->state = H_STUN_READ_BODY;
  664. }
  665. else {
  666. if (is_msg_complete(r) != 0) {
  667. goto skip;
  668. }
  669. else {
  670. /* set content_len to length of fingerprint */
  671. body_len = sizeof(struct stun_attr) + 20;
  672. /* 20 is SHA_DIGEST_LENGTH from openssl/sha.h */
  673. }
  674. }
  675. r->content_len=body_len;
  676. }
  677. else {
  678. p = r->pos;
  679. }
  680. break;
  681. case H_STUN_READ_BODY:
  682. /* check if the whole body was read */
  683. body_len=r->content_len;
  684. if ((r->pos - r->body) >= body_len) {
  685. r->body += body_len;
  686. p = r->body;
  687. if (is_msg_complete(r) != 0) {
  688. r->content_len=0;
  689. goto skip;
  690. }
  691. else {
  692. /* set content_len to length of fingerprint */
  693. body_len = sizeof(struct stun_attr) + 20;
  694. /* 20 is SHA_DIGEST_LENGTH from openssl/sha.h */
  695. r->content_len=body_len;
  696. }
  697. }
  698. else {
  699. p = r->pos;
  700. }
  701. break;
  702. case H_STUN_FP:
  703. /* content_len contains length of fingerprint in this place! */
  704. body_len=r->content_len;
  705. if ((r->pos - r->body) >= body_len) {
  706. r->body += body_len;
  707. p = r->body;
  708. r->state = H_STUN_END;
  709. r->flags |= F_TCP_REQ_COMPLETE |
  710. F_TCP_REQ_HAS_CLEN; /* hack to avoid error check */
  711. r->content_len=0;
  712. goto skip;
  713. }
  714. else {
  715. p = r->pos;
  716. }
  717. break;
  718. change_state_case(H_CONT_LEN1, 'O', 'o', H_CONT_LEN2);
  719. change_state_case(H_CONT_LEN2, 'N', 'n', H_CONT_LEN3);
  720. change_state_case(H_CONT_LEN3, 'T', 't', H_CONT_LEN4);
  721. change_state_case(H_CONT_LEN4, 'E', 'e', H_CONT_LEN5);
  722. change_state_case(H_CONT_LEN5, 'N', 'n', H_CONT_LEN6);
  723. change_state_case(H_CONT_LEN6, 'T', 't', H_CONT_LEN7);
  724. change_state_case(H_CONT_LEN7, '-', '_', H_CONT_LEN8);
  725. change_state_case(H_CONT_LEN8, 'L', 'l', H_CONT_LEN9);
  726. change_state_case(H_CONT_LEN9, 'E', 'e', H_CONT_LEN10);
  727. change_state_case(H_CONT_LEN10, 'N', 'n', H_CONT_LEN11);
  728. change_state_case(H_CONT_LEN11, 'G', 'g', H_CONT_LEN12);
  729. change_state_case(H_CONT_LEN12, 'T', 't', H_CONT_LEN13);
  730. change_state_case(H_CONT_LEN13, 'H', 'h', H_L_COLON);
  731. case H_L_COLON:
  732. switch(*p){
  733. case ' ':
  734. case '\t':
  735. break; /* skip space */
  736. case ':':
  737. r->state=H_CONT_LEN_BODY;
  738. break;
  739. crlf_default_skip_case;
  740. };
  741. p++;
  742. break;
  743. case H_CONT_LEN_BODY:
  744. switch(*p){
  745. case ' ':
  746. case '\t':
  747. break; /* eat space */
  748. case '0':
  749. case '1':
  750. case '2':
  751. case '3':
  752. case '4':
  753. case '5':
  754. case '6':
  755. case '7':
  756. case '8':
  757. case '9':
  758. r->state=H_CONT_LEN_BODY_PARSE;
  759. r->content_len=(*p-'0');
  760. break;
  761. /*FIXME: content length on different lines ! */
  762. crlf_default_skip_case;
  763. }
  764. p++;
  765. break;
  766. case H_CONT_LEN_BODY_PARSE:
  767. switch(*p){
  768. case '0':
  769. case '1':
  770. case '2':
  771. case '3':
  772. case '4':
  773. case '5':
  774. case '6':
  775. case '7':
  776. case '8':
  777. case '9':
  778. r->content_len=r->content_len*10+(*p-'0');
  779. break;
  780. case '\r':
  781. case ' ':
  782. case '\t': /* FIXME: check if line contains only WS */
  783. if(r->content_len<0) {
  784. LM_ERR("bad Content-Length header value %d in"
  785. " state %d\n", r->content_len, r->state);
  786. r->content_len=0;
  787. r->error=TCP_REQ_BAD_LEN;
  788. r->state=H_SKIP; /* skip now */
  789. }
  790. r->state=H_SKIP;
  791. r->flags|=F_TCP_REQ_HAS_CLEN;
  792. break;
  793. case '\n':
  794. /* end of line, parse successful */
  795. if(r->content_len<0) {
  796. LM_ERR("bad Content-Length header value %d in"
  797. " state %d\n", r->content_len, r->state);
  798. r->content_len=0;
  799. r->error=TCP_REQ_BAD_LEN;
  800. r->state=H_SKIP; /* skip now */
  801. }
  802. r->state=H_LF;
  803. r->flags|=F_TCP_REQ_HAS_CLEN;
  804. break;
  805. default:
  806. LM_ERR("bad Content-Length header value, unexpected "
  807. "char %c in state %d\n", *p, r->state);
  808. r->state=H_SKIP; /* try to find another?*/
  809. }
  810. p++;
  811. break;
  812. #ifdef READ_HTTP11
  813. case H_HTTP11_CHUNK_START: /* start a new body chunk: SIZE\r\nBODY\r\n */
  814. r->chunk_size = 0;
  815. r->state = H_HTTP11_CHUNK_SIZE;
  816. break;
  817. case H_HTTP11_CHUNK_BODY: /* content of chunnk */
  818. remaining=r->pos-p;
  819. if (remaining>r->bytes_to_go) remaining=r->bytes_to_go;
  820. r->bytes_to_go-=remaining;
  821. p+=remaining;
  822. if (r->bytes_to_go==0){
  823. r->state = H_HTTP11_CHUNK_END;
  824. /* shift back body content */
  825. if(r->chunk_size>0 && p-r->chunk_size>r->body) {
  826. memmove(r->body + r->content_len, p - r->chunk_size,
  827. r->chunk_size);
  828. r->content_len += r->chunk_size;
  829. }
  830. goto skip;
  831. }
  832. break;
  833. case H_HTTP11_CHUNK_END:
  834. switch(*p){
  835. case '\r':
  836. case ' ':
  837. case '\t': /* skip */
  838. break;
  839. case '\n':
  840. r->state = H_HTTP11_CHUNK_START;
  841. break;
  842. default:
  843. LM_ERR("bad chunk, unexpected "
  844. "char %c in state %d\n", *p, r->state);
  845. r->state=H_SKIP; /* try to find another?*/
  846. }
  847. p++;
  848. break;
  849. case H_HTTP11_CHUNK_SIZE:
  850. switch(*p){
  851. case '0': case '1': case '2': case '3':
  852. case '4': case '5': case '6': case '7':
  853. case '8': case '9':
  854. r->chunk_size <<= 4;
  855. r->chunk_size += *p - '0';
  856. break;
  857. case 'a': case 'b': case 'c': case 'd':
  858. case 'e': case 'f':
  859. r->chunk_size <<= 4;
  860. r->chunk_size += *p - 'a' + 10;
  861. break;
  862. case 'A': case 'B': case 'C': case 'D':
  863. case 'E': case 'F':
  864. r->chunk_size <<= 4;
  865. r->chunk_size += *p - 'A' + 10;
  866. break;
  867. case '\r':
  868. case ' ':
  869. case '\t': /* skip */
  870. break;
  871. case '\n':
  872. /* end of line, parse successful */
  873. r->state=H_HTTP11_CHUNK_BODY;
  874. r->bytes_to_go = r->chunk_size;
  875. if (r->bytes_to_go==0){
  876. r->state=H_HTTP11_CHUNK_FINISH;
  877. r->flags|=F_TCP_REQ_COMPLETE;
  878. p++;
  879. goto skip;
  880. }
  881. break;
  882. default:
  883. LM_ERR("bad chunk size value, unexpected "
  884. "char %c in state %d\n", *p, r->state);
  885. r->state=H_SKIP; /* try to find another?*/
  886. }
  887. p++;
  888. break;
  889. #endif
  890. #ifdef READ_MSRP
  891. case H_MSRP_BODY: /* body of msrp frame */
  892. /* find lf, we are in this state if we are not interested
  893. * in anything till end of line*/
  894. r->flags |= F_TCP_REQ_MSRP_BODY;
  895. p = q_memchr(p, '\n', r->pos-p);
  896. if (p) {
  897. p++;
  898. r->state=H_MSRP_BODY_LF;
  899. } else {
  900. p=r->pos;
  901. }
  902. break;
  903. case H_MSRP_BODY_LF: /* LF in body of msrp frame */
  904. switch (*p) {
  905. case '-':
  906. p--;
  907. r->state=H_MSRP_BODY_END;
  908. break;
  909. default:
  910. r->state=H_MSRP_BODY;
  911. }
  912. p++;
  913. break;
  914. case H_MSRP_BODY_END: /* end of body for msrp frame */
  915. /* find LF and check if it is end-line */
  916. p = q_memchr(p, '\n', r->pos-p);
  917. if (p) {
  918. /* check if it is end line '-------sessid$\r\n' */
  919. if(r->pos - r->start < 10) {
  920. LM_ERR("weird situation when reading MSRP frame"
  921. " - continue reading\n");
  922. /* *p=='\n' */
  923. r->state=H_MSRP_BODY_LF;
  924. p++;
  925. break;
  926. }
  927. if(*(p-1)!='\r') {
  928. /* not ending in '\r\n' - not end-line */
  929. /* *p=='\n' */
  930. r->state=H_MSRP_BODY_LF;
  931. p++;
  932. break;
  933. }
  934. /* locate transaction id in first line
  935. * -- first line exists, that's why we are here */
  936. mfline = q_memchr(r->start, '\n', r->pos-r->start);
  937. mtransid.s = q_memchr(r->start + 5 /* 'MSRP ' */, ' ',
  938. mfline - r->start);
  939. mtransid.len = mtransid.s - r->start - 5;
  940. mtransid.s = r->start + 5;
  941. trim(&mtransid);
  942. if(memcmp(mtransid.s,
  943. p - 1 /*\r*/ - 1 /* '+'|'#'|'$' */ - mtransid.len,
  944. mtransid.len)!=0) {
  945. /* no match on session id - not end-line */
  946. /* *p=='\n' */
  947. r->state=H_MSRP_BODY_LF;
  948. p++;
  949. break;
  950. }
  951. if(memcmp(p - 1 /*\r*/ - 1 /* '+'|'#'|'$' */ - mtransid.len
  952. - 7 /* 7 x '-' */ - 1 /* '\n' */, "\n-------",
  953. 8)!=0) {
  954. /* no match on "\n-------" - not end-line */
  955. /* *p=='\n' */
  956. r->state=H_MSRP_BODY_LF;
  957. p++;
  958. break;
  959. }
  960. r->state=H_MSRP_FINISH;
  961. r->flags|=F_TCP_REQ_COMPLETE;
  962. p++;
  963. goto skip;
  964. } else {
  965. p=r->pos;
  966. }
  967. break;
  968. #endif
  969. default:
  970. LM_CRIT("unexpected state %d\n", r->state);
  971. abort();
  972. }
  973. }
  974. skip:
  975. r->parsed=p;
  976. return bytes;
  977. }
  978. #ifdef READ_MSRP
  979. int msrp_process_msg(char* tcpbuf, unsigned int len,
  980. struct receive_info* rcv_info, struct tcp_connection* con)
  981. {
  982. int ret;
  983. tcp_event_info_t tev;
  984. ret = 0;
  985. LM_DBG("MSRP Message: [[>>>\n%.*s<<<]]\n", len, tcpbuf);
  986. if(likely(sr_event_enabled(SREV_TCP_MSRP_FRAME))) {
  987. memset(&tev, 0, sizeof(tcp_event_info_t));
  988. tev.type = SREV_TCP_MSRP_FRAME;
  989. tev.buf = tcpbuf;
  990. tev.len = len;
  991. tev.rcv = rcv_info;
  992. tev.con = con;
  993. ret = sr_event_exec(SREV_TCP_MSRP_FRAME, (void*)(&tev));
  994. } else {
  995. LM_DBG("no callback registering for handling MSRP - dropping!\n");
  996. }
  997. return ret;
  998. }
  999. #endif
  1000. #ifdef READ_WS
  1001. static int tcp_read_ws(struct tcp_connection *c, int* read_flags)
  1002. {
  1003. int bytes, size, pos, mask_present;
  1004. unsigned int len;
  1005. char *p;
  1006. struct tcp_req *r;
  1007. r=&c->req;
  1008. #ifdef USE_TLS
  1009. if (unlikely(c->type == PROTO_WSS))
  1010. bytes = tls_read(c, read_flags);
  1011. else
  1012. #endif
  1013. bytes = tcp_read(c, read_flags);
  1014. if (bytes <= 0)
  1015. {
  1016. if (likely(r->parsed >= r->pos))
  1017. return 0;
  1018. }
  1019. size = r->pos - r->parsed;
  1020. p = r->parsed;
  1021. pos = 0;
  1022. /*
  1023. 0 1 2 3
  1024. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  1025. +-+-+-+-+-------+-+-------------+-------------------------------+
  1026. |F|R|R|R| opcode|M| Payload len | Extended payload length |
  1027. |I|S|S|S| (4) |A| (7) | (16/64) |
  1028. |N|V|V|V| |S| | (if payload len==126/127) |
  1029. | |1|2|3| |K| | |
  1030. +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
  1031. | Extended payload length continued, if payload len == 127 |
  1032. + - - - - - - - - - - - - - - - +-------------------------------+
  1033. | |Masking-key, if MASK set to 1 |
  1034. +-------------------------------+-------------------------------+
  1035. | Masking-key (continued) | Payload Data |
  1036. +-------------------------------- - - - - - - - - - - - - - - - +
  1037. : Payload Data continued ... :
  1038. + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
  1039. | Payload Data continued ... |
  1040. +---------------------------------------------------------------+
  1041. Do minimal parse required to make sure the full message has been
  1042. received (websocket module will do full parse and validation).
  1043. */
  1044. /* Process first two bytes */
  1045. if (size < pos + 2)
  1046. goto skip;
  1047. pos++;
  1048. mask_present = p[pos] & 0x80;
  1049. len = (p[pos++] & 0xff) & ~0x80;
  1050. /* Work out real length */
  1051. if (len == 126)
  1052. {
  1053. if (size < pos + 2)
  1054. goto skip;
  1055. len = ((p[pos + 0] & 0xff) << 8)
  1056. | ((p[pos + 1] & 0xff) << 0);
  1057. pos += 2;
  1058. }
  1059. else if (len == 127)
  1060. {
  1061. if (size < pos + 8)
  1062. goto skip;
  1063. /* Only decoding the last four bytes of the length...
  1064. This limits the size of WebSocket messages that can be
  1065. handled to 2^32 - which should be plenty for SIP! */
  1066. len = ((p[pos + 4] & 0xff) << 24)
  1067. | ((p[pos + 5] & 0xff) << 16)
  1068. | ((p[pos + 6] & 0xff) << 8)
  1069. | ((p[pos + 7] & 0xff) << 0);
  1070. pos += 8;
  1071. }
  1072. /* Skip mask */
  1073. if (mask_present)
  1074. {
  1075. if (size < pos + 4)
  1076. goto skip;
  1077. pos += 4;
  1078. }
  1079. /* Now check the whole message has been received */
  1080. if (size < pos + len)
  1081. goto skip;
  1082. pos += len;
  1083. r->flags |= F_TCP_REQ_COMPLETE;
  1084. r->parsed = &p[pos];
  1085. skip:
  1086. return bytes;
  1087. }
  1088. static int ws_process_msg(char* tcpbuf, unsigned int len,
  1089. struct receive_info* rcv_info, struct tcp_connection* con)
  1090. {
  1091. int ret;
  1092. tcp_event_info_t tev;
  1093. ret = 0;
  1094. LM_DBG("WebSocket Message: [[>>>\n%.*s<<<]]\n", len, tcpbuf);
  1095. if(likely(sr_event_enabled(SREV_TCP_WS_FRAME_IN))) {
  1096. memset(&tev, 0, sizeof(tcp_event_info_t));
  1097. tev.type = SREV_TCP_WS_FRAME_IN;
  1098. tev.buf = tcpbuf;
  1099. tev.len = len;
  1100. tev.rcv = rcv_info;
  1101. tev.con = con;
  1102. ret = sr_event_exec(SREV_TCP_WS_FRAME_IN, (void*)(&tev));
  1103. } else {
  1104. LM_DBG("no callback registering for handling WebSockets - dropping!\n");
  1105. }
  1106. return ret;
  1107. }
  1108. #endif
  1109. /**
  1110. * @brief wrapper around receive_msg() to clone the tcpbuf content
  1111. *
  1112. * When receiving over TCP, tcpbuf points inside the TCP stream buffer, but during
  1113. * processing of config, msg->buf content might be changed and may corrupt
  1114. * the content of the stream. Safer, make a clone of buf content in a local
  1115. * buffer and give that to receive_msg() to link to msg->buf
  1116. */
  1117. int receive_tcp_msg(char* tcpbuf, unsigned int len,
  1118. struct receive_info* rcv_info, struct tcp_connection* con)
  1119. {
  1120. #ifdef TCP_CLONE_RCVBUF
  1121. #ifdef DYN_BUF
  1122. char *buf = NULL;
  1123. #else
  1124. static char *buf = NULL;
  1125. static unsigned int bsize = 0;
  1126. #endif
  1127. int blen;
  1128. /* cloning is disabled via parameter */
  1129. if(likely(tcp_clone_rcvbuf==0)) {
  1130. #ifdef READ_MSRP
  1131. if(unlikely(con->req.flags&F_TCP_REQ_MSRP_FRAME))
  1132. return msrp_process_msg(tcpbuf, len, rcv_info, con);
  1133. #endif
  1134. #ifdef READ_WS
  1135. if(unlikely(con->type == PROTO_WS || con->type == PROTO_WSS))
  1136. return ws_process_msg(tcpbuf, len, rcv_info, con);
  1137. #endif
  1138. return receive_msg(tcpbuf, len, rcv_info);
  1139. }
  1140. /* min buffer size is BUF_SIZE */
  1141. blen = len;
  1142. if(blen < BUF_SIZE)
  1143. blen = BUF_SIZE;
  1144. #ifdef DYN_BUF
  1145. buf=pkg_malloc(blen+1);
  1146. if (buf==0) {
  1147. LM_ERR("could not allocate receive buffer\n");
  1148. return -1;
  1149. }
  1150. #else
  1151. /* allocate buffer when needed
  1152. * - no buffer yet
  1153. * - existing buffer too small (min size is BUF_SIZE - to accomodate most
  1154. * of SIP messages; expected larger for HTTP/XCAP)
  1155. * - existing buffer too large (e.g., we got a too big message in the past,
  1156. * let's free it)
  1157. *
  1158. * - also, use system memory, not to eat from PKG (same as static buffer
  1159. * from PKG pov)
  1160. */
  1161. if(buf==NULL || bsize < blen || blen < bsize/2) {
  1162. if(buf!=NULL)
  1163. free(buf);
  1164. buf=malloc(blen+1);
  1165. if (buf==0) {
  1166. LM_ERR("could not allocate receive buffer\n");
  1167. return -1;
  1168. }
  1169. bsize = blen;
  1170. }
  1171. #endif
  1172. memcpy(buf, tcpbuf, len);
  1173. buf[len] = '\0';
  1174. #ifdef READ_MSRP
  1175. if(unlikely(con->req.flags&F_TCP_REQ_MSRP_FRAME))
  1176. return msrp_process_msg(buf, len, rcv_info, con);
  1177. #endif
  1178. #ifdef READ_WS
  1179. if(unlikely(con->type == PROTO_WS || con->type == PROTO_WSS))
  1180. return ws_process_msg(buf, len, rcv_info, con);
  1181. #endif
  1182. return receive_msg(buf, len, rcv_info);
  1183. #else /* TCP_CLONE_RCVBUF */
  1184. #ifdef READ_MSRP
  1185. if(unlikely(con->req.flags&F_TCP_REQ_MSRP_FRAME))
  1186. return msrp_process_msg(tcpbuf, len, rcv_info, con);
  1187. #endif
  1188. #ifdef READ_WS
  1189. if(unlikely(con->type == PROTO_WS || con->type == PROTO_WSS))
  1190. return ws_process_msg(tcpbuf, len, rcv_info, con);
  1191. #endif
  1192. return receive_msg(tcpbuf, len, rcv_info);
  1193. #endif /* TCP_CLONE_RCVBUF */
  1194. }
  1195. int tcp_read_req(struct tcp_connection* con, int* bytes_read, int* read_flags)
  1196. {
  1197. int bytes;
  1198. int total_bytes;
  1199. int resp;
  1200. long size;
  1201. struct tcp_req* req;
  1202. struct dest_info dst;
  1203. char c;
  1204. int ret;
  1205. bytes=-1;
  1206. total_bytes=0;
  1207. resp=CONN_RELEASE;
  1208. req=&con->req;
  1209. again:
  1210. if (likely(req->error==TCP_REQ_OK)){
  1211. #ifdef READ_WS
  1212. if (unlikely(con->type == PROTO_WS || con->type == PROTO_WSS))
  1213. bytes=tcp_read_ws(con, read_flags);
  1214. else
  1215. #endif
  1216. bytes=tcp_read_headers(con, read_flags);
  1217. #ifdef EXTRA_DEBUG
  1218. /* if timeout state=0; goto end__req; */
  1219. DBG("read= %d bytes, parsed=%d, state=%d, error=%d\n",
  1220. bytes, (int)(req->parsed-req->start), req->state,
  1221. req->error );
  1222. DBG("tcp_read_req: last char=0x%02X, parsed msg=\n%.*s\n",
  1223. *(req->parsed-1), (int)(req->parsed-req->start),
  1224. req->start);
  1225. #endif
  1226. if (unlikely(bytes==-1)){
  1227. LOG(cfg_get(core, core_cfg, corelog),
  1228. "ERROR: tcp_read_req: error reading \n");
  1229. resp=CONN_ERROR;
  1230. goto end_req;
  1231. }
  1232. total_bytes+=bytes;
  1233. /* eof check:
  1234. * is EOF if eof on fd and req. not complete yet,
  1235. * if req. is complete we might have a second unparsed
  1236. * request after it, so postpone release_with_eof
  1237. */
  1238. if (unlikely((con->state==S_CONN_EOF) &&
  1239. (! TCP_REQ_COMPLETE(req)))) {
  1240. DBG( "tcp_read_req: EOF\n");
  1241. resp=CONN_EOF;
  1242. goto end_req;
  1243. }
  1244. }
  1245. if (unlikely(req->error!=TCP_REQ_OK)){
  1246. LM_ERR("bad request, state=%d, error=%d buf:\n%.*s\nparsed:\n%.*s\n",
  1247. req->state, req->error,
  1248. (int)(req->pos-req->buf), req->buf,
  1249. (int)(req->parsed-req->start), req->start);
  1250. DBG("- received from: port %d\n", con->rcv.src_port);
  1251. print_ip("- received from: ip ",&con->rcv.src_ip, "\n");
  1252. resp=CONN_ERROR;
  1253. goto end_req;
  1254. }
  1255. if (likely(TCP_REQ_COMPLETE(req))){
  1256. #ifdef EXTRA_DEBUG
  1257. DBG("tcp_read_req: end of header part\n");
  1258. DBG("- received from: port %d\n", con->rcv.src_port);
  1259. print_ip("- received from: ip ", &con->rcv.src_ip, "\n");
  1260. DBG("tcp_read_req: headers:\n%.*s.\n",
  1261. (int)(req->body-req->start), req->start);
  1262. #endif
  1263. if (likely(TCP_REQ_HAS_CLEN(req))){
  1264. DBG("tcp_read_req: content-length= %d\n", req->content_len);
  1265. #ifdef EXTRA_DEBUG
  1266. DBG("tcp_read_req: body:\n%.*s\n", req->content_len,req->body);
  1267. #endif
  1268. }else{
  1269. if (cfg_get(tcp, tcp_cfg, accept_no_cl)==0) {
  1270. req->error=TCP_REQ_BAD_LEN;
  1271. LM_ERR("content length not present or unparsable\n");
  1272. resp=CONN_ERROR;
  1273. goto end_req;
  1274. }
  1275. }
  1276. /* if we are here everything is nice and ok*/
  1277. resp=CONN_RELEASE;
  1278. #ifdef EXTRA_DEBUG
  1279. DBG("receiving msg(%p, %d, )\n",
  1280. req->start, (int)(req->parsed-req->start));
  1281. #endif
  1282. /* rcv.bind_address should always be !=0 */
  1283. bind_address=con->rcv.bind_address;
  1284. /* just for debugging use sendipv4 as receiving socket FIXME*/
  1285. /*
  1286. if (con->rcv.dst_ip.af==AF_INET6){
  1287. bind_address=sendipv6_tcp;
  1288. }else{
  1289. bind_address=sendipv4_tcp;
  1290. }
  1291. */
  1292. con->rcv.proto_reserved1=con->id; /* copy the id */
  1293. c=*req->parsed; /* ugly hack: zero term the msg & save the
  1294. previous char, req->parsed should be ok
  1295. because we always alloc BUF_SIZE+1 */
  1296. *req->parsed=0;
  1297. if (req->state==H_PING_CRLF) {
  1298. init_dst_from_rcv(&dst, &con->rcv);
  1299. if (tcp_send(&dst, 0, CRLF, CRLF_LEN) < 0) {
  1300. LM_ERR("CRLF ping: tcp_send() failed\n");
  1301. }
  1302. ret = 0;
  1303. } else if (unlikely(req->state==H_STUN_END)) {
  1304. /* stun request */
  1305. ret = stun_process_msg(req->start, req->parsed-req->start,
  1306. &con->rcv);
  1307. } else
  1308. #ifdef READ_MSRP
  1309. // if (unlikely(req->flags&F_TCP_REQ_MSRP_FRAME)){
  1310. if (unlikely(req->state==H_MSRP_FINISH)){
  1311. /* msrp frame */
  1312. ret = receive_tcp_msg(req->start, req->parsed-req->start,
  1313. &con->rcv, con);
  1314. }else
  1315. #endif
  1316. #ifdef READ_HTTP11
  1317. if (unlikely(req->state==H_HTTP11_CHUNK_FINISH)){
  1318. /* http chunked request */
  1319. req->body[req->content_len] = 0;
  1320. ret = receive_tcp_msg(req->start,
  1321. req->body + req->content_len - req->start,
  1322. &con->rcv, con);
  1323. }else
  1324. #endif
  1325. #ifdef READ_WS
  1326. if (unlikely(con->type == PROTO_WS || con->type == PROTO_WSS)){
  1327. ret = receive_tcp_msg(req->start, req->parsed-req->start,
  1328. &con->rcv, con);
  1329. }else
  1330. #endif
  1331. ret = receive_tcp_msg(req->start, req->parsed-req->start,
  1332. &con->rcv, con);
  1333. if (unlikely(ret < 0)) {
  1334. *req->parsed=c;
  1335. resp=CONN_ERROR;
  1336. goto end_req;
  1337. }
  1338. *req->parsed=c;
  1339. /* prepare for next request */
  1340. size=req->pos-req->parsed;
  1341. req->start=req->buf;
  1342. req->body=0;
  1343. req->error=TCP_REQ_OK;
  1344. req->state=H_SKIP_EMPTY;
  1345. req->flags=0;
  1346. req->content_len=0;
  1347. req->bytes_to_go=0;
  1348. req->pos=req->buf+size;
  1349. if (unlikely(size)){
  1350. memmove(req->buf, req->parsed, size);
  1351. req->parsed=req->buf; /* fix req->parsed after using it */
  1352. #ifdef EXTRA_DEBUG
  1353. DBG("tcp_read_req: preparing for new request, kept %ld"
  1354. " bytes\n", size);
  1355. #endif
  1356. /*if we still have some unparsed bytes, try to parse them too*/
  1357. goto again;
  1358. } else if (unlikely(con->state==S_CONN_EOF)){
  1359. DBG( "tcp_read_req: EOF after reading complete request\n");
  1360. resp=CONN_EOF;
  1361. }
  1362. req->parsed=req->buf; /* fix req->parsed */
  1363. }
  1364. end_req:
  1365. if (likely(bytes_read)) *bytes_read=total_bytes;
  1366. return resp;
  1367. }
  1368. void release_tcpconn(struct tcp_connection* c, long state, int unix_sock)
  1369. {
  1370. long response[2];
  1371. DBG( "releasing con %p, state %ld, fd=%d, id=%d\n",
  1372. c, state, c->fd, c->id);
  1373. DBG(" extra_data %p\n", c->extra_data);
  1374. /* release req & signal the parent */
  1375. c->reader_pid=0; /* reset it */
  1376. if (c->fd!=-1){
  1377. close(c->fd);
  1378. c->fd=-1;
  1379. }
  1380. /* errno==EINTR, EWOULDBLOCK a.s.o todo */
  1381. response[0]=(long)c;
  1382. response[1]=state;
  1383. if (tsend_stream(unix_sock, (char*)response, sizeof(response), -1)<=0)
  1384. LM_ERR("tsend_stream failed\n");
  1385. }
  1386. static ticks_t tcpconn_read_timeout(ticks_t t, struct timer_ln* tl, void* data)
  1387. {
  1388. struct tcp_connection *c;
  1389. c=(struct tcp_connection*)data;
  1390. /* or (struct tcp...*)(tl-offset(c->timer)) */
  1391. if (likely(!(c->state<0) && TICKS_LT(t, c->timeout))){
  1392. /* timeout extended, exit */
  1393. return (ticks_t)(c->timeout - t);
  1394. }
  1395. /* if conn->state is ERROR or BAD => force timeout too */
  1396. if (unlikely(io_watch_del(&io_w, c->fd, -1, IO_FD_CLOSING)<0)){
  1397. LM_ERR("io_watch_del failed for %p"
  1398. " id %d fd %d, state %d, flags %x, main fd %d\n",
  1399. c, c->id, c->fd, c->state, c->flags, c->s);
  1400. }
  1401. tcpconn_listrm(tcp_conn_lst, c, c_next, c_prev);
  1402. release_tcpconn(c, (c->state<0)?CONN_ERROR:CONN_RELEASE, tcpmain_sock);
  1403. return 0;
  1404. }
  1405. /* handle io routine, based on the fd_map type
  1406. * (it will be called from io_wait_loop* )
  1407. * params: fm - pointer to a fd hash entry
  1408. * idx - index in the fd_array (or -1 if not known)
  1409. * return: -1 on error, or when we are not interested any more on reads
  1410. * from this fd (e.g.: we are closing it )
  1411. * 0 on EAGAIN or when by some other way it is known that no more
  1412. * io events are queued on the fd (the receive buffer is empty).
  1413. * Usefull to detect when there are no more io events queued for
  1414. * sigio_rt, epoll_et, kqueue.
  1415. * >0 on successfull read from the fd (when there might be more io
  1416. * queued -- the receive buffer might still be non-empty)
  1417. */
  1418. inline static int handle_io(struct fd_map* fm, short events, int idx)
  1419. {
  1420. int ret;
  1421. int n;
  1422. int read_flags;
  1423. struct tcp_connection* con;
  1424. int s;
  1425. long resp;
  1426. ticks_t t;
  1427. /* update the local config */
  1428. cfg_update();
  1429. switch(fm->type){
  1430. case F_TCPMAIN:
  1431. again:
  1432. ret=n=receive_fd(fm->fd, &con, sizeof(con), &s, 0);
  1433. DBG("received n=%d con=%p, fd=%d\n", n, con, s);
  1434. if (unlikely(n<0)){
  1435. if (errno == EWOULDBLOCK || errno == EAGAIN){
  1436. ret=0;
  1437. break;
  1438. }else if (errno == EINTR) goto again;
  1439. else{
  1440. LM_CRIT("read_fd: %s \n", strerror(errno));
  1441. abort(); /* big error*/
  1442. }
  1443. }
  1444. if (unlikely(n==0)){
  1445. LM_ERR("0 bytes read\n");
  1446. goto error;
  1447. }
  1448. if (unlikely(con==0)){
  1449. LM_CRIT("null pointer\n");
  1450. goto error;
  1451. }
  1452. con->fd=s;
  1453. if (unlikely(s==-1)) {
  1454. LM_ERR("read_fd: no fd read\n");
  1455. goto con_error;
  1456. }
  1457. con->reader_pid=my_pid();
  1458. if (unlikely(con==tcp_conn_lst)){
  1459. LM_CRIT("duplicate connection received: %p, id %d, fd %d, refcnt %d"
  1460. " state %d (n=%d)\n", con, con->id, con->fd,
  1461. atomic_get(&con->refcnt), con->state, n);
  1462. goto con_error;
  1463. break; /* try to recover */
  1464. }
  1465. if (unlikely(con->state==S_CONN_BAD)){
  1466. LM_WARN("received an already bad connection: %p id %d refcnt %d\n",
  1467. con, con->id, atomic_get(&con->refcnt));
  1468. goto con_error;
  1469. }
  1470. /* if we received the fd there is most likely data waiting to
  1471. * be read => process it first to avoid extra sys calls */
  1472. read_flags=((con->flags & (F_CONN_EOF_SEEN|F_CONN_FORCE_EOF)) &&
  1473. !(con->flags & F_CONN_OOB_DATA))? RD_CONN_FORCE_EOF
  1474. :0;
  1475. #ifdef USE_TLS
  1476. repeat_1st_read:
  1477. #endif /* USE_TLS */
  1478. resp=tcp_read_req(con, &n, &read_flags);
  1479. if (unlikely(resp<0)){
  1480. /* some error occured, but on the new fd, not on the tcp
  1481. * main fd, so keep the ret value */
  1482. if (unlikely(resp!=CONN_EOF))
  1483. con->state=S_CONN_BAD;
  1484. release_tcpconn(con, resp, tcpmain_sock);
  1485. break;
  1486. }
  1487. #ifdef USE_TLS
  1488. /* repeat read if requested (for now only tls might do this) */
  1489. if (unlikely(read_flags & RD_CONN_REPEAT_READ))
  1490. goto repeat_1st_read;
  1491. #endif /* USE_TLS */
  1492. /* must be before io_watch_add, io_watch_add might catch some
  1493. * already existing events => might call handle_io and
  1494. * handle_io might decide to del. the new connection =>
  1495. * must be in the list */
  1496. tcpconn_listadd(tcp_conn_lst, con, c_next, c_prev);
  1497. t=get_ticks_raw();
  1498. con->timeout=t+S_TO_TICKS(TCP_CHILD_TIMEOUT);
  1499. /* re-activate the timer */
  1500. con->timer.f=tcpconn_read_timeout;
  1501. local_timer_reinit(&con->timer);
  1502. local_timer_add(&tcp_reader_ltimer, &con->timer,
  1503. S_TO_TICKS(TCP_CHILD_TIMEOUT), t);
  1504. if (unlikely(io_watch_add(&io_w, s, POLLIN, F_TCPCONN, con)<0)){
  1505. LM_CRIT("io_watch_add failed for %p id %d fd %d, state %d, flags %x,"
  1506. " main fd %d, refcnt %d\n",
  1507. con, con->id, con->fd, con->state, con->flags,
  1508. con->s, atomic_get(&con->refcnt));
  1509. tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev);
  1510. local_timer_del(&tcp_reader_ltimer, &con->timer);
  1511. goto con_error;
  1512. }
  1513. break;
  1514. case F_TCPCONN:
  1515. con=(struct tcp_connection*)fm->data;
  1516. if (unlikely(con->state==S_CONN_BAD)){
  1517. resp=CONN_ERROR;
  1518. if (!(con->send_flags.f & SND_F_CON_CLOSE))
  1519. LM_WARN("F_TCPCONN connection marked as bad: %p id %d refcnt %d\n",
  1520. con, con->id, atomic_get(&con->refcnt));
  1521. goto read_error;
  1522. }
  1523. read_flags=((
  1524. #ifdef POLLRDHUP
  1525. (events & POLLRDHUP) |
  1526. #endif /* POLLRDHUP */
  1527. (events & (POLLHUP|POLLERR)) |
  1528. (con->flags & (F_CONN_EOF_SEEN|F_CONN_FORCE_EOF)))
  1529. && !(events & POLLPRI))? RD_CONN_FORCE_EOF: 0;
  1530. #ifdef USE_TLS
  1531. repeat_read:
  1532. #endif /* USE_TLS */
  1533. resp=tcp_read_req(con, &ret, &read_flags);
  1534. if (unlikely(resp<0)){
  1535. read_error:
  1536. ret=-1; /* some error occured */
  1537. if (unlikely(io_watch_del(&io_w, con->fd, idx,
  1538. IO_FD_CLOSING) < 0)){
  1539. LM_CRIT("io_watch_del failed for %p id %d fd %d,"
  1540. " state %d, flags %x, main fd %d, refcnt %d\n",
  1541. con, con->id, con->fd, con->state,
  1542. con->flags, con->s, atomic_get(&con->refcnt));
  1543. }
  1544. tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev);
  1545. local_timer_del(&tcp_reader_ltimer, &con->timer);
  1546. if (unlikely(resp!=CONN_EOF))
  1547. con->state=S_CONN_BAD;
  1548. release_tcpconn(con, resp, tcpmain_sock);
  1549. }else{
  1550. #ifdef USE_TLS
  1551. if (unlikely(read_flags & RD_CONN_REPEAT_READ))
  1552. goto repeat_read;
  1553. #endif /* USE_TLS */
  1554. /* update timeout */
  1555. con->timeout=get_ticks_raw()+S_TO_TICKS(TCP_CHILD_TIMEOUT);
  1556. /* ret= 0 (read the whole socket buffer) if short read &
  1557. * !POLLPRI, bytes read otherwise */
  1558. ret&=(((read_flags & RD_CONN_SHORT_READ) &&
  1559. !(events & POLLPRI)) - 1);
  1560. }
  1561. break;
  1562. case F_NONE:
  1563. LM_CRIT("empty fd map %p (%d): {%d, %d, %p}\n",
  1564. fm, (int)(fm-io_w.fd_hash),
  1565. fm->fd, fm->type, fm->data);
  1566. goto error;
  1567. default:
  1568. LM_CRIT("uknown fd type %d\n", fm->type);
  1569. goto error;
  1570. }
  1571. return ret;
  1572. con_error:
  1573. con->state=S_CONN_BAD;
  1574. release_tcpconn(con, CONN_ERROR, tcpmain_sock);
  1575. return ret;
  1576. error:
  1577. return -1;
  1578. }
  1579. inline static void tcp_reader_timer_run(void)
  1580. {
  1581. ticks_t ticks;
  1582. ticks=get_ticks_raw();
  1583. if (unlikely((ticks-tcp_reader_prev_ticks)<TCPCONN_TIMEOUT_MIN_RUN))
  1584. return;
  1585. tcp_reader_prev_ticks=ticks;
  1586. local_timer_run(&tcp_reader_ltimer, ticks);
  1587. }
  1588. void tcp_receive_loop(int unix_sock)
  1589. {
  1590. /* init */
  1591. tcpmain_sock=unix_sock; /* init com. socket */
  1592. if (init_io_wait(&io_w, get_max_open_fds(), tcp_poll_method)<0)
  1593. goto error;
  1594. tcp_reader_prev_ticks=get_ticks_raw();
  1595. if (init_local_timer(&tcp_reader_ltimer, get_ticks_raw())!=0)
  1596. goto error;
  1597. /* add the unix socket */
  1598. if (io_watch_add(&io_w, tcpmain_sock, POLLIN, F_TCPMAIN, 0)<0){
  1599. LM_CRIT("failed to add socket to the fd list\n");
  1600. goto error;
  1601. }
  1602. /* initialize the config framework */
  1603. if (cfg_child_init()) goto error;
  1604. /* main loop */
  1605. switch(io_w.poll_method){
  1606. case POLL_POLL:
  1607. while(1){
  1608. io_wait_loop_poll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  1609. tcp_reader_timer_run();
  1610. }
  1611. break;
  1612. #ifdef HAVE_SELECT
  1613. case POLL_SELECT:
  1614. while(1){
  1615. io_wait_loop_select(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  1616. tcp_reader_timer_run();
  1617. }
  1618. break;
  1619. #endif
  1620. #ifdef HAVE_SIGIO_RT
  1621. case POLL_SIGIO_RT:
  1622. while(1){
  1623. io_wait_loop_sigio_rt(&io_w, TCP_CHILD_SELECT_TIMEOUT);
  1624. tcp_reader_timer_run();
  1625. }
  1626. break;
  1627. #endif
  1628. #ifdef HAVE_EPOLL
  1629. case POLL_EPOLL_LT:
  1630. while(1){
  1631. io_wait_loop_epoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  1632. tcp_reader_timer_run();
  1633. }
  1634. break;
  1635. case POLL_EPOLL_ET:
  1636. while(1){
  1637. io_wait_loop_epoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 1);
  1638. tcp_reader_timer_run();
  1639. }
  1640. break;
  1641. #endif
  1642. #ifdef HAVE_KQUEUE
  1643. case POLL_KQUEUE:
  1644. while(1){
  1645. io_wait_loop_kqueue(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  1646. tcp_reader_timer_run();
  1647. }
  1648. break;
  1649. #endif
  1650. #ifdef HAVE_DEVPOLL
  1651. case POLL_DEVPOLL:
  1652. while(1){
  1653. io_wait_loop_devpoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  1654. tcp_reader_timer_run();
  1655. }
  1656. break;
  1657. #endif
  1658. default:
  1659. LM_CRIT("no support for poll method %s (%d)\n",
  1660. poll_method_name(io_w.poll_method), io_w.poll_method);
  1661. goto error;
  1662. }
  1663. error:
  1664. destroy_io_wait(&io_w);
  1665. LM_CRIT("exiting...");
  1666. exit(-1);
  1667. }
  1668. int is_msg_complete(struct tcp_req* r)
  1669. {
  1670. if (TCP_REQ_HAS_CLEN(r)) {
  1671. r->state = H_STUN_FP;
  1672. return 0;
  1673. }
  1674. else {
  1675. /* STUN message is complete */
  1676. r->state = H_STUN_END;
  1677. r->flags |= F_TCP_REQ_COMPLETE |
  1678. F_TCP_REQ_HAS_CLEN; /* hack to avoid error check */
  1679. return 1;
  1680. }
  1681. }
  1682. #endif /* USE_TCP */