Blame util.h

Packit 96c956
/*
Packit 96c956
  chronyd/chronyc - Programs for keeping computer clocks accurate.
Packit 96c956
Packit 96c956
 **********************************************************************
Packit 96c956
 * Copyright (C) Richard P. Curnow  1997-2003
Packit 96c956
 * 
Packit 96c956
 * This program is free software; you can redistribute it and/or modify
Packit 96c956
 * it under the terms of version 2 of the GNU General Public License as
Packit 96c956
 * published by the Free Software Foundation.
Packit 96c956
 * 
Packit 96c956
 * This program is distributed in the hope that it will be useful, but
Packit 96c956
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 96c956
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 96c956
 * General Public License for more details.
Packit 96c956
 * 
Packit 96c956
 * You should have received a copy of the GNU General Public License along
Packit 96c956
 * with this program; if not, write to the Free Software Foundation, Inc.,
Packit 96c956
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Packit 96c956
 * 
Packit 96c956
 **********************************************************************
Packit 96c956
Packit 96c956
  =======================================================================
Packit 96c956
Packit 96c956
  Various utility functions
Packit 96c956
  */
Packit 96c956
Packit 96c956
#ifndef GOT_UTIL_H
Packit 96c956
#define GOT_UTIL_H
Packit 96c956
Packit 96c956
#include "sysincl.h"
Packit 96c956
Packit 96c956
#include "addressing.h"
Packit 96c956
#include "ntp.h"
Packit 96c956
#include "candm.h"
Packit 96c956
#include "hash.h"
Packit 96c956
Packit 96c956
/* Zero a timespec */
Packit 96c956
extern void UTI_ZeroTimespec(struct timespec *ts);
Packit 96c956
Packit 96c956
/* Check if a timespec is zero */
Packit 96c956
extern int UTI_IsZeroTimespec(struct timespec *ts);
Packit 96c956
Packit 96c956
/* Convert a timeval into a timespec */
Packit 96c956
extern void UTI_TimevalToTimespec(struct timeval *tv, struct timespec *ts);
Packit 96c956
Packit 96c956
/* Convert a timespec into a timeval */
Packit 96c956
extern void UTI_TimespecToTimeval(struct timespec *ts, struct timeval *tv);
Packit 96c956
Packit 96c956
/* Convert a timespec into a floating point number of seconds */
Packit 96c956
extern double UTI_TimespecToDouble(struct timespec *ts);
Packit 96c956
Packit 96c956
/* Convert a number of seconds expressed in floating point into a
Packit 96c956
   timespec */
Packit 96c956
extern void UTI_DoubleToTimespec(double d, struct timespec *ts);
Packit 96c956
Packit 96c956
/* Normalise a timespec, by adding or subtracting seconds to bring
Packit 96c956
   its nanosecond field into range */
Packit 96c956
extern void UTI_NormaliseTimespec(struct timespec *ts);
Packit 96c956
Packit 96c956
/* Convert a timeval into a floating point number of seconds */
Packit 96c956
extern double UTI_TimevalToDouble(struct timeval *tv);
Packit 96c956
Packit 96c956
/* Convert a number of seconds expressed in floating point into a
Packit 96c956
   timeval */
Packit 96c956
extern void UTI_DoubleToTimeval(double a, struct timeval *b);
Packit 96c956
Packit 96c956
/* Normalise a struct timeval, by adding or subtracting seconds to bring
Packit 96c956
   its microseconds field into range */
Packit 96c956
extern void UTI_NormaliseTimeval(struct timeval *x);
Packit 96c956
Packit 96c956
/* Returns -1 if a comes earlier than b, 0 if a is the same time as b,
Packit 96c956
   and +1 if a comes after b */
Packit 96c956
extern int UTI_CompareTimespecs(struct timespec *a, struct timespec *b);
Packit 96c956
Packit 96c956
/* Calculate result = a - b */
Packit 96c956
extern void UTI_DiffTimespecs(struct timespec *result, struct timespec *a, struct timespec *b);
Packit 96c956
Packit 96c956
/* Calculate result = a - b and return as a double */
Packit 96c956
extern double UTI_DiffTimespecsToDouble(struct timespec *a, struct timespec *b);
Packit 96c956
Packit 96c956
/* Add a double increment to a timespec to get a new one. 'start' is
Packit 96c956
   the starting time, 'end' is the result that we return.  This is
Packit 96c956
   safe to use if start and end are the same */
