]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/include/ntp_types.h
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / ntp / include / ntp_types.h
1 /*
2  *  ntp_types.h - defines how int32 and u_int32 are treated.
3  *
4  * New style: Make sure C99 fixed width integer types are available:
5  * intN_t and uintN_t
6
7  * Old style: defines how int32 and u_int32 are treated.
8  *  For 64 bit systems like the DEC Alpha, they have to be defined
9  *  as int and u_int.
10  *  For 32 bit systems, define them as long and u_long
11  */
12 #ifndef NTP_TYPES_H
13 #define NTP_TYPES_H
14
15 #include <sys/types.h>
16 #if defined(HAVE_INTTYPES_H)
17 # include <inttypes.h>
18 #elif defined(HAVE_STDINT_H)
19 # include <stdint.h>
20 #endif
21
22 /* Bug 2813 */
23 #ifdef HAVE_LIMITS_H
24 # include <limits.h>
25 #endif
26
27 #include "ntp_machine.h"
28
29
30 #ifndef TRUE
31 # define        TRUE    1
32 #endif
33 #ifndef FALSE
34 # define        FALSE   0
35 #endif
36
37 /*
38  * This is another naming conflict.
39  * On NetBSD for MAC the macro "mac" is defined as 1
40  * this is fun for us as a packet structure contains an
41  * optional "mac" member - severe confusion results 8-)
42  * As we hopefully do not have to rely on that macro we
43  * just undefine that.
44  */
45 #ifdef mac
46 #undef mac
47 #endif
48
49 /*
50  * used to quiet compiler warnings
51  */
52 #ifndef UNUSED_ARG
53 #define UNUSED_ARG(arg)         ((void)(arg))
54 #endif
55 #ifndef UNUSED_LOCAL
56 #define UNUSED_LOCAL(arg)       ((void)(arg))
57 #endif
58
59 /*
60  * COUNTOF(array) - size of array in elements
61  */
62 #define COUNTOF(arr)    (sizeof(arr) / sizeof((arr)[0]))
63
64 /*
65  * VMS DECC (v4.1), {u_char,u_short,u_long} are only in SOCKET.H,
66  *                      and u_int isn't defined anywhere
67  */
68 #if defined(VMS)
69 #include <socket.h>
70 typedef unsigned int u_int;
71 #endif /* VMS */
72
73 #ifdef HAVE_UINT32_T
74 # ifndef HAVE_INT32
75    typedef      int32_t         int32;
76 # endif
77 # ifndef HAVE_U_INT32
78    typedef      uint32_t        u_int32;
79 #  if defined(UINT32_MAX) && !defined(U_INT32_MAX)
80 #   define U_INT32_MAX UINT32_MAX
81 #  endif
82 # endif
83 #elif (SIZEOF_INT == 4)
84 # if !defined(HAVE_INT32) && !defined(int32)
85    typedef      int             int32;
86 #  ifndef INT32_MIN
87 #   define INT32_MIN INT_MIN
88 #  endif
89 #  ifndef INT32_MAX
90 #   define INT32_MAX INT_MAX
91 #  endif
92 # endif
93 # if !defined(HAVE_U_INT32) && !defined(u_int32)
94    typedef      unsigned        u_int32;
95 #  if defined(UINT_MAX) && !defined(U_INT32_MAX)
96 #   define U_INT32_MAX UINT_MAX
97 #  endif
98 # endif
99 #else   /* SIZEOF_INT != 4 */
100 # if (SIZEOF_LONG == 4)
101 # if !defined(HAVE_INT32) && !defined(int32)
102     typedef     long            int32;
103 #   ifndef INT32_MIN
104 #    define INT32_MIN LONG_MIN
105 #   endif
106 #   ifndef INT32_MAX
107 #    define INT32_MAX LONG_MAX
108 #   endif
109 #  endif
110 # if !defined(HAVE_U_INT32) && !defined(u_int32)
111     typedef     unsigned long   u_int32;
112 #   if defined(ULONG_MAX) && !defined(U_INT32_MAX)
113 #    define U_INT32_MAX ULONG_MAX
114 #   endif
115 #  endif
116 # else  /* SIZEOF_LONG != 4 */
117 #  include "Bletch: what's 32 bits on this machine?"
118 # endif
119 #endif  /* !HAVE_UINT32_T && SIZEOF_INT != 4 */
120
121 #ifndef U_INT32_MAX
122 # define U_INT32_MAX    0xffffffff
123 #endif
124
125
126 /*
127  * Ugly dance to find out if we have 64bit integer type.
128  */
129 #if !defined(HAVE_INT64)
130
131 /* assume best for now, fix if frustrated later. */
132 # define HAVE_INT64
133 # define HAVE_U_INT64
134
135 /* now check the cascade. Feel free to add things. */
136 # ifdef INT64_MAX
137
138 typedef int64_t int64;
139 typedef uint64_t u_int64;
140
141 # elif SIZEOF_LONG == 8
142
143 typedef long int64;
144 typedef unsigned long u_int64;
145
146 # elif SIZEOF_LONG_LONG == 8
147
148 typedef long long int64;
149 typedef unsigned long long u_int64;
150
151 # else
152
153 /* no 64bit scalar, give it up. */
154 #  undef HAVE_INT64
155 #  undef HAVE_U_INT64
156
157 # endif
158
159 #endif
160
161 /*
162  * and here the trouble starts: We need a representation with more than
163  * 64 bits. If a scalar of that size is not available, we need a struct
164  * that holds the value in split representation.
165  *
166  * To ease the usage a bit, we alwys use a union that is in processor
167  * byte order and might or might not contain a 64bit scalar.
168  */
169
170 #if SIZEOF_SHORT != 2
171 # error short is not 2 bytes -- what is 16 bit integer on this target?
172 #endif
173
174 typedef union {
175 #   ifdef WORDS_BIGENDIAN
176         struct {
177                 int16_t hh; uint16_t hl; uint16_t lh; uint16_t ll;
178         } w_s;
179         struct {
180                 uint16_t hh; uint16_t hl; uint16_t lh; uint16_t ll;
181         } W_s;
182         struct {
183                   int32 hi; u_int32 lo;
184         } d_s;
185         struct {
186                 u_int32 hi; u_int32 lo;
187         } D_s;
188 #   else
189         struct {
190                 uint16_t ll; uint16_t lh; uint16_t hl;   int16_t hh;
191         } w_s;
192         struct {
193                 uint16_t ll; uint16_t lh; uint16_t hl; uint16_t hh;
194         } W_s;
195         struct {
196                 u_int32 lo;   int32 hi;
197         } d_s;
198         struct {
199                 u_int32 lo; u_int32 hi;
200         } D_s;
201 #   endif
202
203 #   ifdef HAVE_INT64
204         int64   q_s;    /*   signed quad scalar */
205         u_int64 Q_s;    /* unsigned quad scalar */
206 #   endif
207 } vint64; /* variant int 64 */
208
209
210 typedef uint8_t         ntp_u_int8_t;
211 typedef uint16_t        ntp_u_int16_t;
212 typedef uint32_t        ntp_u_int32_t;
213
214 typedef struct ntp_uint64_t { u_int32 val[2]; } ntp_uint64_t;
215
216 typedef uint16_t        associd_t; /* association ID */
217 #define ASSOCID_MAX     USHRT_MAX
218 typedef u_int32 keyid_t;        /* cryptographic key ID */
219 #define KEYID_T_MAX     (0xffffffff)
220 typedef u_int32 tstamp_t;       /* NTP seconds timestamp */
221
222 /*
223  * Cloning malloc()'s behavior of always returning pointers suitably
224  * aligned for the strictest alignment requirement of any type is not
225  * easy to do portably, as the maximum alignment required is not
226  * exposed.  Use the size of a union of the types known to represent the
227  * strictest alignment on some platform.
228  */
229 typedef union max_alignment_tag {
230         double          d;
231 } max_alignment;
232
233 #define MAXALIGN                sizeof(max_alignment)
234 #define ALIGN_UNITS(sz)         (((sz) + MAXALIGN - 1) / MAXALIGN)
235 #define ALIGNED_SIZE(sz)        (MAXALIGN * ALIGN_UNITS(sz))
236 #define INC_ALIGNED_PTR(b, m)   ((void *)aligned_ptr((void *)(b), m))
237
238 static inline
239 max_alignment *
240 aligned_ptr(
241         max_alignment * base,
242         size_t          minsize
243         )
244 {
245         return base + ALIGN_UNITS((minsize < 1) ? 1 : minsize);
246 }
247
248 /*
249  * Macro to use in otherwise-empty source files to comply with ANSI C
250  * requirement that each translation unit (source file) contain some
251  * declaration.  This has commonly been done by declaring an unused
252  * global variable of type int or char.  An extern reference to exit()
253  * serves the same purpose without bloat.
254  */
255 #define NONEMPTY_TRANSLATION_UNIT       extern void exit(int);
256
257 /*
258  * On Unix struct sock_timeval is equivalent to struct timeval.
259  * On Windows built with 64-bit time_t, sock_timeval.tv_sec is a long
260  * as required by Windows' socket() interface timeout argument, while
261  * timeval.tv_sec is time_t for the more common use as a UTC time 
262  * within NTP.
263  */
264 #ifndef SYS_WINNT
265 #define sock_timeval    timeval
266 #endif
267
268 /*
269  * On Unix open() works for tty (serial) devices just fine, while on
270  * Windows refclock serial devices are opened using CreateFile, a lower
271  * level than the CRT-provided descriptors, because the C runtime lacks
272  * tty APIs.  For refclocks which wish to use open() as well as or 
273  * instead of refclock_open(), tty_open() is equivalent to open() on
274  * Unix and  implemented in the Windows port similarly to
275  * refclock_open().
276  * Similarly, the termios emulation in the Windows code needs to know
277  * about serial ports being closed, while the Posix systems do not.
278  */
279 #ifndef SYS_WINNT
280 # define tty_open(f, a, m)      open(f, a, m)
281 # define closeserial(fd)        close(fd)
282 # define closesocket(fd)        close(fd)
283 typedef int SOCKET;
284 # define INVALID_SOCKET         (-1)
285 # define SOCKET_ERROR           (-1)
286 # define socket_errno()         (errno)
287 #else   /* SYS_WINNT follows */
288 # define socket_errno()         (errno = WSAGetLastError())
289 #endif
290
291
292
293 #endif  /* NTP_TYPES_H */