qid_gen.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #include <asiolink/qid_gen.h>
  20. #include <sys/time.h>
  21. namespace {
  22. asiolink::QidGenerator qid_generator_instance;
  23. }
  24. namespace asiolink {
  25. QidGenerator&
  26. QidGenerator::getInstance() {
  27. return (qid_generator_instance);
  28. }
  29. QidGenerator::QidGenerator() : dist_(0, 65535),
  30. vgen_(generator_, dist_)
  31. {
  32. seed();
  33. }
  34. void
  35. QidGenerator::seed() {
  36. struct timeval tv;
  37. gettimeofday(&tv, 0);
  38. generator_.seed((tv.tv_sec * 1000000) + tv.tv_usec);
  39. }
  40. isc::dns::qid_t
  41. QidGenerator::generateQid() {
  42. return (vgen_());
  43. }
  44. } // namespace asiolink