]> git.ipfire.org Git - thirdparty/chrony.git/blob - util.h
cmdmon: print path of Unix command socket in debug messages
[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 /* Convert a timeval into a floating point number of seconds */
38 extern void UTI_TimevalToDouble(struct timeval *a, double *b);
39
40 /* Convert a number of seconds expressed in floating point into a
41 timeval */
42 extern void UTI_DoubleToTimeval(double a, struct timeval *b);
43
44 /* Returns -1 if a comes earlier than b, 0 if a is the same time as b,
45 and +1 if a comes after b */
46 extern int UTI_CompareTimevals(struct timeval *a, struct timeval *b);
47
48 /* Normalise a struct timeval, by adding or subtracting seconds to bring
49 its microseconds field into range */
50 extern void UTI_NormaliseTimeval(struct timeval *x);
51
52 /* Calculate result = a - b */
53 extern void UTI_DiffTimevals(struct timeval *result, struct timeval *a, struct timeval *b);
54
55 /* Calculate result = a - b and return as a double */
56 extern void UTI_DiffTimevalsToDouble(double *result, struct timeval *a, struct timeval *b);
57
58 /* Add a double increment to a timeval to get a new one. 'start' is
59 the starting time, 'end' is the result that we return. This is
60 safe to use if start and end are the same */
61 extern void UTI_AddDoubleToTimeval(struct timeval *start, double increment, struct timeval *end);
62
63 /* Calculate the average and difference (as a double) of two timevals */
64 extern void UTI_AverageDiffTimevals(struct timeval *earlier, struct timeval *later, struct timeval *average, double *diff);
65
66 /* Calculate result = a - b + c */
67 extern void UTI_AddDiffToTimeval(struct timeval *a, struct timeval *b, struct timeval *c, struct timeval *result);
68
69 /* Convert a timeval into a temporary string, largely for diagnostic
70 display */
71 extern char *UTI_TimevalToString(struct timeval *tv);
72
73 /* Convert an NTP timestamp into a temporary string, largely for
74 diagnostic display */
75 extern char *UTI_TimestampToString(NTP_int64 *ts);
76
77 /* Convert ref_id into a temporary string, for diagnostics */
78 extern char *UTI_RefidToString(uint32_t ref_id);
79
80 /* Convert an IP address to string, for diagnostics */
81 extern char *UTI_IPToString(IPAddr *ip);
82
83 extern int UTI_StringToIP(const char *addr, IPAddr *ip);
84 extern uint32_t UTI_IPToRefid(IPAddr *ip);
85 extern void UTI_IPHostToNetwork(IPAddr *src, IPAddr *dest);
86 extern void UTI_IPNetworkToHost(IPAddr *src, IPAddr *dest);
87 extern int UTI_CompareIPs(IPAddr *a, IPAddr *b, IPAddr *mask);
88
89 extern void UTI_SockaddrToIPAndPort(struct sockaddr *sa, IPAddr *ip, unsigned short *port);
90 extern int UTI_IPAndPortToSockaddr(IPAddr *ip, unsigned short port, struct sockaddr *sa);
91 extern char *UTI_SockaddrToString(struct sockaddr *sa);
92 extern const char *UTI_SockaddrFamilyToString(int family);
93
94 extern char *UTI_TimeToLogForm(time_t t);
95
96 /* Adjust time following a frequency/offset change */
97 extern void UTI_AdjustTimeval(struct timeval *old_tv, struct timeval *when, struct timeval *new_tv, double *delta, double dfreq, double doffset);
98
99 /* Get a random value to fuzz an NTP timestamp in the given precision */
100 extern uint32_t UTI_GetNTPTsFuzz(int precision);
101
102 extern double UTI_Int32ToDouble(NTP_int32 x);
103 extern NTP_int32 UTI_DoubleToInt32(double x);
104
105 extern void UTI_TimevalToInt64(struct timeval *src, NTP_int64 *dest, uint32_t fuzz);
106
107 extern void UTI_Int64ToTimeval(NTP_int64 *src, struct timeval *dest);
108
109 /* Check if time + offset is sane */
110 extern int UTI_IsTimeOffsetSane(struct timeval *tv, double offset);
111
112 /* Get 2 raised to power of a signed integer */
113 extern double UTI_Log2ToDouble(int l);
114
115 extern void UTI_TimevalNetworkToHost(Timeval *src, struct timeval *dest);
116 extern void UTI_TimevalHostToNetwork(struct timeval *src, Timeval *dest);
117
118 extern double UTI_FloatNetworkToHost(Float x);
119 extern Float UTI_FloatHostToNetwork(double x);
120
121 /* Set FD_CLOEXEC on descriptor */
122 extern int UTI_FdSetCloexec(int fd);
123
124 extern int UTI_GenerateNTPAuth(int hash_id, const unsigned char *key, int key_len,
125 const unsigned char *data, int data_len, unsigned char *auth, int auth_len);
126 extern int UTI_CheckNTPAuth(int hash_id, const unsigned char *key, int key_len,
127 const unsigned char *data, int data_len, const unsigned char *auth, int auth_len);
128
129 /* Decode password encoded in ASCII or HEX */
130 extern int UTI_DecodePasswordFromText(char *key);
131
132 extern int UTI_SetQuitSignalsHandler(void (*handler)(int));
133
134 /* Get directory (as an allocated string) for a path */
135 extern char *UTI_PathToDir(const char *path);
136
137 /* Create a directory with a specified mode (umasked) and set its uid/gid.
138 Create also any parent directories that don't exist with mode 755 and
139 default uid/gid. Returns 1 if created or already exists (even with
140 different mode/uid/gid), 0 otherwise. */
141 extern int UTI_CreateDirAndParents(const char *path, mode_t mode, uid_t uid, gid_t gid);
142
143 /* Check if a directory is secure. It must not have other than the specified
144 permissions and its uid/gid must match the specified values. */
145 extern int UTI_CheckDirPermissions(const char *path, mode_t perm, uid_t uid, gid_t gid);
146
147 #endif /* GOT_UTIL_H */