belle_sip_message.g 84.4 KB
Newer Older
jehan's avatar
jehan committed
1
/*     belle-sip - SIP (RFC3261) library.
2 3 4 5
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
jehan's avatar
jehan committed
6
    the Free Software Foundation, either version 2 of the License, or
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

grammar belle_sip_message;


options { 
  language = C;
} 


jehan's avatar
jehan committed
26 27 28 29 30 31 32
@parser::header {
/*
    belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
jehan's avatar
jehan committed
33
    the Free Software Foundation, either version 2 of the License, or
jehan's avatar
jehan committed
34 35 36 37 38 39 40 41 42 43 44 45 46
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma GCC diagnostic ignored "-Wparentheses"
#pragma GCC diagnostic ignored "-Wunused"
jehan's avatar
jehan committed
47
#ifndef __clang__
48 49 50
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-function"
jehan's avatar
jehan committed
51
#endif
jehan's avatar
jehan committed
52 53 54 55 56 57 58 59
}
@lexer::header {
/*
    belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
60
    the Free Software Foundation, either version 2 of the License, or
jehan's avatar
jehan committed
61 62 63 64 65 66 67 68 69 70 71 72 73
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma GCC diagnostic ignored "-Wparentheses"
#pragma GCC diagnostic ignored "-Wunused"
Simon Morlat's avatar
Simon Morlat committed
74

jehan's avatar
jehan committed
75
}
76 77

@includes { 
Simon Morlat's avatar
Simon Morlat committed
78 79 80 81 82

#include "belle-sip/defs.h"
#include "belle-sip/types.h"
#include "belle-sip/message.h"
#include "belle-sip/http-message.h"
83
#include "parserutils.h"
Ghislain MARY's avatar
Ghislain MARY committed
84
BELLESIP_EXPORT void belle_sip_header_address_set_quoted_displayname(belle_sip_header_address_t* address,const char* value);
85 86 87 88 89 90 91 92 93 94
}

@rulecatch 
{
    if (HASEXCEPTION())
    {

    // This is ugly.  We set the exception type to ANTLR3_RECOGNITION_EXCEPTION so we can always
    // catch them.
    //PREPORTERROR();
jehan's avatar
jehan committed
95
    EXCEPTION->type = ANTLR3_RECOGNITION_EXCEPTION; 
96 97 98 99 100 101 102
    }
}

message returns [belle_sip_message_t* ret]
scope { size_t message_length; }
@init {$ret=NULL;}
  : message_raw[&($message::message_length)] {$ret=$message_raw.ret;};
jehan's avatar
jehan committed
103
  
104 105 106
message_raw [size_t* length]  returns [belle_sip_message_t* ret]
scope { size_t* message_length; }
@init {$message_raw::message_length=length;$ret=NULL;}
jehan's avatar
jehan committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	:  common_request {$ret = $common_request.ret;} 
	   | common_response {$ret = $common_response.ret;} ;

request returns [belle_sip_request_t* ret=NULL] 
  :common_request {$ret=BELLE_SIP_REQUEST($common_request.ret);}; 
    
  
common_request	returns [belle_sip_message_t* ret]
scope { belle_sip_message_t* current; }
@init {$ret=$common_request::current=NULL; }
	:	  (  ( ~(CRLF)* sip_version CRLF) =>  request_line  {$ret=$common_request::current=BELLE_SIP_MESSAGE($request_line.ret);}
	    | ( ~(CRLF)* http_version CRLF) =>  http_request_line{$ret=$common_request::current=BELLE_SIP_MESSAGE($http_request_line.ret);}) 
		message_header[BELLE_SIP_MESSAGE($common_request::current)]+ last_crlf=CRLF {*($message_raw::message_length)=$last_crlf->user1;} /*message_body ?*/ ;

request_line  returns [belle_sip_request_t* ret=NULL]
122
scope { belle_sip_request_t* current; }
jehan's avatar
jehan committed
123 124 125
@init {$request_line::current = belle_sip_request_new(); $ret=$request_line::current; }  
	:	  method {belle_sip_request_set_method($request_line::current,(const char*)($method.text->chars));} 
	    SP 
126 127 128 129
	    (	uri {belle_sip_request_set_uri($request_line::current,$uri.ret);} 
	    	| 
	    	generic_uri {belle_sip_request_set_absolute_uri($request_line::current,$generic_uri.ret);} 
	     )
jehan's avatar
jehan committed
130
	    SP 
131 132
	    sip_version 
	    CRLF ;
jehan's avatar
jehan committed
133
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
134 135 136 137 138
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($request_line::current);
   $ret=NULL;
} 
139
sip_version   
jehan's avatar
jehan committed
140 141 142 143
	:	 {IS_TOKEN(SIP/)}? generic_version; // 'SIP/' DIGIT '.' DIGIT; 


generic_version: alpha+ SLASH DIGIT DOT DIGIT;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

message_header [belle_sip_message_t* message] 

	:	           (/*accept
//                |  accept_encoding
//                |  accept_language
//                |  alert_info
//                |  allow
//                |  authentication_info
//                |  authorization
//                |  header_call_id {belle_sip_message_add_header(message,BELLE_SIP_HEADER($header_call_id.ret));}/*
//                |  call_info
//                |  header_contact {belle_sip_message_add_header(message,BELLE_SIP_HEADER($header_contact.ret));}
//                |  content_disposition
//                |  content_encoding
//                |  content_language*/
//                |  header_content_length  {belle_sip_message_add_header(message,BELLE_SIP_HEADER($header_content_length.ret));}
//                |  header_content_type  {belle_sip_message_add_header(message,BELLE_SIP_HEADER($header_content_type.ret));}
//                |  header_cseq  {belle_sip_message_add_header(message,BELLE_SIP_HEADER($header_cseq.ret));}/*
//                |  date
//                |  error_info
//                |  expires*/
//                |  header_from  {belle_sip_message_add_header(message,BELLE_SIP_HEADER($header_from.ret));}/*
//                |  in_reply_to
//                |  max_forwards
//                |  mime_version
//                |  min_expires
//                |  organization
//                |  priority
//                |  proxy_authenticate
//                |  proxy_authorization
//                |  proxy_require*/
//                |  header_record_route  {belle_sip_message_add_header(message,BELLE_SIP_HEADER($header_record_route.ret));}/*
//                |  reply_to
//                |  require
//                |  retry_after*/
//                |  header_route  {belle_sip_message_add_header(message,BELLE_SIP_HEADER($header_route.ret));}/*
//                |  server
//                |  subject
//                |  supported
//                |  timestamp*/
//                |  header_to  {belle_sip_message_add_header(message,BELLE_SIP_HEADER($header_to.ret));}/*
jehan's avatar
jehan committed
186
//                |  header_diversion  {belle_sip_message_add_header(message,BELLE_SIP_HEADER($header_diversion.ret));}/*
187 188 189 190 191
//                |  unsupported
//                |  user_agent*/
//                |  header_via  {belle_sip_message_add_header(message,BELLE_SIP_HEADER($header_via.ret));}/*
//                |  warning
//                |  www_authenticate*/
jehan's avatar
jehan committed
192 193
                  header_extension_base[(BELLE_SIP_OBJECT_IS_INSTANCE_OF($message,belle_http_request_t) ||BELLE_SIP_OBJECT_IS_INSTANCE_OF($message,belle_http_response_t)) ] {
                    belle_sip_header_t* lheader = BELLE_SIP_HEADER($header_extension_base.ret);
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
                    do {
                      if (lheader == NULL) break; /*sanity check*/
                      
                      belle_sip_message_add_header(message,lheader);
                      }
                    while((lheader=belle_sip_header_get_next(lheader)) != NULL); } 
                ) CRLF 
               ;

       
/*
invitem           
	:	'INVITE' ; //INVITE in caps
ackm 	:	'ACK'; //ACK in caps
optionsm:	'OPTION'; //OPTIONS in caps
byem	:	'BYE' ; //BYE in caps
cancelm :	'CANCEL' ; //CANCEL in caps
registerm
	:	'REGISTER' ; //REGISTER in caps
optionm :	'OPTION';
*/
method 	:	          /* invitem | ackm | optionm | byem | cancelm | registerm |*/extension_method ;

extension_method  
	:	  token;

jehan's avatar
jehan committed
220 221 222 223 224 225 226
common_response returns [belle_sip_message_t* ret=NULL] 
  :   (   (sip_version  ~(CRLF)* CRLF) =>  status_line  {$ret=BELLE_SIP_MESSAGE($status_line.ret);}
      |   (http_version ~(CRLF)*  CRLF) =>  http_status_line{$ret=BELLE_SIP_MESSAGE($http_status_line.ret);}) 
  (message_header[BELLE_SIP_MESSAGE($ret)]+ last_crlf=CRLF {*($message_raw::message_length)=$last_crlf->user1;} /*message_body*/)?  ;

response  returns [belle_sip_response_t* ret=NULL]
	:	  common_response {$ret=BELLE_SIP_RESPONSE($common_response.ret);} ;
227

jehan's avatar
jehan committed
228 229 230
status_line   returns [belle_sip_response_t* ret=NULL]  
scope { belle_sip_response_t* current; }
@init {$ret = belle_sip_response_new(); }
231
	:	  sip_version 
jehan's avatar
jehan committed
232 233
	   SP status_code {belle_sip_response_set_status_code($ret,atoi((char*)$status_code.text->chars));}
	   SP reason_phrase {belle_sip_response_set_reason_phrase($ret,(char*)$reason_phrase.text->chars);}
234
	   CRLF ;
jehan's avatar
jehan committed
235
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
236 237 238 239 240 241
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref( $ret);
   $ret=NULL;
} 

242 243 244 245 246 247 248 249
	
status_code     
	: extension_code;

extension_code  
	:	  DIGIT DIGIT DIGIT;
reason_phrase   
	:	  ~(CRLF)*;
jehan's avatar
jehan committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

//****************http**********************/ 

/*Request-Line   = Method SP Request-URI SP HTTP-Version CRLF*/ 
http_request returns [belle_http_request_t* ret=NULL] 
  :common_request {$ret=BELLE_HTTP_REQUEST($common_request.ret);}; 
  
http_request_line returns [belle_http_request_t *ret=NULL]
scope { belle_http_request_t* current; }
@init {$http_request_line::current = belle_http_request_new(); $ret=$http_request_line::current; }
  : method {belle_http_request_set_method($http_request_line::current,(const char*)($method.text->chars));} 
    SP 
    (generic_uri)=>generic_uri {belle_http_request_set_uri($http_request_line::current,$generic_uri.ret);}
    SP 
    http_version CRLF;
jehan's avatar
jehan committed
265
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
266 267 268 269 270 271 272 273
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($http_request_line::current);
   $ret=NULL;
} 

http_version:  {IS_TOKEN(HTTP/)}? generic_version;

274
http_response  returns [belle_http_response_t* ret=NULL]
jehan's avatar
jehan committed
275 276 277 278 279 280 281 282
  :   common_response {$ret=BELLE_HTTP_RESPONSE($common_response.ret);} ;

http_status_line  returns [belle_http_response_t* ret]   
@init {$ret = belle_http_response_new();  }  
  :   http_version 
     SP status_code {belle_http_response_set_status_code($ret,atoi((char*)$status_code.text->chars));}
     SP reason_phrase {belle_http_response_set_reason_phrase($ret,(char*)$reason_phrase.text->chars);}
     CRLF ;
jehan's avatar
jehan committed
283
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref( $ret);
   $ret=NULL;
}      


/*
  absoluteURI   = scheme ":" ( hier_part | opaque_part )
  hier_part     = ( net_path | abs_path ) [ "?" query ]
  net_path      = "//" authority [ abs_path ]
  abs_path      = "/"  path_segments
*/

298 299
/*comma, semicolon, or question
   mark*/
jehan's avatar
jehan committed
300

301 302 303 304 305 306 307 308 309 310
opaque_part_for_from_to_contact_addr_spec
	:  uric_no_slash_for_from_to_contact_addr_spec uric_for_from_to_contact_addr_spec*;
