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