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