Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 1 | /* |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame^] | 2 | * Sean Middleditch |
| 3 | * sean@sourcemud.org |
| 4 | * |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 5 | * 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 Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 12 | #if !defined(LIBTELNET_INCLUDE) |
| 13 | #define LIBTELNET 1 |
| 14 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 15 | /* 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 Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame^] | 28 | #define LIBTELNET_OPTION_COMPRESS2 86 |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 29 | #define LIBTELNET_OPTION_ZMP 93 |
| 30 | |
| 31 | /* telnet states */ |
| 32 | enum libtelnet_state_t { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame^] | 33 | LIBTELNET_STATE_DATA = 0, |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 34 | LIBTELNET_STATE_IAC, |
| 35 | LIBTELNET_STATE_DO, |
| 36 | LIBTELNET_STATE_DONT, |
| 37 | LIBTELNET_STATE_WILL, |
| 38 | LIBTELNET_STATE_WONT, |
| 39 | LIBTELNET_STATE_SB, |
| 40 | LIBTELNET_STATE_SB_IAC, |
| 41 | }; |
| 42 | |
| 43 | /* error codes */ |
| 44 | enum libtelnet_error_t { |
| 45 | LIBTELNET_ERROR_OK = 0, |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 46 | LIBTELNET_ERROR_NOMEM, /* memory allocation failure */ |
Sean Middleditch | 78f400f | 2009-03-14 01:26:43 -0400 | [diff] [blame] | 47 | LIBTELNET_ERROR_OVERFLOW, /* data exceeds buffer size */ |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 48 | LIBTELNET_ERROR_PROTOCOL, /* invalid sequence of special bytes */ |
| 49 | LIBTELNET_ERROR_UNKNOWN, /* some crazy unexplainable unknown error */ |
| 50 | }; |
| 51 | |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 52 | /* state tracker */ |
| 53 | struct libtelnet_t { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame^] | 54 | /* zlib (mccp2) compression */ |
| 55 | #ifdef HAVE_ZLIB |
| 56 | z_stream *zlib; |
| 57 | #endif |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 58 | /* sub-request buffer */ |
| 59 | unsigned char *buffer; |
| 60 | /* current size of the buffer */ |
| 61 | unsigned int size; |
| 62 | /* length of data in the buffer */ |
| 63 | unsigned int length; |
| 64 | /* current state */ |
| 65 | enum libtelnet_state_t state; |
| 66 | }; |
| 67 | |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 68 | /* libtelnet callback declarations |
| 69 | * APPLICATION MUST IMPLEMENT THESE FUNCTIONS!! |
| 70 | */ |
Sean Middleditch | 78f400f | 2009-03-14 01:26:43 -0400 | [diff] [blame] | 71 | extern void libtelnet_data_cb(struct libtelnet_t *telnet, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 72 | unsigned char *buffer, unsigned int size, void *user_data); |
Sean Middleditch | 78f400f | 2009-03-14 01:26:43 -0400 | [diff] [blame] | 73 | extern void libtelnet_send_cb(struct libtelnet_t *telnet, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 74 | unsigned char *buffer, unsigned int size, void *user_data); |
| 75 | extern void libtelnet_command_cb(struct libtelnet_t *telnet, |
| 76 | unsigned char cmd, void *user_data); |
| 77 | extern void libtelnet_negotiate_cb(struct libtelnet_t *telnet, |
| 78 | unsigned char cmd, unsigned char opt, void *user_data); |
| 79 | extern void libtelnet_subrequest_cb(struct libtelnet_t *telnet, |
| 80 | unsigned char type, unsigned char *data, unsigned int size, |
| 81 | void *user_data); |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 82 | extern void libtelnet_error_cb(struct libtelnet_t *telnet, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 83 | enum libtelnet_error_t error, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 84 | |
| 85 | /* initialize a telnet state tracker */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 86 | extern void libtelnet_init(struct libtelnet_t *telnet); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 87 | |
| 88 | /* free up any memory allocated by a state tracker */ |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 89 | extern void libtelnet_free(struct libtelnet_t *telnet); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 90 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 91 | /* push a byte buffer into the state tracker */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 92 | extern void libtelnet_push(struct libtelnet_t *telnet, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 93 | unsigned char *buffer, unsigned int size, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 94 | |
| 95 | /* send an iac command */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 96 | extern void libtelnet_send_command(struct libtelnet_t *telnet, |
| 97 | unsigned char cmd, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 98 | |
| 99 | /* send negotiation */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 100 | extern void libtelnet_send_negotiate(struct libtelnet_t *telnet, |
| 101 | unsigned char cmd, unsigned char opt, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 102 | |
| 103 | /* send non-command data (escapes IAC bytes) */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 104 | extern void libtelnet_send_data(struct libtelnet_t *telnet, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 105 | unsigned char *buffer, unsigned int size, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 106 | |
| 107 | /* send sub-request */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 108 | extern void libtelnet_send_subrequest(struct libtelnet_t *telnet, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 109 | unsigned char type, unsigned char *buffer, unsigned int size, |
| 110 | void *user_data); |
Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 111 | |
| 112 | #endif /* !defined(LIBTELNET_INCLUDE) */ |