9e86fb9f237e605240e56151cc26ad5f88a841bd
[sip-router] / 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 /* returns pointer to next header line, and fill hdr_f ;
70  * if at end of header returns pointer to the last crlf  (always buf)*/
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                 LOG(L_ERR, "ERROR: get_hdr_field: bad header\n");
97                 goto error;
98         }
99
100         /* eliminate leading whitespace */
101         tmp=eat_lws_end(tmp, end);
102         if (tmp>=end) {
103                 LOG(L_ERR, "ERROR: get_hdr_field: 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                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
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                                 LOG(L_ERR, "ERROR: get_hdr_field: 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                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
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                                 LOG(L_ERR, "ERROR: get_hdr_field: 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("get_hdr_field: cseq <%.*s>: <%.*s> <%.*s>\n",
151                                         hdr->name.len, ZSW(hdr->name.s),
152                                         cseq_b->number.len, ZSW(cseq_b->number.s),
153                                         cseq_b->method.len, cseq_b->method.s);
154                         break;
155                 case HDR_TO_T:
156                         to_b=pkg_malloc(sizeof(struct to_body));
157                         if (to_b==0){
158                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
159                                 goto error;
160                         }
161                         memset(to_b, 0, sizeof(struct to_body));
162                         hdr->body.s=tmp;
163                         tmp=parse_to(tmp, end,to_b);
164                         if (to_b->error==PARSE_ERROR){
165                                 LOG(L_ERR, "ERROR: get_hdr_field: bad to header\n");
166                                 free_to(to_b);
167                                 goto error;
168                         }
169                         hdr->parsed=to_b;
170                         hdr->body.len=tmp-hdr->body.s;
171                         DBG("DEBUG: get_hdr_field: <%.*s> [%d]; uri=[%.*s] \n",
172                                 hdr->name.len, ZSW(hdr->name.s),
173                                 hdr->body.len, to_b->uri.len,ZSW(to_b->uri.s));
174                         DBG("DEBUG: to body [%.*s]\n",to_b->body.len,
175                                 ZSW(to_b->body.s));
176                         break;
177                 case HDR_CONTENTLENGTH_T:
178                         hdr->body.s=tmp;
179                         tmp=parse_content_length(tmp,end, &integer);
180                         if (tmp==0){
181                                 LOG(L_ERR, "ERROR:get_hdr_field: bad content_length header\n");
182                                 goto error;
183                         }
184                         hdr->parsed=(void*)(long)integer;
185                         hdr->body.len=tmp-hdr->body.s;
186                         DBG("DEBUG: get_hdr_body : content_length=%d\n",
187                                         (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                                 LOG(L_ERR, "ERROR:get_hdr_field: 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("DEBUG: get_hdr_body : retry_after=%d\n",
199                                         (unsigned)(long)hdr->parsed);
200                         break;
201                 case HDR_IDENTITY_T:
202                 case HDR_DATE_T:
203                 case HDR_IDENTITY_INFO_T:
204                 case HDR_SUPPORTED_T:
205                 case HDR_REQUIRE_T:
206                 case HDR_CONTENTTYPE_T:
207                 case HDR_FROM_T:
208                 case HDR_CALLID_T:
209                 case HDR_CONTACT_T:
210                 case HDR_ROUTE_T:
211                 case HDR_RECORDROUTE_T:
212                 case HDR_MAXFORWARDS_T:
213                 case HDR_AUTHORIZATION_T:
214                 case HDR_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_OTHER_T:
249                         /* just skip over it */
250                         hdr->body.s=tmp;
251                         /* find end of header */
252                         /* find lf */
253                         do{
254                                 match=q_memchr(tmp, '\n', end-tmp);
255                                 if (match){
256                                         match++;
257                                 }else {
258                                         LOG(L_ERR,
259                                                         "ERROR: get_hdr_field: bad body for <%s>(%d)\n",
260                                                         hdr->name.s, hdr->type);
261                                         /* abort(); */
262                                         tmp=end;
263                                         goto error;
264                                 }
265                                 tmp=match;
266                         }while( match<end &&( (*match==' ')||(*match=='\t') ) );
267                         tmp=match;
268                         hdr->body.len=match-hdr->body.s;
269                         break;
270                 default:
271                         LOG(L_CRIT, "BUG: get_hdr_field: unknown header type %d\n",
272                                         hdr->type);
273                         goto error;
274         }
275         /* jku: if \r covered by current length, shrink it */
276         trim_r( hdr->body );
277         hdr->len=tmp-hdr->name.s;
278         return tmp;
279 error:
280         DBG("get_hdr_field: error exit\n");
281         STATS_BAD_MSG_HDR();
282         hdr->type=HDR_ERROR_T;
283         hdr->len=tmp-hdr->name.s;
284         return tmp;
285 }
286
287
288
289 /* parse the headers and adds them to msg->headers and msg->to, from etc.
290  * It stops when all the headers requested in flags were parsed, on error
291  * (bad header) or end of headers
292  * WARNING: parse_headers was changed to use hdr_flags_t (the flags are now
293  *          different from the header types). Don't call it with a header type
294  *          (HDR_xxx_T), only with header flags (HDR_xxx_F)!*/
295 /* note: it continues where it previously stopped and goes ahead until
296    end is encountered or desired HFs are found; if you call it twice
297    for the same HF which is present only once, it will fail the second
298    time; if you call it twice and the HF is found on second time too,
299    it's not replaced in the well-known HF pointer but just added to
300    header list; if you want to use a dumb convenience function which will
301    give you the first occurrence of a header you are interested in,
302    look at check_transaction_quadruple
303 */
304 int parse_headers(struct sip_msg* const msg, const hdr_flags_t flags, const int next)
305 {
306         struct hdr_field* hf;
307         char* tmp;
308         char* rest;
309         char* end;
310         hdr_flags_t orig_flag;
311
312         end=msg->buf+msg->len;
313         tmp=msg->unparsed;
314
315         if (unlikely(next)) {
316                 orig_flag = msg->parsed_flag;
317                 msg->parsed_flag &= ~flags;
318         }else
319                 orig_flag=0;
320
321 #ifdef EXTRA_DEBUG
322         DBG("parse_headers: flags=%llx\n", (unsigned long long)flags);
323 #endif
324         while( tmp<end && (flags & msg->parsed_flag) != flags){
325                 prefetch_loc_r(tmp+64, 1);
326                 hf=pkg_malloc(sizeof(struct hdr_field));
327                 if (unlikely(hf==0)){
328                         ser_error=E_OUT_OF_MEM;
329                         LOG(L_ERR, "ERROR:parse_headers: memory allocation error\n");
330                         goto error;
331                 }
332                 memset(hf,0, sizeof(struct hdr_field));
333                 hf->type=HDR_ERROR_T;
334                 rest=get_hdr_field(tmp, end, hf);
335                 switch (hf->type){
336                         case HDR_ERROR_T:
337                                 LOG(L_INFO,"ERROR: bad header field [%.*s]\n",
338                                         (end-tmp>20)?20:(int)(end-tmp), tmp);
339                                 goto  error;
340                         case HDR_EOH_T:
341                                 msg->eoh=tmp; /* or rest?*/
342                                 msg->parsed_flag|=HDR_EOH_F;
343                                 pkg_free(hf);
344                                 goto skip;
345                         case HDR_ACCEPTCONTACT_T:
346                         case HDR_ALLOWEVENTS_T:
347                         case HDR_CONTENTENCODING_T:
348                         case HDR_REFERREDBY_T:
349                         case HDR_REJECTCONTACT_T:
350                         case HDR_REQUESTDISPOSITION_T:
351                         case HDR_WWW_AUTHENTICATE_T:
352                         case HDR_PROXY_AUTHENTICATE_T:
353                         case HDR_RETRY_AFTER_T:
354                         case HDR_OTHER_T: /* mark the type as found/parsed*/
355                                 msg->parsed_flag|=HDR_T2F(hf->type);
356                                 break;
357                         case HDR_CALLID_T:
358                                 if (msg->callid==0) msg->callid=hf;
359                                 msg->parsed_flag|=HDR_CALLID_F;
360                                 break;
361                         case HDR_SIPIFMATCH_T:
362                                 if (msg->sipifmatch==0) msg->sipifmatch=hf;
363                                 msg->parsed_flag|=HDR_SIPIFMATCH_F;
364                                 break;
365                         case HDR_TO_T:
366                                 if (msg->to==0) msg->to=hf;
367                                 msg->parsed_flag|=HDR_TO_F;
368                                 break;
369                         case HDR_CSEQ_T:
370                                 if (msg->cseq==0) msg->cseq=hf;
371                                 msg->parsed_flag|=HDR_CSEQ_F;
372                                 break;
373                         case HDR_FROM_T:
374                                 if (msg->from==0) msg->from=hf;
375                                 msg->parsed_flag|=HDR_FROM_F;
376                                 break;
377                         case HDR_CONTACT_T:
378                                 if (msg->contact==0) msg->contact=hf;
379                                 msg->parsed_flag|=HDR_CONTACT_F;
380                                 break;
381                         case HDR_MAXFORWARDS_T:
382                                 if(msg->maxforwards==0) msg->maxforwards=hf;
383                                 msg->parsed_flag|=HDR_MAXFORWARDS_F;
384                                 break;
385                         case HDR_ROUTE_T:
386                                 if (msg->route==0) msg->route=hf;
387                                 msg->parsed_flag|=HDR_ROUTE_F;
388                                 break;
389                         case HDR_RECORDROUTE_T:
390                                 if (msg->record_route==0) msg->record_route = hf;
391                                 msg->parsed_flag|=HDR_RECORDROUTE_F;
392                                 break;
393                         case HDR_CONTENTTYPE_T:
394                                 if (msg->content_type==0) msg->content_type = hf;
395                                 msg->parsed_flag|=HDR_CONTENTTYPE_F;
396                                 break;
397                         case HDR_CONTENTLENGTH_T:
398                                 if (msg->content_length==0) msg->content_length = hf;
399                                 msg->parsed_flag|=HDR_CONTENTLENGTH_F;
400                                 break;
401                         case HDR_AUTHORIZATION_T:
402                                 if (msg->authorization==0) msg->authorization = hf;
403                                 msg->parsed_flag|=HDR_AUTHORIZATION_F;
404                                 break;
405                         case HDR_EXPIRES_T:
406                                 if (msg->expires==0) msg->expires = hf;
407                                 msg->parsed_flag|=HDR_EXPIRES_F;
408                                 break;
409                         case HDR_PROXYAUTH_T:
410                                 if (msg->proxy_auth==0) msg->proxy_auth = hf;
411                                 msg->parsed_flag|=HDR_PROXYAUTH_F;
412                                 break;
413                         case HDR_PROXYREQUIRE_T:
414                                 if (msg->proxy_require==0) msg->proxy_require = hf;
415                                 msg->parsed_flag|=HDR_PROXYREQUIRE_F;
416                                 break;
417                         case HDR_SUPPORTED_T:
418                                 if (msg->supported==0) msg->supported=hf;
419                                 msg->parsed_flag|=HDR_SUPPORTED_F;
420                                 break;
421                         case HDR_REQUIRE_T:
422                                 if (msg->require==0) msg->require=hf;
423                                 msg->parsed_flag|=HDR_REQUIRE_F;
424                                 break;
425                         case HDR_UNSUPPORTED_T:
426                                 if (msg->unsupported==0) msg->unsupported=hf;
427                                 msg->parsed_flag|=HDR_UNSUPPORTED_F;
428                                 break;
429                         case HDR_ALLOW_T:
430                                 if (msg->allow==0) msg->allow = hf;
431                                 msg->parsed_flag|=HDR_ALLOW_F;
432                                 break;
433                         case HDR_EVENT_T:
434                                 if (msg->event==0) msg->event = hf;
435                                 msg->parsed_flag|=HDR_EVENT_F;
436                                 break;
437                         case HDR_ACCEPT_T:
438                                 if (msg->accept==0) msg->accept = hf;
439                                 msg->parsed_flag|=HDR_ACCEPT_F;
440                                 break;
441                         case HDR_ACCEPTLANGUAGE_T:
442                                 if (msg->accept_language==0) msg->accept_language = hf;
443                                 msg->parsed_flag|=HDR_ACCEPTLANGUAGE_F;
444                                 break;
445                         case HDR_ORGANIZATION_T:
446                                 if (msg->organization==0) msg->organization = hf;
447                                 msg->parsed_flag|=HDR_ORGANIZATION_F;
448                                 break;
449                         case HDR_PRIORITY_T:
450                                 if (msg->priority==0) msg->priority = hf;
451                                 msg->parsed_flag|=HDR_PRIORITY_F;
452                                 break;
453                         case HDR_SUBJECT_T:
454                                 if (msg->subject==0) msg->subject = hf;
455                                 msg->parsed_flag|=HDR_SUBJECT_F;
456                                 break;
457                         case HDR_USERAGENT_T:
458                                 if (msg->user_agent==0) msg->user_agent = hf;
459                                 msg->parsed_flag|=HDR_USERAGENT_F;
460                                 break;
461                         case HDR_SERVER_T:
462                                 if (msg->server==0) msg->server = hf;
463                                 msg->parsed_flag|=HDR_SERVER_F;
464                                 break;
465                         case HDR_CONTENTDISPOSITION_T:
466                                 if (msg->content_disposition==0) msg->content_disposition = hf;
467                                 msg->parsed_flag|=HDR_CONTENTDISPOSITION_F;
468                                 break;
469                         case HDR_DIVERSION_T:
470                                 if (msg->diversion==0) msg->diversion = hf;
471                                 msg->parsed_flag|=HDR_DIVERSION_F;
472                                 break;
473                         case HDR_RPID_T:
474                                 if (msg->rpid==0) msg->rpid = hf;
475                                 msg->parsed_flag|=HDR_RPID_F;
476                                 break;
477                         case HDR_REFER_TO_T:
478                                 if (msg->refer_to==0) msg->refer_to = hf;
479                                 msg->parsed_flag|=HDR_REFER_TO_F;
480                                 break;
481                         case HDR_SESSIONEXPIRES_T:
482                                 if (msg->session_expires==0) msg->session_expires = hf;
483                                 msg->parsed_flag|=HDR_SESSIONEXPIRES_F;
484                                 break;
485                         case HDR_MIN_SE_T:
486                                 if (msg->min_se==0) msg->min_se = hf;
487                                 msg->parsed_flag|=HDR_MIN_SE_F;
488                                 break;
489                         case HDR_SUBSCRIPTION_STATE_T:
490                                 if (msg->subscription_state==0) msg->subscription_state = hf;
491                                 msg->parsed_flag|=HDR_SUBSCRIPTION_STATE_F;
492                                 break;
493                         case HDR_VIA_T:
494                                 msg->parsed_flag|=HDR_VIA_F;
495                                 DBG("parse_headers: Via found, flags=%llx\n",
496                                                 (unsigned long long)flags);
497                                 if (msg->via1==0) {
498                                         DBG("parse_headers: this is the first via\n");
499                                         msg->h_via1=hf;
500                                         msg->via1=hf->parsed;
501                                         if (msg->via1->next){
502                                                 msg->via2=msg->via1->next;
503                                                 msg->parsed_flag|=HDR_VIA2_F;
504                                         }
505                                 }else if (msg->via2==0){
506                                         msg->h_via2=hf;
507                                         msg->via2=hf->parsed;
508                                         msg->parsed_flag|=HDR_VIA2_F;
509                                         DBG("parse_headers: this is the second via\n");
510                                 }
511                                 break;
512                         case HDR_DATE_T:
513                                 if (msg->date==0) msg->date=hf;
514                                 msg->parsed_flag|=HDR_DATE_F;
515                                 break;
516                         case HDR_IDENTITY_T:
517                                 if (msg->identity==0) msg->identity=hf;
518                                 msg->parsed_flag|=HDR_IDENTITY_F;
519                                 break;
520                         case HDR_IDENTITY_INFO_T:
521                                 if (msg->identity_info==0) msg->identity_info=hf;
522                                 msg->parsed_flag|=HDR_IDENTITY_INFO_F;
523                                 break;
524                     case HDR_PATH_T:
525                                 if (msg->path==0) msg->path=hf;
526                                 msg->parsed_flag|=HDR_PATH_F;
527                                 break;
528                     case HDR_PRIVACY_T:
529                                 if (msg->privacy==0) msg->privacy=hf;
530                                 msg->parsed_flag|=HDR_PRIVACY_F;
531                                 break;
532                     case HDR_PAI_T:
533                                 if (msg->pai==0) msg->pai=hf;
534                                 msg->parsed_flag|=HDR_PAI_F;
535                                 break;
536                     case HDR_PPI_T:
537                                 if (msg->ppi==0) msg->ppi=hf;
538                                 msg->parsed_flag|=HDR_PPI_F;
539                                 break;
540                     case HDR_REASON_T:
541                                 msg->parsed_flag|=HDR_REASON_F;
542                                 break;
543                         default:
544                                 LOG(L_CRIT, "BUG: parse_headers: unknown header type %d\n",
545                                                         hf->type);
546                                 goto error;
547                 }
548                 /* add the header to the list*/
549                 if (msg->last_header==0){
550                         msg->headers=hf;
551                         msg->last_header=hf;
552                 }else{
553                         msg->last_header->next=hf;
554                         msg->last_header=hf;
555                 }
556 #ifdef EXTRA_DEBUG
557                 DBG("header field type %d, name=<%.*s>, body=<%.*s>\n",
558                         hf->type,
559                         hf->name.len, ZSW(hf->name.s),
560                         hf->body.len, ZSW(hf->body.s));
561 #endif
562                 tmp=rest;
563         }
564 skip:
565         msg->unparsed=tmp;
566         /* restore original flags */
567         msg->parsed_flag |= orig_flag;
568         return 0;
569
570 error:
571         ser_error=E_BAD_REQ;
572         if (hf) pkg_free(hf);
573         /* restore original flags */
574         msg->parsed_flag |= orig_flag;
575         return -1;
576 }
577
578
579
580
581
582 /* returns 0 if ok, -1 for errors */
583 int parse_msg(char* const buf, const unsigned int len, struct sip_msg* const msg)
584 {
585
586         char *tmp;
587         char* rest;
588         struct msg_start *fl;
589         int offset;
590         hdr_flags_t flags;
591
592         /* eat crlf from the beginning */
593         for (tmp=buf; (*tmp=='\n' || *tmp=='\r')&&
594                         tmp-buf < len ; tmp++);
595         offset=tmp-buf;
596         fl=&(msg->first_line);
597         rest=parse_first_line(tmp, len-offset, fl);
598         offset+=rest-tmp;
599         tmp=rest;
600         switch(fl->type){
601                 case SIP_INVALID:
602                         DBG("parse_msg: invalid message\n");
603                         goto error;
604                         break;
605                 case SIP_REQUEST:
606                         DBG("SIP Request:\n");
607                         DBG(" method:  <%.*s>\n",fl->u.request.method.len,
608                                 ZSW(fl->u.request.method.s));
609                         DBG(" uri:     <%.*s>\n",fl->u.request.uri.len,
610                                 ZSW(fl->u.request.uri.s));
611                         DBG(" version: <%.*s>\n",fl->u.request.version.len,
612                                 ZSW(fl->u.request.version.s));
613                         flags=HDR_VIA_F;
614                         break;
615                 case SIP_REPLY:
616                         DBG("SIP Reply  (status):\n");
617                         DBG(" version: <%.*s>\n",fl->u.reply.version.len,
618                                         ZSW(fl->u.reply.version.s));
619                         DBG(" status:  <%.*s>\n", fl->u.reply.status.len,
620                                         ZSW(fl->u.reply.status.s));
621                         DBG(" reason:  <%.*s>\n", fl->u.reply.reason.len,
622                                         ZSW(fl->u.reply.reason.s));
623                         /* flags=HDR_VIA | HDR_VIA2; */
624                         /* we don't try to parse VIA2 for local messages; -Jiri */
625                         flags=HDR_VIA_F;
626                         break;
627                 default:
628                         DBG("unknown type %d\n",fl->type);
629                         goto error;
630         }
631         msg->unparsed=tmp;
632         /*find first Via: */
633         if (parse_headers(msg, flags, 0)==-1) goto error;
634
635 #ifdef EXTRA_DEBUG
636         /* dump parsed data */
637         if (msg->via1){
638                 DBG("first via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
639                         msg->via1->name.len,
640                         ZSW(msg->via1->name.s),
641                         msg->via1->version.len,
642                         ZSW(msg->via1->version.s),
643                         msg->via1->transport.len,
644                         ZSW(msg->via1->transport.s),
645                         msg->via1->host.len,
646                         ZSW(msg->via1->host.s),
647                         msg->via1->port_str.len,
648                         ZSW(msg->via1->port_str.s),
649                         msg->via1->port);
650                 if (msg->via1->params.s)  DBG(";<%.*s>",
651                                 msg->via1->params.len, ZSW(msg->via1->params.s));
652                 if (msg->via1->comment.s)
653                                 DBG(" <%.*s>",
654                                         msg->via1->comment.len, ZSW(msg->via1->comment.s));
655                 DBG ("\n");
656         }
657         if (msg->via2){
658                 DBG("second via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
659                         msg->via2->name.len,
660                         ZSW(msg->via2->name.s),
661                         msg->via2->version.len,
662                         ZSW(msg->via2->version.s),
663                         msg->via2->transport.len,
664                         ZSW(msg->via2->transport.s),
665                         msg->via2->host.len,
666                         ZSW(msg->via2->host.s),
667                         msg->via2->port_str.len,
668                         ZSW(msg->via2->port_str.s),
669                         msg->via2->port);
670                 if (msg->via2->params.s)  DBG(";<%.*s>",
671                                 msg->via2->params.len, ZSW(msg->via2->params.s));
672                 if (msg->via2->comment.s) DBG(" <%.*s>",
673                                 msg->via2->comment.len, ZSW(msg->via2->comment.s));
674                 DBG ("\n");
675         }
676 #endif
677
678
679 #ifdef EXTRA_DEBUG
680         DBG("exiting parse_msg\n");
681 #endif
682
683         return 0;
684
685 error:
686         /* more debugging, msg->orig is/should be null terminated*/
687         LOG(cfg_get(core, core_cfg, corelog), "ERROR: parse_msg: message=<%.*s>\n",
688                         (int)msg->len, ZSW(msg->buf));
689         return -1;
690 }
691
692
693
694 void free_reply_lump( struct lump_rpl *lump)
695 {
696         struct lump_rpl *foo, *bar;
697         for(foo=lump;foo;)
698         {
699                 bar=foo->next;
700                 free_lump_rpl(foo);
701                 foo = bar;
702         }
703 }
704
705
706 /*only the content*/
707 void free_sip_msg(struct sip_msg* const msg)
708 {
709         reset_new_uri(msg);
710         reset_dst_uri(msg);
711         reset_path_vector(msg);
712         reset_instance(msg);
713         reset_ruid(msg);
714         reset_ua(msg);
715         if (msg->headers)     free_hdr_field_lst(msg->headers);
716         if (msg->body && msg->body->free) msg->body->free(&msg->body);
717         if (msg->add_rm)      free_lump_list(msg->add_rm);
718         if (msg->body_lumps)  free_lump_list(msg->body_lumps);
719         if (msg->reply_lump)   free_reply_lump(msg->reply_lump);
720         msg_ldata_reset(msg);
721         /* don't free anymore -- now a pointer to a static buffer */
722 #       ifdef DYN_BUF
723         pkg_free(msg->buf);
724 #       endif
725 }
726
727 /**
728  * reset new uri value
729  */
730 void reset_new_uri(struct sip_msg* const msg)
731 {
732         if(msg->new_uri.s != 0) {
733                 pkg_free(msg->new_uri.s);
734         }
735         msg->new_uri.s = 0;
736         msg->new_uri.len = 0;
737         msg->parsed_uri_ok = 0;
738 }
739
740
741 /*
742  * Make a private copy of the string and assign it to dst_uri
743  */
744 int set_dst_uri(struct sip_msg* const msg, const str* const uri)
745 {
746         char* ptr;
747
748         if (unlikely(!msg || !uri)) {
749                 LOG(L_ERR, "set_dst_uri: Invalid parameter value\n");
750                 return -1;
751         }
752
753         if (unlikely(uri->len == 0)) {
754                 reset_dst_uri(msg);
755         }else if (msg->dst_uri.s && (msg->dst_uri.len >= uri->len)) {
756                 memcpy(msg->dst_uri.s, uri->s, uri->len);
757                 msg->dst_uri.len = uri->len;
758         } else {
759                 ptr = (char*)pkg_malloc(uri->len);
760                 if (!ptr) {
761                         LOG(L_ERR, "set_dst_uri: Not enough memory\n");
762                         return -1;
763                 }
764
765                 memcpy(ptr, uri->s, uri->len);
766                 if (msg->dst_uri.s) pkg_free(msg->dst_uri.s);
767                 msg->dst_uri.s = ptr;
768                 msg->dst_uri.len = uri->len;
769         }
770         return 0;
771 }
772
773
774 void reset_dst_uri(struct sip_msg* const msg)
775 {
776         if(msg->dst_uri.s != 0) {
777                 pkg_free(msg->dst_uri.s);
778         }
779         msg->dst_uri.s = 0;
780         msg->dst_uri.len = 0;
781 }
782
783 int set_path_vector(struct sip_msg* msg, str* path)
784 {
785         char* ptr;
786
787         if (unlikely(!msg || !path)) {
788                 LM_ERR("invalid parameter value\n");
789                 return -1;
790         }
791
792         if (unlikely(path->len == 0)) {
793                 reset_path_vector(msg);
794         } else if (msg->path_vec.s && (msg->path_vec.len >= path->len)) {
795                 memcpy(msg->path_vec.s, path->s, path->len);
796                 msg->path_vec.len = path->len;
797         } else {
798                 ptr = (char*)pkg_malloc(path->len);
799                 if (!ptr) {
800                         LM_ERR("not enough pkg memory\n");
801                         return -1;
802                 }
803
804                 memcpy(ptr, path->s, path->len);
805                 if (msg->path_vec.s) pkg_free(msg->path_vec.s);
806                 msg->path_vec.s = ptr;
807                 msg->path_vec.len = path->len;
808         }
809         return 0;
810 }
811
812
813 void reset_path_vector(struct sip_msg* const msg)
814 {
815         /* only free path vector from pkg IFF it is still in pkg... - ie. if msg is shm we don't free... */
816         if (!(msg->msg_flags&FL_SHM_CLONE)) {
817                 if (msg->path_vec.s)
818                         pkg_free(msg->path_vec.s);
819                 msg->path_vec.s = 0;
820                 msg->path_vec.len = 0;
821         }
822 }
823
824
825 int set_instance(struct sip_msg* msg, str* instance)
826 {
827         char* ptr;
828
829         if (unlikely(!msg || !instance)) {
830                 LM_ERR("invalid instance parameter value\n");
831                 return -1;
832         }
833
834         if (unlikely(instance->len == 0)) {
835                 reset_instance(msg);
836         } else if (msg->instance.s && (msg->instance.len >= instance->len)) {
837                 memcpy(msg->instance.s, instance->s, instance->len);
838                 msg->instance.len = instance->len;
839         } else {
840                 ptr = (char*)pkg_malloc(instance->len);
841                 if (!ptr) {
842                         LM_ERR("not enough pkg memory for instance\n");
843                         return -1;
844                 }
845                 memcpy(ptr, instance->s, instance->len);
846                 if (msg->instance.s) pkg_free(msg->instance.s);
847                 msg->instance.s = ptr;
848                 msg->instance.len = instance->len;
849         }
850         return 0;
851 }
852
853
854 void reset_instance(struct sip_msg* const msg)
855 {
856         if(msg->instance.s != 0) {
857                 pkg_free(msg->instance.s);
858         }
859         msg->instance.s = 0;
860         msg->instance.len = 0;
861 }
862
863
864 int set_ruid(struct sip_msg* msg, str* ruid)
865 {
866         char* ptr;
867
868         if (unlikely(!msg || !ruid)) {
869                 LM_ERR("invalid ruid parameter value\n");
870                 return -1;
871         }
872
873         if (unlikely(ruid->len == 0)) {
874                 reset_ruid(msg);
875         } else if (msg->ruid.s && (msg->ruid.len >= ruid->len)) {
876                 memcpy(msg->ruid.s, ruid->s, ruid->len);
877                 msg->ruid.len = ruid->len;
878         } else {
879                 ptr = (char*)pkg_malloc(ruid->len);
880                 if (!ptr) {
881                         LM_ERR("not enough pkg memory for ruid\n");
882                         return -1;
883                 }
884                 memcpy(ptr, ruid->s, ruid->len);
885                 if (msg->ruid.s) pkg_free(msg->ruid.s);
886                 msg->ruid.s = ptr;
887                 msg->ruid.len = ruid->len;
888         }
889         return 0;
890 }
891
892
893 void reset_ruid(struct sip_msg* const msg)
894 {
895         if(msg->ruid.s != 0) {
896                 pkg_free(msg->ruid.s);
897         }
898         msg->ruid.s = 0;
899         msg->ruid.len = 0;
900 }
901
902
903 int set_ua(struct sip_msg* msg, str* location_ua)
904 {
905         char* ptr;
906
907         if (unlikely(!msg || !location_ua)) {
908                 LM_ERR("invalid location_ua parameter value\n");
909                 return -1;
910         }
911
912         if (unlikely(location_ua->len == 0)) {
913                 reset_ua(msg);
914         } else if (msg->location_ua.s && (msg->location_ua.len >= location_ua->len)) {
915                 memcpy(msg->location_ua.s, location_ua->s, location_ua->len);
916                 msg->location_ua.len = location_ua->len;
917         } else {
918                 ptr = (char*)pkg_malloc(location_ua->len);
919                 if (!ptr) {
920                         LM_ERR("not enough pkg memory for location_ua\n");
921                         return -1;
922                 }
923                 memcpy(ptr, location_ua->s, location_ua->len);
924                 if (msg->location_ua.s) pkg_free(msg->location_ua.s);
925                 msg->location_ua.s = ptr;
926                 msg->location_ua.len = location_ua->len;
927         }
928         return 0;
929 }
930
931
932 void reset_ua(struct sip_msg* const msg)
933 {
934         if(msg->location_ua.s != 0) {
935                 pkg_free(msg->location_ua.s);
936         }
937         msg->location_ua.s = 0;
938         msg->location_ua.len = 0;
939 }
940
941 /**
942  * reset content of msg->ldv (msg_ldata_t structure)
943  */
944 void msg_ldata_reset(sip_msg_t *msg)
945 {
946         if(msg==NULL)
947                 return;
948         memset(&msg->ldv, 0, sizeof(msg_ldata_t));
949 }
950
951
952 hdr_field_t* get_hdr(const sip_msg_t* const msg, const enum _hdr_types_t ht)
953 {
954         hdr_field_t *hdr;
955
956         if (msg->parsed_flag & HDR_T2F(ht))
957                 for(hdr = msg->headers; hdr; hdr = hdr->next) {
958                         if(hdr->type == ht) return hdr;
959                 }
960         return NULL;
961 }
962
963
964 hdr_field_t* next_sibling_hdr(const hdr_field_t* const hf)
965 {
966         hdr_field_t *hdr;
967
968         for(hdr = hf->next; hdr; hdr = hdr->next) {
969                 if(hdr->type == hf->type) return hdr;
970         }
971         return NULL;
972 }
973
974 hdr_field_t* get_hdr_by_name(const sip_msg_t* const msg, const char* const name, const int name_len)
975 {
976         hdr_field_t *hdr;
977
978         for(hdr = msg->headers; hdr; hdr = hdr->next) {
979                 if(hdr->name.len == name_len && *hdr->name.s==*name
980                                 && strncmp(hdr->name.s, name, name_len)==0)
981                         return hdr;
982         }
983         return NULL;
984 }
985
986 /** not used yet */
987 hdr_field_t* next_sibling_hdr_by_name(const hdr_field_t* const hf)
988 {
989         hdr_field_t *hdr;
990
991         for(hdr = hf->next; hdr; hdr = hdr->next) {
992                 if(hdr->name.len == hf->name.len && *hdr->name.s==*hf->name.s
993                                 && strncmp(hdr->name.s, hf->name.s, hf->name.len)==0)
994                         return hdr;
995         }
996         return NULL;
997 }
998
999 /**
1000  * set msg context id
1001  * - return: -1 on error; 0 - on set
1002  */
1003 int msg_ctx_id_set(const sip_msg_t* const msg, msg_ctx_id_t* const mid)
1004 {
1005         if(msg==NULL || mid==NULL)
1006                 return -1;
1007         mid->msgid = msg->id;
1008         mid->pid = msg->pid;
1009         return 0;
1010 }
1011
1012 /**
1013  * check msg context id
1014  * - return: -1 on error; 0 - on no match; 1 - on match
1015  */
1016 int msg_ctx_id_match(const sip_msg_t* const msg, const msg_ctx_id_t* const mid)
1017 {
1018         if(msg==NULL || mid==NULL)
1019                 return -1;
1020         if(msg->id != mid->msgid || msg->pid!=mid->pid)
1021                 return 0;
1022         return 1;
1023 }
1024
1025 /**
1026  * set msg time value
1027  */
1028 int msg_set_time(sip_msg_t* const msg)
1029 {
1030         if(unlikely(msg==NULL))
1031                 return -2;
1032         if(msg->tval.tv_sec!=0)
1033                 return 0;
1034         return gettimeofday(&msg->tval, NULL);
1035 }