xref: /csrg-svn/usr.sbin/timed/timed/globals.h (revision 61885)
159910Sbostic /*-
2*61885Sbostic  * Copyright (c) 1985, 1993
3*61885Sbostic  *	The Regents of the University of California.  All rights reserved.
433123Sbostic  *
542829Sbostic  * %sccs.include.redist.c%
633123Sbostic  *
7*61885Sbostic  *	@(#)globals.h	8.1 (Berkeley) 06/06/93
823666Sgusella  */
923666Sgusella 
1059910Sbostic #ifdef sgi
1159910Sbostic #ident "$Revision: 1.15 $"
1259910Sbostic #endif
1359910Sbostic 
1424920Sbloom #include <sys/param.h>
1523666Sgusella #include <sys/time.h>
1623666Sgusella #include <sys/socket.h>
1759910Sbostic 
1823666Sgusella #include <netinet/in.h>
1923666Sgusella #include <arpa/inet.h>
2023666Sgusella 
2159910Sbostic #include <errno.h>
2259910Sbostic #include <limits.h>
2359910Sbostic #include <netdb.h>
2459910Sbostic #include <stdio.h>
2559910Sbostic #include <stdlib.h>
2659910Sbostic #include <string.h>
2759910Sbostic #include <syslog.h>
2859910Sbostic #include <syslog.h>
2959910Sbostic #include <unistd.h>
3059910Sbostic 
3159910Sbostic #include <protocols/timed.h>
3259910Sbostic #ifdef sgi
3359910Sbostic #include <bstring.h>
3459910Sbostic #include <sys/clock.h>
3559910Sbostic /* use the constant HZ instead of the function CLK_TCK */
3659910Sbostic #undef CLK_TCK
3759910Sbostic #define CLK_TCK HZ
3859910Sbostic #else
3959910Sbostic #define	SECHR	(60*60)
4059910Sbostic #define	SECDAY	(24*SECHR)
4159910Sbostic #endif /* sgi */
4259910Sbostic 
4323666Sgusella extern int errno;
4425579Sbloom extern int sock;
4523666Sgusella 
4659910Sbostic /* Best expected round trip for a measurement.
4759910Sbostic  * This is essentially the number of milliseconds per CPU tick (CLK_TCK?).
4859910Sbostic  * All delays shorter than this are usually reported as 0.
4928830Skarels  */
5059910Sbostic #define MIN_ROUND ((1000-1)/CLK_TCK)
5128830Skarels 
5259910Sbostic 
5359910Sbostic #define SAMPLEINTVL	240		/* synch() freq for master in sec */
5459910Sbostic #define	MAXADJ		20		/* max adjtime() correction in sec */
5559910Sbostic 
5659910Sbostic #define MAX_TRIM	3000000		/* max drift in nsec/sec, 0.3% */
5759910Sbostic #define BIG_ADJ		(MAX_TRIM/1000*SAMPLEINTVL*2)	/* max good adj */
5859910Sbostic 
5959910Sbostic #define MINTOUT		360		/* election delays, 6-15 minutes */
6023666Sgusella #define MAXTOUT		900
6123666Sgusella 
6259910Sbostic #define BAD_STATUS	(-1)
6323666Sgusella #define GOOD		1
6423666Sgusella #define UNREACHABLE	2
6523666Sgusella #define NONSTDTIME	3
6659910Sbostic #define HOSTDOWN	0x7fffffff
6724920Sbloom 
6859910Sbostic #define OFF		0
6959910Sbostic #define ON		1
7024920Sbloom 
7159910Sbostic #define MAX_HOPCNT	10		/* max value for tsp_hpcnt */
7259910Sbostic 
7359910Sbostic #define LOSTHOST	3		/* forget after this many failures */
7459910Sbostic 
7559910Sbostic #define VALID_RANGE (MAXADJ*1000)	/* good times in milliseconds */
7659910Sbostic #define GOOD_RANGE (MIN_ROUND*2)
7759910Sbostic #define VGOOD_RANGE (MIN_ROUND-1)
7859910Sbostic 
7959910Sbostic 
8028830Skarels /*
8128830Skarels  * Global and per-network states.
8228830Skarels  */
8359910Sbostic #define NOMASTER	0		/* no good master */
8459910Sbostic #define SLAVE		1
8528830Skarels #define MASTER		2
8628830Skarels #define IGNORE		4
8728830Skarels #define ALL		(SLAVE|MASTER|IGNORE)
8825579Sbloom #define SUBMASTER	(SLAVE|MASTER)
8924920Sbloom 
9059910Sbostic #define NHOSTS		1013		/* max of hosts controlled by timed
9159910Sbostic 					 * This must be a prime number.
9259910Sbostic 					 */
9359910Sbostic struct hosttbl {
9459910Sbostic 	struct	hosttbl *h_bak;		/* hash chain */
9559910Sbostic 	struct	hosttbl *h_fwd;
9659910Sbostic 	struct  hosttbl *l_bak;		/* "sequential" list */
9759910Sbostic 	struct  hosttbl *l_fwd;
9859910Sbostic 	struct	netinfo *ntp;
9959910Sbostic 	struct	sockaddr_in addr;
10059910Sbostic 	char	name[MAXHOSTNAMELEN+1];
10159910Sbostic 	u_char	head;			/* 1=head of hash chain */
10259910Sbostic 	u_char	good;			/* 0=trusted host, for averaging */
10359910Sbostic 	u_char	noanswer;		/* count of failures to answer */
10459910Sbostic 	u_char	need_set;		/* need a SETTIME */
10524920Sbloom 	u_short seq;
10659910Sbostic 	long	delta;
10724920Sbloom };
10824920Sbloom 
10959910Sbostic /* closed hash table with internal chaining */
11059910Sbostic extern struct hosttbl hosttbl[NHOSTS+1];
11159910Sbostic #define self hosttbl[0]
11259910Sbostic #define hostname (self.name)
11359910Sbostic 
11459910Sbostic 
11525579Sbloom struct netinfo {
11659910Sbostic 	struct	netinfo *next;
11759910Sbostic 	struct	in_addr net;
11859910Sbostic 	u_long	mask;
11959910Sbostic 	struct	in_addr my_addr;
12059910Sbostic 	struct	sockaddr_in dest_addr;	/* broadcast addr or point-point */
12159910Sbostic 	long	status;
12259910Sbostic 	struct timeval slvwait;		/* delay before sending our time */
12359910Sbostic 	int	quit_count;		/* recent QUITs */
12425579Sbloom };
12525579Sbloom 
12659910Sbostic #include "extern.h"
12759910Sbostic 
12859910Sbostic #define tvtomsround(tv) ((tv).tv_sec*1000 + ((tv).tv_usec + 500)/1000)
12959910Sbostic 
13025579Sbloom extern struct netinfo *nettab;
13125579Sbloom extern int status;
13225579Sbloom extern int trace;
13325579Sbloom extern int sock;
13425579Sbloom extern struct sockaddr_in from;
13559910Sbostic extern struct timeval from_when;	/* when the last msg arrived */
13659910Sbostic extern u_short sequence;		/* TSP message sequence number */
13727042Sbloom extern struct netinfo *fromnet, *slavenet;
13825579Sbloom extern FILE *fd;
13925579Sbloom extern long delay1, delay2;
14059910Sbostic extern int nslavenets;			/* nets were I could be a slave */
14159910Sbostic extern int nmasternets;			/* nets were I could be a master */
14259910Sbostic extern int nignorednets;		/* ignored nets */
14359910Sbostic extern int nnets;			/* nets I am connected to */
14425579Sbloom 
14559910Sbostic 
14659910Sbostic #define trace_msg(msg)		{if (trace) fprintf(fd, msg);}
14759910Sbostic 
14859910Sbostic #define trace_sendto_err(addr) {					\
14959910Sbostic 	int st_errno = errno;						\
15059910Sbostic 	syslog(LOG_ERR, "%s %d: sendto %s: %m",				\
15159910Sbostic 		__FILE__, __LINE__, inet_ntoa(addr));			\
15259910Sbostic 	if (trace)							\
15359910Sbostic 		fprintf(fd, "%s %d: sendto %s: %d", __FILE__, __LINE__,	\
15459910Sbostic 			inet_ntoa(addr), st_errno);			\
15559910Sbostic }
15659910Sbostic 
15759910Sbostic 
15859910Sbostic # define max(a,b) 	(a<b ? b : a)
15959910Sbostic # define min(a,b) 	(a>b ? b : a)
16059910Sbostic # define abs(x)		(x>=0 ? x : -(x))
161