rrttl.cc 2.1 KB

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