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