123456789_123456789_123456789_123456789_123456789_

Class: Addrinfo

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::Data
Instance Chain:
self, ::Data
Inherits: Data
Defined in: ext/socket/raddrinfo.c,
ext/socket/lib/socket.rb

Overview

The Addrinfo class maps struct addrinfo to ruby. This structure identifies an Internet host and a service.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(sockaddr) ⇒ Addrinfo .new(sockaddr, family) ⇒ Addrinfo .new(sockaddr, family, socktype) ⇒ Addrinfo .new(sockaddr, family, socktype, protocol) ⇒ Addrinfo

returns a new instance of Addrinfo. The instance contains sockaddr, family, socktype, protocol. sockaddr means struct sockaddr which can be used for connect(2), etc. family, socktype and protocol are integers which is used for arguments of socket(2).

sockaddr is specified as an array or a string. The array should be compatible to the value of IPSocket#addr or UNIXSocket#addr. The string should be struct sockaddr as generated by Socket.sockaddr_in or Socket.unpack_sockaddr_un.

sockaddr examples:

  • “AF_INET”, 46102, “localhost.localdomain”, “127.0.0.1”
  • “AF_INET6”, 42304, “ip6-localhost”, “::1”
  • “AF_UNIX”, “/tmp/sock”
  • ::Socket.sockaddr_in(“smtp”, “2001:DB8::1”)

  • ::Socket.sockaddr_in(80, “172.18.22.42”)

  • ::Socket.sockaddr_in(80, “www.ruby-lang.org”)

  • ::Socket.sockaddr_un(“/tmp/sock”)

In an AF_INET/AF_INET6 sockaddr array, the 4th element, numeric IP address, is used to construct socket address in the Addrinfo instance. If the 3rd element, textual host name, is non-nil, it is also recorded but used only for #inspect.

family is specified as an integer to specify the protocol family such as Socket::PF_INET. It can be a symbol or a string which is the constant name with or without PF_ prefix such as :INET, :INET6, :UNIX, “PF_INET”, etc. If omitted, PF_UNSPEC is assumed.

socktype is specified as an integer to specify the socket type such as Socket::SOCK_STREAM. It can be a symbol or a string which is the constant name with or without SOCK_ prefix such as :STREAM, :DGRAM, :RAW, “SOCK_STREAM”, etc. If omitted, 0 is assumed.

protocol is specified as an integer to specify the protocol such as Socket::IPPROTO_TCP. It must be an integer, unlike family and socktype. If omitted, 0 is assumed. Note that 0 is reasonable value for most protocols, except raw socket.

Class Method Details

.foreach(nodename, service, family = nil, socktype = nil, protocol = nil, flags = nil, &block)

iterates over the list of Addrinfo objects obtained by .getaddrinfo.

Addrinfo.foreach(nil, 80) {|x| p x }
#=> #<Addrinfo: 127.0.0.1:80 TCP (:80)>
#   #<Addrinfo: 127.0.0.1:80 UDP (:80)>
#   #<Addrinfo: [::1]:80 TCP (:80)>
#   #<Addrinfo: [::1]:80 UDP (:80)>
[ GitHub ]

  
# File 'ext/socket/lib/socket.rb', line 231

def self.foreach(nodename, service, family=nil, socktype=nil, protocol=nil, flags=nil, &block)
  Addrinfo.getaddrinfo(nodename, service, family, socktype, protocol, flags).each(&block)
end

.getaddrinfo(nodename, service, family, socktype, protocol, flags) ⇒ Addrinfo, ... .getaddrinfo(nodename, service, family, socktype, protocol) ⇒ Addrinfo, ... .getaddrinfo(nodename, service, family, socktype) ⇒ Addrinfo, ... .getaddrinfo(nodename, service, family) ⇒ Addrinfo, ... .getaddrinfo(nodename, service) ⇒ Addrinfo, ...

returns a list of addrinfo objects as an array.

This method converts nodename (hostname) and service (port) to addrinfo. Since the conversion is not unique, the result is a list of addrinfo objects.

nodename or service can be nil if no conversion intended.

family, socktype and protocol are hint for preferred protocol. If the result will be used for a socket with SOCK_STREAM, SOCK_STREAM should be specified as socktype. If so, getaddrinfo returns addrinfo list appropriate for SOCK_STREAM. If they are omitted or nil is given, the result is not restricted.

