/
tcp_common.h
507 lines (461 loc) · 12.9 KB
/
tcp_common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* Copyright (C) 2015 - OpenSIPS Foundation
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*
* History:
* -------
* 2015-02-16 split from proto_tcp.c (razvanc)
*/
#ifndef _NET_tcp_common_h
#define _NET_tcp_common_h
#include "../../pt.h"
#include "../../receive.h"
#include "tcp_common_defs.h"
/*! \brief
* reads all headers (until double crlf), & parses the content-length header
*
* \note (WARNING: inefficient, tries to reuse receive_msg but will go through
* the headers twice [once here looking for Content-Length and for the end
* of the headers and once in receive_msg]; a more speed efficient version will
* result in either major code duplication or major changes to the receive code)
*
* \return nothing and sets r->state & r->body
* when either r->body!=0 or r->state==H_BODY =>
* all headers have been read. It should be called in a while loop */
inline static void tcp_parse_headers(struct tcp_req *r,
int _crlf_pingpong, int _crlf_drop)
{
unsigned int remaining;
char *p;
#define crlf_default_skip_case \
case '\n': \
r->state=H_LF; \
break; \
default: \
r->state=H_SKIP
#define content_len_beg_case \
case ' ': \
case '\t': \
if (!r->has_content_len) r->state=H_STARTWS; \
else r->state=H_SKIP; \
/* not interested if we already found one */ \
break; \
case 'C': \
case 'c': \
if(!r->has_content_len) r->state=H_CONT_LEN1; \
else r->state=H_SKIP; \
break; \
case 'l': \
case 'L': \
/* short form for Content-Length */ \
if (!r->has_content_len) r->state=H_L_COLON; \
else r->state=H_SKIP; \
break
#define change_state(upper, lower, newstate)\
switch(*p){ \
case upper: \
case lower: \
r->state=(newstate); break; \
crlf_default_skip_case; \
}
#define change_state_case(state0, upper, lower, newstate)\
case state0: \
change_state(upper, lower, newstate); \
p++; \
break
p=r->parsed;
while(p<r->pos && r->error==TCP_REQ_OK){
switch((unsigned char)r->state){
case H_BODY: /* read the body*/
remaining=r->pos-p;
if (remaining>r->bytes_to_go) remaining=r->bytes_to_go;
r->bytes_to_go-=remaining;
p+=remaining;
if (r->bytes_to_go==0){
r->complete=1;
goto skip;
}
break;
case H_SKIP:
/* find lf, we are in this state if we are not interested
* in anything till end of line*/
p=q_memchr(p, '\n', r->pos-p);
if (p){
p++;
r->state=H_LF;
}else{
p=r->pos;
}
break;
case H_LF:
/* terminate on LF CR LF or LF LF */
switch (*p){
case '\r':
r->state=H_LFCR;
break;
case '\n':
/* found LF LF */
r->state=H_BODY;
if (r->has_content_len){
r->body=p+1;
r->bytes_to_go=r->content_len;
if (r->bytes_to_go==0){
r->complete=1;
p++;
goto skip;
}
}else{
LM_DBG("no clen, p=%X\n", *p);
r->error=TCP_REQ_BAD_LEN;
}
break;
content_len_beg_case;
default:
r->state=H_SKIP;
}
p++;
break;
case H_LFCR:
if (*p=='\n'){
/* found LF CR LF */
r->state=H_BODY;
if (r->has_content_len){
r->body=p+1;
r->bytes_to_go=r->content_len;
if (r->bytes_to_go==0){
r->complete=1;
p++;
goto skip;
}
}else{
LM_DBG("no clen, p=%X\n", *p);
r->error=TCP_REQ_BAD_LEN;
}
}else r->state=H_SKIP;
p++;
break;
case H_STARTWS:
switch (*p){
content_len_beg_case;
crlf_default_skip_case;
}
p++;
break;
case H_SKIP_EMPTY:
switch (*p){
case '\n':
break;
case '\r':
if (_crlf_pingpong) {
r->state=H_SKIP_EMPTY_CR_FOUND;
r->start=p;
}
break;
case ' ':
case '\t':
/* skip empty lines */
break;
case 'C':
case 'c':
r->state=H_CONT_LEN1;
r->start=p;
break;
case 'l':
case 'L':
/* short form for Content-Length */
r->state=H_L_COLON;
r->start=p;
break;
default:
r->state=H_SKIP;
r->start=p;
};
p++;
break;
case H_SKIP_EMPTY_CR_FOUND:
if (*p=='\n'){
r->state=H_SKIP_EMPTY_CRLF_FOUND;
p++;
}else{
r->state=H_SKIP_EMPTY;
}
break;
case H_SKIP_EMPTY_CRLF_FOUND:
if (*p=='\r'){
r->state = H_SKIP_EMPTY_CRLFCR_FOUND;
p++;
}else{
r->state = H_SKIP_EMPTY;
}
break;
case H_SKIP_EMPTY_CRLFCR_FOUND:
if (*p=='\n'){
r->state = H_PING_CRLFCRLF;
r->complete = 1;
r->has_content_len = 1; /* hack to avoid error check */
p++;
goto skip;
}else{
r->state = H_SKIP_EMPTY;
}
break;
change_state_case(H_CONT_LEN1, 'O', 'o', H_CONT_LEN2);
change_state_case(H_CONT_LEN2, 'N', 'n', H_CONT_LEN3);
change_state_case(H_CONT_LEN3, 'T', 't', H_CONT_LEN4);
change_state_case(H_CONT_LEN4, 'E', 'e', H_CONT_LEN5);
change_state_case(H_CONT_LEN5, 'N', 'n', H_CONT_LEN6);
change_state_case(H_CONT_LEN6, 'T', 't', H_CONT_LEN7);
change_state_case(H_CONT_LEN7, '-', '_', H_CONT_LEN8);
change_state_case(H_CONT_LEN8, 'L', 'l', H_CONT_LEN9);
change_state_case(H_CONT_LEN9, 'E', 'e', H_CONT_LEN10);
change_state_case(H_CONT_LEN10, 'N', 'n', H_CONT_LEN11);
change_state_case(H_CONT_LEN11, 'G', 'g', H_CONT_LEN12);
change_state_case(H_CONT_LEN12, 'T', 't', H_CONT_LEN13);
change_state_case(H_CONT_LEN13, 'H', 'h', H_L_COLON);
case H_L_COLON:
switch(*p){
case ' ':
case '\t':
break; /* skip space */
case ':':
r->state=H_CONT_LEN_BODY;
break;
crlf_default_skip_case;
};
p++;
break;
case H_CONT_LEN_BODY:
switch(*p){
case ' ':
case '\t':
break; /* eat space */
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
r->state=H_CONT_LEN_BODY_PARSE;
r->content_len=(*p-'0');
break;
/*FIXME: content length on different lines ! */
crlf_default_skip_case;
}
p++;
break;
case H_CONT_LEN_BODY_PARSE:
switch(*p){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
r->content_len=r->content_len*10+(*p-'0');
break;
case '\r':
case ' ':
case '\t': /* FIXME: check if line contains only WS */
r->state=H_SKIP;
r->has_content_len=1;
break;
case '\n':
/* end of line, parse successful */
r->state=H_LF;
r->has_content_len=1;
break;
default:
LM_ERR("bad Content-Length header value, unexpected "
"char %c in state %d\n", *p, r->state);
r->state=H_SKIP; /* try to find another?*/
}
p++;
break;
default:
LM_CRIT("unexpected state %d\n", r->state);
abort();
}
}
if (r->state == H_SKIP_EMPTY_CRLF_FOUND && _crlf_drop) {
r->state = H_SKIP_EMPTY;
r->complete = 1;
r->has_content_len = 1; /* hack to avoid error check */
}
skip:
r->parsed=p;
return;
}
static inline int tcp_handle_req(struct tcp_req *req,
struct tcp_connection *con, int _max_msg_chunks)
{
struct receive_info local_rcv;
char *msg_buf;
int msg_len;
long size;
char c;
if (req->complete){
#ifdef EXTRA_DEBUG
LM_DBG("end of header part\n");
LM_DBG("- received from: port %d\n", con->rcv.src_port);
print_ip("- received from: ip ", &con->rcv.src_ip, "\n");
LM_DBG("headers:\n%.*s.\n",(int)(req->body-req->start), req->start);
#endif
if (req->has_content_len){
LM_DBG("content-length= %d\n", req->content_len);
#ifdef EXTRA_DEBUG
LM_DBG("body:\n%.*s\n", req->content_len,req->body);
#endif
}else{
req->error=TCP_REQ_BAD_LEN;
LM_ERR("content length not present or unparsable\n");
goto error;
}
/* update the timeout - we successfully read the request */
tcp_conn_set_lifetime( con, tcp_con_lifetime);
con->timeout=con->lifetime;
/* if we are here everything is nice and ok*/
update_stat( pt[process_no].load, +1 );
#ifdef EXTRA_DEBUG
LM_DBG("calling receive_msg(%p, %d, )\n",
req->start, (int)(req->parsed-req->start));
#endif
/* rcv.bind_address should always be !=0 */
bind_address=con->rcv.bind_address;
/* just for debugging use sendipv4 as receiving socket FIXME*/
con->rcv.proto_reserved1=con->id; /* copy the id */
c=*req->parsed; /* ugly hack: zero term the msg & save the
previous char, req->parsed should be ok
because we always alloc BUF_SIZE+1 */
*req->parsed=0;
/* prepare for next request */
size=req->pos-req->parsed;
if (req->state==H_PING_CRLFCRLF) {
/* we send the reply */
if (_tcp_common_write( con, con->fd, CRLF, CRLF_LEN) < 0) {
LM_ERR("CRLF pong - _tcp_common_write() failed\n");
}
} else if (req->state!=H_SKIP_EMPTY) {
msg_buf = req->start;
msg_len = req->parsed-req->start;
local_rcv = con->rcv;
if (!size) {
/* did not read any more things - we can release
* the connection */
LM_DBG("Nothing more to read on TCP conn %p, currently in state %d \n",
con,con->state);
if (req != &_tcp_common_current_req) {
/* we have the buffer in the connection tied buff -
* detach it , release the conn and free it afterwards */
con->con_req = NULL;
}
/* TODO - we could indicate to the TCP net layer to release
* the connection -> other worker may read the next available
* message on the pipe */
} else {
LM_DBG("We still have things on the pipe - "
"keeping connection \n");
}
if (receive_msg(msg_buf, msg_len,
&local_rcv) <0)
LM_ERR("receive_msg failed \n");
if (!size && req != &_tcp_common_current_req) {
/* if we no longer need this tcp_req
* we can free it now */
pkg_free(req);
}
}
update_stat( pt[process_no].load, -1 );
if (size) {
/* restoring the char only makes sense if there is something else to
* process, otherwise we can leave it. This prevents us from accessing
* unallocated memory - razvanc */
*req->parsed=c;
memmove(req->buf, req->parsed, size);
}
#ifdef EXTRA_DEBUG
LM_DBG("preparing for new request, kept %ld bytes\n", size);
#endif
init_tcp_req(req, size);
con->msg_attempts = 0;
/* if we still have some unparsed bytes, try to parse them too*/
if (size) return 1;
} else {
/* request not complete - check the if the thresholds are exceeded */
if (con->msg_attempts==0)
/* if first iteration, set a short timeout for reading
* a whole SIP message */
con->timeout = get_ticks() + tcp_max_msg_time;
con->msg_attempts ++;
if (con->msg_attempts == _max_msg_chunks) {
LM_ERR("Made %u read attempts but message is not complete yet - "
"closing connection \n",con->msg_attempts);
goto error;
}
if (req == &_tcp_common_current_req) {
/* let's duplicate this - most likely another conn will come in */
LM_DBG("We didn't manage to read a full request\n");
con->con_req = pkg_malloc(sizeof(struct tcp_req));
if (con->con_req == NULL) {
LM_ERR("No more mem for dynamic con request buffer\n");
goto error;
}
if (req->pos != req->buf) {
/* we have read some bytes */
memcpy(con->con_req->buf,req->buf,req->pos-req->buf);
con->con_req->pos = con->con_req->buf + (req->pos-req->buf);
} else {
con->con_req->pos = con->con_req->buf;
}
if (req->start != req->buf)
con->con_req->start = con->con_req->buf +(req->start-req->buf);
else
con->con_req->start = con->con_req->buf;
if (req->parsed != req->buf)
con->con_req->parsed =con->con_req->buf+(req->parsed-req->buf);
else
con->con_req->parsed = con->con_req->buf;
if (req->body != 0) {
con->con_req->body = con->con_req->buf + (req->body-req->buf);
} else
con->con_req->body = 0;
con->con_req->complete=req->complete;
con->con_req->has_content_len=req->has_content_len;
con->con_req->content_len=req->content_len;
con->con_req->bytes_to_go=req->bytes_to_go;
con->con_req->error = req->error;
con->con_req->state = req->state;
/* req will be reset on the next usage */
}
}
/* everything ok */
return 0;
error:
/* report error */
return -1;
}
#endif