ntp_types.h revision 294554
1164022Sdds/* 2164022Sdds * ntp_types.h - defines how int32 and u_int32 are treated. 3164022Sdds * 4164022Sdds * New style: Make sure C99 fixed width integer types are available: 5164022Sdds * intN_t and uintN_t 6164022Sdds 7164022Sdds * Old style: defines how int32 and u_int32 are treated. 8164022Sdds * For 64 bit systems like the DEC Alpha, they have to be defined 9164022Sdds * as int and u_int. 10164022Sdds * For 32 bit systems, define them as long and u_long 11164022Sdds */ 12164022Sdds#ifndef NTP_TYPES_H 13164022Sdds#define NTP_TYPES_H 14164022Sdds 15164022Sdds#include <sys/types.h> 16164022Sdds#if defined(HAVE_INTTYPES_H) 17164022Sdds# include <inttypes.h> 18164022Sdds#endif 19164022Sdds#if defined(HAVE_STDINT_H) 20164022Sdds# include <stdint.h> 21164022Sdds#endif 22164022Sdds 23164022Sdds/* Bug 2813 */ 24164022Sdds#ifdef HAVE_LIMITS_H 25164022Sdds# include <limits.h> 26164022Sdds#endif 27164022Sdds 28164022Sdds#include "ntp_machine.h" 29164022Sdds 30164022Sdds 31164022Sdds#ifndef TRUE 32164022Sdds# define TRUE 1 33164022Sdds#endif 34164022Sdds#ifndef FALSE 35164022Sdds# define FALSE 0 36164022Sdds#endif 37164022Sdds 38164022Sdds/* 39164022Sdds * This is another naming conflict. 40164022Sdds * On NetBSD for MAC the macro "mac" is defined as 1 41164022Sdds * this is fun for us as a packet structure contains an 42164022Sdds * optional "mac" member - severe confusion results 8-) 43164022Sdds * As we hopefully do not have to rely on that macro we 44164022Sdds * just undefine that. 45164022Sdds */ 46164022Sdds#ifdef mac 47164022Sdds#undef mac 48164022Sdds#endif 49164022Sdds 50164022Sdds/* 51164022Sdds * used to quiet compiler warnings 52164022Sdds */ 53164022Sdds#ifndef UNUSED_ARG 54164022Sdds#define UNUSED_ARG(arg) ((void)(arg)) 55164022Sdds#endif 56164022Sdds#ifndef UNUSED_LOCAL 57164022Sdds#define UNUSED_LOCAL(arg) ((void)(arg)) 58164022Sdds#endif 59164022Sdds 60164022Sdds/* 61164022Sdds * COUNTOF(array) - size of array in elements 62164022Sdds */ 63164022Sdds#define COUNTOF(arr) (sizeof(arr) / sizeof((arr)[0])) 64164022Sdds 65164022Sdds/* 66164022Sdds * VMS DECC (v4.1), {u_char,u_short,u_long} are only in SOCKET.H, 67164022Sdds * and u_int isn't defined anywhere 68164022Sdds */ 69164022Sdds#if defined(VMS) 70164022Sdds#include <socket.h> 71164022Sddstypedef unsigned int u_int; 72164022Sdds#endif /* VMS */ 73164022Sdds 74164022Sdds#ifdef HAVE_UINT32_T 75164022Sdds# ifndef HAVE_INT32 76164022Sdds typedef int32_t int32; 77164022Sdds# endif 78164022Sdds# ifndef HAVE_U_INT32 79164022Sdds typedef uint32_t u_int32; 80164022Sdds# if defined(UINT32_MAX) && !defined(U_INT32_MAX) 81164022Sdds# define U_INT32_MAX UINT32_MAX 82164022Sdds# endif 83164022Sdds# endif 84164022Sdds#elif (SIZEOF_INT == 4) 85164022Sdds# if !defined(HAVE_INT32) && !defined(int32) 86164022Sdds typedef int int32; 87164022Sdds# ifndef INT32_MIN 88164022Sdds# define INT32_MIN INT_MIN 89164022Sdds# endif 90164022Sdds# ifndef INT32_MAX 91164022Sdds# define INT32_MAX INT_MAX 92164022Sdds# endif 93164022Sdds# endif 94164022Sdds# if !defined(HAVE_U_INT32) && !defined(u_int32) 95164022Sdds typedef unsigned u_int32; 96164022Sdds# if defined(UINT_MAX) && !defined(U_INT32_MAX) 97164022Sdds# define U_INT32_MAX UINT_MAX 98164022Sdds# endif 99164022Sdds# endif 100164022Sdds#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) 221 222typedef u_int32 tstamp_t; /* NTP seconds timestamp */ 223 224/* 225 * Cloning malloc()'s behavior of always returning pointers suitably 226 * aligned for the strictest alignment requirement of any type is not 227 * easy to do portably, as the maximum alignment required is not 228 * exposed. Use the size of a union of the types known to represent the 229 * strictest alignment on some platform. 230 */ 231typedef union max_alignment_tag { 232 double d; 233} max_alignment; 234 235#define MAXALIGN sizeof(max_alignment) 236#define ALIGN_UNITS(sz) (((sz) + MAXALIGN - 1) / MAXALIGN) 237#define ALIGNED_SIZE(sz) (MAXALIGN * ALIGN_UNITS(sz)) 238#define INC_ALIGNED_PTR(b, m) ((void *)aligned_ptr((void *)(b), m)) 239 240static inline 241max_alignment * 242aligned_ptr( 243 max_alignment * base, 244 size_t minsize 245 ) 246{ 247 return base + ALIGN_UNITS((minsize < 1) ? 1 : minsize); 248} 249 250/* 251 * Macro to use in otherwise-empty source files to comply with ANSI C 252 * requirement that each translation unit (source file) contain some 253 * declaration. This has commonly been done by declaring an unused 254 * global variable of type int or char. An extern reference to exit() 255 * serves the same purpose without bloat. 256 */ 257#define NONEMPTY_TRANSLATION_UNIT extern void exit(int); 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) 285typedef 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