Similarly, PF_INET6 as family restricts for IPv6.

flags should be bitwise OR of Socket::AI_??? constants such as follows. Note that the exact list of the constants depends on OS.

AI_PASSIVE      Get address to use with bind()
AI_CANONNAME    Fill in the canonical name
AI_NUMERICHOST  Prevent host name resolution
AI_NUMERICSERV  Prevent service name resolution
AI_V4MAPPED     Accept IPv4-mapped IPv6 addresses
AI_ALL          Allow all addresses
AI_ADDRCONFIG   Accept only if any address is assigned

Note that socktype should be specified whenever application knows the usage of the address. Some platform causes an error when socktype is omitted and servname is specified as an integer because some port numbers, 512 for example, are ambiguous without socktype.

Addrinfo.getaddrinfo("www.kame.net", 80, nil, :STREAM)
#=> [#<Addrinfo: 203.178.141.194:80 TCP (www.kame.net)>,
#    #<Addrinfo: [2001:200:dff:fff1:216:3eff:feb1:44d7]:80 TCP (www.kame.net)>]

.ip(host) ⇒ Addrinfo

returns an addrinfo object for IP address.

The port, socktype, protocol of the result is filled by zero. So, it is not appropriate to create a socket.

Addrinfo.ip("localhost") #=> #<Addrinfo: 127.0.0.1 (localhost)>

.tcp(host, port) ⇒ Addrinfo

returns an addrinfo object for TCP address.

Addrinfo.tcp("localhost", "smtp") #=> #<Addrinfo: 127.0.0.1:25 TCP (localhost:smtp)>

.udp(host, port) ⇒ Addrinfo

returns an addrinfo object for UDP address.

Addrinfo.udp("localhost", "daytime") #=> #<Addrinfo: 127.0.0.1:13 UDP (localhost:daytime)>

.unix(path [, socktype]) ⇒ Addrinfo

returns an addrinfo object for UNIX socket address.

socktype specifies the socket type. If it is omitted, :STREAM is used.

Addrinfo.unix("/tmp/sock")         #=> #<Addrinfo: /tmp/sock SOCK_STREAM>
Addrinfo.unix("/tmp/sock", :DGRAM) #=> #<Addrinfo: /tmp/sock SOCK_DGRAM>

Instance Attribute Details

#ip?Boolean (readonly)

returns true if addrinfo is internet (IPv4/IPv6) address. returns false otherwise.

Addrinfo.tcp("127.0.0.1", 80).ip? #=> true
Addrinfo.tcp("::1", 80).ip?       #=> true
Addrinfo.unix("/tmp/sock").ip?    #=> false

#ipv4?Boolean (readonly)

returns true if addrinfo is IPv4 address. returns false otherwise.

Addrinfo.tcp("127.0.0.1", 80).ipv4? #=> true
Addrinfo.tcp("::1", 80).ipv4?       #=> false
Addrinfo.unix("/tmp/sock").ipv4?    #=> false

#ipv4_loopback?Boolean (readonly)

Returns true for IPv4 loopback address (127.0.0.0/8). It returns false otherwise.

#ipv4_multicast?Boolean (readonly)

Returns true for IPv4 multicast address (224.0.0.0/4). It returns false otherwise.

#ipv4_private?Boolean (readonly)

Returns true for IPv4 private address (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16). It returns false otherwise.

#ipv6?Boolean (readonly)

returns true if addrinfo is IPv6 address. returns false otherwise.

Addrinfo.tcp("127.0.0.1", 80).ipv6? #=> false
Addrinfo.tcp("::1", 80).ipv6?       #=> true
Addrinfo.unix("/tmp/sock").ipv6?    #=> false

#ipv6_linklocal?Boolean (readonly)

Returns true for IPv6 link local address (ff80::/10). It returns false otherwise.

#ipv6_loopback?Boolean (readonly)

Returns true for IPv6 loopback address (::1). It returns false otherwise.

#ipv6_mc_global?Boolean (readonly)

Returns true for IPv6 multicast global scope address. It returns false otherwise.

#ipv6_mc_linklocal?Boolean (readonly)

Returns true for IPv6 multicast link-local scope address. It returns false otherwise.

#ipv6_mc_nodelocal?Boolean (readonly)

Returns true for IPv6 multicast node-local scope address. It returns false otherwise.

