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