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