/*********************************************************************************************************
* Software License Agreement (BSD License)                                                               *
* Author: Sebastien Decugis <sdecugis@freediameter.net>							 *
*													 *
* Copyright (c) 2013, WIDE Project and NICT								 *
* 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 WIDE Project or NICT nor the 						 *
*   names of its contributors may be used to endorse or 						 *
*   promote products derived from this software without 						 *
*   specific prior written permission of WIDE Project and 						 *
*   NICT.												 *
* 													 *
* 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.								 *
*********************************************************************************************************/

/* Install the dispatch callbacks */

#include "sh_app.h"

static struct disp_hdl * sh_hdl_fb	= NULL; /* handler for fallback cb */
static struct disp_hdl * sh_hdl_udr	= NULL; /* handler for PUR req cb */
static struct disp_hdl * sh_hdl_pur	= NULL; /* handler for PUR req cb */

static struct sh_udr_msg g_udr_cmd;
static struct sh_pur_msg g_pur_cmd;

	
static struct sh_uda_msg g_uda_cmd;
static struct sh_pua_msg g_pua_cmd;
//static struct dict_avp_request __avp;



/* Default callback for the application. */
#if 0
static int ta_fb_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act)
{
	/* This CB should never be called */
	TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act);
	
	fd_log_debug("Unexpected message received!");
	
	return ENOTSUP;
}
#endif

