GPLization banner introduced to *.[hc] files
[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
31
32 #include <string.h>
33 #include <stdlib.h>
34
35 #include "msg_parser.h"
36 #include "parser_f.h"
37 #include "../ut.h"
38 #include "../error.h"
39 #include "../dprint.h"
40 #include "../data_lump_rpl.h"
41 #include "../mem/mem.h"
42 #include "../error.h"
43 #include "../globals.h"
44 #include "parse_hname2.h"
45 #include "parse_uri.h"
46
47 #ifdef DEBUG_DMALLOC
48 #include <mem/dmalloc.h>
49 #endif
50
51
52 #define parse_hname(_b,_e,_h) parse_hname2((_b),(_e),(_h))
53
54 /* number of via's encounteded */
55 int via_cnt;
56
57 /* returns pointer to next header line, and fill hdr_f ;
58  * if at end of header returns pointer to the last crlf  (always buf)*/
59 char* get_hdr_field(char* buf, char* end, struct hdr_field* hdr)
60 {
61
62         char* tmp;
63         char *match;
64         struct via_body *vb;
65         struct cseq_body* cseq_b;
66         struct to_body* to_b;
67
68         if ((*buf)=='\n' || (*buf)=='\r'){
69                 /* double crlf or lflf or crcr */
70                 DBG("found end of header\n");
71                 hdr->type=HDR_EOH;
72                 return buf;
73         }
74
75         tmp=parse_hname(buf, end, hdr);
76         if (hdr->type==HDR_ERROR){
77                 LOG(L_ERR, "ERROR: get_hdr_field: bad header\n");
78                 goto error;
79         }
80         switch(hdr->type){
81                 case HDR_VIA:
82                         /* keep number of vias parsed -- we want to report it in
83                            replies for diagnostic purposes */
84                         via_cnt++;
85                         vb=pkg_malloc(sizeof(struct via_body));
86                         if (vb==0){
87                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
88                                 goto error;
89                         }
90                         memset(vb,0,sizeof(struct via_body));
91                         hdr->body.s=tmp;
92                         tmp=parse_via(tmp, end, vb);
93                         if (vb->error==PARSE_ERROR){
94                                 LOG(L_ERR, "ERROR: get_hdr_field: bad via\n");
95                                 pkg_free(vb);
96                                 goto error;
97                         }
98                         hdr->parsed=vb;
99                         vb->hdr.s=hdr->name.s;
100                         vb->hdr.len=hdr->name.len;
101                         hdr->body.len=tmp-hdr->body.s;
102                         break;
103                 case HDR_CSEQ:
104                         cseq_b=pkg_malloc(sizeof(struct cseq_body));
105                         if (cseq_b==0){
106                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
107                                 goto error;
108                         }
109                         memset(cseq_b, 0, sizeof(struct cseq_body));
110                         hdr->body.s=tmp;
111                         tmp=parse_cseq(tmp, end, cseq_b);
112                         if (cseq_b->error==PARSE_ERROR){
113                                 LOG(L_ERR, "ERROR: get_hdr_field: bad cseq\n");
114                                 pkg_free(cseq_b);
115                                 goto error;
116                         }
117                         hdr->parsed=cseq_b;
118                         hdr->body.len=tmp-hdr->body.s;
119                         DBG("get_hdr_field: cseq <%s>: <%s> <%s>\n",
120                                         hdr->name.s, cseq_b->number.s, cseq_b->method.s);
121                         break;
122                 case HDR_TO:
123                         to_b=pkg_malloc(sizeof(struct to_body));
124                         if (to_b==0){
125                                 LOG(L_ERR, "get_hdr_field: out of memory\n");
126                                 goto error;
127                         }
128                         memset(to_b, 0, sizeof(struct to_body));
129                         hdr->body.s=tmp;
130                         tmp=parse_to(tmp, end,to_b);
131                         if (to_b->error==PARSE_ERROR){
132                                 LOG(L_ERR, "ERROR: get_hdr_field: bad to header\n");
133                                 pkg_free(to_b);
134                                 goto error;
135                         }
136                         hdr->parsed=to_b;
137                         hdr->body.len=tmp-hdr->body.s;
138                         DBG("DEBUG: get_hdr_field: <%s> [%d]; uri=[%.*s] \n",
139                                 hdr->name.s, hdr->body.len, to_b->uri.len,to_b->uri.s);
140                         DBG("DEBUG: to body [%.*s]\n",to_b->body.len,to_b->body.s);
141                         break;
142                 case HDR_FROM:
143                 case HDR_CALLID:
144                 case HDR_CONTACT:
145                 case HDR_ROUTE:
146                 case HDR_RECORDROUTE:
147                 case HDR_MAXFORWARDS:
148                 case HDR_CONTENTTYPE:
149                 case HDR_CONTENTLENGTH:
150                 case HDR_AUTHORIZATION:
151                 case HDR_EXPIRES:
152                 case HDR_PROXYAUTH:
153                 case HDR_WWWAUTH:
154                 case HDR_SUPPORTED:
155                 case HDR_REQUIRE:
156                 case HDR_PROXYREQUIRE:
157                 case HDR_UNSUPPORTED:
158                 case HDR_ALLOW:
159                 case HDR_EVENT:
160                 case HDR_OTHER:
161                         /* just skip over it */
162                         hdr->body.s=tmp;
163                         /* find end of header */
164                         /* find lf */
165                         do{
166                                 match=q_memchr(tmp, '\n', end-tmp);
167                                 if (match){
168                                         match++;
169                                 }else {
170                                         LOG(L_ERR,
171                                                         "ERROR: get_hdr_field: bad body for <%s>(%d)\n",
172                                                         hdr->name.s, hdr->type);
173                                         /* abort(); */
174                                         tmp=end;
175                                         goto error;
176                                 }
177                                 tmp=match;
178                         }while( match<end &&( (*match==' ')||(*match=='\t') ) );
179                         *(match-1)=0; /*null terminate*/
180                         hdr->body.len=match-hdr->body.s;
181                         break;
182                 default:
183                         LOG(L_CRIT, "BUG: get_hdr_field: unknown header type %d\n",
184                                         hdr->type);
185                         goto error;
186         }
187         /* jku: if \r covered by current length, shrink it */
188         trim_r( hdr->body );
189         return tmp;
190 error:
191         DBG("get_hdr_field: error exit\n");
192         hdr->type=HDR_ERROR;
193         return tmp;
194 }
195
196
197
198 /* parse the headers and adds them to msg->headers and msg->to, from etc.
199  * It stops when all the headers requested in flags were parsed, on error
200  * (bad header) or end of headers */
201 /* note: it continues where it previously stopped and goes ahead until
202    end is encountered or desired HFs are found; if you call it twice
203    for the same HF which is present only once, it will fail the second
204    time; if you call it twice and the HF is found on second time too,
205    it's not replaced in the well-known HF pointer but just added to
206    header list; if you want to use a dumbie convenience function which will
207    give you the first occurance of a header you are interested in,
208    look at check_transaction_quadruple
209 */
210 int parse_headers(struct sip_msg* msg, int flags, int next)
211 {
212         struct hdr_field* hf;
213         char* tmp;
214         char* rest;
215         char* end;
216         int orig_flag;
217
218         end=msg->buf+msg->len;
219         tmp=msg->unparsed;
220         
221         if (next) {
222                 orig_flag = msg->parsed_flag;
223                 msg->parsed_flag &= ~flags;
224         }else
225                 orig_flag=0; 
226         
227         DBG("parse_headers: flags=%d\n", flags);
228         while( tmp<end && (flags & msg->parsed_flag) != flags){
229                 hf=pkg_malloc(sizeof(struct hdr_field));
230                 if (hf==0){
231                         ser_error=E_OUT_OF_MEM;
232                         LOG(L_ERR, "ERROR:parse_headers: memory allocation error\n");
233                         goto error;
234                 }
235                 memset(hf,0, sizeof(struct hdr_field));
236                 hf->type=HDR_ERROR;
237                 rest=get_hdr_field(tmp, msg->buf+msg->len, hf);
238                 switch (hf->type){
239                         case HDR_ERROR:
240                                 LOG(L_INFO,"ERROR: bad header  field\n");
241                                 goto  error;
242                         case HDR_EOH:
243                                 msg->eoh=tmp; /* or rest?*/
244                                 msg->parsed_flag|=HDR_EOH;
245                                 pkg_free(hf);
246                                 goto skip;
247                         case HDR_OTHER: /*do nothing*/
248                                 break;
249                         case HDR_CALLID:
250                                 if (msg->callid==0) msg->callid=hf;
251                                 msg->parsed_flag|=HDR_CALLID;
252                                 break;
253                         case HDR_TO:
254                                 if (msg->to==0) msg->to=hf;
255                                 msg->parsed_flag|=HDR_TO;
256                                 break;
257                         case HDR_CSEQ:
258                                 if (msg->cseq==0) msg->cseq=hf;
259                                 msg->parsed_flag|=HDR_CSEQ;
260                                 break;
261                         case HDR_FROM:
262                                 if (msg->from==0) msg->from=hf;
263                                 msg->parsed_flag|=HDR_FROM;
264                                 break;
265                         case HDR_CONTACT:
266                                 if (msg->contact==0) msg->contact=hf;
267                                 msg->parsed_flag|=HDR_CONTACT;
268                                 break;
269                         case HDR_MAXFORWARDS:
270                                 if(msg->maxforwards==0) msg->maxforwards=hf;
271                                 msg->parsed_flag|=HDR_MAXFORWARDS;
272                                 break;
273                         case HDR_ROUTE:
274                                 if (msg->route==0) msg->route=hf;
275                                 msg->parsed_flag|=HDR_ROUTE;
276                                 break;
277                         case HDR_RECORDROUTE:
278                                 if (msg->record_route==0) msg->record_route = hf;
279                                 msg->parsed_flag|=HDR_RECORDROUTE;
280                                 break;
281                         case HDR_CONTENTTYPE:
282                                 if (msg->content_type==0) msg->content_type = hf;
283                                 msg->parsed_flag|=HDR_CONTENTTYPE;
284                                 break;
285                         case HDR_CONTENTLENGTH:
286                                 if (msg->content_length==0) msg->content_length = hf;
287                                 msg->parsed_flag|=HDR_CONTENTLENGTH;
288                                 break;
289                         case HDR_AUTHORIZATION:
290                                 if (msg->authorization==0) msg->authorization = hf;
291                                 msg->parsed_flag|=HDR_AUTHORIZATION;
292                                 break;
293                         case HDR_EXPIRES:
294                                 if (msg->expires==0) msg->expires = hf;
295                                 msg->parsed_flag|=HDR_EXPIRES;
296                                 break;
297                         case HDR_PROXYAUTH:
298                                 if (msg->proxy_auth==0) msg->proxy_auth = hf;
299                                 msg->parsed_flag|=HDR_PROXYAUTH;
300                                 break;
301                         case HDR_WWWAUTH:
302                                 if (msg->www_auth==0) msg->www_auth = hf;
303                                 msg->parsed_flag|=HDR_WWWAUTH;
304                                 break;
305                         case HDR_SUPPORTED:
306                                 if (msg->supported==0) msg->supported = hf;
307                                 msg->parsed_flag|=HDR_SUPPORTED;
308                                 break;
309                         case HDR_REQUIRE:
310                                 if (msg->require==0) msg->require = hf;
311                                 msg->parsed_flag|=HDR_REQUIRE;
312                                 break;
313                         case HDR_PROXYREQUIRE:
314                                 if (msg->proxy_require==0) msg->proxy_require = hf;
315                                 msg->parsed_flag|=HDR_PROXYREQUIRE;
316                                 break;
317                         case HDR_UNSUPPORTED:
318                                 if (msg->unsupported==0) msg->unsupported=hf;
319                                 msg->parsed_flag|=HDR_UNSUPPORTED;
320                                 break;
321                         case HDR_ALLOW:
322                                 if (msg->allow==0) msg->allow = hf;
323                                 msg->parsed_flag|=HDR_ALLOW;
324                                 break;
325                         case HDR_EVENT:
326                                 if (msg->allow==0) msg->event = hf;
327                                 msg->parsed_flag|=HDR_EVENT;
328                                 break;
329                         case HDR_VIA:
330                                 msg->parsed_flag|=HDR_VIA;
331                                 DBG("parse_headers: Via found, flags=%d\n", flags);
332                                 if (msg->via1==0) {
333                                         DBG("parse_headers: this is the first via\n");
334                                         msg->h_via1=hf;
335                                         msg->via1=hf->parsed;
336                                         if (msg->via1->next){
337                                                 msg->via2=msg->via1->next;
338                                                 msg->parsed_flag|=HDR_VIA2;
339                                         }
340                                 }else if (msg->via2==0){
341                                         msg->h_via2=hf;
342                                         msg->via2=hf->parsed;
343                                         msg->parsed_flag|=HDR_VIA2;
344                                         DBG("parse_headers: this is the second via\n");
345                                 }
346                                 break;
347                         default:
348                                 LOG(L_CRIT, "BUG: parse_headers: unknown header type %d\n",
349                                                         hf->type);
350                                 goto error;
351                 }
352                 /* add the header to the list*/
353                 if (msg->last_header==0){
354                         msg->headers=hf;
355                         msg->last_header=hf;
356                 }else{
357                         msg->last_header->next=hf;
358                         msg->last_header=hf;
359                 }
360 #ifdef EXTRA_DEBUG
361                 DBG("header field type %d, name=<%s>, body=<%s>\n",
362                         hf->type, hf->name.s, hf->body.s);
363 #endif
364                 tmp=rest;
365         }
366 skip:
367         msg->unparsed=tmp;
368         return 0;
369
370 error:
371         ser_error=E_BAD_REQ;
372         if (hf) pkg_free(hf);
373         if (next) msg->parsed_flag |= orig_flag;
374         return -1;
375 }
376
377
378
379
380
381 /* returns 0 if ok, -1 for errors */
382 int parse_msg(char* buf, unsigned int len, struct sip_msg* msg)
383 {
384
385         char *tmp;
386         char* rest;
387         char* first_via;
388         char* second_via;
389         struct msg_start *fl;
390         int offset;
391         int flags;
392
393         /* eat crlf from the beginning */
394         for (tmp=buf; (*tmp=='\n' || *tmp=='\r')&&
395                         tmp-buf < len ; tmp++);
396         offset=tmp-buf;
397         fl=&(msg->first_line);
398         rest=parse_first_line(tmp, len-offset, fl);
399 #if 0
400         rest=parse_fline(tmp, buf+len, fl);
401 #endif
402         offset+=rest-tmp;
403         tmp=rest;
404         switch(fl->type){
405                 case SIP_INVALID:
406                         DBG("parse_msg: invalid message\n");
407                         goto error;
408                         break;
409                 case SIP_REQUEST:
410                         DBG("SIP Request:\n");
411                         DBG(" method:  <%s>\n",fl->u.request.method.s);
412                         DBG(" uri:     <%s>\n",fl->u.request.uri.s);
413                         DBG(" version: <%s>\n",fl->u.request.version.s);
414                         flags=HDR_VIA;
415                         break;
416                 case SIP_REPLY:
417                         DBG("SIP Reply  (status):\n");
418                         DBG(" version: <%s>\n",fl->u.reply.version.s);
419                         DBG(" status:  <%s>\n",fl->u.reply.status.s);
420                         DBG(" reason:  <%s>\n",fl->u.reply.reason.s);
421                         /* flags=HDR_VIA | HDR_VIA2; */
422                         /* we don't try to parse VIA2 for local messages; -Jiri */
423                         flags=HDR_VIA;
424                         break;
425                 default:
426                         DBG("unknown type %d\n",fl->type);
427                         goto error;
428         }
429         msg->unparsed=tmp;
430         /*find first Via: */
431         first_via=0;
432         second_via=0;
433         if (parse_headers(msg, flags, 0)==-1) goto error;
434
435 #ifdef EXTRA_DEBUG
436         /* dump parsed data */
437         if (msg->via1){
438                 DBG(" first  via: <%s/%s/%s> <%s:%s(%d)>",
439                         msg->via1->name.s, msg->via1->version.s,
440                         msg->via1->transport.s, msg->via1->host.s,
441                         msg->via1->port_str.s, msg->via1->port);
442                 if (msg->via1->params.s)  DBG(";<%s>", msg->via1->params.s);
443                 if (msg->via1->comment.s) DBG(" <%s>", msg->via1->comment.s);
444                 DBG ("\n");
445         }
446         if (msg->via2){
447                 DBG(" first  via: <%s/%s/%s> <%s:%s(%d)>",
448                         msg->via2->name.s, msg->via2->version.s,
449                         msg->via2->transport.s, msg->via2->host.s,
450                         msg->via2->port_str.s, msg->via2->port);
451                 if (msg->via2->params.s)  DBG(";<%s>", msg->via2->params.s);
452                 if (msg->via2->comment.s) DBG(" <%s>", msg->via2->comment.s);
453                 DBG ("\n");
454         }
455 #endif
456         
457
458 #ifdef EXTRA_DEBUG
459         DBG("exiting parse_msg\n");
460 #endif
461
462         return 0;
463         
464 error:
465         /* more debugging, msg->orig is/should be null terminated*/
466         LOG(L_ERR, "ERROR: parse_msg: message=<%s>\n", msg->orig);
467         return -1;
468 }
469
470
471
472 void free_reply_lump( struct lump_rpl *lump)
473 {
474         struct lump_rpl *foo, *bar;
475         for(foo=lump;foo;)
476         {
477                 bar=foo->next;
478                 free_lump_rpl(foo);
479                 foo = bar;
480         }
481 }
482
483
484 /*only the content*/
485 void free_sip_msg(struct sip_msg* msg)
486 {
487         if (msg->new_uri.s) { pkg_free(msg->new_uri.s); msg->new_uri.len=0; }
488         if (msg->headers)     free_hdr_field_lst(msg->headers);
489         if (msg->add_rm)      free_lump_list(msg->add_rm);
490         if (msg->repl_add_rm) free_lump_list(msg->repl_add_rm);
491         if (msg->reply_lump)   free_reply_lump(msg->reply_lump);
492         if (msg->parsed_uri_ok) free_uri(&msg->parsed_uri);
493         pkg_free(msg->orig);
494         /* don't free anymore -- now a pointer to a static buffer */
495 #       ifdef DYN_BUF
496         pkg_free(msg->buf); 
497 #       endif
498 }
499
500
501 /* make sure all HFs needed for transaction identification have been
502    parsed; return 0 if those HFs can't be found
503 */
504
505 int check_transaction_quadruple( struct sip_msg* msg )
506 {
507         if ( parse_headers(msg, HDR_FROM|HDR_TO|HDR_CALLID|HDR_CSEQ,0)!=-1
508                 && msg->from && msg->to && msg->callid && msg->cseq ) {
509                 return 1;
510         } else {
511                 ser_error=E_BAD_TUPEL;
512                 return 0;
513         }
514 }