#include "./include/radius_pub.h"
#include "./include/radius_def.h"

// fix multiple definition====================================
BYTE			RADIUS_Version[4] = {1, 0, 0, 2};

int				radius_udp_access_socket_fd;

int				radius_udp_account_socket_fd;

int				radius_udp_disconnect_socket_fd;

int				radius_pid_select;

BYTE			radius_access_id_select;
BYTE			radius_accounting_id_select;
BYTE			radius_disconnect_id_select;

RADIUS_MSG		radius_msg_pdu;

RADIUS_PID		radius_pid[RADIUS_MAX_NUM_OF_PID];

BYTE			radius_udp_msg_recv_buf[RADIUS_MAX_LEN_OF_UDP_MSG];

WORD			radius_mon_flag;

BYTE			radius_asci_tmp_buf[RADIUS_MAX_ASCITMP_LEN];
BYTE			radius_asci_out_buf[RADIUS_MAX_ASCIIOUT_LEN];
BYTE			radius_asci_in_buf[RADIUS_MAX_ASCIIIN_LEN];

char				radius_msg_type_str[RADIUS_MAX_LEN_OF_MSG_TYEP];

RADIUS_ATTR_PARSE_FUNCTION			radius_attr_parse_function[RADIUS_MAX_NUM_OF_ATTRIBUTE];

RADIUS_SAP		radius_sap[RADIUS_MAX_NUM_OF_SAP];

RADIUS_NAS_INFO	radius_nas_info[RADIUS_MAX_NUM_OF_NAS];
//==============================================================

int radius_init(void)
{
	int		i;

	radius_init_socket();

	for (i = 0; i < RADIUS_MAX_NUM_OF_PID; i++)
	{
		radius_init_pid(i);
	}

	radius_attr_parse_init();

	memset(&radius_msg_pdu, 0, sizeof(RADIUS_MSG));

	memset(&radius_nas_info, 0, sizeof(RADIUS_NAS_INFO) * RADIUS_MAX_NUM_OF_NAS);
/*
	for (i = 0; i < RADIUS_MAX_NUM_OF_SAP; i++)
	{
		strcpy(radius_nas_share_key[i], "");
		strcpy(radius_nas_password[i], "");
	}*/

	radius_pid_select = 0;

	radius_access_id_select = 0;
	radius_accounting_id_select = 0;
	radius_disconnect_id_select = 0;

	strcpy((char *)radius_asci_in_buf, "\0");
	strcpy((char *)radius_asci_out_buf, "\0");
	strcpy((char *)radius_asci_tmp_buf, "\0");
	strcpy((char *)radius_udp_msg_recv_buf, "\0");
	strcpy(radius_msg_type_str, "\0");
	radius_mon_flag = RADIUS_MONITOR_NONE;
	radius_debug_set();

	return 0;
}

void radius_t10ms(void)
{
	static DWORD		cnt_100ms = 0;

	if (!(++cnt_100ms % 10))
	{
		radius_mon();
		cnt_100ms = 0;
	}
}

void radius_timer(void)
{
	radius_recv_udp_msg();

	radius_t10ms();

	radius_fsm();
}

int radius_bind(RADIUS_SAP *sap)
{
	int		i;

	if ((sap == NULL) || (sap->radius_ind == NULL) || (sap->radius_cnf == NULL))
		return -1;

	for (i = 0; i < RADIUS_MAX_NUM_OF_SAP; i++)
	{
		if ((radius_sap[i].radius_ind != NULL) || (radius_sap[i].radius_cnf != NULL))
			continue;
    
		memcpy(&radius_sap[i], sap, sizeof(RADIUS_SAP));

		radius_sap[i].name[RADIUS_MAX_LEN_OF_USER_NAME - 1] = '\0';
    
		return i;
	}

	return -1;
}

