1 /* $NetBSD: utilities.c,v 1.1.1.1 2009/12/13 16:57:12 kardel Exp $ */ 2 3 #include "utilities.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 char *buf = (char *) emalloc(sizeof(char) * INET6_ADDRSTRLEN); 119 120 getnameinfo(addr->ai_addr, addr->ai_addrlen, buf, 121 INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); 122 123 return buf; 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 = (char *) emalloc(sizeof(char) * INET6_ADDRSTRLEN); 137 138 getnameinfo(&saddr->sa, SOCKLEN(saddr), buf, 139 INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST); 140 141 142 return buf; 143 } 144 145 /* Converts a struct tv to a date string 146 */ 147 char * 148 tv_to_str ( 149 struct timeval *tv 150 ) 151 { 152 static const char *month_names[] = { 153 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 154 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 155 }; 156 157 char *buf = (char *) emalloc(sizeof(char) * 48); 158 time_t cur_time = time(NULL); 159 struct tm *tm_ptr; 160 161 tm_ptr = localtime(&cur_time); 162 163 164 snprintf(buf, 48, "%i %s %.2d %.2d:%.2d:%.2d.%.3d", 165 tm_ptr->tm_year + 1900, 166 month_names[tm_ptr->tm_mon], 167 tm_ptr->tm_mday, 168 tm_ptr->tm_hour, 169 tm_ptr->tm_min, 170 tm_ptr->tm_sec, 171 (int)tv->tv_usec); 172 173 return buf; 174 } 175 176 177 178 179