Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
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 */
27static VALUE
28socks_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 */
45static VALUE
46socks_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
57void
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}
VALUE rb_define_class(const char *, VALUE)
Defines a top-level class.
Definition: class.c:662
#define shutdown(a, b)
Definition: io.c:667
#define GetOpenFile(obj, fp)
Definition: io.h:127
VALUE rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type)
Definition: ipsocket.c:159
VALUE rb_io_close(VALUE)
Definition: io.c:4824
unsigned long VALUE
#define Qnil
void rb_define_method(VALUE, const char *, VALUE(*)(), int)
#define INET_SOCKS
Definition: rubysocket.h:229
VALUE rb_cTCPSocket
Definition: init.c:19
void rsock_init_sockssocket(void)
Definition: sockssocket.c:58
Definition: io.h:66
int fd
Definition: io.h:68