1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00
putty-source/otherbackends/testback.c

201 lines
5.2 KiB
C
Raw Permalink Normal View History

/*
* Copyright (c) 1999 Simon Tatham
* Copyright (c) 1999 Ben Harris
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/* PuTTY test backends */
#include <stdio.h>
#include <stdlib.h>
#include "putty.h"
static char *loop_init(const BackendVtable *, Seat *, Backend **, LogContext *,
Conf *, const char *, int, char **, bool, bool);
static void loop_free(Backend *);
static void null_reconfig(Backend *, Conf *);
static void null_send(Backend *, const char *, size_t);
static void loop_send(Backend *, const char *, size_t);
static size_t null_sendbuffer(Backend *);
static size_t loop_sendbuffer(Backend *);
static void null_size(Backend *, int, int);
static void null_special(Backend *, SessionSpecialCode, int);
static const SessionSpecial *null_get_specials(Backend *);
static bool null_connected(Backend *);
static int null_exitcode(Backend *);
static bool null_sendok(Backend *);
static bool null_ldisc(Backend *, int);
static void null_provide_ldisc(Backend *, Ldisc *);
static void null_unthrottle(Backend *, size_t);
static int null_cfg_info(Backend *);
const BackendVtable null_backend = {
.init = loop_init,
.free = loop_free,
.reconfig = null_reconfig,
.send = null_send,
.sendbuffer = null_sendbuffer,
.size = null_size,
.special = null_special,
.get_specials = null_get_specials,
.connected = null_connected,
.exitcode = null_exitcode,
.sendok = null_sendok,
.ldisc_option_state = null_ldisc,
.provide_ldisc = null_provide_ldisc,
.unthrottle = null_unthrottle,
.cfg_info = null_cfg_info,
.id = "null",
.displayname_tc = "Null",
.displayname_lc = "null",
.protocol = -1,
.default_port = 0,
};
const BackendVtable loop_backend = {
.init = loop_init,
.free = loop_free,
.reconfig = null_reconfig,
.send = loop_send,
.sendbuffer = loop_sendbuffer,
.size = null_size,
.special = null_special,
.get_specials = null_get_specials,
.connected = null_connected,
.exitcode = null_exitcode,
.sendok = null_sendok,
.ldisc_option_state = null_ldisc,
.provide_ldisc = null_provide_ldisc,
.unthrottle = null_unthrottle,
.cfg_info = null_cfg_info,
.id = "loop",
.displayname_tc = "Loop",
.displayname_lc = "loop",
.protocol = -1,
.default_port = 0,
};
struct loop_state {
New abstraction 'Seat', to pass to backends. This is a new vtable-based abstraction which is passed to a backend in place of Frontend, and it implements only the subset of the Frontend functions needed by a backend. (Many other Frontend functions still exist, notably the wide range of things called by terminal.c providing platform-independent operations on the GUI terminal window.) The purpose of making it a vtable is that this opens up the possibility of creating a backend as an internal implementation detail of some other activity, by providing just that one backend with a custom Seat that implements the methods differently. For example, this refactoring should make it feasible to directly implement an SSH proxy type, aka the 'jump host' feature supported by OpenSSH, aka 'open a secondary SSH session in MAINCHAN_DIRECT_TCP mode, and then expose the main channel of that as the Socket for the primary connection'. (Which of course you can already do by spawning 'plink -nc' as a separate proxy process, but this would permit it in the _same_ process without anything getting confused.) I've centralised a full set of stub methods in misc.c for the new abstraction, which allows me to get rid of several annoying stubs in the previous code. Also, while I'm here, I've moved a lot of duplicated modalfatalbox() type functions from application main program files into wincons.c / uxcons.c, which I think saves duplication overall. (A minor visible effect is that the prefixes on those console-based fatal error messages will now be more consistent between applications.)
2018-10-11 18:58:42 +00:00
Seat *seat;
Backend backend;
size_t sendbuffer;
};
static char *loop_init(const BackendVtable *vt, Seat *seat,
Backend **backend_handle, LogContext *logctx,
Conf *conf, const char *host, int port,
char **realhost, bool nodelay, bool keepalive) {
struct loop_state *st = snew(struct loop_state);
/* No local authentication phase in this protocol */
seat_set_trust_status(seat, false);
New abstraction 'Seat', to pass to backends. This is a new vtable-based abstraction which is passed to a backend in place of Frontend, and it implements only the subset of the Frontend functions needed by a backend. (Many other Frontend functions still exist, notably the wide range of things called by terminal.c providing platform-independent operations on the GUI terminal window.) The purpose of making it a vtable is that this opens up the possibility of creating a backend as an internal implementation detail of some other activity, by providing just that one backend with a custom Seat that implements the methods differently. For example, this refactoring should make it feasible to directly implement an SSH proxy type, aka the 'jump host' feature supported by OpenSSH, aka 'open a secondary SSH session in MAINCHAN_DIRECT_TCP mode, and then expose the main channel of that as the Socket for the primary connection'. (Which of course you can already do by spawning 'plink -nc' as a separate proxy process, but this would permit it in the _same_ process without anything getting confused.) I've centralised a full set of stub methods in misc.c for the new abstraction, which allows me to get rid of several annoying stubs in the previous code. Also, while I'm here, I've moved a lot of duplicated modalfatalbox() type functions from application main program files into wincons.c / uxcons.c, which I think saves duplication overall. (A minor visible effect is that the prefixes on those console-based fatal error messages will now be more consistent between applications.)
2018-10-11 18:58:42 +00:00
st->seat = seat;
st->backend.vt = vt;
*backend_handle = &st->backend;
*realhost = dupstr(host);
return NULL;
}
static void loop_free(Backend *be)
{
struct loop_state *st = container_of(be, struct loop_state, backend);
sfree(st);
}
static void null_reconfig(Backend *be, Conf *conf) {
}
static void null_send(Backend *be, const char *buf, size_t len) {
}
static void loop_send(Backend *be, const char *buf, size_t len) {
struct loop_state *st = container_of(be, struct loop_state, backend);
st->sendbuffer = seat_output(st->seat, 0, buf, len);
}
static size_t null_sendbuffer(Backend *be) {
return 0;
}
static size_t loop_sendbuffer(Backend *be) {
struct loop_state *st = container_of(be, struct loop_state, backend);
return st->sendbuffer;
}
static void null_size(Backend *be, int width, int height) {
}
static void null_special(Backend *be, SessionSpecialCode code, int arg) {
}
static const SessionSpecial *null_get_specials (Backend *be) {
return NULL;
}
static bool null_connected(Backend *be) {
return false;
}
static int null_exitcode(Backend *be) {
return 0;
}
static bool null_sendok(Backend *be) {
return true;
}
static void null_unthrottle(Backend *be, size_t backlog) {
}
static bool null_ldisc(Backend *be, int option) {
return false;
}
static void null_provide_ldisc (Backend *be, Ldisc *ldisc) {
}
static int null_cfg_info(Backend *be)
{
return 0;
}