1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/* connection events, for libreswan
*
* Copyright (C) 2023 Andrew Cagney
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
*
* This program 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 General Public License
* for more details.
*/
#include "enum_names.h"
#include "defs.h"
#include "log.h"
#include "server.h"
#include "connections.h"
#include "connection_event.h"
#include "revival.h" /* for revive_connection() */
#include "list_entry.h"
#include "iface.h" /* for struct iface_endpoint */
static void connection_event_handler(void *arg, const struct timer_event *event);
struct connection_event {
enum connection_event_kind kind;
struct connection *connection;
const char *subplot;
struct timeout *timeout;
struct logger *logger;
};
static void jam_connection_event(struct jambuf *buf, const struct connection_event *event)
{
const struct connection *c = event->connection;
/* currently only one */
jam_string(buf, " ");
jam_enum_short(buf, &connection_event_kind_names, event->kind);
switch (event->kind) {
case CONNECTION_REVIVAL:
if (c->redirect.attempt > 0) {
jam_string(buf, "; redirect");
jam(buf, " attempt %u", c->redirect.attempt);
jam_string(buf, " from ");
jam_address_sensitive(buf, &c->redirect.old_gw_address);
jam_string(buf, " to ");
jam_address_sensitive(buf, &c->redirect.ip);
}
if (c->revival.attempt > 0) {
jam(buf, "; attempt %u", c->revival.attempt);
jam_string(buf, " next in ");
jam_deltatime(buf, c->revival.delay);
jam_string(buf, "s");
if (c->revival.remote.is_set) {
jam_string(buf, " to ");
jam_endpoint_sensitive(buf, &c->revival.remote);
}
if (c->revival.local != NULL) {
jam_string(buf, " via ");
jam_endpoint_sensitive(buf, &c->revival.local->local_endpoint);
}
}
break;
}
jam_string(buf, "; ");
jam_string(buf, event->subplot);
}
void schedule_connection_event(struct connection *c,
enum connection_event_kind event_kind,
const char *subplot,
deltatime_t delay,
const char *impair, struct logger *logger)
{
struct connection_event *d = alloc_thing(struct connection_event, "data");
connection_buf cb;
enum_buf kb;
d->logger = string_logger(HERE, "event %s for "PRI_CONNECTION,
str_enum(&connection_event_kind_names, event_kind, &kb),
pri_connection(c, &cb));
d->kind = event_kind;
d->subplot = subplot;
d->connection = connection_addref(c, d->logger);
c->events[event_kind] = d;
if (impair != NULL) {
llog(RC_LOG, logger,
"IMPAIR: %s: skip scheduling %s event",
impair, impair);
return;
}
schedule_timeout(str_enum(&connection_event_kind_names, event_kind, &kb),
&d->timeout, delay,
connection_event_handler, d);
}
static void discard_connection_event(struct connection_event **e)
{
/*
* When impaired, .timeout is NULL but destroy_timeout()
* handles that.
*/
destroy_timeout(&(*e)->timeout);
connection_delref(&(*e)->connection, (*e)->logger);
free_logger(&(*e)->logger, HERE);
pfree(*e);
*e = NULL;
}
static void delete_connection_event(struct connection_event **e)
{
discard_connection_event(e);
}
static void dispatch_connection_event(struct connection_event *e,
const threadtime_t *inception)
{
ldbg(e->logger, "dispatching");
/* make it invisible */
e->connection->events[e->kind] = NULL;
switch (e->kind) {
case CONNECTION_REVIVAL:
revive_connection(e->connection, e->subplot, inception);
break;
}
discard_connection_event(&e);
}
void connection_event_handler(void *arg, const struct timer_event *event)
{
dispatch_connection_event(arg, &event->inception);
}
void whack_impair_call_connection_event_handler(struct connection *c,
enum connection_event_kind event_kind,
struct logger *logger)
{
struct connection_event *event = c->events[event_kind];
if (event != NULL) {
threadtime_t inception = threadtime_start();
LLOG_JAMBUF(RC_LOG, logger, buf) {
jam_string(buf, "IMPAIR: dispatch");
jam_connection_event(buf, event);
}
/* dispatch will delete */
dispatch_connection_event(event, &inception);
return;
}
enum_buf eb;
llog(RC_LOG, logger, "IMPAIR: no %s event for connection found",
str_enum_short(&connection_event_kind_names, event_kind, &eb));
}
bool connection_event_is_scheduled(const struct connection *c,
enum connection_event_kind event)
{
return (c->events[event] != NULL);
}
bool flush_connection_event(struct connection *c,
enum connection_event_kind event_kind)
{
if (c->events[event_kind] != NULL) {
delete_connection_event(&c->events[event_kind]);
return true;
}
return false;
}
bool flush_connection_events(struct connection *c)
{
bool flushed = false;
for (enum connection_event_kind e = 0; e < CONNECTION_EVENT_KIND_ROOF; e++) {
flushed |= flush_connection_event(c, e);
}
return flushed;
}
|