- ipv6 support (-DUSE_IPV6)
[sip-router] / parser / msg_parser.c
1 /*
2  * $Id$
3  *
4  * sip msg. header proxy parser 
5  *
6  */
7
8 #include <string.h>
9 #include <stdlib.h>
10
11 #include "msg_parser.h"
12 #include "parser_f.h"
13 #include "../ut.h"
14 #include "../error.h"
15 #include "../dprint.h"
16 #include "../data_lump_rpl.h"
17 #include "../mem/mem.h"
18
19 #ifdef DEBUG_DMALLOC
20 #include <mem/dmalloc.h>
21 #endif
22
23
24
25
26
27 /* parses the first line, returns pointer to  next line  & fills fl;
28    also  modifies buffer (to avoid extra copy ops) */
29 char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
30 {
31         
32         char *tmp;
33         char* second;
34         char* third;
35         char* nl;
36         int offset;
37         /* int l; */
38         char* end;
39         char s1,s2,s3;
40         char *prn;
41         unsigned int t;
42
43         /* grammar:
44                 request  =  method SP uri SP version CRLF
45                 response =  version SP status  SP reason  CRLF
46                 (version = "SIP/2.0")
47         */
48         
49
50         end=buffer+len;
51         /* see if it's a reply (status) */
52
53         /* jku  -- parse well-known methods */
54
55         /* drop messages which are so short they are for sure useless;
56            utilize knowledge of minimum size in parsing the first
57            token 
58         */
59         if (len <=16 ) {
60                 LOG(L_INFO, "ERROR: parse_first_line: message too short: %d\n", len);
61                 goto error1;
62         }
63
64         tmp=buffer;
65         /* is it perhaps a reply, ie does it start with "SIP...." ? */
66         if (    (*tmp=='S' || *tmp=='s') && 
67                 strncasecmp( tmp+1, SIP_VERSION+1, SIP_VERSION_LEN-1)==0 &&
68                 (*(tmp+SIP_VERSION_LEN)==' ')) {
69                         fl->type=SIP_REPLY;
70                         fl->u.reply.version.len=SIP_VERSION_LEN;
71                         tmp=buffer+SIP_VERSION_LEN;
72         } else IFISMETHOD( INVITE, 'I' )
73         else IFISMETHOD( CANCEL, 'C')
74         else IFISMETHOD( ACK, 'A' )
75         else IFISMETHOD( BYE, 'B' ) 
76         /* if you want to add another method XXX, include METHOD_XXX in
77            H-file (this is the value which you will take later in
78            processing and define XXX_LEN as length of method name;
79            then just call IFISMETHOD( XXX, 'X' ) ... 'X' is the first
80            latter; everything must be capitals
81         */
82         else {
83                 /* neither reply, nor any of known method requests, 
84                    let's believe it is an unknown method request
85                 */
86                 tmp=eat_token_end(buffer,buffer+len);
87                 if ((tmp==buffer)||(tmp>=end)){
88                         LOG(L_INFO, "ERROR:parse_first_line: empty  or bad first line\n");
89                         goto error1;
90                 }
91                 if (*tmp!=' ') {
92                         LOG(L_INFO, "ERROR:parse_first_line: method not followed by SP\n");
93                         goto error1;
94                 }
95                 fl->type=SIP_REQUEST;
96                 fl->u.request.method_value=METHOD_OTHER;
97                 fl->u.request.method.len=tmp-buffer;
98         }
99
100
101         /* identifying type of message over now; 
102            tmp points at space after; go ahead */
103
104         fl->u.request.method.s=buffer;  /* store ptr to first token */
105         (*tmp)=0;                       /* mark the 1st token end */
106         second=tmp+1;                   /* jump to second token */
107         offset=second-buffer;
108
109 /* EoJku */
110         
111         /* next element */
112         tmp=eat_token_end(second, second+len-offset);
113         if (tmp>=end){
114                 goto error;
115         }
116         offset+=tmp-second;
117         third=eat_space_end(tmp, tmp+len-offset);
118         offset+=third-tmp;
119         if ((third==tmp)||(tmp>=end)){
120                 goto error;
121         }
122         *tmp=0; /* mark the end of the token */
123         fl->u.request.uri.s=second;
124         fl->u.request.uri.len=tmp-second;
125
126         /* jku: parse status code */
127         if (fl->type==SIP_REPLY) {
128                 if (fl->u.request.uri.len!=3) {
129                         LOG(L_INFO, "ERROR:parse_first_line: len(status code)!=3: %s\n",
130                                 second );
131                         goto error;
132                 }
133                 s1=*second; s2=*(second+1);s3=*(second+2);
134                 if (s1>='0' && s1<='9' && 
135                     s2>='0' && s2<='9' &&
136                     s3>='0' && s3<='9' ) {
137                         fl->u.reply.statuscode=(s1-'0')*100+10*(s2-'0')+(s3-'0');
138                 } else {
139                         LOG(L_INFO, "ERROR:parse_first_line: status_code non-numerical: %s\n",
140                                 second );
141                         goto error;
142                 }
143         }
144         /* EoJku */
145
146         /*  last part: for a request it must be the version, for a reply
147          *  it can contain almost anything, including spaces, so we don't care
148          *  about it*/
149         if (fl->type==SIP_REQUEST){
150                 tmp=eat_token_end(third,third+len-offset);
151                 offset+=tmp-third;
152                 if ((tmp==third)||(tmp>=end)){
153                         goto error;
154                 }
155                 if (! is_empty_end(tmp, tmp+len-offset)){
156                         goto error;
157                 }
158         }else{
159                 tmp=eat_token2_end(third,third+len-offset,'\r'); /* find end of line 
160                                                                                                   ('\n' or '\r') */
161                 if (tmp>=end){ /* no crlf in packet => invalid */
162                         goto error;
163                 }
164                 offset+=tmp-third;
165         }
166         nl=eat_line(tmp,len-offset);
167         if (nl>=end){ /* no crlf in packet or only 1 line > invalid */
168                 goto error;
169         }
170         *tmp=0;
171         fl->u.request.version.s=third;
172         fl->u.request.version.len=tmp-third;
173
174         return nl;
175
176 error:
177         LOG(L_INFO, "ERROR:parse_first_line: bad %s first line\n",
178                 (fl->type==SIP_REPLY)?"reply(status)":"request");
179
180         LOG(L_INFO, "ERROR: at line 0 char %d: \n", offset );
181         prn=pkg_malloc( offset );
182         if (prn) {
183                 for (t=0; t<offset; t++)
184                         if (*(buffer+t)) *(prn+t)=*(buffer+t);
185                         else *(prn+t)='°';
186                 LOG(L_INFO, "ERROR: parsed so far: %.*s\n", offset, prn );
187                 pkg_free( prn );
188         };
189 error1:
190         fl->type=SIP_INVALID;
191         LOG(L_INFO, "ERROR:parse_first_line: bad message\n");
192         /* skip  line */
193         nl=eat_line(buffer,len);
194         return nl;
195 }
196
197
198
199 /* returns pointer to next header line, and fill hdr_f ;
200  * if at end of header returns pointer to the last crlf  (always buf)*/
201 char* get_hdr_field(char* buf, char* end, struct hdr_field* hdr)
202 {
203
204         char* tmp;
205         char *match;
206         struct via_body *vb;
207         struct cseq_body* cseq_b;
208         struct to_body* to_b;
209
210         if ((*buf)=='\n' || (*buf)=='\r'){
211                 /* double crlf or lflf or crcr */
212                 DBG("found end of header\n");
213                 hdr->type=HDR_EOH;
214                 return buf;
215         }
216
217         tmp=parse_hname(buf, end, hdr);
218         if (hdr->type==HDR_ERROR){
219                 LOG(L_ERR, "ERROR: get_hdr_field: bad header\n");
220                 goto error;
221         }
222         switch(hdr->type){
223                 case HDR_VIA:
224                         vb=pkg_malloc(sizeof(struct via_body));
225                         if (vb==0){
226                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
227                                 goto error;
228                         }
229                         memset(vb,0,sizeof(struct via_body));
230                         hdr->body.s=tmp;
231                         tmp=parse_via(tmp, end, vb);
232                         if (vb->error==VIA_PARSE_ERROR){
233                                 LOG(L_ERR, "ERROR: get_hdr_field: bad via\n");
234                                 pkg_free(vb);
235                                 goto error;
236                         }
237                         hdr->parsed=vb;
238                         vb->hdr.s=hdr->name.s;
239                         vb->hdr.len=hdr->name.len;
240                         hdr->body.len=tmp-hdr->body.s;
241                         break;
242                 case HDR_CSEQ:
243                         cseq_b=pkg_malloc(sizeof(struct cseq_body));
244                         if (cseq_b==0){
245                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
246                                 goto error;
247                         }
248                         memset(cseq_b, 0, sizeof(struct cseq_body));
249                         hdr->body.s=tmp;
250                         tmp=parse_cseq(tmp, end, cseq_b);
251                         if (cseq_b->error==PARSE_ERROR){
252                                 LOG(L_ERR, "ERROR: get_hdr_field: bad cseq\n");
253                                 pkg_free(cseq_b);
254                                 goto error;
255                         }
256                         hdr->parsed=cseq_b;
257                         hdr->body.len=tmp-hdr->body.s;
258                         DBG("get_hdr_field: cseq <%s>: <%s> <%s>\n",
259                                         hdr->name.s, cseq_b->number.s, cseq_b->method.s);
260                         break;
261                 case HDR_TO:
262                         to_b=pkg_malloc(sizeof(struct to_body));
263                         if (to_b==0){
264                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
265                                 goto error;
266                         }
267                         memset(to_b, 0, sizeof(struct to_body));
268                         hdr->body.s=tmp;
269                         tmp=parse_to(tmp, end,to_b);
270                         if (to_b->error==PARSE_ERROR){
271                                 LOG(L_ERR, "ERROR: get_hdr_field: bad to header\n");
272                                 pkg_free(to_b);
273                                 goto error;
274                         }
275                         hdr->parsed=to_b;
276                         hdr->body.len=tmp-hdr->body.s;
277                         DBG("DEBUG: get_hdr_field: <%s> [%d]; uri=[%.*s] \n",
278                                 hdr->name.s, hdr->body.len, to_b->uri.len,to_b->uri.s);
279                         DBG("DEBUG: to body [%.*s]\n",to_b->body.len,to_b->body.s);
280                         break;
281                 case HDR_CONTENTTYPE:
282                 case HDR_CONTENTLENGTH:
283                 case HDR_FROM:
284                 case HDR_CALLID:
285                 case HDR_CONTACT:
286                 case HDR_ROUTE:   /* janakj, HDR_ROUTE was missing here */
287                 case HDR_RECORDROUTE:
288                 case HDR_MAXFORWARDS:
289                 case HDR_OTHER:
290                         /* just skip over it */
291                         hdr->body.s=tmp;
292                         /* find end of header */
293                         /* find lf */
294                         do{
295                                 match=q_memchr(tmp, '\n', end-tmp);
296                                 if (match){
297                                         match++;
298                                 }else {
299                                         LOG(L_ERR,
300                                                         "ERROR: get_hdr_field: bad body for <%s>(%d)\n",
301                                                         hdr->name.s, hdr->type);
302                                         /* abort(); */
303                                         tmp=end;
304                                         goto error;
305                                 }
306                                 tmp=match;
307                         }while( match<end &&( (*match==' ')||(*match=='\t') ) );
308                         *(match-1)=0; /*null terminate*/
309                         hdr->body.len=match-hdr->body.s;
310                         break;
311                 default:
312                         LOG(L_CRIT, "BUG: get_hdr_field: unknown header type %d\n",
313                                         hdr->type);
314                         goto error;
315         }
316         /* jku: if \r covered by current length, shrink it */
317         trim_r( hdr->body );
318         return tmp;
319 error:
320         DBG("get_hdr_field: error exit\n");
321         hdr->type=HDR_ERROR;
322         return tmp;
323 }
324
325
326
327 char* parse_hostport(char* buf, str* host, short int* port)
328 {
329         char *tmp;
330         int err;
331
332         host->s=buf;
333         for(tmp=buf;(*tmp)&&(*tmp!=':');tmp++);
334         host->len=tmp-buf;
335         if (*tmp==0){
336                 *port=0;
337         }else{
338                 *tmp=0;
339                 *port=str2s((unsigned char*)(tmp+1), strlen(tmp+1), &err);
340                 if (err ){
341                         LOG(L_INFO, 
342                                         "ERROR: hostport: trailing chars in port number: %s\n",
343                                         tmp+1);
344                         /* report error? */
345                 }
346         }
347         return host->s;
348 }
349
350
351
352 /*BUGGY*/
353 char * parse_cseq(char *buf, char* end, struct cseq_body* cb)
354 {
355         char *t, *m, *m_end;
356         char c;
357
358         cb->error=PARSE_ERROR;
359         t=eat_space_end(buf, end);
360         if (t>=end) goto error;
361         
362         cb->number.s=t;
363         t=eat_token_end(t, end);
364         if (t>=end) goto error;
365         m=eat_space_end(t, end);
366         m_end=eat_token_end(m, end);
367         *t=0; /*null terminate it*/
368         cb->number.len=t-cb->number.s;
369
370         if (m_end>=end) goto error;
371         if (m_end==m){
372                 /* null method*/
373                 LOG(L_ERR,  "ERROR:parse_cseq: no method found\n");
374                 goto error;
375         }
376         cb->method.s=m;
377         t=m_end;
378         c=*t;
379         *t=0; /*null terminate it*/
380         cb->method.len=t-cb->method.s;
381         t++;
382         /*check if the header ends here*/
383         if (c=='\n') goto check_continue;
384         do{
385                 for (;(t<end)&&((*t==' ')||(*t=='\t')||(*t=='\r'));t++);
386                 if (t>=end) goto error;
387                 if (*t!='\n'){
388                         LOG(L_ERR, "ERROR:parse_cseq: unexpected char <%c> at end of"
389                                         " cseq\n", *t);
390                         goto error;
391                 }
392                 t++;
393 check_continue:
394                 ;
395         }while( (t<end) && ((*t==' ')||(*t=='\t')) );
396
397         cb->error=PARSE_OK;
398         return t;
399 error:
400         LOG(L_ERR, "ERROR: parse_cseq: bad cseq\n");
401         return t;
402 }
403
404
405
406 /* buf= pointer to begining of uri (sip:x@foo.bar:5060;a=b?h=i)
407    len= len of uri
408 returns: fills uri & returns <0 on error or 0 if ok */
409 int parse_uri(char *buf, int len, struct sip_uri* uri)
410 {
411         char* next, *end;
412         char *user, *passwd, *host, *port, *params, *headers;
413         int host_len, port_len, params_len, headers_len;
414         int ret;
415         
416
417         ret=0;
418         end=buf+len;
419         memset(uri, 0, sizeof(struct sip_uri)); /* zero it all, just to be sure */
420         /* look for "sip:"*/;
421         next=q_memchr(buf, ':',  len);
422         if ((next==0)||(strncmp(buf,"sip",next-buf)!=0)){
423                 LOG(L_DBG, "ERROR: parse_uri: bad sip uri\n");
424                 ret=E_UNSPEC;
425                 goto error;
426         }
427         buf=next+1; /* next char after ':' */
428         if (buf>end){
429                 LOG(L_DBG, "ERROR: parse_uri: uri too short\n");
430                 ret=E_UNSPEC;
431                 goto error;
432         }
433         /*look for '@' */
434         next=q_memchr(buf,'@', end-buf);
435         if (next==0){
436                 /* no '@' found, => no userinfo */
437                 uri->user.s=0;
438                 uri->passwd.s=0;
439                 host=buf;
440         }else{
441                 /* found it */
442                 user=buf;
443                 /* try to find passwd */
444                 passwd=q_memchr(user,':', next-user);
445                 if (passwd==0){
446                         /* no ':' found => no password */
447                         uri->passwd.s=0;
448                         uri->user.s=(char*)pkg_malloc(next-user+1);
449                         if (uri->user.s==0){
450                                 LOG(L_ERR,"ERROR:parse_uri: memory allocation failure\n");
451                                 ret=E_OUT_OF_MEM;
452                                 goto error;
453                         }
454                         memcpy(uri->user.s, user, next-user);
455                         uri->user.len=next-user;
456                         uri->user.s[next-user]=0; /* null terminate it, 
457                                                                            usefull for easy printing*/
458                 }else{
459                         uri->user.s=(char*)pkg_malloc(passwd-user+1);
460                         if (uri->user.s==0){
461                                 LOG(L_ERR,"ERROR:parse_uri: memory allocation failure\n");
462                                 ret=E_OUT_OF_MEM;
463                                 goto error;
464                         }
465                         memcpy(uri->user.s, user, passwd-user);
466                         uri->user.len=passwd-user;
467                         uri->user.s[passwd-user]=0;
468                         passwd++; /*skip ':' */
469                         uri->passwd.s=(char*)pkg_malloc(next-passwd+1);
470                         if (uri->passwd.s==0){
471                                 LOG(L_ERR,"ERROR:parse_uri: memory allocation failure\n");
472                                 ret=E_OUT_OF_MEM;
473                                 goto error;
474                         }
475                         memcpy(uri->passwd.s, passwd, next-passwd);
476                         uri->passwd.len=next-passwd;
477                         uri->passwd.s[next-passwd]=0;
478                 }
479                 host=next+1; /* skip '@' */
480         }
481         /* try to find the rest */
482         if(host>=end){
483                 LOG(L_DBG, "ERROR: parse_uri: missing hostport\n");
484                 ret=E_UNSPEC;
485                 goto error;
486         }
487         headers=q_memchr(host,'?',end-host);
488         params=q_memchr(host,';',end-host);
489         port=q_memchr(host,':',end-host);
490         host_len=(port)?port-host:(params)?params-host:(headers)?headers-host:
491                 end-host;
492         /* get host */
493         uri->host.s=pkg_malloc(host_len+1);
494         if (uri->host.s==0){
495                 LOG(L_ERR, "ERROR: parse_uri: memory allocation error\n");
496                 ret=E_OUT_OF_MEM;
497                 goto error;
498         }
499         memcpy(uri->host.s, host, host_len);
500         uri->host.len=host_len;
501         uri->host.s[host_len]=0;
502         /* get port*/
503         if ((port)&&(port+1<end)){
504                 port++;
505                 if ( ((params) &&(params<port))||((headers) &&(headers<port)) ){
506                         /* error -> invalid uri we found ';' or '?' before ':' */
507                         LOG(L_DBG, "ERROR: parse_uri: malformed sip uri\n");
508                         ret=E_UNSPEC;
509                         goto error;
510                 }
511                 port_len=(params)?params-port:(headers)?headers-port:end-port;
512                 uri->port.s=pkg_malloc(port_len+1);
513                 if (uri->port.s==0){
514                         LOG(L_ERR, "ERROR: parse_uri: memory allocation error\n");
515                         ret=E_OUT_OF_MEM;
516                         goto error;
517                 }
518                 memcpy(uri->port.s, port, port_len);
519                 uri->port.len=port_len;
520                 uri->port.s[port_len]=0;
521         }else uri->port.s=0;
522         /* get params */
523         if ((params)&&(params+1<end)){
524                 params++;
525                 if ((headers) && (headers<params)){
526                         /* error -> invalid uri we found '?' or '?' before ';' */
527                         LOG(L_DBG, "ERROR: parse_uri: malformed sip uri\n");
528                         ret=E_UNSPEC;
529                         goto error;
530                 }
531                 params_len=(headers)?headers-params:end-params;
532                 uri->params.s=pkg_malloc(params_len+1);
533                 if (uri->params.s==0){
534                         LOG(L_ERR, "ERROR: parse_uri: memory allocation error\n");
535                         ret=E_OUT_OF_MEM;
536                         goto error;
537                 }
538                 memcpy(uri->params.s, params, params_len);
539                 uri->params.len=params_len;
540                 uri->params.s[params_len]=0;
541         }else uri->params.s=0;
542         /*get headers */
543         if ((headers)&&(headers+1<end)){
544                 headers++;
545                 headers_len=end-headers;
546                 uri->headers.s=pkg_malloc(headers_len+1);
547                 if(uri->headers.s==0){
548                         LOG(L_ERR, "ERROR: parse_uri: memory allocation error\n");
549                         ret=E_OUT_OF_MEM;
550                         goto error;
551                 }
552                 memcpy(uri->headers.s, headers, headers_len);
553                 uri->headers.len=headers_len;
554                 uri->headers.s[headers_len]=0;
555         }else uri->headers.s=0;
556         
557         return ret;
558 error:
559         free_uri(uri);
560         return ret;
561 }
562
563
564 /* parse the headers and adds them to msg->headers and msg->to, from etc.
565  * It stops when all the headers requested in flags were parsed, on error
566  * (bad header) or end of headers */
567 /* note: it continues where it previously stopped and goes ahead until
568    end is encountered or desired HFs are found; if you call it twice
569    for the same HF which is present only once, it will fail the second
570    time; if you call it twice and the HF is found on second time too,
571    it's not replaced in the well-known HF pointer but just added to
572    header list; if you want to use a dumbie convenience function which will
573    give you the first occurance of a header you are interested in,
574    look at check_transaction_quadruple
575 */
576 int parse_headers(struct sip_msg* msg, int flags)
577 {
578         struct hdr_field* hf;
579         char* tmp;
580         char* rest;
581         char* end;
582
583         end=msg->buf+msg->len;
584         tmp=msg->unparsed;
585
586         DBG("parse_headers: flags=%d\n", flags);
587         while( tmp<end && (flags & msg->parsed_flag) != flags){
588                 hf=pkg_malloc(sizeof(struct hdr_field));
589                 if (hf==0){
590                         LOG(L_ERR, "ERROR:parse_headers: memory allocation error\n");
591                         goto error;
592                 }
593                 memset(hf,0, sizeof(struct hdr_field));
594                 hf->type=HDR_ERROR;
595                 rest=get_hdr_field(tmp, msg->buf+msg->len, hf);
596                 switch (hf->type){
597                         case HDR_ERROR:
598                                 LOG(L_INFO,"ERROR: bad header  field\n");
599                                 goto  error;
600                         case HDR_EOH:
601                                 msg->eoh=tmp; /* or rest?*/
602                                 msg->parsed_flag|=HDR_EOH;
603                                 pkg_free(hf);
604                                 goto skip;
605                         case HDR_OTHER: /*do nothing*/
606                                 break;
607                         case HDR_CALLID:
608                                 if (msg->callid==0) msg->callid=hf;
609                                 msg->parsed_flag|=HDR_CALLID;
610                                 break;
611                         case HDR_TO:
612                                 if (msg->to==0) msg->to=hf;
613                                 msg->parsed_flag|=HDR_TO;
614                                 break;
615                         case HDR_CSEQ:
616                                 if (msg->cseq==0) msg->cseq=hf;
617                                 msg->parsed_flag|=HDR_CSEQ;
618                                 break;
619                         case HDR_FROM:
620                                 if (msg->from==0) msg->from=hf;
621                                 msg->parsed_flag|=HDR_FROM;
622                                 break;
623                         case HDR_CONTACT:
624                                 if (msg->contact==0) msg->contact=hf;
625                                 msg->parsed_flag|=HDR_CONTACT;
626                                 break;
627                         case HDR_MAXFORWARDS:
628                                 if(msg->maxforwards==0) msg->maxforwards=hf;
629                                 msg->parsed_flag|=HDR_MAXFORWARDS;
630                                 break;
631                         case HDR_ROUTE:
632                                 if (msg->route==0) msg->route=hf;
633                                 msg->parsed_flag|=HDR_ROUTE;
634                                 break;
635                         case HDR_RECORDROUTE:
636                                 if (msg->record_route==0) msg->record_route = hf;
637                                 msg->parsed_flag|=HDR_RECORDROUTE;
638                                 break;
639                         case HDR_CONTENTTYPE:
640                                 if (msg->content_type==0) msg->content_type = hf;
641                                 msg->parsed_flag|=HDR_CONTENTTYPE;
642                                 break;
643                         case HDR_CONTENTLENGTH:
644                                 if (msg->content_length==0) msg->content_length = hf;
645                                 msg->parsed_flag|=HDR_CONTENTLENGTH;
646                                 break;
647                         case HDR_VIA:
648                                 msg->parsed_flag|=HDR_VIA;
649                                 DBG("parse_headers: Via found, flags=%d\n", flags);
650                                 if (msg->via1==0) {
651                                         DBG("parse_headers: this is the first via\n");
652                                         msg->h_via1=hf;
653                                         msg->via1=hf->parsed;
654                                         if (msg->via1->next){
655                                                 msg->via2=msg->via1->next;
656                                                 msg->parsed_flag|=HDR_VIA2;
657                                         }
658                                 }else if (msg->via2==0){
659                                         msg->h_via2=hf;
660                                         msg->via2=hf->parsed;
661                                         msg->parsed_flag|=HDR_VIA2;
662                                         DBG("parse_headers: this is the second via\n");
663                                 }
664                                 break;
665                         default:
666                                 LOG(L_CRIT, "BUG: parse_headers: unknown header type %d\n",
667                                                         hf->type);
668                                 goto error;
669                 }
670                 /* add the header to the list*/
671                 if (msg->last_header==0){
672                         msg->headers=hf;
673                         msg->last_header=hf;
674                 }else{
675                         msg->last_header->next=hf;
676                         msg->last_header=hf;
677                 }
678         #ifdef EXTRA_DEBUG
679                 DBG("header field type %d, name=<%s>, body=<%s>\n",
680                         hf->type, hf->name.s, hf->body.s);
681         #endif
682                 tmp=rest;
683         }
684 skip:
685         msg->unparsed=tmp;
686         return 0;
687
688 error:
689         if (hf) pkg_free(hf);
690         return -1;
691 }
692
693
694
695
696
697 /* returns 0 if ok, -1 for errors */
698 int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
699 {
700
701         char *tmp;
702         char* rest;
703         char* first_via;
704         char* second_via;
705         struct msg_start *fl;
706         int offset;
707         int flags;
708
709         /* eat crlf from the beginning */
710         for (tmp=buf; (*tmp=='\n' || *tmp=='\r')&&
711                         tmp-buf < len ; tmp++);
712         offset=tmp-buf;
713         fl=&(msg->first_line);
714         rest=parse_first_line(tmp, len-offset, fl);
715 #if 0
716         rest=parse_fline(tmp, buf+len, fl);
717 #endif
718         offset+=rest-tmp;
719         tmp=rest;
720         switch(fl->type){
721                 case SIP_INVALID:
722                         DBG("parse_msg: invalid message\n");
723                         goto error;
724                         break;
725                 case SIP_REQUEST:
726                         DBG("SIP Request:\n");
727                         DBG(" method:  <%s>\n",fl->u.request.method.s);
728                         DBG(" uri:     <%s>\n",fl->u.request.uri.s);
729                         DBG(" version: <%s>\n",fl->u.request.version.s);
730                         flags=HDR_VIA;
731                         break;
732                 case SIP_REPLY:
733                         DBG("SIP Reply  (status):\n");
734                         DBG(" version: <%s>\n",fl->u.reply.version.s);
735                         DBG(" status:  <%s>\n",fl->u.reply.status.s);
736                         DBG(" reason:  <%s>\n",fl->u.reply.reason.s);
737                         flags=HDR_VIA | HDR_VIA2;
738                         break;
739                 default:
740                         DBG("unknown type %d\n",fl->type);
741                         goto error;
742         }
743         msg->unparsed=tmp;
744         /*find first Via: */
745         first_via=0;
746         second_via=0;
747         if (parse_headers(msg, flags)==-1) goto error;
748
749 #ifdef EXTRA_DEBUG
750         /* dump parsed data */
751         if (msg->via1){
752                 DBG(" first  via: <%s/%s/%s> <%s:%s(%d)>",
753                         msg->via1->name.s, msg->via1->version.s,
754                         msg->via1->transport.s, msg->via1->host.s,
755                         msg->via1->port_str.s, msg->via1->port);
756                 if (msg->via1->params.s)  DBG(";<%s>", msg->via1->params.s);
757                 if (msg->via1->comment.s) DBG(" <%s>", msg->via1->comment.s);
758                 DBG ("\n");
759         }
760         if (msg->via2){
761                 DBG(" first  via: <%s/%s/%s> <%s:%s(%d)>",
762                         msg->via2->name.s, msg->via2->version.s,
763                         msg->via2->transport.s, msg->via2->host.s,
764                         msg->via2->port_str.s, msg->via2->port);
765                 if (msg->via2->params.s)  DBG(";<%s>", msg->via2->params.s);
766                 if (msg->via2->comment.s) DBG(" <%s>", msg->via2->comment.s);
767                 DBG ("\n");
768         }
769 #endif
770         
771
772 #ifdef EXTRA_DEBUG
773         DBG("exiting parse_msg\n");
774 #endif
775
776         return 0;
777         
778 error:
779         /* more debugging, msg->orig is/should be null terminated*/
780         LOG(L_ERR, "ERROR: parse_msg: message=<%s>\n", msg->orig);
781         return -1;
782 }
783
784
785
786 void free_uri(struct sip_uri* u)
787 {
788         if (u){
789                 if (u->user.s)    pkg_free(u->user.s);
790                 if (u->passwd.s)  pkg_free(u->passwd.s);
791                 if (u->host.s)    pkg_free(u->host.s);
792                 if (u->port.s)    pkg_free(u->port.s);
793                 if (u->params.s)  pkg_free(u->params.s);
794                 if (u->headers.s) pkg_free(u->headers.s);
795         }
796 }
797
798 void free_reply_lump( struct lump_rpl *lump)
799 {
800         struct lump_rpl *foo, *bar;
801         for(foo=lump;foo;)
802         {
803                 bar=foo->next;
804                 free_lump_rpl(foo);
805                 foo = bar;
806         }
807 }
808
809 void free_via_param_list(struct via_param* vp)
810 {
811         struct via_param* foo;
812         while(vp){
813                 foo=vp;
814                 vp=vp->next;
815                 pkg_free(foo);
816         }
817 }
818
819
820
821 void free_via_list(struct via_body* vb)
822 {
823         struct via_body* foo;
824         while(vb){
825                 foo=vb;
826                 vb=vb->next;
827                 if (foo->param_lst) free_via_param_list(foo->param_lst);
828                 pkg_free(foo);
829         }
830 }
831
832 void free_to(struct to_body* tb)
833 {
834         struct to_param *tp=tb->param_lst;
835         struct to_param *foo;
836         while (tp){
837                 foo = tp->next;
838                 pkg_free(tp);
839                 tp=foo;
840         }
841         pkg_free(tb);
842 }
843
844 /* frees a hdr_field structure,
845  * WARNING: it frees only parsed (and not name.s, body.s)*/
846 void clean_hdr_field(struct hdr_field* hf)
847 {
848         if (hf->parsed){
849                 switch(hf->type){
850                         case HDR_VIA:
851                                 free_via_list(hf->parsed);
852                                 break;
853                         case HDR_TO:
854                                 free_to(hf->parsed);
855                                 break;
856                         case HDR_CSEQ:
857                                 pkg_free(hf->parsed);
858                                 break;
859                         default:
860                                 LOG(L_CRIT, "BUG: clean_hdr_field: unknown header type %d\n",
861                                                 hf->type);
862                 }
863         }
864 }
865
866
867
868 /* frees a hdr_field list,
869  * WARNING: frees only ->parsed and ->next*/
870 void free_hdr_field_lst(struct hdr_field* hf)
871 {
872         struct hdr_field* foo;
873         
874         while(hf){
875                 foo=hf;
876                 hf=hf->next;
877                 clean_hdr_field(foo);
878                 pkg_free(foo);
879         }
880 }
881
882
883
884 /*only the content*/
885 void free_sip_msg(struct sip_msg* msg)
886 {
887         if (msg->new_uri.s) { pkg_free(msg->new_uri.s); msg->new_uri.len=0; }
888         if (msg->headers)     free_hdr_field_lst(msg->headers);
889         if (msg->add_rm)      free_lump_list(msg->add_rm);
890         if (msg->repl_add_rm) free_lump_list(msg->repl_add_rm);
891         if (msg->reply_lump)   free_reply_lump(msg->reply_lump);
892         pkg_free(msg->orig);
893         /* don't free anymore -- now a pointer to a static buffer */
894 #       ifdef DYN_BUF
895         pkg_free(msg->buf); 
896 #       endif
897 }
898
899
900 #if 0
901 /* it's a macro now*/
902 /* make sure all HFs needed for transaction identification have been
903    parsed; return 0 if those HFs can't be found
904 */
905 int check_transaction_quadruple( struct sip_msg* msg )
906 {
907    return 
908         (parse_headers(msg, HDR_FROM|HDR_TO|HDR_CALLID|HDR_CSEQ)!=-1 &&
909          msg->from && msg->to && msg->callid && msg->cseq);
910   /* replaced by me ( :) andrei)
911    ( (msg->from || (parse_headers( msg, HDR_FROM)!=-1 && msg->from)) &&
912    (msg->to|| (parse_headers( msg, HDR_TO)!=-1 && msg->to)) &&
913    (msg->callid|| (parse_headers( msg, HDR_CALLID)!=-1 && msg->callid)) &&
914    (msg->cseq|| (parse_headers( msg, HDR_CSEQ)!=-1 && msg->cseq)) ) ? 1 : 0;
915   */
916
917 }
918 #endif