Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

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 */
21 static VALUE
22 tcp_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
34 static VALUE
35 tcp_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 */
59 static VALUE
60 tcp_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
67 void
68 rsock_init_tcpsocket(void)
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 */
88 rb_cTCPSocket = rb_define_class("TCPSocket", rb_cIPSocket);
89 rb_define_singleton_method(rb_cTCPSocket, "gethostbyname", tcp_s_gethostbyname, 1);
90 rb_define_method(rb_cTCPSocket, "initialize", tcp_init, -1);
91 }
AF_UNSPEC
#define AF_UNSPEC
Definition: sockport.h:101
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:649
AI_CANONNAME
#define AI_CANONNAME
Definition: addrinfo.h:97
rb_scan_args
#define rb_scan_args(argc, argvp, fmt,...)
Definition: rb_mjit_min_header-2.7.0.h:6372
rb_cTCPSocket
VALUE rb_cTCPSocket
Definition: init.c:19
rsock_init_tcpsocket
void rsock_init_tcpsocket(void)
Definition: tcpsocket.c:68
INET_CLIENT
#define INET_CLIENT
Definition: rubysocket.h:227
VALUE
unsigned long VALUE
Definition: ruby.h:102
rb_define_singleton_method
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1755
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1551
rb_addrinfo
Definition: rubysocket.h:290
socklen_t
int socklen_t
Definition: getaddrinfo.c:83
rb_cIPSocket
VALUE rb_cIPSocket
Definition: init.c:18
obj
const VALUE VALUE obj
Definition: rb_mjit_min_header-2.7.0.h:5742
rsock_make_ipaddr
VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen)
Definition: raddrinfo.c:470
argv
char ** argv
Definition: ruby.c:223
argc
int argc
Definition: ruby.c:222
rsock_make_hostent
VALUE rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE(*ipaddr)(struct sockaddr *, socklen_t))
Definition: raddrinfo.c:816
len
uint8_t len
Definition: escape.c:17
rsock_addrinfo
struct rb_addrinfo * rsock_addrinfo(VALUE host, VALUE port, int family, int socktype, int flags)
Definition: raddrinfo.c:653
rubysocket.h
Qnil
#define Qnil
Definition: ruby.h:469
rsock_init_inetsock
VALUE rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type)
Definition: ipsocket.c:159