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