| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "shared-forward.h" |
| 5 |
|
| 6 |
/* Bidirectional forwarder using splice(). |
| 7 |
* |
| 8 |
* Forwards data between two sides ("server" and "client") via kernel pipe buffers, |
| 9 |
* avoiding userspace copies. Internally uses two independent half-duplex forwarders, |
| 10 |
* one per direction. All four fds must be distinct - use dup()/fcntl(fd, F_DUPFD_CLOEXEC, 3) for bidirectional sockets. |
| 11 |
* |
| 12 |
* When forwarding completes (both directions reach EOF or error), the completion callback is invoked. |
| 13 |
* |
| 14 |
* The SocketForward takes ownership of all fds - they are closed when the SocketForward is freed |
| 15 |
* (or earlier, during normal forwarding when EOF/disconnect is detected). */ |
| 16 |
|
| 17 |
typedef struct SocketForward SocketForward; |
| 18 |
|
| 19 |
typedef int (*socket_forward_done_t)(SocketForward *sf, int error, void *userdata); |
| 20 |
|
| 21 |
/* Create a forwarder between two bidirectional sockets. */ |
| 22 |
int socket_forward_new( |
| 23 |
sd_event *event, |
| 24 |
int server_fd, |
| 25 |
int client_fd, |
| 26 |
socket_forward_done_t on_done, |
| 27 |
void *userdata, |
| 28 |
SocketForward **ret); |
| 29 |
|
| 30 |
/* Create a forwarder between two fd pairs (e.g. stdin/stdout on one side, socket on the other). |
| 31 |
* All four fds must be distinct - use dup()/fcntl(fd, F_DUPFD_CLOEXEC, 3) for bidirectional sockets. */ |
| 32 |
int socket_forward_new_pair( |
| 33 |
sd_event *event, |
| 34 |
int server_read_fd, |
| 35 |
int server_write_fd, |
| 36 |
int client_read_fd, |
| 37 |
int client_write_fd, |
| 38 |
socket_forward_done_t on_done, |
| 39 |
void *userdata, |
| 40 |
SocketForward **ret); |
| 41 |
|
| 42 |
SocketForward* socket_forward_free(SocketForward *sf); |
| 43 |
DEFINE_TRIVIAL_CLEANUP_FUNC(SocketForward*, socket_forward_free); |