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