Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,1843 +0,0 @@
1
-/*
2
- * Copyright (C) 2001-2003 FhG Fokus
3
- *
4
- * This file is part of Kamailio, a free SIP server.
5
- *
6
- * Kamailio is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version
10
- *
11
- * Kamailio is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
- * GNU General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU General Public License
17
- * along with this program; if not, write to the Free Software
18
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
- */
20
-
21
-/** Kamailio core :: tcp readers processes, tcp read and pre-parse msg. functions.
22
- * @file tcp_read.c
23
- * @ingroup core
24
- * Module: @ref core
25
- */
26
-
27
-#ifdef USE_TCP
28
-
29
-#include <stdio.h>
30
-#include <errno.h>
31
-#include <string.h>
32
-
33
-
34
-#include <sys/time.h>
35
-#include <sys/types.h>
36
-#include <sys/select.h>
37
-#include <sys/socket.h>
38
-
39
-#include <unistd.h>
40
-#include <stdlib.h> /* for abort() */
41
-
42
-
43
-#include "dprint.h"
44
-#include "tcp_conn.h"
45
-#include "tcp_read.h"
46
-#include "tcp_stats.h"
47
-#include "tcp_ev.h"
48
-#include "pass_fd.h"
49
-#include "globals.h"
50
-#include "receive.h"
51
-#include "timer.h"
52
-#include "local_timer.h"
53
-#include "ut.h"
54
-#include "trim.h"
55
-#include "pt.h"
56
-#include "cfg/cfg_struct.h"
57
-#ifdef CORE_TLS
58
-#include "tls/tls_server.h"
59
-#else
60
-#include "tls_hooks.h"
61
-#endif /* CORE_TLS */
62
-#ifdef USE_DST_BLACKLIST
63
-#include "dst_blacklist.h"
64
-#endif /* USE_DST_BLACKLIST */
65
-
66
-#define HANDLE_IO_INLINE
67
-#include "io_wait.h"
68
-#include <fcntl.h> /* must be included after io_wait.h if SIGIO_RT is used */
69
-#include "tsend.h"
70
-#include "forward.h"
71
-#include "events.h"
72
-#include "stun.h"
73
-
74
-#ifdef READ_HTTP11
75
-#define HTTP11CONTINUE	"HTTP/1.1 100 Continue\r\nContent-Length: 0\r\n\r\n"
76
-#define HTTP11CONTINUE_LEN	(sizeof(HTTP11CONTINUE)-1)
77
-#endif
78
-
79
-#define TCPCONN_TIMEOUT_MIN_RUN  1 /* run the timers each new tick */
80
-
81
-/* types used in io_wait* */
82
-enum fd_types { F_NONE, F_TCPMAIN, F_TCPCONN };
83
-
84
-/* list of tcp connections handled by this process */
85
-static struct tcp_connection* tcp_conn_lst=0;
86
-static io_wait_h io_w; /* io_wait handler*/
87
-static int tcpmain_sock=-1;
88
-
89
-static struct local_timer tcp_reader_ltimer;
90
-static ticks_t tcp_reader_prev_ticks;
91
-
92
-int is_msg_complete(struct tcp_req* r);
93
-
94
-/**
95
- * control cloning of TCP receive buffer
96
- * - needed for operations working directly inside the buffer
97
- *   (like msg_apply_changes())
98
- */
99
-#define TCP_CLONE_RCVBUF
100
-static int tcp_clone_rcvbuf = 0;
101
-
102
-int tcp_set_clone_rcvbuf(int v)
103
-{
104
-	int r;
105
-	r = tcp_clone_rcvbuf;
106
-	tcp_clone_rcvbuf = v;
107
-	return r;
108
-}
109
-
110
-#ifdef READ_HTTP11
111
-static inline char *strfindcasestrz(str *haystack, char *needlez)
112
-{
113
-	int i,j;
114
-	str needle;
115
-
116
-	needle.s = needlez;
117
-	needle.len = strlen(needlez);
118
-	for(i=0;i<haystack->len-needle.len;i++) {
119
-		for(j=0;j<needle.len;j++) {
120
-			if ( !((haystack->s[i+j]==needle.s[j]) ||
121
-					( isalpha((int)haystack->s[i+j])
122
-						&& ((haystack->s[i+j])^(needle.s[j]))==0x20 )) )
123
-				break;
124
-		}
125
-		if (j==needle.len)
126
-			return haystack->s+i;
127
-	}
128
-	return 0;
129
-}
130
-
131
-int tcp_http11_continue(struct tcp_connection *c)
132
-{
133
-	struct dest_info dst;
134
-	char *p;
135
-	struct msg_start fline;
136
-	int ret;
137
-	str msg;
138
-
139
-	ret = 0;
140
-
141
-	msg.s = c->req.start;
142
-	msg.len = c->req.pos - c->req.start;
143
-#ifdef READ_MSRP
144
-	/* skip if MSRP message */
145
-	if(c->req.flags&F_TCP_REQ_MSRP_FRAME)
146
-		return 0;
147
-#endif
148
-	p = parse_first_line(msg.s, msg.len, &fline);
149
-	if(p==NULL)
150
-		return 0;
151
-
152
-	if(fline.type!=SIP_REQUEST)
153
-		return 0;
154
-
155
-	/* check if http request */
156
-	if(fline.u.request.version.len < HTTP_VERSION_LEN
157
-			|| strncasecmp(fline.u.request.version.s,
158
-				HTTP_VERSION, HTTP_VERSION_LEN))
159
-		return 0;
160
-
161
-	/* check for Expect header */
162
-	if(strfindcasestrz(&msg, "Expect: 100-continue")!=NULL)
163
-	{
164
-		init_dst_from_rcv(&dst, &c->rcv);
165
-		if (tcp_send(&dst, 0, HTTP11CONTINUE, HTTP11CONTINUE_LEN) < 0) {
166
-			LM_ERR("HTTP/1.1 continue failed\n");
167
-		}
168
-	}
169
-	/* check for Transfer-Encoding header */
170
-	if(strfindcasestrz(&msg, "Transfer-Encoding: chunked")!=NULL)
171
-	{
172
-		c->req.flags |= F_TCP_REQ_BCHUNKED;
173
-		ret = 1;
174
-	}
175
-	/* check for HTTP Via header
176
-	 * - HTTP Via format is different that SIP Via
177
-	 * - workaround: replace with Hia to be ignored by SIP parser
178
-	 */
179
-	if((p=strfindcasestrz(&msg, "\nVia:"))!=NULL)
180
-	{
181
-		p++;
182
-		*p = 'H';
183
-		LM_DBG("replaced HTTP Via with Hia [[\n%.*s]]\n", msg.len, msg.s);
184
-	}
185
-	return ret;
186
-}
187
-#endif /* HTTP11 */
188
-
189
-static int tcp_emit_closed_event(struct tcp_connection *con, enum tcp_closed_reason reason)
190
-{
191
-	int ret;
192
-	tcp_closed_event_info_t tev;
193
-
194
-	ret = 0;
195
-	LM_DBG("TCP closed event creation triggered\n");
196
-	if(likely(sr_event_enabled(SREV_TCP_CLOSED))) {
197
-		memset(&tev, 0, sizeof(tcp_closed_event_info_t));
198
-		tev.reason = reason;
199
-		tev.con = con;
200
-		ret = sr_event_exec(SREV_TCP_CLOSED, (void*)(&tev));
201
-	} else {
202
-		LM_DBG("no callback registering for handling TCP closed event - dropping!\n");
203
-	}
204
-	return ret;
205
-}
206
-
207
-
208
-/** reads data from an existing tcp connection.
209
- * Side-effects: blacklisting, sets connection state to S_CONN_OK, tcp stats.
210
- * @param fd - connection file descriptor
211
- * @param c - tcp connection structure. c->state might be changed and
212
- *             receive info might be used for blacklisting.
213
- * @param buf - buffer where the received data will be stored.
214
- * @param b_size - buffer size.
215
- * @param flags - value/result - used to signal a seen or "forced" EOF on the
216
- *     connection (when it is known that no more data will come after the 
217
- *     current socket buffer is emptied )=> return/signal EOF on the first
218
- *     short read (=> don't use it on POLLPRI, as OOB data will cause short
219
- *     reads even if there are still remaining bytes in the socket buffer)
220
- *     input: RD_CONN_FORCE_EOF  - force EOF after the first successful read
221
- *                                 (bytes_read >=0 )
222
- *     output: RD_CONN_SHORT_READ - if the read exhausted all the bytes
223
- *                                  in the socket read buffer.
224
- *             RD_CONN_EOF - if EOF detected (0 bytes read) or forced via
225
- *                           RD_CONN_FORCE_EOF.
226
- *             RD_CONN_REPEAT_READ - the read should be repeated immediately
227
- *                                   (used only by the tls code for now).
228
- *     Note: RD_CONN_SHORT_READ & RD_CONN_EOF _are_ not cleared internally,
229
- *           so one should clear them before calling this function.
230
- * @return number of bytes read, 0 on EOF or -1 on error,
231
- * on EOF it also sets c->state to S_CONN_EOF.
232
- * (to distinguish from reads that would block which could return 0)
233
- * RD_CONN_SHORT_READ is also set in *flags for short reads.
234
- * EOF checking should be done by checking the RD_CONN_EOF flag.
235
- */
236
-int tcp_read_data(int fd, struct tcp_connection *c,
237
-					char* buf, int b_size, int* flags)
238
-{
239
-	int bytes_read;
240
-	
241
-again:
242
-	bytes_read=read(fd, buf, b_size);
243
-	
244
-	if (likely(bytes_read!=b_size)){
245
-		if(unlikely(bytes_read==-1)){
246
-			if (errno == EWOULDBLOCK || errno == EAGAIN){
247
-				bytes_read=0; /* nothing has been read */
248
-			}else if (errno == EINTR) goto again;
249
-			else{
250
-				if (unlikely(c->state==S_CONN_CONNECT)){
251
-					switch(errno){
252
-						case ECONNRESET:
253
-#ifdef USE_DST_BLACKLIST
254
-							dst_blacklist_su(BLST_ERR_CONNECT, c->rcv.proto,
255
-												&c->rcv.src_su,
256
-												&c->send_flags, 0);
257
-#endif /* USE_DST_BLACKLIST */
258
-							TCP_EV_CONNECT_RST(errno, TCP_LADDR(c),
259
-									TCP_LPORT(c), TCP_PSU(c), TCP_PROTO(c));
260
-							break;
261
-						case ETIMEDOUT:
262
-#ifdef USE_DST_BLACKLIST
263
-							dst_blacklist_su(BLST_ERR_CONNECT, c->rcv.proto,
264
-												&c->rcv.src_su,
265
-												&c->send_flags, 0);
266
-#endif /* USE_DST_BLACKLIST */
267
-							TCP_EV_CONNECT_TIMEOUT(errno, TCP_LADDR(c),
268
-									TCP_LPORT(c), TCP_PSU(c), TCP_PROTO(c));
269
-							break;
270
-						default:
271
-							TCP_EV_CONNECT_ERR(errno, TCP_LADDR(c),
272
-									TCP_LPORT(c), TCP_PSU(c), TCP_PROTO(c));
273
-					}
274
-					TCP_STATS_CONNECT_FAILED();
275
-				}else{
276
-						switch(errno){
277
-							case ECONNRESET:
278
-								TCP_STATS_CON_RESET();
279
-							case ETIMEDOUT:
280
-#ifdef USE_DST_BLACKLIST
281
-								dst_blacklist_su(BLST_ERR_SEND, c->rcv.proto,
282
-													&c->rcv.src_su,
283
-													&c->send_flags, 0);
284
-#endif /* USE_DST_BLACKLIST */
285
-								break;
286
-						}
287
-				}
288
-				LOG(cfg_get(core, core_cfg, corelog),
289
-						"error reading: %s (%d) ([%s]:%u ->",
290
-						strerror(errno), errno,
291
-						ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
292
-				LOG(cfg_get(core, core_cfg, corelog),"-> [%s]:%u)\n", ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
293
-				if (errno == ETIMEDOUT) {
294
-					tcp_emit_closed_event(c, TCP_CLOSED_TIMEOUT);
295
-				} else if (errno == ECONNRESET) {
296
-					tcp_emit_closed_event(c, TCP_CLOSED_RESET);
297
-				}
298
-				return -1;
299
-			}
300
-		}else if (unlikely((bytes_read==0) || 
301
-					(*flags & RD_CONN_FORCE_EOF))){
302
-			c->state=S_CONN_EOF;
303
-			*flags|=RD_CONN_EOF;
304
-			tcp_emit_closed_event(c, TCP_CLOSED_EOF);
305
-			LM_DBG("EOF on %p, FD %d ([%s]:%u ->", c, fd, ip_addr2a(&c->rcv.src_ip), c->rcv.src_port);
306
-			LM_DBG("-> [%s]:%u)\n", ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
307
-		}else{
308
-			if (unlikely(c->state==S_CONN_CONNECT || c->state==S_CONN_ACCEPT)){
309
-				TCP_STATS_ESTABLISHED(c->state);
310
-				c->state=S_CONN_OK;
311
-			}
312
-		}
313
-		/* short read */
314
-		*flags|=RD_CONN_SHORT_READ;
315
-	}else{ /* else normal full read */
316
-		if (unlikely(c->state==S_CONN_CONNECT || c->state==S_CONN_ACCEPT)){
317
-			TCP_STATS_ESTABLISHED(c->state);
318
-			c->state=S_CONN_OK;
319
-		}
320
-	}
321
-	return bytes_read;
322
-}
323
-
324
-
325
-
326
-/* reads next available bytes
327
- *   c- tcp connection used for reading, tcp_read changes also c->state on
328
- *      EOF and c->req.error on read error
329
- *   * flags - value/result - used to signal a seen or "forced" EOF on the 
330
- *     connection (when it is known that no more data will come after the 
331
- *     current socket buffer is emptied )=> return/signal EOF on the first 
332
- *     short read (=> don't use it on POLLPRI, as OOB data will cause short
333
- *      reads even if there are still remaining bytes in the socket buffer)
334
- * return number of bytes read, 0 on EOF or -1 on error,
335
- * on EOF it also sets c->state to S_CONN_EOF.
336
- * (to distinguish from reads that would block which could return 0)
337
- * RD_CONN_SHORT_READ is also set in *flags for short reads.
338
- * sets also r->error */
339
-int tcp_read(struct tcp_connection *c, int* flags)
340
-{
341
-	int bytes_free, bytes_read;
342
-	struct tcp_req *r;
343
-	int fd;
344
-
345
-	r=&c->req;
346
-	fd=c->fd;
347
-	bytes_free=r->b_size- (int)(r->pos - r->buf);
348
-	
349
-	if (unlikely(bytes_free==0)){
350
-		LM_ERR("buffer overrun, dropping ([%s]:%u -> [%s]:%u)\n",
351
-				ip_addr2a(&c->rcv.src_ip), c->rcv.src_port,
352
-				ip_addr2a(&c->rcv.dst_ip), c->rcv.dst_port);
353
-		r->error=TCP_REQ_OVERRUN;
354
-		return -1;
355
-	}
356
-	bytes_read = tcp_read_data(fd, c, r->pos, bytes_free, flags);
357
-	if (unlikely(bytes_read < 0)){
358
-		r->error=TCP_READ_ERROR;
359
-		return -1;
360
-	}
361
-#ifdef EXTRA_DEBUG
362
-	LM_DBG("read %d bytes:\n%.*s\n", bytes_read, bytes_read, r->pos);
363
-#endif
364
-	r->pos+=bytes_read;
365
-	return bytes_read;
366
-}
367
-
368
-
369
-
370
-/* reads all headers (until double crlf), & parses the content-length header
371
- * (WARNING: inefficient, tries to reuse receive_msg but will go through
372
- * the headers twice [once here looking for Content-Length and for the end
373
- * of the headers and once in receive_msg]; a more speed efficient version will
374
- * result in either major code duplication or major changes to the receive code)
375
- * returns number of bytes read & sets r->state & r->body
376
- * when either r->body!=0 or r->state==H_BODY =>
377
- * all headers have been read. It should be called in a while loop.
378
- * returns < 0 if error or 0 if EOF */
379
-int tcp_read_headers(struct tcp_connection *c, int* read_flags)
380
-{
381
-	int bytes, remaining;
382
-	char *p;
383
-	struct tcp_req* r;
384
-	unsigned int mc;   /* magic cookie */
385
-	unsigned short body_len;
386
-
387
-#ifdef READ_MSRP
388
-	char *mfline;
389
-	str mtransid;
390
-#endif
391
-
392
-	#define crlf_default_skip_case \
393
-					case '\n': \
394
-						r->state=H_LF; \
395
-						break; \
396
-					default: \
397
-						r->state=H_SKIP
398
-	
399
-	#define content_len_beg_case \
400
-					case ' ': \
401
-					case '\t': \
402
-						if (!TCP_REQ_HAS_CLEN(r)) r->state=H_STARTWS; \
403
-						else r->state=H_SKIP; \
404
-							/* not interested if we already found one */ \
405
-						break; \
406
-					case 'C': \
407
-					case 'c': \
408
-						if(!TCP_REQ_HAS_CLEN(r)) r->state=H_CONT_LEN1; \
409
-						else r->state=H_SKIP; \
410
-						break; \
411
-					case 'l': \
412
-					case 'L': \
413
-						/* short form for Content-Length */ \
414
-						if (!TCP_REQ_HAS_CLEN(r)) r->state=H_L_COLON; \
415
-						else r->state=H_SKIP; \
416
-						break
417
-						
418
-	#define change_state(upper, lower, newstate)\
419
-					switch(*p){ \
420
-						case upper: \
421
-						case lower: \
422
-							r->state=(newstate); break; \
423
-						crlf_default_skip_case; \
424
-					}
425
-	
426
-	#define change_state_case(state0, upper, lower, newstate)\
427
-					case state0: \
428
-							  change_state(upper, lower, newstate); \
429
-							  p++; \
430
-							  break
431
-
432
-
433
-	r=&c->req;
434
-	/* if we still have some unparsed part, parse it first, don't do the read*/
435
-	if (unlikely(r->parsed<r->pos)){
436
-		bytes=0;
437
-	}else{
438
-#ifdef USE_TLS
439
-		if (unlikely(c->type==PROTO_TLS))
440
-			bytes=tls_read(c, read_flags);
441
-		else
442
-#endif
443
-			bytes=tcp_read(c, read_flags);
444
-		if (bytes<=0) return bytes;
445
-	}
446
-	p=r->parsed;
447
-
448
-	while(p<r->pos && r->error==TCP_REQ_OK){
449
-		switch((unsigned char)r->state){
450
-			case H_BODY: /* read the body*/
451
-				remaining=r->pos-p;
452
-				if (remaining>r->bytes_to_go) remaining=r->bytes_to_go;
453
-				r->bytes_to_go-=remaining;
454
-				p+=remaining;
455
-				if (r->bytes_to_go==0){
456
-					r->flags|=F_TCP_REQ_COMPLETE;
457
-					goto skip;
458
-				}
459
-				break;
460
-				
461
-			case H_SKIP:
462
-				/* find lf, we are in this state if we are not interested
463
-				 * in anything till end of line*/
464
-				p=q_memchr(p, '\n', r->pos-p);
465
-				if (p){
466
-#ifdef READ_MSRP
467
-					/* catch if it is MSRP or not with first '\n' */
468
-					if(!((r->flags&F_TCP_REQ_MSRP_NO)
469
-								|| (r->flags&F_TCP_REQ_MSRP_FRAME))) {
470
-						if((r->pos - r->start)>5
471
-									&& strncmp(r->start, "MSRP ", 5)==0)
472
-						{
473
-							r->flags |= F_TCP_REQ_MSRP_FRAME;
474
-						} else {
475
-							r->flags |= F_TCP_REQ_MSRP_NO;
476
-						}
477
-					}
478
-#endif
479
-					p++;
480
-					r->state=H_LF;
481
-				}else{
482
-					p=r->pos;
483
-				}
484
-				break;
485
-				
486
-			case H_LF:
487
-				/* terminate on LF CR LF or LF LF */
488
-				switch (*p){
489
-					case '\r':
490
-						r->state=H_LFCR;
491
-						break;
492
-					case '\n':
493
-						/* found LF LF */
494
-						r->state=H_BODY;
495
-#ifdef READ_HTTP11
496
-						if (cfg_get(tcp, tcp_cfg, accept_no_cl)!=0)
497
-							tcp_http11_continue(c);
498
-#endif
499
-						if (TCP_REQ_HAS_CLEN(r)){
500
-							r->body=p+1;
501
-							r->bytes_to_go=r->content_len;
502
-							if (r->bytes_to_go==0){
503
-								r->flags|=F_TCP_REQ_COMPLETE;
504
-								p++;
505
-								goto skip;
506
-							}
507
-						}else{
508
-							if(cfg_get(tcp, tcp_cfg, accept_no_cl)!=0) {
509
-#ifdef READ_MSRP
510
-								/* if MSRP message */
511
-								if(c->req.flags&F_TCP_REQ_MSRP_FRAME)
512
-								{
513
-									r->body=p+1;
514
-									/* at least 3 bytes: 0\r\n */
515
-									r->bytes_to_go=3;
516
-									p++;
517
-									r->content_len = 0;
518
-									r->state=H_MSRP_BODY;
519
-									break;
520
-								}
521
-#endif
522
-
523
-#ifdef READ_HTTP11
524
-								if(TCP_REQ_BCHUNKED(r)) {
525
-									r->body=p+1;
526
-									/* at least 3 bytes: 0\r\n */
527
-									r->bytes_to_go=3;
528
-									p++;
529
-									r->content_len = 0;
530
-									r->state=H_HTTP11_CHUNK_START;
531
-									break;
532
-								}
533
-#endif
534
-								r->body=p+1;
535
-								r->bytes_to_go=0;
536
-								r->flags|=F_TCP_REQ_COMPLETE;
537
-								p++;
538
-								goto skip;
539
-							} else {
540
-								LM_DBG("no clen, p=%X\n", *p);
541
-								r->error=TCP_REQ_BAD_LEN;
542
-							}
543
-						}
544
-						break;
545
-					case '-':
546
-						r->state=H_SKIP;
547
-#ifdef READ_MSRP
548
-						/* catch end of MSRP frame without body
549
-						 *     '-------sessid$\r\n'
550
-						 * follows headers wihtout extra CRLF */
551
-						if(r->flags&F_TCP_REQ_MSRP_FRAME) {
552
-							p--;
553
-							r->state=H_MSRP_BODY_END;
554
-						}
555
-#endif
556
-						break;
557
-					content_len_beg_case;
558
-					default: 
559
-						r->state=H_SKIP;
560
-				}
561
-				p++;
562
-				break;
563
-			case H_LFCR:
564
-				if (*p=='\n'){
565
-					/* found LF CR LF */
566
-					r->state=H_BODY;
567
-#ifdef READ_HTTP11
568
-					if (cfg_get(tcp, tcp_cfg, accept_no_cl)!=0)
569
-						tcp_http11_continue(c);
570
-#endif
571
-					if (TCP_REQ_HAS_CLEN(r)){
572
-						r->body=p+1;
573
-						r->bytes_to_go=r->content_len;
574
-						if (r->bytes_to_go==0){
575
-							r->flags|=F_TCP_REQ_COMPLETE;
576
-							p++;
577
-							goto skip;
578
-						}
579
-					}else{
580
-						if (cfg_get(tcp, tcp_cfg, accept_no_cl)!=0) {
581
-#ifdef READ_MSRP
582
-							/* if MSRP message */
583
-							if(c->req.flags&F_TCP_REQ_MSRP_FRAME)
584
-							{
585
-								r->body=p+1;
586
-								/* at least 3 bytes: 0\r\n */
587
-								r->bytes_to_go=3;
588
-								p++;
589
-								r->content_len = 0;
590
-								r->state=H_MSRP_BODY;
591
-								break;
592
-							}
593
-#endif
594
-
595
-#ifdef READ_HTTP11
596
-							if(TCP_REQ_BCHUNKED(r)) {
597
-								r->body=p+1;
598
-								/* at least 3 bytes: 0\r\n */
599
-								r->bytes_to_go=3;
600
-								p++;
601
-								r->content_len = 0;
602
-								r->state=H_HTTP11_CHUNK_START;
603
-								break;
604
-							}
605
-#endif
606
-							r->body=p+1;
607
-							r->bytes_to_go=0;
608
-							r->flags|=F_TCP_REQ_COMPLETE;
609
-							p++;
610
-							goto skip;
611
-						} else {
612
-							LM_DBG("no clen, p=%X\n", *p);
613
-							r->error=TCP_REQ_BAD_LEN;
614
-						}
615
-					}
616
-				}else r->state=H_SKIP;
617
-				p++;
618
-				break;
619
-				
620
-			case H_STARTWS:
621
-				switch (*p){
622
-					content_len_beg_case;
623
-					crlf_default_skip_case;
624
-				}
625
-				p++;
626
-				break;
627
-			case H_SKIP_EMPTY:
628
-				switch (*p){
629
-					case '\n':
630
-						break;
631
-					case '\r':
632
-						if (cfg_get(tcp, tcp_cfg, crlf_ping)) {
633
-							r->state=H_SKIP_EMPTY_CR_FOUND;
634
-							r->start=p;
635
-						}
636
-						break;
637
-					case ' ':
638
-					case '\t':
639
-						/* skip empty lines */
640
-						break;
641
-					case 'C': 
642
-					case 'c': 
643
-						r->state=H_CONT_LEN1; 
644
-						r->start=p;
645
-						break;
646
-					case 'l':
647
-					case 'L':
648
-						/* short form for Content-Length */
649
-						r->state=H_L_COLON;
650
-						r->start=p;
651
-						break;
652
-					default:
653
-						/* stun test */						
654
-						if (unlikely(sr_event_enabled(SREV_STUN_IN)) && (unsigned char)*p == 0x00) {
655
-							r->state=H_STUN_MSG;
656
-						/* body will used as pointer to the last used byte */
657
-							r->body=p;
658
-							r->content_len = 0;
659
-							LM_DBG("stun msg detected\n");
660
-						} else {
661
-							r->state=H_SKIP;
662
-						}
663
-						r->start=p;
664
-				};
665
-				p++;
666
-				break;
667
-
668
-			case H_SKIP_EMPTY_CR_FOUND:
669
-				if (*p=='\n'){
670
-					r->state=H_SKIP_EMPTY_CRLF_FOUND;
671
-					p++;
672
-				}else{
673
-					r->state=H_SKIP_EMPTY;
674
-				}
675
-				break;
676
-
677
-			case H_SKIP_EMPTY_CRLF_FOUND:
678
-				if (*p=='\r'){
679
-					r->state = H_SKIP_EMPTY_CRLFCR_FOUND;
680
-					p++;
681
-				}else{
682
-					r->state = H_SKIP_EMPTY;
683
-				}
684
-				break;
685
-
686
-			case H_SKIP_EMPTY_CRLFCR_FOUND:
687
-				if (*p=='\n'){
688
-					r->state = H_PING_CRLF;
689
-					r->flags |= F_TCP_REQ_HAS_CLEN |
690
-							F_TCP_REQ_COMPLETE; /* hack to avoid error check */
691
-					p++;
692
-					goto skip;
693
-				}else{
694
-					r->state = H_SKIP_EMPTY;
695
-				}
696
-				break;
697
-
698
-			case H_STUN_MSG:
699
-				if ((r->pos - r->body) >= sizeof(struct stun_hdr)) {
700
-					/* copy second short from buffer where should be body 
701
-					 * length 
702
-					 */
703
-					memcpy(&body_len, &r->start[sizeof(unsigned short)], 
704
-						sizeof(unsigned short));
705
-					
706
-					body_len = ntohs(body_len);
707
-					
708
-					/* check if there is valid magic cookie */
709
-					memcpy(&mc, &r->start[sizeof(unsigned int)], 
710
-						sizeof(unsigned int));
711
-					mc = ntohl(mc);
712
-					/* using has_content_len as a flag if there should be
713
-					 * fingerprint or no
714
-					 */
715
-					r->flags |= (mc == MAGIC_COOKIE) ? F_TCP_REQ_HAS_CLEN : 0;
716
-					
717
-					r->body += sizeof(struct stun_hdr);
718
-					p = r->body; 
719
-					
720
-					if (body_len > 0) {
721
-						r->state = H_STUN_READ_BODY;
722
-					}
723
-					else {
724
-						if (is_msg_complete(r) != 0) {
725
-							goto skip;
726
-						}
727
-						else {
728
-							/* set content_len to length of fingerprint */
729
-							body_len = sizeof(struct stun_attr) + 20;
730
-							/* 20 is SHA_DIGEST_LENGTH from openssl/sha.h */
731
-						}
732
-					}
733
-					r->content_len=body_len;
734
-				}
735
-				else {
736
-					p = r->pos; 
737
-				}
738
-				break;
739
-				
740
-			case H_STUN_READ_BODY:
741
-				/* check if the whole body was read */
742
-				body_len=r->content_len;
743
-				if ((r->pos - r->body) >= body_len) {
744
-					r->body += body_len;
745
-					p = r->body;
746
-					if (is_msg_complete(r) != 0) {
747
-						r->content_len=0;
748
-						goto skip;
749
-					}
750
-					else {
751
-						/* set content_len to length of fingerprint */
752
-						body_len = sizeof(struct stun_attr) + 20;
753
-						/* 20 is SHA_DIGEST_LENGTH from openssl/sha.h */
754
-						r->content_len=body_len;
755
-					}
756
-				}
757
-				else {
758
-					p = r->pos;
759
-				}
760
-				break;
761
-				
762
-			case H_STUN_FP:
763
-				/* content_len contains length of fingerprint in this place! */
764
-				body_len=r->content_len;
765
-				if ((r->pos - r->body) >= body_len) {
766
-					r->body += body_len;
767
-					p = r->body;
768
-					r->state = H_STUN_END;
769
-					r->flags |= F_TCP_REQ_COMPLETE |
770
-						F_TCP_REQ_HAS_CLEN; /* hack to avoid error check */
771
-					r->content_len=0;
772
-					goto skip;
773
-				}
774
-				else {
775
-					p = r->pos;
776
-				}
777
-				break;
778
-
779
-			change_state_case(H_CONT_LEN1,  'O', 'o', H_CONT_LEN2);
780
-			change_state_case(H_CONT_LEN2,  'N', 'n', H_CONT_LEN3);
781
-			change_state_case(H_CONT_LEN3,  'T', 't', H_CONT_LEN4);
782
-			change_state_case(H_CONT_LEN4,  'E', 'e', H_CONT_LEN5);
783
-			change_state_case(H_CONT_LEN5,  'N', 'n', H_CONT_LEN6);
784
-			change_state_case(H_CONT_LEN6,  'T', 't', H_CONT_LEN7);
785
-			change_state_case(H_CONT_LEN7,  '-', '_', H_CONT_LEN8);
786
-			change_state_case(H_CONT_LEN8,  'L', 'l', H_CONT_LEN9);
787
-			change_state_case(H_CONT_LEN9,  'E', 'e', H_CONT_LEN10);
788
-			change_state_case(H_CONT_LEN10, 'N', 'n', H_CONT_LEN11);
789
-			change_state_case(H_CONT_LEN11, 'G', 'g', H_CONT_LEN12);
790
-			change_state_case(H_CONT_LEN12, 'T', 't', H_CONT_LEN13);
791
-			change_state_case(H_CONT_LEN13, 'H', 'h', H_L_COLON);
792
-
793
-			case H_L_COLON:
794
-				switch(*p){
795
-					case ' ':
796
-					case '\t':
797
-						break; /* skip space */
798
-					case ':':
799
-						r->state=H_CONT_LEN_BODY;
800
-						break;
801
-					crlf_default_skip_case;
802
-				};
803
-				p++;
804
-				break;
805
-
806
-			case  H_CONT_LEN_BODY:
807
-				switch(*p){
808
-					case ' ':
809
-					case '\t':
810
-						break; /* eat space */
811
-					case '0':
812
-					case '1':
813
-					case '2':
814
-					case '3':
815
-					case '4':
816
-					case '5':
817
-					case '6':
818
-					case '7':
819
-					case '8':
820
-					case '9':
821
-						r->state=H_CONT_LEN_BODY_PARSE;
822
-						r->content_len=(*p-'0');
823
-						break;
824
-					/*FIXME: content length on different lines ! */
825
-					crlf_default_skip_case;
826
-				}
827
-				p++;
828
-				break;
829
-
830
-			case H_CONT_LEN_BODY_PARSE:
831
-				switch(*p){
832
-					case '0':
833
-					case '1':
834
-					case '2':
835
-					case '3':
836
-					case '4':
837
-					case '5':
838
-					case '6':
839
-					case '7':
840
-					case '8':
841
-					case '9':
842
-						r->content_len=r->content_len*10+(*p-'0');
843
-						break;
844
-					case '\r':
845
-					case ' ':
846
-					case '\t': /* FIXME: check if line contains only WS */
847
-						if(r->content_len<0) {
848
-							LM_ERR("bad Content-Length header value %d in"
849
-									" state %d\n", r->content_len, r->state);
850
-							r->content_len=0;
851
-							r->error=TCP_REQ_BAD_LEN;
852
-							r->state=H_SKIP; /* skip now */
853
-						}
854
-						r->state=H_SKIP;
855
-						r->flags|=F_TCP_REQ_HAS_CLEN;
856
-						break;
857
-					case '\n':
858
-						/* end of line, parse successful */
859
-						if(r->content_len<0) {
860
-							LM_ERR("bad Content-Length header value %d in"
861
-									" state %d\n", r->content_len, r->state);
862
-							r->content_len=0;
863
-							r->error=TCP_REQ_BAD_LEN;
864
-							r->state=H_SKIP; /* skip now */
865
-						}
866
-						r->state=H_LF;
867
-						r->flags|=F_TCP_REQ_HAS_CLEN;
868
-						break;
869
-					default:
870
-						LM_ERR("bad Content-Length header value, unexpected "
871
-								"char %c in state %d\n", *p, r->state);
872
-						r->state=H_SKIP; /* try to find another?*/
873
-				}
874
-				p++;
875
-				break;
876
-			
877
-#ifdef READ_HTTP11
878
-			case H_HTTP11_CHUNK_START: /* start a new body chunk: SIZE\r\nBODY\r\n */
879
-				r->chunk_size = 0;
880
-				r->state = H_HTTP11_CHUNK_SIZE;
881
-				break;
882
-			case H_HTTP11_CHUNK_BODY: /* content of chunnk */
883
-				remaining=r->pos-p;
884
-				if (remaining>r->bytes_to_go) remaining=r->bytes_to_go;
885
-				r->bytes_to_go-=remaining;
886
-				p+=remaining;
887
-				if (r->bytes_to_go==0){
888
-					r->state = H_HTTP11_CHUNK_END;
889
-					/* shift back body content */
890
-					if(r->chunk_size>0 && p-r->chunk_size>r->body) {
891
-						memmove(r->body + r->content_len, p - r->chunk_size,
892
-								r->chunk_size);
893
-						r->content_len += r->chunk_size;
894
-					}
895
-					goto skip;
896
-				}
897
-				break;
898
-
899
-			case H_HTTP11_CHUNK_END:
900
-				switch(*p){
901
-					case '\r':
902
-					case ' ':
903
-					case '\t': /* skip */