pNet-DNS 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +68 -0
- data/lib/Net/DNS.rb +879 -0
- data/lib/Net/DNS/Header.rb +303 -0
- data/lib/Net/DNS/Nameserver.rb +601 -0
- data/lib/Net/DNS/Packet.rb +851 -0
- data/lib/Net/DNS/Question.rb +117 -0
- data/lib/Net/DNS/RR.rb +630 -0
- data/lib/Net/DNS/RR/A.rb +103 -0
- data/lib/Net/DNS/RR/AAAA.rb +147 -0
- data/lib/Net/DNS/RR/AFSDB.rb +114 -0
- data/lib/Net/DNS/RR/CERT.rb +191 -0
- data/lib/Net/DNS/RR/CNAME.rb +89 -0
- data/lib/Net/DNS/RR/DNAME.rb +84 -0
- data/lib/Net/DNS/RR/EID.rb +70 -0
- data/lib/Net/DNS/RR/HINFO.rb +108 -0
- data/lib/Net/DNS/RR/ISDN.rb +118 -0
- data/lib/Net/DNS/RR/LOC.rb +341 -0
- data/lib/Net/DNS/RR/MB.rb +92 -0
- data/lib/Net/DNS/RR/MG.rb +96 -0
- data/lib/Net/DNS/RR/MINFO.rb +109 -0
- data/lib/Net/DNS/RR/MR.rb +92 -0
- data/lib/Net/DNS/RR/MX.rb +124 -0
- data/lib/Net/DNS/RR/NAPTR.rb +182 -0
- data/lib/Net/DNS/RR/NIMLOC.rb +70 -0
- data/lib/Net/DNS/RR/NS.rb +100 -0
- data/lib/Net/DNS/RR/NSAP.rb +273 -0
- data/lib/Net/DNS/RR/NULL.rb +68 -0
- data/lib/Net/DNS/RR/OPT.rb +251 -0
- data/lib/Net/DNS/RR/PTR.rb +93 -0
- data/lib/Net/DNS/RR/PX.rb +131 -0
- data/lib/Net/DNS/RR/RP.rb +108 -0
- data/lib/Net/DNS/RR/RT.rb +115 -0
- data/lib/Net/DNS/RR/SOA.rb +195 -0
- data/lib/Net/DNS/RR/SPF.rb +46 -0
- data/lib/Net/DNS/RR/SRV.rb +153 -0
- data/lib/Net/DNS/RR/SSHFP.rb +190 -0
- data/lib/Net/DNS/RR/TKEY.rb +219 -0
- data/lib/Net/DNS/RR/TSIG.rb +358 -0
- data/lib/Net/DNS/RR/TXT.rb +162 -0
- data/lib/Net/DNS/RR/UNKNOWN.rb +76 -0
- data/lib/Net/DNS/RR/X25.rb +90 -0
- data/lib/Net/DNS/Resolver.rb +2090 -0
- data/lib/Net/DNS/Resolver/Recurse.rb +478 -0
- data/lib/Net/DNS/Update.rb +189 -0
- data/test/custom.txt +4 -0
- data/test/resolv.conf +4 -0
- data/test/tc_escapedchars.rb +498 -0
- data/test/tc_header.rb +91 -0
- data/test/tc_inet6.rb +169 -0
- data/test/tc_misc.rb +137 -0
- data/test/tc_online.rb +236 -0
- data/test/tc_packet.rb +174 -0
- data/test/tc_packet_unique_push.rb +126 -0
- data/test/tc_question.rb +49 -0
- data/test/tc_recurse.rb +69 -0
- data/test/tc_res_env.rb +59 -0
- data/test/tc_res_file.rb +55 -0
- data/test/tc_res_opt.rb +135 -0
- data/test/tc_resolver.rb +102 -0
- data/test/tc_rr-opt.rb +40 -0
- data/test/tc_rr-rrsort.rb +116 -0
- data/test/tc_rr-txt.rb +138 -0
- data/test/tc_rr-unknown.rb +95 -0
- data/test/tc_rr.rb +246 -0
- data/test/tc_tcp.rb +34 -0
- data/test/tc_tkey.rb +115 -0
- data/test/tc_update.rb +226 -0
- data/test/ts_netdns.rb +17 -0
- data/test/ts_offline.rb +32 -0
- data/test/ts_online.rb +33 -0
- metadata +119 -0
data/lib/Net/DNS/RR/A.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# The contents of this file are subject to the Mozilla
|
2
|
+
# Public Licence Version 1.1 (the "Licence"); you may
|
3
|
+
# not use this file except in compliance with the
|
4
|
+
# Licence. You may obtain a copy of the Licence at
|
5
|
+
# http://www.mozilla.org/MPL
|
6
|
+
# Software distributed under the Licence is distributed
|
7
|
+
# on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
|
8
|
+
# either express or implied. See the Licence of the
|
9
|
+
# specific language governing rights and limitations
|
10
|
+
# under the Licence.
|
11
|
+
# The Original Code is pNet::DNS.
|
12
|
+
# The Initial Developer of the Original Code is
|
13
|
+
# Nominet UK (www.nominet.org.uk). Portions created by
|
14
|
+
# Nominet UK are Copyright (c) Nominet UK 2006.
|
15
|
+
# All rights reserved.
|
16
|
+
module Net
|
17
|
+
module DNS
|
18
|
+
class RR
|
19
|
+
#= NAME
|
20
|
+
#
|
21
|
+
#Net::DNS::RR::A - DNS A resource record
|
22
|
+
#
|
23
|
+
#= DESCRIPTION
|
24
|
+
#
|
25
|
+
#Class for DNS Address (A) resource records.
|
26
|
+
#
|
27
|
+
#=head1 COPYRIGHT
|
28
|
+
#
|
29
|
+
#Copyright (c) 1997-2002 Michael Fuhr.
|
30
|
+
#
|
31
|
+
#Portions Copyright (c) 2002-2004 Chris Reinhardt.
|
32
|
+
#
|
33
|
+
#All rights reserved. This program is free software; you may redistribute
|
34
|
+
#it and/or modify it under the same terms as Perl itself.
|
35
|
+
#
|
36
|
+
#=head1 SEE ALSO
|
37
|
+
#
|
38
|
+
#Net::DNS, Net::DNS::Resolver, Net::DNS::Packet,
|
39
|
+
#Net::DNS::Header, Net::DNS::Question, Net::DNS::RR,
|
40
|
+
#RFC 1035 Section 3.4.1
|
41
|
+
class A < RR
|
42
|
+
#Returns the RR's address field.
|
43
|
+
#
|
44
|
+
# print "address = ", rr.address, "\n"
|
45
|
+
#
|
46
|
+
attr_accessor :address
|
47
|
+
def inet_aton ip
|
48
|
+
A.inet_aton ip
|
49
|
+
end
|
50
|
+
def A.inet_aton ip
|
51
|
+
ret = ip.split(/\./).map{|c| c.to_i}.pack("C*") # .unpack("N").first
|
52
|
+
return ret
|
53
|
+
end
|
54
|
+
def inet_ntoa n
|
55
|
+
A.inet_ntoa n
|
56
|
+
end
|
57
|
+
def A.inet_ntoa n
|
58
|
+
ret= n.unpack("C*").join "."
|
59
|
+
return ret
|
60
|
+
end
|
61
|
+
def new_from_string(string)
|
62
|
+
if (string && (string =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)\s*$/o))
|
63
|
+
a = $1.to_i
|
64
|
+
b = $2.to_i
|
65
|
+
if (a >= 0) && (a <= 255) && (b >= 0) && (b <= 255)
|
66
|
+
c = $3.to_i
|
67
|
+
d = $4.to_i
|
68
|
+
if (c >= 0) && (c <= 255) && (d >= 0) && (d <= 255)
|
69
|
+
|
70
|
+
@address = "#{a}.#{b}.#{c}.#{d}";
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
def new_from_data(data, offset)
|
76
|
+
if (@rdlength > 0)
|
77
|
+
@address = inet_ntoa(data[offset, 4]);
|
78
|
+
# @address = IPAddr.new_ntoh(data[offset, 4])
|
79
|
+
# IPAddr.new(data[offset, 4], Socket::AF_INET).to_s
|
80
|
+
end
|
81
|
+
end
|
82
|
+
def new_from_hash(values)
|
83
|
+
if (values.has_key?(:address))
|
84
|
+
@address=values[:address]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def rdatastr
|
89
|
+
return @address || '';
|
90
|
+
end
|
91
|
+
|
92
|
+
def rr_rdata(*args)
|
93
|
+
if (defined?@address)
|
94
|
+
return inet_aton(@address)
|
95
|
+
else
|
96
|
+
return ""
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
# The contents of this file are subject to the Mozilla
|
2
|
+
# Public Licence Version 1.1 (the "Licence"); you may
|
3
|
+
# not use this file except in compliance with the
|
4
|
+
# Licence. You may obtain a copy of the Licence at
|
5
|
+
# http://www.mozilla.org/MPL
|
6
|
+
# Software distributed under the Licence is distributed
|
7
|
+
# on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
|
8
|
+
# either express or implied. See the Licence of the
|
9
|
+
# specific language governing rights and limitations
|
10
|
+
# under the Licence.
|
11
|
+
# The Original Code is pNet::DNS.
|
12
|
+
# The Initial Developer of the Original Code is
|
13
|
+
# Nominet UK (www.nominet.org.uk). Portions created by
|
14
|
+
# Nominet UK are Copyright (c) Nominet UK 2006.
|
15
|
+
# All rights reserved.
|
16
|
+
module Net
|
17
|
+
module DNS
|
18
|
+
class RR
|
19
|
+
#= NAME
|
20
|
+
#
|
21
|
+
#Net::DNS::RR::AAAA - DNS AAAA resource record
|
22
|
+
#
|
23
|
+
#= DESCRIPTION
|
24
|
+
#
|
25
|
+
#Class for DNS IPv6 Address (AAAA) resource records.
|
26
|
+
#
|
27
|
+
#= BUGS
|
28
|
+
#
|
29
|
+
#The inspect method returns only the preferred method of address
|
30
|
+
#representation ("x:x:x:x:x:x:x:x", as documented in RFC 1884,
|
31
|
+
#Section 2.2, Para 1).
|
32
|
+
#
|
33
|
+
#= COPYRIGHT
|
34
|
+
#
|
35
|
+
#Copyright (c) 1997-2002 Michael Fuhr.
|
36
|
+
#
|
37
|
+
#Portions Copyright (c) 2002-2004 Chris Reinhardt.
|
38
|
+
#
|
39
|
+
#Ruby version Copyright (c) 2006 AlexD (Nominet UK)
|
40
|
+
#
|
41
|
+
#All rights reserved. This program is free software; you may redistribute
|
42
|
+
#it and/or modify it under the same terms as Perl itself.
|
43
|
+
#
|
44
|
+
#= SEE ALSO
|
45
|
+
#
|
46
|
+
#Net::DNS, Net::DNS::Resolver, Net::DNS::Packet,
|
47
|
+
#Net::DNS::Header, Net::DNS::Question, Net::DNS::RR,
|
48
|
+
#RFC 1886 Section 2, RFC 1884 Sections 2.2 & 2.4.4
|
49
|
+
class AAAA < RR
|
50
|
+
#Returns the RR's address field.
|
51
|
+
#
|
52
|
+
# print "address = ", rr.address, "\n"
|
53
|
+
#
|
54
|
+
attr_accessor :address
|
55
|
+
def new_from_data(data, offset)
|
56
|
+
if (@rdlength > 0)
|
57
|
+
addr = data.unpack("\@#{offset} n8");
|
58
|
+
# @address = sprintf("%x:%x:%x:%x:%x:%x:%x:%x", addr);
|
59
|
+
@address=""
|
60
|
+
addr.each { |x| @address += sprintf("%x:", x) }
|
61
|
+
@address = @address[0, @address.length-1]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def new_from_string(string)
|
66
|
+
if (string)
|
67
|
+
# I think this is correct, per RFC 1884 Sections 2.2 & 2.4.4.
|
68
|
+
if (string =~ /^(.*):(\d+)\.(\d+)\.(\d+)\.(\d+)$/)
|
69
|
+
# my ($front, $a, $b, $c, $d) = ($1, $2, $3, $4, $5);
|
70
|
+
front = $1
|
71
|
+
a = $2.to_1
|
72
|
+
b = $3.to_i
|
73
|
+
c = $4.to_i
|
74
|
+
d = $5.to_i
|
75
|
+
string = front + sprintf(":%x:%x",(a << 8 | b),(c << 8 | d));
|
76
|
+
end
|
77
|
+
|
78
|
+
if (string =~ /^(.*)::(.*)$/)
|
79
|
+
# my ($front, $back) = ($1, $2);
|
80
|
+
front = $1
|
81
|
+
back = $2
|
82
|
+
front = front.split(/:/)
|
83
|
+
back = back.split(/:/)
|
84
|
+
fill = 8 - (front ? front.length + 1 : 0)- (back ? back.length + 1 : 0);
|
85
|
+
middle = []
|
86
|
+
fill.times {middle.push("0")}
|
87
|
+
addr = front + middle + back
|
88
|
+
else
|
89
|
+
addr = string.split(/:/);
|
90
|
+
if (addr.length < 8)
|
91
|
+
(8 - addr.length).times {addr.insert(0,"0")}
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
@address = ""
|
96
|
+
addr.each {|a| @address += sprintf("%x:", a.to_i(16))}
|
97
|
+
# remove last ':'
|
98
|
+
@address= @address[0, @address.length-1]
|
99
|
+
|
100
|
+
# sprintf("%x:%x:%x:%x:%x:%x:%x:%x", addr.map { |a| a.to_i(16) });
|
101
|
+
# $self->{"address"} = sprintf("%x:%x:%x:%x:%x:%x:%x:%x", map { hex $_ } @addr);
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def new_from_hash(values)
|
106
|
+
if (values.has_key?(:address))
|
107
|
+
@address=values[:address]
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def rdatastr
|
112
|
+
return @address || '';
|
113
|
+
end
|
114
|
+
|
115
|
+
def rr_rdata(*args)
|
116
|
+
rdata = "";
|
117
|
+
|
118
|
+
if (defined?@address)
|
119
|
+
addr = @address.split(/:/)
|
120
|
+
rdata += addr.map {|a| a.to_i(16) }.pack("n8");
|
121
|
+
end
|
122
|
+
return rdata;
|
123
|
+
end
|
124
|
+
|
125
|
+
@@Regex = nil
|
126
|
+
def AAAA.init_regex
|
127
|
+
if (@@Regex == nil)
|
128
|
+
@@Regex_8Hex = /\A (?:[0-9A-Fa-f]{1,4}:){7} [0-9A-Fa-f]{1,4} \z/x # IPv6 address format a:b:c:d:e:f:g:h
|
129
|
+
@@Regex_CompressedHex = /\A ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) :: ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) \z/x # Compressed IPv6 address format a::b
|
130
|
+
@@Regex_6Hex4Dec = /\A ((?:[0-9A-Fa-f]{1,4}:){6,6}) (\d+)\.(\d+)\.(\d+)\.(\d+) \z/x # IPv4 mapped IPv6 address format a:b:c:d:e:f:w.x.y.z
|
131
|
+
@@Regex_CompressedHex4Dec = /\A ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) :: ((?:[0-9A-Fa-f]{1,4}:)*) (\d+)\.(\d+)\.(\d+)\.(\d+) \z/x # Compressed IPv4 mapped IPv6 address format a::b:w.x.y.z
|
132
|
+
@@Regex = / (?:#{@@Regex_8Hex}) | (?:#{@@Regex_CompressedHex}) | (?:#{@@Regex_6Hex4Dec}) | (?:#{@@Regex_CompressedHex4Dec})/x
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def AAAA.is_valid(a)
|
137
|
+
init_regex
|
138
|
+
if a =~ @@Regex
|
139
|
+
return true
|
140
|
+
else
|
141
|
+
return false
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# The contents of this file are subject to the Mozilla
|
2
|
+
# Public Licence Version 1.1 (the "Licence"); you may
|
3
|
+
# not use this file except in compliance with the
|
4
|
+
# Licence. You may obtain a copy of the Licence at
|
5
|
+
# http://www.mozilla.org/MPL
|
6
|
+
# Software distributed under the Licence is distributed
|
7
|
+
# on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
|
8
|
+
# either express or implied. See the Licence of the
|
9
|
+
# specific language governing rights and limitations
|
10
|
+
# under the Licence.
|
11
|
+
# The Original Code is pNet::DNS.
|
12
|
+
# The Initial Developer of the Original Code is
|
13
|
+
# Nominet UK (www.nominet.org.uk). Portions created by
|
14
|
+
# Nominet UK are Copyright (c) Nominet UK 2006.
|
15
|
+
# All rights reserved.
|
16
|
+
module Net
|
17
|
+
module DNS
|
18
|
+
class RR
|
19
|
+
#= NAME
|
20
|
+
#
|
21
|
+
#Net::DNS::RR::AFSDB - DNS AFSDB resource record
|
22
|
+
#
|
23
|
+
#= DESCRIPTION
|
24
|
+
#
|
25
|
+
#Class for DNS AFS Data Base (AFSDB) resource records.
|
26
|
+
#
|
27
|
+
#= COPYRIGHT
|
28
|
+
#
|
29
|
+
#Copyright (c) 1997-2002 Michael Fuhr.
|
30
|
+
#
|
31
|
+
#Portions Copyright (c) 2002-2004 Chris Reinhardt.
|
32
|
+
#
|
33
|
+
#All rights reserved. This program is free software; you may redistribute
|
34
|
+
#it and/or modify it under the same terms as Perl itself.
|
35
|
+
#
|
36
|
+
#= SEE ALSO
|
37
|
+
#
|
38
|
+
#Net::DNS, Net::DNS::Resolver, Net::DNS::Packet,
|
39
|
+
#Net::DNS::Header, Net::DNS::Question, Net::DNS::RR,
|
40
|
+
#RFC 1183 Section 1
|
41
|
+
class AFSDB < RR
|
42
|
+
#Returns the RR's subtype field. Use of the subtype field is documented
|
43
|
+
#in RFC 1183.
|
44
|
+
#
|
45
|
+
# print "subtype = ", rr.subtype, "\n"
|
46
|
+
#
|
47
|
+
attr_accessor :subtype
|
48
|
+
|
49
|
+
#Returns the RR's hostname field. See RFC 1183.
|
50
|
+
#
|
51
|
+
# print "hostname = ", rr.hostname, "\n"
|
52
|
+
#
|
53
|
+
attr_accessor :hostname
|
54
|
+
def new_from_data (data, offset)
|
55
|
+
if (@rdlength > 0)
|
56
|
+
subtype = data.unpack("\@#{offset} n")[0];
|
57
|
+
offset += Net::DNS::INT16SZ;
|
58
|
+
hostname = Net::DNS::Packet::dn_expand(data, offset)[0];
|
59
|
+
@subtype = subtype;
|
60
|
+
@hostname = hostname;
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def new_from_string(string)
|
65
|
+
if (string!=nil && (string =~ /^(\d+)\s+(\S+)$/o))
|
66
|
+
@subtype = $1;
|
67
|
+
@hostname = $2;
|
68
|
+
@hostname.sub!(/\.+$/o,"");
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def new_from_hash(values)
|
73
|
+
if (values.has_key?(:subtype))
|
74
|
+
@subtype=values[:subtype]
|
75
|
+
end
|
76
|
+
if (values.has_key?(:hostname))
|
77
|
+
@hostname=values[:hostname]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def rdatastr
|
82
|
+
if defined?@subtype
|
83
|
+
return "#{@subtype} #{@hostname}."
|
84
|
+
else
|
85
|
+
return '';
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def rr_rdata (packet, offset)
|
90
|
+
rdata = "";
|
91
|
+
|
92
|
+
if (defined?@subtype)
|
93
|
+
rdata += [@subtype].pack("n");
|
94
|
+
rdata += packet.dn_comp(@hostname, offset + rdata.length);
|
95
|
+
end
|
96
|
+
|
97
|
+
return rdata;
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
def _canonicalRdata
|
103
|
+
# rdata contains a compressed domainname... we should not have that.
|
104
|
+
rdata="";
|
105
|
+
if (defined?@subtype)
|
106
|
+
rdata += @subtype.pack("n");
|
107
|
+
rdata += _name2wire(@hostname);
|
108
|
+
end
|
109
|
+
return rdata;
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,191 @@
|
|
1
|
+
# The contents of this file are subject to the Mozilla
|
2
|
+
# Public Licence Version 1.1 (the "Licence"); you may
|
3
|
+
# not use this file except in compliance with the
|
4
|
+
# Licence. You may obtain a copy of the Licence at
|
5
|
+
# http://www.mozilla.org/MPL
|
6
|
+
# Software distributed under the Licence is distributed
|
7
|
+
# on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
|
8
|
+
# either express or implied. See the Licence of the
|
9
|
+
# specific language governing rights and limitations
|
10
|
+
# under the Licence.
|
11
|
+
# The Original Code is pNet::DNS.
|
12
|
+
# The Initial Developer of the Original Code is
|
13
|
+
# Nominet UK (www.nominet.org.uk). Portions created by
|
14
|
+
# Nominet UK are Copyright (c) Nominet UK 2006.
|
15
|
+
# All rights reserved.
|
16
|
+
require 'base64'
|
17
|
+
module Net
|
18
|
+
module DNS
|
19
|
+
class RR
|
20
|
+
#= NAME
|
21
|
+
#
|
22
|
+
#Net::DNS::RR::CERT - DNS CERT resource record
|
23
|
+
#
|
24
|
+
#= DESCRIPTION
|
25
|
+
#
|
26
|
+
#Class for DNS Certificate (CERT) resource records. (see RFC 2538)
|
27
|
+
#
|
28
|
+
#= COPYRIGHT
|
29
|
+
#
|
30
|
+
#Copyright (c) 1997-2002 Michael Fuhr.
|
31
|
+
#
|
32
|
+
#Portions Copyright (c) 2002-2004 Chris Reinhardt.
|
33
|
+
#
|
34
|
+
#Ruby version Copyright (c) 2006 AlexD (Nominet UK)
|
35
|
+
#
|
36
|
+
#All rights reserved. This program is free software; you may redistribute
|
37
|
+
#it and/or modify it under the same terms as Perl itself.
|
38
|
+
#
|
39
|
+
#= SEE ALSO
|
40
|
+
#
|
41
|
+
#Net::DNS, Net::DNS::Resolver, Net::DNS::Packet,
|
42
|
+
#Net::DNS::Header, Net::DNS::Question, Net::DNS::RR,
|
43
|
+
#RFC 2782
|
44
|
+
class CERT < RR
|
45
|
+
#Returns the format code for the certificate (in numeric form)
|
46
|
+
#
|
47
|
+
# print "format = ", rr.format, "\n"
|
48
|
+
#
|
49
|
+
attr_accessor :format
|
50
|
+
#Returns the key tag for the public key in the certificate
|
51
|
+
#
|
52
|
+
# print "tag = ", rr.tag, "\n"
|
53
|
+
#
|
54
|
+
attr_accessor :tag
|
55
|
+
#Returns the algorithm used by the certificate (in numeric form)
|
56
|
+
#
|
57
|
+
# print "algorithm = ", rr.algorithm, "\n"
|
58
|
+
#
|
59
|
+
attr_accessor :algorithm
|
60
|
+
#Returns the data comprising the certificate itself (in raw binary form)
|
61
|
+
#
|
62
|
+
# print "certificate = ", rr.certificate, "\n"
|
63
|
+
#
|
64
|
+
attr_accessor :certificate
|
65
|
+
FORMATS = {
|
66
|
+
'PKIX' => 1,
|
67
|
+
'SPKI' => 2,
|
68
|
+
'PGP' => 3,
|
69
|
+
'URI' => 253,
|
70
|
+
'OID' => 254,
|
71
|
+
}
|
72
|
+
|
73
|
+
R_FORMATS = FORMATS.invert
|
74
|
+
|
75
|
+
ALGORITHMS = {
|
76
|
+
'RSAMD5' => 1,
|
77
|
+
'DH' => 2,
|
78
|
+
'DSA' => 3,
|
79
|
+
'ECC' => 4,
|
80
|
+
'INDIRECT' => 252,
|
81
|
+
'PRIVATEDNS' => 253,
|
82
|
+
'PRIVATEOID' => 254,
|
83
|
+
}
|
84
|
+
|
85
|
+
R_ALGORITHMS = ALGORITHMS.invert;
|
86
|
+
|
87
|
+
def new_from_data(data, offset)
|
88
|
+
if (@rdlength > 0)
|
89
|
+
format, tag, algorithm = data.unpack("\@#{offset} n2C");
|
90
|
+
|
91
|
+
offset += 2 * Net::DNS::INT16SZ + 1;
|
92
|
+
|
93
|
+
length = @rdlength - (2 * Net::DNS::INT16SZ + 1);
|
94
|
+
certificate = data[offset, length];
|
95
|
+
|
96
|
+
@format = format;
|
97
|
+
@tag = tag;
|
98
|
+
@algorithm = algorithm;
|
99
|
+
@certificate = certificate;
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def new_from_hash(values)
|
104
|
+
if values.has_key?(:format)
|
105
|
+
@format = values[:format]
|
106
|
+
end
|
107
|
+
if values.has_key?(:tag)
|
108
|
+
@tag = values[:tag]
|
109
|
+
end
|
110
|
+
if values.has_key?(:algorithm)
|
111
|
+
@algorithm = values[:algorithm]
|
112
|
+
end
|
113
|
+
if values.has_key?(:certificate)
|
114
|
+
@certificate = values[:certificate]
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def new_from_string(string)
|
119
|
+
if (string==nil)
|
120
|
+
return
|
121
|
+
end
|
122
|
+
|
123
|
+
format, tag, algorithm, rest = string.split(" ")
|
124
|
+
if (rest == nil)
|
125
|
+
return
|
126
|
+
end
|
127
|
+
|
128
|
+
# look up mnemonics
|
129
|
+
# the "die"s may be rash, but proceeding would be dangerous
|
130
|
+
if (algorithm =~ /\D/)
|
131
|
+
if defined?ALGORITHMS[algorithm]
|
132
|
+
algorithm = ALGORITHMS[algorithm]
|
133
|
+
else
|
134
|
+
raise RuntimeError, "Unknown algorithm mnemonic: '#{algorithm}'"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
if (format =~ /\D/)
|
139
|
+
if defined?FORMATS[format]
|
140
|
+
format = FORMATS[format]
|
141
|
+
else
|
142
|
+
die "Unknown format mnemonic: '#{format}'"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
@format = format;
|
147
|
+
@tag = tag;
|
148
|
+
@algorithm = algorithm;
|
149
|
+
@certificate = Base64::decode64([rest].join(''));
|
150
|
+
end
|
151
|
+
|
152
|
+
def rdatastr
|
153
|
+
rdatastr=""
|
154
|
+
|
155
|
+
if (defined?@format)
|
156
|
+
cert = Base64::encode64 @certificate;
|
157
|
+
cert.gsub!(/\n/,"");
|
158
|
+
|
159
|
+
format = @format
|
160
|
+
if defined?R_FORMATS[@format]
|
161
|
+
format = R_FORMATS[@format]
|
162
|
+
end
|
163
|
+
|
164
|
+
algorithm = @algorithm;
|
165
|
+
if defined?R_ALGORITHMS[@algorithm]
|
166
|
+
algorithm = R_ALGORITHMS[@algorithm]
|
167
|
+
end
|
168
|
+
|
169
|
+
rdatastr = "#{format} #{@tag} #{algorithm} #{cert}";
|
170
|
+
else
|
171
|
+
rdatastr = '';
|
172
|
+
end
|
173
|
+
|
174
|
+
return rdatastr;
|
175
|
+
end
|
176
|
+
|
177
|
+
def rr_rdata(packet, offset)
|
178
|
+
rdata = "";
|
179
|
+
|
180
|
+
if (defined?@format)
|
181
|
+
rdata += [@format, @tag].pack("n2")
|
182
|
+
rdata += [@algorithm].pack("C")
|
183
|
+
rdata += @certificate
|
184
|
+
end
|
185
|
+
|
186
|
+
return rdata;
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|