~ubuntu-branches/ubuntu/trusty/zonecheck/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/nresolv/dns_message.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephane Bortzmeyer
  • Date: 2004-03-10 14:08:05 UTC
  • Revision ID: james.westby@ubuntu.com-20040310140805-ij55fso1e23bk8ye
Tags: upstream-2.0.3
ImportĀ upstreamĀ versionĀ 2.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# $Id: dns_message.rb,v 1.12 2003/10/16 11:21:24 sdalu Exp $
 
2
 
 
3
 
4
# AUTHOR   : Stephane D'Alu <sdalu@nic.fr>
 
5
# CREATED  : 2002/08/02 13:58:17
 
6
#
 
7
# COPYRIGHT: AFNIC (c) 2003
 
8
# CONTACT  : 
 
9
# LICENSE  : RUBY
 
10
#
 
11
# $Revision: 1.12 $ 
 
12
# $Date: 2003/10/16 11:21:24 $
 
13
#
 
14
# INSPIRED BY:
 
15
#   - the ruby file: resolv.rb 
 
16
#
 
17
# CONTRIBUTORS: (see also CREDITS file)
 
18
#
 
19
#
 
20
 
 
21
#
 
22
# TODO: add a hash facility to Section
 
23
#
 
24
 
 
25
class NResolv
 
26
    class DNS
 
27
        class Message
 
28
            attr_reader :msgid, :opcode
 
29
 
 
30
            # Allow generation of message id
 
31
            @@genid = rand 0xffff
 
32
            def self.generate_id
 
33
                begin
 
34
                    Thread.exclusive { @@genid = (@@genid + 1) & 0xffff }
 
35
                rescue NameError
 
36
                    @@genid = (@@genid + 1) & 0xffff
 
37
                end
 
38
            end
 
39
            
 
40
            def msgid=(id)
 
41
                if id < 0 || id > 0xffff
 
42
                    raise ArgumentError, 'message id should be in [0..0xffff]'
 
43
                end
 
44
                @msgid = id
 
45
            end
 
46
 
 
47
 
 
48
            def opcode=(code)
 
49
                if code.nil? || code.class != OpCode
 
50
                    raise ArgumentError, 
 
51
                        'expected type NResolv::DNS::OpCode'
 
52
                end
 
53
                @opcode = code
 
54
            end
 
55
 
 
56
 
 
57
            def initialize(id)
 
58
                self.msgid = id.nil? ? Message::generate_id : id
 
59
 
 
60
                @qr                                     = nil
 
61
                @opcode                                 = nil
 
62
                @aa                                     = nil
 
63
                @tc = @rd = @ra                         = nil
 
64
                @ad = @cd                               = nil # RFC 2535
 
65
                @rcode                                  = RCode::NOERROR
 
66
                @question                               = nil
 
67
                @answer = @authority = @additional      = nil
 
68
            end
 
69
 
 
70
 
 
71
 
 
72
 
 
73
            ## DNS Query message
 
74
            ## 
 
75
            ##
 
76
            class Query < Message
 
77
                attr_reader :question
 
78
                attr_reader :rd, :cd
 
79
                attr_writer :rd, :cd
 
80
 
 
81
                def initialize(msgid=nil)
 
82
                    super(msgid)
 
83
                    @qr       = false
 
84
                    @opcode   = OpCode::QUERY
 
85
                    @question = Section::Q::new
 
86
                end
 
87
 
 
88
                def question=(q)
 
89
                    unless q.nil? || q.class == Section::Q
 
90
                        raise ArgumentError,
 
91
                            'expected type NResolv::DNS::Section::Q'
 
92
                    end
 
93
                    @question = q
 
94
                end
 
95
            end
 
96
 
 
97
            ##
 
98
            ##
 
99
            ##
 
100
            class Answer < Message
 
101
                attr_reader :qr, :aa, :rd, :ra, :tc
 
102
                attr_reader :rcode, :question, :answer, :authority, :additional
 
103
                attr_writer :qr, :aa, :rd, :ra, :tc
 
104
 
 
105
                def initialize(msgid=nil)
 
106
                    super(msgid)
 
107
                    @qr         = true
 
108
                    @opcode     = OpCode::QUERY
 
109
                    @question   = Section::Q::new
 
110
                    @answer     = Section::A::new
 
111
                    @authority  = Section::A::new
 
112
                    @additional = Section::A::new
 
113
                end
 
114
 
 
115
                def rcode=(code)
 
116
                    # Sanity check
 
117
                    unless code.kind_of?(RCode)
 
118
                        raise ArgumentError, "expecting #{RCode}"
 
119
                    end
 
120
 
 
121
                    # Initialize
 
122
                    @rcode      = code
 
123
                end
 
124
 
 
125
                def question=(q)
 
126
                    # Sanity check
 
127
                    unless q.kind_of?(Section::Q)
 
128
                        raise ArgumentError, "expecting #{Section::Q}"
 
129
                    end
 
130
 
 
131
                    # Initialize
 
132
                    @question   = q
 
133
                end
 
134
 
 
135
                def answer=(a)
 
136
                    # Sanity check
 
137
                    unless a.kind_of?(Section::A)
 
138
                        raise ArgumentError, "expecting #{Section::A}"
 
139
                    end
 
140
 
 
141
                    # Initialize
 
142
                    @answer     = a
 
143
                end
 
144
                
 
145
                def authority=(a)
 
146
                    # Sanity check
 
147
                    unless a.kind_of?(Section::A)
 
148
                        raise ArgumentError, "expecting #{Section::A}"
 
149
 
 
150
                    end
 
151
                    # Initialize
 
152
                    @authority  = a
 
153
                end
 
154
                
 
155
                def additional=(a)
 
156
                    # Sanity check
 
157
                    unless a.kind_of?(Section::A)
 
158
                        raise ArgumentError, "expecting #{Section::A}"
 
159
                    end
 
160
 
 
161
                    # Initialize
 
162
                    @additional = a
 
163
                end
 
164
            end
 
165
        end
 
166
 
 
167
 
 
168
        ##
 
169
        ## ABSTRACT
 
170
        ##
 
171
        class Section
 
172
            def initialize
 
173
                if self.class == Section
 
174
                    raise "#{self.class} is an abstract class"
 
175
                end
 
176
 
 
177
                @record = []
 
178
            end
 
179
 
 
180
            def length          ; @record.length                ; end
 
181
            def empty?          ; @record.empty?                ; end
 
182
            def [](idx)         ; @record[idx]                  ; end
 
183
            def each(&block)    ; @record.each &block           ; end
 
184
            def reject!(&block) ; @record.reject! &block        ; end
 
185
            def sort!           ; @record.sort! { |x, y| 
 
186
                                    x[0].to_s <=> y[0].to_s }   ; end
 
187
 
 
188
 
 
189
 
 
190
            ##
 
191
            ##
 
192
            ##
 
193
            class A < Section
 
194
                def add(name, rdata, ttl)
 
195
                    # XXX checking
 
196
                    @record << [ name, rdata, ttl ]
 
197
                end
 
198
            end
 
199
            
 
200
            ##
 
201
            ##
 
202
            ##
 
203
            class Q < Section
 
204
                def add(name, rd_class)
 
205
                    # XXX checking
 
206
                    @record << [ name, rd_class ]
 
207
                end
 
208
            end
 
209
        end
 
210
    end
 
211
end
 
212