CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/net/dns/rr/classes.rb
Views: 11784
1
# -*- coding: binary -*-
2
module Net # :nodoc:
3
module DNS
4
5
class RR
6
7
#
8
# This is an auxiliary class to hadle RR class field in a DNS packet.
9
#
10
class Classes
11
12
# An hash with the values of each RR class stored with the
13
# respective id number
14
Classes = {
15
'IN' => 1, # RFC 1035
16
'CH' => 3, # RFC 1035
17
'HS' => 4, # RFC 1035
18
'NONE' => 254, # RFC 2136
19
'ANY' => 255, # RFC 1035
20
}
21
22
# The default value when class is nil in Resource Records
23
@@default = Classes["IN"]
24
25
# Be able to control the default class to assign when
26
# cls argument is +nil+. Default to +IN+
27
def self.default=(str)
28
if Classes.has_key? str
29
@@default = Classes[str]
30
else
31
raise ClassArgumentError, "Unknown class #{str}"
32
end
33
end
34
35
# Checks whether +cls+ is a valid RR class.
36
def self.valid?(cls)
37
case cls
38
when String
39
return Classes.has_key?(cls)
40
when Integer
41
return Classes.invert.has_key?(cls)
42
else
43
raise ClassArgumentError, "Wrong class: #{cls.class}"
44
end
45
end
46
47
# Returns the class in string format, as "IN" or "CH",
48
# given the numeric value
49
def self.to_str(cls)
50
case cls
51
when Integer
52
if Classes.invert.has_key? cls
53
return Classes.invert[cls]
54
else
55
raise ClassArgumentError, "Unknown class number #{cls}"
56
end
57
else
58
raise ClassArgumentError, "Wrong class: #{cls.class}"
59
end
60
end
61
62
# Gives in output the keys from the +Classes+ hash
63
# in a format suited for regexps
64
def self.regexp
65
Classes.keys.join("|")
66
end
67
68
# Creates a new object representing an RR class. Performs some
69
# checks on the argument validity too. Il +cls+ is +nil+, the
70
# default value is +ANY+ or the one set with Classes.default=
71
def initialize(cls)
72
case cls
73
when String
74
# type in the form "A" or "NS"
75
new_from_string(cls.upcase)
76
when Integer
77
# type in numeric form
78
new_from_num(cls)
79
when nil
80
# default type, control with Classes.default=
81
@str = Classes.invert[@@default]
82
@num = @@default
83
else
84
raise ClassArgumentError, "Wrong class: #{cls.class}"
85
end
86
end
87
88
# Constructor for string data class,
89
# *PRIVATE* method
90
def new_from_string(cls)
91
case cls
92
when /^CLASS(\d+)$/
93
new_from_num(Regexp.last_match(1).to_i)
94
else
95
# String with name of class
96
if Classes.has_key? cls
97
@str = cls
98
@num = Classes[cls]
99
else
100
raise ClassArgumentError, "Unknown class #{cls}"
101
end
102
end
103
end
104
105
# Constructor for numeric data class
106
# *PRIVATE* method
107
def new_from_num(cls)
108
raise ClassArgumentError, "Invalid class #{cls}" if cls < 0 || cls > 0xFFFF
109
if Classes.invert.has_key? cls
110
@num = cls
111
@str = Classes.invert[cls]
112
else
113
@num = cls
114
@str = "CLASS#{cls}"
115
end
116
end
117
118
# Returns the class in number format
119
# (default for normal use)
120
def inspect
121
@num
122
end
123
124
# Returns the class in string format,
125
# i.d. "IN" or "CH" or such a string.
126
def to_s
127
@str
128
end
129
130
# Returns the class in numeric format,
131
# usable by the pack methods for data transfers
132
def to_i
133
@num.to_i
134
end
135
136
137
# Should be used only for testing purpouses
138
def to_str
139
@num.to_s
140
end
141
142
private :new_from_num, :new_from_string
143
144
end # class Classes
145
146
end # class RR
147
end # module DNS
148
end # module Net
149
150
class ClassArgumentError < ArgumentError # :nodoc:
151
end
152
153