xref: /openbsd/usr.sbin/ntpd/util.c (revision 9b7c3dbb)
1 /*	$OpenBSD: util.c,v 1.20 2015/12/19 13:58:08 reyk Exp $ */
2 
3 /*
4  * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <limits.h>
20 #include <stdio.h>
21 #include <time.h>
22 
23 #include "ntpd.h"
24 
25 double
26 gettime_corrected(void)
27 {
28 	return (gettime() + getoffset());
29 }
30 
31 double
32 getoffset(void)
33 {
34 	struct timeval	tv;
35 	if (adjtime(NULL, &tv) == -1)
36 		return (0.0);
37 	return (tv.tv_sec + 1.0e-6 * tv.tv_usec);
38 }
39 
40 double
41 gettime(void)
42 {
43 	struct timeval	tv;
44 
45 	if (gettimeofday(&tv, NULL) == -1)
46 		fatal("gettimeofday");
47 
48 	return (gettime_from_timeval(&tv));
49 }
50 
51 double
52 gettime_from_timeval(struct timeval *tv)
53 {
54 	/*
55 	 * Account for overflow on OSes that have a 32-bit time_t.
56 	 */
57 	return ((uint64_t)tv->tv_sec + JAN_1970 + 1.0e-6 * tv->tv_usec);
58 }
59 
60 time_t
61 getmonotime(void)
62 {
63 	struct timespec	ts;
64 
65 	if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
66 		fatal("clock_gettime");
67 
68 	return (ts.tv_sec);
69 }
70 
71 
72 void
73 d_to_tv(double d, struct timeval *tv)
74 {
75 	tv->tv_sec = d;
76 	tv->tv_usec = (d - tv->tv_sec) * 1000000;
77 	while (tv->tv_usec < 0) {
78 		tv->tv_usec += 1000000;
79 		tv->tv_sec -= 1;
80 	}
81 }
82 
83 double
84 lfp_to_d(struct l_fixedpt lfp)
85 {
86 	double	ret;
87 
88 	lfp.int_partl = ntohl(lfp.int_partl);
89 	lfp.fractionl = ntohl(lfp.fractionl);
90 
91 	ret = (double)(lfp.int_partl) + ((double)lfp.fractionl / UINT_MAX);
92 
93 	return (ret);
94 }
95 
96 struct l_fixedpt
97 d_to_lfp(double d)
98 {
99 	struct l_fixedpt	lfp;
100 
101 	lfp.int_partl = htonl((u_int32_t)d);
102 	lfp.fractionl = htonl((u_int32_t)((d - (u_int32_t)d) * UINT_MAX));
103 
104 	return (lfp);
105 }
106 
107 double
108 sfp_to_d(struct s_fixedpt sfp)
109 {
110 	double	ret;
111 
112 	sfp.int_parts = ntohs(sfp.int_parts);
113 	sfp.fractions = ntohs(sfp.fractions);
114 
115 	ret = (double)(sfp.int_parts) + ((double)sfp.fractions / USHRT_MAX);
116 
117 	return (ret);
118 }
119 
120 struct s_fixedpt
121 d_to_sfp(double d)
122 {
123 	struct s_fixedpt	sfp;
124 
125 	sfp.int_parts = htons((u_int16_t)d);
126 	sfp.fractions = htons((u_int16_t)((d - (u_int16_t)d) * USHRT_MAX));
127 
128 	return (sfp);
129 }
130 
131 char *
132 print_rtable(int r)
133 {
134 	static char b[11];
135 
136 	b[0] = 0;
137 	if (r > 0)
138 		snprintf(b, sizeof(b), "rtable %d", r);
139 
140 	return (b);
141 }
142 
143 const char *
144 log_sockaddr(struct sockaddr *sa)
145 {
146 	static char	buf[NI_MAXHOST];
147 
148 	if (getnameinfo(sa, SA_LEN(sa), buf, sizeof(buf), NULL, 0,
149 	    NI_NUMERICHOST))
150 		return ("(unknown)");
151 	else
152 		return (buf);
153 }
154