Class: Resolv::DNS::Name

Inherits:
Object
  • Object
show all
Defined in:
lib/net/dns/resolv.rb,
lib/net/dns/resolvx.rb,
lib/net/dns/resolvx.rb

Overview

DNS names are hierarchical in a similar sense to ruby classes/modules, and the comparison operators are defined similarly to those of Module. A name is < another if it is a subdomain of it.

www.example.com < example.com # -> true
example.com < example.com # -> false
example.com <= example.com # -> true
com < example.com # -> false
bar.com < example.com # -> nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(labels, absolute = true) ⇒ Name

:nodoc:



1044
1045
1046
1047
# File 'lib/net/dns/resolv.rb', line 1044

def initialize(labels, absolute=true) # :nodoc:
  @labels = labels
  @absolute = absolute
end

Class Method Details

.create(arg) ⇒ Object

Create a Name from a Name (in which case arg is returned directly), or from a String (in which case new Name is returned).



1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
# File 'lib/net/dns/resolv.rb', line 1033

def self.create(arg)
  case arg
  when Name
    return arg
  when String
    return Name.new(Label.split(arg), /\.\z/ =~ arg ? true : false)
  else
    raise ArgumentError.new("cannot interpret as DNS name: #{arg.inspect}")
  end
end

Instance Method Details

#+(arg) ⇒ Object

Returns a new Name formed by concatenating self with arg. arg can be a String or a Name.



97
98
99
100
# File 'lib/net/dns/resolvx.rb', line 97

def +(arg)
  arg = Name.create(arg)
  Name.new(@labels + arg.to_a, arg.absolute?)
end

#<(name) ⇒ Object

Summary:

name < other   =>  true, false, or nil

Returns true if name is a subdomain of other. Returns nil if there’s no relationship between the two.



159
160
161
162
163
164
165
# File 'lib/net/dns/resolvx.rb', line 159

def <(name)
  n = Name.create(name)

  return nil unless self.related?(n)

  lt?(n)
end

#<<(arg) ⇒ Object

Append arg to this Name. arg can be a String or a Name.

Returns self.



88
89
90
91
92
93
# File 'lib/net/dns/resolvx.rb', line 88

def <<(arg)
  arg = Name.create(arg)
  @labels.concat(arg.to_a)
  @absolute = arg.absolute?
  self
end

#<=(name) ⇒ Object

Summary:

name <= other   =>  true, false, or nil

Returns true if name is a subdomain of other or is the same as other. Returns nil if there’s no relationship between the two.



183
184
185
186
# File 'lib/net/dns/resolvx.rb', line 183

def <=(name)
  n = Name.create(name)
  self.equal?(n) || self < n
end

#<=>(name) ⇒ Object

Summary:

name <=> other   => -1, 0, +1, nil

Returns -1 if name is a subdomain of other, 0 if name is the same as other, and +1 if other is a subdomain of name, or nil if name has no relationship with other.



205
206
207
208
209
210
211
212
213
214
# File 'lib/net/dns/resolvx.rb', line 205

def <=>(name)
  n = Name.create(name)

  return nil unless self.related?(n)

  return -1 if self.lt?(n)
  return +1 if n.lt?(self)
  # must be #equal?
  return  0
end

#==(other) ⇒ Object Also known as: eql?

Tests equivalence to other, a Name or a String.

Names are equivalent if their labels are equal (comparison is case-insensitive) and their “absoluteness” is equal.

p Name.create("example.COM")  == "EXAMPLE.com" => true
p Name.create("example.com.") == "example.com" => false


1071
1072
1073
1074
1075
# File 'lib/net/dns/resolv.rb', line 1071

def ==(other)
  other = Name.create(other)
  return false unless Name === other
  return @labels == other.to_a && @absolute == other.absolute?
end

#>(name) ⇒ Object

Summary:

name > other   =>  true, false, or nil

Same as other < name, see #<.



171
172
173
174
175
# File 'lib/net/dns/resolvx.rb', line 171

def >(name)
  n = Name.create(name)

  n < self
end

#>=(name) ⇒ Object

Summary:

name >= other   =>  true, false, or nil

Returns true if name is an ancestor of other, or the two DNS names are the same. Returns nil if there’s no relationship between the two.