opaque_part
	:  uric_no_slash uric*;

uric_no_slash_for_from_to_contact_addr_spec	
	: unreserved | escaped  | COLON | AT| AND | EQUAL | PLUS | DOLLARD;	
uric_no_slash
	:COMMA | SEMI | QMARK  | uric_no_slash_for_from_to_contact_addr_spec ;
                  
jehan's avatar
jehan committed
311 312
 scheme: alpha ( alphanum | PLUS | DASH | DOT )*;

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
/*remove hiearachy part because complex to handle the :comma, semicolon, or question
   markexception see rfc3261 section 20.10 Contact*/
generic_uri_for_from_to_contact_addr_spec  returns [belle_generic_uri_t* ret=NULL]    
scope { belle_generic_uri_t* current; }
@init { $generic_uri_for_from_to_contact_addr_spec::current = $ret = belle_generic_uri_new(); }
   : scheme {belle_generic_uri_set_scheme($generic_uri_for_from_to_contact_addr_spec::current,(const char*)$scheme.text->chars);}  
   COLON  opaque_part_for_from_to_contact_addr_spec  {belle_generic_uri_set_opaque_part($generic_uri_for_from_to_contact_addr_spec::current,(const char*)$opaque_part_for_from_to_contact_addr_spec.text->chars) ;} ;
catch [ANTLR3_RECOGNITION_EXCEPTION]
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($generic_uri::current);
   $ret=NULL;
}



jehan's avatar
jehan committed
329 330 331
generic_uri  returns [belle_generic_uri_t* ret=NULL]    
scope { belle_generic_uri_t* current; }
@init { $generic_uri::current = $ret = belle_generic_uri_new(); }
332 333 334 335
   :  hier_part[$generic_uri::current]
      | (scheme {belle_generic_uri_set_scheme($generic_uri::current,(const char*)$scheme.text->chars);}  
      	COLON  (opaque_part {belle_generic_uri_set_opaque_part($generic_uri::current,(const char*)$opaque_part.text->chars) ;}
      		| hier_part[$generic_uri::current]) ) ;
jehan's avatar
jehan committed
336
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
337 338 339 340 341 342 343 344 345
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($generic_uri::current);
   $ret=NULL;
}   
   

hier_part[belle_generic_uri_t* uri] returns [belle_generic_uri_t* ret=NULL]  
:   (
346 347
 (SLASH SLASH path_segments[NULL])=>( SLASH SLASH path_segments[uri])
  | 
jehan's avatar
jehan committed
348
  (SLASH SLASH authority[NULL] (path_segments[NULL])?)=>( SLASH SLASH authority[uri] (path_segments[uri])?) 
jehan's avatar
jehan committed
349
  | 
jehan's avatar
jehan committed
350
  ( path_segments[uri]) ) 
jehan's avatar
jehan committed
351 352 353 354 355 356 357 358 359
  (QMARK query 
            {
            char* unescaped_query;
            unescaped_query=belle_sip_to_unescaped_string((const char *)$query.text->chars);
            belle_generic_uri_set_query(uri,(const char*)unescaped_query);
            belle_sip_free(unescaped_query);
            }) ?;

path_segments[belle_generic_uri_t* uri]
jehan's avatar
jehan committed
360
  : SLASH (segment ( SLASH segment )*)
jehan's avatar
jehan committed
361 362 363 364 365 366
  {
  char* unescaped_path;
  unescaped_path=belle_sip_to_unescaped_string((const char *)$path_segments.text->chars);
  belle_generic_uri_set_path(uri,(const char*)unescaped_path);
  belle_sip_free(unescaped_path);
  };
jehan's avatar
jehan committed
367
segment: pchar* ( SEMI param )*;
jehan's avatar
jehan committed
368 369 370 371 372
param: pchar*;
pchar:  unreserved | escaped | COLON | AT | AND | EQUAL | PLUS | DOLLARD | COMMA;

query: uric+;
uric: reserved | unreserved | escaped;
373 374

uric_for_from_to_contact_addr_spec: reserved_for_from_to_contact_addr_spec | unreserved | escaped;
jehan's avatar
jehan committed
375 376 377 378 379
                        
authority[belle_generic_uri_t* uri] 
:     
  ((authority_userinfo[NULL]) =>authority_userinfo[uri] )? authority_hostport[uri] 
  /*|  authority_hostport[uri]*/ ; 
jehan's avatar
jehan committed
380
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
381 382 383 384 385 386 387 388 389 390
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
}  

authority_userinfo[belle_generic_uri_t* uri] 
scope { belle_generic_uri_t* current; }
@init {$authority_userinfo::current=uri;}
       :  authority_user ( COLON authority_password )?  AT ;
authority_user            :   ( unreserved  | escaped | user_unreserved )+ {
                                                                  char* unescaped_username;
391
                                                                  unescaped_username=belle_sip_to_unescaped_string((const char *)$text->chars);
jehan's avatar
jehan committed
392 393 394 395 396 397
                                                                  belle_generic_uri_set_user($authority_userinfo::current,unescaped_username);
                                                                  belle_sip_free(unescaped_username);
                                                                  };

authority_password        :   ( unreserved | escaped | AND | EQUAL | PLUS | DOLLARD | COMMA )* {
                                                                              char* unescaped_userpasswd;
398
                                                                              const char* source = (const char*)$text->chars;
399
                                                                              if( source != NULL ){
400
                                                                                unescaped_userpasswd=belle_sip_to_unescaped_string((const char *)source);
401 402
                                                                                belle_generic_uri_set_user_password($authority_userinfo::current,unescaped_userpasswd);
                                                                                belle_sip_free(unescaped_userpasswd);
403
                                                                              }
jehan's avatar
jehan committed
404 405 406 407
                                                                              };
authority_hostport[belle_generic_uri_t* uri] 
scope { belle_generic_uri_t* current; }
@init {$authority_hostport::current=uri;}
jehan's avatar
jehan committed
408 409 410 411
        :   host 
		(( COLON port {belle_generic_uri_set_port($authority_hostport::current,$port.ret);})?
		(semi  generic_param [BELLE_SIP_PARAMETERS($authority_hostport::current)])*)
		 {belle_generic_uri_set_host($authority_hostport::current,$host.ret);};
jehan's avatar
jehan committed
412 413 414 415 416

//****************http end**********************/ 

//****************SIP**********************/	

417 418 419 420
generic_param [belle_sip_parameters_t* object]  returns [belle_sip_param_pair_t* ret=NULL]
scope{int is_value; char* gen_value_string;} 
@init { $generic_param::is_value=0; $generic_param::gen_value_string=NULL;}
  :    token (  equal gen_value {$generic_param::gen_value_string=(char*)($gen_value.text->chars);} )? {
jehan's avatar
jehan committed
421 422
                                                     if (object == NULL) {
                                                       $ret=belle_sip_param_pair_new((const char*)($token.text->chars)
423
                                                                                   ,$generic_param::gen_value_string);
jehan's avatar
jehan committed
424 425 426
                                                     } else {
                                                       belle_sip_parameters_set_parameter(object
                                                                                         ,(const char*)($token.text->chars)
427
                                                                                         ,$generic_param::gen_value_string);
jehan's avatar
jehan committed
428 429 430
                                                       $ret=NULL;
                                                     }
                                                     };
jehan's avatar
jehan committed
431 432
gen_value      
  :   token |  quoted_string;
jehan's avatar
jehan committed
433 434 435

quoted_string 
options { greedy = false; }
436
  : DQUOTE (~(DQUOTE | BSLASH) | (BSLASH .))*  DQUOTE ;
jehan's avatar
jehan committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

/*
accept_encoding  
  :   'Accept-Encoding' HCOLON ( encoding (COMMA encoding)* );
encoding         
  :   codings (SEMI accept_param)*;
codings          
  :   content_coding | '*';
content_coding   :  token;

accept_language  
  :   'Accept-Language' HCOLON
                     ( language (COMMA language)* )?;
language         
  :   language_range (SEMI accept_param)*;
language_range   
  :   ( ( alpha alpha alpha alpha alpha alpha alpha alpha ( '-' alpha alpha alpha alpha alpha alpha alpha alpha )* ) | '*' );

alert_info   
  :   'Alert-Info' HCOLON alert_param (COMMA alert_param)*;
  
alert_param  
  :   LAQUOT absoluteURI RAQUOT ( SEMI generic_param )*;

absoluteURI    
  :   token ':' token;
*/
header_allow returns [belle_sip_header_allow_t* ret]     
scope { belle_sip_header_allow_t* current; }
@init {$header_allow::current = belle_sip_header_allow_new(); $ret=$header_allow::current; }
      
:    {IS_TOKEN(Allow)}? token /*'Allow'*/ hcolon methods {belle_sip_header_allow_set_method($header_allow::current,(const char*)($methods.text->chars));} ;
jehan's avatar
jehan committed
469
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_allow::current);
   $ret=NULL;
}
methods : /*LWS?*/ method (comma method)*;
authorization_token: {IS_TOKEN(Authorization)}? token;
digest_token: {IS_TOKEN(Digest)}? token;


header_authorization  returns [belle_sip_header_authorization_t* ret]     
scope { belle_sip_header_authorization_t* current; }
@init {$header_authorization::current = belle_sip_header_authorization_new(); $ret=$header_authorization::current; }
  :   authorization_token /*'Authorization'*/ hcolon credentials[$header_authorization::current];
jehan's avatar
jehan committed
484
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_authorization::current);
   $ret=NULL;
}

credentials  [belle_sip_header_authorization_t* header_authorization_base]     
  :   (digest_token /*'Digest'*/ {belle_sip_header_authorization_set_scheme(header_authorization_base,"Digest");} 
     lws digest_response[header_authorization_base])
                     | other_response[header_authorization_base];
digest_response  [belle_sip_header_authorization_t* header_authorization_base]  
  :   dig_resp[header_authorization_base] (comma dig_resp[header_authorization_base])*;
dig_resp  [belle_sip_header_authorization_t* header_authorization_base]         
  :   username { belle_sip_header_authorization_set_username(header_authorization_base,$username.ret);
                 belle_sip_free($username.ret);
               }
  | realm { belle_sip_header_authorization_set_realm(header_authorization_base,(char*)$realm.ret);
                 belle_sip_free($realm.ret);
           } 
  | nonce { belle_sip_header_authorization_set_nonce(header_authorization_base,(char*)$nonce.ret);
                 belle_sip_free($nonce.ret);
           }
  | digest_uri[header_authorization_base]
  | dresponse  { belle_sip_header_authorization_set_response(header_authorization_base,(char*)$dresponse.ret);
                 belle_sip_free($dresponse.ret);
               }
  | algorithm  {
                belle_sip_header_authorization_set_algorithm(header_authorization_base,(char*)$algorithm.ret);
               } 
  | cnonce{
            belle_sip_header_authorization_set_cnonce(header_authorization_base,(char*)$cnonce.ret);
            belle_sip_free($cnonce.ret);
           }
  | opaque {
            belle_sip_header_authorization_set_opaque(header_authorization_base,(char*)$opaque.ret);
            belle_sip_free($opaque.ret);
           }
  | message_qop{
            belle_sip_header_authorization_set_qop(header_authorization_base,$message_qop.ret);
           }
  | nonce_count{
            belle_sip_header_authorization_set_nonce_count(header_authorization_base,atoi((char*)$nonce_count.ret));
           } 
  | auth_param[header_authorization_base]
   ;
username_token: {IS_TOKEN(username)}? token;
username returns [char* ret=NULL]          
  :   username_token /*'username'*/ equal username_value {
                      $ret = _belle_sip_str_dup_and_unquote_string((char*)$username_value.text->chars);
                       };

username_value    :  quoted_string;

uri_token: {IS_TOKEN(uri)}? token;
digest_uri [belle_sip_header_authorization_t* header_authorization_base]        
  :   uri_token /*'uri'*/ equal DQUOTE uri DQUOTE  
  {belle_sip_header_authorization_set_uri(header_authorization_base,$uri.ret);
   };
