986c8e05f5ed82891cf857e681368b86e4d857a9
[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 #include <sys/time.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 /* global request flags */
87 unsigned int global_req_flags = 0;
88
89 /* returns pointer to next header line, and fill hdr_f ;
90  * if at end of header returns pointer to the last crlf  (always buf)*/
91 char* get_hdr_field(char* const buf, char* const end, struct hdr_field* const hdr)
92 {
93
94         char* tmp;
95         char *match;
96         struct via_body *vb;
97         struct cseq_body* cseq_b;
98         struct to_body* to_b;
99         int integer, err;
100         unsigned uval;
101
102         if ((*buf)=='\n' || (*buf)=='\r'){
103                 /* double crlf or lflf or crcr */
104                 DBG("found end of header\n");
105                 hdr->type=HDR_EOH_T;
106                 return buf;
107         }
108
109         tmp=parse_hname(buf, end, hdr);
110         if (hdr->type==HDR_ERROR_T){
111                 LOG(L_ERR, "ERROR: get_hdr_field: bad header\n");
112                 goto error;
113         }
114
115         /* eliminate leading whitespace */
116         tmp=eat_lws_end(tmp, end);
117         if (tmp>=end) {
118                 LOG(L_ERR, "ERROR: get_hdr_field: HF empty\n");
119                 goto error;
120         }
121
122         /* if header-field well-known, parse it, find its end otherwise ;
123          * after leaving the hdr->type switch, tmp should be set to the
124          * next header field
125          */
126         switch(hdr->type){
127                 case HDR_VIA_T:
128                         /* keep number of vias parsed -- we want to report it in
129                            replies for diagnostic purposes */
130                         via_cnt++;
131                         vb=pkg_malloc(sizeof(struct via_body));
132                         if (vb==0){
133                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
134                                 goto error;
135                         }
136                         memset(vb,0,sizeof(struct via_body));
137                         hdr->body.s=tmp;
138                         tmp=parse_via(tmp, end, vb);
139                         if (vb->error==PARSE_ERROR){
140                                 LOG(L_ERR, "ERROR: get_hdr_field: bad via\n");
141                                 free_via_list(vb);
142                                 goto error;
143                         }
144                         hdr->parsed=vb;
145                         vb->hdr.s=hdr->name.s;
146                         vb->hdr.len=hdr->name.len;
147                         hdr->body.len=tmp-hdr->body.s;
148                         break;
149                 case HDR_CSEQ_T:
150                         cseq_b=pkg_malloc(sizeof(struct cseq_body));
151                         if (cseq_b==0){
152                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
153                                 goto error;
154                         }
155                         memset(cseq_b, 0, sizeof(struct cseq_body));
156                         hdr->body.s=tmp;
157                         tmp=parse_cseq(tmp, end, cseq_b);
158                         if (cseq_b->error==PARSE_ERROR){
159                                 LOG(L_ERR, "ERROR: get_hdr_field: bad cseq\n");
160                                 free_cseq(cseq_b);
161                                 goto error;
162                         }
163                         hdr->parsed=cseq_b;
164                         hdr->body.len=tmp-hdr->body.s;
165                         DBG("get_hdr_field: cseq <%.*s>: <%.*s> <%.*s>\n",
166                                         hdr->name.len, ZSW(hdr->name.s),
167                                         cseq_b->number.len, ZSW(cseq_b->number.s),
168                                         cseq_b->method.len, cseq_b->method.s);
169                         break;
170                 case HDR_TO_T:
171                         to_b=pkg_malloc(sizeof(struct to_body));
172                         if (to_b==0){
173                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
174                                 goto error;
175                         }
176                         memset(to_b, 0, sizeof(struct to_body));
177                         hdr->body.s=tmp;
178                         tmp=parse_to(tmp, end,to_b);
179                         if (to_b->error==PARSE_ERROR){
180                                 LOG(L_ERR, "ERROR: get_hdr_field: bad to header\n");
181                                 free_to(to_b);
182                                 goto error;
183                         }
184                         hdr->parsed=to_b;
185                         hdr->body.len=tmp-hdr->body.s;
186                         DBG("DEBUG: get_hdr_field: <%.*s> [%d]; uri=[%.*s] \n",
187                                 hdr->name.len, ZSW(hdr->name.s),
188                                 hdr->body.len, to_b->uri.len,ZSW(to_b->uri.s));
189                         DBG("DEBUG: to body [%.*s]\n",to_b->body.len,
190                                 ZSW(to_b->body.s));
191                         break;
192                 case HDR_CONTENTLENGTH_T:
193                         hdr->body.s=tmp;
194                         tmp=parse_content_length(tmp,end, &integer);
195                         if (tmp==0){
196                                 LOG(L_ERR, "ERROR:get_hdr_field: bad content_length header\n");
197                                 goto error;
198                         }
199                         hdr->parsed=(void*)(long)integer;
200                         hdr->body.len=tmp-hdr->body.s;
201                         DBG("DEBUG: get_hdr_body : content_length=%d\n",
202                                         (int)(long)hdr->parsed);
203                         break;
204                 case HDR_RETRY_AFTER_T:
205                         hdr->body.s=tmp;
206                         tmp=parse_retry_after(tmp,end, &uval, &err);
207                         if (err){
208                                 LOG(L_ERR, "ERROR:get_hdr_field: bad retry_after header\n");
209                                 goto error;
210                         }
211                         hdr->parsed=(void*)(unsigned long)uval;
212                         hdr->body.len=tmp-hdr->body.s;
213                         DBG("DEBUG: get_hdr_body : retry_after=%d\n",
214                                         (unsigned)(long)hdr->parsed);
215                         break;
216                 case HDR_IDENTITY_T:
217                 case HDR_DATE_T:
218                 case HDR_IDENTITY_INFO_T:
219                 case HDR_SUPPORTED_T:
220                 case HDR_REQUIRE_T:
221                 case HDR_CONTENTTYPE_T:
222                 case HDR_FROM_T:
223                 case HDR_CALLID_T:
224                 case HDR_CONTACT_T:
225                 case HDR_ROUTE_T:
226                 case HDR_RECORDROUTE_T:
227                 case HDR_MAXFORWARDS_T:
228                 case HDR_AUTHORIZATION_T:
229                 case HDR_EXPIRES_T:
230                 case HDR_PROXYAUTH_T:
231                 case HDR_PROXYREQUIRE_T:
232                 case HDR_UNSUPPORTED_T:
233                 case HDR_ALLOW_T:
234                 case HDR_EVENT_T:
235                 case HDR_ACCEPT_T:
236                 case HDR_ACCEPTLANGUAGE_T:
237                 case HDR_ORGANIZATION_T:
238                 case HDR_PRIORITY_T:
239                 case HDR_SUBJECT_T:
240                 case HDR_USERAGENT_T:
241                 case HDR_SERVER_T:
242                 case HDR_CONTENTDISPOSITION_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* const msg, const hdr_flags_t flags, const 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_DIVERSION_T:
485                                 if (msg->diversion==0) msg->diversion = hf;
486                                 msg->parsed_flag|=HDR_DIVERSION_F;
487                                 break;
488                         case HDR_RPID_T:
489                                 if (msg->rpid==0) msg->rpid = hf;
490                                 msg->parsed_flag|=HDR_RPID_F;
491                                 break;
492                         case HDR_REFER_TO_T:
493                                 if (msg->refer_to==0) msg->refer_to = hf;
494                                 msg->parsed_flag|=HDR_REFER_TO_F;
495                                 break;
496                         case HDR_SESSIONEXPIRES_T:
497                                 if (msg->session_expires==0) msg->session_expires = hf;
498                                 msg->parsed_flag|=HDR_SESSIONEXPIRES_F;
499                                 break;
500                         case HDR_MIN_SE_T:
501                                 if (msg->min_se==0) msg->min_se = hf;
502                                 msg->parsed_flag|=HDR_MIN_SE_F;
503                                 break;
504                         case HDR_SUBSCRIPTION_STATE_T:
505                                 if (msg->subscription_state==0) msg->subscription_state = hf;
506                                 msg->parsed_flag|=HDR_SUBSCRIPTION_STATE_F;
507                                 break;
508                         case HDR_VIA_T:
509                                 msg->parsed_flag|=HDR_VIA_F;
510                                 DBG("parse_headers: Via found, flags=%llx\n",
511                                                 (unsigned long long)flags);
512                                 if (msg->via1==0) {
513                                         DBG("parse_headers: this is the first via\n");
514                                         msg->h_via1=hf;
515                                         msg->via1=hf->parsed;
516                                         if (msg->via1->next){
517                                                 msg->via2=msg->via1->next;
518                                                 msg->parsed_flag|=HDR_VIA2_F;
519                                         }
520                                 }else if (msg->via2==0){
521                                         msg->h_via2=hf;
522                                         msg->via2=hf->parsed;
523                                         msg->parsed_flag|=HDR_VIA2_F;
524                                         DBG("parse_headers: this is the second via\n");
525                                 }
526                                 break;
527                         case HDR_DATE_T:
528                                 if (msg->date==0) msg->date=hf;
529                                 msg->parsed_flag|=HDR_DATE_F;
530                                 break;
531                         case HDR_IDENTITY_T:
532                                 if (msg->identity==0) msg->identity=hf;
533                                 msg->parsed_flag|=HDR_IDENTITY_F;
534                                 break;
535                         case HDR_IDENTITY_INFO_T:
536                                 if (msg->identity_info==0) msg->identity_info=hf;
537                                 msg->parsed_flag|=HDR_IDENTITY_INFO_F;
538                                 break;
539                     case HDR_PATH_T:
540                                 if (msg->path==0) msg->path=hf;
541                                 msg->parsed_flag|=HDR_PATH_F;
542                                 break;
543                     case HDR_PRIVACY_T:
544                                 if (msg->privacy==0) msg->privacy=hf;
545                                 msg->parsed_flag|=HDR_PRIVACY_F;
546                                 break;
547                     case HDR_PAI_T:
548                                 if (msg->pai==0) msg->pai=hf;
549                                 msg->parsed_flag|=HDR_PAI_F;
550                                 break;
551                     case HDR_PPI_T:
552                                 if (msg->ppi==0) msg->ppi=hf;
553                                 msg->parsed_flag|=HDR_PPI_F;
554                                 break;
555                     case HDR_REASON_T:
556                                 msg->parsed_flag|=HDR_REASON_F;
557                                 break;
558                         default:
559                                 LOG(L_CRIT, "BUG: parse_headers: unknown header type %d\n",
560                                                         hf->type);
561                                 goto error;
562                 }
563                 /* add the header to the list*/
564                 if (msg->last_header==0){
565                         msg->headers=hf;
566                         msg->last_header=hf;
567                 }else{
568                         msg->last_header->next=hf;
569                         msg->last_header=hf;
570                 }
571 #ifdef EXTRA_DEBUG
572                 DBG("header field type %d, name=<%.*s>, body=<%.*s>\n",
573                         hf->type,
574                         hf->name.len, ZSW(hf->name.s),
575                         hf->body.len, ZSW(hf->body.s));
576 #endif
577                 tmp=rest;
578         }
579 skip:
580         msg->unparsed=tmp;
581         /* restore original flags */
582         msg->parsed_flag |= orig_flag;
583         return 0;
584
585 error:
586         ser_error=E_BAD_REQ;
587         if (hf) pkg_free(hf);
588         /* restore original flags */
589         msg->parsed_flag |= orig_flag;
590         return -1;
591 }
592
593
594
595
596
597 /* returns 0 if ok, -1 for errors */
598 int parse_msg(char* const buf, const unsigned int len, struct sip_msg* const msg)
599 {
600
601         char *tmp;
602         char* rest;
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         offset+=rest-tmp;
614         tmp=rest;
615         switch(fl->type){
616                 case SIP_INVALID:
617                         DBG("parse_msg: invalid message\n");
618                         goto error;
619                         break;
620                 case SIP_REQUEST:
621                         DBG("SIP Request:\n");
622                         DBG(" method:  <%.*s>\n",fl->u.request.method.len,
623                                 ZSW(fl->u.request.method.s));
624                         DBG(" uri:     <%.*s>\n",fl->u.request.uri.len,
625                                 ZSW(fl->u.request.uri.s));
626                         DBG(" version: <%.*s>\n",fl->u.request.version.len,
627                                 ZSW(fl->u.request.version.s));
628                         flags=HDR_VIA_F;
629                         break;
630                 case SIP_REPLY:
631                         DBG("SIP Reply  (status):\n");
632                         DBG(" version: <%.*s>\n",fl->u.reply.version.len,
633                                         ZSW(fl->u.reply.version.s));
634                         DBG(" status:  <%.*s>\n", fl->u.reply.status.len,
635                                         ZSW(fl->u.reply.status.s));
636                         DBG(" reason:  <%.*s>\n", fl->u.reply.reason.len,
637                                         ZSW(fl->u.reply.reason.s));
638                         /* flags=HDR_VIA | HDR_VIA2; */
639                         /* we don't try to parse VIA2 for local messages; -Jiri */
640                         flags=HDR_VIA_F;
641                         break;
642                 default:
643                         DBG("unknown type %d\n",fl->type);
644                         goto error;
645         }
646         msg->unparsed=tmp;
647         /*find first Via: */
648         if (parse_headers(msg, flags, 0)==-1) goto error;
649
650 #ifdef EXTRA_DEBUG
651         /* dump parsed data */
652         if (msg->via1){
653                 DBG("first via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
654                         msg->via1->name.len,
655                         ZSW(msg->via1->name.s),
656                         msg->via1->version.len,
657                         ZSW(msg->via1->version.s),
658                         msg->via1->transport.len,
659                         ZSW(msg->via1->transport.s),
660                         msg->via1->host.len,
661                         ZSW(msg->via1->host.s),
662                         msg->via1->port_str.len,
663                         ZSW(msg->via1->port_str.s),
664                         msg->via1->port);
665                 if (msg->via1->params.s)  DBG(";<%.*s>",
666                                 msg->via1->params.len, ZSW(msg->via1->params.s));
667                 if (msg->via1->comment.s)
668                                 DBG(" <%.*s>",
669                                         msg->via1->comment.len, ZSW(msg->via1->comment.s));
670                 DBG ("\n");
671         }
672         if (msg->via2){
673                 DBG("second via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
674                         msg->via2->name.len,
675                         ZSW(msg->via2->name.s),
676                         msg->via2->version.len,
677                         ZSW(msg->via2->version.s),
678                         msg->via2->transport.len,
679                         ZSW(msg->via2->transport.s),
680                         msg->via2->host.len,
681                         ZSW(msg->via2->host.s),
682                         msg->via2->port_str.len,
683                         ZSW(msg->via2->port_str.s),
684                         msg->via2->port);
685                 if (msg->via2->params.s)  DBG(";<%.*s>",
686                                 msg->via2->params.len, ZSW(msg->via2->params.s));
687                 if (msg->via2->comment.s) DBG(" <%.*s>",
688                                 msg->via2->comment.len, ZSW(msg->via2->comment.s));
689                 DBG ("\n");
690         }
691 #endif
692
693
694 #ifdef EXTRA_DEBUG
695         DBG("exiting parse_msg\n");
696 #endif
697
698         return 0;
699
700 error:
701         /* more debugging, msg->orig is/should be null terminated*/
702         LOG(cfg_get(core, core_cfg, corelog), "ERROR: parse_msg: message=<%.*s>\n",
703                         (int)msg->len, ZSW(msg->buf));
704         return -1;
705 }
706
707
708
709 void free_reply_lump( struct lump_rpl *lump)
710 {
711         struct lump_rpl *foo, *bar;
712         for(foo=lump;foo;)
713         {
714                 bar=foo->next;
715                 free_lump_rpl(foo);
716                 foo = bar;
717         }
718 }
719
720
721 /*only the content*/
722 void free_sip_msg(struct sip_msg* const msg)
723 {
724         if (msg->new_uri.s) { pkg_free(msg->new_uri.s); msg->new_uri.len=0; }
725         if (msg->dst_uri.s) { pkg_free(msg->dst_uri.s); msg->dst_uri.len=0; }
726         if (msg->path_vec.s) { pkg_free(msg->path_vec.s); msg->path_vec.len=0; }
727         if (msg->headers)     free_hdr_field_lst(msg->headers);
728         if (msg->body && msg->body->free) msg->body->free(&msg->body);
729         if (msg->add_rm)      free_lump_list(msg->add_rm);
730         if (msg->body_lumps)  free_lump_list(msg->body_lumps);
731         if (msg->reply_lump)   free_reply_lump(msg->reply_lump);
732         /* don't free anymore -- now a pointer to a static buffer */
733 #       ifdef DYN_BUF
734         pkg_free(msg->buf);
735 #       endif
736 }
737
738
739 /*
740  * Make a private copy of the string and assign it to dst_uri
741  */
742 int set_dst_uri(struct sip_msg* const msg, const str* const uri)
743 {
744         char* ptr;
745
746         if (unlikely(!msg || !uri)) {
747                 LOG(L_ERR, "set_dst_uri: Invalid parameter value\n");
748                 return -1;
749         }
750
751         if (unlikely(uri->len == 0)) {
752                 reset_dst_uri(msg);
753         }else if (msg->dst_uri.s && (msg->dst_uri.len >= uri->len)) {
754                 memcpy(msg->dst_uri.s, uri->s, uri->len);
755                 msg->dst_uri.len = uri->len;
756         } else {
757                 ptr = (char*)pkg_malloc(uri->len);
758                 if (!ptr) {
759                         LOG(L_ERR, "set_dst_uri: Not enough memory\n");
760                         return -1;
761                 }
762
763                 memcpy(ptr, uri->s, uri->len);
764                 if (msg->dst_uri.s) pkg_free(msg->dst_uri.s);
765                 msg->dst_uri.s = ptr;
766                 msg->dst_uri.len = uri->len;
767         }
768         return 0;
769 }
770
771
772 void reset_dst_uri(struct sip_msg* const msg)
773 {
774         if(msg->dst_uri.s != 0) {
775                 pkg_free(msg->dst_uri.s);
776         }
777         msg->dst_uri.s = 0;
778         msg->dst_uri.len = 0;
779 }
780
781 int set_path_vector(struct sip_msg* msg, str* path)
782 {
783         char* ptr;
784
785         if (unlikely(!msg || !path)) {
786                 LM_ERR("invalid parameter value\n");
787                 return -1;
788         }
789
790         if (unlikely(path->len == 0)) {
791                 reset_path_vector(msg);
792         } else if (msg->path_vec.s && (msg->path_vec.len >= path->len)) {
793                 memcpy(msg->path_vec.s, path->s, path->len);
794                 msg->path_vec.len = path->len;
795         } else {
796                 ptr = (char*)pkg_malloc(path->len);
797                 if (!ptr) {
798                         LM_ERR("not enough pkg memory\n");
799                         return -1;
800                 }
801
802                 memcpy(ptr, path->s, path->len);
803                 if (msg->path_vec.s) pkg_free(msg->path_vec.s);
804                 msg->path_vec.s = ptr;
805                 msg->path_vec.len = path->len;
806         }
807         return 0;
808 }
809
810
811 void reset_path_vector(struct sip_msg* msg)
812 {
813         if(msg->path_vec.s != 0) {
814                 pkg_free(msg->path_vec.s);
815         }
816         msg->path_vec.s = 0;
817         msg->path_vec.len = 0;
818 }
819
820
821 hdr_field_t* get_hdr(sip_msg_t *msg, enum _hdr_types_t ht)
822 {
823         hdr_field_t *hdr;
824
825         if (msg->parsed_flag & HDR_T2F(ht))
826                 for(hdr = msg->headers; hdr; hdr = hdr->next) {
827                         if(hdr->type == ht) return hdr;
828                 }
829         return NULL;
830 }
831
832
833 hdr_field_t* next_sibling_hdr(hdr_field_t *hf)
834 {
835         hdr_field_t *hdr;
836
837         for(hdr = hf->next; hdr; hdr = hdr->next) {
838                 if(hdr->type == hf->type) return hdr;
839         }
840         return NULL;
841 }
842
843 hdr_field_t* get_hdr_by_name(sip_msg_t *msg, char *name, int name_len)
844 {
845         hdr_field_t *hdr;
846
847         for(hdr = msg->headers; hdr; hdr = hdr->next) {
848                 if(hdr->name.len == name_len && *hdr->name.s==*name
849                                 && strncmp(hdr->name.s, name, name_len)==0)
850                         return hdr;
851         }
852         return NULL;
853 }
854
855
856 hdr_field_t* next_sibling_hdr_by_name(hdr_field_t *hf)
857 {
858         hdr_field_t *hdr;
859
860         for(hdr = hf->next; hdr; hdr = hdr->next) {
861                 if(hdr->name.len == hf->name.len && *hdr->name.s==*hf->name.s
862                                 && strncmp(hdr->name.s, hf->name.s, hf->name.len)==0)
863                         return hdr;
864         }
865         return NULL;
866 }
867
868 /**
869  * set msg context id
870  * - return: -1 on error; 0 - on set
871  */
872 int msg_ctx_id_set(const sip_msg_t* const msg, msg_ctx_id_t* const mid)
873 {
874         if(msg==NULL || mid==NULL)
875                 return -1;
876         mid->msgid = msg->id;
877         mid->pid = msg->pid;
878         return 0;
879 }
880
881 /**
882  * check msg context id
883  * - return: -1 on error; 0 - on no match; 1 - on match
884  */
885 int msg_ctx_id_match(const sip_msg_t* const msg, const msg_ctx_id_t* const mid)
886 {
887         if(msg==NULL || mid==NULL)
888                 return -1;
889         if(msg->id != mid->msgid || msg->pid!=mid->pid)
890                 return 0;
891         return 1;
892 }
893
894 /**
895  * set msg time value
896  */
897 int msg_set_time(sip_msg_t* const msg)
898 {
899         if(unlikely(msg==NULL))
900                 return -2;
901         if(msg->tval.tv_sec!=0)
902                 return 0;
903         return gettimeofday(&msg->tval, NULL);
904 }