#ipv6_mc_orglocal?Boolean (readonly)

Returns true for IPv6 multicast organization-local scope address. It returns false otherwise.

#ipv6_mc_sitelocal?Boolean (readonly)

Returns true for IPv6 multicast site-local scope address. It returns false otherwise.

#ipv6_multicast?Boolean (readonly)

Returns true for IPv6 multicast address (ff00::/8). It returns false otherwise.

#ipv6_sitelocal?Boolean (readonly)

Returns true for IPv6 site local address (ffc0::/10). It returns false otherwise.

#ipv6_unique_local?Boolean (readonly)

Returns true for IPv6 unique local address (fc00::/7, RFC4193). It returns false otherwise.

#ipv6_unspecified?Boolean (readonly)

Returns true for IPv6 unspecified address (::). It returns false otherwise.

#ipv6_v4compat?Boolean (readonly)

Returns true for IPv4-compatible IPv6 address (::/80). It returns false otherwise.

#ipv6_v4mapped?Boolean (readonly)

Returns true for IPv4-mapped IPv6 address (::ffff:0:0/80). It returns false otherwise.

#unix?Boolean (readonly)

returns true if addrinfo is UNIX address. returns false otherwise.

Addrinfo.tcp("127.0.0.1", 80).unix? #=> false
Addrinfo.tcp("::1", 80).unix?       #=> false
Addrinfo.unix("/tmp/sock").unix?    #=> true

Instance Method Details

#afamilyInteger

returns the address family as an integer.

Addrinfo.tcp("localhost", 80).afamily == Socket::AF_INET #=> true

#bind

creates a socket bound to self.

If a block is given, it is called with the socket and the value of the block is returned. The socket is returned otherwise.

Addrinfo.udp("0.0.0.0", 9981).bind {|s|
  s.local_address.connect {|s| s.send "hello", 0 }
  p s.recv(10) #=> "hello"
}
[ GitHub ]

  
# File 'ext/socket/lib/socket.rb', line 179

def bind
  sock = Socket.new(self.pfamily, self.socktype, self.protocol)
  begin
    sock.ipv6only! if self.ipv6?
    sock.setsockopt(:SOCKET, :REUSEADDR, 1)
    sock.bind(self)
  rescue Exception
    sock.close
    raise
  end
  if block_given?
    begin
      yield sock
    ensure
      sock.close if !sock.closed?
    end
  else
    sock
  end
end

#canonnameString?

returns the canonical name as an string.

nil is returned if no canonical name.

The canonical name is set by .getaddrinfo when AI_CANONNAME is specified.

list = Addrinfo.getaddrinfo("www.ruby-lang.org", 80, :INET, :STREAM, nil, Socket::AI_CANONNAME)
p list[0] #=> #<Addrinfo: 221.186.184.68:80 TCP carbon.ruby-lang.org (www.ruby-lang.org)>
p list[0].canonname #=> "carbon.ruby-lang.org"

#connect([opts]) {|socket| ... } #connect([opts])

creates a socket connected to the address of self.

The optional argument opts is options represented by a hash. opts may have following options:

:timeout

specify the timeout in seconds.

If a block is given, it is called with the socket and the value of the block is returned. The socket is returned otherwise.

Addrinfo.tcp("www.ruby-lang.org", 80).connect {|s|
  s.print "GET / HTTP/1.0\r\nHost: www.ruby-lang.org\r\n\r\n"
  puts s.read
}
[ GitHub ]

  
# File 'ext/socket/lib/socket.rb', line 139

def connect(opts={}, &block)
  connect_internal(nil, opts[:timeout], &block)
end

#connect_from([local_addr_args], [opts]) {|socket| ... } #connect_from([local_addr_args], [opts])

creates a socket connected to the address of self.

If one or more arguments given as local_addr_args, it is used as the local address of the socket. local_addr_args is given for family_addrinfo to obtain actual address.

If local_addr_args is not given, the local address of the socket is not bound.

The optional last argument opts is options represented by a hash. opts may have following options:

:timeout

specify the timeout in seconds.

If a block is given, it is called with the socket and the value of the block is returned. The socket is returned otherwise.

Addrinfo.tcp("www.ruby-lang.org", 80).connect_from("0.0.0.0", 4649) {|s|
  s.print "GET / HTTP/1.0\r\nHost: www.ruby-lang.org\r\n\r\n"
  puts s.read
}

