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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/*
* piaip's simplified implementation of TELNET protocol
* Copyright (c) 2005-2009 Hung-Te Lin <piaip@csie.ntu.edu.tw>
* Improved by Kuang 2009
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#ifdef DEBUG
#define TELOPTS
#define TELCMDS
#endif
#include <arpa/telnet.h>
#include "cmsys.h"
static unsigned int telnet_handler(TelnetCtx *ctx, unsigned char c) ;
static void telnet_write (TelnetCtx *ctx, const void *buf, size_t nbytes);
enum TELNET_IAC_STATES {
IAC_NONE,
IAC_COMMAND,
IAC_WAIT_OPT,
IAC_WAIT_SE,
IAC_PROCESS_OPT,
IAC_ERROR
};
/* We don't reply to most commands, so this maxlen can be minimal.
* Warning: if you want to support ENV passing or other long commands,
* remember to increase this value. Howver, some poorly implemented
* terminals like xxMan may not follow the protocols and user will hang
* on those terminals when IACs were sent.
*/
TelnetCtx *telnet_create_contex(void)
{
return (TelnetCtx*) malloc(sizeof(TelnetCtx));
}
void telnet_free_context(TelnetCtx* ctx)
{
free(ctx);
}
void telnet_ctx_init(TelnetCtx *ctx, const struct TelnetCallback *callback, int fd)
{
memset(ctx, 0, sizeof(TelnetCtx));
// callback structure must be provided.
assert(callback);
ctx->callback = callback;
ctx->fd = fd;
}
void telnet_ctx_set_cc_arg(TelnetCtx *ctx, void *cc_arg)
{
ctx->cc_arg = cc_arg;
}
void telnet_ctx_set_write_arg(TelnetCtx *ctx, void *write_arg)
{
ctx->write_arg = write_arg;
}
void telnet_ctx_set_resize_arg(TelnetCtx *ctx, void *resize_arg)
{
ctx->resize_arg = resize_arg;
}
/* We are the boss. We don't respect to client.
* It's client's responsibility to follow us.
* Please write these codes in i-dont-care opt handlers.
*/
static const char telnet_init_cmds[] = {
/* retrieve terminal type and throw away.
* why? because without this, clients enter line mode.
*/
IAC, DO, TELOPT_TTYPE,
IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE,
/* i'm a smart term with resize ability. */
IAC, DO, TELOPT_NAWS,
/* i will echo. */
IAC, WILL, TELOPT_ECHO,
/* supress ga. */
IAC, WILL, TELOPT_SGA,
/* 8 bit binary. */
IAC, WILL, TELOPT_BINARY,
IAC, DO, TELOPT_BINARY,
};
void telnet_ctx_send_init_cmds(TelnetCtx *ctx)
{
telnet_write(ctx, telnet_init_cmds, sizeof(telnet_init_cmds));
}
ssize_t telnet_process(TelnetCtx *ctx, unsigned char *buf, ssize_t size)
{
/* process buffer */
if (size > 0) {
unsigned char *buf2 = buf;
size_t i = 0, i2 = 0;
/* prescan. because IAC is rare,
* this cost is worthy. */
if (ctx->iac_state == IAC_NONE && memchr(buf, IAC, size) == NULL)
return size;
/* we have to look into the buffer. */
for (i = 0; i < size; i++, buf++)
if(telnet_handler(ctx, *buf) == 0)
*(buf2++) = *buf;
else
i2 ++;
size = (i2 == size) ? -1L : size - i2;
}
return size;
}
#ifdef DBG_OUTRPT
extern unsigned char fakeEscape;
#endif // DBG_OUTRPT
static void
telnet_write (TelnetCtx *ctx, const void *buf, size_t nbytes)
{
if (ctx->callback->write_data)
ctx->callback->write_data(ctx->write_arg, ctx->fd, buf, nbytes);
else
write(ctx->fd, buf, nbytes);
}
/* input: raw character
* output: telnet command if c was handled, otherwise zero.
*/
static unsigned int
telnet_handler(TelnetCtx *ctx, unsigned char c)
{
/* we have to quote all IACs. */
if(c == IAC && !ctx->iac_quote) {
ctx->iac_quote = 1;
return NOP;
}
/* hash client telnet sequences */
if (ctx->cc_arg && ctx->callback->update_client_code) {
void (*cb)(void *, unsigned char) =
ctx->callback->update_client_code;
if(ctx->iac_state == IAC_WAIT_SE) {
// skip suboption
} else {
if(ctx->iac_quote)
cb(ctx->cc_arg, IAC);
cb(ctx->cc_arg, c);
}
}
/* a special case is the top level iac. otherwise, iac is just a quote. */
if (ctx->iac_quote) {
if(ctx->iac_state == IAC_NONE)
ctx->iac_state = IAC_COMMAND;
if(ctx->iac_state == IAC_WAIT_SE && c == SE)
ctx->iac_state = IAC_PROCESS_OPT;
ctx->iac_quote = 0;
}
/* now, let's process commands by state */
switch(ctx->iac_state) {
case IAC_NONE:
return 0;
case IAC_COMMAND:
#if 0 // def DEBUG
{
int cx = c; /* to make compiler happy */
telnet_write(ctx, "-", 1);
if(TELCMD_OK(cx))
telnet_write(ctx, TELCMD(c), strlen(TELCMD(c)));
telnet_write(ctx, " ", 1);
}
#endif
ctx->iac_state = IAC_NONE; /* by default we restore state. */
switch(c) {
case IAC:
// return 0;
// we don't want to allow IACs as input.
return 1;
/* we don't want to process these. or maybe in future. */
case BREAK: /* break */
#ifdef DBG_OUTRPT
fakeEscape = !fakeEscape;
return NOP;
#endif
case ABORT: /* Abort process */
case SUSP: /* Suspend process */
case AO: /* abort output--but let prog finish */
case IP: /* interrupt process--permanently */
case EOR: /* end of record (transparent mode) */
case DM: /* data mark--for connect. cleaning */
case xEOF: /* End of file: EOF is already used... */
return NOP;
case NOP: /* nop */
return NOP;
/* we should process these, but maybe in future. */
case GA: /* you may reverse the line */
case EL: /* erase the current line */
case EC: /* erase the current character */
return NOP;
/* good */
case AYT: /* are you there */
{
#if 0
const char *alive = " I'm still alive, loading: ";
char buf[STRLEN];
/* respond as fast as we can */
telnet_write(ctx, alive, strlen(alive));
cpuload(buf);
telnet_write(ctx, buf, strlen(buf));
telnet_write(ctx, "\r\n", 2);
#else
const char *alive = " I'm still alive.\r\n";
/* respond as fast as we can */
telnet_write(ctx, alive, strlen(alive));
#endif
}
return NOP;
case DONT: /* you are not to use option */
case DO: /* please, you use option */
case WONT: /* I won't use option */
case WILL: /* I will use option */
ctx->iac_opt_req = c;
ctx->iac_state = IAC_WAIT_OPT;
return NOP;
case SB: /* interpret as subnegotiation */
ctx->iac_state = IAC_WAIT_SE;
ctx->iac_buflen = 0;
return NOP;
case SE: /* end sub negotiation */
default:
return NOP;
}
return 1;
case IAC_WAIT_OPT:
#if 0 // def DEBUG
telnet_write(ctx, "-", 1);
if(TELOPT_OK(c))
telnet_write(ctx, TELOPT(c), strlen(TELOPT(c)));
telnet_write(ctx, " ", 1);
#endif
ctx->iac_state = IAC_NONE;
/*
* According to RFC, there're some tricky steps to prevent loop.
* However because we have a poor term which does not allow
* most abilities, let's be a strong boss here.
*
* Although my old imeplementation worked, it's even better to follow this:
* http://www.tcpipguide.com/free/t_TelnetOptionsandOptionNegotiation-3.htm
*/
switch(c) {
/* i-dont-care: i don't care about what client is.
* these should be claimed in init and
* client must follow me. */
case TELOPT_TTYPE: /* termtype or line. */
case TELOPT_NAWS: /* resize terminal */
case TELOPT_SGA: /* supress GA */
case TELOPT_ECHO: /* echo */
case TELOPT_BINARY: /* we are CJK. */
break;
/* i-dont-agree: i don't understand/agree these.
* according to RFC, saying NO stopped further
* requests so there'll not be endless loop. */
case TELOPT_RCP: /* prepare to reconnect */
default:
if (ctx->iac_opt_req == WILL || ctx->iac_opt_req == DO)
{
/* unknown option, reply with won't */
unsigned char cmd[3] = { IAC, DONT, 0 };
if(ctx->iac_opt_req == DO) cmd[1] = WONT;
cmd[2] = c;
telnet_write(ctx, cmd, sizeof(cmd));
}
break;
}
return 1;
case IAC_WAIT_SE:
ctx->iac_buf[ctx->iac_buflen++] = c;
/* no need to convert state because previous quoting will do. */
if(ctx->iac_buflen == TELNET_IAC_MAXLEN) {
/* may be broken protocol?
* whether finished or not, break for safety
* or user may be frozen.
*/
ctx->iac_state = IAC_NONE;
return 0;
}
return 1;
case IAC_PROCESS_OPT:
ctx->iac_state = IAC_NONE;
#if 0 // def DEBUG
telnet_write(ctx, "-", 1);
if(TELOPT_OK(ctx->iac_buf[0]))
telnet_write(ctx, TELOPT(ctx->iac_buf[0]), strlen(TELOPT(ctx->iac_buf[0])));
telnet_write(ctx, " ", 1);
#endif
switch(ctx->iac_buf[0]) {
/* resize terminal */
case TELOPT_NAWS:
{
int w = (ctx->iac_buf[1] << 8) + (ctx->iac_buf[2]);
int h = (ctx->iac_buf[3] << 8) + (ctx->iac_buf[4]);
if (ctx->callback->term_resize)
ctx->callback->term_resize(ctx->resize_arg, w, h);
if (ctx->cc_arg &&
ctx->callback->update_client_code) {
void (*cb)(void *, unsigned char) =
ctx->callback->update_client_code;
cb(ctx->cc_arg, ctx->iac_buf[0]);
if(w==80 && h==24)
cb(ctx->cc_arg, 1);
else if(w==80)
cb(ctx->cc_arg, 2);
else if(h==24)
cb(ctx->cc_arg, 3);
else
cb(ctx->cc_arg, 4);
cb(ctx->cc_arg, IAC);
cb(ctx->cc_arg, SE);
}
}
break;
default:
if (ctx->cc_arg &&
ctx->callback->update_client_code) {
void (*cb)(void *, unsigned char) =
ctx->callback->update_client_code;
int i;
for(i=0;i<ctx->iac_buflen;i++)
cb(ctx->cc_arg, ctx->iac_buf[i]);
cb(ctx->cc_arg, IAC);
cb(ctx->cc_arg, SE);
}
break;
}
return 1;
}
return 1; /* never reached */
}
// vim: sw=4
|