/*
digest_uri_value  :  rquest_uri ;
rquest_uri
  : uri;
*/  
// Equal to request-uri as specified by HTTP/1.1
message_qop  returns [const char* ret=NULL]     
  :   {IS_TOKEN(qop)}? token/*'qop'*/ equal  qop_value {$ret = (const char*)$qop_value.text->chars;};

qop_value
  :  token;

cnonce returns [char* ret=NULL]            
  :   {IS_TOKEN(cnonce)}? token /*'cnonce'*/ equal cnonce_value {
                                              $ret = _belle_sip_str_dup_and_unquote_string((char*)$cnonce_value.text->chars);
                                              };
cnonce_value      
  :   nonce_value;
nonce_count returns [const char* ret=NULL]       
  :   {IS_TOKEN(nc)}? token /*'nc'*/ equal nc_value {$ret=(char*)$nc_value.text->chars;};
nc_value          
  :   huit_lhex; 
dresponse  returns [char* ret=NULL]       
  :   {IS_TOKEN(response)}? token /*'response'*/ equal request_digest{
                      $ret = _belle_sip_str_dup_and_unquote_string((char*)$request_digest.text->chars);
                     };
request_digest    
  :   quoted_string ;/*sp_laquot_sp huit_lhex huit_lhex huit_lhex huit_lhex sp_raquot_sp;
*/
huit_lhex
  : hexdigit+;

auth_param [belle_sip_header_authorization_t* header_authorization_base]        
  :   auth_param_name equal
                     auth_param_value {belle_sip_parameters_set_parameter(BELLE_SIP_PARAMETERS(header_authorization_base)
                                                                         ,(char*)$auth_param_name.text->chars
                                                                         ,(char*)$auth_param_value.text->chars);
                                       }
          ;
auth_param_value : token | quoted_string ;          
auth_param_name   
  :   token;
other_response [belle_sip_header_authorization_t* header_authorization_base]    
  :   auth_scheme {belle_sip_header_authorization_set_scheme(header_authorization_base,(const char*)$auth_scheme.text->chars);} 
     lws auth_param[header_authorization_base]
                     (comma auth_param[header_authorization_base])*;

auth_scheme       
  :   token;
/*
authentication_info  :  'Authentication-Info' HCOLON ainfo
                        (COMMA ainfo)*;
ainfo                
  :   nextnonce | message_qop
                         | response_auth | cnonce
                         | nonce_count;
nextnonce            
  :   'nextnonce' EQUAL nonce_value;
  */
nonce_value         :  quoted_string;
/*
response_auth        
  :   'rspauth' EQUAL response_digest;
response_digest      
  :   LDQUOT hexdigit* RDQUOT;
*/
/*callid header*/
call_id_token: {IS_HEADER_NAMED(Call-ID,i)}? token;

header_call_id  returns [belle_sip_header_call_id_t* ret=NULL]     
scope { belle_sip_header_call_id_t* current; }
@init {$header_call_id::current = belle_sip_header_call_id_new(); $ret=$header_call_id::current; }
  :  call_id_token /*( 'Call-ID' | 'i' )*/ hcolon call_id{belle_sip_header_call_id_set_call_id($header_call_id::current,(const char*) $call_id.text->chars); };
jehan's avatar
jehan committed
616
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_call_id::current);
   $ret=NULL;
}  
call_id   
  :   word ( AT word )? ;
/*
call_info   
  :   'Call-Info' HCOLON info (COMMA info)*;
info        
  :   LAQUOT absoluteURI RAQUOT ( SEMI info_param)*;
info_param  
  :   ( 'purpose' EQUAL ( 'icon' | 'info'
               | 'card' | token ) ) | generic_param;
*/
/* contact header */
contact_token: {IS_HEADER_NAMED(Contact,m)}? token;

header_contact      returns [belle_sip_header_contact_t* ret]   
scope { belle_sip_header_contact_t* current; belle_sip_header_contact_t* first; }
638
@init { $header_contact::current =NULL; $header_contact::first =NULL; $ret=NULL; }
jehan's avatar
jehan committed
639 640 641 642
  :   (contact_token /*'Contact'*/ /*| 'm'*/ ) sp_tab_colon
                  (  (lws? STAR) { $header_contact::current = belle_sip_header_contact_new();
                            belle_sip_header_contact_set_wildcard($header_contact::current,1);}
                  | (contact_param ( COMMA contact_param)*)) {$ret = $header_contact::first; };
jehan's avatar
jehan committed
643
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
644 645
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
646
   $ret = $header_contact::first;
jehan's avatar
jehan committed
647
   if ($ret) belle_sip_object_unref($ret);
jehan's avatar
jehan committed
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
   $ret=NULL;
} 
               
contact_param 
scope { belle_sip_header_contact_t* prev;}
@init { if ($header_contact::current == NULL) {
            $header_contact::current = belle_sip_header_contact_new();
             $header_contact::first = $header_contact::current;
             $contact_param::prev=NULL; 
         } else {
            $contact_param::prev=$header_contact::current;
            belle_sip_header_set_next(BELLE_SIP_HEADER($header_contact::current),(belle_sip_header_t*)belle_sip_header_contact_new());
            $header_contact::current = (belle_sip_header_contact_t*)belle_sip_header_get_next(BELLE_SIP_HEADER($header_contact::current));
         } 
      }
  :   ( name_addr[BELLE_SIP_HEADER_ADDRESS($header_contact::current)] 
     | paramless_addr_spec[BELLE_SIP_HEADER_ADDRESS($header_contact::current)]) (SEMI lws? contact_params lws?)*;


header_address returns [belle_sip_header_address_t* ret]   
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
@init { $ret=belle_sip_header_address_new(); }
  : ( (name_addr_with_generic_uri[BELLE_SIP_HEADER_ADDRESS($ret)]  (SEMI lws? generic_param [BELLE_SIP_PARAMETERS($ret)] lws?)*)
	| addr_spec_with_generic_uri[BELLE_SIP_HEADER_ADDRESS($ret)]) {
						if (!belle_sip_header_address_get_uri($ret) && !belle_sip_header_address_get_absolute_uri(($ret))) {
							belle_sip_object_unref($ret);
							$ret=NULL;
						} 
					};
