blob: 4d53d2ad33fa8549ff0d342ae77f9837e61b0cf0 [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};
33static const unsigned int _buffer_sizes_count =
34 sizeof(_buffer_sizes) / sizeof(_buffer_sizes[0]);
35
Sean Middleditch637df7f2009-03-15 12:57:32 -040036/* event dispatch helper */
37static void _event(struct libtelnet_t *telnet,
38 enum libtelnet_event_type_t type, unsigned char command,
Sean Middleditch9f79cc52009-03-15 13:39:24 -040039 unsigned char telopt, unsigned char *buffer, unsigned int size) {
Sean Middleditch637df7f2009-03-15 12:57:32 -040040 struct libtelnet_event_t ev;
41 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 Middleditch16992272009-03-15 19:42:03 -040051static void _error(struct libtelnet_t *telnet, unsigned line, const char* func,
52 enum 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 Middleditch16992272009-03-15 19:42:03 -040057 snprintf(buffer, sizeof(buffer), "%s:%u in %s: ",
58 __FILE__, line, func);
Sean Middleditchd922c6f2009-03-14 22:35:01 -040059
60 va_start(va, fmt);
61 vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
62 fmt, va);
63 va_end(va);
64
Sean Middleditch16992272009-03-15 19:42:03 -040065 _event(telnet, fatal ? LIBTELNET_EV_ERROR : LIBTELNET_EV_WARNING, err,
Sean Middleditch5cf66662009-03-15 20:02:14 -040066 0, (unsigned char *)buffer, strlen(buffer));
Sean Middleditchd922c6f2009-03-14 22:35:01 -040067}
68
Sean Middleditch72cc9642009-03-15 11:50:36 -040069/* initialize the zlib box for a telnet box; if deflate is non-zero, it
70 * initializes zlib for delating (compression), otherwise for inflating
71 * (decompression)
72 */
Sean Middleditch16992272009-03-15 19:42:03 -040073z_stream *_init_zlib(struct libtelnet_t *telnet, int deflate, int err_fatal) {
Sean Middleditch72cc9642009-03-15 11:50:36 -040074 z_stream *zlib;
75 int rs;
76
77 /* allocate zstream box */
78 if ((zlib = (z_stream *)calloc(1, sizeof(z_stream)))
79 == 0) {
Sean Middleditch16992272009-03-15 19:42:03 -040080 _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, err_fatal,
81 "malloc() failed: %s", strerror(errno));
Sean Middleditch72cc9642009-03-15 11:50:36 -040082 return 0;
83 }
84
85 /* initialize */
86 if (deflate) {
87 if ((rs = deflateInit(zlib, Z_DEFAULT_COMPRESSION)) != Z_OK) {
88 free(zlib);
Sean Middleditch16992272009-03-15 19:42:03 -040089 _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, err_fatal,
90 "deflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -040091 return 0;
92 }
93 } else {
94 if ((rs = inflateInit(zlib)) != Z_OK) {
95 free(zlib);
Sean Middleditch16992272009-03-15 19:42:03 -040096 _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, err_fatal,
97 "inflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -040098 return 0;
99 }
100 }
101
102 return zlib;
103}
104
Sean Middleditch29144852009-03-12 23:14:47 -0400105/* initialize a telnet state tracker */
Sean Middleditch637df7f2009-03-15 12:57:32 -0400106void libtelnet_init(struct libtelnet_t *telnet, libtelnet_event_handler_t eh,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400107 enum libtelnet_mode_t mode, void *user_data) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400108 memset(telnet, 0, sizeof(struct libtelnet_t));
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400109 telnet->ud = user_data;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400110 telnet->eh = eh;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400111 telnet->mode = mode;
Sean Middleditch29144852009-03-12 23:14:47 -0400112}
113
114/* free up any memory allocated by a state tracker */
Sean Middleditch51ad6792009-03-13 20:15:59 -0400115void libtelnet_free(struct libtelnet_t *telnet) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400116 /* free sub-request buffer */
Sean Middleditch51ad6792009-03-13 20:15:59 -0400117 if (telnet->buffer != 0) {
Sean Middleditch29144852009-03-12 23:14:47 -0400118 free(telnet->buffer);
119 telnet->buffer = 0;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400120 telnet->size = 0;
121 telnet->length = 0;
Sean Middleditch29144852009-03-12 23:14:47 -0400122 }
Sean Middleditch9de15982009-03-14 03:35:49 -0400123
Sean Middleditch72cc9642009-03-15 11:50:36 -0400124 /* free zlib box(es) */
125 if (telnet->z_inflate != 0) {
126 inflateEnd(telnet->z_inflate);
127 free(telnet->z_inflate);
128 telnet->z_inflate = 0;
129 }
130 if (telnet->z_deflate != 0) {
131 deflateEnd(telnet->z_deflate);
132 free(telnet->z_deflate);
133 telnet->z_deflate = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400134 }
Sean Middleditch29144852009-03-12 23:14:47 -0400135}
136
Sean Middleditch51ad6792009-03-13 20:15:59 -0400137/* push a byte into the telnet buffer */
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400138static enum libtelnet_error_t _buffer_byte(struct libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400139 unsigned char byte) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400140 unsigned char *new_buffer;
141 int i;
142
Sean Middleditch51ad6792009-03-13 20:15:59 -0400143 /* check if we're out of room */
144 if (telnet->length == telnet->size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400145 /* find the next buffer size */
146 for (i = 0; i != _buffer_sizes_count; ++i) {
147 if (_buffer_sizes[i] == telnet->size)
148 break;
149 }
150
151 /* overflow -- can't grow any more */
152 if (i >= _buffer_sizes_count - 1) {
Sean Middleditch16992272009-03-15 19:42:03 -0400153 _error(telnet, __LINE__, __func__, LIBTELNET_EOVERFLOW, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400154 "subnegotiation buffer size limit reached");
Sean Middleditch51ad6792009-03-13 20:15:59 -0400155 libtelnet_free(telnet);
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400156 return LIBTELNET_EOVERFLOW;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400157 }
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400158
159 /* (re)allocate buffer */
160 new_buffer = (unsigned char *)realloc(telnet->buffer,
161 _buffer_sizes[i + 1]);
162 if (new_buffer == 0) {
Sean Middleditch16992272009-03-15 19:42:03 -0400163 _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, 0,
164 "realloc() failed");
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400165 libtelnet_free(telnet);
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400166 return LIBTELNET_ENOMEM;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400167 }
168
169 telnet->buffer = new_buffer;
170 telnet->size = _buffer_sizes[i + 1];
Sean Middleditch51ad6792009-03-13 20:15:59 -0400171 }
172
173 /* push the byte, all set */
174 telnet->buffer[telnet->length++] = byte;
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400175 return LIBTELNET_EOK;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400176}
177
Sean Middleditch9de15982009-03-14 03:35:49 -0400178static void _process(struct libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400179 unsigned int size) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400180 unsigned char byte;
181 unsigned int i, start;
182 for (i = start = 0; i != size; ++i) {
183 byte = buffer[i];
184 switch (telnet->state) {
185 /* regular data */
Sean Middleditch9de15982009-03-14 03:35:49 -0400186 case LIBTELNET_STATE_DATA:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400187 /* on an IAC byte, pass through all pending bytes and
188 * switch states */
189 if (byte == LIBTELNET_IAC) {
190 if (i != start)
Sean Middleditch637df7f2009-03-15 12:57:32 -0400191 _event(telnet, LIBTELNET_EV_DATA, 0, 0, &buffer[start],
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400192 i - start);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400193 telnet->state = LIBTELNET_STATE_IAC;
194 }
195 break;
196
197 /* IAC command */
198 case LIBTELNET_STATE_IAC:
199 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400200 /* subnegotiation */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400201 case LIBTELNET_SB:
202 telnet->state = LIBTELNET_STATE_SB;
203 break;
204 /* negotiation commands */
205 case LIBTELNET_WILL:
206 telnet->state = LIBTELNET_STATE_WILL;
207 break;
208 case LIBTELNET_WONT:
209 telnet->state = LIBTELNET_STATE_WONT;
210 break;
211 case LIBTELNET_DO:
212 telnet->state = LIBTELNET_STATE_DO;
213 break;
214 case LIBTELNET_DONT:
215 telnet->state = LIBTELNET_STATE_DONT;
216 break;
217 /* IAC escaping */
218 case LIBTELNET_IAC:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400219 _event(telnet, LIBTELNET_EV_DATA, 0, 0, &byte, 1);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400220 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400221 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400222 break;
223 /* some other command */
224 default:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400225 _event(telnet, LIBTELNET_EV_IAC, byte, 0, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400226 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400227 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400228 }
229 break;
230
231 /* negotiation commands */
232 case LIBTELNET_STATE_DO:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400233 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_DO, byte, 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 break;
237 case LIBTELNET_STATE_DONT:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400238 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_DONT, byte, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400239 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400240 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400241 break;
242 case LIBTELNET_STATE_WILL:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400243 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_WILL, byte, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400244 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400245 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400246 break;
247 case LIBTELNET_STATE_WONT:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400248 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_WONT, byte, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400249 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400250 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400251 break;
252
Sean Middleditchda0e6952009-03-15 13:28:09 -0400253 /* subnegotiation -- determine subnegotiation telopt */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400254 case LIBTELNET_STATE_SB:
Sean Middleditchda0e6952009-03-15 13:28:09 -0400255 telnet->sb_telopt = byte;
256 telnet->length = 0;
257 telnet->state = LIBTELNET_STATE_SB_DATA;
258 break;
259
260 /* subnegotiation -- buffer bytes until end request */
261 case LIBTELNET_STATE_SB_DATA:
Sean Middleditch6b372882009-03-14 13:06:47 -0400262 /* IAC command in subnegotiation -- either IAC SE or IAC IAC */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400263 if (byte == LIBTELNET_IAC) {
Sean Middleditchda0e6952009-03-15 13:28:09 -0400264 telnet->state = LIBTELNET_STATE_SB_DATA_IAC;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400265 /* buffer the byte, or bail if we can't */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400266 } else if (_buffer_byte(telnet, byte) != LIBTELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400267 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400268 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400269 }
270 break;
271
Sean Middleditch6b372882009-03-14 13:06:47 -0400272 /* IAC escaping inside a subnegotiation */
Sean Middleditchda0e6952009-03-15 13:28:09 -0400273 case LIBTELNET_STATE_SB_DATA_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400274 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400275 /* end subnegotiation */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400276 case LIBTELNET_SE:
Sean Middleditch9de15982009-03-14 03:35:49 -0400277 /* return to default state */
278 start = i + 1;
279 telnet->state = LIBTELNET_STATE_DATA;
280
Sean Middleditch9de15982009-03-14 03:35:49 -0400281 /* invoke callback */
Sean Middleditch637df7f2009-03-15 12:57:32 -0400282 _event(telnet, LIBTELNET_EV_SUBNEGOTIATION, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400283 telnet->sb_telopt, telnet->buffer, telnet->length);
Sean Middleditch9de15982009-03-14 03:35:49 -0400284
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400285#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400286 /* if we are a client or a proxy and just received the
287 * COMPRESS2 begin marker, setup our zlib box and start
288 * handling the compressed stream if it's not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400289 */
Sean Middleditchda0e6952009-03-15 13:28:09 -0400290 if (telnet->sb_telopt == LIBTELNET_TELOPT_COMPRESS2 &&
Sean Middleditch72cc9642009-03-15 11:50:36 -0400291 telnet->z_inflate == 0 &&
292 (telnet->mode == LIBTELNET_MODE_CLIENT ||
293 telnet->mode == LIBTELNET_MODE_PROXY)) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400294
Sean Middleditch5cf66662009-03-15 20:02:14 -0400295 if ((telnet->z_inflate = _init_zlib(telnet, 0, 1)) == 0)
Sean Middleditch9de15982009-03-14 03:35:49 -0400296 break;
Sean Middleditch9de15982009-03-14 03:35:49 -0400297
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400298 /* notify app that compression was enabled */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400299 _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400300
Sean Middleditch9de15982009-03-14 03:35:49 -0400301 /* any remaining bytes in the buffer are compressed.
302 * we have to re-invoke libtelnet_push to get those
303 * bytes inflated and abort trying to process the
304 * remaining compressed bytes in the current _process
305 * buffer argument
306 */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400307 libtelnet_push(telnet, &buffer[start], size - start);
Sean Middleditch9de15982009-03-14 03:35:49 -0400308 return;
309 }
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400310#endif /* HAVE_ZLIB */
311
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400312 break;
313 /* escaped IAC byte */
314 case LIBTELNET_IAC:
315 /* push IAC into buffer */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400316 if (_buffer_byte(telnet, LIBTELNET_IAC) !=
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400317 LIBTELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400318 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400319 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400320 } else {
Sean Middleditchda0e6952009-03-15 13:28:09 -0400321 telnet->state = LIBTELNET_STATE_SB_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400322 }
323 break;
324 /* something else -- protocol error */
325 default:
Sean Middleditch16992272009-03-15 19:42:03 -0400326 _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400327 "unexpected byte after IAC inside SB: %d",
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400328 byte);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400329 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400330 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400331 break;
332 }
333 break;
334 }
335 }
336
337 /* pass through any remaining bytes */
Sean Middleditchd30fd572009-03-14 12:55:17 -0400338 if (telnet->state == LIBTELNET_STATE_DATA && i != start)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400339 _event(telnet, LIBTELNET_EV_DATA, 0, 0, buffer + start, i - start);
Sean Middleditch29144852009-03-12 23:14:47 -0400340}
341
Sean Middleditch9de15982009-03-14 03:35:49 -0400342/* push a bytes into the state tracker */
343void libtelnet_push(struct libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400344 unsigned int size) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400345#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400346 /* if we have an inflate (decompression) zlib stream, use it */
347 if (telnet->z_inflate != 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400348 unsigned char inflate_buffer[4096];
349 int rs;
350
351 /* initialize zlib state */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400352 telnet->z_inflate->next_in = buffer;
353 telnet->z_inflate->avail_in = size;
354 telnet->z_inflate->next_out = inflate_buffer;
355 telnet->z_inflate->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400356
357 /* inflate until buffer exhausted and all output is produced */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400358 while (telnet->z_inflate->avail_in > 0 || telnet->z_inflate->avail_out == 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400359 /* reset output buffer */
360
361 /* decompress */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400362 rs = inflate(telnet->z_inflate, Z_SYNC_FLUSH);
Sean Middleditch9de15982009-03-14 03:35:49 -0400363
364 /* process the decompressed bytes on success */
365 if (rs == Z_OK || rs == Z_STREAM_END)
366 _process(telnet, inflate_buffer, sizeof(inflate_buffer) -
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400367 telnet->z_inflate->avail_out);
Sean Middleditch9de15982009-03-14 03:35:49 -0400368 else
Sean Middleditch16992272009-03-15 19:42:03 -0400369 _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1,
370 "inflate() failed: %s", zError(rs));
Sean Middleditch9de15982009-03-14 03:35:49 -0400371
372 /* prepare output buffer for next run */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400373 telnet->z_inflate->next_out = inflate_buffer;
374 telnet->z_inflate->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400375
376 /* on error (or on end of stream) disable further inflation */
377 if (rs != Z_OK) {
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400378 _event(telnet, LIBTELNET_EV_COMPRESS, 0, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400379
Sean Middleditch72cc9642009-03-15 11:50:36 -0400380 inflateEnd(telnet->z_inflate);
381 free(telnet->z_inflate);
382 telnet->z_inflate = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400383 break;
384 }
385 }
386
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400387 /* COMPRESS2 is not negotiated, just process */
388 } else
389#endif /* HAVE_ZLIB */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400390 _process(telnet, buffer, size);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400391}
392
393static void _send(struct libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400394 unsigned int size) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400395#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400396 /* if we have a deflate (compression) zlib box, use it */
397 if (telnet->z_deflate != 0) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400398 unsigned char deflate_buffer[1024];
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400399 int rs;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400400
Sean Middleditch72cc9642009-03-15 11:50:36 -0400401 /* initialize z_deflate state */
402 telnet->z_deflate->next_in = buffer;
403 telnet->z_deflate->avail_in = size;
404 telnet->z_deflate->next_out = deflate_buffer;
405 telnet->z_deflate->avail_out = sizeof(deflate_buffer);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400406
407 /* deflate until buffer exhausted and all output is produced */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400408 while (telnet->z_deflate->avail_in > 0 || telnet->z_deflate->avail_out == 0) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400409 /* compress */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400410 if ((rs = deflate(telnet->z_deflate, Z_SYNC_FLUSH)) != Z_OK) {
Sean Middleditch16992272009-03-15 19:42:03 -0400411 _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1,
412 "deflate() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400413 deflateEnd(telnet->z_deflate);
414 free(telnet->z_deflate);
415 telnet->z_deflate = 0;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400416 break;
417 }
418
Sean Middleditch637df7f2009-03-15 12:57:32 -0400419 _event(telnet, LIBTELNET_EV_SEND, 0, 0, deflate_buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400420 sizeof(deflate_buffer) - telnet->z_deflate->avail_out);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400421
422 /* prepare output buffer for next run */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400423 telnet->z_deflate->next_out = deflate_buffer;
424 telnet->z_deflate->avail_out = sizeof(deflate_buffer);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400425 }
426
427 /* COMPRESS2 is not negotiated, just send */
428 } else
429#endif /* HAVE_ZLIB */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400430 _event(telnet, LIBTELNET_EV_SEND, 0, 0, buffer, size);
Sean Middleditch9de15982009-03-14 03:35:49 -0400431}
432
Sean Middleditch29144852009-03-12 23:14:47 -0400433/* send an iac command */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400434void libtelnet_send_command(struct libtelnet_t *telnet, unsigned char cmd) {
Sean Middleditchf9cebec2009-03-13 20:17:31 -0400435 unsigned char bytes[2] = { LIBTELNET_IAC, cmd };
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400436 _send(telnet, bytes, 2);
Sean Middleditch29144852009-03-12 23:14:47 -0400437}
438
439/* send negotiation */
440void libtelnet_send_negotiate(struct libtelnet_t *telnet, unsigned char cmd,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400441 unsigned char opt) {
Sean Middleditchf9cebec2009-03-13 20:17:31 -0400442 unsigned char bytes[3] = { LIBTELNET_IAC, cmd, opt };
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400443 _send(telnet, bytes, 3);
Sean Middleditch29144852009-03-12 23:14:47 -0400444}
445
446/* send non-command data (escapes IAC bytes) */
447void libtelnet_send_data(struct libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400448 unsigned int size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400449 unsigned int i, l;
Sean Middleditch29144852009-03-12 23:14:47 -0400450 for (l = i = 0; i != size; ++i) {
451 /* dump prior portion of text, send escaped bytes */
452 if (buffer[i] == LIBTELNET_IAC) {
453 /* dump prior text if any */
454 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400455 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400456 l = i + 1;
457
458 /* send escape */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400459 libtelnet_send_command(telnet, LIBTELNET_IAC);
Sean Middleditch29144852009-03-12 23:14:47 -0400460 }
461 }
462
463 /* send whatever portion of buffer is left */
464 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400465 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400466}
467
468/* send sub-request */
Sean Middleditch6b372882009-03-14 13:06:47 -0400469void libtelnet_send_subnegotiation(struct libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400470 unsigned char opt, unsigned char *buffer, unsigned int size) {
471 libtelnet_send_command(telnet, LIBTELNET_SB);
472 libtelnet_send_data(telnet, &opt, 1);
473 libtelnet_send_data(telnet, buffer, size);
474 libtelnet_send_command(telnet, LIBTELNET_SE);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400475
476#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400477 /* if we're a proxy and we just sent the COMPRESS2 marker, we must
478 * make sure all further data is compressed if not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400479 */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400480 if (telnet->mode == LIBTELNET_MODE_PROXY &&
481 telnet->z_deflate == 0 &&
482 opt == LIBTELNET_TELOPT_COMPRESS2) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400483
Sean Middleditch16992272009-03-15 19:42:03 -0400484 if ((telnet->z_deflate = _init_zlib(telnet, 1, 1)) == 0)
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400485 return;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400486
487 /* notify app that compression was enabled */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400488 _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400489 }
490#endif /* HAVE_ZLIB */
Sean Middleditch29144852009-03-12 23:14:47 -0400491}
Sean Middleditch124a1c22009-03-15 13:20:03 -0400492
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400493void libtelnet_begin_compress2(struct libtelnet_t *telnet) {
Sean Middleditch124a1c22009-03-15 13:20:03 -0400494#ifdef HAVE_ZLIB
495 z_stream *zlib;
496
497 /* don't do this if we've already got a compression stream */
Sean Middleditch16992272009-03-15 19:42:03 -0400498 if (telnet->z_deflate != 0) {
499 _error(telnet, __LINE__, __func__, LIBTELNET_EBADVAL, 0,
500 "compression already enabled");
Sean Middleditch124a1c22009-03-15 13:20:03 -0400501 return;
Sean Middleditch16992272009-03-15 19:42:03 -0400502 }
Sean Middleditch124a1c22009-03-15 13:20:03 -0400503
504 /* only supported by servers */
Sean Middleditch16992272009-03-15 19:42:03 -0400505 if (telnet->mode != LIBTELNET_MODE_SERVER) {
506 _error(telnet, __LINE__, __func__, LIBTELNET_EBADVAL, 0,
507 "only supported in SERVER mode");
Sean Middleditch124a1c22009-03-15 13:20:03 -0400508 return;
Sean Middleditch16992272009-03-15 19:42:03 -0400509 }
Sean Middleditch124a1c22009-03-15 13:20:03 -0400510
511 /* attempt to create output stream first, bail if we can't */
Sean Middleditch16992272009-03-15 19:42:03 -0400512 if ((zlib = _init_zlib(telnet, 1, 0)) == 0)
Sean Middleditch124a1c22009-03-15 13:20:03 -0400513 return;
514
515 /* send compression marker */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400516 libtelnet_send_subnegotiation(telnet, LIBTELNET_TELOPT_COMPRESS2, 0, 0);
Sean Middleditch124a1c22009-03-15 13:20:03 -0400517
518 /* set our deflate stream */
519 telnet->z_deflate = zlib;
520#endif /* HAVE_ZLIB */
521}