/*********************************************************************************************************
* 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.								 *
*********************************************************************************************************/

/* Header file for the app_test extension. 
 *
 *  This extension provides a way to send configurable messages on the network
 *
 *  See the app_test.conf.sample file for the format of the configuration file.
 */
#ifndef __SH_APP_H__
#define __SH_APP_H__

#include "extension.h"
#include <signal.h>


#define SH_VND_ID	10415
#define SH_APP_ID_OLD	16777217
#define SH_APP_ID	4

#define	HW_VND_ID	2011

/* Mode for the extension */
#define MODE_SERV	0x1
#define	MODE_CLI	0x2
#define	MODE_BENCH	0x4



#ifndef TEST_APP_DEFAULT_SIGNAL
#define TEST_APP_DEFAULT_SIGNAL	SIGUSR1
#endif /* TEST_APP_DEFAULT_SIGNAL */

#if 0
/* encode avp */
#define SEARCH_AVP_OLD_(name, vendor_id, retval) \
	__avp.avp_code = 0;	\
	__avp.avp_name = name;	\
	__avp.avp_vendor = vendor_id;	\
	LOG_D("SEARCH_AVP_ avpName:%s avpVendorID:%d", name, SH_VND_ID); \
	CHECK_FCT_DO( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME_AND_VENDOR, &__avp, retval, ENOENT),  goto out )
#endif
#define SEARCH_AVP_(name, vendor_id, retval) \
{	struct dict_avp_request tmp_avp;\
	tmp_avp.avp_code = 0;	\
	tmp_avp.avp_name = name;	\
	tmp_avp.avp_vendor = vendor_id;	\
	LOG_D("SEARCH_AVP_ avpName:%s avpVendorID:%d", name, SH_VND_ID); \
	CHECK_FCT_DO( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME_AND_VENDOR, &tmp_avp, retval, ENOENT),  goto out ); \
}



#define EN_GRPAVP_U32(udr_search_name, vendor_id, udr, u32_val) \
{	\
	SEARCH_AVP_(udr_search_name, vendor_id, &udr);	\
	CHECK_FCT_DO( fd_msg_avp_new ( udr, 0, &avp ),  goto out );	\
	val.u32 = u32_val;	\
	CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ),  goto out );	\
	CHECK_FCT_DO( fd_msg_avp_add( grpavp, MSG_BRW_LAST_CHILD, avp ),  goto out );	\
}
#define EN_GRPAVP_U32_(udr_search_name, vendor_id, udr, u32_val, grpavp) \
{	\
	SEARCH_AVP_(udr_search_name, vendor_id, &udr);	\
	CHECK_FCT_DO( fd_msg_avp_new ( udr, 0, &avp ),  goto out );	\
	val.u32 = u32_val;	\
	CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ),  goto out );	\
	CHECK_FCT_DO( fd_msg_avp_add( grpavp, MSG_BRW_LAST_CHILD, avp ),  goto out );	\
}
#define EN_GRPAVP_U64(udr_search_name, vendor_id, udr, u64_val) \
{	\
	SEARCH_AVP_(udr_search_name, vendor_id, &udr);	\
	CHECK_FCT_DO( fd_msg_avp_new ( udr, 0, &avp ),  goto out );	\
	val.u64 = u64_val;	\
	CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ),  goto out );	\
	CHECK_FCT_DO( fd_msg_avp_add( grpavp, MSG_BRW_LAST_CHILD, avp ), goto out );	\
}

