-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolv.rb
1661 lines (1427 loc) · 41 KB
/
resolv.rb
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=begin
= resolv library
resolv.rb is a resolver library written in Ruby.
Since it is written in Ruby, it is thread-aware.
I.e. it can resolv many hostnames concurrently.
It is possible to lookup various resources of DNS using DNS module directly.
== example
Resolv.getaddress("www.ruby-lang.org")
Resolv.getname("210.251.121.214").to_s
Resolv::DNS.new.getresources("www.ruby-lang.org", Resolv::DNS::Resource::IN::A).collect {|r| r.address}
Resolv::DNS.new.getresources("ruby-lang.org", Resolv::DNS::Resource::IN::MX).collect {|r| [r.exchange.to_s, r.preference]}
== Resolv class
=== class methods
--- Resolv.getaddress(name)
--- Resolv.getaddresses(name)
--- Resolv.each_address(name) {|address| ...}
They lookups IP addresses of ((|name|)) which represents a hostname
as a string by default resolver.
getaddress returns first entry of lookupped addresses.
getaddresses returns lookupped addresses as an array.
each_address iterates over lookupped addresses.
--- Resolv.getname(address)
--- Resolv.getnames(address)
--- Resolv.each_name(address) {|name| ...}
lookups hostnames of ((|address|)) which represents IP address as a string.
getname returns first entry of lookupped names.
getnames returns lookupped names as an array.
each_names iterates over lookupped names.
== Resolv::Hosts class
hostname resolver using /etc/hosts format.
=== class methods
--- Resolv::Hosts.new(hosts='/etc/hosts')
=== methods
--- Resolv::Hosts#getaddress(name)
--- Resolv::Hosts#getaddresses(name)
--- Resolv::Hosts#each_address(name) {|address| ...}
address lookup methods.
--- Resolv::Hosts#getname(address)
--- Resolv::Hosts#getnames(address)
--- Resolv::Hosts#each_name(address) {|name| ...}
hostnames lookup methods.
== Resolv::DNS class
DNS stub resolver.
=== class methods
--- Resolv::DNS.new(resolv_conf='/etc/resolv.conf')
=== methods
--- Resolv::DNS#getaddress(name)
--- Resolv::DNS#getaddresses(name)
--- Resolv::DNS#each_address(name) {|address| ...}
address lookup methods.
((|name|)) must be a instance of Resolv::DNS::Name or String. Lookupped
address is represented as an instance of Resolv::IPv4 or Resolv::IPv6.
--- Resolv::DNS#getname(address)
--- Resolv::DNS#getnames(address)
--- Resolv::DNS#each_name(address) {|name| ...}
hostnames lookup methods.
((|address|)) must be a instance of Resolv::IPv4, Resolv::IPv6 or String.
Lookupped name is represented as an instance of Resolv::DNS::Name.
--- Resolv::DNS#getresource(name, typeclass)
--- Resolv::DNS#getresources(name, typeclass)
--- Resolv::DNS#each_resource(name, typeclass) {|resource| ...}
They lookup DNS resources of ((|name|)).
((|name|)) must be a instance of Resolv::Name or String.
((|typeclass|)) should be one of follows:
* Resolv::DNS::Resource::IN::ANY
* Resolv::DNS::Resource::IN::NS
* Resolv::DNS::Resource::IN::CNAME
* Resolv::DNS::Resource::IN::SOA
* Resolv::DNS::Resource::IN::HINFO
* Resolv::DNS::Resource::IN::MINFO
* Resolv::DNS::Resource::IN::MX
* Resolv::DNS::Resource::IN::TXT
* Resolv::DNS::Resource::IN::ANY
* Resolv::DNS::Resource::IN::A
* Resolv::DNS::Resource::IN::WKS
* Resolv::DNS::Resource::IN::PTR
* Resolv::DNS::Resource::IN::AAAA
Lookupped resource is represented as an instance of (a subclass of)
Resolv::DNS::Resource.
(Resolv::DNS::Resource::IN::A, etc.)
== Resolv::DNS::Resource::IN::NS class
--- name
== Resolv::DNS::Resource::IN::CNAME class
--- name
== Resolv::DNS::Resource::IN::SOA class
--- mname
--- rname
--- serial
--- refresh
--- retry
--- expire
--- minimum
== Resolv::DNS::Resource::IN::HINFO class
--- cpu
--- os
== Resolv::DNS::Resource::IN::MINFO class
--- rmailbx
--- emailbx
== Resolv::DNS::Resource::IN::MX class
--- preference
--- exchange
== Resolv::DNS::Resource::IN::TXT class
--- data
== Resolv::DNS::Resource::IN::A class
--- address
== Resolv::DNS::Resource::IN::WKS class
--- address
--- protocol
--- bitmap
== Resolv::DNS::Resource::IN::PTR class
--- name
== Resolv::DNS::Resource::IN::AAAA class
--- address
== Resolv::DNS::Name class
=== class methods
--- Resolv::DNS::Name.create(name)
=== methods
--- Resolv::DNS::Name#to_s
== Resolv::DNS::Resource class
== Resolv::IPv4 class
=== class methods
--- Resolv::IPv4.create(address)
=== methods
--- Resolv::IPv4#to_s
--- Resolv::IPv4#to_name
=== constants
--- Resolv::IPv4::Regex
regular expression for IPv4 address.
== Resolv::IPv6 class
=== class methods
--- Resolv::IPv6.create(address)
=== methods
--- Resolv::IPv6#to_s
--- Resolv::IPv6#to_name
=== constants
--- Resolv::IPv6::Regex
regular expression for IPv6 address.
== Bugs
NIS is not supported.
/etc/nsswitch.conf is not supported.
=end
require 'socket'
require 'fcntl'
require 'timeout'
require 'thread'
class Resolv
def self.getaddress(name)
DefaultResolver.getaddress(name)
end
def self.getaddresses(name)
DefaultResolver.getaddresses(name)
end
def self.each_address(name, &block)
DefaultResolver.each_address(name, &block)
end
def self.getname(address)
DefaultResolver.getname(address)
end
def self.getnames(address)
DefaultResolver.getnames(address)
end
def self.each_name(address, &proc)
DefaultResolver.each_name(address, &proc)
end
def initialize(resolvers=[Hosts.new, DNS.new])
@resolvers = resolvers
end
def getaddress(name)
each_address(name) {|address| return address}
raise ResolvError.new("no address for #{name}")
end
def getaddresses(name)
ret = []
each_address(name) {|address| ret << address}
return ret
end
def each_address(name)
if AddressRegex =~ name
yield name
return
end
yielded = false
@resolvers.each {|r|
r.each_address(name) {|address|
yield address.to_s
yielded = true
}
return if yielded
}
end
def getname(address)
each_name(address) {|name| return name}
raise ResolvError.new("no name for #{address}")
end
def getnames(address)
ret = []
each_name(address) {|name| ret << name}
return ret
end
def each_name(address)
yielded = false
@resolvers.each {|r|
r.each_name(address) {|name|
yield name.to_s
yielded = true
}
return if yielded
}
end
class ResolvError < StandardError
end
class ResolvTimeout < TimeoutError
end
class Hosts
DefaultFileName = '/etc/hosts'
def initialize(filename = DefaultFileName)
@filename = filename
@mutex = Mutex.new
@initialized = nil
end
def lazy_initialize
@mutex.synchronize {
unless @initialized
@name2addr = {}
@addr2name = {}
open(@filename) {|f|
f.each {|line|
line.sub!(/#.*/, '')
addr, hostname, *aliases = line.split(/\s+/)
next unless addr
@addr2name[addr] = [] unless @addr2name.include? addr
@addr2name[addr] << hostname
@addr2name[addr] += aliases
@name2addr[hostname] = [] unless @name2addr.include? hostname
@name2addr[hostname] << addr
aliases.each {|n|
@name2addr[n] = [] unless @name2addr.include? n
@name2addr[n] << addr
}
}
}
@name2addr.each {|name, arr| arr.reverse!}
@initialized = true
end
}
end
def getaddress(name)
each_address(name) {|address| return address}
raise ResolvError.new("#{@filename} has no name: #{name}")
end
def getaddresses(name)
ret = []
each_address(name) {|address| ret << address}
return ret
end
def each_address(name, &proc)
lazy_initialize
if @name2addr.include?(name)
@name2addr[name].each(&proc)
end
end
def getname(address)
each_name(address) {|name| return name}
raise ResolvError.new("#{@filename} has no address: #{address}")
end
def getnames(address)
ret = []
each_name(address) {|name| ret << name}
return ret
end
def each_name(address, &proc)
lazy_initialize
if @addr2name.include?(address)
@addr2name[address].each(&proc)
end
end
end
class DNS
# STD0013 (RFC 1035, etc.)
# ftp://ftp.isi.edu/in-notes/iana/assignments/dns-parameters
Port = 53
UDPSize = 512
def initialize(config="/etc/resolv.conf")
@mutex = Mutex.new
@config = Config.new(config)
@initialized = nil
end
def lazy_initialize
@mutex.synchronize {
unless @initialized
@config.lazy_initialize
if nameserver = @config.single?
@requester = Requester::ConnectedUDP.new(nameserver)
else
@requester = Requester::UnconnectedUDP.new
end
@initialized = true
end
}
end
def getaddress(name)
each_address(name) {|address| return address}
raise ResolvError.new("DNS result has no information for #{name}")
end
def getaddresses(name)
ret = []
each_address(name) {|address| ret << address}
return ret
end
def each_address(name)
each_resource(name, Resource::IN::A) {|resource| yield resource.address}
end
def getname(address)
each_name(address) {|name| return name}
raise ResolvError.new("DNS result has no information for #{address}")
end
def getnames(address)
ret = []
each_name(address) {|name| ret << name}
return ret
end
def each_name(address)
case address
when Name
ptr = address
when IPv4::Regex
ptr = IPv4.create(address).to_name
when IPv6::Regex
ptr = IPv6.create(address).to_name
else
raise ResolvError.new("cannot interpret as address: #{address}")
end
each_resource(ptr, Resource::IN::PTR) {|resource| yield resource.name}
end
def getresource(name, typeclass)
each_resource(name, typeclass) {|resource| return resource}
raise ResolvError.new("DNS result has no information for #{name}")
end
def getresources(name, typeclass)
ret = []
each_resource(name, typeclass) {|resource| ret << resource}
return ret
end
def each_resource(name, typeclass, &proc)
lazy_initialize
q = Queue.new
senders = {}
begin
@config.resolv(name) {|candidate, tout, nameserver|
msg = Message.new
msg.rd = 1
msg.add_question(candidate, typeclass)
unless sender = senders[[candidate, nameserver]]
sender = senders[[candidate, nameserver]] =
@requester.sender(msg, candidate, q, nameserver)
end
sender.send
reply = reply_name = nil
timeout(tout, ResolvTimeout) { reply, reply_name = q.pop }
case reply.rcode
when RCode::NoError
extract_resources(reply, reply_name, typeclass, &proc)
return
when RCode::NXDomain
raise Config::NXDomain.new(reply_name)
else
raise Config::OtherResolvError.new(reply_name)
end
}
ensure
@requester.delete(q)
end
end
def extract_resources(msg, name, typeclass)
if typeclass < Resource::ANY
n0 = Name.create(name)
msg.each_answer {|n, ttl, data|
yield data if n0 == n
}
end
yielded = false
n0 = Name.create(name)
msg.each_answer {|n, ttl, data|
if n0 == n
case data
when typeclass
yield data
yielded = true
when Resource::CNAME
n0 = data.name
end
end
}
return if yielded
msg.each_answer {|n, ttl, data|
if n0 == n
case data
when typeclass
yield data
end
end
}
end
class Requester
def initialize
@senders = {}
end
def delete(arg)
case arg
when Sender
@senders.delete_if {|k, s| s == arg }
when Queue
@senders.delete_if {|k, s| s.queue == arg }
else
raise ArgumentError.new("neither Sender or Queue: #{arg}")
end
end
class Sender
def initialize(data, queue)
@data = data
@queue = queue
end
attr_reader :queue
def recv(msg)
@queue.push([msg, @data])
end
end
class UnconnectedUDP < Requester
def initialize
super()
@sock = UDPSocket.new
@sock.fcntl(Fcntl::F_SETFD, 1)
@id = {}
@id.default = -1
@thread = Thread.new {
loop {
reply, from = @sock.recvfrom(UDPSize)
msg = begin
Message.decode(reply)
rescue DecodeError
STDERR.print("DNS message decoding error: #{reply.inspect}\n")
next
end
if s = @senders[[[from[3],from[1]],msg.id]]
s.recv msg
else
#STDERR.print("non-handled DNS message: #{msg.inspect} from #{from.inspect}\n")
end
}
}
end
def sender(msg, data, queue, host, port=Port)
service = [host, port]
id = Thread.exclusive {
@id[service] = (@id[service] + 1) & 0xffff
}
request = msg.encode
request[0,2] = [id].pack('n')
return @senders[[service, id]] =
Sender.new(request, data, @sock, host, port, queue)
end
class Sender < Requester::Sender
def initialize(msg, data, sock, host, port, queue)
super(data, queue)
@msg = msg
@sock = sock
@host = host
@port = port
end
def send
@sock.send(@msg, 0, @host, @port)
end
end
end
class ConnectedUDP < Requester
def initialize(host, port=Port)
super()
@host = host
@port = port
@sock = UDPSocket.new
@sock.connect(host, port)
@sock.fcntl(Fcntl::F_SETFD, 1)
@id = -1
@thread = Thread.new {
loop {
reply = @sock.recv(UDPSize)
msg = begin
Message.decode(reply)
rescue DecodeError
STDERR.print("DNS message decoding error: #{reply.inspect}")
next
end
if s = @senders[msg.id]
s.recv msg
else
#STDERR.print("non-handled DNS message: #{msg.inspect}")
end
}
}
end
def sender(msg, data, queue, host=@host, port=@port)
unless host == @host && port == @port
raise RequestError.new("host/port don't match: #{host}:#{port}")
end
id = Thread.exclusive { @id = (@id + 1) & 0xffff }
request = msg.encode
request[0,2] = [id].pack('n')
return @senders[id] = Sender.new(request, data, @sock, queue)
end
class Sender < Requester::Sender
def initialize(msg, data, sock, queue)
super(data, queue)
@msg = msg
@sock = sock
end
def send
@sock.send(@msg, 0)
end
end
end
class TCP < Requester
def initialize(host, port=Port)
super()
@host = host
@port = port
@sock = TCPSocket.new
@sock.connect(host, port)
@sock.fcntl(Fcntl::F_SETFD, 1)
@id = -1
@senders = {}
@thread = Thread.new {
loop {
len = @sock.read(2).unpack('n')
reply = @sock.read(len)
msg = begin
Message.decode(reply)
rescue DecodeError
STDERR.print("DNS message decoding error: #{reply.inspect}")
next
end
if s = @senders[msg.id]
s.push msg
else
#STDERR.print("non-handled DNS message: #{msg.inspect}")
end
}
}
end
def sender(msg, data, queue, host=@host, port=@port)
unless host == @host && port == @port
raise RequestError.new("host/port don't match: #{host}:#{port}")
end
id = Thread.exclusive { @id = (@id + 1) & 0xffff }
request = msg.encode
request[0,2] = [request.length, id].pack('nn')
return @senders[id] = Sender.new(request, data, @sock, queue)
end
class Sender < Requester::Sender
def initialize(msg, data, sock, queue)
super(data, queue)
@msg = msg
@sock = sock
end
def send
@sock.print(@msg)
@sock.flush
end
end
end
class RequestError < StandardError
end
end
class Config
def initialize(filename="/etc/resolv.conf")
@mutex = Mutex.new
@filename = filename
@initialized = nil
end
def lazy_initialize
@mutex.synchronize {
unless @initialized
@nameserver = []
@search = nil
@ndots = 1
begin
open(@filename) {|f|
f.each {|line|
line.sub!(/[#;].*/, '')
keyword, *args = line.split(/\s+/)
next unless keyword
case keyword
when 'nameserver'
@nameserver += args
when 'domain'
@search = [args[0]]
when 'search'
@search = args
end
}
}
rescue Errno::ENOENT
end
@nameserver = ['0.0.0.0'] if @nameserver.empty?
unless @search
hostname = Socket.gethostname
if /\./ =~ hostname
@search = [$']
else
@search = ['']
end
end
@initialized = true
end
}
end
def single?
lazy_initialize
if @nameserver.length == 1
return @nameserver[0]
else
return nil
end
end
def generate_candidates(name)
candidates = nil
name = name.to_s if Name === name
if /\.\z/ =~ name
candidates = [name]
elsif @ndots <= name.tr('^.', '').length
candidates = [name, *@search.collect {|domain| name + '.' + domain}]
else
candidates = [*@search.collect {|domain| name + '.' + domain}]
end
candidates.collect! {|c|
c = c.dup
c.gsub!(/\.\.+/, '.')
c.chomp!('.')
c
}
return candidates
end
InitialTimeout = 5
def generate_timeouts
ts = [InitialTimeout]
ts << ts[-1] * 2 / @nameserver.length
ts << ts[-1] * 2
ts << ts[-1] * 2
return ts
end
def resolv(name)
candidates = generate_candidates(name)
timeouts = generate_timeouts
begin
candidates.each {|candidate|
begin
timeouts.each {|tout|
@nameserver.each {|nameserver|
begin
yield candidate, tout, nameserver
rescue ResolvTimeout
end
}
}
raise ResolvError.new("DNS resolv timeout: #{name}")
rescue NXDomain
end
}
rescue OtherResolvError
raise ResolvError.new("DNS error: #{$!.message}")
end
raise ResolvError.new("DNS resolv error: #{name}")
end
class NXDomain < ResolvError
end
class OtherResolvError < ResolvError
end
end
module OpCode
Query = 0
IQuery = 1
Status = 2
Notify = 4
Update = 5
end
module RCode
NoError = 0
FormErr = 1
ServFail = 2
NXDomain = 3
NotImp = 4
Refused = 5
YXDomain = 6
YXRRSet = 7
NXRRSet = 8
NotAuth = 9
NotZone = 10
BADVERS = 16
BADSIG = 16
BADKEY = 17
BADTIME = 18
BADMODE = 19
BADNAME = 20
BADALG = 21
end
class DecodeError < StandardError
end
class EncodeError < StandardError
end
module Label
def self.split(arg)
labels = []
arg.scan(/[^\.]+/) {labels << Str.new($&)}
return labels
end
class Str
def initialize(string)
@string = string
@downcase = string.downcase
end
attr_reader :string, :downcase
def to_s
return @string
end
def inspect
return "#<#{self.class} #{self.to_s}>"
end
def ==(other)
return @downcase == other.downcase
end
def eql?(other)
return self == other
end
def hash
return @downcase.hash
end
end
end
class Name
def self.create(arg)
case arg
when Name
return arg
when String
return Name.new(Label.split(arg))
else
raise ArgumentError.new("cannot interprete as DNS name: #{arg.inspect}")
end
end
def initialize(labels)
@labels = labels
end
def ==(other)
return @labels == other.to_a
end
def eql?(other)
return self == other
end
def hash
return @labels.hash
end
def to_a
return @labels
end
def length
return @labels.length
end
def [](i)
return @labels[i]
end
def to_s
return @labels.join('.')
end
end
class Message
@@identifier = -1
def initialize(id = (@@identifier += 1) & 0xffff)
@id = id
@qr = 0
@opcode = 0
@aa = 0
@tc = 0
@rd = 0 # recursion desired
@ra = 0 # recursion available
@rcode = 0
@question = []
@answer = []
@authority = []
@additional = []
end
attr_accessor :id, :qr, :opcode, :aa, :tc, :rd, :ra, :rcode
attr_reader :question, :answer, :authority, :additional
def ==(other)
return @id == other.id &&
@qr == other.qr &&
@opcode == other.opcode &&
@aa == other.aa &&
@tc == other.tc &&
@rd == other.rd &&
@ra == other.ra &&
@rcode == other.rcode &&
@question == other.question &&
@answer == other.answer &&
@authority == other.authority &&
@additional == other.additional
end
def add_question(name, typeclass)
@question << [Name.create(name), typeclass]
end
def each_question
@question.each {|name, typeclass|
yield name, typeclass
}
end
def add_answer(name, ttl, data)
@answer << [Name.create(name), ttl, data]
end
def each_answer
@answer.each {|name, ttl, data|
yield name, ttl, data
}
end
def add_authority(name, ttl, data)
@authority << [Name.create(name), ttl, data]
end
def each_authority
@authority.each {|name, ttl, data|
yield name, ttl, data
}
end
def add_additional(name, ttl, data)
@additional << [Name.create(name), ttl, data]
end
def each_additional
@additional.each {|name, ttl, data|
yield name, ttl, data
}
end
def each_resource
each_answer {|name, ttl, data| yield name, ttl, data}
each_authority {|name, ttl, data| yield name, ttl, data}
each_additional {|name, ttl, data| yield name, ttl, data}
end
def encode
return MessageEncoder.new {|msg|
msg.put_pack('nnnnnn',
@id,
(@qr & 1) << 15 |
(@opcode & 15) << 11 |
(@aa & 1) << 10 |
(@tc & 1) << 9 |
(@rd & 1) << 8 |
(@ra & 1) << 7 |
(@rcode & 15),
@question.length,
@answer.length,
@authority.length,
@additional.length)
@question.each {|q|
name, typeclass = q
msg.put_name(name)
msg.put_pack('nn', typeclass::TypeValue, typeclass::ClassValue)
}
[@answer, @authority, @additional].each {|rr|
rr.each {|r|
name, ttl, data = r