tcp_read.c 44 KB

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