remove need for the struct and enum stuff using typedefs

This commit is contained in:
Sean Middleditch 2009-03-15 23:24:03 -04:00
parent e7c442681a
commit 812358d92a
5 changed files with 65 additions and 65 deletions

31
README
View File

@ -57,9 +57,8 @@ IIa. Initialization
its own libtelnet_t structure, which is passed to all libtelnet its own libtelnet_t structure, which is passed to all libtelnet
API calls. API calls.
void libtelnet_init(struct libtelnet_t *telnet, void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t handler,
libtelnet_event_handler_t handler, enum libtelnet_mode_t mode, libtelnet_mode_t mode, void *user_data);
void *user_data);
The libtelnet_init() function is responsible for initializing The libtelnet_init() function is responsible for initializing
the data in a libtelnet_t structure. It must be called the data in a libtelnet_t structure. It must be called
immediately after establishing a connection and before any other immediately after establishing a connection and before any other
@ -82,14 +81,14 @@ IIa. Initialization
enables special behavior for telnet-proxy (or similar enables special behavior for telnet-proxy (or similar
applications). applications).
boid libtelnet_free(struct libtelnet_t *telnet); boid libtelnet_free(libtelnet_t *telnet);
Releases any internal memory allocated by libtelnet. This must Releases any internal memory allocated by libtelnet. This must
be called whenever a connection is closed, or you will incur be called whenever a connection is closed, or you will incur
memory leaks. memory leaks.
IIb. Receiving Data 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); unsigned char *buffer, unsigned int size, void *user_data);
When your application receives data over the socket from the When your application receives data over the socket from the
remote end, it must pass the received bytes into this function. 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 to libtelnet_send_data(). Do NOT send or buffer unprocessed output
data directly! data directly!
void libtelnet_send_command(struct libtelnet_t *telnet, void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd);
unsigned char cmd);
Sends a single "simple" TELNET command, such as the GO-AHEAD Sends a single "simple" TELNET command, such as the GO-AHEAD
commands (255 249). commands (255 249).
void libtelnet_send_negotiate(struct libtelnet_t *telnet, void libtelnet_send_negotiate(libtelnet_t *telnet,
unsigned char cmd, unsigned char opt); unsigned char cmd, unsigned char opt);
Sends a TELNET negotiation command. The cmd parameter must be Sends a TELNET negotiation command. The cmd parameter must be
one of LIBTELNET_WILL, LIBTELNET_DONT, LIBTELNET_DO, or one of LIBTELNET_WILL, LIBTELNET_DONT, LIBTELNET_DO, or
LIBTELNET_DONT. The opt parameter is the option to LIBTELNET_DONT. The opt parameter is the option to
negotiate. negotiate.
void libtelnet_send_data(struct libtelnet_t *telnet, void libtelnet_send_data(libtelnet_t *telnet, unsigned char *buffer,
unsigned char *buffer, unsigned int size); unsigned int size);
Sends raw data, which would be either the process output from Sends raw data, which would be either the process output from
a server or the user input from a client. 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); unsigned char opt, unsigned char *buffer, unsigned int size);
Sends a TELNET sub-negotiation command. The opt parameter Sends a TELNET sub-negotiation command. The opt parameter
is the sub-negotiation option. is the sub-negotiation option.
@ -150,11 +148,12 @@ IId. Event Handling
pointer passed to libtelnet_init(). pointer passed to libtelnet_init().
struct libtelnet_event_t { struct libtelnet_event_t {
enum libtelnet_event_type_t type;
unsigned char command;
unsigned char telopt;
unsigned char *buffer; unsigned char *buffer;
unsigned int size; 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 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 Here is an example event handler implementation which includes
handlers for several important events. handlers for several important events.
void my_event_handler(struct libtelnet_t *telnet, void my_event_handler(libtelnet_t *telnet, libtelnet_event_t *ev,
libtelnet_event_t *ev, void *user_data) { void *user_data) {
struct user_info *user = (struct user_info *)user_data; struct user_info *user = (struct user_info *)user_data;
switch (ev->type) { switch (ev->type) {

View File

@ -30,14 +30,14 @@ static const unsigned int _buffer_sizes[] = {
8192, 8192,
16384, 16384,
}; };
static const unsigned int _buffer_sizes_count = static const unsigned int _buffer_sizes_count = sizeof(_buffer_sizes) /
sizeof(_buffer_sizes) / sizeof(_buffer_sizes[0]); sizeof(_buffer_sizes[0]);
/* event dispatch helper */ /* event dispatch helper */
static void _event(struct libtelnet_t *telnet, static void _event(libtelnet_t *telnet, libtelnet_event_type_t type,
enum libtelnet_event_type_t type, unsigned char command, unsigned char command, unsigned char telopt, unsigned char *buffer,
unsigned char telopt, unsigned char *buffer, unsigned int size) { unsigned int size) {
struct libtelnet_event_t ev; libtelnet_event_t ev;
ev.type = type; ev.type = type;
ev.command = command; ev.command = command;
ev.telopt = telopt; ev.telopt = telopt;
@ -48,14 +48,13 @@ static void _event(struct libtelnet_t *telnet,
} }
/* error generation function */ /* error generation function */
static void _error(struct libtelnet_t *telnet, unsigned line, const char* func, static void _error(libtelnet_t *telnet, unsigned line, const char* func,
enum libtelnet_error_t err, int fatal, const char *fmt, ...) { libtelnet_error_t err, int fatal, const char *fmt, ...) {
char buffer[512]; char buffer[512];
va_list va; va_list va;
/* format error intro */ /* format error intro */
snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", __FILE__, line, func);
__FILE__, line, func);
va_start(va, fmt); va_start(va, fmt);
vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), 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 * initializes zlib for delating (compression), otherwise for inflating
* (decompression) * (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; z_stream *zlib;
int rs; int rs;
@ -103,16 +102,16 @@ z_stream *_init_zlib(struct libtelnet_t *telnet, int deflate, int err_fatal) {
} }
/* initialize a telnet state tracker */ /* initialize a telnet state tracker */
void libtelnet_init(struct libtelnet_t *telnet, libtelnet_event_handler_t eh, void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh,
enum libtelnet_mode_t mode, void *user_data) { libtelnet_mode_t mode, void *user_data) {
memset(telnet, 0, sizeof(struct libtelnet_t)); memset(telnet, 0, sizeof(libtelnet_t));
telnet->ud = user_data; telnet->ud = user_data;
telnet->eh = eh; telnet->eh = eh;
telnet->mode = mode; telnet->mode = mode;
} }
/* free up any memory allocated by a state tracker */ /* 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 */ /* free sub-request buffer */
if (telnet->buffer != 0) { if (telnet->buffer != 0) {
free(telnet->buffer); free(telnet->buffer);
@ -135,7 +134,7 @@ 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(struct libtelnet_t *telnet, static libtelnet_error_t _buffer_byte(libtelnet_t *telnet,
unsigned char byte) { unsigned char byte) {
unsigned char *new_buffer; unsigned char *new_buffer;
int i; int i;
@ -175,7 +174,7 @@ static enum libtelnet_error_t _buffer_byte(struct libtelnet_t *telnet,
return LIBTELNET_EOK; 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 int size) {
unsigned char byte; unsigned char byte;
unsigned int i, start; 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 */ /* 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) { unsigned int size) {
#ifdef HAVE_ZLIB #ifdef HAVE_ZLIB
/* if we have an inflate (decompression) zlib stream, use it */ /* 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); _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) { unsigned int size) {
#ifdef HAVE_ZLIB #ifdef HAVE_ZLIB
/* if we have a deflate (compression) zlib box, use it */ /* 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 */ /* 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 }; unsigned char bytes[2] = { LIBTELNET_IAC, cmd };
_send(telnet, bytes, 2); _send(telnet, bytes, 2);
} }
/* send negotiation */ /* 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 opt) {
unsigned char bytes[3] = { LIBTELNET_IAC, cmd, opt }; unsigned char bytes[3] = { LIBTELNET_IAC, cmd, opt };
_send(telnet, bytes, 3); _send(telnet, bytes, 3);
} }
/* send non-command data (escapes IAC bytes) */ /* 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 size) {
unsigned int i, l; unsigned int i, l;
for (l = i = 0; i != size; ++i) { 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 */ /* send sub-request */
void libtelnet_send_subnegotiation(struct libtelnet_t *telnet, void libtelnet_send_subnegotiation(libtelnet_t *telnet, unsigned char opt,
unsigned char opt, unsigned char *buffer, unsigned int size) { unsigned char *buffer, unsigned int size) {
libtelnet_send_command(telnet, LIBTELNET_SB); libtelnet_send_command(telnet, LIBTELNET_SB);
libtelnet_send_data(telnet, &opt, 1); libtelnet_send_data(telnet, &opt, 1);
libtelnet_send_data(telnet, buffer, size); libtelnet_send_data(telnet, buffer, size);
@ -490,7 +489,7 @@ void libtelnet_send_subnegotiation(struct libtelnet_t *telnet,
#endif /* HAVE_ZLIB */ #endif /* HAVE_ZLIB */
} }
void libtelnet_begin_compress2(struct libtelnet_t *telnet) { void libtelnet_begin_compress2(libtelnet_t *telnet) {
#ifdef HAVE_ZLIB #ifdef HAVE_ZLIB
z_stream *zlib; z_stream *zlib;

View File

@ -13,8 +13,12 @@
#define LIBTELNET_INCLUDE 1 #define LIBTELNET_INCLUDE 1
/* forward declarations */ /* forward declarations */
struct libtelnet_t; typedef struct libtelnet_t libtelnet_t;
struct libtelnet_cb_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 */ /* telnet special values */
#define LIBTELNET_IAC 255 #define LIBTELNET_IAC 255
@ -141,8 +145,8 @@ struct libtelnet_event_t {
}; };
/* event handler declaration */ /* event handler declaration */
typedef void (*libtelnet_event_handler_t)(struct libtelnet_t *telnet, typedef void (*libtelnet_event_handler_t)(libtelnet_t *telnet,
struct libtelnet_event_t *event, void *user_data); libtelnet_event_t *event, void *user_data);
/* state tracker */ /* state tracker */
struct libtelnet_t { struct libtelnet_t {
@ -170,34 +174,32 @@ struct libtelnet_t {
}; };
/* initialize a telnet state tracker */ /* initialize a telnet state tracker */
extern void libtelnet_init(struct libtelnet_t *telnet, extern void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh,
libtelnet_event_handler_t eh, enum libtelnet_mode_t mode, libtelnet_mode_t mode, void *user_data);
void *user_data);
/* free up any memory allocated by a state tracker */ /* 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 */ /* push a byte buffer into the state tracker */
extern void libtelnet_push(struct libtelnet_t *telnet, extern void libtelnet_push(libtelnet_t *telnet, unsigned char *buffer,
unsigned char *buffer, unsigned int size); unsigned int size);
/* send an iac command */ /* send an iac command */
extern void libtelnet_send_command(struct libtelnet_t *telnet, extern void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd);
unsigned char cmd);
/* send negotiation */ /* send negotiation */
extern void libtelnet_send_negotiate(struct libtelnet_t *telnet, extern void libtelnet_send_negotiate(libtelnet_t *telnet, unsigned char cmd,
unsigned char cmd, unsigned char opt); unsigned char opt);
/* send non-command data (escapes IAC bytes) */ /* send non-command data (escapes IAC bytes) */
extern void libtelnet_send_data(struct libtelnet_t *telnet, extern void libtelnet_send_data(libtelnet_t *telnet, unsigned char *buffer,
unsigned char *buffer, unsigned int size); unsigned int size);
/* send sub-request */ /* 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); unsigned char opt, unsigned char *buffer, unsigned int size);
/* begin sending compressed data (server only) */ /* 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) */ #endif /* !defined(LIBTELNET_INCLUDE) */

View File

@ -29,7 +29,7 @@
#include "libtelnet.h" #include "libtelnet.h"
static struct termios orig_tios; static struct termios orig_tios;
static struct libtelnet_t telnet; static libtelnet_t telnet;
static int do_echo; static int do_echo;
static void _cleanup(void) { 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, static void _event_handler(libtelnet_t *telnet, libtelnet_event_t *ev,
struct libtelnet_event_t *ev, void *user_data) { void *user_data) {
int sock = *(int*)user_data; int sock = *(int*)user_data;
switch (ev->type) { switch (ev->type) {

View File

@ -44,7 +44,7 @@
struct conn_t { struct conn_t {
const char *name; const char *name;
int sock; int sock;
struct libtelnet_t telnet; libtelnet_t telnet;
struct conn_t *remote; 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, static void _event_handler(libtelnet_t *telnet, libtelnet_event_t *ev,
struct libtelnet_event_t *ev, void *user_data) { void *user_data) {
struct conn_t *conn = (struct conn_t*)user_data; struct conn_t *conn = (struct conn_t*)user_data;
switch (ev->type) { switch (ev->type) {