xref: /csrg-svn/usr.sbin/timed/timed/globals.h (revision 59910)
1*59910Sbostic /*-
2*59910Sbostic  * Copyright (c) 1985 The Regents of the University of California.
333123Sbostic  * All rights reserved.
433123Sbostic  *
542829Sbostic  * %sccs.include.redist.c%
633123Sbostic  *
7*59910Sbostic  *	@(#)globals.h	5.1 (Berkeley) 05/11/93
823666Sgusella  */
923666Sgusella 
10*59910Sbostic #ifdef sgi
11*59910Sbostic #ident "$Revision: 1.15 $"
12*59910Sbostic #endif
13*59910Sbostic 
1424920Sbloom #include <sys/param.h>
1523666Sgusella #include <sys/time.h>
1623666Sgusella #include <sys/socket.h>
17*59910Sbostic 
1823666Sgusella #include <netinet/in.h>
1923666Sgusella #include <arpa/inet.h>
2023666Sgusella 
21*59910Sbostic #include <errno.h>
22*59910Sbostic #include <limits.h>
23*59910Sbostic #include <netdb.h>
24*59910Sbostic #include <stdio.h>
25*59910Sbostic #include <stdlib.h>
26*59910Sbostic #include <string.h>
27*59910Sbostic #include <syslog.h>
28*59910Sbostic #include <syslog.h>
29*59910Sbostic #include <unistd.h>
30*59910Sbostic 
31*59910Sbostic #include <protocols/timed.h>
32*59910Sbostic #ifdef sgi
33*59910Sbostic #include <bstring.h>
34*59910Sbostic #include <sys/clock.h>
35*59910Sbostic /* use the constant HZ instead of the function CLK_TCK */
36*59910Sbostic #undef CLK_TCK
37*59910Sbostic #define CLK_TCK HZ
38*59910Sbostic #else
39*59910Sbostic #define	SECHR	(60*60)
40*59910Sbostic #define	SECDAY	(24*SECHR)
41*59910Sbostic #endif /* sgi */
42*59910Sbostic 
4323666Sgusella extern int errno;
4425579Sbloom extern int sock;
4523666Sgusella 
46*59910Sbostic /* Best expected round trip for a measurement.
47*59910Sbostic  * This is essentially the number of milliseconds per CPU tick (CLK_TCK?).
48*59910Sbostic  * All delays shorter than this are usually reported as 0.
4928830Skarels  */
50*59910Sbostic #define MIN_ROUND ((1000-1)/CLK_TCK)
5128830Skarels 
52*59910Sbostic 
53*59910Sbostic #define SAMPLEINTVL	240		/* synch() freq for master in sec */
54*59910Sbostic #define	MAXADJ		20		/* max adjtime() correction in sec */
55*59910Sbostic 
56*59910Sbostic #define MAX_TRIM	3000000		/* max drift in nsec/sec, 0.3% */
57*59910Sbostic #define BIG_ADJ		(MAX_TRIM/1000*SAMPLEINTVL*2)	/* max good adj */
58*59910Sbostic 
59*59910Sbostic #define MINTOUT		360		/* election delays, 6-15 minutes */
6023666Sgusella #define MAXTOUT		900
6123666Sgusella 
62*59910Sbostic #define BAD_STATUS	(-1)
6323666Sgusella #define GOOD		1
6423666Sgusella #define UNREACHABLE	2
6523666Sgusella #define NONSTDTIME	3
66*59910Sbostic #define HOSTDOWN	0x7fffffff
6724920Sbloom 
68*59910Sbostic #define OFF		0
69*59910Sbostic #define ON		1
7024920Sbloom 
71*59910Sbostic #define MAX_HOPCNT	10		/* max value for tsp_hpcnt */
72*59910Sbostic 
73*59910Sbostic #define LOSTHOST	3		/* forget after this many failures */
74*59910Sbostic 
75*59910Sbostic #define VALID_RANGE (MAXADJ*1000)	/* good times in milliseconds */
76*59910Sbostic #define GOOD_RANGE (MIN_ROUND*2)
77*59910Sbostic #define VGOOD_RANGE (MIN_ROUND-1)
78*59910Sbostic 
79*59910Sbostic 
8028830Skarels /*
8128830Skarels  * Global and per-network states.
8228830Skarels  */
83*59910Sbostic #define NOMASTER	0		/* no good master */
84*59910Sbostic #define SLAVE		1
8528830Skarels #define MASTER		2
8628830Skarels #define IGNORE		4
8728830Skarels #define ALL		(SLAVE|MASTER|IGNORE)
8825579Sbloom #define SUBMASTER	(SLAVE|MASTER)
8924920Sbloom 
90*59910Sbostic #define NHOSTS		1013		/* max of hosts controlled by timed
91*59910Sbostic 					 * This must be a prime number.
92*59910Sbostic 					 */
93*59910Sbostic struct hosttbl {
94*59910Sbostic 	struct	hosttbl *h_bak;		/* hash chain */
95*59910Sbostic 	struct	hosttbl *h_fwd;
96*59910Sbostic 	struct  hosttbl *l_bak;		/* "sequential" list */
97*59910Sbostic 	struct  hosttbl *l_fwd;
98*59910Sbostic 	struct	netinfo *ntp;
99*59910Sbostic 	struct	sockaddr_in addr;
100*59910Sbostic 	char	name[MAXHOSTNAMELEN+1];
101*59910Sbostic 	u_char	head;			/* 1=head of hash chain */
102*59910Sbostic 	u_char	good;			/* 0=trusted host, for averaging */
103*59910Sbostic 	u_char	noanswer;		/* count of failures to answer */
104*59910Sbostic 	u_char	need_set;		/* need a SETTIME */
10524920Sbloom 	u_short seq;
106*59910Sbostic 	long	delta;
10724920Sbloom };
10824920Sbloom 
109*59910Sbostic /* closed hash table with internal chaining */
110*59910Sbostic extern struct hosttbl hosttbl[NHOSTS+1];
111*59910Sbostic #define self hosttbl[0]
112*59910Sbostic #define hostname (self.name)
113*59910Sbostic 
114*59910Sbostic 
11525579Sbloom struct netinfo {
116*59910Sbostic 	struct	netinfo *next;
117*59910Sbostic 	struct	in_addr net;
118*59910Sbostic 	u_long	mask;
119*59910Sbostic 	struct	in_addr my_addr;
120*59910Sbostic 	struct	sockaddr_in dest_addr;	/* broadcast addr or point-point */
121*59910Sbostic 	long	status;
122*59910Sbostic 	struct timeval slvwait;		/* delay before sending our time */
123*59910Sbostic 	int	quit_count;		/* recent QUITs */
12425579Sbloom };
12525579Sbloom 
126*59910Sbostic #include "extern.h"
127*59910Sbostic 
128*59910Sbostic #define tvtomsround(tv) ((tv).tv_sec*1000 + ((tv).tv_usec + 500)/1000)
129*59910Sbostic 
13025579Sbloom extern struct netinfo *nettab;
13125579Sbloom extern int status;
13225579Sbloom extern int trace;
13325579Sbloom extern int sock;
13425579Sbloom extern struct sockaddr_in from;
135*59910Sbostic extern struct timeval from_when;	/* when the last msg arrived */
136*59910Sbostic extern u_short sequence;		/* TSP message sequence number */
13727042Sbloom extern struct netinfo *fromnet, *slavenet;
13825579Sbloom extern FILE *fd;
13925579Sbloom extern long delay1, delay2;
140*59910Sbostic extern int nslavenets;			/* nets were I could be a slave */
141*59910Sbostic extern int nmasternets;			/* nets were I could be a master */
142*59910Sbostic extern int nignorednets;		/* ignored nets */
143*59910Sbostic extern int nnets;			/* nets I am connected to */
14425579Sbloom 
145*59910Sbostic 
146*59910Sbostic #define trace_msg(msg)		{if (trace) fprintf(fd, msg);}
147*59910Sbostic 
148*59910Sbostic #define trace_sendto_err(addr) {					\
149*59910Sbostic 	int st_errno = errno;						\
150*59910Sbostic 	syslog(LOG_ERR, "%s %d: sendto %s: %m",				\
151*59910Sbostic 		__FILE__, __LINE__, inet_ntoa(addr));			\
152*59910Sbostic 	if (trace)							\
153*59910Sbostic 		fprintf(fd, "%s %d: sendto %s: %d", __FILE__, __LINE__,	\
154*59910Sbostic 			inet_ntoa(addr), st_errno);			\
155*59910Sbostic }
156*59910Sbostic 
157*59910Sbostic 
158*59910Sbostic # define max(a,b) 	(a<b ? b : a)
159*59910Sbostic # define min(a,b) 	(a>b ? b : a)
160*59910Sbostic # define abs(x)		(x>=0 ? x : -(x))
161