#pragma once

#include <features.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>
#include <netinet/in.h>    //defines struct in_addr
#include <arpa/inet.h>

enum tfe_conn_dir
{
	CONN_DIR_DOWNSTREAM = 0,    //From client to proxy, aka client-side.
	CONN_DIR_UPSTREAM,        //From proxy to server, aka server-side.
	__CONN_DIR_MAX
};

/* network-order */
struct tfe_stream_addr_tuple4_v4
{
	struct in_addr saddr;    /* network order */
	struct in_addr daddr;    /* network order */
	in_port_t source;    /* network order */
	in_port_t dest;        /* network order */
};

#ifndef IPV6_ADDR_LEN
#define IPV6_ADDR_LEN    (sizeof(struct in6_addr))
#endif

struct tfe_stream_addr_tuple4_v6
{
	struct in6_addr saddr;
	struct in6_addr daddr;
	in_port_t source;        /* network order */
	in_port_t dest;        /* network order */
};

#define GRE_TAG_LEN        (4)
struct tfe_stream_addr_gre
{
	uint16_t gre_id;
};

#define VLAN_ID_MASK        (0x0FFF)
#define VLAN_TAG_LEN        (4)
struct tfe_stream_addr_vlan
{
	uint16_t vlan_id;    /* network order */
};

struct tfe_stream_addr_pppoe_session
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
	unsigned int ver:4;
	unsigned int type:4;
#endif
#if __BYTE_ORDER == __BIG_ENDIAN
    unsigned int type:4;
    unsigned int ver:4;
#endif
	unsigned char code;
	unsigned short session_id;
};

#ifndef MAC_ADDR_LEN
#define MAC_ADDR_LEN        (6)
#endif

struct tfe_stream_addr_mac
{
	uint8_t src_mac[MAC_ADDR_LEN]; /* network order */
	uint8_t dst_mac[MAC_ADDR_LEN]; /* network order */
};

struct tfe_stream_addr_ipv4
{
	struct in_addr saddr;    /* network order */
	struct in_addr daddr;    /* network order */
	in_port_t source;            /* network order */
	in_port_t dest;            /* network order */
};

struct tfe_stream_addr_ipv6
{
	struct in6_addr saddr; /* network order */
	struct in6_addr daddr; /* network order */
	in_port_t source;/* network order */
	in_port_t dest;/* network order */
};

struct tfe_stream_addr_tcp
{
	in_port_t source; /* network order */
	in_port_t dest;    /* network order */
};

struct tfe_stream_addr_udp
{
	in_port_t source; /* network order */
	in_port_t dest;    /* network order */
};

struct tfe_stream_addr_l2tp
{
	uint32_t tunnelid; /* network order */
	uint32_t sessionid; /* network order */
};

enum tfe_stream_addr_type
{
	TFE_ADDR_INIT = 0,
	/* 1, 基于IPv4地址的四元组信息 */
	TFE_ADDR_IPV4,
	/* 2, 基于IPv6地址的四元组信息 */
	TFE_ADDR_IPV6,
	TFE_ADDR_VLAN,
	TFE_ADDR_MAC,
	TFE_ADDR_ARP = 5,
	TFE_ADDR_GRE,
	TFE_ADDR_MPLS,
	TFE_ADDR_PPPOE_SES,
	TFE_ADDR_TCP,
	TFE_ADDR_UDP = 10,
	TFE_ADDR_L2TP,
	/* 12, 混合地址类型, 基于IPv4地址的四元组信息 */
	TFE_ADDR_STREAM_TUPLE4_V4,
	/* 13, 混合地址类型, 基于IPv6地址的四元组信息 */
	TFE_ADDR_STREAM_TUPLE4_V6,
};

struct tfe_stream_addr
{
	enum tfe_stream_addr_type addrtype;
	uint8_t addrlen; /* 地址结构长度 */

	union
	{
		struct tfe_stream_addr_tuple4_v4 tuple4_v4[0];
		struct tfe_stream_addr_tuple4_v6 tuple4_v6[0];
		struct tfe_stream_addr_ipv4 ipv4[0];
		struct tfe_stream_addr_ipv6 ipv6[0];
		struct tfe_stream_addr_vlan vlan[0];
		struct tfe_stream_addr_mac mac[0];
		struct tfe_stream_addr_gre gre[0];
		struct tfe_stream_addr_tcp tcp[0];
		struct tfe_stream_addr_udp udp[0];
		struct tfe_stream_addr_pppoe_session pppoe_ses[0];
		struct tfe_stream_addr_l2tp l2tp[0];
		unsigned char paddr[0];
	};
};
const char* tfe_stream_conn_dir_to_str(enum tfe_conn_dir dir);
void tfe_stream_addr_free(struct tfe_stream_addr *addr);
struct tfe_stream_addr * tfe_stream_addr_create_by_fd(int fd, enum tfe_conn_dir dir);
struct tfe_stream_addr * tfe_stream_addr_reverse(struct tfe_stream_addr * orin);

//Follow function's returned pointer should be passed  to  free to release the allocated storage when it is no longer needed.
char* tfe_string_addr_create_by_fd(int fd, enum tfe_conn_dir dir);
char * tfe_stream_addr_to_str(const struct tfe_stream_addr * addr);
int tfe_stream_addr_str_split(char* addr_str, const char** sip, const char** sport, const char** dip, const char** dport);