#define EN_GRPAVP_U64_(udr_search_name, vendor_id, udr, u64_val, grpavp) \
{	\
	SEARCH_AVP_(udr_search_name, vendor_id, &udr);	\
	CHECK_FCT_DO( fd_msg_avp_new ( udr, 0, &avp ),  goto out );	\
	val.u64 = u64_val;	\
	CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ),  goto out );	\
	CHECK_FCT_DO( fd_msg_avp_add( grpavp, MSG_BRW_LAST_CHILD, avp ), goto out );	\
}
#define EN_GRPAVP_STR(udr_search_name, vendor_id, udr, str, grpavp) \
{	\
	SEARCH_AVP_(udr_search_name, vendor_id, &udr);	\
	CHECK_FCT_DO( fd_msg_avp_new ( udr, 0, &avp ),  goto out );	\
	val.os.data = (uint8_t*)(str);	\
	val.os.len	= strlen(str);	\
	CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ),  goto out );	\
	CHECK_FCT_DO( fd_msg_avp_add( grpavp, MSG_BRW_LAST_CHILD, avp ), goto out );	\
}
#define EN_GRPAVP_STR_BCD(udr_search_name, vendor_id, udr, str, str_len, grpavp) \
{	\
	SEARCH_AVP_(udr_search_name, vendor_id, &udr);	\
	CHECK_FCT_DO( fd_msg_avp_new ( udr, 0, &avp ),  goto out );	\
	val.os.data = (uint8_t*)(str); \
	memcpy(val.os.data, str, str_len);\
	val.os.len	= str_len;	\
	CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ),  goto out );	\
	CHECK_FCT_DO( fd_msg_avp_add( grpavp, MSG_BRW_LAST_CHILD, avp ), goto out );	\
}

#define EN_AVP_U32(udr, u32_val, msg_name, log_info) \
{	\
	CHECK_FCT_DO( fd_msg_avp_new ( udr, 0, &avp ), goto out );	\
	val.u32 = u32_val;	\
	CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out );	\
	CHECK_FCT_DO( fd_msg_avp_add( msg_name, MSG_BRW_LAST_CHILD, avp ), goto out );	\
	LOG_N("%s\n", log_info);	\
}

#define EN_AVP_STR(udr, str, msg_name, log_info) \
{	\
	CHECK_FCT_DO( fd_msg_avp_new ( udr, 0, &avp ) ,  goto out );	\
	val.os.data = (unsigned char *)(str);	\
	val.os.len  = strlen(str);	\
	CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ),  goto out );	\
	CHECK_FCT_DO( fd_msg_avp_add( msg_name, MSG_BRW_LAST_CHILD, avp ),  goto out );	\
	LOG_N("%s\n", log_info);	\
}

#define EN_AVP_BCDSTR(udr, str, tmplen, msg_name, log_info) \
{	\
	CHECK_FCT_DO( fd_msg_avp_new ( udr, 0, &avp ) ,  goto out );	\
	val.os.data = (unsigned char *)(str);	\
	val.os.len  = tmplen;	\
	CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ),  goto out );	\
	CHECK_FCT_DO( fd_msg_avp_add( msg_name, MSG_BRW_LAST_CHILD, avp ),  goto out );	\
	LOG_N("%s\n", log_info);	\
}

/* parse avp */
#define PARSE_GRPAVP_U32(grpavp, obj, avp, avphdr, tip, val) \
CHECK_FCT( fd_sh_avp_search_avp ( grpavp, obj, &avp) ); \
if(avp != NULL)	\
{	\
	CHECK_FCT( fd_msg_avp_hdr( avp, &avphdr ) );	\
}
// LOG_N("fj    %s:%d", tip, val);

#define PARSE_GRPAVP_U64(grpavp, obj, avp, avphdr, tip, val) \
CHECK_FCT( fd_sh_avp_search_avp ( grpavp, obj, &avp) ); \
if(avp != NULL)	\
{	\
	CHECK_FCT( fd_msg_avp_hdr( avp, &avphdr ) );	\
}
// LOG_N("fj	%s:%lu", tip, val); 


#define PARSE_GRPAVP_STR(grpavp, obj, avp, avphdr, tip, val) \
CHECK_FCT( fd_sh_avp_search_avp ( grpavp, obj, &avp) ); \
if(avp != NULL)	\
{	\
	CHECK_FCT( fd_msg_avp_hdr( avp, &avphdr ) );	\
}



