/*********************************************************************************************************
* Software License Agreement (BSD License)                                                               *
* Author: Sebastien Decugis <sdecugis@freediameter.net>							 *
*													 *
* Copyright (c) 2015, 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.								 *
*********************************************************************************************************/

#include "fdproto-internal.h"
#include <time.h>

/* This file contains helpers functions to be reused as callbacks in the struct dict_type_data structure.
There are three callbacks there:

 - type_encode    :
 - type_interpret :
 	  Those two callbacks allow to manipulate more natural structures of data in the code, and to 
	map transparently these natural structures with the AVP-encoded format by calling the functions
	msg_avp_value_encode or msg_avp_value_interpret.
 - type_dump : 
 	  This callback if provided gives a more human-readable debug information.

 */

/****************************/
/*    Address  AVP  type    */
/****************************/

/* The interpret and encode functions work with a "struct sockaddr_storage" pointer for mapping 
the contents of the AVP */

int fd_dictfct_Address_encode(void * data, union avp_value * avp_value)
{
	sSS * ss = (sSS *) data;
	uint16_t AddressType = 0;
	size_t	size = 0;
	unsigned char * buf = NULL;
	
	TRACE_ENTRY("%p %p", data, avp_value);
	CHECK_PARAMS( data && avp_value  );
	
	switch (ss->ss_family) {
		case AF_INET:
			{
				/* We are encoding an IP address */
				sSA4 * sin = (sSA4 *)ss;
				
				AddressType = 1;/* see http://www.iana.org/assignments/address-family-numbers/ */
				size = 6;	/* 2 for AddressType + 4 for data */
				
				CHECK_MALLOC(  buf = malloc(size)  );
				
				/* may not work because of alignment: *(uint32_t *)(buf+2) = htonl(sin->sin_addr.s_addr); */
				memcpy(buf + 2, &sin->sin_addr.s_addr, 4);
			}
			break;
				
		case AF_INET6:
			{
				/* We are encoding an IPv6 address */
				sSA6 * sin6 = (sSA6 *)ss;
				
				AddressType = 2;/* see http://www.iana.org/assignments/address-family-numbers/ */
				size = 18;	/* 2 for AddressType + 16 for data */
				
				CHECK_MALLOC(  buf = malloc(size)  );
				
				/* The order is already good here */
				memcpy(buf + 2, &sin6->sin6_addr.s6_addr, 16);
				
			}
			break;
				
		default:
			CHECK_PARAMS( AddressType = 0 );
	}
	
	*(uint16_t *)buf = htons(AddressType);

	avp_value->os.len = size;
	avp_value->os.data = buf;
	
	return 0;
}

int fd_dictfct_Address_interpret(union avp_value * avp_value, void * interpreted)
{
	uint16_t AddressType = 0;
	unsigned char * buf;
	
	TRACE_ENTRY("%p %p", avp_value, interpreted);
	
	CHECK_PARAMS( avp_value && interpreted && (avp_value->os.len >= 2)  );
	
	AddressType = ntohs(*(uint16_t *)avp_value->os.data);
	buf = &avp_value->os.data[2];
	
	switch (AddressType) {
		case 1 /* IP */:
			{
				sSA4 * sin = (sSA4 *)interpreted;
				
				CHECK_PARAMS(  avp_value->os.len == 6  );
				
				sin->sin_family = AF_INET;
				/* sin->sin_addr.s_addr = ntohl( * (uint32_t *) buf); -- may not work because of bad alignment */
				memcpy(&sin->sin_addr.s_addr, buf, 4);
			}
			break;
				
		case 2 /* IP6 */:
			{
				sSA6 * sin6 = (sSA6 *)interpreted;
				
				CHECK_PARAMS(  avp_value->os.len == 18  );
				
				sin6->sin6_family = AF_INET6;
				memcpy(&sin6->sin6_addr.s6_addr, buf, 16);
				
			}
			break;
				
		default:
			CHECK_PARAMS( AddressType = 0 );
	}
	
	return 0;
}

