xref: /netbsd-src/usr.sbin/timed/timed/globals.h (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: globals.h,v 1.8 2002/09/19 00:01:33 mycroft Exp $	*/
2 
3 /*-
4  * Copyright (c) 1985 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)globals.h	8.1 (Berkeley) 6/6/93
36  */
37 
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/socket.h>
41 #include <sys/poll.h>
42 
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 
46 #include <errno.h>
47 #include <limits.h>
48 #include <netdb.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <syslog.h>
53 #include <syslog.h>
54 #include <unistd.h>
55 
56 #include <protocols/timed.h>
57 #define	SECHR	(60*60)
58 #define	SECDAY	(24*SECHR)
59 
60 extern int sock;
61 
62 /* Best expected round trip for a measurement.
63  * This is essentially the number of milliseconds per CPU tick (CLK_TCK?).
64  * All delays shorter than this are usually reported as 0.
65  */
66 #define MIN_ROUND ((1000-1)/CLK_TCK)
67 
68 
69 #define SAMPLEINTVL	240		/* synch() freq for master in sec */
70 #define	MAXADJ		20		/* max adjtime() correction in sec */
71 
72 #define MAX_TRIM	3000000		/* max drift in nsec/sec, 0.3% */
73 #define BIG_ADJ		(MAX_TRIM/1000*SAMPLEINTVL*2)	/* max good adj */
74 
75 #define MINTOUT		360		/* election delays, 6-15 minutes */
76 #define MAXTOUT		900
77 
78 #define BAD_STATUS	(-1)
79 #define GOOD		1
80 #define UNREACHABLE	2
81 #define NONSTDTIME	3
82 #define HOSTDOWN	0x7fffffff
83 
84 #define OFF		0
85 #define ON		1
86 
87 #define MAX_HOPCNT	10		/* max value for tsp_hpcnt */
88 
89 #define LOSTHOST	3		/* forget after this many failures */
90 
91 #define VALID_RANGE (MAXADJ*1000)	/* good times in milliseconds */
92 #define GOOD_RANGE (MIN_ROUND*2)
93 #define VGOOD_RANGE (MIN_ROUND-1)
94 
95 
96 /*
97  * Global and per-network states.
98  */
99 #define NOMASTER	0		/* no good master */
100 #define SLAVE		1
101 #define MASTER		2
102 #define IGNORE		4
103 #define ALL		(SLAVE|MASTER|IGNORE)
104 #define SUBMASTER	(SLAVE|MASTER)
105 
106 #define NHOSTS		1013		/* max of hosts controlled by timed
107 					 * This must be a prime number.
108 					 */
109 struct hosttbl {
110 	struct	hosttbl *h_bak;		/* hash chain */
111 	struct	hosttbl *h_fwd;
112 	struct  hosttbl *l_bak;		/* "sequential" list */
113 	struct  hosttbl *l_fwd;
114 	struct	netinfo *ntp;
115 	struct	sockaddr_in addr;
116 	char	name[MAXHOSTNAMELEN+1];
117 	u_char	head;			/* 1=head of hash chain */
118 	u_char	good;			/* 0=trusted host, for averaging */
119 	u_char	noanswer;		/* count of failures to answer */
120 	u_char	need_set;		/* need a SETTIME */
121 	u_short seq;
122 	long	delta;
123 };
124 
125 /* closed hash table with internal chaining */
126 extern struct hosttbl hosttbl[NHOSTS+1];
127 #define self hosttbl[0]
128 #define hostname (self.name)
129 
130 
131 struct netinfo {
132 	struct	netinfo *next;
133 	struct	in_addr net;
134 	u_long	mask;
135 	struct	in_addr my_addr;
136 	struct	sockaddr_in dest_addr;	/* broadcast addr or point-point */
137 	long	status;
138 	struct timeval slvwait;		/* delay before sending our time */
139 	int	quit_count;		/* recent QUITs */
140 };
141 
142 #include "timed-extern.h"
143 
144 #define tvtomsround(tv) ((tv).tv_sec*1000 + ((tv).tv_usec + 500)/1000)
145 
146 extern struct netinfo *nettab;
147 extern int status;
148 extern int trace;
149 extern int sock;
150 extern struct sockaddr_in from;
151 extern struct timeval from_when;	/* when the last msg arrived */
152 extern u_short sequence;		/* TSP message sequence number */
153 extern struct netinfo *fromnet, *slavenet;
154 extern FILE *fd;
155 extern long delay1, delay2;
156 extern int nslavenets;			/* nets were I could be a slave */
157 extern int nmasternets;			/* nets were I could be a master */
158 extern int nignorednets;		/* ignored nets */
159 extern int nnets;			/* nets I am connected to */
160 
161 
162 #define trace_msg(msg)		{if (trace) fprintf(fd, msg);}
163 
164 #define trace_sendto_err(addr) {					\
165 	int st_errno = errno;						\
166 	syslog(LOG_ERR, "sendto %s: %m", inet_ntoa(addr));		\
167 	if (trace)							\
168 		fprintf(fd, "%s %d: sendto %s: %d", __FILE__, __LINE__,	\
169 			inet_ntoa(addr), st_errno);			\
170 }
171 
172 
173 # define max(a,b) 	(a<b ? b : a)
174 # define min(a,b) 	(a>b ? b : a)
175 # define abs(x)		(x>=0 ? x : -(x))
176