lsquic_util.c revision 5392f7a3
1229fce07SDmitri Tikhonov/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov/*
350aadb33SDmitri Tikhonov * Utility functions
450aadb33SDmitri Tikhonov */
550aadb33SDmitri Tikhonov
650aadb33SDmitri Tikhonov#include <ctype.h>
750aadb33SDmitri Tikhonov#include <stdint.h>
850aadb33SDmitri Tikhonov#include <stdio.h>
950aadb33SDmitri Tikhonov#include <string.h>
1083287402SDmitri Tikhonov#include <time.h>
11461e84d8SAmol Deshpande#ifndef WIN32
12461e84d8SAmol Deshpande#include <sys/time.h>
1383287402SDmitri Tikhonov#include <unistd.h>
14461e84d8SAmol Deshpande#else
15461e84d8SAmol Deshpande#include <vc_compat.h>
16461e84d8SAmol Deshpande#endif
1783287402SDmitri Tikhonov
1883287402SDmitri Tikhonov#if !(defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0) && defined(__APPLE__)
1983287402SDmitri Tikhonov#include <mach/mach_time.h>
2083287402SDmitri Tikhonov#endif
2150aadb33SDmitri Tikhonov
2250aadb33SDmitri Tikhonov#include "lsquic_int_types.h"
2350aadb33SDmitri Tikhonov#include "lsquic_util.h"
245392f7a3SLiteSpeed Tech#if LSQUIC_COUNT_TIME_CALLS
255392f7a3SLiteSpeed Tech#include <stdlib.h>
265392f7a3SLiteSpeed Tech#include "lsquic_types.h"
275392f7a3SLiteSpeed Tech#include "lsquic_logger.h"
285392f7a3SLiteSpeed Tech#endif
2950aadb33SDmitri Tikhonov
3050aadb33SDmitri Tikhonov
3183287402SDmitri Tikhonov#if defined(__APPLE__)
3283287402SDmitri Tikhonovstatic mach_timebase_info_data_t timebase;
3383287402SDmitri Tikhonov#endif
34461e84d8SAmol Deshpande#if defined(WIN32)
35461e84d8SAmol Deshpandestatic LARGE_INTEGER perf_frequency;
36461e84d8SAmol Deshpande#endif
3783287402SDmitri Tikhonov
385392f7a3SLiteSpeed Tech
395392f7a3SLiteSpeed Tech#if LSQUIC_COUNT_TIME_CALLS
405392f7a3SLiteSpeed Techstatic volatile unsigned long n_time_now_calls;
415392f7a3SLiteSpeed Tech
425392f7a3SLiteSpeed Tech
435392f7a3SLiteSpeed Techstatic void
445392f7a3SLiteSpeed Techprint_call_stats (void)
455392f7a3SLiteSpeed Tech{
465392f7a3SLiteSpeed Tech    LSQ_NOTICE("number of lsquic_time_now() calls: %lu", n_time_now_calls);
475392f7a3SLiteSpeed Tech}
485392f7a3SLiteSpeed Tech#endif
495392f7a3SLiteSpeed Tech
505392f7a3SLiteSpeed Tech
5183287402SDmitri Tikhonovvoid
5283287402SDmitri Tikhonovlsquic_init_timers (void)
5383287402SDmitri Tikhonov{
545392f7a3SLiteSpeed Tech#if LSQUIC_COUNT_TIME_CALLS
555392f7a3SLiteSpeed Tech    atexit(print_call_stats);
565392f7a3SLiteSpeed Tech#endif
5783287402SDmitri Tikhonov#if defined(__APPLE__)
5883287402SDmitri Tikhonov    mach_timebase_info(&timebase);
5983287402SDmitri Tikhonov#endif
60461e84d8SAmol Deshpande#if defined(WIN32)
61461e84d8SAmol Deshpande    QueryPerformanceFrequency(&perf_frequency);
62461e84d8SAmol Deshpande#endif
6383287402SDmitri Tikhonov}
6483287402SDmitri Tikhonov
6583287402SDmitri Tikhonov
6650aadb33SDmitri Tikhonovlsquic_time_t
6750aadb33SDmitri Tikhonovlsquic_time_now (void)
6850aadb33SDmitri Tikhonov{
695392f7a3SLiteSpeed Tech#if LSQUIC_COUNT_TIME_CALLS
705392f7a3SLiteSpeed Tech    ++n_time_now_calls;
715392f7a3SLiteSpeed Tech#endif
7283287402SDmitri Tikhonov#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
7383287402SDmitri Tikhonov    struct timespec ts;
7483287402SDmitri Tikhonov    (void) clock_gettime(CLOCK_MONOTONIC, &ts);
7583287402SDmitri Tikhonov    return (lsquic_time_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
7683287402SDmitri Tikhonov#elif defined(__APPLE__)
7783287402SDmitri Tikhonov    lsquic_time_t t = mach_absolute_time();
7883287402SDmitri Tikhonov    t *= timebase.numer;
7983287402SDmitri Tikhonov    t /= timebase.denom;
8083287402SDmitri Tikhonov    t /= 1000;
8183287402SDmitri Tikhonov    return t;
82461e84d8SAmol Deshpande#elif defined(WIN32)
83461e84d8SAmol Deshpande    LARGE_INTEGER counter;
84461e84d8SAmol Deshpande    lsquic_time_t t;
85461e84d8SAmol Deshpande    QueryPerformanceCounter(&counter);
86461e84d8SAmol Deshpande    t = counter.QuadPart;
87461e84d8SAmol Deshpande    t *= 1000000;
88461e84d8SAmol Deshpande    t /= perf_frequency.QuadPart;
89461e84d8SAmol Deshpande    return t;
9083287402SDmitri Tikhonov#else
9183287402SDmitri Tikhonov#   warn Monotonically increasing clock is not available on this platform
9250aadb33SDmitri Tikhonov    struct timeval tv;
9350aadb33SDmitri Tikhonov    (void) gettimeofday(&tv, NULL);
9450aadb33SDmitri Tikhonov    return (lsquic_time_t) tv.tv_sec * 1000000 + tv.tv_usec;
9583287402SDmitri Tikhonov#endif
9650aadb33SDmitri Tikhonov}
9750aadb33SDmitri Tikhonov
9850aadb33SDmitri Tikhonov
9950aadb33SDmitri Tikhonovint
10050aadb33SDmitri Tikhonovlsquic_is_zero (const void *pbuf, size_t bufsz)
10150aadb33SDmitri Tikhonov{
10250aadb33SDmitri Tikhonov    const unsigned char *buf, *end;
10350aadb33SDmitri Tikhonov    const unsigned long *buf_ul;
10450aadb33SDmitri Tikhonov    unsigned n_ul;
10550aadb33SDmitri Tikhonov    unsigned long n_non_zero;
10650aadb33SDmitri Tikhonov
10750aadb33SDmitri Tikhonov    buf = pbuf;
10850aadb33SDmitri Tikhonov    end = buf + bufsz;
10950aadb33SDmitri Tikhonov    buf_ul = (unsigned long *) buf;
11050aadb33SDmitri Tikhonov    n_ul = bufsz / sizeof(buf_ul[0]);
11150aadb33SDmitri Tikhonov    buf += n_ul * sizeof(buf_ul[0]);
11250aadb33SDmitri Tikhonov    n_non_zero = 0;
11350aadb33SDmitri Tikhonov
11450aadb33SDmitri Tikhonov    while (n_ul--)
11550aadb33SDmitri Tikhonov        n_non_zero |= buf_ul[n_ul];
11650aadb33SDmitri Tikhonov
11750aadb33SDmitri Tikhonov    while (buf < end)
11850aadb33SDmitri Tikhonov        n_non_zero |= *buf++;
11950aadb33SDmitri Tikhonov
12050aadb33SDmitri Tikhonov    return n_non_zero == 0;
12150aadb33SDmitri Tikhonov}
12250aadb33SDmitri Tikhonov
12350aadb33SDmitri Tikhonov
1245392f7a3SLiteSpeed Tech/* XXX this function uses static buffer.  Replace it with lsquic_hexdump() if possible */
12550aadb33SDmitri Tikhonovchar *get_bin_str(const void *s, size_t len, size_t max_display_len)
12650aadb33SDmitri Tikhonov{
12750aadb33SDmitri Tikhonov    const unsigned char *p, *pEnd;
12850aadb33SDmitri Tikhonov    char *pOutput;
12950aadb33SDmitri Tikhonov    size_t lenOrg = len;
13050aadb33SDmitri Tikhonov    static char str[512 * 2 + 40] = {0};
13150aadb33SDmitri Tikhonov
13250aadb33SDmitri Tikhonov    /**
13350aadb33SDmitri Tikhonov     * We alloc fixed size buffer, at most max_display_len is 512
13450aadb33SDmitri Tikhonov     */
13550aadb33SDmitri Tikhonov    size_t fit_display_len = (max_display_len > 512 ? 512 : max_display_len);
13650aadb33SDmitri Tikhonov    if (len > fit_display_len)
13750aadb33SDmitri Tikhonov        len = fit_display_len;
13850aadb33SDmitri Tikhonov
13950aadb33SDmitri Tikhonov    pOutput = &str[0] + sprintf(str, "(%zd/%zd)=0x", len, lenOrg);
14050aadb33SDmitri Tikhonov
141461e84d8SAmol Deshpande    for(p = s, pEnd = (unsigned char*)s + len; p < pEnd; ++p)
14250aadb33SDmitri Tikhonov    {
14350aadb33SDmitri Tikhonov        sprintf(pOutput, "%02X", *p);
14450aadb33SDmitri Tikhonov        pOutput += 2;
14550aadb33SDmitri Tikhonov    }
14650aadb33SDmitri Tikhonov    if (lenOrg > len)
14750aadb33SDmitri Tikhonov    {
14850aadb33SDmitri Tikhonov        sprintf(pOutput, "...");
14950aadb33SDmitri Tikhonov        pOutput += 3;
15050aadb33SDmitri Tikhonov    }
15150aadb33SDmitri Tikhonov    return str;
15250aadb33SDmitri Tikhonov}
15350aadb33SDmitri Tikhonov
15450aadb33SDmitri Tikhonov
15555cd0b38SDmitri Tikhonovstatic char
15655cd0b38SDmitri Tikhonovhex_digit(uint8_t n)
15755cd0b38SDmitri Tikhonov{
15855cd0b38SDmitri Tikhonov    return (n < 10) ? (n + '0') : ((n - 10) + 'a');
15955cd0b38SDmitri Tikhonov}
16055cd0b38SDmitri Tikhonov
16155cd0b38SDmitri Tikhonov
16255cd0b38SDmitri Tikhonovsize_t
16355cd0b38SDmitri Tikhonovlsquic_hex_encode (const void *src, size_t src_sz, void *dst, size_t dst_sz)
16455cd0b38SDmitri Tikhonov{
16555cd0b38SDmitri Tikhonov    size_t src_cur, dst_cur;
16655cd0b38SDmitri Tikhonov    const uint8_t *src_hex;
16755cd0b38SDmitri Tikhonov    char *dst_char;
16855cd0b38SDmitri Tikhonov
16955cd0b38SDmitri Tikhonov    src_hex = (const uint8_t *)src;
17055cd0b38SDmitri Tikhonov    dst_char = (char *)dst;
17155cd0b38SDmitri Tikhonov    src_cur = dst_cur = 0;
17255cd0b38SDmitri Tikhonov
17355cd0b38SDmitri Tikhonov    while (src_cur < src_sz && dst_cur < (dst_sz - 2))
17455cd0b38SDmitri Tikhonov    {
17555cd0b38SDmitri Tikhonov        dst_char[dst_cur++] = hex_digit((src_hex[src_cur] & 0xf0) >> 4);
17655cd0b38SDmitri Tikhonov        dst_char[dst_cur++] = hex_digit(src_hex[src_cur++] & 0x0f);
17755cd0b38SDmitri Tikhonov    }
17855cd0b38SDmitri Tikhonov    dst_char[dst_cur++] = '\0';
17955cd0b38SDmitri Tikhonov    return dst_cur;
18055cd0b38SDmitri Tikhonov}
18155cd0b38SDmitri Tikhonov
18255cd0b38SDmitri Tikhonov
1835392f7a3SLiteSpeed Techvoid
1845392f7a3SLiteSpeed Techlsquic_hexstr (const unsigned char *buf, size_t bufsz, char *out, size_t outsz)
1855392f7a3SLiteSpeed Tech{
1865392f7a3SLiteSpeed Tech    static const char b2c[16] = "0123456789ABCDEF";
1875392f7a3SLiteSpeed Tech    const unsigned char *const end_input = buf + bufsz;
1885392f7a3SLiteSpeed Tech    char *const end_output = out + outsz;
1895392f7a3SLiteSpeed Tech
1905392f7a3SLiteSpeed Tech    while (buf < end_input && out + 2 < end_output)
1915392f7a3SLiteSpeed Tech    {
1925392f7a3SLiteSpeed Tech        *out++ = b2c[ *buf >> 4 ];
1935392f7a3SLiteSpeed Tech        *out++ = b2c[ *buf & 0xF ];
1945392f7a3SLiteSpeed Tech        ++buf;
1955392f7a3SLiteSpeed Tech    }
1965392f7a3SLiteSpeed Tech
1975392f7a3SLiteSpeed Tech    if (buf < end_input)
1985392f7a3SLiteSpeed Tech        out[-1] = '!';
1995392f7a3SLiteSpeed Tech
2005392f7a3SLiteSpeed Tech    *out = '\0';
2015392f7a3SLiteSpeed Tech}
2025392f7a3SLiteSpeed Tech
2035392f7a3SLiteSpeed Tech
20450aadb33SDmitri Tikhonovsize_t
2055392f7a3SLiteSpeed Techlsquic_hexdump (const void *src_void, size_t src_sz, char *out, size_t out_sz)
20650aadb33SDmitri Tikhonov{
20750aadb33SDmitri Tikhonov/* Ruler:
20850aadb33SDmitri Tikhonov *
20950aadb33SDmitri Tikhonov      6                       31                        57              73
21050aadb33SDmitri Tikhonov      |                        |                         |               |
21150aadb33SDmitri Tikhonov0000  00 01 02 03 04 05 06 07  08 09 0A 0B 0C 0D 0E 0F  |................|
21250aadb33SDmitri Tikhonov *
21350aadb33SDmitri Tikhonov */
21450aadb33SDmitri Tikhonov#define LINE_SIZE (74 + 1 /* newline */)
21550aadb33SDmitri Tikhonov    const unsigned char       *src     = src_void;
21650aadb33SDmitri Tikhonov    const unsigned char *const src_end = src + src_sz;
21750aadb33SDmitri Tikhonov    char *const                out_end = out + out_sz;
21850aadb33SDmitri Tikhonov    unsigned line = 0;
21950aadb33SDmitri Tikhonov
22050aadb33SDmitri Tikhonov    while (src < src_end && out_end - out >= LINE_SIZE)
22150aadb33SDmitri Tikhonov    {
22250aadb33SDmitri Tikhonov        const unsigned char *limit = src + 16;
22350aadb33SDmitri Tikhonov        if (limit > src_end)
22450aadb33SDmitri Tikhonov            limit = src_end;
22550aadb33SDmitri Tikhonov        unsigned hex_off   = 6;
22650aadb33SDmitri Tikhonov        unsigned alpha_off = 57;
2275392f7a3SLiteSpeed Tech        sprintf(out, "%03X0", line++);
22850aadb33SDmitri Tikhonov        out[4] = ' ';
22950aadb33SDmitri Tikhonov        out[5] = ' ';
23050aadb33SDmitri Tikhonov        while (src < limit)
23150aadb33SDmitri Tikhonov        {
23250aadb33SDmitri Tikhonov            sprintf(out + hex_off, "%02X ", *src);
23350aadb33SDmitri Tikhonov            sprintf(out + alpha_off, "%c", isprint(*src) ? *src : '.');
23450aadb33SDmitri Tikhonov            hex_off += 3;
23550aadb33SDmitri Tikhonov            out[hex_off] = ' ';
23650aadb33SDmitri Tikhonov            hex_off += 30 == hex_off;
23750aadb33SDmitri Tikhonov            out[hex_off] = ' ';
23850aadb33SDmitri Tikhonov            ++alpha_off;
23950aadb33SDmitri Tikhonov            out[alpha_off] = ' ';
24050aadb33SDmitri Tikhonov            ++src;
24150aadb33SDmitri Tikhonov        }
24250aadb33SDmitri Tikhonov        memset(out + hex_off,   ' ', 56 - hex_off);
24350aadb33SDmitri Tikhonov        memset(out + alpha_off, '.', 73 - alpha_off);
24450aadb33SDmitri Tikhonov        out[56] = '|';
24550aadb33SDmitri Tikhonov        out[73] = '|';
24650aadb33SDmitri Tikhonov        out[74] = '\n';
24750aadb33SDmitri Tikhonov        out += LINE_SIZE;
24850aadb33SDmitri Tikhonov    }
24950aadb33SDmitri Tikhonov
25050aadb33SDmitri Tikhonov    if (out < out_end)
25150aadb33SDmitri Tikhonov        *out = '\0';
25250aadb33SDmitri Tikhonov    else
25350aadb33SDmitri Tikhonov        out_end[-1] = '\0';
25450aadb33SDmitri Tikhonov
25550aadb33SDmitri Tikhonov    return out + out_sz - out_end;
25650aadb33SDmitri Tikhonov}
257