1/* $NetBSD: ntp_types.h,v 1.7 2020/05/25 20:47:20 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 43typedef 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> 80typedef 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 148typedef int64_t int64; 149typedef uint64_t u_int64; 150 151# elif SIZEOF_LONG == 8 152 153typedef long int64; 154typedef unsigned long u_int64; 155 156# elif SIZEOF_LONG_LONG == 8 157 158typedef long long int64; 159typedef 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 * 64 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 64bit 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 184typedef 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 220typedef uint8_t ntp_u_int8_t; 221typedef uint16_t ntp_u_int16_t; 222typedef uint32_t ntp_u_int32_t; 223 224typedef struct ntp_uint64_t { u_int32 val[2]; } ntp_uint64_t; 225 226typedef uint16_t associd_t; /* association ID */ 227#define ASSOCID_MAX USHRT_MAX 228typedef u_int32 keyid_t; /* cryptographic key ID */ 229#define KEYID_T_MAX (0xffffffff) 230 231typedef 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 */ 240typedef 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 249static inline 250max_alignment * 251aligned_ptr( 252 max_alignment * base, 253 size_t minsize 254 ) 255{ 256 return base + ALIGN_UNITS((minsize < 1) ? 1 : minsize); 257} 258 259/* 260 * Macro to use in otherwise-empty source files to comply with ANSI C 261 * requirement that each translation unit (source file) contain some 262 * declaration. This has commonly been done by declaring an unused 263 * global variable of type int or char. An extern reference to exit() 264 * serves the same purpose without bloat. 265 */ 266#define NONEMPTY_TRANSLATION_UNIT extern void exit(int); 267 268/* 269 * On Unix struct sock_timeval is equivalent to struct timeval. 270 * On Windows built with 64-bit time_t, sock_timeval.tv_sec is a long 271 * as required by Windows' socket() interface timeout argument, while 272 * timeval.tv_sec is time_t for the more common use as a UTC time 273 * within NTP. 274 */ 275#ifndef SYS_WINNT 276#define sock_timeval timeval 277#endif 278 279/* 280 * On Unix open() works for tty (serial) devices just fine, while on 281 * Windows refclock serial devices are opened using CreateFile, a lower 282 * level than the CRT-provided descriptors, because the C runtime lacks 283 * tty APIs. For refclocks which wish to use open() as well as or 284 * instead of refclock_open(), tty_open() is equivalent to open() on 285 * Unix and implemented in the Windows port similarly to 286 * refclock_open(). 287 * Similarly, the termios emulation in the Windows code needs to know 288 * about serial ports being closed, while the Posix systems do not. 289 */ 290#ifndef SYS_WINNT 291# define tty_open(f, a, m) open(f, a, m) 292# define closeserial(fd) close(fd) 293# define closesocket(fd) close(fd) 294typedef int SOCKET; 295# define INVALID_SOCKET (-1) 296# define SOCKET_ERROR (-1) 297# define socket_errno() (errno) 298#else /* SYS_WINNT follows */ 299# define socket_errno() (errno = WSAGetLastError()) 300#endif 301 302 303 304#endif /* NTP_TYPES_H */ 305