2
0

tcp_read.c 48 KB

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