CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/net/dns/rr/a.rb
Views: 1904
1
# -*- coding: binary -*-
2
##
3
#
4
# Net::DNS::RR::A
5
#
6
# $id$
7
#
8
##
9
10
require 'ipaddr'
11
12
module Net # :nodoc:
13
module DNS
14
15
class RR
16
17
# =Name
18
#
19
# Net::DNS::RR::A DNS A resource record
20
#
21
# =Synopsis
22
#
23
# require "net/dns/rr"
24
#
25
# =Description
26
#
27
# Net::DNS::RR::A is the class to handle resource records of type A, the
28
# most common in a DNS query. Its resource data is an IPv4 (i.e. 32 bit
29
# long) address, hold in the instance variable +address+.
30
# a = Net::DNS::RR::A.new("localhost.movie.edu. 360 IN A 127.0.0.1")
31
#
32
# a = Net::DNS::RR::A.new(:name => "localhost.movie.edu.",
33
# :ttl => 360,
34
# :cls => Net::DNS::IN,
35
# :type => Net::DNS::A,
36
# :address => "127.0.0.1")
37
#
38
# When computing binary data to transmit the RR, the RDATA section is an
39
# Internet address expressed as four decimal numbers separated by dots
40
# without any embedded spaces (e.g.,"10.2.0.52" or "192.0.5.6").
41
#
42
class A < RR
43
attr_reader :address
44
45
# Assign to the RR::A object a new IPv4 address, which can be in the
46
# form of a string or an IPAddr object
47
#
48
# a.address = "192.168.0.1"
49
# a.address = IPAddr.new("10.0.0.1")
50
#
51
def address=(addr)
52
@address = check_address addr
53
build_pack
54
end # address=
55
56
private
57
58
def check_address(addr)
59
address = ""
60
case addr
61
when String
62
address = IPAddr.new addr
63
when Integer # Address in numeric form
64
tempAddr = [(addr>>24),(addr>>16)&0xFF,(addr>>8)&0xFF,addr&0xFF]
65
tempAddr = tempAddr.collect {|x| x.to_s}.join(".")
66
address = IPAddr.new tempAddr
67
when IPAddr
68
address = addr
69
else
70
raise RRArgumentError, "Unknown address type: #{addr}"
71
end
72
raise RRArgumentError, "Must specify an IPv4 address" unless address.ipv4?
73
address
74
rescue ArgumentError
75
raise RRArgumentError, "Invalid address #{addr}"
76
end
77
78
def build_pack
79
@address_pack = @address.hton
80
@rdlength = @address_pack.size
81
end
82
83
def set_type
84
@type = Net::DNS::RR::Types.new("A")
85
end
86
87
def get_data
88
@address_pack
89
end
90
91
def get_inspect
92
"#@address"
93
end
94
95
def subclass_new_from_hash(args)
96
if args.has_key? :address
97
@address = check_address args[:address]
98
elsif args.has_key? :rdata
99
@address = check_address args[:rdata]
100
else
101
# Address field is mandatory
102
raise RRArgumentError, ":address field is mandatory but missing"
103
end
104
end
105
106
def subclass_new_from_string(str)
107
@address = check_address(str)
108
end
109
110
def subclass_new_from_binary(data,offset)
111
a,b,c,d = data.unpack("@#{offset} CCCC")
112
@address = IPAddr.new "#{a}.#{b}.#{c}.#{d}"
113
return offset + 4
114
end
115
116
end # class A
117
118
end # class RR
119
end # module DNS
120
end # module Net
121
122