tcp_read.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /*
  28. * History:
  29. * --------
  30. * 2002-12-?? created by andrei.
  31. * 2003-02-10 zero term before calling receive_msg & undo afterward (andrei)
  32. * 2003-05-13 l: (short form of Content-Length) is now recognized (andrei)
  33. * 2003-07-01 tcp_read & friends take no a single tcp_connection
  34. * parameter & they set c->state to S_CONN_EOF on eof (andrei)
  35. * 2003-07-04 fixed tcp EOF handling (possible infinite loop) (andrei)
  36. * 2005-07-05 migrated to the new io_wait code (andrei)
  37. * 2006-02-03 use tsend_stream instead of send_all (andrei)
  38. * 2006-10-13 added STUN support - state machine for TCP (vlada)
  39. * 2007-02-20 fixed timeout calc. bug (andrei)
  40. * 2007-11-26 improved tcp timers: switched to local_timer (andrei)
  41. * 2008-02-04 optimizations: handle POLLRDHUP (if supported), detect short
  42. * reads (sock. buffer empty) (andrei)
  43. * 2009-02-26 direct blacklist support (andrei)
  44. * 2009-04-09 tcp ev and tcp stats macros added (andrei)
  45. * 2010-05-14 split tcp_read() into tcp_read() and tcp_read_data() (andrei)
  46. * 2010-05-17 new RD_CONN_REPEAT_READ flag, used by the tls hooks (andrei)
  47. */
  48. /** tcp readers processes, tcp read and pre-parse msg. functions.
  49. * @file tcp_read.c
  50. * @ingroup core
  51. * Module: @ref core
  52. */
  53. #ifdef USE_TCP
  54. #include <stdio.h>
  55. #include <errno.h>
  56. #include <string.h>
  57. #include <sys/time.h>
  58. #include <sys/types.h>
  59. #include <sys/select.h>
  60. #include <sys/socket.h>
  61. #include <unistd.h>
  62. #include <stdlib.h> /* for abort() */
  63. #include "dprint.h"
  64. #include "tcp_conn.h"
  65. #include "tcp_read.h"
  66. #include "tcp_stats.h"
  67. #include "tcp_ev.h"
  68. #include "pass_fd.h"
  69. #include "globals.h"
  70. #include "receive.h"
  71. #include "timer.h"
  72. #include "local_timer.h"
  73. #include "ut.h"
  74. #include "trim.h"
  75. #include "pt.h"
  76. #include "cfg/cfg_struct.h"
  77. #ifdef CORE_TLS
  78. #include "tls/tls_server.h"
  79. #else
  80. #include "tls_hooks.h"
  81. #endif /* CORE_TLS */
  82. #ifdef USE_DST_BLACKLIST
  83. #include "dst_blacklist.h"
  84. #endif /* USE_DST_BLACKLIST */
  85. #define HANDLE_IO_INLINE
  86. #include "io_wait.h"
  87. #include <fcntl.h> /* must be included after io_wait.h if SIGIO_RT is used */
  88. #include "tsend.h"
  89. #include "forward.h"
  90. #include "events.h"
  91. #include "stun.h"
  92. #ifdef READ_HTTP11
  93. #define HTTP11CONTINUE "HTTP/1.1 100 Continue\r\nContent-Length: 0\r\n\r\n"
  94. #define HTTP11CONTINUE_LEN (sizeof(HTTP11CONTINUE)-1)
  95. #endif
  96. #define TCPCONN_TIMEOUT_MIN_RUN 1 /* run the timers each new tick */
  97. /* types used in io_wait* */
  98. enum fd_types { F_NONE, F_TCPMAIN, F_TCPCONN };
  99. /* list of tcp connections handled by this process */
  100. static struct tcp_connection* tcp_conn_lst=0;
  101. static io_wait_h io_w; /* io_wait handler*/
  102. static int tcpmain_sock=-1;
  103. static struct local_timer tcp_reader_ltimer;
  104. static ticks_t tcp_reader_prev_ticks;
  105. int is_msg_complete(struct tcp_req* r);
  106. /**
  107. * control cloning of TCP receive buffer
  108. * - needed for operations working directly inside the buffer
  109. * (like msg_apply_changes())
  110. */
  111. #define TCP_CLONE_RCVBUF
  112. static int tcp_clone_rcvbuf = 0;
  113. int tcp_set_clone_rcvbuf(int v)
  114. {
  115. int r;
  116. r = tcp_clone_rcvbuf;
  117. tcp_clone_rcvbuf = v;
  118. return r;
  119. }
  120. #ifdef READ_HTTP11
  121. static inline char *strfindcasestrz(str *haystack, char *needlez)
  122. {
  123. int i,j;
  124. str needle;
  125. needle.s = needlez;
  126. needle.len = strlen(needlez);
  127. for(i=0;i<haystack->len-needle.len;i++) {
  128. for(j=0;j<needle.len;j++) {
  129. if ( !((haystack->s[i+j]==needle.s[j]) ||
  130. ( isalpha((int)haystack->s[i+j])
  131. && ((haystack->s[i+j])^(needle.s[j]))==0x20 )) )
  132. break;
  133. }
  134. if (j==needle.len)
  135. return haystack->s+i;
  136. }
  137. return 0;
  138. }
  139. int tcp_http11_continue(struct tcp_connection *c)
  140. {
  141. struct dest_info dst;
  142. char *p;
  143. struct msg_start fline;
  144. int ret;
  145. str msg;
  146. ret = 0;
  147. msg.s = c->req.start;
  148. msg.len = c->req.pos - c->req.start;
  149. #ifdef READ_MSRP
  150. /* skip if MSRP message */
  151. if(c->req.flags&F_TCP_REQ_MSRP_FRAME)
  152. return 0;
  153. #endif
  154. p = parse_first_line(msg.s, msg.len, &fline);
  155. if(p==NULL)
  156. return 0;
  157. if(fline.type!=SIP_REQUEST)
  158. return 0;
  159. /* check if http request */
  160. if(fline.u.request.version.len < HTTP_VERSION_LEN
  161. || strncasecmp(fline.u.request.version.s,
  162. HTTP_VERSION, HTTP_VERSION_LEN))
  163. return 0;
  164. /* check for Expect header */
  165. if(strfindcasestrz(&msg, "Expect: 100-continue")!=NULL)
  166. {
  167. init_dst_from_rcv(&dst, &c->rcv);
  168. if (tcp_send(&dst, 0, HTTP11CONTINUE, HTTP11CONTINUE_LEN) < 0) {
  169. LM_ERR("HTTP/1.1 continue failed\n");
  170. }
  171. }
  172. /* check for Transfer-Encoding header */
  173. if(strfindcasestrz(&msg, "Transfer-Encoding: chunked")!=NULL)
  174. {
  175. c->req.flags |= F_TCP_REQ_BCHUNKED;
  176. ret = 1;
  177. }
  178. 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. LM_ERR("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. LM_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. LM_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. LM_ERR("bad Content-Length header value, unexpected "
  761. "char %c in state %d\n", *p, r->state);
  762. r->state=H_SKIP; /* try to find another?*/
  763. }
  764. p++;
  765. break;
  766. #ifdef READ_HTTP11
  767. case H_HTTP11_CHUNK_START: /* start a new body chunk: SIZE\r\nBODY\r\n */
  768. r->chunk_size = 0;
  769. r->state = H_HTTP11_CHUNK_SIZE;
  770. break;
  771. case H_HTTP11_CHUNK_BODY: /* content of chunnk */
  772. remaining=r->pos-p;
  773. if (remaining>r->bytes_to_go) remaining=r->bytes_to_go;
  774. r->bytes_to_go-=remaining;
  775. p+=remaining;
  776. if (r->bytes_to_go==0){
  777. r->state = H_HTTP11_CHUNK_END;
  778. /* shift back body content */
  779. if(r->chunk_size>0 && p-r->chunk_size>r->body) {
  780. memmove(r->body + r->content_len, p - r->chunk_size,
  781. r->chunk_size);
  782. r->content_len += r->chunk_size;
  783. }
  784. goto skip;
  785. }
  786. break;
  787. case H_HTTP11_CHUNK_END:
  788. switch(*p){
  789. case '\r':
  790. case ' ':
  791. case '\t': /* skip */
  792. break;
  793. case '\n':
  794. r->state = H_HTTP11_CHUNK_START;
  795. break;
  796. default:
  797. LM_ERR("bad chunk, unexpected "
  798. "char %c in state %d\n", *p, r->state);
  799. r->state=H_SKIP; /* try to find another?*/
  800. }
  801. p++;
  802. break;
  803. case H_HTTP11_CHUNK_SIZE:
  804. switch(*p){
  805. case '0': case '1': case '2': case '3':
  806. case '4': case '5': case '6': case '7':
  807. case '8': case '9':
  808. r->chunk_size <<= 4;
  809. r->chunk_size += *p - '0';
  810. break;
  811. case 'a': case 'b': case 'c': case 'd':
  812. case 'e': case 'f':
  813. r->chunk_size <<= 4;
  814. r->chunk_size += *p - 'a' + 10;
  815. break;
  816. case 'A': case 'B': case 'C': case 'D':
  817. case 'E': case 'F':
  818. r->chunk_size <<= 4;
  819. r->chunk_size += *p - 'A' + 10;
  820. break;
  821. case '\r':
  822. case ' ':
  823. case '\t': /* skip */
  824. break;
  825. case '\n':
  826. /* end of line, parse successful */
  827. r->state=H_HTTP11_CHUNK_BODY;
  828. r->bytes_to_go = r->chunk_size;
  829. if (r->bytes_to_go==0){
  830. r->state=H_HTTP11_CHUNK_FINISH;
  831. r->flags|=F_TCP_REQ_COMPLETE;
  832. p++;
  833. goto skip;
  834. }
  835. break;
  836. default:
  837. LM_ERR("bad chunk size value, unexpected "
  838. "char %c in state %d\n", *p, r->state);
  839. r->state=H_SKIP; /* try to find another?*/
  840. }
  841. p++;
  842. break;
  843. #endif
  844. #ifdef READ_MSRP
  845. case H_MSRP_BODY: /* body of msrp frame */
  846. /* find lf, we are in this state if we are not interested
  847. * in anything till end of line*/
  848. r->flags |= F_TCP_REQ_MSRP_BODY;
  849. p = q_memchr(p, '\n', r->pos-p);
  850. if (p) {
  851. p++;
  852. r->state=H_MSRP_BODY_LF;
  853. } else {
  854. p=r->pos;
  855. }
  856. break;
  857. case H_MSRP_BODY_LF: /* LF in body of msrp frame */
  858. switch (*p) {
  859. case '-':
  860. p--;
  861. r->state=H_MSRP_BODY_END;
  862. break;
  863. default:
  864. r->state=H_MSRP_BODY;
  865. }
  866. p++;
  867. break;
  868. case H_MSRP_BODY_END: /* end of body for msrp frame */
  869. /* find LF and check if it is end-line */
  870. p = q_memchr(p, '\n', r->pos-p);
  871. if (p) {
  872. /* check if it is end line '-------sessid$\r\n' */
  873. if(r->pos - r->start < 10) {
  874. LM_ERR("weird situation when reading MSRP frame"
  875. " - continue reading\n");
  876. /* *p=='\n' */
  877. r->state=H_MSRP_BODY_LF;
  878. p++;
  879. break;
  880. }
  881. if(*(p-1)!='\r') {
  882. /* not ending in '\r\n' - not end-line */
  883. /* *p=='\n' */
  884. r->state=H_MSRP_BODY_LF;
  885. p++;
  886. break;
  887. }
  888. /* locate transaction id in first line
  889. * -- first line exists, that's why we are here */
  890. mfline = q_memchr(r->start, '\n', r->pos-r->start);
  891. mtransid.s = q_memchr(r->start + 5 /* 'MSRP ' */, ' ',
  892. mfline - r->start);
  893. mtransid.len = mtransid.s - r->start - 5;
  894. mtransid.s = r->start + 5;
  895. trim(&mtransid);
  896. if(memcmp(mtransid.s,
  897. p - 1 /*\r*/ - 1 /* '+'|'#'|'$' */ - mtransid.len,
  898. mtransid.len)!=0) {
  899. /* no match on session id - not end-line */
  900. /* *p=='\n' */
  901. r->state=H_MSRP_BODY_LF;
  902. p++;
  903. break;
  904. }
  905. if(memcmp(p - 1 /*\r*/ - 1 /* '+'|'#'|'$' */ - mtransid.len
  906. - 7 /* 7 x '-' */ - 1 /* '\n' */, "\n-------",
  907. 8)!=0) {
  908. /* no match on "\n-------" - not end-line */
  909. /* *p=='\n' */
  910. r->state=H_MSRP_BODY_LF;
  911. p++;
  912. break;
  913. }
  914. r->state=H_MSRP_FINISH;
  915. r->flags|=F_TCP_REQ_COMPLETE;
  916. p++;
  917. goto skip;
  918. } else {
  919. p=r->pos;
  920. }
  921. break;
  922. #endif
  923. default:
  924. LM_CRIT("unexpected state %d\n", r->state);
  925. abort();
  926. }
  927. }
  928. skip:
  929. r->parsed=p;
  930. return bytes;
  931. }
  932. #ifdef READ_MSRP
  933. int msrp_process_msg(char* tcpbuf, unsigned int len,
  934. struct receive_info* rcv_info, struct tcp_connection* con)
  935. {
  936. int ret;
  937. tcp_event_info_t tev;
  938. ret = 0;
  939. LM_DBG("MSRP Message: [[>>>\n%.*s<<<]]\n", len, tcpbuf);
  940. if(likely(sr_event_enabled(SREV_TCP_MSRP_FRAME))) {
  941. memset(&tev, 0, sizeof(tcp_event_info_t));
  942. tev.type = SREV_TCP_MSRP_FRAME;
  943. tev.buf = tcpbuf;
  944. tev.len = len;
  945. tev.rcv = rcv_info;
  946. tev.con = con;
  947. ret = sr_event_exec(SREV_TCP_MSRP_FRAME, (void*)(&tev));
  948. } else {
  949. LM_DBG("no callback registering for handling MSRP - dropping!\n");
  950. }
  951. return ret;
  952. }
  953. #endif
  954. #ifdef READ_WS
  955. static int tcp_read_ws(struct tcp_connection *c, int* read_flags)
  956. {
  957. int bytes, size, pos, mask_present;
  958. unsigned int len;
  959. char *p;
  960. struct tcp_req *r;
  961. r=&c->req;
  962. #ifdef USE_TLS
  963. if (unlikely(c->type == PROTO_WSS))
  964. bytes = tls_read(c, read_flags);
  965. else
  966. #endif
  967. bytes = tcp_read(c, read_flags);
  968. if (bytes <= 0)
  969. {
  970. if (likely(r->parsed >= r->pos))
  971. return 0;
  972. }
  973. size = r->pos - r->parsed;
  974. p = r->parsed;
  975. pos = 0;
  976. /*
  977. 0 1 2 3
  978. 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
  979. +-+-+-+-+-------+-+-------------+-------------------------------+
  980. |F|R|R|R| opcode|M| Payload len | Extended payload length |
  981. |I|S|S|S| (4) |A| (7) | (16/64) |
  982. |N|V|V|V| |S| | (if payload len==126/127) |
  983. | |1|2|3| |K| | |
  984. +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
  985. | Extended payload length continued, if payload len == 127 |
  986. + - - - - - - - - - - - - - - - +-------------------------------+
  987. | |Masking-key, if MASK set to 1 |
  988. +-------------------------------+-------------------------------+
  989. | Masking-key (continued) | Payload Data |
  990. +-------------------------------- - - - - - - - - - - - - - - - +
  991. : Payload Data continued ... :
  992. + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
  993. | Payload Data continued ... |
  994. +---------------------------------------------------------------+
  995. Do minimal parse required to make sure the full message has been
  996. received (websocket module will do full parse and validation).
  997. */
  998. /* Process first two bytes */
  999. if (size < pos + 2)
  1000. goto skip;
  1001. pos++;
  1002. mask_present = p[pos] & 0x80;
  1003. len = (p[pos++] & 0xff) & ~0x80;
  1004. /* Work out real length */
  1005. if (len == 126)
  1006. {
  1007. if (size < pos + 2)
  1008. goto skip;
  1009. len = ((p[pos + 0] & 0xff) << 8)
  1010. | ((p[pos + 1] & 0xff) << 0);
  1011. pos += 2;
  1012. }
  1013. else if (len == 127)
  1014. {
  1015. if (size < pos + 8)
  1016. goto skip;
  1017. /* Only decoding the last four bytes of the length...
  1018. This limits the size of WebSocket messages that can be
  1019. handled to 2^32 - which should be plenty for SIP! */
  1020. len = ((p[pos + 4] & 0xff) << 24)
  1021. | ((p[pos + 5] & 0xff) << 16)
  1022. | ((p[pos + 6] & 0xff) << 8)
  1023. | ((p[pos + 7] & 0xff) << 0);
  1024. pos += 8;
  1025. }
  1026. /* Skip mask */
  1027. if (mask_present)
  1028. {
  1029. if (size < pos + 4)
  1030. goto skip;
  1031. pos += 4;
  1032. }
  1033. /* Now check the whole message has been received */
  1034. if (size < pos + len)
  1035. goto skip;
  1036. pos += len;
  1037. r->flags |= F_TCP_REQ_COMPLETE;
  1038. r->parsed = &p[pos];
  1039. skip:
  1040. return bytes;
  1041. }
  1042. static int ws_process_msg(char* tcpbuf, unsigned int len,
  1043. struct receive_info* rcv_info, struct tcp_connection* con)
  1044. {
  1045. int ret;
  1046. tcp_event_info_t tev;
  1047. ret = 0;
  1048. LM_DBG("WebSocket Message: [[>>>\n%.*s<<<]]\n", len, tcpbuf);
  1049. if(likely(sr_event_enabled(SREV_TCP_WS_FRAME_IN))) {
  1050. memset(&tev, 0, sizeof(tcp_event_info_t));
  1051. tev.type = SREV_TCP_WS_FRAME_IN;
  1052. tev.buf = tcpbuf;
  1053. tev.len = len;
  1054. tev.rcv = rcv_info;
  1055. tev.con = con;
  1056. ret = sr_event_exec(SREV_TCP_WS_FRAME_IN, (void*)(&tev));
  1057. } else {
  1058. LM_DBG("no callback registering for handling WebSockets - dropping!\n");
  1059. }
  1060. return ret;
  1061. }
  1062. #endif
  1063. /**
  1064. * @brief wrapper around receive_msg() to clone the tcpbuf content
  1065. *
  1066. * When receiving over TCP, tcpbuf points inside the TCP stream buffer, but during
  1067. * processing of config, msg->buf content might be changed and may corrupt
  1068. * the content of the stream. Safer, make a clone of buf content in a local
  1069. * buffer and give that to receive_msg() to link to msg->buf
  1070. */
  1071. int receive_tcp_msg(char* tcpbuf, unsigned int len,
  1072. struct receive_info* rcv_info, struct tcp_connection* con)
  1073. {
  1074. #ifdef TCP_CLONE_RCVBUF
  1075. #ifdef DYN_BUF
  1076. char *buf = NULL;
  1077. #else
  1078. static char *buf = NULL;
  1079. static unsigned int bsize = 0;
  1080. #endif
  1081. int blen;
  1082. /* cloning is disabled via parameter */
  1083. if(likely(tcp_clone_rcvbuf==0)) {
  1084. #ifdef READ_MSRP
  1085. if(unlikely(con->req.flags&F_TCP_REQ_MSRP_FRAME))
  1086. return msrp_process_msg(tcpbuf, len, rcv_info, con);
  1087. #endif
  1088. #ifdef READ_WS
  1089. if(unlikely(con->type == PROTO_WS || con->type == PROTO_WSS))
  1090. return ws_process_msg(tcpbuf, len, rcv_info, con);
  1091. #endif
  1092. return receive_msg(tcpbuf, len, rcv_info);
  1093. }
  1094. /* min buffer size is BUF_SIZE */
  1095. blen = len;
  1096. if(blen < BUF_SIZE)
  1097. blen = BUF_SIZE;
  1098. #ifdef DYN_BUF
  1099. buf=pkg_malloc(blen+1);
  1100. if (buf==0) {
  1101. LM_ERR("could not allocate receive buffer\n");
  1102. return -1;
  1103. }
  1104. #else
  1105. /* allocate buffer when needed
  1106. * - no buffer yet
  1107. * - existing buffer too small (min size is BUF_SIZE - to accomodate most
  1108. * of SIP messages; expected larger for HTTP/XCAP)
  1109. * - existing buffer too large (e.g., we got a too big message in the past,
  1110. * let's free it)
  1111. *
  1112. * - also, use system memory, not to eat from PKG (same as static buffer
  1113. * from PKG pov)
  1114. */
  1115. if(buf==NULL || bsize < blen || blen < bsize/2) {
  1116. if(buf!=NULL)
  1117. free(buf);
  1118. buf=malloc(blen+1);
  1119. if (buf==0) {
  1120. LM_ERR("could not allocate receive buffer\n");
  1121. return -1;
  1122. }
  1123. bsize = blen;
  1124. }
  1125. #endif
  1126. memcpy(buf, tcpbuf, len);
  1127. buf[len] = '\0';
  1128. #ifdef READ_MSRP
  1129. if(unlikely(con->req.flags&F_TCP_REQ_MSRP_FRAME))
  1130. return msrp_process_msg(buf, len, rcv_info, con);
  1131. #endif
  1132. #ifdef READ_WS
  1133. if(unlikely(con->type == PROTO_WS || con->type == PROTO_WSS))
  1134. return ws_process_msg(buf, len, rcv_info, con);
  1135. #endif
  1136. return receive_msg(buf, len, rcv_info);
  1137. #else /* TCP_CLONE_RCVBUF */
  1138. #ifdef READ_MSRP
  1139. if(unlikely(con->req.flags&F_TCP_REQ_MSRP_FRAME))
  1140. return msrp_process_msg(tcpbuf, len, rcv_info, con);
  1141. #endif
  1142. #ifdef READ_WS
  1143. if(unlikely(con->type == PROTO_WS || con->type == PROTO_WSS))
  1144. return ws_process_msg(tcpbuf, len, rcv_info, con);
  1145. #endif
  1146. return receive_msg(tcpbuf, len, rcv_info);
  1147. #endif /* TCP_CLONE_RCVBUF */
  1148. }
  1149. int tcp_read_req(struct tcp_connection* con, int* bytes_read, int* read_flags)
  1150. {
  1151. int bytes;
  1152. int total_bytes;
  1153. int resp;
  1154. long size;
  1155. struct tcp_req* req;
  1156. struct dest_info dst;
  1157. char c;
  1158. int ret;
  1159. bytes=-1;
  1160. total_bytes=0;
  1161. resp=CONN_RELEASE;
  1162. req=&con->req;
  1163. again:
  1164. if (likely(req->error==TCP_REQ_OK)){
  1165. #ifdef READ_WS
  1166. if (unlikely(con->type == PROTO_WS || con->type == PROTO_WSS))
  1167. bytes=tcp_read_ws(con, read_flags);
  1168. else
  1169. #endif
  1170. bytes=tcp_read_headers(con, read_flags);
  1171. #ifdef EXTRA_DEBUG
  1172. /* if timeout state=0; goto end__req; */
  1173. DBG("read= %d bytes, parsed=%d, state=%d, error=%d\n",
  1174. bytes, (int)(req->parsed-req->start), req->state,
  1175. req->error );
  1176. DBG("tcp_read_req: last char=0x%02X, parsed msg=\n%.*s\n",
  1177. *(req->parsed-1), (int)(req->parsed-req->start),
  1178. req->start);
  1179. #endif
  1180. if (unlikely(bytes==-1)){
  1181. LOG(cfg_get(core, core_cfg, corelog),
  1182. "ERROR: tcp_read_req: error reading \n");
  1183. resp=CONN_ERROR;
  1184. goto end_req;
  1185. }
  1186. total_bytes+=bytes;
  1187. /* eof check:
  1188. * is EOF if eof on fd and req. not complete yet,
  1189. * if req. is complete we might have a second unparsed
  1190. * request after it, so postpone release_with_eof
  1191. */
  1192. if (unlikely((con->state==S_CONN_EOF) &&
  1193. (! TCP_REQ_COMPLETE(req)))) {
  1194. DBG( "tcp_read_req: EOF\n");
  1195. resp=CONN_EOF;
  1196. goto end_req;
  1197. }
  1198. }
  1199. if (unlikely(req->error!=TCP_REQ_OK)){
  1200. LM_ERR("bad request, state=%d, error=%d buf:\n%.*s\nparsed:\n%.*s\n",
  1201. req->state, req->error,
  1202. (int)(req->pos-req->buf), req->buf,
  1203. (int)(req->parsed-req->start), req->start);
  1204. DBG("- received from: port %d\n", con->rcv.src_port);
  1205. print_ip("- received from: ip ",&con->rcv.src_ip, "\n");
  1206. resp=CONN_ERROR;
  1207. goto end_req;
  1208. }
  1209. if (likely(TCP_REQ_COMPLETE(req))){
  1210. #ifdef EXTRA_DEBUG
  1211. DBG("tcp_read_req: end of header part\n");
  1212. DBG("- received from: port %d\n", con->rcv.src_port);
  1213. print_ip("- received from: ip ", &con->rcv.src_ip, "\n");
  1214. DBG("tcp_read_req: headers:\n%.*s.\n",
  1215. (int)(req->body-req->start), req->start);
  1216. #endif
  1217. if (likely(TCP_REQ_HAS_CLEN(req))){
  1218. DBG("tcp_read_req: content-length= %d\n", req->content_len);
  1219. #ifdef EXTRA_DEBUG
  1220. DBG("tcp_read_req: body:\n%.*s\n", req->content_len,req->body);
  1221. #endif
  1222. }else{
  1223. if (cfg_get(tcp, tcp_cfg, accept_no_cl)==0) {
  1224. req->error=TCP_REQ_BAD_LEN;
  1225. LM_ERR("content length not present or unparsable\n");
  1226. resp=CONN_ERROR;
  1227. goto end_req;
  1228. }
  1229. }
  1230. /* if we are here everything is nice and ok*/
  1231. resp=CONN_RELEASE;
  1232. #ifdef EXTRA_DEBUG
  1233. DBG("receiving msg(%p, %d, )\n",
  1234. req->start, (int)(req->parsed-req->start));
  1235. #endif
  1236. /* rcv.bind_address should always be !=0 */
  1237. bind_address=con->rcv.bind_address;
  1238. /* just for debugging use sendipv4 as receiving socket FIXME*/
  1239. /*
  1240. if (con->rcv.dst_ip.af==AF_INET6){
  1241. bind_address=sendipv6_tcp;
  1242. }else{
  1243. bind_address=sendipv4_tcp;
  1244. }
  1245. */
  1246. con->rcv.proto_reserved1=con->id; /* copy the id */
  1247. c=*req->parsed; /* ugly hack: zero term the msg & save the
  1248. previous char, req->parsed should be ok
  1249. because we always alloc BUF_SIZE+1 */
  1250. *req->parsed=0;
  1251. if (req->state==H_PING_CRLF) {
  1252. init_dst_from_rcv(&dst, &con->rcv);
  1253. if (tcp_send(&dst, 0, CRLF, CRLF_LEN) < 0) {
  1254. LM_ERR("CRLF ping: tcp_send() failed\n");
  1255. }
  1256. ret = 0;
  1257. } else if (unlikely(req->state==H_STUN_END)) {
  1258. /* stun request */
  1259. ret = stun_process_msg(req->start, req->parsed-req->start,
  1260. &con->rcv);
  1261. } else
  1262. #ifdef READ_MSRP
  1263. // if (unlikely(req->flags&F_TCP_REQ_MSRP_FRAME)){
  1264. if (unlikely(req->state==H_MSRP_FINISH)){
  1265. /* msrp frame */
  1266. ret = receive_tcp_msg(req->start, req->parsed-req->start,
  1267. &con->rcv, con);
  1268. }else
  1269. #endif
  1270. #ifdef READ_HTTP11
  1271. if (unlikely(req->state==H_HTTP11_CHUNK_FINISH)){
  1272. /* http chunked request */
  1273. req->body[req->content_len] = 0;
  1274. ret = receive_tcp_msg(req->start,
  1275. req->body + req->content_len - req->start,
  1276. &con->rcv, con);
  1277. }else
  1278. #endif
  1279. #ifdef READ_WS
  1280. if (unlikely(con->type == PROTO_WS || con->type == PROTO_WSS)){
  1281. ret = receive_tcp_msg(req->start, req->parsed-req->start,
  1282. &con->rcv, con);
  1283. }else
  1284. #endif
  1285. ret = receive_tcp_msg(req->start, req->parsed-req->start,
  1286. &con->rcv, con);
  1287. if (unlikely(ret < 0)) {
  1288. *req->parsed=c;
  1289. resp=CONN_ERROR;
  1290. goto end_req;
  1291. }
  1292. *req->parsed=c;
  1293. /* prepare for next request */
  1294. size=req->pos-req->parsed;
  1295. req->start=req->buf;
  1296. req->body=0;
  1297. req->error=TCP_REQ_OK;
  1298. req->state=H_SKIP_EMPTY;
  1299. req->flags=0;
  1300. req->content_len=0;
  1301. req->bytes_to_go=0;
  1302. req->pos=req->buf+size;
  1303. if (unlikely(size)){
  1304. memmove(req->buf, req->parsed, size);
  1305. req->parsed=req->buf; /* fix req->parsed after using it */
  1306. #ifdef EXTRA_DEBUG
  1307. DBG("tcp_read_req: preparing for new request, kept %ld"
  1308. " bytes\n", size);
  1309. #endif
  1310. /*if we still have some unparsed bytes, try to parse them too*/
  1311. goto again;
  1312. } else if (unlikely(con->state==S_CONN_EOF)){
  1313. DBG( "tcp_read_req: EOF after reading complete request\n");
  1314. resp=CONN_EOF;
  1315. }
  1316. req->parsed=req->buf; /* fix req->parsed */
  1317. }
  1318. end_req:
  1319. if (likely(bytes_read)) *bytes_read=total_bytes;
  1320. return resp;
  1321. }
  1322. void release_tcpconn(struct tcp_connection* c, long state, int unix_sock)
  1323. {
  1324. long response[2];
  1325. DBG( "releasing con %p, state %ld, fd=%d, id=%d\n",
  1326. c, state, c->fd, c->id);
  1327. DBG(" extra_data %p\n", c->extra_data);
  1328. /* release req & signal the parent */
  1329. c->reader_pid=0; /* reset it */
  1330. if (c->fd!=-1){
  1331. close(c->fd);
  1332. c->fd=-1;
  1333. }
  1334. /* errno==EINTR, EWOULDBLOCK a.s.o todo */
  1335. response[0]=(long)c;
  1336. response[1]=state;
  1337. if (tsend_stream(unix_sock, (char*)response, sizeof(response), -1)<=0)
  1338. LM_ERR("tsend_stream failed\n");
  1339. }
  1340. static ticks_t tcpconn_read_timeout(ticks_t t, struct timer_ln* tl, void* data)
  1341. {
  1342. struct tcp_connection *c;
  1343. c=(struct tcp_connection*)data;
  1344. /* or (struct tcp...*)(tl-offset(c->timer)) */
  1345. if (likely(!(c->state<0) && TICKS_LT(t, c->timeout))){
  1346. /* timeout extended, exit */
  1347. return (ticks_t)(c->timeout - t);
  1348. }
  1349. /* if conn->state is ERROR or BAD => force timeout too */
  1350. if (unlikely(io_watch_del(&io_w, c->fd, -1, IO_FD_CLOSING)<0)){
  1351. LM_ERR("io_watch_del failed for %p"
  1352. " id %d fd %d, state %d, flags %x, main fd %d\n",
  1353. c, c->id, c->fd, c->state, c->flags, c->s);
  1354. }
  1355. tcpconn_listrm(tcp_conn_lst, c, c_next, c_prev);
  1356. release_tcpconn(c, (c->state<0)?CONN_ERROR:CONN_RELEASE, tcpmain_sock);
  1357. return 0;
  1358. }
  1359. /* handle io routine, based on the fd_map type
  1360. * (it will be called from io_wait_loop* )
  1361. * params: fm - pointer to a fd hash entry
  1362. * idx - index in the fd_array (or -1 if not known)
  1363. * return: -1 on error, or when we are not interested any more on reads
  1364. * from this fd (e.g.: we are closing it )
  1365. * 0 on EAGAIN or when by some other way it is known that no more
  1366. * io events are queued on the fd (the receive buffer is empty).
  1367. * Usefull to detect when there are no more io events queued for
  1368. * sigio_rt, epoll_et, kqueue.
  1369. * >0 on successfull read from the fd (when there might be more io
  1370. * queued -- the receive buffer might still be non-empty)
  1371. */
  1372. inline static int handle_io(struct fd_map* fm, short events, int idx)
  1373. {
  1374. int ret;
  1375. int n;
  1376. int read_flags;
  1377. struct tcp_connection* con;
  1378. int s;
  1379. long resp;
  1380. ticks_t t;
  1381. /* update the local config */
  1382. cfg_update();
  1383. switch(fm->type){
  1384. case F_TCPMAIN:
  1385. again:
  1386. ret=n=receive_fd(fm->fd, &con, sizeof(con), &s, 0);
  1387. DBG("received n=%d con=%p, fd=%d\n", n, con, s);
  1388. if (unlikely(n<0)){
  1389. if (errno == EWOULDBLOCK || errno == EAGAIN){
  1390. ret=0;
  1391. break;
  1392. }else if (errno == EINTR) goto again;
  1393. else{
  1394. LM_CRIT("read_fd: %s \n", strerror(errno));
  1395. abort(); /* big error*/
  1396. }
  1397. }
  1398. if (unlikely(n==0)){
  1399. LM_ERR("0 bytes read\n");
  1400. goto error;
  1401. }
  1402. if (unlikely(con==0)){
  1403. LM_CRIT("null pointer\n");
  1404. goto error;
  1405. }
  1406. con->fd=s;
  1407. if (unlikely(s==-1)) {
  1408. LM_ERR("read_fd: no fd read\n");
  1409. goto con_error;
  1410. }
  1411. con->reader_pid=my_pid();
  1412. if (unlikely(con==tcp_conn_lst)){
  1413. LM_CRIT("duplicate connection received: %p, id %d, fd %d, refcnt %d"
  1414. " state %d (n=%d)\n", con, con->id, con->fd,
  1415. atomic_get(&con->refcnt), con->state, n);
  1416. goto con_error;
  1417. break; /* try to recover */
  1418. }
  1419. if (unlikely(con->state==S_CONN_BAD)){
  1420. LM_WARN("received an already bad connection: %p id %d refcnt %d\n",
  1421. con, con->id, atomic_get(&con->refcnt));
  1422. goto con_error;
  1423. }
  1424. /* if we received the fd there is most likely data waiting to
  1425. * be read => process it first to avoid extra sys calls */
  1426. read_flags=((con->flags & (F_CONN_EOF_SEEN|F_CONN_FORCE_EOF)) &&
  1427. !(con->flags & F_CONN_OOB_DATA))? RD_CONN_FORCE_EOF
  1428. :0;
  1429. #ifdef USE_TLS
  1430. repeat_1st_read:
  1431. #endif /* USE_TLS */
  1432. resp=tcp_read_req(con, &n, &read_flags);
  1433. if (unlikely(resp<0)){
  1434. /* some error occured, but on the new fd, not on the tcp
  1435. * main fd, so keep the ret value */
  1436. if (unlikely(resp!=CONN_EOF))
  1437. con->state=S_CONN_BAD;
  1438. release_tcpconn(con, resp, tcpmain_sock);
  1439. break;
  1440. }
  1441. #ifdef USE_TLS
  1442. /* repeat read if requested (for now only tls might do this) */
  1443. if (unlikely(read_flags & RD_CONN_REPEAT_READ))
  1444. goto repeat_1st_read;
  1445. #endif /* USE_TLS */
  1446. /* must be before io_watch_add, io_watch_add might catch some
  1447. * already existing events => might call handle_io and
  1448. * handle_io might decide to del. the new connection =>
  1449. * must be in the list */
  1450. tcpconn_listadd(tcp_conn_lst, con, c_next, c_prev);
  1451. t=get_ticks_raw();
  1452. con->timeout=t+S_TO_TICKS(TCP_CHILD_TIMEOUT);
  1453. /* re-activate the timer */
  1454. con->timer.f=tcpconn_read_timeout;
  1455. local_timer_reinit(&con->timer);
  1456. local_timer_add(&tcp_reader_ltimer, &con->timer,
  1457. S_TO_TICKS(TCP_CHILD_TIMEOUT), t);
  1458. if (unlikely(io_watch_add(&io_w, s, POLLIN, F_TCPCONN, con)<0)){
  1459. LM_CRIT("io_watch_add failed for %p id %d fd %d, state %d, flags %x,"
  1460. " main fd %d, refcnt %d\n",
  1461. con, con->id, con->fd, con->state, con->flags,
  1462. con->s, atomic_get(&con->refcnt));
  1463. tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev);
  1464. local_timer_del(&tcp_reader_ltimer, &con->timer);
  1465. goto con_error;
  1466. }
  1467. break;
  1468. case F_TCPCONN:
  1469. con=(struct tcp_connection*)fm->data;
  1470. if (unlikely(con->state==S_CONN_BAD)){
  1471. resp=CONN_ERROR;
  1472. if (!(con->send_flags.f & SND_F_CON_CLOSE))
  1473. LM_WARN("F_TCPCONN connection marked as bad: %p id %d refcnt %d\n",
  1474. con, con->id, atomic_get(&con->refcnt));
  1475. goto read_error;
  1476. }
  1477. read_flags=((
  1478. #ifdef POLLRDHUP
  1479. (events & POLLRDHUP) |
  1480. #endif /* POLLRDHUP */
  1481. (events & (POLLHUP|POLLERR)) |
  1482. (con->flags & (F_CONN_EOF_SEEN|F_CONN_FORCE_EOF)))
  1483. && !(events & POLLPRI))? RD_CONN_FORCE_EOF: 0;
  1484. #ifdef USE_TLS
  1485. repeat_read:
  1486. #endif /* USE_TLS */
  1487. resp=tcp_read_req(con, &ret, &read_flags);
  1488. if (unlikely(resp<0)){
  1489. read_error:
  1490. ret=-1; /* some error occured */
  1491. if (unlikely(io_watch_del(&io_w, con->fd, idx,
  1492. IO_FD_CLOSING) < 0)){
  1493. LM_CRIT("io_watch_del failed for %p id %d fd %d,"
  1494. " state %d, flags %x, main fd %d, refcnt %d\n",
  1495. con, con->id, con->fd, con->state,
  1496. con->flags, con->s, atomic_get(&con->refcnt));
  1497. }
  1498. tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev);
  1499. local_timer_del(&tcp_reader_ltimer, &con->timer);
  1500. if (unlikely(resp!=CONN_EOF))
  1501. con->state=S_CONN_BAD;
  1502. release_tcpconn(con, resp, tcpmain_sock);
  1503. }else{
  1504. #ifdef USE_TLS
  1505. if (unlikely(read_flags & RD_CONN_REPEAT_READ))
  1506. goto repeat_read;
  1507. #endif /* USE_TLS */
  1508. /* update timeout */
  1509. con->timeout=get_ticks_raw()+S_TO_TICKS(TCP_CHILD_TIMEOUT);
  1510. /* ret= 0 (read the whole socket buffer) if short read &
  1511. * !POLLPRI, bytes read otherwise */
  1512. ret&=(((read_flags & RD_CONN_SHORT_READ) &&
  1513. !(events & POLLPRI)) - 1);
  1514. }
  1515. break;
  1516. case F_NONE:
  1517. LM_CRIT("empty fd map %p (%d): {%d, %d, %p}\n",
  1518. fm, (int)(fm-io_w.fd_hash),
  1519. fm->fd, fm->type, fm->data);
  1520. goto error;
  1521. default:
  1522. LM_CRIT("uknown fd type %d\n", fm->type);
  1523. goto error;
  1524. }
  1525. return ret;
  1526. con_error:
  1527. con->state=S_CONN_BAD;
  1528. release_tcpconn(con, CONN_ERROR, tcpmain_sock);
  1529. return ret;
  1530. error:
  1531. return -1;
  1532. }
  1533. inline static void tcp_reader_timer_run(void)
  1534. {
  1535. ticks_t ticks;
  1536. ticks=get_ticks_raw();
  1537. if (unlikely((ticks-tcp_reader_prev_ticks)<TCPCONN_TIMEOUT_MIN_RUN))
  1538. return;
  1539. tcp_reader_prev_ticks=ticks;
  1540. local_timer_run(&tcp_reader_ltimer, ticks);
  1541. }
  1542. void tcp_receive_loop(int unix_sock)
  1543. {
  1544. /* init */
  1545. tcpmain_sock=unix_sock; /* init com. socket */
  1546. if (init_io_wait(&io_w, get_max_open_fds(), tcp_poll_method)<0)
  1547. goto error;
  1548. tcp_reader_prev_ticks=get_ticks_raw();
  1549. if (init_local_timer(&tcp_reader_ltimer, get_ticks_raw())!=0)
  1550. goto error;
  1551. /* add the unix socket */
  1552. if (io_watch_add(&io_w, tcpmain_sock, POLLIN, F_TCPMAIN, 0)<0){
  1553. LM_CRIT("failed to add socket to the fd list\n");
  1554. goto error;
  1555. }
  1556. /* initialize the config framework */
  1557. if (cfg_child_init()) goto error;
  1558. /* main loop */
  1559. switch(io_w.poll_method){
  1560. case POLL_POLL:
  1561. while(1){
  1562. io_wait_loop_poll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  1563. tcp_reader_timer_run();
  1564. }
  1565. break;
  1566. #ifdef HAVE_SELECT
  1567. case POLL_SELECT:
  1568. while(1){
  1569. io_wait_loop_select(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  1570. tcp_reader_timer_run();
  1571. }
  1572. break;
  1573. #endif
  1574. #ifdef HAVE_SIGIO_RT
  1575. case POLL_SIGIO_RT:
  1576. while(1){
  1577. io_wait_loop_sigio_rt(&io_w, TCP_CHILD_SELECT_TIMEOUT);
  1578. tcp_reader_timer_run();
  1579. }
  1580. break;
  1581. #endif
  1582. #ifdef HAVE_EPOLL
  1583. case POLL_EPOLL_LT:
  1584. while(1){
  1585. io_wait_loop_epoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  1586. tcp_reader_timer_run();
  1587. }
  1588. break;
  1589. case POLL_EPOLL_ET:
  1590. while(1){
  1591. io_wait_loop_epoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 1);
  1592. tcp_reader_timer_run();
  1593. }
  1594. break;
  1595. #endif
  1596. #ifdef HAVE_KQUEUE
  1597. case POLL_KQUEUE:
  1598. while(1){
  1599. io_wait_loop_kqueue(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  1600. tcp_reader_timer_run();
  1601. }
  1602. break;
  1603. #endif
  1604. #ifdef HAVE_DEVPOLL
  1605. case POLL_DEVPOLL:
  1606. while(1){
  1607. io_wait_loop_devpoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
  1608. tcp_reader_timer_run();
  1609. }
  1610. break;
  1611. #endif
  1612. default:
  1613. LM_CRIT("no support for poll method %s (%d)\n",
  1614. poll_method_name(io_w.poll_method), io_w.poll_method);
  1615. goto error;
  1616. }
  1617. error:
  1618. destroy_io_wait(&io_w);
  1619. LM_CRIT("exiting...");
  1620. exit(-1);
  1621. }
  1622. int is_msg_complete(struct tcp_req* r)
  1623. {
  1624. if (TCP_REQ_HAS_CLEN(r)) {
  1625. r->state = H_STUN_FP;
  1626. return 0;
  1627. }
  1628. else {
  1629. /* STUN message is complete */
  1630. r->state = H_STUN_END;
  1631. r->flags |= F_TCP_REQ_COMPLETE |
  1632. F_TCP_REQ_HAS_CLEN; /* hack to avoid error check */
  1633. return 1;
  1634. }
  1635. }
  1636. #endif /* USE_TCP */