Ruby  2.5.0dev(2017-10-22revision60238)
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 since following reasons:
46  *
47  * - The 3rd element of result is the address family of the first address.
48  * The address families of rest addresses are not returned.
49  * - gethostbyname() is may take 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
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 }
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen)
Definition: raddrinfo.c:396
VALUE rb_cIPSocket
Definition: init.c:14
#define AI_CANONNAME
Definition: addrinfo.h:97
#define INET_CLIENT
Definition: rubysocket.h:221
VALUE rb_cTCPSocket
Definition: init.c:15
VALUE rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE(*ipaddr)(struct sockaddr *, socklen_t))
Definition: raddrinfo.c:704
struct rb_addrinfo * rsock_addrinfo(VALUE host, VALUE port, int family, int socktype, int flags)
Definition: raddrinfo.c:547
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:646
int argc
Definition: ruby.c:187
int socklen_t
Definition: getaddrinfo.c:83
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1908
#define Qnil
Definition: ruby.h:438
unsigned long VALUE
Definition: ruby.h:85
void rsock_init_tcpsocket(void)
Definition: tcpsocket.c:68
register unsigned int len
Definition: zonetab.h:51
#define AF_UNSPEC
Definition: sockport.h:101
VALUE rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type)
Definition: ipsocket.c:152
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
char ** argv
Definition: ruby.c:188