Ruby 1.9.3p327(2012-11-10revision37606)
ext/socket/sockssocket.c
Go to the documentation of this file.
00001 /************************************************
00002 
00003   sockssocket.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 #ifdef SOCKS
00014 /*
00015  * call-seq:
00016  *   SOCKSSocket.new(host, serv) => socket
00017  *
00018  * Opens a SOCKS connection to +host+ via the SOCKS server +serv+.
00019  *
00020  */
00021 static VALUE
00022 socks_init(VALUE sock, VALUE host, VALUE serv)
00023 {
00024     static int init = 0;
00025 
00026     if (init == 0) {
00027         SOCKSinit("ruby");
00028         init = 1;
00029     }
00030 
00031     return rsock_init_inetsock(sock, host, serv, Qnil, Qnil, INET_SOCKS);
00032 }
00033 
00034 #ifdef SOCKS5
00035 /*
00036  * Closes the SOCKS connection.
00037  *
00038  */
00039 static VALUE
00040 socks_s_close(VALUE sock)
00041 {
00042     rb_io_t *fptr;
00043 
00044     if (rb_safe_level() >= 4 && !OBJ_TAINTED(sock)) {
00045         rb_raise(rb_eSecurityError, "Insecure: can't close socket");
00046     }
00047     GetOpenFile(sock, fptr);
00048     shutdown(fptr->fd, 2);
00049     return rb_io_close(sock);
00050 }
00051 #endif
00052 #endif
00053 
00054 void
00055 rsock_init_sockssocket(void)
00056 {
00057 #ifdef SOCKS
00058     /*
00059      * Document-class: SOCKSSocket < TCPSocket
00060      *
00061      * SOCKS is an Internet protocol that routes packets between a client and
00062      * a server through a proxy server.  SOCKS5, if supported, additionally
00063      * provides authentication so only authorized users may access a server.
00064      */
00065     rb_cSOCKSSocket = rb_define_class("SOCKSSocket", rb_cTCPSocket);
00066     rb_define_method(rb_cSOCKSSocket, "initialize", socks_init, 2);
00067 #ifdef SOCKS5
00068     rb_define_method(rb_cSOCKSSocket, "close", socks_s_close, 0);
00069 #endif
00070 #endif
00071 }
00072