class Addrinfo
TheAddrinfo class mapsstruct addrinfo to ruby. This structure identifies an Internet host and a service.
Public Class Methods
Source
# File ext/socket/lib/socket.rb, line 230defself.foreach(nodename,service,family=nil,socktype=nil,protocol=nil,flags=nil,timeout:nil,&block)Addrinfo.getaddrinfo(nodename,service,family,socktype,protocol,flags,timeout:timeout).each(&block)end
iterates over the list ofAddrinfo objects obtained byAddrinfo.getaddrinfo.
Addrinfo.foreach(nil,80) {|x|px }#=> #<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)>
Source
static VALUEaddrinfo_s_getaddrinfo(int argc, VALUE *argv, VALUE self){ VALUE node, service, family, socktype, protocol, flags, opts, timeout; rb_scan_args(argc, argv, "24:", &node, &service, &family, &socktype, &protocol, &flags, &opts); rb_get_kwargs(opts, &id_timeout, 0, 1, &timeout); if (timeout == Qundef) { timeout = Qnil; } return addrinfo_list_new(node, service, family, socktype, protocol, flags, timeout);}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,Addrinfo.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 nameAI_NUMERICHOST Prevent host name resolutionAI_NUMERICSERV Prevent service name resolutionAI_V4MAPPED Accept IPv4-mapped IPv6 addressesAI_ALL Allow all addressesAI_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)>]
Source
static VALUEaddrinfo_s_ip(VALUE self, VALUE host){ VALUE ret; rb_addrinfo_t *rai; ret = addrinfo_firstonly_new(host, Qnil, INT2NUM(PF_UNSPEC), INT2FIX(0), INT2FIX(0), INT2FIX(0)); rai = get_addrinfo(ret); rai->socktype = 0; rai->protocol = 0; return ret;}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)>
Source
static VALUEaddrinfo_initialize(int argc, VALUE *argv, VALUE self){ rb_addrinfo_t *rai; VALUE sockaddr_arg, sockaddr_ary, pfamily, socktype, protocol; int i_pfamily, i_socktype, i_protocol; struct sockaddr *sockaddr_ptr; socklen_t sockaddr_len; VALUE canonname = Qnil, inspectname = Qnil; if (check_addrinfo(self)) rb_raise(rb_eTypeError, "already initialized socket address"); DATA_PTR(self) = rai = alloc_addrinfo(); rb_scan_args(argc, argv, "13", &sockaddr_arg, &pfamily, &socktype, &protocol); i_pfamily = NIL_P(pfamily) ? PF_UNSPEC : rsock_family_arg(pfamily); i_socktype = NIL_P(socktype) ? 0 : rsock_socktype_arg(socktype); i_protocol = NIL_P(protocol) ? 0 : NUM2INT(protocol); sockaddr_ary = rb_check_array_type(sockaddr_arg); if (!NIL_P(sockaddr_ary)) { VALUE afamily = rb_ary_entry(sockaddr_ary, 0); int af; StringValue(afamily); if (rsock_family_to_int(RSTRING_PTR(afamily), RSTRING_LEN(afamily), &af) == -1) rb_raise(rb_eSocket, "unknown address family: %s", StringValueCStr(afamily)); switch (af) { case AF_INET: /* ["AF_INET", 46102, "localhost.localdomain", "127.0.0.1"] */#ifdef INET6 case AF_INET6: /* ["AF_INET6", 42304, "ip6-localhost", "::1"] */#endif { VALUE service = rb_ary_entry(sockaddr_ary, 1); VALUE nodename = rb_ary_entry(sockaddr_ary, 2); VALUE numericnode = rb_ary_entry(sockaddr_ary, 3); int flags; service = INT2NUM(NUM2INT(service)); if (!NIL_P(nodename)) StringValue(nodename); StringValue(numericnode); flags = AI_NUMERICHOST;#ifdef AI_NUMERICSERV flags |= AI_NUMERICSERV;#endif init_addrinfo_getaddrinfo(self, rai, numericnode, service, INT2NUM(i_pfamily ? i_pfamily : af), INT2NUM(i_socktype), INT2NUM(i_protocol), INT2NUM(flags), nodename, service); break; }#ifdef HAVE_TYPE_STRUCT_SOCKADDR_UN case AF_UNIX: /* ["AF_UNIX", "/tmp/sock"] */ { VALUE path = rb_ary_entry(sockaddr_ary, 1); StringValue(path); init_unix_addrinfo(self, rai, path, SOCK_STREAM); break; }#endif default: rb_raise(rb_eSocket, "unexpected address family"); } } else { StringValue(sockaddr_arg); sockaddr_ptr = (struct sockaddr *)RSTRING_PTR(sockaddr_arg); sockaddr_len = RSTRING_SOCKLEN(sockaddr_arg); init_addrinfo(self, rai, sockaddr_ptr, sockaddr_len, i_pfamily, i_socktype, i_protocol, canonname, inspectname); } return self;}returns a new instance ofAddrinfo. 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 ofIPSocket#addr orUNIXSocket#addr. The string should be struct sockaddr as generated bySocket.sockaddr_in orSocket.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 theAddrinfo instance. If the 3rd element, textual host name, is non-nil, it is also recorded but used only forAddrinfo#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.
Source
static VALUEaddrinfo_s_tcp(VALUE self, VALUE host, VALUE port){ return addrinfo_firstonly_new(host, port, INT2NUM(PF_UNSPEC), INT2NUM(SOCK_STREAM), INT2NUM(IPPROTO_TCP), INT2FIX(0));}returns an addrinfo object for TCP address.
Addrinfo.tcp("localhost","smtp")#=> #<Addrinfo: 127.0.0.1:25 TCP (localhost:smtp)>
Source
static VALUEaddrinfo_s_udp(VALUE self, VALUE host, VALUE port){ return addrinfo_firstonly_new(host, port, INT2NUM(PF_UNSPEC), INT2NUM(SOCK_DGRAM), INT2NUM(IPPROTO_UDP), INT2FIX(0));}returns an addrinfo object for UDP address.
Addrinfo.udp("localhost","daytime")#=> #<Addrinfo: 127.0.0.1:13 UDP (localhost:daytime)>
Source
static VALUEaddrinfo_s_unix(int argc, VALUE *argv, VALUE self){ VALUE path, vsocktype, addr; int socktype; rb_addrinfo_t *rai; rb_scan_args(argc, argv, "11", &path, &vsocktype); if (NIL_P(vsocktype)) socktype = SOCK_STREAM; else socktype = rsock_socktype_arg(vsocktype); addr = addrinfo_s_allocate(rb_cAddrinfo); DATA_PTR(addr) = rai = alloc_addrinfo(); init_unix_addrinfo(self, rai, path, socktype); return addr;}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>
Public Instance Methods
Source
static VALUEaddrinfo_afamily(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); return INT2NUM(ai_get_afamily(rai));}returns the address family as an integer.
Addrinfo.tcp("localhost",80).afamily==Socket::AF_INET#=> true
Source
# File ext/socket/lib/socket.rb, line 178defbindsock =Socket.new(self.pfamily,self.socktype,self.protocol)beginsock.ipv6only!ifself.ipv6?sock.setsockopt(:SOCKET,:REUSEADDR,1)sock.bind(self)rescueExceptionsock.closeraiseendifblock_given?beginyieldsockensuresock.closeendelsesockendend
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 }ps.recv(10)#=> "hello"}
Source
static VALUEaddrinfo_canonname(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); return rai->canonname;}returns the canonical name as a string.
nil is returned if no canonical name.
The canonical name is set byAddrinfo.getaddrinfo when AI_CANONNAME is specified.
list =Addrinfo.getaddrinfo("www.ruby-lang.org",80,:INET,:STREAM,nil,Socket::AI_CANONNAME)plist[0]#=> #<Addrinfo: 221.186.184.68:80 TCP carbon.ruby-lang.org (www.ruby-lang.org)>plist[0].canonname#=> "carbon.ruby-lang.org"
Source
# File ext/socket/lib/socket.rb, line 140defconnect(timeout:nil,&block)connect_internal(nil,timeout,&block)end
creates a socket connected to the address of self.
The optional argumentopts 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"putss.read}
Source
# File ext/socket/lib/socket.rb, line 117defconnect_from(*args,timeout:nil,&block)connect_internal(family_addrinfo(*args),timeout,&block)end
creates a socket connected to the address of self.
If one or more arguments given aslocal_addr_args, it is used as the local address of the socket.local_addr_args is given forfamily_addrinfo to obtain actual address.
Iflocal_addr_args is not given, the local address of the socket is not bound.
The optional last argumentopts 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"putss.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"putss.read}
Source
# File ext/socket/lib/socket.rb, line 163defconnect_to(*args,timeout:nil,&block)remote_addrinfo =family_addrinfo(*args)remote_addrinfo.connect_internal(self,timeout,&block)end
creates a socket connected toremote_addr_args and bound to self.
The optional last argumentopts 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"putss.read}
Source
# File ext/socket/lib/socket.rb, line 21deffamily_addrinfo(*args)ifargs.empty?raiseArgumentError,"no address specified"elsifAddrinfo===args.firstraiseArgumentError,"too many arguments"ifargs.length!=1addrinfo =args.firstif (self.pfamily!=addrinfo.pfamily)|| (self.socktype!=addrinfo.socktype)raiseArgumentError,"Addrinfo type mismatch"endaddrinfoelsifself.ip?raiseArgumentError,"IP address needs host and port but #{args.length} arguments given"ifargs.length!=2host,port =argsAddrinfo.getaddrinfo(host,port,self.pfamily,self.socktype,self.protocol)[0]elsifself.unix?raiseArgumentError,"UNIX socket needs single path argument but #{args.length} arguments given"ifargs.length!=1path, =argsAddrinfo.unix(path)elseraiseArgumentError,"unexpected family"endend
creates anAddrinfo 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>
Source
static VALUEaddrinfo_getnameinfo(int argc, VALUE *argv, VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); VALUE vflags; char hbuf[1024], pbuf[1024]; int flags, error; rb_scan_args(argc, argv, "01", &vflags); flags = NIL_P(vflags) ? 0 : NUM2INT(vflags); if (rai->socktype == SOCK_DGRAM) flags |= NI_DGRAM; error = rb_getnameinfo(&rai->addr.addr, rai->sockaddr_len, hbuf, (socklen_t)sizeof(hbuf), pbuf, (socklen_t)sizeof(pbuf), flags); if (error) { rsock_raise_resolution_error("getnameinfo", error); } return rb_assoc_new(rb_str_new2(hbuf), rb_str_new2(pbuf));}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"]
Source
static VALUEaddrinfo_inspect(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); int internet_p; VALUE ret; ret = rb_sprintf("#<%s: ", rb_obj_classname(self)); inspect_sockaddr(self, ret); if (rai->pfamily && ai_get_afamily(rai) != rai->pfamily) { ID id = rsock_intern_protocol_family(rai->pfamily); if (id) rb_str_catf(ret, " %s", rb_id2name(id)); else rb_str_catf(ret, " PF_\?\?\?(%d)", rai->pfamily); } internet_p = rai->pfamily == PF_INET;#ifdef INET6 internet_p = internet_p || rai->pfamily == PF_INET6;#endif if (internet_p && rai->socktype == SOCK_STREAM && (rai->protocol == 0 || rai->protocol == IPPROTO_TCP)) { rb_str_cat2(ret, " TCP"); } else if (internet_p && rai->socktype == SOCK_DGRAM && (rai->protocol == 0 || rai->protocol == IPPROTO_UDP)) { rb_str_cat2(ret, " UDP"); } else { if (rai->socktype) { ID id = rsock_intern_socktype(rai->socktype); if (id) rb_str_catf(ret, " %s", rb_id2name(id)); else rb_str_catf(ret, " SOCK_\?\?\?(%d)", rai->socktype); } if (rai->protocol) { if (internet_p) { ID id = rsock_intern_ipproto(rai->protocol); if (id) rb_str_catf(ret, " %s", rb_id2name(id)); else goto unknown_protocol; } else { unknown_protocol: rb_str_catf(ret, " UNKNOWN_PROTOCOL(%d)", rai->protocol); } } } if (!NIL_P(rai->canonname)) { VALUE name = rai->canonname; rb_str_catf(ret, " %s", StringValueCStr(name)); } if (!NIL_P(rai->inspectname)) { VALUE name = rai->inspectname; rb_str_catf(ret, " (%s)", StringValueCStr(name)); } rb_str_buf_cat2(ret, ">"); return ret;}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>"
Source
VALUErsock_addrinfo_inspect_sockaddr(VALUE self){ return inspect_sockaddr(self, rb_str_new("", 0));}returns a string which shows the sockaddr inaddrinfo 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"
Source
static VALUEaddrinfo_ip_p(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); int family = ai_get_afamily(rai); return IS_IP_FAMILY(family) ? Qtrue : Qfalse;}returns true if addrinfo is internet (IPv4/IPv6) address. returns false otherwise.
Addrinfo.tcp("127.0.0.1",80).ip?#=> trueAddrinfo.tcp("::1",80).ip?#=> trueAddrinfo.unix("/tmp/sock").ip?#=> false
Source
static VALUEaddrinfo_ip_address(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); int family = ai_get_afamily(rai); VALUE vflags; VALUE ret; if (!IS_IP_FAMILY(family)) rb_raise(rb_eSocket, "need IPv4 or IPv6 address"); vflags = INT2NUM(NI_NUMERICHOST|NI_NUMERICSERV); ret = addrinfo_getnameinfo(1, &vflags, self); return rb_ary_entry(ret, 0);}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"
Source
static VALUEaddrinfo_ip_port(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); int family = ai_get_afamily(rai); int port; if (!IS_IP_FAMILY(family)) { bad_family:#ifdef AF_INET6 rb_raise(rb_eSocket, "need IPv4 or IPv6 address");#else rb_raise(rb_eSocket, "need IPv4 address");#endif } switch (family) { case AF_INET: if (rai->sockaddr_len != sizeof(struct sockaddr_in)) rb_raise(rb_eSocket, "unexpected sockaddr size for IPv4"); port = ntohs(rai->addr.in.sin_port); break;#ifdef AF_INET6 case AF_INET6: if (rai->sockaddr_len != sizeof(struct sockaddr_in6)) rb_raise(rb_eSocket, "unexpected sockaddr size for IPv6"); port = ntohs(rai->addr.in6.sin6_port); break;#endif default: goto bad_family; } return INT2NUM(port);}Returns the port number as an integer.
Addrinfo.tcp("127.0.0.1",80).ip_port#=> 80Addrinfo.tcp("::1",80).ip_port#=> 80
Source
static VALUEaddrinfo_ip_unpack(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); int family = ai_get_afamily(rai); VALUE vflags; VALUE ret, portstr; if (!IS_IP_FAMILY(family)) rb_raise(rb_eSocket, "need IPv4 or IPv6 address"); vflags = INT2NUM(NI_NUMERICHOST|NI_NUMERICSERV); ret = addrinfo_getnameinfo(1, &vflags, self); portstr = rb_ary_entry(ret, 1); rb_ary_store(ret, 1, INT2NUM(atoi(StringValueCStr(portstr)))); return ret;}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]
Source
static VALUEaddrinfo_ipv4_p(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); return ai_get_afamily(rai) == AF_INET ? Qtrue : Qfalse;}returns true if addrinfo is IPv4 address. returns false otherwise.
Addrinfo.tcp("127.0.0.1",80).ipv4?#=> trueAddrinfo.tcp("::1",80).ipv4?#=> falseAddrinfo.unix("/tmp/sock").ipv4?#=> false
Source
static VALUEaddrinfo_ipv4_loopback_p(VALUE self){ uint32_t a; if (!extract_in_addr(self, &a)) return Qfalse; if ((a & 0xff000000) == 0x7f000000) /* 127.0.0.0/8 */ return Qtrue; return Qfalse;}Returns true for IPv4 loopback address (127.0.0.0/8). It returns false otherwise.
Source
static VALUEaddrinfo_ipv4_multicast_p(VALUE self){ uint32_t a; if (!extract_in_addr(self, &a)) return Qfalse; if ((a & 0xf0000000) == 0xe0000000) /* 224.0.0.0/4 */ return Qtrue; return Qfalse;}Returns true for IPv4 multicast address (224.0.0.0/4). It returns false otherwise.
Source
static VALUEaddrinfo_ipv4_private_p(VALUE self){ uint32_t a; if (!extract_in_addr(self, &a)) return Qfalse; if ((a & 0xff000000) == 0x0a000000 || /* 10.0.0.0/8 */ (a & 0xfff00000) == 0xac100000 || /* 172.16.0.0/12 */ (a & 0xffff0000) == 0xc0a80000) /* 192.168.0.0/16 */ return Qtrue; return Qfalse;}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.
Source
static VALUEaddrinfo_ipv6_p(VALUE self){#ifdef AF_INET6 rb_addrinfo_t *rai = get_addrinfo(self); return ai_get_afamily(rai) == AF_INET6 ? Qtrue : Qfalse;#else return Qfalse;#endif}returns true if addrinfo is IPv6 address. returns false otherwise.
Addrinfo.tcp("127.0.0.1",80).ipv6?#=> falseAddrinfo.tcp("::1",80).ipv6?#=> trueAddrinfo.unix("/tmp/sock").ipv6?#=> false
Source
static VALUEaddrinfo_ipv6_linklocal_p(VALUE self){ struct in6_addr *addr = extract_in6_addr(self); if (addr && IN6_IS_ADDR_LINKLOCAL(addr)) return Qtrue; return Qfalse;}Returns true for IPv6 link local address (fe80::/10). It returns false otherwise.
Source
static VALUEaddrinfo_ipv6_loopback_p(VALUE self){ struct in6_addr *addr = extract_in6_addr(self); if (addr && IN6_IS_ADDR_LOOPBACK(addr)) return Qtrue; return Qfalse;}Returns true for IPv6 loopback address (::1). It returns false otherwise.
Source
static VALUEaddrinfo_ipv6_mc_global_p(VALUE self){ struct in6_addr *addr = extract_in6_addr(self); if (addr && IN6_IS_ADDR_MC_GLOBAL(addr)) return Qtrue; return Qfalse;}Returns true for IPv6 multicast global scope address. It returns false otherwise.
Source
static VALUEaddrinfo_ipv6_mc_linklocal_p(VALUE self){ struct in6_addr *addr = extract_in6_addr(self); if (addr && IN6_IS_ADDR_MC_LINKLOCAL(addr)) return Qtrue; return Qfalse;}Returns true for IPv6 multicast link-local scope address. It returns false otherwise.
Source
static VALUEaddrinfo_ipv6_mc_nodelocal_p(VALUE self){ struct in6_addr *addr = extract_in6_addr(self); if (addr && IN6_IS_ADDR_MC_NODELOCAL(addr)) return Qtrue; return Qfalse;}Returns true for IPv6 multicast node-local scope address. It returns false otherwise.
Source
static VALUEaddrinfo_ipv6_mc_orglocal_p(VALUE self){ struct in6_addr *addr = extract_in6_addr(self); if (addr && IN6_IS_ADDR_MC_ORGLOCAL(addr)) return Qtrue; return Qfalse;}Returns true for IPv6 multicast organization-local scope address. It returns false otherwise.
Source
static VALUEaddrinfo_ipv6_mc_sitelocal_p(VALUE self){ struct in6_addr *addr = extract_in6_addr(self); if (addr && IN6_IS_ADDR_MC_SITELOCAL(addr)) return Qtrue; return Qfalse;}Returns true for IPv6 multicast site-local scope address. It returns false otherwise.
Source
static VALUEaddrinfo_ipv6_multicast_p(VALUE self){ struct in6_addr *addr = extract_in6_addr(self); if (addr && IN6_IS_ADDR_MULTICAST(addr)) return Qtrue; return Qfalse;}Returns true for IPv6 multicast address (ff00::/8). It returns false otherwise.
Source
static VALUEaddrinfo_ipv6_sitelocal_p(VALUE self){ struct in6_addr *addr = extract_in6_addr(self); if (addr && IN6_IS_ADDR_SITELOCAL(addr)) return Qtrue; return Qfalse;}Returns true for IPv6 site local address (fec0::/10). It returns false otherwise.
Source
static VALUEaddrinfo_ipv6_to_ipv4(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); struct in6_addr *addr; int family = ai_get_afamily(rai); if (family != AF_INET6) return Qnil; addr = &rai->addr.in6.sin6_addr; if (IN6_IS_ADDR_V4MAPPED(addr) || IN6_IS_ADDR_V4COMPAT(addr)) { struct sockaddr_in sin4; INIT_SOCKADDR_IN(&sin4, sizeof(sin4)); memcpy(&sin4.sin_addr, (char*)addr + sizeof(*addr) - sizeof(sin4.sin_addr), sizeof(sin4.sin_addr)); return rsock_addrinfo_new((struct sockaddr *)&sin4, (socklen_t)sizeof(sin4), PF_INET, rai->socktype, rai->protocol, rai->canonname, rai->inspectname); } else { return Qnil; }}Returns IPv4 address of IPv4 mapped/compatible IPv6 address. It returns nil ifself 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#=> nilAddrinfo.ip("192.0.2.3").ipv6_to_ipv4#=> nilAddrinfo.unix("/tmp/sock").ipv6_to_ipv4#=> nil
Source
static VALUEaddrinfo_ipv6_unique_local_p(VALUE self){ struct in6_addr *addr = extract_in6_addr(self); if (addr && IN6_IS_ADDR_UNIQUE_LOCAL(addr)) return Qtrue; return Qfalse;}Returns true for IPv6 unique local address (fc00::/7, RFC4193). It returns false otherwise.
Source
static VALUEaddrinfo_ipv6_unspecified_p(VALUE self){ struct in6_addr *addr = extract_in6_addr(self); if (addr && IN6_IS_ADDR_UNSPECIFIED(addr)) return Qtrue; return Qfalse;}Returns true for IPv6 unspecified address (::). It returns false otherwise.
Source
static VALUEaddrinfo_ipv6_v4compat_p(VALUE self){ struct in6_addr *addr = extract_in6_addr(self); if (addr && IN6_IS_ADDR_V4COMPAT(addr)) return Qtrue; return Qfalse;}Returns true for IPv4-compatible IPv6 address (::/80). It returns false otherwise.
Source
static VALUEaddrinfo_ipv6_v4mapped_p(VALUE self){ struct in6_addr *addr = extract_in6_addr(self); if (addr && IN6_IS_ADDR_V4MAPPED(addr)) return Qtrue; return Qfalse;}Returns true for IPv4-mapped IPv6 address (::ffff:0:0/80). It returns false otherwise.
Source
# File ext/socket/lib/socket.rb, line 200deflisten(backlog=Socket::SOMAXCONN)sock =Socket.new(self.pfamily,self.socktype,self.protocol)beginsock.ipv6only!ifself.ipv6?sock.setsockopt(:SOCKET,:REUSEADDR,1)unlessself.pfamily==Socket::PF_UNIXsock.bind(self)sock.listen(backlog)rescueExceptionsock.closeraiseendifblock_given?beginyieldsockensuresock.closeendelsesockendend
creates a listening socket bound to self.
Source
static VALUEaddrinfo_pfamily(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); return INT2NUM(rai->pfamily);}returns the protocol family as an integer.
Addrinfo.tcp("localhost",80).pfamily==Socket::PF_INET#=> true
Source
static VALUEaddrinfo_protocol(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); return INT2NUM(rai->protocol);}returns the socket type as an integer.
Addrinfo.tcp("localhost",80).protocol==Socket::IPPROTO_TCP#=> true
Source
static VALUEaddrinfo_socktype(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); return INT2NUM(rai->socktype);}returns the socket type as an integer.
Addrinfo.tcp("localhost",80).socktype==Socket::SOCK_STREAM#=> true
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"
Source
static VALUEaddrinfo_to_sockaddr(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); VALUE ret; ret = rb_str_new((char*)&rai->addr, rai->sockaddr_len); return ret;}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"
Source
static VALUEaddrinfo_unix_p(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self);#ifdef AF_UNIX return ai_get_afamily(rai) == AF_UNIX ? Qtrue : Qfalse;#else return Qfalse;#endif}returns true if addrinfo is UNIX address. returns false otherwise.
Addrinfo.tcp("127.0.0.1",80).unix?#=> falseAddrinfo.tcp("::1",80).unix?#=> falseAddrinfo.unix("/tmp/sock").unix?#=> true
Source
static VALUEaddrinfo_unix_path(VALUE self){ rb_addrinfo_t *rai = get_addrinfo(self); int family = ai_get_afamily(rai); struct sockaddr_un *addr; long n; if (family != AF_UNIX) rb_raise(rb_eSocket, "need AF_UNIX address"); addr = &rai->addr.un; n = rai_unixsocket_len(rai); if (n < 0) rb_raise(rb_eSocket, "too short AF_UNIX address: %"PRIuSIZE" bytes given for minimum %"PRIuSIZE" bytes.", (size_t)rai->sockaddr_len, offsetof(struct sockaddr_un, sun_path)); if ((long)sizeof(addr->sun_path) < n) rb_raise(rb_eSocket, "too long AF_UNIX path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)", (size_t)n, sizeof(addr->sun_path)); return rb_str_new(addr->sun_path, n);}Returns the socket path as a string.
Addrinfo.unix("/tmp/sock").unix_path#=> "/tmp/sock"
Protected Instance Methods
Source
# File ext/socket/lib/socket.rb, line 54defconnect_internal(local_addrinfo,timeout=nil)# :yields: socketsock =Socket.new(self.pfamily,self.socktype,self.protocol)beginsock.ipv6only!ifself.ipv6?sock.bindlocal_addrinfoiflocal_addrinfoiftimeoutcasesock.connect_nonblock(self,exception:false)when0# success or EISCONN, other errors raisebreakwhen:wait_writablesock.wait_writable(timeout)orraiseErrno::ETIMEDOUT,'user specified timeout'endwhiletrueelsesock.connect(self)endrescueExceptionsock.closeraiseendifblock_given?beginyieldsockensuresock.closeendelsesockendend
creates a newSocket connected to the address oflocal_addrinfo.
Iflocal_addrinfo is nil, the address of the socket is not bound.
Thetimeout 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.