2009-03-13 03:14:47 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* sub request buffer size increment (defualt 4K) */
|
|
|
|
#define LIBTELNET_BUFFER_SIZE (4 * 1024)
|
|
|
|
/* sub request buffer size (default 16K) */
|
|
|
|
#define LIBTELNET_BUFFER_SIZE_MAX (16 * 1024)
|
|
|
|
|
|
|
|
/* telnet special values */
|
|
|
|
#define LIBTELNET_IAC 255
|
|
|
|
#define LIBTELNET_DONT 254
|
|
|
|
#define LIBTELNET_DO 253
|
|
|
|
#define LIBTELNET_WONT 252
|
|
|
|
#define LIBTELNET_WILL 251
|
|
|
|
#define LIBTELNET_SB 250
|
|
|
|
#define LIBTELNET_SE 240
|
|
|
|
|
|
|
|
/* telnet options */
|
|
|
|
#define LIBTELNET_OPTION_BINARY 0
|
|
|
|
#define LIBTELNET_OPTION_ECHO 1
|
|
|
|
#define LIBTELNET_OPTION_NAWS 31
|
|
|
|
#define LIBTELNET_OPTION_ZMP 93
|
|
|
|
|
|
|
|
/* telnet states */
|
|
|
|
enum libtelnet_state_t {
|
|
|
|
LIBTELNET_STATE_TEXT = 0,
|
|
|
|
LIBTELNET_STATE_IAC,
|
|
|
|
LIBTELNET_STATE_DO,
|
|
|
|
LIBTELNET_STATE_DONT,
|
|
|
|
LIBTELNET_STATE_WILL,
|
|
|
|
LIBTELNET_STATE_WONT,
|
|
|
|
LIBTELNET_STATE_SB,
|
|
|
|
LIBTELNET_STATE_SB_IAC,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* error codes */
|
|
|
|
enum libtelnet_error_t {
|
|
|
|
LIBTELNET_ERROR_OK = 0,
|
2009-03-13 03:26:34 +00:00
|
|
|
LIBTELNET_ERROR_NOMEM, /* memory allocation failure */
|
2009-03-13 03:14:47 +00:00
|
|
|
LIBTELNET_ERROR_OVERFLOW, /* input exceeds buffer size */
|
|
|
|
LIBTELNET_ERROR_PROTOCOL, /* invalid sequence of special bytes */
|
|
|
|
LIBTELNET_ERROR_UNKNOWN, /* some crazy unexplainable unknown error */
|
|
|
|
};
|
|
|
|
|
2009-03-13 03:26:34 +00:00
|
|
|
/* libtelnet callback declarations
|
|
|
|
* APPLICATION MUST IMPLEMENT THESE FUNCTIONS!!
|
|
|
|
*/
|
|
|
|
extern void libtelnet_input_cb(struct libtelnet_t *telnet, unsigned char
|
2009-03-13 03:14:47 +00:00
|
|
|
byte, void *user_data);
|
2009-03-13 03:26:34 +00:00
|
|
|
extern void libtelnet_output_cb(struct libtelnet_t *telnet, unsigned char
|
2009-03-13 03:14:47 +00:00
|
|
|
byte, void *user_data);
|
2009-03-13 03:26:34 +00:00
|
|
|
extern void libtelnet_command_cb(struct libtelnet_t *telnet, unsigned char
|
2009-03-13 03:14:47 +00:00
|
|
|
cmd, void *user_data);
|
2009-03-13 03:26:34 +00:00
|
|
|
extern void libtelnet_negotiate_cb(struct libtelnet_t *telnet, unsigned char
|
2009-03-13 03:14:47 +00:00
|
|
|
cmd, unsigned char opt, void *user_data);
|
2009-03-13 03:26:34 +00:00
|
|
|
extern void libtelnet_subrequest_cb(struct libtelnet_t *telnet, unsigned char
|
2009-03-13 03:14:47 +00:00
|
|
|
cmd, unsigned char type, unsigned char *data, size_t size,
|
|
|
|
void *user_data);
|
2009-03-13 03:26:34 +00:00
|
|
|
extern void libtelnet_error_cb(struct libtelnet_t *telnet,
|
2009-03-13 03:14:47 +00:00
|
|
|
enum libtelnet_error_t error, void *user_data);
|
|
|
|
|
|
|
|
/* state tracker */
|
|
|
|
struct libtelnet_t {
|
|
|
|
/* current state */
|
|
|
|
enum libtelnet_state_t state;
|
|
|
|
/* sub-request buffer */
|
|
|
|
char *buffer;
|
|
|
|
/* current size of the buffer */
|
|
|
|
size_t size;
|
|
|
|
/* length of data in the buffer */
|
|
|
|
size_t length;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* initialize a telnet state tracker */
|
2009-03-13 03:26:34 +00:00
|
|
|
extern void libtelnet_init(struct libtelnet_t *telnet);
|
2009-03-13 03:14:47 +00:00
|
|
|
|
|
|
|
/* free up any memory allocated by a state tracker */
|
2009-03-13 03:26:34 +00:00
|
|
|
extern void libtelnet_close(struct libtelnet_t *telnet);
|
2009-03-13 03:14:47 +00:00
|
|
|
|
|
|
|
/* push a single byte into the state tracker */
|
2009-03-13 03:26:34 +00:00
|
|
|
extern void libtelnet_push_byte(struct libtelnet_t *telnet, unsigned char byte,
|
2009-03-13 03:14:47 +00:00
|
|
|
void *user_data);
|
|
|
|
|
|
|
|
/* push a byte buffer into the state tracker */
|
2009-03-13 03:26:34 +00:00
|
|
|
extern void libtelnet_push_buffer(struct libtelnet_t *telnet,
|
|
|
|
unsigned char *buffer, size_t size, void *user_data);
|
2009-03-13 03:14:47 +00:00
|
|
|
|
|
|
|
/* send an iac command */
|
2009-03-13 03:26:34 +00:00
|
|
|
extern void libtelnet_send_command(struct libtelnet_t *telnet,
|
|
|
|
unsigned char cmd, void *user_data);
|
2009-03-13 03:14:47 +00:00
|
|
|
|
|
|
|
/* send negotiation */
|
2009-03-13 03:26:34 +00:00
|
|
|
extern void libtelnet_send_negotiate(struct libtelnet_t *telnet,
|
|
|
|
unsigned char cmd, unsigned char opt, void *user_data);
|
2009-03-13 03:14:47 +00:00
|
|
|
|
|
|
|
/* send non-command data (escapes IAC bytes) */
|
2009-03-13 03:26:34 +00:00
|
|
|
extern void libtelnet_send_data(struct libtelnet_t *telnet,
|
|
|
|
unsigned char *buffer, size_t size, void *user_data);
|
2009-03-13 03:14:47 +00:00
|
|
|
|
|
|
|
/* send sub-request */
|
2009-03-13 03:26:34 +00:00
|
|
|
extern void libtelnet_send_subrequest(struct libtelnet_t *telnet,
|
|
|
|
unsigned char type, unsigned char *buffer, size_t size, void *user_data);
|