|
Ruby 1.9.2p290(2011-07-09revision32553)
|
00001 /************************************************ 00002 00003 tcpsocket.c - 00004 00005 created at: Thu Mar 31 12:21:29 JST 1994 00006 00007 Copyright (C) 1993-2007 Yukihiro Matsumoto 00008 00009 ************************************************/ 00010 00011 #include "rubysocket.h" 00012 00013 /* 00014 * call-seq: 00015 * TCPSocket.new(remote_host, remote_port, local_host=nil, local_port=nil) 00016 * 00017 * Opens a TCP connection to +remote_host+ on +remote_port+. If +local_host+ 00018 * and +local_port+ are specified, then those parameters are used on the local 00019 * end to establish the connection. 00020 */ 00021 static VALUE 00022 tcp_init(int argc, VALUE *argv, VALUE sock) 00023 { 00024 VALUE remote_host, remote_serv; 00025 VALUE local_host, local_serv; 00026 00027 rb_scan_args(argc, argv, "22", &remote_host, &remote_serv, 00028 &local_host, &local_serv); 00029 00030 return rsock_init_inetsock(sock, remote_host, remote_serv, 00031 local_host, local_serv, INET_CLIENT); 00032 } 00033 00034 static VALUE 00035 tcp_sockaddr(struct sockaddr *addr, size_t len) 00036 { 00037 return rsock_make_ipaddr(addr); 00038 } 00039 00040 /* 00041 * call-seq: 00042 * TCPSocket.gethostbyname(hostname) => [official_hostname, alias_hostnames, address_family, *address_list] 00043 * 00044 * Lookups host information by _hostname_. 00045 * 00046 * TCPSocket.gethostbyname("localhost") 00047 * #=> ["localhost", ["hal"], 2, "127.0.0.1"] 00048 * 00049 */ 00050 static VALUE 00051 tcp_s_gethostbyname(VALUE obj, VALUE host) 00052 { 00053 rb_secure(3); 00054 return rsock_make_hostent(host, rsock_addrinfo(host, Qnil, SOCK_STREAM, AI_CANONNAME), 00055 tcp_sockaddr); 00056 } 00057 00058 /* 00059 * Document-class: ::TCPSocket < IPSocket 00060 * 00061 * TCPSocket represents a TCP/IP client socket. 00062 */ 00063 void 00064 rsock_init_tcpsocket(void) 00065 { 00066 rb_cTCPSocket = rb_define_class("TCPSocket", rb_cIPSocket); 00067 rb_define_singleton_method(rb_cTCPSocket, "gethostbyname", tcp_s_gethostbyname, 1); 00068 rb_define_method(rb_cTCPSocket, "initialize", tcp_init, -1); 00069 } 00070
1.7.3