/*
** SellaNMS
** $Id: netflow.h,v 1.16 2005/05/15 05:21:17 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

#ifndef __DGS_MODULE_MONITOR_NETFLOW_H
#define __DGS_MODULE_MONITOR_NETFLOW_H

#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

#include "defaults.h"
#include "util.h"
#include "storagedata.h"
#include "policy_framework/policy_framework.h"
#include "../monitor.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//#include <ucd-snmp-config.h>
//#include <ucd-snmp-includes.h>
#include <stl/queue>
#include <stl/vector>
#include <stl/string>

#define BUF_SIZE 1024
#define MODULE_MONITOR_NETFLOW_ITERATION_DEFAULT 0
#define MODULE_MONITOR_NETFLOW_INTERVAL_DEFAULT 300
#define MODULE_MONITOR_NETFLOW_PORT_DEFAULT 4444

struct netflow_v5_header {
	uint16_t	version;			//Format Version Number (2 byte)
	uint16_t	count;			//Number of flows exported (1-30)
	uint32_t	uptime;			 //Milliseconds since boot
	uint32_t	unix_secs;		//Seconds since Epoch
	uint32_t	unix_nsecs;		//Residual nanosecs since Epoch
	uint32_t	flow_sequence;	//Sequence counter of total flows seen
	uint8_t	engine_type;	//Type of flow-switching engine
	uint8_t	engine_id;		//Slot number of flow-switching engine
	uint16_t	reserved;		//Unused (zero) bytes
};

struct netflow_v5_record {
	uint32_t	src_addr;		//Source IP Address
	uint32_t	dst_addr;		//Destination IP Address
	uint32_t	next_hop;		//IP Address of Next Hop Router
	uint16_t	input_index;	//SNMP index of input interface
	uint16_t	output_index;	//SNMP index of output interface
	uint32_t	d_pkts;			//Packets in the flow
	uint32_t	d_octets;		//Total number of Layer 3 bytes in the packets of the flow
	uint32_t	uptime_first;	//SysUptime at start of flow
	uint32_t	uptime_last;	//SysUptime at the time the last packet of the flow was received
	uint16_t	src_port;		//TCP/UDP source port number or equivalent
	uint16_t	dst_port;		//TCP/UDP destination port number or equivalent
	uint8_t	pad_1;			//Unused (zero) bytes
	uint8_t	tcp_flags;		//Cumulative OR of TCP flags
	uint8_t	prot;				//IP protocol type (for example, TCP = 6; UDP = 17)
	uint8_t	tos;				//IP type of service (ToS)
	uint16_t	src_as;			//ASN of the source, either origin or peer
	uint16_t	dst_as;			//ASN of the destination, either origin or peer
	uint8_t	dst_mask;		//Source address prefix mask bits
	uint8_t	src_mask;		//Destination address prefix mask bits
	uint8_t	pad_2;			//Unused (zero) bytes
};

typedef queue<char*> netflow_queue_t;

class NetFlow: public Monitor {
	public:
		NetFlow();
		~NetFlow();

	protected:

	private:
		virtual RESULT execute();
		virtual RESULT readConfig();

		RESULT checkFilter(/* Something */);

		static RESULT receive_thread(void *args);
		static RESULT calc_thread(void *args);
		static RESULT storage_thread(void *args);
		static RESULT tree_thread(void *args);
		static RESULT proxy_thread(void *args);

		netflow_queue_t *netflow_queue;
		pthread_mutex_t *netflow_mutex;
		netflow_queue_t *proxy_queue;
		pthread_mutex_t *proxy_mutex;
		netflow_queue_t *tree_queue;
		pthread_mutex_t *tree_mutex;

		vector<string> *proxy;
		vector<unsigned int> *accept;
		vector<unsigned int> *reject;
		unsigned int port;
		unsigned int convert;
};

#endif

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim600: fdm=marker
** vim: noet ts=3 sw=3
*/