# Addrinfo object can be taken for the argument.
Addrinfo.tcp("www.ruby-lang.org", 80).connect_from(Addrinfo.tcp("0.0.0.0", 4649)) {|s|
  s.print "GET / HTTP/1.0\r\nHost: www.ruby-lang.org\r\n\r\n"
  puts s.read
}
[ GitHub ]

  
# File 'ext/socket/lib/socket.rb', line 114

def connect_from(*args, &block)
  opts = Hash === args.last ? args.pop : {}
  local_addr_args = args
  connect_internal(family_addrinfo(*local_addr_args), opts[:timeout], &block)
end

#connect_internal(local_addrinfo, timeout = nil) (private)

creates a new ::Socket connected to the address of local_addrinfo.

If local_addrinfo is nil, the address of the socket is not bound.

The timeout specify the seconds for timeout. Errno::ETIMEDOUT is raised when timeout occur.

If a block is given the created socket is yielded for each address.

[ GitHub ]

  
# File 'ext/socket/lib/socket.rb', line 47

def connect_internal(local_addrinfo, timeout=nil) # :yields: socket
  sock = Socket.new(self.pfamily, self.socktype, self.protocol)
  begin
    sock.ipv6only! if self.ipv6?
    sock.bind local_addrinfo if local_addrinfo
    if timeout
      begin
        sock.connect_nonblock(self)
      rescue IO::WaitWritable
        if !IO.select(nil, [sock], nil, timeout)
          raise Errno::ETIMEDOUT, 'user specified timeout'
        end
        begin
          sock.connect_nonblock(self) # check connection failure
        rescue Errno::EISCONN
        end
      end
    else
      sock.connect(self)
    end
  rescue Exception
    sock.close
    raise
  end
  if block_given?
    begin
      yield sock
    ensure
      sock.close if !sock.closed?
    end
  else
    sock
  end
end

#connect_to([remote_addr_args], [opts]) {|socket| ... } #connect_to([remote_addr_args], [opts])

creates a socket connected to remote_addr_args and bound to self.

The optional last argument opts is options represented by a hash. opts may have following options:

:timeout

specify the timeout in seconds.

If a block is given, it is called with the socket and the value of the block is returned. The socket is returned otherwise.

Addrinfo.tcp("0.0.0.0", 4649).connect_to("www.ruby-lang.org", 80) {|s|
  s.print "GET / HTTP/1.0\r\nHost: www.ruby-lang.org\r\n\r\n"
  puts s.read
}
[ GitHub ]

  
# File 'ext/socket/lib/socket.rb', line 162

def connect_to(*args, &block)
  opts = Hash === args.last ? args.pop : {}
  remote_addr_args = args
  remote_addrinfo = family_addrinfo(*remote_addr_args)
  remote_addrinfo.send(:connect_internal, self, opts[:timeout], &block)
end

#family_addrinfo(*args)

creates an Addrinfo object from the arguments.

The arguments are interpreted as similar to self.

Addrinfo.tcp("0.0.0.0", 4649).family_addrinfo("www.ruby-lang.org", 80)
#=> #<Addrinfo: 221.186.184.68:80 TCP (www.ruby-lang.org:80)>

Addrinfo.unix("/tmp/sock").family_addrinfo("/tmp/sock2")
#=> #<Addrinfo: /tmp/sock2 SOCK_STREAM>
[ GitHub ]

  
# File 'ext/socket/lib/socket.rb', line 14

def family_addrinfo(*args)
  if args.empty?
    raise ArgumentError, "no address specified"
  elsif Addrinfo === args.first
    raise ArgumentError, "too many arguments" if args.length != 1
    addrinfo = args.first
    if (self.pfamily != addrinfo.pfamily) ||
       (self.socktype != addrinfo.socktype)
      raise ArgumentError, "Addrinfo type mismatch"
    end
    addrinfo
  elsif self.ip?
    raise ArgumentError, "IP address needs host and port but #{args.length} arguments given" if args.length != 2
    host, port = args
    Addrinfo.getaddrinfo(host, port, self.pfamily, self.socktype, self.protocol)[0]
  elsif self.unix?
    raise ArgumentError, "UNIX socket needs single path argument but #{args.length} arguments given" if args.length != 1
    path, = args
    Addrinfo.unix(path)
  else
    raise ArgumentError, "unexpected family"
  end
