blob: 575d7bac5a7ad5e23868b5509e45236a57529932 [file] [log] [blame]
Sean Middleditch29144852009-03-12 23:14:47 -04001/*
Sean Middleditch9de15982009-03-14 03:35:49 -04002 * Sean Middleditch
3 * sean@sourcemud.org
4 *
Sean Middleditch29144852009-03-12 23:14:47 -04005 * The author or authors of this code dedicate any and all copyright interest
6 * in this code to the public domain. We make this dedication for the benefit
7 * of the public at large and to the detriment of our heirs and successors. We
8 * intend this dedication to be an overt act of relinquishment in perpetuity of
9 * all present and future rights to this code under copyright law.
10 */
11
Sean Middleditch6aef0732009-03-12 23:27:35 -040012#if !defined(LIBTELNET_INCLUDE)
13#define LIBTELNET 1
14
Sean Middleditch29144852009-03-12 23:14:47 -040015/* telnet special values */
16#define LIBTELNET_IAC 255
17#define LIBTELNET_DONT 254
18#define LIBTELNET_DO 253
19#define LIBTELNET_WONT 252
20#define LIBTELNET_WILL 251
21#define LIBTELNET_SB 250
22#define LIBTELNET_SE 240
23
24/* telnet options */
25#define LIBTELNET_OPTION_BINARY 0
26#define LIBTELNET_OPTION_ECHO 1
27#define LIBTELNET_OPTION_NAWS 31
Sean Middleditch9de15982009-03-14 03:35:49 -040028#define LIBTELNET_OPTION_COMPRESS2 86
Sean Middleditch29144852009-03-12 23:14:47 -040029#define LIBTELNET_OPTION_ZMP 93
30
Sean Middleditch61f8eb62009-03-14 04:57:27 -040031/* libtelnet modes */
32enum libtelnet_mode_t {
33 LIBTELNET_MODE_SERVER = 0,
34 LIBTELNET_MODE_CLIENT
35};
36
Sean Middleditch29144852009-03-12 23:14:47 -040037/* telnet states */
38enum libtelnet_state_t {
Sean Middleditch9de15982009-03-14 03:35:49 -040039 LIBTELNET_STATE_DATA = 0,
Sean Middleditch29144852009-03-12 23:14:47 -040040 LIBTELNET_STATE_IAC,
41 LIBTELNET_STATE_DO,
42 LIBTELNET_STATE_DONT,
43 LIBTELNET_STATE_WILL,
44 LIBTELNET_STATE_WONT,
45 LIBTELNET_STATE_SB,
Sean Middleditch61f8eb62009-03-14 04:57:27 -040046 LIBTELNET_STATE_SB_IAC
Sean Middleditch29144852009-03-12 23:14:47 -040047};
48
49/* error codes */
50enum libtelnet_error_t {
51 LIBTELNET_ERROR_OK = 0,
Sean Middleditchb1e452e2009-03-12 23:26:34 -040052 LIBTELNET_ERROR_NOMEM, /* memory allocation failure */
Sean Middleditch78f400f2009-03-14 01:26:43 -040053 LIBTELNET_ERROR_OVERFLOW, /* data exceeds buffer size */
Sean Middleditch29144852009-03-12 23:14:47 -040054 LIBTELNET_ERROR_PROTOCOL, /* invalid sequence of special bytes */
Sean Middleditch61f8eb62009-03-14 04:57:27 -040055 LIBTELNET_ERROR_UNKNOWN /* some crazy unexplainable unknown error */
Sean Middleditch29144852009-03-12 23:14:47 -040056};
57
Sean Middleditch4d9444d2009-03-13 22:48:05 -040058/* state tracker */
59struct libtelnet_t {
Sean Middleditch9de15982009-03-14 03:35:49 -040060 /* zlib (mccp2) compression */
61#ifdef HAVE_ZLIB
62 z_stream *zlib;
63#endif
Sean Middleditch4d9444d2009-03-13 22:48:05 -040064 /* sub-request buffer */
65 unsigned char *buffer;
66 /* current size of the buffer */
67 unsigned int size;
68 /* length of data in the buffer */
69 unsigned int length;
70 /* current state */
71 enum libtelnet_state_t state;
Sean Middleditch61f8eb62009-03-14 04:57:27 -040072 /* processing mode */
73 enum libtelnet_mode_t mode;
Sean Middleditch4d9444d2009-03-13 22:48:05 -040074};
75
Sean Middleditchb1e452e2009-03-12 23:26:34 -040076/* libtelnet callback declarations
77 * APPLICATION MUST IMPLEMENT THESE FUNCTIONS!!
78 */
Sean Middleditch78f400f2009-03-14 01:26:43 -040079extern void libtelnet_data_cb(struct libtelnet_t *telnet,
Sean Middleditch61f8eb62009-03-14 04:57:27 -040080 unsigned char *buffer, unsigned int size, void *user_data);
Sean Middleditch78f400f2009-03-14 01:26:43 -040081extern void libtelnet_send_cb(struct libtelnet_t *telnet,
Sean Middleditch61f8eb62009-03-14 04:57:27 -040082 unsigned char *buffer, unsigned int size, void *user_data);
Sean Middleditch4d9444d2009-03-13 22:48:05 -040083extern void libtelnet_command_cb(struct libtelnet_t *telnet,
Sean Middleditch61f8eb62009-03-14 04:57:27 -040084 unsigned char cmd, void *user_data);
Sean Middleditch4d9444d2009-03-13 22:48:05 -040085extern void libtelnet_negotiate_cb(struct libtelnet_t *telnet,
Sean Middleditch61f8eb62009-03-14 04:57:27 -040086 unsigned char cmd, unsigned char opt, void *user_data);
Sean Middleditch4d9444d2009-03-13 22:48:05 -040087extern void libtelnet_subrequest_cb(struct libtelnet_t *telnet,
Sean Middleditch61f8eb62009-03-14 04:57:27 -040088 unsigned char type, unsigned char *data, unsigned int size,
89 void *user_data);
90#ifdef HAVE_ZLIB
91extern void libtelnet_compress_cb(struct libtelnet_t *telnet,
92 char enabled, void *user_data);
93#endif
Sean Middleditchb1e452e2009-03-12 23:26:34 -040094extern void libtelnet_error_cb(struct libtelnet_t *telnet,
Sean Middleditch61f8eb62009-03-14 04:57:27 -040095 enum libtelnet_error_t error, void *user_data);
Sean Middleditch29144852009-03-12 23:14:47 -040096
97/* initialize a telnet state tracker */
Sean Middleditch61f8eb62009-03-14 04:57:27 -040098extern void libtelnet_init(struct libtelnet_t *telnet,
99 enum libtelnet_mode_t mode);
Sean Middleditch29144852009-03-12 23:14:47 -0400100
101/* free up any memory allocated by a state tracker */
Sean Middleditch51ad6792009-03-13 20:15:59 -0400102extern void libtelnet_free(struct libtelnet_t *telnet);
Sean Middleditch29144852009-03-12 23:14:47 -0400103
Sean Middleditch29144852009-03-12 23:14:47 -0400104/* push a byte buffer into the state tracker */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400105extern void libtelnet_push(struct libtelnet_t *telnet,
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400106 unsigned char *buffer, unsigned int size, void *user_data);
Sean Middleditch29144852009-03-12 23:14:47 -0400107
108/* send an iac command */
Sean Middleditchb1e452e2009-03-12 23:26:34 -0400109extern void libtelnet_send_command(struct libtelnet_t *telnet,
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400110 unsigned char cmd, void *user_data);
Sean Middleditch29144852009-03-12 23:14:47 -0400111
112/* send negotiation */
Sean Middleditchb1e452e2009-03-12 23:26:34 -0400113extern void libtelnet_send_negotiate(struct libtelnet_t *telnet,
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400114 unsigned char cmd, unsigned char opt, void *user_data);
Sean Middleditch29144852009-03-12 23:14:47 -0400115
116/* send non-command data (escapes IAC bytes) */
Sean Middleditchb1e452e2009-03-12 23:26:34 -0400117extern void libtelnet_send_data(struct libtelnet_t *telnet,
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400118 unsigned char *buffer, unsigned int size, void *user_data);
Sean Middleditch29144852009-03-12 23:14:47 -0400119
120/* send sub-request */
Sean Middleditchb1e452e2009-03-12 23:26:34 -0400121extern void libtelnet_send_subrequest(struct libtelnet_t *telnet,
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400122 unsigned char type, unsigned char *buffer, unsigned int size,
123 void *user_data);
Sean Middleditch6aef0732009-03-12 23:27:35 -0400124
125#endif /* !defined(LIBTELNET_INCLUDE) */