Index: extensions/app_sip/CMakeLists.txt
===================================================================
--- extensions/app_sip/CMakeLists.txt	(revision 431)
+++ extensions/app_sip/CMakeLists.txt	(revision 433)
@@ -6,16 +6,16 @@
 
 # Parser files
-BISON_FILE(diamsip.y)
-FLEX_FILE(diamsip.l)
-SET_SOURCE_FILES_PROPERTIES(lex.diamsip.c diamsip.tab.c PROPERTIES COMPILE_FLAGS "-I ${CMAKE_CURRENT_SOURCE_DIR}")
+BISON_FILE(app_sip.y)
+FLEX_FILE(app_sip.l)
+SET_SOURCE_FILES_PROPERTIES(lex.app_sip.c app_sip.tab.c PROPERTIES COMPILE_FLAGS "-I ${CMAKE_CURRENT_SOURCE_DIR}")
 
 # List of source files
 SET( DIAM_SIP_SRC
-	lex.diamsip.c
-	diamsip.tab.c
-	diamsip.tab.h
-	diamsip.c
-	diamsip.h
-	libdiamsip.c
+	lex.app_sip.c
+	app_sip.tab.c
+	app_sip.tab.h
+	app_sip.c
+	app_sip.h
+	libapp_sip.c
 	md5.c
 	multimediaauth.c
Index: extensions/app_sip/app_sip.c
===================================================================
--- extensions/app_sip/app_sip.c	(revision 433)
+++ extensions/app_sip/app_sip.c	(revision 433)
@@ -0,0 +1,252 @@
+/*********************************************************************************************************
+* Software License Agreement (BSD License)                                                               *
+* Author: Alexandre Westfahl <awestfahl@freediameter.net>						 *
+*													 *
+* Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. 	 *		
+*													 *
+* All rights reserved.											 *
+* 													 *
+* Redistribution and use of this software in source and binary forms, with or without modification, are  *
+* permitted provided that the following conditions are met:						 *
+* 													 *
+* * Redistributions of source code must retain the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer.										 *
+*    													 *
+* * Redistributions in binary form must reproduce the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer in the documentation and/or other						 *
+*   materials provided with the distribution.								 *
+* 													 *
+* * Neither the name of the Teraoka Laboratory nor the 							 *
+*   names of its contributors may be used to endorse or 						 *
+*   promote products derived from this software without 						 *
+*   specific prior written permission of Teraoka Laboratory 						 *
+*   													 *
+* 													 *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
+* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
+*********************************************************************************************************/
+#include "app_sip.h"
+
+//Diameter-SIP server
+struct disp_hdl * app_sip_MAR_hdl=NULL;
+struct disp_hdl * app_sip_LIR_hdl=NULL;
+struct disp_hdl * app_sip_UAR_hdl=NULL;
+struct disp_hdl * app_sip_SAR_hdl=NULL;
+struct disp_hdl * app_sip_PPA_hdl=NULL;
+struct disp_hdl * app_sip_RTA_hdl=NULL;
+
+//Suscriber Locator
+struct disp_hdl * app_sip_SL_LIR_hdl=NULL;
+
+
+
+struct disp_hdl * app_sip_default_hdl=NULL;
+struct session_handler * ds_sess_hdl;
+
+//configuration stucture
+struct as_conf * as_conf=NULL;
+static struct as_conf app_sip_conf;
+
+//dictionary of SIP
+struct app_sip_dict sip_dict;
+
+int app_sip_default_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act)
+{
+	TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act);
+	
+	return 0;
+}
+
+void dump_config()
+{
+	TRACE_DEBUG(FULL,"***Configuration of Diameter-SIP extension***");
+	TRACE_DEBUG(FULL,"# mode: *%d*",as_conf->mode);
+	TRACE_DEBUG(FULL,"# datasource: *%d*",as_conf->datasource);
+	TRACE_DEBUG(FULL,"# mysql_login: *%s*",as_conf->mysql_login);
+	TRACE_DEBUG(FULL,"# mysql_password: *%s*",as_conf->mysql_password);
+	TRACE_DEBUG(FULL,"# mysql_database: *%s*",as_conf->mysql_database);
+	TRACE_DEBUG(FULL,"# mysql_server: *%s*",as_conf->mysql_server);
+	TRACE_DEBUG(FULL,"# mysql_port: *%d*",as_conf->mysql_port);
+	TRACE_DEBUG(FULL,"# mysql_port: *%d*",as_conf->mysql_prefix);
+	TRACE_DEBUG(FULL,"***End of Diameter-SIP configuration extension***");
+}
+
+static int as_conf_init(void)
+{
+	as_conf=&app_sip_conf;
+	//memset(app_sip_conf, 0, sizeof(struct as_conf));
+	
+
+	return 0;
+}
+
+/* entry point */
+int as_entry(char * conffile)
+{
+	TRACE_ENTRY("%p", conffile);
+	
+	struct dict_object * app=NULL;
+	struct disp_when data;
+	pthread_t rtr_thread, ppr_thread;
+	
+	/* Initialize configuration */
+	CHECK_FCT( as_conf_init() );
+	
+	
+	//We parse the configuration file
+	if (conffile != NULL) {
+		CHECK_FCT( as_conf_handle(conffile) );
+	}
+	else
+	{
+		TRACE_DEBUG(INFO, "We need a configuration file for Diameter-SIP extension. See doc/ for an example.");
+	}
+	
+	//TODO: replace by configuration file!!
+	strcpy(as_conf->mysql_prefix,"as_");
+	
+	//We can dump the configuration extracted from app_sip.conf
+	//dump_config();
+	
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Diameter Session Initiation Protocol (SIP) Application", &app, ENOENT) );
+	CHECK_FCT( fd_disp_app_support ( app, NULL, 1, 0 ) );
+	
+	
+	
+	//We set usefull AVPs 
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Auth-Session-State", &sip_dict.Auth_Session_State, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Auth-Application-Id", &sip_dict.Auth_Application_Id, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Destination-Host", &sip_dict.Destination_Host, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Destination-Realm", &sip_dict.Destination_Realm, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Session-Id", &sip_dict.Session_Id, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Redirect-Host", &sip_dict.Redirect_Host, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Redirect-Host-Usage", &sip_dict.Redirect_Host_Usage, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Auth-Data-Item", &sip_dict.SIP_Auth_Data_Item, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Authorization", &sip_dict.SIP_Authorization, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Authenticate", &sip_dict.SIP_Authenticate, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Number-Auth-Items", &sip_dict.SIP_Number_Auth_Items, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Authentication-Scheme", &sip_dict.SIP_Authentication_Scheme, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Authentication-Info", &sip_dict.SIP_Authentication_Info, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Server-URI", &sip_dict.SIP_Server_URI, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Server-Capabilities", &sip_dict.SIP_Server_Capabilities, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Mandatory-Capability", &sip_dict.SIP_Mandatory_Capability, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Optional-Capability", &sip_dict.SIP_Optional_Capability, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Method", &sip_dict.SIP_Method, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-AOR", &sip_dict.SIP_AOR, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Deregistration-Reason", &sip_dict.SIP_Deregistration_Reason, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Reason-Code", &sip_dict.SIP_Reason_Code, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Reason-Info", &sip_dict.SIP_Reason_Info, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Accounting-Information", &sip_dict.SIP_Accounting_Information, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Accounting-Server-URI", &sip_dict.SIP_Accounting_Server_URI, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Credit-Control-Server-URI", &sip_dict.SIP_Credit_Control_Server_URI, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Server-Assignment-Type", &sip_dict.SIP_Server_Assignment_Type, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Item-Number", &sip_dict.SIP_Item_Number, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Authorization-Type", &sip_dict.SIP_User_Authorization_Type, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Supported-User-Data-Type", &sip_dict.SIP_Supported_User_Data_Type, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Data", &sip_dict.SIP_User_Data, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Data-Type", &sip_dict.SIP_User_Data_Type, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Data-Contents", &sip_dict.SIP_User_Data_Contents, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Data-Already-Available", &sip_dict.SIP_User_Data_Already_Available, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Visited-Network-Id", &sip_dict.SIP_Visited_Network_Id, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Realm", &sip_dict.Digest_Realm, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-URI", &sip_dict.Digest_URI, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Nonce", &sip_dict.Digest_Nonce, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-CNonce", &sip_dict.Digest_CNonce, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Method", &sip_dict.Digest_Method, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Nonce-Count", &sip_dict.Digest_Nonce_Count, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Realm", &sip_dict.Digest_Realm, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Response", &sip_dict.Digest_Response, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Response-Auth", &sip_dict.Digest_Response_Auth, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Username", &sip_dict.Digest_Username, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Algorithm", &sip_dict.Digest_Algorithm, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-QoP", &sip_dict.Digest_QOP, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "User-Name", &sip_dict.User_Name, ENOENT) );
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-HA1", &sip_dict.Digest_HA1, ENOENT) );
+	
+	
+	//Register Application
+	memset(&data, 0, sizeof(data));
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Diameter Session Initiation Protocol (SIP) Application", &data.app, ENOENT) );
+	
+	if(as_conf->mode==1)
+	{
+	  //**Command Codes
+	  //MAR
+	  CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Multimedia-Auth-Request", &data.command, ENOENT) );
+	  CHECK_FCT( fd_disp_register( app_sip_MAR_cb, DISP_HOW_CC, &data, &app_sip_MAR_hdl ) );
+	  //RTA
+	  CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Registration-Termination-Answer", &data.command, ENOENT) );
+	  CHECK_FCT( fd_disp_register( app_sip_RTA_cb, DISP_HOW_CC, &data, &app_sip_RTA_hdl ) );
+	  //PPA
+	  CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Push-Profile-Answer", &data.command, ENOENT) );
+	  CHECK_FCT( fd_disp_register( app_sip_PPA_cb, DISP_HOW_CC, &data, &app_sip_PPA_hdl ) );
+	  //LIR
+	  CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Location-Info-Request", &data.command, ENOENT) );
+	  CHECK_FCT( fd_disp_register( app_sip_LIR_cb, DISP_HOW_CC, &data, &app_sip_LIR_hdl ) );
+	  //UAR
+	  CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "User-Authorization-Request", &data.command, ENOENT) );
+	  CHECK_FCT( fd_disp_register( app_sip_UAR_cb, DISP_HOW_CC, &data, &app_sip_UAR_hdl ) );
+	  //SAR
+	  CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Server-Assignment-Request", &data.command, ENOENT) );
+	  CHECK_FCT( fd_disp_register( app_sip_SAR_cb, DISP_HOW_CC, &data, &app_sip_SAR_hdl ) );
+	}
+	if(as_conf->mode==2)
+	{
+	  //LIR
+	  CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Location-Info-Request", &data.command, ENOENT) );
+	  CHECK_FCT( fd_disp_register( app_sip_SL_LIR_cb, DISP_HOW_CC, &data, &app_sip_SL_LIR_hdl ) );
+	}
+	//Callback for unexpected messages
+	CHECK_FCT( fd_disp_register( app_sip_default_cb, DISP_HOW_APPID, &data, &app_sip_default_hdl ) );
+	
+	
+	//We start database connection
+	if(start_mysql_connection())
+		return EINVAL;
+	
+	CHECK_FCT(fd_sess_handler_create(&ds_sess_hdl, free));
+	
+	//Creation of thread for Registration Termination	
+	if(pthread_create(&rtr_thread, NULL,rtr_socket, NULL))
+	{
+		TRACE_DEBUG(INFO,"Creation of thread failed, abort!");
+		return EINVAL;
+	}
+	//Creation of thread for Push Profile	
+	if(pthread_create(&ppr_thread, NULL,ppr_socket, NULL))
+	{
+		TRACE_DEBUG(INFO,"Creation of thread failed, abort!");
+		return EINVAL;
+	}
+	
+	
+	return 0;
+}
+
+//Cleanup callback
+void fd_ext_fini(void)
+{
+	//TODO:unregister other callbacks
+	
+	(void) fd_disp_unregister(&app_sip_MAR_hdl);
+	CHECK_FCT_DO( fd_sess_handler_destroy(&ds_sess_hdl),return);
+	
+	
+	//We close database connection
+	close_mysql_connection();
+	
+
+	
+	TRACE_ENTRY();
+	return ;
+}
+
+EXTENSION_ENTRY("app_sip", as_entry);
Index: extensions/app_sip/app_sip.h
===================================================================
--- extensions/app_sip/app_sip.h	(revision 433)
+++ extensions/app_sip/app_sip.h	(revision 433)
@@ -0,0 +1,268 @@
+/*********************************************************************************************************
+* Software License Agreement (BSD License)                                                               *
+* Author: Alexandre Westfahl <awestfahl@freesipserver.net>						 *
+*													 *
+* Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. 	 *		
+*													 *
+* All rights reserved.											 *
+* 													 *
+* Redistribution and use of this software in source and binary forms, with or without modification, are  *
+* permitted provided that the following conditions are met:						 *
+* 													 *
+* * Redistributions of source code must retain the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer.										 *
+*    													 *
+* * Redistributions in binary form must reproduce the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer in the documentation and/or other						 *
+*   materials provided with the distribution.								 *
+* 													 *
+* * Neither the name of the Teraoka Laboratory nor the 							 *
+*   names of its contributors may be used to endorse or 						 *
+*   promote products derived from this software without 						 *
+*   specific prior written permission of Teraoka Laboratory 						 *
+*   													 *
+* 													 *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
+* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
+*********************************************************************************************************/
+#include <freeDiameter/extension.h>
+#include <sys/time.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <gcrypt.h>
+#include <string.h>
+#include <mysql.h>
+#include "md5.h"
+
+
+#define NONCE_SIZE 16
+#define DIGEST_LEN 16
+
+
+/* Mode for the extension */
+#define MODE_DSSERVER	0x1
+#define	MODE_SL	0x2
+
+//Redirect_Host_Usage
+#define ALL_USER	6
+
+/* The module configuration */
+struct as_conf {
+	int		mode;		/* default MODE_DSSERVER | MODE_SL */
+	enum {ASMYSQL} datasource; 
+	char * mysql_login;
+	char * mysql_password;
+	char * mysql_database;
+	char * mysql_server;
+	char mysql_prefix[10]; //default: as_
+	uint16_t  mysql_port; //if 0, default port will be used
+	
+};
+extern struct as_conf * as_conf;
+
+/* Parse the configuration file */
+int as_conf_handle(char * conffile);
+
+
+extern MYSQL *conn;
+
+
+
+void calc_md5(char *buffer, char * data);
+void clear_digest(uint8_t * digest, char * readable_digest, int digestlength);
+struct avp_hdr * walk_digest(struct avp *avp, int avp_code);
+
+
+//MySQL part
+int start_mysql_connection();
+void request_mysql(char *query);
+void close_mysql_connection();
+int get_sipserver_uri(const unsigned char *sip_aor, const size_t sipaorlen, unsigned char ** sipserver_uri, size_t *sipserverurilen);
+int exist_username(const unsigned char *sip_aor, const size_t sipaorlen);
+int get_sipserver_cap(const unsigned char *sip_aor, const size_t sipaorlen, struct avp **capabilities);
+int get_password(const unsigned char *username, const size_t usernamelen, char *password);
+int check_sipaor(const unsigned char  *username, const size_t usernamelen, const char * sip_aor,const size_t sipaorlen);
+int add_user_datatype(const unsigned char  *sip_aor, const size_t sipaorlen,struct msg *message);
+int set_pending_flag(const unsigned char  *username, const size_t usernamelen);
+int clear_pending_flag(const unsigned char  *username, const size_t usernamelen);
+int set_real_sipserver_uri(const unsigned char  *username, const size_t usernamelen, const unsigned char *sipserver_uri,const size_t sipserverurilen);
+int set_sipserver_uri(const unsigned char  *username, const size_t usernamelen, const unsigned char *sipserver_uri,const size_t sipserverurilen);
+//int get_sipserver_uri(const unsigned char *sip_aor, const size_t sipaorlen, unsigned char ** sipserver_uri, size_t *sipserverurilen);
+int allow_roaming(const unsigned char  *username, const size_t usernamelen, const char * network,const size_t networklen);
+int get_diameter_uri(const unsigned char *sip_aor, const size_t sipaorlen, char ** diameter_uri, size_t *diameterurilen);
+//count functions
+int count_avp(struct msg * message, int code, int vendor);
+
+
+void DigestCalcHA1(char * pszAlg,char * pszUserName,char * pszRealm,char * pszPassword,char * pszNonce,char * pszCNonce,HASHHEX SessionKey);
+void DigestCalcResponse(HASHHEX HA1,char * pszNonce,char * pszNonceCount,char * pszCNonce,char * pszQop,char * pszMethod,char * pszDigestUri,HASHHEX HEntity,HASHHEX Response);
+void DigestCalcResponseAuth(HASHHEX HA1,char * pszNonce,char * pszNonceCount,char * pszCNonce,char * pszQop,char * pszMethod,char * pszDigestUri,HASHHEX HEntity,HASHHEX Response);
+
+int fd_avp_search_avp ( struct avp * groupedavp, struct dict_object * what, struct avp ** avp );
+
+//thread procedure
+void *rtr_socket(void *);
+void *ppr_socket(void *);
+
+struct rtrsipaor
+{
+	char username[200];
+	char sip_aor1[200];
+	char sip_aor2[200];
+	char sip_aor3[200];
+	char strreason[200];
+	char desthost[200];
+	int reason;
+};
+struct pprsipaor
+{
+	char username[200];
+	char label1[200];
+	char value1[200];
+	char label2[200];
+	char value2[200];
+	char desthost[200];  
+};
+int app_sip_RTR_cb(struct rtrsipaor structure);
+int app_sip_PPR_cb(struct pprsipaor structure);
+#define PORT 666 //TODO:put in conf file
+
+int ds_entry();
+void fd_ext_fini(void);
+int app_sip_default_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act);
+int app_sip_MAR_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act);
+int app_sip_RTA_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act);
+int app_sip_PPA_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act);
+int app_sip_LIR_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act);
+int app_sip_UAR_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act);
+int app_sip_SAR_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act);
+
+//Suscriber Locator
+int app_sip_SL_LIR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act);
+//int app_sip_SL_SAR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act);
+
+#define SQL_GETPASSWORD "SELECT `password` FROM ds_users WHERE `username` ='%s'"
+#define SQL_GETPASSWORD_LEN 52
+
+//username by SIP-AOR
+#define SQL_GETUSERNAME  "SELECT `username` FROM ds_users, ds_sip_aor WHERE `sip_aor` ='%s' AND `ds_sip_aor`.`id_user` = `ds_users`.`id_user`"
+#define SQL_GETUSERNAME_LEN 113
+
+//sip server uri by username
+#define SQL_GETSIPURI  "SELECT `sip_server_uri` FROM ds_users WHERE `username` ='%s'"
+#define SQL_GETSIPURI_LEN 60
+
+//sip server uri by SIP-AOR
+#define SQL_GETSIPSERURI  "SELECT `ds_sip_aor`.`sip_server_uri` FROM ds_users, ds_sip_aor WHERE `sip_aor` ='%s' AND `ds_sip_aor`.`id_user` = `ds_users`.`id_user`"
+#define SQL_GETSIPSERURI_LEN 132
+
+//sip capabilities for a SIP-AOR
+#define SQL_GETSIPSERCAP  "SELECT `compulsory`,`id_service` FROM ds_user_services, ds_sip_aor WHERE `sip_aor` ='%s' AND `ds_sip_aor`.`id_user` = `ds_user_services`.`id_user`"
+#define SQL_GETSIPSERCAP_LEN 144
+
+//user data for a user data supported
+#define SQL_GETSIPDATA  "SELECT `label_type`,`data` FROM ds_sip_aor, ds_user_data, ds_data_types WHERE `sip_aor` ='%s' AND `ds_sip_aor`.`id_sip_aor` = `ds_user_data`.`id_sip_aor` AND `ds_data_types`.`id_data_type`=`ds_user_data`.`id_data_type`"
+#define SQL_GETSIPDATA_LEN 216
+
+#define SQL_GETDIAMURI "SELECT `sipserver_uri` FROM ds_sip_aor_map WHERE `sip_aor` ='%s'"
+#define SQL_GETDIAMURI_LEN 61
+
+//networks for this user
+#define SQL_GETUSERNET "SELECT `label_network` FROM ds_users, ds_user_networks, ds_networks WHERE `ds_users`.`username` ='%s' AND `ds_user_networks`.`id_user` = `ds_users`.`id_user` AND `ds_user_networks`.`id_network` = `ds_networks`.`id_network`"
+#define SQL_GETUSERNET_LEN 220
+
+#define SQL_SETSIPURI "UPDATE ds_users SET `temp_sip_server_uri`='%s' WHERE `username` ='%s'"
+#define SQL_SETSIPURI_LEN 65
+
+//TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO 
+#define SQL_RMSIPURI "UPDATE ds_users SET `temp_sip_server_uri`='', `sip_server_uri`='' WHERE `id_user` ='%s'"
+#define SQL_RMSIPURI_LEN 65
+//TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO 
+
+#define SQL_SETREALSIPURI "UPDATE ds_users SET `sip_server_uri`='%s' WHERE `username` ='%s'"
+#define SQL_SETREALSIPURI_LEN 65
+
+#define SQL_SETFLAG "UPDATE ds_users SET `authentication_pending`=1 WHERE `username` ='%s'"
+#define SQL_SETFLAG_LEN 67
+
+#define SQL_CLEARFLAG "UPDATE ds_users SET `authentication_pending`=0, `registrated`=1 WHERE `username` ='%s'"
+#define SQL_CLEARFLAG_LEN 84
+
+#define SQL_GETSIPAOR "SELECT `sip_aor` FROM `ds_sip_aor`, `ds_users` WHERE `ds_sip_aor`.`id_user` = `ds_users`.`id_user` AND `ds_users`.`username` = '%s'"
+#define SQL_GETSIPAOR_LEN 131
+
+//#define SQL_CLEARFLAG "UPDATE ds_users SET `authentication_pending`=0 WHERE `username` ='%s'"
+//#define SQL_CLEARFLAG_LEN 67
+
+extern struct session_handler * ds_sess_hdl;
+
+//AVP code
+#define CODE_SIP_USER_DATA_TYPE	388
+#define CODE_SIP_AOR	122
+
+struct ds_nonce
+{
+	char *nonce;
+};
+
+//Storage for some usefull AVPs
+struct app_sip_dict{
+	struct dict_object * Auth_Session_State;
+	struct dict_object * Auth_Application_Id;
+	struct dict_object * Destination_Host;
+	struct dict_object * Destination_Realm;
+	struct dict_object * User_Name;
+	struct dict_object * Session_Id;
+	struct dict_object * Redirect_Host;
+	struct dict_object * Redirect_Host_Usage;
+	struct dict_object * SIP_Auth_Data_Item;
+	struct dict_object * SIP_Accounting_Information;
+	struct dict_object * SIP_Accounting_Server_URI;
+	struct dict_object * SIP_Credit_Control_Server_URI;
+	struct dict_object * SIP_Server_Assignment_Type;
+	struct dict_object * SIP_Item_Number;
+	struct dict_object * SIP_User_Authorization_Type;
+	struct dict_object * SIP_Supported_User_Data_Type;
+	struct dict_object * SIP_User_Data;
+	struct dict_object * SIP_User_Data_Type;
+	struct dict_object * SIP_User_Data_Contents;
+	struct dict_object * SIP_User_Data_Already_Available;
+	struct dict_object * SIP_Visited_Network_Id;
+	struct dict_object * SIP_Authorization;
+	struct dict_object * SIP_Authenticate;
+	struct dict_object * SIP_Number_Auth_Items;	
+	struct dict_object * SIP_Authentication_Scheme;
+	struct dict_object * SIP_Authentication_Info;	
+	struct dict_object * SIP_Server_URI;
+	struct dict_object * SIP_Server_Capabilities;
+	struct dict_object * SIP_Mandatory_Capability;
+	struct dict_object * SIP_Optional_Capability;
+	struct dict_object * SIP_Method;
+	struct dict_object * SIP_AOR;
+	struct dict_object * SIP_Deregistration_Reason;
+	struct dict_object * SIP_Reason_Code;
+	struct dict_object * SIP_Reason_Info;
+	struct dict_object * Digest_URI;		
+	struct dict_object * Digest_Nonce;
+	struct dict_object * Digest_Nonce_Count;
+	struct dict_object * Digest_CNonce;		
+	struct dict_object * Digest_Realm;		
+	struct dict_object * Digest_Response;	
+	struct dict_object * Digest_Response_Auth;	
+	struct dict_object * Digest_Username;	
+	struct dict_object * Digest_Method;
+	struct dict_object * Digest_QOP;	
+	struct dict_object * Digest_Algorithm;
+	struct dict_object * Digest_HA1;
+};
+
+extern  struct app_sip_dict  sip_dict;
Index: extensions/app_sip/app_sip.l
===================================================================
--- extensions/app_sip/app_sip.l	(revision 433)
+++ extensions/app_sip/app_sip.l	(revision 433)
@@ -0,0 +1,157 @@
+/*********************************************************************************************************
+* Software License Agreement (BSD License)                                                               *
+* Author: Alexandre Westfahl <awestfahl@freediameter.net>						 *
+*													 *
+* Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. 	 *		
+*													 *
+* All rights reserved.											 *
+* Based on ta_conf.l (Sebastien Decugis <sdecugis@nict.go.jp>)					 *
+* 													 *
+* Redistribution and use of this software in source and binary forms, with or without modification, are  *
+* permitted provided that the following conditions are met:						 *
+* 													 *
+* * Redistributions of source code must retain the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer.										 *
+*    													 *
+* * Redistributions in binary form must reproduce the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer in the documentation and/or other						 *
+*   materials provided with the distribution.								 *
+* 													 *
+* * Neither the name of the Teraoka Laboratory nor the 							 *
+*   names of its contributors may be used to endorse or 						 *
+*   promote products derived from this software without 						 *
+*   specific prior written permission of Teraoka Laboratory 						 *
+*   													 *
+* 													 *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
+* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
+*********************************************************************************************************/
+
+
+
+%{
+#include "app_sip.h"
+/* Include yacc tokens definitions */
+#include "app_sip.tab.h"
+
+/* Update the column information */
+#define YY_USER_ACTION { 						\
+	yylloc->first_column = yylloc->last_column + 1; 		\
+	yylloc->last_column = yylloc->first_column + yyleng - 1;	\
+}
+
+/* Avoid warning with newer flex */
+#define YY_NO_INPUT
+
+%}
+
+%option bison-bridge bison-locations
+%option noyywrap
+%option nounput
+
+%%
+
+	/* Update the line count */
+\n			{
+				yylloc->first_line++; 
+				yylloc->last_line++; 
+				yylloc->last_column=0; 
+			}
+	 
+	/* Eat all spaces but not new lines */
+([[:space:]]{-}[\n])+	;
+	/* Eat all comments */
+#.*$			;
+
+	/* Recognize any integer */
+[-]?[[:digit:]]+		{
+				/* Convert this to an integer value */
+				int ret=0;
+				ret = sscanf(yytext, "%i", &yylval->integer);
+				if (ret != 1) {
+					/* No matching: an error occurred */
+					fd_log_debug("Unable to convert the value '%s' to a valid number: %s\n", yytext, strerror(errno));
+					return LEX_ERROR; /* trig an error in yacc parser */
+					/* Maybe we could REJECT instead of failing here? */
+				}
+				return INTEGER;
+			}
+
+	/* Recognize quoted strings -- we do not support escaped \" in the string currently. */
+\"[^\"]+\"		{
+				/* Match a quoted string. Let's be very permissive. */
+				yylval->string = strdup(yytext+1);
+				if (!yylval->string) {
+					fd_log_debug("Unable to copy the string '%s': %s\n", yytext, strerror(errno));
+					TRACE_DEBUG(INFO, "strdup failed");
+					return LEX_ERROR; /* trig an error in yacc parser */
+				}
+				yylval->string[strlen(yytext) - 2] = '\0';
+				return QSTRING;
+			}
+
+
+
+	/* Recognize the tokens */	
+(?i:"mysql_login")	{
+				return ASMYSQL_LOGIN;
+			}
+
+(?i:"mysql_password")		{
+				return ASMYSQL_PASSWORD;
+			}
+
+(?i:"mysql_database")		{
+				return ASMYSQL_DATABASE;
+			}
+
+(?i:"mysql_server")		{
+				return ASMYSQL_SERVER;
+			}
+
+(?i:"mysql_port")		{
+				return ASMYSQL_PORT;
+			}
+			
+(?i:"mode")		{
+				return MODE;
+			}
+
+(?i:"datasource")		{
+				return DATASOURCE;
+			}
+			
+(?i:"mysql")		{
+				yylval->integer = ASMYSQL;
+				return INTEGER;
+			}
+(?i:"dsserver")		{
+				yylval->integer = MODE_DSSERVER;
+				return INTEGER;
+			}
+
+(?i:"sl")	{
+				yylval->integer = MODE_SL;
+				return INTEGER;
+			}
+
+
+			
+	/* Valid single characters for yyparse */
+[=;]			{ return yytext[0]; }
+
+	/* Unrecognized sequence, if it did not match any previous pattern */
+[^[:space:]"*=>;\n]+	{ 
+				fd_log_debug("Unrecognized text on line %d col %d: '%s'.\n", yylloc->first_line, yylloc->first_column, yytext);
+			 	return LEX_ERROR; 
+			}
+
+%%
Index: extensions/app_sip/app_sip.y
===================================================================
--- extensions/app_sip/app_sip.y	(revision 433)
+++ extensions/app_sip/app_sip.y	(revision 433)
@@ -0,0 +1,194 @@
+/*********************************************************************************************************
+* Software License Agreement (BSD License)                                                               *
+* Author: Alexandre Westfahl <awestfahl@freediameter.net>						 *
+*													 *
+* Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. 	 *		
+*													 *
+* All rights reserved.											 *
+* Based on ta_conf.y  (Sebastien Decugis <sdecugis@nict.go.jp>)					 *
+* 													 *
+* Redistribution and use of this software in source and binary forms, with or without modification, are  *
+* permitted provided that the following conditions are met:						 *
+* 													 *
+* * Redistributions of source code must retain the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer.										 *
+*    													 *
+* * Redistributions in binary form must reproduce the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer in the documentation and/or other						 *
+*   materials provided with the distribution.								 *
+* 													 *
+* * Neither the name of the Teraoka Laboratory nor the 							 *
+*   names of its contributors may be used to endorse or 						 *
+*   promote products derived from this software without 						 *
+*   specific prior written permission of Teraoka Laboratory 						 *
+*   													 *
+* 													 *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
+* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
+*********************************************************************************************************/
+
+
+
+/* For development only : */
+%debug 
+%error-verbose
+
+/* The parser receives the configuration file filename as parameter */
+%parse-param {char * conffile}
+
+/* Keep track of location */
+%locations 
+%pure-parser
+
+%{
+#include "app_sip.h"
+#include "app_sip.tab.h"	/* bison is not smart enough to define the YYLTYPE before including this code, so... */
+
+#include <string.h>
+#include <errno.h>
+
+/* Forward declaration */
+int yyparse(char * conffile);
+
+/* Parse the configuration file */
+int as_conf_handle(char * conffile)
+{
+	extern FILE * app_sipin;
+	int ret;
+	
+	TRACE_ENTRY("%p", conffile);
+	
+	TRACE_DEBUG (FULL, "Parsing configuration file: %s...", conffile);
+	
+	app_sipin = fopen(conffile, "r");
+	if (app_sipin == NULL) {
+		ret = errno;
+		fd_log_debug("Unable to open extension configuration file %s for reading: %s\n", conffile, strerror(ret));
+		TRACE_DEBUG (INFO, "Error occurred, message logged -- configuration file.");
+		return ret;
+	}
+
+	ret = yyparse(conffile);
+
+	fclose(app_sipin);
+
+	if (ret != 0) {
+		TRACE_DEBUG (INFO, "Unable to parse the configuration file.");
+		return EINVAL;
+	}
+	
+	return 0;
+}
+
+/* The Lex parser prototype */
+int app_siplex(YYSTYPE *lvalp, YYLTYPE *llocp);
+
+/* Function to report the errors */
+void yyerror (YYLTYPE *ploc, char * conffile, char const *s)
+{
+	TRACE_DEBUG(INFO, "Error in configuration parsing");
+	
+	if (ploc->first_line != ploc->last_line)
+		fd_log_debug("%s:%d.%d-%d.%d : %s\n", conffile, ploc->first_line, ploc->first_column, ploc->last_line, ploc->last_column, s);
+	else if (ploc->first_column != ploc->last_column)
+		fd_log_debug("%s:%d.%d-%d : %s\n", conffile, ploc->first_line, ploc->first_column, ploc->last_column, s);
+	else
+		fd_log_debug("%s:%d.%d : %s\n", conffile, ploc->first_line, ploc->first_column, s);
+}
+
+%}
+
+/* Values returned by lex for token */
+%union {
+	char 		*string;	/* The string is allocated by strdup in lex.*/
+	int		 integer;		/* Store integer values */
+}
+
+/* In case of error in the lexical analysis */
+%token 		LEX_ERROR
+
+/* Key words */
+%token 		MODE
+%token 		DATASOURCE
+%token 		ASMYSQL_LOGIN
+%token 		ASMYSQL_PASSWORD
+%token 		ASMYSQL_DATABASE
+%token 		ASMYSQL_SERVER
+%token 		ASMYSQL_PORT
+
+/* Tokens and types for routing table definition */
+/* A (de)quoted string (malloc'd in lex parser; it must be freed after use) */
+%token <string>	QSTRING
+
+/* An integer value */
+%token <integer> INTEGER
+
+
+
+/* -------------------------------------- */
+%%
+
+	/* The grammar definition */
+conffile:		/* empty grammar is OK */
+			| conffile mode
+			| conffile datasource
+			| conffile mysql_login
+			| conffile mysql_password
+			| conffile mysql_database
+			| conffile mysql_server
+			| conffile mysql_port
+			;
+
+mode:			MODE '=' INTEGER ';'
+			{
+				as_conf->mode = $3;
+			}
+			;
+
+datasource:			DATASOURCE '=' INTEGER ';'
+			{
+				as_conf->datasource = $3;
+			}
+			;
+
+mysql_login:			ASMYSQL_LOGIN '=' QSTRING ';'
+			{
+				free(as_conf->mysql_login);
+				as_conf->mysql_login = $3;
+			}
+			;
+
+mysql_password:			ASMYSQL_PASSWORD '=' QSTRING ';'
+			{
+				free(as_conf->mysql_password);
+				as_conf->mysql_password = $3;
+			}
+			;
+
+mysql_database:			ASMYSQL_DATABASE '=' QSTRING ';'
+			{
+				free(as_conf->mysql_database);
+				as_conf->mysql_database = $3;
+			}
+			;
+
+mysql_server:		ASMYSQL_SERVER '=' QSTRING ';'
+			{
+				free(as_conf->mysql_server);
+				as_conf->mysql_server = $3;
+			}
+			;
+
+mysql_port:		ASMYSQL_PORT '=' INTEGER ';'
+			{
+				as_conf->mysql_port = (uint16_t)$3;
+			}
+			;
Index: extensions/app_sip/diamsip.c
===================================================================
--- extensions/app_sip/diamsip.c	(revision 427)
+++ 	(revision )
@@ -1,252 +1,0 @@
-/*********************************************************************************************************
-* Software License Agreement (BSD License)                                                               *
-* Author: Alexandre Westfahl <awestfahl@freediameter.net>						 *
-*													 *
-* Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. 	 *		
-*													 *
-* All rights reserved.											 *
-* 													 *
-* Redistribution and use of this software in source and binary forms, with or without modification, are  *
-* permitted provided that the following conditions are met:						 *
-* 													 *
-* * Redistributions of source code must retain the above 						 *
-*   copyright notice, this list of conditions and the 							 *
-*   following disclaimer.										 *
-*    													 *
-* * Redistributions in binary form must reproduce the above 						 *
-*   copyright notice, this list of conditions and the 							 *
-*   following disclaimer in the documentation and/or other						 *
-*   materials provided with the distribution.								 *
-* 													 *
-* * Neither the name of the Teraoka Laboratory nor the 							 *
-*   names of its contributors may be used to endorse or 						 *
-*   promote products derived from this software without 						 *
-*   specific prior written permission of Teraoka Laboratory 						 *
-*   													 *
-* 													 *
-* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
-* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
-* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
-* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
-* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
-* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
-* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
-* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
-*********************************************************************************************************/
-#include "diamsip.h"
-
-//Diameter-SIP server
-struct disp_hdl * diamsip_MAR_hdl=NULL;
-struct disp_hdl * diamsip_LIR_hdl=NULL;
-struct disp_hdl * diamsip_UAR_hdl=NULL;
-struct disp_hdl * diamsip_SAR_hdl=NULL;
-struct disp_hdl * diamsip_PPA_hdl=NULL;
-struct disp_hdl * diamsip_RTA_hdl=NULL;
-
-//Suscriber Locator
-struct disp_hdl * diamsipSL_LIR_hdl=NULL;
-
-
-
-struct disp_hdl * diamsip_default_hdl=NULL;
-struct session_handler * ds_sess_hdl;
-
-//configuration stucture
-struct as_conf * as_conf=NULL;
-static struct as_conf app_sip_conf;
-
-//dictionary of SIP
-struct diamsip_dict sip_dict;
-
-int diamsip_default_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act)
-{
-	TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act);
-	
-	return 0;
-}
-
-void dump_config()
-{
-	TRACE_DEBUG(FULL,"***Configuration of Diameter-SIP extension***");
-	TRACE_DEBUG(FULL,"# mode: *%d*",as_conf->mode);
-	TRACE_DEBUG(FULL,"# datasource: *%d*",as_conf->datasource);
-	TRACE_DEBUG(FULL,"# mysql_login: *%s*",as_conf->mysql_login);
-	TRACE_DEBUG(FULL,"# mysql_password: *%s*",as_conf->mysql_password);
-	TRACE_DEBUG(FULL,"# mysql_database: *%s*",as_conf->mysql_database);
-	TRACE_DEBUG(FULL,"# mysql_server: *%s*",as_conf->mysql_server);
-	TRACE_DEBUG(FULL,"# mysql_port: *%d*",as_conf->mysql_port);
-	TRACE_DEBUG(FULL,"# mysql_port: *%d*",as_conf->mysql_prefix);
-	TRACE_DEBUG(FULL,"***End of Diameter-SIP configuration extension***");
-}
-
-static int as_conf_init(void)
-{
-	as_conf=&app_sip_conf;
-	//memset(app_sip_conf, 0, sizeof(struct as_conf));
-	
-
-	return 0;
-}
-
-/* entry point */
-int as_entry(char * conffile)
-{
-	TRACE_ENTRY("%p", conffile);
-	
-	struct dict_object * app=NULL;
-	struct disp_when data;
-	pthread_t rtr_thread, ppr_thread;
-	
-	/* Initialize configuration */
-	CHECK_FCT( as_conf_init() );
-	
-	
-	//We parse the configuration file
-	if (conffile != NULL) {
-		CHECK_FCT( as_conf_handle(conffile) );
-	}
-	else
-	{
-		TRACE_DEBUG(INFO, "We need a configuration file for Diameter-SIP extension. See doc/ for an example.");
-	}
-	
-	//TODO: replace by configuration file!!
-	strcpy(as_conf->mysql_prefix,"as_");
-	
-	//We can dump the configuration extracted from app_sip.conf
-	//dump_config();
-	
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Diameter Session Initiation Protocol (SIP) Application", &app, ENOENT) );
-	CHECK_FCT( fd_disp_app_support ( app, NULL, 1, 0 ) );
-	
-	
-	
-	//We set usefull AVPs 
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Auth-Session-State", &sip_dict.Auth_Session_State, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Auth-Application-Id", &sip_dict.Auth_Application_Id, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Destination-Host", &sip_dict.Destination_Host, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Destination-Realm", &sip_dict.Destination_Realm, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Session-Id", &sip_dict.Session_Id, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Redirect-Host", &sip_dict.Redirect_Host, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Redirect-Host-Usage", &sip_dict.Redirect_Host_Usage, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Auth-Data-Item", &sip_dict.SIP_Auth_Data_Item, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Authorization", &sip_dict.SIP_Authorization, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Authenticate", &sip_dict.SIP_Authenticate, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Number-Auth-Items", &sip_dict.SIP_Number_Auth_Items, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Authentication-Scheme", &sip_dict.SIP_Authentication_Scheme, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Authentication-Info", &sip_dict.SIP_Authentication_Info, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Server-URI", &sip_dict.SIP_Server_URI, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Server-Capabilities", &sip_dict.SIP_Server_Capabilities, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Mandatory-Capability", &sip_dict.SIP_Mandatory_Capability, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Optional-Capability", &sip_dict.SIP_Optional_Capability, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Method", &sip_dict.SIP_Method, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-AOR", &sip_dict.SIP_AOR, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Deregistration-Reason", &sip_dict.SIP_Deregistration_Reason, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Reason-Code", &sip_dict.SIP_Reason_Code, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Reason-Info", &sip_dict.SIP_Reason_Info, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Accounting-Information", &sip_dict.SIP_Accounting_Information, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Accounting-Server-URI", &sip_dict.SIP_Accounting_Server_URI, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Credit-Control-Server-URI", &sip_dict.SIP_Credit_Control_Server_URI, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Server-Assignment-Type", &sip_dict.SIP_Server_Assignment_Type, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Item-Number", &sip_dict.SIP_Item_Number, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Authorization-Type", &sip_dict.SIP_User_Authorization_Type, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Supported-User-Data-Type", &sip_dict.SIP_Supported_User_Data_Type, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Data", &sip_dict.SIP_User_Data, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Data-Type", &sip_dict.SIP_User_Data_Type, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Data-Contents", &sip_dict.SIP_User_Data_Contents, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-User-Data-Already-Available", &sip_dict.SIP_User_Data_Already_Available, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "SIP-Visited-Network-Id", &sip_dict.SIP_Visited_Network_Id, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Realm", &sip_dict.Digest_Realm, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-URI", &sip_dict.Digest_URI, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Nonce", &sip_dict.Digest_Nonce, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-CNonce", &sip_dict.Digest_CNonce, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Method", &sip_dict.Digest_Method, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Nonce-Count", &sip_dict.Digest_Nonce_Count, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Realm", &sip_dict.Digest_Realm, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Response", &sip_dict.Digest_Response, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Response-Auth", &sip_dict.Digest_Response_Auth, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Username", &sip_dict.Digest_Username, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-Algorithm", &sip_dict.Digest_Algorithm, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-QoP", &sip_dict.Digest_QOP, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "User-Name", &sip_dict.User_Name, ENOENT) );
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Digest-HA1", &sip_dict.Digest_HA1, ENOENT) );
-	
-	
-	//Register Application
-	memset(&data, 0, sizeof(data));
-	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Diameter Session Initiation Protocol (SIP) Application", &data.app, ENOENT) );
-	
-	if(as_conf->mode==1)
-	{
-	  //**Command Codes
-	  //MAR
-	  CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Multimedia-Auth-Request", &data.command, ENOENT) );
-	  CHECK_FCT( fd_disp_register( diamsip_MAR_cb, DISP_HOW_CC, &data, &diamsip_MAR_hdl ) );
-	  //RTA
-	  CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Registration-Termination-Answer", &data.command, ENOENT) );
-	  CHECK_FCT( fd_disp_register( diamsip_RTA_cb, DISP_HOW_CC, &data, &diamsip_RTA_hdl ) );
-	  //PPA
-	  CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Push-Profile-Answer", &data.command, ENOENT) );
-	  CHECK_FCT( fd_disp_register( diamsip_PPA_cb, DISP_HOW_CC, &data, &diamsip_PPA_hdl ) );
-	  //LIR
-	  CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Location-Info-Request", &data.command, ENOENT) );
-	  CHECK_FCT( fd_disp_register( diamsip_LIR_cb, DISP_HOW_CC, &data, &diamsip_LIR_hdl ) );
-	  //UAR
-	  CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "User-Authorization-Request", &data.command, ENOENT) );
-	  CHECK_FCT( fd_disp_register( diamsip_UAR_cb, DISP_HOW_CC, &data, &diamsip_UAR_hdl ) );
-	  //SAR
-	  CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Server-Assignment-Request", &data.command, ENOENT) );
-	  CHECK_FCT( fd_disp_register( diamsip_SAR_cb, DISP_HOW_CC, &data, &diamsip_SAR_hdl ) );
-	}
-	if(as_conf->mode==2)
-	{
-	  //LIR
-	  CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Location-Info-Request", &data.command, ENOENT) );
-	  CHECK_FCT( fd_disp_register( diamsipSL_LIR_cb, DISP_HOW_CC, &data, &diamsipSL_LIR_hdl ) );
-	}
-	//Callback for unexpected messages
-	CHECK_FCT( fd_disp_register( diamsip_default_cb, DISP_HOW_APPID, &data, &diamsip_default_hdl ) );
-	
-	
-	//We start database connection
-	if(start_mysql_connection())
-		return EINVAL;
-	
-	CHECK_FCT(fd_sess_handler_create(&ds_sess_hdl, free));
-	
-	//Creation of thread for Registration Termination	
-	if(pthread_create(&rtr_thread, NULL,rtr_socket, NULL))
-	{
-		TRACE_DEBUG(INFO,"Creation of thread failed, abort!");
-		return EINVAL;
-	}
-	//Creation of thread for Push Profile	
-	if(pthread_create(&ppr_thread, NULL,ppr_socket, NULL))
-	{
-		TRACE_DEBUG(INFO,"Creation of thread failed, abort!");
-		return EINVAL;
-	}
-	
-	
-	return 0;
-}
-
-//Cleanup callback
-void fd_ext_fini(void)
-{
-	//TODO:unregister other callbacks
-	
-	(void) fd_disp_unregister(&diamsip_MAR_hdl);
-	CHECK_FCT_DO( fd_sess_handler_destroy(&ds_sess_hdl),return);
-	
-	
-	//We close database connection
-	close_mysql_connection();
-	
-
-	
-	TRACE_ENTRY();
-	return ;
-}
-
-EXTENSION_ENTRY("app_sip", as_entry);
Index: extensions/app_sip/diamsip.h
===================================================================
--- extensions/app_sip/diamsip.h	(revision 432)
+++ 	(revision )
@@ -1,268 +1,0 @@
-/*********************************************************************************************************
-* Software License Agreement (BSD License)                                                               *
-* Author: Alexandre Westfahl <awestfahl@freediameter.net>						 *
-*													 *
-* Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. 	 *		
-*													 *
-* All rights reserved.											 *
-* 													 *
-* Redistribution and use of this software in source and binary forms, with or without modification, are  *
-* permitted provided that the following conditions are met:						 *
-* 													 *
-* * Redistributions of source code must retain the above 						 *
-*   copyright notice, this list of conditions and the 							 *
-*   following disclaimer.										 *
-*    													 *
-* * Redistributions in binary form must reproduce the above 						 *
-*   copyright notice, this list of conditions and the 							 *
-*   following disclaimer in the documentation and/or other						 *
-*   materials provided with the distribution.								 *
-* 													 *
-* * Neither the name of the Teraoka Laboratory nor the 							 *
-*   names of its contributors may be used to endorse or 						 *
-*   promote products derived from this software without 						 *
-*   specific prior written permission of Teraoka Laboratory 						 *
-*   													 *
-* 													 *
-* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
-* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
-* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
-* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
-* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
-* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
-* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
-* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
-*********************************************************************************************************/
-#include <freeDiameter/extension.h>
-#include <sys/time.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <gcrypt.h>
-#include <string.h>
-#include <mysql.h>
-#include "md5.h"
-
-
-#define NONCE_SIZE 16
-#define DIGEST_LEN 16
-
-
-/* Mode for the extension */
-#define MODE_DSSERVER	0x1
-#define	MODE_SL	0x2
-
-//Redirect_Host_Usage
-#define ALL_USER	6
-
-/* The module configuration */
-struct as_conf {
-	int		mode;		/* default MODE_DSSERVER | MODE_SL */
-	enum {ASMYSQL} datasource; 
-	char * mysql_login;
-	char * mysql_password;
-	char * mysql_database;
-	char * mysql_server;
-	char mysql_prefix[10]; //default: as_
-	uint16_t  mysql_port; //if 0, default port will be used
-	
-};
-extern struct as_conf * as_conf;
-
-/* Parse the configuration file */
-int as_conf_handle(char * conffile);
-
-
-extern MYSQL *conn;
-
-
-
-void calc_md5(char *buffer, char * data);
-void clear_digest(uint8_t * digest, char * readable_digest, int digestlength);
-struct avp_hdr * walk_digest(struct avp *avp, int avp_code);
-
-
-//MySQL part
-int start_mysql_connection();
-void request_mysql(char *query);
-void close_mysql_connection();
-int get_diameter_uri(const unsigned char *sip_aor, const size_t sipaorlen, char ** diameter_uri, size_t *diameterurilen);
-int exist_username(const unsigned char *sip_aor, const size_t sipaorlen);
-int get_sipserver_cap(const unsigned char *sip_aor, const size_t sipaorlen, struct avp **capabilities);
-int get_password(const unsigned char *username, const size_t usernamelen, char *password);
-int check_sipaor(const unsigned char  *username, const size_t usernamelen, const char * sip_aor,const size_t sipaorlen);
-int add_user_datatype(const unsigned char  *sip_aor, const size_t sipaorlen,struct msg *message);
-int set_pending_flag(const unsigned char  *username, const size_t usernamelen);
-int clear_pending_flag(const unsigned char  *username, const size_t usernamelen);
-int set_real_sipserver_uri(const unsigned char  *username, const size_t usernamelen, const unsigned char *sipserver_uri,const size_t sipserverurilen);
-int set_sipserver_uri(const unsigned char  *username, const size_t usernamelen, const unsigned char *sipserver_uri,const size_t sipserverurilen);
-int get_sipserver_uri(const unsigned char *sip_aor, const size_t sipaorlen, unsigned char ** sipserver_uri, size_t *sipserverurilen);
-int allow_roaming(const unsigned char  *username, const size_t usernamelen, const char * network,const size_t networklen);
-
-//count functions
-int count_avp(struct msg * message, int code, int vendor);
-
-
-void DigestCalcHA1(char * pszAlg,char * pszUserName,char * pszRealm,char * pszPassword,char * pszNonce,char * pszCNonce,HASHHEX SessionKey);
-void DigestCalcResponse(HASHHEX HA1,char * pszNonce,char * pszNonceCount,char * pszCNonce,char * pszQop,char * pszMethod,char * pszDigestUri,HASHHEX HEntity,HASHHEX Response);
-void DigestCalcResponseAuth(HASHHEX HA1,char * pszNonce,char * pszNonceCount,char * pszCNonce,char * pszQop,char * pszMethod,char * pszDigestUri,HASHHEX HEntity,HASHHEX Response);
-
-int fd_avp_search_avp ( struct avp * groupedavp, struct dict_object * what, struct avp ** avp );
-
-//thread procedure
-void *rtr_socket(void *);
-void *ppr_socket(void *);
-
-struct rtrsipaor
-{
-	char username[200];
-	char sip_aor1[200];
-	char sip_aor2[200];
-	char sip_aor3[200];
-	char strreason[200];
-	char desthost[200];
-	int reason;
-};
-struct pprsipaor
-{
-	char username[200];
-	char label1[200];
-	char value1[200];
-	char label2[200];
-	char value2[200];
-	char desthost[200];  
-};
-int diamsip_RTR_cb(struct rtrsipaor structure);
-int diamsip_PPR_cb(struct pprsipaor structure);
-#define PORT 666 //TODO:put in conf file
-
-int ds_entry();
-void fd_ext_fini(void);
-int diamsip_default_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act);
-int diamsip_MAR_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act);
-int diamsip_RTA_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act);
-int diamsip_PPA_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act);
-int diamsip_LIR_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act);
-int diamsip_UAR_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act);
-int diamsip_SAR_cb( struct msg ** msg, struct avp * avp, struct session * sess, enum disp_action * act);
-
-//Suscriber Locator
-int diamsipSL_LIR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act);
-//int diamsipSL_SAR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act);
-
-#define SQL_GETPASSWORD "SELECT `password` FROM ds_users WHERE `username` ='%s'"
-#define SQL_GETPASSWORD_LEN 52
-
-//username by SIP-AOR
-#define SQL_GETUSERNAME  "SELECT `username` FROM ds_users, ds_sip_aor WHERE `sip_aor` ='%s' AND `ds_sip_aor`.`id_user` = `ds_users`.`id_user`"
-#define SQL_GETUSERNAME_LEN 113
-
-//sip server uri by username
-#define SQL_GETSIPURI  "SELECT `sip_server_uri` FROM ds_users WHERE `username` ='%s'"
-#define SQL_GETSIPURI_LEN 60
-
-//sip server uri by SIP-AOR
-#define SQL_GETSIPSERURI  "SELECT `ds_sip_aor`.`sip_server_uri` FROM ds_users, ds_sip_aor WHERE `sip_aor` ='%s' AND `ds_sip_aor`.`id_user` = `ds_users`.`id_user`"
-#define SQL_GETSIPSERURI_LEN 132
-
-//sip capabilities for a SIP-AOR
-#define SQL_GETSIPSERCAP  "SELECT `compulsory`,`id_service` FROM ds_user_services, ds_sip_aor WHERE `sip_aor` ='%s' AND `ds_sip_aor`.`id_user` = `ds_user_services`.`id_user`"
-#define SQL_GETSIPSERCAP_LEN 144
-
-//user data for a user data supported
-#define SQL_GETSIPDATA  "SELECT `label_type`,`data` FROM ds_sip_aor, ds_user_data, ds_data_types WHERE `sip_aor` ='%s' AND `ds_sip_aor`.`id_sip_aor` = `ds_user_data`.`id_sip_aor` AND `ds_data_types`.`id_data_type`=`ds_user_data`.`id_data_type`"
-#define SQL_GETSIPDATA_LEN 216
-
-#define SQL_GETDIAMURI "SELECT `diameter_uri` FROM ds_sip_aor_map WHERE `sip_aor` ='%s'"
-#define SQL_GETDIAMURI_LEN 61
-
-//networks for this user
-#define SQL_GETUSERNET "SELECT `label_network` FROM ds_users, ds_user_networks, ds_networks WHERE `ds_users`.`username` ='%s' AND `ds_user_networks`.`id_user` = `ds_users`.`id_user` AND `ds_user_networks`.`id_network` = `ds_networks`.`id_network`"
-#define SQL_GETUSERNET_LEN 220
-
-#define SQL_SETSIPURI "UPDATE ds_users SET `temp_sip_server_uri`='%s' WHERE `username` ='%s'"
-#define SQL_SETSIPURI_LEN 65
-
-//TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO 
-#define SQL_RMSIPURI "UPDATE ds_users SET `temp_sip_server_uri`='', `sip_server_uri`='' WHERE `id_user` ='%s'"
-#define SQL_RMSIPURI_LEN 65
-//TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO 
-
-#define SQL_SETREALSIPURI "UPDATE ds_users SET `sip_server_uri`='%s' WHERE `username` ='%s'"
-#define SQL_SETREALSIPURI_LEN 65
-
-#define SQL_SETFLAG "UPDATE ds_users SET `authentication_pending`=1 WHERE `username` ='%s'"
-#define SQL_SETFLAG_LEN 67
-
-#define SQL_CLEARFLAG "UPDATE ds_users SET `authentication_pending`=0, `registrated`=1 WHERE `username` ='%s'"
-#define SQL_CLEARFLAG_LEN 84
-
-#define SQL_GETSIPAOR "SELECT `sip_aor` FROM `ds_sip_aor`, `ds_users` WHERE `ds_sip_aor`.`id_user` = `ds_users`.`id_user` AND `ds_users`.`username` = '%s'"
-#define SQL_GETSIPAOR_LEN 131
-
-//#define SQL_CLEARFLAG "UPDATE ds_users SET `authentication_pending`=0 WHERE `username` ='%s'"
-//#define SQL_CLEARFLAG_LEN 67
-
-extern struct session_handler * ds_sess_hdl;
-
-//AVP code
-#define CODE_SIP_USER_DATA_TYPE	388
-#define CODE_SIP_AOR	122
-
-struct ds_nonce
-{
-	char *nonce;
-};
-
-//Storage for some usefull AVPs
-struct diamsip_dict{
-	struct dict_object * Auth_Session_State;
-	struct dict_object * Auth_Application_Id;
-	struct dict_object * Destination_Host;
-	struct dict_object * Destination_Realm;
-	struct dict_object * User_Name;
-	struct dict_object * Session_Id;
-	struct dict_object * Redirect_Host;
-	struct dict_object * Redirect_Host_Usage;
-	struct dict_object * SIP_Auth_Data_Item;
-	struct dict_object * SIP_Accounting_Information;
-	struct dict_object * SIP_Accounting_Server_URI;
-	struct dict_object * SIP_Credit_Control_Server_URI;
-	struct dict_object * SIP_Server_Assignment_Type;
-	struct dict_object * SIP_Item_Number;
-	struct dict_object * SIP_User_Authorization_Type;
-	struct dict_object * SIP_Supported_User_Data_Type;
-	struct dict_object * SIP_User_Data;
-	struct dict_object * SIP_User_Data_Type;
-	struct dict_object * SIP_User_Data_Contents;
-	struct dict_object * SIP_User_Data_Already_Available;
-	struct dict_object * SIP_Visited_Network_Id;
-	struct dict_object * SIP_Authorization;
-	struct dict_object * SIP_Authenticate;
-	struct dict_object * SIP_Number_Auth_Items;	
-	struct dict_object * SIP_Authentication_Scheme;
-	struct dict_object * SIP_Authentication_Info;	
-	struct dict_object * SIP_Server_URI;
-	struct dict_object * SIP_Server_Capabilities;
-	struct dict_object * SIP_Mandatory_Capability;
-	struct dict_object * SIP_Optional_Capability;
-	struct dict_object * SIP_Method;
-	struct dict_object * SIP_AOR;
-	struct dict_object * SIP_Deregistration_Reason;
-	struct dict_object * SIP_Reason_Code;
-	struct dict_object * SIP_Reason_Info;
-	struct dict_object * Digest_URI;		
-	struct dict_object * Digest_Nonce;
-	struct dict_object * Digest_Nonce_Count;
-	struct dict_object * Digest_CNonce;		
-	struct dict_object * Digest_Realm;		
-	struct dict_object * Digest_Response;	
-	struct dict_object * Digest_Response_Auth;	
-	struct dict_object * Digest_Username;	
-	struct dict_object * Digest_Method;
-	struct dict_object * Digest_QOP;	
-	struct dict_object * Digest_Algorithm;
-	struct dict_object * Digest_HA1;
-};
-
-extern  struct diamsip_dict  sip_dict;
Index: extensions/app_sip/diamsip.l
===================================================================
--- extensions/app_sip/diamsip.l	(revision 392)
+++ 	(revision )
@@ -1,157 +1,0 @@
-/*********************************************************************************************************
-* Software License Agreement (BSD License)                                                               *
-* Author: Alexandre Westfahl <awestfahl@freediameter.net>						 *
-*													 *
-* Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. 	 *		
-*													 *
-* All rights reserved.											 *
-* Based on ta_conf.l (Sebastien Decugis <sdecugis@nict.go.jp>)					 *
-* 													 *
-* Redistribution and use of this software in source and binary forms, with or without modification, are  *
-* permitted provided that the following conditions are met:						 *
-* 													 *
-* * Redistributions of source code must retain the above 						 *
-*   copyright notice, this list of conditions and the 							 *
-*   following disclaimer.										 *
-*    													 *
-* * Redistributions in binary form must reproduce the above 						 *
-*   copyright notice, this list of conditions and the 							 *
-*   following disclaimer in the documentation and/or other						 *
-*   materials provided with the distribution.								 *
-* 													 *
-* * Neither the name of the Teraoka Laboratory nor the 							 *
-*   names of its contributors may be used to endorse or 						 *
-*   promote products derived from this software without 						 *
-*   specific prior written permission of Teraoka Laboratory 						 *
-*   													 *
-* 													 *
-* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
-* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
-* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
-* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
-* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
-* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
-* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
-* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
-*********************************************************************************************************/
-
-
-
-%{
-#include "diamsip.h"
-/* Include yacc tokens definitions */
-#include "diamsip.tab.h"
-
-/* Update the column information */
-#define YY_USER_ACTION { 						\
-	yylloc->first_column = yylloc->last_column + 1; 		\
-	yylloc->last_column = yylloc->first_column + yyleng - 1;	\
-}
-
-/* Avoid warning with newer flex */
-#define YY_NO_INPUT
-
-%}
-
-%option bison-bridge bison-locations
-%option noyywrap
-%option nounput
-
-%%
-
-	/* Update the line count */
-\n			{
-				yylloc->first_line++; 
-				yylloc->last_line++; 
-				yylloc->last_column=0; 
-			}
-	 
-	/* Eat all spaces but not new lines */
-([[:space:]]{-}[\n])+	;
-	/* Eat all comments */
-#.*$			;
-
-	/* Recognize any integer */
-[-]?[[:digit:]]+		{
-				/* Convert this to an integer value */
-				int ret=0;
-				ret = sscanf(yytext, "%i", &yylval->integer);
-				if (ret != 1) {
-					/* No matching: an error occurred */
-					fd_log_debug("Unable to convert the value '%s' to a valid number: %s\n", yytext, strerror(errno));
-					return LEX_ERROR; /* trig an error in yacc parser */
-					/* Maybe we could REJECT instead of failing here? */
-				}
-				return INTEGER;
-			}
-
-	/* Recognize quoted strings -- we do not support escaped \" in the string currently. */
-\"[^\"]+\"		{
-				/* Match a quoted string. Let's be very permissive. */
-				yylval->string = strdup(yytext+1);
-				if (!yylval->string) {
-					fd_log_debug("Unable to copy the string '%s': %s\n", yytext, strerror(errno));
-					TRACE_DEBUG(INFO, "strdup failed");
-					return LEX_ERROR; /* trig an error in yacc parser */
-				}
-				yylval->string[strlen(yytext) - 2] = '\0';
-				return QSTRING;
-			}
-
-
-
-	/* Recognize the tokens */	
-(?i:"mysql_login")	{
-				return ASMYSQL_LOGIN;
-			}
-
-(?i:"mysql_password")		{
-				return ASMYSQL_PASSWORD;
-			}
-
-(?i:"mysql_database")		{
-				return ASMYSQL_DATABASE;
-			}
-
-(?i:"mysql_server")		{
-				return ASMYSQL_SERVER;
-			}
-
-(?i:"mysql_port")		{
-				return ASMYSQL_PORT;
-			}
-			
-(?i:"mode")		{
-				return MODE;
-			}
-
-(?i:"datasource")		{
-				return DATASOURCE;
-			}
-			
-(?i:"mysql")		{
-				yylval->integer = ASMYSQL;
-				return INTEGER;
-			}
-(?i:"dsserver")		{
-				yylval->integer = MODE_DSSERVER;
-				return INTEGER;
-			}
-
-(?i:"sl")	{
-				yylval->integer = MODE_SL;
-				return INTEGER;
-			}
-
-
-			
-	/* Valid single characters for yyparse */
-[=;]			{ return yytext[0]; }
-
-	/* Unrecognized sequence, if it did not match any previous pattern */
-[^[:space:]"*=>;\n]+	{ 
-				fd_log_debug("Unrecognized text on line %d col %d: '%s'.\n", yylloc->first_line, yylloc->first_column, yytext);
-			 	return LEX_ERROR; 
-			}
-
-%%
Index: extensions/app_sip/diamsip.y
===================================================================
--- extensions/app_sip/diamsip.y	(revision 429)
+++ 	(revision )
@@ -1,194 +1,0 @@
-/*********************************************************************************************************
-* Software License Agreement (BSD License)                                                               *
-* Author: Alexandre Westfahl <awestfahl@freediameter.net>						 *
-*													 *
-* Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. 	 *		
-*													 *
-* All rights reserved.											 *
-* Based on ta_conf.y  (Sebastien Decugis <sdecugis@nict.go.jp>)					 *
-* 													 *
-* Redistribution and use of this software in source and binary forms, with or without modification, are  *
-* permitted provided that the following conditions are met:						 *
-* 													 *
-* * Redistributions of source code must retain the above 						 *
-*   copyright notice, this list of conditions and the 							 *
-*   following disclaimer.										 *
-*    													 *
-* * Redistributions in binary form must reproduce the above 						 *
-*   copyright notice, this list of conditions and the 							 *
-*   following disclaimer in the documentation and/or other						 *
-*   materials provided with the distribution.								 *
-* 													 *
-* * Neither the name of the Teraoka Laboratory nor the 							 *
-*   names of its contributors may be used to endorse or 						 *
-*   promote products derived from this software without 						 *
-*   specific prior written permission of Teraoka Laboratory 						 *
-*   													 *
-* 													 *
-* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
-* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
-* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
-* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
-* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
-* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
-* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
-* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
-*********************************************************************************************************/
-
-
-
-/* For development only : */
-%debug 
-%error-verbose
-
-/* The parser receives the configuration file filename as parameter */
-%parse-param {char * conffile}
-
-/* Keep track of location */
-%locations 
-%pure-parser
-
-%{
-#include "diamsip.h"
-#include "diamsip.tab.h"	/* bison is not smart enough to define the YYLTYPE before including this code, so... */
-
-#include <string.h>
-#include <errno.h>
-
-/* Forward declaration */
-int yyparse(char * conffile);
-
-/* Parse the configuration file */
-int as_conf_handle(char * conffile)
-{
-	extern FILE * diamsipin;
-	int ret;
-	
-	TRACE_ENTRY("%p", conffile);
-	
-	TRACE_DEBUG (FULL, "Parsing configuration file: %s...", conffile);
-	
-	diamsipin = fopen(conffile, "r");
-	if (diamsipin == NULL) {
-		ret = errno;
-		fd_log_debug("Unable to open extension configuration file %s for reading: %s\n", conffile, strerror(ret));
-		TRACE_DEBUG (INFO, "Error occurred, message logged -- configuration file.");
-		return ret;
-	}
-
-	ret = yyparse(conffile);
-
-	fclose(diamsipin);
-
-	if (ret != 0) {
-		TRACE_DEBUG (INFO, "Unable to parse the configuration file.");
-		return EINVAL;
-	}
-	
-	return 0;
-}
-
-/* The Lex parser prototype */
-int diamsiplex(YYSTYPE *lvalp, YYLTYPE *llocp);
-
-/* Function to report the errors */
-void yyerror (YYLTYPE *ploc, char * conffile, char const *s)
-{
-	TRACE_DEBUG(INFO, "Error in configuration parsing");
-	
-	if (ploc->first_line != ploc->last_line)
-		fd_log_debug("%s:%d.%d-%d.%d : %s\n", conffile, ploc->first_line, ploc->first_column, ploc->last_line, ploc->last_column, s);
-	else if (ploc->first_column != ploc->last_column)
-		fd_log_debug("%s:%d.%d-%d : %s\n", conffile, ploc->first_line, ploc->first_column, ploc->last_column, s);
-	else
-		fd_log_debug("%s:%d.%d : %s\n", conffile, ploc->first_line, ploc->first_column, s);
-}
-
-%}
-
-/* Values returned by lex for token */
-%union {
-	char 		*string;	/* The string is allocated by strdup in lex.*/
-	int		 integer;		/* Store integer values */
-}
-
-/* In case of error in the lexical analysis */
-%token 		LEX_ERROR
-
-/* Key words */
-%token 		MODE
-%token 		DATASOURCE
-%token 		ASMYSQL_LOGIN
-%token 		ASMYSQL_PASSWORD
-%token 		ASMYSQL_DATABASE
-%token 		ASMYSQL_SERVER
-%token 		ASMYSQL_PORT
-
-/* Tokens and types for routing table definition */
-/* A (de)quoted string (malloc'd in lex parser; it must be freed after use) */
-%token <string>	QSTRING
-
-/* An integer value */
-%token <integer> INTEGER
-
-
-
-/* -------------------------------------- */
-%%
-
-	/* The grammar definition */
-conffile:		/* empty grammar is OK */
-			| conffile mode
-			| conffile datasource
-			| conffile mysql_login
-			| conffile mysql_password
-			| conffile mysql_database
-			| conffile mysql_server
-			| conffile mysql_port
-			;
-
-mode:			MODE '=' INTEGER ';'
-			{
-				as_conf->mode = $3;
-			}
-			;
-
-datasource:			DATASOURCE '=' INTEGER ';'
-			{
-				as_conf->datasource = $3;
-			}
-			;
-
-mysql_login:			ASMYSQL_LOGIN '=' QSTRING ';'
-			{
-				free(as_conf->mysql_login);
-				as_conf->mysql_login = $3;
-			}
-			;
-
-mysql_password:			ASMYSQL_PASSWORD '=' QSTRING ';'
-			{
-				free(as_conf->mysql_password);
-				as_conf->mysql_password = $3;
-			}
-			;
-
-mysql_database:			ASMYSQL_DATABASE '=' QSTRING ';'
-			{
-				free(as_conf->mysql_database);
-				as_conf->mysql_database = $3;
-			}
-			;
-
-mysql_server:		ASMYSQL_SERVER '=' QSTRING ';'
-			{
-				free(as_conf->mysql_server);
-				as_conf->mysql_server = $3;
-			}
-			;
-
-mysql_port:		ASMYSQL_PORT '=' INTEGER ';'
-			{
-				as_conf->mysql_port = (uint16_t)$3;
-			}
-			;
Index: extensions/app_sip/libapp_sip.c
===================================================================
--- extensions/app_sip/libapp_sip.c	(revision 433)
+++ extensions/app_sip/libapp_sip.c	(revision 433)
@@ -0,0 +1,1078 @@
+/*********************************************************************************************************
+* Software License Agreement (BSD License)                                                               *
+* Author: Alexandre Westfahl <awestfahl@freediameter.net>						 *
+*													 *
+* Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. 	 *		
+*													 *
+* All rights reserved.											 *
+* 													 *
+* Redistribution and use of this software in source and binary forms, with or without modification, are  *
+* permitted provided that the following conditions are met:						 *
+* 													 *
+* * Redistributions of source code must retain the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer.										 *
+*    													 *
+* * Redistributions in binary form must reproduce the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer in the documentation and/or other						 *
+*   materials provided with the distribution.								 *
+* 													 *
+* * Neither the name of the Teraoka Laboratory nor the 							 *
+*   names of its contributors may be used to endorse or 						 *
+*   promote products derived from this software without 						 *
+*   specific prior written permission of Teraoka Laboratory 						 *
+*   													 *
+* 													 *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
+* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
+*********************************************************************************************************/
+//#include <mysql.h>
+#include "app_sip.h"
+
+MYSQL *conn;
+
+void clear_digest(uint8_t * digest, char * readable_digest, int digestlength)
+{
+	int i=0;
+	for(i=0;i<digestlength * 2;i++)
+		sprintf(&readable_digest[2 * i], "%2.2hhx", digest[i]);
+	readable_digest[2 * digestlength]='\0';
+	
+	return;
+}
+
+// You must create a table like this "char  clearDigest[DIGEST_LEN*2+1];"
+void calc_md5(char *clearDigest, char * data)
+{
+	gcry_md_hd_t md5;
+	uint8_t * binDigest=NULL;
+	
+	CHECK_MALLOC_DO(binDigest=malloc(DIGEST_LEN),return);
+	
+	gcry_md_open(&md5,GCRY_MD_MD5, 0); 
+	gcry_md_write(md5, (char *)data, sizeof(data));
+	memcpy(binDigest, gcry_md_read(md5,  GCRY_MD_MD5),gcry_md_get_algo_dlen(GCRY_MD_MD5));
+	gcry_md_close(md5);
+	
+	clear_digest(binDigest, clearDigest, DIGEST_LEN);
+	free(binDigest);
+	return;
+}
+
+
+/* Search a given AVP model in an AVP (extracted from libfreediameter/message.c ) */
+int fd_avp_search_avp ( struct avp * groupedavp, struct dict_object * what, struct avp ** avp )
+{
+	struct avp * nextavp;
+	struct avp_hdr * nextavphdr;
+	struct dict_avp_data 	dictdata;
+	
+	
+	TRACE_ENTRY("%p %p %p", groupedavp, what, avp);
+	
+	CHECK_FCT(  fd_dict_getval(what, &dictdata)  );
+	
+	// Loop only in the group AVP 
+	CHECK_FCT(  fd_msg_browse(groupedavp, MSG_BRW_FIRST_CHILD, (void *)&nextavp, NULL)  );
+	CHECK_FCT( fd_msg_avp_hdr( nextavp, &nextavphdr )  );
+	
+	while (nextavphdr) {
+		
+		if ( (nextavphdr->avp_code   == dictdata.avp_code) && (nextavphdr->avp_vendor == dictdata.avp_vendor) ) // always 0 if no Vendor flag
+		{
+			break;
+		}
+		
+		// Otherwise move to next AVP in the grouped AVP 
+		CHECK_FCT( fd_msg_browse(nextavp, MSG_BRW_NEXT, (void *)&nextavp, NULL) );
+		
+		if(nextavp!=NULL)
+		{
+			CHECK_FCT( fd_msg_avp_hdr( nextavp, &nextavphdr )  );
+		}
+		else
+			nextavphdr=NULL;
+	}
+	if (avp)
+		*avp = nextavp;
+	
+	if (avp && nextavp) {
+		struct dictionary * dict;
+		CHECK_FCT( fd_dict_getdict( what, &dict) );
+		CHECK_FCT_DO( fd_msg_parse_dict( nextavp, dict, NULL ),  );
+	}
+	
+	if (avp || nextavp)
+		return 0;
+	else
+		return ENOENT;
+}
+struct avp_hdr *walk_digest(struct avp *avp, int avp_code)
+{
+	struct avp_hdr *temphdr=NULL;
+	CHECK_FCT_DO(fd_msg_browse ( avp, MSG_BRW_WALK, &avp, NULL),return NULL);
+	
+	while(avp!=NULL)
+	{
+		
+		CHECK_FCT_DO( fd_msg_avp_hdr( avp,&temphdr ),return NULL);
+
+		if(temphdr->avp_code==avp_code)
+		{
+			//We found the AVP so we set avp to NULL to exit the loop
+			avp=NULL;
+			return temphdr;
+			
+		}
+		else if(temphdr->avp_code==380)//SIP-Authorization AVP
+		{
+			//We didn't found the AVP but we finished browsing the Authentication AVP
+			avp=NULL;
+			temphdr=NULL;
+			
+			return temphdr;
+		}
+		else
+		{
+			CHECK_FCT_DO(fd_msg_browse ( avp, MSG_BRW_WALK, &avp, NULL),return NULL);
+			temphdr=NULL;
+			
+		}
+	}
+	
+	return temphdr;
+}
+
+int start_mysql_connection()
+{
+	conn = mysql_init(NULL);
+	
+	mysql_options(conn, MYSQL_OPT_RECONNECT, "true");
+	
+	if (!mysql_real_connect(conn, as_conf->mysql_server,as_conf->mysql_login, as_conf->mysql_password, as_conf->mysql_database, as_conf->mysql_port, NULL, 0)) 
+	{//TODO: display error from mysql
+		TRACE_DEBUG(INFO,"Unable to connect to database (%s) with login:%s",as_conf->mysql_database,as_conf->mysql_login);
+		return 1;
+	}
+	return 0;
+	
+}
+
+
+void request_mysql(char *query)
+{
+	//We check if the connection is still up
+	mysql_ping(conn);
+	
+	if (mysql_query(conn, query)) 
+	{
+		TRACE_DEBUG(INFO,"Query %s failed", query);
+		
+	}
+	
+}
+
+void close_mysql_connection()
+{
+	mysql_close(conn);
+	
+}
+
+//If password is null, we just verify this user exist
+//We don't need the password length because it is a table'
+int get_password(const unsigned char *username, const size_t usernamelen, char *password)
+{
+	CHECK_PARAMS(username && usernamelen);
+	
+	int not_found=2;
+	size_t querylen, usernamepurelen;
+	char *query, *username_pure;
+	
+	switch(as_conf->datasource)
+	{
+		//MySQL
+		case ASMYSQL:
+			//We allocate the double size of username because at worst it can be all quotes
+			username_pure=malloc(usernamelen*2+1);
+			//We purify username not to have forbidden characters
+			usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen);
+			
+			//We copy username in query
+			querylen=SQL_GETPASSWORD_LEN + usernamepurelen;
+			query = malloc(querylen+2);
+			snprintf(query, querylen+1, SQL_GETPASSWORD, username_pure);
+			
+			MYSQL_RES *res;
+			MYSQL_ROW row;
+			
+			//We make the query	
+			request_mysql(query);
+			res=mysql_use_result(conn);
+			if(res==NULL)
+			{
+				if(password!=NULL)
+					password[0]='\0';
+				free(query);
+				return 2;
+			}
+			
+			
+			
+			while ((row = mysql_fetch_row(res)) != NULL)
+			{
+				if(strlen(row[0])>0)
+				{
+					if(password!=NULL)
+						strcpy(password,row[0]);
+					
+					not_found=0;
+					break;
+				}
+			}
+			mysql_free_result(res);
+			free(query);
+		break;
+	}
+	return not_found;
+}
+
+int check_sipaor(const unsigned char  *username, const size_t usernamelen, const char * sip_aor,const size_t sipaorlen)
+{
+	CHECK_PARAMS(username && usernamelen && sip_aor && sipaorlen);
+	
+	int not_found=2;
+	size_t querylen, usernamepurelen;
+	char *query, *username_pure;
+	
+	switch(as_conf->datasource)
+	{
+		//MySQL
+		case ASMYSQL:
+			//We allocate the double size of username because at worst it can be all quotes
+			username_pure=malloc(usernamelen*2+1);
+			//We purify username not to have forbidden characters
+			usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen);
+	
+			//We get the list of AOR owned by this user
+			querylen=SQL_GETSIPAOR_LEN + usernamepurelen;
+			query = malloc(querylen+2);
+			snprintf(query, querylen+1, SQL_GETSIPAOR, username_pure);
+			
+			MYSQL_RES *res;
+			MYSQL_ROW row;
+			
+			//We make the query
+			request_mysql(query);
+			res=mysql_use_result(conn);
+			if(res==NULL)
+			{
+				free(query);
+				return 2;
+			}
+			
+			
+			not_found=1;
+			while ((row = mysql_fetch_row(res)) != NULL)
+			{
+				if(strncmp((const char *)sip_aor,row[0],sipaorlen)==0)
+				{
+					not_found=0;
+					break;
+				}
+			}
+			mysql_free_result(res);
+			free(query);
+		break;
+	}
+	return not_found;
+}
+
+int get_diameter_uri(const unsigned char *sip_aor, const size_t sipaorlen, char ** diameter_uri, size_t *diameterurilen)
+{
+	CHECK_PARAMS(sip_aor && sipaorlen);
+
+	size_t querylen, sipaorpurelen;
+	char *query, *sipaor_pure;
+	int not_found=2;
+
+
+
+
+	//a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars
+	if(sipaorlen<5)
+		return 2;
+
+	//NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security
+	switch(as_conf->datasource)
+	{
+		//MySQL
+		case ASMYSQL:
+
+			querylen=SQL_GETDIAMURI_LEN + sipaorlen;
+
+			
+			//We allocate the double size of SIP-URI because at worst it can be all quotes
+			CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1));
+			//We purify SIP-URI not to have forbidden characters
+			sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen);
+			
+			
+			query = malloc(querylen+sipaorpurelen+ 2);
+			snprintf(query, querylen+1, SQL_GETDIAMURI, sipaor_pure);
+			
+			MYSQL_RES *res;
+			MYSQL_ROW row;
+			
+
+			//We make the query	
+			request_mysql(query);
+			res=mysql_use_result(conn);
+			if(res==NULL)
+			{
+				//We couldn't make the request
+				diameter_uri=NULL;
+				return 2;
+			}
+			
+			while ((row = mysql_fetch_row(res)) != NULL)
+			{
+				*diameterurilen=strlen(row[0]);
+				if(*diameterurilen>0)
+				{
+					CHECK_MALLOC(*diameter_uri=malloc(*diameterurilen+1));
+					strcpy(*diameter_uri,row[0]);
+					not_found=0;
+					break;
+				}
+			}
+			mysql_free_result(res);
+			free(query);
+			free(sipaor_pure);
+		break;
+      
+    default:
+      
+      //We must never go here, if so, we must stop diameter_sip
+      diameter_uri=NULL;
+      return 2;
+      
+      break;
+  }
+  
+  //0 if it was found
+  return not_found;
+  
+}
+
+
+int exist_username(const unsigned char *sip_aor, const size_t sipaorlen)
+{
+	CHECK_PARAMS(sip_aor && sipaorlen);
+	
+	size_t querylen, sipaorpurelen;
+	char *query, *sipaor_pure;
+	int not_found=1;
+	
+	//a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars
+	if(sipaorlen<5)
+		return 2;
+	
+	//NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security
+	
+	switch(as_conf->datasource)
+	{
+		//MySQL
+		case ASMYSQL:
+			
+			querylen=SQL_GETUSERNAME_LEN + sipaorlen;
+			
+			
+			//We allocate the double size of SIP-URI because at worst it can be all quotes
+			CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1));
+			//We purify SIP-URI not to have forbidden characters
+			sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen);
+			
+			
+			query = malloc(querylen+sipaorpurelen+ 2);
+			snprintf(query, querylen+1, SQL_GETUSERNAME, sipaor_pure);
+			
+			MYSQL_RES *res;
+			MYSQL_ROW row;
+			
+			
+			//We make the query	
+			request_mysql(query);
+			res=mysql_use_result(conn);
+			if(res==NULL)
+			{
+				//We couldn't make the request
+				return 2;
+			}
+			
+			while ((row = mysql_fetch_row(res)) != NULL)
+			{
+				if(strlen(row[0])>0)
+				{
+					not_found=0;
+					break;
+				}
+			}
+			mysql_free_result(res);
+			free(query);
+			free(sipaor_pure);
+			break;
+			
+		default:
+			
+			//We must never go here, if so, we must stop diameter_sip
+			TRACE_DEBUG(INFO,"FATAL ERROR: the datasource is unknown, please check your config file!");
+			return 2;
+			
+			break;
+	}
+	
+	//0 if it was found
+	return not_found;
+	
+}
+
+//We check if this user can go in the given network
+int allow_roaming(const unsigned char  *username, const size_t usernamelen, const char * network,const size_t networklen)
+{
+	CHECK_PARAMS(username && usernamelen && network && networklen);
+	
+	int not_found=2;
+	size_t querylen, usernamepurelen;
+	char *query, *username_pure;
+	
+	switch(as_conf->datasource)
+	{
+		//MySQL
+		case ASMYSQL:
+			//We allocate the double size of username because at worst it can be all quotes
+			username_pure=malloc(usernamelen*2+1);
+			//We purify username not to have forbidden characters
+			usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen);
+			
+			//We get the list of AOR owned by this user
+			querylen=SQL_GETUSERNET_LEN + usernamepurelen;
+			query = malloc(querylen+2);
+			snprintf(query, querylen+1, SQL_GETUSERNET, username_pure);
+			
+			
+			MYSQL_RES *res;
+			MYSQL_ROW row;
+			
+			
+			//We make the query
+			request_mysql(query);
+			res=mysql_use_result(conn);
+			if(res==NULL)
+			{
+				free(query);
+				return 2;
+			}
+			
+			
+			not_found=1;
+			while ((row = mysql_fetch_row(res)) != NULL)
+			{
+				if(strncmp((const char *)network,row[0],networklen)==0)
+				{
+					not_found=0;
+					break;
+				}
+			}
+			mysql_free_result(res);
+			free(query);
+			break;
+	}
+	return not_found;
+}
+
+//SIP-Server-Capabilities for the SIP-AOR
+int get_sipserver_cap(const unsigned char *sip_aor, const size_t sipaorlen, struct avp **capabilities)
+{
+	CHECK_PARAMS(sip_aor && sipaorlen && capabilities);
+	
+	size_t querylen, sipaorpurelen;
+	char *query, *sipaor_pure;
+	int not_found=2;
+	union avp_value value;
+	struct avp *avp;
+	
+	//a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars
+	if(sipaorlen<5)
+		return 2;
+	
+	//NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security
+	switch(as_conf->datasource)
+	{
+		//MySQL
+		case ASMYSQL:
+			
+			querylen=SQL_GETSIPSERCAP_LEN + sipaorlen;
+			
+			
+			//We allocate the double size of SIP-URI because at worst it can be all quotes
+			CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1));
+			//We purify SIP-URI not to have forbidden characters
+			sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen);
+			
+			
+			query = malloc(querylen+sipaorpurelen+ 2);
+			snprintf(query, querylen+1, SQL_GETSIPSERCAP, sipaor_pure);
+			
+			MYSQL_RES *res;
+			MYSQL_ROW row;
+			
+			//We make the query	
+			request_mysql(query);
+			res=mysql_use_result(conn);
+			if(res==NULL)
+			{
+				//We couldn't make the request
+				return 2;
+			}
+			not_found=1;
+			while ((row = mysql_fetch_row(res)) != NULL)
+			{
+				if(atoi(row[0])==1)
+				{//mandatory
+					CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_Mandatory_Capability, 0, &avp ) );
+					value.i32=(uint32_t)atoi(row[1]);
+					CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
+					CHECK_FCT( fd_msg_avp_add ( *capabilities, MSG_BRW_LAST_CHILD, avp) );
+					
+				}
+				else
+				{//optional
+					CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_Optional_Capability, 0, &avp ) );
+					value.i32=(uint32_t)atoi(row[1]);
+					CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
+					CHECK_FCT( fd_msg_avp_add ( *capabilities, MSG_BRW_LAST_CHILD, avp) );
+				}
+				not_found=0;
+			}
+			
+			mysql_free_result(res);
+			free(query);
+			free(sipaor_pure);
+			break;
+			
+		default:
+			
+			//We must never go here, if so, we must stop diameter_sip
+			TRACE_DEBUG(INFO,"FATAL ERROR: the datasource is unknown, please check your config file!");
+			return 2;
+			
+			break;
+	}
+	
+	//0 if it was found
+	return not_found;
+	
+}
+
+
+//We retrieve datatype
+int add_user_datatype(const unsigned char  *sip_aor, const size_t sipaorlen,struct msg *message)
+{
+	CHECK_PARAMS(sip_aor && sipaorlen && message );
+	
+	size_t querylen, sipaorpurelen;
+	char *query, *sipaor_pure;
+	int not_found=2;
+	union avp_value value;
+	struct avp *avp, *rootavp;
+	unsigned long *length;
+	
+	//a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars
+	if(sipaorlen<5)
+		return 2;
+	
+	//NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security
+	switch(as_conf->datasource)
+	{
+		//MySQL
+		case ASMYSQL:
+			
+			querylen=SQL_GETSIPDATA_LEN + sipaorlen;
+			
+			
+			//We allocate the double size of SIP-URI because at worst it can be all quotes
+			CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1));
+			//We purify SIP-URI not to have forbidden characters
+			sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen);
+			
+			
+			query = malloc(querylen+sipaorpurelen+ 2);
+			snprintf(query, querylen+1, SQL_GETSIPDATA, sipaor_pure);
+			
+			MYSQL_RES *res;
+			MYSQL_ROW row;
+			
+			//We make the query	
+			request_mysql(query);
+			res=mysql_use_result(conn);
+			if(res==NULL)
+			{
+				//We couldn't make the request
+				return 2;
+			}
+			not_found=1;
+			while ((row = mysql_fetch_row(res)) != NULL)
+			{
+				length=mysql_fetch_lengths(res);
+				
+				if(strlen(row[0])>1)
+				{
+					CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_User_Data, 0, &rootavp ) );
+					
+					CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_User_Data_Type, 0, &avp ) );
+					CHECK_MALLOC(value.os.data=malloc(length[0]*sizeof(unsigned char)));
+					strncpy((char *)value.os.data,(char *)row[0],length[0]);
+					value.os.len=(size_t)length[0];
+					CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
+					CHECK_FCT( fd_msg_avp_add ( rootavp, MSG_BRW_LAST_CHILD, avp) );
+					
+					
+					
+					CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_User_Data_Contents, 0, &avp ) );
+					CHECK_MALLOC(value.os.data=malloc(length[1]*sizeof(unsigned char)));
+					memcpy(value.os.data,row[1],length[1]);
+					value.os.len=(size_t)length[1];
+					CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
+					CHECK_FCT( fd_msg_avp_add ( rootavp, MSG_BRW_LAST_CHILD, avp) );
+					
+					//We add SIP_User_Data to message
+					CHECK_FCT( fd_msg_avp_add ( message, MSG_BRW_LAST_CHILD, rootavp) );
+					not_found=0;
+				}
+				
+			}
+			
+			mysql_free_result(res);
+			free(query);
+			free(sipaor_pure);
+			break;
+			
+			default:
+				
+				//We must never go here, if so, we must stop diameter_sip
+				TRACE_DEBUG(INFO,"FATAL ERROR: the datasource is unknown, please check your config file!");
+				return 2;
+				
+				break;
+	}
+	
+	//0 if it was found
+	return not_found;
+	
+	
+	
+}
+
+int set_pending_flag(const unsigned char  *username, const size_t usernamelen)
+{
+	CHECK_PARAMS(username && usernamelen);
+	
+	
+	size_t querylen, usernamepurelen;
+	char *query, *username_pure;
+	
+	switch(as_conf->datasource)
+	{
+		//MySQL
+		case ASMYSQL:
+			//We allocate the double size of username because at worst it can be all quotes
+			username_pure=malloc(usernamelen*2+1);
+			//We purify username not to have forbidden characters
+			usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen);
+			
+			
+			
+			//We clear the flag "authentication pending"
+			querylen=SQL_SETFLAG_LEN + usernamepurelen;
+			query = malloc(querylen+2);
+			snprintf(query, querylen+1, SQL_SETFLAG, username_pure);
+			
+			if (mysql_query(conn, query)) 
+			{
+				TRACE_DEBUG(INFO,"Query %s failed", query);
+				free(query);
+				return 2;
+			}
+			
+			free(query);
+			break;
+	}	
+	return 0;
+}
+int clear_pending_flag(const unsigned char  *username, const size_t usernamelen)
+{
+	CHECK_PARAMS(username && usernamelen);
+	
+	
+	size_t querylen, usernamepurelen;
+	char *query, *username_pure;
+	
+	switch(as_conf->datasource)
+	{
+		//MySQL
+		case ASMYSQL:
+			//We allocate the double size of username because at worst it can be all quotes
+			username_pure=malloc(usernamelen*2+1);
+			//We purify username not to have forbidden characters
+			usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen);
+			
+			
+			
+			//We clear the flag "authentication pending"
+			querylen=SQL_CLEARFLAG_LEN + usernamepurelen;
+			query = malloc(querylen+2);
+			snprintf(query, querylen+1, SQL_CLEARFLAG, username_pure);
+			
+			if (mysql_query(conn, query)) 
+			{
+				TRACE_DEBUG(INFO,"Query %s failed", query);
+				free(query);
+				return 2;
+			}
+			
+			free(query);
+		break;
+	}	
+	return 0;
+}
+
+
+
+int set_sipserver_uri(const unsigned char  *username, const size_t usernamelen, const unsigned char *sipserver_uri,const size_t sipserverurilen)
+{
+	CHECK_PARAMS(username && usernamelen && sipserver_uri && sipserverurilen);
+	
+	
+	size_t querylen, usernamepurelen, sipserveruripurelen;
+	char *query, *username_pure, *sipserveruri_pure;
+	
+	switch(as_conf->datasource)
+	{
+		//MySQL
+		case ASMYSQL:
+			//We allocate the double size of username because at worst it can be all quotes
+			username_pure=malloc(usernamelen*2+1);
+			//We purify username not to have forbidden characters
+			usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen);
+			
+			//We allocate the double size of username because at worst it can be all quotes
+			sipserveruri_pure=malloc(sipserverurilen*2+1);
+			//We purify username not to have forbidden characters
+			sipserveruripurelen=mysql_real_escape_string(conn, sipserveruri_pure, (const char *)sipserver_uri, sipserverurilen);
+			
+			//We clear the flag "authentication pending"
+			querylen=SQL_SETSIPURI_LEN + usernamepurelen + sipserveruripurelen;
+			query = malloc(querylen+2);
+			snprintf(query, querylen+1, SQL_SETSIPURI, sipserveruri_pure,username_pure);
+			
+			if (mysql_query(conn, query)) 
+			{
+				TRACE_DEBUG(INFO,"Query %s failed", query);
+				free(query);
+				return 2;
+			}
+			
+			free(query);
+			break;
+	}	
+	return 0;
+}
+int remove_sipserver_uri(const unsigned char *sipserver_uri,const size_t sipserverurilen)
+{
+	CHECK_PARAMS(sipserver_uri && sipserverurilen);
+	
+
+	size_t querylen, sipserveruripurelen;
+	char *query, *sipserveruri_pure;
+	
+	switch(as_conf->datasource)
+	{
+		//MySQL
+		case ASMYSQL:
+			//We allocate the double size of username because at worst it can be all quotes
+			sipserveruri_pure=malloc(sipserverurilen*2+1);
+			//We purify username not to have forbidden characters
+			sipserveruripurelen=mysql_real_escape_string(conn, sipserveruri_pure, (const char *)sipserver_uri, sipserverurilen);
+			
+			//We clear the flag "authentication pending"
+			querylen=SQL_RMSIPURI_LEN + sipserveruripurelen;
+			query = malloc(querylen+2);
+			snprintf(query, querylen+1, SQL_RMSIPURI, sipserveruri_pure);
+			
+			if (mysql_query(conn, query)) 
+			{
+				TRACE_DEBUG(INFO,"Query %s failed", query);
+				free(query);
+				return 2;
+			}
+			
+			free(query);
+			break;
+	}	
+	return 0;
+}
+int set_real_sipserver_uri(const unsigned char  *username, const size_t usernamelen, const unsigned char *sipserver_uri,const size_t sipserverurilen)
+{
+	CHECK_PARAMS(username && usernamelen && sipserver_uri && sipserverurilen);
+	
+	
+	size_t querylen, usernamepurelen, sipserveruripurelen;
+	char *query, *username_pure, *sipserveruri_pure;
+	
+	switch(as_conf->datasource)
+	{
+		//MySQL
+		case ASMYSQL:
+			//We allocate the double size of username because at worst it can be all quotes
+			username_pure=malloc(usernamelen*2+1);
+			//We purify username not to have forbidden characters
+			usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen);
+			
+			//We allocate the double size of username because at worst it can be all quotes
+			sipserveruri_pure=malloc(sipserverurilen*2+1);
+			//We purify username not to have forbidden characters
+			sipserveruripurelen=mysql_real_escape_string(conn, sipserveruri_pure, (const char *)sipserver_uri, sipserverurilen);
+			
+			//We clear the flag "authentication pending"
+			querylen=SQL_SETREALSIPURI_LEN + usernamepurelen + sipserveruripurelen;
+			query = malloc(querylen+2);
+			snprintf(query, querylen+1, SQL_SETREALSIPURI, sipserveruri_pure,username_pure);
+			
+			if (mysql_query(conn, query)) 
+			{
+				TRACE_DEBUG(INFO,"Query %s failed", query);
+				free(query);
+				return 2;
+			}
+			
+			free(query);
+			break;
+	}	
+	return 0;
+}
+
+int get_sipserver_uri(const unsigned char *sip_aor, const size_t sipaorlen, unsigned char ** sipserver_uri, size_t *sipserverurilen)
+{
+	CHECK_PARAMS(sip_aor && sipaorlen && sipserver_uri && sipserverurilen );
+	
+	size_t querylen, sipaorpurelen;
+	char *query, *sipaor_pure;
+	int not_found=2;
+	
+	
+	
+	
+	//a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars
+	if(sipaorlen<5)
+		return 2;
+	
+	//NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security
+	
+	switch(as_conf->datasource)
+	{
+		//MySQL
+		case ASMYSQL:
+			
+			querylen=SQL_GETSIPSERURI_LEN + sipaorlen;
+			
+			//We allocate the double size of SIP-URI because at worst it can be all quotes
+			CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1));
+			//We purify SIP-URI not to have forbidden characters
+			sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen);
+			
+			
+			query = malloc(querylen+sipaorpurelen+ 2);
+			snprintf(query, querylen+1, SQL_GETSIPSERURI, sipaor_pure);
+			
+			MYSQL_RES *res;
+			MYSQL_ROW row;
+			
+			//We make the query	
+			request_mysql(query);
+			res=mysql_use_result(conn);
+			if(res==NULL)
+			{
+				//We couldn't make the request
+				sipserver_uri=NULL;
+				return 2;
+			}
+			
+			not_found=1;
+			while ((row = mysql_fetch_row(res)) != NULL)
+			{
+				*sipserverurilen=strlen(row[0]);
+				if(*sipserverurilen>4)
+				{
+					CHECK_MALLOC(*sipserver_uri=malloc(*sipserverurilen+1));
+					strcpy((char *)*sipserver_uri,row[0]);
+					not_found=0;
+					break;
+				}
+			}
+			mysql_free_result(res);
+			free(query);
+			free(sipaor_pure);
+			break;
+			
+		default:
+			
+			//We must never go here, if so, we must stop diameter_sip
+			TRACE_DEBUG(INFO,"FATAL ERROR: the datasource is unknown, please check your config file!");
+			sipserver_uri=NULL;
+			return 2;
+			
+			break;
+	}
+	
+	//0 if it was found
+	return not_found;
+	
+}
+
+
+int count_avp(struct msg * message, int code, int vendor)
+{
+	CHECK_PARAMS(message);
+	
+	struct avp_hdr *temphdr;
+	struct avp *avp;
+	int counter=0;
+	
+	CHECK_FCT(fd_msg_browse (message, MSG_BRW_WALK, &avp, NULL));
+	
+	while(avp!=NULL)
+	{
+		
+		CHECK_FCT( fd_msg_avp_hdr( avp,&temphdr ));
+		
+		if(temphdr->avp_code==code && temphdr->avp_vendor==vendor)
+		{
+			counter++;
+		}
+		
+		CHECK_FCT(fd_msg_browse (avp, MSG_BRW_WALK, &avp, NULL));
+	}
+	return counter;
+}
+/* 
+void nonce_add_element(char * nonce)
+{
+	noncechain *newelt=malloc(sizeof(noncechain));
+	
+	newelt->nonce=nonce;
+	
+	newelt->timestamp=(int)time(NULL);
+	newelt->next=NULL;
+	
+	if(listnonce==NULL)
+	{
+		listnonce=newelt;
+	}
+	else
+	{
+		noncechain* temp=listnonce;
+		
+		while(temp->next != NULL)
+		{
+			if(temp->timestamp < ((int)time(NULL)-300))
+			{
+				listnonce=temp->next;
+				free(temp);
+				temp=listnonce;
+			}
+			temp = temp->next;
+		}
+		temp->next = newelt;
+	}
+	
+}
+void nonce_del_element(char * nonce)
+{
+	if(listnonce!=NULL)
+	{
+		noncechain *temp=listnonce, *tempbefore=NULL;
+		
+		if(listnonce->next==NULL && strcmp(listnonce->nonce,nonce)==0)
+		{
+			free(listnonce);
+			listnonce=NULL;
+			return;
+		}
+		while(temp->next != NULL)
+		{
+			if(strcmp(temp->nonce,nonce)==0)
+			{
+				if(tempbefore==NULL)
+				{
+					listnonce=temp->next;
+					free(temp);
+					return;
+				}
+				tempbefore->next=temp->next;
+				free(temp);
+				break;
+			}
+			tempbefore=temp;
+			temp = temp->next;
+		}
+		
+	}
+	
+}
+int nonce_check_element(char * nonce)
+{
+	if(listnonce==NULL)
+	{
+		//Not found
+		return 0;
+	}
+	else
+	{
+		noncechain* temp=listnonce;
+		
+		while(temp->next != NULL)
+		{
+			if(strcmp(temp->nonce,nonce)==0)
+				return 1;
+			else
+				temp = temp->next;
+		}
+	}
+	return 0;
+}
+
+void nonce_deletelistnonce()
+{
+	if(listnonce !=NULL)
+	{
+		noncechain* temp=listnonce;
+	
+		while(listnonce->next != NULL)
+		{
+			temp = listnonce->next;
+		
+			free(listnonce);
+		
+			listnonce=temp;
+		}
+		free(listnonce);
+	}
+}
+*/
Index: extensions/app_sip/libdiamsip.c
===================================================================
--- extensions/app_sip/libdiamsip.c	(revision 432)
+++ 	(revision )
@@ -1,1078 +1,0 @@
-/*********************************************************************************************************
-* Software License Agreement (BSD License)                                                               *
-* Author: Alexandre Westfahl <awestfahl@freediameter.net>						 *
-*													 *
-* Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. 	 *		
-*													 *
-* All rights reserved.											 *
-* 													 *
-* Redistribution and use of this software in source and binary forms, with or without modification, are  *
-* permitted provided that the following conditions are met:						 *
-* 													 *
-* * Redistributions of source code must retain the above 						 *
-*   copyright notice, this list of conditions and the 							 *
-*   following disclaimer.										 *
-*    													 *
-* * Redistributions in binary form must reproduce the above 						 *
-*   copyright notice, this list of conditions and the 							 *
-*   following disclaimer in the documentation and/or other						 *
-*   materials provided with the distribution.								 *
-* 													 *
-* * Neither the name of the Teraoka Laboratory nor the 							 *
-*   names of its contributors may be used to endorse or 						 *
-*   promote products derived from this software without 						 *
-*   specific prior written permission of Teraoka Laboratory 						 *
-*   													 *
-* 													 *
-* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
-* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
-* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
-* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
-* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
-* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
-* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
-* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
-*********************************************************************************************************/
-//#include <mysql.h>
-#include "diamsip.h"
-
-MYSQL *conn;
-
-void clear_digest(uint8_t * digest, char * readable_digest, int digestlength)
-{
-	int i=0;
-	for(i=0;i<digestlength * 2;i++)
-		sprintf(&readable_digest[2 * i], "%2.2hhx", digest[i]);
-	readable_digest[2 * digestlength]='\0';
-	
-	return;
-}
-
-// You must create a table like this "char  clearDigest[DIGEST_LEN*2+1];"
-void calc_md5(char *clearDigest, char * data)
-{
-	gcry_md_hd_t md5;
-	uint8_t * binDigest=NULL;
-	
-	CHECK_MALLOC_DO(binDigest=malloc(DIGEST_LEN),return);
-	
-	gcry_md_open(&md5,GCRY_MD_MD5, 0); 
-	gcry_md_write(md5, (char *)data, sizeof(data));
-	memcpy(binDigest, gcry_md_read(md5,  GCRY_MD_MD5),gcry_md_get_algo_dlen(GCRY_MD_MD5));
-	gcry_md_close(md5);
-	
-	clear_digest(binDigest, clearDigest, DIGEST_LEN);
-	free(binDigest);
-	return;
-}
-
-
-/* Search a given AVP model in an AVP (extracted from libfreediameter/message.c ) */
-int fd_avp_search_avp ( struct avp * groupedavp, struct dict_object * what, struct avp ** avp )
-{
-	struct avp * nextavp;
-	struct avp_hdr * nextavphdr;
-	struct dict_avp_data 	dictdata;
-	
-	
-	TRACE_ENTRY("%p %p %p", groupedavp, what, avp);
-	
-	CHECK_FCT(  fd_dict_getval(what, &dictdata)  );
-	
-	// Loop only in the group AVP 
-	CHECK_FCT(  fd_msg_browse(groupedavp, MSG_BRW_FIRST_CHILD, (void *)&nextavp, NULL)  );
-	CHECK_FCT( fd_msg_avp_hdr( nextavp, &nextavphdr )  );
-	
-	while (nextavphdr) {
-		
-		if ( (nextavphdr->avp_code   == dictdata.avp_code) && (nextavphdr->avp_vendor == dictdata.avp_vendor) ) // always 0 if no Vendor flag
-		{
-			break;
-		}
-		
-		// Otherwise move to next AVP in the grouped AVP 
-		CHECK_FCT( fd_msg_browse(nextavp, MSG_BRW_NEXT, (void *)&nextavp, NULL) );
-		
-		if(nextavp!=NULL)
-		{
-			CHECK_FCT( fd_msg_avp_hdr( nextavp, &nextavphdr )  );
-		}
-		else
-			nextavphdr=NULL;
-	}
-	if (avp)
-		*avp = nextavp;
-	
-	if (avp && nextavp) {
-		struct dictionary * dict;
-		CHECK_FCT( fd_dict_getdict( what, &dict) );
-		CHECK_FCT_DO( fd_msg_parse_dict( nextavp, dict, NULL ),  );
-	}
-	
-	if (avp || nextavp)
-		return 0;
-	else
-		return ENOENT;
-}
-struct avp_hdr *walk_digest(struct avp *avp, int avp_code)
-{
-	struct avp_hdr *temphdr=NULL;
-	CHECK_FCT_DO(fd_msg_browse ( avp, MSG_BRW_WALK, &avp, NULL),return NULL);
-	
-	while(avp!=NULL)
-	{
-		
-		CHECK_FCT_DO( fd_msg_avp_hdr( avp,&temphdr ),return NULL);
-
-		if(temphdr->avp_code==avp_code)
-		{
-			//We found the AVP so we set avp to NULL to exit the loop
-			avp=NULL;
-			return temphdr;
-			
-		}
-		else if(temphdr->avp_code==380)//SIP-Authorization AVP
-		{
-			//We didn't found the AVP but we finished browsing the Authentication AVP
-			avp=NULL;
-			temphdr=NULL;
-			
-			return temphdr;
-		}
-		else
-		{
-			CHECK_FCT_DO(fd_msg_browse ( avp, MSG_BRW_WALK, &avp, NULL),return NULL);
-			temphdr=NULL;
-			
-		}
-	}
-	
-	return temphdr;
-}
-
-int start_mysql_connection()
-{
-	conn = mysql_init(NULL);
-	
-	mysql_options(conn, MYSQL_OPT_RECONNECT, "true");
-	
-	if (!mysql_real_connect(conn, as_conf->mysql_server,as_conf->mysql_login, as_conf->mysql_password, as_conf->mysql_database, as_conf->mysql_port, NULL, 0)) 
-	{//TODO: display error from mysql
-		TRACE_DEBUG(INFO,"Unable to connect to database (%s) with login:%s",as_conf->mysql_database,as_conf->mysql_login);
-		return 1;
-	}
-	return 0;
-	
-}
-
-
-void request_mysql(char *query)
-{
-	//We check if the connection is still up
-	mysql_ping(conn);
-	
-	if (mysql_query(conn, query)) 
-	{
-		TRACE_DEBUG(INFO,"Query %s failed", query);
-		
-	}
-	
-}
-
-void close_mysql_connection()
-{
-	mysql_close(conn);
-	
-}
-
-//If password is null, we just verify this user exist
-//We don't need the password length because it is a table'
-int get_password(const unsigned char *username, const size_t usernamelen, char *password)
-{
-	CHECK_PARAMS(username && usernamelen);
-	
-	int not_found=2;
-	size_t querylen, usernamepurelen;
-	char *query, *username_pure;
-	
-	switch(as_conf->datasource)
-	{
-		//MySQL
-		case ASMYSQL:
-			//We allocate the double size of username because at worst it can be all quotes
-			username_pure=malloc(usernamelen*2+1);
-			//We purify username not to have forbidden characters
-			usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen);
-			
-			//We copy username in query
-			querylen=SQL_GETPASSWORD_LEN + usernamepurelen;
-			query = malloc(querylen+2);
-			snprintf(query, querylen+1, SQL_GETPASSWORD, username_pure);
-			
-			MYSQL_RES *res;
-			MYSQL_ROW row;
-			
-			//We make the query	
-			request_mysql(query);
-			res=mysql_use_result(conn);
-			if(res==NULL)
-			{
-				if(password!=NULL)
-					password[0]='\0';
-				free(query);
-				return 2;
-			}
-			
-			
-			
-			while ((row = mysql_fetch_row(res)) != NULL)
-			{
-				if(strlen(row[0])>0)
-				{
-					if(password!=NULL)
-						strcpy(password,row[0]);
-					
-					not_found=0;
-					break;
-				}
-			}
-			mysql_free_result(res);
-			free(query);
-		break;
-	}
-	return not_found;
-}
-
-int check_sipaor(const unsigned char  *username, const size_t usernamelen, const char * sip_aor,const size_t sipaorlen)
-{
-	CHECK_PARAMS(username && usernamelen && sip_aor && sipaorlen);
-	
-	int not_found=2;
-	size_t querylen, usernamepurelen;
-	char *query, *username_pure;
-	
-	switch(as_conf->datasource)
-	{
-		//MySQL
-		case ASMYSQL:
-			//We allocate the double size of username because at worst it can be all quotes
-			username_pure=malloc(usernamelen*2+1);
-			//We purify username not to have forbidden characters
-			usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen);
-	
-			//We get the list of AOR owned by this user
-			querylen=SQL_GETSIPAOR_LEN + usernamepurelen;
-			query = malloc(querylen+2);
-			snprintf(query, querylen+1, SQL_GETSIPAOR, username_pure);
-			
-			MYSQL_RES *res;
-			MYSQL_ROW row;
-			
-			//We make the query
-			request_mysql(query);
-			res=mysql_use_result(conn);
-			if(res==NULL)
-			{
-				free(query);
-				return 2;
-			}
-			
-			
-			not_found=1;
-			while ((row = mysql_fetch_row(res)) != NULL)
-			{
-				if(strncmp((const char *)sip_aor,row[0],sipaorlen)==0)
-				{
-					not_found=0;
-					break;
-				}
-			}
-			mysql_free_result(res);
-			free(query);
-		break;
-	}
-	return not_found;
-}
-
-int get_diameter_uri(const unsigned char *sip_aor, const size_t sipaorlen, char ** diameter_uri, size_t *diameterurilen)
-{
-	CHECK_PARAMS(sip_aor && sipaorlen);
-
-	size_t querylen, sipaorpurelen;
-	char *query, *sipaor_pure;
-	int not_found=2;
-
-
-
-
-	//a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars
-	if(sipaorlen<5)
-		return 2;
-
-	//NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security
-	switch(as_conf->datasource)
-	{
-		//MySQL
-		case ASMYSQL:
-
-			querylen=SQL_GETDIAMURI_LEN + sipaorlen;
-
-			
-			//We allocate the double size of SIP-URI because at worst it can be all quotes
-			CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1));
-			//We purify SIP-URI not to have forbidden characters
-			sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen);
-			
-			
-			query = malloc(querylen+sipaorpurelen+ 2);
-			snprintf(query, querylen+1, SQL_GETDIAMURI, sipaor_pure);
-			
-			MYSQL_RES *res;
-			MYSQL_ROW row;
-			
-
-			//We make the query	
-			request_mysql(query);
-			res=mysql_use_result(conn);
-			if(res==NULL)
-			{
-				//We couldn't make the request
-				diameter_uri=NULL;
-				return 2;
-			}
-			
-			while ((row = mysql_fetch_row(res)) != NULL)
-			{
-				*diameterurilen=strlen(row[0]);
-				if(*diameterurilen>0)
-				{
-					CHECK_MALLOC(*diameter_uri=malloc(*diameterurilen+1));
-					strcpy(*diameter_uri,row[0]);
-					not_found=0;
-					break;
-				}
-			}
-			mysql_free_result(res);
-			free(query);
-			free(sipaor_pure);
-		break;
-      
-    default:
-      
-      //We must never go here, if so, we must stop diameter_sip
-      diameter_uri=NULL;
-      return 2;
-      
-      break;
-  }
-  
-  //0 if it was found
-  return not_found;
-  
-}
-
-
-int exist_username(const unsigned char *sip_aor, const size_t sipaorlen)
-{
-	CHECK_PARAMS(sip_aor && sipaorlen);
-	
-	size_t querylen, sipaorpurelen;
-	char *query, *sipaor_pure;
-	int not_found=1;
-	
-	//a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars
-	if(sipaorlen<5)
-		return 2;
-	
-	//NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security
-	
-	switch(as_conf->datasource)
-	{
-		//MySQL
-		case ASMYSQL:
-			
-			querylen=SQL_GETUSERNAME_LEN + sipaorlen;
-			
-			
-			//We allocate the double size of SIP-URI because at worst it can be all quotes
-			CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1));
-			//We purify SIP-URI not to have forbidden characters
-			sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen);
-			
-			
-			query = malloc(querylen+sipaorpurelen+ 2);
-			snprintf(query, querylen+1, SQL_GETUSERNAME, sipaor_pure);
-			
-			MYSQL_RES *res;
-			MYSQL_ROW row;
-			
-			
-			//We make the query	
-			request_mysql(query);
-			res=mysql_use_result(conn);
-			if(res==NULL)
-			{
-				//We couldn't make the request
-				return 2;
-			}
-			
-			while ((row = mysql_fetch_row(res)) != NULL)
-			{
-				if(strlen(row[0])>0)
-				{
-					not_found=0;
-					break;
-				}
-			}
-			mysql_free_result(res);
-			free(query);
-			free(sipaor_pure);
-			break;
-			
-		default:
-			
-			//We must never go here, if so, we must stop diameter_sip
-			TRACE_DEBUG(INFO,"FATAL ERROR: the datasource is unknown, please check your config file!");
-			return 2;
-			
-			break;
-	}
-	
-	//0 if it was found
-	return not_found;
-	
-}
-
-//We check if this user can go in the given network
-int allow_roaming(const unsigned char  *username, const size_t usernamelen, const char * network,const size_t networklen)
-{
-	CHECK_PARAMS(username && usernamelen && network && networklen);
-	
-	int not_found=2;
-	size_t querylen, usernamepurelen;
-	char *query, *username_pure;
-	
-	switch(as_conf->datasource)
-	{
-		//MySQL
-		case ASMYSQL:
-			//We allocate the double size of username because at worst it can be all quotes
-			username_pure=malloc(usernamelen*2+1);
-			//We purify username not to have forbidden characters
-			usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen);
-			
-			//We get the list of AOR owned by this user
-			querylen=SQL_GETUSERNET_LEN + usernamepurelen;
-			query = malloc(querylen+2);
-			snprintf(query, querylen+1, SQL_GETUSERNET, username_pure);
-			
-			
-			MYSQL_RES *res;
-			MYSQL_ROW row;
-			
-			
-			//We make the query
-			request_mysql(query);
-			res=mysql_use_result(conn);
-			if(res==NULL)
-			{
-				free(query);
-				return 2;
-			}
-			
-			
-			not_found=1;
-			while ((row = mysql_fetch_row(res)) != NULL)
-			{
-				if(strncmp((const char *)network,row[0],networklen)==0)
-				{
-					not_found=0;
-					break;
-				}
-			}
-			mysql_free_result(res);
-			free(query);
-			break;
-	}
-	return not_found;
-}
-
-//SIP-Server-Capabilities for the SIP-AOR
-int get_sipserver_cap(const unsigned char *sip_aor, const size_t sipaorlen, struct avp **capabilities)
-{
-	CHECK_PARAMS(sip_aor && sipaorlen && capabilities);
-	
-	size_t querylen, sipaorpurelen;
-	char *query, *sipaor_pure;
-	int not_found=2;
-	union avp_value value;
-	struct avp *avp;
-	
-	//a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars
-	if(sipaorlen<5)
-		return 2;
-	
-	//NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security
-	switch(as_conf->datasource)
-	{
-		//MySQL
-		case ASMYSQL:
-			
-			querylen=SQL_GETSIPSERCAP_LEN + sipaorlen;
-			
-			
-			//We allocate the double size of SIP-URI because at worst it can be all quotes
-			CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1));
-			//We purify SIP-URI not to have forbidden characters
-			sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen);
-			
-			
-			query = malloc(querylen+sipaorpurelen+ 2);
-			snprintf(query, querylen+1, SQL_GETSIPSERCAP, sipaor_pure);
-			
-			MYSQL_RES *res;
-			MYSQL_ROW row;
-			
-			//We make the query	
-			request_mysql(query);
-			res=mysql_use_result(conn);
-			if(res==NULL)
-			{
-				//We couldn't make the request
-				return 2;
-			}
-			not_found=1;
-			while ((row = mysql_fetch_row(res)) != NULL)
-			{
-				if(atoi(row[0])==1)
-				{//mandatory
-					CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_Mandatory_Capability, 0, &avp ) );
-					value.i32=(uint32_t)atoi(row[1]);
-					CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
-					CHECK_FCT( fd_msg_avp_add ( *capabilities, MSG_BRW_LAST_CHILD, avp) );
-					
-				}
-				else
-				{//optional
-					CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_Optional_Capability, 0, &avp ) );
-					value.i32=(uint32_t)atoi(row[1]);
-					CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
-					CHECK_FCT( fd_msg_avp_add ( *capabilities, MSG_BRW_LAST_CHILD, avp) );
-				}
-				not_found=0;
-			}
-			
-			mysql_free_result(res);
-			free(query);
-			free(sipaor_pure);
-			break;
-			
-		default:
-			
-			//We must never go here, if so, we must stop diameter_sip
-			TRACE_DEBUG(INFO,"FATAL ERROR: the datasource is unknown, please check your config file!");
-			return 2;
-			
-			break;
-	}
-	
-	//0 if it was found
-	return not_found;
-	
-}
-
-
-//We retrieve datatype
-int add_user_datatype(const unsigned char  *sip_aor, const size_t sipaorlen,struct msg *message)
-{
-	CHECK_PARAMS(sip_aor && sipaorlen && message );
-	
-	size_t querylen, sipaorpurelen;
-	char *query, *sipaor_pure;
-	int not_found=2;
-	union avp_value value;
-	struct avp *avp, *rootavp;
-	unsigned long *length;
-	
-	//a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars
-	if(sipaorlen<5)
-		return 2;
-	
-	//NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security
-	switch(as_conf->datasource)
-	{
-		//MySQL
-		case ASMYSQL:
-			
-			querylen=SQL_GETSIPDATA_LEN + sipaorlen;
-			
-			
-			//We allocate the double size of SIP-URI because at worst it can be all quotes
-			CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1));
-			//We purify SIP-URI not to have forbidden characters
-			sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen);
-			
-			
-			query = malloc(querylen+sipaorpurelen+ 2);
-			snprintf(query, querylen+1, SQL_GETSIPDATA, sipaor_pure);
-			
-			MYSQL_RES *res;
-			MYSQL_ROW row;
-			
-			//We make the query	
-			request_mysql(query);
-			res=mysql_use_result(conn);
-			if(res==NULL)
-			{
-				//We couldn't make the request
-				return 2;
-			}
-			not_found=1;
-			while ((row = mysql_fetch_row(res)) != NULL)
-			{
-				length=mysql_fetch_lengths(res);
-				
-				if(strlen(row[0])>1)
-				{
-					CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_User_Data, 0, &rootavp ) );
-					
-					CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_User_Data_Type, 0, &avp ) );
-					CHECK_MALLOC(value.os.data=malloc(length[0]*sizeof(unsigned char)));
-					strncpy((char *)value.os.data,(char *)row[0],length[0]);
-					value.os.len=(size_t)length[0];
-					CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
-					CHECK_FCT( fd_msg_avp_add ( rootavp, MSG_BRW_LAST_CHILD, avp) );
-					
-					
-					
-					CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_User_Data_Contents, 0, &avp ) );
-					CHECK_MALLOC(value.os.data=malloc(length[1]*sizeof(unsigned char)));
-					memcpy(value.os.data,row[1],length[1]);
-					value.os.len=(size_t)length[1];
-					CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
-					CHECK_FCT( fd_msg_avp_add ( rootavp, MSG_BRW_LAST_CHILD, avp) );
-					
-					//We add SIP_User_Data to message
-					CHECK_FCT( fd_msg_avp_add ( message, MSG_BRW_LAST_CHILD, rootavp) );
-					not_found=0;
-				}
-				
-			}
-			
-			mysql_free_result(res);
-			free(query);
-			free(sipaor_pure);
-			break;
-			
-			default:
-				
-				//We must never go here, if so, we must stop diameter_sip
-				TRACE_DEBUG(INFO,"FATAL ERROR: the datasource is unknown, please check your config file!");
-				return 2;
-				
-				break;
-	}
-	
-	//0 if it was found
-	return not_found;
-	
-	
-	
-}
-
-int set_pending_flag(const unsigned char  *username, const size_t usernamelen)
-{
-	CHECK_PARAMS(username && usernamelen);
-	
-	
-	size_t querylen, usernamepurelen;
-	char *query, *username_pure;
-	
-	switch(as_conf->datasource)
-	{
-		//MySQL
-		case ASMYSQL:
-			//We allocate the double size of username because at worst it can be all quotes
-			username_pure=malloc(usernamelen*2+1);
-			//We purify username not to have forbidden characters
-			usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen);
-			
-			
-			
-			//We clear the flag "authentication pending"
-			querylen=SQL_SETFLAG_LEN + usernamepurelen;
-			query = malloc(querylen+2);
-			snprintf(query, querylen+1, SQL_SETFLAG, username_pure);
-			
-			if (mysql_query(conn, query)) 
-			{
-				TRACE_DEBUG(INFO,"Query %s failed", query);
-				free(query);
-				return 2;
-			}
-			
-			free(query);
-			break;
-	}	
-	return 0;
-}
-int clear_pending_flag(const unsigned char  *username, const size_t usernamelen)
-{
-	CHECK_PARAMS(username && usernamelen);
-	
-	
-	size_t querylen, usernamepurelen;
-	char *query, *username_pure;
-	
-	switch(as_conf->datasource)
-	{
-		//MySQL
-		case ASMYSQL:
-			//We allocate the double size of username because at worst it can be all quotes
-			username_pure=malloc(usernamelen*2+1);
-			//We purify username not to have forbidden characters
-			usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen);
-			
-			
-			
-			//We clear the flag "authentication pending"
-			querylen=SQL_CLEARFLAG_LEN + usernamepurelen;
-			query = malloc(querylen+2);
-			snprintf(query, querylen+1, SQL_CLEARFLAG, username_pure);
-			
-			if (mysql_query(conn, query)) 
-			{
-				TRACE_DEBUG(INFO,"Query %s failed", query);
-				free(query);
-				return 2;
-			}
-			
-			free(query);
-		break;
-	}	
-	return 0;
-}
-
-
-
-int set_sipserver_uri(const unsigned char  *username, const size_t usernamelen, const unsigned char *sipserver_uri,const size_t sipserverurilen)
-{
-	CHECK_PARAMS(username && usernamelen && sipserver_uri && sipserverurilen);
-	
-	
-	size_t querylen, usernamepurelen, sipserveruripurelen;
-	char *query, *username_pure, *sipserveruri_pure;
-	
-	switch(as_conf->datasource)
-	{
-		//MySQL
-		case ASMYSQL:
-			//We allocate the double size of username because at worst it can be all quotes
-			username_pure=malloc(usernamelen*2+1);
-			//We purify username not to have forbidden characters
-			usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen);
-			
-			//We allocate the double size of username because at worst it can be all quotes
-			sipserveruri_pure=malloc(sipserverurilen*2+1);
-			//We purify username not to have forbidden characters
-			sipserveruripurelen=mysql_real_escape_string(conn, sipserveruri_pure, (const char *)sipserver_uri, sipserverurilen);
-			
-			//We clear the flag "authentication pending"
-			querylen=SQL_SETSIPURI_LEN + usernamepurelen + sipserveruripurelen;
-			query = malloc(querylen+2);
-			snprintf(query, querylen+1, SQL_SETSIPURI, sipserveruri_pure,username_pure);
-			
-			if (mysql_query(conn, query)) 
-			{
-				TRACE_DEBUG(INFO,"Query %s failed", query);
-				free(query);
-				return 2;
-			}
-			
-			free(query);
-			break;
-	}	
-	return 0;
-}
-int remove_sipserver_uri(const unsigned char *sipserver_uri,const size_t sipserverurilen)
-{
-	CHECK_PARAMS(sipserver_uri && sipserverurilen);
-	
-
-	size_t querylen, sipserveruripurelen;
-	char *query, *sipserveruri_pure;
-	
-	switch(as_conf->datasource)
-	{
-		//MySQL
-		case ASMYSQL:
-			//We allocate the double size of username because at worst it can be all quotes
-			sipserveruri_pure=malloc(sipserverurilen*2+1);
-			//We purify username not to have forbidden characters
-			sipserveruripurelen=mysql_real_escape_string(conn, sipserveruri_pure, (const char *)sipserver_uri, sipserverurilen);
-			
-			//We clear the flag "authentication pending"
-			querylen=SQL_RMSIPURI_LEN + sipserveruripurelen;
-			query = malloc(querylen+2);
-			snprintf(query, querylen+1, SQL_RMSIPURI, sipserveruri_pure);
-			
-			if (mysql_query(conn, query)) 
-			{
-				TRACE_DEBUG(INFO,"Query %s failed", query);
-				free(query);
-				return 2;
-			}
-			
-			free(query);
-			break;
-	}	
-	return 0;
-}
-int set_real_sipserver_uri(const unsigned char  *username, const size_t usernamelen, const unsigned char *sipserver_uri,const size_t sipserverurilen)
-{
-	CHECK_PARAMS(username && usernamelen && sipserver_uri && sipserverurilen);
-	
-	
-	size_t querylen, usernamepurelen, sipserveruripurelen;
-	char *query, *username_pure, *sipserveruri_pure;
-	
-	switch(as_conf->datasource)
-	{
-		//MySQL
-		case ASMYSQL:
-			//We allocate the double size of username because at worst it can be all quotes
-			username_pure=malloc(usernamelen*2+1);
-			//We purify username not to have forbidden characters
-			usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen);
-			
-			//We allocate the double size of username because at worst it can be all quotes
-			sipserveruri_pure=malloc(sipserverurilen*2+1);
-			//We purify username not to have forbidden characters
-			sipserveruripurelen=mysql_real_escape_string(conn, sipserveruri_pure, (const char *)sipserver_uri, sipserverurilen);
-			
-			//We clear the flag "authentication pending"
-			querylen=SQL_SETREALSIPURI_LEN + usernamepurelen + sipserveruripurelen;
-			query = malloc(querylen+2);
-			snprintf(query, querylen+1, SQL_SETREALSIPURI, sipserveruri_pure,username_pure);
-			
-			if (mysql_query(conn, query)) 
-			{
-				TRACE_DEBUG(INFO,"Query %s failed", query);
-				free(query);
-				return 2;
-			}
-			
-			free(query);
-			break;
-	}	
-	return 0;
-}
-
-int get_sipserver_uri(const unsigned char *sip_aor, const size_t sipaorlen, unsigned char ** sipserver_uri, size_t *sipserverurilen)
-{
-	CHECK_PARAMS(sip_aor && sipaorlen && sipserver_uri && sipserverurilen );
-	
-	size_t querylen, sipaorpurelen;
-	char *query, *sipaor_pure;
-	int not_found=2;
-	
-	
-	
-	
-	//a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars
-	if(sipaorlen<5)
-		return 2;
-	
-	//NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security
-	
-	switch(as_conf->datasource)
-	{
-		//MySQL
-		case ASMYSQL:
-			
-			querylen=SQL_GETSIPSERURI_LEN + sipaorlen;
-			
-			//We allocate the double size of SIP-URI because at worst it can be all quotes
-			CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1));
-			//We purify SIP-URI not to have forbidden characters
-			sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen);
-			
-			
-			query = malloc(querylen+sipaorpurelen+ 2);
-			snprintf(query, querylen+1, SQL_GETSIPSERURI, sipaor_pure);
-			
-			MYSQL_RES *res;
-			MYSQL_ROW row;
-			
-			//We make the query	
-			request_mysql(query);
-			res=mysql_use_result(conn);
-			if(res==NULL)
-			{
-				//We couldn't make the request
-				sipserver_uri=NULL;
-				return 2;
-			}
-			
-			not_found=1;
-			while ((row = mysql_fetch_row(res)) != NULL)
-			{
-				*sipserverurilen=strlen(row[0]);
-				if(*sipserverurilen>4)
-				{
-					CHECK_MALLOC(*sipserver_uri=malloc(*sipserverurilen+1));
-					strcpy((char *)*sipserver_uri,row[0]);
-					not_found=0;
-					break;
-				}
-			}
-			mysql_free_result(res);
-			free(query);
-			free(sipaor_pure);
-			break;
-			
-		default:
-			
-			//We must never go here, if so, we must stop diameter_sip
-			TRACE_DEBUG(INFO,"FATAL ERROR: the datasource is unknown, please check your config file!");
-			sipserver_uri=NULL;
-			return 2;
-			
-			break;
-	}
-	
-	//0 if it was found
-	return not_found;
-	
-}
-
-
-int count_avp(struct msg * message, int code, int vendor)
-{
-	CHECK_PARAMS(message);
-	
-	struct avp_hdr *temphdr;
-	struct avp *avp;
-	int counter=0;
-	
-	CHECK_FCT(fd_msg_browse (message, MSG_BRW_WALK, &avp, NULL));
-	
-	while(avp!=NULL)
-	{
-		
-		CHECK_FCT( fd_msg_avp_hdr( avp,&temphdr ));
-		
-		if(temphdr->avp_code==code && temphdr->avp_vendor==vendor)
-		{
-			counter++;
-		}
-		
-		CHECK_FCT(fd_msg_browse (avp, MSG_BRW_WALK, &avp, NULL));
-	}
-	return counter;
-}
-/* 
-void nonce_add_element(char * nonce)
-{
-	noncechain *newelt=malloc(sizeof(noncechain));
-	
-	newelt->nonce=nonce;
-	
-	newelt->timestamp=(int)time(NULL);
-	newelt->next=NULL;
-	
-	if(listnonce==NULL)
-	{
-		listnonce=newelt;
-	}
-	else
-	{
-		noncechain* temp=listnonce;
-		
-		while(temp->next != NULL)
-		{
-			if(temp->timestamp < ((int)time(NULL)-300))
-			{
-				listnonce=temp->next;
-				free(temp);
-				temp=listnonce;
-			}
-			temp = temp->next;
-		}
-		temp->next = newelt;
-	}
-	
-}
-void nonce_del_element(char * nonce)
-{
-	if(listnonce!=NULL)
-	{
-		noncechain *temp=listnonce, *tempbefore=NULL;
-		
-		if(listnonce->next==NULL && strcmp(listnonce->nonce,nonce)==0)
-		{
-			free(listnonce);
-			listnonce=NULL;
-			return;
-		}
-		while(temp->next != NULL)
-		{
-			if(strcmp(temp->nonce,nonce)==0)
-			{
-				if(tempbefore==NULL)
-				{
-					listnonce=temp->next;
-					free(temp);
-					return;
-				}
-				tempbefore->next=temp->next;
-				free(temp);
-				break;
-			}
-			tempbefore=temp;
-			temp = temp->next;
-		}
-		
-	}
-	
-}
-int nonce_check_element(char * nonce)
-{
-	if(listnonce==NULL)
-	{
-		//Not found
-		return 0;
-	}
-	else
-	{
-		noncechain* temp=listnonce;
-		
-		while(temp->next != NULL)
-		{
-			if(strcmp(temp->nonce,nonce)==0)
-				return 1;
-			else
-				temp = temp->next;
-		}
-	}
-	return 0;
-}
-
-void nonce_deletelistnonce()
-{
-	if(listnonce !=NULL)
-	{
-		noncechain* temp=listnonce;
-	
-		while(listnonce->next != NULL)
-		{
-			temp = listnonce->next;
-		
-			free(listnonce);
-		
-			listnonce=temp;
-		}
-		free(listnonce);
-	}
-}
-*/
Index: extensions/app_sip/locationinfo.c
===================================================================
--- extensions/app_sip/locationinfo.c	(revision 430)
+++ extensions/app_sip/locationinfo.c	(revision 433)
@@ -34,8 +34,8 @@
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
 *********************************************************************************************************/
