2013-10-15 01:41:47 +02:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2013 David Strauss
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
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 <arpa/inet.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "sd-daemon.h"
|
|
|
|
#include "sd-event.h"
|
2013-10-18 02:40:26 +02:00
|
|
|
#include "log.h"
|
2013-10-15 01:41:47 +02:00
|
|
|
#include "socket-util.h"
|
|
|
|
#include "util.h"
|
2013-10-18 02:40:26 +02:00
|
|
|
#include "event-util.h"
|
2013-11-06 18:28:39 +01:00
|
|
|
#include "build.h"
|
2013-11-06 22:40:54 +01:00
|
|
|
#include "set.h"
|
|
|
|
#include "path-util.h"
|
|
|
|
|
|
|
|
#define BUFFER_SIZE (256 * 1024)
|
|
|
|
#define CONNECTIONS_MAX 256
|
2013-10-15 01:41:47 +02:00
|
|
|
|
|
|
|
#define _cleanup_freeaddrinfo_ _cleanup_(freeaddrinfop)
|
2013-11-06 22:40:54 +01:00
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(struct addrinfo *, freeaddrinfo);
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
typedef struct Context {
|
|
|
|
Set *listen;
|
|
|
|
Set *connections;
|
|
|
|
} Context;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
typedef struct Connection {
|
|
|
|
int server_fd, client_fd;
|
|
|
|
int server_to_client_buffer[2]; /* a pipe */
|
|
|
|
int client_to_server_buffer[2]; /* a pipe */
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
size_t server_to_client_buffer_full, client_to_server_buffer_full;
|
|
|
|
size_t server_to_client_buffer_size, client_to_server_buffer_size;
|
|
|
|
|
|
|
|
sd_event_source *server_event_source, *client_event_source;
|
|
|
|
} Connection;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
static const char *arg_remote_host = NULL;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
static void connection_free(Connection *c) {
|
|
|
|
assert(c);
|
2013-10-16 03:55:25 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
sd_event_source_unref(c->server_event_source);
|
|
|
|
sd_event_source_unref(c->client_event_source);
|
2013-10-16 03:55:25 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
if (c->server_fd >= 0)
|
|
|
|
close_nointr_nofail(c->server_fd);
|
|
|
|
if (c->client_fd >= 0)
|
|
|
|
close_nointr_nofail(c->client_fd);
|
2013-10-16 03:55:25 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
close_pipe(c->server_to_client_buffer);
|
|
|
|
close_pipe(c->client_to_server_buffer);
|
2013-10-16 03:55:25 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
free(c);
|
|
|
|
}
|
2013-10-16 03:55:25 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
static void context_free(Context *context) {
|
|
|
|
sd_event_source *es;
|
|
|
|
Connection *c;
|
|
|
|
|
|
|
|
assert(context);
|
|
|
|
|
|
|
|
while ((es = set_steal_first(context->listen)))
|
|
|
|
sd_event_source_unref(es);
|
|
|
|
|
|
|
|
while ((c = set_steal_first(context->connections)))
|
|
|
|
connection_free(c);
|
|
|
|
|
|
|
|
set_free(context->listen);
|
|
|
|
set_free(context->connections);
|
2013-10-16 03:55:25 +02:00
|
|
|
}
|
|
|
|
|
2013-11-07 16:43:25 +01:00
|
|
|
static int get_remote_sockaddr(union sockaddr_union *sa, socklen_t *salen) {
|
2013-10-16 03:55:25 +02:00
|
|
|
int r;
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
assert(sa);
|
|
|
|
assert(salen);
|
2013-10-16 03:55:25 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
if (path_is_absolute(arg_remote_host)) {
|
|
|
|
sa->un.sun_family = AF_UNIX;
|
|
|
|
strncpy(sa->un.sun_path, arg_remote_host, sizeof(sa->un.sun_path)-1);
|
|
|
|
sa->un.sun_path[sizeof(sa->un.sun_path)-1] = 0;
|
2013-10-16 03:55:25 +02:00
|
|
|
|
2013-11-07 16:43:25 +01:00
|
|
|
*salen = offsetof(union sockaddr_union, un.sun_path) + strlen(sa->un.sun_path);
|
2013-10-16 03:55:25 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
} else if (arg_remote_host[0] == '@') {
|
|
|
|
sa->un.sun_family = AF_UNIX;
|
|
|
|
sa->un.sun_path[0] = 0;
|
|
|
|
strncpy(sa->un.sun_path+1, arg_remote_host+1, sizeof(sa->un.sun_path)-2);
|
|
|
|
sa->un.sun_path[sizeof(sa->un.sun_path)-1] = 0;
|
|
|
|
|
2013-11-07 16:43:25 +01:00
|
|
|
*salen = offsetof(union sockaddr_union, un.sun_path) + 1 + strlen(sa->un.sun_path + 1);
|
2013-11-06 22:40:54 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
_cleanup_freeaddrinfo_ struct addrinfo *result = NULL;
|
|
|
|
const char *node, *service;
|
|
|
|
|
|
|
|
struct addrinfo hints = {
|
|
|
|
.ai_family = AF_UNSPEC,
|
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
.ai_flags = AI_ADDRCONFIG
|
|
|
|
};
|
|
|
|
|
|
|
|
service = strrchr(arg_remote_host, ':');
|
|
|
|
if (service) {
|
|
|
|
node = strndupa(arg_remote_host, service - arg_remote_host);
|
|
|
|
service ++;
|
|
|
|
} else {
|
|
|
|
node = arg_remote_host;
|
|
|
|
service = "80";
|
2013-11-06 18:29:24 +01:00
|
|
|
}
|
2013-10-16 03:55:25 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
log_debug("Looking up address info for %s:%s", node, service);
|
|
|
|
r = getaddrinfo(node, service, &hints, &result);
|
|
|
|
if (r != 0) {
|
|
|
|
log_error("Failed to resolve host %s:%s: %s", node, service, gai_strerror(r));
|
|
|
|
return -EHOSTUNREACH;
|
|
|
|
}
|
2013-10-16 03:55:25 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
assert(result);
|
2013-11-07 16:43:25 +01:00
|
|
|
if (result->ai_addrlen > sizeof(union sockaddr_union)) {
|
2013-11-06 22:40:54 +01:00
|
|
|
log_error("Address too long.");
|
|
|
|
return -E2BIG;
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
memcpy(sa, result->ai_addr, result->ai_addrlen);
|
|
|
|
*salen = result->ai_addrlen;
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
static int connection_create_pipes(Connection *c, int buffer[2], size_t *sz) {
|
|
|
|
int r;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
assert(c);
|
|
|
|
assert(buffer);
|
|
|
|
assert(sz);
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
if (buffer[0] >= 0)
|
|
|
|
return 0;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = pipe2(buffer, O_CLOEXEC|O_NONBLOCK);
|
2013-10-15 01:41:47 +02:00
|
|
|
if (r < 0) {
|
2013-11-06 22:40:54 +01:00
|
|
|
log_error("Failed to allocate pipe buffer: %m");
|
|
|
|
return -errno;
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
fcntl(buffer[0], F_SETPIPE_SZ, BUFFER_SIZE);
|
|
|
|
|
|
|
|
r = fcntl(buffer[0], F_GETPIPE_SZ);
|
2013-10-15 01:41:47 +02:00
|
|
|
if (r < 0) {
|
2013-11-06 22:40:54 +01:00
|
|
|
log_error("Failed to get pipe buffer size: %m");
|
|
|
|
return -errno;
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
assert(r > 0);
|
|
|
|
*sz = r;
|
|
|
|
|
2013-10-15 01:41:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
static int connection_shovel(
|
|
|
|
Connection *c,
|
|
|
|
int *from, int buffer[2], int *to,
|
|
|
|
size_t *full, size_t *sz,
|
|
|
|
sd_event_source **from_source, sd_event_source **to_source) {
|
|
|
|
|
|
|
|
bool shoveled;
|
|
|
|
|
|
|
|
assert(c);
|
|
|
|
assert(from);
|
|
|
|
assert(buffer);
|
|
|
|
assert(buffer[0] >= 0);
|
|
|
|
assert(buffer[1] >= 0);
|
|
|
|
assert(to);
|
|
|
|
assert(full);
|
|
|
|
assert(sz);
|
|
|
|
assert(from_source);
|
|
|
|
assert(to_source);
|
|
|
|
|
|
|
|
do {
|
|
|
|
ssize_t z;
|
|
|
|
|
|
|
|
shoveled = false;
|
|
|
|
|
|
|
|
if (*full < *sz && *from >= 0 && *to >= 0) {
|
|
|
|
z = splice(*from, NULL, buffer[1], NULL, *sz - *full, SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
|
|
|
|
if (z > 0) {
|
|
|
|
*full += z;
|
|
|
|
shoveled = true;
|
|
|
|
} else if (z == 0 || errno == EPIPE || errno == ECONNRESET) {
|
|
|
|
*from_source = sd_event_source_unref(*from_source);
|
|
|
|
close_nointr_nofail(*from);
|
|
|
|
*from = -1;
|
|
|
|
} else if (errno != EAGAIN && errno != EINTR) {
|
|
|
|
log_error("Failed to splice: %m");
|
|
|
|
return -errno;
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
if (*full > 0 && *to >= 0) {
|
|
|
|
z = splice(buffer[0], NULL, *to, NULL, *full, SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
|
|
|
|
if (z > 0) {
|
|
|
|
*full -= z;
|
|
|
|
shoveled = true;
|
|
|
|
} else if (z == 0 || errno == EPIPE || errno == ECONNRESET) {
|
|
|
|
*to_source = sd_event_source_unref(*to_source);
|
|
|
|
close_nointr_nofail(*to);
|
|
|
|
*to = -1;
|
|
|
|
} else if (errno != EAGAIN && errno != EINTR) {
|
|
|
|
log_error("Failed to splice: %m");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (shoveled);
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
return 0;
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
static int connection_enable_event_sources(Connection *c, sd_event *event);
|
|
|
|
|
|
|
|
static int traffic_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
|
|
|
|
Connection *c = userdata;
|
2013-10-15 01:41:47 +02:00
|
|
|
int r;
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
assert(s);
|
|
|
|
assert(fd >= 0);
|
|
|
|
assert(c);
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = connection_shovel(c,
|
|
|
|
&c->server_fd, c->server_to_client_buffer, &c->client_fd,
|
|
|
|
&c->server_to_client_buffer_full, &c->server_to_client_buffer_size,
|
|
|
|
&c->server_event_source, &c->client_event_source);
|
|
|
|
if (r < 0)
|
|
|
|
goto quit;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = connection_shovel(c,
|
|
|
|
&c->client_fd, c->client_to_server_buffer, &c->server_fd,
|
|
|
|
&c->client_to_server_buffer_full, &c->client_to_server_buffer_size,
|
|
|
|
&c->client_event_source, &c->server_event_source);
|
|
|
|
if (r < 0)
|
|
|
|
goto quit;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
/* EOF on both sides? */
|
|
|
|
if (c->server_fd == -1 && c->client_fd == -1)
|
|
|
|
goto quit;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
/* Server closed, and all data written to client? */
|
|
|
|
if (c->server_fd == -1 && c->server_to_client_buffer_full <= 0)
|
|
|
|
goto quit;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
/* Client closed, and all data written to server? */
|
|
|
|
if (c->client_fd == -1 && c->client_to_server_buffer_full <= 0)
|
|
|
|
goto quit;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = connection_enable_event_sources(c, sd_event_get(s));
|
|
|
|
if (r < 0)
|
|
|
|
goto quit;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
return 1;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
quit:
|
|
|
|
connection_free(c);
|
|
|
|
return 0; /* ignore errors, continue serving */
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
static int connection_enable_event_sources(Connection *c, sd_event *event) {
|
|
|
|
uint32_t a = 0, b = 0;
|
|
|
|
int r;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
assert(c);
|
|
|
|
assert(event);
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
if (c->server_to_client_buffer_full > 0)
|
|
|
|
b |= EPOLLOUT;
|
|
|
|
if (c->server_to_client_buffer_full < c->server_to_client_buffer_size)
|
|
|
|
a |= EPOLLIN;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
if (c->client_to_server_buffer_full > 0)
|
|
|
|
a |= EPOLLOUT;
|
|
|
|
if (c->client_to_server_buffer_full < c->client_to_server_buffer_size)
|
|
|
|
b |= EPOLLIN;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
if (c->server_event_source)
|
|
|
|
r = sd_event_source_set_io_events(c->server_event_source, a);
|
|
|
|
else if (c->server_fd >= 0)
|
|
|
|
r = sd_event_add_io(event, c->server_fd, a, traffic_cb, c, &c->server_event_source);
|
|
|
|
else
|
|
|
|
r = 0;
|
|
|
|
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to set up server event source: %s", strerror(-r));
|
|
|
|
return r;
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
if (c->client_event_source)
|
|
|
|
r = sd_event_source_set_io_events(c->client_event_source, b);
|
|
|
|
else if (c->client_fd >= 0)
|
|
|
|
r = sd_event_add_io(event, c->client_fd, b, traffic_cb, c, &c->client_event_source);
|
|
|
|
else
|
|
|
|
r = 0;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
if (r < 0) {
|
2013-11-06 23:59:07 +01:00
|
|
|
log_error("Failed to set up client event source: %s", strerror(-r));
|
2013-11-06 22:40:54 +01:00
|
|
|
return r;
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
return 0;
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
static int connect_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
|
|
|
|
Connection *c = userdata;
|
|
|
|
socklen_t solen;
|
|
|
|
int error, r;
|
|
|
|
|
|
|
|
assert(s);
|
|
|
|
assert(fd >= 0);
|
|
|
|
assert(c);
|
|
|
|
|
|
|
|
solen = sizeof(error);
|
|
|
|
r = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &solen);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to issue SO_ERROR: %m");
|
2013-10-22 03:32:08 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
if (error != 0) {
|
|
|
|
log_error("Failed to connect to remote host: %s", strerror(error));
|
2013-10-15 01:41:47 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
c->client_event_source = sd_event_source_unref(c->client_event_source);
|
|
|
|
|
|
|
|
r = connection_create_pipes(c, c->server_to_client_buffer, &c->server_to_client_buffer_size);
|
|
|
|
if (r < 0)
|
2013-10-15 01:41:47 +02:00
|
|
|
goto fail;
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = connection_create_pipes(c, c->client_to_server_buffer, &c->client_to_server_buffer_size);
|
|
|
|
if (r < 0)
|
2013-10-15 01:41:47 +02:00
|
|
|
goto fail;
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = connection_enable_event_sources(c, sd_event_get(s));
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
return 0;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
fail:
|
|
|
|
connection_free(c);
|
|
|
|
return 0; /* ignore errors, continue serving */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_connection_socket(Context *context, sd_event *event, int fd) {
|
2013-11-07 16:43:25 +01:00
|
|
|
union sockaddr_union sa = {};
|
2013-11-06 22:40:54 +01:00
|
|
|
socklen_t salen;
|
|
|
|
Connection *c;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(context);
|
|
|
|
assert(event);
|
|
|
|
assert(fd >= 0);
|
|
|
|
|
|
|
|
if (set_size(context->connections) > CONNECTIONS_MAX) {
|
|
|
|
log_warning("Hit connection limit, refusing connection.");
|
|
|
|
close_nointr_nofail(fd);
|
|
|
|
return 0;
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = set_ensure_allocated(&context->connections, trivial_hash_func, trivial_compare_func);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
c = new0(Connection, 1);
|
|
|
|
if (!c)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
c->server_fd = fd;
|
|
|
|
c->client_fd = -1;
|
|
|
|
c->server_to_client_buffer[0] = c->server_to_client_buffer[1] = -1;
|
|
|
|
c->client_to_server_buffer[0] = c->client_to_server_buffer[1] = -1;
|
|
|
|
|
|
|
|
r = get_remote_sockaddr(&sa, &salen);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
c->client_fd = socket(sa.sa.sa_family, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
|
|
|
|
if (c->client_fd < 0) {
|
|
|
|
log_error("Failed to get remote socket: %m");
|
2013-10-15 01:41:47 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = connect(c->client_fd, &sa.sa, salen);
|
|
|
|
if (r < 0) {
|
|
|
|
if (errno == EINPROGRESS) {
|
|
|
|
r = sd_event_add_io(event, c->client_fd, EPOLLOUT, connect_cb, c, &c->client_event_source);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to add connection socket: %s", strerror(-r));
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-11-06 23:59:07 +01:00
|
|
|
|
|
|
|
r = sd_event_source_set_enabled(c->client_event_source, SD_EVENT_ONESHOT);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to enable oneshot event source: %s", strerror(-r));
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-11-06 22:40:54 +01:00
|
|
|
} else {
|
|
|
|
log_error("Failed to connect to remote host: %m");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r = connection_enable_event_sources(c, event);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
return 0;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
|
|
|
fail:
|
2013-11-06 22:40:54 +01:00
|
|
|
connection_free(c);
|
|
|
|
return 0; /* ignore non-OOM errors, continue serving */
|
2013-10-22 03:32:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int accept_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
|
2013-11-06 22:40:54 +01:00
|
|
|
Context *context = userdata;
|
|
|
|
int nfd = -1, r;
|
2013-10-22 03:32:08 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
assert(s);
|
|
|
|
assert(fd >= 0);
|
2013-10-22 03:32:08 +02:00
|
|
|
assert(revents & EPOLLIN);
|
2013-11-06 22:40:54 +01:00
|
|
|
assert(context);
|
|
|
|
|
|
|
|
nfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
|
|
|
|
if (nfd >= 0) {
|
|
|
|
_cleanup_free_ char *peer = NULL;
|
2013-10-22 03:32:08 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
getpeername_pretty(nfd, &peer);
|
|
|
|
log_debug("New connection from %s", strna(peer));
|
2013-10-22 03:32:08 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = add_connection_socket(context, sd_event_get(s), nfd);
|
2013-10-22 03:41:35 +02:00
|
|
|
if (r < 0) {
|
2013-11-06 22:40:54 +01:00
|
|
|
close_nointr_nofail(fd);
|
|
|
|
return r;
|
2013-10-22 03:41:35 +02:00
|
|
|
}
|
2013-10-22 03:32:08 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
} else if (errno != -EAGAIN)
|
|
|
|
log_warning("Failed to accept() socket: %m");
|
|
|
|
|
2013-10-22 03:32:08 +02:00
|
|
|
r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Error %d while re-enabling listener with ONESHOT: %s", r, strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-10-18 02:40:26 +02:00
|
|
|
return 1;
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
static int add_listen_socket(Context *context, sd_event *event, int fd) {
|
|
|
|
sd_event_source *source;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(context);
|
|
|
|
assert(event);
|
|
|
|
assert(fd >= 0);
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
log_info("Listening on %i", fd);
|
|
|
|
|
|
|
|
r = set_ensure_allocated(&context->listen, trivial_hash_func, trivial_compare_func);
|
2013-10-18 02:40:26 +02:00
|
|
|
if (r < 0) {
|
2013-11-06 22:40:54 +01:00
|
|
|
log_oom();
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = sd_is_socket(fd, 0, SOCK_STREAM, 1);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to determine socket type: %s", strerror(-r));
|
2013-10-18 02:40:26 +02:00
|
|
|
return r;
|
|
|
|
}
|
2013-11-06 22:40:54 +01:00
|
|
|
if (r == 0) {
|
|
|
|
log_error("Passed in socket is not a stream socket.");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = fd_nonblock(fd, true);
|
2013-10-18 02:40:26 +02:00
|
|
|
if (r < 0) {
|
2013-11-06 22:40:54 +01:00
|
|
|
log_error("Failed to mark file descriptor non-blocking: %s", strerror(-r));
|
2013-10-18 02:40:26 +02:00
|
|
|
return r;
|
|
|
|
}
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = sd_event_add_io(event, fd, EPOLLIN, accept_cb, context, &source);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to add event source: %s", strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = set_put(context->listen, source);
|
2013-10-18 02:40:26 +02:00
|
|
|
if (r < 0) {
|
2013-11-06 22:40:54 +01:00
|
|
|
log_error("Failed to add source to set: %s", strerror(-r));
|
|
|
|
sd_event_source_unref(source);
|
2013-10-22 03:32:08 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the watcher to oneshot in case other processes are also
|
|
|
|
* watching to accept(). */
|
2013-11-06 22:40:54 +01:00
|
|
|
r = sd_event_source_set_enabled(source, SD_EVENT_ONESHOT);
|
2013-10-22 03:32:08 +02:00
|
|
|
if (r < 0) {
|
2013-11-06 22:40:54 +01:00
|
|
|
log_error("Failed to enable oneshot mode: %s", strerror(-r));
|
2013-10-18 02:40:26 +02:00
|
|
|
return r;
|
|
|
|
}
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
return 0;
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int help(void) {
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
printf("%s [HOST:PORT]\n"
|
|
|
|
"%s [SOCKET]\n\n"
|
|
|
|
"Bidirectionally proxy local sockets to another (possibly remote) socket.\n\n"
|
|
|
|
" -h --help Show this help\n"
|
|
|
|
" --version Show package version\n",
|
2013-10-15 01:41:47 +02:00
|
|
|
program_invocation_short_name,
|
|
|
|
program_invocation_short_name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
2013-10-15 01:41:47 +02:00
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_VERSION = 0x100,
|
|
|
|
ARG_IGNORE_ENV
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
2013-11-06 18:28:39 +01:00
|
|
|
{}
|
2013-10-15 01:41:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
assert(argc >= 0);
|
|
|
|
assert(argv);
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'h':
|
2013-11-06 18:28:39 +01:00
|
|
|
return help();
|
2013-10-15 01:41:47 +02:00
|
|
|
|
|
|
|
case ARG_VERSION:
|
2013-11-06 18:28:39 +01:00
|
|
|
puts(PACKAGE_STRING);
|
|
|
|
puts(SYSTEMD_FEATURES);
|
2013-10-15 01:41:47 +02:00
|
|
|
return 0;
|
|
|
|
|
2013-11-06 18:28:39 +01:00
|
|
|
case '?':
|
2013-10-15 01:41:47 +02:00
|
|
|
return -EINVAL;
|
2013-11-06 18:28:39 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert_not_reached("Unhandled option");
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
if (optind >= argc) {
|
|
|
|
log_error("Not enough parameters.");
|
2013-10-15 01:41:47 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
if (argc != optind+1) {
|
|
|
|
log_error("Too many parameters.");
|
2013-10-15 01:41:47 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
arg_remote_host = argv[optind];
|
2013-10-15 01:41:47 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2013-11-06 22:40:54 +01:00
|
|
|
_cleanup_event_unref_ sd_event *event = NULL;
|
|
|
|
Context context = {};
|
|
|
|
int r, n, fd;
|
2013-10-15 01:41:47 +02:00
|
|
|
|
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = parse_argv(argc, argv);
|
2013-10-15 01:41:47 +02:00
|
|
|
if (r <= 0)
|
|
|
|
goto finish;
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = sd_event_new(&event);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to allocate event loop: %s", strerror(-r));
|
|
|
|
goto finish;
|
|
|
|
}
|
2013-10-15 01:41:47 +02:00
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
n = sd_listen_fds(1);
|
|
|
|
if (n < 0) {
|
|
|
|
log_error("Failed to receive sockets from parent.");
|
|
|
|
r = n;
|
|
|
|
goto finish;
|
|
|
|
} else if (n == 0) {
|
|
|
|
log_error("Didn't get any sockets passed in.");
|
|
|
|
r = -EINVAL;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
|
|
|
|
r = add_listen_socket(&context, event, fd);
|
|
|
|
if (r < 0)
|
2013-10-18 02:40:26 +02:00
|
|
|
goto finish;
|
2013-10-15 01:41:47 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:40:54 +01:00
|
|
|
r = sd_event_loop(event);
|
2013-10-15 01:41:47 +02:00
|
|
|
if (r < 0) {
|
2013-11-06 22:40:54 +01:00
|
|
|
log_error("Failed to run event loop: %s", strerror(-r));
|
2013-10-15 01:41:47 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
finish:
|
2013-11-06 22:40:54 +01:00
|
|
|
context_free(&context);
|
|
|
|
|
2013-10-15 01:41:47 +02:00
|
|
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
}
|