/*
** CBLM v3
** $Id: common.h,v 1.9 2008/02/04 05:19:50 sella Exp $
** Copyright (c) 2004-2007 James M. Sella. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
** 
** Cheap Bastard Latency Monitor v3
*/

#ifndef __DGS_COMMON_H
#define __DGS_COMMON_H

#ifdef HAVE_CONFIG_H
	#include <config.h>
#endif

#ifndef _GNU_SOURCE
	#define _GNU_SOURCE
#endif

#include <snmptrap.h>

#ifndef __USE_BSD
	#define __USE_BSD
#endif
#include <netinet/ip.h>

#ifndef __FAVOR_BSD
	#define __FAVOR_BSD
#endif
#include <netinet/udp.h>

#include <syslog.h>
#include <assert.h>

#define PROGRAM_PKGN CBLM_PACKAGE
#define PROGRAM_NAME CBLM_PROGRAM
#define PROGRAM_VERS CBLM_VERSION
#define PROGRAM_BUGS CBLM_BUGREPORT

//#define DB_RECON_WAIT (300) /* 5 min */
#define DB_RECON_WAIT (30) /* 5 min */
#define DEF_SYSLOG_FACILITY LOG_LOCAL0

#define DEBUG_ERROR		1 /* Near fatal condition. */
#define DEBUG_WARNING	3 /* Non-normal significant condition. */
#define DEBUG_NOTICE		5 /* Notice: Normal, but significant condition. */
#define DEBUG_INTERNAL	7 /* Intertnal debugging information. Low traffic. */
#define DEBUG_SQL			8 /* SQL statements. Medium traffic. */
#define DEBUG_DUMP		9 /* Dump of internal raw data. High traffic. */
#define DEBUG_MAXVAL		9

#define WARNING_DELAY 300
#define MAX_SIZE 65535

struct packet_t {
	uint8_t version;		// Protocol version.
	struct in_addr src; 	// Sender. Not touched by NAT.
	struct in_addr dst;	// Destination. Not touched by NAT.
	uint64_t tx;			// Transmit time (usec) at originating probe.
	uint64_t ox;			// Receive time (usec) on remote probe (One-way TT)
	uint64_t rx;			// Receive time (usec) on originating probe (RTT calc)
	uint8_t key[16];		// Authorization key (weak).
	uint8_t set;			// The set this packet belongs to.
	uint16_t tot;			// Total number of packets within this set.
	uint16_t seq;			// Packet sequence within this set.
	uint8_t tx_tos;		// Transmit ToS bits.
	uint8_t ox_tos;		// Received ToS bits on remote probe.
	uint8_t rx_tos;		// Received ToS bits on originating probe.
	uint8_t tx_pri;		// Transmit Priority bits.
	uint8_t ox_pri;		// Received Priority bits on remote probe.
	uint8_t rx_pri;		// Received Priority bits on originating probe.
	uint8_t pattern[0];	// Pattern used to bulk up payload size.
} __attribute__((__packed__));

struct packet_list_t {
	struct packet_t packet;
	struct packet_list_t *next;
};

typedef struct args_t {
	char*		db_host;
	int		db_port;
	char*		db_sock;
	char*		db_name;
	char*		db_user;
	char*		db_pass;
	int		db_comp;
	int		write;
	int		print;
	char*		name;
	char*		location;
	char*		description;
	struct in_addr	source;
	struct in_addr	interface;
	char		*config;
	char		*user;
	char		*group;
	char		*chroot;
	char		*stats;
	int		nice;
	int		debug;
} _args_t;

typedef struct tos_t {
	short int tos;
	short int pri;
	struct tos_t* next;
} _tos_t;

typedef struct prefs_t {
	int		first:1;		// First read of the prefercnes (db)
	int		ignore:1;	// Ignore updates to preferences (db)
	int		fragment:2;	// Fragment packets.
	int		trk_ott:2;	// Track one-way delay OTT
	int		trk_jtr:2;	// Track Jitter
	int		trk_tos:2;	// Track ToS changes
	int		trk_pri:2;	// Track Priority changes
	int		trk_ooo:2;	// Track out of order packets
	int		trk_dup:2;	// Track duplicate packets
	uint64_t	last;			// Last update to the preferences (db)
	int16_t	port;			// UDP port to run traffic on
	unsigned int rate;	// Packets per minute per tos type
	unsigned int size;	// Payload size in bytes
	unsigned int ttl;		// Packet TTL
	unsigned int maxott;	// Max TTL in ms
	unsigned int maxage;	// Max age (sec) of probe before ignoring it.
	struct tos_t* tos;	// List of ToS types to transmit
	struct _snmptrap_peer_t *peer;
	char		key[17];		// Authorization key (weak)
	char*		pattern;		// Pattern to load within the packets
} _prefs_t;

typedef struct triptime_t {
	int32_t ott;
	int32_t rtt;
	struct triptime_t* next;
} _triptime_t;

typedef struct data_t {
	struct in_addr src;
	struct in_addr dst;

	uint64_t timestamp;	// Data timestamp
	uint64_t last_warn;	// Last warning
	uint64_t last_alarm;	// Last alarm

	uint32_t rx;
	uint32_t tx;

	int32_t ott_lst;
	int32_t rtt_lst;
	struct triptime_t* tt; // List of trip times (calculate jitter)

	int32_t rtt_min;
	int32_t rtt_max;
	uint64_t rtt_avg;

	int32_t ott_min;
	int32_t ott_max;
	uint64_t ott_avg;

	int32_t jtr_min;
	int32_t jtr_max;
	uint64_t jtr_avg;

	uint32_t set;		// The set this packet belongs to.
	uint32_t seq;		// Packet sequence within this set.
	uint32_t tos;		// ToS bits used.
	uint32_t pri;		// Priority bits used.

	uint32_t tos_a2z;	// ToS A to Z error count.
	uint32_t tos_z2a;	// ToS Z to A error count.

	uint32_t pri_a2z;	// Priority A to Z error count.
	uint32_t pri_z2a;	// Priority Z to A error count.

	uint32_t pkt_ooo;	// Out of order packets;
	uint32_t pkt_dup;	// Duplicate packets;
} _data_t;

struct data_list_t {
	struct data_t data;
	struct data_list_t *next;
};

typedef struct probe_t {
	struct in_addr ip;
	struct probe_t* next;
} _probe_t;

#endif

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