static int sh_parse_pur(struct msg** msg)
{
	struct msg* udr_msg = *msg;
	struct avp *avp, *grpavp=NULL;
	struct avp_hdr *avphdr;

	LOG_D("fj sh_parse_pur");

	SEARCH_AVP_("Session-Id", 0,	&g_pur_cmd.pur_session_id);
	SEARCH_AVP_("DRMP", SH_VND_ID,	&g_pur_cmd.pur_drmp);
	
	SEARCH_AVP_("Vendor-Specific-Application-Id", 0,	&g_pur_cmd.pur_vendor_specific_application_id);
	SEARCH_AVP_("Vendor-Id", 0, &g_pur_cmd.pur_vendor_id);
	SEARCH_AVP_("Auth-Application-Id", 0, &g_pur_cmd.pur_auth_application_d);
	SEARCH_AVP_("Acct-Application-Id", 0, &g_pur_cmd.pur_acct_application_id);

	SEARCH_AVP_("Auth-Session-State", 0,	&g_pur_cmd.pur_auth_session_state);
	SEARCH_AVP_("Origin-Host", 0, &g_pur_cmd.pur_origin_host);
	SEARCH_AVP_("Origin-Realm", 0, &g_pur_cmd.pur_origin_realm);
	SEARCH_AVP_("Destination-Host", 0, &g_pur_cmd.pur_destination_host);
	SEARCH_AVP_("Destination-Realm", 0, &g_pur_cmd.pur_destination_realm);

	SEARCH_AVP_("Supported-Features", SH_VND_ID, &g_pur_cmd.pur_supported_features);
	SEARCH_AVP_("Feature-List-ID", SH_VND_ID, &g_pur_cmd.pur_feature_list_id);
	SEARCH_AVP_("Feature-List", SH_VND_ID, &g_pur_cmd.pur_feature_list);

	
	SEARCH_AVP_("User-Identity", SH_VND_ID, &g_pur_cmd.pur_user_identity);
	SEARCH_AVP_("Public-Identity", SH_VND_ID, &g_pur_cmd.pur_public_identity);
	SEARCH_AVP_("MSISDN", SH_VND_ID, &g_pur_cmd.pur_msisdn);

	
	SEARCH_AVP_("Wildcarded-Public-Identity", SH_VND_ID, &g_pur_cmd.pur_wildcarded_public_identity);
	SEARCH_AVP_("Wildcarded-IMPU", SH_VND_ID, &g_pur_cmd.pur_wildcarded_impu);
	SEARCH_AVP_("User-Name", 0, &g_pur_cmd.pur_user_name);
	SEARCH_AVP_("Data-Reference", SH_VND_ID, &g_pur_cmd.pur_data_reference);

	SEARCH_AVP_("User-Data", SH_VND_ID, &g_pur_cmd.pur_user_date);
	
	SEARCH_AVP_("OC-Supported-Features", SH_VND_ID, &g_pur_cmd.pur_oc_supported_features);
	SEARCH_AVP_("OC-Feature-Vector", SH_VND_ID, &g_pur_cmd.pur_oc_feature_vector);

	/* Session-Id */
	PARSE_AVP_STR(udr_msg, g_pur_cmd.pur_session_id, avp, avphdr, "Session-Id", avphdr->avp_value->os.data)

	/* DRMP */
	PARSE_AVP_U32(udr_msg, g_pur_cmd.pur_drmp, avp, avphdr, "DRMP", avphdr->avp_value->u32)

	/* Vendor-Specific-Application-Id */
	CHECK_FCT( fd_msg_search_avp ( udr_msg, g_pur_cmd.pur_vendor_specific_application_id, &grpavp) );
	if(grpavp != NULL)
	{
		LOG_D("fj Vendor-Specific-Application-Id");

		/* Vendor-Id */	
		PARSE_GRPAVP_U32(grpavp, g_pur_cmd.pur_vendor_id, avp, avphdr, "Vendor-Id", avphdr->avp_value->u32)

		/* Auth-Application-Id */
		PARSE_GRPAVP_U32(grpavp, g_pur_cmd.pur_auth_application_d, avp, avphdr, "Auth-Application-Id", avphdr->avp_value->u32)

		/* Acct-Application-Id */
		PARSE_GRPAVP_U32(grpavp, g_pur_cmd.pur_acct_application_id, avp, avphdr, "Acct-Application-Id", avphdr->avp_value->u32)
	}

	/* Auth-Session-State */
	PARSE_AVP_U32(udr_msg, g_pur_cmd.pur_auth_session_state, avp, avphdr, "Auth-Session-State", avphdr->avp_value->i32)

	/* Origin-Host */
	PARSE_AVP_STR(udr_msg, g_pur_cmd.pur_origin_host, avp, avphdr, "Origin-Host", avphdr->avp_value->os.data)

	/* Origin-Realm */
	PARSE_AVP_STR(udr_msg, g_pur_cmd.pur_origin_realm, avp, avphdr, "Origin-Realm", avphdr->avp_value->os.data)

	/* Destination-Host */
	PARSE_AVP_STR(udr_msg, g_pur_cmd.pur_destination_host, avp, avphdr, "Dest-Host", avphdr->avp_value->os.data)
	
	/* Destination-Realm */
	PARSE_AVP_STR(udr_msg, g_pur_cmd.pur_destination_realm, avp, avphdr, "Dest-Realm", avphdr->avp_value->os.data)

	/* Supported-Features */
	CHECK_FCT( fd_msg_search_avp ( udr_msg, g_pur_cmd.pur_supported_features, &grpavp) );
	if(grpavp != NULL)
	{
		LOG_D("fj Supported-Features ");

		/* Vendor-Id */
		PARSE_GRPAVP_U32(grpavp, g_pur_cmd.pur_vendor_id, avp, avphdr, "Vendor-Id", avphdr->avp_value->u32)

		/* Feature-List-ID */
		PARSE_GRPAVP_U32(grpavp, g_pur_cmd.pur_feature_list_id, avp, avphdr, "Feature-List-ID", avphdr->avp_value->u32)

		/* Feature-List */
		PARSE_GRPAVP_U32(grpavp, g_pur_cmd.pur_feature_list, avp, avphdr, "Feature-List", avphdr->avp_value->u32)
	}

	/* User-Identity  */
	CHECK_FCT( fd_msg_search_avp ( udr_msg, g_pur_cmd.pur_user_identity, &grpavp) );
	if(grpavp != NULL)
	{
		LOG_D("fj User-Identity");

		/* Public-Identity */
		PARSE_GRPAVP_STR(grpavp, g_pur_cmd.pur_public_identity, avp, avphdr, "Public-Identity", avphdr->avp_value->os.data)

		/* MSISDN */
		PARSE_GRPAVP_STR(grpavp, g_pur_cmd.pur_msisdn, avp, avphdr, "MSISDN", avphdr->avp_value->os.data)
	}

	/* wildcarded_public_id */
	PARSE_AVP_STR(udr_msg, g_pur_cmd.pur_wildcarded_public_identity, avp, avphdr, "Wild-Pub-ID", avphdr->avp_value->os.data)	

	/* wildcarded_impu */
	PARSE_AVP_STR(udr_msg, g_pur_cmd.pur_wildcarded_impu, avp, avphdr, "Wild-Pub-IMPU", avphdr->avp_value->os.data)		

	/* User-Name */
	PARSE_AVP_STR(udr_msg, g_pur_cmd.pur_user_name, avp, avphdr, "User-Name", avphdr->avp_value->os.data)		

	/* User-Data */
	PARSE_AVP_STR(udr_msg, g_pur_cmd.pur_user_date, avp, avphdr, "User-Data", avphdr->avp_value->os.data)


	/* OC-Supported-Features */
	CHECK_FCT( fd_msg_search_avp ( udr_msg, g_pur_cmd.pur_oc_supported_features, &grpavp) );
	if(grpavp != NULL)
	{
		LOG_D("fj OC-Supported-Features");

		/* OC-Feature-Vector */
		PARSE_GRPAVP_U64(grpavp, g_pur_cmd.pur_oc_feature_vector, avp, avphdr, "OC-Feature-Vector", avphdr->avp_value->u64)
	}

out:
	return 0;
}