/* Dump the content of an Address AVP */
DECLARE_FD_DUMP_PROTOTYPE(fd_dictfct_Address_dump, union avp_value * avp_value)
{
	union {
		sSA	sa;
		sSS	ss;
		sSA4	sin;
		sSA6	sin6;
	} s;
	uint16_t fam;
	
	FD_DUMP_HANDLE_OFFSET();
	
	memset(&s, 0, sizeof(s));
	
	/* The first two octets represent the address family, http://www.iana.org/assignments/address-family-numbers/ */
	if (avp_value->os.len < 2) {
		CHECK_MALLOC_DO( fd_dump_extend(FD_DUMP_STD_PARAMS, "[invalid length: %zd]", avp_value->os.len), return NULL);
		return *buf;
	}
	
	/* Following octets are the address in network byte order already */
	fam = avp_value->os.data[0] << 8 | avp_value->os.data[1];
	switch (fam) {
		case 1:
			/* IP */
			s.sa.sa_family = AF_INET;
			if ((avp_value->os.len != 6) && (avp_value->os.len != 8)) {
				CHECK_MALLOC_DO( fd_dump_extend(FD_DUMP_STD_PARAMS, "[invalid IP length: %zd]", avp_value->os.len), return NULL);
				return *buf;
			}
			memcpy(&s.sin.sin_addr.s_addr, avp_value->os.data + 2, 4);
			if (avp_value->os.len == 8)
				memcpy(&s.sin.sin_port, avp_value->os.data + 6, 2);
			break;
		case 2:
			/* IP6 */
			s.sa.sa_family = AF_INET6;
			if ((avp_value->os.len != 18) && (avp_value->os.len != 20)) {
				CHECK_MALLOC_DO( fd_dump_extend(FD_DUMP_STD_PARAMS, "[invalid IP6 length: %zd]", avp_value->os.len), return NULL);
				return *buf;
			}
			memcpy(&s.sin6.sin6_addr.s6_addr, avp_value->os.data + 2, 16);
			if (avp_value->os.len == 20)
				memcpy(&s.sin6.sin6_port, avp_value->os.data + 18, 2);
			break;
		case 8:
			/* E.164 */
			CHECK_MALLOC_DO( fd_dump_extend(FD_DUMP_STD_PARAMS, "%.*s", (int)(avp_value->os.len-2), avp_value->os.data+2), return NULL);
			return *buf;
		default:
			CHECK_MALLOC_DO( fd_dump_extend(FD_DUMP_STD_PARAMS, "[unsupported family: 0x%hx]", fam), return NULL);
			return *buf;
	}
	
	return fd_sa_dump(FD_DUMP_STD_PARAMS, &s.sa, NI_NUMERICHOST);
}



/*******************************/
/*    UTF8String  AVP  type    */
/*******************************/

/* Dump the AVP in a natural human-readable format. This dumps the complete length of the AVP, it is up to the caller to truncate if needed */
DECLARE_FD_DUMP_PROTOTYPE(fd_dictfct_UTF8String_dump, union avp_value * avp_value)
{
	size_t l;
	FD_DUMP_HANDLE_OFFSET();
	
	l = avp_value->os.len;
	/* Just in case the string ends in invalid UTF-8 chars, we shorten it */
	while ((l > 0) && (avp_value->os.data[l - 1] & 0x80)) {
		/* this byte is start or cont. of multibyte sequence, as we do not know the next byte we need to delete it. */
		l--;
		if (avp_value->os.data[l] & 0x40)
			break; /* This was a start byte, we can stop the loop */
	}
	
	CHECK_MALLOC_DO( fd_dump_extend(FD_DUMP_STD_PARAMS, "\"%.*s\"", (int)l, (char *)avp_value->os.data), return NULL);
	
	return *buf;
}


/*******************************/
/*    Time  AVP  type    */
/*******************************/

/* The interpret and encode functions work with a "time_t" pointer for mapping 
the contents of the AVP */

/* Unix Epoch starts 1970-01-01, NTP 0 is at 1900-01-01 */
#define DIFF_EPOCH_TO_NTP ((365*(1970-1900) + 17ul) * 24 * 60 * 60)

static int diameter_string_to_time_t(const char *str, size_t len, time_t *result) {
    time_t time_stamp;
    CHECK_PARAMS(len == 4);
   
    time_stamp = (((unsigned long)(str[0]&0xff))<<24) + ((str[1]&0xff)<<16) + ((str[2]&0xff)<<8) + ((str[3]&0xff));
    time_stamp -= DIFF_EPOCH_TO_NTP;
#ifdef FIX__NEEDED_FOR_YEAR_2036_AND_LATER
/* NTP overflows in 2036; after that, values start at zero again */
#define NTP_OVERFLOW_CORRECTION (0x100000000ull)
    /* XXX: debug and find correct conversion */
    if (str[0] & 0x80 == 0x00) {
        time_stamp += NTP_OVERFLOW_CORRECTION;
    }
#endif
    *result = time_stamp;
    return 0;
}