194
195
196
197
# File 'lib/net/dns/resolvx.rb', line 194

def >=(name)
  n = Name.create(name)
  self.equal?(n) || self > n
end

#[](i) ⇒ Object

Returns the ith label.



1110
1111
1112
# File 'lib/net/dns/resolv.rb', line 1110

def [](i)
  return @labels[i]
end

#absolute=(abs) ⇒ Object

Set whether self is absolute or not. This is particularly useful when creating a Name from a String, since the trailing “.” is rarely used in string representations of domain names, even when the domain name is fully qualified. This makes them very difficult to compare to a Name returned from the DNS record decoders, because DNS names are always absolute.



108
109
110
# File 'lib/net/dns/resolvx.rb', line 108

def absolute=(abs)
  @absolute = abs ? true : false
end

#absolute?Boolean

Tests if self is absolute, i.e., had a trailing dot. For example:

example.com.

is absolute, whereas:

example.com

is not. Absolute names will never have the default search domains added to them during resolution.

Returns:

  • (Boolean)


1060
1061
1062
# File 'lib/net/dns/resolv.rb', line 1060

def absolute?
  return @absolute
end

#equal?(name) ⇒ Boolean

Returns whether two names are equal, disregarding the absolute? property of the names.

Note that this differs from #==, which does not consider two names equal if they differ in absoluteness.

Returns:

  • (Boolean)


117
118
119
120
121
# File 'lib/net/dns/resolvx.rb', line 117

def equal?(name)
  n = Name.create(name)

  @labels == n.to_a
end

#hashObject



1095
1096
1097
# File 'lib/net/dns/resolv.rb', line 1095

def hash
  return @labels.hash ^ @absolute.hash
end

#inspectObject



1049
1050
1051
# File 'lib/net/dns/resolv.rb', line 1049

def inspect
  "#<#{self.class}: #{self.to_s}#{@absolute ? '.' : ''}>"
end

#lengthObject

Returns the length of the array of labels.



1105
1106
1107
# File 'lib/net/dns/resolv.rb', line 1105

def length
  return @labels.length
end

#lt?(name) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
151
# File 'lib/net/dns/resolvx.rb', line 148

def lt?(name)
  n = Name.create(name)
  length > n.length && to_a[-n.length, n.length] == n.to_a
end

#related?(name) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
143
144
145
146
# File 'lib/net/dns/resolvx.rb', line 140

def related?(name)
  n = Name.create(name)

  l = length < n.length ? length : n.length

  @labels[-l, l] == n.to_a[-l, l]
end

#subdomain_of?(other) ⇒ Boolean

Tests subdomain-of relation.

domain = Resolv::DNS::Name.create("y.z")
p Resolv::DNS::Name.create("w.x.y.z").subdomain_of?(domain) #=> true
p Resolv::DNS::Name.create("x.y.z").subdomain_of?(domain) #=> true
p Resolv::DNS::Name.create("y.z").subdomain_of?(domain) #=> false
p Resolv::DNS::Name.create("z").subdomain_of?(domain) #=> false
p Resolv::DNS::Name.create("x.y.z.").subdomain_of?(domain) #=> false
p Resolv::DNS::Name.create("w.z").subdomain_of?(domain) #=> false

Returns:

  • (Boolean)


1088
1089
1090
1091
1092
1093
# File 'lib/net/dns/resolv.rb', line 1088

def subdomain_of?(other)
  other = Name.create(other)
  other_len = other.length
  return false if @labels.length <= other_len
  return @labels[-other_len, other_len] == other.to_a
end

#to_aObject

Returns the array of labels, each label is a Label::Str.



1100
1101
1102
# File 'lib/net/dns/resolv.rb', line 1100

def to_a
  return @labels
end

#to_sObject

Returns the domain name as a string.

The domain name doesn’t have a trailing dot even if the name object is absolute.

p Resolv::DNS::Name.create("x.y.z.").to_s #=> "x.y.z"
p Resolv::DNS::Name.create("x.y.z").to_s #=> "x.y.z"


1122
1123
1124
# File 'lib/net/dns/resolv.rb', line 1122

def to_s
  return @labels.join('.')
end