1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-26 01:32:25 +00:00

Rationalise access to, and content of, backends[] array.

Should be no significant change in behaviour.
(Well, entering usernames containing commas on Plink's command line will be
a little harder now.)

[originally from svn r7628]
This commit is contained in:
Jacob Nevins 2007-06-30 21:56:44 +00:00
parent 90e7bf4228
commit 46c00b0f38
23 changed files with 132 additions and 177 deletions

View File

@ -22,10 +22,10 @@ const int be_default_protocol = PROT_TELNET;
const int be_default_protocol = PROT_SSH; const int be_default_protocol = PROT_SSH;
#endif #endif
struct backend_list backends[] = { Backend *backends[] = {
{PROT_SSH, "ssh", &ssh_backend}, &ssh_backend,
{PROT_TELNET, "telnet", &telnet_backend}, &telnet_backend,
{PROT_RLOGIN, "rlogin", &rlogin_backend}, &rlogin_backend,
{PROT_RAW, "raw", &raw_backend}, &raw_backend,
{0, NULL} NULL
}; };

View File

@ -22,11 +22,11 @@ const int be_default_protocol = PROT_TELNET;
const int be_default_protocol = PROT_SSH; const int be_default_protocol = PROT_SSH;
#endif #endif
struct backend_list backends[] = { Backend *backends[] = {
{PROT_SSH, "ssh", &ssh_backend}, &ssh_backend,
{PROT_TELNET, "telnet", &telnet_backend}, &telnet_backend,
{PROT_RLOGIN, "rlogin", &rlogin_backend}, &rlogin_backend,
{PROT_RAW, "raw", &raw_backend}, &raw_backend,
{PROT_SERIAL, "serial", &serial_backend}, &serial_backend,
{0, NULL} NULL
}; };

View File

@ -1,16 +1,11 @@
/* /*
* Linking module for PSCP: list the available backends, but * Linking module for programs that do not support selection of backend
* without accompanying function suites. Used only for name * (such as pscp or pterm).
* lookups.
*/ */
#include <stdio.h> #include <stdio.h>
#include "putty.h" #include "putty.h"
struct backend_list backends[] = { Backend *backends[] = {
{PROT_SSH, "ssh", NULL}, NULL
{PROT_TELNET, "telnet", NULL},
{PROT_RLOGIN, "rlogin", NULL},
{PROT_RAW, "raw", NULL},
{0, NULL}
}; };

View File

