socket-proxyd.c revision 96c374d0a536286e18cad64d1b5ebb8f07c90334
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 David Strauss
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include "sd-daemon.h"
#include "sd-event.h"
#include "log.h"
#include "socket-util.h"
#include "util.h"
#include "event-util.h"
#define BUFFER_SIZE 16384
unsigned int total_clients = 0;
struct proxy {
int listen_fd;
bool ignore_env;
bool remote_is_inet;
const char *remote_host;
const char *remote_service;
};
struct connection {
int fd;
sd_event_source *w;
struct connection *c_destination;
char buffer[BUFFER_SIZE];
};
static void free_connection(struct connection *c) {
sd_event_source_unref(c->w);
close_nointr_nofail(c->fd);
free(c);
}
int r;
r = sd_event_source_set_io_events(c->w, c->events);
if (r < 0) {
return r;
}
r = sd_event_source_set_enabled(c->w, SD_EVENT_ON);
if (r < 0) {
return r;
}
return 0;
}
int r;
r = sd_event_source_set_io_events(c->w, c->events);
if (r < 0) {
return r;
}
if (c->events == 0) {
r = sd_event_source_set_enabled(c->w, SD_EVENT_OFF);
if (r < 0) {
return r;
}
}
return 0;
}
int r = 0;
/* We cannot assume that even a partial send() indicates that
* the next send() will block. Loop until it does. */
len = send(receiver->fd, sender->buffer + sender->buffer_sent_len, sender->buffer_filled_len - sender->buffer_sent_len, 0);
if (len < 0) {
return -errno;
}
else {
/* send() is in a blocking state. */
break;
}
}
/* len < 0 can't occur here. len == 0 is possible but
* undefined behavior for nonblocking send(). */
}
log_debug("send(%d, ...) completed with %lu bytes still buffered.", receiver->fd, sender->buffer_filled_len - sender->buffer_sent_len);
/* Detect a would-block state or partial send. */
/* If the buffer is full, disable events coming for recv. */
if (r < 0) {
return r;
}
}
/* Watch for when the recipient can be sent data again. */
if (r < 0) {
return r;
}
return r;
}
/* If we sent everything without blocking, the buffer is now empty. */
sender->buffer_filled_len = 0;
sender->buffer_sent_len = 0;
/* Enable the sender's receive watcher, in case the buffer was
* full and we disabled it. */
if (r < 0) {
return r;
}
/* Disable the other side's send watcher, as we have no data to send now. */
if (r < 0) {
return r;
}
return 0;
}
int r = 0;
assert(s == c->w);
log_debug("About to recv up to %lu bytes from fd=%d (%lu/BUFFER_SIZE).", BUFFER_SIZE - c->buffer_filled_len, fd, c->buffer_filled_len);
/* Receive until the buffer's full, there's no more data,
while (c->buffer_filled_len < BUFFER_SIZE) {
if (len < 0) {
return -errno;
}
else {
/* recv() is in a blocking state. */
break;
}
}
else if (len == 0) {
free_connection(c);
return 0;
}
c->buffer_filled_len += len;
}
/* Try sending the data immediately. */
return send_buffer(c);
}
else {
return send_buffer(c->c_destination);
}
return r;
}
/* Once sending to the server is unblocked, set up the real watchers. */
int r;
e = sd_event_get(s);
/* Cancel the initial write watcher for the server. */
log_debug("Connected to server. Initializing watchers for receiving data.");
/* A recv watcher for the server. */
r = sd_event_add_io(e, c_server_to_client->fd, EPOLLIN, transfer_data_cb, c_server_to_client, &c_server_to_client->w);
if (r < 0) {
goto fail;
}
/* A recv watcher for the client. */
r = sd_event_add_io(e, c_client_to_server->fd, EPOLLIN, transfer_data_cb, c_client_to_server, &c_client_to_server->w);
if (r < 0) {
goto fail;
}
goto finish;
fail:
return r;
}
int server_fd;
int r = -EBADF;
int len;
if (proxy->remote_is_inet) {
int s;
.ai_flags = AI_PASSIVE};
if (s != 0) {
return r;
}
log_error("getaddrinfo: no result");
return r;
}
/* @TODO: Try connecting to all results instead of just the first. */
if (server_fd < 0) {
return r;
}
/* Ignore EINPROGRESS errors because they're expected for a nonblocking socket. */
if (r < 0 && errno != EINPROGRESS) {
log_error("Error %d while connecting to socket %s:%s: %m", errno, proxy->remote_host, proxy->remote_service);
return r;
}
}
else {
struct sockaddr_un remote;
if (server_fd < 0) {
return -EBADFD;
}
if (r < 0 && errno != EINPROGRESS) {
return -EBADFD;
}
}
return server_fd;
}
struct connection *c_server_to_client;
struct connection *c_client_to_server;
int r = 0;
union sockaddr_union sa;
if (c_server_to_client == NULL) {
log_oom();
goto fail;
}
if (c_client_to_server == NULL) {
log_oom();
goto fail;
}
if (c_server_to_client->fd < 0) {
log_error("Error initiating server connection.");
goto fail;
}
if (c_client_to_server->fd < 0) {
log_error("Error accepting client connection.");
goto fail;
}
char sa_str[INET6_ADDRSTRLEN];
const char *success;
else
}
else {
}
log_debug("Client fd=%d (conn %p) successfully connected. Total clients: %u", c_client_to_server->fd, c_client_to_server, total_clients);
log_debug("Server fd=%d (conn %p) successfully initialized.", c_server_to_client->fd, c_server_to_client);
/* Initialize watcher for send to server; this shows connectivity. */
r = sd_event_add_io(sd_event_get(s), c_server_to_client->fd, EPOLLOUT, connected_to_server_cb, c_server_to_client, &c_server_to_client->w);
if (r < 0) {
log_error("Error %d creating connectivity watcher for fd=%d: %s", r, c_server_to_client->fd, strerror(-r));
goto fail;
}
/* Allow lookups of the opposite connection. */
goto finish;
fail:
log_warning("Accepting a client connection or connecting to the server failed.");
/* Preserve the main loop even if a single proxy setup fails. */
return 1;
}
int r = EXIT_SUCCESS;
r = sd_event_new(&e);
if (r < 0) {
return r;
}
if (r < 0) {
return r;
}
if (r < 0) {
return r;
}
log_debug("Initialized main listener. Entering loop.");
return sd_event_loop(e);
}
static int help(void) {
printf("%s hostname-or-ip port-or-service\n"
"%s unix-domain-socket-path\n\n"
"Inherit a socket. Bidirectionally proxy.\n\n"
" -h --help Show this help\n"
" --version Print version and exit\n"
" --ignore-env Ignore expected systemd environment\n",
return 0;
}
static void version(void) {
}
enum {
ARG_VERSION = 0x100,
};
};
int c;
switch (c) {
case 'h':
help();
return 0;
case '?':
return -EINVAL;
case ARG_VERSION:
version();
return 0;
case ARG_IGNORE_ENV:
p->ignore_env = true;
continue;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
log_error("Incorrect number of positional arguments.");
help();
return -EINVAL;
}
assert(p->remote_host);
if (!p->remote_is_inet) {
log_error("A port or service is not allowed for Unix socket destinations.");
help();
return -EINVAL;
}
assert(p->remote_service);
} else if (p->remote_is_inet) {
log_error("A port or service is required for IP destinations.");
help();
return -EINVAL;
}
return 1;
}
struct proxy p = {};
int r;
log_open();
if (r <= 0)
goto finish;
if (!p.ignore_env) {
int n;
n = sd_listen_fds(1);
if (n == 0) {
log_error("Found zero inheritable sockets. Are you sure this is running as a socket-activated service?");
r = EXIT_FAILURE;
goto finish;
} else if (n < 0) {
r = EXIT_FAILURE;
goto finish;
} else if (n > 1) {
log_error("Can't listen on more than one socket.");
r = EXIT_FAILURE;
goto finish;
}
}
/* @TODO: Check if this proxy can work with datagram sockets. */
if (r < 0) {
goto finish;
}
r = run_main_loop(&p);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}