aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2026-02-23 15:42:43 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2026-02-23 15:42:43 +0100
commit0ac0492b37731982999be34b4936d543bfb27cbe (patch)
tree570470962194fffc16bbf56e1dc79569a8f24ddf
parent87fcfe7dfedfbc6925c164e8404c03dc02921264 (diff)
downloadbusybox-w32-0ac0492b37731982999be34b4936d543bfb27cbe.tar.gz
busybox-w32-0ac0492b37731982999be34b4936d543bfb27cbe.tar.bz2
busybox-w32-0ac0492b37731982999be34b4936d543bfb27cbe.zip
telnet: code shrink
function old new delta write_to_stdout 105 113 +8 put_iac 28 35 +7 write_to_net 596 598 +2 read_from_stdin 227 229 +2 read_from_net 543 534 -9 telnet_main 476 455 -21 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 4/2 up/down: 19/-30) Total: -11 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--networking/telnet.c46
1 files changed, 25 insertions, 21 deletions
diff --git a/networking/telnet.c b/networking/telnet.c
index 18a13860a..8a7803637 100644
--- a/networking/telnet.c
+++ b/networking/telnet.c
@@ -103,9 +103,6 @@ static char *bin_to_hex(const void *hash_value, unsigned hash_length)
103#endif 103#endif
104 104
105enum { 105enum {
106 BUFSIZE = 1024,
107 BUFMASK = BUFSIZE - 1,
108
109 TS_NORMAL = 0, 106 TS_NORMAL = 0,
110 TS_IAC = 1, 107 TS_IAC = 1,
111 TS_OPT = 2, 108 TS_OPT = 2,
@@ -114,31 +111,24 @@ enum {
114 TS_CR = 5, 111 TS_CR = 5,
115 112
116 MAX_NAWS_SIZE = 13, /* pathological 65535x65535 case needs full escaping */ 113 MAX_NAWS_SIZE = 13, /* pathological 65535x65535 case needs full escaping */
114
115 netfd = 3,
117}; 116};
118 117
119typedef unsigned char byte; 118typedef unsigned char byte;
120 119
121enum { netfd = 3 };
122
123typedef struct stdin_to_net { 120typedef struct stdin_to_net {
124 STRUCT_CONNECTION 121 STRUCT_CONNECTION
125 int rdidx, wridx, size; 122 int rdidx, wridx, size;
126 byte buf[BUFSIZE]; 123 //byte buf[BUFSIZE];
127} stdin_to_net_t; 124} stdin_to_net_t;
128
129typedef struct net_to_stdout { 125typedef struct net_to_stdout {
130 STRUCT_CONNECTION 126 STRUCT_CONNECTION
131 int rdidx, wridx, size; 127 int rdidx, wridx, size;
132 byte input_state; 128 byte input_state;
133 byte negotiation_verb; 129 byte negotiation_verb;
134 byte buf[BUFSIZE]; 130 //byte buf[BUFSIZE];
135} net_to_stdout_t; 131} net_to_stdout_t;
136
137static int remaining_free_bytes(int n)
138{
139 return BUFSIZE - n;
140}
141
142#if DEBUG 132#if DEBUG
143static void set_input_state(net_to_stdout_t *conn, int new_state, int c) 133static void set_input_state(net_to_stdout_t *conn, int new_state, int c)
144{ 134{
@@ -155,7 +145,9 @@ static void set_input_state(net_to_stdout_t *conn, int new_state, int c)
155 145
156struct globals { 146struct globals {
157 unsigned flags; 147 unsigned flags;
148/* Set when server agreed to use NAWS: */
158#define FLAGS_NAWS_ON (1 << 0) 149#define FLAGS_NAWS_ON (1 << 0)
150/* SGA option seen and responded to, no longer look for it: */
159#define FLAGS_SGA_SEEN (1 << 1) 151#define FLAGS_SGA_SEEN (1 << 1)
160/* Seen telnet protocol from server and sent our wishes: */ 152/* Seen telnet protocol from server and sent our wishes: */
161#define INITIAL_SENT (1 << 2) 153#define INITIAL_SENT (1 << 2)
@@ -183,12 +175,24 @@ struct globals {
183 net_to_stdout_t conn_net2stdout; 175 net_to_stdout_t conn_net2stdout;
184 struct termios termios_def; 176 struct termios termios_def;
185 struct termios termios_raw; 177 struct termios termios_raw;
178// buf[] arrays in conn structs are conceptually cleaner, but they
179// make G.member offsets larger -> larger code
180#define BUF_TTY2NET ((byte*)bb_common_bufsiz1)
181#define BUF_NET2TTY G.buf2
182#define BUFSIZE 1024
183/* Note: can't just increase BUFSIZE arbitrarily: common bufsiz1 is not guaranteed to be >1k! */
184#define BUFMASK (BUFSIZE - 1)
185 byte buf2[BUFSIZE];
186} FIX_ALIASING; 186} FIX_ALIASING;
187#define G (*ptr_to_globals) 187#define G (*ptr_to_globals)
188#define INIT_G() do { \ 188#define INIT_G() do { \
189 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ 189 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
190} while (0) 190} while (0)
191 191
192static int remaining_free_bytes(int n)
193{
194 return BUFSIZE - n;
195}
192 196
193#if ENABLE_FEATURE_TELNET_WIDTH 197#if ENABLE_FEATURE_TELNET_WIDTH
194static void handle_SIGWINCH(int sig UNUSED_PARAM) 198static void handle_SIGWINCH(int sig UNUSED_PARAM)
@@ -221,7 +225,7 @@ static void put_iac(int c)
221{ 225{
222 stdin_to_net_t *conn = &G.conn_stdin2net; 226 stdin_to_net_t *conn = &G.conn_stdin2net;
223 /* Write directly to stdin2net buffer */ 227 /* Write directly to stdin2net buffer */
224 conn->buf[conn->rdidx] = c; /* "... & 0xff" is implicit */ 228 BUF_TTY2NET[conn->rdidx] = c; /* "... & 0xff" is implicit */
225 conn->rdidx = (conn->rdidx + 1) & BUFMASK; 229 conn->rdidx = (conn->rdidx + 1) & BUFMASK;
226 conn->size++; 230 conn->size++;
227} 231}
@@ -528,7 +532,7 @@ static int read_from_stdin(void *this)
528 if (count == 0) 532 if (count == 0)
529 return 0; 533 return 0;
530 534
531 start = conn->buf + conn->rdidx; 535 start = BUF_TTY2NET + conn->rdidx;
532 count = safe_read(conn->read_fd, start, count); 536 count = safe_read(conn->read_fd, start, count);
533 if (count <= 0) { 537 if (count <= 0) {
534 conn->read_fd = -1; 538 conn->read_fd = -1;
@@ -606,7 +610,7 @@ static int write_to_net(void *this)
606 handle_changes_in_options(conn); /* yes */ 610 handle_changes_in_options(conn); /* yes */
607 611
608 count = MIN(BUFSIZE - conn->wridx, conn->size); 612 count = MIN(BUFSIZE - conn->wridx, conn->size);
609 count = safe_write(conn->write_fd, conn->buf + conn->wridx, count); 613 count = safe_write(conn->write_fd, BUF_TTY2NET + conn->wridx, count);
610 if (count <= 0) { 614 if (count <= 0) {
611 if (count < 0 && errno == EAGAIN) 615 if (count < 0 && errno == EAGAIN)
612 return 0; 616 return 0;
@@ -647,7 +651,7 @@ static int read_from_net(void *this)
647 count = MIN(BUFSIZE - conn->rdidx, count); /* can't be zero */ 651 count = MIN(BUFSIZE - conn->rdidx, count); /* can't be zero */
648 652
649 /* Read directly into circular buffer's linear fragment */ 653 /* Read directly into circular buffer's linear fragment */
650 dst = conn->buf + conn->rdidx; 654 dst = BUF_NET2TTY + conn->rdidx;
651 count = safe_read(conn->read_fd, dst, count); 655 count = safe_read(conn->read_fd, dst, count);
652 dbg("read_from_net bytes:%d input_state:%d %s", 656 dbg("read_from_net bytes:%d input_state:%d %s",
653 count, conn->input_state, bin_to_hex(dst, count > 0 ? count : 0)); 657 count, conn->input_state, bin_to_hex(dst, count > 0 ? count : 0));
@@ -804,7 +808,7 @@ static int read_from_net(void *this)
804 } 808 }
805 809
806 /* Update circular buffer: only the compacted data */ 810 /* Update circular buffer: only the compacted data */
807 count = dst - (conn->buf + conn->rdidx); 811 count = dst - (BUF_NET2TTY + conn->rdidx);
808 conn->size += count; 812 conn->size += count;
809 conn->rdidx = (conn->rdidx + count) & BUFMASK; 813 conn->rdidx = (conn->rdidx + count) & BUFMASK;
810 814
@@ -831,8 +835,8 @@ static int write_to_stdout(void *this)
831 int wr = MIN(BUFSIZE - conn->wridx, conn->size); 835 int wr = MIN(BUFSIZE - conn->wridx, conn->size);
832 ssize_t count; 836 ssize_t count;
833 837
834 dbg("write_to_stdout: wr:%d %s", wr, bin_to_hex(conn->buf + conn->wridx, wr)); 838 dbg("write_to_stdout: wr:%d %s", wr, bin_to_hex(BUF_NET2TTY + conn->wridx, wr));
835 count = safe_write(conn->write_fd, conn->buf + conn->wridx, wr); 839 count = safe_write(conn->write_fd, BUF_NET2TTY + conn->wridx, wr);
836 if (count <= 0) { 840 if (count <= 0) {
837 if (count < 0 && errno == EAGAIN) 841 if (count < 0 && errno == EAGAIN)
838 return 0; 842 return 0;