Commit 132430e2 authored by jehan's avatar jehan

initial release with basic sip uri

parents
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>parser-antlr</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.dltk.core.scriptbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
<dictionary>
<key>?children?</key>
<value>?name?=outputEntries\|?children?=?name?=entry\\\\\\\|\\\|?name?=entry\\\\\\\|\\\|\||</value>
</dictionary>
<dictionary>
<key>?name?</key>
<value></value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.append_environment</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.autoBuildTarget</key>
<value>all</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildArguments</key>
<value></value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildCommand</key>
<value>make</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildLocation</key>
<value>${workspace_loc:/parser-antlr/Debug}</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.cleanBuildTarget</key>
<value>clean</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.contents</key>
<value>org.eclipse.cdt.make.core.activeConfigSettings</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableAutoBuild</key>
<value>false</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableCleanBuild</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableFullBuild</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.fullBuildTarget</key>
<value>all</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.stopOnError</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>
<value>true</value>
</dictionary>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
<nature>org.deved.antlride.core.nature</nature>
</natures>
</projectDescription>
//============================================================================
// Name : parser-antlr.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include "sip_uri.h"
#include <stdio.h>
int main (int argc, char *argv[]) {
sip_uri* L_uri = sip_uri_parse("sip:toto@titi.com:5060;transport=tcp");
printf(sip_uri_to_string(L_uri));
return 0;
}
/*
* SipUri.h
*
* Created on: 18 sept. 2010
* Author: jehanmonnier
*/
#ifndef SIPURI_H_
#define SIPURI_H_
typedef struct _sip_uri sip_uri;
sip_uri* sip_uri_new();
void sip_uri_delete(sip_uri* uri);
sip_uri* sip_uri_parse (const char* uri) ;
/**
* Returns the value of the named header, or null if it is not set.
*
*/
const char* sip_uri_get_header(sip_uri* uri,const char* name);
/**
* Returns an Iterator over the const char*names of all headers present in this SipURI.
*
*/
/*list<const char*>::iterator sip_uri_get_header_names(SipUri* uri) ;*/
/**
* Returns the host part of this SipURI.
*
*/
const char* sip_uri_get_host(sip_uri* uri) ;
/**
* Returns the value of the maddr parameter, or null if this is not set.
*
*/
const char* sip_uri_get_maddr_param(sip_uri* uri) ;
/**
* Returns the value of the method parameter, or null if this is not set.
*
*/
const char* sip_uri_get_method_param(sip_uri* uri) ;
/**
* Returns the port part of this SipURI.
*
*/
unsigned short sip_uri_get_port(sip_uri* uri) ;
/**
* Returns the value of the "transport" parameter, or null if this is not set.
*
*/
const char* sip_uri_get_transport_param(sip_uri* uri) ;
/**
* Returns the value of the "ttl" parameter, or -1 if this is not set.
*
*/
int sip_uri_get_ttl_param(sip_uri* uri) ;
/**
* Returns the user part of this SipURI.
*
*/
const char* sip_uri_get_user(sip_uri* uri) ;
/**
* Returns the value of the userParam, or null if this is not set.
*
*/
const char* sip_uri_get_user_param(sip_uri* uri) ;
/**
* Gets user password of SipURI, or null if it is not set.
*
*/
const char* sip_uri_get_user_password(sip_uri* uri) ;
/**
* Returns whether the the lr parameter is set.
*
*/
unsigned int sip_uri_has_lr_param(sip_uri* uri) ;
/**
*
* Returns true if this SipURI is secure i.e. if this SipURI represents a sips URI.
*
*/
unsigned int sip_uri_is_secure(sip_uri* uri) ;
/**
* Removes the port part of this SipURI.
*
*/
void sip_uri_remove_port(sip_uri* uri) ;
/**
* Sets the value of the specified header fields to be included in a request constructed from the URI.
*
*/
void sip_uri_set_header(sip_uri* uri,const char*name, const char*value) ;
/**
* Set the host part of this SipURI to the newly supplied host parameter.
*
*/
void sip_uri_set_host(sip_uri* uri,const char*host) ;
/**
* Sets the value of the lr parameter of this SipURI.
*
*/
void sip_uri_set_lr_param(sip_uri* uri) ;
/**
* Sets the value of the maddr parameter of this SipURI.
*
*/
void sip_uri_set_maddr_param(sip_uri* uri,const char*mAddr) ;
/**
* Sets the value of the method parameter.
*
*/
void sip_uri_set_method_param(sip_uri* uri,const char*method) ;
/**
* Set the port part of this SipURI to the newly supplied port parameter.
*
*/
void sip_uri_set_port(sip_uri* uri,unsigned short port) ;
/**
* Sets the scheme of this URI to sip or sips depending on whether the argument is true or false.
*
*/
void sip_uri_set_secure(sip_uri* uri,unsigned secure) ;
/**
* Sets the value of the "transport" parameter.
*
*/
void sip_uri_set_transport_param(sip_uri* uri,const char*transport) ;
/**
* Sets the value of the ttl parameter.
*
*/
void sip_uri_set_ttl_param(sip_uri* uri,int ttl) ;
/**
* Sets the user of SipURI.
*
*/
void sip_uri_set_user(sip_uri* uri,const char*user) ;
/**
* Sets the value of the user parameter.
*
*/
void sip_uri_set_user_param(sip_uri* uri,const char*userParam) ;
/**
* Sets the user password associated with the user of SipURI.
*
*/
void sip_uri_set_user_password(sip_uri* uri,const char*userPassword) ;
/**
* This method returns the URI as a string.
*
*/
char* sip_uri_to_string(sip_uri* uri) ;
#endif /*SIPURI_H_*/
/*
* SipUri.cpp
*
* Created on: 18 sept. 2010
* Author: jehanmonnier
*/
#include "sip_uri.h"
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "sipuriParser.h"
#include "sipuriLexer.h"
#define GET_SET_STRING(object_type,attribute) \
const char* object_type##_get_##attribute (object_type* obj) {\
return obj->attribute;\
}\
void object_type##_set_##attribute (object_type* obj,const char* value) {\
if (obj->attribute != NULL) free((void*)obj->attribute);\
obj->attribute=malloc(strlen(value)+1);\
strcpy((char*)(obj->attribute),value);\
}
#define SIP_URI_GET_SET_STRING(attribute) GET_SET_STRING(sip_uri,attribute)
#define GET_SET_UINT(object_type,attribute) \
unsigned int* object_type##_get_##attribute (object_type* obj) {\
return obj->attribute;\
}\
void object_type##_set_##attribute (object_type* obj,unsigned int value) {\
obj->attribute=value;\
}
#define SIP_URI_GET_SET_UINT(attribute) GET_SET_UINT(sip_uri,attribute)
struct _sip_uri {
const char* user;
const char* host;
const char* transport_param;
};
sip_uri* sip_uri_parse (const char* uri) {
pANTLR3_INPUT_STREAM input;
psipuriLexer lex;
pANTLR3_COMMON_TOKEN_STREAM tokens;
psipuriParser parser;
input = antlr3NewAsciiStringCopyStream (
(pANTLR3_UINT8)uri,
(ANTLR3_UINT32)strlen(uri),
NULL);
lex = sipuriLexerNew (input);
tokens = antlr3CommonTokenStreamSourceNew (ANTLR3_SIZE_HINT, TOKENSOURCE(lex));
parser = sipuriParserNew (tokens);
sip_uri* l_parsed_uri = parser->an_sip_uri(parser);
// Must manually clean up
//
parser ->free(parser);
tokens ->free(tokens);
lex ->free(lex);
input ->close(input);
return l_parsed_uri;
}
sip_uri* sip_uri_new () {
sip_uri* lUri = (sip_uri*)malloc(sizeof(sip_uri));
memset(lUri,0,sizeof(sip_uri));
return lUri;
}
void sip_uri_delete(sip_uri* uri) {
free(uri);
}
static char * concat (const char *str, ...) {
va_list ap;
size_t allocated = 100;
char *result = (char *) malloc (allocated);
if (result != NULL)
{
char *newp;
char *wp;
const char* s;
va_start (ap, str);
wp = result;
for (s = str; s != NULL; s = va_arg (ap, const char *)) {
size_t len = strlen (s);
/* Resize the allocated memory if necessary. */
if (wp + len + 1 > result + allocated)
{
allocated = (allocated + len) * 2;
newp = (char *) realloc (result, allocated);
if (newp == NULL)
{
free (result);
return NULL;
}
wp = newp + (wp - result);
result = newp;
}
memcpy (wp, s, len);
wp +=len;
}
/* Terminate the result string. */
*wp++ = '\0';
/* Resize memory to the optimal size. */
newp = realloc (result, wp - result);
if (newp != NULL)
result = newp;
va_end (ap);
}
return result;
}
char* sip_uri_to_string(sip_uri* uri) {
return concat( "sip:"
,(uri->user?uri->user:"")
,(uri->user?"@":"")
,uri->host
,(uri->transport_param?";transport=":"")
,(uri->transport_param?uri->transport_param:"")
,NULL);
}
SIP_URI_GET_SET_STRING(user)
SIP_URI_GET_SET_STRING(host)
SIP_URI_GET_SET_STRING(transport_param)
grammar sipuri;
options { language = C;}
@header { #include "sip_uri.h"}
an_sip_uri returns [sip_uri* ret]
scope { sip_uri* current; }
@init { $an_sip_uri::current = sip_uri_new(); }
: 'sip:' userinfo? hostport uri_parameters {$ret = $an_sip_uri::current;};
userinfo : user ( ':' password )? '@' ;
user : ( unreserved | user_unreserved )+ {sip_uri_set_user($an_sip_uri::current,(const char *)$text->chars);};
user_unreserved : '&' | '=' | '+' | '$' | ',' | ';' | '?' | '/';
password : ( unreserved |'&' | '=' | '+' | '$' | ',' )*;
hostport : host ( ':' port )?;
host : hostname {sip_uri_set_host($an_sip_uri::current,(const char *)$text->chars);};
hostname : ( domainlabel '.' )* toplabel '.'? ;
domainlabel : alphanum | alphanum ( alphanum | '-' )* alphanum ;
toplabel : ALPHA | ALPHA ( alphanum | '-' )* alphanum;
port : DIGIT+ ;
uri_parameters
: ( ';' uri_parameter)*;
uri_parameter
: transport_param | user_param
| ttl_param | maddr_param | lr_param | other_param ;
transport_param
: 'transport=' transport_value;
transport_value: ('udp' | 'tcp' | 'sctp' | 'tls'| other_transport)
{sip_uri_set_transport_param($an_sip_uri::current,(const char *)$text->chars);};
other_transport
: token ;
user_param
: 'user=' ( 'phone' | 'ip' | other_user);
other_user
: token;
ttl_param
: 'ttl=' ttl;
maddr_param
: 'maddr=' host;
lr_param
: 'lr';
other_param : pname ( '=' pvalue )?;
pname
: paramchar+;
pvalue
: paramchar+;
paramchar
: param_unreserved | unreserved | escaped;
param_unreserved
: '[' | ']' | '/' | ':' | '&' | '+' | '$';
fragment ttl
: DIGIT DIGIT? DIGIT? ;
fragment escaped
: '%' HEXDIG HEXDIG;
fragment token
: (alphanum | MARK_LEX | '%' | '+' | '`' )+;
fragment unreserved : alphanum |MARK_LEX;
fragment alphanum : ALPHA | DIGIT ;
fragment MARK_LEX : '-' | '_' | '.' | '!' | '~' | '*' | '\'' ;
fragment HEXDIG
: 'a'..'f' |'A'..'F'|DIGIT;
ALPHA : ('a'..'z'|'A'..'Z');
DIGIT : '0'..'9' ;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment