telnet: convert CR NUL -> CR on input. Closes bug 2569
function old new delta telnet_main 1558 1594 +36 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
6b3f0b0dab
commit
036dbb9d9a
@ -42,11 +42,13 @@ enum {
|
|||||||
UF_ECHO = 0x01,
|
UF_ECHO = 0x01,
|
||||||
UF_SGA = 0x02,
|
UF_SGA = 0x02,
|
||||||
|
|
||||||
TS_0 = 1,
|
TS_NORMAL = 0,
|
||||||
|
TS_COPY = 1,
|
||||||
TS_IAC = 2,
|
TS_IAC = 2,
|
||||||
TS_OPT = 3,
|
TS_OPT = 3,
|
||||||
TS_SUB1 = 4,
|
TS_SUB1 = 4,
|
||||||
TS_SUB2 = 5,
|
TS_SUB2 = 5,
|
||||||
|
TS_CR = 6,
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef unsigned char byte;
|
typedef unsigned char byte;
|
||||||
@ -83,13 +85,13 @@ struct globals {
|
|||||||
}; \
|
}; \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
/* Function prototypes */
|
|
||||||
static void rawmode(void);
|
static void rawmode(void);
|
||||||
static void cookmode(void);
|
static void cookmode(void);
|
||||||
static void do_linemode(void);
|
static void do_linemode(void);
|
||||||
static void will_charmode(void);
|
static void will_charmode(void);
|
||||||
static void telopt(byte c);
|
static void telopt(byte c);
|
||||||
static int subneg(byte c);
|
static void subneg(byte c);
|
||||||
|
|
||||||
static void iac_flush(void)
|
static void iac_flush(void)
|
||||||
{
|
{
|
||||||
@ -170,24 +172,24 @@ static void handle_net_output(int len)
|
|||||||
* So I implemented it. It's really useful for me. I hope that
|
* So I implemented it. It's really useful for me. I hope that
|
||||||
* other people will find it interesting too.
|
* other people will find it interesting too.
|
||||||
*/
|
*/
|
||||||
|
byte outbuf[2 * DATABUFSIZE];
|
||||||
int i, j;
|
|
||||||
byte *p = (byte*)G.buf;
|
byte *p = (byte*)G.buf;
|
||||||
byte outbuf[4*DATABUFSIZE];
|
int j = 0;
|
||||||
|
|
||||||
for (i = len, j = 0; i > 0; i--, p++) {
|
for (; len > 0; len--, p++) {
|
||||||
if (*p == 0x1d) {
|
byte c = *p;
|
||||||
|
if (c == 0x1d) {
|
||||||
con_escape();
|
con_escape();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
outbuf[j++] = *p;
|
outbuf[j++] = c;
|
||||||
if (*p == 0xff)
|
if (c == IAC)
|
||||||
outbuf[j++] = 0xff;
|
outbuf[j++] = c; /* IAC -> IAC IAC */
|
||||||
else if (*p == 0x0d)
|
else if (c == '\r')
|
||||||
outbuf[j++] = 0x00;
|
outbuf[j++] = '\0'; /* CR -> CR NUL */
|
||||||
}
|
}
|
||||||
if (j > 0)
|
if (j > 0)
|
||||||
write(netfd, outbuf, j);
|
full_write(netfd, outbuf, j);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handle_net_input(int len)
|
static void handle_net_input(int len)
|
||||||
@ -198,25 +200,44 @@ static void handle_net_input(int len)
|
|||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
byte c = G.buf[i];
|
byte c = G.buf[i];
|
||||||
|
|
||||||
if (G.telstate == 0) { /* most of the time state == 0 */
|
if (G.telstate == TS_NORMAL) { /* most typical state */
|
||||||
if (c == IAC) {
|
if (c == IAC) {
|
||||||
cstart = i;
|
cstart = i;
|
||||||
G.telstate = TS_IAC;
|
G.telstate = TS_IAC;
|
||||||
}
|
}
|
||||||
|
else if (c == '\r') {
|
||||||
|
cstart = i + 1;
|
||||||
|
G.telstate = TS_CR;
|
||||||
|
}
|
||||||
|
/* No IACs were seen so far, no need to copy
|
||||||
|
* bytes within G.buf: */
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (G.telstate) {
|
switch (G.telstate) {
|
||||||
case TS_0:
|
case TS_CR:
|
||||||
|
/* Prev char was CR. If cur one is NUL, ignore it.
|
||||||
|
* See RFC 1123 section 3.3.1 for discussion of telnet EOL handling.
|
||||||
|
*/
|
||||||
|
G.telstate = TS_COPY;
|
||||||
|
if (c == '\0')
|
||||||
|
break;
|
||||||
|
/* else: fall through - need to handle CR IAC ... properly */
|
||||||
|
|
||||||
|
case TS_COPY: /* Prev char was ordinary */
|
||||||
|
/* Similar to NORMAL, but in TS_COPY we need to copy bytes */
|
||||||
if (c == IAC)
|
if (c == IAC)
|
||||||
G.telstate = TS_IAC;
|
G.telstate = TS_IAC;
|
||||||
else
|
else
|
||||||
G.buf[cstart++] = c;
|
G.buf[cstart++] = c;
|
||||||
|
if (c == '\r')
|
||||||
|
G.telstate = TS_CR;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TS_IAC:
|
case TS_IAC: /* Prev char was IAC */
|
||||||
if (c == IAC) { /* IAC IAC -> 0xFF */
|
if (c == IAC) { /* IAC IAC -> one IAC */
|
||||||
G.buf[cstart++] = c;
|
G.buf[cstart++] = c;
|
||||||
G.telstate = TS_0;
|
G.telstate = TS_COPY;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/* else */
|
/* else */
|
||||||
@ -228,34 +249,38 @@ static void handle_net_input(int len)
|
|||||||
case DONT:
|
case DONT:
|
||||||
case WILL:
|
case WILL:
|
||||||
case WONT:
|
case WONT:
|
||||||
G.telwish = c;
|
G.telwish = c;
|
||||||
G.telstate = TS_OPT;
|
G.telstate = TS_OPT;
|
||||||
break;
|
break;
|
||||||
|
/* DATA MARK must be added later */
|
||||||
default:
|
default:
|
||||||
G.telstate = TS_0; /* DATA MARK must be added later */
|
G.telstate = TS_COPY;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TS_OPT: /* WILL, WONT, DO, DONT */
|
|
||||||
|
case TS_OPT: /* Prev chars were IAC WILL/WONT/DO/DONT */
|
||||||
telopt(c);
|
telopt(c);
|
||||||
G.telstate = TS_0;
|
G.telstate = TS_COPY;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TS_SUB1: /* Subnegotiation */
|
case TS_SUB1: /* Subnegotiation */
|
||||||
case TS_SUB2: /* Subnegotiation */
|
case TS_SUB2: /* Subnegotiation */
|
||||||
if (subneg(c))
|
subneg(c); /* can change G.telstate */
|
||||||
G.telstate = TS_0;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (G.telstate) {
|
|
||||||
|
if (G.telstate != TS_NORMAL) {
|
||||||
|
/* We had some IACs, or CR */
|
||||||
if (G.iaclen)
|
if (G.iaclen)
|
||||||
iac_flush();
|
iac_flush();
|
||||||
if (G.telstate == TS_0)
|
if (G.telstate == TS_COPY) /* we aren't in the middle of IAC */
|
||||||
G.telstate = 0;
|
G.telstate = TS_NORMAL;
|
||||||
len = cstart;
|
len = cstart;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len)
|
if (len)
|
||||||
write(STDOUT_FILENO, G.buf, len);
|
full_write(STDOUT_FILENO, G.buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void put_iac(int c)
|
static void put_iac(int c)
|
||||||
@ -495,7 +520,7 @@ static void telopt(byte c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* subnegotiation -- ignore all (except TTYPE,NAWS) */
|
/* subnegotiation -- ignore all (except TTYPE,NAWS) */
|
||||||
static int subneg(byte c)
|
static void subneg(byte c)
|
||||||
{
|
{
|
||||||
switch (G.telstate) {
|
switch (G.telstate) {
|
||||||
case TS_SUB1:
|
case TS_SUB1:
|
||||||
@ -513,12 +538,13 @@ static int subneg(byte c)
|
|||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
case TS_SUB2:
|
case TS_SUB2:
|
||||||
if (c == SE)
|
if (c == SE) {
|
||||||
return TRUE;
|
G.telstate = TS_COPY;
|
||||||
|
return;
|
||||||
|
}
|
||||||
G.telstate = TS_SUB1;
|
G.telstate = TS_SUB1;
|
||||||
/* break; */
|
break;
|
||||||
}
|
}
|
||||||
return FALSE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rawmode(void)
|
static void rawmode(void)
|
||||||
@ -533,21 +559,13 @@ static void cookmode(void)
|
|||||||
tcsetattr(0, TCSADRAIN, &G.termios_def);
|
tcsetattr(0, TCSADRAIN, &G.termios_def);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* poll gives smaller (-70 bytes) code */
|
|
||||||
#define USE_POLL 1
|
|
||||||
|
|
||||||
int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||||
int telnet_main(int argc UNUSED_PARAM, char **argv)
|
int telnet_main(int argc UNUSED_PARAM, char **argv)
|
||||||
{
|
{
|
||||||
char *host;
|
char *host;
|
||||||
int port;
|
int port;
|
||||||
int len;
|
int len;
|
||||||
#ifdef USE_POLL
|
|
||||||
struct pollfd ufds[2];
|
struct pollfd ufds[2];
|
||||||
#else
|
|
||||||
fd_set readfds;
|
|
||||||
int maxfd;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
INIT_G();
|
INIT_G();
|
||||||
|
|
||||||
@ -585,25 +603,11 @@ int telnet_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
|
|
||||||
signal(SIGINT, record_signo);
|
signal(SIGINT, record_signo);
|
||||||
|
|
||||||
#ifdef USE_POLL
|
|
||||||
ufds[0].fd = 0; ufds[1].fd = netfd;
|
ufds[0].fd = 0; ufds[1].fd = netfd;
|
||||||
ufds[0].events = ufds[1].events = POLLIN;
|
ufds[0].events = ufds[1].events = POLLIN;
|
||||||
#else
|
|
||||||
FD_ZERO(&readfds);
|
|
||||||
FD_SET(STDIN_FILENO, &readfds);
|
|
||||||
FD_SET(netfd, &readfds);
|
|
||||||
maxfd = netfd + 1;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
#ifndef USE_POLL
|
switch (poll(ufds, 2, -1)) {
|
||||||
fd_set rfds = readfds;
|
|
||||||
|
|
||||||
switch (select(maxfd, &rfds, NULL, NULL, NULL))
|
|
||||||
#else
|
|
||||||
switch (poll(ufds, 2, -1))
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
case 0:
|
case 0:
|
||||||
/* timeout */
|
/* timeout */
|
||||||
case -1:
|
case -1:
|
||||||
@ -615,12 +619,7 @@ int telnet_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
||||||
#ifdef USE_POLL
|
if (ufds[0].revents) {
|
||||||
if (ufds[0].revents)
|
|
||||||
#else
|
|
||||||
if (FD_ISSET(STDIN_FILENO, &rfds))
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
len = safe_read(STDIN_FILENO, G.buf, DATABUFSIZE);
|
len = safe_read(STDIN_FILENO, G.buf, DATABUFSIZE);
|
||||||
if (len <= 0)
|
if (len <= 0)
|
||||||
doexit(EXIT_SUCCESS);
|
doexit(EXIT_SUCCESS);
|
||||||
@ -628,12 +627,7 @@ int telnet_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
handle_net_output(len);
|
handle_net_output(len);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_POLL
|
if (ufds[1].revents) {
|
||||||
if (ufds[1].revents)
|
|
||||||
#else
|
|
||||||
if (FD_ISSET(netfd, &rfds))
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
len = safe_read(netfd, G.buf, DATABUFSIZE);
|
len = safe_read(netfd, G.buf, DATABUFSIZE);
|
||||||
if (len <= 0) {
|
if (len <= 0) {
|
||||||
full_write1_str("Connection closed by foreign host\r\n");
|
full_write1_str("Connection closed by foreign host\r\n");
|
||||||
|
Loading…
Reference in New Issue
Block a user