Ruby: ext/socket/tcpsocket.c Source File

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
88 rsock_init_tcpsocket(void)
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 */
108 rb_cTCPSocket = rb_define_class("TCPSocket", rb_cIPSocket);
109 rb_define_singleton_method(rb_cTCPSocket, "gethostbyname", tcp_s_gethostbyname, 1);
110 rb_define_method(rb_cTCPSocket, "initialize", tcp_init, -1);
111}
AI_CANONNAME
#define AI_CANONNAME
Definition: addrinfo.h:97
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition: cxxanyargs.hpp:653
rb_define_singleton_method
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition: cxxanyargs.hpp:668
len
uint8_t len
Definition: escape.c:17
socklen_t
int socklen_t
Definition: getaddrinfo.c:83
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:748
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:2296
rb_get_kwargs
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Definition: class.c:2085
rb_warn
void rb_warn(const char *fmt,...)
Definition: error.c:408
CONST_ID
#define CONST_ID
Definition: symbol.h:47
rb_cIPSocket
VALUE rb_cIPSocket
Definition: init.c:18
rb_cTCPSocket
VALUE rb_cTCPSocket
Definition: init.c:19
rsock_init_inetsock
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
rsock_make_hostent
VALUE rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE(*ipaddr)(struct sockaddr *, socklen_t))
Definition: raddrinfo.c:706
rsock_make_ipaddr
VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen)
Definition: raddrinfo.c:396
rsock_addrinfo
struct rb_addrinfo * rsock_addrinfo(VALUE host, VALUE port, int family, int socktype, int flags)
Definition: raddrinfo.c:543
argc
int argc
Definition: ruby.c:240
argv
char ** argv
Definition: ruby.c:241
rubysocket.h
INET_CLIENT
#define INET_CLIENT
Definition: rubysocket.h:252
AF_UNSPEC
#define AF_UNSPEC
Definition: sockport.h:101
Qundef
#define Qundef
Definition: special_consts.h:53
Qnil
#define Qnil
Definition: special_consts.h:51
NIL_P
#define NIL_P
Definition: special_consts.h:46
rb_addrinfo
Definition: rubysocket.h:312
rsock_init_tcpsocket
void rsock_init_tcpsocket(void)
Definition: tcpsocket.c:88
VALUE
unsigned long VALUE
Definition: value.h:38
ID
unsigned long ID
Definition: value.h:39