5 /* Display a NTP packet in hex with leading address offset
6 * e.g. offset: value, 0: ff 1: fe ... 255: 00
20 fprintf(output, HLINE);
22 for (a = 0; a < pkt_length; a++) {
23 if (a > 0 && a % 8 == 0)
24 fprintf(output, "\n");
26 fprintf(output, "%3d: %02x ", a, pkt[a]);
29 fprintf(output, "\n");
30 fprintf(output, HLINE);
33 /* Output a long floating point value in hex in the style described above
41 fprintf(output, "%s\n", prettydate(ts));
44 /* Output a long floating point value in binary in the style described above
54 fprintf(output, HLINE);
57 short tmp = ((unsigned char *) ts)[a];
60 fprintf(output, "%i: ", a);
63 int texp = (int) pow(2, b);
77 fprintf(output, "\n");
78 fprintf(output, HLINE);
81 /* Output a long floating point value in decimal in the style described above
91 fprintf(output, HLINE);
94 fprintf(output, "%i: %i \t", a, ((unsigned char *) ts)[a]);
96 fprintf(output, "\n");
97 fprintf(output, HLINE);
101 /* Convert a struct addrinfo to a string containing the address in style
106 const struct addrinfo *addr
112 memcpy(&s, addr->ai_addr, min(sizeof(s), addr->ai_addrlen));
114 return ss_to_str(&s);
118 /* Convert a sockaddr_u to a string containing the address in
120 * Why not switch callers to use stoa from libntp? No free() needed
128 return estrdup(stoa(saddr));
133 * Converts a struct tv to a date string
137 const struct timeval *tv
140 const size_t bufsize = 48;
142 time_t gmt_time, local_time;
143 struct tm *p_tm_local;
147 * convert to struct tm in UTC, then intentionally feed
148 * that tm to mktime() which expects local time input, to
149 * derive the offset from UTC to local time.
151 gmt_time = tv->tv_sec;
152 local_time = mktime(gmtime(&gmt_time));
153 p_tm_local = localtime(&gmt_time);
155 /* Local timezone offsets should never cause an overflow. Yeah. */
156 lto = difftime(local_time, gmt_time);
161 buf = emalloc(bufsize);
162 snprintf(buf, bufsize,
163 "%d-%.2d-%.2d %.2d:%.2d:%.2d.%.6d (%+03d%02d)",
164 p_tm_local->tm_year + 1900,
165 p_tm_local->tm_mon + 1,
182 * Formats the hostname and resulting numeric IP address into a string,
183 * avoiding duplication if the "hostname" was in fact a numeric address.
188 const char * hostname,
189 const sockaddr_u * addr
192 const char * addrtxt;
196 addrtxt = stoa(addr);
198 if (strcmp(hostname, addrtxt))
199 cnt = snprintf(result, LIB_BUFLENGTH, "%s %s",
202 cnt = snprintf(result, LIB_BUFLENGTH, "%s", addrtxt);
203 if (cnt >= LIB_BUFLENGTH)
204 snprintf(result, LIB_BUFLENGTH,
205 "hostnameaddr ERROR have %d (%d needed)",
206 LIB_BUFLENGTH, cnt + 1);