]>
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"
37 /* Convert a timeval into a floating point number of seconds */
38 extern void UTI_TimevalToDouble(struct timeval
*a
, double *b
);
40 /* Convert a number of seconds expressed in floating point into a
42 extern void UTI_DoubleToTimeval(double a
, struct timeval
*b
);
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
);
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
);
52 /* Calculate result = a - b */
53 extern void UTI_DiffTimevals(struct timeval
*result
, struct timeval
*a
, struct timeval
*b
);
55 /* Calculate result = a - b and return as a double */
56 extern void UTI_DiffTimevalsToDouble(double *result
, struct timeval
*a
, struct timeval
*b
);
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
);
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
);
66 /* Calculate result = a - b + c */
67 extern void UTI_AddDiffToTimeval(struct timeval
*a
, struct timeval
*b
, struct timeval
*c
, struct timeval
*result
);
69 /* Convert a timeval into a temporary string, largely for diagnostic
71 extern char *UTI_TimevalToString(struct timeval
*tv
);
73 /* Convert an NTP timestamp into a temporary string, largely for
75 extern char *UTI_TimestampToString(NTP_int64
*ts
);
77 /* Convert ref_id into a temporary string, for diagnostics */
78 extern char *UTI_RefidToString(uint32_t ref_id
);
80 /* Convert an IP address to string, for diagnostics */
81 extern char *UTI_IPToString(IPAddr
*ip
);
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
);
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
);
94 extern char *UTI_TimeToLogForm(time_t t
);
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
);
99 /* Get a random value to fuzz an NTP timestamp in the given precision */
100 extern uint32_t UTI_GetNTPTsFuzz(int precision
);
102 extern double UTI_Int32ToDouble(NTP_int32 x
);
103 extern NTP_int32
UTI_DoubleToInt32(double x
);
105 extern void UTI_TimevalToInt64(struct timeval
*src
, NTP_int64
*dest
, uint32_t fuzz
);
107 extern void UTI_Int64ToTimeval(NTP_int64
*src
, struct timeval
*dest
);
109 /* Check if time + offset is sane */
110 extern int UTI_IsTimeOffsetSane(struct timeval
*tv
, double offset
);
112 /* Get 2 raised to power of a signed integer */
113 extern double UTI_Log2ToDouble(int l
);
115 extern void UTI_TimevalNetworkToHost(Timeval
*src
, struct timeval
*dest
);
116 extern void UTI_TimevalHostToNetwork(struct timeval
*src
, Timeval
*dest
);
118 extern double UTI_FloatNetworkToHost(Float x
);
119 extern Float
UTI_FloatHostToNetwork(double x
);
121 /* Set FD_CLOEXEC on descriptor */
122 extern int UTI_FdSetCloexec(int fd
);
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
);
129 /* Decode password encoded in ASCII or HEX */
130 extern int UTI_DecodePasswordFromText(char *key
);
132 extern int UTI_SetQuitSignalsHandler(void (*handler
)(int));
134 /* Get directory (as an allocated string) for a path */
135 extern char *UTI_PathToDir(const char *path
);
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
);
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
);
147 /* Fill buffer with random bytes */
148 extern void UTI_GetRandomBytes(void *buf
, unsigned int len
);
150 #endif /* GOT_UTIL_H */