Ruby: ext/socket/sockssocket.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

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, Qnil, Qnil);
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
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_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition: cxxanyargs.hpp:653
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:748
rb_io_close
VALUE rb_io_close(VALUE)
Definition: io.c:4935
GetOpenFile
#define GetOpenFile
Definition: io.h:125
rb_cTCPSocket
VALUE rb_cTCPSocket
Definition: init.c:19
shutdown
#define shutdown(a, b)
Definition: io.c:724
rsock_init_inetsock
VALUE rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type, VALUE resolv_timeout, VALUE connect_timeout)
Definition: ipsocket.c:171
rubysocket.h
INET_SOCKS
#define INET_SOCKS
Definition: rubysocket.h:254
rsock_init_sockssocket
void rsock_init_sockssocket(void)
Definition: sockssocket.c:58
Qnil
#define Qnil
Definition: special_consts.h:51
rb_io_t
Definition: io.h:61
rb_io_t::fd
int fd
Definition: io.h:65
VALUE
unsigned long VALUE
Definition: value.h:38