File: telnet.c

package info (click to toggle)
gensio 2.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,776 kB
  • sloc: ansic: 45,195; python: 2,718; cpp: 1,129; sh: 586; makefile: 420
file content (299 lines) | stat: -rw-r--r-- 7,401 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 *  gensio - A library for abstracting stream I/O
 *  Copyright (C) 2018  Corey Minyard <minyard@acm.org>
 *
 *  SPDX-License-Identifier: LGPL-2.1-only
 */

#include "config.h"
#include <stdlib.h>
#include <string.h>

#include "telnet.h"

static struct telnet_cmd *
find_cmd(struct telnet_cmd *array, unsigned char option)
{
    int i;

    for (i = 0; array[i].option != TELNET_CMD_END_OPTION; i++) {
	if (array[i].option == option)
	    return &array[i];
    }
    return NULL;
}

void
telnet_cmd_send(telnet_data_t *td, const unsigned char *cmd, int len)
{
    if (buffer_output(&td->out_telnet_cmd, cmd, len) < len) {
	/* Out of data, abort the connection.  This really shouldn't
	   happen.*/
	td->error = 1;
	return;
    }

    td->output_ready(td->cb_data);
}

static void
send_i(telnet_data_t *td, unsigned char type, unsigned char option)
{
    unsigned char i[3];
    i[0] = TN_IAC;
    i[1] = type;
    i[2] = option;
    telnet_cmd_send(td, i, 3);
}

static void
handle_telnet_cmd(telnet_data_t *td)
{
    int size = td->telnet_cmd_pos;
    unsigned char *cmd_str = td->telnet_cmd;
    struct telnet_cmd *cmd;
    int rv;
    unsigned char option;

    if (size < 2)
	return;

    if (cmd_str[1] < TN_SB) { /* A one-byte command. */
	td->cmd_handler(td->cb_data, cmd_str[1]);
    } else if (cmd_str[1] == TN_SB) { /* Option */
	cmd = find_cmd(td->cmds, cmd_str[2]);
	if (!cmd || !cmd->option_handler)
	    return;
	cmd->option_handler(td->cb_data, cmd_str + 2, size - 2);
    } else if (cmd_str[1] == TN_WILL) {
	option = cmd_str[2];
	cmd = find_cmd(td->cmds, option);
	if (!cmd) {
	    send_i(td, TN_DONT, option);
	} else {
	    rv = cmd->i_do;
	    if (!rv && cmd->will_do_handler) {
		rv = cmd->will_do_handler(td->cb_data, TN_WILL);
		cmd->i_do = rv;
	    }
	    if (!rv || !cmd->sent_do) {
		send_i(td, rv ? TN_DO : TN_DONT, option);
		cmd->sent_do = 1;
	    }
	    cmd->rem_will = rv;
	}
    } else if (cmd_str[1] == TN_WONT) {
	option = cmd_str[2];
	cmd = find_cmd(td->cmds, option);
	if (cmd) {
	    if (cmd->will_do_handler)
		cmd->will_do_handler(td->cb_data, TN_WONT);
	    if (cmd->rem_will || !cmd->sent_do) {
		send_i(td, TN_DONT, option);
		cmd->sent_do = 1;
	    }
	    cmd->rem_will = 0;
	}
    } else if (cmd_str[1] == TN_DO) {
	option = cmd_str[2];
	cmd = find_cmd(td->cmds, option);
	if (cmd) {
	    rv = cmd->i_will;
	    if (cmd->will_do_handler) {
		rv = cmd->will_do_handler(td->cb_data, TN_DO);
		cmd->i_will = rv;
	    }
	    if (!rv || !cmd->sent_will) {
		send_i(td, rv ? TN_WILL : TN_WONT, option);
		cmd->sent_will = 1;
	    }
	    cmd->rem_do = rv;
	}
    } else if (cmd_str[1] == TN_DONT) {
	option = cmd_str[2];
	cmd = find_cmd(td->cmds, option);
	if (cmd) {
	    if (cmd->will_do_handler)
		cmd->will_do_handler(td->cb_data, TN_DONT);
	    if (cmd->rem_do || !cmd->sent_will) {
		send_i(td, TN_WONT, option);
		cmd->sent_will = 1;
	    }
	    cmd->rem_do = 0;
	}
    }
}

void
telnet_send_option(telnet_data_t *td, const unsigned char *option,
		   unsigned int len)
{
    unsigned int real_len;
    unsigned int i;

    /* Make sure to account for any duplicate 255s. */
    for (real_len = 0, i = 0; i < len; i++, real_len++) {
	if (option[i] == TN_IAC)
	    real_len++;
    }

    real_len += 4; /* Add the initial and end markers. */

    if (real_len > buffer_left(&td->out_telnet_cmd)) {
	/* Out of data, abort the connection.  This really shouldn't
	   happen.*/
	td->error = 1;
	return;
    }

    buffer_outchar(&td->out_telnet_cmd, TN_IAC);
    buffer_outchar(&td->out_telnet_cmd, TN_SB);
    for (i = 0; i < len; i++) {
	buffer_outchar(&td->out_telnet_cmd, option[i]);
	if (option[i] == TN_IAC)
	    buffer_outchar(&td->out_telnet_cmd, option[i]);
    }
    buffer_outchar(&td->out_telnet_cmd, TN_IAC);
    buffer_outchar(&td->out_telnet_cmd, TN_SE);

    td->output_ready(td->cb_data);
}

