blob: 8b07f6c3eca4ad55010dfd95167f0d7f317905ab [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 Middleditchfbe93e32009-03-15 23:39:31 -040051static libtelnet_error_t _error(libtelnet_t *telnet, unsigned line,
52 const char* func, libtelnet_error_t err, int fatal, const char *fmt,
53 ...) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -040054 char buffer[512];
55 va_list va;
56
57 /* format error intro */
Sean Middleditch812358d2009-03-15 23:24:03 -040058 snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", __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 Middleditchfbe93e32009-03-15 23:39:31 -040067
68 return err;
Sean Middleditchd922c6f2009-03-14 22:35:01 -040069}
70
Sean Middleditch72cc9642009-03-15 11:50:36 -040071/* initialize the zlib box for a telnet box; if deflate is non-zero, it
72 * initializes zlib for delating (compression), otherwise for inflating
Sean Middleditchfbe93e32009-03-15 23:39:31 -040073 * (decompression). returns LIBTELNET_EOK on success, something else on
74 * failure.
Sean Middleditch72cc9642009-03-15 11:50:36 -040075 */
Sean Middleditchfbe93e32009-03-15 23:39:31 -040076libtelnet_error_t _init_zlib(libtelnet_t *telnet, int deflate, int err_fatal) {
77 z_stream *z;
Sean Middleditch72cc9642009-03-15 11:50:36 -040078 int rs;
79
Sean Middleditchfbe93e32009-03-15 23:39:31 -040080 /* if compression is already enabled, fail loudly */
81 if (telnet->z != 0)
82 return _error(telnet, __LINE__, __func__, LIBTELNET_EBADVAL,
83 err_fatal, "cannot initialize compression twice");
84
Sean Middleditch72cc9642009-03-15 11:50:36 -040085 /* allocate zstream box */
Sean Middleditchfbe93e32009-03-15 23:39:31 -040086 if ((z= (z_stream *)calloc(1, sizeof(z_stream))) == 0)
87 return _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, err_fatal,
Sean Middleditch16992272009-03-15 19:42:03 -040088 "malloc() failed: %s", strerror(errno));
Sean Middleditch72cc9642009-03-15 11:50:36 -040089
90 /* initialize */
91 if (deflate) {
Sean Middleditchfbe93e32009-03-15 23:39:31 -040092 if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) {
93 free(z);
94 return _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS,
95 err_fatal, "deflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -040096 }
Sean Middleditchfbe93e32009-03-15 23:39:31 -040097 telnet->flags |= LIBTELNET_PFLAG_DEFLATE;
Sean Middleditch72cc9642009-03-15 11:50:36 -040098 } else {
Sean Middleditchfbe93e32009-03-15 23:39:31 -040099 if ((rs = inflateInit(z)) != Z_OK) {
100 free(z);
101 return _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS,
102 err_fatal, "inflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400103 }
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400104 telnet->flags &= ~LIBTELNET_PFLAG_DEFLATE;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400105 }
106
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400107 telnet->z = z;
108
109 return LIBTELNET_EOK;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400110}
111
Sean Middleditch29144852009-03-12 23:14:47 -0400112/* initialize a telnet state tracker */
Sean Middleditch812358d2009-03-15 23:24:03 -0400113void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh,
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400114 unsigned char flags, void *user_data) {
Sean Middleditch812358d2009-03-15 23:24:03 -0400115 memset(telnet, 0, sizeof(libtelnet_t));
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400116 telnet->ud = user_data;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400117 telnet->eh = eh;
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400118 telnet->flags = flags;
Sean Middleditch29144852009-03-12 23:14:47 -0400119}
120
121/* free up any memory allocated by a state tracker */
Sean Middleditch812358d2009-03-15 23:24:03 -0400122void libtelnet_free(libtelnet_t *telnet) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400123 /* free sub-request buffer */
Sean Middleditch51ad6792009-03-13 20:15:59 -0400124 if (telnet->buffer != 0) {
Sean Middleditch29144852009-03-12 23:14:47 -0400125 free(telnet->buffer);
126 telnet->buffer = 0;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400127 telnet->size = 0;
128 telnet->length = 0;
Sean Middleditch29144852009-03-12 23:14:47 -0400129 }
Sean Middleditch9de15982009-03-14 03:35:49 -0400130
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400131 /* free zlib box */
132 if (telnet->z != 0) {
133 if (telnet->flags & LIBTELNET_PFLAG_DEFLATE)
134 deflateEnd(telnet->z);
135 else
136 inflateEnd(telnet->z);
137 free(telnet->z);
138 telnet->z = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400139 }
Sean Middleditch29144852009-03-12 23:14:47 -0400140}
141
Sean Middleditch51ad6792009-03-13 20:15:59 -0400142/* push a byte into the telnet buffer */
Sean Middleditch812358d2009-03-15 23:24:03 -0400143static libtelnet_error_t _buffer_byte(libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400144 unsigned char byte) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400145 unsigned char *new_buffer;
146 int i;
147
Sean Middleditch51ad6792009-03-13 20:15:59 -0400148 /* check if we're out of room */
149 if (telnet->length == telnet->size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400150 /* find the next buffer size */
151 for (i = 0; i != _buffer_sizes_count; ++i) {
152 if (_buffer_sizes[i] == telnet->size)
153 break;
154 }
155
156 /* overflow -- can't grow any more */
157 if (i >= _buffer_sizes_count - 1) {
Sean Middleditch16992272009-03-15 19:42:03 -0400158 _error(telnet, __LINE__, __func__, LIBTELNET_EOVERFLOW, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400159 "subnegotiation buffer size limit reached");
Sean Middleditch51ad6792009-03-13 20:15:59 -0400160 libtelnet_free(telnet);
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400161 return LIBTELNET_EOVERFLOW;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400162 }
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400163
164 /* (re)allocate buffer */
165 new_buffer = (unsigned char *)realloc(telnet->buffer,
166 _buffer_sizes[i + 1]);
167 if (new_buffer == 0) {
Sean Middleditch16992272009-03-15 19:42:03 -0400168 _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, 0,
169 "realloc() failed");
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400170 libtelnet_free(telnet);
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400171 return LIBTELNET_ENOMEM;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400172 }
173
174 telnet->buffer = new_buffer;
175 telnet->size = _buffer_sizes[i + 1];
Sean Middleditch51ad6792009-03-13 20:15:59 -0400176 }
177
178 /* push the byte, all set */
179 telnet->buffer[telnet->length++] = byte;
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400180 return LIBTELNET_EOK;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400181}
182
Sean Middleditch812358d2009-03-15 23:24:03 -0400183static void _process(libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400184 unsigned int size) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400185 unsigned char byte;
186 unsigned int i, start;
187 for (i = start = 0; i != size; ++i) {
188 byte = buffer[i];
189 switch (telnet->state) {
190 /* regular data */
Sean Middleditch9de15982009-03-14 03:35:49 -0400191 case LIBTELNET_STATE_DATA:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400192 /* on an IAC byte, pass through all pending bytes and
193 * switch states */
194 if (byte == LIBTELNET_IAC) {
195 if (i != start)
Sean Middleditch637df7f2009-03-15 12:57:32 -0400196 _event(telnet, LIBTELNET_EV_DATA, 0, 0, &buffer[start],
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400197 i - start);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400198 telnet->state = LIBTELNET_STATE_IAC;
199 }
200 break;
201
202 /* IAC command */
203 case LIBTELNET_STATE_IAC:
204 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400205 /* subnegotiation */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400206 case LIBTELNET_SB:
207 telnet->state = LIBTELNET_STATE_SB;
208 break;
209 /* negotiation commands */
210 case LIBTELNET_WILL:
211 telnet->state = LIBTELNET_STATE_WILL;
212 break;
213 case LIBTELNET_WONT:
214 telnet->state = LIBTELNET_STATE_WONT;
215 break;
216 case LIBTELNET_DO:
217 telnet->state = LIBTELNET_STATE_DO;
218 break;
219 case LIBTELNET_DONT:
220 telnet->state = LIBTELNET_STATE_DONT;
221 break;
222 /* IAC escaping */
223 case LIBTELNET_IAC:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400224 _event(telnet, LIBTELNET_EV_DATA, 0, 0, &byte, 1);
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 break;
228 /* some other command */
229 default:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400230 _event(telnet, LIBTELNET_EV_IAC, byte, 0, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400231 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400232 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400233 }
234 break;
235
236 /* negotiation commands */
237 case LIBTELNET_STATE_DO:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400238 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_DO, 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_DONT:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400243 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_DONT, 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_WILL:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400248 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_WILL, 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 case LIBTELNET_STATE_WONT:
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400253 _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_WONT, byte, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400254 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400255 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400256 break;
257
Sean Middleditchda0e6952009-03-15 13:28:09 -0400258 /* subnegotiation -- determine subnegotiation telopt */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400259 case LIBTELNET_STATE_SB:
Sean Middleditchda0e6952009-03-15 13:28:09 -0400260 telnet->sb_telopt = byte;
261 telnet->length = 0;
262 telnet->state = LIBTELNET_STATE_SB_DATA;
263 break;
264
265 /* subnegotiation -- buffer bytes until end request */
266 case LIBTELNET_STATE_SB_DATA:
Sean Middleditch6b372882009-03-14 13:06:47 -0400267 /* IAC command in subnegotiation -- either IAC SE or IAC IAC */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400268 if (byte == LIBTELNET_IAC) {
Sean Middleditchda0e6952009-03-15 13:28:09 -0400269 telnet->state = LIBTELNET_STATE_SB_DATA_IAC;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400270 /* buffer the byte, or bail if we can't */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400271 } else if (_buffer_byte(telnet, byte) != LIBTELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400272 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400273 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400274 }
275 break;
276
Sean Middleditch6b372882009-03-14 13:06:47 -0400277 /* IAC escaping inside a subnegotiation */
Sean Middleditchda0e6952009-03-15 13:28:09 -0400278 case LIBTELNET_STATE_SB_DATA_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400279 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400280 /* end subnegotiation */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400281 case LIBTELNET_SE:
Sean Middleditch9de15982009-03-14 03:35:49 -0400282 /* return to default state */
283 start = i + 1;
284 telnet->state = LIBTELNET_STATE_DATA;
285
Sean Middleditch9de15982009-03-14 03:35:49 -0400286 /* invoke callback */
Sean Middleditch637df7f2009-03-15 12:57:32 -0400287 _event(telnet, LIBTELNET_EV_SUBNEGOTIATION, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400288 telnet->sb_telopt, telnet->buffer, telnet->length);
Sean Middleditch9de15982009-03-14 03:35:49 -0400289
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400290#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400291 /* if we are a client or a proxy and just received the
292 * COMPRESS2 begin marker, setup our zlib box and start
293 * handling the compressed stream if it's not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400294 */
Sean Middleditchda0e6952009-03-15 13:28:09 -0400295 if (telnet->sb_telopt == LIBTELNET_TELOPT_COMPRESS2 &&
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400296 telnet->z == 0 &&
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400297 telnet->flags & LIBTELNET_FLAG_PROXY) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400298
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400299 if (_init_zlib(telnet, 0, 1) != LIBTELNET_EOK)
Sean Middleditch9de15982009-03-14 03:35:49 -0400300 break;
Sean Middleditch9de15982009-03-14 03:35:49 -0400301
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400302 /* notify app that compression was enabled */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400303 _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400304
Sean Middleditch9de15982009-03-14 03:35:49 -0400305 /* any remaining bytes in the buffer are compressed.
306 * we have to re-invoke libtelnet_push to get those
307 * bytes inflated and abort trying to process the
308 * remaining compressed bytes in the current _process
309 * buffer argument
310 */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400311 libtelnet_push(telnet, &buffer[start], size - start);
Sean Middleditch9de15982009-03-14 03:35:49 -0400312 return;
313 }
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400314#endif /* HAVE_ZLIB */
315
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400316 break;
317 /* escaped IAC byte */
318 case LIBTELNET_IAC:
319 /* push IAC into buffer */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400320 if (_buffer_byte(telnet, LIBTELNET_IAC) !=
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400321 LIBTELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400322 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400323 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400324 } else {
Sean Middleditchda0e6952009-03-15 13:28:09 -0400325 telnet->state = LIBTELNET_STATE_SB_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400326 }
327 break;
328 /* something else -- protocol error */
329 default:
Sean Middleditch16992272009-03-15 19:42:03 -0400330 _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400331 "unexpected byte after IAC inside SB: %d",
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400332 byte);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400333 start = i + 1;
Sean Middleditch9de15982009-03-14 03:35:49 -0400334 telnet->state = LIBTELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400335 break;
336 }
337 break;
338 }
339 }
340
341 /* pass through any remaining bytes */
Sean Middleditchd30fd572009-03-14 12:55:17 -0400342 if (telnet->state == LIBTELNET_STATE_DATA && i != start)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400343 _event(telnet, LIBTELNET_EV_DATA, 0, 0, buffer + start, i - start);
Sean Middleditch29144852009-03-12 23:14:47 -0400344}
345
Sean Middleditch9de15982009-03-14 03:35:49 -0400346/* push a bytes into the state tracker */
Sean Middleditch812358d2009-03-15 23:24:03 -0400347void libtelnet_push(libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400348 unsigned int size) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400349#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400350 /* if we have an inflate (decompression) zlib stream, use it */
Sean Middleditch4611d912009-03-16 00:53:57 -0400351 if (telnet->z != 0 && !(telnet->flags & LIBTELNET_PFLAG_DEFLATE)) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400352 unsigned char inflate_buffer[4096];
353 int rs;
354
355 /* initialize zlib state */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400356 telnet->z->next_in = buffer;
357 telnet->z->avail_in = size;
358 telnet->z->next_out = inflate_buffer;
359 telnet->z->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400360
361 /* inflate until buffer exhausted and all output is produced */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400362 while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400363 /* reset output buffer */
364
365 /* decompress */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400366 rs = inflate(telnet->z, Z_SYNC_FLUSH);
Sean Middleditch9de15982009-03-14 03:35:49 -0400367
368 /* process the decompressed bytes on success */
369 if (rs == Z_OK || rs == Z_STREAM_END)
370 _process(telnet, inflate_buffer, sizeof(inflate_buffer) -
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400371 telnet->z->avail_out);
Sean Middleditch9de15982009-03-14 03:35:49 -0400372 else
Sean Middleditch16992272009-03-15 19:42:03 -0400373 _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1,
374 "inflate() failed: %s", zError(rs));
Sean Middleditch9de15982009-03-14 03:35:49 -0400375
376 /* prepare output buffer for next run */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400377 telnet->z->next_out = inflate_buffer;
378 telnet->z->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400379
380 /* on error (or on end of stream) disable further inflation */
381 if (rs != Z_OK) {
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400382 _event(telnet, LIBTELNET_EV_COMPRESS, 0, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400383
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400384 inflateEnd(telnet->z);
385 free(telnet->z);
386 telnet->z = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400387 break;
388 }
389 }
390
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400391 /* COMPRESS2 is not negotiated, just process */
392 } else
393#endif /* HAVE_ZLIB */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400394 _process(telnet, buffer, size);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400395}
396
Sean Middleditch812358d2009-03-15 23:24:03 -0400397static void _send(libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400398 unsigned int size) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400399#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400400 /* if we have a deflate (compression) zlib box, use it */
Sean Middleditch4611d912009-03-16 00:53:57 -0400401 if (telnet->z != 0 && telnet->flags & LIBTELNET_PFLAG_DEFLATE) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400402 unsigned char deflate_buffer[1024];
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400403 int rs;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400404
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400405 /* initialize z state */
406 telnet->z->next_in = buffer;
407 telnet->z->avail_in = size;
408 telnet->z->next_out = deflate_buffer;
409 telnet->z->avail_out = sizeof(deflate_buffer);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400410
411 /* deflate until buffer exhausted and all output is produced */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400412 while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400413 /* compress */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400414 if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) {
Sean Middleditch16992272009-03-15 19:42:03 -0400415 _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1,
416 "deflate() failed: %s", zError(rs));
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400417 deflateEnd(telnet->z);
418 free(telnet->z);
419 telnet->z = 0;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400420 break;
421 }
422
Sean Middleditch637df7f2009-03-15 12:57:32 -0400423 _event(telnet, LIBTELNET_EV_SEND, 0, 0, deflate_buffer,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400424 sizeof(deflate_buffer) - telnet->z->avail_out);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400425
426 /* prepare output buffer for next run */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400427 telnet->z->next_out = deflate_buffer;
428 telnet->z->avail_out = sizeof(deflate_buffer);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400429 }
430
431 /* COMPRESS2 is not negotiated, just send */
432 } else
433#endif /* HAVE_ZLIB */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400434 _event(telnet, LIBTELNET_EV_SEND, 0, 0, buffer, size);
Sean Middleditch9de15982009-03-14 03:35:49 -0400435}
436
Sean Middleditch29144852009-03-12 23:14:47 -0400437/* send an iac command */
Sean Middleditch812358d2009-03-15 23:24:03 -0400438void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd) {
Sean Middleditchf9cebec2009-03-13 20:17:31 -0400439 unsigned char bytes[2] = { LIBTELNET_IAC, cmd };
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400440 _send(telnet, bytes, 2);
Sean Middleditch29144852009-03-12 23:14:47 -0400441}
442
443/* send negotiation */
Sean Middleditch812358d2009-03-15 23:24:03 -0400444void libtelnet_send_negotiate(libtelnet_t *telnet, unsigned char cmd,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400445 unsigned char opt) {
Sean Middleditchf9cebec2009-03-13 20:17:31 -0400446 unsigned char bytes[3] = { LIBTELNET_IAC, cmd, opt };
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400447 _send(telnet, bytes, 3);
Sean Middleditch29144852009-03-12 23:14:47 -0400448}
449
450/* send non-command data (escapes IAC bytes) */
Sean Middleditch812358d2009-03-15 23:24:03 -0400451void libtelnet_send_data(libtelnet_t *telnet, unsigned char *buffer,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400452 unsigned int size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400453 unsigned int i, l;
Sean Middleditch29144852009-03-12 23:14:47 -0400454 for (l = i = 0; i != size; ++i) {
455 /* dump prior portion of text, send escaped bytes */
456 if (buffer[i] == LIBTELNET_IAC) {
457 /* dump prior text if any */
458 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400459 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400460 l = i + 1;
461
462 /* send escape */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400463 libtelnet_send_command(telnet, LIBTELNET_IAC);
Sean Middleditch29144852009-03-12 23:14:47 -0400464 }
465 }
466
467 /* send whatever portion of buffer is left */
468 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400469 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400470}
471
472/* send sub-request */
Sean Middleditch812358d2009-03-15 23:24:03 -0400473void libtelnet_send_subnegotiation(libtelnet_t *telnet, unsigned char opt,
474 unsigned char *buffer, unsigned int size) {
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400475 libtelnet_send_command(telnet, LIBTELNET_SB);
476 libtelnet_send_data(telnet, &opt, 1);
477 libtelnet_send_data(telnet, buffer, size);
478 libtelnet_send_command(telnet, LIBTELNET_SE);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400479
480#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400481 /* if we're a proxy and we just sent the COMPRESS2 marker, we must
482 * make sure all further data is compressed if not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400483 */
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400484 if (telnet->flags & LIBTELNET_FLAG_PROXY &&
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400485 telnet->z == 0 &&
Sean Middleditch72cc9642009-03-15 11:50:36 -0400486 opt == LIBTELNET_TELOPT_COMPRESS2) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400487
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400488 if (_init_zlib(telnet, 1, 1) != LIBTELNET_EOK)
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400489 return;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400490
491 /* notify app that compression was enabled */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400492 _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400493 }
494#endif /* HAVE_ZLIB */
Sean Middleditch29144852009-03-12 23:14:47 -0400495}
Sean Middleditch124a1c22009-03-15 13:20:03 -0400496
Sean Middleditch812358d2009-03-15 23:24:03 -0400497void libtelnet_begin_compress2(libtelnet_t *telnet) {
Sean Middleditch124a1c22009-03-15 13:20:03 -0400498#ifdef HAVE_ZLIB
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400499 unsigned char compress2[] = { LIBTELNET_IAC, LIBTELNET_SB,
500 LIBTELNET_TELOPT_COMPRESS2, LIBTELNET_IAC, LIBTELNET_SE };
Sean Middleditch124a1c22009-03-15 13:20:03 -0400501
502 /* don't do this if we've already got a compression stream */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400503 if (telnet->z != 0) {
Sean Middleditch16992272009-03-15 19:42:03 -0400504 _error(telnet, __LINE__, __func__, LIBTELNET_EBADVAL, 0,
505 "compression already enabled");
Sean Middleditch124a1c22009-03-15 13:20:03 -0400506 return;
Sean Middleditch16992272009-03-15 19:42:03 -0400507 }
Sean Middleditch124a1c22009-03-15 13:20:03 -0400508
509 /* attempt to create output stream first, bail if we can't */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400510 if (_init_zlib(telnet, 1, 0) != LIBTELNET_EOK)
Sean Middleditch124a1c22009-03-15 13:20:03 -0400511 return;
512
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400513 /* send compression marker. we send directly to the event handler
514 * instead of passing through _send because _send would result in
515 * the compress marker itself being compressed.
516 */
517 _event(telnet, LIBTELNET_EV_SEND, 0, 0, compress2, sizeof(compress2));
Sean Middleditch124a1c22009-03-15 13:20:03 -0400518#endif /* HAVE_ZLIB */
519}