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 | /* telnet special values */ |
| 13 | #define LIBTELNET_IAC 255 |
| 14 | #define LIBTELNET_DONT 254 |
| 15 | #define LIBTELNET_DO 253 |
| 16 | #define LIBTELNET_WONT 252 |
| 17 | #define LIBTELNET_WILL 251 |
| 18 | #define LIBTELNET_SB 250 |
| 19 | #define LIBTELNET_SE 240 |
| 20 | |
| 21 | /* telnet options */ |
| 22 | #define LIBTELNET_OPTION_BINARY 0 |
| 23 | #define LIBTELNET_OPTION_ECHO 1 |
| 24 | #define LIBTELNET_OPTION_NAWS 31 |
| 25 | #define LIBTELNET_OPTION_ZMP 93 |
| 26 | |
| 27 | /* telnet states */ |
| 28 | enum libtelnet_state_t { |
| 29 | LIBTELNET_STATE_TEXT = 0, |
| 30 | LIBTELNET_STATE_IAC, |
| 31 | LIBTELNET_STATE_DO, |
| 32 | LIBTELNET_STATE_DONT, |
| 33 | LIBTELNET_STATE_WILL, |
| 34 | LIBTELNET_STATE_WONT, |
| 35 | LIBTELNET_STATE_SB, |
| 36 | LIBTELNET_STATE_SB_IAC, |
| 37 | }; |
| 38 | |
| 39 | /* error codes */ |
| 40 | enum libtelnet_error_t { |
| 41 | LIBTELNET_ERROR_OK = 0, |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 42 | LIBTELNET_ERROR_NOMEM, /* memory allocation failure */ |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 43 | LIBTELNET_ERROR_OVERFLOW, /* input exceeds buffer size */ |
| 44 | LIBTELNET_ERROR_PROTOCOL, /* invalid sequence of special bytes */ |
| 45 | LIBTELNET_ERROR_UNKNOWN, /* some crazy unexplainable unknown error */ |
| 46 | }; |
| 47 | |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame^] | 48 | /* state tracker */ |
| 49 | struct libtelnet_t { |
| 50 | /* sub-request buffer */ |
| 51 | unsigned char *buffer; |
| 52 | /* current size of the buffer */ |
| 53 | unsigned int size; |
| 54 | /* length of data in the buffer */ |
| 55 | unsigned int length; |
| 56 | /* current state */ |
| 57 | enum libtelnet_state_t state; |
| 58 | }; |
| 59 | |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 60 | /* libtelnet callback declarations |
| 61 | * APPLICATION MUST IMPLEMENT THESE FUNCTIONS!! |
| 62 | */ |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame^] | 63 | extern void libtelnet_input_cb(struct libtelnet_t *telnet, |
| 64 | unsigned char *buffer, unsigned int size, void *user_data); |
| 65 | extern void libtelnet_output_cb(struct libtelnet_t *telnet, |
| 66 | unsigned char *buffer, unsigned int size, void *user_data); |
| 67 | extern void libtelnet_command_cb(struct libtelnet_t *telnet, |
| 68 | unsigned char cmd, void *user_data); |
| 69 | extern void libtelnet_negotiate_cb(struct libtelnet_t *telnet, |
| 70 | unsigned char cmd, unsigned char opt, void *user_data); |
| 71 | extern void libtelnet_subrequest_cb(struct libtelnet_t *telnet, |
| 72 | unsigned char type, unsigned char *data, unsigned int size, |
| 73 | void *user_data); |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 74 | extern void libtelnet_error_cb(struct libtelnet_t *telnet, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame^] | 75 | enum libtelnet_error_t error, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 76 | |
| 77 | /* initialize a telnet state tracker */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 78 | extern void libtelnet_init(struct libtelnet_t *telnet); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 79 | |
| 80 | /* free up any memory allocated by a state tracker */ |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 81 | extern void libtelnet_free(struct libtelnet_t *telnet); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 82 | |
| 83 | /* push a single byte into the state tracker */ |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame^] | 84 | extern void libtelnet_push_byte(struct libtelnet_t *telnet, |
| 85 | unsigned char byte, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 86 | |
| 87 | /* push a byte buffer into the state tracker */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 88 | extern void libtelnet_push_buffer(struct libtelnet_t *telnet, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame^] | 89 | unsigned char *buffer, unsigned int size, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 90 | |
| 91 | /* send an iac command */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 92 | extern void libtelnet_send_command(struct libtelnet_t *telnet, |
| 93 | unsigned char cmd, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 94 | |
| 95 | /* send negotiation */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 96 | extern void libtelnet_send_negotiate(struct libtelnet_t *telnet, |
| 97 | unsigned char cmd, unsigned char opt, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 98 | |
| 99 | /* send non-command data (escapes IAC bytes) */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 100 | extern void libtelnet_send_data(struct libtelnet_t *telnet, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame^] | 101 | unsigned char *buffer, unsigned int size, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 102 | |
| 103 | /* send sub-request */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 104 | extern void libtelnet_send_subrequest(struct libtelnet_t *telnet, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame^] | 105 | unsigned char type, unsigned char *buffer, unsigned int size, |
| 106 | void *user_data); |
Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 107 | |
| 108 | #endif /* !defined(LIBTELNET_INCLUDE) */ |