rrttl.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <stdint.h>
  16. #include <sstream>
  17. #include <ostream>
  18. #include <dns/buffer.h>
  19. #include <dns/messagerenderer.h>
  20. #include <dns/rrttl.h>
  21. using namespace std;
  22. using namespace isc::dns;
  23. namespace isc {
  24. namespace dns {
  25. RRTTL::RRTTL(const 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(MessageRenderer& 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. }