OLD | NEW |
| (Empty) |
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 /* | |
12 * | |
13 * Copyright (c) 2001-2006, Cisco Systems, Inc. | |
14 * All rights reserved. | |
15 * | |
16 * Redistribution and use in source and binary forms, with or without | |
17 * modification, are permitted provided that the following conditions | |
18 * are met: | |
19 * | |
20 * Redistributions of source code must retain the above copyright | |
21 * notice, this list of conditions and the following disclaimer. | |
22 * | |
23 * Redistributions in binary form must reproduce the above | |
24 * copyright notice, this list of conditions and the following | |
25 * disclaimer in the documentation and/or other materials provided | |
26 * with the distribution. | |
27 * | |
28 * Neither the name of the Cisco Systems, Inc. nor the names of its | |
29 * contributors may be used to endorse or promote products derived | |
30 * from this software without specific prior written permission. | |
31 * | |
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
35 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
36 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
37 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
38 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
39 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
43 * OF THE POSSIBILITY OF SUCH DAMAGE. | |
44 * | |
45 */ | |
46 | |
47 #ifndef RDBX_H | |
48 #define RDBX_H | |
49 | |
50 #include "datatypes.h" | |
51 #include "err.h" | |
52 | |
53 /** | |
54 * Compatibility shim for v1->v2 transition. | |
55 */ | |
56 | |
57 #define srtp_rdbx_get_packet_index rdbx_get_packet_index | |
58 | |
59 /* #define ROC_TEST */ | |
60 | |
61 #ifndef ROC_TEST | |
62 | |
63 typedef uint16_t sequence_number_t; /* 16 bit sequence number */ | |
64 typedef uint32_t rollover_counter_t; /* 32 bit rollover counter */ | |
65 | |
66 #else /* use small seq_num and roc datatypes for testing purposes */ | |
67 | |
68 typedef unsigned char sequence_number_t; /* 8 bit sequence number */ | |
69 typedef uint16_t rollover_counter_t; /* 16 bit rollover counter */ | |
70 | |
71 #endif | |
72 | |
73 #define seq_num_median (1 << (8*sizeof(sequence_number_t) - 1)) | |
74 #define seq_num_max (1 << (8*sizeof(sequence_number_t))) | |
75 | |
76 /* | |
77 * An xtd_seq_num_t is a 64-bit unsigned integer used as an 'extended' | |
78 * sequence number. | |
79 */ | |
80 | |
81 typedef uint64_t xtd_seq_num_t; | |
82 | |
83 | |
84 /* | |
85 * An rdbx_t is a replay database with extended range; it uses an | |
86 * xtd_seq_num_t and a bitmask of recently received indices. | |
87 */ | |
88 | |
89 typedef struct { | |
90 xtd_seq_num_t index; | |
91 bitvector_t bitmask; | |
92 } rdbx_t; | |
93 | |
94 | |
95 /* | |
96 * rdbx_init(rdbx_ptr, ws) | |
97 * | |
98 * initializes the rdbx pointed to by its argument with the window size ws, | |
99 * setting the rollover counter and sequence number to zero | |
100 */ | |
101 | |
102 err_status_t | |
103 rdbx_init(rdbx_t *rdbx, unsigned long ws); | |
104 | |
105 | |
106 /* | |
107 * rdbx_dealloc(rdbx_ptr) | |
108 * | |
109 * frees memory associated with the rdbx | |
110 */ | |
111 | |
112 err_status_t | |
113 rdbx_dealloc(rdbx_t *rdbx); | |
114 | |
115 | |
116 /* | |
117 * rdbx_estimate_index(rdbx, guess, s) | |
118 * | |
119 * given an rdbx and a sequence number s (from a newly arrived packet), | |
120 * sets the contents of *guess to contain the best guess of the packet | |
121 * index to which s corresponds, and returns the difference between | |
122 * *guess and the locally stored synch info | |
123 */ | |
124 | |
125 int | |
126 rdbx_estimate_index(const rdbx_t *rdbx, | |
127 xtd_seq_num_t *guess, | |
128 sequence_number_t s); | |
129 | |
130 /* | |
131 * rdbx_check(rdbx, delta); | |
132 * | |
133 * rdbx_check(&r, delta) checks to see if the xtd_seq_num_t | |
134 * which is at rdbx->window_start + delta is in the rdb | |
135 * | |
136 */ | |
137 | |
138 err_status_t | |
139 rdbx_check(const rdbx_t *rdbx, int difference); | |
140 | |
141 /* | |
142 * replay_add_index(rdbx, delta) | |
143 * | |
144 * adds the xtd_seq_num_t at rdbx->window_start + delta to replay_db | |
145 * (and does *not* check if that xtd_seq_num_t appears in db) | |
146 * | |
147 * this function should be called *only* after replay_check has | |
148 * indicated that the index does not appear in the rdbx, and a mutex | |
149 * should protect the rdbx between these calls if necessary. | |
150 */ | |
151 | |
152 err_status_t | |
153 rdbx_add_index(rdbx_t *rdbx, int delta); | |
154 | |
155 | |
156 /* | |
157 * rdbx_set_roc(rdbx, roc) initalizes the rdbx_t at the location rdbx | |
158 * to have the rollover counter value roc. If that value is less than | |
159 * the current rollover counter value, then the function returns | |
160 * err_status_replay_old; otherwise, err_status_ok is returned. | |
161 * | |
162 */ | |
163 | |
164 err_status_t | |
165 rdbx_set_roc(rdbx_t *rdbx, uint32_t roc); | |
166 | |
167 /* | |
168 * rdbx_get_roc(rdbx) returns the value of the rollover counter for | |
169 * the rdbx_t pointed to by rdbx | |
170 * | |
171 */ | |
172 | |
173 xtd_seq_num_t | |
174 rdbx_get_packet_index(const rdbx_t *rdbx); | |
175 | |
176 /* | |
177 * xtd_seq_num_t functions - these are *internal* functions of rdbx, and | |
178 * shouldn't be used to manipulate rdbx internal values. use the rdbx | |
179 * api instead! | |
180 */ | |
181 | |
182 /* | |
183 * rdbx_get_ws(rdbx_ptr) | |
184 * | |
185 * gets the window size which was used to initialize the rdbx | |
186 */ | |
187 | |
188 unsigned long | |
189 rdbx_get_window_size(const rdbx_t *rdbx); | |
190 | |
191 | |
192 /* index_init(&pi) initializes a packet index pi (sets it to zero) */ | |
193 | |
194 void | |
195 index_init(xtd_seq_num_t *pi); | |
196 | |
197 /* index_advance(&pi, s) advances a xtd_seq_num_t forward by s */ | |
198 | |
199 void | |
200 index_advance(xtd_seq_num_t *pi, sequence_number_t s); | |
201 | |
202 | |
203 /* | |
204 * index_guess(local, guess, s) | |
205 * | |
206 * given a xtd_seq_num_t local (which represents the highest | |
207 * known-to-be-good index) and a sequence number s (from a newly | |
208 * arrived packet), sets the contents of *guess to contain the best | |
209 * guess of the packet index to which s corresponds, and returns the | |
210 * difference between *guess and *local | |
211 */ | |
212 | |
213 int | |
214 index_guess(const xtd_seq_num_t *local, | |
215 xtd_seq_num_t *guess, | |
216 sequence_number_t s); | |
217 | |
218 | |
219 #endif /* RDBX_H */ | |
220 | |
221 | |
222 | |
223 | |
224 | |
225 | |
226 | |
227 | |
228 | |
OLD | NEW |