PadicoTM

Documentation

« back to PM2 home.
socks4-client.c
Go to the documentation of this file.
1/*
2 * Padico -- a High Performance Parallel and Distributed Computing Environment
3 * Copyright (c) 2002-2025 INRIA and the University of Rennes 1
4 * Alexandre DENIS <Alexandre.Denis@inria.fr>
5 * Christian PEREZ <Christian.Perez@inria.fr>
6 *
7 * The software has been registered at the Agency for the Protection of
8 * Programs (APP) under the number IDDN.FR.001.260013.000.S.P.2002.000.10000.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
34#include <Padico/VLink-internals.h>
35#include <Padico/Module.h>
36#include <Padico/SocketFactory.h>
37#include <Padico/Topology.h>
38
39
40/* ********************************************************* */
41
42static int socks4client_create(void*_status, int family, int type, int protocol);
43static int socks4client_connect(void*_status, const struct sockaddr*addr, socklen_t addrlen,
45static int socks4client_close(void*_status);
46
51 {
53 .bind = NULL,
54 .listen = NULL,
55 .connect = &socks4client_connect,
56 .close = &socks4client_close,
57 .setsockopt = NULL,
59 };
60
62static void socks4client_destroy(void*);
63
68 {
69 .instantiate = &socks4client_instantiate,
70 .destroy = &socks4client_destroy
71 };
72
95
96static void socks4client_proxy_connect_notifier(void*key, int fd);
97static int socks4client_proxy_reply_notifier(int fd, void*key);
99
100/* ********************************************************* */
101
110
118
119
120/* ********************************************************* */
121
122PADICO_MODULE_COMPONENT(SOCKS4_client,
123 puk_component_declare("SOCKS4_client",
124 puk_component_provides("PadicoComponent", "component", &socks4client_component_driver),
125 puk_component_provides("SocketFactory", "sf", &socks4client_sf_driver),
126 puk_component_uses("SocketFactory", "socket"),
127 puk_component_attr("gateway", "localhost"),
128 puk_component_attr("port", "1080")));
129
130/* ********************************************************* */
131
133{
134 struct socks4client_status_s*status = padico_malloc(sizeof(struct socks4client_status_s));
135 padico_print("instantiate().\n");
136 status->fd = -1;
137 status->io = NULL;
138 status->connector.notifier = NULL;
139 status->connector.key = NULL;
140 memset(&status->state, 0, sizeof(status->state));
142 padico_print("indirect socket- status = %p\n", status->socket._status);
143 status->gateway = puk_instance_getattr(instance, "gateway");
144 status->port = puk_instance_getattr(instance, "port");
145 padico_out(40, "done.\n");
146 return status;
147}
148
149static void socks4client_destroy(void*_status)
150{
151 struct socks4client_status_s*status = _status;
152 socks4client_close(_status);
153 padico_free(status);
154 padico_out(40, "done.\n");
155}
156
157/* ********************************************************* */
158
159static int socks4client_create(void*_status, int family, int type, int protocol)
160{
161 struct socks4client_status_s*status = _status;
162 assert(family == AF_INET);
164 padico_print("create()- gateway = %s; port = %s\n", status->gateway, status->port);
165 /* create the socket */
166 int rc = (*status->socket.driver->create)(status->socket._status, family, type, protocol);
167 if(rc != 0)
168 goto error;
169 /* resolve proxy into address */
171 if(!gateway)
172 padico_fatal("cannot resolve gateway %s.\n", status->gateway);
173 const uint16_t port = atoi(status->port);
174 const struct in_addr*inaddr = padico_topo_host_getaddr(gateway);
175 const struct sockaddr_in addr =
176 {
177 .sin_family = AF_INET,
178 .sin_port = htons(port),
179 .sin_addr = *inaddr
180 };
181 const socklen_t addrlen = sizeof(addr);
182 padico_print("connecting to proxy %s:%d\n", status->gateway, port);
183 /* connect to the proxy */
184 rc = (*status->socket.driver->connect)(status->socket._status,
185 (struct sockaddr*)&addr, addrlen,
187 status->state.proxy_connecting = 1;
188 padico_out(40, "rc=%d\n", rc);
189 error:
190 return rc;
191}
192
193static int socks4client_connect(void*_status, const struct sockaddr*addr, socklen_t addrlen,
195{
196 struct socks4client_status_s*status = _status;
197 int rc = -1, err = -1;
198 struct sockaddr_in*addr_in = NULL;
199 padico_print("connect().\n");
200 switch(addr->sa_family)
201 {
202 case AF_INET:
203 addr_in = (struct sockaddr_in*)addr;
204 break;
205 case AF_PADICO:
207 {
209 }
210 else
211 {
213 goto error;
214 }
215 break;
216 default:
217 padico_warning("address family not supported: %d\n", addr->sa_family);
219 goto error;
220 }
221 status->connector.notifier = notifier;
222 status->connector.key = key;
223 status->connector.in_addr = *addr_in;
224 status->state.connecting = 1;
225
226 if(status->state.proxy_connected)
227 {
229
230 if(rc == 0 || (rc == -1 && err == EINPROGRESS))
231 {
233 padico_io_activate(status->io);
234 padico_out(60, "callback_connector() registered\n");
235 }
236 }
237 else
238 {
240 }
241 error:
242 padico_out(40, "err=%d\n", err);
243 return -err;
244}
245
246static int socks4client_close(void*_status)
247{
248 struct socks4client_status_s*status = _status;
249 int rc = -1;
250 status->fd = -1;
251 (*status->socket.driver->close)(status->socket._status);
252 /* TODO */
253
254 return rc;
255}
256
262{
263 padico_print("SOCKS4: sending request.\n");
265 {
266 .vn = 0x04,
267 .cd = 0x01,
268 .dstport = htons(status->connector.in_addr.sin_port),
269 .dstip = htonl(status->connector.in_addr.sin_addr.s_addr)
270 };
271 padico_sysio_out(status->fd, &req, sizeof(req));
272 status->state.request_sent = 1;
273
274}
275
280{
281 struct socks4client_status_s*status = key;
282 padico_print("connected to proxy.\n");
283 status->state.proxy_connected = 1;
285 status->fd = fd;
286 if(status->state.connecting)
288}
289
293{
294 struct socks4client_status_s*status = key;
296 padico_sysio_in(status->fd, &reply, sizeof(reply));
297 padico_print("got reply code = 0x%x\n", reply.cd);
298 if(reply.cd == 0x90)
299 {
300 status->state.connected = 1;
301 padico_io_unregister(status->io);
302 status->io = NULL;
303 (*status->connector.notifier)(status->connector.key, status->fd);
304 }
305 return 0;
306}
assert(slab_size > 0)
const const struct ns_rule_bucket_s * key
return NULL
Definition Shm.h:88
void(* padico_socketfactory_connector_t)(void *key, int fd)
static void puk_instance_context_indirect_SocketFactory(puk_instance_t instance, const char *label, struct puk_receptacle_SocketFactory_s *receptacle)
struct padico_addrkey_hash_entry_block_lfstack_s __attribute__
hint whether the queue is empty.
static int done
Definition console-mod.c:49
puk_instance_t instance
the listening bootstrap instance
static int fd
Definition datacheck.c:32
padico_rc_t rc
Definition PadicoTM.h:146
static const struct padico_socketfactory_s socks4client_sf_driver
'SocketFactory' facet for SocketFactory-plain
static const struct puk_component_driver_s socks4client_component_driver
instanciation facet for SocketFactory-plain
#define PADICO_IO_EVENT_READ
the fildes is readable
Definition SysIO.h:62
ssize_t padico_sysio_in(int fildes, void *buf, size_t nbyte)
Definition sysio.c:792
void padico_io_activate(padico_sysio_t io)
Activates the I/O handle - when not activated, the callback is not triggered.
Definition sysio.c:278
void padico_io_unregister(padico_sysio_t io)
Unregister the I/O handle.
Definition sysio.c:298
padico_sysio_t padico_io_register(int fd, unsigned int what, padico_sysio_callback_t callback, void *key)
Registers a file descriptor for polling.
Definition sysio.c:205
ssize_t padico_sysio_out(int fildes, const void *buf, size_t nbyte)
Definition sysio.c:808
#define AF_PADICO_INET
Definition Topology.h:423
#define padico_sockaddr_payload(TYPE, ADDRESS)
Definition Topology.h:441
#define padico_sockaddr_magic(ADDRESS)
Definition Topology.h:445
const struct in_addr * padico_topo_host_getaddr(padico_topo_host_t host)
Get the canonical IP address of a host.
Definition topology.c:1977
padico_topo_host_t padico_topo_host_resolvebyname(const char *hostname)
resolve a host from name
Definition topology.c:2681
#define AF_PADICO
Definition Topology.h:409
mx_request_t req
Definition psp-mx.c:103
static int socks4client_close(void *_status)
static void socks4client_proxy_connect_notifier(void *key, int fd)
notification handler when proxy is connected (connection started in create())
static int socks4client_connect(void *_status, const struct sockaddr *addr, socklen_t addrlen, padico_socketfactory_connector_t notifier, void *key)
static void socks4client_send_request(struct socks4client_status_s *status)
send a CONNECT request to SOCKS proxy when: – socketfactory has issued a connect() – proxy is connect...
static int socks4client_proxy_reply_notifier(int fd, void *key)
notification handler when reply from proxy is received.
static int socks4client_create(void *_status, int family, int type, int protocol)
static void socks4client_destroy(void *)
static void * socks4client_instantiate(puk_instance_t, puk_context_t)
int(* create)(void *_status, int family, int type, int protocol)
create the socket attached to the SocketFactory instance
int(* connect)(void *_status, const struct sockaddr *addr, socklen_t addrlen, padico_socketfactory_connector_t notifier, void *key)
connects the socket to the given address
int(* close)(void *_status)
closes the given socket
struct padico_socketfactory_s * driver
uint8_t vn
version number- should be 0
uint8_t cd
command code- should be 90 for success; 91-93 for failure
uint16_t dstport
destination port, in network byte order
uint8_t cd
command code- should be 1 for CONNECT
uint8_t vn
version number- should be 4
uint32_t dstip
destination IP, in network byte order
struct socks4client_status_s::@79 connector
struct sockaddr_in in_addr
padico_socketfactory_connector_t notifier
struct socks4client_status_s::@80 state
struct puk_receptacle_SocketFactory_s socket
static int port
Definition vrp-test.c:15