Initial commit

Change-Id: I6a4444e3c193dae437cd7929f4c39aba7b749efa
diff --git a/contrib/test_Gx/main_gx.c b/contrib/test_Gx/main_gx.c
new file mode 100644
index 0000000..7811661
--- /dev/null
+++ b/contrib/test_Gx/main_gx.c
@@ -0,0 +1,819 @@
+/****************
+ Contributed by: Krishnan Srinivasan <hsirk_6@yahoo.com>
+ License: to be specified.
+ TODO:
+ 
+ ****************/
+
+
+#include <freeDiameter/extension.h>
+#include <signal.h>
+#include <time.h>
+#define AUTH_APP_ID				    16777238
+#define VENDOR_ID_3GPP  			    10415
+/* The content of this file follows the same structure as dict_base_proto.c */
+
+#if 0
+#define CHECK_dict_new( _type, _data, _parent, _ref )  \
+       CHECK_FCT(  fd_dict_new( fd_g_config->cnf_dict, (_type), (_data), (_parent), (_ref))  );
+#endif
+
+void dump_sess_eyec(struct session *sess, const char *);
+
+static int ccr_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act);
+static int reauth_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act);
+static int cca_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act);
+static struct disp_hdl * ccr_cb_hdl = NULL; /* handler for ccr req cb */
+static struct disp_hdl * cca_cb_hdl = NULL; /* handler for cca req cb */
+static struct disp_hdl * reauth_cb_hdl = NULL; /* handler for cca req cb */
+
+struct dict_object *ccr_cmd = NULL;
+struct dict_object *cca_cmd = NULL;
+struct dict_object *dataobj_re_auth_request_type = NULL;
+struct dict_object * origin_host = NULL;
+struct dict_object * origin_realm = NULL;
+struct dict_object * dest_host = NULL;
+struct dict_object * dest_realm = NULL;
+struct dict_object *reauth_cmd = NULL;
+struct dict_object * auth_app_id    = NULL; 
+struct dict_object * service_cxt_id = NULL ; 
+struct dict_object * cc_req_type    = NULL; 
+struct dict_object * cc_req_num    = NULL; 
+struct dict_object * bearer_usage    = NULL; 
+struct dict_object * pflt_oper    = NULL; 
+struct dict_object * pflt_info    = NULL; 
+struct dict_object * pflt_id	= NULL; 
+struct dict_object * gx_inf;
+struct dict_object * term_cause = NULL;
+
+struct session *g_sess = NULL;
+struct session_handler *g_hdlr = NULL;
+
+enum gx_state {
+    STATE_INIT = 0,
+    STATE_INTERMEDIATE ,
+    STATE_FINAL
+};
+struct gx_sm_t {
+    enum gx_state state;
+    pthread_t tid;
+    struct fifo *events;
+    pthread_mutex_t p_sm_mtx;
+    int req_num;
+    struct session *sess;  
+} g_gx_sm;
+
+int snd_ccr_msg(struct gx_sm_t **gx_sm  , struct dict_object *cmd_r ) ;
+
+void sig_hdlr(void);
+
+void *gx_sm_th(void *sm);
+#define press_key_continue() { printf("%s %d\n", __FUNCTION__, __LINE__);}
+static int app_gx_entry(char * conffile)
+{
+//     TRACE_ENTRY("%p", conffile);	       
+       {
+	       application_id_t dcca_id = AUTH_APP_ID;
+	       application_id_t ccr_id = 272;
+	       application_id_t cca_id = 272;
+	       application_id_t reauth_id = 258;
+	       CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_ID, &dcca_id, &gx_inf, ENOENT));
+       CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_CODE_R, &ccr_id, &ccr_cmd, ENOENT));
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_CODE_A, &cca_id, &cca_cmd, ENOENT));
+  //   CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_CODE_R, &reauth_id, &reauth_cmd, ENOENT));
+
+       }
+
+       /* Applications section */
+#if 0
+     {
+	// Gx interface
+	       {
+	struct dict_application_data data = { AUTH_APP_ID, "3GPP-Gx Application" };
+	 CHECK_dict_new( DICT_APPLICATION, &data, NULL, &gx_inf);
+
+	       }				
+  
+	}
+#endif
+
+
+	// Do registeration and init stuff
+      {
+	struct disp_when data;
+       
+       TRACE_DEBUG(FULL, "Initializing dispatch callbacks for Gx interface");
+	   
+       memset(&data, 0, sizeof(data));
+       data.app = gx_inf;
+	   data.command = ccr_cmd;
+       /* Now specific handler for CCR-CMD */
+	   CHECK_FCT( fd_disp_register( ccr_cb, DISP_HOW_CC, &data, NULL, &ccr_cb_hdl ) );
+
+ 
+       memset(&data, 0, sizeof(data));
+       data.app = gx_inf;
+	   data.command = cca_cmd;
+
+	   CHECK_FCT( fd_disp_register( cca_cb, DISP_HOW_CC, &data, NULL, &cca_cb_hdl ) );
+       
+#ifdef REAUTH
+	CHECK_FCT(fd_dict_search(fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME,
+				       "Re-Auth-Request", &reauth_cmd, ENOENT));
+	memset(&data, 0, sizeof(data));
+       data.app = gx_inf;
+	   data.command = reauth_cmd;
+	printf("register REAUTH\n");
+	   CHECK_FCT( fd_disp_register( reauth_cb, DISP_HOW_CC, &data, NULL, &reauth_cb_hdl ) );
+#endif
+
+
+     }
+
+	
+       CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, 
+			       DICT_AVP, AVP_BY_NAME, 
+			       "Origin-Host", 
+				&origin_host, 
+				ENOENT) );
+
+       CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, 
+			       DICT_AVP, 
+			       AVP_BY_NAME, 
+			       "Origin-Realm", 
+			       &origin_realm, 
+			       ENOENT) );
+
+       CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, 
+			       DICT_AVP, 
+			       AVP_BY_NAME, 
+			       "Destination-Host", 
+				&dest_host, 
+				ENOENT) );
+
+       CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, 
+			       DICT_AVP, 
+			       AVP_BY_NAME, 
+			       "Destination-Realm", 
+			       &dest_realm, 
+			       ENOENT) );
+
+
+       CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, 
+			       DICT_AVP, 
+			       AVP_BY_NAME, 
+			       "Auth-Application-Id", 
+			       &auth_app_id, 
+				ENOENT) );
+
+
+       CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, 
+			       DICT_AVP, 
+			       AVP_BY_NAME, 
+			       "Service-Context-Id", 
+			       &service_cxt_id, 
+				ENOENT) );
+
+    CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, 
+			       DICT_AVP, 
+			       AVP_BY_NAME, 
+			       "CC-Request-Type", 
+			       &cc_req_type, 
+				ENOENT) );
+
+    CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, 
+			       DICT_AVP, 
+			       AVP_BY_NAME, 
+			       "Termination-Cause", 
+			       &term_cause, 
+				ENOENT) );
+
+    CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, 
+			       DICT_AVP, 
+			       AVP_BY_NAME, 
+			       "CC-Request-Number", 
+			       &cc_req_num, 
+				ENOENT) );
+    {
+	struct dict_avp_request req = { VENDOR_ID_3GPP, 0, "Bearer-Usage"}; 
+
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, 
+				   DICT_AVP, 
+				   AVP_BY_NAME_AND_VENDOR, 
+				   &req, 
+				   &bearer_usage, 
+				    ENOENT) );
+    }
+    {
+	struct dict_avp_request req = { VENDOR_ID_3GPP, 0, "Packet-Filter-Operation"}; 
+
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, 
+				   DICT_AVP, 
+				   AVP_BY_NAME_AND_VENDOR, 
+				   &req, 
+				   &pflt_oper, 
+				    ENOENT) );
+    }
+    {
+	struct dict_avp_request req = { VENDOR_ID_3GPP, 0, "Packet-Filter-Information"}; 
+
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, 
+				   DICT_AVP, 
+				   AVP_BY_NAME_AND_VENDOR, 
+				   &req, 
+				   &pflt_info, 
+				    ENOENT) );
+    }
+    {
+	struct dict_avp_request req = { VENDOR_ID_3GPP, 0, "Packet-Filter-Identifier"}; 
+
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, 
+				   DICT_AVP, 
+				   AVP_BY_NAME_AND_VENDOR, 
+				   &req, 
+				   &pflt_id, 
+				    ENOENT) );
+    }
+
+    CHECK_FCT(fd_sess_handler_create( &g_hdlr, free, NULL));
+    CHECK_FCT( fd_fifo_new(&g_gx_sm.events));  
+    CHECK_FCT( fd_disp_app_support( gx_inf, NULL, 1 , 0));
+
+    CHECK_FCT( fd_event_trig_regcb(SIGUSR1, "app_gx", sig_hdlr ) );
+
+
+       TRACE_DEBUG(INFO, "Extension 'Gx' initialized");
+       return 0;
+}
+
+void * gx_sm_th(void *sm)
+{
+   struct gx_sm_t *gx_sm = (struct gx_sm_t *) sm;
+    struct timespec tout;
+    int evt_code;
+
+	    CHECK_SYS_DO( clock_gettime( CLOCK_REALTIME, &tout), goto out);
+	    tout.tv_sec =+ 60 ;
+   
+    while(1) {
+ 
+	fd_event_timedget(gx_sm->events, &tout , ETIMEDOUT, &evt_code, NULL, NULL );
+	 CHECK_SYS_DO( clock_gettime( CLOCK_REALTIME, &tout), goto out);
+	printf("in tout sec %d\n", tout.tv_sec); 
+	if(evt_code == ETIMEDOUT) {
+	    
+	    snd_ccr_msg(&gx_sm, ccr_cmd);
+	    gx_sm->req_num++ ;
+	    gx_sm->state = STATE_INTERMEDIATE;
+	    CHECK_SYS_DO( clock_gettime( CLOCK_REALTIME, &tout), goto out);
+	    tout.tv_sec += 30 ;
+
+	}
+       // printf("press enter\n");
+       // getchar();
+    }
+     
+ out: 
+ return NULL;
+}
+//TBD
+int init_gx_sm(struct gx_sm_t *sm)
+{
+    sm->state = STATE_INIT;
+    sm->tid = 0;//
+    sm->events = NULL;
+    pthread_mutex_t p_sm_mtx;
+    sm->req_num = 0;
+    sm->sess = NULL;
+
+return 0;
+}
+int free_gx_sm(struct gx_sm_t *sm)
+{
+    
+  free(sm);
+}
+struct gx_sm_t *gl_gx_sm = NULL;
+
+void sig_hdlr()
+{
+struct gx_sm_t *gx_sm = gl_gx_sm;
+
+    if( gx_sm) {
+
+	fd_sess_dump( 0 , g_gx_sm.sess);
+
+     } else {
+
+	if(gx_sm= (struct gx_sm_t *)malloc(sizeof(struct gx_sm_t))) {
+	    init_gx_sm(gx_sm);
+	   }
+	   gl_gx_sm = gx_sm;
+	}
+
+      snd_ccr_msg( &gx_sm, ccr_cmd);
+    return;
+}
+
+static void cr_cb_ans(void *data, struct msg  **msg)
+{
+    printf("call back \n");
+   return;
+}
+
+/*				     < Session-Id >
+				   { Origin-Host }
+				   { Origin-Realm }
+				   { Destination-Realm }
+				   { Auth-Application-Id }
+				   { Service-Context-Id }
+				   { CC-Request-Type }
+				   { CC-Request-Number }
+*/
+
+int snd_ccr_msg(struct gx_sm_t **sm , struct dict_object *cmd_r ) 
+{
+       struct msg * req = NULL;
+       struct avp * avp = NULL;
+       union avp_value val;
+       struct ta_mess_info * mi = NULL, *svg;
+       struct session *sess = NULL;
+    struct gx_sm_t *gx_sm = NULL;
+    struct gx_sm_t *ptr = NULL;
+
+       TRACE_DEBUG(FULL, "Creating a new CCR message for sending. %p", gx_sm);
+       
+       /* Create the request from template */
+       CHECK_FCT_DO( fd_msg_new( cmd_r, MSGFL_ALLOC_ETEID, &req ), goto out );
+       
+    gx_sm = *sm;
+       /* Create a new session */
+    if(!gx_sm->sess) {
+
+	   CHECK_FCT_DO( fd_sess_new( &sess, 
+				   fd_g_config->cnf_diamid, 
+				   fd_g_config->cnf_diamid_len, 
+				    "CCR_SESSION", strlen("CCR_SESSION") ), goto out );
+	
+	printf("statemachine: %p %p %p\n", *(&gx_sm), gx_sm , *sm);
+	gx_sm->sess = sess;
+	printf("new session %p \n", sess);
+	//Hold the session till terminate happens
+	       CHECK_FCT( fd_sess_ref_msg(sess) );
+
+      } else {
+
+	sess = gx_sm->sess;
+	printf("use previous session %p \n", sess);
+
+      }
+     
+     fd_sess_dump( 0 , sess);
+    
+       
+//     dump_sess_eyec( sess, __FUNCTION__);
+       /* Now set all AVPs values */
+       
+       /* Session-Id */
+       {
+	       os0_t sid;
+	       size_t sidlen;
+	struct dict_object *sess_id = NULL;
+
+       CHECK_FCT( fd_dict_search(   fd_g_config->cnf_dict
+				   , DICT_AVP, AVP_BY_NAME
+				   , "Session-Id"
+				   , &sess_id, ENOENT) );
+
+	       CHECK_FCT_DO( fd_sess_getsid ( sess, &sid, &sidlen ), goto out );
+	       CHECK_FCT_DO( fd_msg_avp_new ( sess_id, 0, &avp ), goto out );
+	       val.os.data = sid;
+	       val.os.len  = sidlen;
+	       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out );
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_FIRST_CHILD, avp ), goto out );
+       }
+
+       /* Set the Destination-Realm AVP */
+       {
+	       CHECK_FCT_DO( fd_msg_avp_new ( dest_realm, 0, &avp ), goto out  );
+	       val.os.data = (unsigned char *)("vm");
+	       val.os.len  = strlen("vm");
+	       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out  );
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out  );
+       }
+       
+
+       /* Set the Destination-Host AVP if needed*/
+    {
+	       CHECK_FCT_DO( fd_msg_avp_new ( dest_host, 0, &avp ), goto out  );
+	       val.os.data = (unsigned char *)("192.168.101.3");
+	       val.os.len  = strlen("192.168.101.3");
+	       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out  );
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out  );
+    }
+       
+
+       /* Set Origin-Host & Origin-Realm */
+       CHECK_FCT_DO( fd_msg_add_origin ( req, 0 ), goto out  );
+
+
+   /*  Set Auth-Application ID */
+    {
+       CHECK_FCT_DO( fd_msg_avp_new ( auth_app_id, 0, &avp ), goto out  );
+	       val.i32 =  AUTH_APP_ID; // Auth-App id is 4 for CCR 
+	       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out  );
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out  );
+    }
+
+    /*     Set Service Context ID     */
+    {
+
+       CHECK_FCT_DO( fd_msg_avp_new ( service_cxt_id, 0, &avp ), goto out  );
+	val.os.data = (unsigned char *)("test@tst");
+	       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out  );
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out  );
+
+    }
+
+    /*     Set Request Type	      */
+    {
+#define CCR_INIT_REQUEST		1
+#define CCR_UPDATE_REQUEST		2
+#define CCR_TERMINATION_REQUEST 	3
+#define CCR_EVENT_REQUEST		4
+//TODO Change this to use  enum object
+       CHECK_FCT_DO( fd_msg_avp_new ( cc_req_type, 0, &avp ), goto out  );
+	if(gx_sm->state == STATE_INIT)
+	    val.i32 = CCR_INIT_REQUEST;
+	else if(gx_sm->state == STATE_FINAL)
+	    val.i32 = CCR_TERMINATION_REQUEST;
+	 else 
+	    val.i32 = CCR_UPDATE_REQUEST;
+
+	       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out  );
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out  );
+
+    }
+
+    /*     Set Request Number	      */
+    {
+       CHECK_FCT_DO( fd_msg_avp_new ( cc_req_num, 0, &avp ), goto out  );
+	val.i32 = gx_sm->req_num;
+	       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out  );
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out  );
+	gx_sm->req_num++; 
+    }
+
+    switch(gx_sm->state) {
+
+     case STATE_INIT:
+
+	{
+	//Set Bearer-Usage
+    //TODO Change this to use  enum object
+       CHECK_FCT_DO( fd_msg_avp_new ( bearer_usage, 0, &avp ), goto out  );
+	val.i32 = 1;//IMS
+	       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out  );
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out  );
+	} 
+       {
+	//Set Packet Filter Operation 
+	//TODO Change this to use  enum object
+       CHECK_FCT_DO( fd_msg_avp_new ( pflt_oper, 0, &avp ), goto out  );
+	val.i32 = 1;//ADDITION
+	       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out  );
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out  );
+	 } 
+	struct avp *flt_info = NULL; 
+	{
+	    //Set Packet Filter Information 
+	   CHECK_FCT_DO( fd_msg_avp_new ( pflt_info, 0, &flt_info ), goto out  );
+
+   
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, flt_info ), goto out  );
+	} 
+	// Set Packet Filter Identity
+	{
+
+	 CHECK_FCT_DO( fd_msg_avp_new ( pflt_id, 0, &avp ), goto out  );
+	  val.os.data = (unsigned char *)("ID");
+		 val.os.len  = strlen("ID");
+	  CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out  );
+	  if(flt_info) {
+		   CHECK_FCT_DO( fd_msg_avp_add( flt_info, MSG_BRW_LAST_CHILD, avp ), goto out  );
+	   }else {
+		printf("flt_info NULL\n");
+	    }
+
+	 }
+	CHECK_FCT(fd_sess_state_store(g_hdlr, sess, &gx_sm));
+
+	break;
+     case STATE_FINAL:
+	 {
+	//Set Packet Filter Operation 
+	//TODO Change this to use  enum object
+       CHECK_FCT_DO( fd_msg_avp_new ( term_cause, 0, &avp ), goto out  );
+	val.i32 = 1;//Diameter logout
+	       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out  );
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out  );
+	 } 
+	break;
+     default: 
+	printf("State mismatch \n");
+    }
+       fflush(stderr);
+       
+       /* Send the request */
+    printf("CCA %p\n",req);
+// Everthing Done Store the state: reply should retreive it 
+       CHECK_FCT_DO( fd_msg_send( &req, NULL, NULL ), goto out );
+
+out:
+       return;
+}
+
+
+static int reauth_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act)
+{
+       struct msg *ans, *qry;
+       struct avp * a;
+
+       TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act);
+       
+       if (msg == NULL)
+	       return EINVAL;
+       
+       
+       /* Create answer header */
+       qry = *msg;
+       CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, msg, 0 ) );
+       ans = *msg;
+       
+       /* Set the Origin-Host, Origin-Realm, Result-Code AVPs */
+       CHECK_FCT( fd_msg_rescode_set( ans, "DIAMETER_SUCCESS", NULL, NULL, 1 ) );
+       
+       /* Send the answer */
+       CHECK_FCT( fd_msg_send( msg, NULL, NULL ) );
+       
+       
+
+}
+static int dcca_ans_from_req( struct dict_object * obj, struct msg *qry, struct msg **msg)
+{
+       struct avp *avp  = NULL;
+       struct avp_hdr * avpdata;
+    int  rc = -1;
+
+    CHECK_FCT(fd_msg_search_avp( qry, obj, &avp));
+    CHECK_FCT(fd_msg_avp_hdr(avp, &avpdata));
+    avp = NULL;
+
+       CHECK_FCT_DO( fd_msg_avp_new ( obj, 0, &avp ), goto out  );
+       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, avpdata->avp_value ), goto out  );
+       CHECK_FCT_DO( fd_msg_avp_add( *msg, MSG_BRW_LAST_CHILD, avp ), goto out  );
+
+ rc = 0;
+out:
+ return rc;
+
+}
+/* Dummy ccr which :
+   Read the cc-req-type && cc-req-number from the msg and stick it back
+  in the reply
+
+*/
+static int ccr_cb( struct msg ** msg, struct avp * r_avp, struct session * sess, void * opaque, enum disp_action * act)
+{
+       struct msg *ans, *qry;
+       struct avp *avp  = NULL;
+       struct avp_hdr * avp_data;
+    int rc  = - 1; 
+
+       TRACE_ENTRY("%p %p %p %p", msg, r_avp, sess, act);
+       
+       if (msg == NULL)
+	       return EINVAL;
+       
+       
+       /* Create answer header */
+       qry = *msg;
+       CHECK_FCT_DO( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, msg, 0 ) , goto out);
+       ans = *msg;
+       
+       /* Set the Origin-Host, Origin-Realm, Result-Code AVPs */
+       CHECK_FCT_DO( fd_msg_rescode_set( ans, "DIAMETER_SUCCESS", NULL, NULL, 1 ), goto out );
+
+// Get the auth_app_id and from the reply and set it in the reply 
+    CHECK_FCT_DO(dcca_ans_from_req( auth_app_id, qry, msg), goto out);
+    CHECK_FCT_DO(dcca_ans_from_req( cc_req_type, qry, msg), goto out);
+    CHECK_FCT_DO(dcca_ans_from_req( cc_req_num, qry, msg), goto out);
+
+       /* Send the answer */
+       CHECK_FCT_DO( fd_msg_send( msg, NULL, NULL ), goto out );
+    rc  = 0;
+  out:
+    //Free up the memory
+    return rc ;
+
+}
+int send_reauth_req()
+
+{
+       struct dict_application_data appdata;
+    struct avp *avp = NULL;
+    union avp_value val ;
+    struct msg *qry, *ans = NULL;
+    struct msg *req = NULL;
+    struct msg *tst = NULL;
+       struct dict_object * auth_app_id = NULL, *reauth_req_type = NULL;
+
+//    qry = *msg;
+
+
+    { // Send new reauth request
+
+	   CHECK_FCT_DO( fd_msg_new( reauth_cmd, MSGFL_ALLOC_ETEID, &req ), goto out );
+       CHECK_FCT(  fd_dict_getval(gx_inf, &appdata)  );
+    
+       struct msg_hdr * header = NULL;
+	       CHECK_FCT( fd_msg_hdr ( req, &header ) );
+	       header->msg_appl = appdata.application_id;
+
+    }
+
+       /* Session-Id */
+       {
+	       os0_t sid;
+	       size_t sidlen;
+	struct dict_object *sess_id = NULL;
+       CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Session-Id", &sess_id, ENOENT) );
+
+	       //CHECK_FCT_DO( fd_sess_getsid ( sess, &sid, &sidlen ), goto out );
+	       CHECK_FCT_DO( fd_msg_avp_new ( sess_id, 0, &avp ), goto out );
+	       val.os.data = sid;
+	       val.os.len  = sidlen;
+	       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out );
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_FIRST_CHILD, avp ), goto out );
+	       
+       }
+
+       /* Set the Destination-Realm AVP */
+       {
+	       CHECK_FCT_DO( fd_msg_avp_new ( dest_realm, 0, &avp ), goto out  );
+	       val.os.data = (unsigned char *)("vm");
+	       val.os.len  = strlen("vm");
+	       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out  );
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out  );
+       }
+       
+       /* Set the Destination-Host AVP if needed*/
+//     if (ta_conf->dest_host) {
+	       CHECK_FCT_DO( fd_msg_avp_new ( dest_host, 0, &avp ), goto out  );
+	       val.os.data = (unsigned char *)("cli.vm");
+	       val.os.len  = strlen("cli.vm");
+	       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out  );
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out  );
+//     }
+    
+       /* Set Origin-Host & Origin-Realm */
+       CHECK_FCT_DO( fd_msg_add_origin ( req, 0 ), goto out  );
+
+       /* AUTH_Application-ID */
+       CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Auth-Application-Id", &auth_app_id, ENOENT) );
+	       CHECK_FCT_DO( fd_msg_avp_new ( auth_app_id, 0, &avp ), goto out  );
+	       val.i32 = appdata.application_id;
+	       CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out  );
+	       CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out  );
+
+       /* Re-Auth Request Type */
+	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Re-Auth-Request-Type", &reauth_req_type, ENOENT) );
+	   CHECK_FCT_DO( fd_msg_avp_new ( reauth_req_type, 0, &avp ), goto out  );
+	val.i32 = 0;
+	CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out  );
+	CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out  );
+
+
+       CHECK_FCT_DO( fd_msg_send( &req, cr_cb_ans, NULL ), goto out );
+out:
+    return 0 ;
+
+
+}
+/* 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;
+}
+
+static int cca_cb( struct msg ** msg, 
+		   struct avp * t_avp , 
+		   struct session * sess, 
+		   void * opaque, 
+		   enum disp_action * act)
+{
+    struct avp *avp = NULL, *g_avp = NULL;
+    struct msg *req = *msg;
+    struct dict_object *chrg_rule_name = NULL ;
+    struct dict_object *chrg_rule_grp = NULL ;
+       struct avp_hdr * avpdata = NULL;
+    struct dict_avp_request grule_req =   {VENDOR_ID_3GPP, 0,"Charging-Rule-Install"};
+    struct dict_avp_request rule_req =   {VENDOR_ID_3GPP, 0,"Charging-Rule-Name"};
+    struct timespec sess_timeout;
+    struct gx_sm_t *gx_sm = NULL;
+  //  struct session *sess = NULL;
+    
+    
+       CHECK_FCT(fd_sess_state_retrieve( g_hdlr, sess, &gx_sm));
+    fd_sess_dump( 0, sess);
+    if(gx_sm->state != STATE_FINAL) { 
+
+	       CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict
+				   , DICT_AVP, AVP_BY_NAME_AND_VENDOR
+				   , &grule_req 
+				   , &chrg_rule_grp
+				   , ENOENT));
+    
+	   CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict
+				   , DICT_AVP, AVP_BY_NAME_AND_VENDOR
+				   , &rule_req 
+				   , &chrg_rule_name
+				   , ENOENT));
+
+
+	CHECK_FCT(fd_msg_search_avp ( *msg, chrg_rule_grp, &g_avp ));
+	CHECK_FCT(fd_avp_search_avp(g_avp, chrg_rule_name, &avp));
+
+	if(avp) {
+	       CHECK_FCT(fd_msg_avp_hdr(avp, &avpdata));
+	} else {
+	    printf("NULL AVP \n");
+	}
+	printf("charging-rule-name %s\n", avpdata->avp_value->os.data);
+	gx_sm->state = STATE_FINAL;
+	dump_sess_eyec( sess, __FUNCTION__);
+	printf("next dump\n");
+	CHECK_FCT(fd_sess_state_store( g_hdlr, sess, &gx_sm));
+	fd_msg_free(*msg);
+	*msg = NULL;
+
+   } else {
+	printf("Session terminated\n");
+	free_gx_sm(gx_sm);
+	fd_msg_free(*msg);
+	fd_sess_reclaim(&sess);
+	*msg = NULL;
+    }
+    fd_sess_dump( 0 , sess);
+
+    return 0;
+}
+
+static int gx_entry(char * conffile)
+{
+ return 0;
+}
+EXTENSION_ENTRY( "app_gx", app_gx_entry, "dict_dcca_3gpp");
+//EXTENSION_ENTRY( "app_gx", gx_entry);