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) |
Sean Middleditch | aac2c12 | 2009-03-14 18:31:26 -0400 | [diff] [blame] | 13 | #define LIBTELNET_INCLUDE 1 |
Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 14 | |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 15 | /* forward declarations */ |
| 16 | struct libtelnet_t; |
| 17 | struct libtelnet_cb_t; |
| 18 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 19 | /* telnet special values */ |
| 20 | #define LIBTELNET_IAC 255 |
| 21 | #define LIBTELNET_DONT 254 |
| 22 | #define LIBTELNET_DO 253 |
| 23 | #define LIBTELNET_WONT 252 |
| 24 | #define LIBTELNET_WILL 251 |
| 25 | #define LIBTELNET_SB 250 |
Sean Middleditch | c04224b | 2009-03-14 18:30:57 -0400 | [diff] [blame] | 26 | #define LIBTELNET_SB 250 |
| 27 | #define LIBTELNET_GA 249 |
| 28 | #define LIBTELNET_EL 248 |
| 29 | #define LIBTELNET_EC 247 |
| 30 | #define LIBTELNET_AYT 246 |
| 31 | #define LIBTELNET_AO 245 |
| 32 | #define LIBTELNET_IP 244 |
| 33 | #define LIBTELNET_BREAK 243 |
| 34 | #define LIBTELNET_DM 242 |
| 35 | #define LIBTELNET_NOP 241 |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 36 | #define LIBTELNET_SE 240 |
Sean Middleditch | c04224b | 2009-03-14 18:30:57 -0400 | [diff] [blame] | 37 | #define LIBTELNET_EOR 239 |
| 38 | #define LIBTELNET_ABORT 238 |
| 39 | #define LIBTELNET_SUSP 237 |
| 40 | #define LIBTELNET_EOF 236 |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 41 | |
| 42 | /* telnet options */ |
Sean Middleditch | c04224b | 2009-03-14 18:30:57 -0400 | [diff] [blame] | 43 | #define LIBTELNET_TELOPT_BINARY 0 |
| 44 | #define LIBTELNET_TELOPT_ECHO 1 |
| 45 | #define LIBTELNET_TELOPT_RCP 2 |
| 46 | #define LIBTELNET_TELOPT_SGA 3 |
| 47 | #define LIBTELNET_TELOPT_NAMS 4 |
| 48 | #define LIBTELNET_TELOPT_STATUS 5 |
| 49 | #define LIBTELNET_TELOPT_TM 6 |
| 50 | #define LIBTELNET_TELOPT_RCTE 7 |
| 51 | #define LIBTELNET_TELOPT_NAOL 8 |
| 52 | #define LIBTELNET_TELOPT_NAOP 9 |
| 53 | #define LIBTELNET_TELOPT_NAOCRD 10 |
| 54 | #define LIBTELNET_TELOPT_NAOHTS 11 |
| 55 | #define LIBTELNET_TELOPT_NAOHTD 12 |
| 56 | #define LIBTELNET_TELOPT_NAOFFD 13 |
| 57 | #define LIBTELNET_TELOPT_NAOVTS 14 |
| 58 | #define LIBTELNET_TELOPT_NAOVTD 15 |
| 59 | #define LIBTELNET_TELOPT_NAOLFD 16 |
| 60 | #define LIBTELNET_TELOPT_XASCII 17 |
| 61 | #define LIBTELNET_TELOPT_LOGOUT 18 |
| 62 | #define LIBTELNET_TELOPT_BM 19 |
| 63 | #define LIBTELNET_TELOPT_DET 20 |
| 64 | #define LIBTELNET_TELOPT_SUPDUP 21 |
| 65 | #define LIBTELNET_TELOPT_SUPDUPOUTPUT 22 |
| 66 | #define LIBTELNET_TELOPT_SNDLOC 23 |
| 67 | #define LIBTELNET_TELOPT_TTYPE 24 |
| 68 | #define LIBTELNET_TELOPT_EOR 25 |
| 69 | #define LIBTELNET_TELOPT_TUID 26 |
| 70 | #define LIBTELNET_TELOPT_OUTMRK 27 |
| 71 | #define LIBTELNET_TELOPT_TTYLOC 28 |
| 72 | #define LIBTELNET_TELOPT_3270REGIME 29 |
| 73 | #define LIBTELNET_TELOPT_X3PAD 30 |
| 74 | #define LIBTELNET_TELOPT_NAWS 31 |
| 75 | #define LIBTELNET_TELOPT_TSPEED 32 |
| 76 | #define LIBTELNET_TELOPT_LFLOW 33 |
| 77 | #define LIBTELNET_TELOPT_LINEMODE 34 |
| 78 | #define LIBTELNET_TELOPT_XDISPLOC 35 |
| 79 | #define LIBTELNET_TELOPT_ENVIRON 36 |
| 80 | #define LIBTELNET_TELOPT_AUTHENTICATION 37 |
| 81 | #define LIBTELNET_TELOPT_ENCRYPT 38 |
| 82 | #define LIBTELNET_TELOPT_NEW_ENVIRON 39 |
| 83 | #define LIBTELNET_TELOPT_COMPRESS 85 |
| 84 | #define LIBTELNET_TELOPT_COMPRESS2 86 |
| 85 | #define LIBTELNET_TELOPT_ZMP 93 |
| 86 | #define LIBTELNET_TELOPT_EXOPL 255 |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 87 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 88 | /* libtelnet modes */ |
| 89 | enum libtelnet_mode_t { |
| 90 | LIBTELNET_MODE_SERVER = 0, |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 91 | LIBTELNET_MODE_CLIENT, |
| 92 | LIBTELNET_MODE_PROXY |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 93 | }; |
| 94 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 95 | /* telnet states */ |
| 96 | enum libtelnet_state_t { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 97 | LIBTELNET_STATE_DATA = 0, |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 98 | LIBTELNET_STATE_IAC, |
| 99 | LIBTELNET_STATE_DO, |
| 100 | LIBTELNET_STATE_DONT, |
| 101 | LIBTELNET_STATE_WILL, |
| 102 | LIBTELNET_STATE_WONT, |
| 103 | LIBTELNET_STATE_SB, |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame^] | 104 | LIBTELNET_STATE_SB_DATA, |
| 105 | LIBTELNET_STATE_SB_DATA_IAC |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | /* error codes */ |
| 109 | enum libtelnet_error_t { |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 110 | LIBTELNET_EOK = 0, |
| 111 | LIBTELNET_ENOMEM, /* memory allocation failure */ |
| 112 | LIBTELNET_EOVERFLOW, /* data exceeds buffer size */ |
| 113 | LIBTELNET_EPROTOCOL, /* invalid sequence of special bytes */ |
| 114 | LIBTELNET_EUNKNOWN /* some crazy unexplainable unknown error */ |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 115 | }; |
| 116 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 117 | /* event codes */ |
| 118 | enum libtelnet_event_type_t { |
| 119 | LIBTELNET_EV_DATA = 0, |
| 120 | LIBTELNET_EV_SEND, |
| 121 | LIBTELNET_EV_IAC, |
| 122 | LIBTELNET_EV_NEGOTIATE, |
| 123 | LIBTELNET_EV_SUBNEGOTIATION, |
| 124 | LIBTELNET_EV_COMPRESS, |
| 125 | LIBTELNET_EV_ERROR |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 126 | }; |
| 127 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 128 | /* event information */ |
| 129 | struct libtelnet_event_t { |
| 130 | /* type of event */ |
| 131 | enum libtelnet_event_type_t type; |
| 132 | /* command info: only for IAC event */ |
| 133 | unsigned char command; |
| 134 | /* telopt info: for NEGOTIATE and SUBNEGOTIATION events */ |
| 135 | unsigned char telopt; |
| 136 | /* data buffer: for DATA, SEND, SUBNEGOTIATION, and ERROR events */ |
| 137 | unsigned char *buffer; |
| 138 | unsigned int size; |
| 139 | }; |
| 140 | |
| 141 | /* event handler declaration */ |
| 142 | typedef void (*libtelnet_event_handler_t)(struct libtelnet_t *telnet, |
| 143 | struct libtelnet_event_t *event, void *user_data); |
| 144 | |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 145 | /* state tracker */ |
| 146 | struct libtelnet_t { |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 147 | /* event handler */ |
| 148 | libtelnet_event_handler_t eh; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 149 | #ifdef HAVE_ZLIB |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 150 | /* zlib (mccp2) compression */ |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 151 | z_stream *z_deflate; |
| 152 | z_stream *z_inflate; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 153 | #endif |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 154 | /* sub-request buffer */ |
| 155 | unsigned char *buffer; |
| 156 | /* current size of the buffer */ |
| 157 | unsigned int size; |
| 158 | /* length of data in the buffer */ |
| 159 | unsigned int length; |
| 160 | /* current state */ |
| 161 | enum libtelnet_state_t state; |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 162 | /* processing mode */ |
| 163 | enum libtelnet_mode_t mode; |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame^] | 164 | /* current subnegotiation telopt */ |
| 165 | unsigned char sb_telopt; |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 166 | }; |
| 167 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 168 | /* initialize a telnet state tracker */ |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 169 | extern void libtelnet_init(struct libtelnet_t *telnet, |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 170 | libtelnet_event_handler_t eh, enum libtelnet_mode_t mode); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 171 | |
| 172 | /* free up any memory allocated by a state tracker */ |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 173 | extern void libtelnet_free(struct libtelnet_t *telnet); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 174 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 175 | /* push a byte buffer into the state tracker */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 176 | extern void libtelnet_push(struct libtelnet_t *telnet, |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 177 | unsigned char *buffer, unsigned int size, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 178 | |
| 179 | /* send an iac command */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 180 | extern void libtelnet_send_command(struct libtelnet_t *telnet, |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 181 | unsigned char cmd, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 182 | |
| 183 | /* send negotiation */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 184 | extern void libtelnet_send_negotiate(struct libtelnet_t *telnet, |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 185 | unsigned char cmd, unsigned char opt, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 186 | |
| 187 | /* send non-command data (escapes IAC bytes) */ |
Sean Middleditch | b1e452e | 2009-03-12 23:26:34 -0400 | [diff] [blame] | 188 | extern void libtelnet_send_data(struct libtelnet_t *telnet, |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 189 | unsigned char *buffer, unsigned int size, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 190 | |
| 191 | /* send sub-request */ |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 192 | extern void libtelnet_send_subnegotiation(struct libtelnet_t *telnet, |
Sean Middleditch | c974b97 | 2009-03-14 13:23:33 -0400 | [diff] [blame] | 193 | unsigned char opt, unsigned char *buffer, unsigned int size, |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 194 | void *user_data); |
Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 195 | |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 196 | /* begin sending compressed data (server only) */ |
| 197 | extern void libtelnet_begin_compress2(struct libtelnet_t *telnet, |
| 198 | void *user_data); |
| 199 | |
Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 200 | #endif /* !defined(LIBTELNET_INCLUDE) */ |