catch [ANTLR3_RECOGNITION_EXCEPTION]
{
   belle_sip_debug("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   if ($ret) belle_sip_object_unref($ret);
   $ret=NULL;
} 



header_address_param_less returns [belle_sip_header_address_t* ret]   
jehan's avatar
jehan committed
686 687 688 689 690
@init { $ret=NULL; }
  : header_address_base[belle_sip_header_address_new()] {$ret=$header_address_base.ret;}; 

header_address_base[belle_sip_header_address_t* obj]      returns [belle_sip_header_address_t* ret]   
@init { $ret=obj; }
691
     :  ( name_addr_with_generic_uri[BELLE_SIP_HEADER_ADDRESS($ret)]
jehan's avatar
jehan committed
692 693 694 695 696
| addr_spec_with_generic_uri[BELLE_SIP_HEADER_ADDRESS($ret)]) 	{	if (!belle_sip_header_address_get_uri($ret) && !belle_sip_header_address_get_absolute_uri(($ret))) {
						belle_sip_object_unref($ret);
						$ret=NULL;
						} 
					};
jehan's avatar
jehan committed
697
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
698
{
699
   belle_sip_debug("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
jehan's avatar
jehan committed
700 701 702 703 704
   if ($ret) belle_sip_object_unref($ret);
   $ret=NULL;
} 

fast_header_address  returns [belle_sip_header_address_t* ret]   
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
@init { $ret=belle_sip_header_address_new(); }
     :  (  (fast_name_addr_with_generic_uri[BELLE_SIP_HEADER_ADDRESS($ret)]  (SEMI lws? generic_param [BELLE_SIP_PARAMETERS($ret)] lws?)*)
	| fast_addr_spec_with_generic_uri[BELLE_SIP_HEADER_ADDRESS($ret)]) 	{	if (!belle_sip_header_address_get_uri($ret) && !belle_sip_header_address_get_absolute_uri(($ret))) {
							belle_sip_object_unref($ret);
							$ret=NULL;
							} 
						};
catch [ANTLR3_RECOGNITION_EXCEPTION]
{
   belle_sip_debug("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   if ($ret) belle_sip_object_unref($ret);
   $ret=NULL;
} 

fast_header_address_param_less  returns [belle_sip_header_address_t* ret]   
jehan's avatar
jehan committed
720 721 722 723 724 725 726 727 728
@init { $ret=belle_sip_header_address_new(); }
     :  ( 	fast_name_addr_with_generic_uri[BELLE_SIP_HEADER_ADDRESS($ret)]
	| fast_addr_spec_with_generic_uri[BELLE_SIP_HEADER_ADDRESS($ret)]) 	{	if (!belle_sip_header_address_get_uri($ret) && !belle_sip_header_address_get_absolute_uri(($ret))) {
							belle_sip_object_unref($ret);
							$ret=NULL;
							} 
						};
catch [ANTLR3_RECOGNITION_EXCEPTION]
{
729
   belle_sip_debug("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
jehan's avatar
jehan committed
730 731 732 733
   if ($ret) belle_sip_object_unref($ret);
   $ret=NULL;
} 

jehan's avatar
jehan committed
734 735 736
name_addr[belle_sip_header_address_t* object]      
  :      (lws? display_name[object])? sp_laquot  addr_spec[object] raquot_sp;

737 738 739
name_addr_with_generic_uri[belle_sip_header_address_t* object]      
  :      (lws? display_name[object])? sp_laquot  addr_spec_with_generic_uri[object] raquot_sp;

jehan's avatar
jehan committed
740 741 742 743
fast_name_addr_with_generic_uri[belle_sip_header_address_t* object]      
  :      (lws? display_name[object])? sp_laquot  fast_addr_spec_with_generic_uri[object] raquot_sp;


744

jehan's avatar
jehan committed
745 746 747
addr_spec[belle_sip_header_address_t* object]      
  :  lws? uri {belle_sip_header_address_set_uri(object,$uri.ret);} lws?;//| absoluteURI;

748 749 750
addr_spec_with_generic_uri[belle_sip_header_address_t* object]      
  :  lws? (	uri {belle_sip_header_address_set_uri(object,$uri.ret);}  
  	| 
751 752 753 754 755 756 757
  	generic_uri { if (   strcasecmp(belle_generic_uri_get_scheme($generic_uri.ret),"sip") != 0
					 &&  strcasecmp(belle_generic_uri_get_scheme($generic_uri.ret),"sips") != 0 ) {
						 belle_sip_header_address_set_absolute_uri(object,$generic_uri.ret);
					 } else {
						 belle_sip_message("Cannot parse a sip/sips uri as a generic uri");
						 belle_sip_object_unref($generic_uri.ret);
					 }}
758 759
  	) lws?;
  
jehan's avatar
jehan committed
760 761 762 763 764 765 766 767 768 769 770 771
  fast_addr_spec_with_generic_uri[belle_sip_header_address_t* object]      
  :  lws? (	fast_uri {belle_sip_header_address_set_uri(object,$fast_uri.ret);}  
  	| 
  	generic_uri { if (   strcasecmp(belle_generic_uri_get_scheme($generic_uri.ret),"sip") != 0
					 &&  strcasecmp(belle_generic_uri_get_scheme($generic_uri.ret),"sips") != 0 ) {
						 belle_sip_header_address_set_absolute_uri(object,$generic_uri.ret);
					 } else {
						 belle_sip_message("Cannot parse a sip/sips uri as a generic uri");
						 belle_sip_object_unref($generic_uri.ret);
					 }}
  	) lws?;
  	
jehan's avatar
jehan committed
772 773 774
paramless_addr_spec[belle_sip_header_address_t* object]      
  :  lws? paramless_uri {belle_sip_header_address_set_uri(object,$paramless_uri.ret);} lws? ;//| absoluteURI;
  
775 776 777 778 779 780
paramless_addr_spec_with_generic_uri[belle_sip_header_address_t* object]      
  :  lws? (	paramless_uri {belle_sip_header_address_set_uri(object,$paramless_uri.ret);} 
  	| 
  	generic_uri_for_from_to_contact_addr_spec{belle_sip_header_address_set_absolute_uri(object,$generic_uri_for_from_to_contact_addr_spec.ret);}
  	) lws? ;
    
781 782
display_name_tokens 
	:token (lws token)* ;
jehan's avatar
jehan committed
783
display_name[belle_sip_header_address_t* object]  
784
  :  display_name_tokens {belle_sip_header_address_set_displayname(object,(const char*)($display_name_tokens.text->chars));}
785 786 787 788 789 790
     | quoted_string 
     	{
     	char* unescaped_char = belle_sip_string_to_backslash_less_unescaped_string((const char*)($quoted_string.text->chars));
     	belle_sip_header_address_set_quoted_displayname(object,(const char*)unescaped_char);
     	belle_sip_free(unescaped_char);
     	}
jehan's avatar
jehan committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
     ;

contact_params     
  :   /*c_p_q | c_p_expires
                      |*/ contact_extension;
/*c_p_q              
  :   'q' EQUAL qvalue;
c_p_expires        
  :   'expires' EQUAL delta_seconds;*/
contact_extension  
  :   generic_param [BELLE_SIP_PARAMETERS($header_contact::current)];

delta_seconds      
  :   DIGIT+;
/*

content_disposition   
  :   'Content-Disposition' HCOLON
                         disp_type ( SEMI disp_param )*;
disp_type             :  'render' | 'session' | 'icon' | 'alert'
                         | disp_extension_token;

disp_param            
  :   handling_param | generic_param;
handling_param        
  :   'handling' EQUAL
                         ( 'optional' | 'required'
                         | other_handling );
other_handling        
  :   token;
disp_extension_token  
  :   token;

content_encoding  
  :   ( 'Content-Encoding' | 'e' ) HCOLON
                     content_coding (COMMA content_coding)*;

content_language  
  :   'Content-Language' HCOLON
                     language_tag (COMMA language_tag)*;
language_tag      
  :   primary_tag ( '-' subtag )*;
primary_tag       
  :   huit_alpha;
subtag            
  :   huit_alpha;

huit_alpha
  : alpha+;
  ;
*/
/*content_length_token :  {strcmp("Content-Length",(const char*)(INPUT->toStringTT(INPUT,LT(1),LT(14)))->chars) == 0}? token;*/
content_length_token :  {IS_HEADER_NAMED(Content-Length,l)}? token;
header_content_length  returns [belle_sip_header_content_length_t* ret]     
scope { belle_sip_header_content_length_t* current; }
@init {$header_content_length::current = belle_sip_header_content_length_new(); $ret=$header_content_length::current; }
  :  content_length_token /*( 'Content-Length' | 'l' )*/ 
     hcolon 
     content_length {belle_sip_header_content_length_set_content_length($header_content_length::current,atoi((const char*)$content_length.text->chars));};
jehan's avatar
jehan committed
850
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
851 852 853 854 855 856 857 858 859 860 861 862
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_content_length::current);
   $ret=NULL;
}
content_length:DIGIT+;
 
content_type_token :  {IS_HEADER_NAMED(Content-Type,c)}? token;
header_content_type  returns [belle_sip_header_content_type_t* ret=NULL]   
scope { belle_sip_header_content_type_t* current;}
@init { $header_content_type::current = belle_sip_header_content_type_new();$ret=$header_content_type::current; }
  :  content_type_token/* ( 'Content-Type' | 'c' )*/ hcolon media_type;
jehan's avatar
jehan committed
863
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
864 865 866 867 868 869 870 871 872
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
  belle_sip_object_unref($header_content_type::current);
   $ret=NULL;
}   
media_type       
  :  m_type {belle_sip_header_content_type_set_type($header_content_type::current,(const char*)$m_type.text->chars);} 
     slash 
     m_subtype {belle_sip_header_content_type_set_subtype($header_content_type::current,(const char*)$m_subtype.text->chars);} 
jehan's avatar
jehan committed
873
     (semi type_param)? (semi generic_param [BELLE_SIP_PARAMETERS($header_content_type::current)])*;
jehan's avatar
jehan committed
874
m_type           
jehan's avatar
jehan committed
875 876 877 878 879 880 881 882
  : token;

type_param: {IS_TOKEN(type)}? token equal type_param_value;
type_param_value : m_type slash m_subtype {belle_sip_parameters_set_parameter(BELLE_SIP_PARAMETERS($header_content_type::current)
																								,"type"
																								,(const char*)$type_param_value.text->chars);};

/*  discrete_type | composite_type;
jehan's avatar
jehan committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
discrete_type    
  :   'text' | 'image' | 'audio' | 'video'
                    | 'application' | extension_token;
composite_type   
  :   'message' | 'multipart' | extension_token;
extension_token  
  :   ietf_token | x_token;
ietf_token       
  :   token;
x_token          
  :   'x-' token;*/
m_subtype        : token ;/* extension_token | iana_token;
iana_token       
  :   token;
m_parameter      
  :   m_attribute EQUAL m_value;
m_attribute      
  :   token;
m_value          
  :   token | quoted_string;
*/

cseq_token :  {IS_TOKEN(CSeq)}? token;
header_cseq  returns [belle_sip_header_cseq_t* ret]   
scope { belle_sip_header_cseq_t* current; }
@init { $header_cseq::current = belle_sip_header_cseq_new();$ret = $header_cseq::current; }
  : cseq_token
    hcolon 
    seq_number {belle_sip_header_cseq_set_seq_number($header_cseq::current,atoi((const char*)$seq_number.text->chars));} 
    lws 
    method {belle_sip_header_cseq_set_method($header_cseq::current,(const char*)$method.text->chars);} ;
jehan's avatar
jehan committed
914
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
915 916 917 918 919 920 921 922 923 924 925 926 927 928
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_cseq::current);
   $ret=NULL;
}     
seq_number:DIGIT+;

/*Date header*/
date_token: {IS_TOKEN(Date)}? token;

header_date  returns [belle_sip_header_date_t* ret]     
scope { belle_sip_header_date_t* current; }
@init {$header_date::current = belle_sip_header_date_new(); $ret=$header_date::current; }
  :  date_token /*( 'Date' )*/ hcolon sip_date{belle_sip_header_date_set_date($header_date::current,(const char*) $sip_date.text->chars); };
jehan's avatar
jehan committed
929
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_date::current);
   $ret=NULL;
}

date
  : sip_date;

sip_date
  :  /*wkday*/alpha alpha alpha COMMA SP /*date1*/DIGIT DIGIT SP alpha alpha alpha SP DIGIT DIGIT DIGIT DIGIT SP /*time*/DIGIT DIGIT  COLON DIGIT DIGIT  COLON DIGIT DIGIT SP /*GMTT*/alpha alpha alpha;




/*
//  rfc1123-date
//rfc1123-date  =  wkday "," SP date1 SP time SP "GMT"
//date1         =  2DIGIT SP month SP 4DIGIT
//                 ; day month year (e.g., 02 Jun 1982)
//time          =  2DIGIT ":" 2DIGIT ":" 2DIGIT
//                 ; 00:00:00 - 23:59:59
//wkday         =  "Mon" / "Tue" / "Wed"
//                 / "Thu" / "Fri" / "Sat" / "Sun"
//month         =  "Jan" / "Feb" / "Mar" / "Apr"
//                 / "May" / "Jun" / "Jul" / "Aug"
//                 / "Sep" / "Oct" / "Nov" / "Dec"

error_info  
  :   'Error-Info' HCOLON error_uri (COMMA error_uri)*;

error_uri   
  :   LAQUOT absoluteURI RAQUOT ( SEMI generic_param )*;
*/
header_expires returns [belle_sip_header_expires_t* ret]   
scope { belle_sip_header_expires_t* current; }
@init { $header_expires::current = belle_sip_header_expires_new();$ret = $header_expires::current; }     
  :   {IS_TOKEN(Expires)}? token /*'Expires'*/ hcolon delta_seconds {belle_sip_header_expires_set_expires($header_expires::current,atoi((const char *)$delta_seconds.text->chars));};
jehan's avatar
jehan committed
968
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
969 970 971 972 973 974 975 976 977 978 979
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_expires::current);
   $ret=NULL;
}   

from_token:  {IS_HEADER_NAMED(From,f)}? token;
header_from  returns [belle_sip_header_from_t* ret]   
scope { belle_sip_header_from_t* current; }
@init { $header_from::current = belle_sip_header_from_new();$ret = $header_from::current; }
        
Erwan Croze's avatar
Erwan Croze committed
980 981 982 983 984 985 986 987
  :   from_token/* ( 'From' | 'f' )*/ sp_tab_colon from_spec
      {
        if (!belle_sip_header_address_get_uri((belle_sip_header_address_t*)($header_from::current))
              && !belle_sip_header_address_get_absolute_uri((belle_sip_header_address_t*)($header_from::current))) {
          belle_sip_object_unref($header_from::current);
          $ret=NULL;
        }
      };
jehan's avatar
jehan committed
988
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
989 990 991 992 993 994 995
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_from::current);
   $ret=NULL;
} 
  
from_spec   
996
  :   ( name_addr_with_generic_uri[BELLE_SIP_HEADER_ADDRESS($header_from::current)] | paramless_addr_spec_with_generic_uri[BELLE_SIP_HEADER_ADDRESS($header_from::current)] )
Erwan Croze's avatar
Erwan Croze committed
997
      ( SEMI lws? from_param lws?)* ;
jehan's avatar
jehan committed
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
from_param  
  :   /*tag_param |*/ generic_param [BELLE_SIP_PARAMETERS($header_from::current)];
                       
/*
tag_param   
  :   'tag' EQUAL token;
*/
/*
in_reply_to  
  :   'In-Reply-To' HCOLON callid (COMMA callid);
*/
header_max_forwards  returns [belle_sip_header_max_forwards_t* ret]   
scope { belle_sip_header_max_forwards_t* current; }
@init { $header_max_forwards::current = belle_sip_header_max_forwards_new();$ret = $header_max_forwards::current; } 
  :   {IS_TOKEN(Max-Forwards)}? token /*'Max-Forwards'*/ hcolon 
    max_forwards {belle_sip_header_max_forwards_set_max_forwards($header_max_forwards::current,atoi((const char*)$max_forwards.text->chars));};
jehan's avatar
jehan committed
1014
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_max_forwards::current);
   $ret=NULL;
} 
max_forwards:DIGIT+;
/*
mime_version  
  :   'MIME-Version' HCOLON DIGIT+ '.' DIGIT+;

min_expires  
  :   'Min-Expires' HCOLON delta_seconds;

organization  
  :   'Organization' HCOLON (text_utf8_trim)?;

text_utf8_trim
  :
  ;

priority        
  :   'Priority' HCOLON priority_value;
priority_value  
  :   'emergency' | 'urgent' | 'normal'
                   | 'non-urgent' | other_priority;
other_priority  
  :   token;
*/
header_proxy_authenticate   returns [belle_sip_header_proxy_authenticate_t* ret]   
scope { belle_sip_header_proxy_authenticate_t* current; }
@init { $header_proxy_authenticate::current = belle_sip_header_proxy_authenticate_new();$ret = $header_proxy_authenticate::current; } 
  :   {IS_TOKEN(Proxy-Authenticate)}? token /*'Proxy-Authenticate'*/ 
  hcolon challenge[BELLE_SIP_HEADER_WWW_AUTHENTICATE($header_proxy_authenticate::current)];
jehan's avatar
jehan committed
1048
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_proxy_authenticate::current);
   $ret=NULL;
} 