#define PARSE_AVP_U32(msg, obj, avp, avphdr, tip, val) \
CHECK_FCT( fd_msg_search_avp ( msg, obj, &avp) ); \
if(avp != NULL)	\
{	\
	CHECK_FCT( fd_msg_avp_hdr( avp, &avphdr ) );	\
}
// LOG_N("fj %s:%d", tip, val);

#define PARSE_AVP_STR(msg, obj, avp, avphdr, tip, val) \
CHECK_FCT( fd_msg_search_avp ( msg, obj, &avp) );	\
if(avp != NULL)	\
{	\
	CHECK_FCT( fd_msg_avp_hdr( avp, &avphdr ) );	\
}
// LOG_N("fj %s:%s", tip, val);



/* udr avp */
/*
< Session-Id >
[ DRMP ]
{ Vendor-Specific-Application-Id }
{ Auth-Session-State }
{ Origin-Host }
{ Origin-Realm }
[ Destination-Host ]
{ Destination-Realm }
*[ Supported-Features ]
{ User-Identity }
[ Wildcarded-Public-Identity ]
[ Wildcarded-IMPU ]
[ Server-Name ]
*[ Service-Indication ]
*{ Data-Reference }
*[ Identity-Set ]
[ Requested-Domain ]
[ Current-Location ]
*[ DSAI-Tag ]
[ Session-Priority ]
[ User-Name ]
[ Requested-Nodes ]
[ Serving-Node-Indication ]
[ Pre-paging-Supported ]
[ Local-Time-Zone-Indication ]
[ UDR-Flags ]
[ Call-Reference-Info ]
[ OC-Supported-Features ]
*[ AVP ]
*[ Proxy-Info ]
*[ Route-Record ]
*/
struct sh_udr_msg{
	struct dict_object * udr_session_id ;
	struct dict_object * udr_drmp;
	
	struct dict_object * udr_vendor_specific_application_id;
	struct dict_object * udr_auth_application_d;
	struct dict_object * udr_acct_application_id;

	struct dict_object * udr_auth_session_state;
	struct dict_object * udr_origin_host;
	struct dict_object * udr_origin_realm;
	struct dict_object * udr_destination_host;
	struct dict_object * udr_destination_realm;
	
	struct dict_object * udr_supported_features;
	struct dict_object * udr_vendor_id;
	struct dict_object * udr_feature_list_id;
	struct dict_object * udr_feature_list;
	
	struct dict_object * udr_user_identity;
	struct dict_object * udr_public_identity;
	struct dict_object * udr_msisdn;
	
	struct dict_object * udr_wildcarded_public_identity;
	struct dict_object * udr_wildcarded_impu;
	struct dict_object * udr_server_name;
	struct dict_object * udr_service_indication;
	struct dict_object * udr_data_reference;
	struct dict_object * udr_identity_set;
	struct dict_object * udr_requested_domain;
	struct dict_object * udr_current_location;
	struct dict_object * udr_dsai_tag;
	struct dict_object * udr_session_priority;
	struct dict_object * udr_user_name;
	struct dict_object * udr_requested_nodes;
	struct dict_object * udr_serving_node_indication;
	struct dict_object * udr_pre_paging_supported;
	struct dict_object * udr_local_time_zone_indication;
	struct dict_object * udr_udr_flags;
	
	struct dict_object * udr_call_reference_info;
	struct dict_object * udr_call_reference_number;
	struct dict_object * udr_as_number;
	
	struct dict_object * udr_oc_supported_features;
	struct dict_object * udr_oc_feature_vector;
	
	struct dict_object * udr_proxy_info;
	struct dict_object * udr_route_record;
};

