PadicoTM

Documentation

« back to PM2 home.
sf-rfw.c
Go to the documentation of this file.
1/*
2 * Padico -- a High Performance Parallel and Distributed Computing Environment
3 * Copyright (c) 2002-2026 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
29#include <Padico/VLink-internals.h>
30#include <Padico/Module.h>
31
32#include "SocketFactory.h"
33
34static int sfrfw_module_init(void);
35static void sfrfw_module_finalize(void);
36
38
48/* ********************************************************* */
49
50static int sfrfw_create(void*_status, int family, int type, int protocol);
51static int sfrfw_connect(void*_status, const struct sockaddr*_addr, socklen_t addrlen,
53static int sfrfw_close(void*_status);
54
59{
61 .bind = NULL,
62 .listen = NULL,
63 .connect = &sfrfw_connect,
64 .close = &sfrfw_close,
66};
67
69static void sfrfw_destroy(void*);
70
75{
76 .instantiate = &sfrfw_instantiate,
77 .destroy = &sfrfw_destroy
78};
79
90
105
108
109static struct
110{
112 struct sfrfw_connections_hashtable_s connections; /* hash: id -> status */
113 unsigned int next_id;
117} sfrfw_gateway = { .component = NULL };
118
119/* ********************************************************* */
120
123
124static int sfrfw_module_init(void)
125{
126 sfrfw_gateway.component =
127 puk_component_declare("SocketFactory_Reverse_Forward",
128 puk_component_provides("PadicoComponent", "component", &sfrfw_component_driver),
129 puk_component_provides("SocketFactory", "prov-sf", &sfrfw_sf_driver),
130 puk_component_uses("SocketFactory", "used-sf"),
131 puk_component_attr("gateway_node_name", NULL));
133 .xml_tag = "SocketFactory_Reverse_Forward:Connect",
134 .start_handler = NULL,
136 .required_level = PUK_TRUST_CONTROL
137 });
139 .xml_tag = "SocketFactory_Reverse_Forward:Close",
140 .start_handler = NULL,
141 .end_handler = &sfrfw_gateway_close_end_handler,
142 .required_level = PUK_TRUST_CONTROL
143 });
144 sfrfw_gateway.iface_sf = puk_iface_lookup("SocketFactory");
146 sfrfw_gateway.next_id = 1;
147 marcel_mutex_init(&sfrfw_gateway.connections_mtx, 0);
148 sfrfw_gateway.pool = padico_tm_bgthread_pool_create("SocketFactory_Reverse_Forward");
149 return 0;
150}
151
152static void sfrfw_module_finalize(void)
153{
155 puk_xml_delete_action("SocketFactory_Reverse_Forward:Connect");
156 puk_xml_delete_action("SocketFactory_Reverse_Forward:Close");
159}
160
161
162/* ********************************************************* */
163/* *** 'PadicoComponent' for ReverseForward SocketFactory */
164
166{
167 struct sfrfw_status_s*status = padico_malloc(sizeof(struct sfrfw_status_s));
168 *status = (struct sfrfw_status_s) {
170 .context = context,
171 .gateway = NULL,
172 .gateway_id = 0
173 };
174 padico_out(40, "done.\n");
175 return status;
176}
177
178static void sfrfw_destroy(void*_status)
179{
180 struct sfrfw_status_s*status = _status;
181 sfrfw_close(status);
182 padico_free(status);
183}
184
185/* ********************************************************* */
186/* *** 'SocketFactory' for ReverseForwardSocketFactory */
187
188static int sfrfw_create(void*_status, int family, int type, int protocol)
189{
190 return 0;
191}
192
199static void sfrfw_client_acceptor(void*key, int new_fd, const struct sockaddr*addr, socklen_t addrlen);
200static int sfrfw_launch_gateway(const char*server_host, const uint16_t server_port,
201 struct sfrfw_status_s*status, const uint16_t client_port);
202
203static int sfrfw_connect(void*_status, const struct sockaddr*_addr, socklen_t addrlen,
205{
206 struct sfrfw_status_s*status = _status;
207 const struct sockaddr_in*server_addr = (struct sockaddr_in*) _addr;
208 int server_port = ntohs(server_addr->sin_port);
212 {
213 padico_warning("connect: host=%s not found\n", server_host);
214 return -1;
215 }
218 int rc = (*sf.driver->create)(sf._status, AF_INET, SOCK_STREAM, 0);
219 if(rc != 0)
220 {
221 padico_warning("connect: cannot create the socket for host=%s\n", server_host);
222 return -1;
223 }
225 int client_addrlen = sizeof(struct sockaddr_in);
226 bzero(&client_addr, sizeof(client_addr));
227 client_addr.sin_family = AF_INET;
228 client_addr.sin_addr.s_addr = htonl(INADDR_ANY);
229 client_addr.sin_port = 0;
231 if(rc >= 0)
232 {
234 *param = (struct sfrfw_connector_param_s) {
235 .status = status,
236 .notifier = notifier,
237 .key = key
238 };
241 }
242 if(rc != 0)
243 {
244 padico_warning("connect: bind or listen failed.\n");
245 return rc;
246 }
247 /* TODO: if the attribute is not set we could take the server host as gateway */
248 const char*gateway_node_name = puk_context_getattr(status->context, "gateway_node_name");
250 if(status->gateway == NULL)
251 {
252 padico_warning("launch_gateway(): unknown gateway node=%s\n",
254 return -1;
255 }
257 if(rc == 0)
258 rc = -EINPROGRESS;
259 return rc;
260}
261
262static void sfrfw_client_acceptor(void*key, int new_fd, const struct sockaddr*addr, socklen_t addrlen)
263{
265
266 padico_out(50, "close listening socket.\n");
268 puk_instance_context_indirect_SocketFactory(param->status->instance, "used-sf", &sf);
269 (*sf.driver->close)(sf._status);
270 padico_out(10, "connected.\n");
271 (param->notifier)(param->key, new_fd);
272
274}
275
277{
278 struct sfrfw_status_s*status = _key;
279 int err = padico_rc_iserror(rc);
280 if(err != 0)
281 {
282 padico_warning("Error: remote connection failed: %s.\n",
284 err = -1;
285 }
286 else
287 {
288 char*s_id = padico_rc_gettext(rc);
289 status->gateway_id = atoi(s_id);
290 padico_out(10, "launched gateway on node=%s, id=%d\n",
292 }
294}
295
297 struct sfrfw_status_s*status, const uint16_t client_port)
298{
299 padico_out(20, "init gateway side (gateway node=%s, local port=%d, host=%s:%d)..\n",
302 padico_string_catf(s_launch,"<SocketFactory_Reverse_Forward:Connect serverHost=\"%s\" serverPort=\"%d\" " ,
304 padico_string_catf(s_launch,"clientHost=\"%s\" clientPort=\"%d\"/>",
305 padico_topo_hostname(), client_port);
308
309 return -EINPROGRESS;
310}
311
312static int sfrfw_close(void*_status)
313{
314 struct sfrfw_status_s*status = _status;
315 if(status->gateway_id != 0)
316 {
317 //TODO: not useful? the gateway can see when the socket close.
318 padico_out(10, "closing gateway=%s id=%d.\n", padico_topo_node_getname(status->gateway), status->gateway_id);
320 padico_string_catf(s_close,"<SocketFactory_Reverse_Forward:Close id=\"%d\"/>", status->gateway_id);
323 }
324 padico_out(10, "closed.\n");
325 return 0;
326}
327
328/* ********************************************************* */
329/* *** gateway */
330
333static void sfrfw_gateway_set_error(puk_parse_entity_t e, char*error);
334static void sfrfw_gateway_server_callback_connector(void*key, int fd);
335static const int GATEWAY_READ_BUFSIZE = 4 * 1024;
336static int sfrfw_gateway_server_read_callback(int fd, void*key);
338static void sfrfw_gateway_client_callback_connector(void*key, int fd);
339static int sfrfw_gateway_client_read_callback(int fd, void*key);
340static void*sfrfw_gateway_error(void*_status);
341static void sfrfw_gateway_close(struct sfrfw_gateway_status_s*status);
342
343
344/* connection from gateway to the server socket */
346{
347 char error[64] = {0};
348 const char*s_server_host = puk_parse_getattr(e, "serverHost");
349 const char*s_server_port = puk_parse_getattr(e, "serverPort");
350 const char*s_client_host = puk_parse_getattr(e, "clientHost");
351 const char*s_client_port = puk_parse_getattr(e, "clientPort");
353 padico_out(20, "start gateway: host=%s port=%s client host=%s..\n",
357 {
358 sprintf(error, "server host=%s not found", s_server_host);
360 return;
361 }
364 padico_out(20, "connecting to server host=%s port=%s ..\n", s_server_host, s_server_port);
365
367 status->client_host = client_host;
368 status->client_port = atol(s_client_port);
372 int rc = (*sf.driver->create)(sf._status, AF_INET, SOCK_STREAM, 0);
373 if(rc != 0)
374 {
375 sprintf(error, "cannot create the socket for host=%s", s_server_host);
377 sfrfw_gateway_error(status);
378 return;//TODO free
379 }
381 bzero(&server_addr, sizeof(server_addr));
382 server_addr.sin_family = AF_INET;
385 rc = (*sf.driver->connect)(sf._status, (struct sockaddr*) &server_addr, sizeof(server_addr),
387 if(rc != -EINPROGRESS && rc != 0)
388 {
389 sprintf(error, "connection failed towards host=%s port=%s", s_server_host, s_server_port);
391 sfrfw_gateway_error(status);
392 return;
393 }
394 puk_parse_set_rc(e, padico_rc_msg("%d", status->id));
395}
396
398{
399 struct sfrfw_gateway_status_s*status = padico_malloc(sizeof(struct sfrfw_gateway_status_s));
400 marcel_mutex_lock(&sfrfw_gateway.connections_mtx);
401 *status = (struct sfrfw_gateway_status_s) {
402 .client_host = NULL,
403 .client_port = 0,
404 .id = sfrfw_gateway.next_id++, /* int is big enough? */
405 .server_instance = NULL,
406 .server_fd = -1,
407 .server_io = NULL,
408 .client_instance = NULL,
409 .client_fd = -1,
410 .client_io = NULL
411 };
412 sfrfw_connections_hashtable_insert(&sfrfw_gateway.connections, status->id, status);
413 marcel_mutex_unlock(&sfrfw_gateway.connections_mtx);
414 return status;
415}
416
426
428{
429 padico_warning("%s\n", error);
430 padico_rc_t rc = padico_rc_error("Error: %s", error);
432}
433
435{
436 struct sfrfw_gateway_status_s*status = (struct sfrfw_gateway_status_s*) key;
437 status->server_fd = fd;
439 padico_out(10, "connection to server host done (fd=%d).\n", fd);
441}
442
444{
445 struct sfrfw_gateway_status_s*status = key;
446 char buf[GATEWAY_READ_BUFSIZE];
447 int sysrc = padico_sysio_read(fd, &buf, sizeof(buf));
448 if(sysrc > 0)
449 {
450 padico_out(50, "size : %d \n", sysrc);
451 padico_sysio_out(status->client_fd, &buf, sysrc);
452 return 1;
453 }
454 else
455 {
456 padico_out(20, "connection lost\n");
457 padico_tm_bgthread_start(sfrfw_gateway.pool, &sfrfw_gateway_error, status, "gateway_error");
458 return 0;
459 }
460}
461
463{
464 padico_out(20, "reverse connect to client node=%s ..\n", padico_topo_host_getname(status->client_host));
468 int rc = (*sf.driver->create)(sf._status, AF_INET, SOCK_STREAM, 0);
469 if(rc != 0)
470 {
471 padico_warning("reverse connect: cannot create the socket for node=%s\n",
473 padico_tm_bgthread_start(sfrfw_gateway.pool, &sfrfw_gateway_error, status, "gateway_error");
474 //TODO: the sockets of the gateway will be closed but the client will not be informed of the error.
475 return;
476 }
478 int client_addrlen = sizeof(struct sockaddr_in);
479 bzero(&client_addr, sizeof(client_addr));
480 client_addr.sin_family = AF_INET;
482 client_addr.sin_port = status->client_port;
483 padico_out(10, "connect to addr=%s port=%d\n", inet_ntoa(client_addr.sin_addr), ntohs(status->client_port));
486 if(rc != -EINPROGRESS && rc != 0)
487 {
488 padico_warning("reverse connect failed (client node=%s).\n",
490 padico_tm_bgthread_start(sfrfw_gateway.pool, &sfrfw_gateway_error, status, "gateway_error");
491 //TODO: the sockets of the gateway will be closed but the client will not be informed of the error.
492 }
493}
494
496{
497 struct sfrfw_gateway_status_s*status = key;
498 padico_out(10, "reverse connection to client node=%s ok (fd=%d).\n",
500 status->client_fd = fd;
504}
505
507{
508 struct sfrfw_gateway_status_s*status = key;
509 char buf[GATEWAY_READ_BUFSIZE];
510 int sysrc = padico_sysio_read(fd, &buf, sizeof(buf));
511 if(sysrc > 0)
512 {
513 padico_out(50, "size : %d \n", sysrc);
514 padico_sysio_out(status->server_fd, &buf, sysrc);
515 return 1;
516 }
517 else
518 {
519 padico_out(20, "connection lost\n");
520 padico_tm_bgthread_start(sfrfw_gateway.pool, &sfrfw_gateway_error, status, "gateway_error");
521 return 0;
522 }
523}
524
525static void*sfrfw_gateway_error(void*_status)
526{
527 struct sfrfw_gateway_status_s*status = _status;
528 marcel_mutex_lock(&sfrfw_gateway.connections_mtx);
530 marcel_mutex_unlock(&sfrfw_gateway.connections_mtx);
531
532 sfrfw_gateway_close(status);
533 return NULL;
534}
535
537{
538 padico_out(10, "closing connection id=%tu\n", status->id);
539 if(status->server_io)
540 {
543 status->server_io = NULL;
544 }
545 if(status->server_instance)
546 {
549 (*sf.driver->close)(sf._status);
550 }
551 if(status->client_io)
552 {
555 status->client_io = NULL;
556 }
557 if(status->client_instance)
558 {
561 (*sf.driver->close)(sf._status);
562 }
563 padico_free(status);
564}
565
567{
568 const char*s_id = puk_parse_getattr(e, "id");
569 const uintptr_t id = atoi(s_id);
570 padico_out(50, "close request for id=%tu.\n", id);
571 marcel_mutex_lock(&sfrfw_gateway.connections_mtx);
573 if(status)
575 marcel_mutex_unlock(&sfrfw_gateway.connections_mtx);
576 if(status)
577 sfrfw_gateway_close(status);
578}
579
580/* TODO: unload module function */
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
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)
static void puk_instance_indirect_SocketFactory(puk_instance_t instance, const char *label, struct puk_receptacle_SocketFactory_s *receptacle)
static int done
Definition console-mod.c:49
puk_instance_t instance
the listening bootstrap instance
static int fd
Definition datacheck.c:32
puk_component_t padico_ns_host_selector(padico_topo_host_t host, const char *profile, puk_iface_t iface, uint16_t port, int protocol)
static void padico_control_send_async(padico_topo_node_t node, const char *cmd, padico_task_notifier_t notify, void *key)
Send an async request on the control channel.
static void padico_control_send_oneway(padico_topo_node_t node, const char *msg)
Send a "one way" message 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
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
padico_rc_t rc
Definition PadicoTM.h:146
static const struct puk_component_driver_s sfrfw_component_driver
instanciation facet for SocketFactory-reverse-forward
Definition sf-rfw.c:74
static const struct padico_socketfactory_s sfrfw_sf_driver
'SocketFactory' facet for SocketFactory-reverse-forward
Definition sf-rfw.c:58
#define PADICO_IO_EVENT_READ
the fildes is readable
Definition SysIO.h:62
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_read(int fildes, void *buf, ssize_t nbyte)
Definition sysio.c:620
#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
ssize_t padico_sysio_out(int fildes, const void *buf, size_t nbyte)
Definition sysio.c:808
#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
const char * padico_topo_node_getname(padico_topo_node_t node)
Get the canonical name of a node.
Definition topology.c:1917
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_node_t padico_topo_getnodebyname(const char *nodename)
node lookup, by node name
Definition topology.c:2389
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
struct @32::@33 param
struct puk_receptacle_SocketFactory_s sf
static struct @73 sfrfw_gateway
puk_component_t component
Definition sf-rfw.c:116
static int sfrfw_gateway_server_read_callback(int fd, void *key)
Definition sf-rfw.c:443
static puk_instance_t sfrfw_gateway_get_socket(padico_topo_host_t padico_server_host, uint16_t port)
Definition sf-rfw.c:417
static void sfrfw_client_acceptor(void *key, int new_fd, const struct sockaddr *addr, socklen_t addrlen)
Definition sf-rfw.c:262
static void sfrfw_gateway_set_error(puk_parse_entity_t e, char *error)
Definition sf-rfw.c:427
static void sfrfw_gateway_server_callback_connector(void *key, int fd)
Definition sf-rfw.c:434
padico_tm_bgthread_pool_t pool
Definition sf-rfw.c:115
static void sfrfw_gateway_connect_client(struct sfrfw_gateway_status_s *status)
Definition sf-rfw.c:462
static int sfrfw_close(void *_status)
Definition sf-rfw.c:312
static void * sfrfw_gateway_error(void *_status)
Definition sf-rfw.c:525
pthread_mutex_t connections_mtx
Definition sf-rfw.c:114
static void sfrfw_module_finalize(void)
Definition sf-rfw.c:152
static void sfrfw_destroy(void *)
Definition sf-rfw.c:178
struct sfrfw_connections_hashtable_s connections
Definition sf-rfw.c:112
static int sfrfw_launch_gateway(const char *server_host, const uint16_t server_port, struct sfrfw_status_s *status, const uint16_t client_port)
Definition sf-rfw.c:296
static void sfrfw_gateway_close_end_handler(puk_parse_entity_t e)
Definition sf-rfw.c:566
unsigned int next_id
Definition sf-rfw.c:113
static void sfrfw_gateway_connect_server_end_handler(puk_parse_entity_t e)
Definition sf-rfw.c:345
static int sfrfw_module_init(void)
Definition sf-rfw.c:124
static int sfrfw_gateway_client_read_callback(int fd, void *key)
Definition sf-rfw.c:506
static void sfrfw_gateway_close(struct sfrfw_gateway_status_s *status)
Definition sf-rfw.c:536
static void * sfrfw_instantiate(puk_instance_t, puk_context_t)
Definition sf-rfw.c:165
static int sfrfw_connect(void *_status, const struct sockaddr *_addr, socklen_t addrlen, padico_socketfactory_connector_t notifier, void *key)
Definition sf-rfw.c:203
static const int GATEWAY_READ_BUFSIZE
Definition sf-rfw.c:335
static void sfrfw_launch_gateway_notifer(padico_rc_t rc, void *_key)
Definition sf-rfw.c:276
puk_iface_t iface_sf
Definition sf-rfw.c:111
static int sfrfw_create(void *_status, int family, int type, int protocol)
Definition sf-rfw.c:188
static struct sfrfw_gateway_status_s * sfrfw_gateway_new()
Definition sf-rfw.c:397
static void sfrfw_gateway_client_callback_connector(void *key, int fd)
Definition sf-rfw.c:495
int(* create)(void *_status, int family, int type, int protocol)
create the socket attached to the SocketFactory instance
int(* listen)(void *_status, int backlog, const struct sockaddr *addr, socklen_t addrlen, padico_socketfactory_acceptor_t notifier, void *key)
listens incoming connections on the socket
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
int(* bind)(void *_status, struct sockaddr *addr, socklen_t *addrlen)
binds the socket to the given address (expected: AF_INET family) or performs autobind (AF_PADICO fami...
a thread pool, to control and wait running threads
Definition PadicoTM.h:174
struct padico_socketfactory_s * driver
padico_socketfactory_connector_t notifier
Definition sf-rfw.c:196
struct sfrfw_status_s * status
Definition sf-rfw.c:195
padico_sysio_t client_io
Definition sf-rfw.c:103
puk_instance_t server_instance
Definition sf-rfw.c:95
puk_instance_t client_instance
Definition sf-rfw.c:101
padico_sysio_t server_io
Definition sf-rfw.c:97
padico_topo_host_t client_host
Definition sf-rfw.c:99
instance of SocketFactory-reverse-forward
Definition sf-rfw.c:84
unsigned int gateway_id
Definition sf-rfw.c:88
puk_instance_t instance
Definition sf-rfw.c:85
padico_topo_node_t gateway
Definition sf-rfw.c:87
puk_context_t context
Definition sf-rfw.c:86
static int port
Definition vrp-test.c:15