blob: 005b1d274e84c077f36fc1f20166534892f6ed03 [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 Middleditch5b5bc922009-03-15 23:02:10 -040025/* RFC1143 state names */
26#define RFC1143_NO 0x00
27#define RFC1143_YES 0x01
28
29#define RFC1143_WANT 0x02
30#define RFC1143_OP 0x04
31
32#define RFC1143_WANTNO (RFC1143_WANT|RFC1143_YES)
33#define RFC1143_WANTYES (RFC1143_WANT|RFC1143_NO)
34#define RFC1143_WANTNO_OP (RFC1143_WANTNO|RFC1143_OP)
35#define RFC1143_WANTYES_OP (RFC1143_WANTYES|RFC1143_OP)
36
Sean Middleditch4d9444d2009-03-13 22:48:05 -040037/* buffer sizes */
38static const unsigned int _buffer_sizes[] = {
39 0,
40 512,
41 2048,
42 8192,
43 16384,
44};
Sean Middleditch812358d2009-03-15 23:24:03 -040045static const unsigned int _buffer_sizes_count = sizeof(_buffer_sizes) /
46 sizeof(_buffer_sizes[0]);
Sean Middleditch4d9444d2009-03-13 22:48:05 -040047
Sean Middleditch5b5bc922009-03-15 23:02:10 -040048/* event dispatch helper; return value is value of the accept field of the
49 * event struct after dispatch; used for the funky REQUEST event */
Sean Middleditch35b95be2009-03-15 23:46:31 -040050static int _event(libtelnet_t *telnet, libtelnet_event_type_t type,
Sean Middleditch812358d2009-03-15 23:24:03 -040051 unsigned char command, unsigned char telopt, unsigned char *buffer,
52 unsigned int size) {
53 libtelnet_event_t ev;
Sean Middleditch5b5bc922009-03-15 23:02:10 -040054 ev.buffer = buffer;
55 ev.size = size;
Sean Middleditch637df7f2009-03-15 12:57:32 -040056 ev.type = type;
57 ev.command = command;
58 ev.telopt = telopt;
Sean Middleditch5b5bc922009-03-15 23:02:10 -040059 ev.accept = 0;
Sean Middleditch637df7f2009-03-15 12:57:32 -040060
Sean Middleditch9f79cc52009-03-15 13:39:24 -040061 telnet->eh(telnet, &ev, telnet->ud);
Sean Middleditch5b5bc922009-03-15 23:02:10 -040062
63 return ev.accept;
Sean Middleditch637df7f2009-03-15 12:57:32 -040064}
65
Sean Middleditchd922c6f2009-03-14 22:35:01 -040066/* error generation function */
Sean Middleditchfbe93e32009-03-15 23:39:31 -040067static libtelnet_error_t _error(libtelnet_t *telnet, unsigned line,
68 const char* func, libtelnet_error_t err, int fatal, const char *fmt,
69 ...) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -040070 char buffer[512];
71 va_list va;
72
73 /* format error intro */
Sean Middleditch812358d2009-03-15 23:24:03 -040074 snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", __FILE__, line, func);
Sean Middleditchd922c6f2009-03-14 22:35:01 -040075
76 va_start(va, fmt);
77 vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
78 fmt, va);
79 va_end(va);
80
Sean Middleditch16992272009-03-15 19:42:03 -040081 _event(telnet, fatal ? LIBTELNET_EV_ERROR : LIBTELNET_EV_WARNING, err,
Sean Middleditch5cf66662009-03-15 20:02:14 -040082 0, (unsigned char *)buffer, strlen(buffer));
Sean Middleditchfbe93e32009-03-15 23:39:31 -040083
84 return err;
Sean Middleditchd922c6f2009-03-14 22:35:01 -040085}
86
Sean Middleditch72cc9642009-03-15 11:50:36 -040087/* initialize the zlib box for a telnet box; if deflate is non-zero, it
88 * initializes zlib for delating (compression), otherwise for inflating
Sean Middleditchfbe93e32009-03-15 23:39:31 -040089 * (decompression). returns LIBTELNET_EOK on success, something else on
90 * failure.
Sean Middleditch72cc9642009-03-15 11:50:36 -040091 */
Sean Middleditchfbe93e32009-03-15 23:39:31 -040092libtelnet_error_t _init_zlib(libtelnet_t *telnet, int deflate, int err_fatal) {
93 z_stream *z;
Sean Middleditch72cc9642009-03-15 11:50:36 -040094 int rs;
95
Sean Middleditchfbe93e32009-03-15 23:39:31 -040096 /* if compression is already enabled, fail loudly */
97 if (telnet->z != 0)
98 return _error(telnet, __LINE__, __func__, LIBTELNET_EBADVAL,
99 err_fatal, "cannot initialize compression twice");
100
Sean Middleditch72cc9642009-03-15 11:50:36 -0400101 /* allocate zstream box */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400102 if ((z= (z_stream *)calloc(1, sizeof(z_stream))) == 0)
103 return _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, err_fatal,
Sean Middleditch16992272009-03-15 19:42:03 -0400104 "malloc() failed: %s", strerror(errno));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400105
106 /* initialize */
107 if (deflate) {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400108 if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) {
109 free(z);
110 return _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS,
111 err_fatal, "deflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400112 }
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400113 telnet->flags |= LIBTELNET_PFLAG_DEFLATE;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400114 } else {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400115 if ((rs = inflateInit(z)) != Z_OK) {
116 free(z);
117 return _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS,
118 err_fatal, "inflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400119 }
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400120 telnet->flags &= ~LIBTELNET_PFLAG_DEFLATE;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400121 }
122
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400123 telnet->z = z;
124
125 return LIBTELNET_EOK;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400126}
127
Sean Middleditch8b788962009-03-16 01:06:27 -0400128/* push bytes out, compressing them first if need be */
129static void _send(libtelnet_t *telnet, unsigned char *buffer,
130 unsigned int size) {
131#ifdef HAVE_ZLIB
132 /* if we have a deflate (compression) zlib box, use it */
Sean Middleditch823bc002009-03-16 01:09:26 -0400133 if (telnet->z != 0 && telnet->flags & LIBTELNET_PFLAG_DEFLATE) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400134 unsigned char deflate_buffer[1024];
135 int rs;
136
137 /* initialize z state */
138 telnet->z->next_in = buffer;
139 telnet->z->avail_in = size;
140 telnet->z->next_out = deflate_buffer;
141 telnet->z->avail_out = sizeof(deflate_buffer);
142
143 /* deflate until buffer exhausted and all output is produced */
144 while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
145 /* compress */
146 if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) {
147 _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1,
148 "deflate() failed: %s", zError(rs));
149 deflateEnd(telnet->z);
150 free(telnet->z);
151 telnet->z = 0;
152 break;
153 }
154
155 _event(telnet, LIBTELNET_EV_SEND, 0, 0, deflate_buffer,
156 sizeof(deflate_buffer) - telnet->z->avail_out);
157
158 /* prepare output buffer for next run */
159 telnet->z->next_out = deflate_buffer;
160 telnet->z->avail_out = sizeof(deflate_buffer);
161 }
162
163 /* COMPRESS2 is not negotiated, just send */
164 } else
165#endif /* HAVE_ZLIB */
166 _event(telnet, LIBTELNET_EV_SEND, 0, 0, buffer, size);
167}
168
169/* retrieve RFC1143 option state */
170libtelnet_rfc1143_t _get_rfc1143(libtelnet_t *telnet, unsigned char telopt) {
171 static const libtelnet_rfc1143_t empty = { 0, 0, 0};
172 int i;
173
174 /* search for entry */
175 for (i = 0; i != telnet->q_size; ++i)
176 if (telnet->q[i].telopt == telopt)
177 return telnet->q[i];
178
179 /* not found, return empty value */
180 return empty;
181}
182
183/* save RFC1143 option state */
184void _set_rfc1143(libtelnet_t *telnet, libtelnet_rfc1143_t q) {
185 libtelnet_rfc1143_t *qtmp;
186 int i;
187
188 /* search for entry */
189 for (i = 0; i != telnet->q_size; ++i) {
190 if (telnet->q[i].telopt == q.telopt) {
191 telnet->q[i] = q;
192 return;
193 }
194 }
195
196 /* we're going to need to track state for it, so grow the queue
197 * and put the telopt into it; bail on allocation error
198 */
199 if ((qtmp = (libtelnet_rfc1143_t *)malloc(sizeof(
200 libtelnet_rfc1143_t) * (telnet->q_size + 1))) == 0) {
201 _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, 0,
202 "malloc() failed: %s", strerror(errno));
203 return;
204 }
205 telnet->q = qtmp;
206 telnet->q[telnet->q_size++] = q;
207}
208
209/* send a negotiation without going through the RFC1143 checks */
210static void _send_negotiate(libtelnet_t *telnet, unsigned char cmd,
211 unsigned char opt) {
212 unsigned char bytes[3] = { LIBTELNET_IAC, cmd, opt };
213 _send(telnet, bytes, 3);
214}
215
216/* negotiation handling magic for RFC1143 */
217static void _negotiate(libtelnet_t *telnet, unsigned char cmd,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400218 unsigned char telopt) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400219 libtelnet_rfc1143_t q;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400220
221 /* in PROXY mode, just pass it thru and do nothing */
Sean Middleditch562257e2009-03-15 23:56:25 -0400222 if (telnet->flags & LIBTELNET_FLAG_PROXY) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400223 switch (cmd) {
224 case LIBTELNET_WILL:
Sean Middleditch562257e2009-03-15 23:56:25 -0400225 _event(telnet, LIBTELNET_EV_WILL, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400226 break;
227 case LIBTELNET_WONT:
Sean Middleditch562257e2009-03-15 23:56:25 -0400228 _event(telnet, LIBTELNET_EV_WONT, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400229 break;
230 case LIBTELNET_DO:
Sean Middleditch562257e2009-03-15 23:56:25 -0400231 _event(telnet, LIBTELNET_EV_DO, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400232 break;
233 case LIBTELNET_DONT:
Sean Middleditch562257e2009-03-15 23:56:25 -0400234 _event(telnet, LIBTELNET_EV_DONT, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400235 break;
236 }
237 return;
238 }
239
240 /* lookup the current state of the option */
Sean Middleditch8b788962009-03-16 01:06:27 -0400241 q = _get_rfc1143(telnet, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400242
243 /* start processing... */
244 switch (cmd) {
245 /* request to enable option on remote end or confirm DO */
246 case LIBTELNET_WILL:
Sean Middleditch8b788962009-03-16 01:06:27 -0400247 switch (q.him) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400248 case RFC1143_NO:
249 if (_event(telnet, LIBTELNET_EV_WILL, cmd, telopt, 0, 0) == 1) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400250 q.him = RFC1143_YES;
251 _set_rfc1143(telnet, q);
252 _send_negotiate(telnet, LIBTELNET_DO, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400253 } else
Sean Middleditch8b788962009-03-16 01:06:27 -0400254 _send_negotiate(telnet, LIBTELNET_DONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400255 break;
256 case RFC1143_YES:
257 break;
258 case RFC1143_WANTNO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400259 q.him = RFC1143_NO;
260 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400261 _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0,
262 "DONT answered by WILL");
263 break;
264 case RFC1143_WANTNO_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400265 q.him = RFC1143_YES;
266 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400267 _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0,
268 "DONT answered by WILL");
269 break;
270 case RFC1143_WANTYES:
Sean Middleditch8b788962009-03-16 01:06:27 -0400271 q.him = RFC1143_YES;
272 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400273 break;
274 case RFC1143_WANTYES_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400275 q.him = RFC1143_WANTNO;
276 _set_rfc1143(telnet, q);
277 _send_negotiate(telnet, LIBTELNET_DONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400278 break;
279 }
280 break;
281
282 /* request to disable option on remote end, confirm DONT, reject DO */
283 case LIBTELNET_WONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400284 switch (q.him) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400285 case RFC1143_NO:
286 break;
287 case RFC1143_YES:
Sean Middleditch8b788962009-03-16 01:06:27 -0400288 q.him = RFC1143_NO;
289 _set_rfc1143(telnet, q);
290 _send_negotiate(telnet, LIBTELNET_DONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400291 _event(telnet, LIBTELNET_EV_WONT, 0, telopt,
292 0, 0);
293 break;
294 case RFC1143_WANTNO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400295 q.him = RFC1143_NO;
296 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400297 _event(telnet, LIBTELNET_EV_WONT, 0, telopt,
298 0, 0);
299 break;
300 case RFC1143_WANTNO_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400301 q.him = RFC1143_WANTYES;
302 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400303 _event(telnet, LIBTELNET_EV_DO, 0, telopt,
304 0, 0);
305 break;
306 case RFC1143_WANTYES:
307 case RFC1143_WANTYES_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400308 q.him = RFC1143_NO;
309 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400310 break;
311 }
312 break;
313
314 /* request to enable option on local end or confirm WILL */
315 case LIBTELNET_DO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400316 switch (q.us) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400317 case RFC1143_NO:
318 if (_event(telnet, LIBTELNET_EV_DO, cmd, telopt, 0, 0) == 1) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400319 q.us = RFC1143_YES;
320 _set_rfc1143(telnet, q);
321 _send_negotiate(telnet, LIBTELNET_WILL, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400322 } else
Sean Middleditch8b788962009-03-16 01:06:27 -0400323 _send_negotiate(telnet, LIBTELNET_WONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400324 break;
325 case RFC1143_YES:
326 break;
327 case RFC1143_WANTNO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400328 q.us = RFC1143_NO;
329 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400330 _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0,
331 "WONT answered by DO");
332 break;
333 case RFC1143_WANTNO_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400334 q.us = RFC1143_YES;
335 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400336 _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0,
337 "WONT answered by DO");
338 break;
339 case RFC1143_WANTYES:
Sean Middleditch8b788962009-03-16 01:06:27 -0400340 q.us = RFC1143_YES;
341 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400342 break;
343 case RFC1143_WANTYES_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400344 q.us = RFC1143_WANTNO;
345 _set_rfc1143(telnet, q);
346 _send_negotiate(telnet, LIBTELNET_WONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400347 break;
348 }
349 break;
350
351 /* request to disable option on local end, confirm WONT, reject WILL */
352 case LIBTELNET_DONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400353 switch (q.us) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400354 case RFC1143_NO:
355 break;
356 case RFC1143_YES:
Sean Middleditch8b788962009-03-16 01:06:27 -0400357 q.us = RFC1143_NO;
358 _set_rfc1143(telnet, q);
359 _send_negotiate(telnet, LIBTELNET_WONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400360 _event(telnet, LIBTELNET_EV_DONT, 0, telopt, 0, 0);
361 break;
362 case RFC1143_WANTNO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400363 q.us = RFC1143_NO;
364 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400365 _event(telnet, LIBTELNET_EV_WONT, 0, telopt, 0, 0);
366 break;
367 case RFC1143_WANTNO_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400368 q.us = RFC1143_WANTYES;
369 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400370 _event(telnet, LIBTELNET_EV_WILL, 0, telopt, 0, 0);
371 break;
372 case RFC1143_WANTYES:
373 case RFC1143_WANTYES_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400374 q.us = RFC1143_NO;
375 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400376 break;
377 }
378 break;
379 }
380}
381
Sean Middleditch29144852009-03-12 23:14:47 -0400382/* initialize a telnet state tracker */
Sean Middleditch812358d2009-03-15 23:24:03 -0400383void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh,
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400384 unsigned char flags, void *user_data) {
Sean Middleditch812358d2009-03-15 23:24:03 -0400385 memset(telnet, 0, sizeof(libtelnet_t));
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400386 telnet->ud = user_data;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400387 telnet->eh = eh;
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400388 telnet->flags = flags;
Sean Middleditch29144852009-03-12 23:14:47 -0400389}
390
391/* free up any memory allocated by a state tracker */
Sean Middleditch812358d2009-03-15 23:24:03 -0400392void libtelnet_free(libtelnet_t *telnet) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400393 /* free sub-request buffer */
Sean Middleditch51ad6792009-03-13 20:15:59 -0400394 if (telnet->buffer != 0) {
Sean Middleditch29144852009-03-12 23:14:47 -0400395 free(telnet->buffer);
396 telnet->buffer = 0;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400397 telnet->buffer_size = 0;
398 telnet->buffer_pos = 0;
Sean Middleditch29144852009-03-12 23:14:47 -0400399 }
Sean Middleditch9de15982009-03-14 03:35:49 -0400400
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400401 /* free zlib box */
402 if (telnet->z != 0) {
403 if (telnet->flags & LIBTELNET_PFLAG_DEFLATE)
404 deflateEnd(telnet->z);
405 else
406 inflateEnd(telnet->z);
407 free(telnet->z);
408 telnet->z = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400409 }
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400410
411 /* free RFC1143 queue */
412 if (telnet->q) {
413 free(telnet->q);
414 telnet->q = 0;
415 telnet->q_size = 0;
416 }
Sean Middleditch29144852009-03-12 23:14:47 -0400417}
418
Sean Middleditch51ad6792009-03-13 20:15:59 -0400419/* push a byte into the telnet buffer */
Sean Middleditch812358d2009-03-15 23:24:03 -0400420static libtelnet_error_t _buffer_byte(libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400421 unsigned char byte) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400422 unsigned char *new_buffer;
423 int i;
424
Sean Middleditch51ad6792009-03-13 20:15:59 -0400425 /* check if we're out of room */
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400426 if (telnet->buffer_pos == telnet->buffer_size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400427 /* find the next buffer size */
428 for (i = 0; i != _buffer_sizes_count; ++i) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400429 if (_buffer_sizes[i] == telnet->buffer_size)
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400430 break;
431 }
432
433 /* overflow -- can't grow any more */
434 if (i >= _buffer_sizes_count - 1) {
Sean Middleditch16992272009-03-15 19:42:03 -0400435 _error(telnet, __LINE__, __func__, LIBTELNET_EOVERFLOW, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400436 "subnegotiation buffer size limit reached");
Sean Middleditch51ad6792009-03-13 20:15:59 -0400437 libtelnet_free(telnet);
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400438 return LIBTELNET_EOVERFLOW;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400439 }
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400440
441 /* (re)allocate buffer */
442 new_buffer = (unsigned char *)realloc(telnet->buffer,
443 _buffer_sizes[i + 1]);
444 if (new_buffer == 0) {
Sean Middleditch16992272009-03-15 19:42:03 -0400445 _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, 0,
446 "realloc() failed");
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400447 libtelnet_free(telnet);
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400448 return LIBTELNET_ENOMEM;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400449 }
450
451 telnet->buffer = new_buffer;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400452 telnet->buffer_size = _buffer_sizes[i + 1];
Sean Middleditch51ad6792009-03-13 20:15:59 -0400453 }
454
455 /* push the byte, all set */
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400456 telnet->buffer[telnet->buffer_pos++] = byte;
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400457 return LIBTELNET_EOK;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400458}
459
Sean Middleditch812358d2009-03-15 23:24:03 -0400460static void _process(libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400461 unsigned int size) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400462 unsigned char byte;
463 unsigned int i, start;
464 for (i = start = 0; i != size; ++i) {
465 byte = buffer[i];
466 switch (telnet->state) {
467 /* regular data */
Sean Middleditch9de15982009-03-14 03:35:49 -0400468 case LIBTELNET_STATE_DATA:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400469 /* on an IAC byte, pass through all pending bytes and
470 * switch states */
471 if (byte == LIBTELNET_IAC) {
472 if (i != start)
Sean Middleditch637df7f2009-03-15 12:57:32 -0400473 _event(telnet, LIBTELNET_EV_DATA, 0, 0, &buffer[start],
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400474 i - start);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400475 telnet->state = LIBTELNET_STATE_IAC;
476 }
477 break;
478
479 /* IAC command */
480 case LIBTELNET_STATE_IAC:
481 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400482 /* subnegotiation */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400483 case LIBTELNET_SB:
484 telnet->state = LIBTELNET_STATE_SB;
485 break;
486 /* negotiation commands */
487 case LIBTELNET_WILL:
488 telnet->state = LIBTELNET_STATE_WILL;
489 break;
490 case LIBTELNET_WONT:
491 telnet->state = LIBTELNET_STATE_WONT;
492 break;
493 case LIBTELNET_DO:
494 telnet->state = LIBTELNET_STATE_DO;
495 break;
496 case LIBTELNET_DONT:
497 telnet->state = LIBTELNET_STATE_DONT;
498 break;
499 /* IAC escaping */
500 case LIBTELNET_IAC:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400501 _event(telnet, LIBTELNET_EV_DATA, 0, 0, &byte, 1);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400502 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400503 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400504 break;
505 /* some other command */
506 default:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400507 _event(telnet, LIBTELNET_EV_IAC, byte, 0, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400508 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400509 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400510 }
511 break;
512
513 /* negotiation commands */
514 case LIBTELNET_STATE_DO:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400515 _negotiate(telnet, LIBTELNET_DO, byte);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400516 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400517 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400518 break;
519 case LIBTELNET_STATE_DONT:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400520 _negotiate(telnet, LIBTELNET_DONT, byte);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400521 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400522 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400523 break;
524 case LIBTELNET_STATE_WILL:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400525 _negotiate(telnet, LIBTELNET_WILL, byte);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400526 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400527 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400528 break;
529 case LIBTELNET_STATE_WONT:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400530 _negotiate(telnet, LIBTELNET_WONT, byte);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400531 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400532 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400533 break;
534
Sean Middleditchda0e6952009-03-15 13:28:09 -0400535 /* subnegotiation -- determine subnegotiation telopt */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400536 case LIBTELNET_STATE_SB:
Sean Middleditchda0e6952009-03-15 13:28:09 -0400537 telnet->sb_telopt = byte;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400538 telnet->buffer_pos = 0;
Sean Middleditchda0e6952009-03-15 13:28:09 -0400539 telnet->state = LIBTELNET_STATE_SB_DATA;
540 break;
541
542 /* subnegotiation -- buffer bytes until end request */
543 case LIBTELNET_STATE_SB_DATA:
Sean Middleditch6b372882009-03-14 13:06:47 -0400544 /* IAC command in subnegotiation -- either IAC SE or IAC IAC */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400545 if (byte == LIBTELNET_IAC) {
Sean Middleditchda0e6952009-03-15 13:28:09 -0400546 telnet->state = LIBTELNET_STATE_SB_DATA_IAC;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400547 /* buffer the byte, or bail if we can't */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400548 } else if (_buffer_byte(telnet, byte) != LIBTELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400549 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400550 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400551 }
552 break;
553
Sean Middleditch6b372882009-03-14 13:06:47 -0400554 /* IAC escaping inside a subnegotiation */
Sean Middleditchda0e6952009-03-15 13:28:09 -0400555 case LIBTELNET_STATE_SB_DATA_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400556 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400557 /* end subnegotiation */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400558 case LIBTELNET_SE:
Sean Middleditch9de15982009-03-14 03:35:49 -0400559 /* return to default state */
560 start = i + 1;
561 telnet->state = LIBTELNET_STATE_DATA;
562
Sean Middleditch9de15982009-03-14 03:35:49 -0400563 /* invoke callback */
Sean Middleditch637df7f2009-03-15 12:57:32 -0400564 _event(telnet, LIBTELNET_EV_SUBNEGOTIATION, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400565 telnet->sb_telopt, telnet->buffer, telnet->buffer_pos);
Sean Middleditch9de15982009-03-14 03:35:49 -0400566
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400567#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400568 /* if we are a client or a proxy and just received the
569 * COMPRESS2 begin marker, setup our zlib box and start
570 * handling the compressed stream if it's not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400571 */
Sean Middleditchda0e6952009-03-15 13:28:09 -0400572 if (telnet->sb_telopt == LIBTELNET_TELOPT_COMPRESS2 &&
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400573 telnet->z == 0 &&
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400574 telnet->flags & LIBTELNET_FLAG_PROXY) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400575
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400576 if (_init_zlib(telnet, 0, 1) != LIBTELNET_EOK)
Sean Middleditch9de15982009-03-14 03:35:49 -0400577 break;
Sean Middleditch9de15982009-03-14 03:35:49 -0400578
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400579 /* notify app that compression was enabled */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400580 _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400581
Sean Middleditch9de15982009-03-14 03:35:49 -0400582 /* any remaining bytes in the buffer are compressed.
583 * we have to re-invoke libtelnet_push to get those
584 * bytes inflated and abort trying to process the
585 * remaining compressed bytes in the current _process
586 * buffer argument
587 */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400588 libtelnet_push(telnet, &buffer[start], size - start);
Sean Middleditch9de15982009-03-14 03:35:49 -0400589 return;
590 }
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400591#endif /* HAVE_ZLIB */
592
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400593 break;
594 /* escaped IAC byte */
595 case LIBTELNET_IAC:
596 /* push IAC into buffer */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400597 if (_buffer_byte(telnet, LIBTELNET_IAC) !=
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400598 LIBTELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400599 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400600 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400601 } else {
Sean Middleditchda0e6952009-03-15 13:28:09 -0400602 telnet->state = LIBTELNET_STATE_SB_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400603 }
604 break;
605 /* something else -- protocol error */
606 default:
Sean Middleditch16992272009-03-15 19:42:03 -0400607 _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400608 "unexpected byte after IAC inside SB: %d",
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400609 byte);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400610 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400611 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400612 break;
613 }
614 break;
615 }
616 }
617
618 /* pass through any remaining bytes */
Sean Middleditchd30fd572009-03-14 12:55:17 -0400619 if (telnet->state == LIBTELNET_STATE_DATA && i != start)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400620 _event(telnet, LIBTELNET_EV_DATA, 0, 0, buffer + start, i - start);
Sean Middleditch29144852009-03-12 23:14:47 -0400621}
622
Sean Middleditch9de15982009-03-14 03:35:49 -0400623/* push a bytes into the state tracker */
Sean Middleditch812358d2009-03-15 23:24:03 -0400624void libtelnet_push(libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400625 unsigned int size) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400626#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400627 /* if we have an inflate (decompression) zlib stream, use it */
Sean Middleditch4611d912009-03-16 00:53:57 -0400628 if (telnet->z != 0 && !(telnet->flags & LIBTELNET_PFLAG_DEFLATE)) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400629 unsigned char inflate_buffer[4096];
630 int rs;
631
632 /* initialize zlib state */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400633 telnet->z->next_in = buffer;
634 telnet->z->avail_in = size;
635 telnet->z->next_out = inflate_buffer;
636 telnet->z->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400637
638 /* inflate until buffer exhausted and all output is produced */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400639 while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400640 /* reset output buffer */
641
642 /* decompress */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400643 rs = inflate(telnet->z, Z_SYNC_FLUSH);
Sean Middleditch9de15982009-03-14 03:35:49 -0400644
645 /* process the decompressed bytes on success */
646 if (rs == Z_OK || rs == Z_STREAM_END)
647 _process(telnet, inflate_buffer, sizeof(inflate_buffer) -
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400648 telnet->z->avail_out);
Sean Middleditch9de15982009-03-14 03:35:49 -0400649 else
Sean Middleditch16992272009-03-15 19:42:03 -0400650 _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1,
651 "inflate() failed: %s", zError(rs));
Sean Middleditch9de15982009-03-14 03:35:49 -0400652
653 /* prepare output buffer for next run */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400654 telnet->z->next_out = inflate_buffer;
655 telnet->z->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400656
657 /* on error (or on end of stream) disable further inflation */
658 if (rs != Z_OK) {
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400659 _event(telnet, LIBTELNET_EV_COMPRESS, 0, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400660
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400661 inflateEnd(telnet->z);
662 free(telnet->z);
663 telnet->z = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400664 break;
665 }
666 }
667
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400668 /* COMPRESS2 is not negotiated, just process */
669 } else
670#endif /* HAVE_ZLIB */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400671 _process(telnet, buffer, size);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400672}
673
Sean Middleditch29144852009-03-12 23:14:47 -0400674/* send an iac command */
Sean Middleditch812358d2009-03-15 23:24:03 -0400675void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd) {
Sean Middleditchf9cebec2009-03-13 20:17:31 -0400676 unsigned char bytes[2] = { LIBTELNET_IAC, cmd };
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400677 _send(telnet, bytes, 2);
Sean Middleditch29144852009-03-12 23:14:47 -0400678}
679
680/* send negotiation */
Sean Middleditch812358d2009-03-15 23:24:03 -0400681void libtelnet_send_negotiate(libtelnet_t *telnet, unsigned char cmd,
Sean Middleditch8b788962009-03-16 01:06:27 -0400682 unsigned char telopt) {
683 libtelnet_rfc1143_t q;
684
685 /* if we're in proxy mode, just send it now */
686 if (telnet->flags & LIBTELNET_FLAG_PROXY) {
687 unsigned char bytes[3] = { LIBTELNET_IAC, cmd, telopt };
688 _send(telnet, bytes, 3);
689 return;
690 }
691
692 /* get current option states */
693 q = _get_rfc1143(telnet, telopt);
694
695 switch (cmd) {
696 /* advertise willingess to support an option */
697 case LIBTELNET_WILL:
698 switch (q.us) {
699 case RFC1143_NO:
700 q.us = RFC1143_WANTYES;
701 _set_rfc1143(telnet, q);
702 _negotiate(telnet, LIBTELNET_WILL, telopt);
703 break;
704 case RFC1143_YES:
705 break;
706 case RFC1143_WANTNO:
707 q.us = RFC1143_WANTNO_OP;
708 _set_rfc1143(telnet, q);
709 break;
710 case RFC1143_WANTYES:
711 break;
712 case RFC1143_WANTNO_OP:
713 break;
714 case RFC1143_WANTYES_OP:
715 q.us = RFC1143_WANTYES;
716 _set_rfc1143(telnet, q);
717 break;
718 }
719 break;
720
721 /* force turn-off of locally enabled option */
722 case LIBTELNET_WONT:
723 switch (q.us) {
724 case RFC1143_NO:
725 break;
726 case RFC1143_YES:
727 q.us = RFC1143_WANTNO;
728 _set_rfc1143(telnet, q);
729 _negotiate(telnet, LIBTELNET_WONT, telopt);
730 break;
731 case RFC1143_WANTNO:
732 break;
733 case RFC1143_WANTYES:
734 q.us = RFC1143_WANTYES_OP;
735 _set_rfc1143(telnet, q);
736 break;
737 case RFC1143_WANTNO_OP:
738 q.us = RFC1143_WANTNO;
739 _set_rfc1143(telnet, q);
740 break;
741 case RFC1143_WANTYES_OP:
742 break;
743 }
744 break;
745
746 /* ask remote end to enable an option */
747 case LIBTELNET_DO:
748 switch (q.him) {
749 case RFC1143_NO:
750 q.him = RFC1143_WANTYES;
751 _set_rfc1143(telnet, q);
752 _negotiate(telnet, LIBTELNET_DO, telopt);
753 break;
754 case RFC1143_YES:
755 break;
756 case RFC1143_WANTNO:
757 q.him = RFC1143_WANTNO_OP;
758 _set_rfc1143(telnet, q);
759 break;
760 case RFC1143_WANTYES:
761 break;
762 case RFC1143_WANTNO_OP:
763 break;
764 case RFC1143_WANTYES_OP:
765 q.him = RFC1143_WANTYES;
766 _set_rfc1143(telnet, q);
767 break;
768 }
769 break;
770
771 /* demand remote end disable an option */
772 case LIBTELNET_DONT:
773 switch (q.him) {
774 case RFC1143_NO:
775 break;
776 case RFC1143_YES:
777 q.him = RFC1143_WANTNO;
778 _set_rfc1143(telnet, q);
779 _negotiate(telnet, LIBTELNET_DONT, telopt);
780 break;
781 case RFC1143_WANTNO:
782 break;
783 case RFC1143_WANTYES:
784 q.him = RFC1143_WANTYES_OP;
785 _set_rfc1143(telnet, q);
786 break;
787 case RFC1143_WANTNO_OP:
788 q.him = RFC1143_WANTNO;
789 _set_rfc1143(telnet, q);
790 break;
791 case RFC1143_WANTYES_OP:
792 break;
793 }
794 break;
795 }
Sean Middleditch29144852009-03-12 23:14:47 -0400796}
797
798/* send non-command data (escapes IAC bytes) */
Sean Middleditch812358d2009-03-15 23:24:03 -0400799void libtelnet_send_data(libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400800 unsigned int size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400801 unsigned int i, l;
Sean Middleditch29144852009-03-12 23:14:47 -0400802 for (l = i = 0; i != size; ++i) {
803 /* dump prior portion of text, send escaped bytes */
804 if (buffer[i] == LIBTELNET_IAC) {
805 /* dump prior text if any */
806 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400807 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400808 l = i + 1;
809
810 /* send escape */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400811 libtelnet_send_command(telnet, LIBTELNET_IAC);
Sean Middleditch29144852009-03-12 23:14:47 -0400812 }
813 }
814
815 /* send whatever portion of buffer is left */
816 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400817 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400818}
819
820/* send sub-request */
Sean Middleditch812358d2009-03-15 23:24:03 -0400821void libtelnet_send_subnegotiation(libtelnet_t *telnet, unsigned char opt,
822 unsigned char *buffer, unsigned int size) {
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400823 libtelnet_send_command(telnet, LIBTELNET_SB);
824 libtelnet_send_data(telnet, &opt, 1);
825 libtelnet_send_data(telnet, buffer, size);
826 libtelnet_send_command(telnet, LIBTELNET_SE);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400827
828#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400829 /* if we're a proxy and we just sent the COMPRESS2 marker, we must
830 * make sure all further data is compressed if not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400831 */
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400832 if (telnet->flags & LIBTELNET_FLAG_PROXY &&
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400833 telnet->z == 0 &&
Sean Middleditch72cc9642009-03-15 11:50:36 -0400834 opt == LIBTELNET_TELOPT_COMPRESS2) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400835
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400836 if (_init_zlib(telnet, 1, 1) != LIBTELNET_EOK)
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400837 return;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400838
839 /* notify app that compression was enabled */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400840 _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400841 }
842#endif /* HAVE_ZLIB */
Sean Middleditch29144852009-03-12 23:14:47 -0400843}
Sean Middleditch124a1c22009-03-15 13:20:03 -0400844
Sean Middleditch812358d2009-03-15 23:24:03 -0400845void libtelnet_begin_compress2(libtelnet_t *telnet) {
Sean Middleditch124a1c22009-03-15 13:20:03 -0400846#ifdef HAVE_ZLIB
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400847 unsigned char compress2[] = { LIBTELNET_IAC, LIBTELNET_SB,
848 LIBTELNET_TELOPT_COMPRESS2, LIBTELNET_IAC, LIBTELNET_SE };
Sean Middleditch124a1c22009-03-15 13:20:03 -0400849
Sean Middleditch124a1c22009-03-15 13:20:03 -0400850 /* attempt to create output stream first, bail if we can't */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400851 if (_init_zlib(telnet, 1, 0) != LIBTELNET_EOK)
Sean Middleditch124a1c22009-03-15 13:20:03 -0400852 return;
853
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400854 /* send compression marker. we send directly to the event handler
855 * instead of passing through _send because _send would result in
856 * the compress marker itself being compressed.
857 */
858 _event(telnet, LIBTELNET_EV_SEND, 0, 0, compress2, sizeof(compress2));
Sean Middleditch124a1c22009-03-15 13:20:03 -0400859#endif /* HAVE_ZLIB */
860}