/* uda avp */
/*
< Session-Id >
[ DRMP ]
{ Vendor-Specific-Application-Id }
[ Result-Code ]
[ Experimental-Result ]
{ Auth-Session-State }
{ Origin-Host }
{ Origin-Realm }
*[ Supported-Features ]
[ Wildcarded-Public-Identity ]
[ Wildcarded-IMPU ]
[ User-Data ]
[ OC-Supported-Features ]
[ OC-OLR ]
*[ AVP ]
*[ Failed-AVP ]
*[ Proxy-Info ]
*[ Route-Record ]
*/
struct sh_uda_msg{
	struct dict_object * uda_session_id;
	struct dict_object * uda_drmp;
	
	struct dict_object * uda_vendor_specific_application_id;
	struct dict_object * uda_vendor_id;
	struct dict_object * uda_auth_application_d;
	struct dict_object * uda_acct_application_id;
	
	struct dict_object * uda_result_code;
	
	struct dict_object * uda_experimental_result;
	struct dict_object * uda_experimental_result_code;
	
	struct dict_object * uda_auth_session_state;
	struct dict_object * uda_origin_host;
	struct dict_object * uda_origin_realm;
	
	struct dict_object * uda_supported_features;
	struct dict_object * uda_feature_list_id;
	struct dict_object * uda_feature_list;
	
	struct dict_object * uda_wildcarded_public_identity;
	struct dict_object * uda_wildcarded_impu;
	struct dict_object * uda_user_data;
	struct dict_object * uda_oc_supported_features;
	struct dict_object * uda_oc_feature_vector;
	
	struct dict_object * uda_oc_olr;
	struct dict_object * uda_oc_sequence_number;
	struct dict_object * uda_oc_report_type;
	struct dict_object * uda_oc_reduction_percentage;
	struct dict_object * uda_oc_validity_duration;
	
	struct dict_object * uda_failed_avp;
	struct dict_object * uda_proxy_info;
	struct dict_object * uda_route_record;
};


/*
< Profile-Update-Request > ::= < Diameter Header: 307, REQ, PXY, 16777217 >
		< Session-Id >
		[ DRMP ]
		{ Vendor-Specific-Application-Id }
		{ Auth-Session-State }
		{ Origin-Host }
		{ Origin-Realm }
		[ Destination-Host ]
		{ Destination-Realm }
		*[ Supported-Features ]
		{ User-Identity }
		[ Wildcarded-Public-Identity ]
		[ Wildcarded-IMPU ]
		[ User-Name ]
		*{ Data-Reference }
		{ User-Data }
		[ OC-Supported-Features ]
		*[ AVP ]
		*[ Proxy-Info ]
		*[ Route-Record ]
*/
struct sh_pur_msg{
	struct dict_object * pur_session_id ;
	struct dict_object * pur_drmp;
	
	struct dict_object * pur_vendor_specific_application_id;
	struct dict_object * pur_auth_application_d;
	struct dict_object * pur_acct_application_id;

	struct dict_object * pur_auth_session_state;
	struct dict_object * pur_origin_host;
	struct dict_object * pur_origin_realm;
	struct dict_object * pur_destination_host;
	struct dict_object * pur_destination_realm;
	
	struct dict_object * pur_supported_features;
	struct dict_object * pur_vendor_id;
	struct dict_object * pur_feature_list_id;
	struct dict_object * pur_feature_list;
	
	struct dict_object * pur_user_identity;
	struct dict_object * pur_public_identity;
	struct dict_object * pur_msisdn;
	
	struct dict_object * pur_wildcarded_public_identity;
	struct dict_object * pur_wildcarded_impu;
	struct dict_object * pur_user_name;
	struct dict_object * pur_data_reference;
	
	struct dict_object * pur_user_date;
	
	struct dict_object * pur_oc_supported_features;
	struct dict_object * pur_oc_feature_vector;
	
	struct dict_object * pur_proxy_info;
	struct dict_object * udr_route_record;
};


