/*
** CBLM v3
** $Id: packet.h,v 1.4 2006/05/10 02:36:07 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_PACKETS_H
#define __DGS_PACKETS_H

#include "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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <signal.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>

#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h> 
#include <errno.h>
#include <pthread.h>

#include <assert.h>

#define NOPROBES_WAIT 30 // Wait 30 sec before checking for probes again.
#define WARNING_DELAY 300

/* 60 second cycle, with 16 slices per second. */
#define CYCLE_SECONDS		60 // Transmit 'rate' packets every 60 seconds.
#define CYCLE_USEC			(CYCLE_SECONDS * 1000000)
#define SLICES_PER_SEC		16 // Higher values produce smoother output, but use more CPU.
#define SLICES_PER_CYCLE	(CYCLE_SECONDS * SLICES_PER_SEC) // 60*16 slices per polling interval.

/* Buffer sizes */
#define RCVBUF_SIZE (256 * 1024) //256K
#define SNDBUF_SIZE (256 * 1024) //256K

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

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

/* Function definitions. */
int bind_port(fd_set *fdset);
void receive_thread();
void transmit_thread();
void data_thread();

int transmit_packet(int s, struct in_addr *dst, unsigned int pending, struct tos_t *tos, unsigned int set);
int receive_packet(int s);
int reflect_packet(int s, struct packet_t *packet, size_t packet_len, struct sockaddr_in *addr);

#endif

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