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