/*
< Profile-Update-Answer > ::=< Diameter Header: 307, PXY, 16777217 >
		< Session-Id >
		[ DRMP ]
		{ Vendor-Specific-Application-Id }
		[ Result-Code ]
		[ Experimental-Result ]
		{ Auth-Session-State }
		{ Origin-Host }
		{ Origin-Realm }
		[ Wildcarded-Public-Identity ]
		[ Wildcarded-IMPU ]
		[ Repository-Data-ID ]
		[ Data-Reference ]
		*[ Supported-Features ]
		[ OC-Supported-Features ]
		[ OC-OLR ]
		*[ AVP ]
		*[ Failed-AVP ]
		*[ Proxy-Info ]
		*[ Route-Record ]
*/
struct sh_pua_msg{
	struct dict_object * pua_session_id ;
	struct dict_object * pua_drmp;
	
	struct dict_object * pua_vendor_specific_application_id;
	struct dict_object * pua_vendor_id;
	struct dict_object * pua_auth_application_d;
	struct dict_object * pua_acct_application_id;

	struct dict_object * pua_result_code;
	
	struct dict_object * pua_experimental_result;
	struct dict_object * pua_experimental_result_code;

	struct dict_object * pua_auth_session_state;
	
	struct dict_object * pua_origin_host;
	struct dict_object * pua_origin_realm;
	
	struct dict_object * pua_wildcarded_public_identity;
	struct dict_object * pua_wildcarded_impu;
	
	struct dict_object * pua_repository_data_id;
	struct dict_object * pua_service_indication;
	struct dict_object * pua_sequence_number;
	
	struct dict_object * pua_data_reference;

	struct dict_object * pua_supported_features;
	struct dict_object * pua_feature_list_id;
	struct dict_object * pua_feature_list;
	
	struct dict_object * pua_oc_supported_features;
	struct dict_object * pua_oc_feature_vector;

	struct dict_object * pua_oc_olr;
	struct dict_object * pua_oc_sequence_number;
	struct dict_object * pua_oc_report_type;
	struct dict_object * pua_oc_reduction_percentage;
	struct dict_object * pua_oc_validity_duration;
	
	struct dict_object * pua_proxy_info;
	struct dict_object * pua_route_record;
};



/* The module configuration */
struct ta_conf {
	uint32_t	vendor_id;	/* default 999999 */
	uint32_t	appli_id;	/* default 123456 */
	uint32_t	cmd_id;		/* default 234567 */
	uint32_t	avp_id;		/* default 345678 */
	uint32_t	long_avp_id;	/* default 0 */
	size_t		long_avp_len;	/* default 5000 */
	int		mode;		/* default MODE_SERV | MODE_CLI */
	char 	*	dest_realm;	/* default local realm */
	char 	*	dest_host;	/* default NULL */
	char 	*	user_name;	/* default NULL */
	int 		signal;		/* default TEST_APP_DEFAULT_SIGNAL */
	int		bench_concur;	/* default 100 */
	int		bench_duration; /* default 10 */
	struct ta_stats {
		unsigned long long	nb_echoed; /* server */
		unsigned long long	nb_sent;   /* client */
		unsigned long long	nb_recv;   /* client */
		unsigned long long	nb_errs;   /* client */
		unsigned long		shortest;  /* fastest answer, in microseconds */
		unsigned long		longest;   /* slowest answer, in microseconds */
		unsigned long		avg;       /* average answer time, in microseconds */
	} stats;
	pthread_mutex_t		stats_lock;
};
extern struct ta_conf * ta_conf;

/* Parse the configuration file */
int ta_conf_handle(char * conffile);

/* Handle incoming messages (server) */
int sh_serv_init(void);
void sh_serv_fini(void);

/* Create outgoing message (client) */
int sh_cli_init(void);
void sh_cli_finit(void);

/* Benchmark flavour */
int ta_bench_init(void);
void ta_bench_fini(void);

/* Initialize dictionary definitions */
int ta_dict_init(void);


int fd_sh_avp_search_avp ( struct avp * groupedavp, struct dict_object * what, struct avp ** avp );


#endif /* __SH_APP_H__ */