challenge [belle_sip_header_www_authenticate_t* www_authenticate]           
  :   ({IS_TOKEN(Digest)}? token /*'Digest'*/ {belle_sip_header_www_authenticate_set_scheme(www_authenticate,"Digest");} 
     lws digest_cln[www_authenticate] (comma digest_cln[www_authenticate])*)
                       | other_challenge [www_authenticate];
other_challenge [belle_sip_header_www_authenticate_t* www_authenticate]    
  :   auth_scheme {belle_sip_header_www_authenticate_set_scheme(www_authenticate,(char*)$auth_scheme.text->chars);} 
1061 1062
     lws auth_param[(belle_sip_header_authorization_t*)www_authenticate]
                       (comma auth_param[(belle_sip_header_authorization_t*)www_authenticate])*;
jehan's avatar
jehan committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
digest_cln [belle_sip_header_www_authenticate_t* www_authenticate]         
  : 
   realm {belle_sip_header_www_authenticate_set_realm(www_authenticate,(char*)$realm.ret);
           belle_sip_free($realm.ret);} 
  | nonce {belle_sip_header_www_authenticate_set_nonce(www_authenticate,(char*)$nonce.ret);
           belle_sip_free($nonce.ret);}
  | algorithm {belle_sip_header_www_authenticate_set_algorithm(www_authenticate,$algorithm.ret);} 
  | opaque  {belle_sip_header_www_authenticate_set_opaque(www_authenticate,$opaque.ret);
             belle_sip_free($opaque.ret);}
  | qop_opts {belle_sip_header_www_authenticate_set_qop(www_authenticate,$qop_opts.ret);
              /*belle_sip_free($qop_opts.ret);*/}
  | domain {belle_sip_header_www_authenticate_set_domain(www_authenticate,$domain.ret);
             belle_sip_free($domain.ret);} 
  | stale { if (strcmp("true",$stale.ret)==0) {
               belle_sip_header_www_authenticate_set_stale(www_authenticate,1);
             }
          }
     | auth_param[(belle_sip_header_authorization_t*)www_authenticate];
/* the cast above is very BAD, but auth_param works on fields common to the two structures*/

realm returns [char* ret=NULL]              
  :   {IS_TOKEN(realm)}? token /*'realm'*/ equal realm_value {
                      $ret = _belle_sip_str_dup_and_unquote_string((char*)$realm_value.text->chars);
                       };
realm_value         
  :   quoted_string ;

domain returns [char* ret=NULL]              
  :   {IS_TOKEN(domain)}? token /*'domain'*/ equal quoted_string {
                      $ret = _belle_sip_str_dup_and_unquote_string((char*)$quoted_string.text->chars);};
  /* LDQUOT uri
                       ( SP+ uri )* RDQUOT;
uri                 
  :   absoluteURI | '/'.;
*/
nonce  returns [char* ret=NULL]             
  :   {IS_TOKEN(nonce)}? token /*'nonce'*/ equal nonce_value{
                      $ret = _belle_sip_str_dup_and_unquote_string((char*)$nonce_value.text->chars);
                       };
opaque returns [char* ret=NULL]             
  :   {IS_TOKEN(opaque)}? token /*'opaque'*/ equal quoted_string{
                      $ret = _belle_sip_str_dup_and_unquote_string((char*)$quoted_string.text->chars);
                       };

stale  returns [const char* ret=NULL]             
  :   {IS_TOKEN(stale)}? token /*'stale'*/ equal stale_value {$ret=(char*)$stale_value.text->chars;} /* ( 'true' | 'false' )*/;

stale_value:token;

algorithm returns [const char* ret=NULL]           
  :   {IS_TOKEN(algorithm)}? token /*'algorithm'*/ equal /* ( 'MD5' | 'MD5-sess'
                       |*/ alg_value=token {$ret=(char*)$alg_value.text->chars;}/*)*/
  ;

qop_opts returns [belle_sip_list_t* ret=NULL]        
scope { belle_sip_list_t* list; }
@init{$qop_opts::list=NULL;}
  :   {IS_TOKEN(qop)}? token /*'qop'*/ equal 
  /*ldquot*/DQUOTE 
  qop_opts_value 
  (COMMA qop_opts_value)* 
  /*rdquot*/DQUOTE {$ret=$qop_opts::list;} ;
  
qop_opts_value  
: lws? token lws? {$qop_opts::list=belle_sip_list_append($qop_opts::list,belle_sip_strdup((const char*)$token.text->chars));};

header_proxy_authorization  returns [belle_sip_header_proxy_authorization_t* ret=NULL]
scope { belle_sip_header_proxy_authorization_t* current; }
@init { $header_proxy_authorization::current = belle_sip_header_proxy_authorization_new();$ret = $header_proxy_authorization::current; }
  :   {IS_TOKEN(Proxy-Authorization)}? token /*'Proxy-Authorization'*/ hcolon credentials[(belle_sip_header_authorization_t*)$header_proxy_authorization::current];
jehan's avatar
jehan committed
1133
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_proxy_authorization::current);
   $ret=NULL;
}

/*
proxy_require  
  :   'Proxy-Require' HCOLON option_tag
                  (COMMA option_tag)*;
option_tag     
  :   token;
*/
/*FIXME service-route = recorde-route = route, too many copy/past*/
service_route_token:  {IS_TOKEN(Service-Route)}? token;
header_service_route  returns [belle_sip_header_service_route_t* ret=NULL]   
scope { belle_sip_header_service_route_t* current; belle_sip_header_service_route_t* first;}
1151
@init { $header_service_route::current = NULL;$header_service_route::first =NULL;}
jehan's avatar
jehan committed
1152
  :   service_route_token /*'Service-Route'*/ sp_tab_colon srv_route (COMMA srv_route)* {$ret = $header_service_route::first;};
jehan's avatar
jehan committed
1153
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1154 1155
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
1156
   $ret = $header_service_route::first;
jehan's avatar
jehan committed
1157
   if ($ret) belle_sip_object_unref($ret);
jehan's avatar
jehan committed
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
   $ret=NULL;
}
srv_route
scope { belle_sip_header_service_route_t* prev;}
@init { if ($header_service_route::current == NULL) {
            $header_service_route::first = $header_service_route::current = belle_sip_header_service_route_new();
            $srv_route::prev=NULL;
         } else {
            belle_sip_header_t* header = BELLE_SIP_HEADER($header_service_route::current); 
            $srv_route::prev=$header_service_route::current;
            belle_sip_header_set_next(header,(belle_sip_header_t*)($header_service_route::current = belle_sip_header_service_route_new()));
         } 
      }     
jehan's avatar
jehan committed
1171 1172
   :   name_addr[BELLE_SIP_HEADER_ADDRESS($header_service_route::current)] ( SEMI lws? sr_param lws?)*;

jehan's avatar
jehan committed
1173 1174 1175 1176 1177 1178 1179
  
sr_param      
  :   generic_param[BELLE_SIP_PARAMETERS($header_service_route::current)];
  
record_route_token:  {IS_TOKEN(Record-Route)}? token;
header_record_route  returns [belle_sip_header_record_route_t* ret=NULL]   
scope { belle_sip_header_record_route_t* current; belle_sip_header_record_route_t* first;}
1180
@init { $header_record_route::current = NULL;$header_record_route::first =NULL;}
jehan's avatar
jehan committed
1181
  :   record_route_token /*'Record-Route'*/ sp_tab_colon rec_route ( COMMA rec_route)* {$ret = $header_record_route::first;};
jehan's avatar
jehan committed
1182
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1183 1184
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
1185
   $ret = $header_record_route::first;
jehan's avatar
jehan committed
1186
   if ($ret) belle_sip_object_unref($ret);
jehan's avatar
jehan committed
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
   $ret=NULL;
}
rec_route
scope { belle_sip_header_record_route_t* prev;}
@init { if ($header_record_route::current == NULL) {
            $header_record_route::first = $header_record_route::current = belle_sip_header_record_route_new();
            $rec_route::prev=NULL;
         } else {
            belle_sip_header_t* header = BELLE_SIP_HEADER($header_record_route::current); 
            $rec_route::prev=$header_record_route::current;
            belle_sip_header_set_next(header,(belle_sip_header_t*)($header_record_route::current = belle_sip_header_record_route_new()));
         } 
      }     
  :   name_addr[BELLE_SIP_HEADER_ADDRESS($header_record_route::current)] ( SEMI lws? rr_param lws? )*;
  
rr_param      
  :   generic_param[BELLE_SIP_PARAMETERS($header_record_route::current)];
/*
reply_to      
  :   'Reply-To' HCOLON rplyto_spec;
rplyto_spec   
  :   ( name_addr | addr_spec )
                 ( SEMI rplyto_param )*;
rplyto_param  
  :   generic_param;
require       
  :   'Require' HCOLON option_tag (COMMA option_tag)*;

retry_after  
  :   'Retry-After' HCOLON delta_seconds
                 comment? ( SEMI retry_param )*;
*/
comment : LPAREN . RPAREN;
  
/*
retry_param  
  :   ('duration' EQUAL delta_seconds)
                | generic_param;
*/
route_token:  {IS_TOKEN(Route)}? token;
header_route  returns [belle_sip_header_route_t* ret=NULL]   
scope { belle_sip_header_route_t* current;belle_sip_header_route_t* first; }
1229
@init { $header_route::current = NULL; $header_route::first =NULL;}
jehan's avatar
jehan committed
1230
  :   route_token /*'Route'*/ sp_tab_colon route_param ( COMMA route_param)*{$ret = $header_route::first;};
jehan's avatar
jehan committed
1231
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1232 1233
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
1234
   $ret = $header_route::first;
jehan's avatar
jehan committed
1235 1236 1237
   if ($ret) belle_sip_object_unref($ret);
   $ret=NULL;
}
jehan's avatar
jehan committed
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
route_param 
scope { belle_sip_header_route_t* prev;}
@init { if ($header_route::current == NULL) {
            $header_route::first = $header_route::current = belle_sip_header_route_new();
             $route_param::prev=NULL;
         } else {
            belle_sip_header_t* header = BELLE_SIP_HEADER($header_route::current); 
            $route_param::prev=$header_route::current;
            belle_sip_header_set_next(header,(belle_sip_header_t*)($header_route::current = belle_sip_header_route_new()));
         } 
      }      
  :   name_addr[BELLE_SIP_HEADER_ADDRESS($header_route::current)] ( SEMI lws? r_param lws?)*;
jehan's avatar
jehan committed
1250

jehan's avatar
jehan committed
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
r_param      
  :   generic_param[BELLE_SIP_PARAMETERS($header_route::current)];
/*
server           
  :   'Server' HCOLON server_val (lws server_val)*;
*/

/*
subject  
  :   ( 'Subject' | 's' ) HCOLON (text_utf_huit)?;

text_utf_huit
  :
  ;

supported  
  :   ( 'Supported' | 'k' ) HCOLON
              (option_tag (COMMA option_tag)*)?;

timestamp  
  :   'Timestamp' HCOLON (DIGIT)+
               ( '.' (DIGIT)* )? ( lws delay )?;
delay      
  :   (DIGIT)* ( '.' (DIGIT)* )?;
*/
to_token:  {IS_HEADER_NAMED(To,t)}? token;
header_to  returns [belle_sip_header_to_t* ret=NULL]   
scope { belle_sip_header_to_t* current; }
@init { $header_to::current = belle_sip_header_to_new(); $ret = $header_to::current;}
        
  :   to_token /*'To' ( 'To' | 't' )*/ sp_tab_colon to_spec;
jehan's avatar
jehan committed
1282
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1283 1284 1285 1286 1287 1288
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_to::current);
   $ret=NULL;
}  
to_spec   
1289
  :   ( name_addr_with_generic_uri[BELLE_SIP_HEADER_ADDRESS($header_to::current)] | paramless_addr_spec_with_generic_uri[BELLE_SIP_HEADER_ADDRESS($header_to::current)] )
jehan's avatar
jehan committed
1290 1291 1292 1293
               ( SEMI lws? to_param lws?)*;
