/*
** CBLM v3
** $Id: prefs.h,v 1.9 2006/05/17 04:41:31 sella Exp $
** Copyright (c) 2004 James M. Sella. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
** 
** Cheap Bastard Latency Monitor v3
*/

#ifndef __DGS_PREFS_H
#define __DGS_PREFS_H

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

#include "db.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <mysql/mysql.h>

#define DEF_NICENESS 5
#define DEF_PORT 7447
#define DEF_RATE 120
#define DEF_TOS "0:0"
#define DEF_MAXOTT 1000
#define DEF_SIZE 128
#define DEF_PATTERN "kusabana" // Flower
#define DEF_KEY "o-soraizu" // Authorization
#define DEF_STATS "cblm.stats"
#define DEF_MAXAGE 90
#ifdef IPDEFTTL
	#define DEF_TTL IPDEFTTL
#else
	#define DEF_TTL 64
#endif

#define DEF_DBHOST ""
#define DEF_DBPORT MYSQL_PORT
#define DEF_DBTIMO 30 // Connection timeout
#define DEF_DBSOCK ""
#define DEF_DBNAME "cblm"
#define DEF_DBUSER "cblm"
#define DEF_DBPASS ""
#define DEF_DBPASS ""
#define DEF_DBCOMP 0

#define MAX_RATE 32000
#define MAX_MAXOTT 5000
#define MAX_SIZE 65535
#define MIN_MAXAGE 3
#define MAX_MAXAGE 86400
#define MAX_TTL 255

/* Max allowed (usec) to consider a sample garbage. */
#define MAX_RTT (5000 * 1000)	/* 5 sec */
#define MAX_OTT (MAX_RTT / 2)	/* 2.5 sec */
#define MAX_JTR MAX_RTT			/* 5 sec */

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;
	struct in_addr	source;
	struct in_addr	interface;
	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
	time_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
	char		key[17];		// Authorization key (weak)
	char*		pattern;		// Pattern to load within the packets
} _prefs_t;

/* Function Definitions. */
int get_args(int argc, char** argv);
void version(int flag);
void usage();
void use_args();

int read_prefs(int flag);
int write_prefs();
time_t last_prefs();
void add_tos(const char* str, int shift);

void free_args();
void free_prefs();
void free_tos();

void display_args();
void display_prefs(int tty);

#endif 

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