blob: 2980363ea5e999d40e633daa68bb98507b0fae9b [file] [log] [blame]
Sean Middleditch6aef0732009-03-12 23:27:35 -04001/*
Sean Middleditch9de15982009-03-14 03:35:49 -04002 * Sean Middleditch
3 * sean@sourcemud.org
4 *
Sean Middleditch6aef0732009-03-12 23:27:35 -04005 * The author or authors of this code dedicate any and all copyright interest
6 * in this code to the public domain. We make this dedication for the benefit
7 * of the public at large and to the detriment of our heirs and successors. We
8 * intend this dedication to be an overt act of relinquishment in perpetuity of
9 * all present and future rights to this code under copyright law.
10 */
11
Sean Middleditch4d9444d2009-03-13 22:48:05 -040012#include <malloc.h>
Sean Middleditch9de15982009-03-14 03:35:49 -040013#include <string.h>
Sean Middleditchd922c6f2009-03-14 22:35:01 -040014#include <stdio.h>
15#include <errno.h>
16#include <string.h>
17#include <stdarg.h>
Sean Middleditch9de15982009-03-14 03:35:49 -040018
19#ifdef HAVE_ZLIB
20#include "zlib.h"
21#endif
22
Sean Middleditch29144852009-03-12 23:14:47 -040023#include "libtelnet.h"
24
Sean Middleditch9b07e922009-03-19 18:18:44 -040025/* inlinable functions */
26#if __GNUC__ || __STDC_VERSION__ >= 199901L
Sean Middleditch53a86612009-03-22 17:43:29 -040027# define INLINE __inline__
Sean Middleditch9b07e922009-03-19 18:18:44 -040028#else
29# define INLINE
30#endif
31
Sean Middleditch90e79da2009-03-19 15:17:13 -040032/* RFC1143 option negotiation state */
33typedef struct telnet_rfc1143_t {
34 unsigned char telopt;
35 char us:4, him:4;
36} telnet_rfc1143_t;
37
Sean Middleditch5b5bc922009-03-15 23:02:10 -040038/* RFC1143 state names */
Sean Middleditch7491bf42009-03-20 23:52:18 -040039#define Q_NO 0
40#define Q_YES 1
41#define Q_WANTNO 2
42#define Q_WANTYES 3
43#define Q_WANTNO_OP 4
44#define Q_WANTYES_OP 5
Sean Middleditch5b5bc922009-03-15 23:02:10 -040045
Sean Middleditch4d9444d2009-03-13 22:48:05 -040046/* buffer sizes */
Sean Middleditch340a51b2009-03-19 02:08:46 -040047static const size_t _buffer_sizes[] = {
Sean Middleditch4d9444d2009-03-13 22:48:05 -040048 0,
49 512,
50 2048,
51 8192,
52 16384,
53};
Sean Middleditch340a51b2009-03-19 02:08:46 -040054static const size_t _buffer_sizes_count = sizeof(_buffer_sizes) /
Sean Middleditch812358d2009-03-15 23:24:03 -040055 sizeof(_buffer_sizes[0]);
Sean Middleditch4d9444d2009-03-13 22:48:05 -040056
Sean Middleditch34bb0992009-03-21 00:20:44 -040057/* event dispatch helper */
58static INLINE void _event(telnet_t *telnet, telnet_event_type_t type,
Sean Middleditch97a8cb22009-03-16 16:51:41 -040059 unsigned char command, unsigned char telopt,
Sean Middleditche5327da2009-03-21 00:54:50 -040060 const char *buffer, size_t size, const char **argv, size_t argc) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -040061 telnet_event_t ev;
Sean Middleditche5327da2009-03-21 00:54:50 -040062 ev.argv = argv;
63 ev.argc = argc;
Sean Middleditch5b5bc922009-03-15 23:02:10 -040064 ev.buffer = buffer;
65 ev.size = size;
Sean Middleditch637df7f2009-03-15 12:57:32 -040066 ev.type = type;
67 ev.command = command;
68 ev.telopt = telopt;
Sean Middleditch637df7f2009-03-15 12:57:32 -040069
Sean Middleditch9f79cc52009-03-15 13:39:24 -040070 telnet->eh(telnet, &ev, telnet->ud);
Sean Middleditch637df7f2009-03-15 12:57:32 -040071}
72
Sean Middleditchd922c6f2009-03-14 22:35:01 -040073/* error generation function */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040074static telnet_error_t _error(telnet_t *telnet, unsigned line,
75 const char* func, telnet_error_t err, int fatal, const char *fmt,
Sean Middleditchfbe93e32009-03-15 23:39:31 -040076 ...) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -040077 char buffer[512];
78 va_list va;
79
80 /* format error intro */
Sean Middleditch812358d2009-03-15 23:24:03 -040081 snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", __FILE__, line, func);
Sean Middleditchd922c6f2009-03-14 22:35:01 -040082
Sean Middleditchb43c10c2009-03-20 23:21:02 -040083 /* format informational text */
Sean Middleditchd922c6f2009-03-14 22:35:01 -040084 va_start(va, fmt);
85 vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
86 fmt, va);
87 va_end(va);
88
Sean Middleditchb43c10c2009-03-20 23:21:02 -040089 /* send error event to the user */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040090 _event(telnet, fatal ? TELNET_EV_ERROR : TELNET_EV_WARNING, err,
Sean Middleditche5327da2009-03-21 00:54:50 -040091 0, buffer, strlen(buffer), 0, 0);
Sean Middleditchfbe93e32009-03-15 23:39:31 -040092
93 return err;
Sean Middleditchd922c6f2009-03-14 22:35:01 -040094}
95
Sean Middleditche5b47592009-03-16 17:37:43 -040096#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -040097/* initialize the zlib box for a telnet box; if deflate is non-zero, it
98 * initializes zlib for delating (compression), otherwise for inflating
Sean Middleditchf65f27d2009-03-19 02:32:04 -040099 * (decompression). returns TELNET_EOK on success, something else on
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400100 * failure.
Sean Middleditch72cc9642009-03-15 11:50:36 -0400101 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400102telnet_error_t _init_zlib(telnet_t *telnet, int deflate, int err_fatal) {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400103 z_stream *z;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400104 int rs;
105
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400106 /* if compression is already enabled, fail loudly */
107 if (telnet->z != 0)
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400108 return _error(telnet, __LINE__, __func__, TELNET_EBADVAL,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400109 err_fatal, "cannot initialize compression twice");
110
Sean Middleditch72cc9642009-03-15 11:50:36 -0400111 /* allocate zstream box */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400112 if ((z= (z_stream *)calloc(1, sizeof(z_stream))) == 0)
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400113 return _error(telnet, __LINE__, __func__, TELNET_ENOMEM, err_fatal,
Sean Middleditch16992272009-03-15 19:42:03 -0400114 "malloc() failed: %s", strerror(errno));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400115
116 /* initialize */
117 if (deflate) {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400118 if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) {
119 free(z);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400120 return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400121 err_fatal, "deflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400122 }
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400123 telnet->flags |= TELNET_PFLAG_DEFLATE;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400124 } else {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400125 if ((rs = inflateInit(z)) != Z_OK) {
126 free(z);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400127 return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400128 err_fatal, "inflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400129 }
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400130 telnet->flags &= ~TELNET_PFLAG_DEFLATE;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400131 }
132
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400133 telnet->z = z;
134
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400135 return TELNET_EOK;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400136}
Sean Middleditche5b47592009-03-16 17:37:43 -0400137#endif
Sean Middleditch72cc9642009-03-15 11:50:36 -0400138
Sean Middleditch8b788962009-03-16 01:06:27 -0400139/* push bytes out, compressing them first if need be */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400140static void _send(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400141 size_t size) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400142#ifdef HAVE_ZLIB
143 /* if we have a deflate (compression) zlib box, use it */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400144 if (telnet->z != 0 && telnet->flags & TELNET_PFLAG_DEFLATE) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400145 char deflate_buffer[1024];
Sean Middleditch8b788962009-03-16 01:06:27 -0400146 int rs;
147
148 /* initialize z state */
Sean Middleditch97a8cb22009-03-16 16:51:41 -0400149 telnet->z->next_in = (unsigned char *)buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400150 telnet->z->avail_in = size;
Sean Middleditch8daf7742009-03-19 02:05:24 -0400151 telnet->z->next_out = (unsigned char *)deflate_buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400152 telnet->z->avail_out = sizeof(deflate_buffer);
153
154 /* deflate until buffer exhausted and all output is produced */
155 while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
156 /* compress */
157 if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400158 _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
Sean Middleditch8b788962009-03-16 01:06:27 -0400159 "deflate() failed: %s", zError(rs));
160 deflateEnd(telnet->z);
161 free(telnet->z);
162 telnet->z = 0;
163 break;
164 }
165
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400166 _event(telnet, TELNET_EV_SEND, 0, 0, deflate_buffer,
Sean Middleditche5327da2009-03-21 00:54:50 -0400167 sizeof(deflate_buffer) - telnet->z->avail_out, 0, 0);
Sean Middleditch8b788962009-03-16 01:06:27 -0400168
169 /* prepare output buffer for next run */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400170 telnet->z->next_out = (unsigned char *)deflate_buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400171 telnet->z->avail_out = sizeof(deflate_buffer);
172 }
173
174 /* COMPRESS2 is not negotiated, just send */
175 } else
176#endif /* HAVE_ZLIB */
Sean Middleditche5327da2009-03-21 00:54:50 -0400177 _event(telnet, TELNET_EV_SEND, 0, 0, buffer, size, 0, 0);
Sean Middleditch8b788962009-03-16 01:06:27 -0400178}
179
Sean Middleditch34bb0992009-03-21 00:20:44 -0400180/* check if we support a particular telopt; if us is non-zero, we
181 * check if we (local) supports it, otherwise we check if he (remote)
182 * supports it. return non-zero if supported, zero if not supported.
183 */
184static INLINE int _check_telopt(telnet_t *telnet, unsigned char telopt,
185 int us) {
186 int i;
187
188 /* if we have no telopts table, we obviously don't support it */
189 if (telnet->telopts == 0)
190 return 0;
191
192 /* loop unti found or end marker (us and him both 0) */
Sean Middleditchbfc641e2009-03-22 16:26:06 -0400193 for (i = 0; telnet->telopts[i].telopt != -1; ++i) {
194 if (telnet->telopts[i].telopt == telopt) {
195 if (us && telnet->telopts[i].us == TELNET_WILL)
196 return 1;
197 else if (!us && telnet->telopts[i].him == TELNET_DO)
198 return 1;
199 else
200 return 0;
201 }
202 }
Sean Middleditch34bb0992009-03-21 00:20:44 -0400203
204 /* not found, so not supported */
205 return 0;
206}
207
Sean Middleditch8b788962009-03-16 01:06:27 -0400208/* retrieve RFC1143 option state */
Sean Middleditch9b07e922009-03-19 18:18:44 -0400209static INLINE telnet_rfc1143_t _get_rfc1143(telnet_t *telnet,
210 unsigned char telopt) {
Sean Middleditch4987f9f2009-03-19 12:51:40 -0400211 const telnet_rfc1143_t empty = { telopt, 0, 0};
Sean Middleditch8b788962009-03-16 01:06:27 -0400212 int i;
213
214 /* search for entry */
215 for (i = 0; i != telnet->q_size; ++i)
216 if (telnet->q[i].telopt == telopt)
217 return telnet->q[i];
218
219 /* not found, return empty value */
220 return empty;
221}
222
223/* save RFC1143 option state */
Sean Middleditch7491bf42009-03-20 23:52:18 -0400224static INLINE void _set_rfc1143(telnet_t *telnet, unsigned char telopt,
225 char us, char him) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400226 telnet_rfc1143_t *qtmp;
Sean Middleditch8b788962009-03-16 01:06:27 -0400227 int i;
228
229 /* search for entry */
230 for (i = 0; i != telnet->q_size; ++i) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400231 if (telnet->q[i].telopt == telopt) {
232 telnet->q[i].us = us;
233 telnet->q[i].him = him;
Sean Middleditch8b788962009-03-16 01:06:27 -0400234 return;
235 }
236 }
237
238 /* we're going to need to track state for it, so grow the queue
Sean Middleditch1e80f522009-03-22 18:24:18 -0400239 * by 4 (four) elements and put the telopt into it; bail on allocation
240 * error. we go by four because it seems like a reasonable guess as
241 * to the number of enabled options for most simple code, and it
242 * allows for an acceptable number of reallocations for complex code.
Sean Middleditch8b788962009-03-16 01:06:27 -0400243 */
Sean Middleditch5dea1f72009-03-19 12:45:34 -0400244 if ((qtmp = (telnet_rfc1143_t *)realloc(telnet->q, sizeof(
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400245 telnet_rfc1143_t) * (telnet->q_size + 1))) == 0) {
246 _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
Sean Middleditch8b788962009-03-16 01:06:27 -0400247 "malloc() failed: %s", strerror(errno));
248 return;
249 }
Sean Middleditch1e80f522009-03-22 18:24:18 -0400250 memset(&qtmp[telnet->q_size], 0, sizeof(telnet_rfc1143_t *) * 4);
Sean Middleditch8b788962009-03-16 01:06:27 -0400251 telnet->q = qtmp;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400252 telnet->q[telnet->q_size].telopt = telopt;
253 telnet->q[telnet->q_size].us = us;
254 telnet->q[telnet->q_size].him = him;
255 ++telnet->q_size;
Sean Middleditch8b788962009-03-16 01:06:27 -0400256}
257
Sean Middleditch90e79da2009-03-19 15:17:13 -0400258/* send negotiation bytes */
Sean Middleditch9b07e922009-03-19 18:18:44 -0400259static INLINE void _send_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditch90e79da2009-03-19 15:17:13 -0400260 unsigned char telopt) {
261 char bytes[3] = { TELNET_IAC, cmd, telopt };
262 _send(telnet, bytes, 3);
263}
264
Sean Middleditch8b788962009-03-16 01:06:27 -0400265/* negotiation handling magic for RFC1143 */
Sean Middleditch330c2612009-03-20 23:29:17 -0400266static void _negotiate(telnet_t *telnet, unsigned char telopt) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400267 telnet_rfc1143_t q;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400268
269 /* in PROXY mode, just pass it thru and do nothing */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400270 if (telnet->flags & TELNET_FLAG_PROXY) {
Sean Middleditch330c2612009-03-20 23:29:17 -0400271 switch ((int)telnet->state) {
272 case TELNET_STATE_WILL:
Sean Middleditche5327da2009-03-21 00:54:50 -0400273 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400274 break;
Sean Middleditch330c2612009-03-20 23:29:17 -0400275 case TELNET_STATE_WONT:
Sean Middleditche5327da2009-03-21 00:54:50 -0400276 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400277 break;
Sean Middleditch330c2612009-03-20 23:29:17 -0400278 case TELNET_STATE_DO:
Sean Middleditche5327da2009-03-21 00:54:50 -0400279 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400280 break;
Sean Middleditch330c2612009-03-20 23:29:17 -0400281 case TELNET_STATE_DONT:
Sean Middleditche5327da2009-03-21 00:54:50 -0400282 _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400283 break;
284 }
285 return;
286 }
287
288 /* lookup the current state of the option */
Sean Middleditch8b788962009-03-16 01:06:27 -0400289 q = _get_rfc1143(telnet, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400290
291 /* start processing... */
Sean Middleditch330c2612009-03-20 23:29:17 -0400292 switch ((int)telnet->state) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400293 /* request to enable option on remote end or confirm DO */
Sean Middleditch330c2612009-03-20 23:29:17 -0400294 case TELNET_STATE_WILL:
Sean Middleditch8b788962009-03-16 01:06:27 -0400295 switch (q.him) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400296 case Q_NO:
Sean Middleditch34bb0992009-03-21 00:20:44 -0400297 if (_check_telopt(telnet, telopt, 0)) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400298 _set_rfc1143(telnet, telopt, q.us, Q_YES);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400299 _send_negotiate(telnet, TELNET_DO, telopt);
Sean Middleditch94738a42009-03-22 16:33:22 -0400300 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400301 } else
Sean Middleditch90e79da2009-03-19 15:17:13 -0400302 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400303 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400304 case Q_WANTNO:
305 _set_rfc1143(telnet, telopt, q.us, Q_NO);
Sean Middleditche5327da2009-03-21 00:54:50 -0400306 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400307 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400308 "DONT answered by WILL");
309 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400310 case Q_WANTNO_OP:
311 _set_rfc1143(telnet, telopt, q.us, Q_YES);
Sean Middleditche5327da2009-03-21 00:54:50 -0400312 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400313 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400314 "DONT answered by WILL");
315 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400316 case Q_WANTYES:
317 _set_rfc1143(telnet, telopt, q.us, Q_YES);
Sean Middleditche5327da2009-03-21 00:54:50 -0400318 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400319 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400320 case Q_WANTYES_OP:
321 _set_rfc1143(telnet, telopt, q.us, Q_WANTNO);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400322 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditche5327da2009-03-21 00:54:50 -0400323 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400324 break;
325 }
326 break;
327
328 /* request to disable option on remote end, confirm DONT, reject DO */
Sean Middleditch330c2612009-03-20 23:29:17 -0400329 case TELNET_STATE_WONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400330 switch (q.him) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400331 case Q_YES:
332 _set_rfc1143(telnet, telopt, q.us, Q_NO);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400333 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditche5327da2009-03-21 00:54:50 -0400334 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400335 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400336 case Q_WANTNO:
337 _set_rfc1143(telnet, telopt, q.us, Q_NO);
Sean Middleditche5327da2009-03-21 00:54:50 -0400338 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400339 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400340 case Q_WANTNO_OP:
341 _set_rfc1143(telnet, telopt, q.us, Q_WANTYES);
Sean Middleditche5327da2009-03-21 00:54:50 -0400342 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400343 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400344 case Q_WANTYES:
345 case Q_WANTYES_OP:
346 _set_rfc1143(telnet, telopt, q.us, Q_NO);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400347 break;
348 }
349 break;
350
351 /* request to enable option on local end or confirm WILL */
Sean Middleditch330c2612009-03-20 23:29:17 -0400352 case TELNET_STATE_DO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400353 switch (q.us) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400354 case Q_NO:
Sean Middleditch34bb0992009-03-21 00:20:44 -0400355 if (_check_telopt(telnet, telopt, 1)) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400356 _set_rfc1143(telnet, telopt, Q_YES, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400357 _send_negotiate(telnet, TELNET_WILL, telopt);
Sean Middleditch94738a42009-03-22 16:33:22 -0400358 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400359 } else
Sean Middleditch90e79da2009-03-19 15:17:13 -0400360 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400361 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400362 case Q_WANTNO:
363 _set_rfc1143(telnet, telopt, Q_NO, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400364 _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400365 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400366 "WONT answered by DO");
367 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400368 case Q_WANTNO_OP:
369 _set_rfc1143(telnet, telopt, Q_YES, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400370 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400371 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400372 "WONT answered by DO");
373 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400374 case Q_WANTYES:
375 _set_rfc1143(telnet, telopt, Q_YES, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400376 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400377 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400378 case Q_WANTYES_OP:
379 _set_rfc1143(telnet, telopt, Q_WANTNO, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400380 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditche5327da2009-03-21 00:54:50 -0400381 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400382 break;
383 }
384 break;
385
386 /* request to disable option on local end, confirm WONT, reject WILL */
Sean Middleditch330c2612009-03-20 23:29:17 -0400387 case TELNET_STATE_DONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400388 switch (q.us) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400389 case Q_YES:
390 _set_rfc1143(telnet, telopt, Q_NO, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400391 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditche5327da2009-03-21 00:54:50 -0400392 _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400393 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400394 case Q_WANTNO:
395 _set_rfc1143(telnet, telopt, Q_NO, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400396 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400397 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400398 case Q_WANTNO_OP:
399 _set_rfc1143(telnet, telopt, Q_WANTYES, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400400 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400401 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400402 case Q_WANTYES:
403 case Q_WANTYES_OP:
404 _set_rfc1143(telnet, telopt, Q_NO, q.him);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400405 break;
406 }
407 break;
408 }
409}
410
Sean Middleditche5327da2009-03-21 00:54:50 -0400411/* process a subnegotiation buffer; return non-zero if the current buffer
412 * must be aborted and reprocessed due to COMPRESS2 being activated
413 */
414static int _subnegotiate(telnet_t *telnet) {
Sean Middleditchb10946c2009-03-22 18:21:14 -0400415 const char **argv;
416 const char *c;
417 size_t i, argc;
Sean Middleditche5327da2009-03-21 00:54:50 -0400418
Sean Middleditchb10946c2009-03-22 18:21:14 -0400419 switch (telnet->sb_telopt) {
420#ifdef HAVE_ZLIB
Sean Middleditche5327da2009-03-21 00:54:50 -0400421 /* received COMPRESS2 begin marker, setup our zlib box and
422 * start handling the compressed stream if it's not already.
423 */
Sean Middleditchb10946c2009-03-22 18:21:14 -0400424 case TELNET_TELOPT_COMPRESS2:
425 if (telnet->sb_telopt == TELNET_TELOPT_COMPRESS2) {
426 if (_init_zlib(telnet, 0, 1) != TELNET_EOK)
427 return 0;
Sean Middleditche5327da2009-03-21 00:54:50 -0400428
Sean Middleditchb10946c2009-03-22 18:21:14 -0400429 /* standard SB notification */
430 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
431 telnet->buffer, telnet->buffer_pos, 0, 0);
432
433 /* notify app that compression was enabled */
434 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0, 0, 0);
435 return 1;
436 }
437 break;
Sean Middleditche5327da2009-03-21 00:54:50 -0400438#endif /* HAVE_ZLIB */
Sean Middleditchb10946c2009-03-22 18:21:14 -0400439 /* ZMP command */
440 case TELNET_TELOPT_ZMP:
Sean Middleditche5327da2009-03-21 00:54:50 -0400441 /* make sure this is a valid ZMP buffer */
Sean Middleditchb10946c2009-03-22 18:21:14 -0400442 if (telnet->buffer_pos == 0 ||
443 telnet->buffer[telnet->buffer_pos - 1] != 0) {
444 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
445 "incomplete ZMP frame");
446 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
447 telnet->buffer, telnet->buffer_pos, 0, 0);
Sean Middleditche5327da2009-03-21 00:54:50 -0400448 return 0;
Sean Middleditchb10946c2009-03-22 18:21:14 -0400449 }
Sean Middleditche5327da2009-03-21 00:54:50 -0400450
451 /* count arguments */
Sean Middleditchb10946c2009-03-22 18:21:14 -0400452 for (argc = 0, c = telnet->buffer; c != telnet->buffer +
453 telnet->buffer_pos; ++argc)
Sean Middleditche5327da2009-03-21 00:54:50 -0400454 c += strlen(c) + 1;
Sean Middleditche5327da2009-03-21 00:54:50 -0400455
456 /* allocate argument array, bail on error */
457 if ((argv = (const char **)malloc(sizeof(char *) * argc)) == 0) {
458 _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
459 "malloc() failed: %s", strerror(errno));
Sean Middleditchb10946c2009-03-22 18:21:14 -0400460 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
461 telnet->buffer, telnet->buffer_pos, 0, 0);
Sean Middleditche5327da2009-03-21 00:54:50 -0400462 }
463
464 /* populate argument array */
465 for (i = 0, c = telnet->buffer; i != argc; ++i) {
466 argv[i] = c;
467 c += strlen(c) + 1;
468 }
469
Sean Middleditchb10946c2009-03-22 18:21:14 -0400470 /* invoke event with our arguments */
471 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
472 telnet->buffer, telnet->buffer_pos, argv, argc);
Sean Middleditche5327da2009-03-21 00:54:50 -0400473
474 /* free argument array */
475 free(argv);
Sean Middleditchb10946c2009-03-22 18:21:14 -0400476 break;
477 /* other generic subnegotiation */
478 default:
479 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
480 telnet->buffer, telnet->buffer_pos, 0, 0);
481 break;
Sean Middleditche5327da2009-03-21 00:54:50 -0400482 }
483
484 return 0;
485}
486
Sean Middleditch29144852009-03-12 23:14:47 -0400487/* initialize a telnet state tracker */
Sean Middleditch34bb0992009-03-21 00:20:44 -0400488void telnet_init(telnet_t *telnet, const telnet_telopt_t *telopts,
489 telnet_event_handler_t eh, unsigned char flags, void *user_data) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400490 memset(telnet, 0, sizeof(telnet_t));
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400491 telnet->ud = user_data;
Sean Middleditch34bb0992009-03-21 00:20:44 -0400492 telnet->telopts = telopts;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400493 telnet->eh = eh;
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400494 telnet->flags = flags;
Sean Middleditch29144852009-03-12 23:14:47 -0400495}
496
497/* free up any memory allocated by a state tracker */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400498void telnet_free(telnet_t *telnet) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400499 /* free sub-request buffer */
Sean Middleditch51ad6792009-03-13 20:15:59 -0400500 if (telnet->buffer != 0) {
Sean Middleditch29144852009-03-12 23:14:47 -0400501 free(telnet->buffer);
502 telnet->buffer = 0;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400503 telnet->buffer_size = 0;
504 telnet->buffer_pos = 0;
Sean Middleditch29144852009-03-12 23:14:47 -0400505 }
Sean Middleditch9de15982009-03-14 03:35:49 -0400506
Sean Middleditche5b47592009-03-16 17:37:43 -0400507#ifdef HAVE_ZLIB
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400508 /* free zlib box */
509 if (telnet->z != 0) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400510 if (telnet->flags & TELNET_PFLAG_DEFLATE)
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400511 deflateEnd(telnet->z);
512 else
513 inflateEnd(telnet->z);
514 free(telnet->z);
515 telnet->z = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400516 }
Sean Middleditche5b47592009-03-16 17:37:43 -0400517#endif
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400518
519 /* free RFC1143 queue */
520 if (telnet->q) {
521 free(telnet->q);
522 telnet->q = 0;
523 telnet->q_size = 0;
524 }
Sean Middleditch29144852009-03-12 23:14:47 -0400525}
526
Sean Middleditch51ad6792009-03-13 20:15:59 -0400527/* push a byte into the telnet buffer */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400528static telnet_error_t _buffer_byte(telnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400529 unsigned char byte) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400530 char *new_buffer;
Sean Middleditch340a51b2009-03-19 02:08:46 -0400531 size_t i;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400532
Sean Middleditch51ad6792009-03-13 20:15:59 -0400533 /* check if we're out of room */
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400534 if (telnet->buffer_pos == telnet->buffer_size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400535 /* find the next buffer size */
536 for (i = 0; i != _buffer_sizes_count; ++i) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400537 if (_buffer_sizes[i] == telnet->buffer_size)
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400538 break;
539 }
540
541 /* overflow -- can't grow any more */
542 if (i >= _buffer_sizes_count - 1) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400543 _error(telnet, __LINE__, __func__, TELNET_EOVERFLOW, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400544 "subnegotiation buffer size limit reached");
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400545 telnet_free(telnet);
546 return TELNET_EOVERFLOW;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400547 }
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400548
549 /* (re)allocate buffer */
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400550 new_buffer = (char *)realloc(telnet->buffer, _buffer_sizes[i + 1]);
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400551 if (new_buffer == 0) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400552 _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
Sean Middleditch16992272009-03-15 19:42:03 -0400553 "realloc() failed");
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400554 telnet_free(telnet);
555 return TELNET_ENOMEM;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400556 }
557
558 telnet->buffer = new_buffer;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400559 telnet->buffer_size = _buffer_sizes[i + 1];
Sean Middleditch51ad6792009-03-13 20:15:59 -0400560 }
561
562 /* push the byte, all set */
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400563 telnet->buffer[telnet->buffer_pos++] = byte;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400564 return TELNET_EOK;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400565}
566
Sean Middleditch2d5f4bf2009-03-20 23:32:47 -0400567static void _process(telnet_t *telnet, const char *buffer, size_t size) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400568 unsigned char byte;
Sean Middleditch340a51b2009-03-19 02:08:46 -0400569 size_t i, start;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400570 for (i = start = 0; i != size; ++i) {
571 byte = buffer[i];
572 switch (telnet->state) {
573 /* regular data */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400574 case TELNET_STATE_DATA:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400575 /* on an IAC byte, pass through all pending bytes and
576 * switch states */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400577 if (byte == TELNET_IAC) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400578 if (i != start)
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400579 _event(telnet, TELNET_EV_DATA, 0, 0, &buffer[start],
Sean Middleditche5327da2009-03-21 00:54:50 -0400580 i - start, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400581 telnet->state = TELNET_STATE_IAC;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400582 }
583 break;
584
585 /* IAC command */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400586 case TELNET_STATE_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400587 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400588 /* subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400589 case TELNET_SB:
590 telnet->state = TELNET_STATE_SB;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400591 break;
592 /* negotiation commands */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400593 case TELNET_WILL:
594 telnet->state = TELNET_STATE_WILL;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400595 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400596 case TELNET_WONT:
597 telnet->state = TELNET_STATE_WONT;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400598 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400599 case TELNET_DO:
600 telnet->state = TELNET_STATE_DO;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400601 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400602 case TELNET_DONT:
603 telnet->state = TELNET_STATE_DONT;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400604 break;
605 /* IAC escaping */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400606 case TELNET_IAC:
Sean Middleditche5327da2009-03-21 00:54:50 -0400607 _event(telnet, TELNET_EV_DATA, 0, 0, (char*)&byte, 1, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400608 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400609 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400610 break;
611 /* some other command */
612 default:
Sean Middleditche5327da2009-03-21 00:54:50 -0400613 _event(telnet, TELNET_EV_IAC, byte, 0, 0, 0, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400614 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400615 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400616 }
617 break;
618
619 /* negotiation commands */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400620 case TELNET_STATE_WILL:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400621 case TELNET_STATE_WONT:
Sean Middleditch447d3ad2009-03-20 23:22:37 -0400622 case TELNET_STATE_DO:
Sean Middleditch447d3ad2009-03-20 23:22:37 -0400623 case TELNET_STATE_DONT:
Sean Middleditch330c2612009-03-20 23:29:17 -0400624 _negotiate(telnet, byte);
Sean Middleditch447d3ad2009-03-20 23:22:37 -0400625 start = i + 1;
626 telnet->state = TELNET_STATE_DATA;
627 break;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400628
Sean Middleditchda0e6952009-03-15 13:28:09 -0400629 /* subnegotiation -- determine subnegotiation telopt */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400630 case TELNET_STATE_SB:
Sean Middleditchda0e6952009-03-15 13:28:09 -0400631 telnet->sb_telopt = byte;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400632 telnet->buffer_pos = 0;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400633 telnet->state = TELNET_STATE_SB_DATA;
Sean Middleditchda0e6952009-03-15 13:28:09 -0400634 break;
635
636 /* subnegotiation -- buffer bytes until end request */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400637 case TELNET_STATE_SB_DATA:
Sean Middleditch6b372882009-03-14 13:06:47 -0400638 /* IAC command in subnegotiation -- either IAC SE or IAC IAC */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400639 if (byte == TELNET_IAC) {
640 telnet->state = TELNET_STATE_SB_DATA_IAC;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400641 /* buffer the byte, or bail if we can't */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400642 } else if (_buffer_byte(telnet, byte) != TELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400643 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400644 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400645 }
646 break;
647
Sean Middleditch6b372882009-03-14 13:06:47 -0400648 /* IAC escaping inside a subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400649 case TELNET_STATE_SB_DATA_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400650 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400651 /* end subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400652 case TELNET_SE:
Sean Middleditch8c567352009-03-21 01:07:18 -0400653 /* return to default state */
654 start = i + 1;
655 telnet->state = TELNET_STATE_DATA;
656
Sean Middleditche5327da2009-03-21 00:54:50 -0400657 /* process subnegotiation */
658 if (_subnegotiate(telnet) != 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400659 /* any remaining bytes in the buffer are compressed.
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400660 * we have to re-invoke telnet_recv to get those
Sean Middleditch9de15982009-03-14 03:35:49 -0400661 * bytes inflated and abort trying to process the
662 * remaining compressed bytes in the current _process
663 * buffer argument
664 */
Sean Middleditch8c567352009-03-21 01:07:18 -0400665 telnet_recv(telnet, &buffer[start], size - start);
Sean Middleditch9de15982009-03-14 03:35:49 -0400666 return;
667 }
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400668 break;
669 /* escaped IAC byte */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400670 case TELNET_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400671 /* push IAC into buffer */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400672 if (_buffer_byte(telnet, TELNET_IAC) !=
673 TELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400674 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400675 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400676 } else {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400677 telnet->state = TELNET_STATE_SB_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400678 }
679 break;
Sean Middleditch2d5f4bf2009-03-20 23:32:47 -0400680 /* something else -- protocol error. attempt to process
681 * content in subnegotiation buffer, then evaluate the
682 * given command as an IAC code.
683 */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400684 default:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400685 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400686 "unexpected byte after IAC inside SB: %d",
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400687 byte);
Sean Middleditch2d5f4bf2009-03-20 23:32:47 -0400688
Sean Middleditchf6daa852009-03-21 01:16:20 -0400689 /* enter IAC state */
Sean Middleditch3f96c5c2009-03-21 01:11:46 -0400690 start = i + 1;
Sean Middleditchf6daa852009-03-21 01:16:20 -0400691 telnet->state = TELNET_STATE_IAC;
Sean Middleditche5327da2009-03-21 00:54:50 -0400692
693 /* process subnegotiation; see comment in
694 * TELNET_STATE_SB_DATA_IAC about invoking telnet_recv()
695 */
696 if (_subnegotiate(telnet) != 0) {
Sean Middleditch3f96c5c2009-03-21 01:11:46 -0400697 telnet_recv(telnet, &buffer[start], size - start);
Sean Middleditche5327da2009-03-21 00:54:50 -0400698 return;
Sean Middleditch3f96c5c2009-03-21 01:11:46 -0400699 } else {
700 /* recursive call to get the current input byte processed
701 * as a regular IAC command. we could use a goto, but
702 * that would be gross.
703 */
Sean Middleditch3f96c5c2009-03-21 01:11:46 -0400704 _process(telnet, (char *)&byte, 1);
Sean Middleditche5327da2009-03-21 00:54:50 -0400705 }
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400706 break;
707 }
708 break;
709 }
710 }
711
712 /* pass through any remaining bytes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400713 if (telnet->state == TELNET_STATE_DATA && i != start)
Sean Middleditche5327da2009-03-21 00:54:50 -0400714 _event(telnet, TELNET_EV_DATA, 0, 0, buffer + start, i - start, 0, 0);
Sean Middleditch29144852009-03-12 23:14:47 -0400715}
716
Sean Middleditch9de15982009-03-14 03:35:49 -0400717/* push a bytes into the state tracker */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400718void telnet_recv(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400719 size_t size) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400720#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400721 /* if we have an inflate (decompression) zlib stream, use it */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400722 if (telnet->z != 0 && !(telnet->flags & TELNET_PFLAG_DEFLATE)) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400723 char inflate_buffer[4096];
Sean Middleditch9de15982009-03-14 03:35:49 -0400724 int rs;
725
726 /* initialize zlib state */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400727 telnet->z->next_in = (unsigned char*)buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400728 telnet->z->avail_in = size;
Sean Middleditch8daf7742009-03-19 02:05:24 -0400729 telnet->z->next_out = (unsigned char *)inflate_buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400730 telnet->z->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400731
732 /* inflate until buffer exhausted and all output is produced */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400733 while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400734 /* reset output buffer */
735
736 /* decompress */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400737 rs = inflate(telnet->z, Z_SYNC_FLUSH);
Sean Middleditch9de15982009-03-14 03:35:49 -0400738
739 /* process the decompressed bytes on success */
740 if (rs == Z_OK || rs == Z_STREAM_END)
741 _process(telnet, inflate_buffer, sizeof(inflate_buffer) -
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400742 telnet->z->avail_out);
Sean Middleditch9de15982009-03-14 03:35:49 -0400743 else
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400744 _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
Sean Middleditch16992272009-03-15 19:42:03 -0400745 "inflate() failed: %s", zError(rs));
Sean Middleditch9de15982009-03-14 03:35:49 -0400746
747 /* prepare output buffer for next run */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400748 telnet->z->next_out = (unsigned char *)inflate_buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400749 telnet->z->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400750
751 /* on error (or on end of stream) disable further inflation */
752 if (rs != Z_OK) {
Sean Middleditche5327da2009-03-21 00:54:50 -0400753 _event(telnet, TELNET_EV_COMPRESS, 0, 0, 0, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400754
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400755 inflateEnd(telnet->z);
756 free(telnet->z);
757 telnet->z = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400758 break;
759 }
760 }
761
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400762 /* COMPRESS2 is not negotiated, just process */
763 } else
764#endif /* HAVE_ZLIB */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400765 _process(telnet, buffer, size);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400766}
767
Sean Middleditch29144852009-03-12 23:14:47 -0400768/* send an iac command */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400769void telnet_iac(telnet_t *telnet, unsigned char cmd) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400770 char bytes[2] = { TELNET_IAC, cmd };
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400771 _send(telnet, bytes, 2);
Sean Middleditch29144852009-03-12 23:14:47 -0400772}
773
774/* send negotiation */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400775void telnet_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditch8b788962009-03-16 01:06:27 -0400776 unsigned char telopt) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400777 telnet_rfc1143_t q;
Sean Middleditch8b788962009-03-16 01:06:27 -0400778
779 /* if we're in proxy mode, just send it now */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400780 if (telnet->flags & TELNET_FLAG_PROXY) {
781 char bytes[3] = { TELNET_IAC, cmd, telopt };
Sean Middleditch8b788962009-03-16 01:06:27 -0400782 _send(telnet, bytes, 3);
783 return;
784 }
785
786 /* get current option states */
787 q = _get_rfc1143(telnet, telopt);
788
789 switch (cmd) {
790 /* advertise willingess to support an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400791 case TELNET_WILL:
Sean Middleditch8b788962009-03-16 01:06:27 -0400792 switch (q.us) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400793 case Q_NO:
794 _set_rfc1143(telnet, telopt, Q_WANTYES, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400795 _send_negotiate(telnet, TELNET_WILL, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400796 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400797 case Q_WANTNO:
798 _set_rfc1143(telnet, telopt, Q_WANTNO_OP, q.him);
Sean Middleditch8b788962009-03-16 01:06:27 -0400799 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400800 case Q_WANTYES_OP:
801 _set_rfc1143(telnet, telopt, Q_WANTYES, q.him);
Sean Middleditch8b788962009-03-16 01:06:27 -0400802 break;
803 }
804 break;
805
806 /* force turn-off of locally enabled option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400807 case TELNET_WONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400808 switch (q.us) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400809 case Q_YES:
810 _set_rfc1143(telnet, telopt, Q_WANTNO, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400811 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400812 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400813 case Q_WANTYES:
814 _set_rfc1143(telnet, telopt, Q_WANTYES_OP, q.him);
Sean Middleditch8b788962009-03-16 01:06:27 -0400815 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400816 case Q_WANTNO_OP:
817 _set_rfc1143(telnet, telopt, Q_WANTNO, q.him);
Sean Middleditch8b788962009-03-16 01:06:27 -0400818 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400819 }
820 break;
821
822 /* ask remote end to enable an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400823 case TELNET_DO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400824 switch (q.him) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400825 case Q_NO:
826 _set_rfc1143(telnet, telopt, q.us, Q_WANTYES);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400827 _send_negotiate(telnet, TELNET_DO, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400828 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400829 case Q_WANTNO:
830 _set_rfc1143(telnet, telopt, q.us, Q_WANTNO_OP);
Sean Middleditch8b788962009-03-16 01:06:27 -0400831 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400832 case Q_WANTYES_OP:
833 _set_rfc1143(telnet, telopt, q.us, Q_WANTYES);
Sean Middleditch8b788962009-03-16 01:06:27 -0400834 break;
835 }
836 break;
837
838 /* demand remote end disable an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400839 case TELNET_DONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400840 switch (q.him) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400841 case Q_YES:
842 _set_rfc1143(telnet, telopt, q.us, Q_WANTNO);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400843 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400844 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400845 case Q_WANTYES:
846 _set_rfc1143(telnet, telopt, q.us, Q_WANTYES_OP);
Sean Middleditch8b788962009-03-16 01:06:27 -0400847 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400848 case Q_WANTNO_OP:
849 _set_rfc1143(telnet, telopt, q.us, Q_WANTNO);
Sean Middleditch8b788962009-03-16 01:06:27 -0400850 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400851 }
852 break;
853 }
Sean Middleditch29144852009-03-12 23:14:47 -0400854}
855
856/* send non-command data (escapes IAC bytes) */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400857void telnet_send(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400858 size_t size) {
859 size_t i, l;
Sean Middleditchc337ba62009-03-16 16:47:27 -0400860
Sean Middleditch29144852009-03-12 23:14:47 -0400861 for (l = i = 0; i != size; ++i) {
862 /* dump prior portion of text, send escaped bytes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400863 if (buffer[i] == TELNET_IAC) {
Sean Middleditch29144852009-03-12 23:14:47 -0400864 /* dump prior text if any */
865 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400866 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400867 l = i + 1;
868
869 /* send escape */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400870 telnet_iac(telnet, TELNET_IAC);
Sean Middleditch29144852009-03-12 23:14:47 -0400871 }
872 }
873
874 /* send whatever portion of buffer is left */
875 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400876 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400877}
878
Sean Middleditch78943842009-03-19 15:22:06 -0400879/* send subnegotiation header */
Sean Middleditch1443ca42009-03-21 00:21:04 -0400880void telnet_begin_sb(telnet_t *telnet, unsigned char telopt) {
Sean Middleditch78943842009-03-19 15:22:06 -0400881 const char sb[3] = { TELNET_IAC, TELNET_SB, telopt };
882 _send(telnet, sb, 3);
883}
884
885
886/* send complete subnegotiation */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400887void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400888 const char *buffer, size_t size) {
Sean Middleditch90e79da2009-03-19 15:17:13 -0400889 const char sb[3] = { TELNET_IAC, TELNET_SB, telopt };
890 static const char se[2] = { TELNET_IAC, TELNET_SE };
891
892 _send(telnet, sb, 3);
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400893 telnet_send(telnet, buffer, size);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400894 _send(telnet, se, 2);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400895
896#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400897 /* if we're a proxy and we just sent the COMPRESS2 marker, we must
898 * make sure all further data is compressed if not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400899 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400900 if (telnet->flags & TELNET_FLAG_PROXY &&
901 telopt == TELNET_TELOPT_COMPRESS2) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400902
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400903 if (_init_zlib(telnet, 1, 1) != TELNET_EOK)
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400904 return;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400905
906 /* notify app that compression was enabled */
Sean Middleditche5327da2009-03-21 00:54:50 -0400907 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400908 }
909#endif /* HAVE_ZLIB */
Sean Middleditch29144852009-03-12 23:14:47 -0400910}
Sean Middleditch124a1c22009-03-15 13:20:03 -0400911
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400912void telnet_begin_compress2(telnet_t *telnet) {
Sean Middleditch124a1c22009-03-15 13:20:03 -0400913#ifdef HAVE_ZLIB
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400914 static const char compress2[] = { TELNET_IAC, TELNET_SB,
915 TELNET_TELOPT_COMPRESS2, TELNET_IAC, TELNET_SE };
Sean Middleditch124a1c22009-03-15 13:20:03 -0400916
Sean Middleditch124a1c22009-03-15 13:20:03 -0400917 /* attempt to create output stream first, bail if we can't */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400918 if (_init_zlib(telnet, 1, 0) != TELNET_EOK)
Sean Middleditch124a1c22009-03-15 13:20:03 -0400919 return;
920
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400921 /* send compression marker. we send directly to the event handler
922 * instead of passing through _send because _send would result in
923 * the compress marker itself being compressed.
924 */
Sean Middleditche5327da2009-03-21 00:54:50 -0400925 _event(telnet, TELNET_EV_SEND, 0, 0, compress2, sizeof(compress2), 0, 0);
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400926
927 /* notify app that compression was successfully enabled */
Sean Middleditche5327da2009-03-21 00:54:50 -0400928 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0, 0, 0);
Sean Middleditch124a1c22009-03-15 13:20:03 -0400929#endif /* HAVE_ZLIB */
930}
Sean Middleditchd58f49f2009-03-16 12:49:35 -0400931
Sean Middleditch4a156042009-03-16 17:10:58 -0400932/* send formatted data with \r and \n translation in addition to IAC IAC */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400933int telnet_printf(telnet_t *telnet, const char *fmt, ...) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400934 static const char CRLF[] = { '\r', '\n' };
935 static const char CRNUL[] = { '\r', '\0' };
Sean Middleditch4a156042009-03-16 17:10:58 -0400936 char buffer[4096];
937 va_list va;
938 int rs, i, l;
939
940 /* format */
941 va_start(va, fmt);
942 rs = vsnprintf(buffer, sizeof(buffer), fmt, va);
943 va_end(va);
944
945 /* send */
946 for (l = i = 0; i != rs; ++i) {
947 /* special characters */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400948 if (buffer[i] == TELNET_IAC || buffer[i] == '\r' ||
Sean Middleditch4a156042009-03-16 17:10:58 -0400949 buffer[i] == '\n') {
950 /* dump prior portion of text */
951 if (i != l)
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400952 _send(telnet, buffer + l, i - l);
Sean Middleditch4a156042009-03-16 17:10:58 -0400953 l = i + 1;
954
955 /* IAC -> IAC IAC */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400956 if (buffer[i] == TELNET_IAC)
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400957 telnet_iac(telnet, TELNET_IAC);
Sean Middleditch4a156042009-03-16 17:10:58 -0400958 /* automatic translation of \r -> CRNUL */
959 else if (buffer[i] == '\r')
960 _send(telnet, CRNUL, 2);
961 /* automatic translation of \n -> CRLF */
962 else if (buffer[i] == '\n')
963 _send(telnet, CRLF, 2);
964 }
965 }
966
967 /* send whatever portion of buffer is left */
968 if (i != l)
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400969 _send(telnet, buffer + l, i - l);
Sean Middleditch4a156042009-03-16 17:10:58 -0400970
971 return rs;
972}
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400973
Sean Middleditch7491bf42009-03-20 23:52:18 -0400974/* send formatted data through telnet_send */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400975int telnet_printf2(telnet_t *telnet, const char *fmt, ...) {
976 char buffer[4096];
977 va_list va;
978 int rs;
979
980 /* format */
981 va_start(va, fmt);
982 rs = vsnprintf(buffer, sizeof(buffer), fmt, va);
983 va_end(va);
984
985 /* send */
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400986 telnet_send(telnet, buffer, rs);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400987
988 return rs;
989}
Sean Middleditche22b4772009-03-22 16:44:40 -0400990
991/* send ZMP data */
992void telnet_send_zmp(telnet_t *telnet, size_t argc, const char **argv) {
993 size_t i;
994
995 /* ZMP header */
996 telnet_begin_sb(telnet, TELNET_TELOPT_ZMP);
997
998 /* send out each argument, including trailing NUL byte */
999 for (i = 0; i != argc; ++i)
1000 telnet_send(telnet, argv[i], strlen(argv[i] + 1));
1001
1002 /* ZMP footer */
1003 telnet_finish_sb(telnet);
1004}
1005
1006/* send ZMP data using varargs */
1007void telnet_send_zmpv(telnet_t *telnet, ...) {
1008 va_list va;
1009 const char* arg;
1010
1011 /* ZMP header */
1012 telnet_begin_sb(telnet, TELNET_TELOPT_ZMP);
1013
1014 /* send out each argument, including trailing NUL byte */
1015 va_start(va, telnet);
1016 while ((arg = va_arg(va, const char *)) != NULL)
1017 telnet_send(telnet, arg, strlen(arg) + 1);
1018 va_end(va);
1019
1020 /* ZMP footer */
1021 telnet_finish_sb(telnet);
1022}