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