Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* |
| 2 | * rdbx.h |
| 3 | * |
| 4 | * replay database with extended packet indices, using a rollover counter |
| 5 | * |
| 6 | * David A. McGrew |
| 7 | * Cisco Systems, Inc. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #ifndef RDBX_H |
| 12 | #define RDBX_H |
| 13 | |
| 14 | #include "datatypes.h" |
| 15 | #include "err.h" |
| 16 | |
| 17 | /* #define ROC_TEST */ |
| 18 | |
| 19 | #ifndef ROC_TEST |
| 20 | |
| 21 | typedef uint16_t sequence_number_t; /* 16 bit sequence number */ |
| 22 | typedef uint32_t rollover_counter_t; /* 32 bit rollover counter */ |
| 23 | |
| 24 | #else /* use small seq_num and roc datatypes for testing purposes */ |
| 25 | |
| 26 | typedef unsigned char sequence_number_t; /* 8 bit sequence number */ |
| 27 | typedef uint16_t rollover_counter_t; /* 16 bit rollover counter */ |
| 28 | |
| 29 | #endif |
| 30 | |
| 31 | #define seq_num_median (1 << (8*sizeof(sequence_number_t) - 1)) |
| 32 | #define seq_num_max (1 << (8*sizeof(sequence_number_t))) |
| 33 | |
| 34 | /* |
| 35 | * An xtd_seq_num_t is a 64-bit unsigned integer used as an 'extended' |
| 36 | * sequence number. |
| 37 | */ |
| 38 | |
| 39 | typedef uint64_t xtd_seq_num_t; |
| 40 | |
| 41 | |
| 42 | /* |
| 43 | * An rdbx_t is a replay database with extended range; it uses an |
| 44 | * xtd_seq_num_t and a bitmask of recently received indices. |
| 45 | */ |
| 46 | |
| 47 | typedef struct { |
| 48 | xtd_seq_num_t index; |
| 49 | v128_t bitmask; |
| 50 | } rdbx_t; |
| 51 | |
| 52 | |
| 53 | /* |
| 54 | * rdbx_init(rdbx_ptr) |
| 55 | * |
| 56 | * initializes the rdbx pointed to by its argument, setting the |
| 57 | * rollover counter and sequence number to zero |
| 58 | */ |
| 59 | |
| 60 | err_status_t |
| 61 | rdbx_init(rdbx_t *rdbx); |
| 62 | |
| 63 | |
| 64 | /* |
| 65 | * rdbx_estimate_index(rdbx, guess, s) |
| 66 | * |
| 67 | * given an rdbx and a sequence number s (from a newly arrived packet), |
| 68 | * sets the contents of *guess to contain the best guess of the packet |
| 69 | * index to which s corresponds, and returns the difference between |
| 70 | * *guess and the locally stored synch info |
| 71 | */ |
| 72 | |
| 73 | int |
| 74 | rdbx_estimate_index(const rdbx_t *rdbx, |
| 75 | xtd_seq_num_t *guess, |
| 76 | sequence_number_t s); |
| 77 | |
| 78 | /* |
| 79 | * rdbx_check(rdbx, delta); |
| 80 | * |
| 81 | * rdbx_check(&r, delta) checks to see if the xtd_seq_num_t |
| 82 | * which is at rdbx->window_start + delta is in the rdb |
| 83 | * |
| 84 | */ |
| 85 | |
| 86 | err_status_t |
| 87 | rdbx_check(const rdbx_t *rdbx, int difference); |
| 88 | |
| 89 | /* |
| 90 | * replay_add_index(rdbx, delta) |
| 91 | * |
| 92 | * adds the xtd_seq_num_t at rdbx->window_start + delta to replay_db |
| 93 | * (and does *not* check if that xtd_seq_num_t appears in db) |
| 94 | * |
| 95 | * this function should be called *only* after replay_check has |
| 96 | * indicated that the index does not appear in the rdbx, and a mutex |
| 97 | * should protect the rdbx between these calls if necessary. |
| 98 | */ |
| 99 | |
| 100 | err_status_t |
| 101 | rdbx_add_index(rdbx_t *rdbx, int delta); |
| 102 | |
| 103 | /* |
| 104 | * xtd_seq_num_t functions - these are *internal* functions of rdbx, and |
| 105 | * shouldn't be used to manipulate rdbx internal values. use the rdbx |
| 106 | * api instead! |
| 107 | */ |
| 108 | |
| 109 | |
| 110 | /* index_init(&pi) initializes a packet index pi (sets it to zero) */ |
| 111 | |
| 112 | void |
| 113 | index_init(xtd_seq_num_t *pi); |
| 114 | |
| 115 | /* index_advance(&pi, s) advances a xtd_seq_num_t forward by s */ |
| 116 | |
| 117 | void |
| 118 | index_advance(xtd_seq_num_t *pi, sequence_number_t s); |
| 119 | |
| 120 | |
| 121 | /* |
| 122 | * index_guess(local, guess, s) |
| 123 | * |
| 124 | * given a xtd_seq_num_t local (which represents the highest |
| 125 | * known-to-be-good index) and a sequence number s (from a newly |
| 126 | * arrived packet), sets the contents of *guess to contain the best |
| 127 | * guess of the packet index to which s corresponds, and returns the |
| 128 | * difference between *guess and *local |
| 129 | */ |
| 130 | |
| 131 | int |
| 132 | index_guess(const xtd_seq_num_t *local, |
| 133 | xtd_seq_num_t *guess, |
| 134 | sequence_number_t s); |
| 135 | |
| 136 | |
| 137 | #endif /* RDBX_H */ |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |