ntp_types.h revision 298699
149973Scsgr/*
249973Scsgr *  ntp_types.h - defines how int32 and u_int32 are treated.
349973Scsgr *
449973Scsgr * New style: Make sure C99 fixed width integer types are available:
549973Scsgr * intN_t and uintN_t
649973Scsgr
749973Scsgr * Old style: defines how int32 and u_int32 are treated.
849973Scsgr *  For 64 bit systems like the DEC Alpha, they have to be defined
949973Scsgr *  as int and u_int.
1049973Scsgr *  For 32 bit systems, define them as long and u_long
1149973Scsgr */
1249973Scsgr#ifndef NTP_TYPES_H
1349973Scsgr#define NTP_TYPES_H
1450476Speter
15293727Sallanjude#include <sys/types.h>
1649973Scsgr#if defined(HAVE_INTTYPES_H)
1779538Sru# include <inttypes.h>
1849973Scsgr#endif
1979727Sschweikh#if defined(HAVE_STDINT_H)
2049973Scsgr# include <stdint.h>
2149973Scsgr#endif
22293727Sallanjude
2381625Sru/* Bug 2813 */
2449973Scsgr#ifdef HAVE_LIMITS_H
25293727Sallanjude# include <limits.h>
26293727Sallanjude#endif
27293727Sallanjude
2849973Scsgr#include "ntp_machine.h"
2949973Scsgr
3049973Scsgr
3149973Scsgr#ifndef TRUE
3249973Scsgr# define	TRUE	1
33293727Sallanjude#endif
3449973Scsgr#ifndef FALSE
35293727Sallanjude# define	FALSE	0
36293727Sallanjude#endif
37293727Sallanjude
38293727Sallanjude#ifdef HAVE_STDBOOL_H
39293727Sallanjude# include <stdbool.h>
40293727Sallanjude#else
41293727Sallanjudetypedef int bool;	/* Can't use enum TRUE/FALSE because of above */
42293727Sallanjude#endif
43293727Sallanjude
4449973Scsgr
4549973Scsgr/*
46117011Sru * This is another naming conflict.
47117011Sru * On NetBSD for MAC the macro "mac" is defined as 1
48117011Sru * this is fun for us as a packet structure contains an
49117011Sru * optional "mac" member - severe confusion results 8-)
50117011Sru * As we hopefully do not have to rely on that macro we
5184979Sdd * just undefine that.
5250074Scsgr */
53117011Sru#ifdef mac
54117011Sru#undef mac
55117011Sru#endif
56117011Sru
57293727Sallanjude/*
58293727Sallanjude * used to quiet compiler warnings
5949973Scsgr */
60117011Sru#ifndef UNUSED_ARG
61117011Sru#define UNUSED_ARG(arg)		((void)(arg))
6249973Scsgr#endif
6384979Sdd#ifndef UNUSED_LOCAL
6449973Scsgr#define UNUSED_LOCAL(arg)	((void)(arg))
65293727Sallanjude#endif
66165721Sdanger
67165721Sdanger/*
68165721Sdanger * COUNTOF(array) - size of array in elements
69165721Sdanger */
70210676Sjoel#define COUNTOF(arr)	(sizeof(arr) / sizeof((arr)[0]))
7149973Scsgr
7284979Sdd/*
7384979Sdd * VMS DECC (v4.1), {u_char,u_short,u_long} are only in SOCKET.H,
7450531Smpp *			and u_int isn't defined anywhere
7550531Smpp */
76293727Sallanjude#if defined(VMS)
7750531Smpp#include <socket.h>
7850531Smpptypedef unsigned int u_int;
79165721Sdanger#endif /* VMS */
8050531Smpp
81165721Sdanger#ifdef HAVE_UINT32_T
8249973Scsgr# ifndef HAVE_INT32
8349973Scsgr   typedef	int32_t		int32;
8449973Scsgr# endif
8579727Sschweikh# ifndef HAVE_U_INT32
8649973Scsgr   typedef	uint32_t	u_int32;
8749973Scsgr#  if defined(UINT32_MAX) && !defined(U_INT32_MAX)
8850531Smpp#   define U_INT32_MAX UINT32_MAX
89293727Sallanjude#  endif
90293727Sallanjude# endif
91293727Sallanjude#elif (SIZEOF_INT == 4)
92293727Sallanjude# if !defined(HAVE_INT32) && !defined(int32)
93293727Sallanjude   typedef	int		int32;
94140561Sru#  ifndef INT32_MIN
95140561Sru#   define INT32_MIN INT_MIN
96#  endif
97#  ifndef INT32_MAX
98#   define INT32_MAX INT_MAX
99#  endif
100# endif
101# if !defined(HAVE_U_INT32) && !defined(u_int32)
102   typedef	unsigned	u_int32;
103#  if defined(UINT_MAX) && !defined(U_INT32_MAX)
104#   define U_INT32_MAX UINT_MAX
105#  endif
106# endif
107#else	/* SIZEOF_INT != 4 */
108# if (SIZEOF_LONG == 4)
109# if !defined(HAVE_INT32) && !defined(int32)
110    typedef	long		int32;
111#   ifndef INT32_MIN
112#    define INT32_MIN LONG_MIN
113#   endif
114#   ifndef INT32_MAX
115#    define INT32_MAX LONG_MAX
116#   endif
117#  endif
118# if !defined(HAVE_U_INT32) && !defined(u_int32)
119    typedef	unsigned long	u_int32;
120#   if defined(ULONG_MAX) && !defined(U_INT32_MAX)
121#    define U_INT32_MAX ULONG_MAX
122#   endif
123#  endif
124# else	/* SIZEOF_LONG != 4 */
125#  include "Bletch: what's 32 bits on this machine?"
126# endif
127#endif	/* !HAVE_UINT32_T && SIZEOF_INT != 4 */
128
129#ifndef U_INT32_MAX
130# define U_INT32_MAX	0xffffffff
131#endif
132
133
134/*
135 * Ugly dance to find out if we have 64bit integer type.
136 */
137#if !defined(HAVE_INT64)
138
139/* assume best for now, fix if frustrated later. */
140# define HAVE_INT64
141# define HAVE_U_INT64
142
143/* now check the cascade. Feel free to add things. */
144# ifdef INT64_MAX
145
146typedef int64_t int64;
147typedef uint64_t u_int64;
148
149# elif SIZEOF_LONG == 8
150
151typedef long int64;
152typedef unsigned long u_int64;
153
154# elif SIZEOF_LONG_LONG == 8
155
156typedef long long int64;
157typedef unsigned long long u_int64;
158
159# else
160
161/* no 64bit scalar, give it up. */
162#  undef HAVE_INT64
163#  undef HAVE_U_INT64
164
165# endif
166
167#endif
168
169/*
170 * and here the trouble starts: We need a representation with more than
171 * 64 bits. If a scalar of that size is not available, we need a struct
172 * that holds the value in split representation.
173 *
174 * To ease the usage a bit, we alwys use a union that is in processor
175 * byte order and might or might not contain a 64bit scalar.
176 */
177
178#if SIZEOF_SHORT != 2
179# error short is not 2 bytes -- what is 16 bit integer on this target?
180#endif
181
182typedef union {
183#   ifdef WORDS_BIGENDIAN
184	struct {
185		int16_t	hh; uint16_t hl; uint16_t lh; uint16_t ll;
186	} w_s;
187	struct {
188		uint16_t hh; uint16_t hl; uint16_t lh; uint16_t ll;
189	} W_s;
190	struct {
191		  int32 hi; u_int32 lo;
192	} d_s;
193	struct {
194		u_int32	hi; u_int32 lo;
195	} D_s;
196#   else
197	struct {
198		uint16_t ll; uint16_t lh; uint16_t hl;   int16_t hh;
199	} w_s;
200	struct {
201		uint16_t ll; uint16_t lh; uint16_t hl; uint16_t hh;
202	} W_s;
203	struct {
204		u_int32 lo;   int32 hi;
205	} d_s;
206	struct {
207		u_int32 lo; u_int32 hi;
208	} D_s;
209#   endif
210
211#   ifdef HAVE_INT64
212	int64	q_s;	/*   signed quad scalar */
213	u_int64 Q_s;	/* unsigned quad scalar */
214#   endif
215} vint64; /* variant int 64 */
216
217
218typedef uint8_t		ntp_u_int8_t;
219typedef uint16_t	ntp_u_int16_t;
220typedef uint32_t	ntp_u_int32_t;
221
222typedef struct ntp_uint64_t { u_int32 val[2]; } ntp_uint64_t;
223
224typedef uint16_t	associd_t; /* association ID */
225#define ASSOCID_MAX	USHRT_MAX
226typedef u_int32 keyid_t;	/* cryptographic key ID */
227#define KEYID_T_MAX	(0xffffffff)
228
229typedef u_int32 tstamp_t;	/* NTP seconds timestamp */
230
231/*
232 * Cloning malloc()'s behavior of always returning pointers suitably
233 * aligned for the strictest alignment requirement of any type is not
234 * easy to do portably, as the maximum alignment required is not
235 * exposed.  Use the size of a union of the types known to represent the
236 * strictest alignment on some platform.
237 */
238typedef union max_alignment_tag {
239	double		d;
240} max_alignment;
241
242#define MAXALIGN		sizeof(max_alignment)
243#define ALIGN_UNITS(sz)		(((sz) + MAXALIGN - 1) / MAXALIGN)
244#define ALIGNED_SIZE(sz)	(MAXALIGN * ALIGN_UNITS(sz))
245#define INC_ALIGNED_PTR(b, m)	((void *)aligned_ptr((void *)(b), m))
246
247static inline
248max_alignment *
249aligned_ptr(
250	max_alignment *	base,
251	size_t		minsize
252	)
253{
254	return base + ALIGN_UNITS((minsize < 1) ? 1 : minsize);
255}
256
257/*
258 * Macro to use in otherwise-empty source files to comply with ANSI C
259 * requirement that each translation unit (source file) contain some
260 * declaration.  This has commonly been done by declaring an unused
261 * global variable of type int or char.  An extern reference to exit()
262 * serves the same purpose without bloat.
263 */
264#define	NONEMPTY_TRANSLATION_UNIT	extern void exit(int);
265
266/*
267 * On Unix struct sock_timeval is equivalent to struct timeval.
268 * On Windows built with 64-bit time_t, sock_timeval.tv_sec is a long
269 * as required by Windows' socket() interface timeout argument, while
270 * timeval.tv_sec is time_t for the more common use as a UTC time
271 * within NTP.
272 */
273#ifndef SYS_WINNT
274#define	sock_timeval	timeval
275#endif
276
277/*
278 * On Unix open() works for tty (serial) devices just fine, while on
279 * Windows refclock serial devices are opened using CreateFile, a lower
280 * level than the CRT-provided descriptors, because the C runtime lacks
281 * tty APIs.  For refclocks which wish to use open() as well as or
282 * instead of refclock_open(), tty_open() is equivalent to open() on
283 * Unix and  implemented in the Windows port similarly to
284 * refclock_open().
285 * Similarly, the termios emulation in the Windows code needs to know
286 * about serial ports being closed, while the Posix systems do not.
287 */
288#ifndef SYS_WINNT
289# define tty_open(f, a, m)	open(f, a, m)
290# define closeserial(fd)	close(fd)
291# define closesocket(fd)	close(fd)
292typedef int SOCKET;
293# define INVALID_SOCKET		(-1)
294# define SOCKET_ERROR		(-1)
295# define socket_errno()		(errno)
296#else	/* SYS_WINNT follows */
297# define socket_errno()		(errno = WSAGetLastError())
298#endif
299
300
301
302#endif	/* NTP_TYPES_H */
303