01bb40e07a64dd4e1f9a601c89b90744abb27907
[sip-router] / parser / msg_parser.c
1 /*
2  * $Id$
3  *
4  * sip msg. header proxy parser
5  *
6  *
7  * Copyright (C) 2001-2003 FhG Fokus
8  *
9  * This file is part of ser, a free SIP server.
10  *
11  * ser is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version
15  *
16  * For a license to use the ser software under conditions
17  * other than those described here, or to purchase support for this
18  * software, please contact iptel.org by e-mail at the following addresses:
19  *    info@iptel.org
20  *
21  * ser is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29  *
30  * History:
31  * ---------
32  *  2003-02-28  scratchpad compatibility abandoned (jiri)
33  *  2003-01-29  scrathcpad removed (jiri)
34  *  2003-01-27  next baby-step to removing ZT - PRESERVE_ZT (jiri)
35  *  2003-03-31  removed msg->repl_add_rm (andrei)
36  *  2003-04-26 ZSW (jiri)
37  *  2003-05-01  parser extended to support Accept header field (janakj)
38  *  2005-02-23  parse_headers uses hdr_flags_t now (andrei)
39  *  2005-03-02  free_via_list(vb) on via parse error (andrei)
40  *  2007-01-26  parser extended to support Identity, Identity-info and Date
41  *              header fields (gergo)
42  */
43
44 /** Parser :: SIP Message header proxy parser.
45  * @file
46  * @ingroup parser
47  */
48
49 /*! \defgroup parser SIP-router SIP message parser
50  * 
51  * The SIP message parser
52  *
53  */
54
55
56 #include <string.h>
57 #include <stdlib.h>
58
59 #include "../comp_defs.h"
60 #include "msg_parser.h"
61 #include "parser_f.h"
62 #include "../ut.h"
63 #include "../error.h"
64 #include "../dprint.h"
65 #include "../data_lump_rpl.h"
66 #include "../mem/mem.h"
67 #include "../error.h"
68 #include "../core_stats.h"
69 #include "../globals.h"
70 #include "parse_hname2.h"
71 #include "parse_uri.h"
72 #include "parse_content.h"
73 #include "parse_to.h"
74 #include "../compiler_opt.h"
75
76 #ifdef DEBUG_DMALLOC
77 #include <mem/dmalloc.h>
78 #endif
79
80
81 #define parse_hname(_b,_e,_h) parse_hname2((_b),(_e),(_h))
82
83 /* number of via's encountered */
84 int via_cnt;
85 /* global request flags */
86 unsigned int global_req_flags = 0;
87
88 /* returns pointer to next header line, and fill hdr_f ;
89  * if at end of header returns pointer to the last crlf  (always buf)*/
90 char* get_hdr_field(char* buf, char* end, struct hdr_field* hdr)
91 {
92
93         char* tmp;
94         char *match;
95         struct via_body *vb;
96         struct cseq_body* cseq_b;
97         struct to_body* to_b;
98         int integer, err;
99         unsigned uval;
100
101         if ((*buf)=='\n' || (*buf)=='\r'){
102                 /* double crlf or lflf or crcr */
103                 DBG("found end of header\n");
104                 hdr->type=HDR_EOH_T;
105                 return buf;
106         }
107
108         tmp=parse_hname(buf, end, hdr);
109         if (hdr->type==HDR_ERROR_T){
110                 LOG(L_ERR, "ERROR: get_hdr_field: bad header\n");
111                 goto error;
112         }
113
114         /* eliminate leading whitespace */
115         tmp=eat_lws_end(tmp, end);
116         if (tmp>=end) {
117                 LOG(L_ERR, "ERROR: get_hdr_field: HF empty\n");
118                 goto error;
119         }
120
121         /* if header-field well-known, parse it, find its end otherwise ;
122          * after leaving the hdr->type switch, tmp should be set to the
123          * next header field
124          */
125         switch(hdr->type){
126                 case HDR_VIA_T:
127                         /* keep number of vias parsed -- we want to report it in
128                            replies for diagnostic purposes */
129                         via_cnt++;
130                         vb=pkg_malloc(sizeof(struct via_body));
131                         if (vb==0){
132                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
133                                 goto error;
134                         }
135                         memset(vb,0,sizeof(struct via_body));
136                         hdr->body.s=tmp;
137                         tmp=parse_via(tmp, end, vb);
138                         if (vb->error==PARSE_ERROR){
139                                 LOG(L_ERR, "ERROR: get_hdr_field: bad via\n");
140                                 free_via_list(vb);
141                                 goto error;
142                         }
143                         hdr->parsed=vb;
144                         vb->hdr.s=hdr->name.s;
145                         vb->hdr.len=hdr->name.len;
146                         hdr->body.len=tmp-hdr->body.s;
147                         break;
148                 case HDR_CSEQ_T:
149                         cseq_b=pkg_malloc(sizeof(struct cseq_body));
150                         if (cseq_b==0){
151                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
152                                 goto error;
153                         }
154                         memset(cseq_b, 0, sizeof(struct cseq_body));
155                         hdr->body.s=tmp;
156                         tmp=parse_cseq(tmp, end, cseq_b);
157                         if (cseq_b->error==PARSE_ERROR){
158                                 LOG(L_ERR, "ERROR: get_hdr_field: bad cseq\n");
159                                 pkg_free(cseq_b);
160                                 goto error;
161                         }
162                         hdr->parsed=cseq_b;
163                         hdr->body.len=tmp-hdr->body.s;
164                         DBG("get_hdr_field: cseq <%.*s>: <%.*s> <%.*s>\n",
165                                         hdr->name.len, ZSW(hdr->name.s),
166                                         cseq_b->number.len, ZSW(cseq_b->number.s),
167                                         cseq_b->method.len, cseq_b->method.s);
168                         break;
169                 case HDR_TO_T:
170                         to_b=pkg_malloc(sizeof(struct to_body));
171                         if (to_b==0){
172                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
173                                 goto error;
174                         }
175                         memset(to_b, 0, sizeof(struct to_body));
176                         hdr->body.s=tmp;
177                         tmp=parse_to(tmp, end,to_b);
178                         if (to_b->error==PARSE_ERROR){
179                                 LOG(L_ERR, "ERROR: get_hdr_field: bad to header\n");
180                                 pkg_free(to_b);
181                                 goto error;
182                         }
183                         hdr->parsed=to_b;
184                         hdr->body.len=tmp-hdr->body.s;
185                         DBG("DEBUG: get_hdr_field: <%.*s> [%d]; uri=[%.*s] \n",
186                                 hdr->name.len, ZSW(hdr->name.s),
187                                 hdr->body.len, to_b->uri.len,ZSW(to_b->uri.s));
188                         DBG("DEBUG: to body [%.*s]\n",to_b->body.len,
189                                 ZSW(to_b->body.s));
190                         break;
191                 case HDR_CONTENTLENGTH_T:
192                         hdr->body.s=tmp;
193                         tmp=parse_content_length(tmp,end, &integer);
194                         if (tmp==0){
195                                 LOG(L_ERR, "ERROR:get_hdr_field: bad content_length header\n");
196                                 goto error;
197                         }
198                         hdr->parsed=(void*)(long)integer;
199                         hdr->body.len=tmp-hdr->body.s;
200                         DBG("DEBUG: get_hdr_body : content_length=%d\n",
201                                         (int)(long)hdr->parsed);
202                         break;
203                 case HDR_RETRY_AFTER_T:
204                         hdr->body.s=tmp;
205                         tmp=parse_retry_after(tmp,end, &uval, &err);
206                         if (err){
207                                 LOG(L_ERR, "ERROR:get_hdr_field: bad retry_after header\n");
208                                 goto error;
209                         }
210                         hdr->parsed=(void*)(unsigned long)uval;
211                         hdr->body.len=tmp-hdr->body.s;
212                         DBG("DEBUG: get_hdr_body : retry_after=%d\n",
213                                         (unsigned)(long)hdr->parsed);
214                         break;
215                 case HDR_IDENTITY_T:
216                 case HDR_DATE_T:
217                 case HDR_IDENTITY_INFO_T:
218                 case HDR_SUPPORTED_T:
219                 case HDR_REQUIRE_T:
220                 case HDR_CONTENTTYPE_T:
221                 case HDR_FROM_T:
222                 case HDR_CALLID_T:
223                 case HDR_CONTACT_T:
224                 case HDR_ROUTE_T:
225                 case HDR_RECORDROUTE_T:
226                 case HDR_MAXFORWARDS_T:
227                 case HDR_AUTHORIZATION_T:
228                 case HDR_EXPIRES_T:
229                 case HDR_PROXYAUTH_T:
230                 case HDR_PROXYREQUIRE_T:
231                 case HDR_UNSUPPORTED_T:
232                 case HDR_ALLOW_T:
233                 case HDR_EVENT_T:
234                 case HDR_ACCEPT_T:
235                 case HDR_ACCEPTLANGUAGE_T:
236                 case HDR_ORGANIZATION_T:
237                 case HDR_PRIORITY_T:
238                 case HDR_SUBJECT_T:
239                 case HDR_USERAGENT_T:
240                 case HDR_SERVER_T:
241                 case HDR_CONTENTDISPOSITION_T:
242                 case HDR_ACCEPTDISPOSITION_T:
243                 case HDR_DIVERSION_T:
244                 case HDR_RPID_T:
245                 case HDR_SIPIFMATCH_T:
246                 case HDR_REFER_TO_T:
247                 case HDR_SESSIONEXPIRES_T:
248                 case HDR_MIN_SE_T:
249                 case HDR_SUBSCRIPTION_STATE_T:
250                 case HDR_ACCEPTCONTACT_T:
251                 case HDR_ALLOWEVENTS_T:
252                 case HDR_CONTENTENCODING_T:
253                 case HDR_REFERREDBY_T:
254                 case HDR_REJECTCONTACT_T:
255                 case HDR_REQUESTDISPOSITION_T:
256                 case HDR_WWW_AUTHENTICATE_T:
257                 case HDR_PROXY_AUTHENTICATE_T:
258                 case HDR_PATH_T:
259                 case HDR_PRIVACY_T:
260                 case HDR_PAI_T:
261                 case HDR_PPI_T:
262                 case HDR_REASON_T:
263                 case HDR_OTHER_T:
264                         /* just skip over it */
265                         hdr->body.s=tmp;
266                         /* find end of header */
267                         /* find lf */
268                         do{
269                                 match=q_memchr(tmp, '\n', end-tmp);
270                                 if (match){
271                                         match++;
272                                 }else {
273                                         LOG(L_ERR,
274                                                         "ERROR: get_hdr_field: bad body for <%s>(%d)\n",
275                                                         hdr->name.s, hdr->type);
276                                         /* abort(); */
277                                         tmp=end;
278                                         goto error;
279                                 }
280                                 tmp=match;
281                         }while( match<end &&( (*match==' ')||(*match=='\t') ) );
282                         tmp=match;
283                         hdr->body.len=match-hdr->body.s;
284                         break;
285                 default:
286                         LOG(L_CRIT, "BUG: get_hdr_field: unknown header type %d\n",
287                                         hdr->type);
288                         goto error;
289         }
290         /* jku: if \r covered by current length, shrink it */
291         trim_r( hdr->body );
292         hdr->len=tmp-hdr->name.s;
293         return tmp;
294 error:
295         DBG("get_hdr_field: error exit\n");
296         STATS_BAD_MSG_HDR();
297         hdr->type=HDR_ERROR_T;
298         hdr->len=tmp-hdr->name.s;
299         return tmp;
300 }
301
302
303
304 /* parse the headers and adds them to msg->headers and msg->to, from etc.
305  * It stops when all the headers requested in flags were parsed, on error
306  * (bad header) or end of headers
307  * WARNING: parse_headers was changed to use hdr_flags_t (the flags are now
308  *          different from the header types). Don't call it with a header type
309  *          (HDR_xxx_T), only with header flags (HDR_xxx_F)!*/
310 /* note: it continues where it previously stopped and goes ahead until
311    end is encountered or desired HFs are found; if you call it twice
312    for the same HF which is present only once, it will fail the second
313    time; if you call it twice and the HF is found on second time too,
314    it's not replaced in the well-known HF pointer but just added to
315    header list; if you want to use a dumb convenience function which will
316    give you the first occurrence of a header you are interested in,
317    look at check_transaction_quadruple
318 */
319 int parse_headers(struct sip_msg* msg, hdr_flags_t flags, int next)
320 {
321         struct hdr_field* hf;
322         char* tmp;
323         char* rest;
324         char* end;
325         hdr_flags_t orig_flag;
326
327         end=msg->buf+msg->len;
328         tmp=msg->unparsed;
329
330         if (unlikely(next)) {
331                 orig_flag = msg->parsed_flag;
332                 msg->parsed_flag &= ~flags;
333         }else
334                 orig_flag=0;
335
336 #ifdef EXTRA_DEBUG
337         DBG("parse_headers: flags=%llx\n", (unsigned long long)flags);
338 #endif
339         while( tmp<end && (flags & msg->parsed_flag) != flags){
340                 prefetch_loc_r(tmp+64, 1);
341                 hf=pkg_malloc(sizeof(struct hdr_field));
342                 if (unlikely(hf==0)){
343                         ser_error=E_OUT_OF_MEM;
344                         LOG(L_ERR, "ERROR:parse_headers: memory allocation error\n");
345                         goto error;
346                 }
347                 memset(hf,0, sizeof(struct hdr_field));
348                 hf->type=HDR_ERROR_T;
349                 rest=get_hdr_field(tmp, end, hf);
350                 switch (hf->type){
351                         case HDR_ERROR_T:
352                                 LOG(L_INFO,"ERROR: bad header field [%.*s]\n",
353                                         (end-tmp>20)?20:(int)(end-tmp), tmp);
354                                 goto  error;
355                         case HDR_EOH_T:
356                                 msg->eoh=tmp; /* or rest?*/
357                                 msg->parsed_flag|=HDR_EOH_F;
358                                 pkg_free(hf);
359                                 goto skip;
360                         case HDR_ACCEPTCONTACT_T:
361                         case HDR_ALLOWEVENTS_T:
362                         case HDR_CONTENTENCODING_T:
363                         case HDR_REFERREDBY_T:
364                         case HDR_REJECTCONTACT_T:
365                         case HDR_REQUESTDISPOSITION_T:
366                         case HDR_WWW_AUTHENTICATE_T:
367                         case HDR_PROXY_AUTHENTICATE_T:
368                         case HDR_RETRY_AFTER_T:
369                         case HDR_OTHER_T: /* mark the type as found/parsed*/
370                                 msg->parsed_flag|=HDR_T2F(hf->type);
371                                 break;
372                         case HDR_CALLID_T:
373                                 if (msg->callid==0) msg->callid=hf;
374                                 msg->parsed_flag|=HDR_CALLID_F;
375                                 break;
376                         case HDR_SIPIFMATCH_T:
377                                 if (msg->sipifmatch==0) msg->sipifmatch=hf;
378                                 msg->parsed_flag|=HDR_SIPIFMATCH_F;
379                                 break;
380                         case HDR_TO_T:
381                                 if (msg->to==0) msg->to=hf;
382                                 msg->parsed_flag|=HDR_TO_F;
383                                 break;
384                         case HDR_CSEQ_T:
385                                 if (msg->cseq==0) msg->cseq=hf;
386                                 msg->parsed_flag|=HDR_CSEQ_F;
387                                 break;
388                         case HDR_FROM_T:
389                                 if (msg->from==0) msg->from=hf;
390                                 msg->parsed_flag|=HDR_FROM_F;
391                                 break;
392                         case HDR_CONTACT_T:
393                                 if (msg->contact==0) msg->contact=hf;
394                                 msg->parsed_flag|=HDR_CONTACT_F;
395                                 break;
396                         case HDR_MAXFORWARDS_T:
397                                 if(msg->maxforwards==0) msg->maxforwards=hf;
398                                 msg->parsed_flag|=HDR_MAXFORWARDS_F;
399                                 break;
400                         case HDR_ROUTE_T:
401                                 if (msg->route==0) msg->route=hf;
402                                 msg->parsed_flag|=HDR_ROUTE_F;
403                                 break;
404                         case HDR_RECORDROUTE_T:
405                                 if (msg->record_route==0) msg->record_route = hf;
406                                 msg->parsed_flag|=HDR_RECORDROUTE_F;
407                                 break;
408                         case HDR_CONTENTTYPE_T:
409                                 if (msg->content_type==0) msg->content_type = hf;
410                                 msg->parsed_flag|=HDR_CONTENTTYPE_F;
411                                 break;
412                         case HDR_CONTENTLENGTH_T:
413                                 if (msg->content_length==0) msg->content_length = hf;
414                                 msg->parsed_flag|=HDR_CONTENTLENGTH_F;
415                                 break;
416                         case HDR_AUTHORIZATION_T:
417                                 if (msg->authorization==0) msg->authorization = hf;
418                                 msg->parsed_flag|=HDR_AUTHORIZATION_F;
419                                 break;
420                         case HDR_EXPIRES_T:
421                                 if (msg->expires==0) msg->expires = hf;
422                                 msg->parsed_flag|=HDR_EXPIRES_F;
423                                 break;
424                         case HDR_PROXYAUTH_T:
425                                 if (msg->proxy_auth==0) msg->proxy_auth = hf;
426                                 msg->parsed_flag|=HDR_PROXYAUTH_F;
427                                 break;
428                         case HDR_PROXYREQUIRE_T:
429                                 if (msg->proxy_require==0) msg->proxy_require = hf;
430                                 msg->parsed_flag|=HDR_PROXYREQUIRE_F;
431                                 break;
432                         case HDR_SUPPORTED_T:
433                                 if (msg->supported==0) msg->supported=hf;
434                                 msg->parsed_flag|=HDR_SUPPORTED_F;
435                                 break;
436                         case HDR_REQUIRE_T:
437                                 if (msg->require==0) msg->require=hf;
438                                 msg->parsed_flag|=HDR_REQUIRE_F;
439                                 break;
440                         case HDR_UNSUPPORTED_T:
441                                 if (msg->unsupported==0) msg->unsupported=hf;
442                                 msg->parsed_flag|=HDR_UNSUPPORTED_F;
443                                 break;
444                         case HDR_ALLOW_T:
445                                 if (msg->allow==0) msg->allow = hf;
446                                 msg->parsed_flag|=HDR_ALLOW_F;
447                                 break;
448                         case HDR_EVENT_T:
449                                 if (msg->event==0) msg->event = hf;
450                                 msg->parsed_flag|=HDR_EVENT_F;
451                                 break;
452                         case HDR_ACCEPT_T:
453                                 if (msg->accept==0) msg->accept = hf;
454                                 msg->parsed_flag|=HDR_ACCEPT_F;
455                                 break;
456                         case HDR_ACCEPTLANGUAGE_T:
457                                 if (msg->accept_language==0) msg->accept_language = hf;
458                                 msg->parsed_flag|=HDR_ACCEPTLANGUAGE_F;
459                                 break;
460                         case HDR_ORGANIZATION_T:
461                                 if (msg->organization==0) msg->organization = hf;
462                                 msg->parsed_flag|=HDR_ORGANIZATION_F;
463                                 break;
464                         case HDR_PRIORITY_T:
465                                 if (msg->priority==0) msg->priority = hf;
466                                 msg->parsed_flag|=HDR_PRIORITY_F;
467                                 break;
468                         case HDR_SUBJECT_T:
469                                 if (msg->subject==0) msg->subject = hf;
470                                 msg->parsed_flag|=HDR_SUBJECT_F;
471                                 break;
472                         case HDR_USERAGENT_T:
473                                 if (msg->user_agent==0) msg->user_agent = hf;
474                                 msg->parsed_flag|=HDR_USERAGENT_F;
475                                 break;
476                         case HDR_SERVER_T:
477                                 if (msg->server==0) msg->server = hf;
478                                 msg->parsed_flag|=HDR_SERVER_F;
479                                 break;
480                         case HDR_CONTENTDISPOSITION_T:
481                                 if (msg->content_disposition==0) msg->content_disposition = hf;
482                                 msg->parsed_flag|=HDR_CONTENTDISPOSITION_F;
483                                 break;
484                         case HDR_ACCEPTDISPOSITION_T:
485                                 if (msg->accept_disposition==0) msg->accept_disposition = hf;
486                                 msg->parsed_flag|=HDR_ACCEPTDISPOSITION_F;
487                                 break;
488                         case HDR_DIVERSION_T:
489                                 if (msg->diversion==0) msg->diversion = hf;
490                                 msg->parsed_flag|=HDR_DIVERSION_F;
491                                 break;
492                         case HDR_RPID_T:
493                                 if (msg->rpid==0) msg->rpid = hf;
494                                 msg->parsed_flag|=HDR_RPID_F;
495                                 break;
496                         case HDR_REFER_TO_T:
497                                 if (msg->refer_to==0) msg->refer_to = hf;
498                                 msg->parsed_flag|=HDR_REFER_TO_F;
499                                 break;
500                         case HDR_SESSIONEXPIRES_T:
501                                 if (msg->session_expires==0) msg->session_expires = hf;
502                                 msg->parsed_flag|=HDR_SESSIONEXPIRES_F;
503                                 break;
504                         case HDR_MIN_SE_T:
505                                 if (msg->min_se==0) msg->min_se = hf;
506                                 msg->parsed_flag|=HDR_MIN_SE_F;
507                                 break;
508                         case HDR_SUBSCRIPTION_STATE_T:
509                                 if (msg->subscription_state==0) msg->subscription_state = hf;
510                                 msg->parsed_flag|=HDR_SUBSCRIPTION_STATE_F;
511                                 break;
512                         case HDR_VIA_T:
513                                 msg->parsed_flag|=HDR_VIA_F;
514                                 DBG("parse_headers: Via found, flags=%llx\n",
515                                                 (unsigned long long)flags);
516                                 if (msg->via1==0) {
517                                         DBG("parse_headers: this is the first via\n");
518                                         msg->h_via1=hf;
519                                         msg->via1=hf->parsed;
520                                         if (msg->via1->next){
521                                                 msg->via2=msg->via1->next;
522                                                 msg->parsed_flag|=HDR_VIA2_F;
523                                         }
524                                 }else if (msg->via2==0){
525                                         msg->h_via2=hf;
526                                         msg->via2=hf->parsed;
527                                         msg->parsed_flag|=HDR_VIA2_F;
528                                         DBG("parse_headers: this is the second via\n");
529                                 }
530                                 break;
531                         case HDR_DATE_T:
532                                 if (msg->date==0) msg->date=hf;
533                                 msg->parsed_flag|=HDR_DATE_F;
534                                 break;
535                         case HDR_IDENTITY_T:
536                                 if (msg->identity==0) msg->identity=hf;
537                                 msg->parsed_flag|=HDR_IDENTITY_F;
538                                 break;
539                         case HDR_IDENTITY_INFO_T:
540                                 if (msg->identity_info==0) msg->identity_info=hf;
541                                 msg->parsed_flag|=HDR_IDENTITY_INFO_F;
542                                 break;
543                     case HDR_PATH_T:
544                                 if (msg->path==0) msg->path=hf;
545                                 msg->parsed_flag|=HDR_PATH_F;
546                                 break;
547                     case HDR_PRIVACY_T:
548                                 if (msg->privacy==0) msg->privacy=hf;
549                                 msg->parsed_flag|=HDR_PRIVACY_F;
550                                 break;
551                     case HDR_PAI_T:
552                                 if (msg->pai==0) msg->pai=hf;
553                                 msg->parsed_flag|=HDR_PAI_F;
554                                 break;
555                     case HDR_PPI_T:
556                                 if (msg->ppi==0) msg->ppi=hf;
557                                 msg->parsed_flag|=HDR_PPI_F;
558                                 break;
559                     case HDR_REASON_T:
560                                 msg->parsed_flag|=HDR_REASON_F;
561                                 break;
562                         default:
563                                 LOG(L_CRIT, "BUG: parse_headers: unknown header type %d\n",
564                                                         hf->type);
565                                 goto error;
566                 }
567                 /* add the header to the list*/
568                 if (msg->last_header==0){
569                         msg->headers=hf;
570                         msg->last_header=hf;
571                 }else{
572                         msg->last_header->next=hf;
573                         msg->last_header=hf;
574                 }
575 #ifdef EXTRA_DEBUG
576                 DBG("header field type %d, name=<%.*s>, body=<%.*s>\n",
577                         hf->type,
578                         hf->name.len, ZSW(hf->name.s),
579                         hf->body.len, ZSW(hf->body.s));
580 #endif
581                 tmp=rest;
582         }
583 skip:
584         msg->unparsed=tmp;
585         /* restore original flags */
586         msg->parsed_flag |= orig_flag;
587         return 0;
588
589 error:
590         ser_error=E_BAD_REQ;
591         if (hf) pkg_free(hf);
592         /* restore original flags */
593         msg->parsed_flag |= orig_flag;
594         return -1;
595 }
596
597
598
599
600
601 /* returns 0 if ok, -1 for errors */
602 int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
603 {
604
605         char *tmp;
606         char* rest;
607         char* first_via;
608         char* second_via;
609         struct msg_start *fl;
610         int offset;
611         hdr_flags_t flags;
612
613         /* eat crlf from the beginning */
614         for (tmp=buf; (*tmp=='\n' || *tmp=='\r')&&
615                         tmp-buf < len ; tmp++);
616         offset=tmp-buf;
617         fl=&(msg->first_line);
618         rest=parse_first_line(tmp, len-offset, fl);
619 #if 0
620         rest=parse_fline(tmp, buf+len, fl);
621 #endif
622         offset+=rest-tmp;
623         tmp=rest;
624         switch(fl->type){
625                 case SIP_INVALID:
626                         DBG("parse_msg: invalid message\n");
627                         goto error;
628                         break;
629                 case SIP_REQUEST:
630                         DBG("SIP Request:\n");
631                         DBG(" method:  <%.*s>\n",fl->u.request.method.len,
632                                 ZSW(fl->u.request.method.s));
633                         DBG(" uri:     <%.*s>\n",fl->u.request.uri.len,
634                                 ZSW(fl->u.request.uri.s));
635                         DBG(" version: <%.*s>\n",fl->u.request.version.len,
636                                 ZSW(fl->u.request.version.s));
637                         flags=HDR_VIA_F;
638                         break;
639                 case SIP_REPLY:
640                         DBG("SIP Reply  (status):\n");
641                         DBG(" version: <%.*s>\n",fl->u.reply.version.len,
642                                         ZSW(fl->u.reply.version.s));
643                         DBG(" status:  <%.*s>\n", fl->u.reply.status.len,
644                                         ZSW(fl->u.reply.status.s));
645                         DBG(" reason:  <%.*s>\n", fl->u.reply.reason.len,
646                                         ZSW(fl->u.reply.reason.s));
647                         /* flags=HDR_VIA | HDR_VIA2; */
648                         /* we don't try to parse VIA2 for local messages; -Jiri */
649                         flags=HDR_VIA_F;
650                         break;
651                 default:
652                         DBG("unknown type %d\n",fl->type);
653                         goto error;
654         }
655         msg->unparsed=tmp;
656         /*find first Via: */
657         first_via=0;
658         second_via=0;
659         if (parse_headers(msg, flags, 0)==-1) goto error;
660
661 #ifdef EXTRA_DEBUG
662         /* dump parsed data */
663         if (msg->via1){
664                 DBG("first via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
665                         msg->via1->name.len,
666                         ZSW(msg->via1->name.s),
667                         msg->via1->version.len,
668                         ZSW(msg->via1->version.s),
669                         msg->via1->transport.len,
670                         ZSW(msg->via1->transport.s),
671                         msg->via1->host.len,
672                         ZSW(msg->via1->host.s),
673                         msg->via1->port_str.len,
674                         ZSW(msg->via1->port_str.s),
675                         msg->via1->port);
676                 if (msg->via1->params.s)  DBG(";<%.*s>",
677                                 msg->via1->params.len, ZSW(msg->via1->params.s));
678                 if (msg->via1->comment.s)
679                                 DBG(" <%.*s>",
680                                         msg->via1->comment.len, ZSW(msg->via1->comment.s));
681                 DBG ("\n");
682         }
683         if (msg->via2){
684                 DBG("second via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
685                         msg->via2->name.len,
686                         ZSW(msg->via2->name.s),
687                         msg->via2->version.len,
688                         ZSW(msg->via2->version.s),
689                         msg->via2->transport.len,
690                         ZSW(msg->via2->transport.s),
691                         msg->via2->host.len,
692                         ZSW(msg->via2->host.s),
693                         msg->via2->port_str.len,
694                         ZSW(msg->via2->port_str.s),
695                         msg->via2->port);
696                 if (msg->via2->params.s)  DBG(";<%.*s>",
697                                 msg->via2->params.len, ZSW(msg->via2->params.s));
698                 if (msg->via2->comment.s) DBG(" <%.*s>",
699                                 msg->via2->comment.len, ZSW(msg->via2->comment.s));
700                 DBG ("\n");
701         }
702 #endif
703
704
705 #ifdef EXTRA_DEBUG
706         DBG("exiting parse_msg\n");
707 #endif
708
709         return 0;
710
711 error:
712         /* more debugging, msg->orig is/should be null terminated*/
713         LOG(cfg_get(core, core_cfg, corelog), "ERROR: parse_msg: message=<%.*s>\n",
714                         (int)msg->len, ZSW(msg->buf));
715         return -1;
716 }
717
718
719
720 void free_reply_lump( struct lump_rpl *lump)
721 {
722         struct lump_rpl *foo, *bar;
723         for(foo=lump;foo;)
724         {
725                 bar=foo->next;
726                 free_lump_rpl(foo);
727                 foo = bar;
728         }
729 }
730
731
732 /*only the content*/
733 void free_sip_msg(struct sip_msg* msg)
734 {
735         if (msg->new_uri.s) { pkg_free(msg->new_uri.s); msg->new_uri.len=0; }
736         if (msg->dst_uri.s) { pkg_free(msg->dst_uri.s); msg->dst_uri.len=0; }
737         if (msg->path_vec.s) { pkg_free(msg->path_vec.s); msg->path_vec.len=0; }
738         if (msg->headers)     free_hdr_field_lst(msg->headers);
739         if (msg->body && msg->body->free) msg->body->free(&msg->body);
740         if (msg->add_rm)      free_lump_list(msg->add_rm);
741         if (msg->body_lumps)  free_lump_list(msg->body_lumps);
742         if (msg->reply_lump)   free_reply_lump(msg->reply_lump);
743         /* don't free anymore -- now a pointer to a static buffer */
744 #       ifdef DYN_BUF
745         pkg_free(msg->buf);
746 #       endif
747 }
748
749
750 /*
751  * Make a private copy of the string and assign it to dst_uri
752  */
753 int set_dst_uri(struct sip_msg* msg, str* uri)
754 {
755         char* ptr;
756
757         if (unlikely(!msg || !uri)) {
758                 LOG(L_ERR, "set_dst_uri: Invalid parameter value\n");
759                 return -1;
760         }
761
762         if (unlikely(uri->len == 0)) {
763                 reset_dst_uri(msg);
764         }else if (msg->dst_uri.s && (msg->dst_uri.len >= uri->len)) {
765                 memcpy(msg->dst_uri.s, uri->s, uri->len);
766                 msg->dst_uri.len = uri->len;
767         } else {
768                 ptr = (char*)pkg_malloc(uri->len);
769                 if (!ptr) {
770                         LOG(L_ERR, "set_dst_uri: Not enough memory\n");
771                         return -1;
772                 }
773
774                 memcpy(ptr, uri->s, uri->len);
775                 if (msg->dst_uri.s) pkg_free(msg->dst_uri.s);
776                 msg->dst_uri.s = ptr;
777                 msg->dst_uri.len = uri->len;
778         }
779         return 0;
780 }
781
782
783 void reset_dst_uri(struct sip_msg* msg)
784 {
785         if(msg->dst_uri.s != 0) {
786                 pkg_free(msg->dst_uri.s);
787         }
788         msg->dst_uri.s = 0;
789         msg->dst_uri.len = 0;
790 }
791
792 int set_path_vector(struct sip_msg* msg, str* path)
793 {
794         char* ptr;
795
796         if (unlikely(!msg || !path)) {
797                 LM_ERR("invalid parameter value\n");
798                 return -1;
799         }
800
801         if (unlikely(path->len == 0)) {
802                 reset_path_vector(msg);
803         } else if (msg->path_vec.s && (msg->path_vec.len >= path->len)) {
804                 memcpy(msg->path_vec.s, path->s, path->len);
805                 msg->path_vec.len = path->len;
806         } else {
807                 ptr = (char*)pkg_malloc(path->len);
808                 if (!ptr) {
809                         LM_ERR("not enough pkg memory\n");
810                         return -1;
811                 }
812
813                 memcpy(ptr, path->s, path->len);
814                 if (msg->path_vec.s) pkg_free(msg->path_vec.s);
815                 msg->path_vec.s = ptr;
816                 msg->path_vec.len = path->len;
817         }
818         return 0;
819 }
820
821
822 void reset_path_vector(struct sip_msg* msg)
823 {
824         if(msg->path_vec.s != 0) {
825                 pkg_free(msg->path_vec.s);
826         }
827         msg->path_vec.s = 0;
828         msg->path_vec.len = 0;
829 }
830
831
832 struct hdr_field* get_hdr(struct sip_msg *msg, enum _hdr_types_t ht)
833 {
834         struct hdr_field *hdr;
835
836         if (msg->parsed_flag & HDR_T2F(ht))
837                 for(hdr = msg->headers; hdr; hdr = hdr->next) {
838                         if(hdr->type == ht) return hdr;
839                 }
840         return NULL;
841 }
842
843
844 struct hdr_field* next_sibling_hdr(struct hdr_field *hf)
845 {       
846         struct hdr_field *hdr;
847         
848         for(hdr = hf->next; hdr; hdr = hdr->next) {
849                 if(hdr->type == hf->type) return hdr;
850         }
851         return NULL;
852 }