1 /*
2  * sip msg. header proxy parser
3  *
4  * Copyright (C) 2001-2003 FhG Fokus
5  *
6  * This file is part of Kamailio, a free SIP server.
7  *
8  * Kamailio 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  * Kamailio is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23 
24 /** Parser :: SIP Message header proxy parser.
25  * @file
26  * @ingroup parser
27  */
28 
29 /*! \defgroup parser SIP-router SIP message parser
30  *
31  * The SIP message parser
32  *
33  */
34 
35 
36 #include <string.h>
37 #include <stdlib.h>
38 #include <sys/time.h>
39 
40 #include "../comp_defs.h"
41 #include "msg_parser.h"
42 #include "parser_f.h"
43 #include "../ut.h"
44 #include "../error.h"
45 #include "../dprint.h"
46 #include "../data_lump_rpl.h"
47 #include "../mem/mem.h"
48 #include "../error.h"
49 #include "../core_stats.h"
50 #include "../globals.h"
51 #include "parse_hname2.h"
52 #include "parse_uri.h"
53 #include "parse_content.h"
54 #include "parse_to.h"
55 #include "../compiler_opt.h"
56 
57 #ifdef DEBUG_DMALLOC
58 #include <mem/dmalloc.h>
59 #endif
60 
61 
62 #define parse_hname(_b,_e,_h) parse_hname2((_b),(_e),(_h))
63 
64 /* number of via's encountered */
65 int via_cnt;
66 /* global request flags */
67 unsigned int global_req_flags = 0;
68 
69 /* returns pointer to next header line, and fill hdr_f ;
70  * if at end of header returns pointer to the last crlf  (always buf)*/
get_hdr_field(char * const buf,char * const end,struct hdr_field * const hdr)71 char* get_hdr_field(char* const buf, char* const end, struct hdr_field* const hdr)
72 {
73 
74 	char *tmp = 0;
75 	char *match;
76 	struct via_body *vb;
77 	struct cseq_body* cseq_b;
78 	struct to_body* to_b;
79 	int integer, err;
80 	unsigned uval;
81 
82 	if(!buf) {
83 		DBG("null buffer pointer\n");
84 		goto error;
85 	}
86 
87 	if ((*buf)=='\n' || (*buf)=='\r'){
88 		/* double crlf or lflf or crcr */
89 		DBG("found end of header\n");
90 		hdr->type=HDR_EOH_T;
91 		return buf;
92 	}
93 
94 	tmp=parse_hname(buf, end, hdr);
95 	if (hdr->type==HDR_ERROR_T){
96 		ERR("bad header\n");
97 		goto error;
98 	}
99 
100 	/* eliminate leading whitespace */
101 	tmp=eat_lws_end(tmp, end);
102 	if (tmp>=end) {
103 		ERR("HF empty\n");
104 		goto error;
105 	}
106 
107 	/* if header-field well-known, parse it, find its end otherwise ;
108 	 * after leaving the hdr->type switch, tmp should be set to the
109 	 * next header field
110 	 */
111 	switch(hdr->type){
112 		case HDR_VIA_T:
113 			/* keep number of vias parsed -- we want to report it in
114 			   replies for diagnostic purposes */
115 			via_cnt++;
116 			vb=pkg_malloc(sizeof(struct via_body));
117 			if (vb==0){
118 				PKG_MEM_ERROR;
119 				goto error;
120 			}
121 			memset(vb,0,sizeof(struct via_body));
122 			hdr->body.s=tmp;
123 			tmp=parse_via(tmp, end, vb);
124 			if (vb->error==PARSE_ERROR){
125 				ERR("bad via\n");
126 				free_via_list(vb);
127 				goto error;
128 			}
129 			hdr->parsed=vb;
130 			vb->hdr.s=hdr->name.s;
131 			vb->hdr.len=hdr->name.len;
132 			hdr->body.len=tmp-hdr->body.s;
133 			break;
134 		case HDR_CSEQ_T:
135 			cseq_b=pkg_malloc(sizeof(struct cseq_body));
136 			if (cseq_b==0){
137 				PKG_MEM_ERROR;
138 				goto error;
139 			}
140 			memset(cseq_b, 0, sizeof(struct cseq_body));
141 			hdr->body.s=tmp;
142 			tmp=parse_cseq(tmp, end, cseq_b);
143 			if (cseq_b->error==PARSE_ERROR){
144 				ERR("bad cseq\n");
145 				free_cseq(cseq_b);
146 				goto error;
147 			}
148 			hdr->parsed=cseq_b;
149 			hdr->body.len=tmp-hdr->body.s;
150 			DBG("cseq <%.*s>: <%.*s> <%.*s>\n", hdr->name.len, ZSW(hdr->name.s),
151 					cseq_b->number.len, ZSW(cseq_b->number.s), cseq_b->method.len,
152 					cseq_b->method.s);
153 			break;
154 		case HDR_TO_T:
155 			to_b=pkg_malloc(sizeof(struct to_body));
156 			if (to_b==0){
157 				PKG_MEM_ERROR;
158 				goto error;
159 			}
160 			memset(to_b, 0, sizeof(struct to_body));
161 			hdr->body.s=tmp;
162 			tmp=parse_to(tmp, end,to_b);
163 			if (to_b->error==PARSE_ERROR){
164 				ERR("bad to header\n");
165 				free_to(to_b);
166 				goto error;
167 			}
168 			hdr->parsed=to_b;
169 			hdr->body.len=tmp-hdr->body.s;
170 			DBG("<%.*s> [%d]; uri=[%.*s]\n", hdr->name.len, ZSW(hdr->name.s),
171 					hdr->body.len, to_b->uri.len, ZSW(to_b->uri.s));
172 			DBG("to body (%d)[%.*s], to tag (%d)[%.*s]\n", to_b->body.len,
173 					to_b->body.len, ZSW(to_b->body.s), to_b->tag_value.len,
174 					to_b->tag_value.len, ZSW(to_b->tag_value.s));
175 			break;
176 		case HDR_CONTENTLENGTH_T:
177 			hdr->body.s=tmp;
178 			tmp=parse_content_length(tmp,end, &integer);
179 			if (tmp==0){
180 				ERR("bad content_length header\n");
181 				goto error;
182 			}
183 			hdr->parsed=(void*)(long)integer;
184 			hdr->body.len=tmp-hdr->body.s;
185 			DBG("content_length=%d\n", (int)(long)hdr->parsed);
186 			break;
187 		case HDR_RETRY_AFTER_T:
188 			hdr->body.s=tmp;
189 			tmp=parse_retry_after(tmp,end, &uval, &err);
190 			if (err){
191 				ERR("bad retry_after header\n");
192 				goto error;
193 			}
194 			hdr->parsed=(void*)(unsigned long)uval;
195 			hdr->body.len=tmp-hdr->body.s;
196 			DBG("retry_after=%d\n", (unsigned)(long)hdr->parsed);
197 			break;
198 		case HDR_IDENTITY_T:
199 		case HDR_DATE_T:
200 		case HDR_IDENTITY_INFO_T:
201 		case HDR_SUPPORTED_T:
202 		case HDR_REQUIRE_T:
203 		case HDR_CONTENTTYPE_T:
204 		case HDR_FROM_T:
205 		case HDR_CALLID_T:
206 		case HDR_CONTACT_T:
207 		case HDR_ROUTE_T:
208 		case HDR_RECORDROUTE_T:
209 		case HDR_MAXFORWARDS_T:
210 		case HDR_AUTHORIZATION_T:
211 		case HDR_EXPIRES_T:
212 		case HDR_MIN_EXPIRES_T:
213 		case HDR_PROXYAUTH_T:
214 		case HDR_PROXYREQUIRE_T:
215 		case HDR_UNSUPPORTED_T:
216 		case HDR_ALLOW_T:
217 		case HDR_EVENT_T:
218 		case HDR_ACCEPT_T:
219 		case HDR_ACCEPTLANGUAGE_T:
220 		case HDR_ORGANIZATION_T:
221 		case HDR_PRIORITY_T:
222 		case HDR_SUBJECT_T:
223 		case HDR_USERAGENT_T:
224 		case HDR_SERVER_T:
225 		case HDR_CONTENTDISPOSITION_T:
226 		case HDR_DIVERSION_T:
227 		case HDR_RPID_T:
228 		case HDR_SIPIFMATCH_T:
229 		case HDR_REFER_TO_T:
230 		case HDR_SESSIONEXPIRES_T:
231 		case HDR_MIN_SE_T:
232 		case HDR_SUBSCRIPTION_STATE_T:
233 		case HDR_ACCEPTCONTACT_T:
234 		case HDR_ALLOWEVENTS_T:
235 		case HDR_CONTENTENCODING_T:
236 		case HDR_REFERREDBY_T:
237 		case HDR_REJECTCONTACT_T:
238 		case HDR_REQUESTDISPOSITION_T:
239 		case HDR_WWW_AUTHENTICATE_T:
240 		case HDR_PROXY_AUTHENTICATE_T:
241 		case HDR_PATH_T:
242 		case HDR_PRIVACY_T:
243 		case HDR_PAI_T:
244 		case HDR_PPI_T:
245 		case HDR_REASON_T:
246 		case HDR_CALLINFO_T:
247 		case HDR_OTHER_T:
248 			/* just skip over it */
249 			hdr->body.s=tmp;
250 			/* find end of header */
251 			/* find lf */
252 			do{
253 				match=q_memchr(tmp, '\n', end-tmp);
254 				if (match){
255 					match++;
256 				} else {
257 					ERR("no eol - bad body for <%.*s> (hdr type: %d) [%.*s]\n",
258 							 hdr->name.len, hdr->name.s,
259 							hdr->type, ((end-tmp)>128)?128:(int)(end-tmp), tmp);
260 					/* abort(); */
261 					tmp=end;
262 					goto error;
263 				}
264 				tmp=match;
265 			}while( match<end &&( (*match==' ')||(*match=='\t') ) );
266 			tmp=match;
267 			hdr->body.len=match-hdr->body.s;
268 			break;
269 		default:
270 			BUG("unknown header type %d [%.*s]\n", hdr->type,
271 					 ((end-buf)>128)?128:(int)(end-buf), buf);
272 			goto error;
273 	}
274 	/* jku: if \r covered by current length, shrink it */
275 	trim_r( hdr->body );
276 	hdr->len=tmp-hdr->name.s;
277 	return tmp;
278 error:
279 	DBG("error exit\n");
280 	STATS_BAD_MSG_HDR();
281 	hdr->type=HDR_ERROR_T;
282 	hdr->len=tmp-hdr->name.s;
283 	return tmp;
284 }
285 
286 
287 
288 /* parse the headers and adds them to msg->headers and msg->to, from etc.
289  * It stops when all the headers requested in flags were parsed, on error
290  * (bad header) or end of headers
291  * WARNING: parse_headers was changed to use hdr_flags_t (the flags are now
292  *          different from the header types). Don't call it with a header type
293  *          (HDR_xxx_T), only with header flags (HDR_xxx_F)!*/
294 /* note: it continues where it previously stopped and goes ahead until
295    end is encountered or desired HFs are found; if you call it twice
296    for the same HF which is present only once, it will fail the second
297    time; if you call it twice and the HF is found on second time too,
298    it's not replaced in the well-known HF pointer but just added to
299    header list; if you want to use a dumb convenience function which will
300    give you the first occurrence of a header you are interested in,
301    look at check_transaction_quadruple
302 */
parse_headers(struct sip_msg * const msg,const hdr_flags_t flags,const int next)303 int parse_headers(struct sip_msg* const msg, const hdr_flags_t flags, const int next)
304 {
305 	struct hdr_field* hf;
306 	char* tmp;
307 	char* rest;
308 	char* end;
309 	hdr_flags_t orig_flag;
310 
311 	end=msg->buf+msg->len;
312 	tmp=msg->unparsed;
313 
314 	if (unlikely(next)) {
315 		orig_flag = msg->parsed_flag;
316 		msg->parsed_flag &= ~flags;
317 	}else
318 		orig_flag=0;
319 
320 #ifdef EXTRA_DEBUG
321 	DBG("flags=%llx\n", (unsigned long long)flags);
322 #endif
323 	while( tmp<end && (flags & msg->parsed_flag) != flags){
324 		prefetch_loc_r(tmp+64, 1);
325 		hf=pkg_malloc(sizeof(struct hdr_field));
326 		if (unlikely(hf==0)){
327 			PKG_MEM_ERROR;
328 			ser_error=E_OUT_OF_MEM;
329 			goto error;
330 		}
331 		memset(hf,0, sizeof(struct hdr_field));
332 		hf->type=HDR_ERROR_T;
333 		rest=get_hdr_field(tmp, end, hf);
334 		switch (hf->type){
335 			case HDR_ERROR_T:
336 				ERR("bad header field [%.*s]\n",
337 						(end-tmp>100)?100:(int)(end-tmp), tmp);
338 				goto  error;
339 			case HDR_EOH_T:
340 				msg->eoh=tmp; /* or rest?*/
341 				msg->parsed_flag|=HDR_EOH_F;
342 				pkg_free(hf);
343 				goto skip;
344 			case HDR_ACCEPTCONTACT_T:
345 			case HDR_ALLOWEVENTS_T:
346 			case HDR_CONTENTENCODING_T:
347 			case HDR_REFERREDBY_T:
348 			case HDR_REJECTCONTACT_T:
349 			case HDR_REQUESTDISPOSITION_T:
350 			case HDR_WWW_AUTHENTICATE_T:
351 			case HDR_PROXY_AUTHENTICATE_T:
352 			case HDR_RETRY_AFTER_T:
353 			case HDR_OTHER_T: /* mark the type as found/parsed*/
354 				msg->parsed_flag|=HDR_T2F(hf->type);
355 				break;
356 			case HDR_CALLID_T:
357 				if (msg->callid==0) msg->callid=hf;
358 				msg->parsed_flag|=HDR_CALLID_F;
359 				break;
360 			case HDR_SIPIFMATCH_T:
361 				if (msg->sipifmatch==0) msg->sipifmatch=hf;
362 				msg->parsed_flag|=HDR_SIPIFMATCH_F;
363 				break;
364 			case HDR_TO_T:
365 				if (msg->to==0) msg->to=hf;
366 				msg->parsed_flag|=HDR_TO_F;
367 				break;
368 			case HDR_CSEQ_T:
369 				if (msg->cseq==0) msg->cseq=hf;
370 				msg->parsed_flag|=HDR_CSEQ_F;
371 				break;
372 			case HDR_FROM_T:
373 				if (msg->from==0) msg->from=hf;
374 				msg->parsed_flag|=HDR_FROM_F;
375 				break;
376 			case HDR_CONTACT_T:
377 				if (msg->contact==0) msg->contact=hf;
378 				msg->parsed_flag|=HDR_CONTACT_F;
379 				break;
380 			case HDR_MAXFORWARDS_T:
381 				if(msg->maxforwards==0) msg->maxforwards=hf;
382 				msg->parsed_flag|=HDR_MAXFORWARDS_F;
383 				break;
384 			case HDR_ROUTE_T:
385 				if (msg->route==0) msg->route=hf;
386 				msg->parsed_flag|=HDR_ROUTE_F;
387 				break;
388 			case HDR_RECORDROUTE_T:
389 				if (msg->record_route==0) msg->record_route = hf;
390 				msg->parsed_flag|=HDR_RECORDROUTE_F;
391 				break;
392 			case HDR_CONTENTTYPE_T:
393 				if (msg->content_type==0) msg->content_type = hf;
394 				msg->parsed_flag|=HDR_CONTENTTYPE_F;
395 				break;
396 			case HDR_CONTENTLENGTH_T:
397 				if (msg->content_length==0) msg->content_length = hf;
398 				msg->parsed_flag|=HDR_CONTENTLENGTH_F;
399 				break;
400 			case HDR_AUTHORIZATION_T:
401 				if (msg->authorization==0) msg->authorization = hf;
402 				msg->parsed_flag|=HDR_AUTHORIZATION_F;
403 				break;
404 			case HDR_EXPIRES_T:
405 				if (msg->expires==0) msg->expires = hf;
406 				msg->parsed_flag|=HDR_EXPIRES_F;
407 				break;
408 			case HDR_MIN_EXPIRES_T:
409 				if (msg->min_expires==0) msg->min_expires = hf;
410 				msg->parsed_flag|=HDR_MIN_EXPIRES_F;
411 				break;
412 			case HDR_PROXYAUTH_T:
413 				if (msg->proxy_auth==0) msg->proxy_auth = hf;
414 				msg->parsed_flag|=HDR_PROXYAUTH_F;
415 				break;
416 			case HDR_PROXYREQUIRE_T:
417 				if (msg->proxy_require==0) msg->proxy_require = hf;
418 				msg->parsed_flag|=HDR_PROXYREQUIRE_F;
419 				break;
420 			case HDR_SUPPORTED_T:
421 				if (msg->supported==0) msg->supported=hf;
422 				msg->parsed_flag|=HDR_SUPPORTED_F;
423 				break;
424 			case HDR_REQUIRE_T:
425 				if (msg->require==0) msg->require=hf;
426 				msg->parsed_flag|=HDR_REQUIRE_F;
427 				break;
428 			case HDR_UNSUPPORTED_T:
429 				if (msg->unsupported==0) msg->unsupported=hf;
430 				msg->parsed_flag|=HDR_UNSUPPORTED_F;
431 				break;
432 			case HDR_ALLOW_T:
433 				if (msg->allow==0) msg->allow = hf;
434 				msg->parsed_flag|=HDR_ALLOW_F;
435 				break;
436 			case HDR_EVENT_T:
437 				if (msg->event==0) msg->event = hf;
438 				msg->parsed_flag|=HDR_EVENT_F;
439 				break;
440 			case HDR_ACCEPT_T:
441 				if (msg->accept==0) msg->accept = hf;
442 				msg->parsed_flag|=HDR_ACCEPT_F;
443 				break;
444 			case HDR_ACCEPTLANGUAGE_T:
445 				if (msg->accept_language==0) msg->accept_language = hf;
446 				msg->parsed_flag|=HDR_ACCEPTLANGUAGE_F;
447 				break;
448 			case HDR_ORGANIZATION_T:
449 				if (msg->organization==0) msg->organization = hf;
450 				msg->parsed_flag|=HDR_ORGANIZATION_F;
451 				break;
452 			case HDR_PRIORITY_T:
453 				if (msg->priority==0) msg->priority = hf;
454 				msg->parsed_flag|=HDR_PRIORITY_F;
455 				break;
456 			case HDR_SUBJECT_T:
457 				if (msg->subject==0) msg->subject = hf;
458 				msg->parsed_flag|=HDR_SUBJECT_F;
459 				break;
460 			case HDR_USERAGENT_T:
461 				if (msg->user_agent==0) msg->user_agent = hf;
462 				msg->parsed_flag|=HDR_USERAGENT_F;
463 				break;
464 			case HDR_SERVER_T:
465 				if (msg->server==0) msg->server = hf;
466 				msg->parsed_flag|=HDR_SERVER_F;
467 				break;
468 			case HDR_CONTENTDISPOSITION_T:
469 				if (msg->content_disposition==0) msg->content_disposition = hf;
470 				msg->parsed_flag|=HDR_CONTENTDISPOSITION_F;
471 				break;
472 			case HDR_DIVERSION_T:
473 				if (msg->diversion==0) msg->diversion = hf;
474 				msg->parsed_flag|=HDR_DIVERSION_F;
475 				break;
476 			case HDR_RPID_T:
477 				if (msg->rpid==0) msg->rpid = hf;
478 				msg->parsed_flag|=HDR_RPID_F;
479 				break;
480 			case HDR_REFER_TO_T:
481 				if (msg->refer_to==0) msg->refer_to = hf;
482 				msg->parsed_flag|=HDR_REFER_TO_F;
483 				break;
484 			case HDR_SESSIONEXPIRES_T:
485 				if (msg->session_expires==0) msg->session_expires = hf;
486 				msg->parsed_flag|=HDR_SESSIONEXPIRES_F;
487 				break;
488 			case HDR_MIN_SE_T:
489 				if (msg->min_se==0) msg->min_se = hf;
490 				msg->parsed_flag|=HDR_MIN_SE_F;
491 				break;
492 			case HDR_SUBSCRIPTION_STATE_T:
493 				if (msg->subscription_state==0) msg->subscription_state = hf;
494 				msg->parsed_flag|=HDR_SUBSCRIPTION_STATE_F;
495 				break;
496 			case HDR_VIA_T:
497 				msg->parsed_flag|=HDR_VIA_F;
498 				DBG("Via found, flags=%llx\n", (unsigned long long)flags);
499 				if (msg->via1==0) {
500 					DBG("this is the first via\n");
501 					msg->h_via1=hf;
502 					msg->via1=hf->parsed;
503 					if (msg->via1->next){
504 						msg->via2=msg->via1->next;
505 						msg->parsed_flag|=HDR_VIA2_F;
506 					}
507 				}else if (msg->via2==0){
508 					msg->h_via2=hf;
509 					msg->via2=hf->parsed;
510 					msg->parsed_flag|=HDR_VIA2_F;
511 					DBG("this is the second via\n");
512 				}
513 				break;
514 			case HDR_DATE_T:
515 				if (msg->date==0) msg->date=hf;
516 				msg->parsed_flag|=HDR_DATE_F;
517 				break;
518 			case HDR_IDENTITY_T:
519 				if (msg->identity==0) msg->identity=hf;
520 				msg->parsed_flag|=HDR_IDENTITY_F;
521 				break;
522 			case HDR_IDENTITY_INFO_T:
523 				if (msg->identity_info==0) msg->identity_info=hf;
524 				msg->parsed_flag|=HDR_IDENTITY_INFO_F;
525 				break;
526 		    case HDR_PATH_T:
527 				if (msg->path==0) msg->path=hf;
528 				msg->parsed_flag|=HDR_PATH_F;
529 				break;
530 		    case HDR_PRIVACY_T:
531 				if (msg->privacy==0) msg->privacy=hf;
532 				msg->parsed_flag|=HDR_PRIVACY_F;
533 				break;
534 		    case HDR_PAI_T:
535 				if (msg->pai==0) msg->pai=hf;
536 				msg->parsed_flag|=HDR_PAI_F;
537 				break;
538 		    case HDR_PPI_T:
539 				if (msg->ppi==0) msg->ppi=hf;
540 				msg->parsed_flag|=HDR_PPI_F;
541 				break;
542 		    case HDR_REASON_T:
543 				msg->parsed_flag|=HDR_REASON_F;
544 				break;
545 			case HDR_CALLINFO_T:
546 				msg->parsed_flag|=HDR_CALLINFO_F;
547 				break;
548 			default:
549 				BUG("unknown header type %d\n", hf->type);
550 				goto error;
551 		}
552 		/* add the header to the list*/
553 		if (msg->last_header==0){
554 			msg->headers=hf;
555 			msg->last_header=hf;
556 		}else{
557 			msg->last_header->next=hf;
558 			msg->last_header=hf;
559 		}
560 #ifdef EXTRA_DEBUG
561 		DBG("header field type %d, name=<%.*s>, body=<%.*s>\n", hf->type,
562 				hf->name.len, ZSW(hf->name.s), hf->body.len, ZSW(hf->body.s));
563 #endif
564 		tmp=rest;
565 	}
566 skip:
567 	msg->unparsed=tmp;
568 	/* restore original flags */
569 	msg->parsed_flag |= orig_flag;
570 	return 0;
571 
572 error:
573 	ser_error=E_BAD_REQ;
574 	if (hf) pkg_free(hf);
575 	/* restore original flags */
576 	msg->parsed_flag |= orig_flag;
577 	return -1;
578 }
579 
580 
581 
582 
583 
584 /* returns 0 if ok, -1 for errors */
parse_msg(char * const buf,const unsigned int len,struct sip_msg * const msg)585 int parse_msg(char* const buf, const unsigned int len, struct sip_msg* const msg)
586 {
587 
588 	char *tmp;
589 	char* rest;
590 	struct msg_start *fl;
591 	int offset;
592 	hdr_flags_t flags;
593 
594 	/* eat crlf & whitespaces from the beginning */
595 	for (tmp=buf; (tmp-buf < len)
596 			&& (*tmp=='\n' || *tmp=='\r' || *tmp=='\0'
597 				|| *tmp=='\t' || *tmp==' ');
598 			tmp++);
599 	offset=tmp-buf;
600 	fl=&(msg->first_line);
601 	rest=parse_first_line(tmp, len-offset, fl);
602 	offset+=rest-tmp;
603 	tmp=rest;
604 	switch(fl->type){
605 		case SIP_INVALID:
606 			DBG("invalid message\n");
607 			goto error;
608 			break;
609 		case SIP_REQUEST:
610 			DBG("SIP Request:\n");
611 			DBG(" method:  <%.*s>\n", fl->u.request.method.len,
612 					ZSW(fl->u.request.method.s));
613 			DBG(" uri:     <%.*s>\n", fl->u.request.uri.len,
614 					ZSW(fl->u.request.uri.s));
615 			DBG(" version: <%.*s>\n", fl->u.request.version.len,
616 					ZSW(fl->u.request.version.s));
617 			flags=HDR_VIA_F;
618 			break;
619 		case SIP_REPLY:
620 			DBG("SIP Reply  (status):\n");
621 			DBG(" version: <%.*s>\n", fl->u.reply.version.len,
622 					ZSW(fl->u.reply.version.s));
623 			DBG(" status:  <%.*s>\n", fl->u.reply.status.len,
624 					ZSW(fl->u.reply.status.s));
625 			DBG(" reason:  <%.*s>\n", fl->u.reply.reason.len,
626 					ZSW(fl->u.reply.reason.s));
627 			/* flags=HDR_VIA | HDR_VIA2; */
628 			/* we don't try to parse VIA2 for local messages; -Jiri */
629 			flags=HDR_VIA_F;
630 			break;
631 		default:
632 			DBG("unknown type %d\n", fl->type);
633 			goto error;
634 	}
635 	msg->unparsed=tmp;
636 	/*find first Via: */
637 	if (parse_headers(msg, flags, 0)==-1) goto error;
638 
639 #ifdef EXTRA_DEBUG
640 	/* dump parsed data */
641 	if (msg->via1){
642 		DBG("first via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
643 				msg->via1->name.len, ZSW(msg->via1->name.s),
644 				msg->via1->version.len, ZSW(msg->via1->version.s),
645 				msg->via1->transport.len, ZSW(msg->via1->transport.s),
646 				msg->via1->host.len, ZSW(msg->via1->host.s),
647 				msg->via1->port_str.len, ZSW(msg->via1->port_str.s),
648 				msg->via1->port);
649 		if (msg->via1->params.s)
650 			DBG(";<%.*s>", msg->via1->params.len, ZSW(msg->via1->params.s));
651 		if (msg->via1->comment.s)
652 			DBG(" <%.*s>", msg->via1->comment.len, ZSW(msg->via1->comment.s));
653 		DBG ("\n");
654 	}
655 	if (msg->via2){
656 		DBG("second via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
657 				msg->via2->name.len, ZSW(msg->via2->name.s),
658 				msg->via2->version.len, ZSW(msg->via2->version.s),
659 				msg->via2->transport.len, ZSW(msg->via2->transport.s),
660 				msg->via2->host.len, ZSW(msg->via2->host.s),
661 				msg->via2->port_str.len, ZSW(msg->via2->port_str.s),
662 				msg->via2->port);
663 		if (msg->via2->params.s)
664 			DBG(";<%.*s>", msg->via2->params.len, ZSW(msg->via2->params.s));
665 		if (msg->via2->comment.s)
666 			DBG(" <%.*s>", msg->via2->comment.len, ZSW(msg->via2->comment.s));
667 		DBG ("\n");
668 	}
669 #endif
670 
671 
672 #ifdef EXTRA_DEBUG
673 	DBG("exiting parse_msg\n");
674 #endif
675 
676 	return 0;
677 
678 error:
679 	/* more debugging, msg->orig is/should be null terminated*/
680 	LOG(cfg_get(core, core_cfg, corelog), "ERROR: parse_msg: message=<%.*s>\n",
681 			(int)msg->len, ZSW(msg->buf));
682 	return -1;
683 }
684 
685 
686 
free_reply_lump(struct lump_rpl * lump)687 void free_reply_lump( struct lump_rpl *lump)
688 {
689 	struct lump_rpl *foo, *bar;
690 	for(foo=lump;foo;)
691 	{
692 		bar=foo->next;
693 		free_lump_rpl(foo);
694 		foo = bar;
695 	}
696 }
697 
698 
699 /*only the content*/
free_sip_msg(struct sip_msg * const msg)700 void free_sip_msg(struct sip_msg* const msg)
701 {
702 	reset_new_uri(msg);
703 	reset_dst_uri(msg);
704 	reset_path_vector(msg);
705 	reset_instance(msg);
706 	reset_ruid(msg);
707 	reset_ua(msg);
708 	if (msg->headers)     free_hdr_field_lst(msg->headers);
709 	if (msg->body && msg->body->free) msg->body->free(&msg->body);
710 	if (msg->add_rm)      free_lump_list(msg->add_rm);
711 	if (msg->body_lumps)  free_lump_list(msg->body_lumps);
712 	if (msg->reply_lump)   free_reply_lump(msg->reply_lump);
713 	msg_ldata_reset(msg);
714 	/* no free of msg->buf -- a pointer to a static buffer */
715 }
716 
717 /**
718  * reset new uri value
719  */
reset_new_uri(struct sip_msg * const msg)720 void reset_new_uri(struct sip_msg* const msg)
721 {
722 	if(msg->new_uri.s != 0) {
723 		pkg_free(msg->new_uri.s);
724 	}
725 	msg->new_uri.s = 0;
726 	msg->new_uri.len = 0;
727 	msg->parsed_uri_ok = 0;
728 }
729 
730 
731 /*
732  * Make a private copy of the string and assign it to dst_uri
733  */
set_dst_uri(struct sip_msg * const msg,const str * const uri)734 int set_dst_uri(struct sip_msg* const msg, const str* const uri)
735 {
736 	char* ptr;
737 
738 	if (unlikely(!msg || !uri)) {
739 		ERR("Invalid parameter value\n");
740 		return -1;
741 	}
742 
743 	if (unlikely(uri->len == 0)) {
744 		reset_dst_uri(msg);
745 	}else if (msg->dst_uri.s && (msg->dst_uri.len >= uri->len)) {
746 		memcpy(msg->dst_uri.s, uri->s, uri->len);
747 		msg->dst_uri.len = uri->len;
748 	} else {
749 		ptr = (char*)pkg_malloc(uri->len + 1);
750 		if (!ptr) {
751 			PKG_MEM_ERROR;
752 			return -1;
753 		}
754 
755 		memcpy(ptr, uri->s, uri->len);
756 		if (msg->dst_uri.s) pkg_free(msg->dst_uri.s);
757 		msg->dst_uri.s = ptr;
758 		msg->dst_uri.len = uri->len;
759 		msg->dst_uri.s[msg->dst_uri.len] = '\0';
760 	}
761 	return 0;
762 }
763 
764 
reset_dst_uri(struct sip_msg * const msg)765 void reset_dst_uri(struct sip_msg* const msg)
766 {
767 	if(msg->dst_uri.s != 0) {
768 		pkg_free(msg->dst_uri.s);
769 	}
770 	msg->dst_uri.s = 0;
771 	msg->dst_uri.len = 0;
772 }
773 
set_path_vector(struct sip_msg * msg,str * path)774 int set_path_vector(struct sip_msg* msg, str* path)
775 {
776 	char* ptr;
777 
778 	if (unlikely(!msg || !path)) {
779 		ERR("invalid parameter value\n");
780 		return -1;
781 	}
782 
783 	if (unlikely(path->len == 0)) {
784 		reset_path_vector(msg);
785 	} else if (msg->path_vec.s && (msg->path_vec.len >= path->len)) {
786 		memcpy(msg->path_vec.s, path->s, path->len);
787 		msg->path_vec.len = path->len;
788 	} else {
789 		ptr = (char*)pkg_malloc(path->len);
790 		if (!ptr) {
791 			PKG_MEM_ERROR;
792 			return -1;
793 		}
794 
795 		memcpy(ptr, path->s, path->len);
796 		if (msg->path_vec.s) pkg_free(msg->path_vec.s);
797 		msg->path_vec.s = ptr;
798 		msg->path_vec.len = path->len;
799 	}
800 	return 0;
801 }
802 
803 
reset_path_vector(struct sip_msg * const msg)804 void reset_path_vector(struct sip_msg* const msg)
805 {
806 	if (!shm_address_in(msg->path_vec.s)) {
807 		if (msg->path_vec.s)
808 			pkg_free(msg->path_vec.s);
809 		msg->path_vec.s = 0;
810 		msg->path_vec.len = 0;
811 	} else {
812 		LM_WARN("Found path_vec that is not in pkg mem!\n");
813 	}
814 }
815 
816 
set_instance(struct sip_msg * msg,str * instance)817 int set_instance(struct sip_msg* msg, str* instance)
818 {
819 	char* ptr;
820 
821 	if (unlikely(!msg || !instance)) {
822 		ERR("invalid instance parameter value\n");
823 		return -1;
824 	}
825 
826 	if (unlikely(instance->len == 0)) {
827 		reset_instance(msg);
828 	} else if (msg->instance.s && (msg->instance.len >= instance->len)) {
829 		memcpy(msg->instance.s, instance->s, instance->len);
830 		msg->instance.len = instance->len;
831 	} else {
832 		ptr = (char*)pkg_malloc(instance->len);
833 		if (!ptr) {
834 			PKG_MEM_ERROR;
835 			return -1;
836 		}
837 		memcpy(ptr, instance->s, instance->len);
838 		if (msg->instance.s) pkg_free(msg->instance.s);
839 		msg->instance.s = ptr;
840 		msg->instance.len = instance->len;
841 	}
842 	return 0;
843 }
844 
845 
reset_instance(struct sip_msg * const msg)846 void reset_instance(struct sip_msg* const msg)
847 {
848 	if(msg->instance.s != 0) {
849 		pkg_free(msg->instance.s);
850 	}
851 	msg->instance.s = 0;
852 	msg->instance.len = 0;
853 }
854 
855 
set_ruid(struct sip_msg * msg,str * ruid)856 int set_ruid(struct sip_msg* msg, str* ruid)
857 {
858 	char* ptr;
859 
860 	if (unlikely(!msg || !ruid)) {
861 		ERR("invalid ruid parameter value\n");
862 		return -1;
863 	}
864 
865 	if (unlikely(ruid->len == 0)) {
866 		reset_ruid(msg);
867 	} else if (msg->ruid.s && (msg->ruid.len >= ruid->len)) {
868 		memcpy(msg->ruid.s, ruid->s, ruid->len);
869 		msg->ruid.len = ruid->len;
870 	} else {
871 		ptr = (char*)pkg_malloc(ruid->len);
872 		if (!ptr) {
873 			PKG_MEM_ERROR;
874 			return -1;
875 		}
876 		memcpy(ptr, ruid->s, ruid->len);
877 		if (msg->ruid.s) pkg_free(msg->ruid.s);
878 		msg->ruid.s = ptr;
879 		msg->ruid.len = ruid->len;
880 	}
881 	return 0;
882 }
883 
884 
reset_ruid(struct sip_msg * const msg)885 void reset_ruid(struct sip_msg* const msg)
886 {
887 	if(msg->ruid.s != 0) {
888 		pkg_free(msg->ruid.s);
889 	}
890 	msg->ruid.s = 0;
891 	msg->ruid.len = 0;
892 }
893 
894 
set_ua(struct sip_msg * msg,str * location_ua)895 int set_ua(struct sip_msg* msg, str* location_ua)
896 {
897 	char* ptr;
898 
899 	if (unlikely(!msg || !location_ua)) {
900 		ERR("invalid location_ua parameter value\n");
901 		return -1;
902 	}
903 
904 	if (unlikely(location_ua->len == 0)) {
905 		reset_ua(msg);
906 	} else if (msg->location_ua.s && (msg->location_ua.len >= location_ua->len)) {
907 		memcpy(msg->location_ua.s, location_ua->s, location_ua->len);
908 		msg->location_ua.len = location_ua->len;
909 	} else {
910 		ptr = (char*)pkg_malloc(location_ua->len);
911 		if (!ptr) {
912 			PKG_MEM_ERROR;
913 			return -1;
914 		}
915 		memcpy(ptr, location_ua->s, location_ua->len);
916 		if (msg->location_ua.s) pkg_free(msg->location_ua.s);
917 		msg->location_ua.s = ptr;
918 		msg->location_ua.len = location_ua->len;
919 	}
920 	return 0;
921 }
922 
923 
reset_ua(struct sip_msg * const msg)924 void reset_ua(struct sip_msg* const msg)
925 {
926 	if(msg->location_ua.s != 0) {
927 		pkg_free(msg->location_ua.s);
928 	}
929 	msg->location_ua.s = 0;
930 	msg->location_ua.len = 0;
931 }
932 
933 /**
934  * reset content of msg->ldv (msg_ldata_t structure)
935  */
msg_ldata_reset(sip_msg_t * msg)936 void msg_ldata_reset(sip_msg_t *msg)
937 {
938 	if(msg==NULL)
939 		return;
940 	memset(&msg->ldv, 0, sizeof(msg_ldata_t));
941 }
942 
943 
get_hdr(const sip_msg_t * const msg,const enum _hdr_types_t ht)944 hdr_field_t* get_hdr(const sip_msg_t* const msg, const enum _hdr_types_t ht)
945 {
946 	hdr_field_t *hdr;
947 
948 	if (msg->parsed_flag & HDR_T2F(ht))
949 		for(hdr = msg->headers; hdr; hdr = hdr->next) {
950 			if(hdr->type == ht) return hdr;
951 		}
952 	return NULL;
953 }
954 
955 
next_sibling_hdr(const hdr_field_t * const hf)956 hdr_field_t* next_sibling_hdr(const hdr_field_t* const hf)
957 {
958 	hdr_field_t *hdr;
959 
960 	for(hdr = hf->next; hdr; hdr = hdr->next) {
961 		if(hdr->type == hf->type) return hdr;
962 	}
963 	return NULL;
964 }
965 
get_hdr_by_name(const sip_msg_t * const msg,const char * const name,const int name_len)966 hdr_field_t* get_hdr_by_name(const sip_msg_t* const msg, const char* const name, const int name_len)
967 {
968 	hdr_field_t *hdr;
969 
970 	for(hdr = msg->headers; hdr; hdr = hdr->next) {
971 		if(hdr->name.len == name_len && *hdr->name.s==*name
972 				&& strncasecmp(hdr->name.s, name, name_len)==0)
973 			return hdr;
974 	}
975 	return NULL;
976 }
977 
978 /** not used yet */
next_sibling_hdr_by_name(const hdr_field_t * const hf)979 hdr_field_t* next_sibling_hdr_by_name(const hdr_field_t* const hf)
980 {
981 	hdr_field_t *hdr;
982 
983 	for(hdr = hf->next; hdr; hdr = hdr->next) {
984 		if(hdr->name.len == hf->name.len && *hdr->name.s==*hf->name.s
985 				&& strncasecmp(hdr->name.s, hf->name.s, hf->name.len)==0)
986 			return hdr;
987 	}
988 	return NULL;
989 }
990 
991 /**
992  * set msg context id
993  * - return: -1 on error; 0 - on set
994  */
msg_ctx_id_set(const sip_msg_t * const msg,msg_ctx_id_t * const mid)995 int msg_ctx_id_set(const sip_msg_t* const msg, msg_ctx_id_t* const mid)
996 {
997 	if(msg==NULL || mid==NULL)
998 		return -1;
999 	mid->msgid = msg->id;
1000 	mid->pid = msg->pid;
1001 	return 0;
1002 }
1003 
1004 /**
1005  * check msg context id
1006  * - return: -1 on error; 0 - on no match; 1 - on match
1007  */
msg_ctx_id_match(const sip_msg_t * const msg,const msg_ctx_id_t * const mid)1008 int msg_ctx_id_match(const sip_msg_t* const msg, const msg_ctx_id_t* const mid)
1009 {
1010 	if(msg==NULL || mid==NULL)
1011 		return -1;
1012 	if(msg->id != mid->msgid || msg->pid!=mid->pid)
1013 		return 0;
1014 	return 1;
1015 }
1016 
1017 /**
1018  * set msg time value
1019  */
msg_set_time(sip_msg_t * const msg)1020 int msg_set_time(sip_msg_t* const msg)
1021 {
1022 	if(unlikely(msg==NULL))
1023 		return -2;
1024 	if(msg->tval.tv_sec!=0)
1025 		return 0;
1026 	return gettimeofday(&msg->tval, NULL);
1027 }
1028 
1029 /**
1030  * get source ip, port and protocol in SIP URI format
1031  * - tmode - 0: short format (transport=udp is not added, being default)
1032  */
get_src_uri(sip_msg_t * m,int tmode,str * uri)1033 int get_src_uri(sip_msg_t *m, int tmode, str *uri)
1034 {
1035 	static char buf[MAX_URI_SIZE];
1036 	char* p;
1037 	str ip, port;
1038 	int len;
1039 	str proto;
1040 
1041 	if (!uri || !m) {
1042 		ERR("invalid parameter value\n");
1043 		return -1;
1044 	}
1045 
1046 	if(tmode==0) {
1047 		switch(m->rcv.proto) {
1048 			case PROTO_NONE:
1049 			case PROTO_UDP:
1050 				proto.s = 0; /* Do not add transport parameter, UDP is default */
1051 				proto.len = 0;
1052 			break;
1053 			default:
1054 				if(get_valid_proto_string(m->rcv.proto, 1, 0, &proto)<0) {
1055 					ERR("unknown transport protocol\n");
1056 					return -1;
1057 				}
1058 		}
1059 	} else {
1060 		if(get_valid_proto_string(m->rcv.proto, 1, 0, &proto)<0) {
1061 			ERR("unknown transport protocol\n");
1062 			return -1;
1063 		}
1064 	}
1065 
1066 	ip.s = ip_addr2a(&m->rcv.src_ip);
1067 	ip.len = strlen(ip.s);
1068 
1069 	port.s = int2str(m->rcv.src_port, &port.len);
1070 
1071 	len = 4 + ip.len + 2*(m->rcv.src_ip.af==AF_INET6)+ 1 + port.len;
1072 	if (proto.s) {
1073 		len += TRANSPORT_PARAM_LEN;
1074 		len += proto.len;
1075 	}
1076 
1077 	if (len > MAX_URI_SIZE) {
1078 		ERR("buffer too small\n");
1079 		return -1;
1080 	}
1081 
1082 	p = buf;
1083 	memcpy(p, "sip:", 4);
1084 	p += 4;
1085 
1086 	if (m->rcv.src_ip.af==AF_INET6)
1087 		*p++ = '[';
1088 	memcpy(p, ip.s, ip.len);
1089 	p += ip.len;
1090 	if (m->rcv.src_ip.af==AF_INET6)
1091 		*p++ = ']';
1092 
1093 	*p++ = ':';
1094 
1095 	memcpy(p, port.s, port.len);
1096 	p += port.len;
1097 
1098 	if (proto.s) {
1099 		memcpy(p, TRANSPORT_PARAM, TRANSPORT_PARAM_LEN);
1100 		p += TRANSPORT_PARAM_LEN;
1101 
1102 		memcpy(p, proto.s, proto.len);
1103 		p += proto.len;
1104 	}
1105 
1106 	uri->s = buf;
1107 	uri->len = len;
1108 
1109 	return 0;
1110 }
1111 
1112 /**
1113  * get source proto:ip:port (socket address format)
1114  */
get_src_address_socket(sip_msg_t * m,str * ssock)1115 int get_src_address_socket(sip_msg_t *m, str *ssock)
1116 {
1117 	static char buf[MAX_URI_SIZE];
1118 	char* p;
1119 	str ip, port;
1120 	int len;
1121 	str proto;
1122 
1123 	if (!ssock || !m) {
1124 		ERR("invalid parameter value\n");
1125 		return -1;
1126 	}
1127 
1128 	if(get_valid_proto_string(m->rcv.proto, 1, 0, &proto)<0) {
1129 		ERR("unknown transport protocol\n");
1130 		return -1;
1131 	}
1132 
1133 	ip.s = ip_addr2a(&m->rcv.src_ip);
1134 	ip.len = strlen(ip.s);
1135 
1136 	port.s = int2str(m->rcv.src_port, &port.len);
1137 
1138 	len = proto.len + 1 + ip.len + 2*(m->rcv.src_ip.af==AF_INET6)+ 1 + port.len;
1139 
1140 	if (len+1 >= MAX_URI_SIZE) {
1141 		ERR("buffer too small\n");
1142 		return -1;
1143 	}
1144 
1145 	p = buf;
1146 
1147 	memcpy(p, proto.s, proto.len);
1148 	p += proto.len;
1149 
1150 	*p++ = ':';
1151 
1152 	if (m->rcv.src_ip.af==AF_INET6)
1153 		*p++ = '[';
1154 	memcpy(p, ip.s, ip.len);
1155 	p += ip.len;
1156 	if (m->rcv.src_ip.af==AF_INET6)
1157 		*p++ = ']';
1158 
1159 	*p++ = ':';
1160 
1161 	memcpy(p, port.s, port.len);
1162 	p += port.len;
1163 	*p = '\0';
1164 
1165 	ssock->s = buf;
1166 	ssock->len = len;
1167 
1168 	return 0;
1169 }
1170 
1171 /**
1172  * get received-on-socket ip, port and protocol in SIP URI format
1173  * - tmode - 0: short format (transport=udp is not added, being default)
1174  * - atype - 0: listen address; 1: advertised address
1175  */
get_rcv_socket_uri(sip_msg_t * m,int tmode,str * uri,int atype)1176 int get_rcv_socket_uri(sip_msg_t *m, int tmode, str *uri, int atype)
1177 {
1178 	static char buf[MAX_URI_SIZE];
1179 	char* p;
1180 	str ip, port;
1181 	int len;
1182 	str proto;
1183 
1184 	if (!uri || !m || !m->rcv.bind_address) {
1185 		ERR("invalid parameter value\n");
1186 		return -1;
1187 	}
1188 
1189 	if(tmode==0) {
1190 		switch(m->rcv.proto) {
1191 			case PROTO_NONE:
1192 			case PROTO_UDP:
1193 				proto.s = 0; /* Do not add transport parameter, UDP is default */
1194 				proto.len = 0;
1195 			break;
1196 			default:
1197 				if(get_valid_proto_string(m->rcv.proto, 1, 0, &proto)<0) {
1198 					ERR("unknown transport protocol\n");
1199 					return -1;
1200 				}
1201 		}
1202 	} else {
1203 		if(get_valid_proto_string(m->rcv.proto, 1, 0, &proto)<0) {
1204 			ERR("unknown transport protocol\n");
1205 			return -1;
1206 		}
1207 	}
1208 
1209 	if(atype==0 || m->rcv.bind_address->useinfo.address_str.len<=0) {
1210 		ip.s = m->rcv.bind_address->address_str.s;
1211 		ip.len = m->rcv.bind_address->address_str.len;
1212 	} else {
1213 		ip.s = m->rcv.bind_address->useinfo.address_str.s;
1214 		ip.len = m->rcv.bind_address->useinfo.address_str.len;
1215 	}
1216 
1217 	if(atype==0|| m->rcv.bind_address->useinfo.port_no_str.len <= 0) {
1218 		port.s = m->rcv.bind_address->port_no_str.s;
1219 		port.len = m->rcv.bind_address->port_no_str.len;
1220 	} else {
1221 		port.s = m->rcv.bind_address->useinfo.port_no_str.s;
1222 		port.len = m->rcv.bind_address->useinfo.port_no_str.len;
1223 	}
1224 
1225 	len = 4 + ip.len + 2*(m->rcv.src_ip.af==AF_INET6)+ 1 + port.len;
1226 	if (proto.s) {
1227 		len += TRANSPORT_PARAM_LEN;
1228 		len += proto.len;
1229 	}
1230 
1231 	if (len > MAX_URI_SIZE) {
1232 		ERR("buffer too small\n");
1233 		return -1;
1234 	}
1235 
1236 	p = buf;
1237 	memcpy(p, "sip:", 4);
1238 	p += 4;
1239 
1240 	if (m->rcv.src_ip.af==AF_INET6)
1241 		*p++ = '[';
1242 	memcpy(p, ip.s, ip.len);
1243 	p += ip.len;
1244 	if (m->rcv.src_ip.af==AF_INET6)
1245 		*p++ = ']';
1246 
1247 	*p++ = ':';
1248 
1249 	memcpy(p, port.s, port.len);
1250 	p += port.len;
1251 
1252 	if (proto.s) {
1253 		memcpy(p, TRANSPORT_PARAM, TRANSPORT_PARAM_LEN);
1254 		p += TRANSPORT_PARAM_LEN;
1255 
1256 		memcpy(p, proto.s, proto.len);
1257 		p += proto.len;
1258 	}
1259 
1260 	uri->s = buf;
1261 	uri->len = len;
1262 
1263 	return 0;
1264 }
1265 
1266 
1267 /*! \brief returns a pointer to the begining of the msg's body
1268  */
get_body(sip_msg_t * const msg)1269 char* get_body(sip_msg_t* const msg)
1270 {
1271 	int offset;
1272 	unsigned int len;
1273 
1274 	if ( parse_headers(msg, HDR_EOH_F, 0)==-1 ) {
1275 		LM_ERR("failed to parse to end of headers\n");
1276 		return 0;
1277 	}
1278 
1279 	if (msg->unparsed) {
1280 		len=(unsigned int)(msg->unparsed-msg->buf);
1281 	} else {
1282 		LM_ERR("unparsed hook for end of headers is not set\n");
1283 		return 0;
1284 	}
1285 
1286 	if ((len+2<=msg->len) && (strncmp(CRLF,msg->unparsed,CRLF_LEN)==0) ) {
1287 		offset = CRLF_LEN;
1288 	} else if ( (len+1<=msg->len) &&
1289 				(*(msg->unparsed)=='\n' || *(msg->unparsed)=='\r' ) ) {
1290 		offset = 1;
1291 	} else {
1292 		LM_ERR("failed to locate end of headers (%p %p - %d %d [%s])\n",
1293 				msg->buf, msg->unparsed, msg->len, len, msg->unparsed);
1294 		return 0;
1295 	}
1296 
1297 	return msg->unparsed + offset;
1298 }
1299 
1300 /*! \brief make sure all HFs needed for transaction identification have been
1301  * parsed; return 0 if those HFs can't be found
1302 */
check_transaction_quadruple(sip_msg_t * const msg)1303 int check_transaction_quadruple(sip_msg_t* const msg)
1304 {
1305 	if ( parse_headers(msg, HDR_FROM_F|HDR_TO_F|HDR_CALLID_F|HDR_CSEQ_F,0)!=-1
1306 		&& msg->from && msg->to && msg->callid && msg->cseq ) {
1307 		return 1;
1308 	} else {
1309 		ser_error=E_BAD_TUPEL;
1310 		return 0;
1311 	}
1312 }
1313