Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 1 | /* |
| 2 | * The author or authors of this code dedicate any and all copyright interest |
| 3 | * in this code to the public domain. We make this dedication for the benefit |
| 4 | * of the public at large and to the detriment of our heirs and successors. We |
| 5 | * intend this dedication to be an overt act of relinquishment in perpetuity of |
| 6 | * all present and future rights to this code under copyright law. |
| 7 | */ |
| 8 | |
Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame^] | 9 | #if !defined(LIBTELNET_INCLUDE) |
| 10 | #define LIBTELNET 1 |
| 11 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 12 | /* sub request buffer size increment (defualt 4K) */ |
| 13 | #define LIBTELNET_BUFFER_SIZE (4 * 1024) |
| 14 | /* sub request buffer size (default 16K) */ |
| 15 | #define LIBTELNET_BUFFER_SIZE_MAX (16 * 1024) |
| 16 | |
| 17 | /* telnet special values */ |
| 18 | #define LIBTELNET_IAC 255 |
| 19 | #define LIBTELNET_DONT 254 |
| 20 | #define LIBTELNET_DO 253 |
| 21 | #define LIBTELNET_WONT 252 |
| 22 | #define LIBTELNET_WILL 251 |
| 23 | #define LIBTELNET_SB 250 |
| 24 | #define LIBTELNET_SE 240 |
| 25 | |
| 26 | /* telnet options */ |
| 27 | #define LIBTELNET_OPTION_BINARY 0 |
| 28 | #define LIBTELNET_OPTION_ECHO 1 |
| 29 | #define LIBTELNET_OPTION_NAWS 31 |
| 30 | #define LIBTELNET_OPTION_ZMP 93 |
| 31 | |
| 32 | /* telnet states */ |
| 33 | enum libtelnet_state_t { |
| 34 | LIBTELNET_STATE_TEXT = 0, |
| 35 | LIBTELNET_STATE_IAC, |
| 36 | LIBTELNET_STATE_DO, |
| 37 | LIBTELNET_STATE_DONT, |
| 38 | LIBTELNET_STATE_WILL, |
| 39 | LIBTELNET_STATE_WONT, |
| 40 | LIBTELNET_STATE_SB, |
| 41 | LIBTELNET_STATE_SB_IAC, |
| 42 | }; |
| 43 | |
| 44 | /* error codes */ |
| 45 | enum libtelnet_error_t { |
| 46 | LIBTELNET_ERROR_OK = 0, |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 47 | LIBTELNET_ERROR_NOMEM, /* memory allocation failure */ |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 48 | LIBTELNET_ERROR_OVERFLOW, /* input exceeds buffer size */ |
| 49 | LIBTELNET_ERROR_PROTOCOL, /* invalid sequence of special bytes */ |
| 50 | LIBTELNET_ERROR_UNKNOWN, /* some crazy unexplainable unknown error */ |
| 51 | }; |
| 52 | |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 53 | /* libtelnet callback declarations |
| 54 | * APPLICATION MUST IMPLEMENT THESE FUNCTIONS!! |
| 55 | */ |
| 56 | extern void libtelnet_input_cb(struct libtelnet_t *telnet, unsigned char |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 57 | byte, void *user_data); |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 58 | extern void libtelnet_output_cb(struct libtelnet_t *telnet, unsigned char |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 59 | byte, void *user_data); |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 60 | extern void libtelnet_command_cb(struct libtelnet_t *telnet, unsigned char |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 61 | cmd, void *user_data); |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 62 | extern void libtelnet_negotiate_cb(struct libtelnet_t *telnet, unsigned char |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 63 | cmd, unsigned char opt, void *user_data); |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 64 | extern void libtelnet_subrequest_cb(struct libtelnet_t *telnet, unsigned char |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 65 | cmd, unsigned char type, unsigned char *data, size_t size, |
| 66 | void *user_data); |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 67 | extern void libtelnet_error_cb(struct libtelnet_t *telnet, |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 68 | enum libtelnet_error_t error, void *user_data); |
| 69 | |
| 70 | /* state tracker */ |
| 71 | struct libtelnet_t { |
| 72 | /* current state */ |
| 73 | enum libtelnet_state_t state; |
| 74 | /* sub-request buffer */ |
| 75 | char *buffer; |
| 76 | /* current size of the buffer */ |
| 77 | size_t size; |
| 78 | /* length of data in the buffer */ |
| 79 | size_t length; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /* initialize a telnet state tracker */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 83 | extern void libtelnet_init(struct libtelnet_t *telnet); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 84 | |
| 85 | /* free up any memory allocated by a state tracker */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 86 | extern void libtelnet_close(struct libtelnet_t *telnet); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 87 | |
| 88 | /* push a single byte into the state tracker */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 89 | extern void libtelnet_push_byte(struct libtelnet_t *telnet, unsigned char byte, |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 90 | void *user_data); |
| 91 | |
| 92 | /* push a byte buffer into the state tracker */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 93 | extern void libtelnet_push_buffer(struct libtelnet_t *telnet, |
| 94 | unsigned char *buffer, size_t size, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 95 | |
| 96 | /* send an iac command */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 97 | extern void libtelnet_send_command(struct libtelnet_t *telnet, |
| 98 | unsigned char cmd, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 99 | |
| 100 | /* send negotiation */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 101 | extern void libtelnet_send_negotiate(struct libtelnet_t *telnet, |
| 102 | unsigned char cmd, unsigned char opt, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 103 | |
| 104 | /* send non-command data (escapes IAC bytes) */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 105 | extern void libtelnet_send_data(struct libtelnet_t *telnet, |
| 106 | unsigned char *buffer, size_t size, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 107 | |
| 108 | /* send sub-request */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 109 | extern void libtelnet_send_subrequest(struct libtelnet_t *telnet, |
| 110 | unsigned char type, unsigned char *buffer, size_t size, void *user_data); |
Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame^] | 111 | |
| 112 | #endif /* !defined(LIBTELNET_INCLUDE) */ |