Packit 96c956
extern void UTI_AddDoubleToTimespec(struct timespec *start, double increment, struct timespec *end);
Packit 96c956
Packit 96c956
/* Calculate the average and difference (as a double) of two timespecs */
Packit 96c956
extern void UTI_AverageDiffTimespecs(struct timespec *earlier, struct timespec *later, struct timespec *average, double *diff);
Packit 96c956
Packit 96c956
/* Calculate result = a - b + c */
Packit 96c956
extern void UTI_AddDiffToTimespec(struct timespec *a, struct timespec *b, struct timespec *c, struct timespec *result);
Packit 96c956
Packit 96c956
/* Convert a timespec into a temporary string, largely for diagnostic
Packit 96c956
   display */
Packit 96c956
extern char *UTI_TimespecToString(struct timespec *ts);
Packit 96c956
Packit 96c956
/* Convert an NTP timestamp into a temporary string, largely for
Packit 96c956
   diagnostic display */
Packit 96c956
extern char *UTI_Ntp64ToString(NTP_int64 *ts);
Packit 96c956
Packit 96c956
/* Convert ref_id into a temporary string, for diagnostics */
Packit 96c956
extern char *UTI_RefidToString(uint32_t ref_id);
Packit 96c956
Packit 96c956
/* Convert an IP address to string, for diagnostics */
Packit 96c956
extern char *UTI_IPToString(IPAddr *ip);
Packit 96c956
Packit 96c956
extern int UTI_StringToIP(const char *addr, IPAddr *ip);
Packit 96c956
extern uint32_t UTI_IPToRefid(IPAddr *ip);
Packit 96c956
extern uint32_t UTI_IPToHash(IPAddr *ip);
Packit 96c956
extern void UTI_IPHostToNetwork(IPAddr *src, IPAddr *dest);
Packit 96c956
extern void UTI_IPNetworkToHost(IPAddr *src, IPAddr *dest);
Packit 96c956
extern int UTI_CompareIPs(IPAddr *a, IPAddr *b, IPAddr *mask);
Packit 96c956
Packit 96c956
extern void UTI_SockaddrToIPAndPort(struct sockaddr *sa, IPAddr *ip, unsigned short *port);
Packit 96c956
extern int UTI_IPAndPortToSockaddr(IPAddr *ip, unsigned short port, struct sockaddr *sa);
Packit 96c956
extern char *UTI_SockaddrToString(struct sockaddr *sa);
Packit 96c956
extern const char *UTI_SockaddrFamilyToString(int family);
Packit 96c956
Packit 96c956
extern char *UTI_TimeToLogForm(time_t t);
Packit 96c956
Packit 96c956
/* Adjust time following a frequency/offset change */
Packit 96c956
extern void UTI_AdjustTimespec(struct timespec *old_ts, struct timespec *when, struct timespec *new_ts, double *delta_time, double dfreq, double doffset);
Packit 96c956
Packit 96c956
/* Get zero NTP timestamp with random bits below precision */
Packit 96c956
extern void UTI_GetNtp64Fuzz(NTP_int64 *ts, int precision);
Packit 96c956
Packit 96c956
extern double UTI_Ntp32ToDouble(NTP_int32 x);
Packit 96c956
extern NTP_int32 UTI_DoubleToNtp32(double x);
Packit 96c956
Packit 96c956
/* Zero an NTP timestamp */
Packit 96c956
extern void UTI_ZeroNtp64(NTP_int64 *ts);
Packit 96c956
Packit 96c956
/* Check if an NTP timestamp is zero */
Packit 96c956
extern int UTI_IsZeroNtp64(NTP_int64 *ts);
Packit 96c956
Packit 96c956
/* Compare two NTP timestamps.  Returns -1 if a is before b, 0 if a is equal to
Packit 96c956
   b, and 1 if a is after b. */
Packit 96c956
extern int UTI_CompareNtp64(NTP_int64 *a, NTP_int64 *b);
Packit 96c956
Packit 96c956
/* Compare an NTP timestamp with up to three other timestamps.  Returns 0
Packit 96c956
   if a is not equal to any of b1, b2, and b3, 1 otherwise. */
