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