1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 09:27:59 +00:00
putty-source/pinger.c
Simon Tatham f4fbaa1bd9 Rework special-commands system to add an integer argument.
In order to list cross-certifiable host keys in the GUI specials menu,
the SSH backend has been inventing new values on the end of the
Telnet_Special enumeration, starting from the value TS_LOCALSTART.
This is inelegant, and also makes it awkward to break up special
handlers (e.g. to dispatch different specials to different SSH
layers), since if all you know about a special is that it's somewhere
in the TS_LOCALSTART+n space, you can't tell what _general kind_ of
thing it is. Also, if I ever need another open-ended set of specials
in future, I'll have to remember which TS_LOCALSTART+n codes are in
which set.

So here's a revamp that causes every special to take an extra integer
argument. For all previously numbered specials, this argument is
passed as zero and ignored, but there's a new main special code for
SSH host key cross-certification, in which the integer argument is an
index into the backend's list of available keys. TS_LOCALSTART is now
a thing of the past: if I need any other open-ended sets of specials
in future, I can add a new top-level code with a nicely separated
space of arguments.

While I'm at it, I've removed the legacy misnomer 'Telnet_Special'
from the code completely; the enum is now SessionSpecialCode, the
struct containing full details of a menu entry is SessionSpecial, and
the enum values now start SS_ rather than TS_.
2018-09-24 09:43:39 +01:00

73 lines
1.6 KiB
C

/*
* pinger.c: centralised module that deals with sending SS_PING
* keepalives, to avoid replicating this code in multiple backends.
*/
#include "putty.h"
struct pinger_tag {
int interval;
int pending;
unsigned long when_set, next;
Backend *backend;
};
static void pinger_schedule(Pinger pinger);
static void pinger_timer(void *ctx, unsigned long now)
{
Pinger pinger = (Pinger)ctx;
if (pinger->pending && now == pinger->next) {
backend_special(pinger->backend, SS_PING, 0);
pinger->pending = FALSE;
pinger_schedule(pinger);
}
}
static void pinger_schedule(Pinger pinger)
{
unsigned long next;
if (!pinger->interval) {
pinger->pending = FALSE; /* cancel any pending ping */
return;
}
next = schedule_timer(pinger->interval * TICKSPERSEC,
pinger_timer, pinger);
if (!pinger->pending ||
(next - pinger->when_set) < (pinger->next - pinger->when_set)) {
pinger->next = next;
pinger->when_set = timing_last_clock();
pinger->pending = TRUE;
}
}
Pinger pinger_new(Conf *conf, Backend *backend)
{
Pinger pinger = snew(struct pinger_tag);
pinger->interval = conf_get_int(conf, CONF_ping_interval);
pinger->pending = FALSE;
pinger->backend = backend;
pinger_schedule(pinger);
return pinger;
}
void pinger_reconfig(Pinger pinger, Conf *oldconf, Conf *newconf)
{
int newinterval = conf_get_int(newconf, CONF_ping_interval);
if (conf_get_int(oldconf, CONF_ping_interval) != newinterval) {
pinger->interval = newinterval;
pinger_schedule(pinger);
}
}
void pinger_free(Pinger pinger)
{
expire_timer_context(pinger);
sfree(pinger);
}