File: types.rb

package info (click to toggle)
ruby-net-dns 0.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 452 kB
  • sloc: ruby: 3,944; makefile: 6
file content (191 lines) | stat: -rw-r--r-- 6,579 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
module Net # :nodoc:
  module DNS
    class RR
      # This is an auxiliary class to handle RR type field in a DNS packet.
      class Types
        TYPES = {
          'SIGZERO' => 0, # RFC2931 consider this a pseudo type
          'A' => 1, # RFC 1035, Section 3.4.1
          'NS' => 2,       # RFC 1035, Section 3.3.11
          'MD' => 3,       # RFC 1035, Section 3.3.4 (obsolete)
          'MF' => 4,       # RFC 1035, Section 3.3.5 (obsolete)
          'CNAME' => 5, # RFC 1035, Section 3.3.1
          'SOA' => 6, # RFC 1035, Section 3.3.13
          'MB' => 7,       # RFC 1035, Section 3.3.3
          'MG' => 8,       # RFC 1035, Section 3.3.6
          'MR' => 9,       # RFC 1035, Section 3.3.8
          'NULL' => 10, # RFC 1035, Section 3.3.10
          'WKS' => 11,      # RFC 1035, Section 3.4.2 (deprecated)
          'PTR' => 12,      # RFC 1035, Section 3.3.12
          'HINFO' => 13,      # RFC 1035, Section 3.3.2
          'MINFO' => 14,      # RFC 1035, Section 3.3.7
          'MX' => 15, # RFC 1035, Section 3.3.9
          'TXT' => 16, # RFC 1035, Section 3.3.14
          'RP' => 17, # RFC 1183, Section 2.2
          'AFSDB' => 18, # RFC 1183, Section 1
          'X25' => 19, # RFC 1183, Section 3.1
          'ISDN' => 20, # RFC 1183, Section 3.2
          'RT' => 21, # RFC 1183, Section 3.3
          'NSAP' => 22, # RFC 1706, Section 5
          'NSAP_PTR' => 23, # RFC 1348 (obsolete)
          # The following 2 RRs are impemented in Net::DNS::SEC, TODO
          'SIG' => 24,      # RFC 2535, Section 4.1
          'KEY' => 25,      # RFC 2535, Section 3.1
          'PX' => 26, # RFC 2163,
          'GPOS' => 27,      # RFC 1712 (obsolete)
          'AAAA' => 28,      # RFC 1886, Section 2.1
          'LOC' => 29, # RFC 1876
          # The following RR is implemented in Net::DNS::SEC, TODO
          'NXT' => 30,      # RFC 2535, Section 5.2
          'EID' => 31,      # draft-ietf-nimrod-dns-xx.txt
          'NIMLOC' => 32, # draft-ietf-nimrod-dns-xx.txt
          'SRV' => 33, # RFC 2052
          'ATMA' => 34, # ???
          'NAPTR' => 35, # RFC 2168
          'KX' => 36, # RFC 2230
          'CERT' => 37, # RFC 2538
          'DNAME' => 39, # RFC 2672
          'OPT' => 41, # RFC 2671
          # The following 4 RRs are implemented in Net::DNS::SEC TODO
          'DS' => 43, # draft-ietf-dnsext-delegation-signer
          'SSHFP' => 44,      # draft-ietf-secsh-dns (No RFC # yet at time of coding)
          'RRSIG' => 46,      # draft-ietf-dnsext-dnssec-2535typecode-change
          'NSEC' => 47, # draft-ietf-dnsext-dnssec-2535typecode-change
          'DNSKEY' => 48, # draft-ietf-dnsext-dnssec-2535typecode-change
          'UINFO' => 100, # non-standard
          'UID' => 101,     # non-standard
          'GID' => 102,     # non-standard
          'UNSPEC' => 103, # non-standard
          'TKEY' => 249,     # RFC 2930
          'TSIG' => 250,     # RFC 2931
          'IXFR' => 251,     # RFC 1995
          'AXFR' => 252,     # RFC 1035
          'MAILB' => 253,     # RFC 1035 (MB, MG, MR)
          'MAILA' => 254,     # RFC 1035 (obsolete - see MX)
          'ANY' => 255, # RFC 1035
        }.freeze

        # The default value when type is nil in Resource Records
        @@default = TYPES["A"]

        def self.default
          @@default
        end

        # Be able to control the default type to assign when
        # type is +nil+. Default to +A+
        def self.default=(str)
          if TYPES.key? str
            @@default = TYPES[str]
          else
            raise ArgumentError, "Unknown type #{str}"
          end
        end

        # Checks whether +type+ is a valid RR type.
        def self.valid?(type)
          case type
          when String
            TYPES.key?(type)
          when Integer
            TYPES.invert.key?(type)
          else
            raise ArgumentError, "Wrong type class: #{type.class}"
          end
        end

        # Returns the type in string format, as "A" or "NS",
        # given the numeric value
        def self.to_str(type)
          case type
          when Integer
            if TYPES.invert.key? type
              TYPES.invert[type]
            else
              raise ArgumentError, "Unknown type number #{type}"
            end
          else
            raise ArgumentError, "Wrong type class: #{type.class}"
          end
        end

        # Gives in output the keys from the +Types+ hash
        # in a format suited for regexps
        def self.regexp
          # Longest ones go first, so the regex engine will match AAAA before A.
          TYPES.keys.sort { |a, b| b.length <=> a.length }.join("|")
        end

        # Creates a new object representing an RR type. Performs some
        # checks on the argument validity too. Il +type+ is +nil+, the
        # default value is +ANY+ or the one set with Types.default=
        def initialize(type)
          case type
          when String
            # type in the form "A" or "NS"
            new_from_string(type.upcase)
          when Integer
            # type in numeric form
            new_from_num(type)
          when nil
            # default type, control with Types.default=
            @str = TYPES.invert[@@default]
            @num = @@default
          else
            raise ArgumentError, "Wrong type class: #{type.class}"
          end
        end

        # Returns the type in number format
        # (default for normal use)
        def inspect
          @num
        end

        # Returns the type in string format,
        # i.d. "A" or "NS" or such a string.
        def to_s
          @str
        end

        # Returns the type in numeric format,
        # usable by the pack methods for data transfers
        def to_i
          @num.to_i
        end

        def to_str
          @num.to_s
        end

        private

        # Constructor for string data type.
        def new_from_string(type)
          case type
          when /^TYPE\\d+/
            # TODO!!!
          else
            # String with name of type
            if TYPES.key? type
              @str = type
              @num = TYPES[type]
            else
              raise ArgumentError, "Unknown type #{type}"
            end
          end
        end

        # Contructor for numeric data type.
        def new_from_num(type)
          if TYPES.invert.key? type
            @num = type
            @str = TYPES.invert[type]
          else
            raise ArgumentError, "Unkown type number #{type}"
          end
        end
      end
    end
  end
end