Class: IPSocket
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::BasicSocket ,
::IO
|
|
Instance Chain:
self,
::BasicSocket ,
::IO
|
|
Inherits: |
BasicSocket
|
Defined in: | ext/socket/ipsocket.c |
Overview
IPSocket
is the super class of ::TCPSocket
and ::UDPSocket
.
Class Attribute Summary
::BasicSocket
- Inherited
.do_not_reverse_lookup | Gets the global do_not_reverse_lookup flag. |
.do_not_reverse_lookup= | Sets the global do_not_reverse_lookup flag. |
Class Method Summary
-
.getaddress(host) ⇒ ipaddress
Lookups the IP address of host.
::BasicSocket
- Inherited
.for_fd | Returns a socket object which contains the file descriptor, fd. |
Instance Attribute Summary
::BasicSocket
- Inherited
#do_not_reverse_lookup | Gets the do_not_reverse_lookup flag of basicsocket. |
#do_not_reverse_lookup= | Sets the do_not_reverse_lookup flag of basicsocket. |
Instance Method Summary
-
#addr([reverse_lookup]) ⇒ Array, ...
Returns the local address as an array which contains address_family, port, hostname and numeric_address.
-
#inspect ⇒ String
Return a string describing this
IPSocket
object. -
#peeraddr([reverse_lookup]) ⇒ Array, ...
Returns the remote address as an array which contains address_family, port, hostname and numeric_address.
-
#recvfrom(maxlen) ⇒ Array, ipaddr
Receives a message and return the message as a string and an address which the message come from.
::BasicSocket
- Inherited
#close_read | Disallows further read using shutdown system call. |
#close_write | Disallows further write using shutdown system call. |
#connect_address | Returns an address of the socket suitable for connect in the local machine. |
#getpeereid | Returns the user and group on the peer of the UNIX socket. |
#getpeername | Returns the remote address of the socket as a sockaddr string. |
#getsockname | Returns the local address of the socket as a sockaddr string. |
#getsockopt | Gets a socket option. |
#local_address | Returns an |
#recv | Receives a message. |
#recv_nonblock | Receives up to maxlen bytes from |
#recvmsg | recvmsg receives a message using recvmsg(2) system call in blocking manner. |
#recvmsg_nonblock | recvmsg receives a message using recvmsg(2) system call in non-blocking manner. |
#remote_address | Returns an |
#send | send mesg via basicsocket. |
#sendmsg | sendmsg sends a message using sendmsg(2) system call in blocking manner. |
#sendmsg_nonblock | sendmsg_nonblock sends a message using sendmsg(2) system call in non-blocking manner. |
#setsockopt | Sets a socket option. |
#shutdown | Calls shutdown(2) system call. |
#__recvmsg, #__recvmsg_nonblock, #__sendmsg, #__sendmsg_nonblock, #read_nonblock, #write_nonblock, #__read_nonblock, #__recv_nonblock, #__write_nonblock |
Class Method Details
.getaddress(host) ⇒ ipaddress
Lookups the IP address of host.
require 'socket'
IPSocket.getaddress("localhost") #=> "127.0.0.1"
IPSocket.getaddress("ip6-localhost") #=> "::1"
# File 'ext/socket/ipsocket.c', line 1513
static VALUE ip_s_getaddress(VALUE obj, VALUE host) { union_sockaddr addr; struct rb_addrinfo *res = rsock_addrinfo(host, Qnil, AF_UNSPEC, SOCK_STREAM, 0); socklen_t len = res->ai->ai_addrlen; /* just take the first one */ memcpy(&addr, res->ai->ai_addr, len); rb_freeaddrinfo(res); return rsock_make_ipaddr(&addr.addr, len); }
Instance Method Details
#addr([reverse_lookup]) ⇒ Array
, ...
Returns the local address as an array which contains address_family, port, hostname and numeric_address.
If reverse_lookup
is true
or :hostname
, hostname is obtained from numeric_address using reverse lookup. Or if it is false
, or :numeric
, hostname is the same as numeric_address. Or if it is nil
or omitted, obeys to ipsocket.do_not_reverse_lookup
. See Socket.getaddrinfo also.
TCPSocket.open("www.ruby-lang.org", 80) {|sock|
p sock.addr #=> ["AF_INET", 49429, "hal", "192.168.0.128"]
p sock.addr(true) #=> ["AF_INET", 49429, "hal", "192.168.0.128"]
p sock.addr(false) #=> ["AF_INET", 49429, "192.168.0.128", "192.168.0.128"]
p sock.addr(:hostname) #=> ["AF_INET", 49429, "hal", "192.168.0.128"]
p sock.addr(:numeric) #=> ["AF_INET", 49429, "192.168.0.128", "192.168.0.128"]
}
# File 'ext/socket/ipsocket.c', line 1422
static VALUE ip_addr(int argc, VALUE *argv, VALUE sock) { union_sockaddr addr; socklen_t len = (socklen_t)sizeof addr; int norevlookup; if (argc < 1 || !rsock_revlookup_flag(argv[0], &norevlookup)) norevlookup = rb_io_mode(sock) & FMODE_NOREVLOOKUP; if (getsockname(rb_io_descriptor(sock), &addr.addr, &len) < 0) rb_sys_fail("getsockname(2)"); return rsock_ipaddr(&addr.addr, len, norevlookup); }
#inspect ⇒ String
Return a string describing this IPSocket
object.
# File 'ext/socket/ipsocket.c', line 1368
static VALUE ip_inspect(VALUE sock) { VALUE str = rb_call_super(0, 0); rb_io_t *fptr = RFILE(sock)->fptr; union_sockaddr addr; socklen_t len = (socklen_t)sizeof addr; ID id; if (fptr && fptr->fd >= 0 && getsockname(fptr->fd, &addr.addr, &len) >= 0 && (id = rsock_intern_family(addr.addr.sa_family)) != 0) { VALUE family = rb_id2str(id); char hbuf[1024], pbuf[1024]; long slen = RSTRING_LEN(str); const char last = (slen > 1 && RSTRING_PTR(str)[slen - 1] == '>') ? (--slen, '>') : 0; str = rb_str_subseq(str, 0, slen); rb_str_cat_cstr(str, ", "); rb_str_append(str, family); if (!rb_getnameinfo(&addr.addr, len, hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), NI_NUMERICHOST | NI_NUMERICSERV)) { rb_str_cat_cstr(str, ", "); rb_str_cat_cstr(str, hbuf); rb_str_cat_cstr(str, ", "); rb_str_cat_cstr(str, pbuf); } if (last) rb_str_cat(str, &last, 1); } return str; }
#peeraddr([reverse_lookup]) ⇒ Array
, ...
Returns the remote address as an array which contains address_family, port, hostname and numeric_address. It is defined for connection oriented socket such as ::TCPSocket
.
If reverse_lookup
is true
or :hostname
, hostname is obtained from numeric_address using reverse lookup. Or if it is false
, or :numeric
, hostname is the same as numeric_address. Or if it is nil
or omitted, obeys to ipsocket.do_not_reverse_lookup
. See Socket.getaddrinfo also.
TCPSocket.open("www.ruby-lang.org", 80) {|sock|
p sock.peeraddr #=> ["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68"]
p sock.peeraddr(true) #=> ["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68"]
p sock.peeraddr(false) #=> ["AF_INET", 80, "221.186.184.68", "221.186.184.68"]
p sock.peeraddr(:hostname) #=> ["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68"]
p sock.peeraddr(:numeric) #=> ["AF_INET", 80, "221.186.184.68", "221.186.184.68"]
}
# File 'ext/socket/ipsocket.c', line 1460
static VALUE ip_peeraddr(int argc, VALUE *argv, VALUE sock) { union_sockaddr addr; socklen_t len = (socklen_t)sizeof addr; int norevlookup; if (argc < 1 || !rsock_revlookup_flag(argv[0], &norevlookup)) norevlookup = rb_io_mode(sock) & FMODE_NOREVLOOKUP; if (getpeername(rb_io_descriptor(sock), &addr.addr, &len) < 0) rb_sys_fail("getpeername(2)"); return rsock_ipaddr(&addr.addr, len, norevlookup); }
#recvfrom(maxlen) ⇒ Array
, ipaddr
#recvfrom(maxlen, flags) ⇒ Array
, ipaddr
Array
, ipaddr
#recvfrom(maxlen, flags) ⇒ Array
, ipaddr
Receives a message and return the message as a string and an address which the message come from.
maxlen is the maximum number of bytes to receive.
flags should be a bitwise OR of Socket::MSG_* constants.
ipaddr is the same as IPSocket#peeraddr,addr
.
u1 = UDPSocket.new
u1.bind("127.0.0.1", 4913)
u2 = UDPSocket.new
u2.send "uuuu", 0, "127.0.0.1", 4913
p u1.recvfrom(10) #=> ["uuuu", ["AF_INET", 33230, "localhost", "127.0.0.1"]]
# File 'ext/socket/ipsocket.c', line 1495
static VALUE ip_recvfrom(int argc, VALUE *argv, VALUE sock) { return rsock_s_recvfrom(sock, argc, argv, RECV_IP); }