mirror of https://gerrit.osmocom.org/libtelnet
remove need for the struct and enum stuff using typedefs
This commit is contained in:
parent
e7c442681a
commit
812358d92a
31
README
31
README
|
@ -57,9 +57,8 @@ IIa. Initialization
|
|||
its own libtelnet_t structure, which is passed to all libtelnet
|
||||
API calls.
|
||||
|
||||
void libtelnet_init(struct libtelnet_t *telnet,
|
||||
libtelnet_event_handler_t handler, enum libtelnet_mode_t mode,
|
||||
void *user_data);
|
||||
void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t handler,
|
||||
libtelnet_mode_t mode, void *user_data);
|
||||
The libtelnet_init() function is responsible for initializing
|
||||
the data in a libtelnet_t structure. It must be called
|
||||
immediately after establishing a connection and before any other
|
||||
|
@ -82,14 +81,14 @@ IIa. Initialization
|
|||
enables special behavior for telnet-proxy (or similar
|
||||
applications).
|
||||
|
||||
boid libtelnet_free(struct libtelnet_t *telnet);
|
||||
boid libtelnet_free(libtelnet_t *telnet);
|
||||
Releases any internal memory allocated by libtelnet. This must
|
||||
be called whenever a connection is closed, or you will incur
|
||||
memory leaks.
|
||||
|
||||
IIb. Receiving Data
|
||||
|
||||
void libtelnet_push(struct libtelnet_t *telnet,
|
||||
void libtelnet_push(libtelnet_t *telnet,
|
||||
unsigned char *buffer, unsigned int size, void *user_data);
|
||||
When your application receives data over the socket from the
|
||||
remote end, it must pass the received bytes into this function.
|
||||
|
@ -111,24 +110,23 @@ IIc. Sending Data
|
|||
to libtelnet_send_data(). Do NOT send or buffer unprocessed output
|
||||
data directly!
|
||||
|
||||
void libtelnet_send_command(struct libtelnet_t *telnet,
|
||||
unsigned char cmd);
|
||||
void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd);
|
||||
Sends a single "simple" TELNET command, such as the GO-AHEAD
|
||||
commands (255 249).
|
||||
|
||||
void libtelnet_send_negotiate(struct libtelnet_t *telnet,
|
||||
void libtelnet_send_negotiate(libtelnet_t *telnet,
|
||||
unsigned char cmd, unsigned char opt);
|
||||
Sends a TELNET negotiation command. The cmd parameter must be
|
||||
one of LIBTELNET_WILL, LIBTELNET_DONT, LIBTELNET_DO, or
|
||||
LIBTELNET_DONT. The opt parameter is the option to
|
||||
negotiate.
|
||||
|
||||
void libtelnet_send_data(struct libtelnet_t *telnet,
|
||||
unsigned char *buffer, unsigned int size);
|
||||
void libtelnet_send_data(libtelnet_t *telnet, unsigned char *buffer,
|
||||
unsigned int size);
|
||||
Sends raw data, which would be either the process output from
|
||||
a server or the user input from a client.
|
||||
|
||||
void libtelnet_send_subnegotiation(struct libtelnet_t *telnet,
|
||||
void libtelnet_send_subnegotiation(libtelnet_t *telnet,
|
||||
unsigned char opt, unsigned char *buffer, unsigned int size);
|
||||
Sends a TELNET sub-negotiation command. The opt parameter
|
||||
is the sub-negotiation option.
|
||||
|
@ -150,11 +148,12 @@ IId. Event Handling
|
|||
pointer passed to libtelnet_init().
|
||||
|
||||
struct libtelnet_event_t {
|
||||
enum libtelnet_event_type_t type;
|
||||
unsigned char command;
|
||||
unsigned char telopt;
|
||||
unsigned char *buffer;
|
||||
unsigned int size;
|
||||
libtelnet_event_type_t type;
|
||||
unsigned char command;
|
||||
unsigned char telopt;
|
||||
unsigned char accept;
|
||||
};
|
||||
|
||||
The enumeration values of libtelnet_event_type_t are described in
|
||||
|
@ -169,8 +168,8 @@ IId. Event Handling
|
|||
Here is an example event handler implementation which includes
|
||||
handlers for several important events.
|
||||
|
||||
void my_event_handler(struct libtelnet_t *telnet,
|
||||
libtelnet_event_t *ev, void *user_data) {
|
||||
void my_event_handler(libtelnet_t *telnet, libtelnet_event_t *ev,
|
||||
void *user_data) {
|
||||
struct user_info *user = (struct user_info *)user_data;
|
||||
|
||||
switch (ev->type) {
|
||||
|
|
49
libtelnet.c
49
libtelnet.c
|
@ -30,14 +30,14 @@ static const unsigned int _buffer_sizes[] = {
|
|||
8192,
|
||||
16384,
|
||||
};
|
||||
static const unsigned int _buffer_sizes_count =
|
||||
sizeof(_buffer_sizes) / sizeof(_buffer_sizes[0]);
|
||||
static const unsigned int _buffer_sizes_count = sizeof(_buffer_sizes) /
|
||||
sizeof(_buffer_sizes[0]);
|
||||
|
||||
/* event dispatch helper */
|
||||
static void _event(struct libtelnet_t *telnet,
|
||||
enum libtelnet_event_type_t type, unsigned char command,
|
||||
unsigned char telopt, unsigned char *buffer, unsigned int size) {
|
||||
struct libtelnet_event_t ev;
|
||||
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;
|
||||
ev.type = type;
|
||||
ev.command = command;
|
||||
ev.telopt = telopt;
|
||||
|
@ -48,14 +48,13 @@ static void _event(struct libtelnet_t *telnet,
|
|||
}
|
||||
|
||||
/* error generation function */
|
||||
static void _error(struct libtelnet_t *telnet, unsigned line, const char* func,
|
||||
enum libtelnet_error_t err, int fatal, const char *fmt, ...) {
|
||||
static void _error(libtelnet_t *telnet, unsigned line, const char* func,
|
||||
libtelnet_error_t err, int fatal, const char *fmt, ...) {
|
||||
char buffer[512];
|
||||
va_list va;
|
||||
|
||||
/* format error intro */
|
||||
snprintf(buffer, sizeof(buffer), "%s:%u in %s: ",
|
||||
__FILE__, line, func);
|
||||
snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", __FILE__, line, func);
|
||||
|
||||
va_start(va, fmt);
|
||||
vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
|
||||
|
@ -70,7 +69,7 @@ static void _error(struct libtelnet_t *telnet, unsigned line, const char* func,
|
|||
* initializes zlib for delating (compression), otherwise for inflating
|
||||
* (decompression)
|
||||
*/
|
||||
z_stream *_init_zlib(struct libtelnet_t *telnet, int deflate, int err_fatal) {
|
||||
z_stream *_init_zlib(libtelnet_t *telnet, int deflate, int err_fatal) {
|
||||
z_stream *zlib;
|
||||
int rs;
|
||||
|
||||
|
@ -103,16 +102,16 @@ z_stream *_init_zlib(struct libtelnet_t *telnet, int deflate, int err_fatal) {
|
|||
}
|
||||
|
||||
/* initialize a telnet state tracker */
|
||||
void libtelnet_init(struct libtelnet_t *telnet, libtelnet_event_handler_t eh,
|
||||
enum libtelnet_mode_t mode, void *user_data) {
|
||||
memset(telnet, 0, sizeof(struct libtelnet_t));
|
||||
void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh,
|
||||
libtelnet_mode_t mode, void *user_data) {
|
||||
memset(telnet, 0, sizeof(libtelnet_t));
|
||||
telnet->ud = user_data;
|
||||
telnet->eh = eh;
|
||||
telnet->mode = mode;
|
||||
}
|
||||
|
||||
/* free up any memory allocated by a state tracker */
|
||||
void libtelnet_free(struct libtelnet_t *telnet) {
|
||||
void libtelnet_free(libtelnet_t *telnet) {
|
||||
/* free sub-request buffer */
|
||||
if (telnet->buffer != 0) {
|
||||
free(telnet->buffer);
|
||||
|
@ -135,7 +134,7 @@ void libtelnet_free(struct libtelnet_t *telnet) {
|
|||
}
|
||||
|
||||
/* push a byte into the telnet buffer */
|
||||
static enum libtelnet_error_t _buffer_byte(struct libtelnet_t *telnet,
|
||||
static libtelnet_error_t _buffer_byte(libtelnet_t *telnet,
|
||||
unsigned char byte) {
|
||||
unsigned char *new_buffer;
|
||||
int i;
|
||||
|
@ -175,7 +174,7 @@ static enum libtelnet_error_t _buffer_byte(struct libtelnet_t *telnet,
|
|||
return LIBTELNET_EOK;
|
||||
}
|
||||
|
||||
static void _process(struct libtelnet_t *telnet, unsigned char *buffer,
|
||||
static void _process(libtelnet_t *telnet, unsigned char *buffer,
|
||||
unsigned int size) {
|
||||
unsigned char byte;
|
||||
unsigned int i, start;
|
||||
|
@ -340,7 +339,7 @@ static void _process(struct libtelnet_t *telnet, unsigned char *buffer,
|
|||
}
|
||||
|
||||
/* push a bytes into the state tracker */
|
||||
void libtelnet_push(struct libtelnet_t *telnet, unsigned char *buffer,
|
||||
void libtelnet_push(libtelnet_t *telnet, unsigned char *buffer,
|
||||
unsigned int size) {
|
||||
#ifdef HAVE_ZLIB
|
||||
/* if we have an inflate (decompression) zlib stream, use it */
|
||||
|
@ -390,7 +389,7 @@ void libtelnet_push(struct libtelnet_t *telnet, unsigned char *buffer,
|
|||
_process(telnet, buffer, size);
|
||||
}
|
||||
|
||||
static void _send(struct libtelnet_t *telnet, unsigned char *buffer,
|
||||
static void _send(libtelnet_t *telnet, unsigned char *buffer,
|
||||
unsigned int size) {
|
||||
#ifdef HAVE_ZLIB
|
||||
/* if we have a deflate (compression) zlib box, use it */
|
||||
|
@ -431,20 +430,20 @@ static void _send(struct libtelnet_t *telnet, unsigned char *buffer,
|
|||
}
|
||||
|
||||
/* send an iac command */
|
||||
void libtelnet_send_command(struct libtelnet_t *telnet, unsigned char cmd) {
|
||||
void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd) {
|
||||
unsigned char bytes[2] = { LIBTELNET_IAC, cmd };
|
||||
_send(telnet, bytes, 2);
|
||||
}
|
||||
|
||||
/* send negotiation */
|
||||
void libtelnet_send_negotiate(struct libtelnet_t *telnet, unsigned char cmd,
|
||||
void libtelnet_send_negotiate(libtelnet_t *telnet, unsigned char cmd,
|
||||
unsigned char opt) {
|
||||
unsigned char bytes[3] = { LIBTELNET_IAC, cmd, opt };
|
||||
_send(telnet, bytes, 3);
|
||||
}
|
||||
|
||||
/* send non-command data (escapes IAC bytes) */
|
||||
void libtelnet_send_data(struct libtelnet_t *telnet, unsigned char *buffer,
|
||||
void libtelnet_send_data(libtelnet_t *telnet, unsigned char *buffer,
|
||||
unsigned int size) {
|
||||
unsigned int i, l;
|
||||
for (l = i = 0; i != size; ++i) {
|
||||
|
@ -466,8 +465,8 @@ void libtelnet_send_data(struct libtelnet_t *telnet, unsigned char *buffer,
|
|||
}
|
||||
|
||||
/* send sub-request */
|
||||
void libtelnet_send_subnegotiation(struct libtelnet_t *telnet,
|
||||
unsigned char opt, unsigned char *buffer, unsigned int size) {
|
||||
void libtelnet_send_subnegotiation(libtelnet_t *telnet, unsigned char opt,
|
||||
unsigned char *buffer, unsigned int size) {
|
||||
libtelnet_send_command(telnet, LIBTELNET_SB);
|
||||
libtelnet_send_data(telnet, &opt, 1);
|
||||
libtelnet_send_data(telnet, buffer, size);
|
||||
|
@ -490,7 +489,7 @@ void libtelnet_send_subnegotiation(struct libtelnet_t *telnet,
|
|||
#endif /* HAVE_ZLIB */
|
||||
}
|
||||
|
||||
void libtelnet_begin_compress2(struct libtelnet_t *telnet) {
|
||||
void libtelnet_begin_compress2(libtelnet_t *telnet) {
|
||||
#ifdef HAVE_ZLIB
|
||||
z_stream *zlib;
|
||||
|
||||
|
|
38
libtelnet.h
38
libtelnet.h
|
@ -13,8 +13,12 @@
|
|||
#define LIBTELNET_INCLUDE 1
|
||||
|
||||
/* forward declarations */
|
||||
struct libtelnet_t;
|
||||
struct libtelnet_cb_t;
|
||||
typedef struct libtelnet_t libtelnet_t;
|
||||
typedef struct libtelnet_event_t libtelnet_event_t;
|
||||
typedef enum libtelnet_mode_t libtelnet_mode_t;
|
||||
typedef enum libtelnet_state_t libtelnet_state_t;
|
||||
typedef enum libtelnet_error_t libtelnet_error_t;
|
||||
typedef enum libtelnet_event_type_t libtelnet_event_type_t;
|
||||
|
||||
/* telnet special values */
|
||||
#define LIBTELNET_IAC 255
|
||||
|
@ -141,8 +145,8 @@ struct libtelnet_event_t {
|
|||
};
|
||||
|
||||
/* event handler declaration */
|
||||
typedef void (*libtelnet_event_handler_t)(struct libtelnet_t *telnet,
|
||||
struct libtelnet_event_t *event, void *user_data);
|
||||
typedef void (*libtelnet_event_handler_t)(libtelnet_t *telnet,
|
||||
libtelnet_event_t *event, void *user_data);
|
||||
|
||||
/* state tracker */
|
||||
struct libtelnet_t {
|
||||
|
@ -170,34 +174,32 @@ struct libtelnet_t {
|
|||
};
|
||||
|
||||
/* initialize a telnet state tracker */
|
||||
extern void libtelnet_init(struct libtelnet_t *telnet,
|
||||
libtelnet_event_handler_t eh, enum libtelnet_mode_t mode,
|
||||
void *user_data);
|
||||
extern void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh,
|
||||
libtelnet_mode_t mode, void *user_data);
|
||||
|
||||
/* free up any memory allocated by a state tracker */
|
||||
extern void libtelnet_free(struct libtelnet_t *telnet);
|
||||
extern void libtelnet_free(libtelnet_t *telnet);
|
||||
|
||||
/* push a byte buffer into the state tracker */
|
||||
extern void libtelnet_push(struct libtelnet_t *telnet,
|
||||
unsigned char *buffer, unsigned int size);
|
||||
extern void libtelnet_push(libtelnet_t *telnet, unsigned char *buffer,
|
||||
unsigned int size);
|
||||
|
||||
/* send an iac command */
|
||||
extern void libtelnet_send_command(struct libtelnet_t *telnet,
|
||||
unsigned char cmd);
|
||||
extern void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd);
|
||||
|
||||
/* send negotiation */
|
||||
extern void libtelnet_send_negotiate(struct libtelnet_t *telnet,
|
||||
unsigned char cmd, unsigned char opt);
|
||||
extern void libtelnet_send_negotiate(libtelnet_t *telnet, unsigned char cmd,
|
||||
unsigned char opt);
|
||||
|
||||
/* send non-command data (escapes IAC bytes) */
|
||||
extern void libtelnet_send_data(struct libtelnet_t *telnet,
|
||||
unsigned char *buffer, unsigned int size);
|
||||
extern void libtelnet_send_data(libtelnet_t *telnet, unsigned char *buffer,
|
||||
unsigned int size);
|
||||
|
||||
/* send sub-request */
|
||||
extern void libtelnet_send_subnegotiation(struct libtelnet_t *telnet,
|
||||
extern void libtelnet_send_subnegotiation(libtelnet_t *telnet,
|
||||
unsigned char opt, unsigned char *buffer, unsigned int size);
|
||||
|
||||
/* begin sending compressed data (server only) */
|
||||
extern void libtelnet_begin_compress2(struct libtelnet_t *telnet);
|
||||
extern void libtelnet_begin_compress2(libtelnet_t *telnet);
|
||||
|
||||
#endif /* !defined(LIBTELNET_INCLUDE) */
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
#include "libtelnet.h"
|
||||
|
||||
static struct termios orig_tios;
|
||||
static struct libtelnet_t telnet;
|
||||
static libtelnet_t telnet;
|
||||
static int do_echo;
|
||||
|
||||
static void _cleanup(void) {
|
||||
|
@ -76,8 +76,8 @@ static void _send(int sock, unsigned char *buffer, unsigned int size) {
|
|||
}
|
||||
}
|
||||
|
||||
static void _event_handler(struct libtelnet_t *telnet,
|
||||
struct libtelnet_event_t *ev, void *user_data) {
|
||||
static void _event_handler(libtelnet_t *telnet, libtelnet_event_t *ev,
|
||||
void *user_data) {
|
||||
int sock = *(int*)user_data;
|
||||
|
||||
switch (ev->type) {
|
||||
|
|
|
@ -44,7 +44,7 @@
|
|||
struct conn_t {
|
||||
const char *name;
|
||||
int sock;
|
||||
struct libtelnet_t telnet;
|
||||
libtelnet_t telnet;
|
||||
struct conn_t *remote;
|
||||
};
|
||||
|
||||
|
@ -161,8 +161,8 @@ static void _send(int sock, unsigned char *buffer, unsigned int size) {
|
|||
}
|
||||
}
|
||||
|
||||
static void _event_handler(struct libtelnet_t *telnet,
|
||||
struct libtelnet_event_t *ev, void *user_data) {
|
||||
static void _event_handler(libtelnet_t *telnet, libtelnet_event_t *ev,
|
||||
void *user_data) {
|
||||
struct conn_t *conn = (struct conn_t*)user_data;
|
||||
|
||||
switch (ev->type) {
|
||||
|
|
Loading…
Reference in New Issue