Packit 96c956
extern int UTI_IsEqualAnyNtp64(NTP_int64 *a, NTP_int64 *b1, NTP_int64 *b2, NTP_int64 *b3);
Packit 96c956
Packit 96c956
/* Convert a timespec into an NTP timestamp */
Packit 96c956
extern void UTI_TimespecToNtp64(struct timespec *src, NTP_int64 *dest, NTP_int64 *fuzz);
Packit 96c956
Packit 96c956
/* Convert an NTP timestamp into a timespec */
Packit 96c956
extern void UTI_Ntp64ToTimespec(NTP_int64 *src, struct timespec *dest);
Packit 96c956
Packit 96c956
/* Check if time + offset is sane */
Packit 96c956
extern int UTI_IsTimeOffsetSane(struct timespec *ts, double offset);
Packit 96c956
Packit 96c956
/* Get 2 raised to power of a signed integer */
Packit 96c956
extern double UTI_Log2ToDouble(int l);
Packit 96c956
Packit 96c956
extern void UTI_TimespecNetworkToHost(Timespec *src, struct timespec *dest);
Packit 96c956
extern void UTI_TimespecHostToNetwork(struct timespec *src, Timespec *dest);
Packit 96c956
Packit 96c956
extern double UTI_FloatNetworkToHost(Float x);
Packit 96c956
extern Float UTI_FloatHostToNetwork(double x);
Packit 96c956
Packit 96c956
/* Set FD_CLOEXEC on descriptor */
Packit 96c956
extern int UTI_FdSetCloexec(int fd);
Packit 96c956
Packit 96c956
extern void UTI_SetQuitSignalsHandler(void (*handler)(int), int ignore_sigpipe);
Packit 96c956
Packit 96c956
/* Get directory (as an allocated string) for a path */
Packit 96c956
extern char *UTI_PathToDir(const char *path);
Packit 96c956
Packit 96c956
/* Create a directory with a specified mode (umasked) and set its uid/gid.
Packit 96c956
   Create also any parent directories that don't exist with mode 755 and
Packit 96c956
   default uid/gid.  Returns 1 if created or already exists (even with
Packit 96c956
   different mode/uid/gid), 0 otherwise. */
Packit 96c956
extern int UTI_CreateDirAndParents(const char *path, mode_t mode, uid_t uid, gid_t gid);
Packit 96c956
Packit 96c956
/* Check if a directory is secure.  It must not have other than the specified
Packit 96c956
   permissions and its uid/gid must match the specified values. */
Packit 96c956
extern int UTI_CheckDirPermissions(const char *path, mode_t perm, uid_t uid, gid_t gid);
Packit 96c956
Packit 96c956
/* Set process user/group IDs and drop supplementary groups */
Packit 96c956
extern void UTI_DropRoot(uid_t uid, gid_t gid);
Packit 96c956
Packit 96c956
/* Fill buffer with random bytes from /dev/urandom */
Packit 96c956
extern void UTI_GetRandomBytesUrandom(void *buf, unsigned int len);
Packit 96c956
Packit 96c956
/* Fill buffer with random bytes from /dev/urandom or a faster source if it's
Packit 96c956
   available (e.g. arc4random()), which may not necessarily be suitable for
Packit 96c956
   generating long-term keys */
Packit 96c956
extern void UTI_GetRandomBytes(void *buf, unsigned int len);
Packit 96c956
Packit 96c956
/* Macros to get maximum and minimum of two values */
Packit 96c956
#ifdef MAX
Packit 96c956
#undef MAX
Packit 96c956
#endif
Packit 96c956
#define MAX(x, y) ((x) > (y) ? (x) : (y))
Packit 96c956
#ifdef MIN
Packit 96c956
#undef MIN
Packit 96c956
#endif
Packit 96c956
#define MIN(x, y) ((x) < (y) ? (x) : (y))
Packit 96c956
Packit 96c956
/* Macro to clamp a value between two values */
Packit 96c956
#define CLAMP(min, x, max) (MAX((min), MIN((x), (max))))
Packit 96c956
Packit 96c956
#define SQUARE(x) ((x) * (x))
Packit 96c956
Packit 96c956
#endif /* GOT_UTIL_H */