mirror of https://gerrit.osmocom.org/libtelnet
more informative errors
This commit is contained in:
parent
3032302674
commit
d922c6f7e0
97
libtelnet.c
97
libtelnet.c
|
@ -11,6 +11,10 @@
|
||||||
|
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
#ifdef HAVE_ZLIB
|
#ifdef HAVE_ZLIB
|
||||||
#include "zlib.h"
|
#include "zlib.h"
|
||||||
|
@ -18,6 +22,19 @@
|
||||||
|
|
||||||
#include "libtelnet.h"
|
#include "libtelnet.h"
|
||||||
|
|
||||||
|
#ifdef ERROR
|
||||||
|
# undef ERROR
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define ERROR(telnet, code, user_data, msg) \
|
||||||
|
_error(telnet, __FILE__, __LINE__, code, user_data, "%s", msg)
|
||||||
|
#define ERROR_NOMEM(telnet, user_data, msg) \
|
||||||
|
_error(telnet, __FILE__, __LINE__, LIBTELNET_ERROR_NOMEM, user_data, \
|
||||||
|
"%s: %s", msg, strerror(errno))
|
||||||
|
#define ERROR_ZLIB(telnet, user_data, rs, msg) \
|
||||||
|
_error(telnet, __FILE__, __LINE__, LIBTELNET_ERROR_UNKNOWN, \
|
||||||
|
user_data, "%s: %s", msg, zError(rs))
|
||||||
|
|
||||||
/* buffer sizes */
|
/* buffer sizes */
|
||||||
static const unsigned int _buffer_sizes[] = {
|
static const unsigned int _buffer_sizes[] = {
|
||||||
0,
|
0,
|
||||||
|
@ -29,6 +46,30 @@ static const unsigned int _buffer_sizes[] = {
|
||||||
static const unsigned int _buffer_sizes_count =
|
static const unsigned int _buffer_sizes_count =
|
||||||
sizeof(_buffer_sizes) / sizeof(_buffer_sizes[0]);
|
sizeof(_buffer_sizes) / sizeof(_buffer_sizes[0]);
|
||||||
|
|
||||||
|
/* error generation function */
|
||||||
|
static void _error(struct libtelnet_t *telnet, const char *file, unsigned line,
|
||||||
|
enum libtelnet_error_t err, void *user_data, const char *fmt, ...) {
|
||||||
|
char buffer[512];
|
||||||
|
va_list va;
|
||||||
|
|
||||||
|
/* format error intro */
|
||||||
|
snprintf(buffer, sizeof(buffer), "%s:%u: ",
|
||||||
|
file, line);
|
||||||
|
|
||||||
|
va_start(va, fmt);
|
||||||
|
vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
|
||||||
|
fmt, va);
|
||||||
|
va_end(va);
|
||||||
|
|
||||||
|
/* invoke user's custom error handler if possible; otherwise,
|
||||||
|
* print to stderr
|
||||||
|
*/
|
||||||
|
if (telnet->cb->error)
|
||||||
|
telnet->cb->error(telnet, err, buffer, user_data);
|
||||||
|
else
|
||||||
|
fprintf(stderr, "**ERROR**: libtelnet: %s\n", buffer);
|
||||||
|
}
|
||||||
|
|
||||||
/* initialize a telnet state tracker */
|
/* initialize a telnet state tracker */
|
||||||
void libtelnet_init(struct libtelnet_t *telnet, struct libtelnet_cb_t *cb,
|
void libtelnet_init(struct libtelnet_t *telnet, struct libtelnet_cb_t *cb,
|
||||||
enum libtelnet_mode_t mode) {
|
enum libtelnet_mode_t mode) {
|
||||||
|
@ -59,8 +100,8 @@ void libtelnet_free(struct libtelnet_t *telnet) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* push a byte into the telnet buffer */
|
/* push a byte into the telnet buffer */
|
||||||
static enum libtelnet_error_t _buffer_byte(
|
static enum libtelnet_error_t _buffer_byte(struct libtelnet_t *telnet,
|
||||||
struct libtelnet_t *telnet, unsigned char byte, void *user_data) {
|
unsigned char byte, void *user_data) {
|
||||||
unsigned char *new_buffer;
|
unsigned char *new_buffer;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
@ -74,7 +115,8 @@ static enum libtelnet_error_t _buffer_byte(
|
||||||
|
|
||||||
/* overflow -- can't grow any more */
|
/* overflow -- can't grow any more */
|
||||||
if (i >= _buffer_sizes_count - 1) {
|
if (i >= _buffer_sizes_count - 1) {
|
||||||
telnet->cb->error(telnet, LIBTELNET_ERROR_OVERFLOW, user_data);
|
_error(telnet, __FILE__, __LINE__, LIBTELNET_ERROR_OVERFLOW,
|
||||||
|
user_data, "subnegotiation buffer size limit reached");
|
||||||
libtelnet_free(telnet);
|
libtelnet_free(telnet);
|
||||||
return LIBTELNET_ERROR_OVERFLOW;
|
return LIBTELNET_ERROR_OVERFLOW;
|
||||||
}
|
}
|
||||||
|
@ -83,8 +125,7 @@ static enum libtelnet_error_t _buffer_byte(
|
||||||
new_buffer = (unsigned char *)realloc(telnet->buffer,
|
new_buffer = (unsigned char *)realloc(telnet->buffer,
|
||||||
_buffer_sizes[i + 1]);
|
_buffer_sizes[i + 1]);
|
||||||
if (new_buffer == 0) {
|
if (new_buffer == 0) {
|
||||||
telnet->cb->error(telnet, LIBTELNET_ERROR_NOMEM,
|
ERROR_NOMEM(telnet, user_data, "realloc() failed");
|
||||||
user_data);
|
|
||||||
libtelnet_free(telnet);
|
libtelnet_free(telnet);
|
||||||
return LIBTELNET_ERROR_NOMEM;
|
return LIBTELNET_ERROR_NOMEM;
|
||||||
}
|
}
|
||||||
|
@ -197,8 +238,8 @@ static void _process(struct libtelnet_t *telnet, unsigned char *buffer,
|
||||||
|
|
||||||
/* zero-size buffer is a protocol error */
|
/* zero-size buffer is a protocol error */
|
||||||
if (telnet->length == 0) {
|
if (telnet->length == 0) {
|
||||||
telnet->cb->error(telnet, LIBTELNET_ERROR_PROTOCOL,
|
ERROR(telnet, LIBTELNET_ERROR_PROTOCOL, user_data,
|
||||||
user_data);
|
"subnegotiation has zero data");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,20 +255,21 @@ static void _process(struct libtelnet_t *telnet, unsigned char *buffer,
|
||||||
*/
|
*/
|
||||||
if (telnet->mode == LIBTELNET_MODE_CLIENT &&
|
if (telnet->mode == LIBTELNET_MODE_CLIENT &&
|
||||||
telnet->buffer[0] == LIBTELNET_TELOPT_COMPRESS2) {
|
telnet->buffer[0] == LIBTELNET_TELOPT_COMPRESS2) {
|
||||||
|
int rs;
|
||||||
|
|
||||||
/* allocate zstream box */
|
/* allocate zstream box */
|
||||||
if ((telnet->zlib = (z_stream *)malloc(sizeof(z_stream)))
|
if ((telnet->zlib = (z_stream *)malloc(sizeof(z_stream)))
|
||||||
== 0) {
|
== 0) {
|
||||||
telnet->cb->error(telnet,
|
ERROR_NOMEM(telnet, user_data, "malloc() failed");
|
||||||
LIBTELNET_ERROR_NOMEM, user_data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* initialize */
|
/* initialize */
|
||||||
memset(telnet->zlib, 0, sizeof(z_stream));
|
memset(telnet->zlib, 0, sizeof(z_stream));
|
||||||
if (inflateInit(telnet->zlib) != Z_OK) {
|
if ((rs = inflateInit(telnet->zlib)) != Z_OK) {
|
||||||
free(telnet->zlib);
|
free(telnet->zlib);
|
||||||
telnet->zlib = 0;
|
telnet->zlib = 0;
|
||||||
telnet->cb->error(telnet,
|
ERROR_ZLIB(telnet, user_data, rs,
|
||||||
LIBTELNET_ERROR_UNKNOWN, user_data);
|
"inflateInit() failed");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -260,8 +302,9 @@ static void _process(struct libtelnet_t *telnet, unsigned char *buffer,
|
||||||
break;
|
break;
|
||||||
/* something else -- protocol error */
|
/* something else -- protocol error */
|
||||||
default:
|
default:
|
||||||
telnet->cb->error(telnet, LIBTELNET_ERROR_PROTOCOL,
|
_error(telnet, __FILE__, __LINE__, LIBTELNET_ERROR_PROTOCOL,
|
||||||
user_data);
|
user_data, "unexpected byte after IAC inside SB: %d",
|
||||||
|
byte);
|
||||||
start = i + 1;
|
start = i + 1;
|
||||||
telnet->state = LIBTELNET_STATE_DATA;
|
telnet->state = LIBTELNET_STATE_DATA;
|
||||||
break;
|
break;
|
||||||
|
@ -304,8 +347,7 @@ void libtelnet_push(struct libtelnet_t *telnet, unsigned char *buffer,
|
||||||
_process(telnet, inflate_buffer, sizeof(inflate_buffer) -
|
_process(telnet, inflate_buffer, sizeof(inflate_buffer) -
|
||||||
telnet->zlib->avail_out, user_data);
|
telnet->zlib->avail_out, user_data);
|
||||||
else
|
else
|
||||||
telnet->cb->error(telnet, LIBTELNET_ERROR_UNKNOWN,
|
ERROR_ZLIB(telnet, user_data, rs, "inflate() failed");
|
||||||
user_data);
|
|
||||||
|
|
||||||
/* prepare output buffer for next run */
|
/* prepare output buffer for next run */
|
||||||
telnet->zlib->next_out = inflate_buffer;
|
telnet->zlib->next_out = inflate_buffer;
|
||||||
|
@ -336,6 +378,7 @@ static void _send(struct libtelnet_t *telnet, unsigned char *buffer,
|
||||||
*/
|
*/
|
||||||
if (telnet->mode == LIBTELNET_MODE_SERVER && telnet->zlib != 0) {
|
if (telnet->mode == LIBTELNET_MODE_SERVER && telnet->zlib != 0) {
|
||||||
unsigned char deflate_buffer[1024];
|
unsigned char deflate_buffer[1024];
|
||||||
|
int rs;
|
||||||
|
|
||||||
/* initialize zlib state */
|
/* initialize zlib state */
|
||||||
telnet->zlib->next_in = buffer;
|
telnet->zlib->next_in = buffer;
|
||||||
|
@ -345,12 +388,9 @@ static void _send(struct libtelnet_t *telnet, unsigned char *buffer,
|
||||||
|
|
||||||
/* deflate until buffer exhausted and all output is produced */
|
/* deflate until buffer exhausted and all output is produced */
|
||||||
while (telnet->zlib->avail_in > 0 || telnet->zlib->avail_out == 0) {
|
while (telnet->zlib->avail_in > 0 || telnet->zlib->avail_out == 0) {
|
||||||
/* reset output buffer */
|
|
||||||
|
|
||||||
/* compress */
|
/* compress */
|
||||||
if (deflate(telnet->zlib, Z_SYNC_FLUSH) != Z_OK) {
|
if ((rs = deflate(telnet->zlib, Z_SYNC_FLUSH)) != Z_OK) {
|
||||||
telnet->cb->error(telnet, LIBTELNET_ERROR_UNKNOWN,
|
ERROR_ZLIB(telnet, user_data, rs, "deflate() failed");
|
||||||
user_data);
|
|
||||||
deflateEnd(telnet->zlib);
|
deflateEnd(telnet->zlib);
|
||||||
free(telnet->zlib);
|
free(telnet->zlib);
|
||||||
telnet->zlib = 0;
|
telnet->zlib = 0;
|
||||||
|
@ -422,20 +462,21 @@ void libtelnet_send_subnegotiation(struct libtelnet_t *telnet,
|
||||||
*/
|
*/
|
||||||
if (telnet->mode == LIBTELNET_MODE_SERVER && opt ==
|
if (telnet->mode == LIBTELNET_MODE_SERVER && opt ==
|
||||||
LIBTELNET_TELOPT_COMPRESS2) {
|
LIBTELNET_TELOPT_COMPRESS2) {
|
||||||
|
int rs;
|
||||||
|
|
||||||
/* allocate zstream box */
|
/* allocate zstream box */
|
||||||
if ((telnet->zlib = (z_stream *)malloc(sizeof(z_stream)))
|
if ((telnet->zlib = (z_stream *)malloc(sizeof(z_stream))) == 0) {
|
||||||
== 0) {
|
ERROR_NOMEM(telnet, user_data, "malloc() failed");
|
||||||
telnet->cb->error(telnet,
|
return;
|
||||||
LIBTELNET_ERROR_NOMEM, user_data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* initialize */
|
/* initialize */
|
||||||
memset(telnet->zlib, 0, sizeof(z_stream));
|
memset(telnet->zlib, 0, sizeof(z_stream));
|
||||||
if (deflateInit(telnet->zlib, Z_DEFAULT_COMPRESSION) != Z_OK) {
|
if ((rs = deflateInit(telnet->zlib, Z_DEFAULT_COMPRESSION)) != Z_OK) {
|
||||||
free(telnet->zlib);
|
free(telnet->zlib);
|
||||||
telnet->zlib = 0;
|
telnet->zlib = 0;
|
||||||
telnet->cb->error(telnet,
|
ERROR_ZLIB(telnet, user_data, rs, "delateInit() failed");
|
||||||
LIBTELNET_ERROR_UNKNOWN, user_data);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* notify app that compression was enabled */
|
/* notify app that compression was enabled */
|
||||||
|
|
27
libtelnet.h
27
libtelnet.h
|
@ -115,24 +115,23 @@ enum libtelnet_error_t {
|
||||||
/* libtelnet callback declarations */
|
/* libtelnet callback declarations */
|
||||||
struct libtelnet_cb_t {
|
struct libtelnet_cb_t {
|
||||||
/* received (processed) data */
|
/* received (processed) data */
|
||||||
void (*data)(struct libtelnet_t *telnet,
|
void (*data)(struct libtelnet_t *telnet, unsigned char *buffer,
|
||||||
unsigned char *buffer, unsigned int size, void *user_data);
|
unsigned int size, void *user_data);
|
||||||
/* processed data to buffer for sending */
|
/* processed data to buffer for sending */
|
||||||
void (*send)(struct libtelnet_t *telnet,
|
void (*send)(struct libtelnet_t *telnet, unsigned char *buffer,
|
||||||
unsigned char *buffer, unsigned int size, void *user_data);
|
unsigned int size, void *user_data);
|
||||||
/* unknown command notification */
|
/* unknown command notification */
|
||||||
void (*command)(struct libtelnet_t *telnet,
|
void (*command)(struct libtelnet_t *telnet, unsigned char cmd,
|
||||||
unsigned char cmd, void *user_data);
|
|
||||||
/* negotiation notification */
|
|
||||||
void (*negotiate)(struct libtelnet_t *telnet,
|
|
||||||
unsigned char cmd, unsigned char opt, void *user_data);
|
|
||||||
/* unknown subnegotiation notification */
|
|
||||||
void (*subnegotiation)(struct libtelnet_t *telnet,
|
|
||||||
unsigned char opt, unsigned char *data, unsigned int size,
|
|
||||||
void *user_data);
|
void *user_data);
|
||||||
|
/* negotiation notification */
|
||||||
|
void (*negotiate)(struct libtelnet_t *telnet, unsigned char cmd,
|
||||||
|
unsigned char opt, void *user_data);
|
||||||
|
/* unknown subnegotiation notification */
|
||||||
|
void (*subnegotiation)(struct libtelnet_t *telnet, unsigned char opt,
|
||||||
|
unsigned char *data, unsigned int size, void *user_data);
|
||||||
/* error handler */
|
/* error handler */
|
||||||
void (*error)(struct libtelnet_t *telnet,
|
void (*error)(struct libtelnet_t *telnet, enum libtelnet_error_t error,
|
||||||
enum libtelnet_error_t error, void *user_data);
|
const char *msg, void *user_data);
|
||||||
|
|
||||||
#ifdef HAVE_ZLIB
|
#ifdef HAVE_ZLIB
|
||||||
void (*compress)(struct libtelnet_t *telnet,
|
void (*compress)(struct libtelnet_t *telnet,
|
||||||
|
|
|
@ -208,11 +208,11 @@ static void _compress_cb(struct libtelnet_t *telnet, char enabled,
|
||||||
printf("%s COMPRESSION %s\e[0m\n", conn->name, enabled ? "ON" : "OFF");
|
printf("%s COMPRESSION %s\e[0m\n", conn->name, enabled ? "ON" : "OFF");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _error_cb(struct libtelnet_t *telnet,
|
static void _error_cb(struct libtelnet_t *telnet, enum libtelnet_error_t error,
|
||||||
enum libtelnet_error_t error, void *user_data) {
|
const char *msg, void *user_data) {
|
||||||
struct conn_t *conn = (struct conn_t*)user_data;
|
struct conn_t *conn = (struct conn_t*)user_data;
|
||||||
|
|
||||||
printf("%s ERROR: %d\e[0m\n", conn->name, (int)error);
|
printf("%s ERROR: %s\e[0m\n", conn->name, msg);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue