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