unsigned int
process_telnet_data(unsigned char *outdata, unsigned int outlen,
		    unsigned char **r_indata, unsigned int *inlen,
		    telnet_data_t *td)
{
    unsigned int i, j;
    unsigned char *indata = *r_indata;

    /* If it's a telnet port, get the commands out of the stream. */
    for (i = 0, j = 0; i < *inlen && j < outlen; i++) {
	if (td->telnet_cmd_pos != 0) {
	    unsigned char tn_byte;

	    tn_byte = indata[i];

	    if ((td->telnet_cmd_pos == 1) && (tn_byte == TN_IAC)) {
		/* Two IACs in a row causes one IAC to be sent, so
		   just let this one go through. */
		outdata[j++] = tn_byte;
		td->telnet_cmd_pos = 0;
		continue;
	    }

	    if (td->telnet_cmd_pos == 1) {
		/* These are two byte commands, so we have
		   everything we need to handle the command. */
		td->telnet_cmd[td->telnet_cmd_pos++] = tn_byte;
		if (tn_byte < TN_SB) {
		    handle_telnet_cmd(td);
		    td->telnet_cmd_pos = 0;
		}
	    } else if (td->telnet_cmd_pos == 2) {
		td->telnet_cmd[td->telnet_cmd_pos++] = tn_byte;
		if (td->telnet_cmd[1] != TN_SB) {
		    /* It's a will/won't/do/don't */
		    handle_telnet_cmd(td);
		    td->telnet_cmd_pos = 0;
		}
	    } else {
		/* It's in a suboption, look for the end and IACs. */
	      if (td->suboption_iac) {
		    if (tn_byte == TN_SE) {
			/* Remove the IAC 240 from the end. */
			td->telnet_cmd_pos--;
			handle_telnet_cmd(td);
			td->telnet_cmd_pos = 0;
		    } else if (tn_byte == TN_IAC) {
			/* Don't do anything, a double 255 means
			   we leave on 255 in. */
		    } else {
			/* If we have an IAC and an invalid
			   character, delete them both */
			td->telnet_cmd_pos--;
		    }
		    td->suboption_iac = 0;
		} else {
		    if (td->telnet_cmd_pos > MAX_TELNET_CMD_SIZE)
			/* Always store the last character
			   received in the final postition (the
			   array is one bigger than the max size)
			   so we can detect the end of the
			   suboption. */
			td->telnet_cmd_pos = MAX_TELNET_CMD_SIZE;

		    td->telnet_cmd[td->telnet_cmd_pos++] = tn_byte;
		    if (tn_byte == TN_IAC)
			td->suboption_iac = 1;
		}
	    }
	} else if (indata[i] == TN_IAC) {
	    td->telnet_cmd[td->telnet_cmd_pos++] = TN_IAC;
	    td->suboption_iac = 0;
	} else {
	    outdata[j++] = indata[i];
	}
    }

    *inlen -= i;
    *r_indata = indata + i;

    return j;
}

unsigned int
process_telnet_xmit(unsigned char *outdata, unsigned int outlen,
		    const unsigned char **indata, size_t *r_inlen)
{
    unsigned int i, j = 0;
    unsigned int inlen = *r_inlen;
    const unsigned char *ibuf = *indata;

    /* Double the IACs on a telnet transmit stream. */
    for (i = 0; i < inlen; i++) {
	if (ibuf[i] == TN_IAC) {
	    if (outlen < 2)
		    break;
	    outdata[j++] = TN_IAC;
	    outdata[j++] = TN_IAC;
	    outlen -= 2;
	} else {
	    if (outlen < 1)
		break;
	    outdata[j++] = ibuf[i];
	    outlen--;
	}
    }

    *indata = ibuf + i;
    *r_inlen = inlen - i;

    return j;
}

void
telnet_init(telnet_data_t *td,
	    void *cb_data,
	    void (*output_ready)(void *cb_data),
	    void (*cmd_handler)(void *cb_data, unsigned char cmd),
	    struct telnet_cmd *cmds,
	    const unsigned char *init_seq,
	    int init_seq_len)
{
    memset(td, 0, sizeof(*td));
    buffer_init(&td->out_telnet_cmd, td->out_telnet_cmdbuf,
		sizeof(td->out_telnet_cmdbuf));
    td->cb_data = cb_data;
    td->output_ready = output_ready;
    td->cmd_handler = cmd_handler;
    td->cmds = cmds;

    telnet_cmd_send(td, init_seq, init_seq_len);
}

void
telnet_cleanup(telnet_data_t *td)
{
    td->cmds = NULL;
}