@ -10,12 +10,12 @@ const int be_default_protocol = PROT_TELNET;
const char *const appname = "PuTTYtel"; const char *const appname = "PuTTYtel";
struct backend_list backends[] = { Backend *backends[] = {
{PROT_TELNET, "telnet", &telnet_backend}, &telnet_backend,
{PROT_RLOGIN, "rlogin", &rlogin_backend}, &rlogin_backend,
{PROT_RAW, "raw", &raw_backend}, &raw_backend,
{PROT_SERIAL, "serial", &serial_backend}, &serial_backend,
{0, NULL} NULL
}; };
/* /*

View File

@ -10,11 +10,11 @@ const int be_default_protocol = PROT_TELNET;
const char *const appname = "PuTTYtel"; const char *const appname = "PuTTYtel";
struct backend_list backends[] = { Backend *backends[] = {
{PROT_TELNET, "telnet", &telnet_backend}, &telnet_backend,
{PROT_RLOGIN, "rlogin", &rlogin_backend}, &rlogin_backend,
{PROT_RAW, "raw", &raw_backend}, &raw_backend,
{0, NULL} NULL
}; };
/* /*

View File

@ -15,20 +15,6 @@
#define HOST_BOX_TITLE "Host Name (or IP address)" #define HOST_BOX_TITLE "Host Name (or IP address)"
#define PORT_BOX_TITLE "Port" #define PORT_BOX_TITLE "Port"
/*
* Convenience function: determine whether this binary supports a
* given backend.
*/
static int have_backend(int protocol)
{
struct backend_list *p = backends;
for (p = backends; p->name; p++) {
if (p->protocol == protocol)
return 1;
}
return 0;
}
static void config_host_handler(union control *ctrl, void *dlg, static void config_host_handler(union control *ctrl, void *dlg,
void *data, int event) void *data, int event)
{ {
@ -1166,7 +1152,7 @@ void setup_config_box(struct controlbox *b, int midsession,
hp->port = c; hp->port = c;
ctrl_columns(s, 1, 100); ctrl_columns(s, 1, 100);
if (!have_backend(PROT_SSH)) { if (!backend_from_proto(PROT_SSH)) {
ctrl_radiobuttons(s, "Connection type:", NO_SHORTCUT, 3, ctrl_radiobuttons(s, "Connection type:", NO_SHORTCUT, 3,
HELPCTX(session_hostname), HELPCTX(session_hostname),
config_protocolbuttons_handler, P(hp), config_protocolbuttons_handler, P(hp),
@ -1257,7 +1243,7 @@ void setup_config_box(struct controlbox *b, int midsession,
{ {
char *sshlogname, *sshrawlogname; char *sshlogname, *sshrawlogname;
if ((midsession && protocol == PROT_SSH) || if ((midsession && protocol == PROT_SSH) ||
(!midsession && have_backend(PROT_SSH))) { (!midsession && backend_from_proto(PROT_SSH))) {
sshlogname = "SSH packets"; sshlogname = "SSH packets";
sshrawlogname = "SSH packets and raw data"; sshrawlogname = "SSH packets and raw data";
} else { } else {
@ -1293,7 +1279,7 @@ void setup_config_box(struct controlbox *b, int midsession,
dlg_stdcheckbox_handler, I(offsetof(Config,logflush))); dlg_stdcheckbox_handler, I(offsetof(Config,logflush)));
if ((midsession && protocol == PROT_SSH) || if ((midsession && protocol == PROT_SSH) ||
(!midsession && have_backend(PROT_SSH))) { (!midsession && backend_from_proto(PROT_SSH))) {
s = ctrl_getset(b, "Session/Logging", "ssh", s = ctrl_getset(b, "Session/Logging", "ssh",
"Options specific to SSH packet logging"); "Options specific to SSH packet logging");
ctrl_checkbox(s, "Omit known password fields", 'k', ctrl_checkbox(s, "Omit known password fields", 'k',
@ -1912,7 +1898,7 @@ void setup_config_box(struct controlbox *b, int midsession,
* when we're not doing SSH. * when we're not doing SSH.
*/ */
if (have_backend(PROT_SSH) && (!midsession || protocol == PROT_SSH)) { if (backend_from_proto(PROT_SSH) && (!midsession || protocol == PROT_SSH)) {
/* /*
* The Connection/SSH panel. * The Connection/SSH panel.

View File

@ -210,13 +210,10 @@ static void mac_startup(void) {
default_protocol = be_default_protocol; default_protocol = be_default_protocol;
/* Find the appropriate default port. */ /* Find the appropriate default port. */
{ {
int i; Backend *b = backend_from_proto(default_protocol);
default_port = 0; /* illegal */ default_port = 0; /* illegal */
for (i = 0; backends[i].backend != NULL; i++) if (b)
if (backends[i].protocol == default_protocol) { default_port = b->default_port;
default_port = backends[i].backend->default_port;
break;
}
} }
flags = FLAG_INTERACTIVE; flags = FLAG_INTERACTIVE;

View File

@ -115,12 +115,7 @@ void mac_startsession(Session *s)
* Select protocol. This is farmed out into a table in a * Select protocol. This is farmed out into a table in a
* separate file to enable an ssh-free variant. * separate file to enable an ssh-free variant.
*/ */
s->back = NULL; s->back = backend_from_proto(s->cfg.protocol);
for (i = 0; backends[i].backend != NULL; i++)
if (backends[i].protocol == s->cfg.protocol) {
s->back = backends[i].backend;
break;
}
if (s->back == NULL) if (s->back == NULL)
fatalbox("Unsupported protocol number found"); fatalbox("Unsupported protocol number found");

View File

@ -232,15 +232,9 @@
/* /*
* Set up a backend. * Set up a backend.
*/ */
{ back = backend_from_proto(cfg.protocol);
int i; if (!back)
back = &pty_backend; back = &pty_backend;
for (i = 0; backends[i].backend != NULL; i++)
if (backends[i].protocol == cfg.protocol) {
back = backends[i].backend;
break;
}
}
{ {
const char *error; const char *error;

10
putty.h
View File

@ -389,14 +389,12 @@ struct backend_tag {
*/ */
void (*unthrottle) (void *handle, int); void (*unthrottle) (void *handle, int);
int (*cfg_info) (void *handle); int (*cfg_info) (void *handle);
char *name;
int protocol;
int default_port; int default_port;
}; };
extern struct backend_list { extern Backend *backends[];
int protocol;
char *name;
Backend *backend;
} backends[];
/* /*
* Suggested default protocol provided by the backend link module. * Suggested default protocol provided by the backend link module.
@ -778,6 +776,8 @@ void random_destroy_seed(void);
/* /*
* Exports from settings.c. * Exports from settings.c.
*/ */
Backend *backend_from_name(const char *name);
Backend *backend_from_proto(int proto);
char *save_settings(char *section, Config * cfg); char *save_settings(char *section, Config * cfg);
void save_open_settings(void *sesskey, Config *cfg); void save_open_settings(void *sesskey, Config *cfg);
void load_settings(char *section, Config * cfg); void load_settings(char *section, Config * cfg);

2
raw.c
View File

@ -278,5 +278,7 @@ Backend raw_backend = {
raw_provide_logctx, raw_provide_logctx,
raw_unthrottle, raw_unthrottle,
raw_cfg_info, raw_cfg_info,
"raw",
PROT_RAW,
1 1
}; };

View File

@ -349,5 +349,7 @@ Backend rlogin_backend = {
rlogin_provide_logctx, rlogin_provide_logctx,
rlogin_unthrottle, rlogin_unthrottle,
rlogin_cfg_info, rlogin_cfg_info,
"rlogin",
PROT_RLOGIN,
1 1
}; };

View File

@ -52,6 +52,29 @@ const char *const ttymodes[] = {
"CS8", "PARENB", "PARODD", NULL "CS8", "PARENB", "PARODD", NULL
}; };
/*
* Convenience functions to access the backends[] array
* (which is only present in tools that manage settings).
*/
Backend *backend_from_name(const char *name)
{
Backend **p;
for (p = backends; *p != NULL; p++)
if (!strcmp((*p)->name, name))
return *p;
return NULL;
}
Backend *backend_from_proto(int proto)
{
Backend **p;
for (p = backends; *p != NULL; p++)
if ((*p)->protocol == proto)
return *p;
return NULL;
}
static void gpps(void *handle, const char *name, const char *def, static void gpps(void *handle, const char *name, const char *def,
char *val, int len) char *val, int len)
{ {
@ -259,11 +282,11 @@ void save_open_settings(void *sesskey, Config *cfg)
write_setting_i(sesskey, "SSHLogOmitPasswords", cfg->logomitpass); write_setting_i(sesskey, "SSHLogOmitPasswords", cfg->logomitpass);
write_setting_i(sesskey, "SSHLogOmitData", cfg->logomitdata); write_setting_i(sesskey, "SSHLogOmitData", cfg->logomitdata);
p = "raw"; p = "raw";
for (i = 0; backends[i].name != NULL; i++) {
if (backends[i].protocol == cfg->protocol) { const Backend *b = backend_from_proto(cfg->protocol);
p = backends[i].name; if (b)
break; p = b->name;
} }
write_setting_s(sesskey, "Protocol", p); write_setting_s(sesskey, "Protocol", p);
write_setting_i(sesskey, "PortNumber", cfg->port); write_setting_i(sesskey, "PortNumber", cfg->port);
/* The CloseOnExit numbers are arranged in a different order from /* The CloseOnExit numbers are arranged in a different order from
@ -476,12 +499,13 @@ void load_open_settings(void *sesskey, Config *cfg)
gpps(sesskey, "Protocol", "default", prot, 10); gpps(sesskey, "Protocol", "default", prot, 10);
cfg->protocol = default_protocol; cfg->protocol = default_protocol;
cfg->port = default_port; cfg->port = default_port;
for (i = 0; backends[i].name != NULL; i++) {
if (!strcmp(prot, backends[i].name)) { const Backend *b = backend_from_name(prot);
cfg->protocol = backends[i].protocol; if (b) {
cfg->protocol = b->protocol;
gppi(sesskey, "PortNumber", default_port, &cfg->port); gppi(sesskey, "PortNumber", default_port, &cfg->port);
break;
} }
}
/* Address family selection */ /* Address family selection */
gppi(sesskey, "AddressFamily", ADDRTYPE_UNSPEC, &cfg->addressfamily); gppi(sesskey, "AddressFamily", ADDRTYPE_UNSPEC, &cfg->addressfamily);

2
ssh.c
View File

@ -9146,5 +9146,7 @@ Backend ssh_backend = {
ssh_provide_logctx, ssh_provide_logctx,
ssh_unthrottle, ssh_unthrottle,
ssh_cfg_info, ssh_cfg_info,
"ssh",
PROT_SSH,
22 22
}; };

View File

@ -1106,5 +1106,7 @@ Backend telnet_backend = {
telnet_provide_logctx, telnet_provide_logctx,
telnet_unthrottle, telnet_unthrottle,
telnet_cfg_info, telnet_cfg_info,
"telnet",
PROT_TELNET,
23 23
}; };

View File

@ -59,14 +59,14 @@ Backend null_backend = {
null_init, null_free, null_reconfig, null_send, null_sendbuffer, null_size, null_init, null_free, null_reconfig, null_send, null_sendbuffer, null_size,
null_special, null_get_specials, null_connected, null_exitcode, null_sendok, null_special, null_get_specials, null_connected, null_exitcode, null_sendok,
null_ldisc, null_provide_ldisc, null_provide_logctx, null_unthrottle, null_ldisc, null_provide_ldisc, null_provide_logctx, null_unthrottle,
null_cfg_info, 0 null_cfg_info, "null", -1, 0
}; };
Backend loop_backend = { Backend loop_backend = {
loop_init, loop_free, null_reconfig, loop_send, null_sendbuffer, null_size, loop_init, loop_free, null_reconfig, loop_send, null_sendbuffer, null_size,
null_special, null_get_specials, null_connected, null_exitcode, null_sendok, null_special, null_get_specials, null_connected, null_exitcode, null_sendok,
null_ldisc, null_provide_ldisc, null_provide_logctx, null_unthrottle, null_ldisc, null_provide_ldisc, null_provide_logctx, null_unthrottle,
null_cfg_info, 0 null_cfg_info, "loop", -1, 0
}; };
struct loop_state { struct loop_state {

View File

@ -596,15 +596,11 @@ int main(int argc, char **argv)
* Override the default protocol if PLINK_PROTOCOL is set. * Override the default protocol if PLINK_PROTOCOL is set.
*/ */
char *p = getenv("PLINK_PROTOCOL"); char *p = getenv("PLINK_PROTOCOL");
int i;
if (p) { if (p) {
for (i = 0; backends[i].backend != NULL; i++) { const Backend *b = backend_from_name(p);
if (!strcmp(backends[i].name, p)) { if (b) {
default_protocol = cfg.protocol = backends[i].protocol; default_protocol = cfg.protocol = b->protocol;
default_port = cfg.port = default_port = cfg.port = b->default_port;
backends[i].backend->default_port;
break;
}
} }
} }
} }
@ -681,19 +677,14 @@ int main(int argc, char **argv)
*/ */
r = strchr(p, ','); r = strchr(p, ',');
if (r) { if (r) {
int i, j; const Backend *b;
for (i = 0; backends[i].backend != NULL; i++) { *r = '\0';
j = strlen(backends[i].name); b = backend_from_name(p);
if (j == r - p && if (b) {
!memcmp(backends[i].name, p, j)) { default_protocol = cfg.protocol = b->protocol;
default_protocol = cfg.protocol = portnumber = b->default_port;
backends[i].protocol;
portnumber =
backends[i].backend->default_port;
p = r + 1;
break;
}
} }
p = r + 1;
} }
/* /*
@ -836,19 +827,11 @@ int main(int argc, char **argv)
* Select protocol. This is farmed out into a table in a * Select protocol. This is farmed out into a table in a
* separate file to enable an ssh-free variant. * separate file to enable an ssh-free variant.
*/ */
{ back = backend_from_proto(cfg.protocol);
int i; if (back == NULL) {
back = NULL; fprintf(stderr,
for (i = 0; backends[i].backend != NULL; i++) "Internal fault: Unsupported protocol found\n");
if (backends[i].protocol == cfg.protocol) { return 1;
back = backends[i].backend;
break;
}
if (back == NULL) {
fprintf(stderr,
"Internal fault: Unsupported protocol found\n");
return 1;
}
} }
/* /*

View File

@ -1085,5 +1085,7 @@ Backend pty_backend = {
pty_provide_logctx, pty_provide_logctx,
pty_unthrottle, pty_unthrottle,
pty_cfg_info, pty_cfg_info,
"pty",
-1,
1 1
}; };

View File

@ -33,13 +33,7 @@ void cleanup_exit(int code)
Backend *select_backend(Config *cfg) Backend *select_backend(Config *cfg)
{ {
int i; Backend *back = backend_from_proto(cfg->protocol);
Backend *back = NULL;
for (i = 0; backends[i].backend != NULL; i++)
if (backends[i].protocol == cfg->protocol) {
back = backends[i].backend;
break;
}
assert(back != NULL); assert(back != NULL);
return back; return back;
} }
@ -137,13 +131,10 @@ int main(int argc, char **argv)
default_protocol = be_default_protocol; default_protocol = be_default_protocol;
/* Find the appropriate default port. */ /* Find the appropriate default port. */
{ {
int i; Backend *b = backend_from_proto(default_protocol);
default_port = 0; /* illegal */ default_port = 0; /* illegal */
for (i = 0; backends[i].backend != NULL; i++) if (b)
if (backends[i].protocol == default_protocol) { default_port = b->default_port;
default_port = backends[i].backend->default_port;
break;
}
} }
return pt_main(argc, argv); return pt_main(argc, argv);
} }

View File

@ -536,5 +536,7 @@ Backend serial_backend = {
serial_provide_logctx, serial_provide_logctx,
serial_unthrottle, serial_unthrottle,
serial_cfg_info, serial_cfg_info,
"serial",
PROT_SERIAL,
1 1
}; };

View File

@ -219,12 +219,7 @@ static void start_backend(void)
* Select protocol. This is farmed out into a table in a * Select protocol. This is farmed out into a table in a
* separate file to enable an ssh-free variant. * separate file to enable an ssh-free variant.
*/ */
back = NULL; back = backend_from_proto(cfg.protocol);
for (i = 0; backends[i].backend != NULL; i++)
if (backends[i].protocol == cfg.protocol) {
back = backends[i].backend;
break;
}
if (back == NULL) { if (back == NULL) {
char *str = dupprintf("%s Internal Error", appname); char *str = dupprintf("%s Internal Error", appname);
MessageBox(NULL, "Unsupported protocol number found", MessageBox(NULL, "Unsupported protocol number found",
@ -369,13 +364,10 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
default_protocol = be_default_protocol; default_protocol = be_default_protocol;
/* Find the appropriate default port. */ /* Find the appropriate default port. */
{ {
int i; Backend *b = backend_from_proto(default_protocol);
default_port = 0; /* illegal */ default_port = 0; /* illegal */
for (i = 0; backends[i].backend != NULL; i++) if (b)
if (backends[i].protocol == default_protocol) { default_port = b->default_port;
default_port = backends[i].backend->default_port;
break;
}
} }
cfg.logtype = LGTYP_NONE; cfg.logtype = LGTYP_NONE;

View File

@ -307,13 +307,10 @@ int main(int argc, char **argv)
char *p = getenv("PLINK_PROTOCOL"); char *p = getenv("PLINK_PROTOCOL");
int i; int i;
if (p) { if (p) {
for (i = 0; backends[i].backend != NULL; i++) { const Backend *b = backend_from_name(p);
if (!strcmp(backends[i].name, p)) { if (b) {
default_protocol = cfg.protocol = backends[i].protocol; default_protocol = cfg.protocol = b->protocol;
default_port = cfg.port = default_port = cfg.port = b->default_port;
backends[i].backend->default_port;
break;
}
} }
} }
} }
@ -380,19 +377,14 @@ int main(int argc, char **argv)
*/ */
r = strchr(p, ','); r = strchr(p, ',');
if (r) { if (r) {
int i, j; const Backend *b;
for (i = 0; backends[i].backend != NULL; i++) { *r = '\0';
j = strlen(backends[i].name); b = backend_from_name(p);
if (j == r - p && if (b) {
!memcmp(backends[i].name, p, j)) { default_protocol = cfg.protocol = b->protocol;
default_protocol = cfg.protocol = portnumber = b->default_port;
backends[i].protocol;
portnumber =
backends[i].backend->default_port;
p = r + 1;
break;
}
} }
p = r + 1;
} }
/* /*
@ -535,19 +527,11 @@ int main(int argc, char **argv)
* Select protocol. This is farmed out into a table in a * Select protocol. This is farmed out into a table in a
* separate file to enable an ssh-free variant. * separate file to enable an ssh-free variant.
*/ */
{ back = backend_from_proto(cfg.protocol);
int i; if (back == NULL) {
back = NULL; fprintf(stderr,
for (i = 0; backends[i].backend != NULL; i++) "Internal fault: Unsupported protocol found\n");
if (backends[i].protocol == cfg.protocol) { return 1;
back = backends[i].backend;
break;
}
if (back == NULL) {
fprintf(stderr,
"Internal fault: Unsupported protocol found\n");
return 1;
}
} }
/* /*

View File

@ -454,5 +454,7 @@ Backend serial_backend = {
serial_provide_logctx, serial_provide_logctx,
serial_unthrottle, serial_unthrottle,
serial_cfg_info, serial_cfg_info,
"serial",
PROT_SERIAL,
1 1
}; };