static int sh_parse_udr(struct msg** msg)
{
	struct msg* udr_msg = *msg;
	struct avp *avp, *grpavp=NULL;
	struct avp_hdr *avphdr;

	LOG_D("fj sh_parse_udr");


	SEARCH_AVP_("Session-Id", 0, &g_udr_cmd.udr_session_id);
	SEARCH_AVP_("DRMP", SH_VND_ID, &g_udr_cmd.udr_drmp);
	SEARCH_AVP_("Vendor-Specific-Application-Id", 0, &g_udr_cmd.udr_vendor_specific_application_id);
	SEARCH_AVP_("Vendor-Id", 0, &g_udr_cmd.udr_vendor_id);
	SEARCH_AVP_("Auth-Application-Id", 0, &g_udr_cmd.udr_auth_application_d);
	SEARCH_AVP_("Acct-Application-Id", 0, &g_udr_cmd.udr_acct_application_id);

	SEARCH_AVP_("Auth-Session-State", 0, &g_udr_cmd.udr_auth_session_state);
	SEARCH_AVP_("Origin-Host", 0, &g_udr_cmd.udr_origin_host);
	SEARCH_AVP_("Origin-Realm", 0, &g_udr_cmd.udr_origin_realm);
	SEARCH_AVP_("Destination-Host", 0, &g_udr_cmd.udr_destination_host);
	SEARCH_AVP_("Destination-Realm", 0, &g_udr_cmd.udr_destination_realm);
	
	SEARCH_AVP_("Supported-Features", SH_VND_ID, &g_udr_cmd.udr_supported_features);
	SEARCH_AVP_("Feature-List-ID", SH_VND_ID, &g_udr_cmd.udr_feature_list_id);
	SEARCH_AVP_("Feature-List", SH_VND_ID, &g_udr_cmd.udr_feature_list);
	
	SEARCH_AVP_("User-Identity", SH_VND_ID, &g_udr_cmd.udr_user_identity);
	SEARCH_AVP_("Public-Identity", SH_VND_ID, &g_udr_cmd.udr_public_identity);
	SEARCH_AVP_("MSISDN", SH_VND_ID, &g_udr_cmd.udr_msisdn);

	
	SEARCH_AVP_("Wildcarded-Public-Identity", SH_VND_ID, &g_udr_cmd.udr_wildcarded_public_identity);
	SEARCH_AVP_("Wildcarded-IMPU", SH_VND_ID, &g_udr_cmd.udr_wildcarded_impu);
	SEARCH_AVP_("Server-Name", SH_VND_ID, &g_udr_cmd.udr_server_name);
	SEARCH_AVP_("Service-Indication", SH_VND_ID, &g_udr_cmd.udr_service_indication);
	SEARCH_AVP_("Data-Reference", SH_VND_ID, &g_udr_cmd.udr_data_reference);
	SEARCH_AVP_("Identity-Set", SH_VND_ID, &g_udr_cmd.udr_identity_set);
	SEARCH_AVP_("Requested-Domain", SH_VND_ID, &g_udr_cmd.udr_requested_domain);
	SEARCH_AVP_("Current-Location", SH_VND_ID, &g_udr_cmd.udr_current_location);
	SEARCH_AVP_("DSAI-Tag", SH_VND_ID, &g_udr_cmd.udr_dsai_tag);
	SEARCH_AVP_("Session-Priority", SH_VND_ID, &g_udr_cmd.udr_session_priority);
	SEARCH_AVP_("User-Name", 0, &g_udr_cmd.udr_user_name);
	SEARCH_AVP_("Requested-Nodes", SH_VND_ID, &g_udr_cmd.udr_requested_nodes);
	SEARCH_AVP_("Serving-Node-Indication", SH_VND_ID, &g_udr_cmd.udr_serving_node_indication);
	SEARCH_AVP_("Pre-paging-Supported", SH_VND_ID, &g_udr_cmd.udr_pre_paging_supported);
	SEARCH_AVP_("Local-Time-Zone-Indication", SH_VND_ID, &g_udr_cmd.udr_local_time_zone_indication);
	SEARCH_AVP_("UDR-Flags", SH_VND_ID, &g_udr_cmd.udr_udr_flags);
	
	SEARCH_AVP_("Call-Reference-Info", SH_VND_ID, &g_udr_cmd.udr_call_reference_info);
	SEARCH_AVP_("Call-Reference-Number", SH_VND_ID, &g_udr_cmd.udr_call_reference_number);
	SEARCH_AVP_("AS-Number", SH_VND_ID, &g_udr_cmd.udr_as_number);

	
	SEARCH_AVP_("OC-Supported-Features", SH_VND_ID, &g_udr_cmd.udr_oc_supported_features);
	SEARCH_AVP_("OC-Feature-Vector", SH_VND_ID, &g_udr_cmd.udr_oc_feature_vector);
	

	/* Session-Id */
	PARSE_AVP_STR(udr_msg, g_udr_cmd.udr_session_id, avp, avphdr, "fj Session-Id", avphdr->avp_value->os.data)

	/* DRMP */
	PARSE_AVP_U32(udr_msg, g_udr_cmd.udr_drmp, avp, avphdr, "fj DRMP", avphdr->avp_value->u32)

	/* Vendor-Specific-Application-Id */
	CHECK_FCT( fd_msg_search_avp ( udr_msg, g_udr_cmd.udr_vendor_specific_application_id, &grpavp) );
	if(grpavp != NULL)
	{
		LOG_D("fj Vendor-Specific-Application-Id");

		/* Vendor-Id */	
		PARSE_GRPAVP_U32(grpavp, g_udr_cmd.udr_vendor_id, avp, avphdr, "Vendor-Id", avphdr->avp_value->u32)

		/* Auth-Application-Id */
		PARSE_GRPAVP_U32(grpavp, g_udr_cmd.udr_auth_application_d, avp, avphdr, "Auth-Application-Id", avphdr->avp_value->u32)

		/* Acct-Application-Id */
		PARSE_GRPAVP_U32(grpavp, g_udr_cmd.udr_acct_application_id, avp, avphdr, "Acct-Application-Id", avphdr->avp_value->u32)
	}

	/* Auth-Session-State */
	PARSE_AVP_U32(udr_msg, g_udr_cmd.udr_auth_session_state, avp, avphdr, "Auth-Session-State", avphdr->avp_value->i32)

	/* Origin-Host */
	PARSE_AVP_STR(udr_msg, g_udr_cmd.udr_origin_host, avp, avphdr, "Origin-Host", avphdr->avp_value->os.data)

	/* Origin-Realm */
	PARSE_AVP_STR(udr_msg, g_udr_cmd.udr_origin_realm, avp, avphdr, "Origin-Realm", avphdr->avp_value->os.data)

	/* Destination-Host */
	PARSE_AVP_STR(udr_msg, g_udr_cmd.udr_destination_host, avp, avphdr, "Dest-Host", avphdr->avp_value->os.data)
	
	/* Destination-Realm */
	PARSE_AVP_STR(udr_msg, g_udr_cmd.udr_destination_realm, avp, avphdr, "Dest-Realm", avphdr->avp_value->os.data)

	/* Supported-Features */
	CHECK_FCT( fd_msg_search_avp ( udr_msg, g_udr_cmd.udr_supported_features, &grpavp) );
	if(grpavp != NULL)
	{
		LOG_D("fj Supported-Features ");

		/* Vendor-Id */
		PARSE_GRPAVP_U32(grpavp, g_udr_cmd.udr_vendor_id, avp, avphdr, "Vendor-Id", avphdr->avp_value->u32)

		/* Feature-List-ID */
		PARSE_GRPAVP_U32(grpavp, g_udr_cmd.udr_feature_list_id, avp, avphdr, "Feature-List-ID", avphdr->avp_value->u32)

		/* Feature-List */
		PARSE_GRPAVP_U32(grpavp, g_udr_cmd.udr_feature_list, avp, avphdr, "Feature-List", avphdr->avp_value->u32)
	}

	/* User-Identity  */
	CHECK_FCT( fd_msg_search_avp ( udr_msg, g_udr_cmd.udr_user_identity, &grpavp) );
	if(grpavp != NULL)
	{
		LOG_D("fj User-Identity");

		/* Public-Identity */
		PARSE_GRPAVP_STR(grpavp, g_udr_cmd.udr_public_identity, avp, avphdr, "Public-Identity", avphdr->avp_value->os.data)

		/* MSISDN */
		PARSE_GRPAVP_STR(grpavp, g_udr_cmd.udr_msisdn, avp, avphdr, "MSISDN", avphdr->avp_value->os.data)
	}

	/* wildcarded_public_id */
	PARSE_AVP_STR(udr_msg, g_udr_cmd.udr_wildcarded_public_identity, avp, avphdr, "Wild-Pub-ID", avphdr->avp_value->os.data)	

	/* wildcarded_impu */
	PARSE_AVP_STR(udr_msg, g_udr_cmd.udr_wildcarded_impu, avp, avphdr, "Wild-Pub-IMPU", avphdr->avp_value->os.data)		

	/* server_name */
	PARSE_AVP_STR(udr_msg, g_udr_cmd.udr_server_name, avp, avphdr, "Server-Name", avphdr->avp_value->os.data)	

	/* Service-Indication */
	PARSE_AVP_STR(udr_msg, g_udr_cmd.udr_service_indication, avp, avphdr, "Service-ID", avphdr->avp_value->os.data)		

	/* Data-Reference */
	PARSE_AVP_U32(udr_msg, g_udr_cmd.udr_data_reference, avp, avphdr, "Data-Reference", avphdr->avp_value->u32)

	/* Identity-Set */
	PARSE_AVP_U32(udr_msg, g_udr_cmd.udr_identity_set, avp, avphdr, "Identity-Set", avphdr->avp_value->u32)

	/* Req-Domain*/
	PARSE_AVP_U32(udr_msg, g_udr_cmd.udr_requested_domain, avp, avphdr, "Req-Domain", avphdr->avp_value->u32)

	/* Cur-Location*/
	PARSE_AVP_U32(udr_msg, g_udr_cmd.udr_current_location, avp, avphdr, "Cur-Location", avphdr->avp_value->u32)

	/* DSAI-Tag */
	PARSE_AVP_STR(udr_msg, g_udr_cmd.udr_dsai_tag, avp, avphdr, "DSAI-Tag", avphdr->avp_value->os.data)		

	/* Session-Priority */
	PARSE_AVP_U32(udr_msg, g_udr_cmd.udr_session_priority, avp, avphdr, "Session-Priority", avphdr->avp_value->u32)

	/* User-Name*/
	PARSE_AVP_STR(udr_msg, g_udr_cmd.udr_user_name, avp, avphdr, "User-Name", avphdr->avp_value->os.data)		

	PARSE_AVP_U32(udr_msg, g_udr_cmd.udr_requested_nodes, avp, avphdr, "Requested-Nodes", avphdr->avp_value->u32)
	PARSE_AVP_U32(udr_msg, g_udr_cmd.udr_serving_node_indication, avp, avphdr, "Serving-Node-Indication", avphdr->avp_value->u32)
	PARSE_AVP_U32(udr_msg, g_udr_cmd.udr_pre_paging_supported, avp, avphdr, "Pre-paging-Supported", avphdr->avp_value->u32)
	PARSE_AVP_U32(udr_msg, g_udr_cmd.udr_local_time_zone_indication, avp, avphdr, "Local-Time-Zone-Indication", avphdr->avp_value->u32)
	PARSE_AVP_U32(udr_msg, g_udr_cmd.udr_udr_flags, avp, avphdr, "UDR-Flags", avphdr->avp_value->u32)

	/* Call-Reference-Info */
	CHECK_FCT( fd_msg_search_avp ( udr_msg, g_udr_cmd.udr_call_reference_info, &grpavp) );
	if(grpavp != NULL)
	{
		LOG_D("fj Call-Reference-Info");

		/* Call-Reference-Number */
		PARSE_GRPAVP_STR(grpavp, g_udr_cmd.udr_call_reference_number, avp, avphdr, "Call-Reference-Number", avphdr->avp_value->os.data)

		/* AS-Number */
		PARSE_GRPAVP_STR(grpavp, g_udr_cmd.udr_as_number, avp, avphdr, "AS-Number", avphdr->avp_value->os.data)
	}

	/* OC-Supported-Features */
	CHECK_FCT( fd_msg_search_avp ( udr_msg, g_udr_cmd.udr_oc_supported_features, &grpavp) );
	if(grpavp != NULL)
	{
		LOG_D("fj Call-Reference-Info");

		/* OC-Feature-Vector */
		PARSE_GRPAVP_U64(grpavp, g_udr_cmd.udr_oc_feature_vector, avp, avphdr, "OC-Feature-Vector", avphdr->avp_value->u64)
	}
out:
	
	return 0;
}

