lsquic_packet_common.c revision c7d81ce1
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc. See LICENSE. */ 2/* 3 * lsquic_packet_common.c -- some common packet-related routines 4 */ 5 6#include <stdio.h> 7#include <stdlib.h> 8 9#include "lsquic_logger.h" 10#include "lsquic_packet_common.h" 11 12 13const char * 14lsquic_frame_types_to_str (char *buf, size_t bufsz, 15 enum quic_ft_bit frame_types) 16{ 17 char *p; 18 int i, w; 19 size_t sz; 20 21 if (bufsz > 0) 22 buf[0] = '\0'; 23 24 p = buf; 25 for (i = 0; i < N_QUIC_FRAMES; ++i) 26 { 27 if (frame_types & (1 << i)) 28 { 29 sz = bufsz - (p - buf); 30 w = snprintf(p, sz, "%.*s%s", p > buf, " ", 31 frame_type_2_str[i] + sizeof("QUIC_FRAME_") - 1); 32 if (w > (int) sz) 33 { 34 LSQ_WARN("not enough room for all frame types"); 35 break; 36 } 37 p += w; 38 } 39 frame_types &= ~(1 << i); 40 } 41 42 return buf; 43} 44 45 46enum packno_bits 47calc_packno_bits (lsquic_packno_t packno, lsquic_packno_t least_unacked, 48 uint64_t n_in_flight) 49{ 50 uint64_t delta; 51 unsigned bits; 52 53 delta = packno - least_unacked; 54 if (n_in_flight > delta) 55 delta = n_in_flight; 56 57 delta *= 4; 58 bits = (delta > (1ULL << 8)) 59 + (delta > (1ULL << 16)) 60 + (delta > (1ULL << 32)); 61 62 return bits; 63} 64 65 66lsquic_packno_t 67restore_packno (lsquic_packno_t cur_packno, 68 unsigned len, 69 lsquic_packno_t max_packno) 70{ 71 lsquic_packno_t candidates[3], epoch_delta; 72 int64_t diffs[3]; 73 unsigned min; 74 75 epoch_delta = 1ULL << (len << 3); 76 candidates[1] = (max_packno & ~(epoch_delta - 1)) + cur_packno; 77 candidates[0] = candidates[1] - epoch_delta; 78 candidates[2] = candidates[1] + epoch_delta; 79 80 diffs[0] = llabs((int64_t) candidates[0] - (int64_t) max_packno); 81 diffs[1] = llabs((int64_t) candidates[1] - (int64_t) max_packno); 82 diffs[2] = llabs((int64_t) candidates[2] - (int64_t) max_packno); 83 84 min = diffs[1] < diffs[0]; 85 if (diffs[2] < diffs[min]) 86 min = 2; 87 88 return candidates[min]; 89} 90 91 92const char *const lsquic_hety2str[] = 93{ 94 [HETY_NOT_SET] = "Short", 95 [HETY_VERNEG] = "Version Negotiation", 96 [HETY_INITIAL] = "Initial", 97 [HETY_RETRY] = "Retry", 98 [HETY_HANDSHAKE] = "Handshake", 99 [HETY_0RTT] = "0-RTT", 100}; 101