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