Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
tcpsocket.c
Go to the documentation of this file.
1/************************************************
2
3 tcpsocket.c -
4
5 created at: Thu Mar 31 12:21:29 JST 1994
6
7 Copyright (C) 1993-2007 Yukihiro Matsumoto
8
9************************************************/
10
11#include "rubysocket.h"
12
13/*
14 * call-seq:
15 * TCPSocket.new(remote_host, remote_port, local_host=nil, local_port=nil)
16 *
17 * Opens a TCP connection to +remote_host+ on +remote_port+. If +local_host+
18 * and +local_port+ are specified, then those parameters are used on the local
19 * end to establish the connection.
20 */
21static VALUE
22tcp_init(int argc, VALUE *argv, VALUE sock)
23{
24 VALUE remote_host, remote_serv;
25 VALUE local_host, local_serv;
26
27 rb_scan_args(argc, argv, "22", &remote_host, &remote_serv,
28 &local_host, &local_serv);
29
30 return rsock_init_inetsock(sock, remote_host, remote_serv,
31 local_host, local_serv, INET_CLIENT);
32}
33
34static VALUE
35tcp_sockaddr(struct sockaddr *addr, socklen_t len)
36{
37 return rsock_make_ipaddr(addr, len);
38}
39
40/*
41 * call-seq:
42 * TCPSocket.gethostbyname(hostname) => [official_hostname, alias_hostnames, address_family, *address_list]
43 *
44 * Use Addrinfo.getaddrinfo instead.
45 * This method is deprecated for the following reasons:
46 *
47 * - The 3rd element of the result is the address family of the first address.
48 * The address families of the rest of the addresses are not returned.
49 * - gethostbyname() may take a long time and it may block other threads.
50 * (GVL cannot be released since gethostbyname() is not thread safe.)
51 * - This method uses gethostbyname() function already removed from POSIX.
52 *
53 * This method lookups host information by _hostname_.
54 *
55 * TCPSocket.gethostbyname("localhost")
56 * #=> ["localhost", ["hal"], 2, "127.0.0.1"]
57 *
58 */
59static VALUE
60tcp_s_gethostbyname(VALUE obj, VALUE host)
61{
62 struct rb_addrinfo *res =
63 rsock_addrinfo(host, Qnil, AF_UNSPEC, SOCK_STREAM, AI_CANONNAME);
64 return rsock_make_hostent(host, res, tcp_sockaddr);
65}
66
67void
69{
70 /*
71 * Document-class: TCPSocket < IPSocket
72 *
73 * TCPSocket represents a TCP/IP client socket.
74 *
75 * A simple client may look like:
76 *
77 * require 'socket'
78 *
79 * s = TCPSocket.new 'localhost', 2000
80 *
81 * while line = s.gets # Read lines from socket
82 * puts line # and print them
83 * end
84 *
85 * s.close # close socket when done
86 *
87 */
89 rb_define_singleton_method(rb_cTCPSocket, "gethostbyname", tcp_s_gethostbyname, 1);
90 rb_define_method(rb_cTCPSocket, "initialize", tcp_init, -1);
91}
#define AI_CANONNAME
Definition: addrinfo.h:97
int socklen_t
Definition: getaddrinfo.c:83
VALUE rb_define_class(const char *, VALUE)
Defines a top-level class.
Definition: class.c:662
VALUE rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type)
Definition: ipsocket.c:159
VALUE rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE(*ipaddr)(struct sockaddr *, socklen_t))
Definition: raddrinfo.c:816
VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen)
Definition: raddrinfo.c:470
struct rb_addrinfo * rsock_addrinfo(VALUE host, VALUE port, int family, int socktype, int flags)
Definition: raddrinfo.c:653
const VALUE VALUE obj
unsigned long VALUE
__inline__ const void *__restrict__ size_t len
void rb_define_singleton_method(VALUE, const char *, VALUE(*)(), int)
#define rb_scan_args(argc, argvp, fmt,...)
#define Qnil
const VALUE * argv
void rb_define_method(VALUE, const char *, VALUE(*)(), int)
#define INET_CLIENT
Definition: rubysocket.h:227
VALUE rb_cIPSocket
Definition: init.c:18
VALUE rb_cTCPSocket
Definition: init.c:19
#define AF_UNSPEC
Definition: sockport.h:101
void rsock_init_tcpsocket(void)
Definition: tcpsocket.c:68