blob: 1f9e60681a9b46bc6d70c923af17019aab06f29c [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 Middleditch4d9444d2009-03-13 22:48:05 -040025/* buffer sizes */
26static const unsigned int _buffer_sizes[] = {
27 0,
28 512,
29 2048,
30 8192,
31 16384,
32};
Sean Middleditch812358d2009-03-15 23:24:03 -040033static const unsigned int _buffer_sizes_count = sizeof(_buffer_sizes) /
34 sizeof(_buffer_sizes[0]);
Sean Middleditch4d9444d2009-03-13 22:48:05 -040035
Sean Middleditch637df7f2009-03-15 12:57:32 -040036/* event dispatch helper */
Sean Middleditch812358d2009-03-15 23:24:03 -040037static void _event(libtelnet_t *telnet, libtelnet_event_type_t type,
38 unsigned char command, unsigned char telopt, unsigned char *buffer,
39 unsigned int size) {
40 libtelnet_event_t ev;
Sean Middleditch637df7f2009-03-15 12:57:32 -040041 ev.type = type;
42 ev.command = command;
43 ev.telopt = telopt;
44 ev.buffer = buffer;
45 ev.size = size;
46
Sean Middleditch9f79cc52009-03-15 13:39:24 -040047 telnet->eh(telnet, &ev, telnet->ud);
Sean Middleditch637df7f2009-03-15 12:57:32 -040048}
49
Sean Middleditchd922c6f2009-03-14 22:35:01 -040050/* error generation function */
Sean Middleditch812358d2009-03-15 23:24:03 -040051static void _error(libtelnet_t *telnet, unsigned line, const char* func,
52 libtelnet_error_t err, int fatal, const char *fmt, ...) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -040053 char buffer[512];
54 va_list va;
55
56 /* format error intro */
Sean Middleditch812358d2009-03-15 23:24:03 -040057 snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", __FILE__, line, func);
Sean Middleditchd922c6f2009-03-14 22:35:01 -040058
59 va_start(va, fmt);
60 vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
61 fmt, va);
62 va_end(va);
63
Sean Middleditch16992272009-03-15 19:42:03 -040064 _event(telnet, fatal ? LIBTELNET_EV_ERROR : LIBTELNET_EV_WARNING, err,
Sean Middleditch5cf66662009-03-15 20:02:14 -040065 0, (unsigned char *)buffer, strlen(buffer));
Sean Middleditchd922c6f2009-03-14 22:35:01 -040066}
67
Sean Middleditch72cc9642009-03-15 11:50:36 -040068/* initialize the zlib box for a telnet box; if deflate is non-zero, it
69 * initializes zlib for delating (compression), otherwise for inflating
70 * (decompression)
71 */
Sean Middleditch812358d2009-03-15 23:24:03 -040072z_stream *_init_zlib(libtelnet_t *telnet, int deflate, int err_fatal) {
Sean Middleditch72cc9642009-03-15 11:50:36 -040073 z_stream *zlib;
74 int rs;
75
76 /* allocate zstream box */
77 if ((zlib = (z_stream *)calloc(1, sizeof(z_stream)))
78 == 0) {
Sean Middleditch16992272009-03-15 19:42:03 -040079 _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, err_fatal,
80 "malloc() failed: %s", strerror(errno));
Sean Middleditch72cc9642009-03-15 11:50:36 -040081 return 0;
82 }
83
84 /* initialize */
85 if (deflate) {
86 if ((rs = deflateInit(zlib, Z_DEFAULT_COMPRESSION)) != Z_OK) {
87 free(zlib);
Sean Middleditch16992272009-03-15 19:42:03 -040088 _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, err_fatal,
89 "deflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -040090 return 0;
91 }
92 } else {
93 if ((rs = inflateInit(zlib)) != Z_OK) {
94 free(zlib);
Sean Middleditch16992272009-03-15 19:42:03 -040095 _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, err_fatal,
96 "inflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -040097 return 0;
98 }
99 }
100
101 return zlib;
102}
103
Sean Middleditch29144852009-03-12 23:14:47 -0400104/* initialize a telnet state tracker */
Sean Middleditch812358d2009-03-15 23:24:03 -0400105void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh,
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400106 unsigned char flags, void *user_data) {
Sean Middleditch812358d2009-03-15 23:24:03 -0400107 memset(telnet, 0, sizeof(libtelnet_t));
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400108 telnet->ud = user_data;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400109 telnet->eh = eh;
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400110 telnet->flags = flags;
Sean Middleditch29144852009-03-12 23:14:47 -0400111}
112
113/* free up any memory allocated by a state tracker */
Sean Middleditch812358d2009-03-15 23:24:03 -0400114void libtelnet_free(libtelnet_t *telnet) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400115 /* free sub-request buffer */
Sean Middleditch51ad6792009-03-13 20:15:59 -0400116 if (telnet->buffer != 0) {
Sean Middleditch29144852009-03-12 23:14:47 -0400117 free(telnet->buffer);
118 telnet->buffer = 0;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400119 telnet->size = 0;
120 telnet->length = 0;
Sean Middleditch29144852009-03-12 23:14:47 -0400121 }
Sean Middleditch9de15982009-03-14 03:35:49 -0400122
Sean Middleditch72cc9642009-03-15 11:50:36 -0400123 /* free zlib box(es) */
124 if (telnet->z_inflate != 0) {
125 inflateEnd(telnet->z_inflate);
126 free(telnet->z_inflate);
127 telnet->z_inflate = 0;
128 }
129 if (telnet->z_deflate != 0) {
130 deflateEnd(telnet->z_deflate);
131 free(telnet->z_deflate);
132 telnet->z_deflate = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400133 }
Sean Middleditch29144852009-03-12 23:14:47 -0400134}
135
Sean Middleditch51ad6792009-03-13 20:15:59 -0400136/* push a byte into the telnet buffer */
Sean Middleditch812358d2009-03-15 23:24:03 -0400137static libtelnet_error_t _buffer_byte(libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400138 unsigned char byte) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400139 unsigned char *new_buffer;
140 int i;
141
Sean Middleditch51ad6792009-03-13 20:15:59 -0400142 /* check if we're out of room */
143 if (telnet->length == telnet->size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400144 /* find the next buffer size */
145 for (i = 0; i != _buffer_sizes_count; ++i) {
146 if (_buffer_sizes[i] == telnet->size)
147 break;
148 }
149
150 /* overflow -- can't grow any more */
151 if (i >= _buffer_sizes_count - 1) {
Sean Middleditch16992272009-03-15 19:42:03 -0400152 _error(telnet, __LINE__, __func__, LIBTELNET_EOVERFLOW, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400153 "subnegotiation buffer size limit reached");
Sean Middleditch51ad6792009-03-13 20:15:59 -0400154 libtelnet_free(telnet);
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400155 return LIBTELNET_EOVERFLOW;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400156 }
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400157
158 /* (re)allocate buffer */
159 new_buffer = (unsigned char *)realloc(telnet->buffer,
160 _buffer_sizes[i + 1]);
161 if (new_buffer == 0) {
Sean Middleditch16992272009-03-15 19:42:03 -0400162 _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, 0,
163 "realloc() failed");
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400164 libtelnet_free(telnet);
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400165 return LIBTELNET_ENOMEM;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400166 }
167
168 telnet->buffer = new_buffer;
169 telnet->size = _buffer_sizes[i + 1];
Sean Middleditch51ad6792009-03-13 20:15:59 -0400170 }
171
172 /* push the byte, all set */
173 telnet->buffer[telnet->length++] = byte;
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400174 return LIBTELNET_EOK;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400175}
176
Sean Middleditch812358d2009-03-15 23:24:03 -0400177static void _process(libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400178 unsigned int size) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400179 unsigned char byte;
180 unsigned int i, start;
181 for (i = start = 0; i != size; ++i) {
182 byte = buffer[i];
183 switch (telnet->state) {
184 /* regular data */
Sean Middleditch9de15982009-03-14 03:35:49 -0400185 case LIBTELNET_STATE_DATA:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400186 /* on an IAC byte, pass through all pending bytes and
187 * switch states */
188 if (byte == LIBTELNET_IAC) {
189 if (i != start)
Sean Middleditch637df7f2009-03-15 12:57:32 -0400190 _event(telnet, LIBTELNET_EV_DATA, 0, 0, &buffer[start],
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400191 i - start);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400192 telnet->state = LIBTELNET_STATE_IAC;
193 }
194 break;
195
196 /* IAC command */
197 case LIBTELNET_STATE_IAC:
198 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400199 /* subnegotiation */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400200 case LIBTELNET_SB:
201 telnet->state = LIBTELNET_STATE_SB;
202 break;
203 /* negotiation commands */
204 case LIBTELNET_WILL:
205 telnet->state = LIBTELNET_STATE_WILL;
206 break;
207 case LIBTELNET_WONT:
208 telnet->state = LIBTELNET_STATE_WONT;
209 break;
210 case LIBTELNET_DO:
211 telnet->state = LIBTELNET_STATE_DO;
212 break;
213 case LIBTELNET_DONT:
214 telnet->state = LIBTELNET_STATE_DONT;
215 break;
216 /* IAC escaping */
217 case LIBTELNET_IAC:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400218 _event(telnet, LIBTELNET_EV_DATA, 0, 0, &byte, 1);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400219 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400220 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400221 break;
222 /* some other command */
223 default:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400224 _event(telnet, LIBTELNET_EV_IAC, byte, 0, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400225 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400226 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400227 }
228 break;
229
230 /* negotiation commands */
231 case LIBTELNET_STATE_DO:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400232 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_DO, byte, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400233 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400234 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400235 break;
236 case LIBTELNET_STATE_DONT:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400237 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_DONT, byte, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400238 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400239 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400240 break;
241 case LIBTELNET_STATE_WILL:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400242 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_WILL, byte, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400243 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400244 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400245 break;
246 case LIBTELNET_STATE_WONT:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400247 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_WONT, byte, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400248 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400249 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400250 break;
251
Sean Middleditchda0e6952009-03-15 13:28:09 -0400252 /* subnegotiation -- determine subnegotiation telopt */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400253 case LIBTELNET_STATE_SB:
Sean Middleditchda0e6952009-03-15 13:28:09 -0400254 telnet->sb_telopt = byte;
255 telnet->length = 0;
256 telnet->state = LIBTELNET_STATE_SB_DATA;
257 break;
258
259 /* subnegotiation -- buffer bytes until end request */
260 case LIBTELNET_STATE_SB_DATA:
Sean Middleditch6b372882009-03-14 13:06:47 -0400261 /* IAC command in subnegotiation -- either IAC SE or IAC IAC */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400262 if (byte == LIBTELNET_IAC) {
Sean Middleditchda0e6952009-03-15 13:28:09 -0400263 telnet->state = LIBTELNET_STATE_SB_DATA_IAC;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400264 /* buffer the byte, or bail if we can't */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400265 } else if (_buffer_byte(telnet, byte) != LIBTELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400266 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400267 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400268 }
269 break;
270
Sean Middleditch6b372882009-03-14 13:06:47 -0400271 /* IAC escaping inside a subnegotiation */
Sean Middleditchda0e6952009-03-15 13:28:09 -0400272 case LIBTELNET_STATE_SB_DATA_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400273 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400274 /* end subnegotiation */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400275 case LIBTELNET_SE:
Sean Middleditch9de15982009-03-14 03:35:49 -0400276 /* return to default state */
277 start = i + 1;
278 telnet->state = LIBTELNET_STATE_DATA;
279
Sean Middleditch9de15982009-03-14 03:35:49 -0400280 /* invoke callback */
Sean Middleditch637df7f2009-03-15 12:57:32 -0400281 _event(telnet, LIBTELNET_EV_SUBNEGOTIATION, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400282 telnet->sb_telopt, telnet->buffer, telnet->length);
Sean Middleditch9de15982009-03-14 03:35:49 -0400283
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400284#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400285 /* if we are a client or a proxy and just received the
286 * COMPRESS2 begin marker, setup our zlib box and start
287 * handling the compressed stream if it's not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400288 */
Sean Middleditchda0e6952009-03-15 13:28:09 -0400289 if (telnet->sb_telopt == LIBTELNET_TELOPT_COMPRESS2 &&
Sean Middleditch72cc9642009-03-15 11:50:36 -0400290 telnet->z_inflate == 0 &&
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400291 telnet->flags & LIBTELNET_FLAG_PROXY) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400292
Sean Middleditch5cf66662009-03-15 20:02:14 -0400293 if ((telnet->z_inflate = _init_zlib(telnet, 0, 1)) == 0)
Sean Middleditch9de15982009-03-14 03:35:49 -0400294 break;
Sean Middleditch9de15982009-03-14 03:35:49 -0400295
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400296 /* notify app that compression was enabled */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400297 _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400298
Sean Middleditch9de15982009-03-14 03:35:49 -0400299 /* any remaining bytes in the buffer are compressed.
300 * we have to re-invoke libtelnet_push to get those
301 * bytes inflated and abort trying to process the
302 * remaining compressed bytes in the current _process
303 * buffer argument
304 */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400305 libtelnet_push(telnet, &buffer[start], size - start);
Sean Middleditch9de15982009-03-14 03:35:49 -0400306 return;
307 }
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400308#endif /* HAVE_ZLIB */
309
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400310 break;
311 /* escaped IAC byte */
312 case LIBTELNET_IAC:
313 /* push IAC into buffer */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400314 if (_buffer_byte(telnet, LIBTELNET_IAC) !=
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400315 LIBTELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400316 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400317 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400318 } else {
Sean Middleditchda0e6952009-03-15 13:28:09 -0400319 telnet->state = LIBTELNET_STATE_SB_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400320 }
321 break;
322 /* something else -- protocol error */
323 default:
Sean Middleditch16992272009-03-15 19:42:03 -0400324 _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400325 "unexpected byte after IAC inside SB: %d",
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400326 byte);
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 break;
330 }
331 break;
332 }
333 }
334
335 /* pass through any remaining bytes */
Sean Middleditchd30fd572009-03-14 12:55:17 -0400336 if (telnet->state == LIBTELNET_STATE_DATA && i != start)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400337 _event(telnet, LIBTELNET_EV_DATA, 0, 0, buffer + start, i - start);
Sean Middleditch29144852009-03-12 23:14:47 -0400338}
339
Sean Middleditch9de15982009-03-14 03:35:49 -0400340/* push a bytes into the state tracker */
Sean Middleditch812358d2009-03-15 23:24:03 -0400341void libtelnet_push(libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400342 unsigned int size) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400343#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400344 /* if we have an inflate (decompression) zlib stream, use it */
345 if (telnet->z_inflate != 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400346 unsigned char inflate_buffer[4096];
347 int rs;
348
349 /* initialize zlib state */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400350 telnet->z_inflate->next_in = buffer;
351 telnet->z_inflate->avail_in = size;
352 telnet->z_inflate->next_out = inflate_buffer;
353 telnet->z_inflate->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400354
355 /* inflate until buffer exhausted and all output is produced */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400356 while (telnet->z_inflate->avail_in > 0 || telnet->z_inflate->avail_out == 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400357 /* reset output buffer */
358
359 /* decompress */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400360 rs = inflate(telnet->z_inflate, Z_SYNC_FLUSH);
Sean Middleditch9de15982009-03-14 03:35:49 -0400361
362 /* process the decompressed bytes on success */
363 if (rs == Z_OK || rs == Z_STREAM_END)
364 _process(telnet, inflate_buffer, sizeof(inflate_buffer) -
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400365 telnet->z_inflate->avail_out);
Sean Middleditch9de15982009-03-14 03:35:49 -0400366 else
Sean Middleditch16992272009-03-15 19:42:03 -0400367 _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1,
368 "inflate() failed: %s", zError(rs));
Sean Middleditch9de15982009-03-14 03:35:49 -0400369
370 /* prepare output buffer for next run */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400371 telnet->z_inflate->next_out = inflate_buffer;
372 telnet->z_inflate->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400373
374 /* on error (or on end of stream) disable further inflation */
375 if (rs != Z_OK) {
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400376 _event(telnet, LIBTELNET_EV_COMPRESS, 0, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400377
Sean Middleditch72cc9642009-03-15 11:50:36 -0400378 inflateEnd(telnet->z_inflate);
379 free(telnet->z_inflate);
380 telnet->z_inflate = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400381 break;
382 }
383 }
384
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400385 /* COMPRESS2 is not negotiated, just process */
386 } else
387#endif /* HAVE_ZLIB */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400388 _process(telnet, buffer, size);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400389}
390
Sean Middleditch812358d2009-03-15 23:24:03 -0400391static void _send(libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400392 unsigned int size) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400393#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400394 /* if we have a deflate (compression) zlib box, use it */
395 if (telnet->z_deflate != 0) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400396 unsigned char deflate_buffer[1024];
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400397 int rs;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400398
Sean Middleditch72cc9642009-03-15 11:50:36 -0400399 /* initialize z_deflate state */
400 telnet->z_deflate->next_in = buffer;
401 telnet->z_deflate->avail_in = size;
402 telnet->z_deflate->next_out = deflate_buffer;
403 telnet->z_deflate->avail_out = sizeof(deflate_buffer);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400404
405 /* deflate until buffer exhausted and all output is produced */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400406 while (telnet->z_deflate->avail_in > 0 || telnet->z_deflate->avail_out == 0) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400407 /* compress */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400408 if ((rs = deflate(telnet->z_deflate, Z_SYNC_FLUSH)) != Z_OK) {
Sean Middleditch16992272009-03-15 19:42:03 -0400409 _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1,
410 "deflate() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400411 deflateEnd(telnet->z_deflate);
412 free(telnet->z_deflate);
413 telnet->z_deflate = 0;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400414 break;
415 }
416
Sean Middleditch637df7f2009-03-15 12:57:32 -0400417 _event(telnet, LIBTELNET_EV_SEND, 0, 0, deflate_buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400418 sizeof(deflate_buffer) - telnet->z_deflate->avail_out);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400419
420 /* prepare output buffer for next run */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400421 telnet->z_deflate->next_out = deflate_buffer;
422 telnet->z_deflate->avail_out = sizeof(deflate_buffer);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400423 }
424
425 /* COMPRESS2 is not negotiated, just send */
426 } else
427#endif /* HAVE_ZLIB */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400428 _event(telnet, LIBTELNET_EV_SEND, 0, 0, buffer, size);
Sean Middleditch9de15982009-03-14 03:35:49 -0400429}
430
Sean Middleditch29144852009-03-12 23:14:47 -0400431/* send an iac command */
Sean Middleditch812358d2009-03-15 23:24:03 -0400432void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd) {
Sean Middleditchf9cebec2009-03-13 20:17:31 -0400433 unsigned char bytes[2] = { LIBTELNET_IAC, cmd };
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400434 _send(telnet, bytes, 2);
Sean Middleditch29144852009-03-12 23:14:47 -0400435}
436
437/* send negotiation */
Sean Middleditch812358d2009-03-15 23:24:03 -0400438void libtelnet_send_negotiate(libtelnet_t *telnet, unsigned char cmd,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400439 unsigned char opt) {
Sean Middleditchf9cebec2009-03-13 20:17:31 -0400440 unsigned char bytes[3] = { LIBTELNET_IAC, cmd, opt };
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400441 _send(telnet, bytes, 3);
Sean Middleditch29144852009-03-12 23:14:47 -0400442}
443
444/* send non-command data (escapes IAC bytes) */
Sean Middleditch812358d2009-03-15 23:24:03 -0400445void libtelnet_send_data(libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400446 unsigned int size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400447 unsigned int i, l;
Sean Middleditch29144852009-03-12 23:14:47 -0400448 for (l = i = 0; i != size; ++i) {
449 /* dump prior portion of text, send escaped bytes */
450 if (buffer[i] == LIBTELNET_IAC) {
451 /* dump prior text if any */
452 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400453 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400454 l = i + 1;
455
456 /* send escape */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400457 libtelnet_send_command(telnet, LIBTELNET_IAC);
Sean Middleditch29144852009-03-12 23:14:47 -0400458 }
459 }
460
461 /* send whatever portion of buffer is left */
462 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400463 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400464}
465
466/* send sub-request */
Sean Middleditch812358d2009-03-15 23:24:03 -0400467void libtelnet_send_subnegotiation(libtelnet_t *telnet, unsigned char opt,
468 unsigned char *buffer, unsigned int size) {
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400469 libtelnet_send_command(telnet, LIBTELNET_SB);
470 libtelnet_send_data(telnet, &opt, 1);
471 libtelnet_send_data(telnet, buffer, size);
472 libtelnet_send_command(telnet, LIBTELNET_SE);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400473
474#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400475 /* if we're a proxy and we just sent the COMPRESS2 marker, we must
476 * make sure all further data is compressed if not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400477 */
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400478 if (telnet->flags & LIBTELNET_FLAG_PROXY &&
Sean Middleditch72cc9642009-03-15 11:50:36 -0400479 telnet->z_deflate == 0 &&
480 opt == LIBTELNET_TELOPT_COMPRESS2) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400481
Sean Middleditch16992272009-03-15 19:42:03 -0400482 if ((telnet->z_deflate = _init_zlib(telnet, 1, 1)) == 0)
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400483 return;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400484
485 /* notify app that compression was enabled */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400486 _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400487 }
488#endif /* HAVE_ZLIB */
Sean Middleditch29144852009-03-12 23:14:47 -0400489}
Sean Middleditch124a1c22009-03-15 13:20:03 -0400490
Sean Middleditch812358d2009-03-15 23:24:03 -0400491void libtelnet_begin_compress2(libtelnet_t *telnet) {
Sean Middleditch124a1c22009-03-15 13:20:03 -0400492#ifdef HAVE_ZLIB
493 z_stream *zlib;
494
495 /* don't do this if we've already got a compression stream */
Sean Middleditch16992272009-03-15 19:42:03 -0400496 if (telnet->z_deflate != 0) {
497 _error(telnet, __LINE__, __func__, LIBTELNET_EBADVAL, 0,
498 "compression already enabled");
Sean Middleditch124a1c22009-03-15 13:20:03 -0400499 return;
Sean Middleditch16992272009-03-15 19:42:03 -0400500 }
Sean Middleditch124a1c22009-03-15 13:20:03 -0400501
502 /* attempt to create output stream first, bail if we can't */
Sean Middleditch16992272009-03-15 19:42:03 -0400503 if ((zlib = _init_zlib(telnet, 1, 0)) == 0)
Sean Middleditch124a1c22009-03-15 13:20:03 -0400504 return;
505
506 /* send compression marker */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400507 libtelnet_send_subnegotiation(telnet, LIBTELNET_TELOPT_COMPRESS2, 0, 0);
Sean Middleditch124a1c22009-03-15 13:20:03 -0400508
509 /* set our deflate stream */
510 telnet->z_deflate = zlib;
511#endif /* HAVE_ZLIB */
512}