2019-02-03 23:35:03 +01:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2019-02-03 04:38:59 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
2019-02-03 15:18:28 +01:00
|
|
|
#include <signal.h>
|
2019-02-03 04:38:59 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <poll.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2019-02-03 14:09:21 +01:00
|
|
|
#include <arpa/inet.h>
|
2019-02-03 04:38:59 +01:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2019-02-03 17:08:56 +01:00
|
|
|
#define DEFAULT_PORT 2222
|
|
|
|
#define DEFAULT_DELAY 10000 /* milliseconds */
|
|
|
|
#define DEFAULT_MAX_LINE_LENGTH 32
|
|
|
|
#define DEFAULT_MAX_CLIENTS 4096
|
2019-02-03 21:46:23 +01:00
|
|
|
#define DEFAULT_CONFIG_FILE "/etc/endlessh/config"
|
2019-02-03 04:38:59 +01:00
|
|
|
|
|
|
|
#define XSTR(s) STR(s)
|
|
|
|
#define STR(s) #s
|
|
|
|
|
|
|
|
static long long
|
|
|
|
uepoch(void)
|
|
|
|
{
|
|
|
|
struct timespec tv;
|
2019-02-03 21:45:31 +01:00
|
|
|
#ifdef __linux__
|
|
|
|
clock_gettime(CLOCK_REALTIME_COARSE, &tv);
|
|
|
|
#else
|
2019-02-03 04:38:59 +01:00
|
|
|
clock_gettime(CLOCK_REALTIME, &tv);
|
2019-02-03 21:45:31 +01:00
|
|
|
#endif
|
2019-02-03 04:38:59 +01:00
|
|
|
return tv.tv_sec * 1000ULL + tv.tv_nsec / 1000000ULL;
|
|
|
|
}
|
|
|
|
|
2019-02-03 15:18:28 +01:00
|
|
|
static enum loglevel {
|
|
|
|
LOG_NONE,
|
|
|
|
LOG_INFO,
|
|
|
|
LOG_DEBUG
|
|
|
|
} loglevel = LOG_NONE;
|
|
|
|
|
|
|
|
static void
|
|
|
|
logmsg(enum loglevel level, const char *format, ...)
|
|
|
|
{
|
|
|
|
if (loglevel >= level) {
|
2019-02-03 22:01:24 +01:00
|
|
|
/* Print a timestamp */
|
2019-02-03 15:18:28 +01:00
|
|
|
long long now = uepoch();
|
2019-02-03 22:01:24 +01:00
|
|
|
time_t t = now / 1000;
|
|
|
|
char date[64];
|
|
|
|
struct tm tm[1];
|
|
|
|
strftime(date, sizeof(date), "%Y-%m-%dT%H:%M:%S", gmtime_r(&t, tm));
|
|
|
|
printf("%s.%03lldZ ", date, now % 1000);
|
|
|
|
|
|
|
|
/* Print the rest of the log message */
|
2019-02-03 15:18:28 +01:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
vprintf(format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fputc('\n', stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 04:38:59 +01:00
|
|
|
struct client {
|
2019-02-03 14:09:21 +01:00
|
|
|
char ipaddr[INET6_ADDRSTRLEN];
|
|
|
|
long long connect_time;
|
|
|
|
long long send_next;
|
|
|
|
long long bytes_sent;
|
2019-02-03 04:38:59 +01:00
|
|
|
struct client *next;
|
2019-02-03 14:09:21 +01:00
|
|
|
int port;
|
2019-02-03 04:38:59 +01:00
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct client *
|
|
|
|
client_new(int fd, long long send_next)
|
|
|
|
{
|
|
|
|
struct client *c = malloc(sizeof(*c));
|
|
|
|
if (c) {
|
2019-02-03 14:09:21 +01:00
|
|
|
c->connect_time = uepoch();
|
2019-02-03 04:38:59 +01:00
|
|
|
c->send_next = send_next;
|
2019-02-03 14:09:21 +01:00
|
|
|
c->bytes_sent = 0;
|
2019-02-03 04:38:59 +01:00
|
|
|
c->next = 0;
|
|
|
|
c->fd = fd;
|
2019-02-03 14:09:21 +01:00
|
|
|
|
|
|
|
/* Get IP address */
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
socklen_t len = sizeof(addr);
|
|
|
|
if (getpeername(fd, (struct sockaddr*)&addr, &len) == -1) {
|
|
|
|
c->ipaddr[0] = 0;
|
|
|
|
} else {
|
|
|
|
if (addr.ss_family == AF_INET) {
|
|
|
|
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
|
|
|
|
c->port = ntohs(s->sin_port);
|
|
|
|
inet_ntop(AF_INET, &s->sin_addr,
|
|
|
|
c->ipaddr, sizeof(c->ipaddr));
|
|
|
|
} else {
|
|
|
|
struct sockaddr_in6 *s = (struct sockaddr_in6 *)&addr;
|
|
|
|
c->port = ntohs(s->sin6_port);
|
|
|
|
inet_ntop(AF_INET6, &s->sin6_addr,
|
|
|
|
c->ipaddr, sizeof(c->ipaddr));
|
|
|
|
}
|
|
|
|
}
|
2019-02-03 04:38:59 +01:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2019-02-03 15:18:28 +01:00
|
|
|
static void
|
|
|
|
client_destroy(struct client *client)
|
|
|
|
{
|
|
|
|
logmsg(LOG_DEBUG, "close(%d)", client->fd);
|
|
|
|
long long dt = uepoch() - client->connect_time;
|
|
|
|
logmsg(LOG_INFO,
|
2019-02-03 19:06:14 +01:00
|
|
|
"CLOSE host=%s port=%d fd=%d "
|
2019-02-03 15:18:28 +01:00
|
|
|
"time=%lld.%03lld bytes=%lld",
|
|
|
|
client->ipaddr, client->port, client->fd,
|
|
|
|
dt / 1000, dt % 1000,
|
|
|
|
client->bytes_sent);
|
|
|
|
close(client->fd);
|
|
|
|
free(client);
|
|
|
|
}
|
|
|
|
|
2019-02-03 04:38:59 +01:00
|
|
|
struct queue {
|
|
|
|
struct client *head;
|
|
|
|
struct client *tail;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
queue_init(struct queue *q)
|
|
|
|
{
|
|
|
|
q->head = q->tail = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct client *
|
|
|
|
queue_remove(struct queue *q, int fd)
|
|
|
|
{
|
2019-02-03 16:06:12 +01:00
|
|
|
/* Yes, this is a linear search, but the element we're looking for
|
|
|
|
* is virtually always one of the first few elements.
|
|
|
|
*/
|
2019-02-03 04:38:59 +01:00
|
|
|
struct client *c;
|
|
|
|
struct client **prev = &q->head;
|
|
|
|
for (c = q->head; c; prev = &c->next, c = c->next) {
|
|
|
|
if (c->fd == fd) {
|
|
|
|
if (q->tail == c)
|
|
|
|
q->tail = 0;
|
|
|
|
*prev = c->next;
|
|
|
|
c->next = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
queue_append(struct queue *q, struct client *c)
|
|
|
|
{
|
|
|
|
if (!q->tail) {
|
|
|
|
q->head = q->tail = c;
|
|
|
|
} else {
|
|
|
|
q->tail->next = c;
|
|
|
|
q->tail = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 15:18:28 +01:00
|
|
|
static void
|
|
|
|
queue_destroy(struct queue *q)
|
|
|
|
{
|
|
|
|
struct client *c = q->head;
|
|
|
|
while (c) {
|
|
|
|
struct client *dead = c;
|
|
|
|
c = c->next;
|
|
|
|
client_destroy(dead);
|
|
|
|
}
|
|
|
|
q->head = q->tail = 0;
|
|
|
|
}
|
|
|
|
|
2019-02-03 04:38:59 +01:00
|
|
|
struct pollvec {
|
|
|
|
size_t cap;
|
|
|
|
size_t fill;
|
|
|
|
struct pollfd *fds;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
pollvec_init(struct pollvec *v)
|
|
|
|
{
|
|
|
|
v->cap = 4;
|
|
|
|
v->fill = 0;
|
2019-02-03 05:18:56 +01:00
|
|
|
v->fds = malloc(v->cap * sizeof(v->fds[0]));
|
2019-02-03 04:38:59 +01:00
|
|
|
if (!v->fds) {
|
|
|
|
fprintf(stderr, "endlessh: pollvec initialization failed\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pollvec_clear(struct pollvec *v)
|
|
|
|
{
|
|
|
|
v->fill = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pollvec_push(struct pollvec *v, int fd, short events)
|
|
|
|
{
|
|
|
|
if (v->cap == v->fill) {
|
2019-02-03 05:18:56 +01:00
|
|
|
size_t size = v->cap * 2 * sizeof(v->fds[0]);
|
|
|
|
if (size < v->cap * sizeof(v->fds[0]))
|
2019-02-03 04:38:59 +01:00
|
|
|
return 0; // overflow
|
|
|
|
struct pollfd *grow = realloc(v->fds, size);
|
|
|
|
if (!grow)
|
|
|
|
return 0;
|
|
|
|
v->fds = grow;
|
2019-02-03 05:18:56 +01:00
|
|
|
v->cap *= 2;
|
2019-02-03 04:38:59 +01:00
|
|
|
}
|
|
|
|
v->fds[v->fill].fd = fd;
|
|
|
|
v->fds[v->fill].events = events;
|
|
|
|
v->fill++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-02-03 15:18:28 +01:00
|
|
|
static void
|
|
|
|
pollvec_free(struct pollvec *v)
|
|
|
|
{
|
|
|
|
free(v->fds);
|
|
|
|
v->fds = 0;
|
|
|
|
}
|
|
|
|
|
2019-02-03 04:38:59 +01:00
|
|
|
static void
|
2019-02-03 17:55:15 +01:00
|
|
|
die(void)
|
2019-02-03 04:38:59 +01:00
|
|
|
{
|
2019-02-03 17:55:15 +01:00
|
|
|
fprintf(stderr, "endlessh: fatal: %s\n", strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
2019-02-03 04:38:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-02-03 15:34:41 +01:00
|
|
|
randline(char *line, int maxlen)
|
2019-02-03 04:38:59 +01:00
|
|
|
{
|
2019-02-03 15:34:41 +01:00
|
|
|
int len = 3 + rand() % (maxlen - 2);
|
2019-02-03 04:38:59 +01:00
|
|
|
for (int i = 0; i < len - 2; i++)
|
|
|
|
line[i] = 32 + rand() % 95;
|
|
|
|
line[len - 2] = 13;
|
|
|
|
line[len - 1] = 10;
|
|
|
|
if (memcmp(line, "SSH-", 4) == 0)
|
|
|
|
line[0] = 'X';
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2019-02-03 15:18:28 +01:00
|
|
|
static volatile sig_atomic_t running = 1;
|
2019-02-03 14:09:21 +01:00
|
|
|
|
2019-02-03 04:38:59 +01:00
|
|
|
static void
|
2019-02-03 15:18:28 +01:00
|
|
|
sigterm_handler(int signal)
|
2019-02-03 04:38:59 +01:00
|
|
|
{
|
2019-02-03 15:18:28 +01:00
|
|
|
(void)signal;
|
|
|
|
running = 0;
|
2019-02-03 04:38:59 +01:00
|
|
|
}
|
|
|
|
|
2019-02-03 17:47:20 +01:00
|
|
|
static volatile sig_atomic_t reload = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
sighup_handler(int signal)
|
|
|
|
{
|
|
|
|
(void)signal;
|
|
|
|
reload = 1;
|
|
|
|
}
|
|
|
|
|
2019-02-03 17:08:56 +01:00
|
|
|
struct config {
|
|
|
|
int port;
|
|
|
|
int delay;
|
|
|
|
int max_line_length;
|
|
|
|
int max_clients;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define CONFIG_DEFAULT { \
|
|
|
|
.port = DEFAULT_PORT, \
|
|
|
|
.delay = DEFAULT_DELAY, \
|
|
|
|
.max_line_length = DEFAULT_MAX_LINE_LENGTH, \
|
|
|
|
.max_clients = DEFAULT_MAX_CLIENTS, \
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
config_set_port(struct config *c, const char *s, int hardfail)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
char *end;
|
|
|
|
long tmp = strtol(s, &end, 10);
|
|
|
|
if (errno || *end || tmp < 1 || tmp > 65535) {
|
|
|
|
fprintf(stderr, "endlessh: Invalid port: %s\n", s);
|
|
|
|
if (hardfail)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
c->port = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
config_set_delay(struct config *c, const char *s, int hardfail)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
char *end;
|
|
|
|
long tmp = strtol(s, &end, 10);
|
|
|
|
if (errno || *end || tmp < 1 || tmp > INT_MAX) {
|
|
|
|
fprintf(stderr, "endlessh: Invalid delay: %s\n", s);
|
|
|
|
if (hardfail)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
c->delay = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
config_set_max_clients(struct config *c, const char *s, int hardfail)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
char *end;
|
|
|
|
long tmp = strtol(s, &end, 10);
|
|
|
|
if (errno || *end || tmp < 1 || tmp > INT_MAX) {
|
|
|
|
fprintf(stderr, "endlessh: Invalid max clients: %s\n", s);
|
|
|
|
if (hardfail)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
c->max_clients = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
config_set_max_line_length(struct config *c, const char *s, int hardfail)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
char *end;
|
|
|
|
long tmp = strtol(s, &end, 10);
|
|
|
|
if (errno || *end || tmp < 3 || tmp > 255) {
|
|
|
|
fprintf(stderr, "endlessh: Invalid line length: %s\n", s);
|
|
|
|
if (hardfail)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
c->max_line_length = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 23:35:03 +01:00
|
|
|
enum config_key {
|
|
|
|
KEY_INVALID,
|
|
|
|
KEY_PORT,
|
|
|
|
KEY_DELAY,
|
|
|
|
KEY_MAX_LINE_LENGTH,
|
|
|
|
KEY_MAX_CLIENTS,
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum config_key
|
|
|
|
config_key_parse(const char *tok)
|
2019-02-03 17:47:20 +01:00
|
|
|
{
|
2019-02-03 23:35:03 +01:00
|
|
|
static const char *const table[] = {
|
|
|
|
[KEY_PORT] = "Port",
|
|
|
|
[KEY_DELAY] = "Delay",
|
|
|
|
[KEY_MAX_LINE_LENGTH] = "MaxLineLength",
|
|
|
|
[KEY_MAX_CLIENTS] = "MaxClients",
|
|
|
|
};
|
|
|
|
for (size_t i = 1; i < sizeof(table) / sizeof(*table); i++)
|
|
|
|
if (!strcmp(tok, table[i]))
|
|
|
|
return i;
|
|
|
|
return KEY_INVALID;
|
2019-02-03 17:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
config_load(struct config *c, const char *file, int hardfail)
|
|
|
|
{
|
|
|
|
long lineno = 0;
|
|
|
|
FILE *f = fopen(file, "r");
|
|
|
|
if (f) {
|
2019-02-03 23:35:03 +01:00
|
|
|
size_t len = 0;
|
|
|
|
char *line = 0;
|
|
|
|
while (getline(&line, &len, f) != -1) {
|
2019-02-03 17:47:20 +01:00
|
|
|
lineno++;
|
2019-02-03 23:35:03 +01:00
|
|
|
|
|
|
|
/* Remove comments */
|
|
|
|
char *comment = strchr(line, '#');
|
|
|
|
if (comment)
|
|
|
|
*comment = 0;
|
|
|
|
|
|
|
|
/* Parse tokes on line */
|
2019-02-03 17:47:20 +01:00
|
|
|
char *save = 0;
|
2019-02-03 23:35:03 +01:00
|
|
|
char *tokens[3];
|
|
|
|
int ntokens = 0;
|
|
|
|
for (; ntokens < 3; ntokens++) {
|
|
|
|
char *tok = strtok_r(ntokens ? 0 : line, " \r\n", &save);
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
tokens[ntokens] = tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ntokens) {
|
|
|
|
case 0: /* Empty line */
|
|
|
|
continue;
|
|
|
|
case 1:
|
|
|
|
fprintf(stderr, "%s:%ld: Missing value\n", file, lineno);
|
|
|
|
if (hardfail) exit(EXIT_FAILURE);
|
|
|
|
continue;
|
|
|
|
case 2: /* Expected */
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
fprintf(stderr, "%s:%ld: Too many values\n", file, lineno);
|
|
|
|
if (hardfail) exit(EXIT_FAILURE);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum config_key key = config_key_parse(tokens[0]);
|
|
|
|
switch (key) {
|
|
|
|
case KEY_INVALID:
|
|
|
|
fprintf(stderr, "%s:%ld: Unknown option '%s'\n",
|
|
|
|
file, lineno, tokens[0]);
|
|
|
|
break;
|
|
|
|
case KEY_PORT:
|
|
|
|
config_set_port(c, tokens[1], hardfail);
|
|
|
|
break;
|
|
|
|
case KEY_DELAY:
|
|
|
|
config_set_delay(c, tokens[1], hardfail);
|
|
|
|
break;
|
|
|
|
case KEY_MAX_LINE_LENGTH:
|
|
|
|
config_set_max_line_length(c, tokens[1], hardfail);
|
|
|
|
break;
|
|
|
|
case KEY_MAX_CLIENTS:
|
|
|
|
config_set_max_line_length(c, tokens[1], hardfail);
|
|
|
|
break;
|
2019-02-03 17:47:20 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-03 23:35:03 +01:00
|
|
|
|
|
|
|
free(line);
|
2019-02-03 17:47:20 +01:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
config_log(const struct config *c)
|
|
|
|
{
|
|
|
|
logmsg(LOG_INFO, "Port %d", c->port);
|
|
|
|
logmsg(LOG_INFO, "Delay %ld", c->delay);
|
|
|
|
logmsg(LOG_INFO, "MaxLineLength %d", c->max_line_length);
|
|
|
|
logmsg(LOG_INFO, "MaxClients %d", c->max_clients);
|
|
|
|
}
|
|
|
|
|
2019-02-03 04:38:59 +01:00
|
|
|
static void
|
|
|
|
usage(FILE *f)
|
|
|
|
{
|
2019-02-03 17:47:20 +01:00
|
|
|
fprintf(f, "Usage: endlessh [-vh] [-d MS] [-f CONFIG] [-l LEN] "
|
2019-02-03 15:34:41 +01:00
|
|
|
"[-m LIMIT] [-p PORT]\n");
|
2019-02-03 04:38:59 +01:00
|
|
|
fprintf(f, " -d INT Message millisecond delay ["
|
|
|
|
XSTR(DEFAULT_DELAY) "]\n");
|
2019-02-03 18:00:35 +01:00
|
|
|
fprintf(f, " -f Set and load config file ["
|
2019-02-03 17:47:20 +01:00
|
|
|
DEFAULT_CONFIG_FILE "]\n");
|
2019-02-03 04:38:59 +01:00
|
|
|
fprintf(f, " -h Print this help message and exit\n");
|
2019-02-03 15:34:41 +01:00
|
|
|
fprintf(f, " -l INT Maximum banner line length (3-255) ["
|
2019-02-03 17:08:56 +01:00
|
|
|
XSTR(DEFAULT_MAX_LINE_LENGTH) "]\n");
|
2019-02-03 04:38:59 +01:00
|
|
|
fprintf(f, " -m INT Maximum number of clients ["
|
|
|
|
XSTR(DEFAULT_MAX_CLIENTS) "]\n");
|
|
|
|
fprintf(f, " -p INT Listening port [" XSTR(DEFAULT_PORT) "]\n");
|
|
|
|
fprintf(f, " -v Print diagnostics to standard output "
|
|
|
|
"(repeatable)\n");
|
|
|
|
}
|
|
|
|
|
2019-02-03 17:55:15 +01:00
|
|
|
static int
|
|
|
|
server_create(int port)
|
|
|
|
{
|
2019-02-03 18:48:37 +01:00
|
|
|
int r, s, value;
|
2019-02-03 17:55:15 +01:00
|
|
|
|
2019-02-03 18:48:37 +01:00
|
|
|
s = socket(AF_INET6, SOCK_STREAM, 0);
|
|
|
|
logmsg(LOG_DEBUG, "socket() = %d", s);
|
2019-02-03 17:55:15 +01:00
|
|
|
if (s == -1) die();
|
|
|
|
|
2019-02-03 18:48:37 +01:00
|
|
|
/* Socket options are best effort, allowed to fail */
|
|
|
|
|
|
|
|
value = 1;
|
|
|
|
r = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value));
|
|
|
|
logmsg(LOG_DEBUG, "setsockopt(SO_REUSEADDR, true) = %d", r);
|
|
|
|
if (r == -1)
|
|
|
|
logmsg(LOG_DEBUG, "errno = %d, %s", errno, strerror(errno));
|
|
|
|
|
|
|
|
struct sockaddr_in6 addr = {
|
|
|
|
.sin6_family = AF_INET6,
|
|
|
|
.sin6_port = htons(port),
|
|
|
|
.sin6_addr = in6addr_any
|
2019-02-03 18:28:22 +01:00
|
|
|
};
|
2019-02-03 17:55:15 +01:00
|
|
|
r = bind(s, (void *)&addr, sizeof(addr));
|
2019-02-03 18:48:37 +01:00
|
|
|
logmsg(LOG_DEBUG, "bind(port=%d) = %d", port, r);
|
2019-02-03 17:55:15 +01:00
|
|
|
if (r == -1) die();
|
|
|
|
|
|
|
|
r = listen(s, INT_MAX);
|
2019-02-03 18:48:37 +01:00
|
|
|
logmsg(LOG_DEBUG, "listen() = %d", r);
|
2019-02-03 17:55:15 +01:00
|
|
|
if (r == -1) die();
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-02-03 04:38:59 +01:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2019-02-03 17:08:56 +01:00
|
|
|
struct config config = CONFIG_DEFAULT;
|
2019-02-03 17:47:20 +01:00
|
|
|
const char *config_file = DEFAULT_CONFIG_FILE;
|
|
|
|
config_load(&config, config_file, 1);
|
2019-02-03 04:38:59 +01:00
|
|
|
|
|
|
|
int option;
|
2019-02-03 17:47:20 +01:00
|
|
|
while ((option = getopt(argc, argv, "d:f:hl:m:p:v")) != -1) {
|
2019-02-03 04:38:59 +01:00
|
|
|
switch (option) {
|
|
|
|
case 'd':
|
2019-02-03 17:08:56 +01:00
|
|
|
config_set_delay(&config, optarg, 1);
|
2019-02-03 04:38:59 +01:00
|
|
|
break;
|
2019-02-03 17:47:20 +01:00
|
|
|
case 'f':
|
|
|
|
config_file = optarg;
|
|
|
|
config_load(&config, optarg, 1);
|
|
|
|
break;
|
2019-02-03 04:38:59 +01:00
|
|
|
case 'h':
|
|
|
|
usage(stdout);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
break;
|
2019-02-03 15:34:41 +01:00
|
|
|
case 'l':
|
2019-02-03 17:08:56 +01:00
|
|
|
config_set_max_line_length(&config, optarg, 1);
|
2019-02-03 15:34:41 +01:00
|
|
|
break;
|
2019-02-03 04:38:59 +01:00
|
|
|
case 'm':
|
2019-02-03 17:08:56 +01:00
|
|
|
config_set_max_clients(&config, optarg, 1);
|
2019-02-03 04:38:59 +01:00
|
|
|
break;
|
|
|
|
case 'p':
|
2019-02-03 17:08:56 +01:00
|
|
|
config_set_port(&config, optarg, 1);
|
2019-02-03 04:38:59 +01:00
|
|
|
break;
|
|
|
|
case 'v':
|
2019-02-03 14:18:55 +01:00
|
|
|
if (!loglevel++)
|
|
|
|
setvbuf(stdout, 0, _IOLBF, 0);
|
2019-02-03 04:38:59 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(stderr);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 19:03:40 +01:00
|
|
|
if (argv[optind]) {
|
|
|
|
fprintf(stderr, "endlessh: too many arguments\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2019-02-03 17:08:56 +01:00
|
|
|
/* Log configuration */
|
2019-02-03 17:47:20 +01:00
|
|
|
config_log(&config);
|
2019-02-03 17:08:56 +01:00
|
|
|
|
2019-02-03 21:31:16 +01:00
|
|
|
/* Install the signal handlers */
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2019-02-03 17:47:20 +01:00
|
|
|
{
|
|
|
|
struct sigaction sa = {.sa_handler = sigterm_handler};
|
2019-02-03 17:55:15 +01:00
|
|
|
int r = sigaction(SIGTERM, &sa, 0);
|
|
|
|
if (r == -1)
|
|
|
|
die();
|
2019-02-03 17:47:20 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
struct sigaction sa = {.sa_handler = sighup_handler};
|
2019-02-03 17:55:15 +01:00
|
|
|
int r = sigaction(SIGHUP, &sa, 0);
|
|
|
|
if (r == -1)
|
|
|
|
die();
|
2019-02-03 17:47:20 +01:00
|
|
|
}
|
2019-02-03 15:18:28 +01:00
|
|
|
|
2019-02-03 15:36:04 +01:00
|
|
|
int nclients = 0;
|
2019-02-03 04:38:59 +01:00
|
|
|
|
|
|
|
struct queue queue[1];
|
|
|
|
queue_init(queue);
|
|
|
|
|
|
|
|
struct pollvec pollvec[1];
|
|
|
|
pollvec_init(pollvec);
|
|
|
|
|
2019-02-03 17:55:15 +01:00
|
|
|
int server = server_create(config.port);
|
2019-02-03 04:38:59 +01:00
|
|
|
|
|
|
|
srand(time(0));
|
2019-02-03 15:18:28 +01:00
|
|
|
while (running) {
|
2019-02-03 17:47:20 +01:00
|
|
|
if (reload) {
|
|
|
|
/* Configuration reload requested (SIGHUP) */
|
2019-02-03 17:55:15 +01:00
|
|
|
int oldport = config.port;
|
2019-02-03 17:47:20 +01:00
|
|
|
config_load(&config, config_file, 0);
|
|
|
|
config_log(&config);
|
2019-02-03 17:55:15 +01:00
|
|
|
if (oldport != config.port) {
|
|
|
|
close(server);
|
|
|
|
server = server_create(config.port);
|
|
|
|
}
|
2019-02-03 17:47:20 +01:00
|
|
|
reload = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enqueue the listening socket first */
|
2019-02-03 04:38:59 +01:00
|
|
|
pollvec_clear(pollvec);
|
2019-02-03 17:08:56 +01:00
|
|
|
if (nclients < config.max_clients)
|
|
|
|
pollvec_push(pollvec, server, POLLIN);
|
|
|
|
else
|
|
|
|
pollvec_push(pollvec, -1, POLLIN);
|
2019-02-03 04:38:59 +01:00
|
|
|
|
2019-02-03 17:47:20 +01:00
|
|
|
/* Enqueue clients that are due for another message */
|
2019-02-03 04:38:59 +01:00
|
|
|
int timeout = -1;
|
|
|
|
long long now = uepoch();
|
|
|
|
for (struct client *c = queue->head; c; c = c->next) {
|
|
|
|
if (c->send_next <= now) {
|
|
|
|
pollvec_push(pollvec, c->fd, POLLOUT);
|
|
|
|
} else {
|
|
|
|
timeout = c->send_next - now;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for next event */
|
2019-02-03 14:09:21 +01:00
|
|
|
logmsg(LOG_DEBUG, "poll(%zu, %d)%s", pollvec->fill, timeout,
|
2019-02-03 17:08:56 +01:00
|
|
|
nclients >= config.max_clients ? " (no accept)" : "");
|
2019-02-03 04:38:59 +01:00
|
|
|
int r = poll(pollvec->fds, pollvec->fill, timeout);
|
2019-02-03 14:09:21 +01:00
|
|
|
logmsg(LOG_DEBUG, "= %d", r);
|
2019-02-03 04:38:59 +01:00
|
|
|
if (r == -1) {
|
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
2019-02-03 14:09:21 +01:00
|
|
|
logmsg(LOG_DEBUG, "EINTR");
|
2019-02-03 04:38:59 +01:00
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "endlessh: fatal: %s\n", strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for new incoming connections */
|
|
|
|
if (pollvec->fds[0].revents & POLLIN) {
|
|
|
|
int fd = accept(server, 0, 0);
|
2019-02-03 14:09:21 +01:00
|
|
|
logmsg(LOG_DEBUG, "accept() = %d", fd);
|
2019-02-03 04:38:59 +01:00
|
|
|
if (fd == -1) {
|
|
|
|
const char *msg = strerror(errno);
|
|
|
|
switch (errno) {
|
|
|
|
case EMFILE:
|
|
|
|
case ENFILE:
|
2019-02-03 17:08:56 +01:00
|
|
|
config.max_clients = nclients;
|
2019-02-03 14:09:21 +01:00
|
|
|
logmsg(LOG_INFO,
|
2019-02-03 17:08:56 +01:00
|
|
|
"MaxClients %d",
|
2019-02-03 15:18:28 +01:00
|
|
|
nclients);
|
2019-02-03 05:19:44 +01:00
|
|
|
break;
|
|
|
|
case ECONNABORTED:
|
|
|
|
case EINTR:
|
2019-02-03 04:38:59 +01:00
|
|
|
case ENOBUFS:
|
|
|
|
case ENOMEM:
|
|
|
|
case EPROTO:
|
|
|
|
fprintf(stderr, "endlessh: warning: %s\n", msg);
|
2019-02-03 05:19:44 +01:00
|
|
|
break;
|
2019-02-03 04:38:59 +01:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "endlessh: fatal: %s\n", msg);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
} else {
|
2019-02-03 17:08:56 +01:00
|
|
|
long long send_next = uepoch() + config.delay / 2;
|
|
|
|
struct client *client = client_new(fd, send_next);
|
2019-02-03 04:38:59 +01:00
|
|
|
if (!client) {
|
|
|
|
fprintf(stderr, "endlessh: warning: out of memory\n");
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
nclients++;
|
2019-02-03 19:06:14 +01:00
|
|
|
logmsg(LOG_INFO, "ACCEPT host=%s port=%d fd=%d n=%d/%d",
|
2019-02-03 15:18:28 +01:00
|
|
|
client->ipaddr, client->port, client->fd,
|
2019-02-03 17:08:56 +01:00
|
|
|
nclients, config.max_clients);
|
2019-02-03 14:09:21 +01:00
|
|
|
queue_append(queue, client);
|
2019-02-03 04:38:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write lines to ready clients */
|
|
|
|
for (size_t i = 1; i < pollvec->fill; i++) {
|
|
|
|
short fd = pollvec->fds[i].fd;
|
|
|
|
short revents = pollvec->fds[i].revents;
|
|
|
|
struct client *client = queue_remove(queue, fd);
|
|
|
|
|
|
|
|
if (revents & POLLHUP) {
|
2019-02-03 15:18:28 +01:00
|
|
|
client_destroy(client);
|
2019-02-03 04:38:59 +01:00
|
|
|
nclients--;
|
|
|
|
|
|
|
|
} else if (revents & POLLOUT) {
|
2019-02-03 14:09:21 +01:00
|
|
|
char line[256];
|
2019-02-03 17:08:56 +01:00
|
|
|
int len = randline(line, config.max_line_length);
|
2019-02-03 15:18:28 +01:00
|
|
|
/* Don't really care if send is short */
|
2019-02-03 14:09:21 +01:00
|
|
|
ssize_t out = send(fd, line, len, MSG_DONTWAIT);
|
2019-02-03 15:18:28 +01:00
|
|
|
if (out < 0)
|
|
|
|
client_destroy(client);
|
|
|
|
else {
|
|
|
|
logmsg(LOG_DEBUG, "send(%d) = %d", fd, (int)out);
|
|
|
|
client->bytes_sent += out;
|
2019-02-03 17:08:56 +01:00
|
|
|
client->send_next = uepoch() + config.delay;
|
2019-02-03 15:18:28 +01:00
|
|
|
queue_append(queue, client);
|
|
|
|
}
|
2019-02-03 04:38:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-03 15:18:28 +01:00
|
|
|
|
|
|
|
pollvec_free(pollvec);
|
|
|
|
queue_destroy(queue);
|
2019-02-03 04:38:59 +01:00
|
|
|
}
|