Class: Resolv::DNS::Name

Inherits:
Object
  • Object
show all
Defined in:
lib/resolv.rb

Overview

A representation of a DNS name.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(labels, absolute = true) ⇒ Name

:nodoc:



1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
# File 'lib/resolv.rb', line 1430

def initialize(labels, absolute=true) # :nodoc:
  labels = labels.map {|label|
    case label
    when String then Label::Str.new(label)
    when Label::Str then label
    else
      raise ArgumentError, "unexpected label: #{label.inspect}"
    end
  }
  @labels = labels
  @absolute = absolute
end

Class Method Details

.create(arg) ⇒ Object

Creates a new DNS name from arg. arg can be:

Name

returns arg.

String

Creates a new Name.



1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
# File 'lib/resolv.rb', line 1400

def self.create(arg)
  case arg
  when Name
    return arg
  when String
    # A hostname is runtime data rather than a programming mistake, so
    # both size limits surface as ResolvError to stay rescuable alongside
    # the rest of name resolution. The type check below is a caller
    # mistake and keeps raising ArgumentError.
    begin
      labels = Label.split(arg)
    rescue ArgumentError => e
      raise ResolvError.new(e.message)
    end
    # Label::Str enforces the per-label limit. Only the total is knowable
    # here, and it counts the encoded form, so size starts at 1 for the
    # root label's terminating zero octet. [RFC 1035 2.3.4, 3.1]
    size = 1
    labels.each do |label|
      size += 1 + label.string.bytesize
      if size > 255
        raise ResolvError.new("DNS name is too long (#{size} octets, max 255): #{arg.inspect}")
      end
    end
    return Name.new(labels, /\.\z/ =~ arg ? true : false)
  else
    raise ArgumentError.new("cannot interpret as DNS name: #{arg.inspect}")
  end
end

Instance Method Details

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

:nodoc:



1454
1455
1456
1457
1458
# File 'lib/resolv.rb', line 1454

def ==(other) # :nodoc:
  return false unless Name === other
  return false unless @absolute == other.absolute?
  return @labels == other.to_a
end

#[](i) ⇒ Object

:nodoc:



1496
1497
1498
# File 'lib/resolv.rb', line 1496

def [](i) # :nodoc:
  return @labels[i]
end

#absolute? ⇒ Boolean

True if this name is absolute.

Returns:

  • (Boolean)


1450
1451
1452
# File 'lib/resolv.rb', line 1450

def absolute?
  return @absolute
end

#hash ⇒ Object

:nodoc:



1484
1485
1486
# File 'lib/resolv.rb', line 1484

def hash # :nodoc:
  return @labels.hash ^ @absolute.hash
end

#inspect ⇒ Object

:nodoc:



1443
1444
1445
# File 'lib/resolv.rb', line 1443

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

#length ⇒ Object

:nodoc:



1492
1493
1494
# File 'lib/resolv.rb', line 1492

def length # :nodoc:
  return @labels.length
end

#subdomain_of?(other) ⇒ Boolean

Returns true if other is a subdomain.

Example:

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)

Raises:

  • (ArgumentError)


1476
1477
1478
1479
1480
1481
1482
# File 'lib/resolv.rb', line 1476

def subdomain_of?(other)
  raise ArgumentError, "not a domain name: #{other.inspect}" unless Name === other
  return false if @absolute != other.absolute?
  other_len = other.length
  return false if @labels.length <= other_len
  return @labels[-other_len, other_len] == other.to_a
end

#to_a ⇒ Object

:nodoc:



1488
1489
1490
# File 'lib/resolv.rb', line 1488

def to_a # :nodoc:
  return @labels
end

#to_s ⇒ Object

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::DNS::Name.create("x.y.z.").to_s #=> "x.y.z"
p Resolv::DNS::Name.create("x.y.z").to_s #=> "x.y.z"


1511
1512
1513
# File 'lib/resolv.rb', line 1511

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