1#include "utilities.h"
2
3/* Display a NTP packet in hex with leading address offset
4 * e.g. offset: value, 0: ff 1: fe ... 255: 00
5 */
6void
7pkt_output (
8		struct pkt *dpkg,
9		int pkt_length,
10		FILE *output
11	   )
12{
13	register int a;
14	u_char *pkt;
15
16	pkt = (u_char *)dpkg;
17
18	fprintf(output, HLINE);
19
20	for (a = 0; a < pkt_length; a++) {
21		if (a > 0 && a % 8 == 0)
22			fprintf(output, "\n");
23
24		fprintf(output, "%d: %x \t", a, pkt[a]);
25	}
26
27	fprintf(output, "\n");
28	fprintf(output, HLINE);
29}
30
31/* Output a long floating point value in hex in the style described above
32 */
33void
34l_fp_output (
35		l_fp *ts,
36		FILE *output
37	    )
38{
39	register int a;
40
41	fprintf(output, HLINE);
42
43	for(a=0; a<8; a++)
44		fprintf(output, "%i: %x \t", a, ((unsigned char *) ts)[a]);
45
46	fprintf(output, "\n");
47	fprintf(output, HLINE);
48
49}
50
51/* Output a long floating point value in binary in the style described above
52 */
53void
54l_fp_output_bin (
55		l_fp *ts,
56		FILE *output
57		)
58{
59	register int a, b;
60
61	fprintf(output, HLINE);
62
63	for(a=0; a<8; a++) {
64		short tmp = ((unsigned char *) ts)[a];
65		tmp++;
66
67		fprintf(output, "%i: ", a);
68
69		for(b=7; b>=0; b--) {
70			int texp = (int) pow(2, b);
71
72			if(tmp - texp > 0) {
73				fprintf(output, "1");
74				tmp -= texp;
75			}
76			else {
77				fprintf(output, "0");
78			}
79		}
80
81		fprintf(output, " ");
82	}
83
84	fprintf(output, "\n");
85	fprintf(output, HLINE);
86}
87
88/* Output a long floating point value in decimal in the style described above
89 */
90void
91l_fp_output_dec (
92		l_fp *ts,
93		FILE *output
94	    )
95{
96	register int a;
97
98	fprintf(output, HLINE);
99
100	for(a=0; a<8; a++)
101		fprintf(output, "%i: %i \t", a, ((unsigned char *) ts)[a]);
102
103	fprintf(output, "\n");
104	fprintf(output, HLINE);
105
106}
107
108/* Convert a struct addrinfo to a string containing the address in style
109 * of inet_ntoa
110 */
111char *
112addrinfo_to_str (
113		struct addrinfo *addr
114		)
115{
116	char *buf = (char *) emalloc(sizeof(char) * INET6_ADDRSTRLEN);
117
118	getnameinfo(addr->ai_addr, addr->ai_addrlen, buf,
119			INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
120
121	return buf;
122}
123
124/* Convert a sockaddr_u to a string containing the address in
125 * style of inet_ntoa
126 * Why not switch callers to use stoa from libntp?  No free() needed
127 * in that case.
128 */
129char *
130ss_to_str (
131		sockaddr_u *saddr
132		)
133{
134	char *buf = (char *) emalloc(sizeof(char) * INET6_ADDRSTRLEN);
135
136	getnameinfo(&saddr->sa, SOCKLEN(saddr), buf,
137			INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
138
139
140	return buf;
141}
142
143/* Converts a struct tv to a date string
144 */
145char *
146tv_to_str (
147		struct timeval *tv
148	  )
149{
150	static const char *month_names[] = {
151		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
152		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
153	};
154
155	char *buf = (char *) emalloc(sizeof(char) * 48);
156	time_t cur_time = time(NULL);
157	struct tm *tm_ptr;
158
159	tm_ptr = localtime(&cur_time);
160
161
162	snprintf(buf, 48, "%i %s %.2d %.2d:%.2d:%.2d.%.3d",
163			tm_ptr->tm_year + 1900,
164			month_names[tm_ptr->tm_mon],
165			tm_ptr->tm_mday,
166			tm_ptr->tm_hour,
167			tm_ptr->tm_min,
168			tm_ptr->tm_sec,
169			(int)tv->tv_usec);
170
171	return buf;
172}
173
174
175
176
177