static int time_t_to_diameter_string(time_t time_stamp, char **result) {
    uint64_t out = time_stamp;
    char *conv;
    /* XXX: 2036 fix */
    out += DIFF_EPOCH_TO_NTP;
    CHECK_PARAMS( (out >> 32) == 0);

    CHECK_MALLOC(conv=(char *)malloc(5));
    
    conv[0] = (out>>24) & 0xff;
    conv[1] = (out>>16) & 0xff;
    conv[2] = (out>> 8) & 0xff;
    conv[3] =  out      & 0xff;
    conv[4] = '\0';
    *result = conv;
    return 0;
}

int time_t_to_diameter_timestamp(char *strTime)
{
	time_t now;
	int out;
	
	time(&now);	

	out = now+DIFF_EPOCH_TO_NTP;

	strTime[0] = (out>>24) & 0xff;
	strTime[1] = (out>>16) & 0xff;
	strTime[2] = (out>> 8) & 0xff;
	strTime[3] =  out      & 0xff;
	strTime[4] = '\0';
  
	return 1;
}

int fd_dictfct_Time_encode(void * data, union avp_value * avp_value)
{
	char * buf;
	size_t len;
	
	TRACE_ENTRY("%p %p", data, avp_value);
	CHECK_PARAMS( data && avp_value  );
	
	CHECK_FCT( time_t_to_diameter_string( *((time_t *)data), &buf) );
	/* FIXME: return len from the function above? */ len = 4;
	
	avp_value->os.len = len;
	avp_value->os.data = (uint8_t *)buf;
	return 0;
}

int fd_dictfct_Time_interpret(union avp_value * avp_value, void * interpreted)
{
	TRACE_ENTRY("%p %p", avp_value, interpreted);
	
	CHECK_PARAMS( avp_value && interpreted );
	
	return diameter_string_to_time_t((const char *)avp_value->os.data, avp_value->os.len, interpreted);
}

static void _format_offs (long offset, char *buf) {
    int offs_hours, offs_minutes, sgn = 1;
    if (offset < 0) {
        offset = -offset;
        sgn = 1;
    }
    offs_hours = (int)(offset/3600);
    offs_minutes = (offset%3600)/60;

    char* s = buf;

    *(s++) = sgn == 1 ? '+' : '-';
    *(s++) = (char)(offs_hours/10) + '0';
    *(s++) = offs_hours%10 + '0';

    if (offs_minutes == 0) {
        *(s++) = '\0';
    } else {
        *(s++) = (char)(offs_minutes/10) + '0';
        *(s++) = offs_minutes%10 + '0';
        *(s++) = '\0';
    }
}

DECLARE_FD_DUMP_PROTOTYPE(fd_dictfct_Time_dump, union avp_value * avp_value)
{
	time_t val;
	struct tm conv;
	char tz_buf[7];
		
	FD_DUMP_HANDLE_OFFSET();
	
	if (avp_value->os.len != 4) {
		CHECK_MALLOC_DO( fd_dump_extend(FD_DUMP_STD_PARAMS, "[invalid length: %zd]", avp_value->os.len), return NULL);
		return *buf;
	}

	if (diameter_string_to_time_t((char *)avp_value->os.data, avp_value->os.len, &val) != 0) {
		CHECK_MALLOC_DO( fd_dump_extend(FD_DUMP_STD_PARAMS, "[time conversion error]"), return NULL);
		return *buf;
	}
	
	CHECK_MALLOC_DO( localtime_r(&val, &conv), return NULL);
	_format_offs(conv.tm_gmtoff, tz_buf);
	CHECK_MALLOC_DO( fd_dump_extend(FD_DUMP_STD_PARAMS, "%d%02d%02dT%02d%02d%02d%s", conv.tm_year+1900, conv.tm_mon+1, conv.tm_mday, conv.tm_hour, conv.tm_min, conv.tm_sec, tz_buf), return NULL);
	return *buf;
}

/* Check that a given AVP value contains all the characters from data in the same order */
static char error_message[80];
int fd_dictfct_CharInOS_check(void * data, union avp_value * val, char ** error_msg)
{
	char * inChar = data;
	char * inData = (char *)val->os.data;
	int i = 0;
	CHECK_PARAMS(data);
	while (*inChar != '\0') {
		while (i < val->os.len) {
			if (*inChar == inData[i++]) {
				inChar++;
				break;
			}
		}
		if (i >= val->os.len)
			break;
	}
	if (*inChar == '\0')
		return 0;
	
	if (error_msg) {
		snprintf(error_message, sizeof(error_message), "Could not find '%c' in AVP", *inChar);
		*error_msg = error_message;
	}
	return EBADMSG;
}