static int sh_rec_pur( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act)
{
	struct dict_object * pur_origin_host = NULL;

	struct msg *ans/*, *qry*/;
	struct avp * a, *grpavp;
	union avp_value val;
	
	TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act);
	LOG_D("fj sh_rec_pur in\n");
	
	if (msg == NULL)
		return EINVAL;

	sh_parse_pur(msg);

	SEARCH_AVP_("Origin-Host", 0, &pur_origin_host);
	
	/* Value of Origin-Host */
	if (! (ta_conf->mode & MODE_BENCH)) {
		fprintf(stderr, "ECHO PUR received from ");
		CHECK_FCT( fd_msg_search_avp ( *msg, pur_origin_host, &a) );
		if (a) {
			struct avp_hdr * hdr;
			CHECK_FCT( fd_msg_avp_hdr( a, &hdr ) );
			fprintf(stderr, "'%.*s'", (int)hdr->avp_value->os.len, hdr->avp_value->os.data);
		} else {
			fprintf(stderr, "no_Origin-Host");
		}
		fprintf(stderr, ", replying...\n");
	}
	
	/* Create answer header */
	// qry = *msg;
	CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, msg, 0 ) );
	ans = *msg;


	/* set application id */
	{
		struct msg_hdr * msg_hdr = NULL;
		ASSERT( 0 == fd_msg_hdr ( ans, &msg_hdr ) );
		msg_hdr->msg_appl = SH_APP_ID;
		msg_hdr->msg_flags = msg_hdr->msg_flags & (~CMD_FLAG_PROXIABLE);
	}
	

	/* resolve uda avps */
	// SEARCH_AVP_("Session-Id", 0, &uda_session_id);
	SEARCH_AVP_("DRMP", SH_VND_ID, &g_pua_cmd.pua_drmp);
	SEARCH_AVP_("Vendor-Specific-Application-Id", 0, &g_pua_cmd.pua_vendor_specific_application_id);
	SEARCH_AVP_("Result-Code", 0, &g_pua_cmd.pua_result_code);
	SEARCH_AVP_("Experimental-Result", 0, &g_pua_cmd.pua_experimental_result);
	SEARCH_AVP_("Auth-Session-State", 0, &g_pua_cmd.pua_auth_session_state);
	SEARCH_AVP_("Origin-Host", 0, &g_pua_cmd.pua_origin_host);
	SEARCH_AVP_("Origin-Realm", 0, &g_pua_cmd.pua_origin_realm);


	SEARCH_AVP_("Wildcarded-Public-Identity", SH_VND_ID, &g_pua_cmd.pua_wildcarded_public_identity );
	SEARCH_AVP_("Wildcarded-IMPU", SH_VND_ID, &g_pua_cmd.pua_wildcarded_impu );
	SEARCH_AVP_("Repository-Data-ID", SH_VND_ID, &g_pua_cmd.pua_repository_data_id );

	SEARCH_AVP_("Data-Reference", SH_VND_ID, &g_pua_cmd.pua_data_reference);

	SEARCH_AVP_("Supported-Features", SH_VND_ID, &g_pua_cmd.pua_supported_features );
	SEARCH_AVP_("OC-Supported-Features", SH_VND_ID, &g_pua_cmd.pua_oc_supported_features );
	SEARCH_AVP_("OC-OLR", SH_VND_ID, &g_pua_cmd.pua_oc_olr );
	

	EN_AVP_U32(g_pua_cmd.pua_drmp, 0, ans, "DRMP Ok")

	/* Set Vendor-Specific-Application-Id AVP if needed*/
	{
		CHECK_FCT( fd_msg_avp_new ( g_pua_cmd.pua_vendor_specific_application_id, 0, &grpavp ) );

		EN_GRPAVP_U32("Vendor-Id", 0, g_pua_cmd.pua_vendor_id, SH_VND_ID)
		EN_GRPAVP_U32("Auth-Application-Id", 0, g_pua_cmd.pua_auth_application_d, SH_APP_ID)
		EN_GRPAVP_U32("Acct-Application-Id", 0,g_pua_cmd.pua_acct_application_id, SH_APP_ID)

		CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, grpavp ) );
		
		LOG_D("Vendor-Specific-Application-Id Ok\n");
	}

	// EN_AVP_U32(g_uda_cmd.uda_result_code, 2001, ans, "Result-Code Ok\n")

	/* Set the Experimental-Result group AVP if needed*/
	{
		CHECK_FCT( fd_msg_avp_new ( g_pua_cmd.pua_experimental_result, 0, &grpavp )  );

		EN_GRPAVP_U32("Vendor-Id", 0, g_pua_cmd.pua_vendor_id, SH_VND_ID)
		EN_GRPAVP_U32("Experimental-Result-Code", 0, g_pua_cmd.pua_experimental_result_code, 3)
		CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, grpavp )  );
		
		LOG_D("Experimental-Result Ok\n");
	}
	
	EN_AVP_U32(g_pua_cmd.pua_auth_session_state, 0, ans, "Auth-Session-State Ok\n")


	EN_AVP_STR(g_pua_cmd.pua_wildcarded_public_identity, "wildcarded_public_id", ans, "wildcarded_public_id Ok")
	EN_AVP_STR( g_pua_cmd.pua_wildcarded_impu, "wildcarded_impu", ans, "wildcarded_impu Ok")
	/* Set the Repository-Data-ID AVP */
	{
		CHECK_FCT( fd_msg_avp_new ( g_pua_cmd.pua_repository_data_id, 0, &grpavp ) );

		EN_GRPAVP_STR("Service-Indication", SH_VND_ID, g_pua_cmd.pua_service_indication, "Service-Indicatioin", grpavp)
		EN_GRPAVP_U32_("Sequence-Number", SH_VND_ID, g_pua_cmd.pua_sequence_number, 1234, grpavp)

		CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, grpavp )  );

		LOG_D("Repository-Data-ID Ok\n");
	}

	EN_AVP_U32(g_pua_cmd.pua_data_reference, 0, ans, "Data-Reference Ok\n")

	/* Set the Supported-Features group AVP if needed*/
	{
		CHECK_FCT( fd_msg_avp_new ( g_pua_cmd.pua_supported_features, 0, &grpavp ) );
	
		EN_GRPAVP_U32_("Vendor-Id", 0, g_pua_cmd.pua_vendor_id, SH_VND_ID, grpavp)
		EN_GRPAVP_U32_("Feature-List-ID", SH_VND_ID, g_pua_cmd.pua_feature_list_id, 2, grpavp)
		EN_GRPAVP_U32_("Feature-List", SH_VND_ID, g_pua_cmd.pua_feature_list, 3, grpavp)

		CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, grpavp ) );
		
		LOG_D("Supported-Features Ok\n");
	}	

	/* Set the OC-Supported-Features AVP */
	{
		CHECK_FCT( fd_msg_avp_new ( g_pua_cmd.pua_oc_supported_features, 0, &grpavp ) );

		EN_GRPAVP_U64("OC-Feature-Vector", SH_VND_ID, g_pua_cmd.pua_oc_feature_vector, 1)

		CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, grpavp )  );

		LOG_D("OC-Feature-Vector Ok\n");
	}
	
	/* Set the OC-OLR group AVP if needed*/
	{
		CHECK_FCT( fd_msg_avp_new ( g_pua_cmd.pua_oc_olr, 0, &grpavp ) );
	
		EN_GRPAVP_U64("OC-Sequence-Number", SH_VND_ID, g_pua_cmd.pua_oc_sequence_number, 1)
		EN_GRPAVP_U32("OC-Report-Type", SH_VND_ID,g_pua_cmd.pua_oc_report_type, 2)
		EN_GRPAVP_U32("OC-Reduction-Percentage", SH_VND_ID, g_pua_cmd.pua_oc_reduction_percentage, 3)
		EN_GRPAVP_U32("OC-Validity-Duration", SH_VND_ID, g_pua_cmd.pua_oc_validity_duration, 4)

		CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, grpavp ) );
		
		LOG_D("OC-OLR Ok\n");
	}
	
	/* 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 ) );
	
	/* Add this value to the stats */
	CHECK_POSIX_DO( pthread_mutex_lock(&ta_conf->stats_lock), );
	ta_conf->stats.nb_echoed++;
	CHECK_POSIX_DO( pthread_mutex_unlock(&ta_conf->stats_lock), );

