]>
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
);
92 extern char *UTI_TimeToLogForm(time_t t
);
94 /* Adjust time following a frequency/offset change */
95 extern void UTI_AdjustTimeval(struct timeval
*old_tv
, struct timeval
*when
, struct timeval
*new_tv
, double *delta
, double dfreq
, double doffset
);
97 /* Get a random value to fuzz an NTP timestamp in the given precision */
98 extern uint32_t UTI_GetNTPTsFuzz(int precision
);
100 extern double UTI_Int32ToDouble(NTP_int32 x
);
101 extern NTP_int32
UTI_DoubleToInt32(double x
);
103 extern void UTI_TimevalToInt64(struct timeval
*src
, NTP_int64
*dest
, uint32_t fuzz
);
105 extern void UTI_Int64ToTimeval(NTP_int64
*src
, struct timeval
*dest
);
107 /* Check if time + offset is sane */
108 extern int UTI_IsTimeOffsetSane(struct timeval
*tv
, double offset
);
110 /* Get 2 raised to power of a signed integer */
111 extern double UTI_Log2ToDouble(int l
);
113 extern void UTI_TimevalNetworkToHost(Timeval
*src
, struct timeval
*dest
);
114 extern void UTI_TimevalHostToNetwork(struct timeval
*src
, Timeval
*dest
);
116 extern double UTI_FloatNetworkToHost(Float x
);
117 extern Float
UTI_FloatHostToNetwork(double x
);
119 /* Set FD_CLOEXEC on descriptor */
120 extern int UTI_FdSetCloexec(int fd
);
122 extern int UTI_GenerateNTPAuth(int hash_id
, const unsigned char *key
, int key_len
,
123 const unsigned char *data
, int data_len
, unsigned char *auth
, int auth_len
);
124 extern int UTI_CheckNTPAuth(int hash_id
, const unsigned char *key
, int key_len
,
125 const unsigned char *data
, int data_len
, const unsigned char *auth
, int auth_len
);
127 /* Decode password encoded in ASCII or HEX */
128 extern int UTI_DecodePasswordFromText(char *key
);
130 #if defined (INLINE_UTILITIES)
131 #define INLINE_STATIC inline static
134 #define INLINE_STATIC
135 #endif /* defined (INLINE_UTILITIES) */
137 #endif /* GOT_UTIL_H */