]> git.ipfire.org Git - thirdparty/chrony.git/blob - util.h
util: add UTI_IPSockAddrToString()
[thirdparty/chrony.git] / util.h
1 /*
2 chronyd/chronyc - Programs for keeping computer clocks accurate.
3
4 **********************************************************************
5 * Copyright (C) Richard P. Curnow 1997-2003
6 *
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.
10 *
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.
15 *
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.
19 *
20 **********************************************************************
21
22 =======================================================================
23
24 Various utility functions
25 */
26
27 #ifndef GOT_UTIL_H
28 #define GOT_UTIL_H
29
30 #include "sysincl.h"
31
32 #include "addressing.h"
33 #include "ntp.h"
34 #include "candm.h"
35 #include "hash.h"
36
37 /* Zero a timespec */
38 extern void UTI_ZeroTimespec(struct timespec *ts);
39
40 /* Check if a timespec is zero */
41 extern int UTI_IsZeroTimespec(struct timespec *ts);
42
43 /* Convert a timeval into a timespec */
44 extern void UTI_TimevalToTimespec(struct timeval *tv, struct timespec *ts);
45
46 /* Convert a timespec into a timeval */
47 extern void UTI_TimespecToTimeval(struct timespec *ts, struct timeval *tv);
48
49 /* Convert a timespec into a floating point number of seconds */
50 extern double UTI_TimespecToDouble(struct timespec *ts);
51
52 /* Convert a number of seconds expressed in floating point into a
53 timespec */
54 extern void UTI_DoubleToTimespec(double d, struct timespec *ts);
55
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);
59
60 /* Convert a timeval into a floating point number of seconds */
61 extern double UTI_TimevalToDouble(struct timeval *tv);
62
63 /* Convert a number of seconds expressed in floating point into a
64 timeval */
65 extern void UTI_DoubleToTimeval(double a, struct timeval *b);
66
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);
70
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);
74
75 /* Calculate result = a - b */
76 extern void UTI_DiffTimespecs(struct timespec *result, struct timespec *a, struct timespec *b);
77
78 /* Calculate result = a - b and return as a double */
79 extern double UTI_DiffTimespecsToDouble(struct timespec *a, struct timespec *b);
80
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);
85
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);
88
89 /* Calculate result = a - b + c */
90 extern void UTI_AddDiffToTimespec(struct timespec *a, struct timespec *b, struct timespec *c, struct timespec *result);
91
92 /* Convert a timespec into a temporary string, largely for diagnostic
93 display */
94 extern char *UTI_TimespecToString(struct timespec *ts);
95
96 /* Convert an NTP timestamp into a temporary string, largely for
97 diagnostic display */
98 extern char *UTI_Ntp64ToString(NTP_int64 *ts);
99
100 /* Convert ref_id into a temporary string, for diagnostics */
101 extern char *UTI_RefidToString(uint32_t ref_id);
102
103 /* Convert an IP address to string, for diagnostics */
104 extern char *UTI_IPToString(IPAddr *ip);
105
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);
112
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);
117
118 extern char *UTI_IPSockAddrToString(IPSockAddr *sa);
119
120 extern char *UTI_TimeToLogForm(time_t t);
121
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);
124
125 /* Get zero NTP timestamp with random bits below precision */
126 extern void UTI_GetNtp64Fuzz(NTP_int64 *ts, int precision);
127
128 extern double UTI_Ntp32ToDouble(NTP_int32 x);
129 extern NTP_int32 UTI_DoubleToNtp32(double x);
130
131 /* Zero an NTP timestamp */
132 extern void UTI_ZeroNtp64(NTP_int64 *ts);
133
134 /* Check if an NTP timestamp is zero */
135 extern int UTI_IsZeroNtp64(NTP_int64 *ts);
136
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);
140
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);
144
145 /* Convert a timespec into an NTP timestamp */
146 extern void UTI_TimespecToNtp64(struct timespec *src, NTP_int64 *dest, NTP_int64 *fuzz);
147
148 /* Convert an NTP timestamp into a timespec */
149 extern void UTI_Ntp64ToTimespec(NTP_int64 *src, struct timespec *dest);
150
151 /* Check if time + offset is sane */
152 extern int UTI_IsTimeOffsetSane(struct timespec *ts, double offset);
153
154 /* Get 2 raised to power of a signed integer */
155 extern double UTI_Log2ToDouble(int l);
156
157 extern void UTI_TimespecNetworkToHost(Timespec *src, struct timespec *dest);
158 extern void UTI_TimespecHostToNetwork(struct timespec *src, Timespec *dest);
159
160 extern double UTI_FloatNetworkToHost(Float x);
161 extern Float UTI_FloatHostToNetwork(double x);
162
163 /* Set FD_CLOEXEC on descriptor */
164 extern int UTI_FdSetCloexec(int fd);
165
166 extern void UTI_SetQuitSignalsHandler(void (*handler)(int), int ignore_sigpipe);
167
168 /* Get directory (as an allocated string) for a path */
169 extern char *UTI_PathToDir(const char *path);
170
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);
176
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);
180
181 /* Set process user/group IDs and drop supplementary groups */
182 extern void UTI_DropRoot(uid_t uid, gid_t gid);
183
184 /* Fill buffer with random bytes from /dev/urandom */
185 extern void UTI_GetRandomBytesUrandom(void *buf, unsigned int len);
186
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);
191
192 /* Macros to get maximum and minimum of two values */
193 #ifdef MAX
194 #undef MAX
195 #endif
196 #define MAX(x, y) ((x) > (y) ? (x) : (y))
197 #ifdef MIN
198 #undef MIN
199 #endif
200 #define MIN(x, y) ((x) < (y) ? (x) : (y))
201
202 /* Macro to clamp a value between two values */
203 #define CLAMP(min, x, max) (MAX((min), MIN((x), (max))))
204
205 #define SQUARE(x) ((x) * (x))
206
207 #endif /* GOT_UTIL_H */