1 /*-
2  * Copyright (c) 2010, 2011, 2012
3  *	Thorsten Glaser <tg@mirbsd.org>
4  *
5  * Provided that these terms and disclaimer and all copyright notices
6  * are retained or reproduced in an accompanying document, permission
7  * is granted to deal in this work without restriction, including un-
8  * limited rights to use, publicly perform, distribute, sell, modify,
9  * merge, give away, or sublicence.
10  *
11  * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
12  * the utmost extent permitted by applicable law, neither express nor
13  * implied; without malicious intent or gross negligence. In no event
14  * may a licensor, author or contributor be held liable for indirect,
15  * direct, other damage, loss, or other issues arising in any way out
16  * of dealing in the work, even if advised of the possibility of such
17  * damage or existence of a defect, except proven that it results out
18  * of said person's immediate fault when using the work as intended.
19  *-
20  * Helper functions shared between rdate(8) and ntpd(8) partially de-
21  * rived from OpenNTPD code by Henning Brauer.
22  */
23 
24 #include <sys/param.h>
25 #include <sys/time.h>
26 #include <sys/socket.h>
27 
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 
31 #include <err.h>
32 #include <netdb.h>
33 #include <stddef.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 #ifdef IN_RDATE
39 #include "rdate.h"
40 #else
41 #include "ntpd.h"
42 #endif
43 
44 __RCSID("$MirOS: src/usr.sbin/rdate/cutil.c,v 1.4 2012/04/02 17:23:36 tg Exp $");
45 
46 const char *
log_sockaddr(struct sockaddr * sa)47 log_sockaddr(struct sockaddr *sa)
48 {
49 	static char buf[NI_MAXHOST + 8];
50 	struct sockinet {
51 		uint8_t si_len;
52 		uint8_t si_family;
53 		uint8_t si_port[2];
54 	} *si = (struct sockinet *)sa;
55 	unsigned short s_port;
56 
57 #ifdef IN_NTPD
58 	/* traditional ntpd style */
59 #define gnibuf		buf
60 #define portfmtstr	"*%u"
61 #else
62 	/* classic output style */
63 #define gnibuf		(buf + 1)
64 #define portfmtstr	"]:%u"
65 	buf[0] = '[';
66 #endif
67 
68 	if (sa == NULL || sa == (void *)(ptrdiff_t)(sizeof(void *)))
69 		return ("(not set)");
70 
71 	if (getnameinfo(sa, SA_LEN(sa), gnibuf, NI_MAXHOST, NULL, 0,
72 	    NI_NUMERICHOST))
73 		return ("(unknown)");
74 
75 	memcpy(&s_port, &si->si_port, sizeof(unsigned short));
76 	s_port = ntohs(s_port);
77 	if (!s_port || s_port == 123)
78 		/* don't print port as it's default or empty */
79 		return (gnibuf);
80 
81 	/* append port and return whole string */
82 	snprintf(buf + strlen(buf), 8, portfmtstr, (unsigned int)s_port);
83 	return (buf);
84 }
85 
86 /* get the current POSIX time in NTP format */
87 double
gettime(void)88 gettime(void)
89 {
90 	register double d;
91 	struct timeval tv;
92 
93 	if (gettimeofday(&tv, NULL))
94 		err(1, "Could not get local time of day");
95 	d = tv.tv_usec;
96 	d /= 1000000;
97 	d += timet2posix(tv.tv_sec);
98 	/* 1970 - 1900 in POSIX seconds */
99 	d += 2208988800.0;
100 	return (d);
101 }
102