out:
	printf("sh_rec_pur end.\n");
	return 0;
}

static int sh_rec_udr( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act)
{
	struct msg *ans/*, *qry*/;
	struct avp * a, *grpavp;
	union avp_value val;
	
	TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act);
	LOG_D("fj sh_rec_udr in\n");
	
	if (msg == NULL)
		return EINVAL;

	sh_parse_udr(msg);

	SEARCH_AVP_("Origin-Host", 0, &g_uda_cmd.uda_origin_host);
	SEARCH_AVP_("Session-Id", 0, &g_uda_cmd.uda_session_id);
	
	/* Value of Origin-Host */
	if (! (ta_conf->mode & MODE_BENCH)) {
		fprintf(stderr, "ECHO UDR received from ");
		CHECK_FCT( fd_msg_search_avp ( *msg, g_uda_cmd.uda_origin_host, &a) );
		if (a) {
			struct avp_hdr * hdr;
			CHECK_FCT( fd_msg_avp_hdr( a, &hdr ) );
			fprintf(stderr, "'%.*s'", (int)hdr->avp_value->os.len, hdr->avp_value->os.data);
		} else {
			fprintf(stderr, "no_Origin-Host");
		}
		fprintf(stderr, ", replying...\n");
	}
	
	/* Create answer header */
	// qry = *msg;
	CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, msg, 0 ) );
	ans = *msg;

	/* set application id */
	{
		struct msg_hdr * msg_hdr = NULL;
		ASSERT( 0 == fd_msg_hdr ( ans, &msg_hdr ) );
		msg_hdr->msg_appl	= SH_APP_ID;
		msg_hdr->msg_flags	= msg_hdr->msg_flags & (~CMD_FLAG_PROXIABLE);
	}
	
	/* resolve uda avps */
	// SEARCH_AVP_("Session-Id", 0, &uda_session_id);
	SEARCH_AVP_("DRMP", SH_VND_ID, &g_uda_cmd.uda_drmp);
	SEARCH_AVP_("Vendor-Specific-Application-Id", 0, &g_uda_cmd.uda_vendor_specific_application_id);
	SEARCH_AVP_("Result-Code", 0, &g_uda_cmd.uda_result_code);
	SEARCH_AVP_("Experimental-Result", 0, &g_uda_cmd.uda_experimental_result);
	SEARCH_AVP_("Auth-Session-State", 0, &g_uda_cmd.uda_auth_session_state);
	
	SEARCH_AVP_("Origin-Realm", 0, &g_uda_cmd.uda_origin_realm);

	SEARCH_AVP_("Supported-Features", SH_VND_ID, &g_uda_cmd.uda_supported_features );
	SEARCH_AVP_("Wildcarded-Public-Identity", SH_VND_ID, &g_uda_cmd.uda_wildcarded_public_identity );
	SEARCH_AVP_("Wildcarded-IMPU", SH_VND_ID, &g_uda_cmd.uda_wildcarded_impu );
	SEARCH_AVP_("User-Data", SH_VND_ID, &g_uda_cmd.uda_user_data );
	SEARCH_AVP_("OC-Supported-Features", SH_VND_ID, &g_uda_cmd.uda_oc_supported_features );
	SEARCH_AVP_("OC-OLR", SH_VND_ID, &g_uda_cmd.uda_oc_olr );
	

	EN_AVP_U32(g_uda_cmd.uda_drmp, 0, ans, "DRMP Ok")

	/* Set Vendor-Specific-Application-Id AVP if needed*/
	{
		CHECK_FCT( fd_msg_avp_new ( g_uda_cmd.uda_vendor_specific_application_id, 0, &grpavp ) );

		EN_GRPAVP_U32("Vendor-Id", 0, g_uda_cmd.uda_vendor_id, SH_VND_ID)
		EN_GRPAVP_U32("Auth-Application-Id", 0, g_uda_cmd.uda_auth_application_d, SH_APP_ID)
		EN_GRPAVP_U32("Acct-Application-Id", 0,g_uda_cmd.uda_acct_application_id, SH_APP_ID)

		CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, grpavp ) );
		
		LOG_D("Vendor-Specific-Application-Id Ok\n");
	}

	// EN_AVP_U32(g_uda_cmd.uda_result_code, 2001, ans, "Result-Code Ok\n")

	/* Set the Experimental-Result group AVP if needed*/
	{
		CHECK_FCT( fd_msg_avp_new ( g_uda_cmd.uda_experimental_result, 0, &grpavp )  );

		EN_GRPAVP_U32("Vendor-Id", 0, g_uda_cmd.uda_vendor_id, 3)
		EN_GRPAVP_U32("Experimental-Result-Code", 0, g_uda_cmd.uda_experimental_result_code, 3)
		CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, grpavp )  );
		
		LOG_D("Experimental-Result Ok\n");
	}
	
	EN_AVP_U32(g_uda_cmd.uda_auth_session_state, 0, ans, "Auth-Session-State Ok\n")

	/* Set the Supported-Features group AVP if needed*/
	{
		CHECK_FCT( fd_msg_avp_new ( g_uda_cmd.uda_supported_features, 0, &grpavp ) );
	
		EN_GRPAVP_U32("Vendor-Id", 0, g_uda_cmd.uda_vendor_id, 1)
		EN_GRPAVP_U32("Feature-List-ID", SH_VND_ID, g_uda_cmd.uda_feature_list_id, 2)
		EN_GRPAVP_U32("Feature-List", SH_VND_ID, g_uda_cmd.uda_feature_list, 3)

		CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, grpavp ) );
		
		LOG_D("Supported-Features Ok\n");
	}	
	EN_AVP_STR(g_uda_cmd.uda_wildcarded_public_identity, "wildcarded_public_id", ans, "wildcarded_public_id Ok")
	EN_AVP_STR( g_uda_cmd.uda_wildcarded_impu, "wildcarded_impu", ans, "wildcarded_impu Ok")
	EN_AVP_STR( g_uda_cmd.uda_user_data, "User-Data", ans, "User-Data Ok")
	/* Set the OC-Supported-Features AVP */
	{
		CHECK_FCT( fd_msg_avp_new ( g_uda_cmd.uda_oc_supported_features, 0, &grpavp ) );

		EN_GRPAVP_U64("OC-Feature-Vector", SH_VND_ID, g_uda_cmd.uda_oc_feature_vector, 1)

		CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, grpavp )  );

		LOG_D("OC-Feature-Vector Ok\n");
	}
	/* Set the OC-OLR group AVP if needed*/
	{
		CHECK_FCT( fd_msg_avp_new ( g_uda_cmd.uda_oc_olr, 0, &grpavp ) );
	
		EN_GRPAVP_U64("OC-Sequence-Number", SH_VND_ID, g_uda_cmd.uda_oc_sequence_number, 1)
		EN_GRPAVP_U32("OC-Report-Type", SH_VND_ID,g_uda_cmd.uda_oc_report_type, 2)
		EN_GRPAVP_U32("OC-Reduction-Percentage", SH_VND_ID, g_uda_cmd.uda_oc_reduction_percentage, 3)
		EN_GRPAVP_U32("OC-Validity-Duration", SH_VND_ID, g_uda_cmd.uda_oc_validity_duration, 4)

		CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, grpavp ) );
		
		LOG_D("Supported-Features Ok\n");
