]>
git.ipfire.org Git - thirdparty/chrony.git/blob - util.h
2 chronyd/chronyc - Programs for keeping computer clocks accurate.
4 **********************************************************************
5 * Copyright (C) Richard P. Curnow 1997-2003
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 **********************************************************************
22 =======================================================================
24 Various utility functions
32 #include "addressing.h"
38 extern void UTI_ZeroTimespec(struct timespec
*ts
);
40 /* Check if a timespec is zero */
41 extern int UTI_IsZeroTimespec(struct timespec
*ts
);
43 /* Convert a timeval into a timespec */
44 extern void UTI_TimevalToTimespec(struct timeval
*tv
, struct timespec
*ts
);
46 /* Convert a timespec into a timeval */
47 extern void UTI_TimespecToTimeval(struct timespec
*ts
, struct timeval
*tv
);
49 /* Convert a timespec into a floating point number of seconds */
50 extern double UTI_TimespecToDouble(struct timespec
*ts
);
52 /* Convert a number of seconds expressed in floating point into a
54 extern void UTI_DoubleToTimespec(double d
, struct timespec
*ts
);
56 /* Normalise a timespec, by adding or subtracting seconds to bring
57 its nanosecond field into range */
58 extern void UTI_NormaliseTimespec(struct timespec
*ts
);
60 /* Convert a timeval into a floating point number of seconds */
61 extern double UTI_TimevalToDouble(struct timeval
*tv
);
63 /* Convert a number of seconds expressed in floating point into a
65 extern void UTI_DoubleToTimeval(double a
, struct timeval
*b
);
67 /* Normalise a struct timeval, by adding or subtracting seconds to bring
68 its microseconds field into range */
69 extern void UTI_NormaliseTimeval(struct timeval
*x
);
71 /* Returns -1 if a comes earlier than b, 0 if a is the same time as b,
72 and +1 if a comes after b */
73 extern int UTI_CompareTimespecs(struct timespec
*a
, struct timespec
*b
);
75 /* Calculate result = a - b */
76 extern void UTI_DiffTimespecs(struct timespec
*result
, struct timespec
*a
, struct timespec
*b
);
78 /* Calculate result = a - b and return as a double */
79 extern double UTI_DiffTimespecsToDouble(struct timespec
*a
, struct timespec
*b
);
81 /* Add a double increment to a timespec to get a new one. 'start' is
82 the starting time, 'end' is the result that we return. This is
83 safe to use if start and end are the same */
84 extern void UTI_AddDoubleToTimespec(struct timespec
*start
, double increment
, struct timespec
*end
);
86 /* Calculate the average and difference (as a double) of two timespecs */
87 extern void UTI_AverageDiffTimespecs(struct timespec
*earlier
, struct timespec
*later
, struct timespec
*average
, double *diff
);
89 /* Calculate result = a - b + c */
90 extern void UTI_AddDiffToTimespec(struct timespec
*a
, struct timespec
*b
, struct timespec
*c
, struct timespec
*result
);
92 /* Convert a timespec into a temporary string, largely for diagnostic
94 extern char *UTI_TimespecToString(struct timespec
*ts
);
96 /* Convert an NTP timestamp into a temporary string, largely for
98 extern char *UTI_Ntp64ToString(NTP_int64
*ts
);
100 /* Convert ref_id into a temporary string, for diagnostics */
101 extern char *UTI_RefidToString(uint32_t ref_id
);
103 /* Convert an IP address to string, for diagnostics */
104 extern char *UTI_IPToString(IPAddr
*ip
);
106 extern int UTI_StringToIP(const char *addr
, IPAddr
*ip
);
107 extern uint32_t UTI_IPToRefid(IPAddr
*ip
);
108 extern uint32_t UTI_IPToHash(IPAddr
*ip
);
109 extern void UTI_IPHostToNetwork(IPAddr
*src
, IPAddr
*dest
);
110 extern void UTI_IPNetworkToHost(IPAddr
*src
, IPAddr
*dest
);
111 extern int UTI_CompareIPs(IPAddr
*a
, IPAddr
*b
, IPAddr
*mask
);
113 extern void UTI_SockaddrToIPAndPort(struct sockaddr
*sa
, IPAddr
*ip
, unsigned short *port
);
114 extern int UTI_IPAndPortToSockaddr(IPAddr
*ip
, unsigned short port
, struct sockaddr
*sa
);
115 extern char *UTI_SockaddrToString(struct sockaddr
*sa
);
116 extern const char *UTI_SockaddrFamilyToString(int family
);
118 extern char *UTI_IPSockAddrToString(IPSockAddr
*sa
);
120 extern char *UTI_TimeToLogForm(time_t t
);
122 /* Adjust time following a frequency/offset change */
123 extern void UTI_AdjustTimespec(struct timespec
*old_ts
, struct timespec
*when
, struct timespec
*new_ts
, double *delta_time
, double dfreq
, double doffset
);
125 /* Get zero NTP timestamp with random bits below precision */
126 extern void UTI_GetNtp64Fuzz(NTP_int64
*ts
, int precision
);
128 extern double UTI_Ntp32ToDouble(NTP_int32 x
);
129 extern NTP_int32
UTI_DoubleToNtp32(double x
);
131 /* Zero an NTP timestamp */
132 extern void UTI_ZeroNtp64(NTP_int64
*ts
);
134 /* Check if an NTP timestamp is zero */
135 extern int UTI_IsZeroNtp64(NTP_int64
*ts
);
137 /* Compare two NTP timestamps. Returns -1 if a is before b, 0 if a is equal to
138 b, and 1 if a is after b. */
139 extern int UTI_CompareNtp64(NTP_int64
*a
, NTP_int64
*b
);
141 /* Compare an NTP timestamp with up to three other timestamps. Returns 0
142 if a is not equal to any of b1, b2, and b3, 1 otherwise. */
143 extern int UTI_IsEqualAnyNtp64(NTP_int64
*a
, NTP_int64
*b1
, NTP_int64
*b2
, NTP_int64
*b3
);
145 /* Convert a timespec into an NTP timestamp */
146 extern void UTI_TimespecToNtp64(struct timespec
*src
, NTP_int64
*dest
, NTP_int64
*fuzz
);
148 /* Convert an NTP timestamp into a timespec */
149 extern void UTI_Ntp64ToTimespec(NTP_int64
*src
, struct timespec
*dest
);
151 /* Check if time + offset is sane */
152 extern int UTI_IsTimeOffsetSane(struct timespec
*ts
, double offset
);
154 /* Get 2 raised to power of a signed integer */
155 extern double UTI_Log2ToDouble(int l
);
157 extern void UTI_TimespecNetworkToHost(Timespec
*src
, struct timespec
*dest
);
158 extern void UTI_TimespecHostToNetwork(struct timespec
*src
, Timespec
*dest
);
160 extern double UTI_FloatNetworkToHost(Float x
);
161 extern Float
UTI_FloatHostToNetwork(double x
);
163 /* Set FD_CLOEXEC on descriptor */
164 extern int UTI_FdSetCloexec(int fd
);
166 extern void UTI_SetQuitSignalsHandler(void (*handler
)(int), int ignore_sigpipe
);
168 /* Get directory (as an allocated string) for a path */
169 extern char *UTI_PathToDir(const char *path
);
171 /* Create a directory with a specified mode (umasked) and set its uid/gid.
172 Create also any parent directories that don't exist with mode 755 and
173 default uid/gid. Returns 1 if created or already exists (even with
174 different mode/uid/gid), 0 otherwise. */
175 extern int UTI_CreateDirAndParents(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
);
177 /* Check if a directory is secure. It must not have other than the specified
178 permissions and its uid/gid must match the specified values. */
179 extern int UTI_CheckDirPermissions(const char *path
, mode_t perm
, uid_t uid
, gid_t gid
);
181 /* Set process user/group IDs and drop supplementary groups */
182 extern void UTI_DropRoot(uid_t uid
, gid_t gid
);
184 /* Fill buffer with random bytes from /dev/urandom */
185 extern void UTI_GetRandomBytesUrandom(void *buf
, unsigned int len
);
187 /* Fill buffer with random bytes from /dev/urandom or a faster source if it's
188 available (e.g. arc4random()), which may not necessarily be suitable for
189 generating long-term keys */
190 extern void UTI_GetRandomBytes(void *buf
, unsigned int len
);
192 /* Macros to get maximum and minimum of two values */
196 #define MAX(x, y) ((x) > (y) ? (x) : (y))
200 #define MIN(x, y) ((x) < (y) ? (x) : (y))
202 /* Macro to clamp a value between two values */
203 #define CLAMP(min, x, max) (MAX((min), MIN((x), (max))))
205 #define SQUARE(x) ((x) * (x))
207 #endif /* GOT_UTIL_H */