rrttl.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (C) 2010 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. #include <stdint.h>
  15. #include <sstream>
  16. #include <ostream>
  17. #include <util/buffer.h>
  18. #include <dns/messagerenderer.h>
  19. #include <dns/rrttl.h>
  20. using namespace std;
  21. using namespace isc::dns;
  22. using namespace isc::util;
  23. namespace isc {
  24. namespace dns {
  25. RRTTL::RRTTL(const std::string& ttlstr) {
  26. // Some systems (at least gcc-4.4) flow negative values over into
  27. // unsigned integer, where older systems failed to parse. We want
  28. // that failure here, so we extract into int64 and check the value
  29. int64_t val;
  30. istringstream iss(ttlstr);
  31. iss >> dec >> val;
  32. if (iss.rdstate() == ios::eofbit && val >= 0 && val <= 0xffffffff) {
  33. ttlval_ = static_cast<uint32_t>(val);
  34. } else {
  35. isc_throw(InvalidRRTTL, "invalid TTL");
  36. }
  37. }
  38. RRTTL::RRTTL(InputBuffer& buffer) {
  39. if (buffer.getLength() - buffer.getPosition() < sizeof(uint32_t)) {
  40. isc_throw(IncompleteRRTTL, "incomplete wire-format TTL value");
  41. }
  42. ttlval_ = buffer.readUint32();
  43. }
  44. const string
  45. RRTTL::toText() const {
  46. ostringstream oss;
  47. oss << ttlval_;
  48. return (oss.str());
  49. }
  50. void
  51. RRTTL::toWire(OutputBuffer& buffer) const {
  52. buffer.writeUint32(ttlval_);
  53. }
  54. void
  55. RRTTL::toWire(AbstractMessageRenderer& renderer) const {
  56. renderer.writeUint32(ttlval_);
  57. }
  58. ostream&
  59. operator<<(ostream& os, const RRTTL& rrttl) {
  60. os << rrttl.toText();
  61. return (os);
  62. }
  63. }
  64. }