PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/net/dns/rr/aaaa.rb

http://github.com/bluemonk/net-dns
Ruby | 94 lines | 60 code | 13 blank | 21 comment | 1 complexity | f730607e42b0b4485021756125cc8485 MD5 | raw file
  1. module Net
  2. module DNS
  3. class RR
  4. #
  5. # = IPv6 Address Record (AAAA)
  6. #
  7. # Class for DNS IPv6 Address (AAAA) resource records.
  8. #
  9. class AAAA < RR
  10. # Gets the current IPv6 address for this record.
  11. #
  12. # Returns an instance of IPAddr.
  13. attr_reader :address
  14. # Assigns a new IPv6 address to this record, which can be in the
  15. # form of a <tt>String</tt> or an <tt>IPAddr</tt> object.
  16. #
  17. # Examples
  18. #
  19. # a.address = "192.168.0.1"
  20. # a.address = IPAddr.new("10.0.0.1")
  21. #
  22. # Returns the new allocated instance of IPAddr.
  23. def address=(string_or_ipaddr)
  24. @address = check_address(string_or_ipaddr)
  25. build_pack
  26. @address
  27. end
  28. # Gets the standardized value for this record,
  29. # represented by the value of <tt>address</tt>.
  30. #
  31. # Returns a String.
  32. def value
  33. address.to_s
  34. end
  35. private
  36. def subclass_new_from_hash(options)
  37. if options.key?(:address)
  38. @address = check_address(options[:address])
  39. else
  40. raise ArgumentError, ":address field is mandatory"
  41. end
  42. end
  43. def subclass_new_from_string(str)
  44. @address = check_address(str)
  45. end
  46. def subclass_new_from_binary(data, offset)
  47. tokens = data.unpack("@#{offset} n8")
  48. @address = IPAddr.new(format("%x:%x:%x:%x:%x:%x:%x:%x", *tokens))
  49. offset + 16
  50. end
  51. def set_type
  52. @type = Net::DNS::RR::Types.new("AAAA")
  53. end
  54. def get_inspect
  55. value
  56. end
  57. def check_address(input)
  58. address = case input
  59. when IPAddr
  60. input
  61. when String
  62. IPAddr.new(input)
  63. else
  64. raise ArgumentError, "Invalid IP address `#{input}'"
  65. end
  66. unless address.ipv6?
  67. raise(ArgumentError, "Must specify an IPv6 address")
  68. end
  69. address
  70. end
  71. def build_pack
  72. @address_pack = @address.hton
  73. @rdlength = @address_pack.size
  74. end
  75. def get_data
  76. @address_pack
  77. end
  78. end
  79. end
  80. end
  81. end