-#include "diamsip.h"
+#include "app_sip.h"
 
 
-int diamsip_LIR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act)
+int app_sip_LIR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act)
 {
 	TRACE_ENTRY("%p %p %p %p", msg, paramavp, sess, act);
Index: extensions/app_sip/locationinfosl.c
===================================================================
--- extensions/app_sip/locationinfosl.c	(revision 430)
+++ extensions/app_sip/locationinfosl.c	(revision 433)
@@ -34,8 +34,8 @@
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
 *********************************************************************************************************/
-#include "diamsip.h"
+#include "app_sip.h"
 
 //This callback is specific to SUSCRIBER LOCATOR. We must look for the "serving" SIP server
-int diamsipSL_LIR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act)
+int app_sip_SL_LIR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act)
 {
 	TRACE_ENTRY("%p %p %p %p", msg, paramavp, sess, act);
Index: extensions/app_sip/md5.c
===================================================================
--- extensions/app_sip/md5.c	(revision 409)
+++ extensions/app_sip/md5.c	(revision 433)
@@ -8,5 +8,5 @@
 
 /*********************************************************************************/
-#include"diamsip.h"
+#include"app_sip.h"
  
 /*
Index: extensions/app_sip/multimediaauth.c
===================================================================
--- extensions/app_sip/multimediaauth.c	(revision 430)
+++ extensions/app_sip/multimediaauth.c	(revision 433)
@@ -34,8 +34,8 @@
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
 *********************************************************************************************************/
-#include "diamsip.h"
-
-
-int diamsip_MAR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act)
+#include "app_sip.h"
+
+
+int app_sip_MAR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act)
 {
 	struct msg *ans, *qry;
Index: extensions/app_sip/pushprofile.c
===================================================================
--- extensions/app_sip/pushprofile.c	(revision 420)
+++ extensions/app_sip/pushprofile.c	(revision 433)
@@ -34,5 +34,5 @@
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
 *********************************************************************************************************/
-#include "diamsip.h"
+#include "app_sip.h"
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -77,5 +77,5 @@
 				{
 					//We received something, we can send an PPR
-					diamsip_PPR_cb(pprsip);
+					app_sip_PPR_cb(pprsip);
 				}
 			}