#if SH_TEST_ON
		fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, ans, fd_g_config->cnf_dict, 0, 1));
#endif
	}

	/* 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 ) );
	
	/* Add this value to the stats */
	CHECK_POSIX_DO( pthread_mutex_lock(&ta_conf->stats_lock), );
	ta_conf->stats.nb_echoed++;
	CHECK_POSIX_DO( pthread_mutex_unlock(&ta_conf->stats_lock), );

out:
	return 0;
}

extern struct dict_object * sh_appli;
extern struct dict_object * sh_vendor;
extern struct dict_object * udr_cmd;
extern struct dict_object * pur_cmd;

int sh_serv_init(void)
{
	struct disp_when data;
	
	TRACE_DEBUG(FULL, "Initializing dispatch callbacks for test");
	
	memset(&data, 0, sizeof(data));
	data.app = sh_appli;
	data.command = udr_cmd; // udr_cmd; // ta_cmd_r;
	
	/* fallback CB if command != Test-Request received */
	// CHECK_FCT( fd_disp_register( ta_fb_cb, DISP_HOW_APPID, &data, NULL, &sh_hdl_fb ) );

	/* Now specific handler for UDR */
	CHECK_FCT( fd_disp_register( sh_rec_udr, DISP_HOW_CC, &data, NULL, &sh_hdl_udr ) );

	memset(&data, 0, sizeof(data));
	data.app = sh_appli;
	data.command = pur_cmd; // udr_cmd; // ta_cmd_r;
	/* Now specific handler for PUR */
	CHECK_FCT( fd_disp_register( sh_rec_pur, DISP_HOW_CC, &data, NULL, &sh_hdl_pur ) );

	return 0;
}

void sh_serv_fini(void)
{
	if (sh_hdl_fb) {
		(void) fd_disp_unregister(&sh_hdl_fb, NULL);
	}
	if (sh_hdl_udr) {
		(void) fd_disp_unregister(&sh_hdl_udr, NULL);
	}
	if (sh_hdl_pur) {
		(void) fd_disp_unregister(&sh_hdl_pur, NULL);
	}

	return;
}
