Class | Dnsruby::Name |
In: |
lib/Dnsruby/name.rb
|
Parent: | Object |
A representation of a DNS name (RFC1035, section 3.1)
MaxNameLength | = | 255 |
== | -> | eql? |
labels | [R] |
Creates a new Dnsruby::Name from arg. arg can be :
# File lib/Dnsruby/name.rb, line 43 43: def self.create(arg) 44: case arg 45: when Name 46: return Name.new(arg.labels, arg.absolute?) 47: when String 48: # arg.gsub!(/\.$/o, "") 49: if (arg==".") 50: return Name.new([],true) 51: end 52: if (arg=="") 53: return Name.new([],false) 54: end 55: return Name.new(split_escaped(arg), /\.\z/ =~ arg ? true : false) 56: # return Name.new(Label.split(arg), /\.\z/ =~ arg ? true : false) 57: when Array 58: return Name.new(arg, /\.\z/ =~ (arg.last ? ((arg.last.kind_of?String)?arg.last : arg.last.string) : arg.last) ? true : false) 59: else 60: raise ArgumentError.new("cannot interpret as DNS name: #{arg.inspect}") 61: end 62: end
# File lib/Dnsruby/name.rb, line 69 69: def self.split(name) 70: encodedlabels = name2encodedlabels(name) 71: labels = encodedlabels.each {|el| Name.decode(el.to_s)} 72: return labels 73: end
# File lib/Dnsruby/name.rb, line 135 135: def <=>(other) 136: # return -1 if other less than us, +1 if greater than us 137: return 0 if (canonical == other.canonical) 138: if (canonically_before(other)) 139: return +1 140: end 141: return -1 142: end
# File lib/Dnsruby/name.rb, line 144 144: def canonically_before(n) 145: if (!(Name === n)) 146: n = Name.create(n) 147: end 148: # Work out whether this name is canonically before the passed Name 149: # RFC 4034 section 6.1 150: # For the purposes of DNS security, owner names are ordered by treating 151: #individual labels as unsigned left-justified octet strings. The 152: #absence of a octet sorts before a zero value octet, and uppercase 153: #US-ASCII letters are treated as if they were lowercase US-ASCII 154: #letters. 155: #To compute the canonical ordering of a set of DNS names, start by 156: #sorting the names according to their most significant (rightmost) 157: #labels. For names in which the most significant label is identical, 158: #continue sorting according to their next most significant label, and 159: #so forth. 160: 161: # Get the list of labels for both names, and then swap them 162: my_labels = @labels.reverse 163: other_labels = n.labels.reverse 164: my_labels.each_index {|i| 165: if (!other_labels[i]) 166: return false 167: end 168: next if (other_labels[i].downcase == my_labels[i].downcase) 169: return (my_labels[i].downcase < other_labels[i].downcase) 170: } 171: return true 172: end
# File lib/Dnsruby/name.rb, line 94 94: def downcase 95: labels = [] 96: @labels.each do |label| labels << Label.new(label.downcase) end 97: return Name.new(labels) 98: end
Tests subdomain-of relation : returns true if this name is a subdomain of other.
domain = Resolv::Name.create("y.z") p Resolv::Name.create("w.x.y.z").subdomain_of?(domain) #=> true p Resolv::Name.create("x.y.z").subdomain_of?(domain) #=> true p Resolv::Name.create("y.z").subdomain_of?(domain) #=> false p Resolv::Name.create("z").subdomain_of?(domain) #=> false p Resolv::Name.create("x.y.z.").subdomain_of?(domain) #=> false p Resolv::Name.create("w.z").subdomain_of?(domain) #=> false
# File lib/Dnsruby/name.rb, line 190 190: def subdomain_of?(other) 191: raise ArgumentError, "not a domain name: #{other.inspect}" unless Name === other 192: return false if @absolute != other.absolute? 193: other_len = other.length 194: return false if @labels.length <= other_len 195: return @labels[-other_len, other_len] == other.to_a 196: end
returns the domain name as a string.
The domain name doesn‘t have a trailing dot even if the name object is absolute.
Example :
p Resolv::Name.create("x.y.z.").to_s #=> "x.y.z" p Resolv::Name.create("x.y.z").to_s #=> "x.y.z"
# File lib/Dnsruby/name.rb, line 224 224: def to_s 225: return to_str(@labels) 226: end