lsquic_util.c revision e68b0452
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc. See LICENSE. */ 2/* 3 * Utility functions 4 */ 5 6#include <ctype.h> 7#include <stdint.h> 8#include <stdio.h> 9#include <string.h> 10#include <time.h> 11#ifndef WIN32 12#include <sys/time.h> 13#include <unistd.h> 14#else 15#include <vc_compat.h> 16#endif 17#include <netinet/in.h> 18#include <arpa/inet.h> 19#include <sys/socket.h> 20 21#if !(defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0) && defined(__APPLE__) 22#include <mach/mach_time.h> 23#endif 24 25#include "lsquic_int_types.h" 26#include "lsquic_util.h" 27#if LSQUIC_COUNT_TIME_CALLS 28#include <stdlib.h> 29#include "lsquic_types.h" 30#include "lsquic_logger.h" 31#endif 32 33 34#if defined(__APPLE__) 35static mach_timebase_info_data_t timebase; 36#endif 37#if defined(WIN32) 38static LARGE_INTEGER perf_frequency; 39#endif 40 41 42#if LSQUIC_COUNT_TIME_CALLS 43static volatile unsigned long n_time_now_calls; 44 45 46static void 47print_call_stats (void) 48{ 49 LSQ_NOTICE("number of lsquic_time_now() calls: %lu", n_time_now_calls); 50} 51#endif 52 53 54void 55lsquic_init_timers (void) 56{ 57#if LSQUIC_COUNT_TIME_CALLS 58 atexit(print_call_stats); 59#endif 60#if defined(__APPLE__) 61 mach_timebase_info(&timebase); 62#endif 63#if defined(WIN32) 64 QueryPerformanceFrequency(&perf_frequency); 65#endif 66} 67 68 69lsquic_time_t 70lsquic_time_now (void) 71{ 72#if LSQUIC_COUNT_TIME_CALLS 73 ++n_time_now_calls; 74#endif 75#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 76 struct timespec ts; 77 (void) clock_gettime(CLOCK_MONOTONIC, &ts); 78 return (lsquic_time_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000; 79#elif defined(__APPLE__) 80 lsquic_time_t t = mach_absolute_time(); 81 t *= timebase.numer; 82 t /= timebase.denom; 83 t /= 1000; 84 return t; 85#elif defined(WIN32) 86 LARGE_INTEGER counter; 87 lsquic_time_t t; 88 QueryPerformanceCounter(&counter); 89 t = counter.QuadPart; 90 t *= 1000000; 91 t /= perf_frequency.QuadPart; 92 return t; 93#else 94# warn Monotonically increasing clock is not available on this platform 95 struct timeval tv; 96 (void) gettimeofday(&tv, NULL); 97 return (lsquic_time_t) tv.tv_sec * 1000000 + tv.tv_usec; 98#endif 99} 100 101 102int 103lsquic_is_zero (const void *pbuf, size_t bufsz) 104{ 105 const unsigned char *buf, *end; 106 const unsigned long *buf_ul; 107 unsigned n_ul; 108 unsigned long n_non_zero; 109 110 buf = pbuf; 111 end = buf + bufsz; 112 buf_ul = (unsigned long *) buf; 113 n_ul = bufsz / sizeof(buf_ul[0]); 114 buf += n_ul * sizeof(buf_ul[0]); 115 n_non_zero = 0; 116 117 while (n_ul--) 118 n_non_zero |= buf_ul[n_ul]; 119 120 while (buf < end) 121 n_non_zero |= *buf++; 122 123 return n_non_zero == 0; 124} 125 126 127/* XXX this function uses static buffer. Replace it with lsquic_hexdump() if possible */ 128char *get_bin_str(const void *s, size_t len, size_t max_display_len) 129{ 130 const unsigned char *p, *pEnd; 131 char *pOutput; 132 size_t lenOrg = len; 133 static char str[512 * 2 + 40] = {0}; 134 135 /** 136 * We alloc fixed size buffer, at most max_display_len is 512 137 */ 138 size_t fit_display_len = (max_display_len > 512 ? 512 : max_display_len); 139 if (len > fit_display_len) 140 len = fit_display_len; 141 142 pOutput = &str[0] + sprintf(str, "(%zd/%zd)=0x", len, lenOrg); 143 144 for(p = s, pEnd = (unsigned char*)s + len; p < pEnd; ++p) 145 { 146 sprintf(pOutput, "%02X", *p); 147 pOutput += 2; 148 } 149 if (lenOrg > len) 150 { 151 sprintf(pOutput, "..."); 152 pOutput += 3; 153 } 154 return str; 155} 156 157 158static char 159hex_digit(uint8_t n) 160{ 161 return (n < 10) ? (n + '0') : ((n - 10) + 'a'); 162} 163 164 165size_t 166lsquic_hex_encode (const void *src, size_t src_sz, void *dst, size_t dst_sz) 167{ 168 size_t src_cur, dst_cur; 169 const uint8_t *src_hex; 170 char *dst_char; 171 172 src_hex = (const uint8_t *)src; 173 dst_char = (char *)dst; 174 src_cur = dst_cur = 0; 175 176 while (src_cur < src_sz && dst_cur < (dst_sz - 2)) 177 { 178 dst_char[dst_cur++] = hex_digit((src_hex[src_cur] & 0xf0) >> 4); 179 dst_char[dst_cur++] = hex_digit(src_hex[src_cur++] & 0x0f); 180 } 181 dst_char[dst_cur++] = '\0'; 182 return dst_cur; 183} 184 185 186void 187lsquic_hexstr (const unsigned char *buf, size_t bufsz, char *out, size_t outsz) 188{ 189 static const char b2c[16] = "0123456789ABCDEF"; 190 const unsigned char *const end_input = buf + bufsz; 191 char *const end_output = out + outsz; 192 193 while (buf < end_input && out + 2 < end_output) 194 { 195 *out++ = b2c[ *buf >> 4 ]; 196 *out++ = b2c[ *buf & 0xF ]; 197 ++buf; 198 } 199 200 if (buf < end_input) 201 out[-1] = '!'; 202 203 *out = '\0'; 204} 205 206 207size_t 208lsquic_hexdump (const void *src_void, size_t src_sz, char *out, size_t out_sz) 209{ 210/* Ruler: 211 * 212 6 31 57 73 213 | | | | 2140000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F |................| 215 * 216 */ 217#define LINE_SIZE (74 + 1 /* newline */) 218 const unsigned char *src = src_void; 219 const unsigned char *const src_end = src + src_sz; 220 char *const out_end = out + out_sz; 221 unsigned line = 0; 222 223 while (src < src_end && out_end - out >= LINE_SIZE) 224 { 225 const unsigned char *limit = src + 16; 226 if (limit > src_end) 227 limit = src_end; 228 unsigned hex_off = 6; 229 unsigned alpha_off = 57; 230 sprintf(out, "%03X0", line++); 231 out[4] = ' '; 232 out[5] = ' '; 233 while (src < limit) 234 { 235 sprintf(out + hex_off, "%02X ", *src); 236 sprintf(out + alpha_off, "%c", isprint(*src) ? *src : '.'); 237 hex_off += 3; 238 out[hex_off] = ' '; 239 hex_off += 30 == hex_off; 240 out[hex_off] = ' '; 241 ++alpha_off; 242 out[alpha_off] = ' '; 243 ++src; 244 } 245 memset(out + hex_off, ' ', 56 - hex_off); 246 memset(out + alpha_off, '.', 73 - alpha_off); 247 out[56] = '|'; 248 out[73] = '|'; 249 out[74] = '\n'; 250 out += LINE_SIZE; 251 } 252 253 if (out < out_end) 254 *out = '\0'; 255 else 256 out_end[-1] = '\0'; 257 258 return out + out_sz - out_end; 259} 260 261 262/* Returns true if socket addresses are equal, false otherwise. Only 263 * families, IP addresses, and ports are compared. 264 */ 265int 266lsquic_sockaddr_eq (const struct sockaddr *a, const struct sockaddr *b) 267{ 268 if (a->sa_family == AF_INET) 269 return a->sa_family == b->sa_family 270 && ((struct sockaddr_in *) a)->sin_addr.s_addr 271 == ((struct sockaddr_in *) b)->sin_addr.s_addr 272 && ((struct sockaddr_in *) a)->sin_port 273 == ((struct sockaddr_in *) b)->sin_port; 274 else 275 return a->sa_family == b->sa_family 276 && ((struct sockaddr_in6 *) a)->sin6_port == 277 ((struct sockaddr_in6 *) b)->sin6_port 278 && 0 == memcmp(&((struct sockaddr_in6 *) a)->sin6_addr, 279 &((struct sockaddr_in6 *) b)->sin6_addr, 280 sizeof(((struct sockaddr_in6 *) b)->sin6_addr)); 281} 282 283 284void 285lsquic_sockaddr2str (const struct sockaddr *addr, char *buf, size_t sz) 286{ 287 unsigned short port; 288 int len; 289 290 switch (addr->sa_family) 291 { 292 case AF_INET: 293 port = ntohs(((struct sockaddr_in *) addr)->sin_port); 294 if (!inet_ntop(AF_INET, &((struct sockaddr_in *) addr)->sin_addr, 295 buf, sz)) 296 buf[0] = '\0'; 297 break; 298 case AF_INET6: 299 port = ntohs(((struct sockaddr_in6 *) addr)->sin6_port); 300 if (!inet_ntop(AF_INET6, &((struct sockaddr_in6 *) addr)->sin6_addr, 301 buf, sz)) 302 buf[0] = '\0'; 303 break; 304 default: 305 port = 0; 306 (void) snprintf(buf, sz, "<invalid family %d>", addr->sa_family); 307 break; 308 } 309 310 len = strlen(buf); 311 if (len < (int) sz) 312 snprintf(buf + len, sz - (size_t) len, ":%hu", port); 313} 314