PadicoTM

Documentation

« back to PM2 home.
sf-srtun.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
30#include <Padico/PadicoTM.h>
31#include <Padico/VLink-internals.h>
32#include <Padico/Module.h>
33#include <Padico/SocketFactory.h>
34
35static int sfsrtun_module_init(void);
36static void sfsrtun_module_finalize(void);
37
39 "SysIO", "Topology", "PadicoControl");
40
45/* ********************************************************* */
46
47static int sfsrtun_create(void*_instance, int family, int type, int protocol);
48static int sfsrtun_connect(void*_instance, const struct sockaddr*addr, socklen_t addrlen,
50static int sfsrtun_close(void*_instance);
51
56 {
58 .bind = NULL,
59 .listen = NULL,
60 .connect = &sfsrtun_connect,
61 .close = &sfsrtun_close,
63 };
64
66static void sfsrtun_destroy(void*);
67
72 {
73 .instantiate = &sfsrtun_instantiate,
74 .destroy = &sfsrtun_destroy
75 };
76
81{
82 int fd;
87 const char*ssh_path;
88 const char*ssh_host;
90 const char*ssh_cipher;
91
92/* puk_instance_t instance; */
93/* puk_context_t context; */
94
95 unsigned int server_id;
97};
98
99
100static struct
101{
103
105 unsigned int next_id;
109
110static struct
111{
112 int pid ;
114
115static int sfsrtun_callback_connector(int fd, void*key);
116
117
118
119/* ********************************************************* */
120
123
124static int sfsrtun_module_init(void)
125{
126 puk_component_declare("SocketFactory_Reversed_SSH_Tunnel",
127 puk_component_provides("PadicoComponent", "component", &sfsrtun_component_driver),
128 puk_component_provides("SocketFactory", "sf", &sfsrtun_sf_driver),
129 puk_component_attr("ssh_host", NULL),
130 puk_component_attr("ssh_port", NULL),
131 puk_component_attr("ssh_path", "/usr/bin/ssh"),
132 puk_component_attr("ssh_cipher", "blowfish"));
133
135 .xml_tag = "SocketFactory_Reversed_SSH_Tunnel:Connect",
136 .start_handler = NULL,
138 .required_level = PUK_TRUST_CONTROL
139 });
140
142 .xml_tag = "SocketFactory_Reversed_SSH_Tunnel:Close",
143 .start_handler = NULL,
144 .end_handler = &sfsrtun_close_end_handler,
145 .required_level = PUK_TRUST_CONTROL
146 });
147
148 /* Init sfsrtun */
149 sfsrtun.iface_sf = puk_iface_lookup("SocketFactory");
150 sfsrtun.next_id = 1;
151 marcel_mutex_init(&sfsrtun.connections_mtx, 0);
152 sfsrtun.port_offset = 0;
153 sfsrtun.pool = padico_tm_bgthread_pool_create("SocketFactory_Reversed_SSH_Tunnel");
154
155 sfsrtun_server.pid = -1;
156 return 0;
157}
158
159static void sfsrtun_module_finalize(void)
160{
162}
163
164/* ********************************************************* */
165/* *** 'PadicoComponent' for SocketFactory_Reversed_SSH_Tunnel */
166
168{
170 instance->fd = -1;
171 instance->pid = -1;
172 instance->io_connect = NULL;
173 instance->connector = NULL;
174 instance->ssh_path = puk_context_getattr(context, "ssh_path");
175 instance->ssh_cipher = puk_context_getattr(context, "ssh_cipher");
176 instance->ssh_host = puk_context_getattr(context, "ssh_host");
177 const char*ssh_port = puk_context_getattr(context, "ssh_port");
178 instance->ssh_port = atoi(ssh_port) + sfsrtun.port_offset++;
179
180/* instance->instance = assembly; */
181/* instance->context = context; */
182 instance->server = NULL;
183 instance->server_id = 0;
184
185
186 padico_out(5, "path=%s; host=%s; port=%d; cipher=%s- done.\n",
187 instance->ssh_path, instance->ssh_host, instance->ssh_port, instance->ssh_cipher);
188 return instance;
189}
190
191static void sfsrtun_destroy(void*_instance)
192{
194
196 if(instance->pid != -1) {
197 int rc = -1, err = -1;
199 rc = kill(instance->pid, SIGTERM);
202 instance->pid = -1;
203 if(rc) {
204 padico_warning("kill()- errno=%d (%s).\n", err, strerror(err));
205 }
206 }
208 padico_out(40, "done.\n");
209}
210
211
212/* ********************************************************* */
213/* *** 'SocketFactory' for SocketFactory_Reversed_SSH_Tunnel */
214
215static int sfsrtun_create(void*_instance, int family, int type, int protocol)
216{
218 int fd = padico_sysio_socket(family, type, protocol);
219 int err = padico_sysio_errno();
220 assert(family == AF_INET);
222 padico_out(10, "family=%d\n", family);
223 if(fd > -1) {
224 err = 0;
225 instance->fd = fd;
227 }
228 padico_out(40, "fd=%d; err=%d\n", fd, err);
229 return -err;
230}
231
232
233/* Checks whether this node is the server, if not sends a message to the server */
235{
236 padico_out(50, "Launch server\n");
237
238 if (instance->server == NULL) {
239 padico_warning("Error: instance->server == NULL.\n");
240 return -1;
241 }
242 const char*server_host = padico_topo_host_getname(instance->server);
243 padico_out(40, "*** server host=%s\n", server_host);
244
245
246 const int ssh_tunnel_rport = ntohs(inaddr->sin_port);
248 padico_out(50, "ssh tunnel rport: %d\n", ssh_tunnel_rport);
249 padico_out(50, "ssh tunnel port: %d\n", instance->ssh_port);
250 padico_out(50, "ssh tunnel host: %s\n", instance->ssh_host);
251 padico_out(50, "ssh tunnel server: %s\n", server_host);
252
253 padico_out(20, "init server side (server node=%s, client port=%d, server port=%d)...\n",
255
256 padico_string_catf(s_launch,"<SocketFactory_Reversed_SSH_Tunnel:Connect clientHost=\"%s\" clientPort=\"%d\" serverPort=\"%d\" cipher=\"%s\" sshPath=\"%s\"/>" ,
257 instance->ssh_host, instance->ssh_port, ssh_tunnel_rport, instance->ssh_cipher, instance->ssh_path);
258
262
263 int err = padico_rc_iserror(rc);
264 if(err != 0) {
265 padico_warning("Error: remote connection for host=%s failed: %s.\n",
267 err = -1;
268 } else {
269 char*s_id = padico_rc_gettext(rc);
270 instance->server_id = atoi(s_id);
271 padico_out(10, "launched reversed tunnel on node=%s, id=%d\n",
272 server_host, instance->server_id);
273 }
275
276 return err;
277}
278
279
280
281static int sfsrtun_connect(void*_instance, const struct sockaddr*addr, socklen_t addrlen,
283{
284 int rc = -1;
285 int err = -1;
287 struct sockaddr_in inaddr;
288 int inaddrlen = sizeof(inaddr);
289 int nodelay = 1;
290
291 padico_out(50, "::connect()\n");
292 memcpy(&inaddr, addr, sizeof(struct sockaddr_in));
293 inaddr.sin_port = htons(instance->ssh_port);
294 inet_aton("127.0.0.1", &inaddr.sin_addr);
295
296 /* Resolve server name */
298/* instance->server = padico_server_host; */
300 // instance->server = padico_topo_getnodebyname(server_host);
302
303 if(padico_server_host == NULL) {
304 padico_warning("connect: host=%s not found\n", server_host);
305 return -1;
306 } else
307 padico_out(40, "server host=%s\n", server_host);
308
309 /* Launch server */
311 if(rc == -1) {
312 padico_warning("Problem while launching server\n");
313 return rc;
314 }
315
316
317 /* Wait for socket to open */
318 rc = PUK_ABI_FSYS_WRAP(setsockopt)(instance->fd, SOL_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay));
320 if(rc)
321 goto exit;
322 retry:
323 rc = PUK_ABI_FSYS_WRAP(connect)(instance->fd, (struct sockaddr*)&inaddr, inaddrlen);
325 padico_out(50, "::connect() rc=%d errno=%d (%s)\n", rc, err, strerror(err));
326 if(rc == 0 || (rc == -1 && err == EINPROGRESS))
327 {
329 instance->connector = notifier;
330 instance->connector_key = key;
331 padico_io_activate(instance->io_connect);
332 padico_out(60, "callback_connector() registered\n");
333 }
334 else if(rc == -1 && (err == ECONNREFUSED || err == EINTR))
335 {
336/* int status = -1; */
337/* rc = waitpid(instance->pid, &status, WNOHANG); */
338/* if(rc == instance->pid) */
339/* { */
340/* padico_warning("SSH-Reversed-Tunnel: ssh error- status=%d.\n", status); */
341/* instance->pid = -1; */
342/* err = ENETUNREACH; /\** @todo check what is the best errno when ssh fails. *\/ */
343/* goto exit; */
344/* } */
345/* else */
346/* { */
347 padico_out(6, "::connect()- tunnel not ready (%s).\n", strerror(err));
349 goto retry;
350/* } */
351 }
352 else
353 {
354 padico_warning("connect() error while connecting to ssh- errno=%d (%s)\n", err, strerror(err));
355 }
356 padico_out(40, "err=%d\n", err);
357 exit:
358 return -err;
359}
360
361static int sfsrtun_close(void*_instance)
362{
364 int rc = -1;
365 if(instance->io_connect) {
366 padico_io_deactivate(instance->io_connect);
367 padico_io_unregister(instance->io_connect);
368 instance->io_connect = NULL;
369 }
370 if(instance->fd != -1) {
371 rc = PUK_ABI_FSYS_WRAP(close)(instance->fd);
373 padico_out(50, "::close() rc=%d errno=%d (%s)\n", rc, err, strerror(err));
374 instance->fd = -1;
375 }
376
377 if(instance->server_id != 0) {
378 padico_out(10, "closing server=%s id=%d.\n", padico_topo_host_getname(instance->server), instance->server_id);
380 padico_string_catf(s_close,"<SocketFactory-Reversed-SSH-Tunne:Close id=\"%d\"/>", instance->server_id);
381
382 /* If client == server, then we do not need to do anything, ssh tunnel will be killed by destroy() */
383 if(instance->server == padico_topo_getlocalhost())
385 /* If client != server, then we need to send a message */
386 else
389 }
390 padico_out(10, "closed.\n");
391
392 return rc;
393}
394
395/* ********************************************************* */
396
398{
400 (*instance->connector)(instance->connector_key, instance->fd);
401 return NULL;
402}
403
404static int sfsrtun_callback_connector(int fd, void*key)
405{
406 int again = 0;
408 padico_out(40, "fd=%d\n", fd);
409 assert(fd == instance->fd);
411 "SocketFactory_Reversed_SSH_Tunnel:sfsrtun_connector_finalizer");
412 return again;
413}
414
415
416/**************************************************************/
419{
420 padico_warning("%s\n", error);
421 padico_rc_t rc = padico_rc_error("Error: %s", error);
423}
424
425
427{
428 const char*s_id = puk_parse_getattr(e, "id");
429 const unsigned long id = atoi(s_id);
430 padico_out(50, "close request for id=%lu.\n", id);
431}
432
433
434/* connection from gateway to the server socket */
436{
437 /* Retrieve parameters */
438 char error[64] = {0};
439 const char*s_client_host = puk_parse_getattr(e, "clientHost");
440 const char*s_client_port = puk_parse_getattr(e, "clientPort");
441 const char*s_server_port = puk_parse_getattr(e, "serverPort");
442 const char*s_ssh_cipher = puk_parse_getattr(e, "cipher");
443 const char*s_ssh_path = puk_parse_getattr(e, "sshPath");
445 padico_out(20, "start server reversed tunnel: client host=%s client port=%s server port=%s..\n",
447
448 /* Check client existence */
450 if (padico_client_host == NULL) {
451 sprintf(error, "client host=%s not found", s_client_host);
453 return;
454 }
455
456 // TUNNEL
457 pid_t pid = -1;
458 int rc = -1, err = -1;
459 if(sfsrtun_server.pid == -1) {
461 pid = fork();
464 if(pid == -1) {
465 padico_warning("fork() failed!\n");
466 }
467 else if(pid == 0) {
468 /* Get server hostname */
469/* struct in_addr server_host; */
470/* inet_aton("127.0.0.1", &server_host); */
471/* const char*s_server_host = padico_strdup(inet_ntoa(server_host)); */
472
473 /* build ssh reversed tunnel string */
474/* padico_topo_node_getname(padico_topo_getlocalnode()); */
475/* struct in_addr inaddr; */
476/* inet_aton("127.0.0.1", &inaddr); */
477/* const char*ssh_tunnel_host = padico_strdup(inet_ntoa(inaddr)); */
479/* padico_string_printf(s_ssh_tunnel, "%d:%s:%d", s_client_port, s_server_host, s_server_port); */
482
483 // TODO: verifier s'il ne faut pas plustot l'ip
484
486 const char*argv[] =
487 {
488 s_ssh_path, /* path to ssh- default: /usr/bin/ssh */
489 "-n", /* no stdin */
490 "-T", /* no pseudo-tty */
491 "-N", /* no remote command */
492 "-e", "none", /* disable escape sequences */
493 "-R", ssh_tunnel, /* the reversed tunnel itself */
494 "-c", s_ssh_cipher, /* cipher- default: blowfish (much faster than 3des) */
495 s_client_host, /* our gateway host */
496 NULL /* NULL-terminated array */
497 };
498
499 padico_print("ssh_path=%s; ssh_tunnel=%s; ssh_host=%s; cipher=%s\n",
501 putenv("LD_LIBRARY_PATH=");
503 puk_abi_real_errno() = 0;
504 rc = PUK_ABI_PROC_REAL(execv)(s_ssh_path, (char**)argv);
507 padico_fatal("exec() failed for ssh- rc=%d; errno=%d (%s).\n", rc, err, strerror(err));
508 } else {
509 /* father: PadicoTM process */
510 sfsrtun_server.pid = pid;
511 padico_out(10, "father: pid that does not fork :>\n");
512 }
513 }
514}
assert(slab_size > 0)
puk_component_t assembly
Serial assembly used.
struct ns_rulebucket_hash_entry_s * e
const const struct ns_rule_bucket_s * key
return NULL
Definition Shm.h:88
goto retry
Definition Shm.h:88
void(* padico_socketfactory_connector_t)(void *key, int fd)
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
static void padico_control_send_oneway(padico_topo_node_t node, const char *msg)
Send a "one way" message on the control channel.
static padico_req_t padico_control_send(padico_topo_node_t node, const char *cmd)
Send a blocking request on the control channel.
void padico_tm_bgthread_start(padico_tm_bgthread_pool_t pool, padico_thread_fun_t f, void *arg, const char *name)
Definition tm-rttask.c:126
static void puk_spinlock_release(void)
Definition PadicoTM.h:83
void padico_tm_bgthread_pool_wait(padico_tm_bgthread_pool_t pool)
finalize a pool of threads: waits for all thread to complete
Definition tm-rttask.c:78
padico_tm_bgthread_pool_t padico_tm_bgthread_pool_create(const char *name)
create a new thread pool
Definition tm-rttask.c:56
#define PUK_ABI_PROC_REAL(SYM)
Definition PadicoTM.h:91
padico_rc_t padico_tm_req_wait(padico_req_t req)
static void puk_spinlock_acquire(void)
Definition PadicoTM.h:81
#define puk_abi_real_errno()
Definition PadicoTM.h:110
#define PUK_ABI_FSYS_WRAP(SYM)
Definition PadicoTM.h:88
padico_rc_t rc
Definition PadicoTM.h:146
void padico_tm_msleep(long msec)
Definition tm-boot.c:384
#define __puk_abi_wrap__errno
Definition PadicoTM.h:101
static const struct puk_component_driver_s sfsrtun_component_driver
instanciation facet for SocketFactory-srtun
Definition sf-srtun.c:71
static const struct padico_socketfactory_s sfsrtun_sf_driver
'SocketFactory' facet for SocketFactory-srtun
Definition sf-srtun.c:55
int padico_sysio_errno(void)
Definition sysio.c:575
int padico_sysio_socket(int domain, int type, int protocol)
Definition sysio.c:604
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
#define PADICO_IO_EVENT_WRITE
the fildes is writable
Definition SysIO.h:63
#define padico_io_t
for backwards compatibility with legacy codes
Definition SysIO.h:53
void padico_io_deactivate(padico_sysio_t io)
Deactivate the I/O handle - should be used for temporarly deactivate callback trigger.
Definition sysio.c:288
static padico_topo_node_t padico_topo_host_getnode(padico_topo_host_t host)
Get the first node of a host.
Definition Topology.h:220
#define padico_topo_hostname()
Definition Topology.h:320
padico_topo_host_t padico_topo_host_resolve_byinaddr(const struct in_addr *addr)
Get a host description record from an inet address (create if it didn't exist)
Definition topology.c:2452
#define padico_topo_getlocalhost()
Definition Topology.h:319
const char * padico_topo_host_getname(padico_topo_host_t host)
Get the canonical name of a host.
Definition topology.c:1944
padico_topo_host_t padico_topo_host_resolvebyname(const char *hostname)
resolve a host from name
Definition topology.c:2681
mx_request_t req
Definition psp-mx.c:103
static void sfsrtun_module_finalize(void)
Definition sf-srtun.c:159
static int sfsrtun_create(void *_instance, int family, int type, int protocol)
Definition sf-srtun.c:215
static void sfsrtun_server_set_error(puk_parse_entity_t e, char *error)
Server.
Definition sf-srtun.c:418
padico_tm_bgthread_pool_t pool
Definition sf-srtun.c:107
static void * sfsrtun_instantiate(puk_instance_t, puk_context_t)
Definition sf-srtun.c:167
pthread_mutex_t connections_mtx
Definition sf-srtun.c:106
static void sfsrtun_connect_server_end_handler(puk_parse_entity_t e)
Definition sf-srtun.c:435
static int sfsrtun_connect(void *_instance, const struct sockaddr *addr, socklen_t addrlen, padico_socketfactory_connector_t notifier, void *key)
Definition sf-srtun.c:281
static int sfsrtun_callback_connector(int fd, void *key)
Definition sf-srtun.c:404
static int sfsrtun_launch_server(struct sfsrtun_instance_s *instance, struct sockaddr_in *inaddr)
Definition sf-srtun.c:234
unsigned int next_id
Definition sf-srtun.c:105
static struct @84 sfsrtun_server
static void * sfsrtun_connector_finalizer(void *_instance)
Definition sf-srtun.c:397
static int sfsrtun_module_init(void)
Definition sf-srtun.c:124
static void sfsrtun_destroy(void *)
Definition sf-srtun.c:191
int port_offset
Definition sf-srtun.c:102
static int sfsrtun_close(void *_instance)
Definition sf-srtun.c:361
puk_iface_t iface_sf
Definition sf-srtun.c:104
static void sfsrtun_close_end_handler(puk_parse_entity_t e)
Definition sf-srtun.c:426
static struct @83 sfsrtun
int pid
Definition sf-srtun.c:112
int(* create)(void *_status, int family, int type, int protocol)
create the socket attached to the SocketFactory instance
a thread pool, to control and wait running threads
Definition PadicoTM.h:174
instance of SocketFactory-srtun
Definition sf-srtun.c:81
unsigned int server_id
Definition sf-srtun.c:95
padico_sysio_t io_connect
Definition sf-srtun.c:83
const char * ssh_host
Definition sf-srtun.c:88
const char * ssh_path
Definition sf-srtun.c:87
padico_socketfactory_connector_t connector
Definition sf-srtun.c:84
const char * ssh_cipher
Definition sf-srtun.c:90
void * connector_key
Definition sf-srtun.c:85
padico_topo_host_t server
Definition sf-srtun.c:96