Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

sockssocket.c
Go to the documentation of this file.
1 /************************************************
2
3 sockssocket.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 #ifdef SOCKS
14 /*
15 * call-seq:
16 * SOCKSSocket.new(host, port) => socket
17 *
18 * Opens a SOCKS connection to +host+ via the SOCKS server.
19 *
20 * The SOCKS server configuration varies by implementation
21 *
22 * When using the Dante libsocks/libsocksd implementation it is configured as SOCKS_SERVER env var.
23 *
24 * See: https://manpages.debian.org/testing/dante-client/socksify.1.en.html for full env variable support.
25 *
26 */
27 static VALUE
28 socks_init(VALUE sock, VALUE host, VALUE port)
29 {
30 static int init = 0;
31
32 if (init == 0) {
33 SOCKSinit("ruby");
34 init = 1;
35 }
36
37 return rsock_init_inetsock(sock, host, port, Qnil, Qnil, INET_SOCKS);
38 }
39
40 #ifdef SOCKS5
41 /*
42 * Closes the SOCKS connection.
43 *
44 */
45 static VALUE
46 socks_s_close(VALUE sock)
47 {
48 rb_io_t *fptr;
49
50 GetOpenFile(sock, fptr);
51 shutdown(fptr->fd, 2);
52 return rb_io_close(sock);
53 }
54 #endif
55 #endif
56
57 void
58 rsock_init_sockssocket(void)
59 {
60 #ifdef SOCKS
61 /*
62 * Document-class: SOCKSSocket < TCPSocket
63 *
64 * SOCKS is an Internet protocol that routes packets between a client and
65 * a server through a proxy server. SOCKS5, if supported, additionally
66 * provides authentication so only authorized users may access a server.
67 */
68 rb_cSOCKSSocket = rb_define_class("SOCKSSocket", rb_cTCPSocket);
69 rb_define_method(rb_cSOCKSSocket, "initialize", socks_init, 2);
70 #ifdef SOCKS5
71 rb_define_method(rb_cSOCKSSocket, "close", socks_s_close, 0);
72 #endif
73 #endif
74 }
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:649
rb_io_close
VALUE rb_io_close(VALUE)
Definition: io.c:4820
rb_cTCPSocket
VALUE rb_cTCPSocket
Definition: init.c:19
VALUE
unsigned long VALUE
Definition: ruby.h:102
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1551
INET_SOCKS
#define INET_SOCKS
Definition: rubysocket.h:229
rb_io_t::fd
int fd
Definition: io.h:68
shutdown
#define shutdown(a, b)
Definition: io.c:667
GetOpenFile
#define GetOpenFile(obj, fp)
Definition: io.h:127
rsock_init_sockssocket
void rsock_init_sockssocket(void)
Definition: sockssocket.c:58
rubysocket.h
Qnil
#define Qnil
Definition: ruby.h:469
rb_io_t
Definition: io.h:66
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