1280849Scy/* ntpsim.h
2280849Scy *
3280849Scy * The header file for the ntp discrete event simulator.
4280849Scy *
5280849Scy * Written By:	Sachin Kamboj
6280849Scy *		University of Delaware
7280849Scy *		Newark, DE 19711
8280849Scy * Copyright (c) 2006
9132451Sroberto */
10132451Sroberto
11280849Scy#ifndef NTPSIM_H
12280849Scy#define NTPSIM_H
13132451Sroberto
14132451Sroberto#include <stdio.h>
15132451Sroberto#include <math.h>
16280849Scy#ifdef HAVE_SYS_SOCKET_H
17132451Sroberto#include <sys/socket.h>
18280849Scy#endif
19132451Sroberto#include <arpa/inet.h>
20132451Sroberto#include "ntp_syslog.h"
21132451Sroberto#include "ntp_fp.h"
22132451Sroberto#include "ntp.h"
23132451Sroberto#include "ntp_select.h"
24132451Sroberto#include "ntp_malloc.h"
25132451Sroberto#include "ntp_refclock.h"
26132451Sroberto#include "recvbuff.h"
27132451Sroberto#include "ntp_io.h"
28132451Sroberto#include "ntp_stdlib.h"
29280849Scy#include "ntp_prio_q.h"
30132451Sroberto
31280849Scy/* CONSTANTS */
32132451Sroberto
33280849Scy#ifdef PI
34280849Scy# undef PI
35280849Scy#endif
36280849Scy#define PI 3.1415926535         /* The world's most famous constant */
37280849Scy#define SIM_TIME 86400		/* end simulation time */
38280849Scy#define NET_DLY .001            /* network delay */
39280849Scy#define PROC_DLY .001		/* processing delay */
40280849Scy#define BEEP_DLY 3600           /* beep interval (s) */
41280849Scy
42280849Scy
43280849Scy/* Discrete Event Queue
44280849Scy * --------------------
45280849Scy * The NTP simulator is a discrete event simulator.
46280849Scy *
47280849Scy * Central to this simulator is an event queue which is a priority queue
48280849Scy * in which the "priority" is given by the time of arrival of the event.
49280849Scy *
50280849Scy * A discrete set of events can happen and are stored in the queue to arrive
51280849Scy * at a particular time.
52132451Sroberto */
53280849Scy
54280849Scy/* Possible Discrete Events */
55280849Scy
56132451Srobertotypedef enum {
57280849Scy    BEEP,          /* Event to record simulator stats */
58280849Scy    CLOCK,         /* Event to advance the clock to the specified time */
59280849Scy    TIMER,         /* Event that designates a timer interrupt. */
60280849Scy    PACKET         /* Event that designates arrival of a packet */
61132451Sroberto} funcTkn;
62132451Sroberto
63280849Scy
64280849Scy/* Event information */
65280849Scy
66132451Srobertotypedef struct {
67280849Scy    double time;       /* Time at which event occurred */
68280849Scy    funcTkn function;  /* Type of event that occured */
69280849Scy    union {
70280849Scy        struct pkt evnt_pkt;
71280849Scy        struct recvbuf evnt_buf;
72280849Scy    } buffer;          /* Other data associated with the event */
73132451Sroberto#define ntp_pkt buffer.evnt_pkt
74132451Sroberto#define rcv_buf buffer.evnt_buf
75132451Sroberto} Event;
76132451Sroberto
77132451Sroberto
78280849Scy/* Server Script Information */
79280849Scytypedef struct script_info_tag script_info;
80280849Scystruct script_info_tag {
81280849Scy	script_info *	link;
82280849Scy	double		duration;
83280849Scy	double		freq_offset;
84280849Scy	double		wander;
85280849Scy	double		jitter;
86280849Scy	double		prop_delay;
87280849Scy	double		proc_delay;
88280849Scy};
89132451Sroberto
90280849Scytypedef DECL_FIFO_ANCHOR(script_info) script_info_fifo;
91132451Sroberto
92132451Sroberto
93280849Scy/* Server Structures */
94132451Sroberto
95280849Scytypedef struct server_info_tag server_info;
96280849Scystruct server_info_tag {
97280849Scy	server_info *		link;
98280849Scy	double			server_time;
99280849Scy	sockaddr_u *		addr;
100280849Scy	script_info_fifo *	script;
101280849Scy	script_info *		curr_script;
102280849Scy};
103132451Sroberto
104280849Scytypedef DECL_FIFO_ANCHOR(server_info) server_info_fifo;
105132451Sroberto
106280849Scy
107280849Scy/* Simulation control information */
108280849Scy
109280849Scytypedef struct Sim_Info {
110280849Scy    double sim_time;      /* Time in the simulation */
111280849Scy    double end_time;      /* Time at which simulation needs to be ended */
112280849Scy    double beep_delay;    /* Delay between simulation "beeps" at which
113280849Scy                             simulation  stats are recorded. */
114280849Scy    int num_of_servers;   /* Number of servers in the simulation */
115280849Scy    server_info *servers; /* Pointer to array of servers */
116280849Scy} sim_info;
117280849Scy
118280849Scy
119280849Scy/* Local Clock (Client) Variables */
120280849Scy
121280849Scytypedef struct Local_Clock_Info {
122280849Scy    double local_time;		/* Client disciplined time */
123280849Scy    double adj;			/* Remaining time correction */
124280849Scy    double slew;		/* Correction Slew Rate */
125280849Scy    double last_read_time;	/* Last time the clock was read */
126280849Scy} local_clock_info;
127280849Scy
128280849Scyextern local_clock_info simclock; /* Local Clock Variables */
129280849Scyextern sim_info simulation;	  /* Simulation Control Variables */
130280849Scy
131280849Scy/* Function Prototypes */
132280849Scy
133280849Scyint	 ntpsim			(int argc, char *argv[]);
134280849ScyEvent    *event			(double t, funcTkn f);
135280849Scyvoid     sim_event_timer	(Event *e);
136280849Scyint      simulate_server	(sockaddr_u *serv_addr, endpt *inter,
137280849Scy				 struct pkt *rpkt);
138280849Scyvoid     sim_update_clocks	(Event *e);
139280849Scyvoid     sim_event_recv_packet	(Event *e);
140280849Scyvoid     sim_event_beep		(Event *e);
141280849Scyvoid     abortsim		(char *errmsg);
142280849Scydouble	 gauss			(double, double);
143280849Scydouble	 poisson		(double, double);
144280849Scyvoid     create_server_associations(void);
145280849Scy
146280849Scy#endif	/* NTPSIM_H */
147