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   bitvector_t bitmask;
50 } rdbx_t;
51 
52 
53 /*
54  * rdbx_init(rdbx_ptr, ws)
55  *
56  * initializes the rdbx pointed to by its argument with the window size ws,
57  * setting the rollover counter and sequence number to zero
58  */
59 
60 err_status_t
61 rdbx_init(rdbx_t *rdbx, unsigned long ws);
62 
63 
64 /*
65  * rdbx_uninit(rdbx_ptr)
66  *
67  * frees memory associated with the rdbx
68  */
69 
70 err_status_t
71 rdbx_uninit(rdbx_t *rdbx);
72 
73 
74 /*
75  * rdbx_estimate_index(rdbx, guess, s)
76  *
77  * given an rdbx and a sequence number s (from a newly arrived packet),
78  * sets the contents of *guess to contain the best guess of the packet
79  * index to which s corresponds, and returns the difference between
80  * *guess and the locally stored synch info
81  */
82 
83 int
84 rdbx_estimate_index(const rdbx_t *rdbx,
85 		    xtd_seq_num_t *guess,
86 		    sequence_number_t s);
87 
88 /*
89  * rdbx_check(rdbx, delta);
90  *
91  * rdbx_check(&r, delta) checks to see if the xtd_seq_num_t
92  * which is at rdbx->window_start + delta is in the rdb
93  *
94  */
95 
96 err_status_t
97 rdbx_check(const rdbx_t *rdbx, int difference);
98 
99 /*
100  * replay_add_index(rdbx, delta)
101  *
102  * adds the xtd_seq_num_t at rdbx->window_start + delta to replay_db
103  * (and does *not* check if that xtd_seq_num_t appears in db)
104  *
105  * this function should be called *only* after replay_check has
106  * indicated that the index does not appear in the rdbx, and a mutex
107  * should protect the rdbx between these calls if necessary.
108  */
109 
110 err_status_t
111 rdbx_add_index(rdbx_t *rdbx, int delta);
112 
113 
114 /*
115  * rdbx_set_roc(rdbx, roc) initalizes the rdbx_t at the location rdbx
116  * to have the rollover counter value roc.  If that value is less than
117  * the current rollover counter value, then the function returns
118  * err_status_replay_old; otherwise, err_status_ok is returned.
119  *
120  */
121 
122 err_status_t
123 rdbx_set_roc(rdbx_t *rdbx, uint32_t roc);
124 
125 /*
126  * rdbx_get_roc(rdbx) returns the value of the rollover counter for
127  * the rdbx_t pointed to by rdbx
128  *
129  */
130 
131 xtd_seq_num_t
132 rdbx_get_packet_index(const rdbx_t *rdbx);
133 
134 /*
135  * xtd_seq_num_t functions - these are *internal* functions of rdbx, and
136  * shouldn't be used to manipulate rdbx internal values.  use the rdbx
137  * api instead!
138  */
139 
140 /*
141  * rdbx_get_ws(rdbx_ptr)
142  *
143  * gets the window size which was used to initialize the rdbx
144  */
145 
146 unsigned long
147 rdbx_get_window_size(const rdbx_t *rdbx);
148 
149 
150 /* index_init(&pi) initializes a packet index pi (sets it to zero) */
151 
152 void
153 index_init(xtd_seq_num_t *pi);
154 
155 /* index_advance(&pi, s) advances a xtd_seq_num_t forward by s */
156 
157 void
158 index_advance(xtd_seq_num_t *pi, sequence_number_t s);
159 
160 
161 /*
162  * index_guess(local, guess, s)
163  *
164  * given a xtd_seq_num_t local (which represents the highest
165  * known-to-be-good index) and a sequence number s (from a newly
166  * arrived packet), sets the contents of *guess to contain the best
167  * guess of the packet index to which s corresponds, and returns the
168  * difference between *guess and *local
169  */
170 
171 int
172 index_guess(const xtd_seq_num_t *local,
173 		   xtd_seq_num_t *guess,
174 		   sequence_number_t s);
175 
176 
177 #endif /* RDBX_H */
178 
179 
180 
181 
182 
183 
184 
185 
186 
187