to_param  
  :   /*tag_param |*/ generic_param [BELLE_SIP_PARAMETERS($header_to::current)];

jehan's avatar
jehan committed
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
diversion_token: {IS_HEADER_NAMED(Diversion,d)}? token;
header_diversion returns [belle_sip_header_diversion_t* ret=NULL]
scope { belle_sip_header_diversion_t* current; }
@init { $header_diversion::current = belle_sip_header_diversion_new(); $ret = $header_diversion::current;}

  :   diversion_token /*'Diversion' ( 'Diversion' | 'd' )*/ sp_tab_colon diversion_spec;
catch [ANTLR3_RECOGNITION_EXCEPTION]
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_diversion::current);
   $ret=NULL;
}
diversion_spec
  :   ( name_addr_with_generic_uri[BELLE_SIP_HEADER_ADDRESS($header_diversion::current)] | paramless_addr_spec_with_generic_uri[BELLE_SIP_HEADER_ADDRESS($header_diversion::current)] )
               ( SEMI lws? diversion_param lws?)*;
diversion_param
  :   /*tag_param |*/ generic_param [BELLE_SIP_PARAMETERS($header_diversion::current)];

jehan's avatar
jehan committed
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
refer_to_token:  {IS_TOKEN(Refer-To)}? token;
header_refer_to  returns [belle_sip_header_refer_to_t* ret=NULL]   
  :   refer_to_token /*'Refer-To'*/ 
      sp_tab_colon 
      refer_to_spec[BELLE_SIP_HEADER_ADDRESS(belle_sip_header_refer_to_new())] {$ret = BELLE_SIP_HEADER_REFER_TO($refer_to_spec.ret);};

referred_by_token:  {IS_TOKEN(Referred-By)}? token;
header_referred_by  returns [belle_sip_header_referred_by_t* ret=NULL]   
  :   referred_by_token /*'Referred-By'*/ 
      sp_tab_colon 
      refer_to_spec[BELLE_SIP_HEADER_ADDRESS(belle_sip_header_referred_by_new())] {$ret = BELLE_SIP_HEADER_REFERRED_BY($refer_to_spec.ret);};
  
refer_to_spec [belle_sip_header_address_t* address]    returns [belle_sip_header_address_t* ret]
@init {$ret=address;}
  :   (( name_addr[address] | paramless_addr_spec[address])  ( SEMI lws? generic_param [BELLE_SIP_PARAMETERS(address)] lws? )* );
jehan's avatar
jehan committed
1327
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref(address);
   $ret=NULL;
} 

    
/*
unsupported  
  :   'Unsupported' HCOLON option_tag (COMMA option_tag)*;
*/
header_user_agent  returns [belle_sip_header_user_agent_t* ret]   
scope { belle_sip_header_user_agent_t* current; }
@init { $header_user_agent::current = belle_sip_header_user_agent_new();$ret = $header_user_agent::current;}
  :   {IS_TOKEN(User-Agent)}? token /*'User-Agent'*/ hcolon server_val (lws server_val)*;
jehan's avatar
jehan committed
1343
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_user_agent::current);
   $ret=NULL;
} 

server_val    : word {belle_sip_header_user_agent_add_product($header_user_agent::current,(const char*)$word.text->chars); };
/*serval_item    
  :   product | comment  ;
product          
  :   token (SLASH product_version)?;
product_version  
  :   token;
  */
via_token:  {IS_HEADER_NAMED(Via,v)}? token;
header_via  returns [belle_sip_header_via_t* ret]   
scope { belle_sip_header_via_t* current; belle_sip_header_via_t* first; }
1361
@init { $header_via::current = NULL;$header_via::first =NULL;$ret = NULL;}
jehan's avatar
jehan committed
1362 1363
        
  :   via_token/* ( 'via' | 'v' )*/ hcolon via_parm (comma via_parm)* {$ret = $header_via::first;} ;
jehan's avatar
jehan committed
1364
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1365 1366
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
1367
   $ret = $header_via::first;
jehan's avatar
jehan committed
1368 1369 1370
   if ($ret) belle_sip_object_unref($ret);
   $ret=NULL;
}
jehan's avatar
jehan committed
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
via_parm
scope { belle_sip_header_via_t* prev;}
@init { if ($header_via::current == NULL) {
            $header_via::first = $header_via::current = belle_sip_header_via_new();
            $via_parm::prev=NULL;
         } else {
      belle_sip_header_t* header;
            $via_parm::prev=$header_via::current;
            header = BELLE_SIP_HEADER($header_via::current); 
            belle_sip_header_set_next(header,(belle_sip_header_t*)($header_via::current = belle_sip_header_via_new()));
         } 
      }          
  :   sent_protocol  lws sent_by ( semi via_params )*;
  
via_params        
  :   /*via_ttl | via_maddr
                     |  via_branch
                     | via_extension */ via_received[$header_via::current] | generic_param [BELLE_SIP_PARAMETERS($header_via::current)];
/*via_ttl           
  :   'ttl' EQUAL ttl;
via_maddr         
  :   'maddr' EQUAL host;*/
via_received [belle_sip_header_via_t* object] 
  : {IS_TOKEN(received)}? token EQUAL via_address {belle_sip_header_via_set_received(object,(const char*)$via_address.text->chars);};

via_address: ipv4address | ipv6address;
/*
via_branch        
  :   'branch' EQUAL token;
via_extension     
  :   generic_param;*/
sent_protocol     
  :   (protocol_name slash protocol_version) {belle_sip_header_via_set_protocol($header_via::current,(const char*)$text->chars);}
                     slash transport {belle_sip_header_via_set_transport($header_via::current,(const char*)$transport.text->chars);} ;
protocol_name     
  :   /*'SIP' |*/ token;
protocol_version  
  :   token;
transport         
  :  /* 'UDP' | 'TCP' | 'TLS' | 'SCTP'
                     | */ other_transport;
other_transport
  : token;

sent_by           
  :   host {belle_sip_header_via_set_host($header_via::current,$host.ret);}
     ( COLON port {belle_sip_header_via_set_port($header_via::current,$port.ret);} )? ;


/*
warning        
  :   'Warning' HCOLON warning_value (COMMA warning_value)*;
warning_value  
  :   warn_code SP warn_agent SP warn_text;
warn_code      
  :   DIGIT DIGIT DIGIT;
warn_agent     
  :   hostport | pseudonym;
                  //  the name or pseudonym of the server adding
                  //  the Warning header, for use in debugging
warn_text      
  :   quoted_string;
pseudonym      
  :   token;
*/
header_www_authenticate returns [belle_sip_header_www_authenticate_t* ret]   
scope { belle_sip_header_www_authenticate_t* current; }
@init { $header_www_authenticate::current = belle_sip_header_www_authenticate_new();$ret = $header_www_authenticate::current; } 
  :   {IS_TOKEN(WWW-Authenticate)}? token /*'WWW-Authenticate'*/ hcolon challenge[$header_www_authenticate::current];
jehan's avatar
jehan committed
1440
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_www_authenticate::current);
   $ret=NULL;
}
state_value: token ;

header_subscription_state  returns [belle_sip_header_subscription_state_t* ret] 
scope { belle_sip_header_subscription_state_t* current; }
@init { $header_subscription_state::current = belle_sip_header_subscription_state_new();$ret = $header_subscription_state::current; } 
 : {IS_TOKEN(Subscription-State)}? token /*"Subscription-State"*/ 
 hcolon state_value {belle_sip_header_subscription_state_set_state($header_subscription_state::current,(const char*)$state_value.text->chars);} 
 (semi  generic_param [BELLE_SIP_PARAMETERS($header_subscription_state::current)])* ;
jehan's avatar
jehan committed
1454
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1455 1456 1457 1458 1459 1460
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_subscription_state::current);
   $ret=NULL;
}

1461 1462 1463 1464 1465 1466
header_event  returns [belle_sip_header_event_t* ret] 
scope { belle_sip_header_event_t* current; }
@init { $header_event::current = belle_sip_header_event_new();$ret = $header_event::current; } 
 : {IS_TOKEN(Event)}? token /*"Event"*/ 
 hcolon event_package {belle_sip_header_event_set_package_name($header_event::current,(const char*)$event_package.text->chars);} 
 (semi  generic_param [BELLE_SIP_PARAMETERS($header_event::current)])* ;
jehan's avatar
jehan committed
1467
catch [ANTLR3_RECOGNITION_EXCEPTION]
1468 1469 1470 1471 1472 1473 1474 1475
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_event::current);
   $ret=NULL;
}
event_package
	:	 token;

jehan's avatar
jehan committed
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
/*
Replaces         = "Replaces" HCOLON replaces-values
                          *(COMMA replaces-values)

      replaces-values  = callid *( SEMI replaces-param )
      callid           = token [ "@" token ]
      replaces-param   = to-tag | from-tag | extension-param
      to-tag           = "to-tag" EQUAL ( UUID | "*" )
      from-tag         = "from-tag" EQUAL UUID
      extension-param  = token [ EQUAL ( token | quoted-string ) ]
*/    
header_replaces  returns [belle_sip_header_replaces_t* ret] 
scope { belle_sip_header_replaces_t* current; }
@init { $header_replaces::current = belle_sip_header_replaces_new();$ret = $header_replaces::current; } 
 : {IS_TOKEN(Replaces)}? token /*"Replaces"*/ 
 hcolon call_id {belle_sip_header_replaces_set_call_id($header_replaces::current,(const char*)$call_id.text->chars);} 
  (semi  generic_param [BELLE_SIP_PARAMETERS($header_replaces::current)])* ;
jehan's avatar
jehan committed
1493
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_replaces::current);
   $ret=NULL;
}

//**********************************Privacy*******************************//


header_p_preferred_identity returns [belle_sip_header_p_preferred_identity_t* ret]   
@init { $ret=NULL; }
  :  {IS_TOKEN(P-Preferred-Identity)}? token /*"P-Preferred-Identity"*/ 
 sp_tab_colon header_address_base[(belle_sip_header_address_t*)belle_sip_header_p_preferred_identity_new()] {$ret=(belle_sip_header_p_preferred_identity_t*)$header_address_base.ret;}; 
  
header_privacy  returns [belle_sip_header_privacy_t* ret]   
scope { belle_sip_header_privacy_t* current; }
@init { $header_privacy::current = belle_sip_header_privacy_new();$ret = $header_privacy::current;}
  :   {IS_TOKEN(Privacy)}? token /*'Privacy'*/ hcolon privacy_val (semi privacy_val)*;
jehan's avatar
jehan committed
1512
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1513 1514 1515 1516 1517 1518 1519
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($ret);
   $ret=NULL;
} 
privacy_val: token {belle_sip_header_privacy_add_privacy($header_privacy::current,(const char*)$token.text->chars);};

jehan's avatar
jehan committed
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
//**********************************Supported*******************************//
header_supported  returns [belle_sip_header_supported_t* ret]
scope { belle_sip_header_supported_t* current; }
@init { $header_supported::current = belle_sip_header_supported_new();$ret = $header_supported::current;}
:   {IS_TOKEN(Supported)}? token /*'Supported'*/ hcolon supported_val (comma supported_val)*;
catch [ANTLR3_RECOGNITION_EXCEPTION]
{
	belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
	belle_sip_object_unref($ret);
	$ret=NULL;
}
supported_val: token {belle_sip_header_supported_add_supported($header_supported::current,(const char*)$token.text->chars);};

Viacheslav Demydiuk's avatar
Viacheslav Demydiuk committed
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
//**********************************Require*******************************//
header_require  returns [belle_sip_header_require_t* ret]
scope { belle_sip_header_require_t* current; }
@init { $header_require::current = belle_sip_header_require_new();$ret = $header_require::current;}
:   {IS_TOKEN(Require)}? token /*'Require'*/ hcolon require_val (comma require_val)*;
catch [ANTLR3_RECOGNITION_EXCEPTION]
{
	belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
	belle_sip_object_unref($ret);
	$ret=NULL;
}
require_val: token {belle_sip_header_require_add_require($header_require::current,(const char*)$token.text->chars);};

1546
//**********************************Content-Disposition*******************************//
1547

1548 1549
content_disposition_value: token ;

1550 1551
header_content_disposition  returns [belle_sip_header_content_disposition_t* ret]
scope { belle_sip_header_content_disposition_t* current; }
1552 1553 1554 1555
@init { $header_content_disposition::current = belle_sip_header_content_disposition_new();$ret = $header_content_disposition::current; }
: {IS_TOKEN(Content-Disposition)}? token /*"Content-Disposition"*/
hcolon content_disposition_value {belle_sip_header_content_disposition_set_content_disposition($header_content_disposition::current,(const char*)$content_disposition_value.text->chars);}
(semi  generic_param [BELLE_SIP_PARAMETERS($header_content_disposition::current)])* ;
1556 1557 1558
catch [ANTLR3_RECOGNITION_EXCEPTION]
{
	belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
1559
	belle_sip_object_unref($header_content_disposition::current);
1560 1561 1562 1563 1564 1565 1566
	$ret=NULL;
}
//**********************************Accept*******************************//


accept_token :  {IS_TOKEN(Accept)}? token;
header_accept  returns [belle_sip_header_accept_t* ret=NULL]
1567 1568 1569
scope { belle_sip_header_accept_t* current; belle_sip_header_accept_t* first; }
@init { $header_accept::current = NULL;$header_accept::first =NULL;$ret = NULL;}
:  accept_token/* ( 'Accept')*/ hcolon accept_param (comma accept_param)* {$ret = $header_accept::first;} ;
1570 1571 1572
catch [ANTLR3_RECOGNITION_EXCEPTION]
{
	belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
1573 1574
	$ret = $header_accept::first;
	if ($ret) belle_sip_object_unref($ret);
1575 1576
	$ret=NULL;
}
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589

accept_param
scope { belle_sip_header_accept_t* prev;}
@init { if ($header_accept::current == NULL) {
	$header_accept::first = $header_accept::current = belle_sip_header_accept_new();
	$accept_param::prev=NULL;
} else {
	belle_sip_header_t* header;
	$accept_param::prev=$header_accept::current;
	header = BELLE_SIP_HEADER($header_accept::current);
	belle_sip_header_set_next(header,(belle_sip_header_t*)($header_accept::current = belle_sip_header_accept_new()));
}
}
1590 1591 1592 1593 1594 1595 1596
:  accept_main_media_type {belle_sip_header_accept_set_type($header_accept::current,(const char*)$accept_main_media_type.text->chars);}
slash
accept_sub_media_type {belle_sip_header_accept_set_subtype($header_accept::current,(const char*)$accept_sub_media_type.text->chars);}
(semi  generic_param [BELLE_SIP_PARAMETERS($header_accept::current)])*;

accept_main_media_type: token;
accept_sub_media_type: token;
jehan's avatar
jehan committed
1597

jehan's avatar
jehan committed
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
//****************************Reason***********************************//
/*
Reason            =  "Reason" HCOLON reason-value *(COMMA reason-value)
 reason-value      =  protocol *(SEMI reason-params)
 protocol          =  "SIP" / "Q.850" / token
 reason-params     =  protocol-cause / reason-text
                      / reason-extension
 protocol-cause    =  "cause" EQUAL cause
 cause             =  1*DIGIT
 reason-text       =  "text" EQUAL quoted-string
 reason-extension  =  generic-param
jehan's avatar
jehan committed
1609

jehan's avatar
jehan committed
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
*/
header_reason  returns [belle_sip_header_reason_t* ret=NULL]
scope { belle_sip_header_reason_t* current;belle_sip_header_reason_t* first; }
@init { $header_reason::current = NULL; $header_reason::first =NULL;}
:   {IS_TOKEN(Reason)}? token /*'Reason'*/ hcolon header_reason_param  ( comma header_reason_param)* {$ret = $header_reason::first; };
catch [ANTLR3_RECOGNITION_EXCEPTION]
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   $ret = $header_reason::first;
   if ($ret) belle_sip_object_unref($ret);
   $ret=NULL;
} 
header_reason_param 
scope { belle_sip_header_reason_t* prev;}
@init { if ($header_reason::current == NULL) {
            $header_reason::first = $header_reason::current = belle_sip_header_reason_new();
             $header_reason_param::prev=NULL;
         } else {
            belle_sip_header_t* header = BELLE_SIP_HEADER($header_reason::current); 
            $header_reason_param::prev=$header_reason::current;
            belle_sip_header_set_next(header,(belle_sip_header_t*)($header_reason::current = belle_sip_header_reason_new()));
         } 
      }      
	:header_reason_protocol {belle_sip_header_reason_set_protocol($header_reason::current,(const char*)$header_reason_protocol.text->chars);} 
	(semi  generic_param [BELLE_SIP_PARAMETERS($header_reason::current)])*;
header_reason_protocol
	:	token;
1637
//*********************************************//
jehan's avatar
jehan committed
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678

/*
AuthenticationInfo = "Authentication-Info" ":" auth-info
        auth-info          = 1#(nextnonce | [ message-qop ]
                               | [ response-auth ] | [ cnonce ]
                               | [nonce-count] )
        nextnonce          = "nextnonce" "=" nonce-value
        response-auth      = "rspauth" "=" response-digest
        response-digest    = <"> *LHEX <">
*/

header_authentication_info  returns [belle_sip_header_authentication_info_t* ret=NULL]     
scope { belle_sip_header_authentication_info_t* current; }
@init {$header_authentication_info::current = belle_sip_header_authentication_info_new(); $ret=$header_authentication_info::current; }
  :   {IS_TOKEN( Authentication-Info)}? token /*' Authentication-Info'*/ hcolon   auth_info ( comma auth_info)*;
  catch [ANTLR3_RECOGNITION_EXCEPTION]
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($header_authentication_info::current);
   $ret=NULL;
}
 auth_info
  	:	
  ( next_nonce { belle_sip_header_authentication_info_set_next_nonce($header_authentication_info::current,(char*)$next_nonce.ret);
                 belle_sip_free($next_nonce.ret);
           }
  |cnonce{
            belle_sip_header_authentication_info_set_cnonce($header_authentication_info::current,(char*)$cnonce.ret);
            belle_sip_free($cnonce.ret);
           }
  | authentication_info_message_qop{
            belle_sip_header_authentication_info_set_qop($header_authentication_info::current,$authentication_info_message_qop.ret);
           }
   |nonce_count{
            belle_sip_header_authentication_info_set_nonce_count($header_authentication_info::current,atoi((char*)$nonce_count.ret));
           } 
   |rspauth {
   	belle_sip_header_authentication_info_set_rsp_auth($header_authentication_info::current,$rspauth.ret);
   	 belle_sip_free($rspauth.ret);
           }) ;

1679 1680 1681
qop_token
	:	 {IS_TOKEN(qop)}? token/*'qop'*/;
/*as defined by 3261*/
jehan's avatar
jehan committed
1682
authentication_info_message_qop  returns [const char* ret=NULL]     
1683
  :   qop_token equal  token  {  $ret = (const char*)$token.text->chars;};
jehan's avatar
jehan committed
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698

rspauth  returns [char* ret=NULL]             
  :   {IS_TOKEN(rspauth)}? token /*'nonce'*/ equal quoted_string{
                      $ret = _belle_sip_str_dup_and_unquote_string((char*)$quoted_string.text->chars);
                       };
next_nonce  returns [char* ret=NULL]             
  :   {IS_TOKEN(nextnonce)}? token /*'nonce'*/ equal nonce_value{
                      $ret = _belle_sip_str_dup_and_unquote_string((char*)$nonce_value.text->chars);
                       };
                       
quoted_algorithm  returns [char* ret=NULL]             
  :   {IS_TOKEN(nextnonce)}? token /*'nonce'*/ equal nonce_value{
                      $ret = _belle_sip_str_dup_and_unquote_string((char*)$nonce_value.text->chars);
                       };
//**********************************************//
jehan's avatar
jehan committed
1699 1700 1701
header  returns [belle_sip_header_t* ret=NULL]
	 : header_extension_base[FALSE] {$ret=$header_extension_base.ret;};
	
jehan's avatar
jehan committed
1702
//********************************************************************************************//
jehan's avatar
jehan committed
1703
header_extension_base[ANTLR3_BOOLEAN is_http]  returns [belle_sip_header_t* ret]
jehan's avatar
jehan committed
1704
scope {int as_value;}
1705
@init {$header_extension_base::as_value=0;$ret=NULL;}  
jehan's avatar
jehan committed
1706
  :    (header_name 
1707
       hcolon /*sp_tab_colon*/ /*because LWS can be in both colon or header_value*/ 
1708 1709
       (header_value[(const char*)$header_name.text->chars,$is_http ]{$header_extension_base::as_value=1;$ret=$header_value.ret;})?)  {
       	if (!$ret && !$header_extension_base::as_value) { /*to handle value parsing error*/
1710 1711 1712 1713 1714
       	/*special case: header without value*/
       		$ret=belle_sip_header_create((const char*)$header_name.text->chars,NULL);
       	}
       };
                   
jehan's avatar
jehan committed
1715
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1716 1717 1718 1719 1720
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   if ($ret) belle_sip_object_unref($ret);
   $ret=NULL;
}                   
jehan's avatar
jehan committed
1721 1722 1723
header_name       
  :   token;

1724
header_value[const char* name, ANTLR3_BOOLEAN is_http] returns [belle_sip_header_t* ret] 
jehan's avatar
jehan committed
1725
options { greedy = false; }      
1726
 @init {$ret=NULL;}  
1727
  :  (~(SP|CRLF) ((CRLF SP) | ~CRLF)* )  {
1728 1729 1730 1731 1732 1733
                    if ($is_http) {
                    	$ret=belle_http_header_create($name,(const char*)$header_value.text->chars);
                    }else {
                    	$ret=belle_sip_header_create($name,(const char*)$header_value.text->chars);
                    }
                   } ; 
jehan's avatar
jehan committed
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
  
message_body
options { greedy = false; }  
  :   OCTET+;

paramless_uri  returns [belle_sip_uri_t* ret=NULL]    
scope { belle_sip_uri_t* current; }
@init { $paramless_uri::current = belle_sip_uri_new(); }
   :  sip_schema[$paramless_uri::current] ( (userinfo[$paramless_uri::current]) =>(userinfo[$paramless_uri::current] hostport[$paramless_uri::current]) | hostport[$paramless_uri::current])
   headers[$paramless_uri::current]? {$ret = $paramless_uri::current;}; 
jehan's avatar
jehan committed
1744
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($paramless_uri::current);
   $ret=NULL;
}

uri  returns [belle_sip_uri_t* ret=NULL]    
scope { belle_sip_uri_t* current; }
@init { $uri::current = belle_sip_uri_new(); }
   :  sip_schema[$uri::current] ( ((userinfo[NULL])=>userinfo[$uri::current] hostport[$uri::current]) | hostport[$uri::current] ) 
   uri_parameters[$uri::current]? 
   headers[$uri::current]? {$ret = $uri::current;}; 
jehan's avatar
jehan committed
1757
catch [ANTLR3_RECOGNITION_EXCEPTION]
jehan's avatar
jehan committed
1758 1759 1760 1761 1762 1763
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($uri::current);
   $ret=NULL;
}

jehan's avatar
jehan committed
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
fast_uri  returns [belle_sip_uri_t* ret=NULL]    
scope { belle_sip_uri_t* current; }
@init { $fast_uri::current = belle_sip_uri_new(); }
   :  sip_schema[$fast_uri::current] ( ((userinfo[NULL])=>userinfo[$fast_uri::current] fast_hostport[$fast_uri::current]) | fast_hostport[$fast_uri::current] ) 
   uri_parameters[$fast_uri::current]? 
   headers[$fast_uri::current]? {$ret = $fast_uri::current;}; 
