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