rdbx.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #ifndef RDBX_H
  11. #define RDBX_H
  12. #include "datatypes.h"
  13. #include "err.h"
  14. /* #define ROC_TEST */
  15. #ifndef ROC_TEST
  16. typedef uint16_t sequence_number_t; /* 16 bit sequence number */
  17. typedef uint32_t rollover_counter_t; /* 32 bit rollover counter */
  18. #else /* use small seq_num and roc datatypes for testing purposes */
  19. typedef unsigned char sequence_number_t; /* 8 bit sequence number */
  20. typedef uint16_t rollover_counter_t; /* 16 bit rollover counter */
  21. #endif
  22. #define seq_num_median (1 << (8*sizeof(sequence_number_t) - 1))
  23. #define seq_num_max (1 << (8*sizeof(sequence_number_t)))
  24. /*
  25. * An xtd_seq_num_t is a 64-bit unsigned integer used as an 'extended'
  26. * sequence number.
  27. */
  28. typedef uint64_t xtd_seq_num_t;
  29. /*
  30. * An rdbx_t is a replay database with extended range; it uses an
  31. * xtd_seq_num_t and a bitmask of recently received indices.
  32. */
  33. typedef struct {
  34. xtd_seq_num_t index;
  35. bitvector_t bitmask;
  36. } rdbx_t;
  37. /*
  38. * rdbx_init(rdbx_ptr, ws)
  39. *
  40. * initializes the rdbx pointed to by its argument with the window size ws,
  41. * setting the rollover counter and sequence number to zero
  42. */
  43. err_status_t
  44. rdbx_init(rdbx_t *rdbx, unsigned long ws);
  45. /*
  46. * rdbx_dealloc(rdbx_ptr)
  47. *
  48. * frees memory associated with the rdbx
  49. */
  50. err_status_t
  51. rdbx_dealloc(rdbx_t *rdbx);
  52. /*
  53. * rdbx_estimate_index(rdbx, guess, s)
  54. *
  55. * given an rdbx and a sequence number s (from a newly arrived packet),
  56. * sets the contents of *guess to contain the best guess of the packet
  57. * index to which s corresponds, and returns the difference between
  58. * *guess and the locally stored synch info
  59. */
  60. int
  61. rdbx_estimate_index(const rdbx_t *rdbx,
  62. xtd_seq_num_t *guess,
  63. sequence_number_t s);
  64. /*
  65. * rdbx_check(rdbx, delta);
  66. *
  67. * rdbx_check(&r, delta) checks to see if the xtd_seq_num_t
  68. * which is at rdbx->window_start + delta is in the rdb
  69. *
  70. */
  71. err_status_t
  72. rdbx_check(const rdbx_t *rdbx, int difference);
  73. /*
  74. * replay_add_index(rdbx, delta)
  75. *
  76. * adds the xtd_seq_num_t at rdbx->window_start + delta to replay_db
  77. * (and does *not* check if that xtd_seq_num_t appears in db)
  78. *
  79. * this function should be called *only* after replay_check has
  80. * indicated that the index does not appear in the rdbx, and a mutex
  81. * should protect the rdbx between these calls if necessary.
  82. */
  83. err_status_t
  84. rdbx_add_index(rdbx_t *rdbx, int delta);
  85. /*
  86. * rdbx_set_roc(rdbx, roc) initalizes the rdbx_t at the location rdbx
  87. * to have the rollover counter value roc. If that value is less than
  88. * the current rollover counter value, then the function returns
  89. * err_status_replay_old; otherwise, err_status_ok is returned.
  90. *
  91. */
  92. err_status_t
  93. rdbx_set_roc(rdbx_t *rdbx, uint32_t roc);
  94. /*
  95. * rdbx_get_roc(rdbx) returns the value of the rollover counter for
  96. * the rdbx_t pointed to by rdbx
  97. *
  98. */
  99. xtd_seq_num_t
  100. rdbx_get_packet_index(const rdbx_t *rdbx);
  101. /*
  102. * xtd_seq_num_t functions - these are *internal* functions of rdbx, and
  103. * shouldn't be used to manipulate rdbx internal values. use the rdbx
  104. * api instead!
  105. */
  106. /*
  107. * rdbx_get_ws(rdbx_ptr)
  108. *
  109. * gets the window size which was used to initialize the rdbx
  110. */
  111. unsigned long
  112. rdbx_get_window_size(const rdbx_t *rdbx);
  113. /* index_init(&pi) initializes a packet index pi (sets it to zero) */
  114. void
  115. index_init(xtd_seq_num_t *pi);
  116. /* index_advance(&pi, s) advances a xtd_seq_num_t forward by s */
  117. void
  118. index_advance(xtd_seq_num_t *pi, sequence_number_t s);
  119. /*
  120. * index_guess(local, guess, s)
  121. *
  122. * given a xtd_seq_num_t local (which represents the highest
  123. * known-to-be-good index) and a sequence number s (from a newly
  124. * arrived packet), sets the contents of *guess to contain the best
  125. * guess of the packet index to which s corresponds, and returns the
  126. * difference between *guess and *local
  127. */
  128. int
  129. index_guess(const xtd_seq_num_t *local,
  130. xtd_seq_num_t *guess,
  131. sequence_number_t s);
  132. #endif /* RDBX_H */