qid_gen.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // qid_gen defines a generator for query id's
  15. //
  16. // We probably want to merge this with the weighted random in the nsas
  17. // (and other parts where we need randomness, perhaps another thing
  18. // for a general libutil?)
  19. #ifndef __QID_GEN_H
  20. #define __QID_GEN_H
  21. #include <dns/message.h>
  22. #include <boost/random/mersenne_twister.hpp>
  23. #include <boost/random/uniform_int.hpp>
  24. #include <boost/random/variate_generator.hpp>
  25. namespace asiolink {
  26. /// This class generates Qids for outgoing queries
  27. ///
  28. /// It is implemented as a singleton; the public way to access it
  29. /// is to call getInstance()->generateQid().
  30. ///
  31. /// It automatically seeds it with the current time when it is first
  32. /// used.
  33. class QidGenerator {
  34. public:
  35. /// \brief Returns the singleton instance of the QidGenerator
  36. ///
  37. /// Returns a reference to the singleton instance of the generator
  38. static QidGenerator& getInstance();
  39. /// \brief Default constructor
  40. ///
  41. /// It is recommended that getInstance is used rather than creating
  42. /// separate instances of this class.
  43. ///
  44. /// The constructor automatically seeds the generator with the
  45. /// current time.
  46. QidGenerator();
  47. /// Generate a Qid
  48. ///
  49. /// \return A random Qid
  50. isc::dns::qid_t generateQid();
  51. /// \brief Seeds the QidGenerator (based on the current time)
  52. ///
  53. /// This is automatically called by the constructor
  54. void seed();
  55. private:
  56. // "Mersenne Twister: A 623-dimensionally equidistributed
  57. // uniform pseudo-random number generator", Makoto Matsumoto and
  58. // Takuji Nishimura, ACM Transactions on Modeling and Computer
  59. // Simulation: Special Issue on Uniform Random Number Generation,
  60. // Vol. 8, No. 1, January 1998, pp. 3-30.
  61. //
  62. // mt19937 is an implementation of one of the pseudo random
  63. // generators described in this paper.
  64. boost::mt19937 generator_;
  65. // For qid's we want a uniform distribution
  66. boost::uniform_int<> dist_;
  67. boost::variate_generator<boost::mt19937&, boost::uniform_int<> > vgen_;
  68. };
  69. } // namespace asiolink
  70. #endif // __QID_GEN_H