catch [ANTLR3_RECOGNITION_EXCEPTION]
{
   belle_sip_message("[\%s]  reason [\%s]",(const char*)EXCEPTION->name,(const char*)EXCEPTION->message);
   belle_sip_object_unref($fast_uri::current);
   $ret=NULL;
}


jehan's avatar
jehan committed
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
sip_token:  {IS_TOKEN(sip)}? token;
sips_token:  {IS_TOKEN(sips)}? token;

sip_schema[belle_sip_uri_t* uri]  : (sips_token {belle_sip_uri_set_secure(uri,1);}
            | sip_token) COLON ;
userinfo[belle_sip_uri_t* uri] 
scope { belle_sip_uri_t* current; }
@init {$userinfo::current=uri;}
       :  user ( COLON password )?  AT;
user            :   ( unreserved  | escaped | user_unreserved )+ {
                                                                  char* unescaped_username;
                                                                  unescaped_username=belle_sip_to_unescaped_string((const char *)$text->chars);
                                                                  belle_sip_uri_set_user($userinfo::current,unescaped_username);
                                                                  belle_sip_free(unescaped_username);
                                                                  };
password        :   ( unreserved | escaped |AND | EQUAL | PLUS | DOLLARD | COMMA )* {
                                                                              char* unescaped_userpasswd;
1795
                                                                              const char* source = (const char*)$text->chars;
1796
                                                                              if( source != NULL ){
1797
                                                                                unescaped_userpasswd=belle_sip_to_unescaped_string((const char *)source);
1798 1799
                                                                                belle_sip_uri_set_user_password($userinfo::current,unescaped_userpasswd);
                                                                                belle_sip_free(unescaped_userpasswd);
1800
                                                                              }
jehan's avatar
jehan committed
1801 1802 1803 1804 1805 1806
                                                                              };
hostport[belle_sip_uri_t* uri] 
scope { belle_sip_uri_t* current; }
@init {$hostport::current=uri;}
        :   host ( COLON port {belle_sip_uri_set_port($hostport::current,$port.ret);})? {belle_sip_uri_set_host($hostport::current,$host.ret);};

jehan's avatar
jehan committed
1807 1808 1809 1810 1811 1812
fast_hostport[belle_sip_uri_t* uri] 
scope { belle_sip_uri_t* current; }
@init {$fast_hostport::current=uri;}
        :  fast_host ( COLON port {belle_sip_uri_set_port($fast_hostport::current,$port.ret);})? {belle_sip_uri_set_host($fast_hostport::current,$fast_host.ret);};


jehan's avatar
jehan committed
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
uri_parameters[belle_sip_uri_t* uri] 
scope { belle_sip_uri_t* current; }
@init {$uri_parameters::current=uri;}    
  :   ( (semi uri_parameter)|(lws? SEMI) )+; /*allow semi only teven if not in the rfc*/
uri_parameter  //all parameters are considered as other     
  :    other_param ;
other_param       
:  pname {
  char* unescaped_parameters = belle_sip_to_unescaped_string((const char *) $pname.text->chars);
  belle_sip_parameters_set_parameter(BELLE_SIP_PARAMETERS($uri_parameters::current)
                                      ,unescaped_parameters
                                      ,NULL);
  belle_sip_free(unescaped_parameters);
}
  |
   (pname EQUAL pvalue)  {
   char* unescaped_pname = belle_sip_to_unescaped_string((const char *) $pname.text->chars);
   char* unescaped_pvalue = belle_sip_to_unescaped_string((const char *) $pvalue.text->chars);
   belle_sip_parameters_set_parameter(BELLE_SIP_PARAMETERS($uri_parameters::current)
                                      ,unescaped_pname
                                      ,unescaped_pvalue);
  belle_sip_free(unescaped_pname);
  belle_sip_free(unescaped_pvalue);
  }
   ;

pname             
  :   paramchar+;
pvalue            
  :   paramchar+;
paramchar         
  :   (param_unreserved)=>param_unreserved | unreserved | escaped; 
param_unreserved  
  :   LSBRAQUET | RSBRAQUET | SLASH | COLON | AND | PLUS | DOLLARD | DOT;

headers[belle_sip_uri_t* uri] 
scope { belle_sip_uri_t* current; int is_hvalue; }
@init {$headers::current=uri; $headers::is_hvalue=0;}
jehan's avatar
jehan committed
1851 1852
                :  QMARK uri_header ( AND uri_header )* ;
uri_header           
jehan's avatar
jehan committed
1853
scope {int is_hvalue; }
jehan's avatar
jehan committed
1854 1855
@init {$uri_header::is_hvalue=0;}
              : hname EQUAL (hvalue{$uri_header::is_hvalue = 1;})? {
jehan's avatar
jehan committed
1856
                      char* unescaped_hname = belle_sip_to_unescaped_string((const char *)$hname.text->chars);
jehan's avatar
jehan committed
1857
                      char* unescaped_hvalue = ($uri_header::is_hvalue)?belle_sip_to_unescaped_string((const char *)$hvalue.text->chars):NULL;
jehan's avatar
jehan committed
1858 1859 1860 1861 1862 1863 1864
                      belle_sip_uri_set_header($headers::current,unescaped_hname,unescaped_hvalue);
                      belle_sip_free(unescaped_hname);
                      if (unescaped_hvalue) belle_sip_free(unescaped_hvalue);
                  };
hname           :  ( hnv_unreserved | unreserved | escaped )+;
hvalue          :  ( hnv_unreserved | unreserved | escaped )+;

1865 1866
/*hnv-unreserved  =  "[" / "]" / "/" / "?" / ":" / "+" / "$"*/
hnv_unreserved  :   LSBRAQUET | RSBRAQUET  | SLASH | QMARK | COLON | PLUS | DOLLARD ;
jehan's avatar
jehan committed
1867 1868


jehan's avatar
jehan committed
1869 1870


jehan's avatar
jehan committed
1871
	
jehan's avatar
jehan committed
1872 1873 1874 1875 1876

fast_host returns [const char* ret]
scope { const char* current; }
@init {$fast_host::current=$ret=NULL;}
            :  (fast_hostname 	{$fast_host::current=(const char *)$fast_hostname.text->chars;}
1877
                    /*| ipv4address 	{$fast_host::current=(const char *)$ipv4address.text->chars;} because cannot discriminate quickly betwenn 192.168.0.1 and 192.168.0.com*/
jehan's avatar
jehan committed
1878 1879
                    | ipv6reference  {$fast_host::current=(const char *)$ipv6reference.ret;})  {$ret=$fast_host::current;}; 
 	
1880
fast_hostname:  alphanum ( alphanum | DASH | DOT )*;
jehan's avatar
jehan committed
1881

jehan's avatar
jehan committed
1882 1883 1884 1885 1886 1887 1888 1889
//*************************common tokens*******************************/

user_unreserved :  AND | EQUAL | PLUS | DOLLARD | COMMA | SEMI | QMARK | SLASH;
host returns [const char* ret]
scope { const char* current; }
@init {$host::current=$ret=NULL;}
            :   (hostname {$host::current=(const char *)$hostname.text->chars;}
                    | ipv4address {$host::current=(const char *)$ipv4address.text->chars;}
jehan's avatar
jehan committed
1890
                    | ipv6reference {$host::current=(const char *)$ipv6reference.ret;}) {$ret=$host::current;};
jehan's avatar
jehan committed
1891 1892 1893
hostname        :   ( domainlabel DOT )* (toplabel)=>toplabel DOT? ;
  
domainlabel     :   alphanum | (alphanum ( alphanum | DASH )* alphanum) ;
1894
toplabel        :   alphanum (  DASH?  alphanum)+;
jehan's avatar
jehan committed
1895 1896

ipv4address    :  three_digit DOT three_digit DOT three_digit DOT three_digit ;
jehan's avatar
jehan committed
1897
ipv6reference returns [const char* ret=NULL]  :  LSBRAQUET ipv6address RSBRAQUET {$ret=(const char *)$ipv6address.text->chars;};
jehan's avatar
jehan committed
1898 1899 1900 1901 1902
ipv6address    :  hexpart ( COLON ipv4address )? ;
hexpart        :  hexseq | hexseq COLON COLON ( hexseq )? | COLON COLON ( hexseq )?;
hexseq         :  hex4 ( COLON hex4)*;
hex4           :  hexdigit+;/* hexdigit hexdigit hexdigit ;*/

1903 1904 1905
port  returns [int ret]
@init {$ret=-1;}
	:  DIGIT+ { $ret=atoi((const char *)$text->chars); };
jehan's avatar
jehan committed
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916

escaped     :  PERCENT hexdigit hexdigit;
ttl : three_digit;
three_digit: (DIGIT) => DIGIT 
            | 
            (DIGIT DIGIT) => (DIGIT DIGIT) 
            | 
            (DIGIT DIGIT DIGIT) =>(DIGIT DIGIT DIGIT) ; 
token       
  :   (alphanum | mark | PERCENT | PLUS | BQUOTE  )+;

1917 1918 1919
reserved_for_from_to_contact_addr_spec: 
	COLON | AT | AND | EQUAL | PLUS | DOLLARD  | SLASH;
	
jehan's avatar
jehan committed
1920
reserved    
1921
  :   SEMI | COMMA  | QMARK  | reserved_for_from_to_contact_addr_spec ;
jehan's avatar
jehan committed
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
                     
unreserved :    alphanum |mark;  
alphanum :     alpha | DIGIT ;                      
hexdigit 
  : HEX_CHAR|DIGIT; 
alpha : HEX_CHAR | COMMON_CHAR; 
 
word        
  :   (alphanum | mark   | PERCENT 
                      | PLUS | BQUOTE |
                     LAQUOT | RAQUOT |
                     COLON | BSLASH | DQUOTE | SLASH | LSBRAQUET | RSBRAQUET | QMARK | LBRACE | RBRACE )+;

mark  :          DASH | USCORE | DOT | EMARK | TILDE | STAR | SQUOTE | LPAREN | RPAREN ; 
sp_tab_colon 
	:( SP | HTAB )* COLON ;	
hcolon  : sp_tab_colon lws? //SWS;
         ;//|( SP | HTAB )* COLON LWS+;
ldquot  :  lws? DQUOTE ;
rdquot : DQUOTE lws?;
semi: lws? SEMI lws?;
comma : lws? COMMA lws?;
sp_laquot
  : lws? LAQUOT ;
raquot_sp
  : RAQUOT lws?;
equal:
   lws? EQUAL lws?;
slash : lws? SLASH lws?;
lws : (SP* CRLF SP+) | SP+ ; //linear whitespace
//*************************************common tokens*****************/	

COMMON_CHAR
  : 'g'..'z' | 'G'..'Z' ; 

HEX_CHAR: 'a'..'f' |'A'..'F';
DIGIT : '0'..'9' ;
AT: '@';
AND: '&';
DOLLARD: '$';
QMARK: '?';
EMARK: '!';
DASH: '-';
CRLF  : '\r\n' { USER1 = (int)((char*)ctx->pLexer->input->currentLine - (char*)ctx->pLexer->input->data); /*GETCHARINDEX()*/;};
HTAB  : '	';
OR : '|';
PERCENT: '%';
DQUOTE  : '"';
SQUOTE  : '\'';
BQUOTE: '`';
BSLASH: '\\';
LBRACE: '{';
RBRACE: '}';
USCORE: '_';
TILDE: '~';
DOT: '.';


PLUS: '+';
COLON
  : ':'
  ;
SEMI
  : ';'
  ;
COMMA
  : ','
  ;
LAQUOT
  : '<'
  ;
RAQUOT
  : '>'
  ;

RPAREN
  : ')'
  ;

LPAREN
  : '('
  ;
RSBRAQUET
  : ']'
  ;

LSBRAQUET
  : '['
  ;
  
EQUAL
  : '='
  ;

SLASH
  : '/'
  ;

STAR
  : '*'
  ; 
SP
  : ' '
  ;
OCTET : . ; 


jehan's avatar
jehan committed
2029