2009-03-13 03:27:35 +00:00
|
|
|
/*
|
2009-03-14 07:35:49 +00:00
|
|
|
* Sean Middleditch
|
|
|
|
* sean@sourcemud.org
|
|
|
|
*
|
2009-03-13 03:27:35 +00:00
|
|
|
* The author or authors of this code dedicate any and all copyright interest
|
|
|
|
* in this code to the public domain. We make this dedication for the benefit
|
|
|
|
* of the public at large and to the detriment of our heirs and successors. We
|
|
|
|
* intend this dedication to be an overt act of relinquishment in perpetuity of
|
|
|
|
* all present and future rights to this code under copyright law.
|
|
|
|
*/
|
|
|
|
|
2009-03-14 02:48:05 +00:00
|
|
|
#include <malloc.h>
|
2009-03-14 07:35:49 +00:00
|
|
|
#include <string.h>
|
2009-03-15 02:35:01 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2009-03-14 07:35:49 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
#include "zlib.h"
|
|
|
|
#endif
|
|
|
|
|
2009-03-13 03:14:47 +00:00
|
|
|
#include "libtelnet.h"
|
|
|
|
|
2009-03-14 02:48:05 +00:00
|
|
|
/* buffer sizes */
|
|
|
|
static const unsigned int _buffer_sizes[] = {
|
|
|
|
0,
|
|
|
|
512,
|
|
|
|
2048,
|
|
|
|
8192,
|
|
|
|
16384,
|
|
|
|
};
|
2009-03-16 03:24:03 +00:00
|
|
|
static const unsigned int _buffer_sizes_count = sizeof(_buffer_sizes) /
|
|
|
|
sizeof(_buffer_sizes[0]);
|
2009-03-14 02:48:05 +00:00
|
|
|
|
2009-03-15 16:57:32 +00:00
|
|
|
/* event dispatch helper */
|
2009-03-16 03:24:03 +00:00
|
|
|
static void _event(libtelnet_t *telnet, libtelnet_event_type_t type,
|
|
|
|
unsigned char command, unsigned char telopt, unsigned char *buffer,
|
|
|
|
unsigned int size) {
|
|
|
|
libtelnet_event_t ev;
|
2009-03-15 16:57:32 +00:00
|
|
|
ev.type = type;
|
|
|
|
ev.command = command;
|
|
|
|
ev.telopt = telopt;
|
|
|
|
ev.buffer = buffer;
|
|
|
|
ev.size = size;
|
|
|
|
|
2009-03-15 17:39:24 +00:00
|
|
|
telnet->eh(telnet, &ev, telnet->ud);
|
2009-03-15 16:57:32 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 02:35:01 +00:00
|
|
|
/* error generation function */
|
2009-03-16 03:24:03 +00:00
|
|
|
static void _error(libtelnet_t *telnet, unsigned line, const char* func,
|
|
|
|
libtelnet_error_t err, int fatal, const char *fmt, ...) {
|
2009-03-15 02:35:01 +00:00
|
|
|
char buffer[512];
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
/* format error intro */
|
2009-03-16 03:24:03 +00:00
|
|
|
snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", __FILE__, line, func);
|
2009-03-15 02:35:01 +00:00
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
|
|
|
|
fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
2009-03-15 23:42:03 +00:00
|
|
|
_event(telnet, fatal ? LIBTELNET_EV_ERROR : LIBTELNET_EV_WARNING, err,
|
2009-03-16 00:02:14 +00:00
|
|
|
0, (unsigned char *)buffer, strlen(buffer));
|
2009-03-15 02:35:01 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 15:50:36 +00:00
|
|
|
/* initialize the zlib box for a telnet box; if deflate is non-zero, it
|
|
|
|
* initializes zlib for delating (compression), otherwise for inflating
|
|
|
|
* (decompression)
|
|
|
|
*/
|
2009-03-16 03:24:03 +00:00
|
|
|
z_stream *_init_zlib(libtelnet_t *telnet, int deflate, int err_fatal) {
|
2009-03-15 15:50:36 +00:00
|
|
|
z_stream *zlib;
|
|
|
|
int rs;
|
|
|
|
|
|
|
|
/* allocate zstream box */
|
|
|
|
if ((zlib = (z_stream *)calloc(1, sizeof(z_stream)))
|
|
|
|
== 0) {
|
2009-03-15 23:42:03 +00:00
|
|
|
_error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, err_fatal,
|
|
|
|
"malloc() failed: %s", strerror(errno));
|
2009-03-15 15:50:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize */
|
|
|
|
if (deflate) {
|
|
|
|
if ((rs = deflateInit(zlib, Z_DEFAULT_COMPRESSION)) != Z_OK) {
|
|
|
|
free(zlib);
|
2009-03-15 23:42:03 +00:00
|
|
|
_error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, err_fatal,
|
|
|
|
"deflateInit() failed: %s", zError(rs));
|
2009-03-15 15:50:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((rs = inflateInit(zlib)) != Z_OK) {
|
|
|
|
free(zlib);
|
2009-03-15 23:42:03 +00:00
|
|
|
_error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, err_fatal,
|
|
|
|
"inflateInit() failed: %s", zError(rs));
|
2009-03-15 15:50:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return zlib;
|
|
|
|
}
|
|
|
|
|
2009-03-13 03:14:47 +00:00
|
|
|
/* initialize a telnet state tracker */
|
2009-03-16 03:24:03 +00:00
|
|
|
void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh,
|
2009-03-16 03:29:46 +00:00
|
|
|
unsigned char flags, void *user_data) {
|
2009-03-16 03:24:03 +00:00
|
|
|
memset(telnet, 0, sizeof(libtelnet_t));
|
2009-03-15 17:39:24 +00:00
|
|
|
telnet->ud = user_data;
|
2009-03-15 16:57:32 +00:00
|
|
|
telnet->eh = eh;
|
2009-03-16 03:29:46 +00:00
|
|
|
telnet->flags = flags;
|
2009-03-13 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* free up any memory allocated by a state tracker */
|
2009-03-16 03:24:03 +00:00
|
|
|
void libtelnet_free(libtelnet_t *telnet) {
|
2009-03-14 07:35:49 +00:00
|
|
|
/* free sub-request buffer */
|
2009-03-14 00:15:59 +00:00
|
|
|
if (telnet->buffer != 0) {
|
2009-03-13 03:14:47 +00:00
|
|
|
free(telnet->buffer);
|
|
|
|
telnet->buffer = 0;
|
2009-03-14 00:15:59 +00:00
|
|
|
telnet->size = 0;
|
|
|
|
telnet->length = 0;
|
|
|
|
}
|
2009-03-14 07:35:49 +00:00
|
|
|
|
2009-03-15 15:50:36 +00:00
|
|
|
/* free zlib box(es) */
|
|
|
|
if (telnet->z_inflate != 0) {
|
|
|
|
inflateEnd(telnet->z_inflate);
|
|
|
|
free(telnet->z_inflate);
|
|
|
|
telnet->z_inflate = 0;
|
|
|
|
}
|
|
|
|
if (telnet->z_deflate != 0) {
|
|
|
|
deflateEnd(telnet->z_deflate);
|
|
|
|
free(telnet->z_deflate);
|
|
|
|
telnet->z_deflate = 0;
|
2009-03-14 07:35:49 +00:00
|
|
|
}
|
2009-03-14 00:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* push a byte into the telnet buffer */
|
2009-03-16 03:24:03 +00:00
|
|
|
static libtelnet_error_t _buffer_byte(libtelnet_t *telnet,
|
2009-03-15 17:39:24 +00:00
|
|
|
unsigned char byte) {
|
2009-03-14 02:48:05 +00:00
|
|
|
unsigned char *new_buffer;
|
|
|
|
int i;
|
|
|
|
|
2009-03-14 00:15:59 +00:00
|
|
|
/* check if we're out of room */
|
|
|
|
if (telnet->length == telnet->size) {
|
2009-03-14 02:48:05 +00:00
|
|
|
/* find the next buffer size */
|
|
|
|
for (i = 0; i != _buffer_sizes_count; ++i) {
|
|
|
|
if (_buffer_sizes[i] == telnet->size)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* overflow -- can't grow any more */
|
|
|
|
if (i >= _buffer_sizes_count - 1) {
|
2009-03-15 23:42:03 +00:00
|
|
|
_error(telnet, __LINE__, __func__, LIBTELNET_EOVERFLOW, 0,
|
2009-03-15 17:39:24 +00:00
|
|
|
"subnegotiation buffer size limit reached");
|
2009-03-14 00:15:59 +00:00
|
|
|
libtelnet_free(telnet);
|
2009-03-15 15:54:07 +00:00
|
|
|
return LIBTELNET_EOVERFLOW;
|
2009-03-14 02:48:05 +00:00
|
|
|
}
|
2009-03-14 00:15:59 +00:00
|
|
|
|
2009-03-14 02:48:05 +00:00
|
|
|
/* (re)allocate buffer */
|
|
|
|
new_buffer = (unsigned char *)realloc(telnet->buffer,
|
|
|
|
_buffer_sizes[i + 1]);
|
|
|
|
if (new_buffer == 0) {
|
2009-03-15 23:42:03 +00:00
|
|
|
_error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, 0,
|
|
|
|
"realloc() failed");
|
2009-03-14 02:48:05 +00:00
|
|
|
libtelnet_free(telnet);
|
2009-03-15 15:54:07 +00:00
|
|
|
return LIBTELNET_ENOMEM;
|
2009-03-14 00:15:59 +00:00
|
|
|
}
|
2009-03-14 02:48:05 +00:00
|
|
|
|
|
|
|
telnet->buffer = new_buffer;
|
|
|
|
telnet->size = _buffer_sizes[i + 1];
|
2009-03-13 03:14:47 +00:00
|
|
|
}
|
2009-03-14 00:15:59 +00:00
|
|
|
|
|
|
|
/* push the byte, all set */
|
|
|
|
telnet->buffer[telnet->length++] = byte;
|
2009-03-15 15:54:07 +00:00
|
|
|
return LIBTELNET_EOK;
|
2009-03-13 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 03:24:03 +00:00
|
|
|
static void _process(libtelnet_t *telnet, unsigned char *buffer,
|
2009-03-15 17:39:24 +00:00
|
|
|
unsigned int size) {
|
2009-03-14 03:39:18 +00:00
|
|
|
unsigned char byte;
|
|
|
|
unsigned int i, start;
|
|
|
|
for (i = start = 0; i != size; ++i) {
|
|
|
|
byte = buffer[i];
|
|
|
|
switch (telnet->state) {
|
|
|
|
/* regular data */
|
2009-03-14 07:35:49 +00:00
|
|
|
case LIBTELNET_STATE_DATA:
|
2009-03-14 03:39:18 +00:00
|
|
|
/* on an IAC byte, pass through all pending bytes and
|
|
|
|
* switch states */
|
|
|
|
if (byte == LIBTELNET_IAC) {
|
|
|
|
if (i != start)
|
2009-03-15 16:57:32 +00:00
|
|
|
_event(telnet, LIBTELNET_EV_DATA, 0, 0, &buffer[start],
|
2009-03-15 17:39:24 +00:00
|
|
|
i - start);
|
2009-03-14 03:39:18 +00:00
|
|
|
telnet->state = LIBTELNET_STATE_IAC;
|
|
|
|
}
|
2009-03-13 03:14:47 +00:00
|
|
|
break;
|
2009-03-14 03:39:18 +00:00
|
|
|
|
|
|
|
/* IAC command */
|
|
|
|
case LIBTELNET_STATE_IAC:
|
|
|
|
switch (byte) {
|
2009-03-14 17:06:47 +00:00
|
|
|
/* subnegotiation */
|
2009-03-14 03:39:18 +00:00
|
|
|
case LIBTELNET_SB:
|
|
|
|
telnet->state = LIBTELNET_STATE_SB;
|
|
|
|
break;
|
|
|
|
/* negotiation commands */
|
|
|
|
case LIBTELNET_WILL:
|
|
|
|
telnet->state = LIBTELNET_STATE_WILL;
|
|
|
|
break;
|
|
|
|
case LIBTELNET_WONT:
|
|
|
|
telnet->state = LIBTELNET_STATE_WONT;
|
|
|
|
break;
|
|
|
|
case LIBTELNET_DO:
|
|
|
|
telnet->state = LIBTELNET_STATE_DO;
|
|
|
|
break;
|
|
|
|
case LIBTELNET_DONT:
|
|
|
|
telnet->state = LIBTELNET_STATE_DONT;
|
|
|
|
break;
|
|
|
|
/* IAC escaping */
|
|
|
|
case LIBTELNET_IAC:
|
2009-03-15 17:39:24 +00:00
|
|
|
_event(telnet, LIBTELNET_EV_DATA, 0, 0, &byte, 1);
|
2009-03-14 03:39:18 +00:00
|
|
|
start = i + 1;
|
2009-03-14 07:35:49 +00:00
|
|
|
telnet->state = LIBTELNET_STATE_DATA;
|
2009-03-14 03:39:18 +00:00
|
|
|
break;
|
|
|
|
/* some other command */
|
|
|
|
default:
|
2009-03-15 17:39:24 +00:00
|
|
|
_event(telnet, LIBTELNET_EV_IAC, byte, 0, 0, 0);
|
2009-03-14 03:39:18 +00:00
|
|
|
start = i + 1;
|
2009-03-14 07:35:49 +00:00
|
|
|
telnet->state = LIBTELNET_STATE_DATA;
|
2009-03-14 03:39:18 +00:00
|
|
|
}
|
2009-03-13 03:14:47 +00:00
|
|
|
break;
|
2009-03-14 03:39:18 +00:00
|
|
|
|
|
|
|
/* negotiation commands */
|
|
|
|
case LIBTELNET_STATE_DO:
|
2009-03-15 17:39:24 +00:00
|
|
|
_event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_DO, byte, 0, 0);
|
2009-03-14 03:39:18 +00:00
|
|
|
start = i + 1;
|
2009-03-14 07:35:49 +00:00
|
|
|
telnet->state = LIBTELNET_STATE_DATA;
|
2009-03-13 03:14:47 +00:00
|
|
|
break;
|
2009-03-14 03:39:18 +00:00
|
|
|
case LIBTELNET_STATE_DONT:
|
2009-03-15 17:39:24 +00:00
|
|
|
_event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_DONT, byte, 0, 0);
|
2009-03-14 03:39:18 +00:00
|
|
|
start = i + 1;
|
2009-03-14 07:35:49 +00:00
|
|
|
telnet->state = LIBTELNET_STATE_DATA;
|
2009-03-14 03:39:18 +00:00
|
|
|
break;
|
|
|
|
case LIBTELNET_STATE_WILL:
|
2009-03-15 17:39:24 +00:00
|
|
|
_event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_WILL, byte, 0, 0);
|
2009-03-14 03:39:18 +00:00
|
|
|
start = i + 1;
|
2009-03-14 07:35:49 +00:00
|
|
|
telnet->state = LIBTELNET_STATE_DATA;
|
2009-03-14 03:39:18 +00:00
|
|
|
break;
|
|
|
|
case LIBTELNET_STATE_WONT:
|
2009-03-15 17:39:24 +00:00
|
|
|
_event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_WONT, byte, 0, 0);
|
2009-03-14 03:39:18 +00:00
|
|
|
start = i + 1;
|
2009-03-14 07:35:49 +00:00
|
|
|
telnet->state = LIBTELNET_STATE_DATA;
|
2009-03-13 03:14:47 +00:00
|
|
|
break;
|
2009-03-14 03:39:18 +00:00
|
|
|
|
2009-03-15 17:28:09 +00:00
|
|
|
/* subnegotiation -- determine subnegotiation telopt */
|
2009-03-14 03:39:18 +00:00
|
|
|
case LIBTELNET_STATE_SB:
|
2009-03-15 17:28:09 +00:00
|
|
|
telnet->sb_telopt = byte;
|
|
|
|
telnet->length = 0;
|
|
|
|
telnet->state = LIBTELNET_STATE_SB_DATA;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* subnegotiation -- buffer bytes until end request */
|
|
|
|
case LIBTELNET_STATE_SB_DATA:
|
2009-03-14 17:06:47 +00:00
|
|
|
/* IAC command in subnegotiation -- either IAC SE or IAC IAC */
|
2009-03-14 03:39:18 +00:00
|
|
|
if (byte == LIBTELNET_IAC) {
|
2009-03-15 17:28:09 +00:00
|
|
|
telnet->state = LIBTELNET_STATE_SB_DATA_IAC;
|
2009-03-14 03:39:18 +00:00
|
|
|
/* buffer the byte, or bail if we can't */
|
2009-03-15 17:39:24 +00:00
|
|
|
} else if (_buffer_byte(telnet, byte) != LIBTELNET_EOK) {
|
2009-03-14 03:39:18 +00:00
|
|
|
start = i + 1;
|
2009-03-14 07:35:49 +00:00
|
|
|
telnet->state = LIBTELNET_STATE_DATA;
|
2009-03-14 03:39:18 +00:00
|
|
|
}
|
2009-03-13 03:14:47 +00:00
|
|
|
break;
|
2009-03-14 03:39:18 +00:00
|
|
|
|
2009-03-14 17:06:47 +00:00
|
|
|
/* IAC escaping inside a subnegotiation */
|
2009-03-15 17:28:09 +00:00
|
|
|
case LIBTELNET_STATE_SB_DATA_IAC:
|
2009-03-14 03:39:18 +00:00
|
|
|
switch (byte) {
|
2009-03-14 17:06:47 +00:00
|
|
|
/* end subnegotiation */
|
2009-03-14 03:39:18 +00:00
|
|
|
case LIBTELNET_SE:
|
2009-03-14 07:35:49 +00:00
|
|
|
/* return to default state */
|
|
|
|
start = i + 1;
|
|
|
|
telnet->state = LIBTELNET_STATE_DATA;
|
|
|
|
|
|
|
|
/* invoke callback */
|
2009-03-15 16:57:32 +00:00
|
|
|
_event(telnet, LIBTELNET_EV_SUBNEGOTIATION, 0,
|
2009-03-15 17:39:24 +00:00
|
|
|
telnet->sb_telopt, telnet->buffer, telnet->length);
|
2009-03-14 07:35:49 +00:00
|
|
|
|
2009-03-14 08:57:27 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2009-03-15 15:50:36 +00:00
|
|
|
/* if we are a client or a proxy and just received the
|
|
|
|
* COMPRESS2 begin marker, setup our zlib box and start
|
|
|
|
* handling the compressed stream if it's not already.
|
2009-03-14 08:57:27 +00:00
|
|
|
*/
|
2009-03-15 17:28:09 +00:00
|
|
|
if (telnet->sb_telopt == LIBTELNET_TELOPT_COMPRESS2 &&
|
2009-03-15 15:50:36 +00:00
|
|
|
telnet->z_inflate == 0 &&
|
2009-03-16 03:29:46 +00:00
|
|
|
telnet->flags & LIBTELNET_FLAG_PROXY) {
|
2009-03-15 15:50:36 +00:00
|
|
|
|
2009-03-16 00:02:14 +00:00
|
|
|
if ((telnet->z_inflate = _init_zlib(telnet, 0, 1)) == 0)
|
2009-03-14 07:35:49 +00:00
|
|
|
break;
|
|
|
|
|
2009-03-14 08:57:27 +00:00
|
|
|
/* notify app that compression was enabled */
|
2009-03-15 17:39:24 +00:00
|
|
|
_event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0);
|
2009-03-14 08:57:27 +00:00
|
|
|
|
2009-03-14 07:35:49 +00:00
|
|
|
/* any remaining bytes in the buffer are compressed.
|
|
|
|
* we have to re-invoke libtelnet_push to get those
|
|
|
|
* bytes inflated and abort trying to process the
|
|
|
|
* remaining compressed bytes in the current _process
|
|
|
|
* buffer argument
|
|
|
|
*/
|
2009-03-15 17:39:24 +00:00
|
|
|
libtelnet_push(telnet, &buffer[start], size - start);
|
2009-03-14 07:35:49 +00:00
|
|
|
return;
|
2009-03-14 03:39:18 +00:00
|
|
|
}
|
2009-03-14 08:57:27 +00:00
|
|
|
#endif /* HAVE_ZLIB */
|
|
|
|
|
2009-03-14 03:39:18 +00:00
|
|
|
break;
|
|
|
|
/* escaped IAC byte */
|
|
|
|
case LIBTELNET_IAC:
|
|
|
|
/* push IAC into buffer */
|
2009-03-15 17:39:24 +00:00
|
|
|
if (_buffer_byte(telnet, LIBTELNET_IAC) !=
|
2009-03-15 15:54:07 +00:00
|
|
|
LIBTELNET_EOK) {
|
2009-03-14 03:39:18 +00:00
|
|
|
start = i + 1;
|
2009-03-14 07:35:49 +00:00
|
|
|
telnet->state = LIBTELNET_STATE_DATA;
|
2009-03-14 03:39:18 +00:00
|
|
|
} else {
|
2009-03-15 17:28:09 +00:00
|
|
|
telnet->state = LIBTELNET_STATE_SB_DATA;
|
2009-03-14 03:39:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
/* something else -- protocol error */
|
|
|
|
default:
|
2009-03-15 23:42:03 +00:00
|
|
|
_error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0,
|
2009-03-15 17:39:24 +00:00
|
|
|
"unexpected byte after IAC inside SB: %d",
|
2009-03-15 02:35:01 +00:00
|
|
|
byte);
|
2009-03-14 03:39:18 +00:00
|
|
|
start = i + 1;
|
2009-03-14 07:35:49 +00:00
|
|
|
telnet->state = LIBTELNET_STATE_DATA;
|
2009-03-14 03:39:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-03-13 03:14:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-14 03:39:18 +00:00
|
|
|
/* pass through any remaining bytes */
|
2009-03-14 16:55:17 +00:00
|
|
|
if (telnet->state == LIBTELNET_STATE_DATA && i != start)
|
2009-03-15 17:39:24 +00:00
|
|
|
_event(telnet, LIBTELNET_EV_DATA, 0, 0, buffer + start, i - start);
|
2009-03-13 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
2009-03-14 07:35:49 +00:00
|
|
|
/* push a bytes into the state tracker */
|
2009-03-16 03:24:03 +00:00
|
|
|
void libtelnet_push(libtelnet_t *telnet, unsigned char *buffer,
|
2009-03-15 17:39:24 +00:00
|
|
|
unsigned int size) {
|
2009-03-14 08:57:27 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2009-03-15 15:50:36 +00:00
|
|
|
/* if we have an inflate (decompression) zlib stream, use it */
|
|
|
|
if (telnet->z_inflate != 0) {
|
2009-03-14 07:35:49 +00:00
|
|
|
unsigned char inflate_buffer[4096];
|
|
|
|
int rs;
|
|
|
|
|
|
|
|
/* initialize zlib state */
|
2009-03-15 15:50:36 +00:00
|
|
|
telnet->z_inflate->next_in = buffer;
|
|
|
|
telnet->z_inflate->avail_in = size;
|
|
|
|
telnet->z_inflate->next_out = inflate_buffer;
|
|
|
|
telnet->z_inflate->avail_out = sizeof(inflate_buffer);
|
2009-03-14 07:35:49 +00:00
|
|
|
|
|
|
|
/* inflate until buffer exhausted and all output is produced */
|
2009-03-15 15:50:36 +00:00
|
|
|
while (telnet->z_inflate->avail_in > 0 || telnet->z_inflate->avail_out == 0) {
|
2009-03-14 07:35:49 +00:00
|
|
|
/* reset output buffer */
|
|
|
|
|
|
|
|
/* decompress */
|
2009-03-15 15:50:36 +00:00
|
|
|
rs = inflate(telnet->z_inflate, Z_SYNC_FLUSH);
|
2009-03-14 07:35:49 +00:00
|
|
|
|
|
|
|
/* process the decompressed bytes on success */
|
|
|
|
if (rs == Z_OK || rs == Z_STREAM_END)
|
|
|
|
_process(telnet, inflate_buffer, sizeof(inflate_buffer) -
|
2009-03-15 17:39:24 +00:00
|
|
|
telnet->z_inflate->avail_out);
|
2009-03-14 07:35:49 +00:00
|
|
|
else
|
2009-03-15 23:42:03 +00:00
|
|
|
_error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1,
|
|
|
|
"inflate() failed: %s", zError(rs));
|
2009-03-14 07:35:49 +00:00
|
|
|
|
|
|
|
/* prepare output buffer for next run */
|
2009-03-15 15:50:36 +00:00
|
|
|
telnet->z_inflate->next_out = inflate_buffer;
|
|
|
|
telnet->z_inflate->avail_out = sizeof(inflate_buffer);
|
2009-03-14 07:35:49 +00:00
|
|
|
|
|
|
|
/* on error (or on end of stream) disable further inflation */
|
|
|
|
if (rs != Z_OK) {
|
2009-03-15 17:39:24 +00:00
|
|
|
_event(telnet, LIBTELNET_EV_COMPRESS, 0, 0, 0, 0);
|
2009-03-14 08:57:27 +00:00
|
|
|
|
2009-03-15 15:50:36 +00:00
|
|
|
inflateEnd(telnet->z_inflate);
|
|
|
|
free(telnet->z_inflate);
|
|
|
|
telnet->z_inflate = 0;
|
2009-03-14 07:35:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-14 08:57:27 +00:00
|
|
|
/* COMPRESS2 is not negotiated, just process */
|
|
|
|
} else
|
|
|
|
#endif /* HAVE_ZLIB */
|
2009-03-15 17:39:24 +00:00
|
|
|
_process(telnet, buffer, size);
|
2009-03-14 08:57:27 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 03:24:03 +00:00
|
|
|
static void _send(libtelnet_t *telnet, unsigned char *buffer,
|
2009-03-15 17:39:24 +00:00
|
|
|
unsigned int size) {
|
2009-03-14 08:57:27 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2009-03-15 15:50:36 +00:00
|
|
|
/* if we have a deflate (compression) zlib box, use it */
|
|
|
|
if (telnet->z_deflate != 0) {
|
2009-03-14 08:57:27 +00:00
|
|
|
unsigned char deflate_buffer[1024];
|
2009-03-15 02:35:01 +00:00
|
|
|
int rs;
|
2009-03-14 08:57:27 +00:00
|
|
|
|
2009-03-15 15:50:36 +00:00
|
|
|
/* initialize z_deflate state */
|
|
|
|
telnet->z_deflate->next_in = buffer;
|
|
|
|
telnet->z_deflate->avail_in = size;
|
|
|
|
telnet->z_deflate->next_out = deflate_buffer;
|
|
|
|
telnet->z_deflate->avail_out = sizeof(deflate_buffer);
|
2009-03-14 08:57:27 +00:00
|
|
|
|
|
|
|
/* deflate until buffer exhausted and all output is produced */
|
2009-03-15 15:50:36 +00:00
|
|
|
while (telnet->z_deflate->avail_in > 0 || telnet->z_deflate->avail_out == 0) {
|
2009-03-14 08:57:27 +00:00
|
|
|
/* compress */
|
2009-03-15 15:50:36 +00:00
|
|
|
if ((rs = deflate(telnet->z_deflate, Z_SYNC_FLUSH)) != Z_OK) {
|
2009-03-15 23:42:03 +00:00
|
|
|
_error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1,
|
|
|
|
"deflate() failed: %s", zError(rs));
|
2009-03-15 15:50:36 +00:00
|
|
|
deflateEnd(telnet->z_deflate);
|
|
|
|
free(telnet->z_deflate);
|
|
|
|
telnet->z_deflate = 0;
|
2009-03-14 08:57:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-03-15 16:57:32 +00:00
|
|
|
_event(telnet, LIBTELNET_EV_SEND, 0, 0, deflate_buffer,
|
2009-03-15 17:39:24 +00:00
|
|
|
sizeof(deflate_buffer) - telnet->z_deflate->avail_out);
|
2009-03-14 08:57:27 +00:00
|
|
|
|
|
|
|
/* prepare output buffer for next run */
|
2009-03-15 15:50:36 +00:00
|
|
|
telnet->z_deflate->next_out = deflate_buffer;
|
|
|
|
telnet->z_deflate->avail_out = sizeof(deflate_buffer);
|
2009-03-14 08:57:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* COMPRESS2 is not negotiated, just send */
|
|
|
|
} else
|
|
|
|
#endif /* HAVE_ZLIB */
|
2009-03-15 17:39:24 +00:00
|
|
|
_event(telnet, LIBTELNET_EV_SEND, 0, 0, buffer, size);
|
2009-03-14 07:35:49 +00:00
|
|
|
}
|
|
|
|
|
2009-03-13 03:14:47 +00:00
|
|
|
/* send an iac command */
|
2009-03-16 03:24:03 +00:00
|
|
|
void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd) {
|
2009-03-14 00:17:31 +00:00
|
|
|
unsigned char bytes[2] = { LIBTELNET_IAC, cmd };
|
2009-03-15 17:39:24 +00:00
|
|
|
_send(telnet, bytes, 2);
|
2009-03-13 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* send negotiation */
|
2009-03-16 03:24:03 +00:00
|
|
|
void libtelnet_send_negotiate(libtelnet_t *telnet, unsigned char cmd,
|
2009-03-15 17:39:24 +00:00
|
|
|
unsigned char opt) {
|
2009-03-14 00:17:31 +00:00
|
|
|
unsigned char bytes[3] = { LIBTELNET_IAC, cmd, opt };
|
2009-03-15 17:39:24 +00:00
|
|
|
_send(telnet, bytes, 3);
|
2009-03-13 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* send non-command data (escapes IAC bytes) */
|
2009-03-16 03:24:03 +00:00
|
|
|
void libtelnet_send_data(libtelnet_t *telnet, unsigned char *buffer,
|
2009-03-15 17:39:24 +00:00
|
|
|
unsigned int size) {
|
2009-03-14 02:48:05 +00:00
|
|
|
unsigned int i, l;
|
2009-03-13 03:14:47 +00:00
|
|
|
for (l = i = 0; i != size; ++i) {
|
|
|
|
/* dump prior portion of text, send escaped bytes */
|
|
|
|
if (buffer[i] == LIBTELNET_IAC) {
|
|
|
|
/* dump prior text if any */
|
|
|
|
if (i != l)
|
2009-03-15 17:39:24 +00:00
|
|
|
_send(telnet, buffer + l, i - l);
|
2009-03-13 03:14:47 +00:00
|
|
|
l = i + 1;
|
|
|
|
|
|
|
|
/* send escape */
|
2009-03-15 17:39:24 +00:00
|
|
|
libtelnet_send_command(telnet, LIBTELNET_IAC);
|
2009-03-13 03:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send whatever portion of buffer is left */
|
|
|
|
if (i != l)
|
2009-03-15 17:39:24 +00:00
|
|
|
_send(telnet, buffer + l, i - l);
|
2009-03-13 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* send sub-request */
|
2009-03-16 03:24:03 +00:00
|
|
|
void libtelnet_send_subnegotiation(libtelnet_t *telnet, unsigned char opt,
|
|
|
|
unsigned char *buffer, unsigned int size) {
|
2009-03-15 17:39:24 +00:00
|
|
|
libtelnet_send_command(telnet, LIBTELNET_SB);
|
|
|
|
libtelnet_send_data(telnet, &opt, 1);
|
|
|
|
libtelnet_send_data(telnet, buffer, size);
|
|
|
|
libtelnet_send_command(telnet, LIBTELNET_SE);
|
2009-03-14 08:57:27 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_ZLIB
|
2009-03-15 15:50:36 +00:00
|
|
|
/* if we're a proxy and we just sent the COMPRESS2 marker, we must
|
|
|
|
* make sure all further data is compressed if not already.
|
2009-03-14 08:57:27 +00:00
|
|
|
*/
|
2009-03-16 03:29:46 +00:00
|
|
|
if (telnet->flags & LIBTELNET_FLAG_PROXY &&
|
2009-03-15 15:50:36 +00:00
|
|
|
telnet->z_deflate == 0 &&
|
|
|
|
opt == LIBTELNET_TELOPT_COMPRESS2) {
|
2009-03-15 02:35:01 +00:00
|
|
|
|
2009-03-15 23:42:03 +00:00
|
|
|
if ((telnet->z_deflate = _init_zlib(telnet, 1, 1)) == 0)
|
2009-03-15 02:35:01 +00:00
|
|
|
return;
|
2009-03-14 08:57:27 +00:00
|
|
|
|
|
|
|
/* notify app that compression was enabled */
|
2009-03-15 17:39:24 +00:00
|
|
|
_event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0);
|
2009-03-14 08:57:27 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_ZLIB */
|
2009-03-13 03:14:47 +00:00
|
|
|
}
|
2009-03-15 17:20:03 +00:00
|
|
|
|
2009-03-16 03:24:03 +00:00
|
|
|
void libtelnet_begin_compress2(libtelnet_t *telnet) {
|
2009-03-15 17:20:03 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
z_stream *zlib;
|
|
|
|
|
|
|
|
/* don't do this if we've already got a compression stream */
|
2009-03-15 23:42:03 +00:00
|
|
|
if (telnet->z_deflate != 0) {
|
|
|
|
_error(telnet, __LINE__, __func__, LIBTELNET_EBADVAL, 0,
|
|
|
|
"compression already enabled");
|
2009-03-15 17:20:03 +00:00
|
|
|
return;
|
2009-03-15 23:42:03 +00:00
|
|
|
}
|
2009-03-15 17:20:03 +00:00
|
|
|
|
|
|
|
/* attempt to create output stream first, bail if we can't */
|
2009-03-15 23:42:03 +00:00
|
|
|
if ((zlib = _init_zlib(telnet, 1, 0)) == 0)
|
2009-03-15 17:20:03 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* send compression marker */
|
2009-03-15 17:39:24 +00:00
|
|
|
libtelnet_send_subnegotiation(telnet, LIBTELNET_TELOPT_COMPRESS2, 0, 0);
|
2009-03-15 17:20:03 +00:00
|
|
|
|
|
|
|
/* set our deflate stream */
|
|
|
|
telnet->z_deflate = zlib;
|
|
|
|
#endif /* HAVE_ZLIB */
|
|
|
|
}
|