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