1 /* SPDX-License-Identifier: LGPL-2.1+ */
12 #include "sd-daemon.h"
14 #include "sd-resolve.h"
16 #include "alloc-util.h"
17 #include "errno-util.h"
20 #include "main-func.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "pretty-print.h"
24 #include "resolve-private.h"
26 #include "socket-util.h"
27 #include "string-util.h"
30 #define BUFFER_SIZE (256 * 1024)
32 static unsigned arg_connections_max
= 256;
33 static const char *arg_remote_host
= NULL
;
35 typedef struct Context
{
43 typedef struct Connection
{
46 int server_fd
, client_fd
;
47 int server_to_client_buffer
[2]; /* a pipe */
48 int client_to_server_buffer
[2]; /* a pipe */
50 size_t server_to_client_buffer_full
, client_to_server_buffer_full
;
51 size_t server_to_client_buffer_size
, client_to_server_buffer_size
;
53 sd_event_source
*server_event_source
, *client_event_source
;
55 sd_resolve_query
*resolve_query
;
58 static void connection_free(Connection
*c
) {
62 set_remove(c
->context
->connections
, c
);
64 sd_event_source_unref(c
->server_event_source
);
65 sd_event_source_unref(c
->client_event_source
);
67 safe_close(c
->server_fd
);
68 safe_close(c
->client_fd
);
70 safe_close_pair(c
->server_to_client_buffer
);
71 safe_close_pair(c
->client_to_server_buffer
);
73 sd_resolve_query_unref(c
->resolve_query
);
78 static void context_clear(Context
*context
) {
81 set_free_with_destructor(context
->listen
, sd_event_source_unref
);
82 set_free_with_destructor(context
->connections
, connection_free
);
84 sd_event_unref(context
->event
);
85 sd_resolve_unref(context
->resolve
);
88 static int connection_create_pipes(Connection
*c
, int buffer
[static 2], size_t *sz
) {
98 r
= pipe2(buffer
, O_CLOEXEC
|O_NONBLOCK
);
100 return log_error_errno(errno
, "Failed to allocate pipe buffer: %m");
102 (void) fcntl(buffer
[0], F_SETPIPE_SZ
, BUFFER_SIZE
);
104 r
= fcntl(buffer
[0], F_GETPIPE_SZ
);
106 return log_error_errno(errno
, "Failed to get pipe buffer size: %m");
114 static int connection_shovel(
116 int *from
, int buffer
[2], int *to
,
117 size_t *full
, size_t *sz
,
118 sd_event_source
**from_source
, sd_event_source
**to_source
) {
125 assert(buffer
[0] >= 0);
126 assert(buffer
[1] >= 0);
138 if (*full
< *sz
&& *from
>= 0 && *to
>= 0) {
139 z
= splice(*from
, NULL
, buffer
[1], NULL
, *sz
- *full
, SPLICE_F_MOVE
|SPLICE_F_NONBLOCK
);
143 } else if (z
== 0 || ERRNO_IS_DISCONNECT(errno
)) {
144 *from_source
= sd_event_source_unref(*from_source
);
145 *from
= safe_close(*from
);
146 } else if (!IN_SET(errno
, EAGAIN
, EINTR
))
147 return log_error_errno(errno
, "Failed to splice: %m");
150 if (*full
> 0 && *to
>= 0) {
151 z
= splice(buffer
[0], NULL
, *to
, NULL
, *full
, SPLICE_F_MOVE
|SPLICE_F_NONBLOCK
);
155 } else if (z
== 0 || ERRNO_IS_DISCONNECT(errno
)) {
156 *to_source
= sd_event_source_unref(*to_source
);
157 *to
= safe_close(*to
);
158 } else if (!IN_SET(errno
, EAGAIN
, EINTR
))
159 return log_error_errno(errno
, "Failed to splice: %m");
166 static int connection_enable_event_sources(Connection
*c
);
168 static int traffic_cb(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
169 Connection
*c
= userdata
;
176 r
= connection_shovel(c
,
177 &c
->server_fd
, c
->server_to_client_buffer
, &c
->client_fd
,
178 &c
->server_to_client_buffer_full
, &c
->server_to_client_buffer_size
,
179 &c
->server_event_source
, &c
->client_event_source
);
183 r
= connection_shovel(c
,
184 &c
->client_fd
, c
->client_to_server_buffer
, &c
->server_fd
,
185 &c
->client_to_server_buffer_full
, &c
->client_to_server_buffer_size
,
186 &c
->client_event_source
, &c
->server_event_source
);
190 /* EOF on both sides? */
191 if (c
->server_fd
== -1 && c
->client_fd
== -1)
194 /* Server closed, and all data written to client? */
195 if (c
->server_fd
== -1 && c
->server_to_client_buffer_full
<= 0)
198 /* Client closed, and all data written to server? */
199 if (c
->client_fd
== -1 && c
->client_to_server_buffer_full
<= 0)
202 r
= connection_enable_event_sources(c
);
210 return 0; /* ignore errors, continue serving */
213 static int connection_enable_event_sources(Connection
*c
) {
214 uint32_t a
= 0, b
= 0;
219 if (c
->server_to_client_buffer_full
> 0)
221 if (c
->server_to_client_buffer_full
< c
->server_to_client_buffer_size
)
224 if (c
->client_to_server_buffer_full
> 0)
226 if (c
->client_to_server_buffer_full
< c
->client_to_server_buffer_size
)
229 if (c
->server_event_source
)
230 r
= sd_event_source_set_io_events(c
->server_event_source
, a
);
231 else if (c
->server_fd
>= 0)
232 r
= sd_event_add_io(c
->context
->event
, &c
->server_event_source
, c
->server_fd
, a
, traffic_cb
, c
);
237 return log_error_errno(r
, "Failed to set up server event source: %m");
239 if (c
->client_event_source
)
240 r
= sd_event_source_set_io_events(c
->client_event_source
, b
);
241 else if (c
->client_fd
>= 0)
242 r
= sd_event_add_io(c
->context
->event
, &c
->client_event_source
, c
->client_fd
, b
, traffic_cb
, c
);
247 return log_error_errno(r
, "Failed to set up client event source: %m");
252 static int connection_complete(Connection
*c
) {
257 r
= connection_create_pipes(c
, c
->server_to_client_buffer
, &c
->server_to_client_buffer_size
);
261 r
= connection_create_pipes(c
, c
->client_to_server_buffer
, &c
->client_to_server_buffer_size
);
265 r
= connection_enable_event_sources(c
);
273 return 0; /* ignore errors, continue serving */
276 static int connect_cb(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
277 Connection
*c
= userdata
;
285 solen
= sizeof(error
);
286 r
= getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &error
, &solen
);
288 log_error_errno(errno
, "Failed to issue SO_ERROR: %m");
293 log_error_errno(error
, "Failed to connect to remote host: %m");
297 c
->client_event_source
= sd_event_source_unref(c
->client_event_source
);
299 return connection_complete(c
);
303 return 0; /* ignore errors, continue serving */
306 static int connection_start(Connection
*c
, struct sockaddr
*sa
, socklen_t salen
) {
313 c
->client_fd
= socket(sa
->sa_family
, SOCK_STREAM
|SOCK_NONBLOCK
|SOCK_CLOEXEC
, 0);
314 if (c
->client_fd
< 0) {
315 log_error_errno(errno
, "Failed to get remote socket: %m");
319 r
= connect(c
->client_fd
, sa
, salen
);
321 if (errno
== EINPROGRESS
) {
322 r
= sd_event_add_io(c
->context
->event
, &c
->client_event_source
, c
->client_fd
, EPOLLOUT
, connect_cb
, c
);
324 log_error_errno(r
, "Failed to add connection socket: %m");
328 r
= sd_event_source_set_enabled(c
->client_event_source
, SD_EVENT_ONESHOT
);
330 log_error_errno(r
, "Failed to enable oneshot event source: %m");
334 log_error_errno(errno
, "Failed to connect to remote host: %m");
338 r
= connection_complete(c
);
347 return 0; /* ignore errors, continue serving */
350 static int resolve_handler(sd_resolve_query
*q
, int ret
, const struct addrinfo
*ai
, Connection
*c
) {
355 log_error("Failed to resolve host: %s", gai_strerror(ret
));
359 c
->resolve_query
= sd_resolve_query_unref(c
->resolve_query
);
361 return connection_start(c
, ai
->ai_addr
, ai
->ai_addrlen
);
365 return 0; /* ignore errors, continue serving */
368 static int resolve_remote(Connection
*c
) {
370 static const struct addrinfo hints
= {
371 .ai_family
= AF_UNSPEC
,
372 .ai_socktype
= SOCK_STREAM
,
373 .ai_flags
= AI_ADDRCONFIG
376 union sockaddr_union sa
= {};
377 const char *node
, *service
;
380 if (IN_SET(arg_remote_host
[0], '/', '@')) {
383 salen
= sockaddr_un_set_path(&sa
.un
, arg_remote_host
);
385 log_error_errno(salen
, "Specified address doesn't fit in an AF_UNIX address, refusing: %m");
389 return connection_start(c
, &sa
.sa
, salen
);
392 service
= strrchr(arg_remote_host
, ':');
394 node
= strndupa(arg_remote_host
, service
- arg_remote_host
);
397 node
= arg_remote_host
;
401 log_debug("Looking up address info for %s:%s", node
, service
);
402 r
= resolve_getaddrinfo(c
->context
->resolve
, &c
->resolve_query
, node
, service
, &hints
, resolve_handler
, NULL
, c
);
404 log_error_errno(r
, "Failed to resolve remote host: %m");
412 return 0; /* ignore errors, continue serving */
415 static int add_connection_socket(Context
*context
, int fd
) {
422 if (set_size(context
->connections
) > arg_connections_max
) {
423 log_warning("Hit connection limit, refusing connection.");
428 r
= set_ensure_allocated(&context
->connections
, NULL
);
434 c
= new0(Connection
, 1);
440 c
->context
= context
;
443 c
->server_to_client_buffer
[0] = c
->server_to_client_buffer
[1] = -1;
444 c
->client_to_server_buffer
[0] = c
->client_to_server_buffer
[1] = -1;
446 r
= set_put(context
->connections
, c
);
453 return resolve_remote(c
);
456 static int accept_cb(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
457 _cleanup_free_
char *peer
= NULL
;
458 Context
*context
= userdata
;
463 assert(revents
& EPOLLIN
);
466 nfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
468 if (!ERRNO_IS_ACCEPT_AGAIN(errno
))
469 log_warning_errno(errno
, "Failed to accept() socket: %m");
471 (void) getpeername_pretty(nfd
, true, &peer
);
472 log_debug("New connection from %s", strna(peer
));
474 r
= add_connection_socket(context
, nfd
);
476 log_error_errno(r
, "Failed to accept connection, ignoring: %m");
481 r
= sd_event_source_set_enabled(s
, SD_EVENT_ONESHOT
);
483 log_error_errno(r
, "Error while re-enabling listener with ONESHOT: %m");
484 sd_event_exit(context
->event
, r
);
491 static int add_listen_socket(Context
*context
, int fd
) {
492 sd_event_source
*source
;
498 r
= set_ensure_allocated(&context
->listen
, NULL
);
504 r
= sd_is_socket(fd
, 0, SOCK_STREAM
, 1);
506 return log_error_errno(r
, "Failed to determine socket type: %m");
508 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
509 "Passed in socket is not a stream socket.");
511 r
= fd_nonblock(fd
, true);
513 return log_error_errno(r
, "Failed to mark file descriptor non-blocking: %m");
515 r
= sd_event_add_io(context
->event
, &source
, fd
, EPOLLIN
, accept_cb
, context
);
517 return log_error_errno(r
, "Failed to add event source: %m");
519 r
= set_put(context
->listen
, source
);
521 log_error_errno(r
, "Failed to add source to set: %m");
522 sd_event_source_unref(source
);
526 /* Set the watcher to oneshot in case other processes are also
527 * watching to accept(). */
528 r
= sd_event_source_set_enabled(source
, SD_EVENT_ONESHOT
);
530 return log_error_errno(r
, "Failed to enable oneshot mode: %m");
535 static int help(void) {
536 _cleanup_free_
char *link
= NULL
;
539 r
= terminal_urlify_man("systemd-socket-proxyd", "8", &link
);
543 printf("%1$s [HOST:PORT]\n"
545 "Bidirectionally proxy local sockets to another (possibly remote) socket.\n\n"
546 " -c --connections-max= Set the maximum number of connections to be accepted\n"
547 " -h --help Show this help\n"
548 " --version Show package version\n"
549 "\nSee the %2$s for details.\n"
550 , program_invocation_short_name
557 static int parse_argv(int argc
, char *argv
[]) {
564 static const struct option options
[] = {
565 { "connections-max", required_argument
, NULL
, 'c' },
566 { "help", no_argument
, NULL
, 'h' },
567 { "version", no_argument
, NULL
, ARG_VERSION
},
576 while ((c
= getopt_long(argc
, argv
, "c:h", options
, NULL
)) >= 0)
587 r
= safe_atou(optarg
, &arg_connections_max
);
589 log_error("Failed to parse --connections-max= argument: %s", optarg
);
593 if (arg_connections_max
< 1)
594 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
595 "Connection limit is too low.");
603 assert_not_reached("Unhandled option");
607 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
608 "Not enough parameters.");
610 if (argc
!= optind
+1)
611 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
612 "Too many parameters.");
614 arg_remote_host
= argv
[optind
];
618 static int run(int argc
, char *argv
[]) {
619 _cleanup_(context_clear
) Context context
= {};
622 log_parse_environment();
625 r
= parse_argv(argc
, argv
);
629 r
= sd_event_default(&context
.event
);
631 return log_error_errno(r
, "Failed to allocate event loop: %m");
633 r
= sd_resolve_default(&context
.resolve
);
635 return log_error_errno(r
, "Failed to allocate resolver: %m");
637 r
= sd_resolve_attach_event(context
.resolve
, context
.event
, 0);
639 return log_error_errno(r
, "Failed to attach resolver: %m");
641 sd_event_set_watchdog(context
.event
, true);
643 r
= sd_listen_fds(1);
645 return log_error_errno(r
, "Failed to receive sockets from parent.");
647 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Didn't get any sockets passed in.");
651 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
652 r
= add_listen_socket(&context
, fd
);
657 r
= sd_event_loop(context
.event
);
659 return log_error_errno(r
, "Failed to run event loop: %m");
664 DEFINE_MAIN_FUNCTION(run
);