X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/514702987c9252fcb0ab98882a6603b3bd0505ce..11ddab61475d6b28bf953f924d6545ffb6bad8ff:/telnet.c diff --git a/telnet.c b/telnet.c index 1a85b28e..cae64d83 100644 --- a/telnet.c +++ b/telnet.c @@ -1,4 +1,3 @@ -#include #include #include @@ -192,6 +191,7 @@ typedef struct telnet_tag { Socket s; void *frontend; + void *ldisc; int term_width, term_height; int opt_states[NUM_OPTS]; @@ -223,14 +223,20 @@ static void c_write1(Telnet telnet, int c) sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); } -static void log_option(char *sender, int cmd, int option) +static void log_option(Telnet telnet, char *sender, int cmd, int option) { - char buf[50]; - sprintf(buf, "%s:\t%s %s", sender, - (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : - cmd == DO ? "DO" : cmd == DONT ? "DONT" : ""), - telopt(option)); - logevent(buf); + char *buf; + /* + * The strange-looking "" below is there to avoid a + * trigraph - a double question mark followed by > maps to a + * closing brace character! + */ + buf = dupprintf("%s:\t%s %s", sender, + (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : + cmd == DO ? "DO" : cmd == DONT ? "DONT" : ""), + telopt(option)); + logevent(telnet->frontend, buf); + sfree(buf); } static void send_opt(Telnet telnet, int cmd, int option) @@ -241,7 +247,7 @@ static void send_opt(Telnet telnet, int cmd, int option) b[1] = cmd; b[2] = option; telnet->bufsize = sk_write(telnet->s, b, 3); - log_option("client", cmd, option); + log_option(telnet, "client", cmd, option); } static void deactivate_option(Telnet telnet, const struct Opt *o) @@ -261,7 +267,8 @@ static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled) telnet->echoing = !enabled; else if (o->option == TELOPT_SGA && o->send == DO) telnet->editing = !enabled; - ldisc_send(NULL, 0, 0); /* cause ldisc to notice the change */ + if (telnet->ldisc) /* cause ldisc to notice the change */ + ldisc_send(telnet->ldisc, NULL, 0, 0); /* Ensure we get the minimum options */ if (!telnet->activated) { @@ -312,7 +319,7 @@ static void proc_rec_opt(Telnet telnet, int cmd, int option) { const struct Opt *const *o; - log_option("server", cmd, option); + log_option(telnet, "server", cmd, option); for (o = opts; *o; o++) { if ((*o)->option == option && (*o)->ack == cmd) { switch (telnet->opt_states[(*o)->index]) { @@ -366,7 +373,7 @@ static void process_subneg(Telnet telnet) switch (telnet->sb_opt) { case TELOPT_TSPEED: if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { - char logbuf[sizeof(cfg.termspeed) + 80]; + char *logbuf; b[0] = IAC; b[1] = SB; b[2] = TELOPT_TSPEED; @@ -376,15 +383,16 @@ static void process_subneg(Telnet telnet) b[n] = IAC; b[n + 1] = SE; telnet->bufsize = sk_write(telnet->s, b, n + 2); - logevent("server:\tSB TSPEED SEND"); - sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed); - logevent(logbuf); + logevent(telnet->frontend, "server:\tSB TSPEED SEND"); + logbuf = dupprintf("client:\tSB TSPEED IS %s", cfg.termspeed); + logevent(telnet->frontend, logbuf); + sfree(logbuf); } else - logevent("server:\tSB TSPEED "); + logevent(telnet->frontend, "server:\tSB TSPEED "); break; case TELOPT_TTYPE: if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { - char logbuf[sizeof(cfg.termtype) + 80]; + char *logbuf; b[0] = IAC; b[1] = SB; b[2] = TELOPT_TTYPE; @@ -398,21 +406,23 @@ static void process_subneg(Telnet telnet) b[n + 5] = SE; telnet->bufsize = sk_write(telnet->s, b, n + 6); b[n + 4] = 0; - logevent("server:\tSB TTYPE SEND"); - sprintf(logbuf, "client:\tSB TTYPE IS %s", b + 4); - logevent(logbuf); + logevent(telnet->frontend, "server:\tSB TTYPE SEND"); + logbuf = dupprintf("client:\tSB TTYPE IS %s", b + 4); + logevent(telnet->frontend, logbuf); + sfree(logbuf); } else - logevent("server:\tSB TTYPE \r\n"); + logevent(telnet->frontend, "server:\tSB TTYPE \r\n"); break; case TELOPT_OLD_ENVIRON: case TELOPT_NEW_ENVIRON: p = telnet->sb_buf; q = p + telnet->sb_len; if (p < q && *p == TELQUAL_SEND) { - char logbuf[50]; + char *logbuf; p++; - sprintf(logbuf, "server:\tSB %s SEND", telopt(telnet->sb_opt)); - logevent(logbuf); + logbuf = dupprintf("server:\tSB %s SEND", telopt(telnet->sb_opt)); + logevent(telnet->frontend, logbuf); + sfree(logbuf); if (telnet->sb_opt == TELOPT_OLD_ENVIRON) { if (cfg.rfc_environ) { value = RFC_VALUE; @@ -473,9 +483,10 @@ static void process_subneg(Telnet telnet) b[n++] = IAC; b[n++] = SE; telnet->bufsize = sk_write(telnet->s, b, n); - sprintf(logbuf, "client:\tSB %s IS %s", telopt(telnet->sb_opt), - n == 6 ? "" : ""); - logevent(logbuf); + logbuf = dupprintf("client:\tSB %s IS %s", telopt(telnet->sb_opt), + n == 6 ? "" : ""); + logevent(telnet->frontend, logbuf); + sfree(logbuf); } break; } @@ -603,7 +614,7 @@ static int telnet_closing(Plug plug, char *error_msg, int error_code, } if (error_msg) { /* A socket error has occurred. */ - logevent(error_msg); + logevent(telnet->frontend, error_msg); connection_fatal("%s", error_msg); } /* Otherwise, the remote side closed the connection normally. */ return 0; @@ -662,9 +673,10 @@ static char *telnet_init(void *frontend_handle, void **backend_handle, * Try to find host. */ { - char buf[200]; - sprintf(buf, "Looking up host \"%.170s\"", host); - logevent(buf); + char *buf; + buf = dupprintf("Looking up host \"%s\"", host); + logevent(telnet->frontend, buf); + sfree(buf); } addr = sk_namelookup(host, realhost); if ((err = sk_addr_error(addr))) @@ -677,10 +689,11 @@ static char *telnet_init(void *frontend_handle, void **backend_handle, * Open socket. */ { - char buf[200], addrbuf[100]; + char *buf, addrbuf[100]; sk_getaddr(addr, addrbuf, 100); - sprintf(buf, "Connecting to %.100s port %d", addrbuf, port); - logevent(buf); + buf = dupprintf("Connecting to %s port %d", addrbuf, port); + logevent(telnet->frontend, buf); + sfree(buf); } telnet->s = new_connection(addr, *realhost, port, 0, 1, nodelay, (Plug) telnet); @@ -736,7 +749,7 @@ static int telnet_send(void *handle, char *buf, int len) while (p < buf + len) { char *q = p; - while (iswritable((unsigned char) *p) && p < buf + len) + while (p < buf + len && iswritable((unsigned char) *p)) p++; telnet->bufsize = sk_write(telnet->s, q, p - q); @@ -766,7 +779,7 @@ static void telnet_size(void *handle, int width, int height) { Telnet telnet = (Telnet) handle; unsigned char b[16]; - char logbuf[50]; + char *logbuf; telnet->term_width = width; telnet->term_height = height; @@ -783,10 +796,11 @@ static void telnet_size(void *handle, int width, int height) b[7] = IAC; b[8] = SE; telnet->bufsize = sk_write(telnet->s, b, 9); - sprintf(logbuf, "client:\tSB NAWS %d,%d", - ((unsigned char) b[3] << 8) + (unsigned char) b[4], - ((unsigned char) b[5] << 8) + (unsigned char) b[6]); - logevent(logbuf); + logbuf = dupprintf("client:\tSB NAWS %d,%d", + ((unsigned char) b[3] << 8) + (unsigned char) b[4], + ((unsigned char) b[5] << 8) + (unsigned char) b[6]); + logevent(telnet->frontend, logbuf); + sfree(logbuf); } /* @@ -888,7 +902,7 @@ static Socket telnet_socket(void *handle) static int telnet_sendok(void *handle) { - Telnet telnet = (Telnet) handle; + /* Telnet telnet = (Telnet) handle; */ return 1; } @@ -908,9 +922,20 @@ static int telnet_ldisc(void *handle, int option) return FALSE; } -static int telnet_exitcode(void *handle) +static void telnet_provide_ldisc(void *handle, void *ldisc) { Telnet telnet = (Telnet) handle; + telnet->ldisc = ldisc; +} + +static void telnet_provide_logctx(void *handle, void *logctx) +{ + /* This is a stub. */ +} + +static int telnet_exitcode(void *handle) +{ + /* Telnet telnet = (Telnet) handle; */ /* Telnet doesn't transmit exit codes back to the client */ return 0; } @@ -925,6 +950,8 @@ Backend telnet_backend = { telnet_exitcode, telnet_sendok, telnet_ldisc, + telnet_provide_ldisc, + telnet_provide_logctx, telnet_unthrottle, 23 };