@@ -92,5 +92,5 @@
 }
 //Called to send a PPR
-int diamsip_PPR_cb(struct pprsipaor structure)
+int app_sip_PPR_cb(struct pprsipaor structure)
 {
 	/*
@@ -255,5 +255,5 @@
 
 //Called when an PPA arrive
-int diamsip_PPA_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act)
+int app_sip_PPA_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act)
 {
 	//TODO: PPA reception
Index: extensions/app_sip/registrationtermination.c
===================================================================
--- extensions/app_sip/registrationtermination.c	(revision 420)
+++ extensions/app_sip/registrationtermination.c	(revision 433)
@@ -34,5 +34,5 @@
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
 *********************************************************************************************************/
-#include "diamsip.h"
+#include "app_sip.h"
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -75,5 +75,5 @@
 				{
 					//We received something, we can send an RTR
-					diamsip_RTR_cb(rtrsip);
+					app_sip_RTR_cb(rtrsip);
 				}
 			}
@@ -88,5 +88,5 @@
 }
 //Called to send a RTR
-int diamsip_RTR_cb(struct rtrsipaor structure)
+int app_sip_RTR_cb(struct rtrsipaor structure)
 {
 	TRACE_ENTRY("%p", structure);
@@ -258,5 +258,5 @@
 
 //Called when an RTA arrive
-int diamsip_RTA_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act)
+int app_sip_RTA_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act)
 {
 	//TODO: RTA reception
Index: extensions/app_sip/serverassignment.c
===================================================================
--- extensions/app_sip/serverassignment.c	(revision 432)
+++ extensions/app_sip/serverassignment.c	(revision 433)
@@ -34,8 +34,8 @@
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
 *********************************************************************************************************/
-#include "diamsip.h"
+#include "app_sip.h"
 
 
-int diamsip_SAR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act)
+int app_sip_SAR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act)
 {
 	TRACE_ENTRY("%p %p %p %p", msg, paramavp, sess, act);
Index: extensions/app_sip/tools/app_sip_rtr.c
===================================================================
--- extensions/app_sip/tools/app_sip_rtr.c	(revision 433)
+++ extensions/app_sip/tools/app_sip_rtr.c	(revision 433)
@@ -0,0 +1,290 @@
+/*********************************************************************************************************
+* Software License Agreement (BSD License)                                                               *
+* Author: Alexandre Westfahl <awestfahl@freediameter.net>						 *
+*													 *
+* Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. 	 *		
+*													 *
+* All rights reserved.											 *
+* 													 *
+* Redistribution and use of this software in source and binary forms, with or without modification, are  *
+* permitted provided that the following conditions are met:						 *
+* 													 *
+* * Redistributions of source code must retain the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer.										 *
+*    													 *
+* * Redistributions in binary form must reproduce the above 						 *
+*   copyright notice, this list of conditions and the 							 *
+*   following disclaimer in the documentation and/or other						 *
+*   materials provided with the distribution.								 *
+* 													 *
+* * Neither the name of the Teraoka Laboratory nor the 							 *
+*   names of its contributors may be used to endorse or 						 *
+*   promote products derived from this software without 						 *
+*   specific prior written permission of Teraoka Laboratory 						 *
+*   													 *
+* 													 *
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
+* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
+* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
+* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
+*********************************************************************************************************/
+
+
+// This file is separated from the source code because it is a separate command which will call registration termination function in Diameter-SIP
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+typedef int SOCKET;
+typedef struct sockaddr_in SOCKADDR_IN;
+typedef struct sockaddr SOCKADDR;
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#define PORT 666
+#include <errno.h>
+
+struct rtrsipaor
+{
+	char username[200];
+	char sip_aor1[200];
+	char sip_aor2[200];
+	char sip_aor3[200];
+	char strreason[200];
+	char desthost[200];
+	int reason;
+};
+
+int main (int argc, char **argv)
+{
+	SOCKET sock;
+    SOCKADDR_IN sin;
+    struct rtrsipaor rtrsip;
+    int numaor=0,i=0;
+    
+	sock = socket(AF_INET, SOCK_STREAM, 0);
+	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
+    sin.sin_family = AF_INET;
+    sin.sin_port = htons(PORT);
+    
+    //We initialize the structure
+    rtrsip.username[0]='\0';
+    rtrsip.sip_aor1[0]='\0';
+    rtrsip.sip_aor2[0]='\0';
+    rtrsip.sip_aor3[0]='\0';
+    rtrsip.strreason[0]='\0';
+    rtrsip.desthost[0]='\0';
+    rtrsip.reason=-1;
+    
+    
+	//Start of arguments check
+	if(argc<3)
+	{
+		fprintf(stderr,"Missing arguments! You must at least provide a username.\n");
+		return 1;
+	}
+	
+	
+	for (i=1;i<argc;i++)
+	{
+		//We must check if it is a value or the name
+		if(strncmp(argv[i],"-",1)==0)
+		{
+			if(strcmp(argv[i],"-u")==0)
+			{
+				//Username
+				if(strlen(argv[i+1])<199)
+				{
+					strcpy(rtrsip.username,argv[i+1]);
+					//We must not check the value
+					i++;
+				}
+				else
+				{
+					fprintf(stderr,"Username is too long!\n");
+				}
+			}
+			else if(strcmp(argv[i],"-a")==0)
+			{
+				i++;
+				int j=i;
+			
+				for(j=i;j<argc;j++)
+				{
+					
+					if(strncmp(argv[i],"-",1)!=0)
+					{
+						if(strlen(argv[i])>199)
+						{
+							fprintf(stderr,"SIP-AOR is too long!\n");
+						}
+						else if(strncmp(argv[i],"sip",3)!=0)
+						{
+							//Bad format of SIP-AOR
+							fprintf(stderr,"A SIP-AOR must start by 'sip:' or 'sips:'. Aborting...\n");
+							return 1;
+						}
+						else
+						{
+							if(numaor<3)
+							{
+								switch(numaor)
+								{
+									case 0:
+										strcpy(rtrsip.sip_aor1,argv[i]);
+									break;
+									case 1:
+										strcpy(rtrsip.sip_aor2,argv[i]);
+									break;
+									case 2:
+										strcpy(rtrsip.sip_aor3,argv[i]);
+									break;
+								}
+								numaor++;
+							}
+							else
+							{
+								fprintf(stderr,"You can not provide more than 3 SIP-AOR at the same time!\n");
+								break;
+							}
+						}
+						i=j+1;
+					}
+					else
+					{
+						//We have a new argument
+						i--;
+						break;
+					}
+				}
+			}
+			else if(strcmp(argv[i],"-r")==0)
+			{
+				
+				if(strlen(argv[i+1])>199)
+				{
+					fprintf(stderr,"Deregistration reason is too long!\n");
+				}
+				else
+				{
+					strcpy(rtrsip.strreason,argv[i+1]);
+				}
+				i++;
+			}
+			else if(strcmp(argv[i],"-h")==0)
+			{
+				//Remote SIP Server
+				if(strlen(argv[i+1])>199)
+				{
+					fprintf(stderr,"Host is too long!\n");
+				}
+				else if(strlen(argv[i+1])<5)
+				{
+					fprintf(stderr,"Host is too short!\n");
+				}
+				else
+				{
+					strcpy(rtrsip.desthost,argv[i+1]);
+				}
+				i++;
+			}
+			else if(strcmp(argv[i],"-pt")==0)
+			{
+				//Permanent Termination
+				rtrsip.reason=0;
+			}
+			else if(strcmp(argv[i],"-nssa")==0)
+			{
+				//New SIP Server Assigned
+				rtrsip.reason=1;
+			}
+			else if(strcmp(argv[i],"-ssc")==0)
+			{
+				//SIP Server Change
+				rtrsip.reason=2;
+			}
+			else if(strcmp(argv[i],"-rss")==0)
+			{
+				//Remote SIP Server
+				rtrsip.reason=3;
+			}
+			else
+			{
+				fprintf(stderr,"Unknown argument: %s\n",argv[i]);
+			}
+		}
+		else
+		{
+			fprintf(stderr,"Unknown argument: %s\n",argv[i]);
+			i++;
+		}
+			
+	}
+	
+	//If no SIP-AOR provided, we remove all
+	if(numaor<1)
+	{
+		fprintf(stderr,"All SIP-AOR of %s will be deregistrated.\n",rtrsip.username);
+	}
+	
+	//We want a username
+	if(strlen(rtrsip.username)==0)
+	{
+		fprintf(stderr,"You must provide a username!\n");
+		return 1;
+	}
+	
+	if(rtrsip.desthost[0]=='\0')
+	{
+		fprintf(stderr,"You must provide the hostname of SIP-Server!\n");
+		return 1;
+	}
+    
+    
+   
+    
+    /*
+    fprintf(stderr,"*%s*\n",rtrsip.username);
+	fprintf(stderr,"*%s*\n",rtrsip.sip_aor1);
+	fprintf(stderr,"*%s*\n",rtrsip.sip_aor2);
+	fprintf(stderr,"*%s*\n",rtrsip.sip_aor3);
+	fprintf(stderr,"*%d*\n",rtrsip.reason);
+	fprintf(stderr,"*%s*\n",rtrsip.strreason);
+	
+	//return 0;
+	*/
+	
+	
+	//TODO: check args
+	if(!connect(sock, (SOCKADDR*)&sin, sizeof(sin)))
+    {
+       fprintf(stderr,"Connexion succeed!\n");
+        
+ 
+        if(send(sock, &rtrsip, sizeof(struct rtrsipaor), 0))
+			fprintf(stderr,"sent OK!\n");
+		else
+			fprintf(stderr,"not sent\n");
+        
+    }
+    else
+    {
+        fprintf(stderr,"Unable to connect\n");
+    }
+ 
+    close(sock);
+
+	return 0;
+}
+
+
+
+
+
+
Index: extensions/app_sip/tools/rtrsipaor.c
===================================================================
--- extensions/app_sip/tools/rtrsipaor.c	(revision 432)
+++ 	(revision )
@@ -1,290 +1,0 @@
-/*********************************************************************************************************
-* Software License Agreement (BSD License)                                                               *
-* Author: Alexandre Westfahl <awestfahl@freediameter.net>						 *
-*													 *
-* Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. 	 *		
-*													 *
-* All rights reserved.											 *
-* 													 *
-* Redistribution and use of this software in source and binary forms, with or without modification, are  *
-* permitted provided that the following conditions are met:						 *
-* 													 *
-* * Redistributions of source code must retain the above 						 *
-*   copyright notice, this list of conditions and the 							 *
-*   following disclaimer.										 *
-*    													 *
-* * Redistributions in binary form must reproduce the above 						 *
-*   copyright notice, this list of conditions and the 							 *
-*   following disclaimer in the documentation and/or other						 *
-*   materials provided with the distribution.								 *
-* 													 *
-* * Neither the name of the Teraoka Laboratory nor the 							 *
-*   names of its contributors may be used to endorse or 						 *
-*   promote products derived from this software without 						 *
-*   specific prior written permission of Teraoka Laboratory 						 *
-*   													 *
-* 													 *
-* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
-* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
-* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
-* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
-* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
-* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
-* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
-* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
-*********************************************************************************************************/
-
-
-// This file is separated from the source code because it is a separate command which will call registration termination function in Diameter-SIP
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <unistd.h>
-typedef int SOCKET;
-typedef struct sockaddr_in SOCKADDR_IN;
-typedef struct sockaddr SOCKADDR;
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#define PORT 666
-#include <errno.h>
-
-struct rtrsipaor
-{
-	char username[200];
-	char sip_aor1[200];
-	char sip_aor2[200];
-	char sip_aor3[200];
-	char strreason[200];
-	char desthost[200];
-	int reason;
-};
-
-int main (int argc, char **argv)
-{
-	SOCKET sock;
-    SOCKADDR_IN sin;
-    struct rtrsipaor rtrsip;
-    int numaor=0,i=0;
-    
-	sock = socket(AF_INET, SOCK_STREAM, 0);
-	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
-    sin.sin_family = AF_INET;
-    sin.sin_port = htons(PORT);
-    
-    //We initialize the structure
-    rtrsip.username[0]='\0';
-    rtrsip.sip_aor1[0]='\0';
-    rtrsip.sip_aor2[0]='\0';
-    rtrsip.sip_aor3[0]='\0';
-    rtrsip.strreason[0]='\0';
-    rtrsip.desthost[0]='\0';
-    rtrsip.reason=-1;
-    
-    
-	//Start of arguments check
-	if(argc<3)
-	{
-		fprintf(stderr,"Missing arguments! You must at least provide a username.\n");
-		return 1;
-	}
-	
-	
-	for (i=1;i<argc;i++)
-	{
-		//We must check if it is a value or the name
-		if(strncmp(argv[i],"-",1)==0)
-		{
-			if(strcmp(argv[i],"-u")==0)
-			{
-				//Username
-				if(strlen(argv[i+1])<199)
-				{
-					strcpy(rtrsip.username,argv[i+1]);
-					//We must not check the value
-					i++;
-				}
-				else
-				{
-					fprintf(stderr,"Username is too long!\n");
-				}
-			}
-			else if(strcmp(argv[i],"-a")==0)
-			{
-				i++;
-				int j=i;
-			
-				for(j=i;j<argc;j++)
-				{
-					
-					if(strncmp(argv[i],"-",1)!=0)
-					{
-						if(strlen(argv[i])>199)
-						{
-							fprintf(stderr,"SIP-AOR is too long!\n");
-						}
-						else if(strncmp(argv[i],"sip",3)!=0)
-						{
-							//Bad format of SIP-AOR
-							fprintf(stderr,"A SIP-AOR must start by 'sip:' or 'sips:'. Aborting...\n");
-							return 1;
-						}
-						else
-						{
-							if(numaor<3)
-							{
-								switch(numaor)
-								{
-									case 0:
-										strcpy(rtrsip.sip_aor1,argv[i]);
-									break;
-									case 1:
-										strcpy(rtrsip.sip_aor2,argv[i]);
-									break;
-									case 2:
-										strcpy(rtrsip.sip_aor3,argv[i]);
-									break;
-								}
-								numaor++;
-							}
-							else
-							{
-								fprintf(stderr,"You can not provide more than 3 SIP-AOR at the same time!\n");
-								break;
-							}
-						}
-						i=j+1;
-					}
-					else
-					{
-						//We have a new argument
-						i--;
-						break;
-					}
-				}
-			}
-			else if(strcmp(argv[i],"-r")==0)
-			{
-				
-				if(strlen(argv[i+1])>199)
-				{
-					fprintf(stderr,"Deregistration reason is too long!\n");
-				}
-				else
-				{
-					strcpy(rtrsip.strreason,argv[i+1]);
-				}
-				i++;
-			}
-			else if(strcmp(argv[i],"-h")==0)
-			{
-				//Remote SIP Server
-				if(strlen(argv[i+1])>199)
-				{
-					fprintf(stderr,"Host is too long!\n");
-				}
-				else if(strlen(argv[i+1])<5)
-				{
-					fprintf(stderr,"Host is too short!\n");
-				}
-				else
-				{
-					strcpy(rtrsip.desthost,argv[i+1]);
-				}
-				i++;
-			}
-			else if(strcmp(argv[i],"-pt")==0)
-			{
-				//Permanent Termination
-				rtrsip.reason=0;
-			}
-			else if(strcmp(argv[i],"-nssa")==0)
-			{
-				//New SIP Server Assigned
-				rtrsip.reason=1;
-			}
-			else if(strcmp(argv[i],"-ssc")==0)
-			{
-				//SIP Server Change
-				rtrsip.reason=2;
-			}
-			else if(strcmp(argv[i],"-rss")==0)
-			{
-				//Remote SIP Server
-				rtrsip.reason=3;
-			}
-			else
-			{
-				fprintf(stderr,"Unknown argument: %s\n",argv[i]);
-			}
-		}
-		else
-		{
-			fprintf(stderr,"Unknown argument: %s\n",argv[i]);
-			i++;
-		}
-			
-	}
-	
-	//If no SIP-AOR provided, we remove all
-	if(numaor<1)
-	{
-		fprintf(stderr,"All SIP-AOR of %s will be deregistrated.\n",rtrsip.username);
-	}
-	
-	//We want a username
-	if(strlen(rtrsip.username)==0)
-	{
-		fprintf(stderr,"You must provide a username!\n");
-		return 1;
-	}
-	
-	if(rtrsip.desthost[0]=='\0')
-	{
-		fprintf(stderr,"You must provide the hostname of SIP-Server!\n");
-		return 1;
-	}
-    
-    
-   
-    
-    /*
-    fprintf(stderr,"*%s*\n",rtrsip.username);
-	fprintf(stderr,"*%s*\n",rtrsip.sip_aor1);
-	fprintf(stderr,"*%s*\n",rtrsip.sip_aor2);
-	fprintf(stderr,"*%s*\n",rtrsip.sip_aor3);
-	fprintf(stderr,"*%d*\n",rtrsip.reason);
-	fprintf(stderr,"*%s*\n",rtrsip.strreason);
-	
-	//return 0;
-	*/
-	
-	
-	//TODO: check args
-	if(!connect(sock, (SOCKADDR*)&sin, sizeof(sin)))
-    {
-       fprintf(stderr,"Connexion succeed!\n");
-        
- 
-        if(send(sock, &rtrsip, sizeof(struct rtrsipaor), 0))
-			fprintf(stderr,"sent OK!\n");
-		else
-			fprintf(stderr,"not sent\n");
-        
-    }
-    else
-    {
-        fprintf(stderr,"Unable to connect\n");
-    }
- 
-    close(sock);
-
-	return 0;
-}
-
-
-
-
-
-
Index: extensions/app_sip/userauthorization.c
===================================================================
--- extensions/app_sip/userauthorization.c	(revision 430)
+++ extensions/app_sip/userauthorization.c	(revision 433)
@@ -34,8 +34,8 @@
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
 *********************************************************************************************************/
-#include "diamsip.h"
+#include "app_sip.h"
 
 
-int diamsip_UAR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act)
+int app_sip_UAR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, enum disp_action * act)
 {
 	TRACE_ENTRY("%p %p %p %p", msg, paramavp, sess, act);
