Class: IPSocket

Inherits:
BasicSocket show all
Defined in:
ext/socket/ipsocket.c,
lib/ipaddr.rb,
ext/socket/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, #read_nonblock, #recv, #recv_nonblock, #recvmsg, #recvmsg_nonblock, #remote_address, #send, #sendmsg, #sendmsg_nonblock, #setsockopt, #shutdown, #write_nonblock

Methods inherited from IO

#beep, #check_winsize_changed, #clear_screen, console, #console_mode, #console_mode=, #cooked, #cooked!, #cursor, #cursor=, #cursor_down, #cursor_left, #cursor_right, #cursor_up, default_console_size, #echo=, #echo?, #erase_line, #erase_screen, #expect, #getch, #getpass, #goto, #goto_column, #iflush, io_maybe_wait, io_maybe_wait_readable, io_maybe_wait_writable, io_wait, #ioflush, #noecho, #nonblock, #nonblock=, #nonblock?, #nread, #oflush, #pathconf, #pressed?, #raw, #raw!, #ready?, #scroll_backward, #scroll_forward, thread_fd_close, thread_fd_wait, thread_fd_writable, #wait, #wait_priority, #wait_readable, #wait_writable, #winsize, #winsize=

Class Method Details

.getaddress(host) ⇒ Object

Lookups the IP address of host.

require 'socket'

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


797
798
799
800
801
802
803
# File 'lib/ipaddr.rb', line 797

def getaddress(s)
  if valid_v6?(s)
    s
  else
    getaddress_orig(s)
  end
end

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

Returns:



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'ext/socket/ipsocket.c', line 274

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);
}

#inspectString

Return a string describing this IPSocket object.

Returns:



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'ext/socket/ipsocket.c', line 220

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

Returns:



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'ext/socket/ipsocket.c', line 315

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

Overloads:



353
354
355
356
357
# File 'ext/socket/ipsocket.c', line 353

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