Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
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, connect_timeout: 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 *
21 * [:connect_timeout] specify the timeout in seconds.
22 */
23static VALUE
24tcp_init(int argc, VALUE *argv, VALUE sock)
25{
26 VALUE remote_host, remote_serv;
27 VALUE local_host, local_serv;
28 VALUE opt;
29 static ID keyword_ids[2];
30 VALUE kwargs[2];
31 VALUE resolv_timeout = Qnil;
32 VALUE connect_timeout = Qnil;
33
34 if (!keyword_ids[0]) {
35 CONST_ID(keyword_ids[0], "resolv_timeout");
36 CONST_ID(keyword_ids[1], "connect_timeout");
37 }
38
39 rb_scan_args(argc, argv, "22:", &remote_host, &remote_serv,
40 &local_host, &local_serv, &opt);
41
42 if (!NIL_P(opt)) {
43 rb_get_kwargs(opt, keyword_ids, 0, 2, kwargs);
44 if (kwargs[0] != Qundef) { resolv_timeout = kwargs[0]; }
45 if (kwargs[1] != Qundef) { connect_timeout = kwargs[1]; }
46 }
47
48 return rsock_init_inetsock(sock, remote_host, remote_serv,
49 local_host, local_serv, INET_CLIENT,
50 resolv_timeout, connect_timeout);
51}
52
53static VALUE
54tcp_sockaddr(struct sockaddr *addr, socklen_t len)
55{
56 return rsock_make_ipaddr(addr, len);
57}
58
59/*
60 * call-seq:
61 * TCPSocket.gethostbyname(hostname) => [official_hostname, alias_hostnames, address_family, *address_list]
62 *
63 * Use Addrinfo.getaddrinfo instead.
64 * This method is deprecated for the following reasons:
65 *
66 * - The 3rd element of the result is the address family of the first address.
67 * The address families of the rest of the addresses are not returned.
68 * - gethostbyname() may take a long time and it may block other threads.
69 * (GVL cannot be released since gethostbyname() is not thread safe.)
70 * - This method uses gethostbyname() function already removed from POSIX.
71 *
72 * This method lookups host information by _hostname_.
73 *
74 * TCPSocket.gethostbyname("localhost")
75 * #=> ["localhost", ["hal"], 2, "127.0.0.1"]
76 *
77 */
78static VALUE
79tcp_s_gethostbyname(VALUE obj, VALUE host)
80{
81 rb_warn("TCPSocket.gethostbyname is deprecated; use Addrinfo.getaddrinfo instead.");
82 struct rb_addrinfo *res =
83 rsock_addrinfo(host, Qnil, AF_UNSPEC, SOCK_STREAM, AI_CANONNAME);
84 return rsock_make_hostent(host, res, tcp_sockaddr);
85}
86
87void
89{
90 /*
91 * Document-class: TCPSocket < IPSocket
92 *
93 * TCPSocket represents a TCP/IP client socket.
94 *
95 * A simple client may look like:
96 *
97 * require 'socket'
98 *
99 * s = TCPSocket.new 'localhost', 2000
100 *
101 * while line = s.gets # Read lines from socket
102 * puts line # and print them
103 * end
104 *
105 * s.close # close socket when done
106 *
107 */
109 rb_define_singleton_method(rb_cTCPSocket, "gethostbyname", tcp_s_gethostbyname, 1);
110 rb_define_method(rb_cTCPSocket, "initialize", tcp_init, -1);
111}
#define AI_CANONNAME
Definition: addrinfo.h:97
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition: cxxanyargs.hpp:653
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition: cxxanyargs.hpp:668
uint8_t len
Definition: escape.c:17
int socklen_t
Definition: getaddrinfo.c:83
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:748
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:2296
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Definition: class.c:2085
void rb_warn(const char *fmt,...)
Definition: error.c:408
#define CONST_ID
Definition: symbol.h:47
VALUE rb_cIPSocket
Definition: init.c:18
VALUE rb_cTCPSocket
Definition: init.c:19
VALUE rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type, VALUE resolv_timeout, VALUE connect_timeout)
Definition: ipsocket.c:171
VALUE rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE(*ipaddr)(struct sockaddr *, socklen_t))
Definition: raddrinfo.c:706
VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen)
Definition: raddrinfo.c:396
struct rb_addrinfo * rsock_addrinfo(VALUE host, VALUE port, int family, int socktype, int flags)
Definition: raddrinfo.c:543
int argc
Definition: ruby.c:240
char ** argv
Definition: ruby.c:241
#define INET_CLIENT
Definition: rubysocket.h:252
#define AF_UNSPEC
Definition: sockport.h:101
#define Qundef
#define Qnil
#define NIL_P
void rsock_init_tcpsocket(void)
Definition: tcpsocket.c:88
unsigned long VALUE
Definition: value.h:38
unsigned long ID
Definition: value.h:39