webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
rdbx.h
Go to the documentation of this file.
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 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 
57 /* #define ROC_TEST */
58 
59 #ifndef ROC_TEST
60 
61 typedef uint16_t srtp_sequence_number_t; /* 16 bit sequence number */
62 typedef uint32_t srtp_rollover_counter_t; /* 32 bit rollover counter */
63 
64 #else /* use small seq_num and roc datatypes for testing purposes */
65 
66 typedef unsigned char srtp_sequence_number_t; /* 8 bit sequence number */
67 typedef uint16_t srtp_rollover_counter_t; /* 16 bit rollover counter */
68 
69 #endif
70 
71 #define seq_num_median (1 << (8 * sizeof(srtp_sequence_number_t) - 1))
72 #define seq_num_max (1 << (8 * sizeof(srtp_sequence_number_t)))
73 
74 /*
75  * An rtp_xtd_seq_num_t is a 64-bit unsigned integer used as an 'extended'
76  * sequence number.
77  */
79 
80 
81 /*
82  * An srtp_rdbx_t is a replay database with extended range; it uses an
83  * xtd_seq_num_t and a bitmask of recently received indices.
84  */
85 typedef struct {
86  srtp_xtd_seq_num_t index;
88 } srtp_rdbx_t;
89 
90 
91 /*
92  * srtp_rdbx_init(rdbx_ptr, ws)
93  *
94  * initializes the rdbx pointed to by its argument with the window size ws,
95  * setting the rollover counter and sequence number to zero
96  */
97 srtp_err_status_t srtp_rdbx_init(srtp_rdbx_t *rdbx, unsigned long ws);
98 
99 
100 /*
101  * srtp_rdbx_dealloc(rdbx_ptr)
102  *
103  * frees memory associated with the rdbx
104  */
106 
107 
108 /*
109  * srtp_rdbx_estimate_index(rdbx, guess, s)
110  *
111  * given an rdbx and a sequence number s (from a newly arrived packet),
112  * sets the contents of *guess to contain the best guess of the packet
113  * index to which s corresponds, and returns the difference between
114  * *guess and the locally stored synch info
115  */
116 int srtp_rdbx_estimate_index(const srtp_rdbx_t *rdbx, srtp_xtd_seq_num_t *guess, srtp_sequence_number_t s);
117 
118 /*
119  * srtp_rdbx_check(rdbx, delta);
120  *
121  * srtp_rdbx_check(&r, delta) checks to see if the xtd_seq_num_t
122  * which is at rdbx->window_start + delta is in the rdb
123  *
124  */
125 srtp_err_status_t srtp_rdbx_check(const srtp_rdbx_t *rdbx, int difference);
126 
127 /*
128  * srtp_replay_add_index(rdbx, delta)
129  *
130  * adds the srtp_xtd_seq_num_t at rdbx->window_start + delta to replay_db
131  * (and does *not* check if that xtd_seq_num_t appears in db)
132  *
133  * this function should be called *only* after replay_check has
134  * indicated that the index does not appear in the rdbx, and a mutex
135  * should protect the rdbx between these calls if necessary.
136  */
138 
139 
140 /*
141  * srtp_rdbx_set_roc(rdbx, roc) initalizes the srtp_rdbx_t at the location rdbx
142  * to have the rollover counter value roc. If that value is less than
143  * the current rollover counter value, then the function returns
144  * srtp_err_status_replay_old; otherwise, srtp_err_status_ok is returned.
145  *
146  */
148 
149 /*
150  * srtp_rdbx_get_roc(rdbx) returns the value of the rollover counter for
151  * the srtp_rdbx_t pointed to by rdbx
152  *
153  */
154 srtp_xtd_seq_num_t srtp_rdbx_get_packet_index(const srtp_rdbx_t *rdbx);
155 
156 /*
157  * srtp_xtd_seq_num_t functions - these are *internal* functions of rdbx, and
158  * shouldn't be used to manipulate rdbx internal values. use the rdbx
159  * api instead!
160  */
161 
162 /*
163  * srtp_rdbx_get_ws(rdbx_ptr)
164  *
165  * gets the window size which was used to initialize the rdbx
166  */
167 unsigned long srtp_rdbx_get_window_size(const srtp_rdbx_t *rdbx);
168 
169 
170 /* index_init(&pi) initializes a packet index pi (sets it to zero) */
171 void srtp_index_init(srtp_xtd_seq_num_t *pi);
172 
173 /* index_advance(&pi, s) advances a xtd_seq_num_t forward by s */
174 void srtp_index_advance(srtp_xtd_seq_num_t *pi, srtp_sequence_number_t s);
175 
176 
177 /*
178  * srtp_index_guess(local, guess, s)
179  *
180  * given a srtp_xtd_seq_num_t local (which represents the highest
181  * known-to-be-good index) and a sequence number s (from a newly
182  * arrived packet), sets the contents of *guess to contain the best
183  * guess of the packet index to which s corresponds, and returns the
184  * difference between *guess and *local
185  */
186 int srtp_index_guess(const srtp_xtd_seq_num_t *local, srtp_xtd_seq_num_t *guess, srtp_sequence_number_t s);
187 
188 
189 #ifdef __cplusplus
190 }
191 #endif
192 
193 #endif /* RDBX_H */
srtp_err_status_t srtp_rdbx_dealloc(srtp_rdbx_t *rdbx)
Definition: rdbx.c:204
srtp_err_status_t srtp_rdbx_set_roc(srtp_rdbx_t *rdbx, uint32_t roc)
Definition: rdbx.c:218
srtp_xtd_seq_num_t index
Definition: rdbx.h:86
int srtp_rdbx_estimate_index(const srtp_rdbx_t *rdbx, srtp_xtd_seq_num_t *guess, srtp_sequence_number_t s)
Definition: rdbx.c:316
unsigned long long uint64_t
Definition: ptypes.h:120
unsigned int uint32_t
Definition: ptypes.h:105
Definition: rdbx.h:85
uint32_t srtp_rollover_counter_t
Definition: rdbx.h:62
void srtp_index_init(srtp_xtd_seq_num_t *pi)
Definition: rdbx.c:93
void srtp_index_advance(srtp_xtd_seq_num_t *pi, srtp_sequence_number_t s)
Definition: rdbx.c:102
uint64_t srtp_xtd_seq_num_t
Definition: rdbx.h:78
unsigned short uint16_t
Definition: ptypes.h:97
unsigned long srtp_rdbx_get_window_size(const srtp_rdbx_t *rdbx)
Definition: rdbx.c:253
bitvector_t bitmask
Definition: rdbx.h:87
int srtp_index_guess(const srtp_xtd_seq_num_t *local, srtp_xtd_seq_num_t *guess, srtp_sequence_number_t s)
Definition: rdbx.c:127
srtp_err_status_t srtp_rdbx_init(srtp_rdbx_t *rdbx, unsigned long ws)
Definition: rdbx.c:186
struct A s
srtp_err_status_t srtp_rdbx_check(const srtp_rdbx_t *rdbx, int difference)
Definition: rdbx.c:262
srtp_err_status_t srtp_rdbx_add_index(srtp_rdbx_t *rdbx, int delta)
Definition: rdbx.c:288
srtp_xtd_seq_num_t srtp_rdbx_get_packet_index(const srtp_rdbx_t *rdbx)
Definition: rdbx.c:243
srtp_err_status_t
Definition: srtp.h:245
#define pi
Definition: n-body.c:13
uint16_t srtp_sequence_number_t
Definition: rdbx.h:61
Definition: datatypes.h:420