scratchpad removed, textops changed to skip the first line
[sip-router] / parser / msg_parser.c
1 /*
2  * $Id$
3  *
4  * sip msg. header proxy parser 
5  *
6  *
7  * Copyright (C) 2001-2003 Fhg Fokus
8  *
9  * This file is part of ser, a free SIP server.
10  *
11  * ser is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version
15  *
16  * For a license to use the ser software under conditions
17  * other than those described here, or to purchase support for this
18  * software, please contact iptel.org by e-mail at the following addresses:
19  *    info@iptel.org
20  *
21  * ser is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License 
27  * along with this program; if not, write to the Free Software 
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29  *
30  * History:
31  * ---------
32  * 2003-01-29 scrathcpad removed (jiri)
33  * 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
34  */
35
36
37 #include <string.h>
38 #include <stdlib.h>
39
40 #include "../comp_defs.h"
41 #include "msg_parser.h"
42 #include "parser_f.h"
43 #include "../ut.h"
44 #include "../error.h"
45 #include "../dprint.h"
46 #include "../data_lump_rpl.h"
47 #include "../mem/mem.h"
48 #include "../error.h"
49 #include "../globals.h"
50 #include "parse_hname2.h"
51 #include "parse_uri.h"
52 #include "parse_content.h"
53
54 #ifdef DEBUG_DMALLOC
55 #include <mem/dmalloc.h>
56 #endif
57
58
59 #define parse_hname(_b,_e,_h) parse_hname2((_b),(_e),(_h))
60
61 /* number of via's encounteded */
62 int via_cnt;
63
64 /* returns pointer to next header line, and fill hdr_f ;
65  * if at end of header returns pointer to the last crlf  (always buf)*/
66 char* get_hdr_field(char* buf, char* end, struct hdr_field* hdr)
67 {
68
69         char* tmp;
70         char *match;
71         struct via_body *vb;
72         struct cseq_body* cseq_b;
73         struct to_body* to_b;
74         int integer;
75
76         if ((*buf)=='\n' || (*buf)=='\r'){
77                 /* double crlf or lflf or crcr */
78                 DBG("found end of header\n");
79                 hdr->type=HDR_EOH;
80                 return buf;
81         }
82
83         tmp=parse_hname(buf, end, hdr);
84         if (hdr->type==HDR_ERROR){
85                 LOG(L_ERR, "ERROR: get_hdr_field: bad header\n");
86                 goto error;
87         }
88
89 #ifndef PRESERVE_ZT
90         /* eliminate leading whitespace */
91         tmp=eat_lws_end(tmp, end);
92         if (tmp>=end) {
93                 LOG(L_ERR, "ERROR: get_hdr_field: HF empty\n");
94                 goto error;
95         }
96 #else
97         ;
98 #endif
99
100         /* if header-field well-known, parse it, find its end otherwise ;
101          * after leaving the hdr->type switch, tmp should be set to the
102          * next header field
103          */
104         switch(hdr->type){
105                 case HDR_VIA:
106                         /* keep number of vias parsed -- we want to report it in
107                            replies for diagnostic purposes */
108                         via_cnt++;
109                         vb=pkg_malloc(sizeof(struct via_body));
110                         if (vb==0){
111                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
112                                 goto error;
113                         }
114                         memset(vb,0,sizeof(struct via_body));
115                         hdr->body.s=tmp;
116                         tmp=parse_via(tmp, end, vb);
117                         if (vb->error==PARSE_ERROR){
118                                 LOG(L_ERR, "ERROR: get_hdr_field: bad via\n");
119                                 pkg_free(vb);
120                                 goto error;
121                         }
122                         hdr->parsed=vb;
123                         vb->hdr.s=hdr->name.s;
124                         vb->hdr.len=hdr->name.len;
125                         hdr->body.len=tmp-hdr->body.s;
126                         break;
127                 case HDR_CSEQ:
128                         cseq_b=pkg_malloc(sizeof(struct cseq_body));
129                         if (cseq_b==0){
130                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
131                                 goto error;
132                         }
133                         memset(cseq_b, 0, sizeof(struct cseq_body));
134                         hdr->body.s=tmp;
135                         tmp=parse_cseq(tmp, end, cseq_b);
136                         if (cseq_b->error==PARSE_ERROR){
137                                 LOG(L_ERR, "ERROR: get_hdr_field: bad cseq\n");
138                                 pkg_free(cseq_b);
139                                 goto error;
140                         }
141                         hdr->parsed=cseq_b;
142                         hdr->body.len=tmp-hdr->body.s;
143                         DBG("get_hdr_field: cseq <%.*s>: <%.*s> <%.*s>\n",
144                                         hdr->name.len, hdr->name.s, 
145                                         cseq_b->number.len, cseq_b->number.s, 
146                                         cseq_b->method.len, cseq_b->method.s);
147                         break;
148                 case HDR_TO:
149                         to_b=pkg_malloc(sizeof(struct to_body));
150                         if (to_b==0){
151                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
152                                 goto error;
153                         }
154                         memset(to_b, 0, sizeof(struct to_body));
155                         hdr->body.s=tmp;
156                         tmp=parse_to(tmp, end,to_b);
157                         if (to_b->error==PARSE_ERROR){
158                                 LOG(L_ERR, "ERROR: get_hdr_field: bad to header\n");
159                                 pkg_free(to_b);
160                                 goto error;
161                         }
162                         hdr->parsed=to_b;
163                         hdr->body.len=tmp-hdr->body.s;
164                         DBG("DEBUG: get_hdr_field: <%.*s> [%d]; uri=[%.*s] \n",
165                                 hdr->name.len, hdr->name.s, 
166                                 hdr->body.len, to_b->uri.len,to_b->uri.s);
167                         DBG("DEBUG: to body [%.*s]\n",to_b->body.len,to_b->body.s);
168                         break;
169                 case HDR_CONTENTLENGTH:
170                         hdr->body.s=tmp;
171                         tmp=parse_content_length(tmp,end, &integer);
172                         if (tmp==0){
173                                 LOG(L_ERR, "ERROR:get_hdr_field: bad content_length header\n");
174                                 goto error;
175                         }
176                         hdr->parsed=(void*)integer;
177                         hdr->body.len=tmp-hdr->body.s;
178                         DBG("DEBUG: get_hdr_body : content_length=%d\n",(int)hdr->parsed);
179                         break;
180                 case HDR_CONTENTTYPE:
181                 case HDR_FROM:
182                 case HDR_CALLID:
183                 case HDR_CONTACT:
184                 case HDR_ROUTE:
185                 case HDR_RECORDROUTE:
186                 case HDR_MAXFORWARDS:
187                 case HDR_AUTHORIZATION:
188                 case HDR_EXPIRES:
189                 case HDR_PROXYAUTH:
190                 case HDR_WWWAUTH:
191                 case HDR_SUPPORTED:
192                 case HDR_REQUIRE:
193                 case HDR_PROXYREQUIRE:
194                 case HDR_UNSUPPORTED:
195                 case HDR_ALLOW:
196                 case HDR_EVENT:
197                 case HDR_OTHER:
198                         /* just skip over it */
199                         hdr->body.s=tmp;
200                         /* find end of header */
201                         /* find lf */
202                         do{
203                                 match=q_memchr(tmp, '\n', end-tmp);
204                                 if (match){
205                                         match++;
206                                 }else {
207                                         LOG(L_ERR,
208                                                         "ERROR: get_hdr_field: bad body for <%s>(%d)\n",
209                                                         hdr->name.s, hdr->type);
210                                         /* abort(); */
211                                         tmp=end;
212                                         goto error;
213                                 }
214                                 tmp=match;
215                         }while( match<end &&( (*match==' ')||(*match=='\t') ) );
216 #ifdef PRESERVE_ZT
217                         *(match-1)=0; /*null terminate*/
218 #else
219                         tmp=match;
220 #endif
221                         hdr->body.len=match-hdr->body.s;
222                         break;
223                 default:
224                         LOG(L_CRIT, "BUG: get_hdr_field: unknown header type %d\n",
225                                         hdr->type);
226                         goto error;
227         }
228         /* jku: if \r covered by current length, shrink it */
229         trim_r( hdr->body );
230 #ifndef PRESERVE_ZT
231         hdr->len=tmp-hdr->name.s;
232 #endif
233         return tmp;
234 error:
235         DBG("get_hdr_field: error exit\n");
236         hdr->type=HDR_ERROR;
237 #ifndef PRESERVE_ZT
238         hdr->len=tmp-hdr->name.s;
239 #endif
240         return tmp;
241 }
242
243
244
245 /* parse the headers and adds them to msg->headers and msg->to, from etc.
246  * It stops when all the headers requested in flags were parsed, on error
247  * (bad header) or end of headers */
248 /* note: it continues where it previously stopped and goes ahead until
249    end is encountered or desired HFs are found; if you call it twice
250    for the same HF which is present only once, it will fail the second
251    time; if you call it twice and the HF is found on second time too,
252    it's not replaced in the well-known HF pointer but just added to
253    header list; if you want to use a dumbie convenience function which will
254    give you the first occurance of a header you are interested in,
255    look at check_transaction_quadruple
256 */
257 int parse_headers(struct sip_msg* msg, int flags, int next)
258 {
259         struct hdr_field* hf;
260         char* tmp;
261         char* rest;
262         char* end;
263         int orig_flag;
264
265         end=msg->buf+msg->len;
266         tmp=msg->unparsed;
267         
268         if (next) {
269                 orig_flag = msg->parsed_flag;
270                 msg->parsed_flag &= ~flags;
271         }else
272                 orig_flag=0; 
273         
274         DBG("parse_headers: flags=%d\n", flags);
275         while( tmp<end && (flags & msg->parsed_flag) != flags){
276                 hf=pkg_malloc(sizeof(struct hdr_field));
277                 if (hf==0){
278                         ser_error=E_OUT_OF_MEM;
279                         LOG(L_ERR, "ERROR:parse_headers: memory allocation error\n");
280                         goto error;
281                 }
282                 memset(hf,0, sizeof(struct hdr_field));
283                 hf->type=HDR_ERROR;
284                 rest=get_hdr_field(tmp, msg->buf+msg->len, hf);
285                 switch (hf->type){
286                         case HDR_ERROR:
287                                 LOG(L_INFO,"ERROR: bad header  field\n");
288                                 goto  error;
289                         case HDR_EOH:
290                                 msg->eoh=tmp; /* or rest?*/
291                                 msg->parsed_flag|=HDR_EOH;
292                                 pkg_free(hf);
293                                 goto skip;
294                         case HDR_OTHER: /*do nothing*/
295                                 break;
296                         case HDR_CALLID:
297                                 if (msg->callid==0) msg->callid=hf;
298                                 msg->parsed_flag|=HDR_CALLID;
299                                 break;
300                         case HDR_TO:
301                                 if (msg->to==0) msg->to=hf;
302                                 msg->parsed_flag|=HDR_TO;
303                                 break;
304                         case HDR_CSEQ:
305                                 if (msg->cseq==0) msg->cseq=hf;
306                                 msg->parsed_flag|=HDR_CSEQ;
307                                 break;
308                         case HDR_FROM:
309                                 if (msg->from==0) msg->from=hf;
310                                 msg->parsed_flag|=HDR_FROM;
311                                 break;
312                         case HDR_CONTACT:
313                                 if (msg->contact==0) msg->contact=hf;
314                                 msg->parsed_flag|=HDR_CONTACT;
315                                 break;
316                         case HDR_MAXFORWARDS:
317                                 if(msg->maxforwards==0) msg->maxforwards=hf;
318                                 msg->parsed_flag|=HDR_MAXFORWARDS;
319                                 break;
320                         case HDR_ROUTE:
321                                 if (msg->route==0) msg->route=hf;
322                                 msg->parsed_flag|=HDR_ROUTE;
323                                 break;
324                         case HDR_RECORDROUTE:
325                                 if (msg->record_route==0) msg->record_route = hf;
326                                 msg->parsed_flag|=HDR_RECORDROUTE;
327                                 break;
328                         case HDR_CONTENTTYPE:
329                                 if (msg->content_type==0) msg->content_type = hf;
330                                 msg->parsed_flag|=HDR_CONTENTTYPE;
331                                 break;
332                         case HDR_CONTENTLENGTH:
333                                 if (msg->content_length==0) msg->content_length = hf;
334                                 msg->parsed_flag|=HDR_CONTENTLENGTH;
335                                 break;
336                         case HDR_AUTHORIZATION:
337                                 if (msg->authorization==0) msg->authorization = hf;
338                                 msg->parsed_flag|=HDR_AUTHORIZATION;
339                                 break;
340                         case HDR_EXPIRES:
341                                 if (msg->expires==0) msg->expires = hf;
342                                 msg->parsed_flag|=HDR_EXPIRES;
343                                 break;
344                         case HDR_PROXYAUTH:
345                                 if (msg->proxy_auth==0) msg->proxy_auth = hf;
346                                 msg->parsed_flag|=HDR_PROXYAUTH;
347                                 break;
348                         case HDR_WWWAUTH:
349                                 if (msg->www_auth==0) msg->www_auth = hf;
350                                 msg->parsed_flag|=HDR_WWWAUTH;
351                                 break;
352                         case HDR_SUPPORTED:
353                                 if (msg->supported==0) msg->supported = hf;
354                                 msg->parsed_flag|=HDR_SUPPORTED;
355                                 break;
356                         case HDR_REQUIRE:
357                                 if (msg->require==0) msg->require = hf;
358                                 msg->parsed_flag|=HDR_REQUIRE;
359                                 break;
360                         case HDR_PROXYREQUIRE:
361                                 if (msg->proxy_require==0) msg->proxy_require = hf;
362                                 msg->parsed_flag|=HDR_PROXYREQUIRE;
363                                 break;
364                         case HDR_UNSUPPORTED:
365                                 if (msg->unsupported==0) msg->unsupported=hf;
366                                 msg->parsed_flag|=HDR_UNSUPPORTED;
367                                 break;
368                         case HDR_ALLOW:
369                                 if (msg->allow==0) msg->allow = hf;
370                                 msg->parsed_flag|=HDR_ALLOW;
371                                 break;
372                         case HDR_EVENT:
373                                 if (msg->allow==0) msg->event = hf;
374                                 msg->parsed_flag|=HDR_EVENT;
375                                 break;
376                         case HDR_VIA:
377                                 msg->parsed_flag|=HDR_VIA;
378                                 DBG("parse_headers: Via found, flags=%d\n", flags);
379                                 if (msg->via1==0) {
380                                         DBG("parse_headers: this is the first via\n");
381                                         msg->h_via1=hf;
382                                         msg->via1=hf->parsed;
383                                         if (msg->via1->next){
384                                                 msg->via2=msg->via1->next;
385                                                 msg->parsed_flag|=HDR_VIA2;
386                                         }
387                                 }else if (msg->via2==0){
388                                         msg->h_via2=hf;
389                                         msg->via2=hf->parsed;
390                                         msg->parsed_flag|=HDR_VIA2;
391                                         DBG("parse_headers: this is the second via\n");
392                                 }
393                                 break;
394                         default:
395                                 LOG(L_CRIT, "BUG: parse_headers: unknown header type %d\n",
396                                                         hf->type);
397                                 goto error;
398                 }
399                 /* add the header to the list*/
400                 if (msg->last_header==0){
401                         msg->headers=hf;
402                         msg->last_header=hf;
403                 }else{
404                         msg->last_header->next=hf;
405                         msg->last_header=hf;
406                 }
407 #ifdef EXTRA_DEBUG
408                 DBG("header field type %d, name=<%.*s>, body=<%.*s>\n",
409                         hf->type, 
410                         hf->name.len, hf->name.s, 
411                         hf->body.len, hf->body.s);
412 #endif
413                 tmp=rest;
414         }
415 skip:
416         msg->unparsed=tmp;
417         return 0;
418
419 error:
420         ser_error=E_BAD_REQ;
421         if (hf) pkg_free(hf);
422         if (next) msg->parsed_flag |= orig_flag;
423         return -1;
424 }
425
426
427
428
429
430 /* returns 0 if ok, -1 for errors */
431 int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
432 {
433
434         char *tmp;
435         char* rest;
436         char* first_via;
437         char* second_via;
438         struct msg_start *fl;
439         int offset;
440         int flags;
441
442         /* eat crlf from the beginning */
443         for (tmp=buf; (*tmp=='\n' || *tmp=='\r')&&
444                         tmp-buf < len ; tmp++);
445         offset=tmp-buf;
446         fl=&(msg->first_line);
447         rest=parse_first_line(tmp, len-offset, fl);
448 #if 0
449         rest=parse_fline(tmp, buf+len, fl);
450 #endif
451         offset+=rest-tmp;
452         tmp=rest;
453         switch(fl->type){
454                 case SIP_INVALID:
455                         DBG("parse_msg: invalid message\n");
456                         goto error;
457                         break;
458                 case SIP_REQUEST:
459                         DBG("SIP Request:\n");
460                         DBG(" method:  <%.*s>\n",fl->u.request.method.len,
461                                 fl->u.request.method.s);
462                         DBG(" uri:     <%.*s>\n",fl->u.request.uri.len,
463                                 fl->u.request.uri.s);
464                         DBG(" version: <%.*s>\n",fl->u.request.version.len,
465                                 fl->u.request.version.s);
466                         flags=HDR_VIA;
467                         break;
468                 case SIP_REPLY:
469                         DBG("SIP Reply  (status):\n");
470                         DBG(" version: <%.*s>\n",fl->u.reply.version.len,
471                                         fl->u.reply.version.s);
472                         DBG(" status:  <%.*s>\n", fl->u.reply.status.len,
473                                         fl->u.reply.status.s);
474                         DBG(" reason:  <%.*s>\n", fl->u.reply.reason.len,
475                                         fl->u.reply.reason.s);
476                         /* flags=HDR_VIA | HDR_VIA2; */
477                         /* we don't try to parse VIA2 for local messages; -Jiri */
478                         flags=HDR_VIA;
479                         break;
480                 default:
481                         DBG("unknown type %d\n",fl->type);
482                         goto error;
483         }
484         msg->unparsed=tmp;
485         /*find first Via: */
486         first_via=0;
487         second_via=0;
488         if (parse_headers(msg, flags, 0)==-1) goto error;
489
490 #ifdef EXTRA_DEBUG
491         /* dump parsed data */
492         if (msg->via1){
493                 DBG(" first  via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
494                         msg->via1->name.len, 
495                         msg->via1->name.s, 
496                         msg->via1->version.len,
497                         msg->via1->version.s,
498                         msg->via1->transport.len 
499                         msg->via1->transport.s, 
500                         msg->via1->host.len,
501                         msg->via1->host.s,
502                         msg->via1->port_str.len, 
503                         msg->via1->port_str.s, 
504                         msg->via1->port);
505                 if (msg->via1->params.s)  DBG(";<%.*s>", 
506                                 msg->via1->params.len, msg->via1->params.s);
507                 if (msg->via1->comment.s) 
508                                 DBG(" <%.*s>", 
509                                         msg->via1->comment.len, msg->via1->comment.s);
510                 DBG ("\n");
511         }
512         if (msg->via2){
513                 DBG(" first  via: <%.*s/%.*s/%.*s> <%.*s:%.*s(%d)>",
514                         msg->via2->name.len, 
515                         msg->via2->name.s, 
516                         msg->via2->version.len,
517                         msg->via2->version.s,
518                         msg->via2->transport.len, 
519                         msg->via2->transport.s, 
520                         msg->via2->host.len,
521                         msg->via2->host.s,
522                         msg->via2->port_str.len, 
523                         msg->via2->port_str.s, 
524                         msg->via2->port);
525                 if (msg->via2->params.s)  DBG(";<%.*s>", 
526                                 msg->via2->params.len, msg->via2->params.s);
527                 if (msg->via2->comment.s) DBG(" <%.*s>", 
528                                 msg->via2->comment.len, msg->via2->comment.s);
529                 DBG ("\n");
530         }
531 #endif
532         
533
534 #ifdef EXTRA_DEBUG
535         DBG("exiting parse_msg\n");
536 #endif
537
538         return 0;
539         
540 error:
541         /* more debugging, msg->orig is/should be null terminated*/
542 #ifdef SCRATCH
543         LOG(L_ERR, "ERROR: parse_msg: message=<%.*s>\n", (int)msg->len, msg->orig);
544 #else
545         LOG(L_ERR, "ERROR: parse_msg: message=<%.*s>\n", (int)msg->len, msg->buf);
546 #endif
547         return -1;
548 }
549
550
551
552 void free_reply_lump( struct lump_rpl *lump)
553 {
554         struct lump_rpl *foo, *bar;
555         for(foo=lump;foo;)
556         {
557                 bar=foo->next;
558                 free_lump_rpl(foo);
559                 foo = bar;
560         }
561 }
562
563
564 /*only the content*/
565 void free_sip_msg(struct sip_msg* msg)
566 {
567         if (msg->new_uri.s) { pkg_free(msg->new_uri.s); msg->new_uri.len=0; }
568         if (msg->headers)     free_hdr_field_lst(msg->headers);
569         if (msg->add_rm)      free_lump_list(msg->add_rm);
570         if (msg->repl_add_rm) free_lump_list(msg->repl_add_rm);
571         if (msg->reply_lump)   free_reply_lump(msg->reply_lump);
572 #ifdef SCRATCH
573         pkg_free(msg->orig);
574 #endif
575         /* don't free anymore -- now a pointer to a static buffer */
576 #       ifdef DYN_BUF
577         pkg_free(msg->buf); 
578 #       endif
579 }
580
581
582 /* make sure all HFs needed for transaction identification have been
583    parsed; return 0 if those HFs can't be found
584 */
585
586 int check_transaction_quadruple( struct sip_msg* msg )
587 {
588         if ( parse_headers(msg, HDR_FROM|HDR_TO|HDR_CALLID|HDR_CSEQ,0)!=-1
589                 && msg->from && msg->to && msg->callid && msg->cseq ) {
590                 return 1;
591         } else {
592                 ser_error=E_BAD_TUPEL;
593                 return 0;
594         }
595 }