1: #include <config.h>
2: #include "utilities.h"
3: #include <assert.h>
4:
5: /* Display a NTP packet in hex with leading address offset
6: * e.g. offset: value, 0: ff 1: fe ... 255: 00
7: */
8: void
9: pkt_output (
10: struct pkt *dpkg,
11: int pkt_length,
12: FILE *output
13: )
14: {
15: register int a;
16: u_char *pkt;
17:
18: pkt = (u_char *)dpkg;
19:
20: fprintf(output, HLINE);
21:
22: for (a = 0; a < pkt_length; a++) {
23: if (a > 0 && a % 8 == 0)
24: fprintf(output, "\n");
25:
26: fprintf(output, "%d: %x \t", a, pkt[a]);
27: }
28:
29: fprintf(output, "\n");
30: fprintf(output, HLINE);
31: }
32:
33: /* Output a long floating point value in hex in the style described above
34: */
35: void
36: l_fp_output (
37: l_fp *ts,
38: FILE *output
39: )
40: {
41: register int a;
42:
43: fprintf(output, HLINE);
44:
45: for(a=0; a<8; a++)
46: fprintf(output, "%i: %x \t", a, ((unsigned char *) ts)[a]);
47:
48: fprintf(output, "\n");
49: fprintf(output, HLINE);
50:
51: }
52:
53: /* Output a long floating point value in binary in the style described above
54: */
55: void
56: l_fp_output_bin (
57: l_fp *ts,
58: FILE *output
59: )
60: {
61: register int a, b;
62:
63: fprintf(output, HLINE);
64:
65: for(a=0; a<8; a++) {
66: short tmp = ((unsigned char *) ts)[a];
67: tmp++;
68:
69: fprintf(output, "%i: ", a);
70:
71: for(b=7; b>=0; b--) {
72: int texp = (int) pow(2, b);
73:
74: if(tmp - texp > 0) {
75: fprintf(output, "1");
76: tmp -= texp;
77: }
78: else {
79: fprintf(output, "0");
80: }
81: }
82:
83: fprintf(output, " ");
84: }
85:
86: fprintf(output, "\n");
87: fprintf(output, HLINE);
88: }
89:
90: /* Output a long floating point value in decimal in the style described above
91: */
92: void
93: l_fp_output_dec (
94: l_fp *ts,
95: FILE *output
96: )
97: {
98: register int a;
99:
100: fprintf(output, HLINE);
101:
102: for(a=0; a<8; a++)
103: fprintf(output, "%i: %i \t", a, ((unsigned char *) ts)[a]);
104:
105: fprintf(output, "\n");
106: fprintf(output, HLINE);
107:
108: }
109:
110: /* Convert a struct addrinfo to a string containing the address in style
111: * of inet_ntoa
112: */
113: char *
114: addrinfo_to_str (
115: struct addrinfo *addr
116: )
117: {
118: sockaddr_u s;
119:
120: memset(&s, 0, sizeof(s));
121: memcpy(&s, addr->ai_addr, min(sizeof(s), addr->ai_addrlen));
122:
123: return ss_to_str(&s);
124: }
125:
126: /* Convert a sockaddr_u to a string containing the address in
127: * style of inet_ntoa
128: * Why not switch callers to use stoa from libntp? No free() needed
129: * in that case.
130: */
131: char *
132: ss_to_str (
133: sockaddr_u *saddr
134: )
135: {
136: char * buf;
137:
138: buf = emalloc(INET6_ADDRSTRLEN);
139: strncpy(buf, stoa(saddr), INET6_ADDRSTRLEN);
140:
141: return buf;
142: }
143: /*
144: * Converts a struct tv to a date string
145: */
146: char *
147: tv_to_str(
148: const struct timeval *tv
149: )
150: {
151: const size_t bufsize = 48;
152: char *buf;
153: time_t gmt_time, local_time;
154: struct tm *p_tm_local;
155: int hh, mm, lto;
156:
157: /*
158: * convert to struct tm in UTC, then intentionally feed
159: * that tm to mktime() which expects local time input, to
160: * derive the offset from UTC to local time.
161: */
162: gmt_time = tv->tv_sec;
163: local_time = mktime(gmtime(&gmt_time));
164: p_tm_local = localtime(&gmt_time);
165:
166: /* Local timezone offsets should never cause an overflow. Yeah. */
167: lto = difftime(local_time, gmt_time);
168: lto /= 60;
169: hh = lto / 60;
170: mm = abs(lto % 60);
171:
172: buf = emalloc(bufsize);
173: snprintf(buf, bufsize,
174: "%d-%.2d-%.2d %.2d:%.2d:%.2d.%.6d (%+03d%02d)",
175: p_tm_local->tm_year + 1900,
176: p_tm_local->tm_mon + 1,
177: p_tm_local->tm_mday,
178: p_tm_local->tm_hour,
179: p_tm_local->tm_min,
180: p_tm_local->tm_sec,
181: (int)tv->tv_usec,
182: hh,
183: mm);
184:
185: return buf;
186: }
187:
188:
189:
190:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>