replace mode with flags (only a PROXY flag so far)

This commit is contained in:
Sean Middleditch 2009-03-15 23:29:46 -04:00
parent 812358d92a
commit 08bb05f96e
5 changed files with 17 additions and 32 deletions

13
README
View File

@ -58,7 +58,7 @@ IIa. Initialization
API calls. API calls.
void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t handler, void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t handler,
libtelnet_mode_t mode, void *user_data); unsigned char flags, 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
@ -73,13 +73,10 @@ IIa. Initialization
information about the connection, including a socket descriptor information about the connection, including a socket descriptor
for implementing LIBTELNET_EV_SEND event handling. for implementing LIBTELNET_EV_SEND event handling.
The mode parameter must be one of LIBTELNET_MODE_SERVER, The flags parameter can be any of the following flag constants
LIBTELNET_MODE_CLIENT, or LIBTELNET_MODE_PROXY. These slightly bit-or'd together, or 0 to leave all options disabled.
alter the behavior of libtelnet in certain instances. If you are
implementing a TELNET server, use the SERVER mode. If you are LIBTELNET_FLAG_PROXY - operate in proxy mode
implementing a client, use the CLIENT mode. The PROXY mode
enables special behavior for telnet-proxy (or similar
applications).
boid libtelnet_free(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

View File

@ -103,11 +103,11 @@ z_stream *_init_zlib(libtelnet_t *telnet, int deflate, int err_fatal) {
/* initialize a telnet state tracker */ /* initialize a telnet state tracker */
void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh, void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh,
libtelnet_mode_t mode, void *user_data) { unsigned char flags, void *user_data) {
memset(telnet, 0, sizeof(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->flags = flags;
} }
/* free up any memory allocated by a state tracker */ /* free up any memory allocated by a state tracker */
@ -288,8 +288,7 @@ static void _process(libtelnet_t *telnet, unsigned char *buffer,
*/ */
if (telnet->sb_telopt == LIBTELNET_TELOPT_COMPRESS2 && if (telnet->sb_telopt == LIBTELNET_TELOPT_COMPRESS2 &&
telnet->z_inflate == 0 && telnet->z_inflate == 0 &&
(telnet->mode == LIBTELNET_MODE_CLIENT || telnet->flags & LIBTELNET_FLAG_PROXY) {
telnet->mode == LIBTELNET_MODE_PROXY)) {
if ((telnet->z_inflate = _init_zlib(telnet, 0, 1)) == 0) if ((telnet->z_inflate = _init_zlib(telnet, 0, 1)) == 0)
break; break;
@ -476,7 +475,7 @@ void libtelnet_send_subnegotiation(libtelnet_t *telnet, unsigned char opt,
/* if we're a proxy and we just sent the COMPRESS2 marker, we must /* if we're a proxy and we just sent the COMPRESS2 marker, we must
* make sure all further data is compressed if not already. * make sure all further data is compressed if not already.
*/ */
if (telnet->mode == LIBTELNET_MODE_PROXY && if (telnet->flags & LIBTELNET_FLAG_PROXY &&
telnet->z_deflate == 0 && telnet->z_deflate == 0 &&
opt == LIBTELNET_TELOPT_COMPRESS2) { opt == LIBTELNET_TELOPT_COMPRESS2) {
@ -500,13 +499,6 @@ void libtelnet_begin_compress2(libtelnet_t *telnet) {
return; return;
} }
/* only supported by servers */
if (telnet->mode != LIBTELNET_MODE_SERVER) {
_error(telnet, __LINE__, __func__, LIBTELNET_EBADVAL, 0,
"only supported in SERVER mode");
return;
}
/* attempt to create output stream first, bail if we can't */ /* attempt to create output stream first, bail if we can't */
if ((zlib = _init_zlib(telnet, 1, 0)) == 0) if ((zlib = _init_zlib(telnet, 1, 0)) == 0)
return; return;

View File

@ -89,12 +89,8 @@ typedef enum libtelnet_event_type_t libtelnet_event_type_t;
#define LIBTELNET_TELOPT_ZMP 93 #define LIBTELNET_TELOPT_ZMP 93
#define LIBTELNET_TELOPT_EXOPL 255 #define LIBTELNET_TELOPT_EXOPL 255
/* libtelnet modes */ /* libtelnet feature flags */
enum libtelnet_mode_t { #define LIBTELNET_FLAG_PROXY (1<<0)
LIBTELNET_MODE_SERVER = 0,
LIBTELNET_MODE_CLIENT,
LIBTELNET_MODE_PROXY
};
/* telnet states */ /* telnet states */
enum libtelnet_state_t { enum libtelnet_state_t {
@ -167,15 +163,15 @@ struct libtelnet_t {
unsigned int length; unsigned int length;
/* current state */ /* current state */
enum libtelnet_state_t state; enum libtelnet_state_t state;
/* processing mode */ /* option flags */
enum libtelnet_mode_t mode; unsigned char flags;
/* current subnegotiation telopt */ /* current subnegotiation telopt */
unsigned char sb_telopt; unsigned char sb_telopt;
}; };
/* initialize a telnet state tracker */ /* initialize a telnet state tracker */
extern void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh, extern void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh,
libtelnet_mode_t mode, void *user_data); unsigned char flags, 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(libtelnet_t *telnet); extern void libtelnet_free(libtelnet_t *telnet);

View File

@ -225,7 +225,7 @@ int main(int argc, char **argv) {
do_echo = 1; do_echo = 1;
/* initialize telnet box */ /* initialize telnet box */
libtelnet_init(&telnet, _event_handler, LIBTELNET_MODE_CLIENT, &sock); libtelnet_init(&telnet, _event_handler, 0, &sock);
/* initialize poll descriptors */ /* initialize poll descriptors */
memset(pfd, 0, sizeof(pfd)); memset(pfd, 0, sizeof(pfd));

View File

@ -336,9 +336,9 @@ int main(int argc, char **argv) {
client.remote = &server; client.remote = &server;
/* initialize telnet boxes */ /* initialize telnet boxes */
libtelnet_init(&server.telnet, _event_handler, LIBTELNET_MODE_PROXY, libtelnet_init(&server.telnet, _event_handler, LIBTELNET_FLAG_PROXY,
&server); &server);
libtelnet_init(&client.telnet, _event_handler, LIBTELNET_MODE_PROXY, libtelnet_init(&client.telnet, _event_handler, LIBTELNET_FLAG_PROXY,
&client); &client);
/* initialize poll descriptors */ /* initialize poll descriptors */