Class: IPSocket

Inherits:
BasicSocket show all
Defined in:
ipsocket.c,
ipsocket.c

Overview

IPSocket is the super class of TCPSocket and UDPSocket.

Direct Known Subclasses

TCPSocket, UDPSocket

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicSocket

#close_read, #close_write, #connect_address, do_not_reverse_lookup, #do_not_reverse_lookup, do_not_reverse_lookup=, #do_not_reverse_lookup=, for_fd, #getpeereid, #getpeername, #getsockname, #getsockopt, #local_address, #recv, #recv_nonblock, #recvmsg, #recvmsg_nonblock, #remote_address, #send, #sendmsg, #sendmsg_nonblock, #setsockopt, #shutdown

Class Method Details

.getaddress(host) ⇒ Object

Lookups the IP address of host.

IPSocket.getaddress("localhost")     #=> "127.0.0.1"
IPSocket.getaddress("ip6-localhost") #=> "::1"


310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'ipsocket.c', line 310

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 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"]
}

Returns:

  • (Array)


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'ipsocket.c', line 215

static VALUE
ip_addr(int argc, VALUE *argv, VALUE sock)
{
    rb_io_t *fptr;
    union_sockaddr addr;
    socklen_t len = (socklen_t)sizeof addr;
    int norevlookup;

    GetOpenFile(sock, fptr);

    if (argc < 1 || !rsock_revlookup_flag(argv[0], &norevlookup))
	norevlookup = fptr->mode & FMODE_NOREVLOOKUP;
    if (getsockname(fptr->fd, &addr.addr, &len) < 0)
	rb_sys_fail("getsockname(2)");
    return rsock_ipaddr(&addr.addr, len, norevlookup);
}

#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 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"]
}

Returns:

  • (Array)


256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'ipsocket.c', line 256

static VALUE
ip_peeraddr(int argc, VALUE *argv, VALUE sock)
{
    rb_io_t *fptr;
    union_sockaddr addr;
    socklen_t len = (socklen_t)sizeof addr;
    int norevlookup;

    GetOpenFile(sock, fptr);

    if (argc < 1 || !rsock_revlookup_flag(argv[0], &norevlookup))
	norevlookup = fptr->mode & FMODE_NOREVLOOKUP;
    if (getpeername(fptr->fd, &addr.addr, &len) < 0)
	rb_sys_fail("getpeername(2)");
    return rsock_ipaddr(&addr.addr, len, norevlookup);
}

#recvfrom(maxlen) ⇒ Array #recvfrom(maxlen, flags) ⇒ Array

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 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"]]

Overloads:

  • #recvfrom(maxlen) ⇒ Array

    Returns:

    • (Array)
  • #recvfrom(maxlen, flags) ⇒ Array

    Returns:

    • (Array)


294
295
296
297
298
# File 'ipsocket.c', line 294

static VALUE
ip_recvfrom(int argc, VALUE *argv, VALUE sock)
{
    return rsock_s_recvfrom(sock, argc, argv, RECV_IP);
}