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 | |
| 9 | /* sub request buffer size increment (defualt 4K) */ |
| 10 | #define LIBTELNET_BUFFER_SIZE (4 * 1024) |
| 11 | /* sub request buffer size (default 16K) */ |
| 12 | #define LIBTELNET_BUFFER_SIZE_MAX (16 * 1024) |
| 13 | |
| 14 | /* telnet special values */ |
| 15 | #define LIBTELNET_IAC 255 |
| 16 | #define LIBTELNET_DONT 254 |
| 17 | #define LIBTELNET_DO 253 |
| 18 | #define LIBTELNET_WONT 252 |
| 19 | #define LIBTELNET_WILL 251 |
| 20 | #define LIBTELNET_SB 250 |
| 21 | #define LIBTELNET_SE 240 |
| 22 | |
| 23 | /* telnet options */ |
| 24 | #define LIBTELNET_OPTION_BINARY 0 |
| 25 | #define LIBTELNET_OPTION_ECHO 1 |
| 26 | #define LIBTELNET_OPTION_NAWS 31 |
| 27 | #define LIBTELNET_OPTION_ZMP 93 |
| 28 | |
| 29 | /* telnet states */ |
| 30 | enum libtelnet_state_t { |
| 31 | LIBTELNET_STATE_TEXT = 0, |
| 32 | LIBTELNET_STATE_IAC, |
| 33 | LIBTELNET_STATE_DO, |
| 34 | LIBTELNET_STATE_DONT, |
| 35 | LIBTELNET_STATE_WILL, |
| 36 | LIBTELNET_STATE_WONT, |
| 37 | LIBTELNET_STATE_SB, |
| 38 | LIBTELNET_STATE_SB_IAC, |
| 39 | }; |
| 40 | |
| 41 | /* error codes */ |
| 42 | enum libtelnet_error_t { |
| 43 | LIBTELNET_ERROR_OK = 0, |
| 44 | LIBTELNET_ERROR_OVERFLOW, /* input exceeds buffer size */ |
| 45 | LIBTELNET_ERROR_PROTOCOL, /* invalid sequence of special bytes */ |
| 46 | LIBTELNET_ERROR_UNKNOWN, /* some crazy unexplainable unknown error */ |
| 47 | }; |
| 48 | |
| 49 | /* callback prototypes */ |
| 50 | typedef (void)(*libtelnet_input)(struct libtelnet_t *telnet, unsigned char |
| 51 | byte, void *user_data); |
| 52 | typedef (void)(*libtelnet_output)(struct libtelnet_t *telnet, unsigned char |
| 53 | byte, void *user_data); |
| 54 | typedef (void)(*libtelnet_command)(struct libtelnet_t *telnet, unsigned char |
| 55 | cmd, void *user_data); |
| 56 | typedef (void)(*libtelnet_negotiate)(struct libtelnet_t *telnet, unsigned char |
| 57 | cmd, unsigned char opt, void *user_data); |
| 58 | typedef (void)(*libtelnet_subrequest)(struct libtelnet_t *telnet, unsigned char |
| 59 | cmd, unsigned char type, unsigned char *data, size_t size, |
| 60 | void *user_data); |
| 61 | typedef (void)(*libtelnet_error)(struct libtelnet_t *telnet, |
| 62 | enum libtelnet_error_t error, void *user_data); |
| 63 | |
| 64 | /* state tracker */ |
| 65 | struct libtelnet_t { |
| 66 | /* current state */ |
| 67 | enum libtelnet_state_t state; |
| 68 | /* sub-request buffer */ |
| 69 | char *buffer; |
| 70 | /* current size of the buffer */ |
| 71 | size_t size; |
| 72 | /* length of data in the buffer */ |
| 73 | size_t length; |
| 74 | |
| 75 | /* callbacks */ |
| 76 | libtelnet_input input_cb; |
| 77 | libtelnet_output output_cb; |
| 78 | libtelnet_command command_cb; |
| 79 | libtelnet_negotiate negotiate_cb; |
| 80 | libtelnet_subrequest subrequest_cb; |
| 81 | }; |
| 82 | |
| 83 | /* initialize a telnet state tracker */ |
| 84 | void libtelnet_init(struct libtelnet_t *telnet); |
| 85 | |
| 86 | /* free up any memory allocated by a state tracker */ |
| 87 | void libtelnet_close(struct libtelnet_t *telnet); |
| 88 | |
| 89 | /* push a single byte into the state tracker */ |
| 90 | void libtelnet_push_byte(struct libtelnet_t *telnet, unsigned char byte, |
| 91 | void *user_data); |
| 92 | |
| 93 | /* push a byte buffer into the state tracker */ |
| 94 | void libtelnet_push_buffer(struct libtelnet_t *telnet, unsigned char *buffer, |
| 95 | size_t size, void *user_data); |
| 96 | |
| 97 | /* send an iac command */ |
| 98 | void libtelnet_send_command(struct libtelnet_t *telnet, unsigned char cmd, |
| 99 | void *user_data); |
| 100 | |
| 101 | /* send negotiation */ |
| 102 | void libtelnet_send_negotiate(struct libtelnet_t *telnet, unsigned char cmd, |
| 103 | unsigned char opt, void *user_data); |
| 104 | |
| 105 | /* send non-command data (escapes IAC bytes) */ |
| 106 | void libtelnet_send_data(struct libtelnet_t *telnet, unsigned char *buffer, |
| 107 | size_t size, void *user_data); |
| 108 | |
| 109 | /* send sub-request */ |
| 110 | void libtelnet_send_subrequest(struct libtelnet_t *telnet, unsigned char type, |
| 111 | unsigned char *buffer, size_t size, void *user_data); |