blob: ea0e6616189d8085e03c542aa194d28f1d3155a5 [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 Middleditch637df7f2009-03-15 12:57:32 -040025/* error handler helpers */
Sean Middleditchd922c6f2009-03-14 22:35:01 -040026#ifdef ERROR
27# undef ERROR
28#endif
Sean Middleditch9f79cc52009-03-15 13:39:24 -040029#define ERROR(telnet, code, msg) \
30 _error(telnet, __FILE__, __LINE__, code, "%s", msg)
31#define ERROR_NOMEM(telnet, msg) \
32 _error(telnet, __FILE__, __LINE__, LIBTELNET_ENOMEM, \
Sean Middleditchd922c6f2009-03-14 22:35:01 -040033 "%s: %s", msg, strerror(errno))
Sean Middleditch9f79cc52009-03-15 13:39:24 -040034#define ERROR_ZLIB(telnet, rs, msg) \
Sean Middleditchf66a7ee2009-03-15 11:54:07 -040035 _error(telnet, __FILE__, __LINE__, LIBTELNET_EUNKNOWN, \
Sean Middleditch9f79cc52009-03-15 13:39:24 -040036 "%s: %s", msg, zError(rs))
Sean Middleditchd922c6f2009-03-14 22:35:01 -040037
Sean Middleditch4d9444d2009-03-13 22:48:05 -040038/* buffer sizes */
39static const unsigned int _buffer_sizes[] = {
40 0,
41 512,
42 2048,
43 8192,
44 16384,
45};
46static const unsigned int _buffer_sizes_count =
47 sizeof(_buffer_sizes) / sizeof(_buffer_sizes[0]);
48
Sean Middleditch637df7f2009-03-15 12:57:32 -040049/* event dispatch helper */
50static void _event(struct libtelnet_t *telnet,
51 enum libtelnet_event_type_t type, unsigned char command,
Sean Middleditch9f79cc52009-03-15 13:39:24 -040052 unsigned char telopt, unsigned char *buffer, unsigned int size) {
Sean Middleditch637df7f2009-03-15 12:57:32 -040053 struct libtelnet_event_t ev;
54 ev.type = type;
55 ev.command = command;
56 ev.telopt = telopt;
57 ev.buffer = buffer;
58 ev.size = size;
59
Sean Middleditch9f79cc52009-03-15 13:39:24 -040060 telnet->eh(telnet, &ev, telnet->ud);
Sean Middleditch637df7f2009-03-15 12:57:32 -040061}
62
Sean Middleditchd922c6f2009-03-14 22:35:01 -040063/* error generation function */
64static void _error(struct libtelnet_t *telnet, const char *file, unsigned line,
Sean Middleditch9f79cc52009-03-15 13:39:24 -040065 enum libtelnet_error_t err, const char *fmt, ...) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -040066 char buffer[512];
67 va_list va;
68
69 /* format error intro */
70 snprintf(buffer, sizeof(buffer), "%s:%u: ",
71 file, line);
72
73 va_start(va, fmt);
74 vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
75 fmt, va);
76 va_end(va);
77
Sean Middleditch9f79cc52009-03-15 13:39:24 -040078 _event(telnet, LIBTELNET_EV_ERROR, err, 0, 0, 0);
Sean Middleditchd922c6f2009-03-14 22:35:01 -040079}
80
Sean Middleditch72cc9642009-03-15 11:50:36 -040081/* initialize the zlib box for a telnet box; if deflate is non-zero, it
82 * initializes zlib for delating (compression), otherwise for inflating
83 * (decompression)
84 */
Sean Middleditch9f79cc52009-03-15 13:39:24 -040085z_stream *_init_zlib(struct libtelnet_t *telnet, int deflate) {
Sean Middleditch72cc9642009-03-15 11:50:36 -040086 z_stream *zlib;
87 int rs;
88
89 /* allocate zstream box */
90 if ((zlib = (z_stream *)calloc(1, sizeof(z_stream)))
91 == 0) {
Sean Middleditch9f79cc52009-03-15 13:39:24 -040092 ERROR_NOMEM(telnet, "malloc() failed");
Sean Middleditch72cc9642009-03-15 11:50:36 -040093 return 0;
94 }
95
96 /* initialize */
97 if (deflate) {
98 if ((rs = deflateInit(zlib, Z_DEFAULT_COMPRESSION)) != Z_OK) {
99 free(zlib);
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400100 ERROR_ZLIB(telnet, rs, "deflateInit() failed");
Sean Middleditch72cc9642009-03-15 11:50:36 -0400101 return 0;
102 }
103 } else {
104 if ((rs = inflateInit(zlib)) != Z_OK) {
105 free(zlib);
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400106 ERROR_ZLIB(telnet, rs, "inflateInit() failed");
Sean Middleditch72cc9642009-03-15 11:50:36 -0400107 return 0;
108 }
109 }
110
111 return zlib;
112}
113
Sean Middleditch29144852009-03-12 23:14:47 -0400114/* initialize a telnet state tracker */
Sean Middleditch637df7f2009-03-15 12:57:32 -0400115void libtelnet_init(struct libtelnet_t *telnet, libtelnet_event_handler_t eh,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400116 enum libtelnet_mode_t mode, void *user_data) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400117 memset(telnet, 0, sizeof(struct libtelnet_t));
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400118 telnet->ud = user_data;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400119 telnet->eh = eh;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400120 telnet->mode = mode;
Sean Middleditch29144852009-03-12 23:14:47 -0400121}
122
123/* free up any memory allocated by a state tracker */
Sean Middleditch51ad6792009-03-13 20:15:59 -0400124void libtelnet_free(struct libtelnet_t *telnet) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400125 /* free sub-request buffer */
Sean Middleditch51ad6792009-03-13 20:15:59 -0400126 if (telnet->buffer != 0) {
Sean Middleditch29144852009-03-12 23:14:47 -0400127 free(telnet->buffer);
128 telnet->buffer = 0;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400129 telnet->size = 0;
130 telnet->length = 0;
Sean Middleditch29144852009-03-12 23:14:47 -0400131 }
Sean Middleditch9de15982009-03-14 03:35:49 -0400132
Sean Middleditch72cc9642009-03-15 11:50:36 -0400133 /* free zlib box(es) */
134 if (telnet->z_inflate != 0) {
135 inflateEnd(telnet->z_inflate);
136 free(telnet->z_inflate);
137 telnet->z_inflate = 0;
138 }
139 if (telnet->z_deflate != 0) {
140 deflateEnd(telnet->z_deflate);
141 free(telnet->z_deflate);
142 telnet->z_deflate = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400143 }
Sean Middleditch29144852009-03-12 23:14:47 -0400144}
145
Sean Middleditch51ad6792009-03-13 20:15:59 -0400146/* push a byte into the telnet buffer */
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400147static enum libtelnet_error_t _buffer_byte(struct libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400148 unsigned char byte) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400149 unsigned char *new_buffer;
150 int i;
151
Sean Middleditch51ad6792009-03-13 20:15:59 -0400152 /* check if we're out of room */
153 if (telnet->length == telnet->size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400154 /* find the next buffer size */
155 for (i = 0; i != _buffer_sizes_count; ++i) {
156 if (_buffer_sizes[i] == telnet->size)
157 break;
158 }
159
160 /* overflow -- can't grow any more */
161 if (i >= _buffer_sizes_count - 1) {
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400162 _error(telnet, __FILE__, __LINE__, LIBTELNET_EOVERFLOW,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400163 "subnegotiation buffer size limit reached");
Sean Middleditch51ad6792009-03-13 20:15:59 -0400164 libtelnet_free(telnet);
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400165 return LIBTELNET_EOVERFLOW;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400166 }
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400167
168 /* (re)allocate buffer */
169 new_buffer = (unsigned char *)realloc(telnet->buffer,
170 _buffer_sizes[i + 1]);
171 if (new_buffer == 0) {
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400172 ERROR_NOMEM(telnet, "realloc() failed");
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400173 libtelnet_free(telnet);
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400174 return LIBTELNET_ENOMEM;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400175 }
176
177 telnet->buffer = new_buffer;
178 telnet->size = _buffer_sizes[i + 1];
Sean Middleditch51ad6792009-03-13 20:15:59 -0400179 }
180
181 /* push the byte, all set */
182 telnet->buffer[telnet->length++] = byte;
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400183 return LIBTELNET_EOK;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400184}
185
Sean Middleditch9de15982009-03-14 03:35:49 -0400186static void _process(struct libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400187 unsigned int size) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400188 unsigned char byte;
189 unsigned int i, start;
190 for (i = start = 0; i != size; ++i) {
191 byte = buffer[i];
192 switch (telnet->state) {
193 /* regular data */
Sean Middleditch9de15982009-03-14 03:35:49 -0400194 case LIBTELNET_STATE_DATA:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400195 /* on an IAC byte, pass through all pending bytes and
196 * switch states */
197 if (byte == LIBTELNET_IAC) {
198 if (i != start)
Sean Middleditch637df7f2009-03-15 12:57:32 -0400199 _event(telnet, LIBTELNET_EV_DATA, 0, 0, &buffer[start],
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400200 i - start);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400201 telnet->state = LIBTELNET_STATE_IAC;
202 }
203 break;
204
205 /* IAC command */
206 case LIBTELNET_STATE_IAC:
207 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400208 /* subnegotiation */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400209 case LIBTELNET_SB:
210 telnet->state = LIBTELNET_STATE_SB;
211 break;
212 /* negotiation commands */
213 case LIBTELNET_WILL:
214 telnet->state = LIBTELNET_STATE_WILL;
215 break;
216 case LIBTELNET_WONT:
217 telnet->state = LIBTELNET_STATE_WONT;
218 break;
219 case LIBTELNET_DO:
220 telnet->state = LIBTELNET_STATE_DO;
221 break;
222 case LIBTELNET_DONT:
223 telnet->state = LIBTELNET_STATE_DONT;
224 break;
225 /* IAC escaping */
226 case LIBTELNET_IAC:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400227 _event(telnet, LIBTELNET_EV_DATA, 0, 0, &byte, 1);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400228 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400229 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400230 break;
231 /* some other command */
232 default:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400233 _event(telnet, LIBTELNET_EV_IAC, byte, 0, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400234 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400235 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400236 }
237 break;
238
239 /* negotiation commands */
240 case LIBTELNET_STATE_DO:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400241 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_DO, byte, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400242 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400243 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400244 break;
245 case LIBTELNET_STATE_DONT:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400246 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_DONT, byte, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400247 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400248 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400249 break;
250 case LIBTELNET_STATE_WILL:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400251 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_WILL, byte, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400252 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400253 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400254 break;
255 case LIBTELNET_STATE_WONT:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400256 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_WONT, byte, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400257 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400258 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400259 break;
260
Sean Middleditchda0e6952009-03-15 13:28:09 -0400261 /* subnegotiation -- determine subnegotiation telopt */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400262 case LIBTELNET_STATE_SB:
Sean Middleditchda0e6952009-03-15 13:28:09 -0400263 telnet->sb_telopt = byte;
264 telnet->length = 0;
265 telnet->state = LIBTELNET_STATE_SB_DATA;
266 break;
267
268 /* subnegotiation -- buffer bytes until end request */
269 case LIBTELNET_STATE_SB_DATA:
Sean Middleditch6b372882009-03-14 13:06:47 -0400270 /* IAC command in subnegotiation -- either IAC SE or IAC IAC */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400271 if (byte == LIBTELNET_IAC) {
Sean Middleditchda0e6952009-03-15 13:28:09 -0400272 telnet->state = LIBTELNET_STATE_SB_DATA_IAC;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400273 /* buffer the byte, or bail if we can't */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400274 } else if (_buffer_byte(telnet, byte) != LIBTELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400275 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400276 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400277 }
278 break;
279
Sean Middleditch6b372882009-03-14 13:06:47 -0400280 /* IAC escaping inside a subnegotiation */
Sean Middleditchda0e6952009-03-15 13:28:09 -0400281 case LIBTELNET_STATE_SB_DATA_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400282 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400283 /* end subnegotiation */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400284 case LIBTELNET_SE:
Sean Middleditch9de15982009-03-14 03:35:49 -0400285 /* return to default state */
286 start = i + 1;
287 telnet->state = LIBTELNET_STATE_DATA;
288
Sean Middleditch9de15982009-03-14 03:35:49 -0400289 /* invoke callback */
Sean Middleditch637df7f2009-03-15 12:57:32 -0400290 _event(telnet, LIBTELNET_EV_SUBNEGOTIATION, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400291 telnet->sb_telopt, telnet->buffer, telnet->length);
Sean Middleditch9de15982009-03-14 03:35:49 -0400292
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400293#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400294 /* if we are a client or a proxy and just received the
295 * COMPRESS2 begin marker, setup our zlib box and start
296 * handling the compressed stream if it's not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400297 */
Sean Middleditchda0e6952009-03-15 13:28:09 -0400298 if (telnet->sb_telopt == LIBTELNET_TELOPT_COMPRESS2 &&
Sean Middleditch72cc9642009-03-15 11:50:36 -0400299 telnet->z_inflate == 0 &&
300 (telnet->mode == LIBTELNET_MODE_CLIENT ||
301 telnet->mode == LIBTELNET_MODE_PROXY)) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400302
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400303 if ((telnet->z_inflate = _init_zlib(telnet, 0))
Sean Middleditch72cc9642009-03-15 11:50:36 -0400304 == 0)
Sean Middleditch9de15982009-03-14 03:35:49 -0400305 break;
Sean Middleditch9de15982009-03-14 03:35:49 -0400306
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400307 /* notify app that compression was enabled */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400308 _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400309
Sean Middleditch9de15982009-03-14 03:35:49 -0400310 /* any remaining bytes in the buffer are compressed.
311 * we have to re-invoke libtelnet_push to get those
312 * bytes inflated and abort trying to process the
313 * remaining compressed bytes in the current _process
314 * buffer argument
315 */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400316 libtelnet_push(telnet, &buffer[start], size - start);
Sean Middleditch9de15982009-03-14 03:35:49 -0400317 return;
318 }
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400319#endif /* HAVE_ZLIB */
320
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400321 break;
322 /* escaped IAC byte */
323 case LIBTELNET_IAC:
324 /* push IAC into buffer */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400325 if (_buffer_byte(telnet, LIBTELNET_IAC) !=
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400326 LIBTELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400327 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400328 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400329 } else {
Sean Middleditchda0e6952009-03-15 13:28:09 -0400330 telnet->state = LIBTELNET_STATE_SB_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400331 }
332 break;
333 /* something else -- protocol error */
334 default:
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400335 _error(telnet, __FILE__, __LINE__, LIBTELNET_EPROTOCOL,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400336 "unexpected byte after IAC inside SB: %d",
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400337 byte);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400338 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400339 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400340 break;
341 }
342 break;
343 }
344 }
345
346 /* pass through any remaining bytes */
Sean Middleditchd30fd572009-03-14 12:55:17 -0400347 if (telnet->state == LIBTELNET_STATE_DATA && i != start)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400348 _event(telnet, LIBTELNET_EV_DATA, 0, 0, buffer + start, i - start);
Sean Middleditch29144852009-03-12 23:14:47 -0400349}
350
Sean Middleditch9de15982009-03-14 03:35:49 -0400351/* push a bytes into the state tracker */
352void libtelnet_push(struct libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400353 unsigned int size) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400354#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400355 /* if we have an inflate (decompression) zlib stream, use it */
356 if (telnet->z_inflate != 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400357 unsigned char inflate_buffer[4096];
358 int rs;
359
360 /* initialize zlib state */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400361 telnet->z_inflate->next_in = buffer;
362 telnet->z_inflate->avail_in = size;
363 telnet->z_inflate->next_out = inflate_buffer;
364 telnet->z_inflate->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400365
366 /* inflate until buffer exhausted and all output is produced */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400367 while (telnet->z_inflate->avail_in > 0 || telnet->z_inflate->avail_out == 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400368 /* reset output buffer */
369
370 /* decompress */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400371 rs = inflate(telnet->z_inflate, Z_SYNC_FLUSH);
Sean Middleditch9de15982009-03-14 03:35:49 -0400372
373 /* process the decompressed bytes on success */
374 if (rs == Z_OK || rs == Z_STREAM_END)
375 _process(telnet, inflate_buffer, sizeof(inflate_buffer) -
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400376 telnet->z_inflate->avail_out);
Sean Middleditch9de15982009-03-14 03:35:49 -0400377 else
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400378 ERROR_ZLIB(telnet, rs, "inflate() failed");
Sean Middleditch9de15982009-03-14 03:35:49 -0400379
380 /* prepare output buffer for next run */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400381 telnet->z_inflate->next_out = inflate_buffer;
382 telnet->z_inflate->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400383
384 /* on error (or on end of stream) disable further inflation */
385 if (rs != Z_OK) {
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400386 _event(telnet, LIBTELNET_EV_COMPRESS, 0, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400387
Sean Middleditch72cc9642009-03-15 11:50:36 -0400388 inflateEnd(telnet->z_inflate);
389 free(telnet->z_inflate);
390 telnet->z_inflate = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400391 break;
392 }
393 }
394
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400395 /* COMPRESS2 is not negotiated, just process */
396 } else
397#endif /* HAVE_ZLIB */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400398 _process(telnet, buffer, size);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400399}
400
401static void _send(struct libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400402 unsigned int size) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400403#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400404 /* if we have a deflate (compression) zlib box, use it */
405 if (telnet->z_deflate != 0) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400406 unsigned char deflate_buffer[1024];
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400407 int rs;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400408
Sean Middleditch72cc9642009-03-15 11:50:36 -0400409 /* initialize z_deflate state */
410 telnet->z_deflate->next_in = buffer;
411 telnet->z_deflate->avail_in = size;
412 telnet->z_deflate->next_out = deflate_buffer;
413 telnet->z_deflate->avail_out = sizeof(deflate_buffer);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400414
415 /* deflate until buffer exhausted and all output is produced */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400416 while (telnet->z_deflate->avail_in > 0 || telnet->z_deflate->avail_out == 0) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400417 /* compress */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400418 if ((rs = deflate(telnet->z_deflate, Z_SYNC_FLUSH)) != Z_OK) {
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400419 ERROR_ZLIB(telnet, rs, "deflate() failed");
Sean Middleditch72cc9642009-03-15 11:50:36 -0400420 deflateEnd(telnet->z_deflate);
421 free(telnet->z_deflate);
422 telnet->z_deflate = 0;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400423 break;
424 }
425
Sean Middleditch637df7f2009-03-15 12:57:32 -0400426 _event(telnet, LIBTELNET_EV_SEND, 0, 0, deflate_buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400427 sizeof(deflate_buffer) - telnet->z_deflate->avail_out);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400428
429 /* prepare output buffer for next run */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400430 telnet->z_deflate->next_out = deflate_buffer;
431 telnet->z_deflate->avail_out = sizeof(deflate_buffer);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400432 }
433
434 /* COMPRESS2 is not negotiated, just send */
435 } else
436#endif /* HAVE_ZLIB */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400437 _event(telnet, LIBTELNET_EV_SEND, 0, 0, buffer, size);
Sean Middleditch9de15982009-03-14 03:35:49 -0400438}
439
Sean Middleditch29144852009-03-12 23:14:47 -0400440/* send an iac command */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400441void libtelnet_send_command(struct libtelnet_t *telnet, unsigned char cmd) {
Sean Middleditchf9cebec2009-03-13 20:17:31 -0400442 unsigned char bytes[2] = { LIBTELNET_IAC, cmd };
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400443 _send(telnet, bytes, 2);
Sean Middleditch29144852009-03-12 23:14:47 -0400444}
445
446/* send negotiation */
447void libtelnet_send_negotiate(struct libtelnet_t *telnet, unsigned char cmd,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400448 unsigned char opt) {
Sean Middleditchf9cebec2009-03-13 20:17:31 -0400449 unsigned char bytes[3] = { LIBTELNET_IAC, cmd, opt };
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400450 _send(telnet, bytes, 3);
Sean Middleditch29144852009-03-12 23:14:47 -0400451}
452
453/* send non-command data (escapes IAC bytes) */
454void libtelnet_send_data(struct libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400455 unsigned int size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400456 unsigned int i, l;
Sean Middleditch29144852009-03-12 23:14:47 -0400457 for (l = i = 0; i != size; ++i) {
458 /* dump prior portion of text, send escaped bytes */
459 if (buffer[i] == LIBTELNET_IAC) {
460 /* dump prior text if any */
461 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400462 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400463 l = i + 1;
464
465 /* send escape */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400466 libtelnet_send_command(telnet, LIBTELNET_IAC);
Sean Middleditch29144852009-03-12 23:14:47 -0400467 }
468 }
469
470 /* send whatever portion of buffer is left */
471 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400472 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400473}
474
475/* send sub-request */
Sean Middleditch6b372882009-03-14 13:06:47 -0400476void libtelnet_send_subnegotiation(struct libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400477 unsigned char opt, unsigned char *buffer, unsigned int size) {
478 libtelnet_send_command(telnet, LIBTELNET_SB);
479 libtelnet_send_data(telnet, &opt, 1);
480 libtelnet_send_data(telnet, buffer, size);
481 libtelnet_send_command(telnet, LIBTELNET_SE);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400482
483#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400484 /* if we're a proxy and we just sent the COMPRESS2 marker, we must
485 * make sure all further data is compressed if not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400486 */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400487 if (telnet->mode == LIBTELNET_MODE_PROXY &&
488 telnet->z_deflate == 0 &&
489 opt == LIBTELNET_TELOPT_COMPRESS2) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400490
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400491 if ((telnet->z_deflate = _init_zlib(telnet, 1)) == 0)
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400492 return;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400493
494 /* notify app that compression was enabled */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400495 _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400496 }
497#endif /* HAVE_ZLIB */
Sean Middleditch29144852009-03-12 23:14:47 -0400498}
Sean Middleditch124a1c22009-03-15 13:20:03 -0400499
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400500void libtelnet_begin_compress2(struct libtelnet_t *telnet) {
Sean Middleditch124a1c22009-03-15 13:20:03 -0400501#ifdef HAVE_ZLIB
502 z_stream *zlib;
503
504 /* don't do this if we've already got a compression stream */
505 if (telnet->z_deflate != 0)
506 return;
507
508 /* only supported by servers */
509 if (telnet->mode != LIBTELNET_MODE_SERVER)
510 return;
511
512 /* attempt to create output stream first, bail if we can't */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400513 if ((zlib = _init_zlib(telnet, 1)) == 0)
Sean Middleditch124a1c22009-03-15 13:20:03 -0400514 return;
515
516 /* send compression marker */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400517 libtelnet_send_subnegotiation(telnet, LIBTELNET_TELOPT_COMPRESS2, 0, 0);
Sean Middleditch124a1c22009-03-15 13:20:03 -0400518
519 /* set our deflate stream */
520 telnet->z_deflate = zlib;
521#endif /* HAVE_ZLIB */
522}