tcp_read.c 48 KB

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