end

#getnameinfoArray, service #getnameinfo(flags) ⇒ Array, service

returns nodename and service as a pair of strings. This converts struct sockaddr in addrinfo to textual representation.

flags should be bitwise OR of Socket::NI_??? constants.

Addrinfo.tcp("127.0.0.1", 80).getnameinfo #=> ["localhost", "www"]

Addrinfo.tcp("127.0.0.1", 80).getnameinfo(Socket::NI_NUMERICSERV)
#=> ["localhost", "80"]

#inspectString

returns a string which shows addrinfo in human-readable form.

Addrinfo.tcp("localhost", 80).inspect #=> "#<Addrinfo: 127.0.0.1:80 TCP (localhost)>"
Addrinfo.unix("/tmp/sock").inspect    #=> "#<Addrinfo: /tmp/sock SOCK_STREAM>"

#inspect_sockaddrString

returns a string which shows the sockaddr in addrinfo with human-readable form.

Addrinfo.tcp("localhost", 80).inspect_sockaddr     #=> "127.0.0.1:80"
Addrinfo.tcp("ip6-localhost", 80).inspect_sockaddr #=> "[::1]:80"
Addrinfo.unix("/tmp/sock").inspect_sockaddr        #=> "/tmp/sock"

#ip_addressString

Returns the IP address as a string.

Addrinfo.tcp("127.0.0.1", 80).ip_address    #=> "127.0.0.1"
Addrinfo.tcp("::1", 80).ip_address          #=> "::1"

#ip_portport

Returns the port number as an integer.

Addrinfo.tcp("127.0.0.1", 80).ip_port    #=> 80
Addrinfo.tcp("::1", 80).ip_port          #=> 80

#ip_unpackArray, port

Returns the IP address and port number as 2-element array.

Addrinfo.tcp("127.0.0.1", 80).ip_unpack    #=> ["127.0.0.1", 80]
Addrinfo.tcp("::1", 80).ip_unpack          #=> ["::1", 80]

#ipv6_to_ipv4

Returns IPv4 address of IPv4 mapped/compatible IPv6 address. It returns nil if self is not IPv4 mapped/compatible IPv6 address.

Addrinfo.ip("::192.0.2.3").ipv6_to_ipv4      #=> #<Addrinfo: 192.0.2.3>
Addrinfo.ip("::ffff:192.0.2.3").ipv6_to_ipv4 #=> #<Addrinfo: 192.0.2.3>
Addrinfo.ip("::1").ipv6_to_ipv4              #=> nil
Addrinfo.ip("192.0.2.3").ipv6_to_ipv4        #=> nil
Addrinfo.unix("/tmp/sock").ipv6_to_ipv4      #=> nil

#listen(backlog = Socket::SOMAXCONN)

creates a listening socket bound to self.

[ GitHub ]

  
# File 'ext/socket/lib/socket.rb', line 201

def listen(backlog=Socket::SOMAXCONN)
  sock = Socket.new(self.pfamily, self.socktype, self.protocol)
  begin
    sock.ipv6only! if self.ipv6?
    sock.setsockopt(:SOCKET, :REUSEADDR, 1)
    sock.bind(self)
    sock.listen(backlog)
  rescue Exception
    sock.close
    raise
  end
  if block_given?
    begin
      yield sock
    ensure
      sock.close if !sock.closed?
    end
  else
    sock
  end
end

#pfamilyInteger

returns the protocol family as an integer.

Addrinfo.tcp("localhost", 80).pfamily == Socket::PF_INET #=> true

#protocolInteger

returns the socket type as an integer.

Addrinfo.tcp("localhost", 80).protocol == Socket::IPPROTO_TCP #=> true

#socktypeInteger

returns the socket type as an integer.

Addrinfo.tcp("localhost", 80).socktype == Socket::SOCK_STREAM #=> true

#to_sockaddrString #to_sString
Also known as: #to_sockaddr

returns the socket address as packed struct sockaddr string.

Addrinfo.tcp("localhost", 80).to_sockaddr
#=> "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"

#to_sockaddrString #to_sString

Alias for #to_s.

#unix_pathpath

Returns the socket path as a string.

Addrinfo.unix("/tmp/sock").unix_path       #=> "/tmp/sock"